blob: 749a5d28c14ac78ff6d4af03875aac5a56385906 [file] [log] [blame]
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001/*==============================================================================
2Copyright (c) 2016-2018, The Linux Foundation. All rights reserved.
3
4Redistribution and use in source and binary forms, with or without
5modification, are permitted provided that the following conditions are
6met:
7 * Redistributions of source code must retain the above copyright
8 notice, this list of conditions and the following disclaimer.
9 * Redistributions in binary form must reproduce the above
10 copyright notice, this list of conditions and the following
11 disclaimer in the documentation and/or other materials provided
12 with the distribution.
13 * Neither the name of The Linux Foundation nor the names of its
14 contributors may be used to endorse or promote products derived
15 from this software without specific prior written permission.
16
17THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
18WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
19MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
20ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
21BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
24BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
25WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
26OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
27IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28==============================================================================*/
29
Laurence Lundblade9e3651c2018-10-10 11:49:55 +080030/*==============================================================================
31 Modifications beyond the version released on CAF are under the MIT license:
32
33 Copyright 2018 Laurence Lundblade
34
35 Permission is hereby granted, free of charge, to any person obtaining
36 a copy of this software and associated documentation files (the
37 "Software"), to deal in the Software without restriction, including
38 without limitation the rights to use, copy, modify, merge, publish,
39 distribute, sublicense, and/or sell copies of the Software, and to
40 permit persons to whom the Software is furnished to do so, subject to
41 the following conditions:
42
43 The above copyright notice and this permission notice shall be included
44 in all copies or substantial portions of the Software.
45
46 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
47 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
48 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
49 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
50 BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
51 ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
52 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
53 SOFTWARE.
54 ==============================================================================*/
55
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080056#include "qcbor.h"
57#include "qcbor_decode_tests.h"
58#include <stdio.h>
59#include <strings.h>
Laurence Lundblade9e3651c2018-10-10 11:49:55 +080060#include <math.h> // for fabs()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080061#include <stdlib.h>
62
63
64// TODO: test other than the normal decoder mode
65
66static void printencoded(const char *szLabel, const uint8_t *pEncoded, size_t nLen)
67{
68 if(szLabel) {
69 printf("%s ", szLabel);
70 }
71
Laurence Lundblade570fab52018-10-13 18:28:27 +080072 size_t i;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080073 for(i = 0; i < nLen; i++) {
74 uint8_t Z = pEncoded[i];
75 printf("%02x ", Z);
76 }
77 printf("\n");
78
79 fflush(stdout);
80}
81
82
83// TODO: -- add a test for counting the top level items and adding it back in with AddRaw()
84
85
Laurence Lundbladeb836efb2018-10-28 20:09:58 +070086static const uint8_t spExpectedEncodedInts[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080087 0x98, 0x2f, 0x3b, 0x7f, 0xff, 0xff, 0xff, 0xff,
88 0xff, 0xff, 0xff, 0x3b, 0x00, 0x00, 0x00, 0x01,
89 0x00, 0x00, 0x00, 0x00, 0x3a, 0xff, 0xff, 0xff,
90 0xff, 0x3a, 0xff, 0xff, 0xff, 0xfe, 0x3a, 0xff,
91 0xff, 0xff, 0xfd, 0x3a, 0x7f, 0xff, 0xff, 0xff,
92 0x3a, 0x7f, 0xff, 0xff, 0xfe, 0x3a, 0x00, 0x01,
93 0x00, 0x01, 0x3a, 0x00, 0x01, 0x00, 0x00, 0x39,
94 0xff, 0xff, 0x39, 0xff, 0xfe, 0x39, 0xff, 0xfd,
95 0x39, 0x01, 0x00, 0x38, 0xff, 0x38, 0xfe, 0x38,
96 0xfd, 0x38, 0x18, 0x37, 0x36, 0x20, 0x00, 0x00,
97 0x01, 0x16, 0x17, 0x18, 0x18, 0x18, 0x19, 0x18,
98 0x1a, 0x18, 0xfe, 0x18, 0xff, 0x19, 0x01, 0x00,
99 0x19, 0x01, 0x01, 0x19, 0xff, 0xfe, 0x19, 0xff,
100 0xff, 0x1a, 0x00, 0x01, 0x00, 0x00, 0x1a, 0x00,
101 0x01, 0x00, 0x01, 0x1a, 0x00, 0x01, 0x00, 0x02,
102 0x1a, 0x7f, 0xff, 0xff, 0xff, 0x1a, 0x7f, 0xff,
103 0xff, 0xff, 0x1a, 0x80, 0x00, 0x00, 0x00, 0x1a,
104 0x80, 0x00, 0x00, 0x01, 0x1a, 0xff, 0xff, 0xff,
105 0xfe, 0x1a, 0xff, 0xff, 0xff, 0xff, 0x1b, 0x00,
106 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x1b,
107 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01,
108 0x1b, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
109 0xff, 0x1b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
110 0xff, 0xff};
111
112
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800113// return CBOR error or -1 if type of value doesn't match
114
115static int IntegerValuesParseTestInternal(QCBORDecodeContext *pDCtx)
116{
117 QCBORItem Item;
118 int nCBORError;
119
120 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
121 return nCBORError;
122 if(Item.uDataType != QCBOR_TYPE_ARRAY)
123 return -1;
124
125 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
126 return nCBORError;
127 if(Item.uDataType != QCBOR_TYPE_INT64 || // Todo; fix this for 32-bit machines
Laurence Lundblade570fab52018-10-13 18:28:27 +0800128 Item.val.int64 != -9223372036854775807LL - 1)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800129 return -1;
130
131 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
132 return nCBORError;
133 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade570fab52018-10-13 18:28:27 +0800134 Item.val.int64 != -4294967297)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800135 return -1;
136
137 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
138 return nCBORError;
139 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade570fab52018-10-13 18:28:27 +0800140 Item.val.int64 != -4294967296)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800141 return -1;
142
143 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
144 return nCBORError;
145 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade570fab52018-10-13 18:28:27 +0800146 Item.val.int64 != -4294967295)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800147 return -1;
148
149 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
150 return nCBORError;
151 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade570fab52018-10-13 18:28:27 +0800152 Item.val.int64 != -4294967294)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800153 return -1;
154
155
156 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
157 return nCBORError;
158 if(Item.uDataType != QCBOR_TYPE_INT64 ||
159 Item.val.int64 != -2147483648)
160 return -1;
161
162 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
163 return nCBORError;
164 if(Item.uDataType != QCBOR_TYPE_INT64 ||
165 Item.val.int64 != -2147483647)
166 return -1;
167
168 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
169 return nCBORError;
170 if(Item.uDataType != QCBOR_TYPE_INT64 ||
171 Item.val.int64 != -65538)
172 return -1;
173
174 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
175 return nCBORError;
176 if(Item.uDataType != QCBOR_TYPE_INT64 ||
177 Item.val.int64 != -65537)
178 return -1;
179
180 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
181 return nCBORError;
182 if(Item.uDataType != QCBOR_TYPE_INT64 ||
183 Item.val.int64 != -65536)
184 return -1;
185
186
187 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
188 return nCBORError;
189 if(Item.uDataType != QCBOR_TYPE_INT64 ||
190 Item.val.int64 != -65535)
191 return -1;
192
193
194 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
195 return nCBORError;
196 if(Item.uDataType != QCBOR_TYPE_INT64 ||
197 Item.val.int64 != -65534)
198 return -1;
199
200
201 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
202 return nCBORError;
203 if(Item.uDataType != QCBOR_TYPE_INT64 ||
204 Item.val.int64 != -257)
205 return -1;
206
207 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
208 return nCBORError;
209 if(Item.uDataType != QCBOR_TYPE_INT64 ||
210 Item.val.int64 != -256)
211 return -1;
212
213 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
214 return nCBORError;
215 if(Item.uDataType != QCBOR_TYPE_INT64 ||
216 Item.val.int64 != -255)
217 return -1;
218
219 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
220 return nCBORError;
221 if(Item.uDataType != QCBOR_TYPE_INT64 ||
222 Item.val.int64 != -254)
223 return -1;
224
225
226 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
227 return nCBORError;
228 if(Item.uDataType != QCBOR_TYPE_INT64 ||
229 Item.val.int64 != -25)
230 return -1;
231
232
233 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
234 return nCBORError;
235 if(Item.uDataType != QCBOR_TYPE_INT64 ||
236 Item.val.int64 != -24)
237 return -1;
238
239
240 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
241 return nCBORError;
242 if(Item.uDataType != QCBOR_TYPE_INT64 ||
243 Item.val.int64 != -23)
244 return -1;
245
246
247 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
248 return nCBORError;
249 if(Item.uDataType != QCBOR_TYPE_INT64 ||
250 Item.val.int64 != -1)
251 return -1;
252
253
254 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
255 return nCBORError;
256 if(Item.uDataType != QCBOR_TYPE_INT64 ||
257 Item.val.int64 != 0)
258 return -1;
259
260
261 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
262 return nCBORError;
263 if(Item.uDataType != QCBOR_TYPE_INT64 ||
264 Item.val.int64 != 0)
265 return -1;
266
267 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
268 return nCBORError;
269 if(Item.uDataType != QCBOR_TYPE_INT64 ||
270 Item.val.int64 != 1)
271 return -1;
272
273
274 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
275 return nCBORError;
276 if(Item.uDataType != QCBOR_TYPE_INT64 ||
277 Item.val.int64 != 22)
278 return -1;
279
280
281 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
282 return nCBORError;
283 if(Item.uDataType != QCBOR_TYPE_INT64 ||
284 Item.val.int64 != 23)
285 return -1;
286
287
288 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
289 return nCBORError;
290 if(Item.uDataType != QCBOR_TYPE_INT64 ||
291 Item.val.int64 != 24)
292 return -1;
293
294
295 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
296 return nCBORError;
297 if(Item.uDataType != QCBOR_TYPE_INT64 ||
298 Item.val.int64 != 25)
299 return -1;
300
301 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
302 return nCBORError;
303 if(Item.uDataType != QCBOR_TYPE_INT64 ||
304 Item.val.int64 != 26)
305 return -1;
306
307
308 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
309 return nCBORError;
310 if(Item.uDataType != QCBOR_TYPE_INT64 ||
311 Item.val.int64 != 254)
312 return -1;
313
314
315 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
316 return nCBORError;
317 if(Item.uDataType != QCBOR_TYPE_INT64 ||
318 Item.val.int64 != 255)
319 return -1;
320
321
322 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
323 return nCBORError;
324 if(Item.uDataType != QCBOR_TYPE_INT64 ||
325 Item.val.int64 != 256)
326 return -1;
327
328
329 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
330 return nCBORError;
331 if(Item.uDataType != QCBOR_TYPE_INT64 ||
332 Item.val.int64 != 257)
333 return -1;
334
335 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
336 return nCBORError;
337 if(Item.uDataType != QCBOR_TYPE_INT64 ||
338 Item.val.int64 != 65534)
339 return -1;
340
341
342 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
343 return nCBORError;
344 if(Item.uDataType != QCBOR_TYPE_INT64 ||
345 Item.val.int64 != 65535)
346 return -1;
347
348
349 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
350 return nCBORError;
351 if(Item.uDataType != QCBOR_TYPE_INT64 ||
352 Item.val.int64 != 65536)
353 return -1;
354
355 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
356 return nCBORError;
357 if(Item.uDataType != QCBOR_TYPE_INT64 ||
358 Item.val.int64 != 65537)
359 return -1;
360
361 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
362 return nCBORError;
363 if(Item.uDataType != QCBOR_TYPE_INT64 ||
364 Item.val.int64 != 65538)
365 return -1;
366
367 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
368 return nCBORError;
369 if(Item.uDataType != QCBOR_TYPE_INT64 ||
370 Item.val.int64 != 2147483647)
371 return -1;
372
373 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
374 return nCBORError;
375 if(Item.uDataType != QCBOR_TYPE_INT64 ||
376 Item.val.int64 != 2147483647)
377 return -1;
378
379 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
380 return nCBORError;
381 if(Item.uDataType != QCBOR_TYPE_INT64 ||
382 Item.val.int64 != 2147483648)
383 return -1;
384
385 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
386 return nCBORError;
387 if(Item.uDataType != QCBOR_TYPE_INT64 ||
388 Item.val.int64 != 2147483649)
389 return -1;
390
391 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
392 return nCBORError;
393 if(Item.uDataType != QCBOR_TYPE_INT64 ||
394 Item.val.int64 != 4294967294)
395 return -1;
396
397
398 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
399 return nCBORError;
400 if(Item.uDataType != QCBOR_TYPE_INT64 ||
401 Item.val.int64 != 4294967295)
402 return -1;
403
404
405 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
406 return nCBORError;
407 if(Item.uDataType != QCBOR_TYPE_INT64 ||
408 Item.val.int64 != 4294967296)
409 return -1;
410
411
412 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
413 return nCBORError;
414 if(Item.uDataType != QCBOR_TYPE_INT64 ||
415 Item.val.int64 != 4294967297)
416 return -1;
417
418
419
420 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
421 return nCBORError;
422 if(Item.uDataType != QCBOR_TYPE_INT64 ||
423 Item.val.int64 != 9223372036854775807LL)
424 return -1;
425
426
427 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
428 return nCBORError;
429 if(Item.uDataType != QCBOR_TYPE_UINT64 ||
430 Item.val.uint64 != 18446744073709551615ULL)
431 return -1;
432
433
434 if(QCBORDecode_Finish(pDCtx) != QCBOR_SUCCESS) {
435 return -1;
436 }
437
438 return 0;
439}
440
441
442/*
443 Tests the decoding of lots of different integers sizes
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +0800444 and values.
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800445 */
446
447int IntegerValuesParseTest()
448{
449 int n;
450 QCBORDecodeContext DCtx;
451
Laurence Lundbladeb836efb2018-10-28 20:09:58 +0700452 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedEncodedInts), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800453
454 n = IntegerValuesParseTestInternal(&DCtx);
455
456 return(n);
457}
458
459
460/*
461 Creates a simple CBOR array and returns it in *pEncoded. The array is malloced
462 and needs to be freed. This is used by several tests.
463
464 Two of the inputs can be set. Two other items in the array are fixed.
465
466 */
467
468static int CreateSimpleArray(int nInt1, int nInt2, uint8_t **pEncoded, size_t *pEncodedLen)
469{
470 QCBOREncodeContext ECtx;
471 int nReturn = -1;
472
473 *pEncoded = NULL;
474 *pEncodedLen = INT32_MAX;
475
476 // loop runs CBOR encoding twice. First with no buffer to
477 // calucate the length so buffer can be allocated correctly,
478 // and last with the buffer to do the actual encoding
479 do {
Laurence Lundblade9e3651c2018-10-10 11:49:55 +0800480 QCBOREncode_Init(&ECtx, (UsefulBuf){*pEncoded, *pEncodedLen});
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800481 QCBOREncode_OpenArray(&ECtx);
482 QCBOREncode_AddInt64(&ECtx, nInt1);
483 QCBOREncode_AddInt64(&ECtx, nInt2);
484 QCBOREncode_AddBytes(&ECtx, ((UsefulBufC) {"galactic", 8}));
485 QCBOREncode_AddBytes(&ECtx, ((UsefulBufC) {"haven token", 11}));
486 QCBOREncode_CloseArray(&ECtx);
487
488 if(QCBOREncode_Finish(&ECtx, pEncodedLen))
489 goto Done;
490
491 if(*pEncoded != NULL) {
492 nReturn = 0;
493 goto Done;
494 }
495 *pEncoded = malloc(*pEncodedLen);
496 if(*pEncoded == NULL) {
497 nReturn = -1;
498 goto Done;
499 }
500
501 } while(1);
502Done:
503 return (nReturn);
504
505}
506
507
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800508/*
509 {"first integer": 42,
510 "an array of two strings": ["string1", "string2"],
511 "map in a map": {
512 "bytes 1": h'78787878',
513 "bytes 2": h'79797979',
514 "another int": 98, "text 2":
515 "lies, damn lies and statistics"}
516 }
517 */
518
519static uint8_t pValidMapEncoded[] = {
Laurence Lundbladeb836efb2018-10-28 20:09:58 +0700520 0xa3, 0x6d, 0x66, 0x69, 0x72, 0x73, 0x74, 0x20, 0x69, 0x6e,
521 0x74, 0x65, 0x67, 0x65, 0x72, 0x18, 0x2a, 0x77, 0x61, 0x6e,
522 0x20, 0x61, 0x72, 0x72, 0x61, 0x79, 0x20, 0x6f, 0x66, 0x20,
523 0x74, 0x77, 0x6f, 0x20, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67,
524 0x73, 0x82, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x31,
525 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x32, 0x6c, 0x6d,
526 0x61, 0x70, 0x20, 0x69, 0x6e, 0x20, 0x61, 0x20, 0x6d, 0x61,
527 0x70, 0xa4, 0x67, 0x62, 0x79, 0x74, 0x65, 0x73, 0x20, 0x31,
528 0x44, 0x78, 0x78, 0x78, 0x78, 0x67, 0x62, 0x79, 0x74, 0x65,
529 0x73, 0x20, 0x32, 0x44, 0x79, 0x79, 0x79, 0x79, 0x6b, 0x61,
530 0x6e, 0x6f, 0x74, 0x68, 0x65, 0x72, 0x20, 0x69, 0x6e, 0x74,
531 0x18, 0x62, 0x66, 0x74, 0x65, 0x78, 0x74, 0x20, 0x32, 0x78,
532 0x1e, 0x6c, 0x69, 0x65, 0x73, 0x2c, 0x20, 0x64, 0x61, 0x6d,
533 0x6e, 0x20, 0x6c, 0x69, 0x65, 0x73, 0x20, 0x61, 0x6e, 0x64,
534 0x20, 0x73, 0x74, 0x61, 0x74, 0x69, 0x73, 0x74, 0x69, 0x63,
535 0x73 } ;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800536
537static 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)
538{
539 QCBORDecodeContext DCtx;
540 QCBORItem Item;
541 int nReturn = -1; // assume error until success
542
543 QCBORDecode_Init(&DCtx, (UsefulBufC){pEncoded, nLen}, QCBOR_DECODE_MODE_NORMAL);
544
545 // Make sure the first thing is a map
546 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || Item.uDataType != QCBOR_TYPE_ARRAY)
547 goto Done;
548
549 // First integer
550 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 | Item.uDataType != QCBOR_TYPE_INT64)
551 goto Done;
552 *pInt1 = Item.val.int64;
553
554 // Second integer
555 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || Item.uDataType != QCBOR_TYPE_INT64)
556 goto Done;
557 *pInt2 = Item.val.int64;
558
559 // First string
560 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || Item.uDataType != QCBOR_TYPE_BYTE_STRING)
561 goto Done;
562 *pBuf3 = Item.val.string.ptr;
563 *pBuf3Len = Item.val.string.len;
564
565 // Second string
566 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || Item.uDataType != QCBOR_TYPE_BYTE_STRING)
567 goto Done;
568 *pBuf4 = Item.val.string.ptr;
569 *pBuf4Len = Item.val.string.len;
570
571 nReturn = 0;
572
573Done:
574 return(nReturn);
575}
576
577
578
Laurence Lundblade9e3651c2018-10-10 11:49:55 +0800579
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800580int SimpleArrayTest()
581{
582 uint8_t *pEncoded;
583 size_t nEncodedLen;
584
585 int64_t i1, i2;
586 size_t i3, i4;
587 const uint8_t *s3, *s4;
588
589
590 if(CreateSimpleArray(23, 6000, &pEncoded, &nEncodedLen) < 0) {
591 return(-1);
592 }
593
594 ParseOrderedArray(pEncoded, nEncodedLen, &i1, &i2, &s3, &i3, &s4, &i4);
595
596 if(i1 != 23 ||
597 i2 != 6000 ||
598 i3 != 8 ||
599 i4 != 11 ||
Laurence Lundbladea44d5062018-10-17 18:45:12 +0530600 memcmp("galactic", s3, 8) !=0 ||
601 memcmp("haven token", s4, 11) !=0) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800602 printf("SimpleArraryTest Failed\n");
603 return(-1);
604 }
605
606 return(0);
607}
608
609
Laurence Lundblade9e3651c2018-10-10 11:49:55 +0800610
Laurence Lundbladeb836efb2018-10-28 20:09:58 +0700611static uint8_t spDeepArrays[] = {0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x80};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800612
613int ParseDeepArrayTest()
614{
615 QCBORDecodeContext DCtx;
616 int nReturn = 0;
617 int i;
618
Laurence Lundbladeb836efb2018-10-28 20:09:58 +0700619 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spDeepArrays), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800620
621 for(i = 0; i < 10; i++) {
622 QCBORItem Item;
623
624 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
625 Item.uDataType != QCBOR_TYPE_ARRAY ||
626 Item.uNestingLevel != i) {
627 nReturn = -1;
628 break;
629 }
630 }
631
632 return(nReturn);
633}
634
635
Laurence Lundbladeb836efb2018-10-28 20:09:58 +0700636static uint8_t spTooDeepArrays[] = {0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x80};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800637
638int ParseTooDeepArrayTest()
639{
640 QCBORDecodeContext DCtx;
641 int nReturn = 0;
642 int i;
643 QCBORItem Item;
644
645
Laurence Lundbladeb836efb2018-10-28 20:09:58 +0700646 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTooDeepArrays), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800647
648 for(i = 0; i < 10; i++) {
649
650 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
651 Item.uDataType != QCBOR_TYPE_ARRAY ||
652 Item.uNestingLevel != i) {
653 nReturn = -1;
654 break;
655 }
656 }
657
658 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_ARRAY_NESTING_TOO_DEEP)
659 nReturn = -1;
660
661 return(nReturn);
662}
663
664
665
666
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800667int ShortBufferParseTest()
668{
669 int nResult = 0;
670 QCBORDecodeContext DCtx;
671 int num;
672
Laurence Lundbladeb836efb2018-10-28 20:09:58 +0700673 for(num = sizeof(spExpectedEncodedInts)-1; num; num--) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800674 int n;
675
Laurence Lundbladeb836efb2018-10-28 20:09:58 +0700676 QCBORDecode_Init(&DCtx, (UsefulBufC){spExpectedEncodedInts, num}, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800677
678 n = IntegerValuesParseTestInternal(&DCtx);
679
680 //printf("Len %d, result: %d\n", num, n);
681
682 if(n != QCBOR_ERR_HIT_END) {
683 nResult = -1;
684 goto Done;
685 }
686 }
687Done:
688 return nResult;
689}
690
691
Laurence Lundblade9e3651c2018-10-10 11:49:55 +0800692
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800693int ShortBufferParseTest2()
694{
695 uint8_t *pEncoded;
696 int nReturn;
697 size_t nEncodedLen;
698
699 int64_t i1, i2;
700 size_t i3, i4;
701 const uint8_t *s3, *s4;
702
703 nReturn = 0;
704
705 if(CreateSimpleArray(23, 6000, &pEncoded, &nEncodedLen) < 0) {
706 return(-1);
707 }
708
709 //printencoded(pEncoded, nEncodedLen);
710
711 for(nEncodedLen--; nEncodedLen; nEncodedLen--) {
712 int nResult = ParseOrderedArray(pEncoded, (uint32_t)nEncodedLen, &i1, &i2, &s3, &i3, &s4, &i4);
713 if(nResult == 0) {
714 nReturn = -1;
715 }
716 }
717
718 return(nReturn);
719}
720
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530721/*
722 Decode and thoroughly check a moderately complex
723 set of maps
724 */
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800725static int ParseMapTest1()
726{
727 QCBORDecodeContext DCtx;
728 QCBORItem Item;
729 int nCBORError;
730
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800731 QCBORDecode_Init(&DCtx, (UsefulBufC){pValidMapEncoded, sizeof(pValidMapEncoded)}, QCBOR_DECODE_MODE_NORMAL);
732
733 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
734 return nCBORError;
735 if(Item.uDataType != QCBOR_TYPE_MAP ||
736 Item.val.uCount != 3)
737 return -1;
738
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800739 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
740 return nCBORError;
741 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
742 Item.label.string.len != 13 ||
743 Item.uDataType != QCBOR_TYPE_INT64 ||
744 Item.val.int64 != 42 ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530745 Item.uDataAlloc ||
746 Item.uLabelAlloc ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800747 memcmp(Item.label.string.ptr, "first integer", 13))
748 return -1;
749
750 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
751 return nCBORError;
752 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
753 Item.label.string.len != 23 ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530754 Item.uDataAlloc ||
755 Item.uLabelAlloc ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800756 memcmp(Item.label.string.ptr, "an array of two strings", 23) ||
757 Item.uDataType != QCBOR_TYPE_ARRAY ||
758 Item.val.uCount != 2)
759 return -1;
760
761 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
762 return nCBORError;
763 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
764 Item.val.string.len != 7 ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530765 Item.uDataAlloc ||
766 Item.uLabelAlloc ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800767 memcmp(Item.val.string.ptr, "string1", 7))
768 return -1;
769
770 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
771 return nCBORError;
772 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
773 Item.val.string.len != 7 ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530774 Item.uDataAlloc ||
775 Item.uLabelAlloc ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800776 memcmp(Item.val.string.ptr, "string2", 7))
777 return -1;
778
779 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
780 return nCBORError;
781 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
782 Item.label.string.len != 12 ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530783 Item.uDataAlloc ||
784 Item.uLabelAlloc ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800785 memcmp(Item.label.string.ptr, "map in a map", 12) ||
786 Item.uDataType != QCBOR_TYPE_MAP ||
787 Item.val.uCount != 4)
788 return -1;
789
790 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
791 return nCBORError;
792 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
793 Item.label.string.len != 7 ||
794 memcmp(Item.label.string.ptr, "bytes 1", 7)||
795 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
796 Item.val.string.len != 4 ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530797 Item.uDataAlloc ||
798 Item.uLabelAlloc ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800799 memcmp(Item.val.string.ptr, "xxxx", 4))
800 return -1;
801
802 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
803 return nCBORError;
804 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
805 Item.label.string.len != 7 ||
806 memcmp(Item.label.string.ptr, "bytes 2", 7) ||
807 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
808 Item.val.string.len != 4 ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530809 Item.uDataAlloc ||
810 Item.uLabelAlloc ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800811 memcmp(Item.val.string.ptr, "yyyy", 4))
812 return -1;
813
814 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
815 return nCBORError;
816 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
817 Item.label.string.len != 11 ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530818 Item.uDataAlloc ||
819 Item.uLabelAlloc ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800820 memcmp(Item.label.string.ptr, "another int", 11) ||
821 Item.uDataType != QCBOR_TYPE_INT64 ||
822 Item.val.int64 != 98)
823 return -1;
824
825 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
826 return nCBORError;
827 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
828 Item.label.string.len != 6 ||
829 memcmp(Item.label.string.ptr, "text 2", 6)||
830 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
831 Item.val.string.len != 30 ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530832 Item.uDataAlloc ||
833 Item.uLabelAlloc ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800834 memcmp(Item.val.string.ptr, "lies, damn lies and statistics", 30))
835 return -1;
836
837 return 0;
838}
839
840
841
842/*
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530843 Fully or partially decode pValidMapEncoded. When
844 partially decoding check for the right error code.
845 How much partial decoding depends on nLevel.
846
847 The partial decodes test error conditions of
848 incomplete encoded input.
849
850 This could be combined with the above test
851 and made prettier and maybe a little more
852 thorough.
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800853 */
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +0800854static int ExtraBytesTest(int nLevel)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800855{
856 QCBORDecodeContext DCtx;
857 QCBORItem Item;
858 int nCBORError;
859
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800860 QCBORDecode_Init(&DCtx, (UsefulBufC){pValidMapEncoded, sizeof(pValidMapEncoded)}, QCBOR_DECODE_MODE_NORMAL);
861
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +0800862 if(nLevel < 1) {
863 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_EXTRA_BYTES) {
864 return -1;
865 } else {
866 return 0;
867 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800868 }
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530869
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +0800870
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800871 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
872 return nCBORError;
873 if(Item.uDataType != QCBOR_TYPE_MAP ||
874 Item.val.uCount != 3)
875 return -1;
876
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +0800877 if(nLevel < 2) {
878 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
879 return -1;
880 } else {
881 return 0;
882 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800883 }
884
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +0800885
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800886 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
887 return nCBORError;
888 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
889 Item.label.string.len != 13 ||
890 Item.uDataType != QCBOR_TYPE_INT64 ||
891 Item.val.uCount != 42 ||
892 memcmp(Item.label.string.ptr, "first integer", 13))
893 return -1;
894
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +0800895 if(nLevel < 3) {
896 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
897 return -1;
898 } else {
899 return 0;
900 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800901 }
902
903 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
904 return nCBORError;
905 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
906 Item.label.string.len != 23 ||
907 memcmp(Item.label.string.ptr, "an array of two strings", 23) ||
908 Item.uDataType != QCBOR_TYPE_ARRAY ||
909 Item.val.uCount != 2)
910 return -1;
911
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +0800912
913 if(nLevel < 4) {
914 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
915 return -1;
916 } else {
917 return 0;
918 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800919 }
920
921
922 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
923 return nCBORError;
924 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
925 Item.val.string.len != 7 ||
926 memcmp(Item.val.string.ptr, "string1", 7))
927 return -1;
928
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +0800929 if(nLevel < 5) {
930 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
931 return -1;
932 } else {
933 return 0;
934 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800935 }
936
937 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
938 return nCBORError;
939 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
940 Item.val.string.len != 7 ||
941 memcmp(Item.val.string.ptr, "string2", 7))
942 return -1;
943
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +0800944 if(nLevel < 6) {
945 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
946 return -1;
947 } else {
948 return 0;
949 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800950 }
951
952 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
953 return nCBORError;
954 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
955 Item.label.string.len != 12 ||
956 memcmp(Item.label.string.ptr, "map in a map", 12) ||
957 Item.uDataType != QCBOR_TYPE_MAP ||
958 Item.val.uCount != 4)
959 return -1;
960
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +0800961 if(nLevel < 7) {
962 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
963 return -1;
964 } else {
965 return 0;
966 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800967 }
968
969 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
970 return nCBORError;
971 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
972 Item.label.string.len != 7 ||
973 memcmp(Item.label.string.ptr, "bytes 1", 7)||
974 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
975 Item.val.string.len != 4 ||
976 memcmp(Item.val.string.ptr, "xxxx", 4))
977 return -1;
978
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +0800979 if(nLevel < 8) {
980 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
981 return -1;
982 } else {
983 return 0;
984 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800985 }
986
987 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
988 return nCBORError;
989 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
990 Item.label.string.len != 7 ||
991 memcmp(Item.label.string.ptr, "bytes 2", 7) ||
992 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
993 Item.val.string.len != 4 ||
994 memcmp(Item.val.string.ptr, "yyyy", 4))
995 return -1;
996
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +0800997 if(nLevel < 9) {
998 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
999 return -1;
1000 } else {
1001 return 0;
1002 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001003 }
1004
1005 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1006 return nCBORError;
1007 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
1008 Item.label.string.len != 11 ||
1009 memcmp(Item.label.string.ptr, "another int", 11) ||
1010 Item.uDataType != QCBOR_TYPE_INT64 ||
1011 Item.val.int64 != 98)
1012 return -1;
1013
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001014 if(nLevel < 10) {
1015 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
1016 return -1;
1017 } else {
1018 return 0;
1019 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001020 }
1021
1022 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1023 return nCBORError;
1024 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
1025 Item.label.string.len != 6 ||
1026 memcmp(Item.label.string.ptr, "text 2", 6)||
1027 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1028 Item.val.string.len != 30 ||
1029 memcmp(Item.val.string.ptr, "lies, damn lies and statistics", 30))
1030 return -1;
1031
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301032 if(QCBORDecode_Finish(&DCtx)) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001033 return -1;
1034 }
1035
1036 return 0;
1037}
1038
1039
1040
1041
1042int ParseMapTest()
1043{
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301044 // Parse a moderatly complex map structure very thoroughl
1045 int n = ParseMapTest1();
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001046
1047 if(!n) {
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001048 for(int i = 0; i < 10; i++) {
1049 n = ExtraBytesTest(i);
1050 if(n) {
1051 break;
1052 }
1053 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001054 }
1055
1056 return(n);
1057}
1058
1059
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07001060static 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 +08001061
1062int ParseSimpleTest()
1063{
1064 QCBORDecodeContext DCtx;
1065 QCBORItem Item;
1066 int nCBORError;
1067
1068
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07001069 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSimpleValues), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001070
1071
1072 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1073 return nCBORError;
1074 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
1075 Item.val.uCount != 10)
1076 return -1;
1077
1078 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1079 return nCBORError;
1080 if(Item.uDataType != QCBOR_TYPE_FALSE)
1081 return -1;
1082
1083 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1084 return nCBORError;
1085 if(Item.uDataType != QCBOR_TYPE_TRUE)
1086 return -1;
1087
1088 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1089 return nCBORError;
1090 if(Item.uDataType != QCBOR_TYPE_NULL)
1091 return -1;
1092
1093 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1094 return nCBORError;
1095 if(Item.uDataType != QCBOR_TYPE_UNDEF)
1096 return -1;
1097
1098 // A break
Laurence Lundblade9e3651c2018-10-10 11:49:55 +08001099 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_BREAK)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001100 return -1;
1101
1102 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1103 return nCBORError;
1104 if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 0)
1105 return -1;
1106
1107 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1108 return nCBORError;
1109 if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 19)
1110 return -1;
1111
1112 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_INVALID_CBOR)
1113 return -1;
1114
1115 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_INVALID_CBOR)
1116 return -1;
1117
1118 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_INVALID_CBOR)
1119 return -1;
1120
1121 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1122 return nCBORError;
1123 if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 32)
1124 return -1;
1125
1126 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1127 return nCBORError;
1128 if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 255)
1129 return -1;
1130
1131 return 0;
1132
1133}
1134
1135
1136struct FailInput {
1137 UsefulBufC Input;
1138 int nError;
1139};
1140
1141
1142struct FailInput Failures[] = {
1143 { {(uint8_t[]){0x18}, 1}, QCBOR_ERR_HIT_END }, // 1 byte integer missing the byte
1144 { {(uint8_t[]){0x1c}, 1}, QCBOR_ERR_UNSUPPORTED }, // Reserved additional info = 28
1145 { {(uint8_t[]){0x1d}, 1}, QCBOR_ERR_UNSUPPORTED }, // Reserved additional info = 29
1146 { {(uint8_t[]){0x1e}, 1}, QCBOR_ERR_UNSUPPORTED }, // Reserved additional info = 30
1147 { {(uint8_t[]){0x1f}, 1}, QCBOR_ERR_UNSUPPORTED }, // Indefinite length integer
1148 { {(uint8_t[]){0x3c}, 1}, QCBOR_ERR_UNSUPPORTED }, // 1 byte integer missing the byte
1149 { {(uint8_t[]){0x3d}, 1}, QCBOR_ERR_UNSUPPORTED }, // 1 byte integer missing the byte
1150 { {(uint8_t[]){0x3e}, 1}, QCBOR_ERR_UNSUPPORTED }, // 1 byte integer missing the byte
1151 { {(uint8_t[]){0x3f}, 1}, QCBOR_ERR_UNSUPPORTED }, // Indefinite length negative integer
1152 { {(uint8_t[]){0x41}, 1}, QCBOR_ERR_HIT_END }, // Short byte string
1153 { {(uint8_t[]){0x5c}, 1}, QCBOR_ERR_UNSUPPORTED }, // Reserved additional info = 28
1154 { {(uint8_t[]){0x5f}, 1}, QCBOR_ERR_UNSUPPORTED }, // Indefinite length byte string
1155 { {(uint8_t[]){0x61}, 1}, QCBOR_ERR_HIT_END }, // Short UTF-8 string
1156 { {(uint8_t[]){0x7c}, 1}, QCBOR_ERR_UNSUPPORTED }, // Reserved additional info = 28
1157 { {(uint8_t[]){0x7f}, 1}, QCBOR_ERR_UNSUPPORTED }, // Indefinite length UTF-8 string
1158 { {(uint8_t[]){0xff}, 1}, QCBOR_ERR_UNSUPPORTED } , // break
1159 { {(uint8_t[]){0xf8, 0x00}, 2}, QCBOR_ERR_INVALID_CBOR }, // An invalid encoding of a simple type
1160 { {(uint8_t[]){0xf8, 0x1f}, 2}, QCBOR_ERR_INVALID_CBOR }, // An invalid encoding of a simple type
1161 { {(uint8_t[]){0xc0, 0x00}, 2}, QCBOR_ERR_BAD_OPT_TAG }, // Text-based date, with an integer
1162 { {(uint8_t[]){0xc1, 0x41, 0x33}, 3}, QCBOR_ERR_BAD_OPT_TAG }, // Epoch date, with an byte string
1163 { {(uint8_t[]){0xc1, 0xc0, 0x00}, 3}, QCBOR_ERR_BAD_OPT_TAG }, // tagged as both epoch and string dates
1164 { {(uint8_t[]){0xc2, 0x00}, 2}, QCBOR_ERR_BAD_OPT_TAG } // big num tagged an int, not a byte string
1165
1166};
1167
1168
1169void Dump(UsefulBufC Input, int x)
1170{
1171 char label[10];
1172
1173 sprintf(label, "%d", x);
1174
1175 printencoded(label, Input.ptr, Input.len);
1176}
1177
1178
1179int FailureTests()
1180{
1181 int nResult = 0;
1182
1183 struct FailInput *pFEnd = &Failures[0] + sizeof(Failures)/sizeof(struct FailInput);
1184
1185 for(struct FailInput *pF = &Failures[0]; pF < pFEnd ;pF++) {
1186 QCBORDecodeContext DCtx;
1187 QCBORItem Item;
1188 int nCBORError;
1189
1190 QCBORDecode_Init(&DCtx, pF->Input, QCBOR_DECODE_MODE_NORMAL);
1191
1192 while(1) {
1193 nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
1194 if(QCBOR_ERR_HIT_END == nCBORError) {
1195 break;
1196 }
1197 if(nCBORError != pF->nError) {
1198 nResult = 1;
1199 // Dump(pF->Input, nCBORError);
1200 break;
1201 }
1202 }
1203 }
1204
1205 {
1206 QCBORDecodeContext DCtx;
1207 QCBORItem Item;
1208 int nCBORError;
1209
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07001210 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSimpleValues), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001211
1212 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1213 return nCBORError;
1214 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
1215 Item.val.uCount != 10)
1216 return -1;
1217
1218 DCtx.InBuf.magic = 0; // Corrupt the UsefulInputBuf
1219
1220 nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
1221 if(nCBORError != QCBOR_ERR_HIT_END)
1222 return -1;
1223 }
1224
1225
1226 return nResult;
1227}
1228
1229
1230
1231
1232static void Recurser(uint8_t *pBuf, int nLen, int nLenMax)
1233{
1234
1235 if(nLen >= nLenMax) {
1236 return;
1237 }
1238
1239 //printf("__%d__%d__\n", nLen, nLenMax);
1240
1241 for(int i = 0; i < 256; i++) {
1242 pBuf[nLen] = i;
1243
1244 QCBORDecodeContext DCtx;
1245 QCBORItem Item;
1246 int nCBORError;
1247
1248 UsefulBufC Input = {pBuf, nLen+1};
1249
1250 QCBORDecode_Init(&DCtx, Input, QCBOR_DECODE_MODE_NORMAL);
1251
1252 while(1) {
1253 nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
1254 if(QCBOR_ERR_HIT_END == nCBORError) {
1255 break;
1256 }
1257 if(nCBORError != QCBOR_SUCCESS) {
1258 if(nCBORError != QCBOR_ERR_UNSUPPORTED && nCBORError != QCBOR_ERR_HIT_END && nCBORError != QCBOR_ERR_INVALID_CBOR) {
1259 //Dump(Input, nCBORError);
1260 }
1261 break;
1262 }
1263 }
1264 //Dump(Input, -1);
1265
1266
1267 Recurser(pBuf, nLen+1, nLenMax);
1268 }
1269}
1270
1271
1272/*
1273 Runs all possible input strings of a given length. This is set to 3 to make the test
1274 run in reasonable time.
1275 Main point of this test is to not crash.
1276 */
1277
1278int ComprehensiveInputTest()
1279{
1280 uint8_t pBuf[3]; // 3 keeps it running in reasonable time. 4 takes tens of minutes.
1281
1282 Recurser(pBuf, 0, sizeof(pBuf));
1283
1284 return 0;
1285}
1286
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07001287static uint8_t spDateTestInput[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001288 0xc0, // tag for string date
1289 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string
1290
1291 0xc1, // tag for epoch date
1292 0x1a, 0x53, 0x72, 0x4E, 0x00, // Epoch date 1400000000; Tue, 13 May 2014 16:53:20 GMT
1293
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001294 // CBOR_TAG_B64
1295 0xc1, 0xcf, 0xd8, 0x22, // 0xee, // Epoch date with extra tags TODO: fix this test
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001296 0x1a, 0x53, 0x72, 0x4E, 0x01,
1297
1298 0xc1, // tag for epoch date
1299 0x1b, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, // Too large integer
1300
1301 0xc1, // tag for epoch date
1302 0xfa, 0x3f, 0x8c, 0xcc, 0xcd, // double with value 1.1
1303
1304 0xc1, // tag for epoch date
1305 0xfa, 0x7f, 0x7f, 0xff, 0xff // 3.4028234663852886e+38 too large
1306
1307};
1308
1309
1310// have to check float expected only to within an epsilon
1311int CHECK_EXPECTED_DOUBLE(double val, double expected) {
1312
1313 double diff = val - expected;
1314
1315 diff = fabs(diff);
1316
1317 return diff > 0.0000001;
1318}
1319
1320
1321int DateParseTest()
1322{
1323 QCBORDecodeContext DCtx;
1324 QCBORItem Item;
1325 int nCBORError;
1326
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07001327 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spDateTestInput), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001328
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001329 const uint64_t uTags[] = {15};
1330 QCBORTagListIn TagList = {1, uTags};
1331
1332 QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TagList);
1333
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001334 // String date
1335 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1336 return -1;
1337 if(Item.uDataType != QCBOR_TYPE_DATE_STRING ||
Laurence Lundblade9e3651c2018-10-10 11:49:55 +08001338 UsefulBuf_Compare(Item.val.dateString, UsefulBuf_FromSZ("1985-04-12"))){
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001339 return -1;
1340 }
1341
1342 // Epoch date
1343 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1344 return -1;
1345 if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH ||
1346 Item.val.epochDate.nSeconds != 1400000000 ||
1347 Item.val.epochDate.fSecondsFraction != 0 ) {
1348 return -1;
1349 }
1350
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001351 // Epoch date with extra CBOR_TAG_B64 tag that doesn't really mean anything
1352 // but want to be sure extra tag doesn't cause a problem
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001353 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1354 return -1;
1355 if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH ||
1356 Item.val.epochDate.nSeconds != 1400000001 ||
1357 Item.val.epochDate.fSecondsFraction != 0 ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001358 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_B64)) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001359 return -1;
1360 }
1361
1362 // Epoch date that is too large for our representation
1363 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_DATE_OVERFLOW) {
1364 return -1;
1365 }
1366
1367 // Epoch date in float format with fractional seconds
1368 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1369 return -1;
1370 if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH ||
1371 Item.val.epochDate.nSeconds != 1 ||
1372 CHECK_EXPECTED_DOUBLE(Item.val.epochDate.fSecondsFraction, 0.1 )) {
1373 return -1;
1374 }
1375
1376 // Epoch date float that is too large for our representation
1377 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_DATE_OVERFLOW) {
1378 return -1;
1379 }
1380
1381 // TODO: could use a few more tests with float, double, and half precsion and negative (but coverage is still pretty good)
1382
1383 return 0;
1384}
1385
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001386// Really simple basic input for tagging test
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07001387static uint8_t spOptTestInput[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001388 0xd9, 0xd9, 0xf7, // CBOR magic number
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001389 0x81, // Array of one
1390 0xd8, 0x04, // non-preferred serialization of tag 4
1391 0x82, 0x01, 0x03}; // fraction 1/3
1392
1393static uint8_t spEncodedLargeTag[] = {0xdb, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x80};
1394
1395// 0x9192939495969798, 0x88, 0x01, 0x04
1396static uint8_t spLotsOfTags[] = {0xdb, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0xd8, 0x88, 0xc5, 0xc4, 0x80};
1397
1398/*
1399 The cbor.me parse of this.
1400 55799(55799(55799({6(7(-23)): 5859837686836516696(7({7(-20): 11({17(-18): 17(17(17("Organization"))),
1401 9(-17): 773("SSG"), -15: 4(5(6(7(8(9(10(11(12(13(14(15("Confusion")))))))))))), 17(-16): 17("San Diego"),
1402 17(-14): 17("US")}), 23(-19): 19({-11: 9({-9: -7}),
1403 90599561(90599561(90599561(-10))): 12(h'0102030405060708090A')})})),
1404 16(-22): 23({11(8(7(-5))): 8(-3)})})))
1405 */
1406static uint8_t spCSRWithTags[] = {
1407 0xd9, 0xd9, 0xf7, 0xd9, 0xd9, 0xf7, 0xd9, 0xd9, 0xf7, 0xa2,
1408 0xc6, 0xc7, 0x36,
1409 0xdb, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0xc7, 0xa2,
1410 0xda, 0x00, 0x00, 0x00, 0x07, 0x33,
1411 0xcb, 0xa5,
1412 0xd1, 0x31,
1413 0xd1, 0xd1, 0xd1, 0x6c,
1414 0x4f, 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74, 0x69, 0x6f, 0x6e,
1415 0xc9, 0x30,
1416 0xd9, 0x03, 0x05, 0x63,
1417 0x53, 0x53, 0x47,
1418 0x2e,
1419 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf, 0x69,
1420 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73, 0x69, 0x6f, 0x6e,
1421 0xd1, 0x2f,
1422 0xd1, 0x69,
1423 0x53, 0x61, 0x6e, 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f,
1424 0xd1, 0x2d,
1425 0xd1, 0x62,
1426 0x55, 0x53,
1427 0xd7, 0x32,
1428 0xd3, 0xa2,
1429 0x2a,
1430 0xc9, 0xa1,
1431 0x28,
1432 0x26,
1433 0xda, 0x05, 0x66, 0x70, 0x89, 0xda, 0x05, 0x66, 0x70, 0x89, 0xda, 0x05, 0x66, 0x70, 0x89, 0x29,
1434 0xcc, 0x4a,
1435 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,0x07, 0x08, 0x09, 0x0a,
1436 0xd0, 0x35,
1437 0xd7, 0xa1,
1438 0xcb, 0xc8, 0xc7, 0x24,
1439 0xc8, 0x22};
1440
1441static int CheckCSRMaps(QCBORDecodeContext *pDC);
1442
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001443
1444int OptTagParseTest()
1445{
1446 QCBORDecodeContext DCtx;
1447 QCBORItem Item;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001448
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07001449 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spOptTestInput), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001450
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001451 //-------------------------
1452 // This text matches the magic number tag and the fraction tag
1453 if(QCBORDecode_GetNext(&DCtx, &Item)) {
1454 return -2;
1455 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001456 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001457 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC)) {
1458 return -3;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001459 }
1460
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001461 if(QCBORDecode_GetNext(&DCtx, &Item)) {
1462 return -4;
1463 }
1464 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
1465 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_FRACTION) ||
1466 Item.val.uCount != 2) {
1467 return -5;
1468 }
1469
1470 // --------------------------------
1471 // This test decodes the very large tag, but it is not in
1472 // any list so it is ignored.
1473 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEncodedLargeTag), QCBOR_DECODE_MODE_NORMAL);
1474 if(QCBORDecode_GetNext(&DCtx, &Item)) {
1475 return -6;
1476 }
1477 if(Item.uTagBits) {
1478 return -7;
1479 }
1480
1481 // ----------------------------------
1482 // This test sets up a caller-config list that includes the very large tage and then matches it.
1483 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEncodedLargeTag), QCBOR_DECODE_MODE_NORMAL);
1484 const uint64_t puList[] = {0x9192939495969798, 257};
1485 const QCBORTagListIn TL = {2, puList};
1486 QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TL);
1487
1488 if(QCBORDecode_GetNext(&DCtx, &Item)) {
1489 return -8;
1490 }
1491 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
1492 !QCBORDecode_IsTagged(&DCtx, &Item, 0x9192939495969798) ||
1493 QCBORDecode_IsTagged(&DCtx, &Item, 257) ||
1494 QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_BIGFLOAT) ||
1495 Item.val.uCount != 0) {
1496 return -9;
1497 }
1498
1499 //------------------------
1500 // This test sets up a caller-configured list, and looks up something not in it
1501 const uint64_t puLongList[17] = {1,2,1};
1502 const QCBORTagListIn TLLong = {17, puLongList};
1503 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEncodedLargeTag), QCBOR_DECODE_MODE_NORMAL);
1504 QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TLLong);
1505 if(QCBORDecode_GetNext(&DCtx, &Item)) {
1506 return -11;
1507 }
1508
1509 // -----------------------
1510 // This tests retrievel of the full tag list
1511 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spLotsOfTags), QCBOR_DECODE_MODE_NORMAL);
1512 uint64_t puTags[16];
1513 QCBORTagListOut Out = {0, 4, puTags};
1514 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
1515 return -12;
1516 }
1517 if(puTags[0] != 0x9192939495969798 ||
1518 puTags[1] != 0x88 ||
1519 puTags[2] != 0x05 ||
1520 puTags[3] != 0x04) {
1521 return -13;
1522 }
1523
1524 // ----------------------
1525 // This text if too small of an out list
1526 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spLotsOfTags), QCBOR_DECODE_MODE_NORMAL);
1527 QCBORTagListOut OutSmall = {0, 3, puTags};
1528 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &OutSmall) != QCBOR_ERR_TOO_MANY_TAGS) {
1529 return -14;
1530 }
1531
1532 // ---------------
1533 // Parse a version of the "CSR" that has had a ton of tags randomly inserted
1534 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRWithTags), QCBOR_DECODE_MODE_NORMAL);
1535 int n = CheckCSRMaps(&DCtx);
1536 if(n) {
1537 return n-2000;
1538 }
1539
1540 Out = (QCBORTagListOut){0,16, puTags};
1541 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRWithTags), QCBOR_DECODE_MODE_NORMAL);
1542
1543 const uint64_t puTagList[] = {773, 1, 90599561};
1544 const QCBORTagListIn TagList = {3, puTagList};
1545 QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TagList);
1546
1547
1548 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
1549 return -100;
1550 }
1551 if(Item.uDataType != QCBOR_TYPE_MAP ||
1552 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC) ||
1553 QCBORDecode_IsTagged(&DCtx, &Item, 90599561) ||
1554 QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_DATE_EPOCH) ||
1555 Item.val.uCount != 2 ||
1556 puTags[0] != CBOR_TAG_CBOR_MAGIC ||
1557 puTags[1] != CBOR_TAG_CBOR_MAGIC ||
1558 puTags[2] != CBOR_TAG_CBOR_MAGIC ||
1559 Out.uNumUsed != 3) {
1560 return -101;
1561 }
1562
1563 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
1564 return -102;
1565 }
1566 if(Item.uDataType != QCBOR_TYPE_MAP ||
1567 QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC) ||
1568 QCBORDecode_IsTagged(&DCtx, &Item, 6) ||
1569 QCBORDecode_IsTagged(&DCtx, &Item, 7) || // item is tagged 7, but 7 is not configured to be recognized
1570 Item.val.uCount != 2 ||
1571 puTags[0] != 5859837686836516696 ||
1572 puTags[1] != 7 ||
1573 Out.uNumUsed != 2) {
1574 return -103;
1575 }
1576
1577 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
1578 return -104;
1579 }
1580 if(Item.uDataType != QCBOR_TYPE_MAP ||
1581 Item.uTagBits ||
1582 Item.val.uCount != 5 ||
1583 puTags[0] != 0x0b ||
1584 Out.uNumUsed != 1) {
1585 return -105;
1586 }
1587
1588 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
1589 return -106;
1590 }
1591 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1592 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_COSE_MAC0) ||
1593 Item.val.string.len != 12 ||
1594 puTags[0] != CBOR_TAG_COSE_MAC0 ||
1595 puTags[1] != CBOR_TAG_COSE_MAC0 ||
1596 puTags[2] != CBOR_TAG_COSE_MAC0 ||
1597 Out.uNumUsed != 3) {
1598 return -105;
1599 }
1600
1601 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
1602 return -107;
1603 }
1604 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1605 !QCBORDecode_IsTagged(&DCtx, &Item, 773) ||
1606 Item.val.string.len != 3 ||
1607 puTags[0] != 773 ||
1608 Out.uNumUsed != 1) {
1609 return -108;
1610 }
1611
1612 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
1613 return -109;
1614 }
1615 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1616 !QCBORDecode_IsTagged(&DCtx, &Item, 4) ||
1617 Item.val.string.len != 9 ||
1618 puTags[0] != 4 ||
1619 puTags[11] != 0x0f ||
1620 Out.uNumUsed != 12) {
1621 return -110;
1622 }
1623
1624 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
1625 return -111;
1626 }
1627 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1628 !QCBORDecode_IsTagged(&DCtx, &Item, 17) ||
1629 Item.val.string.len != 9 ||
1630 puTags[0] != 17 ||
1631 Out.uNumUsed != 1) {
1632 return -112;
1633 }
1634
1635 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
1636 return -111;
1637 }
1638 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1639 !QCBORDecode_IsTagged(&DCtx, &Item, 17) ||
1640 Item.val.string.len != 2 ||
1641 puTags[0] != 17 ||
1642 Out.uNumUsed != 1) {
1643 return -112;
1644 }
1645
1646 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
1647 return -113;
1648 }
1649 if(Item.uDataType != QCBOR_TYPE_MAP ||
1650 QCBORDecode_IsTagged(&DCtx, &Item, 19) ||
1651 Item.val.uCount != 2 ||
1652 puTags[0] != 19 ||
1653 Out.uNumUsed != 1) {
1654 return -114;
1655 }
1656
1657 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
1658 return -115;
1659 }
1660 if(Item.uDataType != QCBOR_TYPE_MAP ||
1661 QCBORDecode_IsTagged(&DCtx, &Item, 9) ||
1662 Item.uTagBits ||
1663 Item.val.uCount != 1 ||
1664 puTags[0] != 9 ||
1665 Out.uNumUsed != 1) {
1666 return -116;
1667 }
1668
1669 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
1670 return -116;
1671 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001672 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001673 Item.val.int64 != -7 ||
1674 Item.uTagBits ||
1675 Out.uNumUsed != 0) {
1676 return -117;
1677 }
1678
1679 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
1680 return -118;
1681 }
1682 if(Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
1683 Item.val.string.len != 10 ||
1684 Item.uTagBits ||
1685 puTags[0] != 12 ||
1686 Out.uNumUsed != 1) {
1687 return -119;
1688 }
1689
1690 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
1691 return -120;
1692 }
1693 if(Item.uDataType != QCBOR_TYPE_MAP ||
1694 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_ENC_AS_B16) ||
1695 Item.val.uCount != 1 ||
1696 puTags[0] != 0x17 ||
1697 Out.uNumUsed != 1) {
1698 return -121;
1699 }
1700
1701 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
1702 return -122;
1703 }
1704 if(Item.uDataType != QCBOR_TYPE_INT64 ||
1705 QCBORDecode_IsTagged(&DCtx, &Item, 8) ||
1706 Item.val.int64 != -3 ||
1707 puTags[0] != 8 ||
1708 Out.uNumUsed != 1) {
1709 return -123;
1710 }
1711
1712 if(QCBORDecode_Finish(&DCtx)) {
1713 return -124;
1714 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001715
1716 return 0;
1717}
1718
1719
1720
1721
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07001722static uint8_t spBigNumInput[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001723 0x83,
1724 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1725 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1726 0xA4,
1727 0x63, 0x42, 0x4E, 0x2B,
1728 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1729 0x18, 0x40,
1730 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1731 0x63, 0x42, 0x4E, 0x2D,
1732 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1733 0x38, 0x3F,
1734 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
1735
1736
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07001737static uint8_t spBigNum[] = {0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001738
1739
1740int BignumParseTest()
1741{
1742 QCBORDecodeContext DCtx;
1743 QCBORItem Item;
1744 int nCBORError;
1745
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07001746 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNumInput), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001747
1748
1749 //
1750 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1751 return -1;
1752 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
1753 return -1;
1754 }
1755
1756 //
1757 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1758 return -1;
1759 if(Item.uDataType != QCBOR_TYPE_POSBIGNUM ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07001760 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001761 return -1;
1762 }
1763
1764 //
1765 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1766 return -1;
1767 if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07001768 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001769 return -1;
1770 }
1771
1772 //
1773 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1774 return -1;
1775 if(Item.uDataType != QCBOR_TYPE_MAP) {
1776 return -1;
1777 }
1778
1779 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1780 return -1;
1781 if(Item.uDataType != QCBOR_TYPE_POSBIGNUM ||
1782 Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07001783 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001784 return -1;
1785 }
1786
1787 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1788 return -1;
1789 if(Item.uDataType != QCBOR_TYPE_POSBIGNUM ||
1790 Item.uLabelType != QCBOR_TYPE_INT64 ||
1791 Item.label.int64 != 64 ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07001792 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001793 return -1;
1794 }
1795
1796 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1797 return -1;
1798 if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM ||
1799 Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07001800 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001801 return -1;
1802 }
1803
1804 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1805 return -1;
1806 if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM ||
1807 Item.uLabelType != QCBOR_TYPE_INT64 ||
1808 Item.label.int64 != -64 ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07001809 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001810 return -1;
1811 }
1812
1813 return 0;
1814}
1815
1816
1817
Laurence Lundbladea44d5062018-10-17 18:45:12 +05301818static 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 +08001819{
1820 QCBORItem Item;
1821 int nCBORError;
1822
1823 if((nCBORError = QCBORDecode_GetNext(pCtx, &Item))) return -1;
1824 if(Item.uDataType != uDataType) return -1;
1825 if(uNestingLevel > 0) {
1826 if(Item.uLabelType != QCBOR_TYPE_INT64 && Item.uLabelType != QCBOR_TYPE_UINT64) return -1;
1827 if(Item.uLabelType == QCBOR_TYPE_INT64) {
1828 if(Item.label.int64 != nLabel) return -1;
1829 } else {
Laurence Lundblade570fab52018-10-13 18:28:27 +08001830 if(Item.label.uint64 != (uint64_t)nLabel) return -1;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001831 }
1832 }
1833 if(Item.uNestingLevel != uNestingLevel) return -1;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05301834 if(Item.uNextNestLevel != uNextNest) return -1;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001835
1836 if(pItem) {
1837 *pItem = Item;
1838 }
1839 return 0;
1840}
1841
1842
Laurence Lundblade742df4a2018-10-13 20:07:17 +08001843// Same code checks definite and indefinite length versions of the map
1844static int CheckCSRMaps(QCBORDecodeContext *pDC)
1845{
Laurence Lundbladea44d5062018-10-17 18:45:12 +05301846 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 0, 1, 0, NULL)) return -1;
Laurence Lundblade742df4a2018-10-13 20:07:17 +08001847
Laurence Lundbladea44d5062018-10-17 18:45:12 +05301848 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 1, 2, -23, NULL)) return -1;
Laurence Lundblade742df4a2018-10-13 20:07:17 +08001849
Laurence Lundbladea44d5062018-10-17 18:45:12 +05301850 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 2, 3, -20, NULL)) return -1;
Laurence Lundblade742df4a2018-10-13 20:07:17 +08001851
Laurence Lundbladea44d5062018-10-17 18:45:12 +05301852 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -18, NULL)) return -1;
1853 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -17, NULL)) return -1;
1854 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -15, NULL)) return -1;
1855 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -16, NULL)) return -1;
1856 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 2, -14, NULL)) return -1;
Laurence Lundblade742df4a2018-10-13 20:07:17 +08001857
Laurence Lundbladea44d5062018-10-17 18:45:12 +05301858 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 2, 3, -19, NULL)) return -1;
1859 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 3, 4, -11, NULL)) return -1;
Laurence Lundblade742df4a2018-10-13 20:07:17 +08001860
Laurence Lundbladea44d5062018-10-17 18:45:12 +05301861 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_INT64, 4, 3, -9, NULL)) return -1;
1862 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_BYTE_STRING, 3, 1, -10, NULL)) return -1;
Laurence Lundblade742df4a2018-10-13 20:07:17 +08001863
Laurence Lundbladea44d5062018-10-17 18:45:12 +05301864 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 1, 2, -22, NULL)) return -1;
1865 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_INT64, 2, 0, -5, NULL)) return -1;
Laurence Lundblade742df4a2018-10-13 20:07:17 +08001866
1867 if(QCBORDecode_Finish(pDC)) return -2;
1868
1869 return 0;
1870}
1871
1872
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001873/*
1874// cbor.me decoded output
1875{
1876 -23: {
1877 -20: {
1878 -18: "Organization",
1879 -17: "SSG",
1880 -15: "Confusion",
1881 -16: "San Diego",
1882 -14: "US"
1883 },
1884 -19: {
1885 -11: {
1886 -9: -7
1887 },
1888 -10: '\u0001\u0002\u0003\u0004\u0005\u0006\a\b\t\n'
1889 }
1890 },
1891 -22: {
1892 -5: -3
1893 }
1894}
1895 */
1896
Laurence Lundblade742df4a2018-10-13 20:07:17 +08001897
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07001898static uint8_t spCSRInput[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001899 0xa2, 0x36, 0xa2, 0x33, 0xa5, 0x31, 0x6c, 0x4f,
1900 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74,
1901 0x69, 0x6f, 0x6e, 0x30, 0x63, 0x53, 0x53, 0x47,
1902 0x2e, 0x69, 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73,
1903 0x69, 0x6f, 0x6e, 0x2f, 0x69, 0x53, 0x61, 0x6e,
1904 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f, 0x2d, 0x62,
1905 0x55, 0x53, 0x32, 0xa2, 0x2a, 0xa1, 0x28, 0x26,
1906 0x29, 0x4a, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
1907 0x07, 0x08, 0x09, 0x0a, 0x35, 0xa1, 0x24, 0x22};
1908
1909int NestedMapTest()
1910{
1911 QCBORDecodeContext DCtx;
1912
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07001913 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001914
Laurence Lundblade742df4a2018-10-13 20:07:17 +08001915 return CheckCSRMaps(&DCtx);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001916}
1917
Laurence Lundblade742df4a2018-10-13 20:07:17 +08001918// Same map as above, but using indefinite lengths
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07001919static uint8_t spCSRInputIndefLen[] = {
Laurence Lundblade742df4a2018-10-13 20:07:17 +08001920 0xbf, 0x36, 0xbf, 0x33, 0xbf, 0x31, 0x6c, 0x4f,
1921 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74,
1922 0x69, 0x6f, 0x6e, 0x30, 0x63, 0x53, 0x53, 0x47,
1923 0x2e, 0x69, 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73,
1924 0x69, 0x6f, 0x6e, 0x2f, 0x69, 0x53, 0x61, 0x6e,
1925 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f, 0x2d, 0x62,
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07001926 0x55, 0x53, 0xff, 0x32, 0xbf, 0x2a, 0xbf, 0x28,
1927 0x26, 0xff, 0x29, 0x4a, 0x01, 0x02, 0x03, 0x04,
1928 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0xff, 0xff,
1929 0x35, 0xbf, 0x24, 0x22, 0xff, 0xff};
Laurence Lundblade742df4a2018-10-13 20:07:17 +08001930
1931int NestedMapTestIndefLen()
1932{
1933 QCBORDecodeContext DCtx;
1934
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07001935 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInputIndefLen), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade742df4a2018-10-13 20:07:17 +08001936
1937 return CheckCSRMaps(&DCtx);
1938}
1939
1940
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001941
Laurence Lundblade17ede402018-10-13 11:43:07 +08001942static UsefulBufC make_nested_indefinite_arrays(int n, UsefulBuf Storage)
1943{
1944 UsefulOutBuf UOB;
1945 UsefulOutBuf_Init(&UOB, Storage);
1946
1947 int i;
1948 for(i = 0; i < n; i++) {
1949 UsefulOutBuf_AppendByte(&UOB, 0x9f);
1950 }
1951
1952 for(i = 0; i < n; i++) {
1953 UsefulOutBuf_AppendByte(&UOB, 0xff);
1954 }
1955 return UsefulOutBuf_OutUBuf(&UOB);
1956}
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08001957
1958
Laurence Lundblade17ede402018-10-13 11:43:07 +08001959static int parse_indeflen_nested(UsefulBufC Nested, int nNestLevel)
1960{
1961 QCBORDecodeContext DC;
1962 QCBORDecode_Init(&DC, Nested, 0);
1963
1964 int j;
1965 for(j = 0; j < nNestLevel; j++) {
1966 QCBORItem Item;
1967 int nReturn = QCBORDecode_GetNext(&DC, &Item);
1968 if(j >= QCBOR_MAX_ARRAY_NESTING) {
1969 // Should be in error
1970 if(nReturn != QCBOR_ERR_ARRAY_NESTING_TOO_DEEP) {
1971 return -4;
1972 } else {
1973 return 0; // Decoding doesn't recover after an error
1974 }
1975 } else {
1976 // Should be no error
1977 if(nReturn) {
1978 return -9; // Should not have got an error
1979 }
1980 }
1981 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
1982 return -7;
1983 }
1984 }
1985 int nReturn = QCBORDecode_Finish(&DC);
1986 if(nReturn) {
1987 return -3;
1988 }
1989 return 0;
1990}
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08001991
1992
Laurence Lundbladea44d5062018-10-17 18:45:12 +05301993int IndefiniteLengthNestTest()
Laurence Lundblade17ede402018-10-13 11:43:07 +08001994{
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05301995 UsefulBuf_MAKE_STACK_UB(Storage, 50);
Laurence Lundblade17ede402018-10-13 11:43:07 +08001996 int i;
Laurence Lundblade6de37062018-10-15 12:22:42 +05301997 for(i=1; i < QCBOR_MAX_ARRAY_NESTING+4; i++) {
Laurence Lundblade17ede402018-10-13 11:43:07 +08001998 UsefulBufC Nested = make_nested_indefinite_arrays(i, Storage);
1999 int nReturn = parse_indeflen_nested(Nested, i);
2000 if(nReturn) {
2001 return nReturn;
2002 }
2003 }
2004 return 0;
2005}
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002006
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002007
Laurence Lundblade6de37062018-10-15 12:22:42 +05302008
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002009static const uint8_t spIndefiniteArray[] = {0x9f, 0x01, 0x82, 0x02, 0x03, 0xff}; // [1, [2, 3]]
2010static const uint8_t spIndefiniteArrayBad1[] = {0x9f}; // No closing break
2011static const uint8_t spIndefiniteArrayBad2[] = {0x9f, 0x9f, 0x02, 0xff}; // Not enough closing breaks
2012static const uint8_t spIndefiniteArrayBad3[] = {0x9f, 0x02, 0xff, 0xff}; // Too many closing breaks
2013static const uint8_t spIndefiniteArrayBad4[] = {0x81, 0x9f}; // Unclosed indeflen inside def len
2014static const uint8_t spIndefiniteArrayBad5[] = {0x9f, 0xd1, 0xff}; // confused tag
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002015
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302016int IndefiniteLengthArrayMapTest()
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002017{
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002018 int nResult;
2019 // --- first test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002020 UsefulBufC IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArray);
Laurence Lundblade17ede402018-10-13 11:43:07 +08002021
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002022 // Decode it and see if it is OK
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05302023 UsefulBuf_MAKE_STACK_UB(MemPool, 150);
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002024 QCBORDecodeContext DC;
2025 QCBORItem Item;
2026 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
2027
2028 QCBORDecode_SetMemPool(&DC, MemPool, false);
Laurence Lundblade6de37062018-10-15 12:22:42 +05302029
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002030 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05302031
2032 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
2033 Item.uNestingLevel != 0 ||
2034 Item.uNextNestLevel != 1) {
2035 return -111;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002036 }
2037
2038 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05302039 if(Item.uDataType != QCBOR_TYPE_INT64 ||
2040 Item.uNestingLevel != 1 ||
2041 Item.uNextNestLevel != 1) {
2042 return -2;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002043 }
2044
2045 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05302046 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
2047 Item.uNestingLevel != 1 ||
2048 Item.uNextNestLevel != 2) {
2049 return -3;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002050 }
2051
2052 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05302053 if(Item.uDataType != QCBOR_TYPE_INT64 |
2054 Item.uNestingLevel != 2 ||
2055 Item.uNextNestLevel != 2) {
2056 return -4;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002057 }
2058
2059 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05302060 if(Item.uDataType != QCBOR_TYPE_INT64 |
2061 Item.uNestingLevel != 2 ||
2062 Item.uNextNestLevel != 0) {
2063 return -5;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002064 }
2065
2066 if(QCBORDecode_Finish(&DC)) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05302067 return -6;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002068 }
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002069
2070 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002071 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad1);
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002072
2073 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
2074
2075 QCBORDecode_SetMemPool(&DC, MemPool, false);
2076
2077 nResult = QCBORDecode_GetNext(&DC, &Item);
2078 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05302079 return -7;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002080 }
2081
Laurence Lundblade570fab52018-10-13 18:28:27 +08002082 nResult = QCBORDecode_Finish(&DC);
Laurence Lundblade6de37062018-10-15 12:22:42 +05302083 if(nResult != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
2084 return -8;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002085 }
2086
2087
2088 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002089 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad2);
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002090
2091 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
2092
2093 QCBORDecode_SetMemPool(&DC, MemPool, false);
2094
2095 nResult = QCBORDecode_GetNext(&DC, &Item);
2096 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05302097 return -9;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002098 }
2099
2100 nResult = QCBORDecode_GetNext(&DC, &Item);
2101 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05302102 return -10;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002103 }
2104
2105 nResult = QCBORDecode_GetNext(&DC, &Item);
2106 if(nResult || Item.uDataType != QCBOR_TYPE_INT64) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05302107 return -11;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002108 }
2109
2110 nResult = QCBORDecode_Finish(&DC);
Laurence Lundblade6de37062018-10-15 12:22:42 +05302111 if(nResult != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
2112 return -12;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002113 }
2114
2115
2116 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002117 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad3);
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002118
2119 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
2120
2121 QCBORDecode_SetMemPool(&DC, MemPool, false);
2122
2123 nResult = QCBORDecode_GetNext(&DC, &Item);
2124 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05302125 return -13;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002126 }
2127
2128 nResult = QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05302129 if(nResult != QCBOR_ERR_BAD_BREAK) {
2130 return -14;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002131 }
Laurence Lundblade6de37062018-10-15 12:22:42 +05302132
Laurence Lundblade570fab52018-10-13 18:28:27 +08002133
2134 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002135 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad4);
Laurence Lundblade570fab52018-10-13 18:28:27 +08002136
2137 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
2138
2139 QCBORDecode_SetMemPool(&DC, MemPool, false);
2140
2141 nResult = QCBORDecode_GetNext(&DC, &Item);
2142 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05302143 return -15;
Laurence Lundblade570fab52018-10-13 18:28:27 +08002144 }
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302145
Laurence Lundblade570fab52018-10-13 18:28:27 +08002146 nResult = QCBORDecode_GetNext(&DC, &Item);
2147 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05302148 return -16;
Laurence Lundblade570fab52018-10-13 18:28:27 +08002149 }
2150
2151 nResult = QCBORDecode_Finish(&DC);
Laurence Lundblade6de37062018-10-15 12:22:42 +05302152 if(nResult != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
2153 return -17;
Laurence Lundblade570fab52018-10-13 18:28:27 +08002154 }
2155
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302156 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002157 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad5);
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302158
2159 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
2160
2161 QCBORDecode_SetMemPool(&DC, MemPool, false);
2162
2163 nResult = QCBORDecode_GetNext(&DC, &Item);
2164 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05302165 return -18;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302166 }
2167
2168 nResult = QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05302169 if(nResult != QCBOR_ERR_BAD_BREAK) {
2170 return -19;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302171 }
2172
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002173 return 0;
2174}
2175
Laurence Lundblade17ede402018-10-13 11:43:07 +08002176
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002177static const uint8_t spIndefiniteLenString[] = {
Laurence Lundblade17ede402018-10-13 11:43:07 +08002178 0x81, // Array of length one
2179 0x7f, // text string marked with indefinite length
2180 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment
2181 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment
2182 0xff // ending break
2183};
2184
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002185static const uint8_t spIndefiniteLenStringBad2[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302186 0x81, // Array of length one
2187 0x7f, // text string marked with indefinite length
2188 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment
2189 0x44, 0x6d, 0x69, 0x6e, 0x67, // second segment of wrong type
2190 0xff // ending break
2191};
2192
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002193static const uint8_t spIndefiniteLenStringBad3[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302194 0x81, // Array of length one
2195 0x7f, // text string marked with indefinite length
2196 0x01, 0x02, // Not a string
2197 0xff // ending break
2198};
2199
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002200static const uint8_t spIndefiniteLenStringBad4[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302201 0x81, // Array of length one
2202 0x7f, // text string marked with indefinite length
2203 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment
2204 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment
2205 // missing end of string
2206};
2207
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002208static const uint8_t spIndefiniteLenStringLabel[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302209 0xa1, // Array of length one
2210 0x7f, // text string marked with indefinite length
2211 0x65, 0x73, 0x74, 0x72, 0x75, 0x75, // first segment
2212 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment
2213 0xff, // ending break
2214 0x01 // integer being labeled.
2215};
2216
2217static UsefulBufC MakeIndefiniteBigBstr(UsefulBuf Storage)
2218{
2219 UsefulOutBuf UOB;
2220
2221 UsefulOutBuf_Init(&UOB, Storage);
2222 UsefulOutBuf_AppendByte(&UOB, 0x81);
2223 UsefulOutBuf_AppendByte(&UOB, 0x5f);
2224
2225 int i = 0;
2226 for(int nChunkSize = 1; nChunkSize <= 128; nChunkSize *= 2) {
2227 UsefulOutBuf_AppendByte(&UOB, 0x58);
2228 UsefulOutBuf_AppendByte(&UOB, (uint8_t)nChunkSize);
2229 for(int j = 0; j < nChunkSize; j++ ) {
2230 UsefulOutBuf_AppendByte(&UOB, i);
2231 i++;
2232 }
2233 }
2234 UsefulOutBuf_AppendByte(&UOB, 0xff);
2235
2236 return UsefulOutBuf_OutUBuf(&UOB);
2237}
2238
2239static int CheckBigString(UsefulBufC BigString)
2240{
2241 if(BigString.len != 255) {
2242 return 1;
2243 }
2244
2245 for(uint8_t i = 0; i < 255; i++){
2246 if(((const uint8_t *)BigString.ptr)[i] != i) {
2247 return 1;
2248 }
2249 }
2250 return 0;
2251}
2252
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05302253
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302254int IndefiniteLengthStringTest()
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302255{
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302256 QCBORDecodeContext DC;
2257 QCBORItem Item;
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05302258 // big enough for MakeIndefiniteBigBstr() + MemPool overhead
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05302259 UsefulBuf_MAKE_STACK_UB(MemPool, 320);
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302260
2261 // --- Simple normal indefinite length string ------
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002262 UsefulBufC IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenString);
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302263 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002264
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05302265 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302266 return -1;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302267 }
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002268
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302269 if(QCBORDecode_GetNext(&DC, &Item)) {
2270 return -2;
2271 }
2272 if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.uDataAlloc) {
2273 return -3;
2274 }
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002275
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302276 if(QCBORDecode_GetNext(&DC, &Item)) {
2277 return -4;
2278 }
2279 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || !Item.uDataAlloc) {
2280 return -5;
2281 }
2282 if(QCBORDecode_Finish(&DC)) {
2283 return -6;
2284 }
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302285
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302286 // ----- types mismatch ---
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002287 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad2), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302288
2289 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
2290 return -7;
2291 }
2292
2293 if(QCBORDecode_GetNext(&DC, &Item)) {
2294 return -8;
2295 }
2296 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
2297 return -9;
2298 }
2299
2300 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_INDEFINITE_STRING_SEG) {
2301 return -10;
2302 }
2303
2304 // ----- not a string ---
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002305 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad3), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302306
2307 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
2308 return -11;
2309 }
2310
2311 if(QCBORDecode_GetNext(&DC, &Item)) {
2312 return -12;
2313 }
2314 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
2315 return -13;
2316 }
2317
2318 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_INDEFINITE_STRING_SEG) {
2319 return -14;
2320 }
2321
2322 // ----- no end -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002323 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad4), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302324
2325 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
2326 return -15;
2327 }
2328
2329 if(QCBORDecode_GetNext(&DC, &Item)) {
2330 return -16;
2331 }
2332 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
2333 return -17;
2334 }
2335
2336 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_HIT_END) {
2337 return -18;
2338 }
2339
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302340 // ------ Don't set a string allocator and see an error -----
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302341 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
2342
2343 QCBORDecode_GetNext(&DC, &Item);
2344 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302345 return -19;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302346 }
2347
2348 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_NO_STRING_ALLOCATOR) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302349 return -20;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302350 }
2351
2352 // ----- Mempool is way too small -----
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05302353 UsefulBuf_MAKE_STACK_UB(MemPoolTooSmall, 20); // 20 is too small no matter what
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302354
2355 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
2356 if(!QCBORDecode_SetMemPool(&DC, MemPoolTooSmall, false)) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302357 return -21;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302358 }
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302359
2360 // ----- Mempool is way too small -----
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05302361 UsefulBuf_MAKE_STACK_UB(BigIndefBStrStorage, 290);
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302362 UsefulBufC BigIndefBStr = MakeIndefiniteBigBstr(BigIndefBStrStorage);
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302363
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05302364 UsefulBuf_MAKE_STACK_UB(MemPoolSmall, 80); // 80 is big enough for MemPool overhead, but not BigIndefBStr
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302365
2366 QCBORDecode_Init(&DC, BigIndefBStr, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302367 if(QCBORDecode_SetMemPool(&DC, MemPoolSmall, false)) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302368 return -22;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302369 }
2370
2371 QCBORDecode_GetNext(&DC, &Item);
2372 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302373 return -23;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302374 }
2375 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_STRING_ALLOC) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302376 return -24;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302377 }
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302378
2379 // ---- big bstr -----
2380 QCBORDecode_Init(&DC, BigIndefBStr, QCBOR_DECODE_MODE_NORMAL);
2381
2382 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
2383 return -25;
2384 }
2385
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05302386 if(QCBORDecode_GetNext(&DC, &Item)) {
2387 return -26;
2388 }
2389 if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.uDataAlloc) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302390 return -26;
2391 }
2392
2393 if(QCBORDecode_GetNext(&DC, &Item)) {
2394 return -27;
2395 }
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05302396 if(Item.uDataType != QCBOR_TYPE_BYTE_STRING || !Item.uDataAlloc || Item.uNestingLevel != 1) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302397 return -28;
2398 }
2399 if(CheckBigString(Item.val.string)) {
2400 return -3;
2401 }
2402 if(QCBORDecode_Finish(&DC)) {
2403 return -29;
2404 }
2405
2406 // --- label is an indefinite length string ------
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002407 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringLabel), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302408
2409 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
2410 return -30;
2411 }
2412
2413 QCBORDecode_GetNext(&DC, &Item);
2414 if(Item.uDataType != QCBOR_TYPE_MAP) {
2415 return -31;
2416 }
2417
2418 if(QCBORDecode_GetNext(&DC, &Item)){
2419 return -32;
2420 }
2421 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || Item.uDataType != QCBOR_TYPE_INT64 ||
2422 Item.uDataAlloc || !Item.uLabelAlloc ||
2423 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("struuming"))) {
2424 return -33;
2425 }
2426
2427 if(QCBORDecode_Finish(&DC)) {
2428 return -34;
2429 }
2430
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002431 return 0;
2432}
2433
2434
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302435int AllocAllStringsTest()
2436{
2437 QCBORDecodeContext DC;
2438
2439 // First test, use the "CSRMap" as easy input and checking
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002440 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302441
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05302442 UsefulBuf_MAKE_STACK_UB(Pool, 300);
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302443
2444 QCBORDecode_SetMemPool(&DC, Pool, 1); // Turn on copying.
2445
2446 if(CheckCSRMaps(&DC)) {
2447 return -1;
2448 }
2449
2450 // Next parse, save pointers to a few strings, destroy original and see all is OK.
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05302451 UsefulBuf_MAKE_STACK_UB(CopyOfStorage, 160);
2452 UsefulBufC CopyOf = UsefulBuf_Copy(CopyOfStorage, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded));
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002453
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302454 QCBORDecode_Init(&DC, CopyOf, QCBOR_DECODE_MODE_NORMAL);
2455 QCBORDecode_SetMemPool(&DC, Pool, 1); // Turn on copying.
2456
2457 int nCBORError;
2458 QCBORItem Item1, Item2, Item3, Item4;
2459 if((nCBORError = QCBORDecode_GetNext(&DC, &Item1)))
2460 return nCBORError;
2461 if(Item1.uDataType != QCBOR_TYPE_MAP ||
2462 Item1.val.uCount != 3)
2463 return -1;
2464 if((nCBORError = QCBORDecode_GetNext(&DC, &Item1)))
2465 return nCBORError;
2466 if((nCBORError = QCBORDecode_GetNext(&DC, &Item2)))
2467 return nCBORError;
2468 if((nCBORError = QCBORDecode_GetNext(&DC, &Item3)))
2469 return nCBORError;
2470 if((nCBORError = QCBORDecode_GetNext(&DC, &Item4)))
2471 return nCBORError;
2472
Laurence Lundblade05ec57b2018-10-21 01:50:03 +05302473 UsefulBuf_Set(CopyOfStorage, '_');
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302474
2475 if(Item1.uLabelType != QCBOR_TYPE_TEXT_STRING ||
2476 Item1.label.string.len != 13 ||
2477 Item1.uDataType != QCBOR_TYPE_INT64 ||
2478 Item1.val.int64 != 42 ||
2479 memcmp(Item1.label.string.ptr, "first integer", 13))
2480 return -1;
2481
2482
2483 if(Item2.uLabelType != QCBOR_TYPE_TEXT_STRING ||
2484 Item2.label.string.len != 23 ||
2485 memcmp(Item2.label.string.ptr, "an array of two strings", 23) ||
2486 Item2.uDataType != QCBOR_TYPE_ARRAY ||
2487 Item2.val.uCount != 2)
2488 return -1;
2489
2490 if(Item3.uDataType != QCBOR_TYPE_TEXT_STRING ||
2491 Item3.val.string.len != 7 ||
2492 memcmp(Item3.val.string.ptr, "string1", 7))
2493 return -1;
2494
2495 if(Item4.uDataType != QCBOR_TYPE_TEXT_STRING ||
2496 Item4.val.string.len != 7 ||
2497 memcmp(Item4.val.string.ptr, "string2", 7))
2498 return -1;
2499
2500 // Next parse with a pool that is too small
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05302501 UsefulBuf_MAKE_STACK_UB(SmallPool, 80);
2502 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302503 QCBORDecode_SetMemPool(&DC, SmallPool, 1); // Turn on copying.
2504 if((nCBORError = QCBORDecode_GetNext(&DC, &Item1)))
2505 return nCBORError;
2506 if(Item1.uDataType != QCBOR_TYPE_MAP ||
2507 Item1.val.uCount != 3)
2508 return -1;
2509 if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item1))){
2510 if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item2))) {
2511 if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item3))) {
2512 nCBORError = QCBORDecode_GetNext(&DC, &Item4);
2513 }
2514 }
2515 }
2516 if(nCBORError != QCBOR_ERR_STRING_ALLOC) {
2517 return -5;
2518 }
2519
2520 return 0;
2521}
2522
2523
2524int MemPoolTest(void)
Laurence Lundblade0155b622018-10-12 20:04:37 +08002525{
2526 QCBORDecodeContext DC;
2527
2528 const uint8_t pMinimalCBOR[] = {0xa0}; // One empty map
2529
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05302530 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pMinimalCBOR),0);
Laurence Lundblade0155b622018-10-12 20:04:37 +08002531
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05302532 UsefulBuf_MAKE_STACK_UB(Pool, 100);
Laurence Lundblade0155b622018-10-12 20:04:37 +08002533
2534 QCBORDecode_SetMemPool(&DC, Pool, 0);
2535
2536 // Cheat a little to get to the string allocator object
2537 // so we can call it directly to test it
2538 QCBORStringAllocator *pAlloc = (QCBORStringAllocator *)DC.pStringAllocator;
2539
2540 // Ask for too much in one go
2541 // 90 < 100, but there is some overhead taken out of the 100
2542 UsefulBuf Allocated = (*pAlloc->fAllocate)(pAlloc->pAllocaterContext, NULL, 90);
2543 if(!UsefulBuf_IsNULL(Allocated)) {
2544 return -1;
2545 }
2546
2547
2548
2549 QCBORDecode_SetMemPool(&DC, Pool, 0);
2550
2551 // Cheat a little to get to the string allocator object
2552 // so we can call it directly to test it
2553 pAlloc = (QCBORStringAllocator *)DC.pStringAllocator;
2554
2555 Allocated = (*pAlloc->fAllocate)(pAlloc->pAllocaterContext, NULL, 30);
2556 if(UsefulBuf_IsNULL(Allocated)) { // expected to succeed
2557 return -1;
2558 }
2559 UsefulBuf Allocated2 = (*pAlloc->fAllocate)(pAlloc->pAllocaterContext, NULL, 30);
2560 if(!UsefulBuf_IsNULL(Allocated2)) { // expected to fail
2561 return -1;
2562 }
2563 (*pAlloc->fFree)(pAlloc->pAllocaterContext, Allocated.ptr);
2564 Allocated = (*pAlloc->fAllocate)(pAlloc->pAllocaterContext, NULL, 30);
2565 if(UsefulBuf_IsNULL(Allocated)) { // succeed because of the free
2566 return -1;
2567 }
2568
2569
2570 QCBORDecode_SetMemPool(&DC, Pool, 0);
2571
2572 // Cheat a little to get to the string allocator object
2573 // so we can call it directly to test it
2574 pAlloc = (QCBORStringAllocator *)DC.pStringAllocator;
2575 Allocated = (*pAlloc->fAllocate)(pAlloc->pAllocaterContext, NULL, 20);
2576 if(UsefulBuf_IsNULL(Allocated)) { // expected to succeed
2577 return -1;
2578 }
2579 Allocated2 = (*pAlloc->fAllocate)(pAlloc->pAllocaterContext, Allocated.ptr, 25);
2580 if(UsefulBuf_IsNULL(Allocated2)) { // expected to fail
2581 return -1;
2582 }
2583 if(Allocated2.ptr != Allocated.ptr || Allocated2.len != 25) {
2584 return -1;
2585 }
2586
Laurence Lundblade0155b622018-10-12 20:04:37 +08002587 return 0;
2588}
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002589
2590
2591
Laurence Lundblade9e3651c2018-10-10 11:49:55 +08002592
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002593
2594