blob: fd2bb97bb28c6515e3d7acd3d8f44a9427069cce [file] [log] [blame]
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001/*==============================================================================
Laurence Lundbladed92a6162018-11-01 11:38:35 +07002 Copyright (c) 2016-2018, The Linux Foundation.
Laurence Lundbladeee851742020-01-08 08:37:05 -08003 Copyright (c) 2018-2020, Laurence Lundblade.
Laurence Lundbladed92a6162018-11-01 11:38:35 +07004 All rights reserved.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005
Laurence Lundblade0dbc9172018-11-01 14:17:21 +07006Redistribution and use in source and binary forms, with or without
7modification, are permitted provided that the following conditions are
8met:
9 * Redistributions of source code must retain the above copyright
10 notice, this list of conditions and the following disclaimer.
11 * Redistributions in binary form must reproduce the above
12 copyright notice, this list of conditions and the following
13 disclaimer in the documentation and/or other materials provided
14 with the distribution.
15 * Neither the name of The Linux Foundation nor the names of its
16 contributors, nor the name "Laurence Lundblade" may be used to
17 endorse or promote products derived from this software without
18 specific prior written permission.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -080019
Laurence Lundblade0dbc9172018-11-01 14:17:21 +070020THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
21WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
22MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
23ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
24BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
27BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
29OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
30IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Laurence Lundbladeee851742020-01-08 08:37:05 -080031 =============================================================================*/
Laurence Lundblade9e3651c2018-10-10 11:49:55 +080032
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080033#include "qcbor_decode_tests.h"
Laurence Lundblade844bb5c2020-03-01 17:27:25 -080034#include "qcbor/qcbor_encode.h"
35#include "qcbor/qcbor_decode.h"
Laurence Lundbladed4728fd2018-12-17 15:15:56 -080036#include <string.h>
Laurence Lundblade9e3651c2018-10-10 11:49:55 +080037#include <math.h> // for fabs()
Laurence Lundbladebb1062e2019-08-12 23:28:54 -070038#include "not_well_formed_cbor.h"
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -080039
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080040
Laurence Lundbladea2e29072018-12-30 09:20:06 -080041#ifdef PRINT_FUNCTIONS_FOR_DEBUGGING
Laurence Lundblade20db9c92018-12-17 11:40:37 -080042#include <stdio.h>
Laurence Lundbladea2e29072018-12-30 09:20:06 -080043
44static void PrintUsefulBufC(const char *szLabel, UsefulBufC Buf)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080045{
46 if(szLabel) {
47 printf("%s ", szLabel);
48 }
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -080049
Laurence Lundblade570fab52018-10-13 18:28:27 +080050 size_t i;
Laurence Lundbladea2e29072018-12-30 09:20:06 -080051 for(i = 0; i < Buf.len; i++) {
52 uint8_t Z = ((uint8_t *)Buf.ptr)[i];
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080053 printf("%02x ", Z);
54 }
55 printf("\n");
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -080056
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080057 fflush(stdout);
58}
Laurence Lundblade20db9c92018-12-17 11:40:37 -080059#endif
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080060
61
Laurence Lundbladeb836efb2018-10-28 20:09:58 +070062static const uint8_t spExpectedEncodedInts[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080063 0x98, 0x2f, 0x3b, 0x7f, 0xff, 0xff, 0xff, 0xff,
64 0xff, 0xff, 0xff, 0x3b, 0x00, 0x00, 0x00, 0x01,
65 0x00, 0x00, 0x00, 0x00, 0x3a, 0xff, 0xff, 0xff,
66 0xff, 0x3a, 0xff, 0xff, 0xff, 0xfe, 0x3a, 0xff,
67 0xff, 0xff, 0xfd, 0x3a, 0x7f, 0xff, 0xff, 0xff,
68 0x3a, 0x7f, 0xff, 0xff, 0xfe, 0x3a, 0x00, 0x01,
69 0x00, 0x01, 0x3a, 0x00, 0x01, 0x00, 0x00, 0x39,
70 0xff, 0xff, 0x39, 0xff, 0xfe, 0x39, 0xff, 0xfd,
71 0x39, 0x01, 0x00, 0x38, 0xff, 0x38, 0xfe, 0x38,
72 0xfd, 0x38, 0x18, 0x37, 0x36, 0x20, 0x00, 0x00,
73 0x01, 0x16, 0x17, 0x18, 0x18, 0x18, 0x19, 0x18,
74 0x1a, 0x18, 0xfe, 0x18, 0xff, 0x19, 0x01, 0x00,
75 0x19, 0x01, 0x01, 0x19, 0xff, 0xfe, 0x19, 0xff,
76 0xff, 0x1a, 0x00, 0x01, 0x00, 0x00, 0x1a, 0x00,
77 0x01, 0x00, 0x01, 0x1a, 0x00, 0x01, 0x00, 0x02,
78 0x1a, 0x7f, 0xff, 0xff, 0xff, 0x1a, 0x7f, 0xff,
79 0xff, 0xff, 0x1a, 0x80, 0x00, 0x00, 0x00, 0x1a,
80 0x80, 0x00, 0x00, 0x01, 0x1a, 0xff, 0xff, 0xff,
81 0xfe, 0x1a, 0xff, 0xff, 0xff, 0xff, 0x1b, 0x00,
82 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x1b,
83 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01,
84 0x1b, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
85 0xff, 0x1b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
86 0xff, 0xff};
87
88
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080089// return CBOR error or -1 if type of value doesn't match
90
Laurence Lundbladec5fef682020-01-25 11:38:45 -080091static int32_t IntegerValuesParseTestInternal(QCBORDecodeContext *pDCtx)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080092{
Laurence Lundbladee6bcef12020-04-01 10:56:27 -070093 QCBORItem Item;
94 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -080095
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080096 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -070097 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080098 if(Item.uDataType != QCBOR_TYPE_ARRAY)
99 return -1;
100
101 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700102 return (int32_t)nCBORError;
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800103 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade570fab52018-10-13 18:28:27 +0800104 Item.val.int64 != -9223372036854775807LL - 1)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800105 return -1;
106
107 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700108 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800109 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade570fab52018-10-13 18:28:27 +0800110 Item.val.int64 != -4294967297)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800111 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800112
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800113 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700114 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800115 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade570fab52018-10-13 18:28:27 +0800116 Item.val.int64 != -4294967296)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800117 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800118
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800119 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700120 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800121 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade570fab52018-10-13 18:28:27 +0800122 Item.val.int64 != -4294967295)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800123 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800124
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800125 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700126 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800127 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade570fab52018-10-13 18:28:27 +0800128 Item.val.int64 != -4294967294)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800129 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800130
131
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800132 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700133 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800134 if(Item.uDataType != QCBOR_TYPE_INT64 ||
135 Item.val.int64 != -2147483648)
136 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800137
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800138 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700139 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800140 if(Item.uDataType != QCBOR_TYPE_INT64 ||
141 Item.val.int64 != -2147483647)
142 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800143
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800144 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700145 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800146 if(Item.uDataType != QCBOR_TYPE_INT64 ||
147 Item.val.int64 != -65538)
148 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800149
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800150 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700151 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800152 if(Item.uDataType != QCBOR_TYPE_INT64 ||
153 Item.val.int64 != -65537)
154 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800155
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800156 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700157 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800158 if(Item.uDataType != QCBOR_TYPE_INT64 ||
159 Item.val.int64 != -65536)
160 return -1;
161
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800162
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800163 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700164 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800165 if(Item.uDataType != QCBOR_TYPE_INT64 ||
166 Item.val.int64 != -65535)
167 return -1;
168
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800169
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800170 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700171 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800172 if(Item.uDataType != QCBOR_TYPE_INT64 ||
173 Item.val.int64 != -65534)
174 return -1;
175
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800176
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800177 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700178 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800179 if(Item.uDataType != QCBOR_TYPE_INT64 ||
180 Item.val.int64 != -257)
181 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800182
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800183 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700184 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800185 if(Item.uDataType != QCBOR_TYPE_INT64 ||
186 Item.val.int64 != -256)
187 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800188
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800189 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700190 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800191 if(Item.uDataType != QCBOR_TYPE_INT64 ||
192 Item.val.int64 != -255)
193 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800194
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800195 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700196 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800197 if(Item.uDataType != QCBOR_TYPE_INT64 ||
198 Item.val.int64 != -254)
199 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800200
201
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800202 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700203 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800204 if(Item.uDataType != QCBOR_TYPE_INT64 ||
205 Item.val.int64 != -25)
206 return -1;
207
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800208
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800209 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700210 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800211 if(Item.uDataType != QCBOR_TYPE_INT64 ||
212 Item.val.int64 != -24)
213 return -1;
214
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800215
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800216 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700217 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800218 if(Item.uDataType != QCBOR_TYPE_INT64 ||
219 Item.val.int64 != -23)
220 return -1;
221
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800222
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800223 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700224 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800225 if(Item.uDataType != QCBOR_TYPE_INT64 ||
226 Item.val.int64 != -1)
227 return -1;
228
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800229
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800230 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700231 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800232 if(Item.uDataType != QCBOR_TYPE_INT64 ||
233 Item.val.int64 != 0)
234 return -1;
235
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800236
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800237 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700238 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800239 if(Item.uDataType != QCBOR_TYPE_INT64 ||
240 Item.val.int64 != 0)
241 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800242
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800243 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700244 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800245 if(Item.uDataType != QCBOR_TYPE_INT64 ||
246 Item.val.int64 != 1)
247 return -1;
248
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800249
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800250 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700251 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800252 if(Item.uDataType != QCBOR_TYPE_INT64 ||
253 Item.val.int64 != 22)
254 return -1;
255
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800256
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800257 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700258 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800259 if(Item.uDataType != QCBOR_TYPE_INT64 ||
260 Item.val.int64 != 23)
261 return -1;
262
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800263
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800264 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700265 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800266 if(Item.uDataType != QCBOR_TYPE_INT64 ||
267 Item.val.int64 != 24)
268 return -1;
269
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800270
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800271 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700272 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800273 if(Item.uDataType != QCBOR_TYPE_INT64 ||
274 Item.val.int64 != 25)
275 return -1;
276
277 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700278 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800279 if(Item.uDataType != QCBOR_TYPE_INT64 ||
280 Item.val.int64 != 26)
281 return -1;
282
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800283
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800284 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700285 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800286 if(Item.uDataType != QCBOR_TYPE_INT64 ||
287 Item.val.int64 != 254)
288 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800289
290
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800291 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700292 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800293 if(Item.uDataType != QCBOR_TYPE_INT64 ||
294 Item.val.int64 != 255)
295 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800296
297
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800298 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700299 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800300 if(Item.uDataType != QCBOR_TYPE_INT64 ||
301 Item.val.int64 != 256)
302 return -1;
303
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800304
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800305 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700306 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800307 if(Item.uDataType != QCBOR_TYPE_INT64 ||
308 Item.val.int64 != 257)
309 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800310
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800311 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700312 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800313 if(Item.uDataType != QCBOR_TYPE_INT64 ||
314 Item.val.int64 != 65534)
315 return -1;
316
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800317
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800318 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700319 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800320 if(Item.uDataType != QCBOR_TYPE_INT64 ||
321 Item.val.int64 != 65535)
322 return -1;
323
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800324
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800325 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700326 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800327 if(Item.uDataType != QCBOR_TYPE_INT64 ||
328 Item.val.int64 != 65536)
329 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800330
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800331 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700332 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800333 if(Item.uDataType != QCBOR_TYPE_INT64 ||
334 Item.val.int64 != 65537)
335 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800336
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800337 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700338 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800339 if(Item.uDataType != QCBOR_TYPE_INT64 ||
340 Item.val.int64 != 65538)
341 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800342
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800343 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700344 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800345 if(Item.uDataType != QCBOR_TYPE_INT64 ||
346 Item.val.int64 != 2147483647)
347 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800348
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800349 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700350 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800351 if(Item.uDataType != QCBOR_TYPE_INT64 ||
352 Item.val.int64 != 2147483647)
353 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800354
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800355 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700356 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800357 if(Item.uDataType != QCBOR_TYPE_INT64 ||
358 Item.val.int64 != 2147483648)
359 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800360
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800361 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700362 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800363 if(Item.uDataType != QCBOR_TYPE_INT64 ||
364 Item.val.int64 != 2147483649)
365 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800366
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800367 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700368 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800369 if(Item.uDataType != QCBOR_TYPE_INT64 ||
370 Item.val.int64 != 4294967294)
371 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800372
373
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700374 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
375 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800376 if(Item.uDataType != QCBOR_TYPE_INT64 ||
377 Item.val.int64 != 4294967295)
378 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800379
380
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800381 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700382 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800383 if(Item.uDataType != QCBOR_TYPE_INT64 ||
384 Item.val.int64 != 4294967296)
385 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800386
387
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800388 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700389 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800390 if(Item.uDataType != QCBOR_TYPE_INT64 ||
391 Item.val.int64 != 4294967297)
392 return -1;
393
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800394
395
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800396 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700397 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800398 if(Item.uDataType != QCBOR_TYPE_INT64 ||
399 Item.val.int64 != 9223372036854775807LL)
400 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800401
402
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800403 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700404 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800405 if(Item.uDataType != QCBOR_TYPE_UINT64 ||
406 Item.val.uint64 != 18446744073709551615ULL)
407 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800408
409
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800410 if(QCBORDecode_Finish(pDCtx) != QCBOR_SUCCESS) {
411 return -1;
412 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800413
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800414 return 0;
415}
416
417
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800418// One less than the smallest negative integer allowed in C. Decoding
419// this should fail.
420static const uint8_t spTooSmallNegative[] = {
421 0x3b, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
Laurence Lundblade21d1d812019-09-28 22:47:49 -1000422};
423
424
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800425/*
426 Tests the decoding of lots of different integers sizes
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +0800427 and values.
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800428 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800429int32_t IntegerValuesParseTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800430{
Laurence Lundblade21d1d812019-09-28 22:47:49 -1000431 int nReturn;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800432 QCBORDecodeContext DCtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800433
Laurence Lundblade21d1d812019-09-28 22:47:49 -1000434 QCBORDecode_Init(&DCtx,
435 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedEncodedInts),
436 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800437
Laurence Lundblade21d1d812019-09-28 22:47:49 -1000438 // The really big test of all successes
439 nReturn = IntegerValuesParseTestInternal(&DCtx);
440 if(nReturn) {
441 return nReturn;
442 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800443
Laurence Lundblade21d1d812019-09-28 22:47:49 -1000444 // The one large negative integer that can be parsed
445 QCBORDecode_Init(&DCtx,
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800446 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTooSmallNegative),
Laurence Lundblade21d1d812019-09-28 22:47:49 -1000447 QCBOR_DECODE_MODE_NORMAL);
448
449 QCBORItem item;
450 if(QCBORDecode_GetNext(&DCtx, &item) != QCBOR_ERR_INT_OVERFLOW) {
451 nReturn = -4000;
452 }
453
454 return(nReturn);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800455}
456
457
458/*
Laurence Lundbladeee851742020-01-08 08:37:05 -0800459 Creates a simple CBOR array and returns it in *pEncoded. The array is
460 malloced and needs to be freed. This is used by several tests.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800461
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800462 Two of the inputs can be set. Two other items in the array are fixed.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800463
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800464 */
465
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -0800466static uint8_t spSimpleArrayBuffer[50];
467
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800468static int32_t CreateSimpleArray(int nInt1, int nInt2, uint8_t **pEncoded, size_t *pEncodedLen)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800469{
470 QCBOREncodeContext ECtx;
471 int nReturn = -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800472
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800473 *pEncoded = NULL;
474 *pEncodedLen = INT32_MAX;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800475
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800476 // loop runs CBOR encoding twice. First with no buffer to
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -0800477 // calculate the length so buffer can be allocated correctly,
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800478 // and last with the buffer to do the actual encoding
479 do {
Laurence Lundblade0595e932018-11-02 22:22:47 +0700480 QCBOREncode_Init(&ECtx, (UsefulBuf){*pEncoded, *pEncodedLen});
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800481 QCBOREncode_OpenArray(&ECtx);
482 QCBOREncode_AddInt64(&ECtx, nInt1);
483 QCBOREncode_AddInt64(&ECtx, nInt2);
484 QCBOREncode_AddBytes(&ECtx, ((UsefulBufC) {"galactic", 8}));
485 QCBOREncode_AddBytes(&ECtx, ((UsefulBufC) {"haven token", 11}));
486 QCBOREncode_CloseArray(&ECtx);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800487
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -0800488 if(QCBOREncode_FinishGetSize(&ECtx, pEncodedLen))
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800489 goto Done;
490
491 if(*pEncoded != NULL) {
492 nReturn = 0;
493 goto Done;
494 }
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -0800495
496 // Use static buffer to avoid dependency on malloc()
497 if(*pEncodedLen > sizeof(spSimpleArrayBuffer)) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800498 goto Done;
499 }
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -0800500 *pEncoded = spSimpleArrayBuffer;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800501
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800502 } while(1);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800503
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -0800504Done:
505 return nReturn;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800506}
507
508
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800509/*
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800510 Some basic CBOR with map and array used in a lot of tests.
511 The map labels are all strings
512
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800513 {"first integer": 42,
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900514 "an array of two strings": [
515 "string1", "string2"
516 ],
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800517 "map in a map": {
518 "bytes 1": h'78787878',
519 "bytes 2": h'79797979',
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900520 "another int": 98,
521 "text 2": "lies, damn lies and statistics"
522 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800523 }
524 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800525static const uint8_t pValidMapEncoded[] = {
Laurence Lundbladeb836efb2018-10-28 20:09:58 +0700526 0xa3, 0x6d, 0x66, 0x69, 0x72, 0x73, 0x74, 0x20, 0x69, 0x6e,
527 0x74, 0x65, 0x67, 0x65, 0x72, 0x18, 0x2a, 0x77, 0x61, 0x6e,
528 0x20, 0x61, 0x72, 0x72, 0x61, 0x79, 0x20, 0x6f, 0x66, 0x20,
529 0x74, 0x77, 0x6f, 0x20, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67,
530 0x73, 0x82, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x31,
531 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x32, 0x6c, 0x6d,
532 0x61, 0x70, 0x20, 0x69, 0x6e, 0x20, 0x61, 0x20, 0x6d, 0x61,
533 0x70, 0xa4, 0x67, 0x62, 0x79, 0x74, 0x65, 0x73, 0x20, 0x31,
534 0x44, 0x78, 0x78, 0x78, 0x78, 0x67, 0x62, 0x79, 0x74, 0x65,
535 0x73, 0x20, 0x32, 0x44, 0x79, 0x79, 0x79, 0x79, 0x6b, 0x61,
536 0x6e, 0x6f, 0x74, 0x68, 0x65, 0x72, 0x20, 0x69, 0x6e, 0x74,
537 0x18, 0x62, 0x66, 0x74, 0x65, 0x78, 0x74, 0x20, 0x32, 0x78,
538 0x1e, 0x6c, 0x69, 0x65, 0x73, 0x2c, 0x20, 0x64, 0x61, 0x6d,
539 0x6e, 0x20, 0x6c, 0x69, 0x65, 0x73, 0x20, 0x61, 0x6e, 0x64,
540 0x20, 0x73, 0x74, 0x61, 0x74, 0x69, 0x73, 0x74, 0x69, 0x63,
Laurence Lundblade02625d42020-06-25 14:41:41 -0700541 0x73 };
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800542
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800543static int32_t ParseOrderedArray(const uint8_t *pEncoded,
Laurence Lundblade02625d42020-06-25 14:41:41 -0700544 size_t nLen,
545 int64_t *pInt1,
546 int64_t *pInt2,
547 const uint8_t **pBuf3,
548 size_t *pBuf3Len,
549 const uint8_t **pBuf4,
550 size_t *pBuf4Len)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800551{
552 QCBORDecodeContext DCtx;
553 QCBORItem Item;
554 int nReturn = -1; // assume error until success
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800555
Laurence Lundbladeee851742020-01-08 08:37:05 -0800556 QCBORDecode_Init(&DCtx,
557 (UsefulBufC){pEncoded, nLen},
558 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800559
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800560 // Make sure the first thing is a map
561 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || Item.uDataType != QCBOR_TYPE_ARRAY)
562 goto Done;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800563
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800564 // First integer
Laurence Lundblade12b495d2018-12-17 11:15:54 -0800565 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || Item.uDataType != QCBOR_TYPE_INT64)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800566 goto Done;
567 *pInt1 = Item.val.int64;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800568
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800569 // Second integer
570 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || Item.uDataType != QCBOR_TYPE_INT64)
571 goto Done;
572 *pInt2 = Item.val.int64;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800573
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800574 // First string
575 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || Item.uDataType != QCBOR_TYPE_BYTE_STRING)
576 goto Done;
577 *pBuf3 = Item.val.string.ptr;
578 *pBuf3Len = Item.val.string.len;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800579
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800580 // Second string
581 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || Item.uDataType != QCBOR_TYPE_BYTE_STRING)
582 goto Done;
583 *pBuf4 = Item.val.string.ptr;
584 *pBuf4Len = Item.val.string.len;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800585
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800586 nReturn = 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800587
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800588Done:
589 return(nReturn);
590}
591
592
593
Laurence Lundblade9e3651c2018-10-10 11:49:55 +0800594
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800595int32_t SimpleArrayTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800596{
597 uint8_t *pEncoded;
598 size_t nEncodedLen;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800599
Laurence Lundblade5e390822019-01-06 12:35:01 -0800600 int64_t i1=0, i2=0;
601 size_t i3=0, i4=0;
602 const uint8_t *s3= (uint8_t *)"";
603 const uint8_t *s4= (uint8_t *)"";
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800604
605
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800606 if(CreateSimpleArray(23, 6000, &pEncoded, &nEncodedLen) < 0) {
607 return(-1);
608 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800609
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800610 ParseOrderedArray(pEncoded, nEncodedLen, &i1, &i2, &s3, &i3, &s4, &i4);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800611
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800612 if(i1 != 23 ||
613 i2 != 6000 ||
614 i3 != 8 ||
615 i4 != 11 ||
Laurence Lundbladea44d5062018-10-17 18:45:12 +0530616 memcmp("galactic", s3, 8) !=0 ||
617 memcmp("haven token", s4, 11) !=0) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800618 return(-1);
619 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800620
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800621 return(0);
622}
623
624
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700625/*
626 [
627 0,
628 [],
629 [
630 [],
631 [
632 0
633 ],
634 {},
635 {
636 1: {},
637 2: {},
638 3: []
639 }
640 ]
641 ]
642 */
643static uint8_t sEmpties[] = {0x83, 0x00, 0x80, 0x84, 0x80, 0x81, 0x00, 0xa0,
644 0xa3, 0x01, 0xa0, 0x02, 0xa0, 0x03, 0x80};
645
Laurence Lundblade02625d42020-06-25 14:41:41 -0700646/* Same as above, but with indefinte lengths */
647static uint8_t sEmptiesIndef[] = {
6480x9F,
6490x00,
6500x9F,
6510xFF,
6520x9F,
6530x9F,
6540xFF,
6550x9F,
6560x00,
6570xFF,
6580xBF,
6590xFF,
6600xBF,
6610x01,
6620xBF,
6630xFF,
6640x02,
6650xBF,
6660xFF,
6670x03,
6680x9F,
6690xFF,
6700xFF,
6710xFF,
672 0xFF};
673
674
675
676static int32_t CheckEmpties(UsefulBufC input, bool bCheckCounts)
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700677{
678 QCBORDecodeContext DCtx;
679 QCBORItem Item;
680
Laurence Lundbladeee851742020-01-08 08:37:05 -0800681 QCBORDecode_Init(&DCtx,
Laurence Lundblade02625d42020-06-25 14:41:41 -0700682 input,
Laurence Lundbladeee851742020-01-08 08:37:05 -0800683 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700684
685 // Array with 3 items
686 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
687 Item.uDataType != QCBOR_TYPE_ARRAY ||
688 Item.uNestingLevel != 0 ||
689 Item.uNextNestLevel != 1 ||
Laurence Lundblade02625d42020-06-25 14:41:41 -0700690 (bCheckCounts && Item.val.uCount != 3)) {
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700691 return -1;
692 }
693
694 // An integer 0
695 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
696 Item.uDataType != QCBOR_TYPE_INT64 ||
697 Item.uNestingLevel != 1 ||
698 Item.uNextNestLevel != 1 ||
699 Item.val.uint64 != 0) {
700 return -2;
701 }
702
703 // An empty array
704 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
705 Item.uDataType != QCBOR_TYPE_ARRAY ||
706 Item.uNestingLevel != 1 ||
707 Item.uNextNestLevel != 1 ||
Laurence Lundblade02625d42020-06-25 14:41:41 -0700708 (bCheckCounts && Item.val.uCount != 0)) {
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700709 return -3;
710 }
711
712 // An array with 4 items
713 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
714 Item.uDataType != QCBOR_TYPE_ARRAY ||
715 Item.uNestingLevel != 1 ||
716 Item.uNextNestLevel != 2 ||
Laurence Lundblade02625d42020-06-25 14:41:41 -0700717 (bCheckCounts && Item.val.uCount != 4)) {
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700718 return -4;
719 }
720
721 // An empty array
722 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
723 Item.uDataType != QCBOR_TYPE_ARRAY ||
724 Item.uNestingLevel != 2 ||
725 Item.uNextNestLevel != 2 ||
Laurence Lundblade02625d42020-06-25 14:41:41 -0700726 (bCheckCounts && Item.val.uCount != 0)) {
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700727 return -5;
728 }
729
730 // An array with 1 item
731 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
732 Item.uDataType != QCBOR_TYPE_ARRAY ||
733 Item.uNestingLevel != 2 ||
734 Item.uNextNestLevel != 3 ||
Laurence Lundblade02625d42020-06-25 14:41:41 -0700735 (bCheckCounts && Item.val.uCount != 1)) {
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700736 return -6;
737 }
738
739 // An integer 0
740 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
741 Item.uDataType != QCBOR_TYPE_INT64 ||
742 Item.uNestingLevel != 3 ||
743 Item.uNextNestLevel != 2 ||
744 Item.val.uint64 != 0) {
745 return -7;
746 }
747
748 // An empty map
749 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
750 Item.uDataType != QCBOR_TYPE_MAP ||
751 Item.uNestingLevel != 2 ||
752 Item.uNextNestLevel != 2 ||
Laurence Lundblade02625d42020-06-25 14:41:41 -0700753 (bCheckCounts && Item.val.uCount != 0)) {
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700754 return -8;
755 }
756
Laurence Lundblade5e87da62020-06-07 03:24:28 -0700757 // A map with 3 items
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700758 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
759 Item.uDataType != QCBOR_TYPE_MAP ||
760 Item.uNestingLevel != 2 ||
761 Item.uNextNestLevel != 3 ||
Laurence Lundblade02625d42020-06-25 14:41:41 -0700762 (bCheckCounts && Item.val.uCount != 3)) {
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700763 return -9;
764 }
765
766 // An empty map
767 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
768 Item.uDataType != QCBOR_TYPE_MAP ||
769 Item.uNestingLevel != 3 ||
770 Item.uNextNestLevel != 3 ||
Laurence Lundblade02625d42020-06-25 14:41:41 -0700771 (bCheckCounts && Item.val.uCount != 0)) {
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700772 return -10;
773 }
774
775 // An empty map
776 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
777 Item.uDataType != QCBOR_TYPE_MAP ||
778 Item.uNestingLevel != 3 ||
779 Item.uNextNestLevel != 3 ||
Laurence Lundblade02625d42020-06-25 14:41:41 -0700780 (bCheckCounts && Item.val.uCount != 0)) {
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700781 return -11;
782 }
783
784 // An empty array
785 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
786 Item.uDataType != QCBOR_TYPE_ARRAY ||
787 Item.uNestingLevel != 3 ||
788 Item.uNextNestLevel != 0 ||
Laurence Lundblade02625d42020-06-25 14:41:41 -0700789 (bCheckCounts && Item.val.uCount != 0)) {
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700790 return -12;
791 }
792
793 if(QCBORDecode_Finish(&DCtx) != QCBOR_SUCCESS) {
794 return -13;
795 }
Laurence Lundblade02625d42020-06-25 14:41:41 -0700796 return 0;
797}
798
799
800int32_t EmptyMapsAndArraysTest()
801{
802 int nResult;
803 nResult = CheckEmpties(UsefulBuf_FROM_BYTE_ARRAY_LITERAL(sEmpties),
804 true);
805 if(nResult) {
806 return nResult;
807 }
808
809 nResult = CheckEmpties(UsefulBuf_FROM_BYTE_ARRAY_LITERAL(sEmptiesIndef),
810 false);
811
812 if(nResult) {
813 return nResult -100;
814 }
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700815
816 return 0;
817}
818
Laurence Lundblade9e3651c2018-10-10 11:49:55 +0800819
Laurence Lundbladeee851742020-01-08 08:37:05 -0800820static uint8_t spDeepArrays[] = {0x81, 0x81, 0x81, 0x81, 0x81, 0x81,
821 0x81, 0x81, 0x81, 0x80};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800822
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800823int32_t ParseDeepArrayTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800824{
825 QCBORDecodeContext DCtx;
826 int nReturn = 0;
827 int i;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800828
Laurence Lundbladeee851742020-01-08 08:37:05 -0800829 QCBORDecode_Init(&DCtx,
830 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spDeepArrays),
831 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800832
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800833 for(i = 0; i < 10; i++) {
834 QCBORItem Item;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800835
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800836 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
837 Item.uDataType != QCBOR_TYPE_ARRAY ||
838 Item.uNestingLevel != i) {
839 nReturn = -1;
840 break;
841 }
842 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800843
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800844 return(nReturn);
845}
846
Laurence Lundblade972e59c2018-11-11 15:57:23 +0700847// Big enough to test nesting to the depth of 24
Laurence Lundbladeee851742020-01-08 08:37:05 -0800848static uint8_t spTooDeepArrays[] = {0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81,
849 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81,
850 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81,
851 0x81, 0x81, 0x81, 0x80};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800852
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800853int32_t ParseTooDeepArrayTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800854{
855 QCBORDecodeContext DCtx;
856 int nReturn = 0;
857 int i;
858 QCBORItem Item;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800859
860
Laurence Lundbladeee851742020-01-08 08:37:05 -0800861 QCBORDecode_Init(&DCtx,
862 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTooDeepArrays),
863 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800864
Laurence Lundblade972e59c2018-11-11 15:57:23 +0700865 for(i = 0; i < QCBOR_MAX_ARRAY_NESTING1; i++) {
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800866
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800867 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
868 Item.uDataType != QCBOR_TYPE_ARRAY ||
869 Item.uNestingLevel != i) {
870 nReturn = -1;
871 break;
872 }
873 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800874
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800875 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_ARRAY_NESTING_TOO_DEEP)
876 nReturn = -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800877
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800878 return(nReturn);
879}
880
881
882
883
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800884int32_t ShortBufferParseTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800885{
Laurence Lundbladebb1062e2019-08-12 23:28:54 -0700886 int nResult = 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800887
Laurence Lundblade06350ea2020-01-27 19:32:40 -0800888 for(size_t nNum = sizeof(spExpectedEncodedInts)-1; nNum; nNum--) {
Laurence Lundbladebb1062e2019-08-12 23:28:54 -0700889 QCBORDecodeContext DCtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800890
Laurence Lundbladeee851742020-01-08 08:37:05 -0800891 QCBORDecode_Init(&DCtx,
892 (UsefulBufC){spExpectedEncodedInts, nNum},
893 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800894
Laurence Lundblade06350ea2020-01-27 19:32:40 -0800895 const int nErr = IntegerValuesParseTestInternal(&DCtx);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800896
Laurence Lundbladebb1062e2019-08-12 23:28:54 -0700897 if(nErr != QCBOR_ERR_HIT_END && nErr != QCBOR_ERR_NO_MORE_ITEMS) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800898 nResult = -1;
899 goto Done;
900 }
901 }
902Done:
903 return nResult;
904}
905
906
Laurence Lundblade9e3651c2018-10-10 11:49:55 +0800907
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800908int32_t ShortBufferParseTest2()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800909{
910 uint8_t *pEncoded;
911 int nReturn;
912 size_t nEncodedLen;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800913
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800914 int64_t i1, i2;
915 size_t i3, i4;
916 const uint8_t *s3, *s4;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800917
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800918 nReturn = 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800919
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800920 if(CreateSimpleArray(23, 6000, &pEncoded, &nEncodedLen) < 0) {
921 return(-1);
922 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800923
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800924 for(nEncodedLen--; nEncodedLen; nEncodedLen--) {
925 int nResult = ParseOrderedArray(pEncoded, (uint32_t)nEncodedLen, &i1, &i2, &s3, &i3, &s4, &i4);
926 if(nResult == 0) {
927 nReturn = -1;
928 }
929 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800930
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800931 return(nReturn);
932}
933
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530934/*
935 Decode and thoroughly check a moderately complex
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800936 set of maps. Can be run in QCBOR_DECODE_MODE_NORMAL or in
937 QCBOR_DECODE_MODE_MAP_STRINGS_ONLY.
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530938 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800939static int32_t ParseMapTest1(QCBORDecodeMode nMode)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800940{
941 QCBORDecodeContext DCtx;
942 QCBORItem Item;
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700943 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800944
Laurence Lundbladeee851742020-01-08 08:37:05 -0800945 QCBORDecode_Init(&DCtx,
946 (UsefulBufC){pValidMapEncoded, sizeof(pValidMapEncoded)},
947 nMode);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800948
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900949 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700950 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900951 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800952 if(Item.uDataType != QCBOR_TYPE_MAP ||
953 Item.val.uCount != 3)
954 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800955
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900956 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700957 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900958 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800959 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800960 Item.uDataType != QCBOR_TYPE_INT64 ||
961 Item.val.int64 != 42 ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530962 Item.uDataAlloc ||
963 Item.uLabelAlloc ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900964 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("first integer"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800965 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900966 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800967
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900968 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700969 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900970 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800971 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530972 Item.uDataAlloc ||
973 Item.uLabelAlloc ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900974 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("an array of two strings")) ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800975 Item.uDataType != QCBOR_TYPE_ARRAY ||
976 Item.val.uCount != 2)
977 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800978
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900979 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700980 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900981 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800982 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530983 Item.uDataAlloc ||
984 Item.uLabelAlloc ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900985 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("string1"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800986 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900987 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800988
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900989 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700990 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900991 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800992 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530993 Item.uDataAlloc ||
994 Item.uLabelAlloc ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900995 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("string2"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800996 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900997 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800998
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900999 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001000 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001001 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001002 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301003 Item.uDataAlloc ||
1004 Item.uLabelAlloc ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001005 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("map in a map")) ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001006 Item.uDataType != QCBOR_TYPE_MAP ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001007 Item.val.uCount != 4) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001008 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001009 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001010
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001011 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001012 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001013 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001014 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001015 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("bytes 1"))||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001016 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301017 Item.uDataAlloc ||
1018 Item.uLabelAlloc ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001019 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("xxxx"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001020 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001021 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001022
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001023 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001024 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001025 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001026 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001027 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("bytes 2")) ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001028 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301029 Item.uDataAlloc ||
1030 Item.uLabelAlloc ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001031 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("yyyy"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001032 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001033 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001034
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001035 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001036 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001037 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001038 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301039 Item.uDataAlloc ||
1040 Item.uLabelAlloc ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001041 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("another int")) ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001042 Item.uDataType != QCBOR_TYPE_INT64 ||
1043 Item.val.int64 != 98)
1044 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001045
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001046 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001047 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001048 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001049 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001050 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("text 2"))||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001051 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301052 Item.uDataAlloc ||
1053 Item.uLabelAlloc ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001054 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("lies, damn lies and statistics"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001055 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001056 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001057
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001058 return 0;
1059}
1060
1061
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001062/*
1063 Decode and thoroughly check a moderately complex
1064 set of maps
1065 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001066int32_t ParseMapAsArrayTest()
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001067{
1068 QCBORDecodeContext DCtx;
1069 QCBORItem Item;
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001070 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001071
Laurence Lundbladeee851742020-01-08 08:37:05 -08001072 QCBORDecode_Init(&DCtx,
1073 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded),
1074 QCBOR_DECODE_MODE_MAP_AS_ARRAY);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001075
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001076 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001077 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001078 }
Laurence Lundbladed61cbf32018-12-09 11:42:21 -08001079 if(Item.uDataType != QCBOR_TYPE_MAP_AS_ARRAY ||
1080 Item.val.uCount != 6) {
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001081 return -1;
1082 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001083
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001084 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001085 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001086 }
1087 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1088 Item.uDataAlloc ||
1089 Item.uLabelAlloc ||
1090 Item.uLabelType != QCBOR_TYPE_NONE ||
1091 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("first integer"))) {
1092 return -2;
1093 }
1094
1095 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001096 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001097 }
1098 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1099 Item.uDataType != QCBOR_TYPE_INT64 ||
1100 Item.val.int64 != 42 ||
1101 Item.uDataAlloc ||
1102 Item.uLabelAlloc) {
1103 return -3;
1104 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001105
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001106 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001107 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001108 }
1109 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1110 Item.uDataAlloc ||
1111 Item.uLabelAlloc ||
1112 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("an array of two strings")) ||
1113 Item.uDataType != QCBOR_TYPE_TEXT_STRING) {
1114 return -4;
1115 }
1116
1117 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001118 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001119 }
1120 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1121 Item.uDataAlloc ||
1122 Item.uLabelAlloc ||
1123 Item.uDataType != QCBOR_TYPE_ARRAY ||
1124 Item.val.uCount != 2) {
1125 return -5;
1126 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001127
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001128 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001129 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001130 }
1131 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1132 Item.val.string.len != 7 ||
1133 Item.uDataAlloc ||
1134 Item.uLabelAlloc ||
1135 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("string1"))) {
1136 return -6;
1137 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001138
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001139 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001140 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001141 }
1142 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1143 Item.uDataAlloc ||
1144 Item.uLabelAlloc ||
1145 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("string2"))) {
1146 return -7;
1147 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001148
1149
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001150 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001151 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001152 }
1153 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1154 Item.uDataAlloc ||
1155 Item.uLabelAlloc ||
1156 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("map in a map"))) {
1157 return -8;
1158 }
1159
1160 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001161 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001162 }
1163 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1164 Item.uDataAlloc ||
1165 Item.uLabelAlloc ||
Laurence Lundbladed61cbf32018-12-09 11:42:21 -08001166 Item.uDataType != QCBOR_TYPE_MAP_AS_ARRAY ||
1167 Item.val.uCount != 8) {
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001168 return -9;
1169 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001170
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001171 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001172 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001173 }
1174 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1175 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("bytes 1"))||
1176 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1177 Item.uDataAlloc ||
1178 Item.uLabelAlloc) {
1179 return -10;
1180 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001181
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001182 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001183 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001184 }
1185 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1186 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
1187 Item.uDataAlloc ||
1188 Item.uLabelAlloc ||
1189 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("xxxx"))) {
1190 return -11;
1191 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001192
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001193 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001194 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001195 }
1196 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1197 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("bytes 2")) ||
1198 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1199 Item.uDataAlloc ||
1200 Item.uLabelAlloc) {
1201 return -12;
1202 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001203
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001204 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001205 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001206 }
1207 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1208 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
1209 Item.uDataAlloc ||
1210 Item.uLabelAlloc ||
1211 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("yyyy"))) {
1212 return -13;
1213 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001214
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001215 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001216 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001217 }
1218 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1219 Item.uDataAlloc ||
1220 Item.uLabelAlloc ||
1221 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("another int")) ||
1222 Item.uDataType != QCBOR_TYPE_TEXT_STRING) {
1223 return -14;
1224 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001225
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001226 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001227 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001228 }
1229 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1230 Item.uDataAlloc ||
1231 Item.uLabelAlloc ||
1232 Item.uDataType != QCBOR_TYPE_INT64 ||
1233 Item.val.int64 != 98) {
1234 return -15;
1235 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001236
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001237 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001238 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001239 }
1240 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1241 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("text 2"))||
1242 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1243 Item.uDataAlloc ||
1244 Item.uLabelAlloc) {
1245 return -16;
1246 }
1247
1248 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001249 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001250 }
1251 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1252 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1253 Item.uDataAlloc ||
1254 Item.uLabelAlloc ||
1255 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("lies, damn lies and statistics"))) {
1256 return -17;
1257 }
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001258
1259
1260 /*
1261 Test with map that nearly QCBOR_MAX_ITEMS_IN_ARRAY items in a
1262 map that when interpreted as an array will be too many. Test
1263 data just has the start of the map, not all the items in the map.
1264 */
1265 static const uint8_t pTooLargeMap[] = {0xb9, 0xff, 0xfd};
1266
1267 QCBORDecode_Init(&DCtx,
1268 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pTooLargeMap),
1269 QCBOR_DECODE_MODE_MAP_AS_ARRAY);
1270
1271 if((QCBOR_ERR_ARRAY_TOO_LONG != QCBORDecode_GetNext(&DCtx, &Item))) {
1272 return -50;
1273 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001274
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001275 return 0;
1276}
1277
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001278
1279/*
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301280 Fully or partially decode pValidMapEncoded. When
1281 partially decoding check for the right error code.
1282 How much partial decoding depends on nLevel.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001283
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301284 The partial decodes test error conditions of
1285 incomplete encoded input.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001286
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301287 This could be combined with the above test
1288 and made prettier and maybe a little more
1289 thorough.
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001290 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001291static int32_t ExtraBytesTest(int nLevel)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001292{
1293 QCBORDecodeContext DCtx;
1294 QCBORItem Item;
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001295 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001296
Laurence Lundbladeee851742020-01-08 08:37:05 -08001297 QCBORDecode_Init(&DCtx,
1298 (UsefulBufC){pValidMapEncoded, sizeof(pValidMapEncoded)},
1299 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001300
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001301 if(nLevel < 1) {
1302 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_EXTRA_BYTES) {
1303 return -1;
1304 } else {
1305 return 0;
1306 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001307 }
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301308
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001309
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001310 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001311 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001312 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001313 if(Item.uDataType != QCBOR_TYPE_MAP ||
1314 Item.val.uCount != 3)
1315 return -1;
1316
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001317 if(nLevel < 2) {
1318 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
1319 return -1;
1320 } else {
1321 return 0;
1322 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001323 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001324
1325
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001326 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001327 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001328 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001329 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001330 Item.uDataType != QCBOR_TYPE_INT64 ||
1331 Item.val.uCount != 42 ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001332 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("first integer"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001333 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001334 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001335
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001336 if(nLevel < 3) {
1337 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
1338 return -1;
1339 } else {
1340 return 0;
1341 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001342 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001343
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001344 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001345 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001346 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001347 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001348 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("an array of two strings")) ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001349 Item.uDataType != QCBOR_TYPE_ARRAY ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001350 Item.val.uCount != 2) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001351 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001352 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001353
1354
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001355 if(nLevel < 4) {
1356 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
1357 return -1;
1358 } else {
1359 return 0;
1360 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001361 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001362
1363
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001364 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001365 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001366 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001367 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001368 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("string1"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001369 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001370 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001371
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001372 if(nLevel < 5) {
1373 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
1374 return -1;
1375 } else {
1376 return 0;
1377 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001378 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001379
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001380 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001381 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001382 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001383 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001384 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("string2"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001385 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001386 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001387
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001388 if(nLevel < 6) {
1389 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
1390 return -1;
1391 } else {
1392 return 0;
1393 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001394 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001395
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001396 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001397 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001398 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001399 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001400 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("map in a map")) ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001401 Item.uDataType != QCBOR_TYPE_MAP ||
1402 Item.val.uCount != 4)
1403 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001404
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001405 if(nLevel < 7) {
1406 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
1407 return -1;
1408 } else {
1409 return 0;
1410 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001411 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001412
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001413 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001414 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001415 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001416 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001417 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("bytes 1")) ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001418 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001419 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("xxxx"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001420 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001421 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001422
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001423 if(nLevel < 8) {
1424 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
1425 return -1;
1426 } else {
1427 return 0;
1428 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001429 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001430
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001431 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001432 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001433 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001434 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001435 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("bytes 2")) ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001436 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001437 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("yyyy"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001438 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001439 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001440
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001441 if(nLevel < 9) {
1442 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
1443 return -1;
1444 } else {
1445 return 0;
1446 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001447 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001448
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001449 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001450 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001451 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001452 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001453 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("another int")) ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001454 Item.uDataType != QCBOR_TYPE_INT64 ||
1455 Item.val.int64 != 98)
1456 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001457
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001458 if(nLevel < 10) {
1459 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
1460 return -1;
1461 } else {
1462 return 0;
1463 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001464 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001465
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001466 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001467 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001468 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001469 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001470 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("text 2"))||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001471 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001472 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("lies, damn lies and statistics"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001473 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001474 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001475
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301476 if(QCBORDecode_Finish(&DCtx)) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001477 return -1;
1478 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001479
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001480 return 0;
1481}
1482
1483
1484
Laurence Lundblade844bb5c2020-03-01 17:27:25 -08001485
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001486int32_t ParseMapTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001487{
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001488 // Parse a moderatly complex map structure very thoroughly
1489 int32_t nResult = ParseMapTest1(QCBOR_DECODE_MODE_NORMAL);
1490 if(nResult) {
1491 return nResult;
1492 }
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08001493
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001494 // Again, but in strings-only mode. It should succeed since the input
1495 // map has only string labels.
1496 nResult = ParseMapTest1(QCBOR_DECODE_MODE_MAP_STRINGS_ONLY);
1497 if(nResult) {
1498 return nResult;
1499 }
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08001500
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001501 // Again, but try to finish the decoding before the end of the
1502 // input at 10 different place and see that the right error code
1503 // is returned.
1504 for(int i = 0; i < 10; i++) {
1505 nResult = ExtraBytesTest(i);
1506 if(nResult) {
1507 break;
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001508 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001509 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001510
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001511 return nResult;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001512}
1513
1514
Laurence Lundbladeee851742020-01-08 08:37:05 -08001515static uint8_t spSimpleValues[] = {0x8a, 0xf4, 0xf5, 0xf6, 0xf7, 0xff,
1516 0xe0, 0xf3, 0xf8, 0x00, 0xf8, 0x13,
1517 0xf8, 0x1f, 0xf8, 0x20, 0xf8, 0xff};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001518
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001519int32_t ParseSimpleTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001520{
1521 QCBORDecodeContext DCtx;
1522 QCBORItem Item;
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001523 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001524
1525
Laurence Lundbladeee851742020-01-08 08:37:05 -08001526 QCBORDecode_Init(&DCtx,
1527 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSimpleValues),
1528 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001529
1530
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001531 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001532 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001533 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
1534 Item.val.uCount != 10)
1535 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001536
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001537 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001538 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001539 if(Item.uDataType != QCBOR_TYPE_FALSE)
1540 return -1;
1541
1542 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001543 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001544 if(Item.uDataType != QCBOR_TYPE_TRUE)
1545 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001546
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001547 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001548 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001549 if(Item.uDataType != QCBOR_TYPE_NULL)
1550 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001551
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001552 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001553 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001554 if(Item.uDataType != QCBOR_TYPE_UNDEF)
1555 return -1;
1556
1557 // A break
Laurence Lundblade9e3651c2018-10-10 11:49:55 +08001558 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_BREAK)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001559 return -1;
1560
1561 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001562 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001563 if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 0)
1564 return -1;
1565
1566 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001567 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001568 if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 19)
1569 return -1;
1570
Laurence Lundblade077475f2019-04-26 09:06:33 -07001571 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_TYPE_7)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001572 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001573
Laurence Lundblade077475f2019-04-26 09:06:33 -07001574 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_TYPE_7)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001575 return -1;
1576
Laurence Lundblade077475f2019-04-26 09:06:33 -07001577 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_TYPE_7)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001578 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001579
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001580 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001581 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001582 if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 32)
1583 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001584
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001585 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001586 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001587 if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 255)
1588 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001589
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001590 return 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001591
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001592}
1593
1594
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001595static int IsNotWellFormedError(QCBORError nErr)
1596{
1597 switch(nErr){
1598 case QCBOR_ERR_INDEFINITE_STRING_CHUNK:
1599 case QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN:
1600 case QCBOR_ERR_UNSUPPORTED:
1601 case QCBOR_ERR_HIT_END:
1602 case QCBOR_ERR_BAD_TYPE_7:
1603 case QCBOR_ERR_BAD_BREAK:
1604 case QCBOR_ERR_EXTRA_BYTES:
1605 case QCBOR_ERR_BAD_INT:
1606 return 1;
1607 default:
1608 return 0;
1609 }
1610}
1611
1612
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001613int32_t NotWellFormedTests()
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001614{
1615 // Loop over all the not-well-formed instance of CBOR
1616 // that are test vectors in not_well_formed_cbor.h
1617 const uint16_t nArraySize = sizeof(paNotWellFormedCBOR)/sizeof(struct someBinaryBytes);
1618 for(uint16_t nIterate = 0; nIterate < nArraySize; nIterate++) {
1619 const struct someBinaryBytes *pBytes = &paNotWellFormedCBOR[nIterate];
1620 const UsefulBufC Input = (UsefulBufC){pBytes->p, pBytes->n};
1621
Laurence Lundbladeee851742020-01-08 08:37:05 -08001622 // Set up decoder context. String allocator needed for indefinite
1623 // string test cases
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001624 QCBORDecodeContext DCtx;
1625 QCBORDecode_Init(&DCtx, Input, QCBOR_DECODE_MODE_NORMAL);
1626 UsefulBuf_MAKE_STACK_UB(Pool, 100);
1627 QCBORDecode_SetMemPool(&DCtx, Pool, 0);
1628
1629 // Loop getting items until no more to get
1630 QCBORError nCBORError;
1631 do {
1632 QCBORItem Item;
1633
1634 nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
1635 } while(nCBORError == QCBOR_SUCCESS);
1636
1637 // Every test vector must fail with
1638 // a not-well-formed error. If not
1639 // this test fails.
1640 if(!IsNotWellFormedError(nCBORError)) {
1641 // Return index of failure in the error code
1642 return 2000 + nIterate;
1643 }
1644 }
1645 return 0;
1646}
1647
1648
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001649struct FailInput {
Laurence Lundblade59289e52019-12-30 13:44:37 -08001650 UsefulBufC Input;
1651 QCBORError nError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001652};
1653
Laurence Lundblade59289e52019-12-30 13:44:37 -08001654
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001655static int32_t ProcessFailures(struct FailInput *pFailInputs, size_t nNumFails)
Laurence Lundblade59289e52019-12-30 13:44:37 -08001656{
1657 for(struct FailInput *pF = pFailInputs; pF < pFailInputs + nNumFails; pF++) {
1658 // Set up the decoding context including a memory pool so that
1659 // indefinite length items can be checked
1660 QCBORDecodeContext DCtx;
1661 QCBORDecode_Init(&DCtx, pF->Input, QCBOR_DECODE_MODE_NORMAL);
1662 UsefulBuf_MAKE_STACK_UB(Pool, 100);
Laurence Lundblade830fbf92020-05-31 17:22:33 -07001663
Laurence Lundblade59289e52019-12-30 13:44:37 -08001664 QCBORError nCBORError = QCBORDecode_SetMemPool(&DCtx, Pool, 0);
1665 if(nCBORError) {
1666 return -9;
1667 }
Laurence Lundblade0a042a92020-06-12 14:09:50 -07001668
Laurence Lundblade59289e52019-12-30 13:44:37 -08001669 // Iterate until there is an error of some sort error
1670 QCBORItem Item;
1671 do {
Laurence Lundblade02625d42020-06-25 14:41:41 -07001672 // Set to something none-zero, something other than QCBOR_TYPE_NONE
Laurence Lundblade59289e52019-12-30 13:44:37 -08001673 memset(&Item, 0x33, sizeof(Item));
1674
1675 nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
1676 } while(nCBORError == QCBOR_SUCCESS);
1677
1678 // Must get the expected error or the this test fails
1679 // The data and label type must also be QCBOR_TYPE_NONE
1680 if(nCBORError != pF->nError ||
1681 Item.uDataType != QCBOR_TYPE_NONE ||
1682 Item.uLabelType != QCBOR_TYPE_NONE) {
Laurence Lundblade06350ea2020-01-27 19:32:40 -08001683 // return index of CBOR + 100
Laurence Lundblade830fbf92020-05-31 17:22:33 -07001684 const size_t nIndex = (size_t)(pF - pFailInputs);
Laurence Lundblade06350ea2020-01-27 19:32:40 -08001685 return (int32_t)(nIndex * 100 + nCBORError);
Laurence Lundblade59289e52019-12-30 13:44:37 -08001686 }
1687 }
1688
1689 return 0;
1690}
1691
1692
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001693struct FailInput Failures[] = {
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001694 // Most of this is copied from not_well_formed.h. Here the error code
1695 // returned is also checked.
1696
1697 // Indefinite length strings must be closed off
1698 // An indefinite length byte string not closed off
1699 { {(uint8_t[]){0x5f, 0x41, 0x00}, 3}, QCBOR_ERR_HIT_END },
1700 // An indefinite length text string not closed off
1701 { {(uint8_t[]){0x7f, 0x61, 0x00}, 3}, QCBOR_ERR_HIT_END },
1702
1703
1704 // All the chunks in an indefinite length string must be of the type of indefinite length string
1705 // indefinite length byte string with text string chunk
1706 { {(uint8_t[]){0x5f, 0x61, 0x00, 0xff}, 4}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1707 // indefinite length text string with a byte string chunk
1708 { {(uint8_t[]){0x7f, 0x41, 0x00, 0xff}, 4}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1709 // indefinite length byte string with an positive integer chunk
1710 { {(uint8_t[]){0x5f, 0x00, 0xff}, 3}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1711 // indefinite length byte string with an negative integer chunk
1712 { {(uint8_t[]){0x5f, 0x21, 0xff}, 3}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1713 // indefinite length byte string with an array chunk
1714 { {(uint8_t[]){0x5f, 0x80, 0xff}, 3}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1715 // indefinite length byte string with an map chunk
1716 { {(uint8_t[]){0x5f, 0xa0, 0xff}, 3}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1717 // indefinite length byte string with tagged integer chunk
1718 { {(uint8_t[]){0x5f, 0xc0, 0x00, 0xff}, 4}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1719 // indefinite length byte string with an simple type chunk
1720 { {(uint8_t[]){0x5f, 0xe0, 0xff}, 3}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1721 { {(uint8_t[]){0x5f, 0x5f, 0x41, 0x00, 0xff, 0xff}, 6}, QCBOR_ERR_INDEFINITE_STRING_CHUNK},
1722 // indefinite length text string with indefinite string inside
1723 { {(uint8_t[]){0x7f, 0x7f, 0x61, 0x00, 0xff, 0xff}, 6}, QCBOR_ERR_INDEFINITE_STRING_CHUNK},
1724
1725
1726 // Definte length maps and arrays must be closed by having the right number of items
1727 // A definte length array that is supposed to have 1 item, but has none
1728 { {(uint8_t[]){0x81}, 1}, QCBOR_ERR_HIT_END },
1729 // A definte length array that is supposed to have 2 items, but has only 1
1730 { {(uint8_t[]){0x82, 0x00}, 2}, QCBOR_ERR_HIT_END },
1731 // A definte length array that is supposed to have 511 items, but has only 1
1732 { {(uint8_t[]){0x9a, 0x01, 0xff, 0x00}, 4}, QCBOR_ERR_HIT_END },
1733 // A definte length map that is supposed to have 1 item, but has none
1734 { {(uint8_t[]){0xa1}, 1}, QCBOR_ERR_HIT_END },
1735 // A definte length map that is supposed to have s item, but has only 1
1736 { {(uint8_t[]){0xa2, 0x01, 0x02}, 3}, QCBOR_ERR_HIT_END },
1737
1738
1739 // Indefinte length maps and arrays must be ended by a break
1740 // Indefinite length array with zero items and no break
1741 { {(uint8_t[]){0x9f}, 1}, QCBOR_ERR_HIT_END },
1742 // Indefinite length array with two items and no break
1743 { {(uint8_t[]){0x9f, 0x01, 0x02}, 3}, QCBOR_ERR_HIT_END },
1744 // Indefinite length map with zero items and no break
1745 { {(uint8_t[]){0xbf}, 1}, QCBOR_ERR_HIT_END },
1746 // Indefinite length map with two items and no break
1747 { {(uint8_t[]){0xbf, 0x01, 0x02, 0x01, 0x02}, 5}, QCBOR_ERR_HIT_END },
1748
1749
1750 // Nested maps and arrays must be closed off (some extra nested test vectors)
Laurence Lundblade642282a2020-06-23 12:00:33 -07001751 // Unclosed indefinite array containing a closed definite length array
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001752 { {(uint8_t[]){0x9f, 0x80, 0x00}, 3}, QCBOR_ERR_HIT_END },
Laurence Lundblade642282a2020-06-23 12:00:33 -07001753 // Definite length array containing an unclosed indefinite length array
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001754 { {(uint8_t[]){0x81, 0x9f}, 2}, QCBOR_ERR_HIT_END },
1755 // Deeply nested definite length arrays with deepest one unclosed
1756 { {(uint8_t[]){0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81}, 9}, QCBOR_ERR_HIT_END },
1757 // Deeply nested indefinite length arrays with deepest one unclosed
1758 { {(uint8_t[]){0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0xff, 0xff, 0xff, 0xff}, 9}, QCBOR_ERR_HIT_END },
1759 // Mixed nesting with indefinite unclosed
Laurence Lundblade0a042a92020-06-12 14:09:50 -07001760 { {(uint8_t[]){0x9f, 0x81, 0x9f, 0x81, 0x9f, 0x9f, 0xff, 0xff, 0xff}, 9}, QCBOR_ERR_HIT_END },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001761 // Mixed nesting with definite unclosed
Laurence Lundbladeee851742020-01-08 08:37:05 -08001762 { {(uint8_t[]){0x9f, 0x82, 0x9f, 0x81, 0x9f, 0x9f, 0xff, 0xff, 0xff, 0xff}, 10}, QCBOR_ERR_BAD_BREAK },
Laurence Lundblade0a042a92020-06-12 14:09:50 -07001763 // TODO: a few more definite indefinite length combos and check with CBORbis.
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001764
1765
1766 // The "argument" for the data item is incomplete
1767 // Positive integer missing 1 byte argument
1768 { {(uint8_t[]){0x18}, 1}, QCBOR_ERR_HIT_END },
1769 // Positive integer missing 2 byte argument
1770 { {(uint8_t[]){0x19}, 1}, QCBOR_ERR_HIT_END },
1771 // Positive integer missing 4 byte argument
1772 { {(uint8_t[]){0x1a}, 1}, QCBOR_ERR_HIT_END },
1773 // Positive integer missing 8 byte argument
1774 { {(uint8_t[]){0x1b}, 1}, QCBOR_ERR_HIT_END },
1775 // Positive integer missing 1 byte of 2 byte argument
1776 { {(uint8_t[]){0x19, 0x01}, 2}, QCBOR_ERR_HIT_END },
1777 // Positive integer missing 2 bytes of 4 byte argument
1778 { {(uint8_t[]){0x1a, 0x01, 0x02}, 3}, QCBOR_ERR_HIT_END },
1779 // Positive integer missing 1 bytes of 7 byte argument
1780 { {(uint8_t[]){0x1b, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07}, 8}, QCBOR_ERR_HIT_END },
1781 // Negative integer missing 1 byte argument
1782 { {(uint8_t[]){0x38}, 1}, QCBOR_ERR_HIT_END },
1783 // Binary string missing 1 byte argument
1784 { {(uint8_t[]){0x58}, 1}, QCBOR_ERR_HIT_END },
1785 // Text string missing 1 byte argument
1786 { {(uint8_t[]){0x78}, 1}, QCBOR_ERR_HIT_END },
1787 // Array missing 1 byte argument
1788 { {(uint8_t[]){0x98}, 1}, QCBOR_ERR_HIT_END },
1789 // Map missing 1 byte argument
1790 { {(uint8_t[]){0xb8}, 1}, QCBOR_ERR_HIT_END },
1791 // Tag missing 1 byte argument
1792 { {(uint8_t[]){0xd8}, 1}, QCBOR_ERR_HIT_END },
1793 // Simple missing 1 byte argument
1794 { {(uint8_t[]){0xf8}, 1}, QCBOR_ERR_HIT_END },
1795
1796
1797 // Breaks must not occur in definite length arrays and maps
1798 // Array of length 1 with sole member replaced by a break
1799 { {(uint8_t[]){0x81, 0xff}, 2}, QCBOR_ERR_BAD_BREAK },
1800 // Array of length 2 with 2nd member replaced by a break
1801 { {(uint8_t[]){0x82, 0x00, 0xff}, 3}, QCBOR_ERR_BAD_BREAK },
1802 // Map of length 1 with sole member label replaced by a break
1803 { {(uint8_t[]){0xa1, 0xff}, 2}, QCBOR_ERR_BAD_BREAK },
1804 // Map of length 1 with sole member label replaced by break
1805 // Alternate representation that some decoders handle difference
1806 { {(uint8_t[]){0xa1, 0xff, 0x00}, 3}, QCBOR_ERR_BAD_BREAK },
1807 // Array of length 1 with 2nd member value replaced by a break
1808 { {(uint8_t[]){0xa1, 0x00, 0xff}, 3}, QCBOR_ERR_BAD_BREAK },
1809 // Map of length 2 with 2nd member replaced by a break
1810 { {(uint8_t[]){0xa2, 0x00, 0x00, 0xff}, 4}, QCBOR_ERR_BAD_BREAK },
1811
1812
1813 // Breaks must not occur on their own out of an indefinite length data item
1814 // A bare break is not well formed
1815 { {(uint8_t[]){0xff}, 1}, QCBOR_ERR_BAD_BREAK },
1816 // A bare break after a zero length definite length array
1817 { {(uint8_t[]){0x80, 0xff}, 2}, QCBOR_ERR_BAD_BREAK },
1818 // A bare break after a zero length indefinite length map
1819 { {(uint8_t[]){0x9f, 0xff, 0xff}, 3}, QCBOR_ERR_BAD_BREAK },
1820
1821
1822 // Forbidden two byte encodings of simple types
1823 // Must use 0xe0 instead
1824 { {(uint8_t[]){0xf8, 0x00}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1825 // Should use 0xe1 instead
1826 { {(uint8_t[]){0xf8, 0x01}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1827 // Should use 0xe2 instead
1828 { {(uint8_t[]){0xf8, 0x02}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1829 // Should use 0xe3 instead
1830 { {(uint8_t[]){0xf8, 0x03}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1831 // Should use 0xe4 instead
1832 { {(uint8_t[]){0xf8, 0x04}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1833 // Should use 0xe5 instead
1834 { {(uint8_t[]){0xf8, 0x05}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1835 // Should use 0xe6 instead
1836 { {(uint8_t[]){0xf8, 0x06}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1837 // Should use 0xe7 instead
1838 { {(uint8_t[]){0xf8, 0x07}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1839 // Should use 0xe8 instead
1840 { {(uint8_t[]){0xf8, 0x08}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1841 // Should use 0xe9 instead
1842 { {(uint8_t[]){0xf8, 0x09}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1843 // Should use 0xea instead
1844 { {(uint8_t[]){0xf8, 0x0a}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1845 // Should use 0xeb instead
1846 { {(uint8_t[]){0xf8, 0x0b}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1847 // Should use 0xec instead
1848 { {(uint8_t[]){0xf8, 0x0c}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1849 // Should use 0xed instead
1850 { {(uint8_t[]){0xf8, 0x0d}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1851 // Should use 0xee instead
1852 { {(uint8_t[]){0xf8, 0x0e}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1853 // Should use 0xef instead
1854 { {(uint8_t[]){0xf8, 0x0f}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1855 // Should use 0xf0 instead
1856 { {(uint8_t[]){0xf8, 0x10}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1857 // Should use 0xf1 instead
1858 { {(uint8_t[]){0xf8, 0x11}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1859 // Should use 0xf2 instead
1860 { {(uint8_t[]){0xf8, 0x12}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1861 // Must use 0xf3 instead
1862 { {(uint8_t[]){0xf8, 0x13}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1863 // Must use 0xf4 instead
1864 { {(uint8_t[]){0xf8, 0x14}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1865 // Must use 0xf5 instead
1866 { {(uint8_t[]){0xf8, 0x15}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1867 // Must use 0xf6 instead
1868 { {(uint8_t[]){0xf8, 0x16}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1869 // Must use 0xf7 instead
1870 { {(uint8_t[]){0xf8, 0x17}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1871 // Must use 0xf8 instead
1872 { {(uint8_t[]){0xf8, 0x18}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1873
1874
1875 // Integers with additional info indefinite length
1876 // Positive integer with additional info indefinite length
1877 { {(uint8_t[]){0x1f}, 1}, QCBOR_ERR_BAD_INT },
1878 // Negative integer with additional info indefinite length
1879 { {(uint8_t[]){0x3f}, 1}, QCBOR_ERR_BAD_INT },
1880 // CBOR tag with "argument" an indefinite length
1881 { {(uint8_t[]){0xdf, 0x00}, 2}, QCBOR_ERR_BAD_INT },
1882 // CBOR tag with "argument" an indefinite length alternate vector
1883 { {(uint8_t[]){0xdf}, 1}, QCBOR_ERR_BAD_INT },
1884
1885
1886 // Missing bytes from a deterministic length string
1887 // A byte string is of length 1 without the 1 byte
1888 { {(uint8_t[]){0x41}, 1}, QCBOR_ERR_HIT_END },
1889 // A text string is of length 1 without the 1 byte
1890 { {(uint8_t[]){0x61}, 1}, QCBOR_ERR_HIT_END },
Laurence Lundblade42272e42020-01-31 07:50:53 -08001891 // Byte string should have 2^32-15 bytes, but has one
1892 { {(uint8_t[]){0x5a, 0xff, 0xff, 0xff, 0xf0, 0x00}, 6}, QCBOR_ERR_HIT_END },
1893 // Byte string should have 2^32-15 bytes, but has one
1894 { {(uint8_t[]){0x7a, 0xff, 0xff, 0xff, 0xf0, 0x00}, 6}, QCBOR_ERR_HIT_END },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001895
1896
1897 // Use of unassigned additional information values
1898 // Major type positive integer with reserved value 28
1899 { {(uint8_t[]){0x1c}, 1}, QCBOR_ERR_UNSUPPORTED },
1900 // Major type positive integer with reserved value 29
1901 { {(uint8_t[]){0x1d}, 1}, QCBOR_ERR_UNSUPPORTED },
1902 // Major type positive integer with reserved value 30
1903 { {(uint8_t[]){0x1e}, 1}, QCBOR_ERR_UNSUPPORTED },
1904 // Major type negative integer with reserved value 28
1905 { {(uint8_t[]){0x3c}, 1}, QCBOR_ERR_UNSUPPORTED },
1906 // Major type negative integer with reserved value 29
1907 { {(uint8_t[]){0x3d}, 1}, QCBOR_ERR_UNSUPPORTED },
1908 // Major type negative integer with reserved value 30
1909 { {(uint8_t[]){0x3e}, 1}, QCBOR_ERR_UNSUPPORTED },
1910 // Major type byte string with reserved value 28 length
1911 { {(uint8_t[]){0x5c}, 1}, QCBOR_ERR_UNSUPPORTED },
1912 // Major type byte string with reserved value 29 length
1913 { {(uint8_t[]){0x5d}, 1}, QCBOR_ERR_UNSUPPORTED },
1914 // Major type byte string with reserved value 30 length
1915 { {(uint8_t[]){0x5e}, 1}, QCBOR_ERR_UNSUPPORTED },
1916 // Major type text string with reserved value 28 length
1917 { {(uint8_t[]){0x7c}, 1}, QCBOR_ERR_UNSUPPORTED },
1918 // Major type text string with reserved value 29 length
1919 { {(uint8_t[]){0x7d}, 1}, QCBOR_ERR_UNSUPPORTED },
1920 // Major type text string with reserved value 30 length
1921 { {(uint8_t[]){0x7e}, 1}, QCBOR_ERR_UNSUPPORTED },
1922 // Major type array with reserved value 28 length
1923 { {(uint8_t[]){0x9c}, 1}, QCBOR_ERR_UNSUPPORTED },
1924 // Major type array with reserved value 29 length
1925 { {(uint8_t[]){0x9d}, 1}, QCBOR_ERR_UNSUPPORTED },
1926 // Major type array with reserved value 30 length
1927 { {(uint8_t[]){0x9e}, 1}, QCBOR_ERR_UNSUPPORTED },
1928 // Major type map with reserved value 28 length
1929 { {(uint8_t[]){0xbc}, 1}, QCBOR_ERR_UNSUPPORTED },
1930 // Major type map with reserved value 29 length
1931 { {(uint8_t[]){0xbd}, 1}, QCBOR_ERR_UNSUPPORTED },
1932 // Major type map with reserved value 30 length
1933 { {(uint8_t[]){0xbe}, 1}, QCBOR_ERR_UNSUPPORTED },
1934 // Major type tag with reserved value 28 length
1935 { {(uint8_t[]){0xdc}, 1}, QCBOR_ERR_UNSUPPORTED },
1936 // Major type tag with reserved value 29 length
1937 { {(uint8_t[]){0xdd}, 1}, QCBOR_ERR_UNSUPPORTED },
1938 // Major type tag with reserved value 30 length
1939 { {(uint8_t[]){0xde}, 1}, QCBOR_ERR_UNSUPPORTED },
1940 // Major type simple with reserved value 28 length
1941 { {(uint8_t[]){0xfc}, 1}, QCBOR_ERR_UNSUPPORTED },
1942 // Major type simple with reserved value 29 length
1943 { {(uint8_t[]){0xfd}, 1}, QCBOR_ERR_UNSUPPORTED },
1944 // Major type simple with reserved value 30 length
1945 { {(uint8_t[]){0xfe}, 1}, QCBOR_ERR_UNSUPPORTED },
1946
1947
1948 // Maps must have an even number of data items (key & value)
1949 // Map with 1 item when it should have 2
1950 { {(uint8_t[]){0xa1, 0x00}, 2}, QCBOR_ERR_HIT_END },
1951 // Map with 3 item when it should have 4
1952 { {(uint8_t[]){0xa2, 0x00, 0x00, 0x00}, 2}, QCBOR_ERR_HIT_END },
1953 // Map with 1 item when it should have 2
1954 { {(uint8_t[]){0xbf, 0x00, 0xff}, 3}, QCBOR_ERR_BAD_BREAK },
1955 // Map with 3 item when it should have 4
1956 { {(uint8_t[]){0xbf, 0x00, 0x00, 0x00, 0xff}, 5}, QCBOR_ERR_BAD_BREAK },
1957
1958
1959 // In addition to not-well-formed, some invalid CBOR
Laurence Lundbladeee851742020-01-08 08:37:05 -08001960 // Text-based date, with an integer
1961 { {(uint8_t[]){0xc0, 0x00}, 2}, QCBOR_ERR_BAD_OPT_TAG },
1962 // Epoch date, with an byte string
1963 { {(uint8_t[]){0xc1, 0x41, 0x33}, 3}, QCBOR_ERR_BAD_OPT_TAG },
1964 // tagged as both epoch and string dates
1965 { {(uint8_t[]){0xc1, 0xc0, 0x00}, 3}, QCBOR_ERR_BAD_OPT_TAG },
1966 // big num tagged an int, not a byte string
1967 { {(uint8_t[]){0xc2, 0x00}, 2}, QCBOR_ERR_BAD_OPT_TAG },
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001968};
1969
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001970int32_t DecodeFailureTests()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001971{
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001972 int32_t nResult;
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001973
Laurence Lundblade59289e52019-12-30 13:44:37 -08001974 nResult = ProcessFailures(Failures, sizeof(Failures)/sizeof(struct FailInput));
1975 if(nResult) {
1976 return nResult;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001977 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001978
Laurence Lundblade3a6042e2019-06-28 19:58:04 -07001979 // Corrupt the UsefulInputBuf and see that
1980 // it reflected correctly for CBOR decoding
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001981 {
1982 QCBORDecodeContext DCtx;
Laurence Lundblade3a6042e2019-06-28 19:58:04 -07001983 QCBORItem Item;
1984 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001985
Laurence Lundblade3a6042e2019-06-28 19:58:04 -07001986 QCBORDecode_Init(&DCtx,
1987 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSimpleValues),
1988 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001989
1990 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001991 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001992 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
Laurence Lundblade3a6042e2019-06-28 19:58:04 -07001993 Item.val.uCount != 10) {
1994 // This wasn't supposed to happen
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001995 return -1;
Laurence Lundblade3a6042e2019-06-28 19:58:04 -07001996 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001997
Laurence Lundblade3a6042e2019-06-28 19:58:04 -07001998 DCtx.InBuf.magic = 0; // Reach in and corrupt the UsefulInputBuf
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001999
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002000 nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundblade3a6042e2019-06-28 19:58:04 -07002001 if(nCBORError != QCBOR_ERR_HIT_END) {
2002 // Did not get back the error expected
2003 return -2;
2004 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002005 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002006
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002007/*
2008 This test is disabled until QCBOREncode_EncodeHead() is brought in so
2009 the size encoded can be tied to SIZE_MAX and work for all size CPUs.
2010
2011 This relies on the largest string allowed being SIZE_MAX -4 rather than
2012 SIZE_MAX. That way the test can be performed.
2013 {
2014 QCBORDecodeContext DCtx;
2015 QCBORItem Item;
2016
2017 static uint8_t foo[] = {0x5b, 0xff, 0xff, 0xff, 0xff,
2018 0xff, 0xff, 0xff, 0xff};
2019
2020 QCBORDecode_Init(&DCtx,
2021 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(foo),
2022 QCBOR_DECODE_MODE_NORMAL);
2023
2024 if(QCBOR_ERR_STRING_TOO_LONG != QCBORDecode_GetNext(&DCtx, &Item)) {
2025 return -4;
2026 }
2027 }
2028*/
2029
Laurence Lundblade3a6042e2019-06-28 19:58:04 -07002030 return 0;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002031}
2032
2033
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002034/* Try all 256 values of the byte at nLen including recursing for
2035 each of the values to try values at nLen+1 ... up to nLenMax
2036 */
Laurence Lundblade06350ea2020-01-27 19:32:40 -08002037static void ComprehensiveInputRecurser(uint8_t *pBuf, size_t nLen, size_t nLenMax)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002038{
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002039 if(nLen >= nLenMax) {
2040 return;
2041 }
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08002042
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002043 for(int inputByte = 0; inputByte < 256; inputByte++) {
2044 // Set up the input
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002045 pBuf[nLen] = (uint8_t)inputByte;
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08002046 const UsefulBufC Input = {pBuf, nLen+1};
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08002047
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002048 // Get ready to parse
2049 QCBORDecodeContext DCtx;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002050 QCBORDecode_Init(&DCtx, Input, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002051
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002052 // Parse by getting the next item until an error occurs
2053 // Just about every possible decoder error can occur here
2054 // The goal of this test is not to check for the correct
2055 // error since that is not really possible. It is to
2056 // see that there is no crash on hostile input.
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002057 while(1) {
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002058 QCBORItem Item;
2059 QCBORError nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002060 if(nCBORError != QCBOR_SUCCESS) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002061 break;
2062 }
2063 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002064
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002065 ComprehensiveInputRecurser(pBuf, nLen+1, nLenMax);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002066 }
2067}
2068
2069
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002070int32_t ComprehensiveInputTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002071{
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002072 // Size 2 tests 64K inputs and runs quickly
2073 uint8_t pBuf[2];
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002074
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002075 ComprehensiveInputRecurser(pBuf, 0, sizeof(pBuf));
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002076
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002077 return 0;
2078}
2079
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002080
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002081int32_t BigComprehensiveInputTest()
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002082{
2083 // size 3 tests 16 million inputs and runs OK
2084 // in seconds on fast machines. Size 4 takes
2085 // 10+ minutes and 5 half a day on fast
2086 // machines. This test is kept separate from
2087 // the others so as to no slow down the use
2088 // of them as a very frequent regression.
2089 uint8_t pBuf[3]; //
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08002090
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002091 ComprehensiveInputRecurser(pBuf, 0, sizeof(pBuf));
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08002092
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002093 return 0;
2094}
2095
2096
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002097static uint8_t spDateTestInput[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002098 0xc0, // tag for string date
2099 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002100
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002101 0xc1, // tag for epoch date
2102 0x1a, 0x53, 0x72, 0x4E, 0x00, // Epoch date 1400000000; Tue, 13 May 2014 16:53:20 GMT
2103
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002104 // CBOR_TAG_B64
2105 0xc1, 0xcf, 0xd8, 0x22, // 0xee, // Epoch date with extra tags TODO: fix this test
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002106 0x1a, 0x53, 0x72, 0x4E, 0x01,
2107
2108 0xc1, // tag for epoch date
2109 0x1b, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, // Too large integer
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002110
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002111 0xc1, // tag for epoch date
2112 0xfa, 0x3f, 0x8c, 0xcc, 0xcd, // double with value 1.1
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002113
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002114 0xc1, // tag for epoch date
Laurence Lundbladea1ad8782019-11-08 00:12:11 -08002115 0xfa, 0x7f, 0x7f, 0xff, 0xff, // 3.4028234663852886e+38 too large
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002116
Laurence Lundbladea1ad8782019-11-08 00:12:11 -08002117 0xc1, // tag for epoch date
2118 0xfb, 0x43, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 9223372036854775808.000000 just barely too large
2119 //0xfa, 0x7f, 0x7f, 0xff, 0xff // 3.4028234663852886e+38 too large
2120
2121 0xc1, // tag for epoch date
2122 0xfb, 0x43, 0xdf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe // 9223372036854773760 largest supported
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002123};
2124
2125
2126// have to check float expected only to within an epsilon
2127int CHECK_EXPECTED_DOUBLE(double val, double expected) {
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002128
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002129 double diff = val - expected;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002130
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002131 diff = fabs(diff);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002132
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002133 return diff > 0.0000001;
2134}
2135
2136
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002137int32_t DateParseTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002138{
2139 QCBORDecodeContext DCtx;
2140 QCBORItem Item;
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07002141 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002142
Laurence Lundbladeee851742020-01-08 08:37:05 -08002143 QCBORDecode_Init(&DCtx,
2144 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spDateTestInput),
2145 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002146
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002147 const uint64_t uTags[] = {15};
2148 QCBORTagListIn TagList = {1, uTags};
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002149
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002150 QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TagList);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002151
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002152 // String date
2153 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
2154 return -1;
2155 if(Item.uDataType != QCBOR_TYPE_DATE_STRING ||
Laurence Lundblade9e3651c2018-10-10 11:49:55 +08002156 UsefulBuf_Compare(Item.val.dateString, UsefulBuf_FromSZ("1985-04-12"))){
Laurence Lundblade67bd5512018-11-02 21:44:06 +07002157 return -2;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002158 }
2159
2160 // Epoch date
2161 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade67bd5512018-11-02 21:44:06 +07002162 return -3;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002163 if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH ||
2164 Item.val.epochDate.nSeconds != 1400000000 ||
2165 Item.val.epochDate.fSecondsFraction != 0 ) {
Laurence Lundblade67bd5512018-11-02 21:44:06 +07002166 return -4;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002167 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002168
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002169 // Epoch date with extra CBOR_TAG_B64 tag that doesn't really mean anything
2170 // but want to be sure extra tag doesn't cause a problem
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002171 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade67bd5512018-11-02 21:44:06 +07002172 return -5;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002173 if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH ||
2174 Item.val.epochDate.nSeconds != 1400000001 ||
2175 Item.val.epochDate.fSecondsFraction != 0 ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002176 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_B64)) {
Laurence Lundblade67bd5512018-11-02 21:44:06 +07002177 return -6;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002178 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002179
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002180 // Epoch date that is too large for our representation
2181 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_DATE_OVERFLOW) {
Laurence Lundblade67bd5512018-11-02 21:44:06 +07002182 return -7;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002183 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002184
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002185 // Epoch date in float format with fractional seconds
2186 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade67bd5512018-11-02 21:44:06 +07002187 return -8;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002188 if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH ||
2189 Item.val.epochDate.nSeconds != 1 ||
2190 CHECK_EXPECTED_DOUBLE(Item.val.epochDate.fSecondsFraction, 0.1 )) {
Laurence Lundblade67bd5512018-11-02 21:44:06 +07002191 return -9;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002192 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002193
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002194 // Epoch date float that is too large for our representation
2195 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_DATE_OVERFLOW) {
Laurence Lundblade67bd5512018-11-02 21:44:06 +07002196 return -10;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002197 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002198
Laurence Lundbladea1ad8782019-11-08 00:12:11 -08002199 // Epoch date double that is just slightly too large
2200 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_DATE_OVERFLOW) {
2201 return -11;
2202 }
2203
2204 // Largest double epoch date supported
2205 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_SUCCESS ||
2206 Item.uDataType != QCBOR_TYPE_DATE_EPOCH ||
2207 Item.val.epochDate.nSeconds != 9223372036854773760 ||
2208 Item.val.epochDate.nSeconds == 0) {
2209 return -12;
2210 }
Laurence Lundbladeee851742020-01-08 08:37:05 -08002211 // TODO: could use a few more tests with float, double, and half precsion
2212 // and negative (but coverage is still pretty good)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002213
2214 return 0;
2215}
2216
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002217// Really simple basic input for tagging test
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002218static uint8_t spOptTestInput[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002219 0xd9, 0xd9, 0xf7, // CBOR magic number
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002220 0x81, // Array of one
2221 0xd8, 0x04, // non-preferred serialization of tag 4
2222 0x82, 0x01, 0x03}; // fraction 1/3
2223
Laurence Lundblade59289e52019-12-30 13:44:37 -08002224/*
2225 DB 9192939495969798 # tag(10489608748473423768)
2226 80 # array(0)
2227 */
Laurence Lundbladeee851742020-01-08 08:37:05 -08002228static uint8_t spEncodedLargeTag[] = {0xdb, 0x91, 0x92, 0x93, 0x94, 0x95,
2229 0x96, 0x97, 0x98, 0x80};
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002230
Laurence Lundblade59289e52019-12-30 13:44:37 -08002231/*
2232DB 9192939495969798 # tag(10489608748473423768)
2233 D8 88 # tag(136)
2234 C6 # tag(6)
2235 C7 # tag(7)
2236 80 # array(0)
2237*/
Laurence Lundbladeee851742020-01-08 08:37:05 -08002238static uint8_t spLotsOfTags[] = {0xdb, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96,
2239 0x97, 0x98, 0xd8, 0x88, 0xc6, 0xc7, 0x80};
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002240
2241/*
2242 The cbor.me parse of this.
2243 55799(55799(55799({6(7(-23)): 5859837686836516696(7({7(-20): 11({17(-18): 17(17(17("Organization"))),
Laurence Lundblade59289e52019-12-30 13:44:37 -08002244 9(-17): 773("SSG"), -15: 16(17(6(7(8(9(10(11(12(13(14(15("Confusion")))))))))))), 17(-16): 17("San Diego"),
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002245 17(-14): 17("US")}), 23(-19): 19({-11: 9({-9: -7}),
2246 90599561(90599561(90599561(-10))): 12(h'0102030405060708090A')})})),
2247 16(-22): 23({11(8(7(-5))): 8(-3)})})))
2248 */
2249static uint8_t spCSRWithTags[] = {
2250 0xd9, 0xd9, 0xf7, 0xd9, 0xd9, 0xf7, 0xd9, 0xd9, 0xf7, 0xa2,
2251 0xc6, 0xc7, 0x36,
2252 0xdb, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0xc7, 0xa2,
2253 0xda, 0x00, 0x00, 0x00, 0x07, 0x33,
2254 0xcb, 0xa5,
2255 0xd1, 0x31,
2256 0xd1, 0xd1, 0xd1, 0x6c,
2257 0x4f, 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74, 0x69, 0x6f, 0x6e,
2258 0xc9, 0x30,
2259 0xd9, 0x03, 0x05, 0x63,
2260 0x53, 0x53, 0x47,
2261 0x2e,
Laurence Lundblade59289e52019-12-30 13:44:37 -08002262 0xd0, 0xd1, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf, 0x69,
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002263 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73, 0x69, 0x6f, 0x6e,
2264 0xd1, 0x2f,
2265 0xd1, 0x69,
2266 0x53, 0x61, 0x6e, 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f,
2267 0xd1, 0x2d,
2268 0xd1, 0x62,
2269 0x55, 0x53,
2270 0xd7, 0x32,
2271 0xd3, 0xa2,
2272 0x2a,
2273 0xc9, 0xa1,
2274 0x28,
2275 0x26,
2276 0xda, 0x05, 0x66, 0x70, 0x89, 0xda, 0x05, 0x66, 0x70, 0x89, 0xda, 0x05, 0x66, 0x70, 0x89, 0x29,
2277 0xcc, 0x4a,
2278 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,0x07, 0x08, 0x09, 0x0a,
2279 0xd0, 0x35,
2280 0xd7, 0xa1,
2281 0xcb, 0xc8, 0xc7, 0x24,
2282 0xc8, 0x22};
2283
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002284static int32_t CheckCSRMaps(QCBORDecodeContext *pDC);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002285
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002286
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002287int32_t OptTagParseTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002288{
2289 QCBORDecodeContext DCtx;
2290 QCBORItem Item;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002291
Laurence Lundbladeee851742020-01-08 08:37:05 -08002292 QCBORDecode_Init(&DCtx,
2293 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spOptTestInput),
2294 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002295
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002296 //-------------------------
2297 // This text matches the magic number tag and the fraction tag
2298 if(QCBORDecode_GetNext(&DCtx, &Item)) {
2299 return -2;
2300 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002301 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002302 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC)) {
2303 return -3;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002304 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002305
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002306 if(QCBORDecode_GetNext(&DCtx, &Item)) {
2307 return -4;
2308 }
Laurence Lundblade59289e52019-12-30 13:44:37 -08002309
2310#ifdef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002311 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
Laurence Lundblade59289e52019-12-30 13:44:37 -08002312 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_DECIMAL_FRACTION) ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002313 Item.val.uCount != 2) {
2314 return -5;
2315 }
Laurence Lundblade59289e52019-12-30 13:44:37 -08002316#else
2317 if(Item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION) {
2318 return -6;
2319 }
2320#endif
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002321
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002322 // --------------------------------
2323 // This test decodes the very large tag, but it is not in
2324 // any list so it is ignored.
Laurence Lundbladeee851742020-01-08 08:37:05 -08002325 QCBORDecode_Init(&DCtx,
2326 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEncodedLargeTag),
2327 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002328 if(QCBORDecode_GetNext(&DCtx, &Item)) {
2329 return -6;
2330 }
Laurence Lundblade830fbf92020-05-31 17:22:33 -07002331 /*
2332 if(Item.uTagBits) { // TODO: make sure it is OK to remove this
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002333 return -7;
Laurence Lundblade830fbf92020-05-31 17:22:33 -07002334 }*/
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002335
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002336 // ----------------------------------
Laurence Lundbladeee851742020-01-08 08:37:05 -08002337 // This test sets up a caller-config list that includes the very large
2338 // tage and then matches it.
2339 QCBORDecode_Init(&DCtx,
2340 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEncodedLargeTag),
2341 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002342 const uint64_t puList[] = {0x9192939495969798, 257};
2343 const QCBORTagListIn TL = {2, puList};
2344 QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002345
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002346 if(QCBORDecode_GetNext(&DCtx, &Item)) {
2347 return -8;
2348 }
2349 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
2350 !QCBORDecode_IsTagged(&DCtx, &Item, 0x9192939495969798) ||
2351 QCBORDecode_IsTagged(&DCtx, &Item, 257) ||
2352 QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_BIGFLOAT) ||
2353 Item.val.uCount != 0) {
2354 return -9;
2355 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002356
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002357 //------------------------
Laurence Lundbladeee851742020-01-08 08:37:05 -08002358 // Sets up a caller-configured list and look up something not in it
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002359 const uint64_t puLongList[17] = {1,2,1};
2360 const QCBORTagListIn TLLong = {17, puLongList};
Laurence Lundbladeee851742020-01-08 08:37:05 -08002361 QCBORDecode_Init(&DCtx,
2362 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEncodedLargeTag),
2363 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002364 QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TLLong);
2365 if(QCBORDecode_GetNext(&DCtx, &Item)) {
2366 return -11;
2367 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002368
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002369 // -----------------------
2370 // This tests retrievel of the full tag list
Laurence Lundbladeee851742020-01-08 08:37:05 -08002371 QCBORDecode_Init(&DCtx,
2372 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spLotsOfTags),
2373 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002374 uint64_t puTags[16];
2375 QCBORTagListOut Out = {0, 4, puTags};
2376 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2377 return -12;
2378 }
2379 if(puTags[0] != 0x9192939495969798 ||
2380 puTags[1] != 0x88 ||
Laurence Lundblade59289e52019-12-30 13:44:37 -08002381 puTags[2] != 0x06 ||
2382 puTags[3] != 0x07) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002383 return -13;
2384 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002385
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002386 // ----------------------
2387 // This text if too small of an out list
Laurence Lundbladeee851742020-01-08 08:37:05 -08002388 QCBORDecode_Init(&DCtx,
2389 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spLotsOfTags),
2390 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002391 QCBORTagListOut OutSmall = {0, 3, puTags};
2392 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &OutSmall) != QCBOR_ERR_TOO_MANY_TAGS) {
2393 return -14;
2394 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002395
Laurence Lundblade830fbf92020-05-31 17:22:33 -07002396#if 0
2397 // TODO: this test needs to be re evaluated
2398
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002399 // ---------------
2400 // Parse a version of the "CSR" that has had a ton of tags randomly inserted
Laurence Lundbladeee851742020-01-08 08:37:05 -08002401 QCBORDecode_Init(&DCtx,
2402 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRWithTags),
2403 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002404 int n = CheckCSRMaps(&DCtx);
2405 if(n) {
2406 return n-2000;
2407 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002408
Laurence Lundblade59289e52019-12-30 13:44:37 -08002409 Out = (QCBORTagListOut){0, 16, puTags};
Laurence Lundbladeee851742020-01-08 08:37:05 -08002410 QCBORDecode_Init(&DCtx,
2411 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRWithTags),
2412 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002413
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002414 const uint64_t puTagList[] = {773, 1, 90599561};
2415 const QCBORTagListIn TagList = {3, puTagList};
2416 QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TagList);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002417
2418
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002419 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2420 return -100;
2421 }
2422 if(Item.uDataType != QCBOR_TYPE_MAP ||
2423 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC) ||
2424 QCBORDecode_IsTagged(&DCtx, &Item, 90599561) ||
2425 QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_DATE_EPOCH) ||
2426 Item.val.uCount != 2 ||
2427 puTags[0] != CBOR_TAG_CBOR_MAGIC ||
2428 puTags[1] != CBOR_TAG_CBOR_MAGIC ||
2429 puTags[2] != CBOR_TAG_CBOR_MAGIC ||
2430 Out.uNumUsed != 3) {
2431 return -101;
2432 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002433
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002434 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2435 return -102;
2436 }
2437 if(Item.uDataType != QCBOR_TYPE_MAP ||
2438 QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC) ||
2439 QCBORDecode_IsTagged(&DCtx, &Item, 6) ||
2440 QCBORDecode_IsTagged(&DCtx, &Item, 7) || // item is tagged 7, but 7 is not configured to be recognized
2441 Item.val.uCount != 2 ||
2442 puTags[0] != 5859837686836516696 ||
2443 puTags[1] != 7 ||
2444 Out.uNumUsed != 2) {
2445 return -103;
2446 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002447
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002448 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2449 return -104;
2450 }
2451 if(Item.uDataType != QCBOR_TYPE_MAP ||
2452 Item.uTagBits ||
2453 Item.val.uCount != 5 ||
2454 puTags[0] != 0x0b ||
2455 Out.uNumUsed != 1) {
2456 return -105;
2457 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002458
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002459 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2460 return -106;
2461 }
2462 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
2463 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_COSE_MAC0) ||
2464 Item.val.string.len != 12 ||
2465 puTags[0] != CBOR_TAG_COSE_MAC0 ||
2466 puTags[1] != CBOR_TAG_COSE_MAC0 ||
2467 puTags[2] != CBOR_TAG_COSE_MAC0 ||
2468 Out.uNumUsed != 3) {
2469 return -105;
2470 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002471
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002472 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2473 return -107;
2474 }
2475 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
2476 !QCBORDecode_IsTagged(&DCtx, &Item, 773) ||
2477 Item.val.string.len != 3 ||
2478 puTags[0] != 773 ||
2479 Out.uNumUsed != 1) {
2480 return -108;
2481 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002482
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002483 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2484 return -109;
2485 }
2486 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade59289e52019-12-30 13:44:37 -08002487 !QCBORDecode_IsTagged(&DCtx, &Item, 16) ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002488 Item.val.string.len != 9 ||
Laurence Lundblade59289e52019-12-30 13:44:37 -08002489 puTags[0] != 16 ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002490 puTags[11] != 0x0f ||
2491 Out.uNumUsed != 12) {
2492 return -110;
2493 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002494
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002495 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2496 return -111;
2497 }
2498 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
2499 !QCBORDecode_IsTagged(&DCtx, &Item, 17) ||
2500 Item.val.string.len != 9 ||
2501 puTags[0] != 17 ||
2502 Out.uNumUsed != 1) {
2503 return -112;
2504 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002505
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002506 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2507 return -111;
2508 }
2509 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
2510 !QCBORDecode_IsTagged(&DCtx, &Item, 17) ||
2511 Item.val.string.len != 2 ||
2512 puTags[0] != 17 ||
2513 Out.uNumUsed != 1) {
2514 return -112;
2515 }
2516
2517 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2518 return -113;
2519 }
2520 if(Item.uDataType != QCBOR_TYPE_MAP ||
2521 QCBORDecode_IsTagged(&DCtx, &Item, 19) ||
2522 Item.val.uCount != 2 ||
2523 puTags[0] != 19 ||
2524 Out.uNumUsed != 1) {
2525 return -114;
2526 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002527
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002528 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2529 return -115;
2530 }
2531 if(Item.uDataType != QCBOR_TYPE_MAP ||
2532 QCBORDecode_IsTagged(&DCtx, &Item, 9) ||
2533 Item.uTagBits ||
2534 Item.val.uCount != 1 ||
2535 puTags[0] != 9 ||
2536 Out.uNumUsed != 1) {
2537 return -116;
2538 }
2539
2540 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2541 return -116;
2542 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002543 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002544 Item.val.int64 != -7 ||
2545 Item.uTagBits ||
2546 Out.uNumUsed != 0) {
2547 return -117;
2548 }
2549
2550 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2551 return -118;
2552 }
2553 if(Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
2554 Item.val.string.len != 10 ||
2555 Item.uTagBits ||
2556 puTags[0] != 12 ||
2557 Out.uNumUsed != 1) {
2558 return -119;
2559 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002560
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002561 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2562 return -120;
2563 }
2564 if(Item.uDataType != QCBOR_TYPE_MAP ||
2565 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_ENC_AS_B16) ||
2566 Item.val.uCount != 1 ||
2567 puTags[0] != 0x17 ||
2568 Out.uNumUsed != 1) {
2569 return -121;
2570 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002571
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002572 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2573 return -122;
2574 }
2575 if(Item.uDataType != QCBOR_TYPE_INT64 ||
2576 QCBORDecode_IsTagged(&DCtx, &Item, 8) ||
2577 Item.val.int64 != -3 ||
2578 puTags[0] != 8 ||
2579 Out.uNumUsed != 1) {
2580 return -123;
2581 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002582
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002583 if(QCBORDecode_Finish(&DCtx)) {
2584 return -124;
2585 }
Laurence Lundblade830fbf92020-05-31 17:22:33 -07002586#else
2587 (void)spCSRWithTags;
2588#endif
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002589 return 0;
2590}
2591
2592
2593
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002594
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002595static uint8_t spBigNumInput[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002596 0x83,
2597 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2598 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2599 0xA4,
2600 0x63, 0x42, 0x4E, 0x2B,
2601 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2602 0x18, 0x40,
2603 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2604 0x63, 0x42, 0x4E, 0x2D,
2605 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2606 0x38, 0x3F,
2607 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
2608
2609
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002610static uint8_t spBigNum[] = {0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002611
2612
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002613int32_t BignumParseTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002614{
2615 QCBORDecodeContext DCtx;
2616 QCBORItem Item;
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07002617 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002618
Laurence Lundbladeee851742020-01-08 08:37:05 -08002619 QCBORDecode_Init(&DCtx,
2620 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNumInput),
2621 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002622
2623
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002624 //
2625 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
2626 return -1;
2627 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade830fbf92020-05-31 17:22:33 -07002628 return -2;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002629 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002630
2631 //
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002632 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07002633 return -3;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002634 if(Item.uDataType != QCBOR_TYPE_POSBIGNUM ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002635 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07002636 return -4;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002637 }
2638
2639 //
2640 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07002641 return -5;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002642 if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002643 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07002644 return -6;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002645 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002646
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002647 //
2648 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07002649 return -7;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002650 if(Item.uDataType != QCBOR_TYPE_MAP) {
Laurence Lundblade830fbf92020-05-31 17:22:33 -07002651 return -8;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002652 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002653
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002654 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07002655 return -9;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002656 if(Item.uDataType != QCBOR_TYPE_POSBIGNUM ||
2657 Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002658 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07002659 return -10;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002660 }
2661
2662 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07002663 return -11;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002664 if(Item.uDataType != QCBOR_TYPE_POSBIGNUM ||
2665 Item.uLabelType != QCBOR_TYPE_INT64 ||
2666 Item.label.int64 != 64 ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002667 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07002668 return -12;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002669 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002670
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002671 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07002672 return -13;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002673 if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM ||
2674 Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002675 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07002676 return -14;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002677 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002678
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002679 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07002680 return -15;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002681 if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM ||
2682 Item.uLabelType != QCBOR_TYPE_INT64 ||
2683 Item.label.int64 != -64 ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002684 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07002685 return -16;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002686 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002687
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002688 return 0;
2689}
2690
2691
2692
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002693static int32_t CheckItemWithIntLabel(QCBORDecodeContext *pCtx,
Laurence Lundbladeee851742020-01-08 08:37:05 -08002694 uint8_t uDataType,
2695 uint8_t uNestingLevel,
2696 uint8_t uNextNest,
2697 int64_t nLabel,
2698 QCBORItem *pItem)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002699{
2700 QCBORItem Item;
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07002701 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002702
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002703 if((nCBORError = QCBORDecode_GetNext(pCtx, &Item))) return -1;
2704 if(Item.uDataType != uDataType) return -1;
2705 if(uNestingLevel > 0) {
Laurence Lundbladeee851742020-01-08 08:37:05 -08002706 if(Item.uLabelType != QCBOR_TYPE_INT64 &&
2707 Item.uLabelType != QCBOR_TYPE_UINT64) {
2708 return -1;
2709 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002710 if(Item.uLabelType == QCBOR_TYPE_INT64) {
2711 if(Item.label.int64 != nLabel) return -1;
2712 } else {
Laurence Lundblade570fab52018-10-13 18:28:27 +08002713 if(Item.label.uint64 != (uint64_t)nLabel) return -1;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002714 }
2715 }
2716 if(Item.uNestingLevel != uNestingLevel) return -1;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302717 if(Item.uNextNestLevel != uNextNest) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002718
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002719 if(pItem) {
2720 *pItem = Item;
2721 }
2722 return 0;
2723}
2724
2725
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002726// Same code checks definite and indefinite length versions of the map
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002727static int32_t CheckCSRMaps(QCBORDecodeContext *pDC)
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002728{
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302729 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 0, 1, 0, NULL)) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002730
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302731 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 1, 2, -23, NULL)) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002732
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302733 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 2, 3, -20, NULL)) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002734
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302735 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -18, NULL)) return -1;
2736 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -17, NULL)) return -1;
2737 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -15, NULL)) return -1;
2738 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -16, NULL)) return -1;
2739 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 2, -14, NULL)) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002740
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302741 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 2, 3, -19, NULL)) return -1;
2742 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 3, 4, -11, NULL)) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002743
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302744 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_INT64, 4, 3, -9, NULL)) return -1;
2745 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_BYTE_STRING, 3, 1, -10, NULL)) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002746
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302747 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 1, 2, -22, NULL)) return -1;
2748 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_INT64, 2, 0, -5, NULL)) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002749
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002750 if(QCBORDecode_Finish(pDC)) return -2;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002751
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002752 return 0;
2753}
2754
2755
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002756/*
2757// cbor.me decoded output
2758{
2759 -23: {
2760 -20: {
2761 -18: "Organization",
2762 -17: "SSG",
2763 -15: "Confusion",
2764 -16: "San Diego",
2765 -14: "US"
2766 },
2767 -19: {
2768 -11: {
2769 -9: -7
2770 },
2771 -10: '\u0001\u0002\u0003\u0004\u0005\u0006\a\b\t\n'
2772 }
2773 },
2774 -22: {
2775 -5: -3
2776 }
2777}
2778 */
2779
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002780
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002781static uint8_t spCSRInput[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002782 0xa2, 0x36, 0xa2, 0x33, 0xa5, 0x31, 0x6c, 0x4f,
2783 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74,
2784 0x69, 0x6f, 0x6e, 0x30, 0x63, 0x53, 0x53, 0x47,
2785 0x2e, 0x69, 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73,
2786 0x69, 0x6f, 0x6e, 0x2f, 0x69, 0x53, 0x61, 0x6e,
2787 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f, 0x2d, 0x62,
2788 0x55, 0x53, 0x32, 0xa2, 0x2a, 0xa1, 0x28, 0x26,
2789 0x29, 0x4a, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
2790 0x07, 0x08, 0x09, 0x0a, 0x35, 0xa1, 0x24, 0x22};
2791
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002792int32_t NestedMapTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002793{
2794 QCBORDecodeContext DCtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002795
Laurence Lundbladeee851742020-01-08 08:37:05 -08002796 QCBORDecode_Init(&DCtx,
2797 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput),
2798 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002799
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002800 return CheckCSRMaps(&DCtx);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002801}
2802
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08002803
2804
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002805int32_t StringDecoderModeFailTest()
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08002806{
2807 QCBORDecodeContext DCtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002808
Laurence Lundbladeee851742020-01-08 08:37:05 -08002809 QCBORDecode_Init(&DCtx,
2810 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput),
2811 QCBOR_DECODE_MODE_MAP_STRINGS_ONLY);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002812
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08002813 QCBORItem Item;
2814 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002815
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08002816 if(QCBORDecode_GetNext(&DCtx, &Item)) {
2817 return -1;
2818 }
2819 if(Item.uDataType != QCBOR_TYPE_MAP) {
2820 return -2;
2821 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002822
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08002823 nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
2824 if(nCBORError != QCBOR_ERR_MAP_LABEL_TYPE) {
2825 return -3;
2826 }
2827
2828 return 0;
2829}
2830
2831
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002832// Same map as above, but using indefinite lengths
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002833static uint8_t spCSRInputIndefLen[] = {
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002834 0xbf, 0x36, 0xbf, 0x33, 0xbf, 0x31, 0x6c, 0x4f,
2835 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74,
2836 0x69, 0x6f, 0x6e, 0x30, 0x63, 0x53, 0x53, 0x47,
2837 0x2e, 0x69, 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73,
2838 0x69, 0x6f, 0x6e, 0x2f, 0x69, 0x53, 0x61, 0x6e,
2839 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f, 0x2d, 0x62,
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002840 0x55, 0x53, 0xff, 0x32, 0xbf, 0x2a, 0xbf, 0x28,
2841 0x26, 0xff, 0x29, 0x4a, 0x01, 0x02, 0x03, 0x04,
2842 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0xff, 0xff,
2843 0x35, 0xbf, 0x24, 0x22, 0xff, 0xff};
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002844
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002845int32_t NestedMapTestIndefLen()
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002846{
2847 QCBORDecodeContext DCtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002848
Laurence Lundbladeee851742020-01-08 08:37:05 -08002849 QCBORDecode_Init(&DCtx,
2850 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInputIndefLen),
2851 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002852
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002853 return CheckCSRMaps(&DCtx);
2854}
2855
2856
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002857
Laurence Lundblade17ede402018-10-13 11:43:07 +08002858static UsefulBufC make_nested_indefinite_arrays(int n, UsefulBuf Storage)
2859{
2860 UsefulOutBuf UOB;
2861 UsefulOutBuf_Init(&UOB, Storage);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002862
Laurence Lundblade17ede402018-10-13 11:43:07 +08002863 int i;
2864 for(i = 0; i < n; i++) {
2865 UsefulOutBuf_AppendByte(&UOB, 0x9f);
2866 }
2867
2868 for(i = 0; i < n; i++) {
2869 UsefulOutBuf_AppendByte(&UOB, 0xff);
2870 }
2871 return UsefulOutBuf_OutUBuf(&UOB);
2872}
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002873
2874
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002875static int32_t parse_indeflen_nested(UsefulBufC Nested, int nNestLevel)
Laurence Lundblade17ede402018-10-13 11:43:07 +08002876{
2877 QCBORDecodeContext DC;
2878 QCBORDecode_Init(&DC, Nested, 0);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002879
Laurence Lundblade17ede402018-10-13 11:43:07 +08002880 int j;
2881 for(j = 0; j < nNestLevel; j++) {
2882 QCBORItem Item;
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07002883 QCBORError nReturn = QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade17ede402018-10-13 11:43:07 +08002884 if(j >= QCBOR_MAX_ARRAY_NESTING) {
2885 // Should be in error
2886 if(nReturn != QCBOR_ERR_ARRAY_NESTING_TOO_DEEP) {
2887 return -4;
2888 } else {
2889 return 0; // Decoding doesn't recover after an error
2890 }
2891 } else {
2892 // Should be no error
2893 if(nReturn) {
2894 return -9; // Should not have got an error
2895 }
2896 }
2897 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
2898 return -7;
2899 }
2900 }
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07002901 QCBORError nReturn = QCBORDecode_Finish(&DC);
Laurence Lundblade17ede402018-10-13 11:43:07 +08002902 if(nReturn) {
2903 return -3;
2904 }
2905 return 0;
2906}
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002907
2908
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002909int32_t IndefiniteLengthNestTest()
Laurence Lundblade17ede402018-10-13 11:43:07 +08002910{
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05302911 UsefulBuf_MAKE_STACK_UB(Storage, 50);
Laurence Lundblade17ede402018-10-13 11:43:07 +08002912 int i;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002913 for(i=1; i < QCBOR_MAX_ARRAY_NESTING+4; i++) {
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08002914 const UsefulBufC Nested = make_nested_indefinite_arrays(i, Storage);
Laurence Lundblade17ede402018-10-13 11:43:07 +08002915 int nReturn = parse_indeflen_nested(Nested, i);
2916 if(nReturn) {
2917 return nReturn;
2918 }
2919 }
2920 return 0;
2921}
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002922
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002923
Laurence Lundbladeee851742020-01-08 08:37:05 -08002924// [1, [2, 3]]
2925static const uint8_t spIndefiniteArray[] = {0x9f, 0x01, 0x82, 0x02, 0x03, 0xff};
2926// No closing break
2927static const uint8_t spIndefiniteArrayBad1[] = {0x9f};
2928// Not enough closing breaks
2929static const uint8_t spIndefiniteArrayBad2[] = {0x9f, 0x9f, 0x02, 0xff};
2930// Too many closing breaks
2931static const uint8_t spIndefiniteArrayBad3[] = {0x9f, 0x02, 0xff, 0xff};
2932// Unclosed indeflen inside def len
2933static const uint8_t spIndefiniteArrayBad4[] = {0x81, 0x9f};
2934// confused tag
2935static const uint8_t spIndefiniteArrayBad5[] = {0x9f, 0xd1, 0xff};
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002936
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002937int32_t IndefiniteLengthArrayMapTest()
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002938{
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07002939 QCBORError nResult;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002940 // --- first test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002941 UsefulBufC IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArray);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002942
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002943 // Decode it and see if it is OK
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05302944 UsefulBuf_MAKE_STACK_UB(MemPool, 150);
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002945 QCBORDecodeContext DC;
2946 QCBORItem Item;
2947 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002948
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002949 QCBORDecode_SetMemPool(&DC, MemPool, false);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002950
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002951 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05302952
2953 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
2954 Item.uNestingLevel != 0 ||
2955 Item.uNextNestLevel != 1) {
2956 return -111;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002957 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002958
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002959 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05302960 if(Item.uDataType != QCBOR_TYPE_INT64 ||
2961 Item.uNestingLevel != 1 ||
2962 Item.uNextNestLevel != 1) {
2963 return -2;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002964 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002965
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002966 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05302967 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
2968 Item.uNestingLevel != 1 ||
2969 Item.uNextNestLevel != 2) {
2970 return -3;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002971 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002972
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002973 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade12b495d2018-12-17 11:15:54 -08002974 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade6de37062018-10-15 12:22:42 +05302975 Item.uNestingLevel != 2 ||
2976 Item.uNextNestLevel != 2) {
2977 return -4;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002978 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002979
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002980 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade12b495d2018-12-17 11:15:54 -08002981 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade6de37062018-10-15 12:22:42 +05302982 Item.uNestingLevel != 2 ||
2983 Item.uNextNestLevel != 0) {
2984 return -5;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002985 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002986
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002987 if(QCBORDecode_Finish(&DC)) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05302988 return -6;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002989 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002990
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002991 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002992 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad1);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002993
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002994 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002995
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002996 QCBORDecode_SetMemPool(&DC, MemPool, false);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002997
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002998 nResult = QCBORDecode_GetNext(&DC, &Item);
2999 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303000 return -7;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003001 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003002
Laurence Lundblade570fab52018-10-13 18:28:27 +08003003 nResult = QCBORDecode_Finish(&DC);
Laurence Lundblade6de37062018-10-15 12:22:42 +05303004 if(nResult != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
3005 return -8;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003006 }
3007
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003008
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003009 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003010 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad2);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003011
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003012 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003013
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003014 QCBORDecode_SetMemPool(&DC, MemPool, false);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003015
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003016 nResult = QCBORDecode_GetNext(&DC, &Item);
3017 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303018 return -9;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003019 }
3020
3021 nResult = QCBORDecode_GetNext(&DC, &Item);
3022 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303023 return -10;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003024 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003025
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003026 nResult = QCBORDecode_GetNext(&DC, &Item);
3027 if(nResult || Item.uDataType != QCBOR_TYPE_INT64) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303028 return -11;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003029 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003030
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003031 nResult = QCBORDecode_Finish(&DC);
Laurence Lundblade6de37062018-10-15 12:22:42 +05303032 if(nResult != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
3033 return -12;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003034 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003035
3036
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003037 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003038 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad3);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003039
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003040 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003041
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003042 QCBORDecode_SetMemPool(&DC, MemPool, false);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003043
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003044 nResult = QCBORDecode_GetNext(&DC, &Item);
3045 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303046 return -13;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003047 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003048
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003049 nResult = QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade642282a2020-06-23 12:00:33 -07003050 if(nResult != QCBOR_SUCCESS) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303051 return -14;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003052 }
Laurence Lundblade6de37062018-10-15 12:22:42 +05303053
Laurence Lundblade642282a2020-06-23 12:00:33 -07003054 nResult = QCBORDecode_GetNext(&DC, &Item);
3055 if(nResult != QCBOR_ERR_BAD_BREAK) {
3056 return -140;
3057 }
3058
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003059
Laurence Lundblade570fab52018-10-13 18:28:27 +08003060 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003061 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad4);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003062
Laurence Lundblade570fab52018-10-13 18:28:27 +08003063 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003064
Laurence Lundblade570fab52018-10-13 18:28:27 +08003065 QCBORDecode_SetMemPool(&DC, MemPool, false);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003066
Laurence Lundblade570fab52018-10-13 18:28:27 +08003067 nResult = QCBORDecode_GetNext(&DC, &Item);
3068 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303069 return -15;
Laurence Lundblade570fab52018-10-13 18:28:27 +08003070 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003071
Laurence Lundblade570fab52018-10-13 18:28:27 +08003072 nResult = QCBORDecode_GetNext(&DC, &Item);
3073 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303074 return -16;
Laurence Lundblade570fab52018-10-13 18:28:27 +08003075 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003076
Laurence Lundblade570fab52018-10-13 18:28:27 +08003077 nResult = QCBORDecode_Finish(&DC);
Laurence Lundblade6de37062018-10-15 12:22:42 +05303078 if(nResult != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
3079 return -17;
Laurence Lundblade570fab52018-10-13 18:28:27 +08003080 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003081
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303082 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003083 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad5);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003084
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303085 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003086
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303087 QCBORDecode_SetMemPool(&DC, MemPool, false);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003088
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303089 nResult = QCBORDecode_GetNext(&DC, &Item);
3090 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303091 return -18;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303092 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003093
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303094 nResult = QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05303095 if(nResult != QCBOR_ERR_BAD_BREAK) {
3096 return -19;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303097 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003098
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003099 return 0;
3100}
3101
Laurence Lundblade17ede402018-10-13 11:43:07 +08003102
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003103static const uint8_t spIndefiniteLenString[] = {
Laurence Lundblade17ede402018-10-13 11:43:07 +08003104 0x81, // Array of length one
3105 0x7f, // text string marked with indefinite length
3106 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment
3107 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment
3108 0xff // ending break
3109};
3110
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003111static const uint8_t spIndefiniteLenStringBad2[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303112 0x81, // Array of length one
3113 0x7f, // text string marked with indefinite length
3114 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment
3115 0x44, 0x6d, 0x69, 0x6e, 0x67, // second segment of wrong type
3116 0xff // ending break
3117};
3118
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003119static const uint8_t spIndefiniteLenStringBad3[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303120 0x81, // Array of length one
3121 0x7f, // text string marked with indefinite length
3122 0x01, 0x02, // Not a string
3123 0xff // ending break
3124};
3125
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003126static const uint8_t spIndefiniteLenStringBad4[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303127 0x81, // Array of length one
3128 0x7f, // text string marked with indefinite length
3129 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment
3130 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment
3131 // missing end of string
3132};
3133
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003134static const uint8_t spIndefiniteLenStringLabel[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303135 0xa1, // Array of length one
3136 0x7f, // text string marked with indefinite length
3137 0x65, 0x73, 0x74, 0x72, 0x75, 0x75, // first segment
3138 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment
3139 0xff, // ending break
3140 0x01 // integer being labeled.
3141};
3142
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003143/**
3144 Make an indefinite length string
3145
3146 @param Storage Storage for string, must be 144 bytes in size
3147 @return The indefinite length string
3148
3149 This makes an array with one indefinite length string that has 7 chunks
3150 from size of 1 byte up to 64 bytes.
3151 */
3152static UsefulBufC MakeIndefiniteBigBstr(UsefulBuf Storage)
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303153{
3154 UsefulOutBuf UOB;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003155
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303156 UsefulOutBuf_Init(&UOB, Storage);
3157 UsefulOutBuf_AppendByte(&UOB, 0x81);
3158 UsefulOutBuf_AppendByte(&UOB, 0x5f);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003159
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003160 uint8_t uStringByte = 0;
3161 // Use of type int is intentional
3162 for(int uChunkSize = 1; uChunkSize <= 128; uChunkSize *= 2) {
3163 // Not using preferred encoding here, but that is OK.
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303164 UsefulOutBuf_AppendByte(&UOB, 0x58);
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003165 UsefulOutBuf_AppendByte(&UOB, (uint8_t)uChunkSize);
3166 for(int j = 0; j < uChunkSize; j++) {
3167 UsefulOutBuf_AppendByte(&UOB, uStringByte);
3168 uStringByte++;
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303169 }
3170 }
3171 UsefulOutBuf_AppendByte(&UOB, 0xff);
3172
3173 return UsefulOutBuf_OutUBuf(&UOB);
3174}
3175
3176static int CheckBigString(UsefulBufC BigString)
3177{
3178 if(BigString.len != 255) {
3179 return 1;
3180 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003181
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303182 for(uint8_t i = 0; i < 255; i++){
3183 if(((const uint8_t *)BigString.ptr)[i] != i) {
3184 return 1;
3185 }
3186 }
3187 return 0;
3188}
3189
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05303190
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003191int32_t IndefiniteLengthStringTest()
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303192{
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303193 QCBORDecodeContext DC;
3194 QCBORItem Item;
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05303195 // big enough for MakeIndefiniteBigBstr() + MemPool overhead
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003196 UsefulBuf_MAKE_STACK_UB(MemPool, 350);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003197
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303198 // --- Simple normal indefinite length string ------
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003199 UsefulBufC IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenString);
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303200 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003201
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05303202 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303203 return -1;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303204 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003205
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303206 if(QCBORDecode_GetNext(&DC, &Item)) {
3207 return -2;
3208 }
3209 if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.uDataAlloc) {
3210 return -3;
3211 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003212
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303213 if(QCBORDecode_GetNext(&DC, &Item)) {
3214 return -4;
3215 }
3216 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || !Item.uDataAlloc) {
3217 return -5;
3218 }
3219 if(QCBORDecode_Finish(&DC)) {
3220 return -6;
3221 }
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303222
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303223 // ----- types mismatch ---
Laurence Lundbladeee851742020-01-08 08:37:05 -08003224 QCBORDecode_Init(&DC,
3225 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad2),
3226 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003227
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303228 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
3229 return -7;
3230 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003231
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303232 if(QCBORDecode_GetNext(&DC, &Item)) {
3233 return -8;
3234 }
3235 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
3236 return -9;
3237 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003238
Laurence Lundblade30816f22018-11-10 13:40:22 +07003239 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_INDEFINITE_STRING_CHUNK) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303240 return -10;
3241 }
3242
3243 // ----- not a string ---
Laurence Lundbladeee851742020-01-08 08:37:05 -08003244 QCBORDecode_Init(&DC,
3245 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad3),
3246 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003247
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303248 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
3249 return -11;
3250 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003251
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303252 if(QCBORDecode_GetNext(&DC, &Item)) {
3253 return -12;
3254 }
3255 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
3256 return -13;
3257 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003258
Laurence Lundblade30816f22018-11-10 13:40:22 +07003259 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_INDEFINITE_STRING_CHUNK) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303260 return -14;
3261 }
3262
3263 // ----- no end -----
Laurence Lundbladeee851742020-01-08 08:37:05 -08003264 QCBORDecode_Init(&DC,
3265 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad4),
3266 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003267
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303268 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
3269 return -15;
3270 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003271
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303272 if(QCBORDecode_GetNext(&DC, &Item)) {
3273 return -16;
3274 }
3275 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
3276 return -17;
3277 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003278
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303279 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_HIT_END) {
3280 return -18;
3281 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003282
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303283 // ------ Don't set a string allocator and see an error -----
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303284 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003285
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303286 QCBORDecode_GetNext(&DC, &Item);
3287 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303288 return -19;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303289 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003290
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303291 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_NO_STRING_ALLOCATOR) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303292 return -20;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303293 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003294
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303295 // ----- Mempool is way too small -----
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003296 UsefulBuf_MAKE_STACK_UB(MemPoolTooSmall, QCBOR_DECODE_MIN_MEM_POOL_SIZE-1);
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303297
3298 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
3299 if(!QCBORDecode_SetMemPool(&DC, MemPoolTooSmall, false)) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303300 return -21;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303301 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003302
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303303 // ----- Mempool is way too small -----
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05303304 UsefulBuf_MAKE_STACK_UB(BigIndefBStrStorage, 290);
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08003305 const UsefulBufC BigIndefBStr = MakeIndefiniteBigBstr(BigIndefBStrStorage);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003306
Laurence Lundbladeee851742020-01-08 08:37:05 -08003307 // 80 is big enough for MemPool overhead, but not BigIndefBStr
3308 UsefulBuf_MAKE_STACK_UB(MemPoolSmall, 80);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003309
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303310 QCBORDecode_Init(&DC, BigIndefBStr, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303311 if(QCBORDecode_SetMemPool(&DC, MemPoolSmall, false)) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303312 return -22;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303313 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003314
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303315 QCBORDecode_GetNext(&DC, &Item);
3316 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303317 return -23;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303318 }
Laurence Lundblade30816f22018-11-10 13:40:22 +07003319 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_STRING_ALLOCATE) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303320 return -24;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303321 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003322
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303323 // ---- big bstr -----
3324 QCBORDecode_Init(&DC, BigIndefBStr, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003325
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303326 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
3327 return -25;
3328 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003329
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05303330 if(QCBORDecode_GetNext(&DC, &Item)) {
3331 return -26;
3332 }
3333 if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.uDataAlloc) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303334 return -26;
3335 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003336
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303337 if(QCBORDecode_GetNext(&DC, &Item)) {
3338 return -27;
3339 }
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05303340 if(Item.uDataType != QCBOR_TYPE_BYTE_STRING || !Item.uDataAlloc || Item.uNestingLevel != 1) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303341 return -28;
3342 }
3343 if(CheckBigString(Item.val.string)) {
3344 return -3;
3345 }
3346 if(QCBORDecode_Finish(&DC)) {
3347 return -29;
3348 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003349
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303350 // --- label is an indefinite length string ------
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003351 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringLabel), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003352
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303353 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
3354 return -30;
3355 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003356
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303357 QCBORDecode_GetNext(&DC, &Item);
3358 if(Item.uDataType != QCBOR_TYPE_MAP) {
3359 return -31;
3360 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003361
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303362 if(QCBORDecode_GetNext(&DC, &Item)){
3363 return -32;
3364 }
Laurence Lundbladeee851742020-01-08 08:37:05 -08003365 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
3366 Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303367 Item.uDataAlloc || !Item.uLabelAlloc ||
3368 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("struuming"))) {
3369 return -33;
3370 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003371
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303372 if(QCBORDecode_Finish(&DC)) {
3373 return -34;
3374 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003375
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003376 return 0;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003377}
3378
3379
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003380int32_t AllocAllStringsTest()
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303381{
3382 QCBORDecodeContext DC;
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003383 QCBORError nCBORError;
3384
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003385
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303386 // First test, use the "CSRMap" as easy input and checking
Laurence Lundbladeee851742020-01-08 08:37:05 -08003387 QCBORDecode_Init(&DC,
3388 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput),
3389 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003390
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003391 UsefulBuf_MAKE_STACK_UB(Pool, sizeof(spCSRInput) + QCBOR_DECODE_MIN_MEM_POOL_SIZE);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003392
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003393 nCBORError = QCBORDecode_SetMemPool(&DC, Pool, 1); // Turn on copying.
3394 if(nCBORError) {
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303395 return -1;
3396 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003397
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003398 if(CheckCSRMaps(&DC)) {
3399 return -2;
3400 }
3401
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303402 // Next parse, save pointers to a few strings, destroy original and see all is OK.
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003403 UsefulBuf_MAKE_STACK_UB(CopyOfStorage, sizeof(pValidMapEncoded) + QCBOR_DECODE_MIN_MEM_POOL_SIZE);
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08003404 const UsefulBufC CopyOf = UsefulBuf_Copy(CopyOfStorage, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded));
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003405
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303406 QCBORDecode_Init(&DC, CopyOf, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08003407 UsefulBuf_Set(Pool, '/');
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303408 QCBORDecode_SetMemPool(&DC, Pool, 1); // Turn on copying.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003409
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303410 QCBORItem Item1, Item2, Item3, Item4;
3411 if((nCBORError = QCBORDecode_GetNext(&DC, &Item1)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07003412 return (int32_t)nCBORError;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303413 if(Item1.uDataType != QCBOR_TYPE_MAP ||
3414 Item1.val.uCount != 3)
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003415 return -3;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303416 if((nCBORError = QCBORDecode_GetNext(&DC, &Item1)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07003417 return (int32_t)nCBORError;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303418 if((nCBORError = QCBORDecode_GetNext(&DC, &Item2)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07003419 return (int32_t)nCBORError;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303420 if((nCBORError = QCBORDecode_GetNext(&DC, &Item3)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07003421 return (int32_t)nCBORError;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303422 if((nCBORError = QCBORDecode_GetNext(&DC, &Item4)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07003423 return (int32_t)nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003424
Laurence Lundblade05ec57b2018-10-21 01:50:03 +05303425 UsefulBuf_Set(CopyOfStorage, '_');
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003426
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303427 if(Item1.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303428 Item1.uDataType != QCBOR_TYPE_INT64 ||
3429 Item1.val.int64 != 42 ||
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003430 Item1.uDataAlloc != 0 ||
3431 Item1.uLabelAlloc == 0 ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09003432 UsefulBuf_Compare(Item1.label.string, UsefulBuf_FromSZ("first integer"))) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003433 return -4;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09003434 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003435
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303436
3437 if(Item2.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09003438 UsefulBuf_Compare(Item2.label.string, UsefulBuf_FromSZ("an array of two strings")) ||
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303439 Item2.uDataType != QCBOR_TYPE_ARRAY ||
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003440 Item2.uDataAlloc != 0 ||
3441 Item2.uLabelAlloc == 0 ||
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303442 Item2.val.uCount != 2)
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003443 return -5;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003444
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303445 if(Item3.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003446 Item3.uDataAlloc == 0 ||
3447 Item3.uLabelAlloc != 0 ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09003448 UsefulBuf_Compare(Item3.val.string, UsefulBuf_FromSZ("string1"))) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003449 return -6;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09003450 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003451
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303452 if(Item4.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003453 Item4.uDataAlloc == 0 ||
3454 Item4.uLabelAlloc != 0 ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09003455 UsefulBuf_Compare(Item4.val.string, UsefulBuf_FromSZ("string2"))) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003456 return -7;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09003457 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003458
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303459 // Next parse with a pool that is too small
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003460 UsefulBuf_MAKE_STACK_UB(SmallPool, QCBOR_DECODE_MIN_MEM_POOL_SIZE + 1);
Laurence Lundbladeee851742020-01-08 08:37:05 -08003461 QCBORDecode_Init(&DC,
3462 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded),
3463 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303464 QCBORDecode_SetMemPool(&DC, SmallPool, 1); // Turn on copying.
3465 if((nCBORError = QCBORDecode_GetNext(&DC, &Item1)))
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003466 return -8;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303467 if(Item1.uDataType != QCBOR_TYPE_MAP ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09003468 Item1.val.uCount != 3) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003469 return -9;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09003470 }
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303471 if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item1))){
3472 if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item2))) {
3473 if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item3))) {
3474 nCBORError = QCBORDecode_GetNext(&DC, &Item4);
3475 }
3476 }
3477 }
Laurence Lundblade30816f22018-11-10 13:40:22 +07003478 if(nCBORError != QCBOR_ERR_STRING_ALLOCATE) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003479 return -10;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303480 }
3481
3482 return 0;
3483}
3484
Laurence Lundbladef6531662018-12-04 10:42:22 +09003485
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303486
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003487int32_t MemPoolTest(void)
Laurence Lundblade0155b622018-10-12 20:04:37 +08003488{
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003489 // Set up the decoder with a tiny bit of CBOR to parse because
3490 // nothing can be done with it unless that is set up.
Laurence Lundbladef6531662018-12-04 10:42:22 +09003491 QCBORDecodeContext DC;
3492 const uint8_t pMinimalCBOR[] = {0xa0}; // One empty map
3493 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pMinimalCBOR),0);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003494
Laurence Lundbladef6531662018-12-04 10:42:22 +09003495 // Set up an memory pool of 100 bytes
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003496 // Then fish into the internals of the decode context
3497 // to get the allocator function so it can be called directly.
3498 // Also figure out how much pool is available for use
3499 // buy subtracting out the overhead.
Laurence Lundbladef6531662018-12-04 10:42:22 +09003500 UsefulBuf_MAKE_STACK_UB(Pool, 100);
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003501 QCBORError nError = QCBORDecode_SetMemPool(&DC, Pool, 0);
3502 if(nError) {
3503 return -9;
3504 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003505 QCBORStringAllocate pAlloc = DC.StringAllocator.pfAllocator;
3506 void *pAllocCtx = DC.StringAllocator.pAllocateCxt;
3507 size_t uAvailPool = Pool.len - QCBOR_DECODE_MIN_MEM_POOL_SIZE;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003508
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003509 // First test -- ask for one more byte than available and see failure
3510 UsefulBuf Allocated = (*pAlloc)(pAllocCtx, NULL, uAvailPool+1);
Laurence Lundbladef6531662018-12-04 10:42:22 +09003511 if(!UsefulBuf_IsNULL(Allocated)) {
3512 return -1;
3513 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003514
Laurence Lundbladef6531662018-12-04 10:42:22 +09003515 // Re do the set up for the next test that will do a successful alloc,
3516 // a fail, a free and then success
Laurence Lundbladef6531662018-12-04 10:42:22 +09003517 QCBORDecode_SetMemPool(&DC, Pool, 0);
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003518 pAlloc = DC.StringAllocator.pfAllocator;
3519 pAllocCtx = DC.StringAllocator.pAllocateCxt;
3520 uAvailPool = Pool.len - QCBOR_DECODE_MIN_MEM_POOL_SIZE;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003521
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003522 // Allocate one byte less than available and see success
3523 Allocated = (pAlloc)(pAllocCtx, NULL, uAvailPool-1);
Laurence Lundbladef6531662018-12-04 10:42:22 +09003524 if(UsefulBuf_IsNULL(Allocated)) { // expected to succeed
3525 return -2;
3526 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003527 // Ask for some more and see failure
3528 UsefulBuf Allocated2 = (*pAlloc)(pAllocCtx, NULL, uAvailPool/2);
Laurence Lundbladef6531662018-12-04 10:42:22 +09003529 if(!UsefulBuf_IsNULL(Allocated2)) { // expected to fail
3530 return -3;
3531 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003532 // Free the first allocate, retry the second and see success
3533 (*pAlloc)(pAllocCtx, Allocated.ptr, 0); // Free
3534 Allocated = (*pAlloc)(pAllocCtx, NULL, uAvailPool/2);
Laurence Lundbladef6531662018-12-04 10:42:22 +09003535 if(UsefulBuf_IsNULL(Allocated)) { // succeed because of the free
3536 return -4;
3537 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003538
Laurence Lundbladef6531662018-12-04 10:42:22 +09003539 // Re do set up for next test that involves a successful alloc,
3540 // and a successful realloc and a failed realloc
3541 QCBORDecode_SetMemPool(&DC, Pool, 0);
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003542 pAlloc = DC.StringAllocator.pfAllocator;
3543 pAllocCtx = DC.StringAllocator.pAllocateCxt;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003544
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003545 // Allocate half the pool and see success
3546 Allocated = (*pAlloc)(pAllocCtx, NULL, uAvailPool/2);
Laurence Lundbladef6531662018-12-04 10:42:22 +09003547 if(UsefulBuf_IsNULL(Allocated)) { // expected to succeed
3548 return -5;
3549 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003550 // Reallocate to take up the whole pool and see success
3551 Allocated2 = (*pAlloc)(pAllocCtx, Allocated.ptr, uAvailPool);
Laurence Lundbladef6531662018-12-04 10:42:22 +09003552 if(UsefulBuf_IsNULL(Allocated2)) {
3553 return -6;
3554 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003555 // Make sure its the same pointer and the size is right
Laurence Lundbladef6531662018-12-04 10:42:22 +09003556 if(Allocated2.ptr != Allocated.ptr || Allocated2.len != uAvailPool) {
3557 return -7;
3558 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003559 // Try to allocate more to be sure there is failure after a realloc
3560 UsefulBuf Allocated3 = (*pAlloc)(pAllocCtx, Allocated.ptr, uAvailPool+1);
3561 if(!UsefulBuf_IsNULL(Allocated3)) {
Laurence Lundbladef6531662018-12-04 10:42:22 +09003562 return -8;
3563 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003564
Laurence Lundbladef6531662018-12-04 10:42:22 +09003565 return 0;
3566}
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003567
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003568
3569/* Just enough of an allocator to test configuration of one */
3570static UsefulBuf AllocateTestFunction(void *pCtx, void *pOldMem, size_t uNewSize)
3571{
3572 (void)pOldMem; // unused variable
3573
3574 if(uNewSize) {
3575 // Assumes the context pointer is the buffer and
3576 // nothing too big will ever be asked for.
3577 // This is only good for this basic test!
3578 return (UsefulBuf) {pCtx, uNewSize};
3579 } else {
3580 return NULLUsefulBuf;
3581 }
3582}
3583
3584
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003585int32_t SetUpAllocatorTest(void)
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003586{
3587 // Set up the decoder with a tiny bit of CBOR to parse because
3588 // nothing can be done with it unless that is set up.
3589 QCBORDecodeContext DC;
3590 const uint8_t pMinimalCBOR[] = {0x62, 0x48, 0x69}; // "Hi"
3591 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pMinimalCBOR),0);
3592
3593 uint8_t pAllocatorBuffer[50];
3594
3595 // This is really just to test that this call works.
3596 // The full functionality of string allocators is tested
3597 // elsewhere with the MemPool internal allocator.
3598 QCBORDecode_SetUpAllocator(&DC, AllocateTestFunction, pAllocatorBuffer, 1);
3599
3600 QCBORItem Item;
3601 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_SUCCESS) {
3602 return -1;
3603 }
3604
3605 if(Item.uDataAlloc == 0 ||
3606 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
3607 Item.val.string.ptr != pAllocatorBuffer) {
3608 return -2;
3609 }
3610
3611 if(QCBORDecode_Finish(&DC) != QCBOR_SUCCESS) {
3612 return -3;
3613 }
3614
3615 return 0;
3616}
3617
Laurence Lundblade59289e52019-12-30 13:44:37 -08003618
3619#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
Laurence Lundbladea826c502020-05-10 21:07:00 -07003620/* exponent, mantissa
Laurence Lundblade59289e52019-12-30 13:44:37 -08003621 [
3622 4([-1, 3]),
Laurence Lundbladea826c502020-05-10 21:07:00 -07003623 4([-20, 4759477275222530853136]),
3624 4([9223372036854775807, -4759477275222530853137]),
Laurence Lundblade59289e52019-12-30 13:44:37 -08003625 5([300, 100]),
Laurence Lundbladea826c502020-05-10 21:07:00 -07003626 5([-20, 4759477275222530853136]),
Laurence Lundblade59289e52019-12-30 13:44:37 -08003627 5([-9223372036854775807, -4759477275222530853137])
Laurence Lundbladea826c502020-05-10 21:07:00 -07003628 5([ 9223372036854775806, -4759477275222530853137])
3629 5([ 9223372036854775806, 9223372036854775806])]
Laurence Lundblade59289e52019-12-30 13:44:37 -08003630 ]
3631 */
3632
3633static const uint8_t spExpectedExponentsAndMantissas[] = {
3634 0x87,
3635 0xC4, 0x82, 0x20,
3636 0x03,
3637 0xC4, 0x82, 0x33,
3638 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,
3639 0xC4, 0x82, 0x1B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
3640 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,
3641 0xC5, 0x82, 0x19, 0x01, 0x2C,
3642 0x18, 0x64,
3643 0xC5, 0x82, 0x33,
3644 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,
3645 0xC5, 0x82, 0x3B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE,
3646 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,
Laurence Lundbladea826c502020-05-10 21:07:00 -07003647 0xC5, 0x82, 0x1B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE,
3648 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,
Laurence Lundblade59289e52019-12-30 13:44:37 -08003649 0xC5, 0x82, 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE,
3650 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE
3651};
3652
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003653int32_t ExponentAndMantissaDecodeTests(void)
Laurence Lundblade59289e52019-12-30 13:44:37 -08003654{
3655 QCBORDecodeContext DC;
3656 QCBORError nCBORError;
3657 QCBORItem item;
3658
Laurence Lundblade17af4902020-01-07 19:11:55 -08003659 static const uint8_t spBigNumMantissa[] = {0x01, 0x02, 0x03, 0x04, 0x05,
3660 0x06, 0x07, 0x08, 0x09, 0x010};
3661 UsefulBufC BN = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNumMantissa);
Laurence Lundblade59289e52019-12-30 13:44:37 -08003662
3663
3664 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedExponentsAndMantissas), QCBOR_DECODE_MODE_NORMAL);
3665
3666 nCBORError = QCBORDecode_GetNext(&DC, &item);
3667 if(nCBORError != QCBOR_SUCCESS) {
3668 return 1;
3669 }
3670
3671 if(item.uDataType != QCBOR_TYPE_ARRAY) {
3672 return 2;
3673 }
3674
3675 nCBORError = QCBORDecode_GetNext(&DC, &item);
3676 if(nCBORError != QCBOR_SUCCESS) {
3677 return 3;
3678 }
3679
3680 if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION ||
3681 item.val.expAndMantissa.Mantissa.nInt != 3 ||
3682 item.val.expAndMantissa.nExponent != -1) {
3683 return 4;
3684 }
3685
3686 nCBORError = QCBORDecode_GetNext(&DC, &item);
3687 if(nCBORError != QCBOR_SUCCESS) {
3688 return 5;
3689 }
3690
3691 if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM ||
3692 item.val.expAndMantissa.nExponent != -20 ||
3693 UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) {
3694 return 6;
3695 }
3696
3697 nCBORError = QCBORDecode_GetNext(&DC, &item);
3698 if(nCBORError != QCBOR_SUCCESS) {
3699 return 7;
3700 }
3701
3702 if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM ||
3703 item.val.expAndMantissa.nExponent != 9223372036854775807 ||
3704 UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) {
3705 return 8;
3706 }
3707
3708 nCBORError = QCBORDecode_GetNext(&DC, &item);
3709 if(nCBORError != QCBOR_SUCCESS) {
3710 return 9;
3711 }
3712
3713 if(item.uDataType != QCBOR_TYPE_BIGFLOAT ||
3714 item.val.expAndMantissa.Mantissa.nInt != 100 ||
3715 item.val.expAndMantissa.nExponent != 300) {
3716 return 10;
3717 }
3718
Laurence Lundbladea826c502020-05-10 21:07:00 -07003719 // 5([-20, 4759477275222530853136]),
Laurence Lundblade59289e52019-12-30 13:44:37 -08003720 nCBORError = QCBORDecode_GetNext(&DC, &item);
3721 if(nCBORError != QCBOR_SUCCESS) {
3722 return 11;
3723 }
Laurence Lundblade59289e52019-12-30 13:44:37 -08003724 if(item.uDataType != QCBOR_TYPE_BIGFLOAT_POS_BIGNUM ||
3725 item.val.expAndMantissa.nExponent != -20 ||
3726 UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) {
3727 return 12;
3728 }
3729
Laurence Lundbladea826c502020-05-10 21:07:00 -07003730 // 5([-9223372036854775807, -4759477275222530853137])
Laurence Lundblade59289e52019-12-30 13:44:37 -08003731 nCBORError = QCBORDecode_GetNext(&DC, &item);
3732 if(nCBORError != QCBOR_SUCCESS) {
3733 return 13;
3734 }
Laurence Lundblade59289e52019-12-30 13:44:37 -08003735 if(item.uDataType != QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM ||
3736 item.val.expAndMantissa.nExponent != -9223372036854775807 ||
3737 UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) {
3738 return 14;
3739 }
3740
Laurence Lundbladea826c502020-05-10 21:07:00 -07003741 // 5([ 9223372036854775806, -4759477275222530853137])
3742 nCBORError = QCBORDecode_GetNext(&DC, &item);
3743 if(nCBORError != QCBOR_SUCCESS) {
3744 return 13;
3745 }
3746 if(item.uDataType != QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM ||
3747 item.val.expAndMantissa.nExponent != 9223372036854775806 ||
3748 UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) {
3749 return 14;
3750 }
3751
3752
3753 // 5([ 9223372036854775806, 9223372036854775806])]
Laurence Lundblade59289e52019-12-30 13:44:37 -08003754 nCBORError = QCBORDecode_GetNext(&DC, &item);
3755 if(nCBORError != QCBOR_SUCCESS) {
3756 return 15;
3757 }
Laurence Lundblade59289e52019-12-30 13:44:37 -08003758 if(item.uDataType != QCBOR_TYPE_BIGFLOAT ||
3759 item.val.expAndMantissa.nExponent != 9223372036854775806 ||
3760 item.val.expAndMantissa.Mantissa.nInt!= 9223372036854775806 ) {
3761 return 16;
3762 }
3763
3764 /* Now encode some stuff and then decode it */
3765 uint8_t pBuf[40];
3766 QCBOREncodeContext EC;
3767 UsefulBufC Encoded;
3768
3769 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(pBuf));
3770 QCBOREncode_OpenArray(&EC);
3771 QCBOREncode_AddDecimalFraction(&EC, 999, 1000); // 999 * (10 ^ 1000)
3772 QCBOREncode_AddBigFloat(&EC, 100, INT32_MIN);
3773 QCBOREncode_AddDecimalFractionBigNum(&EC, BN, false, INT32_MAX);
3774 QCBOREncode_CloseArray(&EC);
3775 QCBOREncode_Finish(&EC, &Encoded);
3776
3777
3778 QCBORDecode_Init(&DC, Encoded, QCBOR_DECODE_MODE_NORMAL);
3779 nCBORError = QCBORDecode_GetNext(&DC, &item);
3780 if(nCBORError != QCBOR_SUCCESS) {
3781 return 13;
3782 }
3783
3784 nCBORError = QCBORDecode_GetNext(&DC, &item);
3785 if(nCBORError != QCBOR_SUCCESS) {
3786 return 13;
3787 }
3788
3789 if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION ||
3790 item.val.expAndMantissa.nExponent != 1000 ||
3791 item.val.expAndMantissa.Mantissa.nInt != 999) {
3792 return 15;
3793 }
3794
3795 nCBORError = QCBORDecode_GetNext(&DC, &item);
3796 if(nCBORError != QCBOR_SUCCESS) {
3797 return 13;
3798 }
3799
3800 if(item.uDataType != QCBOR_TYPE_BIGFLOAT ||
3801 item.val.expAndMantissa.nExponent != INT32_MIN ||
3802 item.val.expAndMantissa.Mantissa.nInt != 100) {
3803 return 15;
3804 }
3805
3806 nCBORError = QCBORDecode_GetNext(&DC, &item);
3807 if(nCBORError != QCBOR_SUCCESS) {
3808 return 13;
3809 }
3810
3811 if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM ||
3812 item.val.expAndMantissa.nExponent != INT32_MAX ||
3813 UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) {
3814 return 12;
3815 }
3816
3817 return 0;
3818}
3819
3820
3821static struct FailInput ExponentAndMantissaFailures[] = {
3822 // Exponent > INT64_MAX
3823 { {(uint8_t[]){0xC4, 0x82, 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
3824 0xFF, 0xFF, 0x1B, 0x80, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
3825 0xFF, 0xFF,}, 20}, QCBOR_ERR_BAD_EXP_AND_MANTISSA},
3826 // Mantissa > INT64_MAX
3827 { {(uint8_t[]){0xC4, 0x82, 0x1B, 0x80, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
3828 0xFF, 0xFF, 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05,
3829 0x06, 0x07, 0x08, 0x09, 0x10}, 23}, QCBOR_ERR_BAD_EXP_AND_MANTISSA},
3830 // End of input
3831 { {(uint8_t[]){0xC4, 0x82}, 2}, QCBOR_ERR_HIT_END},
3832 // End of input
3833 { {(uint8_t[]){0xC4, 0x82, 0x01}, 3}, QCBOR_ERR_HIT_END},
3834 // bad content for big num
3835 { {(uint8_t[]){0xC4, 0x82, 0x01, 0xc3, 0x01}, 5}, QCBOR_ERR_BAD_OPT_TAG},
3836 // bad content for big num
3837 { {(uint8_t[]){0xC4, 0x82, 0xc2, 0x01, 0x1f}, 5}, QCBOR_ERR_BAD_INT},
3838 // Bad integer for exponent
3839 { {(uint8_t[]){0xC4, 0x82, 0x01, 0x1f}, 4}, QCBOR_ERR_BAD_INT},
3840 // Bad integer for mantissa
3841 { {(uint8_t[]){0xC4, 0x82, 0x1f, 0x01}, 4}, QCBOR_ERR_BAD_INT},
3842 // 3 items in array
3843 { {(uint8_t[]){0xC4, 0x83, 0x03, 0x01, 02}, 5}, QCBOR_ERR_BAD_EXP_AND_MANTISSA},
3844 // unterminated indefinite length array
3845 { {(uint8_t[]){0xC4, 0x9f, 0x03, 0x01, 0x02}, 5}, QCBOR_ERR_BAD_EXP_AND_MANTISSA},
3846 // Empty array
3847 { {(uint8_t[]){0xC4, 0x80}, 2}, QCBOR_ERR_NO_MORE_ITEMS},
3848 // Second is not an integer
3849 { {(uint8_t[]){0xC4, 0x82, 0x03, 0x40}, 4}, QCBOR_ERR_BAD_EXP_AND_MANTISSA},
3850 // First is not an integer
3851 { {(uint8_t[]){0xC4, 0x82, 0x40}, 3}, QCBOR_ERR_BAD_EXP_AND_MANTISSA},
3852 // Not an array
3853 { {(uint8_t[]){0xC4, 0xa2}, 2}, QCBOR_ERR_BAD_EXP_AND_MANTISSA}
3854};
3855
3856
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003857int32_t ExponentAndMantissaDecodeFailTests()
Laurence Lundblade59289e52019-12-30 13:44:37 -08003858{
3859 return ProcessFailures(ExponentAndMantissaFailures,
3860 sizeof(ExponentAndMantissaFailures)/sizeof(struct FailInput));
3861}
3862
3863#endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */
Laurence Lundbladebb87be22020-04-09 19:15:32 -07003864
3865
3866
3867/*
3868 Some basic CBOR with map and array used in a lot of tests.
3869 The map labels are all strings
3870
Laurence Lundblade8ffdb742020-05-07 02:49:18 -07003871 {
3872 "first integer": 42,
Laurence Lundbladebb87be22020-04-09 19:15:32 -07003873 "an array of two strings": [
3874 "string1", "string2"
3875 ],
3876 "map in a map": {
3877 "bytes 1": h'78787878',
3878 "bytes 2": h'79797979',
3879 "another int": 98,
3880 "text 2": "lies, damn lies and statistics"
3881 }
3882 }
3883 */
3884
Laurence Lundbladeeb0b53d2020-04-14 18:34:25 -07003885#include "qcbor/qcbor_decode_map.h"
Laurence Lundblade1341c592020-04-11 14:19:05 -07003886#include <stdio.h>
Laurence Lundbladebb87be22020-04-09 19:15:32 -07003887
Laurence Lundblade9c905e82020-04-25 11:31:38 -07003888static char strbuf[10];
3889const char *PrintType(uint8_t type) {
3890 switch(type) {
3891 case QCBOR_TYPE_INT64: return "INT64";
3892 case QCBOR_TYPE_UINT64: return "UINT64";
3893 case QCBOR_TYPE_ARRAY: return "ARRAY";
3894 case QCBOR_TYPE_MAP: return "MAP";
3895 case QCBOR_TYPE_BYTE_STRING: return "BYTE_STRING";
3896 case QCBOR_TYPE_TEXT_STRING: return "TEXT_STRING";
3897 default:
3898 sprintf(strbuf, "%d", type);
3899 return strbuf;
3900 }
3901}
3902
3903
3904void PrintItem(QCBORItem Item)
3905{
3906 printf("\nData: %s nest: %d,%d %s\n", PrintType(Item.uDataType), Item.uNestingLevel, Item.uNextNestLevel, Item.uDataAlloc ? "Allocated":"");
3907 if(Item.uLabelType) {
3908 printf("Label: %s ", PrintType(Item.uLabelType));
3909 if(Item.uLabelType == QCBOR_TYPE_INT64) {
3910 printf("%lld\n", Item.label.int64);
3911 } else if(Item.uLabelType == QCBOR_TYPE_TEXT_STRING) {
3912 printf("\"%4.4s\"\n", Item.label.string.ptr);
3913 }
3914 }
3915}
3916
Laurence Lundblade8ffdb742020-05-07 02:49:18 -07003917
Laurence Lundbladebb87be22020-04-09 19:15:32 -07003918int32_t EnterMapTest()
3919{
Laurence Lundblade8ffdb742020-05-07 02:49:18 -07003920 QCBORItem Item1, Item2, Item3;
3921 int64_t nDecodedInt1, nDecodedInt2;
3922 UsefulBufC B1, B2, S1, S2, S3;
Laurence Lundblade9c905e82020-04-25 11:31:38 -07003923
Laurence Lundbladebb87be22020-04-09 19:15:32 -07003924 QCBORDecodeContext DCtx;
3925 QCBORError nCBORError;
3926
3927 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0);
Laurence Lundblade1341c592020-04-11 14:19:05 -07003928
Laurence Lundbladebb87be22020-04-09 19:15:32 -07003929 QCBORDecode_EnterMap(&DCtx);
Laurence Lundblade1341c592020-04-11 14:19:05 -07003930
Laurence Lundblade843a10c2020-05-23 13:57:00 -07003931 QCBORDecode_GetInt64InMapSZ(&DCtx, "first integer", &nDecodedInt1);
Laurence Lundblade1341c592020-04-11 14:19:05 -07003932
Laurence Lundblade8ffdb742020-05-07 02:49:18 -07003933 QCBORDecode_EnterMapFromMapSZ(&DCtx, "map in a map");
Laurence Lundblade843a10c2020-05-23 13:57:00 -07003934 QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2);
3935 QCBORDecode_GetBytesInMapSZ(&DCtx, "bytes 1", &B1);
3936 QCBORDecode_GetBytesInMapSZ(&DCtx, "bytes 2", &B2);
Laurence Lundblade8ffdb742020-05-07 02:49:18 -07003937 QCBORDecode_GetTextInMapSZ(&DCtx, "text 2", &S1);
3938 QCBORDecode_ExitMap(&DCtx);
Laurence Lundblade1341c592020-04-11 14:19:05 -07003939
Laurence Lundblade8ffdb742020-05-07 02:49:18 -07003940 QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings");
3941 QCBORDecode_GetNext(&DCtx, &Item1);
3942 QCBORDecode_GetNext(&DCtx, &Item2);
3943 if(QCBORDecode_GetNext(&DCtx, &Item3) != QCBOR_ERR_NO_MORE_ITEMS) {
3944 return -400;
3945 }
3946 QCBORDecode_ExitArray(&DCtx);
Laurence Lundblade3f9ef042020-04-14 13:15:51 -07003947
Laurence Lundblade8ffdb742020-05-07 02:49:18 -07003948 // Parse the same array again using GetText() instead of GetItem()
3949 QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings");
3950 QCBORDecode_GetText(&DCtx, &S2);
3951 QCBORDecode_GetText(&DCtx, &S3);
Laurence Lundblade02625d42020-06-25 14:41:41 -07003952 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) {
3953 return 5000;
3954 }
3955 QCBORDecode_GetText(&DCtx, &S3);
3956 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_NO_MORE_ITEMS) {
3957 return 5001;
3958 }
3959
Laurence Lundblade8ffdb742020-05-07 02:49:18 -07003960 QCBORDecode_ExitArray(&DCtx);
Laurence Lundblade1341c592020-04-11 14:19:05 -07003961
Laurence Lundbladebb87be22020-04-09 19:15:32 -07003962 QCBORDecode_ExitMap(&DCtx);
3963
3964 nCBORError = QCBORDecode_Finish(&DCtx);
3965
3966 if(nCBORError) {
3967 return (int32_t)nCBORError;
3968 }
3969
3970 if(nDecodedInt1 != 42) {
Laurence Lundblade8ffdb742020-05-07 02:49:18 -07003971 return 1001;
Laurence Lundbladebb87be22020-04-09 19:15:32 -07003972 }
3973
Laurence Lundblade1341c592020-04-11 14:19:05 -07003974 if(nDecodedInt2 != 98) {
Laurence Lundblade8ffdb742020-05-07 02:49:18 -07003975 return 1002;
3976 }
3977
3978 if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING ||
3979 UsefulBuf_Compare(Item1.val.string, UsefulBuf_FromSZ("string1"))){
3980 return 1003;
3981 }
3982
3983 if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING ||
3984 UsefulBuf_Compare(Item2.val.string, UsefulBuf_FromSZ("string2"))){
3985 return 1004;
3986 }
3987
3988 if(UsefulBuf_Compare(S1, UsefulBuf_FromSZ("lies, damn lies and statistics"))){
3989 return 1005;
Laurence Lundbladebb87be22020-04-09 19:15:32 -07003990 }
3991
Laurence Lundblade8ffdb742020-05-07 02:49:18 -07003992 if(UsefulBuf_Compare(B1, UsefulBuf_FromSZ("xxxx"))){
3993 return 1006;
3994 }
3995
3996 if(UsefulBuf_Compare(B2, UsefulBuf_FromSZ("yyyy"))){
3997 return 1007;
3998 }
3999
4000 if(UsefulBuf_Compare(S2, UsefulBuf_FromSZ("string1"))){
4001 return 1008;
4002 }
4003
4004 if(UsefulBuf_Compare(S3, UsefulBuf_FromSZ("string2"))){
4005 return 1009;
4006 }
Laurence Lundblade937ea812020-05-08 11:38:23 -07004007
4008 // These tests confirm the cursor is at the right place after entering a map or array
4009
4010 // Confirm cursor is at right place
4011 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0);
4012 QCBORDecode_EnterMap(&DCtx);
4013 QCBORDecode_GetNext(&DCtx, &Item1);
4014 if(Item1.uDataType != QCBOR_TYPE_INT64) {
4015 return 2001;
4016 }
4017
4018
4019 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0);
4020 QCBORDecode_GetNext(&DCtx, &Item1);
4021 QCBORDecode_GetNext(&DCtx, &Item1);
4022 QCBORDecode_EnterArray(&DCtx);
4023 QCBORDecode_GetNext(&DCtx, &Item1);
4024 if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING) {
4025 return 2002;
4026 }
4027
4028 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0);
4029 QCBORDecode_EnterMap(&DCtx);
Laurence Lundblade64b607e2020-05-13 13:05:57 -07004030 QCBORDecode_GetNext(&DCtx, &Item1);
4031 QCBORDecode_GetNext(&DCtx, &Item1);
4032 QCBORDecode_GetNext(&DCtx, &Item1);
Laurence Lundblade937ea812020-05-08 11:38:23 -07004033 QCBORDecode_EnterMapFromMapSZ(&DCtx, "map in a map");
4034 QCBORDecode_GetNext(&DCtx, &Item1);
4035 if(Item1.uDataType != QCBOR_TYPE_BYTE_STRING) {
4036 return 2003;
4037 }
4038
4039 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0);
4040 QCBORDecode_EnterMap(&DCtx);
4041 QCBORDecode_GetNext(&DCtx, &Item1);
4042 QCBORDecode_GetNext(&DCtx, &Item1);
4043 QCBORDecode_GetNext(&DCtx, &Item1);
4044 QCBORDecode_GetNext(&DCtx, &Item1);
4045 QCBORDecode_GetNext(&DCtx, &Item1);
4046 QCBORDecode_GetNext(&DCtx, &Item1);
4047 QCBORDecode_GetNext(&DCtx, &Item1);
4048 QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings");
4049 QCBORDecode_GetNext(&DCtx, &Item1);
4050 if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING) {
Laurence Lundblade64b607e2020-05-13 13:05:57 -07004051 return 2004;
Laurence Lundblade937ea812020-05-08 11:38:23 -07004052 }
4053
Laurence Lundblade2b843b52020-06-16 20:51:03 -07004054 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0);
4055 QCBORDecode_EnterMap(&DCtx);
4056 QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings");
4057 QCBORDecode_ExitArray(&DCtx);
4058 QCBORDecode_GetNext(&DCtx, &Item1);
4059 if(Item1.uDataType != QCBOR_TYPE_MAP && Item1.uLabelAlloc != QCBOR_TYPE_TEXT_STRING) {
4060 return 2006;
4061 }
4062 QCBORDecode_ExitMap(&DCtx);
4063 if(QCBORDecode_GetNext(&DCtx, &Item1) != QCBOR_ERR_NO_MORE_ITEMS) {
4064 return 2007;
4065 }
4066
Laurence Lundblade937ea812020-05-08 11:38:23 -07004067
Laurence Lundblade9c905e82020-04-25 11:31:38 -07004068 return 0;
4069}
4070
4071
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004072struct NumberConversion {
4073 char *szDescription;
4074 UsefulBufC CBOR;
4075 int64_t nConvertedToInt64;
4076 QCBORError uErrorInt64;
4077 uint64_t uConvertToUInt64;
4078 QCBORError uErrorUint64;
4079 double dConvertToDouble;
4080 QCBORError uErrorDouble;
4081};
4082
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07004083static const struct NumberConversion NumberConversions[] = {
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004084 {
Laurence Lundbladeda095972020-06-06 18:35:33 -07004085 "negative bignum -1",
4086 {(uint8_t[]){0xc3, 0x41, 0x00}, 3},
4087 -1,
4088 QCBOR_SUCCESS,
4089 0,
4090 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
4091 -1.0,
4092 QCBOR_SUCCESS
4093 },
4094 {
Laurence Lundblade9ab5abb2020-05-20 12:10:45 -07004095 "Decimal Fraction with positive bignum 257 * 10e3",
Laurence Lundblade887add82020-05-17 05:50:34 -07004096 {(uint8_t[]){0xC4, 0x82, 0x1B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03,
4097 0xC2, 0x42, 0x01, 0x01}, 15},
4098 257000,
4099 QCBOR_SUCCESS,
4100 257000,
4101 QCBOR_SUCCESS,
4102 257000.0,
4103 QCBOR_SUCCESS
4104 },
4105 {
Laurence Lundbladeda095972020-06-06 18:35:33 -07004106 "bigfloat with negative bignum -258 * 2e3",
Laurence Lundblade887add82020-05-17 05:50:34 -07004107 {(uint8_t[]){0xC5, 0x82, 0x1B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03,
4108 0xC3, 0x42, 0x01, 0x01}, 15},
Laurence Lundbladeda095972020-06-06 18:35:33 -07004109 -2064,
Laurence Lundblade887add82020-05-17 05:50:34 -07004110 QCBOR_SUCCESS,
4111 0,
4112 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
Laurence Lundbladeda095972020-06-06 18:35:33 -07004113 -2064.0,
Laurence Lundblade887add82020-05-17 05:50:34 -07004114 QCBOR_SUCCESS
4115 },
4116 {
Laurence Lundblade9ab5abb2020-05-20 12:10:45 -07004117 "bigfloat with positive bignum 257 * 2e3",
Laurence Lundblade887add82020-05-17 05:50:34 -07004118 {(uint8_t[]){0xC5, 0x82, 0x1B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03,
4119 0xC2, 0x42, 0x01, 0x01}, 15},
4120 2056,
4121 QCBOR_SUCCESS,
4122 2056,
4123 QCBOR_SUCCESS,
4124 2056.0,
4125 QCBOR_SUCCESS
4126 },
4127 {
Laurence Lundbladeda095972020-06-06 18:35:33 -07004128 "negative bignum 0xc349010000000000000000 -18446744073709551617",
Laurence Lundblade887add82020-05-17 05:50:34 -07004129 {(uint8_t[]){0xc3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 11},
4130 0,
4131 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
4132 0,
4133 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
4134 -18446744073709551617.0,
4135 QCBOR_SUCCESS
4136 },
4137 {
Laurence Lundblade313b2862020-05-16 01:23:06 -07004138 "Positive bignum 0x01020304 indefinite length string",
4139 {(uint8_t[]){0xC2, 0x5f, 0x42, 0x01, 0x02, 0x41, 0x03, 0x41, 0x04, 0xff}, 10},
4140 0x01020304,
4141 QCBOR_SUCCESS,
4142 0x01020304,
4143 QCBOR_SUCCESS,
4144 16909060.0,
4145 QCBOR_SUCCESS
4146 },
4147 {
Laurence Lundblade887add82020-05-17 05:50:34 -07004148 "Decimal Fraction with neg bignum [9223372036854775807, -4759477275222530853137]",
Laurence Lundblade313b2862020-05-16 01:23:06 -07004149 {(uint8_t[]){0xC4, 0x82, 0x1B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
4150 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,}, 23},
4151 0,
4152 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
4153 0,
4154 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
4155 -INFINITY,
4156 QCBOR_SUCCESS
4157 },
4158 {
4159 "big float [9223372036854775806, 9223372036854775806]",
4160 {(uint8_t[]){0xC5, 0x82, 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE,
4161 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE}, 20},
4162 0,
4163 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
4164 0,
4165 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
4166 INFINITY,
4167 QCBOR_SUCCESS
4168 },
4169 {
Laurence Lundblade983500d2020-05-14 11:49:34 -07004170 "Big float 3 * 2^^2",
4171 {(uint8_t[]){0xC5, 0x82, 0x02, 0x03}, 4},
4172 12,
4173 QCBOR_SUCCESS,
4174 12,
4175 QCBOR_SUCCESS,
4176 12.0,
4177 QCBOR_SUCCESS
4178 },
Laurence Lundblade983500d2020-05-14 11:49:34 -07004179 {
Laurence Lundblade313b2862020-05-16 01:23:06 -07004180 "Positive integer 18446744073709551615",
Laurence Lundblade983500d2020-05-14 11:49:34 -07004181 {(uint8_t[]){0x1b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, 9},
4182 0,
4183 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
4184 18446744073709551615ULL,
4185 QCBOR_SUCCESS,
4186 18446744073709551615.0,
4187 QCBOR_SUCCESS
4188 },
Laurence Lundblade983500d2020-05-14 11:49:34 -07004189 {
Laurence Lundblade313b2862020-05-16 01:23:06 -07004190 "Positive bignum 0xffff",
Laurence Lundblade983500d2020-05-14 11:49:34 -07004191 {(uint8_t[]){0xC2, 0x42, 0xff, 0xff}, 4},
4192 65536-1,
4193 QCBOR_SUCCESS,
4194 0xffff,
4195 QCBOR_SUCCESS,
4196 65535.0,
4197 QCBOR_SUCCESS
4198 },
4199 {
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004200 "Postive integer 0",
4201 {(uint8_t[]){0x0}, 1},
4202 0LL,
4203 QCBOR_SUCCESS,
4204 0ULL,
4205 QCBOR_SUCCESS,
4206 0.0,
4207 QCBOR_SUCCESS
4208 },
4209 {
Laurence Lundblade313b2862020-05-16 01:23:06 -07004210 "Negative integer -18446744073709551616",
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004211 {(uint8_t[]){0x3b, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }, 9},
4212 -9223372036854775807-1, // INT64_MIN
4213 QCBOR_SUCCESS,
4214 0ULL,
4215 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
4216 -9223372036854775808.0,
4217 QCBOR_SUCCESS
4218 },
4219 {
Laurence Lundblade313b2862020-05-16 01:23:06 -07004220 "Double Floating point value 100.3",
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004221 {(uint8_t[]){0xfb, 0x40, 0x59, 0x13, 0x33, 0x33, 0x33, 0x33, 0x33}, 9},
4222 100L,
4223 QCBOR_SUCCESS,
4224 100ULL,
4225 QCBOR_SUCCESS,
4226 100.3,
4227 QCBOR_SUCCESS
4228 },
4229 {
4230 "Floating point value NaN 0xfa7fc00000",
4231 {(uint8_t[]){0xfa, 0x7f, 0xc0, 0x00, 0x00}, 5},
4232 0,
4233 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
4234 0,
4235 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
4236 NAN,
4237 QCBOR_SUCCESS
4238 },
4239 {
Laurence Lundblade313b2862020-05-16 01:23:06 -07004240 "half-precision Floating point value -4",
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004241 {(uint8_t[]){0xf9, 0xc4, 0x00}, 3},
4242 -4,
4243 QCBOR_SUCCESS,
4244 0,
4245 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
4246 -4.0,
4247 QCBOR_SUCCESS
4248 },
4249 {
4250 "Decimal fraction 3/10",
4251 {(uint8_t[]){0xC4, 0x82, 0x20, 0x03}, 4},
4252 0,
4253 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
4254 0,
4255 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
4256 0.30000000000000004,
4257 QCBOR_SUCCESS
4258 }
4259};
Laurence Lundblade9c905e82020-04-25 11:31:38 -07004260
4261
4262
Laurence Lundblade313b2862020-05-16 01:23:06 -07004263int32_t IntegerConvertTest()
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004264{
Laurence Lundblade9ab5abb2020-05-20 12:10:45 -07004265 const int nNumTests = sizeof(NumberConversions)/sizeof(struct NumberConversion);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004266
Laurence Lundblade9ab5abb2020-05-20 12:10:45 -07004267 for(int nIndex = 0; nIndex < nNumTests; nIndex++) {
4268 const struct NumberConversion *pF = &NumberConversions[nIndex];
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07004269
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004270 // Set up the decoding context including a memory pool so that
4271 // indefinite length items can be checked
4272 QCBORDecodeContext DCtx;
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004273 UsefulBuf_MAKE_STACK_UB(Pool, 100);
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07004274
4275 /* ----- test conversion to int64_t ------ */
4276 QCBORDecode_Init(&DCtx, pF->CBOR, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004277 QCBORError nCBORError = QCBORDecode_SetMemPool(&DCtx, Pool, 0);
4278 if(nCBORError) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07004279 return (int32_t)(1000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004280 }
4281
4282 int64_t nInt;
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004283 QCBORDecode_GetInt64ConvertAll(&DCtx, 0xffff, &nInt);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07004284 if(QCBORDecode_GetError(&DCtx) != pF->uErrorInt64) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07004285 return (int32_t)(2000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004286 }
4287 if(pF->uErrorInt64 == QCBOR_SUCCESS && pF->nConvertedToInt64 != nInt) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07004288 return (int32_t)(3000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004289 }
4290
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07004291 /* ----- test conversion to uint64_t ------ */
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004292 QCBORDecode_Init(&DCtx, pF->CBOR, QCBOR_DECODE_MODE_NORMAL);
4293 nCBORError = QCBORDecode_SetMemPool(&DCtx, Pool, 0);
4294 if(nCBORError) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07004295 return (int32_t)(1000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004296 }
4297 uint64_t uInt;
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004298 QCBORDecode_GetUInt64ConvertAll(&DCtx, 0xffff, &uInt);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07004299 if(QCBORDecode_GetError(&DCtx) != pF->uErrorUint64) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07004300 return (int32_t)(4000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004301 }
4302 if(pF->uErrorUint64 == QCBOR_SUCCESS && pF->uConvertToUInt64 != uInt) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07004303 return (int32_t)(5000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004304 }
4305
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07004306 /* ----- test conversion to double ------ */
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004307 QCBORDecode_Init(&DCtx, pF->CBOR, QCBOR_DECODE_MODE_NORMAL);
4308 nCBORError = QCBORDecode_SetMemPool(&DCtx, Pool, 0);
4309 if(nCBORError) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07004310 return (int32_t)(1000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004311 }
4312 double d;
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004313 QCBORDecode_GetDoubleConvertAll(&DCtx, 0xffff, &d);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07004314 if(QCBORDecode_GetError(&DCtx) != pF->uErrorDouble) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07004315 return (int32_t)(6000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004316 }
4317 if(pF->uErrorDouble == QCBOR_SUCCESS) {
4318 if(isnan(pF->dConvertToDouble)) {
Laurence Lundblade983500d2020-05-14 11:49:34 -07004319 // NaN's can't be compared for equality. A NaN is
4320 // never equal to anything including another NaN
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004321 if(!isnan(d)) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07004322 return (int32_t)(7000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004323 }
4324 } else {
4325 // TODO: this comparison may need a margin of error
4326 if(pF->dConvertToDouble != d) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07004327 return (int32_t)(8000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07004328 }
4329 }
4330 }
4331 }
4332
4333 return 0;
4334}
4335
Laurence Lundblade313b2862020-05-16 01:23:06 -07004336int32_t IntegerConvertTestOld()
Laurence Lundblade9c905e82020-04-25 11:31:38 -07004337{
Laurence Lundblade9c905e82020-04-25 11:31:38 -07004338 QCBORDecodeContext DCtx;
4339 QCBORError nCBORError;
4340
Laurence Lundbladea826c502020-05-10 21:07:00 -07004341 /* exponent, mantissa
Laurence Lundblade9c905e82020-04-25 11:31:38 -07004342 [
4343 4([-1, 3]),
4344 4([-20, 4759477275222530853136]),
4345 4([9223372036854775807, -4759477275222530853137]),
4346 5([300, 100]),
4347 5([-20, 4759477275222530853136]),
4348 5([-9223372036854775807, -4759477275222530853137])
Laurence Lundbladea826c502020-05-10 21:07:00 -07004349 5([ 9223372036854775806, -4759477275222530853137])
4350 5([ 9223372036854775806, 9223372036854775806])]
Laurence Lundblade9c905e82020-04-25 11:31:38 -07004351 ]
4352 */
4353
4354 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedExponentsAndMantissas), 0);
4355
4356 QCBORItem Item;
4357 nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
4358 if(nCBORError) {
4359 return -1;
4360 }
4361
4362 int64_t integer;
Laurence Lundbladea826c502020-05-10 21:07:00 -07004363 // 4([-1, 3]),
Laurence Lundblade9c905e82020-04-25 11:31:38 -07004364 QCBORDecode_GetInt64ConvertAll(&DCtx, QCBOR_CONVERT_TYPE_DECIMAL_FRACTION, &integer);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07004365 if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW) {
Laurence Lundblade9c905e82020-04-25 11:31:38 -07004366 return -2;
4367 }
4368 DCtx.uLastError = 0; // TODO: a method for this
4369
Laurence Lundbladea826c502020-05-10 21:07:00 -07004370 // 4([-20, 4759477275222530853136]),
Laurence Lundblade9c905e82020-04-25 11:31:38 -07004371 QCBORDecode_GetInt64ConvertAll(&DCtx, QCBOR_CONVERT_TYPE_DECIMAL_FRACTION, &integer);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07004372 if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW) {
Laurence Lundblade9c905e82020-04-25 11:31:38 -07004373 return -2;
4374 }
4375 DCtx.uLastError = 0; // TODO: a method for this
4376
Laurence Lundbladea826c502020-05-10 21:07:00 -07004377 // 4([9223372036854775807, -4759477275222530853137]),
4378 QCBORDecode_GetInt64ConvertAll(&DCtx, QCBOR_CONVERT_TYPE_DECIMAL_FRACTION, &integer);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07004379 if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW) {
Laurence Lundbladea826c502020-05-10 21:07:00 -07004380 return -2;
4381 }
4382 DCtx.uLastError = 0; // TODO: a method for this
4383
4384 // 5([300, 100]),
Laurence Lundblade9c905e82020-04-25 11:31:38 -07004385 QCBORDecode_GetInt64ConvertAll(&DCtx, QCBOR_CONVERT_TYPE_DECIMAL_FRACTION|QCBOR_CONVERT_TYPE_BIGFLOAT, &integer);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07004386 if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW) {
Laurence Lundblade9c905e82020-04-25 11:31:38 -07004387 return -2;
4388 }
4389 DCtx.uLastError = 0; // TODO: a method for this
4390
Laurence Lundbladea826c502020-05-10 21:07:00 -07004391 // 5([-20, 4759477275222530853136]),
Laurence Lundblade9c905e82020-04-25 11:31:38 -07004392 QCBORDecode_GetInt64ConvertAll(&DCtx, QCBOR_CONVERT_TYPE_DECIMAL_FRACTION|QCBOR_CONVERT_TYPE_BIGFLOAT, &integer);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07004393 if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW) {
Laurence Lundblade9c905e82020-04-25 11:31:38 -07004394 return -2;
4395 }
4396 DCtx.uLastError = 0; // TODO: a method for this
4397
Laurence Lundbladea826c502020-05-10 21:07:00 -07004398 // 5([-9223372036854775807, -4759477275222530853137])
4399 QCBORDecode_GetInt64ConvertAll(&DCtx, QCBOR_CONVERT_TYPE_DECIMAL_FRACTION|QCBOR_CONVERT_TYPE_BIGFLOAT, &integer);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07004400 if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW) {
Laurence Lundbladea826c502020-05-10 21:07:00 -07004401 return -2;
4402 }
4403 DCtx.uLastError = 0; // TODO: a method for this
4404
4405 // 5([ 9223372036854775806, -4759477275222530853137])
4406 QCBORDecode_GetInt64ConvertAll(&DCtx, QCBOR_CONVERT_TYPE_DECIMAL_FRACTION|QCBOR_CONVERT_TYPE_BIGFLOAT, &integer);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07004407 if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW) {
Laurence Lundbladea826c502020-05-10 21:07:00 -07004408 return -2;
4409 }
4410 DCtx.uLastError = 0; // TODO: a method for this
4411
4412 // 5([ 9223372036854775806, 9223372036854775806])]
4413 QCBORDecode_GetInt64ConvertAll(&DCtx, QCBOR_CONVERT_TYPE_DECIMAL_FRACTION|QCBOR_CONVERT_TYPE_BIGFLOAT, &integer);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07004414 if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW) {
Laurence Lundbladea826c502020-05-10 21:07:00 -07004415 return -2;
4416 }
4417 DCtx.uLastError = 0; // TODO: a method for this
4418
4419
4420
4421 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedExponentsAndMantissas), 0);
4422
4423 nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
4424 if(nCBORError) {
4425 return -1;
4426 }
4427
4428 uint64_t uinteger;
4429 // 4([-1, 3]),
4430 QCBORDecode_GetUInt64ConvertAll(&DCtx, QCBOR_CONVERT_TYPE_DECIMAL_FRACTION, &uinteger);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07004431 if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW) {
Laurence Lundbladea826c502020-05-10 21:07:00 -07004432 return -2;
4433 }
4434 DCtx.uLastError = 0; // TODO: a method for this
4435
4436 // 4([-20, 4759477275222530853136]),
4437 QCBORDecode_GetUInt64ConvertAll(&DCtx, QCBOR_CONVERT_TYPE_DECIMAL_FRACTION, &uinteger);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07004438 if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW) {
Laurence Lundbladea826c502020-05-10 21:07:00 -07004439 return -2;
4440 }
4441 DCtx.uLastError = 0; // TODO: a method for this
4442
4443 // 4([9223372036854775807, -4759477275222530853137]),
4444 QCBORDecode_GetUInt64ConvertAll(&DCtx, QCBOR_CONVERT_TYPE_DECIMAL_FRACTION, &uinteger);
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07004445 if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_NUMBER_SIGN_CONVERSION) {
Laurence Lundbladea826c502020-05-10 21:07:00 -07004446 return -2;
4447 }
4448 DCtx.uLastError = 0; // TODO: a method for this
4449
4450 // 5([300, 100]),
4451 QCBORDecode_GetUInt64ConvertAll(&DCtx, QCBOR_CONVERT_TYPE_DECIMAL_FRACTION|QCBOR_CONVERT_TYPE_BIGFLOAT, &uinteger);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07004452 if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW) {
Laurence Lundbladea826c502020-05-10 21:07:00 -07004453 return -2;
4454 }
4455 DCtx.uLastError = 0; // TODO: a method for this
4456
4457 // 5([-20, 4759477275222530853136]),
4458 QCBORDecode_GetUInt64ConvertAll(&DCtx, QCBOR_CONVERT_TYPE_DECIMAL_FRACTION|QCBOR_CONVERT_TYPE_BIGFLOAT, &uinteger);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07004459 if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW) {
Laurence Lundbladea826c502020-05-10 21:07:00 -07004460 return -2;
4461 }
4462 DCtx.uLastError = 0; // TODO: a method for this
4463
4464 // 5([-9223372036854775807, -4759477275222530853137])
4465 QCBORDecode_GetUInt64ConvertAll(&DCtx, QCBOR_CONVERT_TYPE_DECIMAL_FRACTION|QCBOR_CONVERT_TYPE_BIGFLOAT, &uinteger);
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07004466 if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_NUMBER_SIGN_CONVERSION) {
Laurence Lundbladea826c502020-05-10 21:07:00 -07004467 return -2;
4468 }
4469 DCtx.uLastError = 0; // TODO: a method for this
4470
4471 // 5([ 9223372036854775806, -4759477275222530853137])
4472 QCBORDecode_GetUInt64ConvertAll(&DCtx, QCBOR_CONVERT_TYPE_DECIMAL_FRACTION|QCBOR_CONVERT_TYPE_BIGFLOAT, &uinteger);
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07004473 if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_NUMBER_SIGN_CONVERSION) {
Laurence Lundbladea826c502020-05-10 21:07:00 -07004474 return -2;
4475 }
4476 DCtx.uLastError = 0; // TODO: a method for this
4477
4478 // 5([ 9223372036854775806, 9223372036854775806])]
4479 QCBORDecode_GetUInt64ConvertAll(&DCtx, QCBOR_CONVERT_TYPE_DECIMAL_FRACTION|QCBOR_CONVERT_TYPE_BIGFLOAT, &uinteger);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07004480 if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW) {
Laurence Lundbladea826c502020-05-10 21:07:00 -07004481 return -2;
4482 }
4483 DCtx.uLastError = 0; // TODO: a method for this
4484
4485
4486
4487 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedExponentsAndMantissas), 0);
4488 nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
4489 if(nCBORError) {
4490 return -1;
4491 }
4492
4493 double dResult;
4494 // 4([-1, 3]),
4495 QCBORDecode_GetDoubleConvertAll(&DCtx, QCBOR_CONVERT_TYPE_DECIMAL_FRACTION, &dResult);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07004496 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS &&
Laurence Lundbladea826c502020-05-10 21:07:00 -07004497 dResult != 0.3) {
4498 return -2;
4499 }
4500
4501 // 4([-20, 4759477275222530853136]),
4502 QCBORDecode_GetDoubleConvertAll(&DCtx, QCBOR_CONVERT_TYPE_DECIMAL_FRACTION, &dResult);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07004503 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS &&
Laurence Lundbladea826c502020-05-10 21:07:00 -07004504 dResult != 47.408855671161923) {
4505 return -2;
4506 }
4507
4508 // 4([9223372036854775807, -4759477275222530853137]),
4509 QCBORDecode_GetDoubleConvertAll(&DCtx, QCBOR_CONVERT_TYPE_DECIMAL_FRACTION, &dResult);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07004510 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS &&
Laurence Lundbladea826c502020-05-10 21:07:00 -07004511 dResult != -INFINITY) {
4512 return -2;
4513 }
4514
4515 // 5([300, 100]),
4516 QCBORDecode_GetDoubleConvertAll(&DCtx, QCBOR_CONVERT_TYPE_BIGFLOAT, &dResult);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07004517 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS &&
Laurence Lundbladea826c502020-05-10 21:07:00 -07004518 dResult != -INFINITY) {
4519 return -2;
4520 }
4521
4522 // 5([-20, 4759477275222530853136]),
4523 QCBORDecode_GetDoubleConvertAll(&DCtx, QCBOR_CONVERT_TYPE_BIGFLOAT, &dResult);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07004524 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS &&
Laurence Lundbladea826c502020-05-10 21:07:00 -07004525 dResult != 4521260802379792.0) {
4526 return -2;
4527 }
4528
4529 // 5([-9223372036854775807, -4759477275222530853137])
4530 QCBORDecode_GetDoubleConvertAll(&DCtx, QCBOR_CONVERT_TYPE_BIGFLOAT, &dResult);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07004531 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS &&
Laurence Lundbladea826c502020-05-10 21:07:00 -07004532 dResult != -0.0) {
4533 return -2;
4534 }
4535
4536 // 5([9223372036854775806, 9223372036854775806])]
4537 QCBORDecode_GetDoubleConvertAll(&DCtx, QCBOR_CONVERT_TYPE_BIGFLOAT, &dResult);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07004538 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS &&
Laurence Lundbladea826c502020-05-10 21:07:00 -07004539 dResult != INFINITY) {
4540 return -2;
4541 }
4542
Laurence Lundbladebb87be22020-04-09 19:15:32 -07004543 return 0;
Laurence Lundbladebb87be22020-04-09 19:15:32 -07004544}
Laurence Lundblade97c61bf2020-05-02 11:24:06 -07004545
4546
Laurence Lundbladee3553422020-05-02 11:11:17 -07004547int32_t CBORSequenceDecodeTests(void)
4548{
4549 QCBORDecodeContext DCtx;
4550 QCBORItem Item;
4551 QCBORError uCBORError;
4552
4553 // --- Test a sequence with extra bytes ---
4554
4555 // The input for the date test happens to be a sequence so it
4556 // is reused. It is a sequence because it doesn't start as
4557 // an array or map.
4558 QCBORDecode_Init(&DCtx,
4559 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spDateTestInput),
4560 QCBOR_DECODE_MODE_NORMAL);
4561
4562 // Get the first item
4563 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
4564 if(uCBORError != QCBOR_SUCCESS) {
4565 return 1;
4566 }
4567 if(Item.uDataType != QCBOR_TYPE_DATE_STRING) {
4568 return 2;
4569 }
4570
4571 // Get a second item
4572 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
4573 if(uCBORError != QCBOR_SUCCESS) {
4574 return 2;
4575 }
4576 if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH) {
4577 return 3;
4578 }
4579
4580 // A sequence can have stuff at the end that may
4581 // or may not be valid CBOR. The protocol decoder knows
4582 // when to stop by definition of the protocol, not
4583 // when the top-level map or array is ended.
4584 // Finish still has to be called to know that
4585 // maps and arrays (if there were any) were closed
4586 // off correctly. When called like this it
4587 // must return the error QCBOR_ERR_EXTRA_BYTES.
4588 uCBORError = QCBORDecode_Finish(&DCtx);
4589 if(uCBORError != QCBOR_ERR_EXTRA_BYTES) {
4590 return 4;
4591 }
4592
4593
4594 // --- Test an empty input ----
4595 uint8_t empty[1];
4596 UsefulBufC Empty = {empty, 0};
4597 QCBORDecode_Init(&DCtx,
4598 Empty,
4599 QCBOR_DECODE_MODE_NORMAL);
4600
4601 uCBORError = QCBORDecode_Finish(&DCtx);
4602 if(uCBORError != QCBOR_SUCCESS) {
4603 return 5;
4604 }
4605
4606
4607 // --- Sequence with unclosed indefinite length array ---
4608 static const uint8_t xx[] = {0x01, 0x9f, 0x02};
4609
4610 QCBORDecode_Init(&DCtx,
4611 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(xx),
4612 QCBOR_DECODE_MODE_NORMAL);
4613
4614 // Get the first item
4615 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
4616 if(uCBORError != QCBOR_SUCCESS) {
4617 return 7;
4618 }
4619 if(Item.uDataType != QCBOR_TYPE_INT64) {
4620 return 8;
4621 }
4622
4623 // Get a second item
4624 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
4625 if(uCBORError != QCBOR_SUCCESS) {
4626 return 9;
4627 }
4628 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
4629 return 10;
4630 }
4631
4632 // Try to finish before consuming all bytes to confirm
4633 // that the still-open error is returned.
4634 uCBORError = QCBORDecode_Finish(&DCtx);
4635 if(uCBORError != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
4636 return 11;
4637 }
4638
4639
4640 // --- Sequence with a closed indefinite length array ---
4641 static const uint8_t yy[] = {0x01, 0x9f, 0xff};
4642
4643 QCBORDecode_Init(&DCtx,
4644 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(yy),
4645 QCBOR_DECODE_MODE_NORMAL);
4646
4647 // Get the first item
4648 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
4649 if(uCBORError != QCBOR_SUCCESS) {
4650 return 12;
4651 }
4652 if(Item.uDataType != QCBOR_TYPE_INT64) {
4653 return 13;
4654 }
4655
4656 // Get a second item
4657 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
4658 if(uCBORError != QCBOR_SUCCESS) {
4659 return 14;
4660 }
4661 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
4662 return 15;
4663 }
4664
4665 // Try to finish before consuming all bytes to confirm
4666 // that the still-open error is returned.
4667 uCBORError = QCBORDecode_Finish(&DCtx);
4668 if(uCBORError != QCBOR_SUCCESS) {
4669 return 16;
4670 }
4671
4672
4673 return 0;
4674}
4675
Laurence Lundbladee15326f2020-06-15 15:50:23 -07004676
Laurence Lundblade70ecead2020-06-15 19:40:06 -07004677
Laurence Lundbladee15326f2020-06-15 15:50:23 -07004678int32_t IntToTests()
4679{
4680 int nErrCode;
4681 int32_t n32;
4682 int16_t n16;
4683 int8_t n8;
4684 uint32_t u32;
4685 uint16_t u16;
4686 uint8_t u8;
4687 uint64_t u64;
4688
4689 nErrCode = QCBOR_Int64ToInt32(1, &n32);
4690 if(nErrCode == -1 || n32 != 1) {
4691 return 1;
4692 }
4693
4694 nErrCode = QCBOR_Int64ToInt32((int64_t)INT32_MAX, &n32);
4695 if(nErrCode == -1 || n32 != INT32_MAX) {
4696 return 2;
4697 }
4698
4699 nErrCode = QCBOR_Int64ToInt32((int64_t)INT32_MIN, &n32);
4700 if(nErrCode == -1 || n32 != INT32_MIN) {
4701 return 3;
4702 }
4703
4704 nErrCode = QCBOR_Int64ToInt32(((int64_t)INT32_MAX)+1, &n32);
4705 if(nErrCode != -1) {
4706 return 4;
4707 }
4708
4709 nErrCode = QCBOR_Int64ToInt32(((int64_t)INT32_MIN)-1, &n32);
4710 if(nErrCode != -1) {
4711 return 5;
4712 }
4713
4714
4715 nErrCode = QCBOR_Int64ToInt16((int64_t)INT16_MAX, &n16);
4716 if(nErrCode == -1 || n16 != INT16_MAX) {
4717 return 6;
4718 }
4719
4720 nErrCode = QCBOR_Int64ToInt16((int64_t)INT16_MIN, &n16);
4721 if(nErrCode == -1 || n16 != INT16_MIN) {
4722 return 7;
4723 }
4724
4725 nErrCode = QCBOR_Int64ToInt16(1, &n16);
4726 if(nErrCode == -1 || n16 != 1) {
4727 return 8;
4728 }
4729
4730 nErrCode = QCBOR_Int64ToInt16(((int64_t)INT16_MAX)+1, &n16);
4731 if(nErrCode != -1) {
4732 return 9;
4733 }
4734
4735 nErrCode = QCBOR_Int64ToInt16(((int64_t)INT16_MIN)-1, &n16);
4736 if(nErrCode != -1) {
4737 return 10;
4738 }
4739
4740
4741 nErrCode = QCBOR_Int64ToInt8(1, &n8);
4742 if(nErrCode == -1 || n8 != 1) {
4743 return 11;
4744 }
4745
4746 nErrCode = QCBOR_Int64ToInt8((int64_t)INT8_MAX, &n8);
4747 if(nErrCode == -1 || n8 != INT8_MAX) {
4748 return 12;
4749 }
4750
4751 nErrCode = QCBOR_Int64ToInt8((int64_t)INT8_MIN, &n8);
4752 if(nErrCode == -1 || n8 != INT8_MIN) {
4753 return 13;
4754 }
4755
4756 nErrCode = QCBOR_Int64ToInt8(((int64_t)INT8_MAX)+1, &n8);
4757 if(nErrCode != -1) {
4758 return 14;
4759 }
4760
4761 nErrCode = QCBOR_Int64ToInt8(((int64_t)INT8_MIN)-1, &n8);
4762 if(nErrCode != -1) {
4763 return 15;
4764 }
4765
4766
4767 nErrCode = QCBOR_Int64ToUInt32(1, &u32);
4768 if(nErrCode == -1 || u32 != 1) {
4769 return 16;
4770 }
4771
4772 nErrCode = QCBOR_Int64ToUInt32((int64_t)UINT32_MAX, &u32);
4773 if(nErrCode == -1 || u32 != UINT32_MAX) {
4774 return 17;
4775 }
4776
4777 nErrCode = QCBOR_Int64ToUInt32((int64_t)0, &u32);
4778 if(nErrCode == -1 || u32 != 0) {
4779 return 18;
4780 }
4781
4782 nErrCode = QCBOR_Int64ToUInt32(((int64_t)UINT32_MAX)+1, &u32);
4783 if(nErrCode != -1) {
4784 return 19;
4785 }
4786
4787 nErrCode = QCBOR_Int64ToUInt32((int64_t)-1, &u32);
4788 if(nErrCode != -1) {
4789 return 20;
4790 }
4791
4792
4793 nErrCode = QCBOR_Int64UToInt16((int64_t)UINT16_MAX, &u16);
4794 if(nErrCode == -1 || u16 != UINT16_MAX) {
4795 return 21;
4796 }
4797
4798 nErrCode = QCBOR_Int64UToInt16((int64_t)0, &u16);
4799 if(nErrCode == -1 || u16 != 0) {
4800 return 22;
4801 }
4802
4803 nErrCode = QCBOR_Int64UToInt16(1, &u16);
4804 if(nErrCode == -1 || u16 != 1) {
4805 return 23;
4806 }
4807
4808 nErrCode = QCBOR_Int64UToInt16(((int64_t)UINT16_MAX)+1, &u16);
4809 if(nErrCode != -1) {
4810 return 24;
4811 }
4812
4813 nErrCode = QCBOR_Int64UToInt16((int64_t)-1, &u16);
4814 if(nErrCode != -1) {
4815 return 25;
4816 }
4817
4818
4819 nErrCode = QCBOR_Int64ToUInt8((int64_t)UINT8_MAX, &u8);
4820 if(nErrCode == -1 || u8 != UINT8_MAX) {
4821 return 26;
4822 }
4823
4824 nErrCode = QCBOR_Int64ToUInt8((int64_t)0, &u8);
4825 if(nErrCode == -1 || u8 != 0) {
4826 return 27;
4827 }
4828
4829 nErrCode = QCBOR_Int64ToUInt8(1, &u8);
4830 if(nErrCode == -1 || u8 != 1) {
4831 return 28;
4832 }
4833
4834 nErrCode = QCBOR_Int64ToUInt8(((int64_t)UINT16_MAX)+1, &u8);
4835 if(nErrCode != -1) {
4836 return 29;
4837 }
4838
4839 nErrCode = QCBOR_Int64ToUInt8((int64_t)-1, &u8);
4840 if(nErrCode != -1) {
4841 return 30;
4842 }
4843
4844
4845 nErrCode = QCBOR_Int64ToUInt64(1, &u64);
4846 if(nErrCode == -1 || u64 != 1) {
4847 return 31;
4848 }
4849
4850 nErrCode = QCBOR_Int64ToUInt64(INT64_MAX, &u64);
4851 if(nErrCode == -1 || u64 != INT64_MAX) {
4852 return 32;
4853 }
4854
4855 nErrCode = QCBOR_Int64ToUInt64((int64_t)0, &u64);
4856 if(nErrCode == -1 || u64 != 0) {
4857 return 33;
4858 }
4859
4860 nErrCode = QCBOR_Int64ToUInt64((int64_t)-1, &u64);
4861 if(nErrCode != -1) {
4862 return 34;
4863 }
4864
4865 return 0;
4866}
4867
Laurence Lundblade0750fc42020-06-20 21:02:34 -07004868
4869/*
4870A sequence with
4871 A wrapping bstr
4872 containing a map
4873 1
4874 2
4875 A wrapping bstr
4876 containing an array
4877 3
4878 wrapping bstr
4879 4
4880 5
4881 6
4882 array
4883 7
4884 8
4885
4886 */
4887
4888static UsefulBufC foo(UsefulBuf ffo)
4889{
4890 UsefulBufC Encoded;
4891 QCBOREncodeContext EC;
4892 QCBORError uErr;
4893
4894 QCBOREncode_Init(&EC, ffo);
4895
4896 QCBOREncode_BstrWrap(&EC);
4897 QCBOREncode_OpenMap(&EC);
4898 QCBOREncode_AddInt64ToMapN(&EC, 100, 1);
4899 QCBOREncode_AddInt64ToMapN(&EC, 200, 2);
4900 QCBOREncode_CloseMap(&EC);
4901 QCBOREncode_BstrWrap(&EC);
4902 QCBOREncode_OpenArray(&EC);
4903 QCBOREncode_AddInt64(&EC, 3);
4904 QCBOREncode_BstrWrap(&EC);
4905 QCBOREncode_AddInt64(&EC, 4);
4906 QCBOREncode_CloseBstrWrap(&EC, NULL);
4907 QCBOREncode_AddInt64(&EC, 5);
4908 QCBOREncode_CloseArray(&EC);
4909 QCBOREncode_CloseBstrWrap(&EC, NULL);
4910 QCBOREncode_AddInt64(&EC, 6);
4911 QCBOREncode_CloseBstrWrap(&EC, NULL);
4912 QCBOREncode_OpenArray(&EC);
4913 QCBOREncode_AddInt64(&EC, 7);
4914 QCBOREncode_AddInt64(&EC, 8);
4915 QCBOREncode_CloseArray(&EC);
4916
4917 uErr = QCBOREncode_Finish(&EC, &Encoded);
4918
4919 return Encoded;
4920}
4921
4922
4923int32_t EnterBstrTest()
4924{
4925 MakeUsefulBufOnStack(ffo, 100);
4926
4927 QCBORDecodeContext DC;
4928
4929 QCBORDecode_Init(&DC, foo(ffo), 0);
4930
4931 int64_t i1, i2, i3, i4, i5, i6, i7, i8;
4932
4933
4934 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAGSPEC_MATCH_TAG_CONTENT_TYPE, NULL);
4935 QCBORDecode_EnterMap(&DC);
4936 QCBORDecode_GetInt64InMapN(&DC, 100, &i1);
4937 QCBORDecode_GetInt64InMapN(&DC, 200, &i2);
4938 QCBORDecode_ExitMap(&DC);
4939 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAGSPEC_MATCH_TAG_CONTENT_TYPE, NULL);
4940 QCBORDecode_EnterArray(&DC);
4941 QCBORDecode_GetInt64(&DC, &i3);
4942 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAGSPEC_MATCH_TAG_CONTENT_TYPE, NULL);
4943 QCBORDecode_GetInt64(&DC, &i4);
4944 QCBORDecode_ExitBstrWrapped(&DC);
4945 QCBORDecode_GetInt64(&DC, &i5);
4946 QCBORDecode_ExitArray(&DC);
4947 QCBORDecode_ExitBstrWrapped(&DC);
4948 QCBORDecode_GetInt64(&DC, &i6);
4949 QCBORDecode_ExitBstrWrapped(&DC);
4950 QCBORDecode_EnterArray(&DC);
4951 QCBORDecode_GetInt64(&DC, &i7);
4952 QCBORDecode_GetInt64(&DC, &i8);
4953 QCBORDecode_ExitArray(&DC);
4954
4955 QCBORError uErr = QCBORDecode_Finish(&DC);
4956
4957 return (int32_t)uErr;
4958}