blob: 2f2614d2256c2ba1872177022d41d8cd358b2e0e [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 Lundbladef0ea5f32019-01-11 20:10:26 -08003 Copyright (c) 2018-2019, 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 Lundblade9e3651c2018-10-10 11:49:55 +080031 ==============================================================================*/
32
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080033#include "qcbor_decode_tests.h"
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -080034#include "qcbor.h"
Laurence Lundbladed4728fd2018-12-17 15:15:56 -080035#include <string.h>
Laurence Lundblade9e3651c2018-10-10 11:49:55 +080036#include <math.h> // for fabs()
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -080037
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080038
Laurence Lundbladea2e29072018-12-30 09:20:06 -080039#ifdef PRINT_FUNCTIONS_FOR_DEBUGGING
Laurence Lundblade20db9c92018-12-17 11:40:37 -080040#include <stdio.h>
Laurence Lundbladea2e29072018-12-30 09:20:06 -080041
42static void PrintUsefulBufC(const char *szLabel, UsefulBufC Buf)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080043{
44 if(szLabel) {
45 printf("%s ", szLabel);
46 }
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -080047
Laurence Lundblade570fab52018-10-13 18:28:27 +080048 size_t i;
Laurence Lundbladea2e29072018-12-30 09:20:06 -080049 for(i = 0; i < Buf.len; i++) {
50 uint8_t Z = ((uint8_t *)Buf.ptr)[i];
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080051 printf("%02x ", Z);
52 }
53 printf("\n");
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -080054
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080055 fflush(stdout);
56}
Laurence Lundbladea2e29072018-12-30 09:20:06 -080057
58/*static void printencoded(const char *szLabel, const uint8_t *pEncoded, size_t nLen)
59{
60 PrintUsefulBufC(szLabel, (UsefulBufC){pEncoded, nLen});
61}*/
Laurence Lundblade20db9c92018-12-17 11:40:37 -080062#endif
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080063
64
Laurence Lundbladeb836efb2018-10-28 20:09:58 +070065static const uint8_t spExpectedEncodedInts[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080066 0x98, 0x2f, 0x3b, 0x7f, 0xff, 0xff, 0xff, 0xff,
67 0xff, 0xff, 0xff, 0x3b, 0x00, 0x00, 0x00, 0x01,
68 0x00, 0x00, 0x00, 0x00, 0x3a, 0xff, 0xff, 0xff,
69 0xff, 0x3a, 0xff, 0xff, 0xff, 0xfe, 0x3a, 0xff,
70 0xff, 0xff, 0xfd, 0x3a, 0x7f, 0xff, 0xff, 0xff,
71 0x3a, 0x7f, 0xff, 0xff, 0xfe, 0x3a, 0x00, 0x01,
72 0x00, 0x01, 0x3a, 0x00, 0x01, 0x00, 0x00, 0x39,
73 0xff, 0xff, 0x39, 0xff, 0xfe, 0x39, 0xff, 0xfd,
74 0x39, 0x01, 0x00, 0x38, 0xff, 0x38, 0xfe, 0x38,
75 0xfd, 0x38, 0x18, 0x37, 0x36, 0x20, 0x00, 0x00,
76 0x01, 0x16, 0x17, 0x18, 0x18, 0x18, 0x19, 0x18,
77 0x1a, 0x18, 0xfe, 0x18, 0xff, 0x19, 0x01, 0x00,
78 0x19, 0x01, 0x01, 0x19, 0xff, 0xfe, 0x19, 0xff,
79 0xff, 0x1a, 0x00, 0x01, 0x00, 0x00, 0x1a, 0x00,
80 0x01, 0x00, 0x01, 0x1a, 0x00, 0x01, 0x00, 0x02,
81 0x1a, 0x7f, 0xff, 0xff, 0xff, 0x1a, 0x7f, 0xff,
82 0xff, 0xff, 0x1a, 0x80, 0x00, 0x00, 0x00, 0x1a,
83 0x80, 0x00, 0x00, 0x01, 0x1a, 0xff, 0xff, 0xff,
84 0xfe, 0x1a, 0xff, 0xff, 0xff, 0xff, 0x1b, 0x00,
85 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x1b,
86 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01,
87 0x1b, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
88 0xff, 0x1b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
89 0xff, 0xff};
90
91
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080092// return CBOR error or -1 if type of value doesn't match
93
94static int IntegerValuesParseTestInternal(QCBORDecodeContext *pDCtx)
95{
96 QCBORItem Item;
97 int nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -080098
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080099 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
100 return nCBORError;
101 if(Item.uDataType != QCBOR_TYPE_ARRAY)
102 return -1;
103
104 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
105 return nCBORError;
106 if(Item.uDataType != QCBOR_TYPE_INT64 || // Todo; fix this for 32-bit machines
Laurence Lundblade570fab52018-10-13 18:28:27 +0800107 Item.val.int64 != -9223372036854775807LL - 1)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800108 return -1;
109
110 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
111 return nCBORError;
112 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade570fab52018-10-13 18:28:27 +0800113 Item.val.int64 != -4294967297)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800114 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800115
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800116 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
117 return nCBORError;
118 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade570fab52018-10-13 18:28:27 +0800119 Item.val.int64 != -4294967296)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800120 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800121
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800122 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
123 return nCBORError;
124 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade570fab52018-10-13 18:28:27 +0800125 Item.val.int64 != -4294967295)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800126 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800127
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800128 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
129 return nCBORError;
130 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade570fab52018-10-13 18:28:27 +0800131 Item.val.int64 != -4294967294)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800132 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800133
134
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800135 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
136 return nCBORError;
137 if(Item.uDataType != QCBOR_TYPE_INT64 ||
138 Item.val.int64 != -2147483648)
139 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800140
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800141 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
142 return nCBORError;
143 if(Item.uDataType != QCBOR_TYPE_INT64 ||
144 Item.val.int64 != -2147483647)
145 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800146
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800147 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
148 return nCBORError;
149 if(Item.uDataType != QCBOR_TYPE_INT64 ||
150 Item.val.int64 != -65538)
151 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800152
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800153 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
154 return nCBORError;
155 if(Item.uDataType != QCBOR_TYPE_INT64 ||
156 Item.val.int64 != -65537)
157 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800158
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800159 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
160 return nCBORError;
161 if(Item.uDataType != QCBOR_TYPE_INT64 ||
162 Item.val.int64 != -65536)
163 return -1;
164
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800165
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800166 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
167 return nCBORError;
168 if(Item.uDataType != QCBOR_TYPE_INT64 ||
169 Item.val.int64 != -65535)
170 return -1;
171
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800172
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800173 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
174 return nCBORError;
175 if(Item.uDataType != QCBOR_TYPE_INT64 ||
176 Item.val.int64 != -65534)
177 return -1;
178
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800179
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800180 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
181 return nCBORError;
182 if(Item.uDataType != QCBOR_TYPE_INT64 ||
183 Item.val.int64 != -257)
184 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800185
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800186 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
187 return nCBORError;
188 if(Item.uDataType != QCBOR_TYPE_INT64 ||
189 Item.val.int64 != -256)
190 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800191
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800192 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
193 return nCBORError;
194 if(Item.uDataType != QCBOR_TYPE_INT64 ||
195 Item.val.int64 != -255)
196 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800197
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800198 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
199 return nCBORError;
200 if(Item.uDataType != QCBOR_TYPE_INT64 ||
201 Item.val.int64 != -254)
202 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800203
204
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800205 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
206 return nCBORError;
207 if(Item.uDataType != QCBOR_TYPE_INT64 ||
208 Item.val.int64 != -25)
209 return -1;
210
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800211
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800212 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
213 return nCBORError;
214 if(Item.uDataType != QCBOR_TYPE_INT64 ||
215 Item.val.int64 != -24)
216 return -1;
217
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800218
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800219 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
220 return nCBORError;
221 if(Item.uDataType != QCBOR_TYPE_INT64 ||
222 Item.val.int64 != -23)
223 return -1;
224
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800225
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800226 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
227 return nCBORError;
228 if(Item.uDataType != QCBOR_TYPE_INT64 ||
229 Item.val.int64 != -1)
230 return -1;
231
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800232
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800233 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
234 return nCBORError;
235 if(Item.uDataType != QCBOR_TYPE_INT64 ||
236 Item.val.int64 != 0)
237 return -1;
238
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800239
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800240 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
241 return nCBORError;
242 if(Item.uDataType != QCBOR_TYPE_INT64 ||
243 Item.val.int64 != 0)
244 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800245
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800246 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
247 return nCBORError;
248 if(Item.uDataType != QCBOR_TYPE_INT64 ||
249 Item.val.int64 != 1)
250 return -1;
251
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800252
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800253 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
254 return nCBORError;
255 if(Item.uDataType != QCBOR_TYPE_INT64 ||
256 Item.val.int64 != 22)
257 return -1;
258
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800259
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800260 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
261 return nCBORError;
262 if(Item.uDataType != QCBOR_TYPE_INT64 ||
263 Item.val.int64 != 23)
264 return -1;
265
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800266
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800267 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
268 return nCBORError;
269 if(Item.uDataType != QCBOR_TYPE_INT64 ||
270 Item.val.int64 != 24)
271 return -1;
272
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800273
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800274 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
275 return nCBORError;
276 if(Item.uDataType != QCBOR_TYPE_INT64 ||
277 Item.val.int64 != 25)
278 return -1;
279
280 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
281 return nCBORError;
282 if(Item.uDataType != QCBOR_TYPE_INT64 ||
283 Item.val.int64 != 26)
284 return -1;
285
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800286
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800287 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
288 return nCBORError;
289 if(Item.uDataType != QCBOR_TYPE_INT64 ||
290 Item.val.int64 != 254)
291 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800292
293
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800294 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
295 return nCBORError;
296 if(Item.uDataType != QCBOR_TYPE_INT64 ||
297 Item.val.int64 != 255)
298 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800299
300
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800301 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
302 return nCBORError;
303 if(Item.uDataType != QCBOR_TYPE_INT64 ||
304 Item.val.int64 != 256)
305 return -1;
306
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800307
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800308 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
309 return nCBORError;
310 if(Item.uDataType != QCBOR_TYPE_INT64 ||
311 Item.val.int64 != 257)
312 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800313
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800314 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
315 return nCBORError;
316 if(Item.uDataType != QCBOR_TYPE_INT64 ||
317 Item.val.int64 != 65534)
318 return -1;
319
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800320
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800321 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
322 return nCBORError;
323 if(Item.uDataType != QCBOR_TYPE_INT64 ||
324 Item.val.int64 != 65535)
325 return -1;
326
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800327
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800328 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
329 return nCBORError;
330 if(Item.uDataType != QCBOR_TYPE_INT64 ||
331 Item.val.int64 != 65536)
332 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800333
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800334 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
335 return nCBORError;
336 if(Item.uDataType != QCBOR_TYPE_INT64 ||
337 Item.val.int64 != 65537)
338 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800339
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800340 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
341 return nCBORError;
342 if(Item.uDataType != QCBOR_TYPE_INT64 ||
343 Item.val.int64 != 65538)
344 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800345
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800346 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
347 return nCBORError;
348 if(Item.uDataType != QCBOR_TYPE_INT64 ||
349 Item.val.int64 != 2147483647)
350 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800351
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800352 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
353 return nCBORError;
354 if(Item.uDataType != QCBOR_TYPE_INT64 ||
355 Item.val.int64 != 2147483647)
356 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800357
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800358 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
359 return nCBORError;
360 if(Item.uDataType != QCBOR_TYPE_INT64 ||
361 Item.val.int64 != 2147483648)
362 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800363
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800364 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
365 return nCBORError;
366 if(Item.uDataType != QCBOR_TYPE_INT64 ||
367 Item.val.int64 != 2147483649)
368 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800369
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800370 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
371 return nCBORError;
372 if(Item.uDataType != QCBOR_TYPE_INT64 ||
373 Item.val.int64 != 4294967294)
374 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800375
376
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800377 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
378 return nCBORError;
379 if(Item.uDataType != QCBOR_TYPE_INT64 ||
380 Item.val.int64 != 4294967295)
381 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800382
383
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800384 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
385 return nCBORError;
386 if(Item.uDataType != QCBOR_TYPE_INT64 ||
387 Item.val.int64 != 4294967296)
388 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800389
390
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800391 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
392 return nCBORError;
393 if(Item.uDataType != QCBOR_TYPE_INT64 ||
394 Item.val.int64 != 4294967297)
395 return -1;
396
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800397
398
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800399 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
400 return nCBORError;
401 if(Item.uDataType != QCBOR_TYPE_INT64 ||
402 Item.val.int64 != 9223372036854775807LL)
403 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800404
405
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800406 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
407 return nCBORError;
408 if(Item.uDataType != QCBOR_TYPE_UINT64 ||
409 Item.val.uint64 != 18446744073709551615ULL)
410 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800411
412
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800413 if(QCBORDecode_Finish(pDCtx) != QCBOR_SUCCESS) {
414 return -1;
415 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800416
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800417 return 0;
418}
419
420
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800421/*
422 Tests the decoding of lots of different integers sizes
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +0800423 and values.
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800424 */
425
426int IntegerValuesParseTest()
427{
428 int n;
429 QCBORDecodeContext DCtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800430
Laurence Lundbladeb836efb2018-10-28 20:09:58 +0700431 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedEncodedInts), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800432
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800433 n = IntegerValuesParseTestInternal(&DCtx);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800434
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800435 return(n);
436}
437
438
439/*
440 Creates a simple CBOR array and returns it in *pEncoded. The array is malloced
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800441 and needs to be freed. This is used by several tests.
442
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800443 Two of the inputs can be set. Two other items in the array are fixed.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800444
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800445 */
446
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -0800447static uint8_t spSimpleArrayBuffer[50];
448
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800449static int CreateSimpleArray(int nInt1, int nInt2, uint8_t **pEncoded, size_t *pEncodedLen)
450{
451 QCBOREncodeContext ECtx;
452 int nReturn = -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800453
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800454 *pEncoded = NULL;
455 *pEncodedLen = INT32_MAX;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800456
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800457 // loop runs CBOR encoding twice. First with no buffer to
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -0800458 // calculate the length so buffer can be allocated correctly,
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800459 // and last with the buffer to do the actual encoding
460 do {
Laurence Lundblade0595e932018-11-02 22:22:47 +0700461 QCBOREncode_Init(&ECtx, (UsefulBuf){*pEncoded, *pEncodedLen});
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800462 QCBOREncode_OpenArray(&ECtx);
463 QCBOREncode_AddInt64(&ECtx, nInt1);
464 QCBOREncode_AddInt64(&ECtx, nInt2);
465 QCBOREncode_AddBytes(&ECtx, ((UsefulBufC) {"galactic", 8}));
466 QCBOREncode_AddBytes(&ECtx, ((UsefulBufC) {"haven token", 11}));
467 QCBOREncode_CloseArray(&ECtx);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800468
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -0800469 if(QCBOREncode_FinishGetSize(&ECtx, pEncodedLen))
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800470 goto Done;
471
472 if(*pEncoded != NULL) {
473 nReturn = 0;
474 goto Done;
475 }
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -0800476
477 // Use static buffer to avoid dependency on malloc()
478 if(*pEncodedLen > sizeof(spSimpleArrayBuffer)) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800479 goto Done;
480 }
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -0800481 *pEncoded = spSimpleArrayBuffer;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800482
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800483 } while(1);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800484
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -0800485Done:
486 return nReturn;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800487}
488
489
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800490/*
491 {"first integer": 42,
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900492 "an array of two strings": [
493 "string1", "string2"
494 ],
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800495 "map in a map": {
496 "bytes 1": h'78787878',
497 "bytes 2": h'79797979',
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900498 "another int": 98,
499 "text 2": "lies, damn lies and statistics"
500 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800501 }
502 */
503
504static uint8_t pValidMapEncoded[] = {
Laurence Lundbladeb836efb2018-10-28 20:09:58 +0700505 0xa3, 0x6d, 0x66, 0x69, 0x72, 0x73, 0x74, 0x20, 0x69, 0x6e,
506 0x74, 0x65, 0x67, 0x65, 0x72, 0x18, 0x2a, 0x77, 0x61, 0x6e,
507 0x20, 0x61, 0x72, 0x72, 0x61, 0x79, 0x20, 0x6f, 0x66, 0x20,
508 0x74, 0x77, 0x6f, 0x20, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67,
509 0x73, 0x82, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x31,
510 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x32, 0x6c, 0x6d,
511 0x61, 0x70, 0x20, 0x69, 0x6e, 0x20, 0x61, 0x20, 0x6d, 0x61,
512 0x70, 0xa4, 0x67, 0x62, 0x79, 0x74, 0x65, 0x73, 0x20, 0x31,
513 0x44, 0x78, 0x78, 0x78, 0x78, 0x67, 0x62, 0x79, 0x74, 0x65,
514 0x73, 0x20, 0x32, 0x44, 0x79, 0x79, 0x79, 0x79, 0x6b, 0x61,
515 0x6e, 0x6f, 0x74, 0x68, 0x65, 0x72, 0x20, 0x69, 0x6e, 0x74,
516 0x18, 0x62, 0x66, 0x74, 0x65, 0x78, 0x74, 0x20, 0x32, 0x78,
517 0x1e, 0x6c, 0x69, 0x65, 0x73, 0x2c, 0x20, 0x64, 0x61, 0x6d,
518 0x6e, 0x20, 0x6c, 0x69, 0x65, 0x73, 0x20, 0x61, 0x6e, 0x64,
519 0x20, 0x73, 0x74, 0x61, 0x74, 0x69, 0x73, 0x74, 0x69, 0x63,
520 0x73 } ;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800521
522static int ParseOrderedArray(const uint8_t *pEncoded, size_t nLen, int64_t *pInt1, int64_t *pInt2, const uint8_t **pBuf3, size_t *pBuf3Len, const uint8_t **pBuf4, size_t *pBuf4Len)
523{
524 QCBORDecodeContext DCtx;
525 QCBORItem Item;
526 int nReturn = -1; // assume error until success
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800527
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800528 QCBORDecode_Init(&DCtx, (UsefulBufC){pEncoded, nLen}, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800529
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800530 // Make sure the first thing is a map
531 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || Item.uDataType != QCBOR_TYPE_ARRAY)
532 goto Done;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800533
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800534 // First integer
Laurence Lundblade12b495d2018-12-17 11:15:54 -0800535 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || Item.uDataType != QCBOR_TYPE_INT64)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800536 goto Done;
537 *pInt1 = Item.val.int64;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800538
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800539 // Second integer
540 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || Item.uDataType != QCBOR_TYPE_INT64)
541 goto Done;
542 *pInt2 = Item.val.int64;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800543
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800544 // First string
545 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || Item.uDataType != QCBOR_TYPE_BYTE_STRING)
546 goto Done;
547 *pBuf3 = Item.val.string.ptr;
548 *pBuf3Len = Item.val.string.len;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800549
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800550 // Second string
551 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || Item.uDataType != QCBOR_TYPE_BYTE_STRING)
552 goto Done;
553 *pBuf4 = Item.val.string.ptr;
554 *pBuf4Len = Item.val.string.len;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800555
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800556 nReturn = 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800557
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800558Done:
559 return(nReturn);
560}
561
562
563
Laurence Lundblade9e3651c2018-10-10 11:49:55 +0800564
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800565int SimpleArrayTest()
566{
567 uint8_t *pEncoded;
568 size_t nEncodedLen;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800569
Laurence Lundblade5e390822019-01-06 12:35:01 -0800570 int64_t i1=0, i2=0;
571 size_t i3=0, i4=0;
572 const uint8_t *s3= (uint8_t *)"";
573 const uint8_t *s4= (uint8_t *)"";
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800574
575
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800576 if(CreateSimpleArray(23, 6000, &pEncoded, &nEncodedLen) < 0) {
577 return(-1);
578 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800579
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800580 ParseOrderedArray(pEncoded, nEncodedLen, &i1, &i2, &s3, &i3, &s4, &i4);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800581
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800582 if(i1 != 23 ||
583 i2 != 6000 ||
584 i3 != 8 ||
585 i4 != 11 ||
Laurence Lundbladea44d5062018-10-17 18:45:12 +0530586 memcmp("galactic", s3, 8) !=0 ||
587 memcmp("haven token", s4, 11) !=0) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800588 return(-1);
589 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800590
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800591 return(0);
592}
593
594
Laurence Lundblade9e3651c2018-10-10 11:49:55 +0800595
Laurence Lundbladeb836efb2018-10-28 20:09:58 +0700596static uint8_t spDeepArrays[] = {0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x80};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800597
598int ParseDeepArrayTest()
599{
600 QCBORDecodeContext DCtx;
601 int nReturn = 0;
602 int i;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800603
Laurence Lundbladeb836efb2018-10-28 20:09:58 +0700604 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spDeepArrays), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800605
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800606 for(i = 0; i < 10; i++) {
607 QCBORItem Item;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800608
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800609 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
610 Item.uDataType != QCBOR_TYPE_ARRAY ||
611 Item.uNestingLevel != i) {
612 nReturn = -1;
613 break;
614 }
615 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800616
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800617 return(nReturn);
618}
619
Laurence Lundblade972e59c2018-11-11 15:57:23 +0700620// Big enough to test nesting to the depth of 24
621static uint8_t spTooDeepArrays[] = {0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81,
622 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81,
623 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81,
624 0x80};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800625
626int ParseTooDeepArrayTest()
627{
628 QCBORDecodeContext DCtx;
629 int nReturn = 0;
630 int i;
631 QCBORItem Item;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800632
633
Laurence Lundbladeb836efb2018-10-28 20:09:58 +0700634 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTooDeepArrays), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800635
Laurence Lundblade972e59c2018-11-11 15:57:23 +0700636 for(i = 0; i < QCBOR_MAX_ARRAY_NESTING1; i++) {
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800637
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800638 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
639 Item.uDataType != QCBOR_TYPE_ARRAY ||
640 Item.uNestingLevel != i) {
641 nReturn = -1;
642 break;
643 }
644 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800645
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800646 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_ARRAY_NESTING_TOO_DEEP)
647 nReturn = -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800648
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800649 return(nReturn);
650}
651
652
653
654
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800655int ShortBufferParseTest()
656{
657 int nResult = 0;
658 QCBORDecodeContext DCtx;
659 int num;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800660
Laurence Lundbladeb836efb2018-10-28 20:09:58 +0700661 for(num = sizeof(spExpectedEncodedInts)-1; num; num--) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800662 int n;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800663
Laurence Lundbladeb836efb2018-10-28 20:09:58 +0700664 QCBORDecode_Init(&DCtx, (UsefulBufC){spExpectedEncodedInts, num}, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800665
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800666 n = IntegerValuesParseTestInternal(&DCtx);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800667
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800668 //printf("Len %d, result: %d\n", num, n);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800669
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800670 if(n != QCBOR_ERR_HIT_END) {
671 nResult = -1;
672 goto Done;
673 }
674 }
675Done:
676 return nResult;
677}
678
679
Laurence Lundblade9e3651c2018-10-10 11:49:55 +0800680
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800681int ShortBufferParseTest2()
682{
683 uint8_t *pEncoded;
684 int nReturn;
685 size_t nEncodedLen;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800686
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800687 int64_t i1, i2;
688 size_t i3, i4;
689 const uint8_t *s3, *s4;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800690
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800691 nReturn = 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800692
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800693 if(CreateSimpleArray(23, 6000, &pEncoded, &nEncodedLen) < 0) {
694 return(-1);
695 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800696
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800697 for(nEncodedLen--; nEncodedLen; nEncodedLen--) {
698 int nResult = ParseOrderedArray(pEncoded, (uint32_t)nEncodedLen, &i1, &i2, &s3, &i3, &s4, &i4);
699 if(nResult == 0) {
700 nReturn = -1;
701 }
702 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800703
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800704 return(nReturn);
705}
706
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530707/*
708 Decode and thoroughly check a moderately complex
709 set of maps
710 */
Laurence Lundbladeea567ac2018-12-09 14:03:21 -0800711static int ParseMapTest1(QCBORDecodeMode nMode)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800712{
713 QCBORDecodeContext DCtx;
714 QCBORItem Item;
715 int nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800716
Laurence Lundbladeea567ac2018-12-09 14:03:21 -0800717 QCBORDecode_Init(&DCtx, (UsefulBufC){pValidMapEncoded, sizeof(pValidMapEncoded)}, nMode);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800718
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900719 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800720 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900721 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800722 if(Item.uDataType != QCBOR_TYPE_MAP ||
723 Item.val.uCount != 3)
724 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800725
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900726 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800727 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900728 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800729 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800730 Item.uDataType != QCBOR_TYPE_INT64 ||
731 Item.val.int64 != 42 ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530732 Item.uDataAlloc ||
733 Item.uLabelAlloc ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900734 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("first integer"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800735 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900736 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800737
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900738 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800739 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900740 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800741 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530742 Item.uDataAlloc ||
743 Item.uLabelAlloc ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900744 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("an array of two strings")) ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800745 Item.uDataType != QCBOR_TYPE_ARRAY ||
746 Item.val.uCount != 2)
747 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800748
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900749 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800750 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900751 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800752 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530753 Item.uDataAlloc ||
754 Item.uLabelAlloc ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900755 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("string1"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800756 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900757 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800758
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900759 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800760 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900761 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800762 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530763 Item.uDataAlloc ||
764 Item.uLabelAlloc ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900765 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("string2"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800766 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900767 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800768
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900769 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800770 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900771 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800772 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530773 Item.uDataAlloc ||
774 Item.uLabelAlloc ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900775 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("map in a map")) ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800776 Item.uDataType != QCBOR_TYPE_MAP ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900777 Item.val.uCount != 4) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800778 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900779 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800780
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900781 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800782 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900783 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800784 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900785 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("bytes 1"))||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800786 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530787 Item.uDataAlloc ||
788 Item.uLabelAlloc ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900789 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("xxxx"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800790 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900791 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800792
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900793 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800794 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900795 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800796 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900797 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("bytes 2")) ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800798 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530799 Item.uDataAlloc ||
800 Item.uLabelAlloc ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900801 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("yyyy"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800802 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900803 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800804
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900805 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800806 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900807 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800808 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530809 Item.uDataAlloc ||
810 Item.uLabelAlloc ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900811 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("another int")) ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800812 Item.uDataType != QCBOR_TYPE_INT64 ||
813 Item.val.int64 != 98)
814 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800815
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900816 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800817 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900818 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800819 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900820 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("text 2"))||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800821 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530822 Item.uDataAlloc ||
823 Item.uLabelAlloc ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900824 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("lies, damn lies and statistics"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800825 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900826 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800827
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800828 return 0;
829}
830
831
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900832/*
833 Decode and thoroughly check a moderately complex
834 set of maps
835 */
836int ParseMapAsArrayTest()
837{
838 QCBORDecodeContext DCtx;
839 QCBORItem Item;
840 int nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800841
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900842 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), QCBOR_DECODE_MODE_MAP_AS_ARRAY);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800843
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900844 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
845 return nCBORError;
846 }
Laurence Lundbladed61cbf32018-12-09 11:42:21 -0800847 if(Item.uDataType != QCBOR_TYPE_MAP_AS_ARRAY ||
848 Item.val.uCount != 6) {
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900849 return -1;
850 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800851
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900852 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
853 return nCBORError;
854 }
855 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
856 Item.uDataAlloc ||
857 Item.uLabelAlloc ||
858 Item.uLabelType != QCBOR_TYPE_NONE ||
859 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("first integer"))) {
860 return -2;
861 }
862
863 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
864 return nCBORError;
865 }
866 if(Item.uLabelType != QCBOR_TYPE_NONE ||
867 Item.uDataType != QCBOR_TYPE_INT64 ||
868 Item.val.int64 != 42 ||
869 Item.uDataAlloc ||
870 Item.uLabelAlloc) {
871 return -3;
872 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800873
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900874 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
875 return nCBORError;
876 }
877 if(Item.uLabelType != QCBOR_TYPE_NONE ||
878 Item.uDataAlloc ||
879 Item.uLabelAlloc ||
880 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("an array of two strings")) ||
881 Item.uDataType != QCBOR_TYPE_TEXT_STRING) {
882 return -4;
883 }
884
885 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
886 return nCBORError;
887 }
888 if(Item.uLabelType != QCBOR_TYPE_NONE ||
889 Item.uDataAlloc ||
890 Item.uLabelAlloc ||
891 Item.uDataType != QCBOR_TYPE_ARRAY ||
892 Item.val.uCount != 2) {
893 return -5;
894 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800895
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900896 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
897 return nCBORError;
898 }
899 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
900 Item.val.string.len != 7 ||
901 Item.uDataAlloc ||
902 Item.uLabelAlloc ||
903 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("string1"))) {
904 return -6;
905 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800906
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900907 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
908 return nCBORError;
909 }
910 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
911 Item.uDataAlloc ||
912 Item.uLabelAlloc ||
913 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("string2"))) {
914 return -7;
915 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800916
917
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900918 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
919 return nCBORError;
920 }
921 if(Item.uLabelType != QCBOR_TYPE_NONE ||
922 Item.uDataAlloc ||
923 Item.uLabelAlloc ||
924 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("map in a map"))) {
925 return -8;
926 }
927
928 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
929 return nCBORError;
930 }
931 if(Item.uLabelType != QCBOR_TYPE_NONE ||
932 Item.uDataAlloc ||
933 Item.uLabelAlloc ||
Laurence Lundbladed61cbf32018-12-09 11:42:21 -0800934 Item.uDataType != QCBOR_TYPE_MAP_AS_ARRAY ||
935 Item.val.uCount != 8) {
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900936 return -9;
937 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800938
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900939 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
940 return nCBORError;
941 }
942 if(Item.uLabelType != QCBOR_TYPE_NONE ||
943 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("bytes 1"))||
944 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
945 Item.uDataAlloc ||
946 Item.uLabelAlloc) {
947 return -10;
948 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800949
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900950 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
951 return nCBORError;
952 }
953 if(Item.uLabelType != QCBOR_TYPE_NONE ||
954 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
955 Item.uDataAlloc ||
956 Item.uLabelAlloc ||
957 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("xxxx"))) {
958 return -11;
959 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800960
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900961 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
962 return nCBORError;
963 }
964 if(Item.uLabelType != QCBOR_TYPE_NONE ||
965 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("bytes 2")) ||
966 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
967 Item.uDataAlloc ||
968 Item.uLabelAlloc) {
969 return -12;
970 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800971
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900972 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
973 return nCBORError;
974 }
975 if(Item.uLabelType != QCBOR_TYPE_NONE ||
976 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
977 Item.uDataAlloc ||
978 Item.uLabelAlloc ||
979 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("yyyy"))) {
980 return -13;
981 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800982
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900983 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
984 return nCBORError;
985 }
986 if(Item.uLabelType != QCBOR_TYPE_NONE ||
987 Item.uDataAlloc ||
988 Item.uLabelAlloc ||
989 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("another int")) ||
990 Item.uDataType != QCBOR_TYPE_TEXT_STRING) {
991 return -14;
992 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800993
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900994 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
995 return nCBORError;
996 }
997 if(Item.uLabelType != QCBOR_TYPE_NONE ||
998 Item.uDataAlloc ||
999 Item.uLabelAlloc ||
1000 Item.uDataType != QCBOR_TYPE_INT64 ||
1001 Item.val.int64 != 98) {
1002 return -15;
1003 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001004
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001005 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
1006 return nCBORError;
1007 }
1008 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1009 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("text 2"))||
1010 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1011 Item.uDataAlloc ||
1012 Item.uLabelAlloc) {
1013 return -16;
1014 }
1015
1016 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
1017 return nCBORError;
1018 }
1019 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1020 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1021 Item.uDataAlloc ||
1022 Item.uLabelAlloc ||
1023 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("lies, damn lies and statistics"))) {
1024 return -17;
1025 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001026
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001027 return 0;
1028}
1029
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001030
1031/*
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301032 Fully or partially decode pValidMapEncoded. When
1033 partially decoding check for the right error code.
1034 How much partial decoding depends on nLevel.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001035
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301036 The partial decodes test error conditions of
1037 incomplete encoded input.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001038
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301039 This could be combined with the above test
1040 and made prettier and maybe a little more
1041 thorough.
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001042 */
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001043static int ExtraBytesTest(int nLevel)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001044{
1045 QCBORDecodeContext DCtx;
1046 QCBORItem Item;
1047 int nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001048
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001049 QCBORDecode_Init(&DCtx, (UsefulBufC){pValidMapEncoded, sizeof(pValidMapEncoded)}, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001050
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001051 if(nLevel < 1) {
1052 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_EXTRA_BYTES) {
1053 return -1;
1054 } else {
1055 return 0;
1056 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001057 }
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301058
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001059
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001060 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001061 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001062 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001063 if(Item.uDataType != QCBOR_TYPE_MAP ||
1064 Item.val.uCount != 3)
1065 return -1;
1066
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001067 if(nLevel < 2) {
1068 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
1069 return -1;
1070 } else {
1071 return 0;
1072 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001073 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001074
1075
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001076 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001077 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001078 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001079 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001080 Item.uDataType != QCBOR_TYPE_INT64 ||
1081 Item.val.uCount != 42 ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001082 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("first integer"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001083 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001084 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001085
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001086 if(nLevel < 3) {
1087 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
1088 return -1;
1089 } else {
1090 return 0;
1091 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001092 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001093
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001094 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001095 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001096 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001097 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001098 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("an array of two strings")) ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001099 Item.uDataType != QCBOR_TYPE_ARRAY ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001100 Item.val.uCount != 2) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001101 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001102 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001103
1104
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001105 if(nLevel < 4) {
1106 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
1107 return -1;
1108 } else {
1109 return 0;
1110 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001111 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001112
1113
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001114 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001115 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001116 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001117 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001118 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("string1"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001119 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001120 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001121
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001122 if(nLevel < 5) {
1123 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
1124 return -1;
1125 } else {
1126 return 0;
1127 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001128 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001129
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001130 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001131 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001132 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001133 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001134 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("string2"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001135 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001136 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001137
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001138 if(nLevel < 6) {
1139 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
1140 return -1;
1141 } else {
1142 return 0;
1143 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001144 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001145
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001146 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001147 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001148 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001149 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001150 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("map in a map")) ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001151 Item.uDataType != QCBOR_TYPE_MAP ||
1152 Item.val.uCount != 4)
1153 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001154
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001155 if(nLevel < 7) {
1156 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
1157 return -1;
1158 } else {
1159 return 0;
1160 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001161 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001162
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001163 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001164 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001165 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001166 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001167 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("bytes 1")) ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001168 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001169 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("xxxx"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001170 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001171 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001172
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001173 if(nLevel < 8) {
1174 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
1175 return -1;
1176 } else {
1177 return 0;
1178 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001179 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001180
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001181 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001182 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001183 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001184 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001185 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("bytes 2")) ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001186 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001187 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("yyyy"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001188 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001189 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001190
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001191 if(nLevel < 9) {
1192 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
1193 return -1;
1194 } else {
1195 return 0;
1196 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001197 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001198
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001199 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001200 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001201 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001202 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001203 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("another int")) ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001204 Item.uDataType != QCBOR_TYPE_INT64 ||
1205 Item.val.int64 != 98)
1206 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001207
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001208 if(nLevel < 10) {
1209 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
1210 return -1;
1211 } else {
1212 return 0;
1213 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001214 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001215
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001216 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001217 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001218 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001219 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001220 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("text 2"))||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001221 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001222 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("lies, damn lies and statistics"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001223 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001224 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001225
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301226 if(QCBORDecode_Finish(&DCtx)) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001227 return -1;
1228 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001229
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001230 return 0;
1231}
1232
1233
1234
1235
1236int ParseMapTest()
1237{
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301238 // Parse a moderatly complex map structure very thoroughl
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08001239 int n = ParseMapTest1(QCBOR_DECODE_MODE_NORMAL);
1240
1241 n = ParseMapTest1(QCBOR_DECODE_MODE_MAP_STRINGS_ONLY);
1242
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001243 if(!n) {
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001244 for(int i = 0; i < 10; i++) {
1245 n = ExtraBytesTest(i);
1246 if(n) {
1247 break;
1248 }
1249 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001250 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001251
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001252 return(n);
1253}
1254
1255
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07001256static uint8_t spSimpleValues[] = {0x8a, 0xf4, 0xf5, 0xf6, 0xf7, 0xff, 0xe0, 0xf3, 0xf8, 0x00, 0xf8, 0x13, 0xf8, 0x1f, 0xf8, 0x20, 0xf8, 0xff};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001257
1258int ParseSimpleTest()
1259{
1260 QCBORDecodeContext DCtx;
1261 QCBORItem Item;
1262 int nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001263
1264
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07001265 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSimpleValues), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001266
1267
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001268 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1269 return nCBORError;
1270 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
1271 Item.val.uCount != 10)
1272 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001273
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001274 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1275 return nCBORError;
1276 if(Item.uDataType != QCBOR_TYPE_FALSE)
1277 return -1;
1278
1279 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1280 return nCBORError;
1281 if(Item.uDataType != QCBOR_TYPE_TRUE)
1282 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001283
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001284 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1285 return nCBORError;
1286 if(Item.uDataType != QCBOR_TYPE_NULL)
1287 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001288
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001289 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1290 return nCBORError;
1291 if(Item.uDataType != QCBOR_TYPE_UNDEF)
1292 return -1;
1293
1294 // A break
Laurence Lundblade9e3651c2018-10-10 11:49:55 +08001295 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_BREAK)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001296 return -1;
1297
1298 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1299 return nCBORError;
1300 if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 0)
1301 return -1;
1302
1303 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1304 return nCBORError;
1305 if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 19)
1306 return -1;
1307
Laurence Lundblade077475f2019-04-26 09:06:33 -07001308 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_TYPE_7)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001309 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001310
Laurence Lundblade077475f2019-04-26 09:06:33 -07001311 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_TYPE_7)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001312 return -1;
1313
Laurence Lundblade077475f2019-04-26 09:06:33 -07001314 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_TYPE_7)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001315 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001316
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001317 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1318 return nCBORError;
1319 if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 32)
1320 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001321
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001322 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1323 return nCBORError;
1324 if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 255)
1325 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001326
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001327 return 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001328
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001329}
1330
1331
1332struct FailInput {
Laurence Lundblade3a6042e2019-06-28 19:58:04 -07001333 UsefulBufC Input; // CBOR to decode
1334 QCBORError nError; // The error expected
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001335};
1336
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001337struct FailInput Failures[] = {
Laurence Lundblade3a6042e2019-06-28 19:58:04 -07001338 { {(uint8_t[]){0xa1, 0x00}, 2}, QCBOR_ERR_HIT_END }, // map with odd number of entries
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001339 { {(uint8_t[]){0x18}, 1}, QCBOR_ERR_HIT_END }, // 1 byte integer missing the byte
1340 { {(uint8_t[]){0x1c}, 1}, QCBOR_ERR_UNSUPPORTED }, // Reserved additional info = 28
1341 { {(uint8_t[]){0x1d}, 1}, QCBOR_ERR_UNSUPPORTED }, // Reserved additional info = 29
1342 { {(uint8_t[]){0x1e}, 1}, QCBOR_ERR_UNSUPPORTED }, // Reserved additional info = 30
Laurence Lundblade3a6042e2019-06-28 19:58:04 -07001343 { {(uint8_t[]){0x1f}, 1}, QCBOR_ERR_BAD_INT }, // Indefinite length integer
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001344 { {(uint8_t[]){0x3c}, 1}, QCBOR_ERR_UNSUPPORTED }, // 1 byte integer missing the byte
1345 { {(uint8_t[]){0x3d}, 1}, QCBOR_ERR_UNSUPPORTED }, // 1 byte integer missing the byte
1346 { {(uint8_t[]){0x3e}, 1}, QCBOR_ERR_UNSUPPORTED }, // 1 byte integer missing the byte
Laurence Lundblade3a6042e2019-06-28 19:58:04 -07001347 { {(uint8_t[]){0x3f}, 1}, QCBOR_ERR_BAD_INT }, // Indefinite length negative integer
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001348 { {(uint8_t[]){0x41}, 1}, QCBOR_ERR_HIT_END }, // Short byte string
1349 { {(uint8_t[]){0x5c}, 1}, QCBOR_ERR_UNSUPPORTED }, // Reserved additional info = 28
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001350 { {(uint8_t[]){0x61}, 1}, QCBOR_ERR_HIT_END }, // Short UTF-8 string
1351 { {(uint8_t[]){0x7c}, 1}, QCBOR_ERR_UNSUPPORTED }, // Reserved additional info = 28
Laurence Lundblade3a6042e2019-06-28 19:58:04 -07001352 { {(uint8_t[]){0xff}, 1}, QCBOR_ERR_BAD_BREAK } , // break
Laurence Lundblade077475f2019-04-26 09:06:33 -07001353 { {(uint8_t[]){0xf8, 0x00}, 2}, QCBOR_ERR_BAD_TYPE_7 }, // An invalid encoding of a simple type
1354 { {(uint8_t[]){0xf8, 0x1f}, 2}, QCBOR_ERR_BAD_TYPE_7 }, // An invalid encoding of a simple type
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001355 { {(uint8_t[]){0xc0, 0x00}, 2}, QCBOR_ERR_BAD_OPT_TAG }, // Text-based date, with an integer
1356 { {(uint8_t[]){0xc1, 0x41, 0x33}, 3}, QCBOR_ERR_BAD_OPT_TAG }, // Epoch date, with an byte string
1357 { {(uint8_t[]){0xc1, 0xc0, 0x00}, 3}, QCBOR_ERR_BAD_OPT_TAG }, // tagged as both epoch and string dates
Laurence Lundblade3a6042e2019-06-28 19:58:04 -07001358 { {(uint8_t[]){0xc2, 0x00}, 2}, QCBOR_ERR_BAD_OPT_TAG }, // big num tagged an int, not a byte string
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001359};
1360
Laurence Lundblade3a6042e2019-06-28 19:58:04 -07001361int DecodeFailureTests()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001362{
Laurence Lundblade3a6042e2019-06-28 19:58:04 -07001363 // Loop over the failures
1364 const struct FailInput * const pFEnd = &Failures[0] +
1365 sizeof(Failures)/sizeof(struct FailInput);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001366
Laurence Lundblade3a6042e2019-06-28 19:58:04 -07001367 for(const struct FailInput *pF = &Failures[0]; pF < pFEnd ;pF++) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001368 QCBORDecodeContext DCtx;
Laurence Lundblade3a6042e2019-06-28 19:58:04 -07001369 QCBORItem Item;
1370 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001371
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001372 QCBORDecode_Init(&DCtx, pF->Input, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001373
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001374 while(1) {
1375 nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundblade3a6042e2019-06-28 19:58:04 -07001376 if(QCBOR_SUCCESS == nCBORError) {
1377 // Must end in an error, so if success keep going
1378 continue;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001379 }
Laurence Lundblade3a6042e2019-06-28 19:58:04 -07001380
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001381 if(nCBORError != pF->nError) {
Laurence Lundblade3a6042e2019-06-28 19:58:04 -07001382 // Not success, nor error expected so a test failure
1383 // Return code is 1000 plus index into Failures of test that failed
1384 return 1000 + (int)(pF - &Failures[0]);
1385 } else {
1386 // Got the error expected
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001387 break;
1388 }
1389 }
1390 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001391
Laurence Lundblade3a6042e2019-06-28 19:58:04 -07001392 // Corrupt the UsefulInputBuf and see that
1393 // it reflected correctly for CBOR decoding
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001394 {
1395 QCBORDecodeContext DCtx;
Laurence Lundblade3a6042e2019-06-28 19:58:04 -07001396 QCBORItem Item;
1397 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001398
Laurence Lundblade3a6042e2019-06-28 19:58:04 -07001399 QCBORDecode_Init(&DCtx,
1400 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSimpleValues),
1401 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001402
1403 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1404 return nCBORError;
1405 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
Laurence Lundblade3a6042e2019-06-28 19:58:04 -07001406 Item.val.uCount != 10) {
1407 // This wasn't supposed to happen
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001408 return -1;
Laurence Lundblade3a6042e2019-06-28 19:58:04 -07001409 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001410
Laurence Lundblade3a6042e2019-06-28 19:58:04 -07001411 DCtx.InBuf.magic = 0; // Reach in and corrupt the UsefulInputBuf
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001412
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001413 nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundblade3a6042e2019-06-28 19:58:04 -07001414 if(nCBORError != QCBOR_ERR_HIT_END) {
1415 // Did not get back the error expected
1416 return -2;
1417 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001418 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001419
Laurence Lundblade3a6042e2019-06-28 19:58:04 -07001420 return 0;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001421}
1422
1423
Laurence Lundbladea2e29072018-12-30 09:20:06 -08001424/* Try all 256 values of the byte at nLen including recursing for
1425 each of the values to try values at nLen+1 ... up to nLenMax
1426 */
1427static void ComprehensiveInputRecurser(uint8_t *pBuf, int nLen, int nLenMax)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001428{
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001429 if(nLen >= nLenMax) {
1430 return;
1431 }
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08001432
Laurence Lundbladea2e29072018-12-30 09:20:06 -08001433 for(int inputByte = 0; inputByte < 256; inputByte++) {
1434 // Set up the input
1435 pBuf[nLen] = inputByte;
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08001436 const UsefulBufC Input = {pBuf, nLen+1};
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08001437
Laurence Lundbladea2e29072018-12-30 09:20:06 -08001438 // Get ready to parse
1439 QCBORDecodeContext DCtx;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001440 QCBORDecode_Init(&DCtx, Input, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001441
Laurence Lundbladea2e29072018-12-30 09:20:06 -08001442 // Parse by getting the next item until an error occurs
1443 // Just about every possible decoder error can occur here
1444 // The goal of this test is not to check for the correct
1445 // error since that is not really possible. It is to
1446 // see that there is no crash on hostile input.
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001447 while(1) {
Laurence Lundbladea2e29072018-12-30 09:20:06 -08001448 QCBORItem Item;
1449 QCBORError nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001450 if(nCBORError != QCBOR_SUCCESS) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001451 break;
1452 }
1453 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001454
Laurence Lundbladea2e29072018-12-30 09:20:06 -08001455 ComprehensiveInputRecurser(pBuf, nLen+1, nLenMax);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001456 }
1457}
1458
1459
1460/*
Laurence Lundbladea2e29072018-12-30 09:20:06 -08001461 Public function for initialization. See header qcbor.h
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001462 */
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001463int ComprehensiveInputTest()
1464{
Laurence Lundbladea2e29072018-12-30 09:20:06 -08001465 // Size 2 tests 64K inputs and runs quickly
1466 uint8_t pBuf[2];
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001467
Laurence Lundbladea2e29072018-12-30 09:20:06 -08001468 ComprehensiveInputRecurser(pBuf, 0, sizeof(pBuf));
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001469
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001470 return 0;
1471}
1472
Laurence Lundbladea2e29072018-12-30 09:20:06 -08001473
1474/*
1475 Public function for initialization. See header qcbor.h
1476 */
1477int BigComprehensiveInputTest()
1478{
1479 // size 3 tests 16 million inputs and runs OK
1480 // in seconds on fast machines. Size 4 takes
1481 // 10+ minutes and 5 half a day on fast
1482 // machines. This test is kept separate from
1483 // the others so as to no slow down the use
1484 // of them as a very frequent regression.
1485 uint8_t pBuf[3]; //
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08001486
Laurence Lundbladea2e29072018-12-30 09:20:06 -08001487 ComprehensiveInputRecurser(pBuf, 0, sizeof(pBuf));
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08001488
Laurence Lundbladea2e29072018-12-30 09:20:06 -08001489 return 0;
1490}
1491
1492
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07001493static uint8_t spDateTestInput[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001494 0xc0, // tag for string date
1495 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001496
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001497 0xc1, // tag for epoch date
1498 0x1a, 0x53, 0x72, 0x4E, 0x00, // Epoch date 1400000000; Tue, 13 May 2014 16:53:20 GMT
1499
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001500 // CBOR_TAG_B64
1501 0xc1, 0xcf, 0xd8, 0x22, // 0xee, // Epoch date with extra tags TODO: fix this test
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001502 0x1a, 0x53, 0x72, 0x4E, 0x01,
1503
1504 0xc1, // tag for epoch date
1505 0x1b, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, // Too large integer
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001506
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001507 0xc1, // tag for epoch date
1508 0xfa, 0x3f, 0x8c, 0xcc, 0xcd, // double with value 1.1
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001509
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001510 0xc1, // tag for epoch date
1511 0xfa, 0x7f, 0x7f, 0xff, 0xff // 3.4028234663852886e+38 too large
1512
1513};
1514
1515
1516// have to check float expected only to within an epsilon
1517int CHECK_EXPECTED_DOUBLE(double val, double expected) {
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001518
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001519 double diff = val - expected;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001520
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001521 diff = fabs(diff);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001522
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001523 return diff > 0.0000001;
1524}
1525
1526
1527int DateParseTest()
1528{
1529 QCBORDecodeContext DCtx;
1530 QCBORItem Item;
1531 int nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001532
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07001533 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spDateTestInput), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001534
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001535 const uint64_t uTags[] = {15};
1536 QCBORTagListIn TagList = {1, uTags};
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001537
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001538 QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TagList);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001539
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001540 // String date
1541 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1542 return -1;
1543 if(Item.uDataType != QCBOR_TYPE_DATE_STRING ||
Laurence Lundblade9e3651c2018-10-10 11:49:55 +08001544 UsefulBuf_Compare(Item.val.dateString, UsefulBuf_FromSZ("1985-04-12"))){
Laurence Lundblade67bd5512018-11-02 21:44:06 +07001545 return -2;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001546 }
1547
1548 // Epoch date
1549 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade67bd5512018-11-02 21:44:06 +07001550 return -3;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001551 if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH ||
1552 Item.val.epochDate.nSeconds != 1400000000 ||
1553 Item.val.epochDate.fSecondsFraction != 0 ) {
Laurence Lundblade67bd5512018-11-02 21:44:06 +07001554 return -4;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001555 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001556
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001557 // Epoch date with extra CBOR_TAG_B64 tag that doesn't really mean anything
1558 // but want to be sure extra tag doesn't cause a problem
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001559 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade67bd5512018-11-02 21:44:06 +07001560 return -5;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001561 if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH ||
1562 Item.val.epochDate.nSeconds != 1400000001 ||
1563 Item.val.epochDate.fSecondsFraction != 0 ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001564 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_B64)) {
Laurence Lundblade67bd5512018-11-02 21:44:06 +07001565 return -6;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001566 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001567
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001568 // Epoch date that is too large for our representation
1569 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_DATE_OVERFLOW) {
Laurence Lundblade67bd5512018-11-02 21:44:06 +07001570 return -7;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001571 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001572
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001573 // Epoch date in float format with fractional seconds
1574 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade67bd5512018-11-02 21:44:06 +07001575 return -8;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001576 if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH ||
1577 Item.val.epochDate.nSeconds != 1 ||
1578 CHECK_EXPECTED_DOUBLE(Item.val.epochDate.fSecondsFraction, 0.1 )) {
Laurence Lundblade67bd5512018-11-02 21:44:06 +07001579 return -9;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001580 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001581
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001582 // Epoch date float that is too large for our representation
1583 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_DATE_OVERFLOW) {
Laurence Lundblade67bd5512018-11-02 21:44:06 +07001584 return -10;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001585 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001586
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001587 // TODO: could use a few more tests with float, double, and half precsion and negative (but coverage is still pretty good)
1588
1589 return 0;
1590}
1591
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001592// Really simple basic input for tagging test
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07001593static uint8_t spOptTestInput[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001594 0xd9, 0xd9, 0xf7, // CBOR magic number
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001595 0x81, // Array of one
1596 0xd8, 0x04, // non-preferred serialization of tag 4
1597 0x82, 0x01, 0x03}; // fraction 1/3
1598
1599static uint8_t spEncodedLargeTag[] = {0xdb, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x80};
1600
1601// 0x9192939495969798, 0x88, 0x01, 0x04
1602static uint8_t spLotsOfTags[] = {0xdb, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0xd8, 0x88, 0xc5, 0xc4, 0x80};
1603
1604/*
1605 The cbor.me parse of this.
1606 55799(55799(55799({6(7(-23)): 5859837686836516696(7({7(-20): 11({17(-18): 17(17(17("Organization"))),
1607 9(-17): 773("SSG"), -15: 4(5(6(7(8(9(10(11(12(13(14(15("Confusion")))))))))))), 17(-16): 17("San Diego"),
1608 17(-14): 17("US")}), 23(-19): 19({-11: 9({-9: -7}),
1609 90599561(90599561(90599561(-10))): 12(h'0102030405060708090A')})})),
1610 16(-22): 23({11(8(7(-5))): 8(-3)})})))
1611 */
1612static uint8_t spCSRWithTags[] = {
1613 0xd9, 0xd9, 0xf7, 0xd9, 0xd9, 0xf7, 0xd9, 0xd9, 0xf7, 0xa2,
1614 0xc6, 0xc7, 0x36,
1615 0xdb, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0xc7, 0xa2,
1616 0xda, 0x00, 0x00, 0x00, 0x07, 0x33,
1617 0xcb, 0xa5,
1618 0xd1, 0x31,
1619 0xd1, 0xd1, 0xd1, 0x6c,
1620 0x4f, 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74, 0x69, 0x6f, 0x6e,
1621 0xc9, 0x30,
1622 0xd9, 0x03, 0x05, 0x63,
1623 0x53, 0x53, 0x47,
1624 0x2e,
1625 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf, 0x69,
1626 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73, 0x69, 0x6f, 0x6e,
1627 0xd1, 0x2f,
1628 0xd1, 0x69,
1629 0x53, 0x61, 0x6e, 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f,
1630 0xd1, 0x2d,
1631 0xd1, 0x62,
1632 0x55, 0x53,
1633 0xd7, 0x32,
1634 0xd3, 0xa2,
1635 0x2a,
1636 0xc9, 0xa1,
1637 0x28,
1638 0x26,
1639 0xda, 0x05, 0x66, 0x70, 0x89, 0xda, 0x05, 0x66, 0x70, 0x89, 0xda, 0x05, 0x66, 0x70, 0x89, 0x29,
1640 0xcc, 0x4a,
1641 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,0x07, 0x08, 0x09, 0x0a,
1642 0xd0, 0x35,
1643 0xd7, 0xa1,
1644 0xcb, 0xc8, 0xc7, 0x24,
1645 0xc8, 0x22};
1646
1647static int CheckCSRMaps(QCBORDecodeContext *pDC);
1648
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001649
1650int OptTagParseTest()
1651{
1652 QCBORDecodeContext DCtx;
1653 QCBORItem Item;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001654
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07001655 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spOptTestInput), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001656
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001657 //-------------------------
1658 // This text matches the magic number tag and the fraction tag
1659 if(QCBORDecode_GetNext(&DCtx, &Item)) {
1660 return -2;
1661 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001662 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001663 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC)) {
1664 return -3;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001665 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001666
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001667 if(QCBORDecode_GetNext(&DCtx, &Item)) {
1668 return -4;
1669 }
1670 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
1671 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_FRACTION) ||
1672 Item.val.uCount != 2) {
1673 return -5;
1674 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001675
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001676 // --------------------------------
1677 // This test decodes the very large tag, but it is not in
1678 // any list so it is ignored.
1679 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEncodedLargeTag), QCBOR_DECODE_MODE_NORMAL);
1680 if(QCBORDecode_GetNext(&DCtx, &Item)) {
1681 return -6;
1682 }
1683 if(Item.uTagBits) {
1684 return -7;
1685 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001686
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001687 // ----------------------------------
1688 // This test sets up a caller-config list that includes the very large tage and then matches it.
1689 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEncodedLargeTag), QCBOR_DECODE_MODE_NORMAL);
1690 const uint64_t puList[] = {0x9192939495969798, 257};
1691 const QCBORTagListIn TL = {2, puList};
1692 QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001693
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001694 if(QCBORDecode_GetNext(&DCtx, &Item)) {
1695 return -8;
1696 }
1697 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
1698 !QCBORDecode_IsTagged(&DCtx, &Item, 0x9192939495969798) ||
1699 QCBORDecode_IsTagged(&DCtx, &Item, 257) ||
1700 QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_BIGFLOAT) ||
1701 Item.val.uCount != 0) {
1702 return -9;
1703 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001704
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001705 //------------------------
1706 // This test sets up a caller-configured list, and looks up something not in it
1707 const uint64_t puLongList[17] = {1,2,1};
1708 const QCBORTagListIn TLLong = {17, puLongList};
1709 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEncodedLargeTag), QCBOR_DECODE_MODE_NORMAL);
1710 QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TLLong);
1711 if(QCBORDecode_GetNext(&DCtx, &Item)) {
1712 return -11;
1713 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001714
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001715 // -----------------------
1716 // This tests retrievel of the full tag list
1717 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spLotsOfTags), QCBOR_DECODE_MODE_NORMAL);
1718 uint64_t puTags[16];
1719 QCBORTagListOut Out = {0, 4, puTags};
1720 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
1721 return -12;
1722 }
1723 if(puTags[0] != 0x9192939495969798 ||
1724 puTags[1] != 0x88 ||
1725 puTags[2] != 0x05 ||
1726 puTags[3] != 0x04) {
1727 return -13;
1728 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001729
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001730 // ----------------------
1731 // This text if too small of an out list
1732 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spLotsOfTags), QCBOR_DECODE_MODE_NORMAL);
1733 QCBORTagListOut OutSmall = {0, 3, puTags};
1734 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &OutSmall) != QCBOR_ERR_TOO_MANY_TAGS) {
1735 return -14;
1736 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001737
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001738 // ---------------
1739 // Parse a version of the "CSR" that has had a ton of tags randomly inserted
1740 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRWithTags), QCBOR_DECODE_MODE_NORMAL);
1741 int n = CheckCSRMaps(&DCtx);
1742 if(n) {
1743 return n-2000;
1744 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001745
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001746 Out = (QCBORTagListOut){0,16, puTags};
1747 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRWithTags), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001748
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001749 const uint64_t puTagList[] = {773, 1, 90599561};
1750 const QCBORTagListIn TagList = {3, puTagList};
1751 QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TagList);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001752
1753
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001754 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
1755 return -100;
1756 }
1757 if(Item.uDataType != QCBOR_TYPE_MAP ||
1758 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC) ||
1759 QCBORDecode_IsTagged(&DCtx, &Item, 90599561) ||
1760 QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_DATE_EPOCH) ||
1761 Item.val.uCount != 2 ||
1762 puTags[0] != CBOR_TAG_CBOR_MAGIC ||
1763 puTags[1] != CBOR_TAG_CBOR_MAGIC ||
1764 puTags[2] != CBOR_TAG_CBOR_MAGIC ||
1765 Out.uNumUsed != 3) {
1766 return -101;
1767 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001768
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001769 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
1770 return -102;
1771 }
1772 if(Item.uDataType != QCBOR_TYPE_MAP ||
1773 QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC) ||
1774 QCBORDecode_IsTagged(&DCtx, &Item, 6) ||
1775 QCBORDecode_IsTagged(&DCtx, &Item, 7) || // item is tagged 7, but 7 is not configured to be recognized
1776 Item.val.uCount != 2 ||
1777 puTags[0] != 5859837686836516696 ||
1778 puTags[1] != 7 ||
1779 Out.uNumUsed != 2) {
1780 return -103;
1781 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001782
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001783 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
1784 return -104;
1785 }
1786 if(Item.uDataType != QCBOR_TYPE_MAP ||
1787 Item.uTagBits ||
1788 Item.val.uCount != 5 ||
1789 puTags[0] != 0x0b ||
1790 Out.uNumUsed != 1) {
1791 return -105;
1792 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001793
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001794 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
1795 return -106;
1796 }
1797 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1798 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_COSE_MAC0) ||
1799 Item.val.string.len != 12 ||
1800 puTags[0] != CBOR_TAG_COSE_MAC0 ||
1801 puTags[1] != CBOR_TAG_COSE_MAC0 ||
1802 puTags[2] != CBOR_TAG_COSE_MAC0 ||
1803 Out.uNumUsed != 3) {
1804 return -105;
1805 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001806
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001807 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
1808 return -107;
1809 }
1810 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1811 !QCBORDecode_IsTagged(&DCtx, &Item, 773) ||
1812 Item.val.string.len != 3 ||
1813 puTags[0] != 773 ||
1814 Out.uNumUsed != 1) {
1815 return -108;
1816 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001817
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001818 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
1819 return -109;
1820 }
1821 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1822 !QCBORDecode_IsTagged(&DCtx, &Item, 4) ||
1823 Item.val.string.len != 9 ||
1824 puTags[0] != 4 ||
1825 puTags[11] != 0x0f ||
1826 Out.uNumUsed != 12) {
1827 return -110;
1828 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001829
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001830 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
1831 return -111;
1832 }
1833 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1834 !QCBORDecode_IsTagged(&DCtx, &Item, 17) ||
1835 Item.val.string.len != 9 ||
1836 puTags[0] != 17 ||
1837 Out.uNumUsed != 1) {
1838 return -112;
1839 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001840
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001841 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
1842 return -111;
1843 }
1844 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1845 !QCBORDecode_IsTagged(&DCtx, &Item, 17) ||
1846 Item.val.string.len != 2 ||
1847 puTags[0] != 17 ||
1848 Out.uNumUsed != 1) {
1849 return -112;
1850 }
1851
1852 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
1853 return -113;
1854 }
1855 if(Item.uDataType != QCBOR_TYPE_MAP ||
1856 QCBORDecode_IsTagged(&DCtx, &Item, 19) ||
1857 Item.val.uCount != 2 ||
1858 puTags[0] != 19 ||
1859 Out.uNumUsed != 1) {
1860 return -114;
1861 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001862
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001863 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
1864 return -115;
1865 }
1866 if(Item.uDataType != QCBOR_TYPE_MAP ||
1867 QCBORDecode_IsTagged(&DCtx, &Item, 9) ||
1868 Item.uTagBits ||
1869 Item.val.uCount != 1 ||
1870 puTags[0] != 9 ||
1871 Out.uNumUsed != 1) {
1872 return -116;
1873 }
1874
1875 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
1876 return -116;
1877 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001878 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001879 Item.val.int64 != -7 ||
1880 Item.uTagBits ||
1881 Out.uNumUsed != 0) {
1882 return -117;
1883 }
1884
1885 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
1886 return -118;
1887 }
1888 if(Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
1889 Item.val.string.len != 10 ||
1890 Item.uTagBits ||
1891 puTags[0] != 12 ||
1892 Out.uNumUsed != 1) {
1893 return -119;
1894 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001895
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001896 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
1897 return -120;
1898 }
1899 if(Item.uDataType != QCBOR_TYPE_MAP ||
1900 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_ENC_AS_B16) ||
1901 Item.val.uCount != 1 ||
1902 puTags[0] != 0x17 ||
1903 Out.uNumUsed != 1) {
1904 return -121;
1905 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001906
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001907 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
1908 return -122;
1909 }
1910 if(Item.uDataType != QCBOR_TYPE_INT64 ||
1911 QCBORDecode_IsTagged(&DCtx, &Item, 8) ||
1912 Item.val.int64 != -3 ||
1913 puTags[0] != 8 ||
1914 Out.uNumUsed != 1) {
1915 return -123;
1916 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001917
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001918 if(QCBORDecode_Finish(&DCtx)) {
1919 return -124;
1920 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001921
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001922 return 0;
1923}
1924
1925
1926
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001927
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07001928static uint8_t spBigNumInput[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001929 0x83,
1930 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1931 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1932 0xA4,
1933 0x63, 0x42, 0x4E, 0x2B,
1934 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1935 0x18, 0x40,
1936 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1937 0x63, 0x42, 0x4E, 0x2D,
1938 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1939 0x38, 0x3F,
1940 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
1941
1942
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07001943static uint8_t spBigNum[] = {0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001944
1945
1946int BignumParseTest()
1947{
1948 QCBORDecodeContext DCtx;
1949 QCBORItem Item;
1950 int nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001951
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07001952 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNumInput), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001953
1954
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001955 //
1956 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1957 return -1;
1958 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
1959 return -1;
1960 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001961
1962 //
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001963 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1964 return -1;
1965 if(Item.uDataType != QCBOR_TYPE_POSBIGNUM ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07001966 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001967 return -1;
1968 }
1969
1970 //
1971 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1972 return -1;
1973 if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07001974 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001975 return -1;
1976 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001977
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001978 //
1979 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1980 return -1;
1981 if(Item.uDataType != QCBOR_TYPE_MAP) {
1982 return -1;
1983 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001984
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001985 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1986 return -1;
1987 if(Item.uDataType != QCBOR_TYPE_POSBIGNUM ||
1988 Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07001989 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001990 return -1;
1991 }
1992
1993 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1994 return -1;
1995 if(Item.uDataType != QCBOR_TYPE_POSBIGNUM ||
1996 Item.uLabelType != QCBOR_TYPE_INT64 ||
1997 Item.label.int64 != 64 ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07001998 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001999 return -1;
2000 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002001
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002002 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
2003 return -1;
2004 if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM ||
2005 Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002006 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002007 return -1;
2008 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002009
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002010 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
2011 return -1;
2012 if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM ||
2013 Item.uLabelType != QCBOR_TYPE_INT64 ||
2014 Item.label.int64 != -64 ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002015 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002016 return -1;
2017 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002018
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002019 return 0;
2020}
2021
2022
2023
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302024static int CheckItemWithIntLabel(QCBORDecodeContext *pCtx, uint8_t uDataType, uint8_t uNestingLevel, uint8_t uNextNest, int64_t nLabel, QCBORItem *pItem)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002025{
2026 QCBORItem Item;
2027 int nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002028
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002029 if((nCBORError = QCBORDecode_GetNext(pCtx, &Item))) return -1;
2030 if(Item.uDataType != uDataType) return -1;
2031 if(uNestingLevel > 0) {
2032 if(Item.uLabelType != QCBOR_TYPE_INT64 && Item.uLabelType != QCBOR_TYPE_UINT64) return -1;
2033 if(Item.uLabelType == QCBOR_TYPE_INT64) {
2034 if(Item.label.int64 != nLabel) return -1;
2035 } else {
Laurence Lundblade570fab52018-10-13 18:28:27 +08002036 if(Item.label.uint64 != (uint64_t)nLabel) return -1;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002037 }
2038 }
2039 if(Item.uNestingLevel != uNestingLevel) return -1;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302040 if(Item.uNextNestLevel != uNextNest) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002041
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002042 if(pItem) {
2043 *pItem = Item;
2044 }
2045 return 0;
2046}
2047
2048
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002049// Same code checks definite and indefinite length versions of the map
2050static int CheckCSRMaps(QCBORDecodeContext *pDC)
2051{
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302052 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 0, 1, 0, NULL)) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002053
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302054 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 1, 2, -23, NULL)) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002055
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302056 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 2, 3, -20, NULL)) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002057
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302058 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -18, NULL)) return -1;
2059 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -17, NULL)) return -1;
2060 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -15, NULL)) return -1;
2061 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -16, NULL)) return -1;
2062 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 2, -14, NULL)) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002063
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302064 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 2, 3, -19, NULL)) return -1;
2065 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 3, 4, -11, NULL)) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002066
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302067 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_INT64, 4, 3, -9, NULL)) return -1;
2068 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_BYTE_STRING, 3, 1, -10, NULL)) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002069
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302070 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 1, 2, -22, NULL)) return -1;
2071 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_INT64, 2, 0, -5, NULL)) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002072
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002073 if(QCBORDecode_Finish(pDC)) return -2;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002074
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002075 return 0;
2076}
2077
2078
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002079/*
2080// cbor.me decoded output
2081{
2082 -23: {
2083 -20: {
2084 -18: "Organization",
2085 -17: "SSG",
2086 -15: "Confusion",
2087 -16: "San Diego",
2088 -14: "US"
2089 },
2090 -19: {
2091 -11: {
2092 -9: -7
2093 },
2094 -10: '\u0001\u0002\u0003\u0004\u0005\u0006\a\b\t\n'
2095 }
2096 },
2097 -22: {
2098 -5: -3
2099 }
2100}
2101 */
2102
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002103
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002104static uint8_t spCSRInput[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002105 0xa2, 0x36, 0xa2, 0x33, 0xa5, 0x31, 0x6c, 0x4f,
2106 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74,
2107 0x69, 0x6f, 0x6e, 0x30, 0x63, 0x53, 0x53, 0x47,
2108 0x2e, 0x69, 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73,
2109 0x69, 0x6f, 0x6e, 0x2f, 0x69, 0x53, 0x61, 0x6e,
2110 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f, 0x2d, 0x62,
2111 0x55, 0x53, 0x32, 0xa2, 0x2a, 0xa1, 0x28, 0x26,
2112 0x29, 0x4a, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
2113 0x07, 0x08, 0x09, 0x0a, 0x35, 0xa1, 0x24, 0x22};
2114
2115int NestedMapTest()
2116{
2117 QCBORDecodeContext DCtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002118
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002119 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002120
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002121 return CheckCSRMaps(&DCtx);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002122}
2123
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08002124
2125
2126int StringDecoderModeFailTest()
2127{
2128 QCBORDecodeContext DCtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002129
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08002130 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput), QCBOR_DECODE_MODE_MAP_STRINGS_ONLY);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002131
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08002132 QCBORItem Item;
2133 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002134
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08002135 if(QCBORDecode_GetNext(&DCtx, &Item)) {
2136 return -1;
2137 }
2138 if(Item.uDataType != QCBOR_TYPE_MAP) {
2139 return -2;
2140 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002141
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08002142 nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
2143 if(nCBORError != QCBOR_ERR_MAP_LABEL_TYPE) {
2144 return -3;
2145 }
2146
2147 return 0;
2148}
2149
2150
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002151// Same map as above, but using indefinite lengths
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002152static uint8_t spCSRInputIndefLen[] = {
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002153 0xbf, 0x36, 0xbf, 0x33, 0xbf, 0x31, 0x6c, 0x4f,
2154 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74,
2155 0x69, 0x6f, 0x6e, 0x30, 0x63, 0x53, 0x53, 0x47,
2156 0x2e, 0x69, 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73,
2157 0x69, 0x6f, 0x6e, 0x2f, 0x69, 0x53, 0x61, 0x6e,
2158 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f, 0x2d, 0x62,
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002159 0x55, 0x53, 0xff, 0x32, 0xbf, 0x2a, 0xbf, 0x28,
2160 0x26, 0xff, 0x29, 0x4a, 0x01, 0x02, 0x03, 0x04,
2161 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0xff, 0xff,
2162 0x35, 0xbf, 0x24, 0x22, 0xff, 0xff};
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002163
2164int NestedMapTestIndefLen()
2165{
2166 QCBORDecodeContext DCtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002167
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002168 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInputIndefLen), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002169
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002170 return CheckCSRMaps(&DCtx);
2171}
2172
2173
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002174
Laurence Lundblade17ede402018-10-13 11:43:07 +08002175static UsefulBufC make_nested_indefinite_arrays(int n, UsefulBuf Storage)
2176{
2177 UsefulOutBuf UOB;
2178 UsefulOutBuf_Init(&UOB, Storage);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002179
Laurence Lundblade17ede402018-10-13 11:43:07 +08002180 int i;
2181 for(i = 0; i < n; i++) {
2182 UsefulOutBuf_AppendByte(&UOB, 0x9f);
2183 }
2184
2185 for(i = 0; i < n; i++) {
2186 UsefulOutBuf_AppendByte(&UOB, 0xff);
2187 }
2188 return UsefulOutBuf_OutUBuf(&UOB);
2189}
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002190
2191
Laurence Lundblade17ede402018-10-13 11:43:07 +08002192static int parse_indeflen_nested(UsefulBufC Nested, int nNestLevel)
2193{
2194 QCBORDecodeContext DC;
2195 QCBORDecode_Init(&DC, Nested, 0);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002196
Laurence Lundblade17ede402018-10-13 11:43:07 +08002197 int j;
2198 for(j = 0; j < nNestLevel; j++) {
2199 QCBORItem Item;
2200 int nReturn = QCBORDecode_GetNext(&DC, &Item);
2201 if(j >= QCBOR_MAX_ARRAY_NESTING) {
2202 // Should be in error
2203 if(nReturn != QCBOR_ERR_ARRAY_NESTING_TOO_DEEP) {
2204 return -4;
2205 } else {
2206 return 0; // Decoding doesn't recover after an error
2207 }
2208 } else {
2209 // Should be no error
2210 if(nReturn) {
2211 return -9; // Should not have got an error
2212 }
2213 }
2214 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
2215 return -7;
2216 }
2217 }
2218 int nReturn = QCBORDecode_Finish(&DC);
2219 if(nReturn) {
2220 return -3;
2221 }
2222 return 0;
2223}
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002224
2225
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302226int IndefiniteLengthNestTest()
Laurence Lundblade17ede402018-10-13 11:43:07 +08002227{
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05302228 UsefulBuf_MAKE_STACK_UB(Storage, 50);
Laurence Lundblade17ede402018-10-13 11:43:07 +08002229 int i;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002230 for(i=1; i < QCBOR_MAX_ARRAY_NESTING+4; i++) {
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08002231 const UsefulBufC Nested = make_nested_indefinite_arrays(i, Storage);
Laurence Lundblade17ede402018-10-13 11:43:07 +08002232 int nReturn = parse_indeflen_nested(Nested, i);
2233 if(nReturn) {
2234 return nReturn;
2235 }
2236 }
2237 return 0;
2238}
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002239
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002240
Laurence Lundblade6de37062018-10-15 12:22:42 +05302241
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002242static const uint8_t spIndefiniteArray[] = {0x9f, 0x01, 0x82, 0x02, 0x03, 0xff}; // [1, [2, 3]]
2243static const uint8_t spIndefiniteArrayBad1[] = {0x9f}; // No closing break
2244static const uint8_t spIndefiniteArrayBad2[] = {0x9f, 0x9f, 0x02, 0xff}; // Not enough closing breaks
2245static const uint8_t spIndefiniteArrayBad3[] = {0x9f, 0x02, 0xff, 0xff}; // Too many closing breaks
2246static const uint8_t spIndefiniteArrayBad4[] = {0x81, 0x9f}; // Unclosed indeflen inside def len
2247static const uint8_t spIndefiniteArrayBad5[] = {0x9f, 0xd1, 0xff}; // confused tag
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002248
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302249int IndefiniteLengthArrayMapTest()
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002250{
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002251 int nResult;
2252 // --- first test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002253 UsefulBufC IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArray);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002254
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002255 // Decode it and see if it is OK
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05302256 UsefulBuf_MAKE_STACK_UB(MemPool, 150);
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002257 QCBORDecodeContext DC;
2258 QCBORItem Item;
2259 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002260
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002261 QCBORDecode_SetMemPool(&DC, MemPool, false);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002262
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002263 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05302264
2265 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
2266 Item.uNestingLevel != 0 ||
2267 Item.uNextNestLevel != 1) {
2268 return -111;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002269 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002270
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002271 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05302272 if(Item.uDataType != QCBOR_TYPE_INT64 ||
2273 Item.uNestingLevel != 1 ||
2274 Item.uNextNestLevel != 1) {
2275 return -2;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002276 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002277
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002278 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05302279 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
2280 Item.uNestingLevel != 1 ||
2281 Item.uNextNestLevel != 2) {
2282 return -3;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002283 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002284
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002285 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade12b495d2018-12-17 11:15:54 -08002286 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade6de37062018-10-15 12:22:42 +05302287 Item.uNestingLevel != 2 ||
2288 Item.uNextNestLevel != 2) {
2289 return -4;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002290 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002291
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002292 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade12b495d2018-12-17 11:15:54 -08002293 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade6de37062018-10-15 12:22:42 +05302294 Item.uNestingLevel != 2 ||
2295 Item.uNextNestLevel != 0) {
2296 return -5;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002297 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002298
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002299 if(QCBORDecode_Finish(&DC)) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05302300 return -6;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002301 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002302
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002303 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002304 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad1);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002305
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002306 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002307
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002308 QCBORDecode_SetMemPool(&DC, MemPool, false);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002309
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002310 nResult = QCBORDecode_GetNext(&DC, &Item);
2311 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05302312 return -7;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002313 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002314
Laurence Lundblade570fab52018-10-13 18:28:27 +08002315 nResult = QCBORDecode_Finish(&DC);
Laurence Lundblade6de37062018-10-15 12:22:42 +05302316 if(nResult != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
2317 return -8;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002318 }
2319
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002320
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002321 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002322 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad2);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002323
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002324 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002325
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002326 QCBORDecode_SetMemPool(&DC, MemPool, false);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002327
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002328 nResult = QCBORDecode_GetNext(&DC, &Item);
2329 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05302330 return -9;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002331 }
2332
2333 nResult = QCBORDecode_GetNext(&DC, &Item);
2334 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05302335 return -10;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002336 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002337
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002338 nResult = QCBORDecode_GetNext(&DC, &Item);
2339 if(nResult || Item.uDataType != QCBOR_TYPE_INT64) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05302340 return -11;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002341 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002342
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002343 nResult = QCBORDecode_Finish(&DC);
Laurence Lundblade6de37062018-10-15 12:22:42 +05302344 if(nResult != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
2345 return -12;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002346 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002347
2348
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002349 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002350 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad3);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002351
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002352 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002353
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002354 QCBORDecode_SetMemPool(&DC, MemPool, false);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002355
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002356 nResult = QCBORDecode_GetNext(&DC, &Item);
2357 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05302358 return -13;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002359 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002360
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002361 nResult = QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05302362 if(nResult != QCBOR_ERR_BAD_BREAK) {
2363 return -14;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002364 }
Laurence Lundblade6de37062018-10-15 12:22:42 +05302365
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002366
Laurence Lundblade570fab52018-10-13 18:28:27 +08002367 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002368 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad4);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002369
Laurence Lundblade570fab52018-10-13 18:28:27 +08002370 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002371
Laurence Lundblade570fab52018-10-13 18:28:27 +08002372 QCBORDecode_SetMemPool(&DC, MemPool, false);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002373
Laurence Lundblade570fab52018-10-13 18:28:27 +08002374 nResult = QCBORDecode_GetNext(&DC, &Item);
2375 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05302376 return -15;
Laurence Lundblade570fab52018-10-13 18:28:27 +08002377 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002378
Laurence Lundblade570fab52018-10-13 18:28:27 +08002379 nResult = QCBORDecode_GetNext(&DC, &Item);
2380 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05302381 return -16;
Laurence Lundblade570fab52018-10-13 18:28:27 +08002382 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002383
Laurence Lundblade570fab52018-10-13 18:28:27 +08002384 nResult = QCBORDecode_Finish(&DC);
Laurence Lundblade6de37062018-10-15 12:22:42 +05302385 if(nResult != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
2386 return -17;
Laurence Lundblade570fab52018-10-13 18:28:27 +08002387 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002388
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302389 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002390 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad5);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002391
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302392 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002393
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302394 QCBORDecode_SetMemPool(&DC, MemPool, false);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002395
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302396 nResult = QCBORDecode_GetNext(&DC, &Item);
2397 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05302398 return -18;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302399 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002400
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302401 nResult = QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05302402 if(nResult != QCBOR_ERR_BAD_BREAK) {
2403 return -19;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302404 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002405
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002406 return 0;
2407}
2408
Laurence Lundblade17ede402018-10-13 11:43:07 +08002409
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002410static const uint8_t spIndefiniteLenString[] = {
Laurence Lundblade17ede402018-10-13 11:43:07 +08002411 0x81, // Array of length one
2412 0x7f, // text string marked with indefinite length
2413 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment
2414 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment
2415 0xff // ending break
2416};
2417
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002418static const uint8_t spIndefiniteLenStringBad2[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302419 0x81, // Array of length one
2420 0x7f, // text string marked with indefinite length
2421 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment
2422 0x44, 0x6d, 0x69, 0x6e, 0x67, // second segment of wrong type
2423 0xff // ending break
2424};
2425
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002426static const uint8_t spIndefiniteLenStringBad3[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302427 0x81, // Array of length one
2428 0x7f, // text string marked with indefinite length
2429 0x01, 0x02, // Not a string
2430 0xff // ending break
2431};
2432
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002433static const uint8_t spIndefiniteLenStringBad4[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302434 0x81, // Array of length one
2435 0x7f, // text string marked with indefinite length
2436 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment
2437 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment
2438 // missing end of string
2439};
2440
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002441static const uint8_t spIndefiniteLenStringLabel[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302442 0xa1, // Array of length one
2443 0x7f, // text string marked with indefinite length
2444 0x65, 0x73, 0x74, 0x72, 0x75, 0x75, // first segment
2445 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment
2446 0xff, // ending break
2447 0x01 // integer being labeled.
2448};
2449
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08002450static UsefulBufC MakeIndefiniteBigBstr(UsefulBuf Storage) // TODO: size this
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302451{
2452 UsefulOutBuf UOB;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002453
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302454 UsefulOutBuf_Init(&UOB, Storage);
2455 UsefulOutBuf_AppendByte(&UOB, 0x81);
2456 UsefulOutBuf_AppendByte(&UOB, 0x5f);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002457
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302458 int i = 0;
2459 for(int nChunkSize = 1; nChunkSize <= 128; nChunkSize *= 2) {
2460 UsefulOutBuf_AppendByte(&UOB, 0x58);
2461 UsefulOutBuf_AppendByte(&UOB, (uint8_t)nChunkSize);
2462 for(int j = 0; j < nChunkSize; j++ ) {
2463 UsefulOutBuf_AppendByte(&UOB, i);
2464 i++;
2465 }
2466 }
2467 UsefulOutBuf_AppendByte(&UOB, 0xff);
2468
2469 return UsefulOutBuf_OutUBuf(&UOB);
2470}
2471
2472static int CheckBigString(UsefulBufC BigString)
2473{
2474 if(BigString.len != 255) {
2475 return 1;
2476 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002477
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302478 for(uint8_t i = 0; i < 255; i++){
2479 if(((const uint8_t *)BigString.ptr)[i] != i) {
2480 return 1;
2481 }
2482 }
2483 return 0;
2484}
2485
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05302486
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302487int IndefiniteLengthStringTest()
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302488{
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302489 QCBORDecodeContext DC;
2490 QCBORItem Item;
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05302491 // big enough for MakeIndefiniteBigBstr() + MemPool overhead
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08002492 UsefulBuf_MAKE_STACK_UB(MemPool, 350);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002493
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302494 // --- Simple normal indefinite length string ------
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002495 UsefulBufC IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenString);
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302496 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002497
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05302498 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302499 return -1;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302500 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002501
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302502 if(QCBORDecode_GetNext(&DC, &Item)) {
2503 return -2;
2504 }
2505 if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.uDataAlloc) {
2506 return -3;
2507 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002508
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302509 if(QCBORDecode_GetNext(&DC, &Item)) {
2510 return -4;
2511 }
2512 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || !Item.uDataAlloc) {
2513 return -5;
2514 }
2515 if(QCBORDecode_Finish(&DC)) {
2516 return -6;
2517 }
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302518
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302519 // ----- types mismatch ---
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002520 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad2), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002521
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302522 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
2523 return -7;
2524 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002525
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302526 if(QCBORDecode_GetNext(&DC, &Item)) {
2527 return -8;
2528 }
2529 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
2530 return -9;
2531 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002532
Laurence Lundblade30816f22018-11-10 13:40:22 +07002533 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_INDEFINITE_STRING_CHUNK) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302534 return -10;
2535 }
2536
2537 // ----- not a string ---
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002538 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad3), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002539
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302540 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
2541 return -11;
2542 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002543
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302544 if(QCBORDecode_GetNext(&DC, &Item)) {
2545 return -12;
2546 }
2547 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
2548 return -13;
2549 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002550
Laurence Lundblade30816f22018-11-10 13:40:22 +07002551 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_INDEFINITE_STRING_CHUNK) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302552 return -14;
2553 }
2554
2555 // ----- no end -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002556 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad4), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002557
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302558 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
2559 return -15;
2560 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002561
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302562 if(QCBORDecode_GetNext(&DC, &Item)) {
2563 return -16;
2564 }
2565 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
2566 return -17;
2567 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002568
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302569 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_HIT_END) {
2570 return -18;
2571 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002572
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302573 // ------ Don't set a string allocator and see an error -----
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302574 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002575
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302576 QCBORDecode_GetNext(&DC, &Item);
2577 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302578 return -19;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302579 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002580
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302581 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_NO_STRING_ALLOCATOR) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302582 return -20;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302583 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002584
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302585 // ----- Mempool is way too small -----
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08002586 UsefulBuf_MAKE_STACK_UB(MemPoolTooSmall, QCBOR_DECODE_MIN_MEM_POOL_SIZE-1);
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302587
2588 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
2589 if(!QCBORDecode_SetMemPool(&DC, MemPoolTooSmall, false)) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302590 return -21;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302591 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002592
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302593 // ----- Mempool is way too small -----
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05302594 UsefulBuf_MAKE_STACK_UB(BigIndefBStrStorage, 290);
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08002595 const UsefulBufC BigIndefBStr = MakeIndefiniteBigBstr(BigIndefBStrStorage);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002596
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05302597 UsefulBuf_MAKE_STACK_UB(MemPoolSmall, 80); // 80 is big enough for MemPool overhead, but not BigIndefBStr
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002598
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302599 QCBORDecode_Init(&DC, BigIndefBStr, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302600 if(QCBORDecode_SetMemPool(&DC, MemPoolSmall, false)) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302601 return -22;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302602 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002603
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302604 QCBORDecode_GetNext(&DC, &Item);
2605 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302606 return -23;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302607 }
Laurence Lundblade30816f22018-11-10 13:40:22 +07002608 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_STRING_ALLOCATE) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302609 return -24;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302610 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002611
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302612 // ---- big bstr -----
2613 QCBORDecode_Init(&DC, BigIndefBStr, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002614
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302615 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
2616 return -25;
2617 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002618
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05302619 if(QCBORDecode_GetNext(&DC, &Item)) {
2620 return -26;
2621 }
2622 if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.uDataAlloc) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302623 return -26;
2624 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002625
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302626 if(QCBORDecode_GetNext(&DC, &Item)) {
2627 return -27;
2628 }
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05302629 if(Item.uDataType != QCBOR_TYPE_BYTE_STRING || !Item.uDataAlloc || Item.uNestingLevel != 1) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302630 return -28;
2631 }
2632 if(CheckBigString(Item.val.string)) {
2633 return -3;
2634 }
2635 if(QCBORDecode_Finish(&DC)) {
2636 return -29;
2637 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002638
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302639 // --- label is an indefinite length string ------
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002640 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringLabel), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002641
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302642 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
2643 return -30;
2644 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002645
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302646 QCBORDecode_GetNext(&DC, &Item);
2647 if(Item.uDataType != QCBOR_TYPE_MAP) {
2648 return -31;
2649 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002650
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302651 if(QCBORDecode_GetNext(&DC, &Item)){
2652 return -32;
2653 }
2654 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || Item.uDataType != QCBOR_TYPE_INT64 ||
2655 Item.uDataAlloc || !Item.uLabelAlloc ||
2656 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("struuming"))) {
2657 return -33;
2658 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002659
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302660 if(QCBORDecode_Finish(&DC)) {
2661 return -34;
2662 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002663
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08002664 return 0;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002665}
2666
2667
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302668int AllocAllStringsTest()
2669{
2670 QCBORDecodeContext DC;
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08002671 QCBORError nCBORError;
2672
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002673
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302674 // First test, use the "CSRMap" as easy input and checking
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002675 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002676
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08002677 UsefulBuf_MAKE_STACK_UB(Pool, sizeof(spCSRInput) + QCBOR_DECODE_MIN_MEM_POOL_SIZE);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002678
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08002679 nCBORError = QCBORDecode_SetMemPool(&DC, Pool, 1); // Turn on copying.
2680 if(nCBORError) {
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302681 return -1;
2682 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002683
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08002684 if(CheckCSRMaps(&DC)) {
2685 return -2;
2686 }
2687
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302688 // Next parse, save pointers to a few strings, destroy original and see all is OK.
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08002689 UsefulBuf_MAKE_STACK_UB(CopyOfStorage, sizeof(pValidMapEncoded) + QCBOR_DECODE_MIN_MEM_POOL_SIZE);
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08002690 const UsefulBufC CopyOf = UsefulBuf_Copy(CopyOfStorage, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded));
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002691
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302692 QCBORDecode_Init(&DC, CopyOf, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08002693 UsefulBuf_Set(Pool, '/');
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302694 QCBORDecode_SetMemPool(&DC, Pool, 1); // Turn on copying.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002695
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302696 QCBORItem Item1, Item2, Item3, Item4;
2697 if((nCBORError = QCBORDecode_GetNext(&DC, &Item1)))
2698 return nCBORError;
2699 if(Item1.uDataType != QCBOR_TYPE_MAP ||
2700 Item1.val.uCount != 3)
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08002701 return -3;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302702 if((nCBORError = QCBORDecode_GetNext(&DC, &Item1)))
2703 return nCBORError;
2704 if((nCBORError = QCBORDecode_GetNext(&DC, &Item2)))
2705 return nCBORError;
2706 if((nCBORError = QCBORDecode_GetNext(&DC, &Item3)))
2707 return nCBORError;
2708 if((nCBORError = QCBORDecode_GetNext(&DC, &Item4)))
2709 return nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002710
Laurence Lundblade05ec57b2018-10-21 01:50:03 +05302711 UsefulBuf_Set(CopyOfStorage, '_');
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002712
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302713 if(Item1.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302714 Item1.uDataType != QCBOR_TYPE_INT64 ||
2715 Item1.val.int64 != 42 ||
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08002716 Item1.uDataAlloc != 0 ||
2717 Item1.uLabelAlloc == 0 ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09002718 UsefulBuf_Compare(Item1.label.string, UsefulBuf_FromSZ("first integer"))) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08002719 return -4;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09002720 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002721
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302722
2723 if(Item2.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09002724 UsefulBuf_Compare(Item2.label.string, UsefulBuf_FromSZ("an array of two strings")) ||
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302725 Item2.uDataType != QCBOR_TYPE_ARRAY ||
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08002726 Item2.uDataAlloc != 0 ||
2727 Item2.uLabelAlloc == 0 ||
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302728 Item2.val.uCount != 2)
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08002729 return -5;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002730
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302731 if(Item3.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08002732 Item3.uDataAlloc == 0 ||
2733 Item3.uLabelAlloc != 0 ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09002734 UsefulBuf_Compare(Item3.val.string, UsefulBuf_FromSZ("string1"))) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08002735 return -6;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09002736 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002737
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302738 if(Item4.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08002739 Item4.uDataAlloc == 0 ||
2740 Item4.uLabelAlloc != 0 ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09002741 UsefulBuf_Compare(Item4.val.string, UsefulBuf_FromSZ("string2"))) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08002742 return -7;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09002743 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002744
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302745 // Next parse with a pool that is too small
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08002746 UsefulBuf_MAKE_STACK_UB(SmallPool, QCBOR_DECODE_MIN_MEM_POOL_SIZE + 1);
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05302747 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302748 QCBORDecode_SetMemPool(&DC, SmallPool, 1); // Turn on copying.
2749 if((nCBORError = QCBORDecode_GetNext(&DC, &Item1)))
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08002750 return -8;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302751 if(Item1.uDataType != QCBOR_TYPE_MAP ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09002752 Item1.val.uCount != 3) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08002753 return -9;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09002754 }
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302755 if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item1))){
2756 if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item2))) {
2757 if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item3))) {
2758 nCBORError = QCBORDecode_GetNext(&DC, &Item4);
2759 }
2760 }
2761 }
Laurence Lundblade30816f22018-11-10 13:40:22 +07002762 if(nCBORError != QCBOR_ERR_STRING_ALLOCATE) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08002763 return -10;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302764 }
2765
2766 return 0;
2767}
2768
Laurence Lundbladef6531662018-12-04 10:42:22 +09002769
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302770
2771int MemPoolTest(void)
Laurence Lundblade0155b622018-10-12 20:04:37 +08002772{
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08002773 // Set up the decoder with a tiny bit of CBOR to parse because
2774 // nothing can be done with it unless that is set up.
Laurence Lundbladef6531662018-12-04 10:42:22 +09002775 QCBORDecodeContext DC;
2776 const uint8_t pMinimalCBOR[] = {0xa0}; // One empty map
2777 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pMinimalCBOR),0);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002778
Laurence Lundbladef6531662018-12-04 10:42:22 +09002779 // Set up an memory pool of 100 bytes
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08002780 // Then fish into the internals of the decode context
2781 // to get the allocator function so it can be called directly.
2782 // Also figure out how much pool is available for use
2783 // buy subtracting out the overhead.
Laurence Lundbladef6531662018-12-04 10:42:22 +09002784 UsefulBuf_MAKE_STACK_UB(Pool, 100);
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08002785 QCBORError nError = QCBORDecode_SetMemPool(&DC, Pool, 0);
2786 if(nError) {
2787 return -9;
2788 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08002789 QCBORStringAllocate pAlloc = DC.StringAllocator.pfAllocator;
2790 void *pAllocCtx = DC.StringAllocator.pAllocateCxt;
2791 size_t uAvailPool = Pool.len - QCBOR_DECODE_MIN_MEM_POOL_SIZE;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002792
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08002793 // First test -- ask for one more byte than available and see failure
2794 UsefulBuf Allocated = (*pAlloc)(pAllocCtx, NULL, uAvailPool+1);
Laurence Lundbladef6531662018-12-04 10:42:22 +09002795 if(!UsefulBuf_IsNULL(Allocated)) {
2796 return -1;
2797 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002798
Laurence Lundbladef6531662018-12-04 10:42:22 +09002799 // Re do the set up for the next test that will do a successful alloc,
2800 // a fail, a free and then success
Laurence Lundbladef6531662018-12-04 10:42:22 +09002801 QCBORDecode_SetMemPool(&DC, Pool, 0);
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08002802 pAlloc = DC.StringAllocator.pfAllocator;
2803 pAllocCtx = DC.StringAllocator.pAllocateCxt;
2804 uAvailPool = Pool.len - QCBOR_DECODE_MIN_MEM_POOL_SIZE;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002805
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08002806 // Allocate one byte less than available and see success
2807 Allocated = (pAlloc)(pAllocCtx, NULL, uAvailPool-1);
Laurence Lundbladef6531662018-12-04 10:42:22 +09002808 if(UsefulBuf_IsNULL(Allocated)) { // expected to succeed
2809 return -2;
2810 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08002811 // Ask for some more and see failure
2812 UsefulBuf Allocated2 = (*pAlloc)(pAllocCtx, NULL, uAvailPool/2);
Laurence Lundbladef6531662018-12-04 10:42:22 +09002813 if(!UsefulBuf_IsNULL(Allocated2)) { // expected to fail
2814 return -3;
2815 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08002816 // Free the first allocate, retry the second and see success
2817 (*pAlloc)(pAllocCtx, Allocated.ptr, 0); // Free
2818 Allocated = (*pAlloc)(pAllocCtx, NULL, uAvailPool/2);
Laurence Lundbladef6531662018-12-04 10:42:22 +09002819 if(UsefulBuf_IsNULL(Allocated)) { // succeed because of the free
2820 return -4;
2821 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002822
Laurence Lundbladef6531662018-12-04 10:42:22 +09002823 // Re do set up for next test that involves a successful alloc,
2824 // and a successful realloc and a failed realloc
2825 QCBORDecode_SetMemPool(&DC, Pool, 0);
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08002826 pAlloc = DC.StringAllocator.pfAllocator;
2827 pAllocCtx = DC.StringAllocator.pAllocateCxt;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002828
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08002829 // Allocate half the pool and see success
2830 Allocated = (*pAlloc)(pAllocCtx, NULL, uAvailPool/2);
Laurence Lundbladef6531662018-12-04 10:42:22 +09002831 if(UsefulBuf_IsNULL(Allocated)) { // expected to succeed
2832 return -5;
2833 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08002834 // Reallocate to take up the whole pool and see success
2835 Allocated2 = (*pAlloc)(pAllocCtx, Allocated.ptr, uAvailPool);
Laurence Lundbladef6531662018-12-04 10:42:22 +09002836 if(UsefulBuf_IsNULL(Allocated2)) {
2837 return -6;
2838 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08002839 // Make sure its the same pointer and the size is right
Laurence Lundbladef6531662018-12-04 10:42:22 +09002840 if(Allocated2.ptr != Allocated.ptr || Allocated2.len != uAvailPool) {
2841 return -7;
2842 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08002843 // Try to allocate more to be sure there is failure after a realloc
2844 UsefulBuf Allocated3 = (*pAlloc)(pAllocCtx, Allocated.ptr, uAvailPool+1);
2845 if(!UsefulBuf_IsNULL(Allocated3)) {
Laurence Lundbladef6531662018-12-04 10:42:22 +09002846 return -8;
2847 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002848
Laurence Lundbladef6531662018-12-04 10:42:22 +09002849 return 0;
2850}
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002851
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08002852
2853/* Just enough of an allocator to test configuration of one */
2854static UsefulBuf AllocateTestFunction(void *pCtx, void *pOldMem, size_t uNewSize)
2855{
2856 (void)pOldMem; // unused variable
2857
2858 if(uNewSize) {
2859 // Assumes the context pointer is the buffer and
2860 // nothing too big will ever be asked for.
2861 // This is only good for this basic test!
2862 return (UsefulBuf) {pCtx, uNewSize};
2863 } else {
2864 return NULLUsefulBuf;
2865 }
2866}
2867
2868
2869int SetUpAllocatorTest(void)
2870{
2871 // Set up the decoder with a tiny bit of CBOR to parse because
2872 // nothing can be done with it unless that is set up.
2873 QCBORDecodeContext DC;
2874 const uint8_t pMinimalCBOR[] = {0x62, 0x48, 0x69}; // "Hi"
2875 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pMinimalCBOR),0);
2876
2877 uint8_t pAllocatorBuffer[50];
2878
2879 // This is really just to test that this call works.
2880 // The full functionality of string allocators is tested
2881 // elsewhere with the MemPool internal allocator.
2882 QCBORDecode_SetUpAllocator(&DC, AllocateTestFunction, pAllocatorBuffer, 1);
2883
2884 QCBORItem Item;
2885 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_SUCCESS) {
2886 return -1;
2887 }
2888
2889 if(Item.uDataAlloc == 0 ||
2890 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
2891 Item.val.string.ptr != pAllocatorBuffer) {
2892 return -2;
2893 }
2894
2895 if(QCBORDecode_Finish(&DC) != QCBOR_SUCCESS) {
2896 return -3;
2897 }
2898
2899 return 0;
2900}
2901