blob: 6e8f282a34926c28348e27fafe7706f4c5451987 [file] [log] [blame]
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001/*==============================================================================
Laurence Lundbladed92a6162018-11-01 11:38:35 +07002 Copyright (c) 2016-2018, The Linux Foundation.
3 Copyright (c) 2018, Laurence Lundblade.
4 All rights reserved.
Laurence Lundblade9e3651c2018-10-10 11:49:55 +08005
Laurence Lundblade0dbc9172018-11-01 14:17:21 +07006Redistribution and use in source and binary forms, with or without
7modification, are permitted provided that the following conditions are
8met:
9 * Redistributions of source code must retain the above copyright
10 notice, this list of conditions and the following disclaimer.
11 * Redistributions in binary form must reproduce the above
12 copyright notice, this list of conditions and the following
13 disclaimer in the documentation and/or other materials provided
14 with the distribution.
15 * Neither the name of The Linux Foundation nor the names of its
16 contributors, nor the name "Laurence Lundblade" may be used to
17 endorse or promote products derived from this software without
18 specific prior written permission.
Laurence Lundblade9e3651c2018-10-10 11:49:55 +080019
Laurence Lundblade0dbc9172018-11-01 14:17:21 +070020THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
21WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
22MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
23ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
24BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
27BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
29OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
30IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Laurence Lundblade9e3651c2018-10-10 11:49:55 +080031 ==============================================================================*/
32
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080033#include "qcbor.h"
34#include "qcbor_decode_tests.h"
Laurence Lundbladed4728fd2018-12-17 15:15:56 -080035#include <string.h>
Laurence Lundblade9e3651c2018-10-10 11:49:55 +080036#include <math.h> // for fabs()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080037#include <stdlib.h>
38
Laurence Lundblade20db9c92018-12-17 11:40:37 -080039#ifdef PRINT_FUNCTIONS_FOR_DEBUGGINGXX
40#include <stdio.h>
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080041static void printencoded(const char *szLabel, const uint8_t *pEncoded, size_t nLen)
42{
43 if(szLabel) {
44 printf("%s ", szLabel);
45 }
46
Laurence Lundblade570fab52018-10-13 18:28:27 +080047 size_t i;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080048 for(i = 0; i < nLen; i++) {
49 uint8_t Z = pEncoded[i];
50 printf("%02x ", Z);
51 }
52 printf("\n");
53
54 fflush(stdout);
55}
Laurence Lundblade20db9c92018-12-17 11:40:37 -080056#endif
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080057
58
Laurence Lundbladeb836efb2018-10-28 20:09:58 +070059static const uint8_t spExpectedEncodedInts[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080060 0x98, 0x2f, 0x3b, 0x7f, 0xff, 0xff, 0xff, 0xff,
61 0xff, 0xff, 0xff, 0x3b, 0x00, 0x00, 0x00, 0x01,
62 0x00, 0x00, 0x00, 0x00, 0x3a, 0xff, 0xff, 0xff,
63 0xff, 0x3a, 0xff, 0xff, 0xff, 0xfe, 0x3a, 0xff,
64 0xff, 0xff, 0xfd, 0x3a, 0x7f, 0xff, 0xff, 0xff,
65 0x3a, 0x7f, 0xff, 0xff, 0xfe, 0x3a, 0x00, 0x01,
66 0x00, 0x01, 0x3a, 0x00, 0x01, 0x00, 0x00, 0x39,
67 0xff, 0xff, 0x39, 0xff, 0xfe, 0x39, 0xff, 0xfd,
68 0x39, 0x01, 0x00, 0x38, 0xff, 0x38, 0xfe, 0x38,
69 0xfd, 0x38, 0x18, 0x37, 0x36, 0x20, 0x00, 0x00,
70 0x01, 0x16, 0x17, 0x18, 0x18, 0x18, 0x19, 0x18,
71 0x1a, 0x18, 0xfe, 0x18, 0xff, 0x19, 0x01, 0x00,
72 0x19, 0x01, 0x01, 0x19, 0xff, 0xfe, 0x19, 0xff,
73 0xff, 0x1a, 0x00, 0x01, 0x00, 0x00, 0x1a, 0x00,
74 0x01, 0x00, 0x01, 0x1a, 0x00, 0x01, 0x00, 0x02,
75 0x1a, 0x7f, 0xff, 0xff, 0xff, 0x1a, 0x7f, 0xff,
76 0xff, 0xff, 0x1a, 0x80, 0x00, 0x00, 0x00, 0x1a,
77 0x80, 0x00, 0x00, 0x01, 0x1a, 0xff, 0xff, 0xff,
78 0xfe, 0x1a, 0xff, 0xff, 0xff, 0xff, 0x1b, 0x00,
79 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x1b,
80 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01,
81 0x1b, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
82 0xff, 0x1b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
83 0xff, 0xff};
84
85
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080086// return CBOR error or -1 if type of value doesn't match
87
88static int IntegerValuesParseTestInternal(QCBORDecodeContext *pDCtx)
89{
90 QCBORItem Item;
91 int nCBORError;
92
93 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
94 return nCBORError;
95 if(Item.uDataType != QCBOR_TYPE_ARRAY)
96 return -1;
97
98 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
99 return nCBORError;
100 if(Item.uDataType != QCBOR_TYPE_INT64 || // Todo; fix this for 32-bit machines
Laurence Lundblade570fab52018-10-13 18:28:27 +0800101 Item.val.int64 != -9223372036854775807LL - 1)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800102 return -1;
103
104 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
105 return nCBORError;
106 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade570fab52018-10-13 18:28:27 +0800107 Item.val.int64 != -4294967297)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800108 return -1;
109
110 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
111 return nCBORError;
112 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade570fab52018-10-13 18:28:27 +0800113 Item.val.int64 != -4294967296)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800114 return -1;
115
116 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
117 return nCBORError;
118 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade570fab52018-10-13 18:28:27 +0800119 Item.val.int64 != -4294967295)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800120 return -1;
121
122 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
123 return nCBORError;
124 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade570fab52018-10-13 18:28:27 +0800125 Item.val.int64 != -4294967294)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800126 return -1;
127
128
129 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
130 return nCBORError;
131 if(Item.uDataType != QCBOR_TYPE_INT64 ||
132 Item.val.int64 != -2147483648)
133 return -1;
134
135 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
136 return nCBORError;
137 if(Item.uDataType != QCBOR_TYPE_INT64 ||
138 Item.val.int64 != -2147483647)
139 return -1;
140
141 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
142 return nCBORError;
143 if(Item.uDataType != QCBOR_TYPE_INT64 ||
144 Item.val.int64 != -65538)
145 return -1;
146
147 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
148 return nCBORError;
149 if(Item.uDataType != QCBOR_TYPE_INT64 ||
150 Item.val.int64 != -65537)
151 return -1;
152
153 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
154 return nCBORError;
155 if(Item.uDataType != QCBOR_TYPE_INT64 ||
156 Item.val.int64 != -65536)
157 return -1;
158
159
160 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
161 return nCBORError;
162 if(Item.uDataType != QCBOR_TYPE_INT64 ||
163 Item.val.int64 != -65535)
164 return -1;
165
166
167 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
168 return nCBORError;
169 if(Item.uDataType != QCBOR_TYPE_INT64 ||
170 Item.val.int64 != -65534)
171 return -1;
172
173
174 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
175 return nCBORError;
176 if(Item.uDataType != QCBOR_TYPE_INT64 ||
177 Item.val.int64 != -257)
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 != -256)
184 return -1;
185
186 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
187 return nCBORError;
188 if(Item.uDataType != QCBOR_TYPE_INT64 ||
189 Item.val.int64 != -255)
190 return -1;
191
192 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
193 return nCBORError;
194 if(Item.uDataType != QCBOR_TYPE_INT64 ||
195 Item.val.int64 != -254)
196 return -1;
197
198
199 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
200 return nCBORError;
201 if(Item.uDataType != QCBOR_TYPE_INT64 ||
202 Item.val.int64 != -25)
203 return -1;
204
205
206 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
207 return nCBORError;
208 if(Item.uDataType != QCBOR_TYPE_INT64 ||
209 Item.val.int64 != -24)
210 return -1;
211
212
213 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
214 return nCBORError;
215 if(Item.uDataType != QCBOR_TYPE_INT64 ||
216 Item.val.int64 != -23)
217 return -1;
218
219
220 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
221 return nCBORError;
222 if(Item.uDataType != QCBOR_TYPE_INT64 ||
223 Item.val.int64 != -1)
224 return -1;
225
226
227 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
228 return nCBORError;
229 if(Item.uDataType != QCBOR_TYPE_INT64 ||
230 Item.val.int64 != 0)
231 return -1;
232
233
234 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
235 return nCBORError;
236 if(Item.uDataType != QCBOR_TYPE_INT64 ||
237 Item.val.int64 != 0)
238 return -1;
239
240 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
241 return nCBORError;
242 if(Item.uDataType != QCBOR_TYPE_INT64 ||
243 Item.val.int64 != 1)
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 != 22)
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 != 23)
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 != 24)
265 return -1;
266
267
268 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
269 return nCBORError;
270 if(Item.uDataType != QCBOR_TYPE_INT64 ||
271 Item.val.int64 != 25)
272 return -1;
273
274 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
275 return nCBORError;
276 if(Item.uDataType != QCBOR_TYPE_INT64 ||
277 Item.val.int64 != 26)
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 != 254)
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 != 255)
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 != 256)
299 return -1;
300
301
302 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
303 return nCBORError;
304 if(Item.uDataType != QCBOR_TYPE_INT64 ||
305 Item.val.int64 != 257)
306 return -1;
307
308 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
309 return nCBORError;
310 if(Item.uDataType != QCBOR_TYPE_INT64 ||
311 Item.val.int64 != 65534)
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 != 65535)
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 != 65536)
326 return -1;
327
328 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
329 return nCBORError;
330 if(Item.uDataType != QCBOR_TYPE_INT64 ||
331 Item.val.int64 != 65537)
332 return -1;
333
334 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
335 return nCBORError;
336 if(Item.uDataType != QCBOR_TYPE_INT64 ||
337 Item.val.int64 != 65538)
338 return -1;
339
340 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
341 return nCBORError;
342 if(Item.uDataType != QCBOR_TYPE_INT64 ||
343 Item.val.int64 != 2147483647)
344 return -1;
345
346 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
347 return nCBORError;
348 if(Item.uDataType != QCBOR_TYPE_INT64 ||
349 Item.val.int64 != 2147483647)
350 return -1;
351
352 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
353 return nCBORError;
354 if(Item.uDataType != QCBOR_TYPE_INT64 ||
355 Item.val.int64 != 2147483648)
356 return -1;
357
358 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
359 return nCBORError;
360 if(Item.uDataType != QCBOR_TYPE_INT64 ||
361 Item.val.int64 != 2147483649)
362 return -1;
363
364 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
365 return nCBORError;
366 if(Item.uDataType != QCBOR_TYPE_INT64 ||
367 Item.val.int64 != 4294967294)
368 return -1;
369
370
371 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
372 return nCBORError;
373 if(Item.uDataType != QCBOR_TYPE_INT64 ||
374 Item.val.int64 != 4294967295)
375 return -1;
376
377
378 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
379 return nCBORError;
380 if(Item.uDataType != QCBOR_TYPE_INT64 ||
381 Item.val.int64 != 4294967296)
382 return -1;
383
384
385 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
386 return nCBORError;
387 if(Item.uDataType != QCBOR_TYPE_INT64 ||
388 Item.val.int64 != 4294967297)
389 return -1;
390
391
392
393 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
394 return nCBORError;
395 if(Item.uDataType != QCBOR_TYPE_INT64 ||
396 Item.val.int64 != 9223372036854775807LL)
397 return -1;
398
399
400 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
401 return nCBORError;
402 if(Item.uDataType != QCBOR_TYPE_UINT64 ||
403 Item.val.uint64 != 18446744073709551615ULL)
404 return -1;
405
406
407 if(QCBORDecode_Finish(pDCtx) != QCBOR_SUCCESS) {
408 return -1;
409 }
410
411 return 0;
412}
413
414
415/*
416 Tests the decoding of lots of different integers sizes
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +0800417 and values.
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800418 */
419
420int IntegerValuesParseTest()
421{
422 int n;
423 QCBORDecodeContext DCtx;
424
Laurence Lundbladeb836efb2018-10-28 20:09:58 +0700425 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedEncodedInts), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800426
427 n = IntegerValuesParseTestInternal(&DCtx);
428
429 return(n);
430}
431
432
433/*
434 Creates a simple CBOR array and returns it in *pEncoded. The array is malloced
435 and needs to be freed. This is used by several tests.
436
437 Two of the inputs can be set. Two other items in the array are fixed.
438
439 */
440
441static int CreateSimpleArray(int nInt1, int nInt2, uint8_t **pEncoded, size_t *pEncodedLen)
442{
443 QCBOREncodeContext ECtx;
444 int nReturn = -1;
445
446 *pEncoded = NULL;
447 *pEncodedLen = INT32_MAX;
448
449 // loop runs CBOR encoding twice. First with no buffer to
450 // calucate the length so buffer can be allocated correctly,
451 // and last with the buffer to do the actual encoding
452 do {
Laurence Lundblade0595e932018-11-02 22:22:47 +0700453 QCBOREncode_Init(&ECtx, (UsefulBuf){*pEncoded, *pEncodedLen});
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800454 QCBOREncode_OpenArray(&ECtx);
455 QCBOREncode_AddInt64(&ECtx, nInt1);
456 QCBOREncode_AddInt64(&ECtx, nInt2);
457 QCBOREncode_AddBytes(&ECtx, ((UsefulBufC) {"galactic", 8}));
458 QCBOREncode_AddBytes(&ECtx, ((UsefulBufC) {"haven token", 11}));
459 QCBOREncode_CloseArray(&ECtx);
460
Laurence Lundblade0595e932018-11-02 22:22:47 +0700461 UsefulBufC Encoded;
462 if(QCBOREncode_Finish(&ECtx, &Encoded))
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800463 goto Done;
464
465 if(*pEncoded != NULL) {
Laurence Lundblade0595e932018-11-02 22:22:47 +0700466 *pEncodedLen = Encoded.len;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800467 nReturn = 0;
468 goto Done;
469 }
Laurence Lundblade0595e932018-11-02 22:22:47 +0700470 *pEncoded = malloc(Encoded.len);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800471 if(*pEncoded == NULL) {
472 nReturn = -1;
473 goto Done;
474 }
475
476 } while(1);
477Done:
478 return (nReturn);
479
480}
481
482
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800483/*
484 {"first integer": 42,
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900485 "an array of two strings": [
486 "string1", "string2"
487 ],
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800488 "map in a map": {
489 "bytes 1": h'78787878',
490 "bytes 2": h'79797979',
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900491 "another int": 98,
492 "text 2": "lies, damn lies and statistics"
493 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800494 }
495 */
496
497static uint8_t pValidMapEncoded[] = {
Laurence Lundbladeb836efb2018-10-28 20:09:58 +0700498 0xa3, 0x6d, 0x66, 0x69, 0x72, 0x73, 0x74, 0x20, 0x69, 0x6e,
499 0x74, 0x65, 0x67, 0x65, 0x72, 0x18, 0x2a, 0x77, 0x61, 0x6e,
500 0x20, 0x61, 0x72, 0x72, 0x61, 0x79, 0x20, 0x6f, 0x66, 0x20,
501 0x74, 0x77, 0x6f, 0x20, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67,
502 0x73, 0x82, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x31,
503 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x32, 0x6c, 0x6d,
504 0x61, 0x70, 0x20, 0x69, 0x6e, 0x20, 0x61, 0x20, 0x6d, 0x61,
505 0x70, 0xa4, 0x67, 0x62, 0x79, 0x74, 0x65, 0x73, 0x20, 0x31,
506 0x44, 0x78, 0x78, 0x78, 0x78, 0x67, 0x62, 0x79, 0x74, 0x65,
507 0x73, 0x20, 0x32, 0x44, 0x79, 0x79, 0x79, 0x79, 0x6b, 0x61,
508 0x6e, 0x6f, 0x74, 0x68, 0x65, 0x72, 0x20, 0x69, 0x6e, 0x74,
509 0x18, 0x62, 0x66, 0x74, 0x65, 0x78, 0x74, 0x20, 0x32, 0x78,
510 0x1e, 0x6c, 0x69, 0x65, 0x73, 0x2c, 0x20, 0x64, 0x61, 0x6d,
511 0x6e, 0x20, 0x6c, 0x69, 0x65, 0x73, 0x20, 0x61, 0x6e, 0x64,
512 0x20, 0x73, 0x74, 0x61, 0x74, 0x69, 0x73, 0x74, 0x69, 0x63,
513 0x73 } ;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800514
515static 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)
516{
517 QCBORDecodeContext DCtx;
518 QCBORItem Item;
519 int nReturn = -1; // assume error until success
520
521 QCBORDecode_Init(&DCtx, (UsefulBufC){pEncoded, nLen}, QCBOR_DECODE_MODE_NORMAL);
522
523 // Make sure the first thing is a map
524 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || Item.uDataType != QCBOR_TYPE_ARRAY)
525 goto Done;
526
527 // First integer
Laurence Lundblade12b495d2018-12-17 11:15:54 -0800528 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || Item.uDataType != QCBOR_TYPE_INT64)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800529 goto Done;
530 *pInt1 = Item.val.int64;
531
532 // Second integer
533 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || Item.uDataType != QCBOR_TYPE_INT64)
534 goto Done;
535 *pInt2 = Item.val.int64;
536
537 // First string
538 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || Item.uDataType != QCBOR_TYPE_BYTE_STRING)
539 goto Done;
540 *pBuf3 = Item.val.string.ptr;
541 *pBuf3Len = Item.val.string.len;
542
543 // Second string
544 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || Item.uDataType != QCBOR_TYPE_BYTE_STRING)
545 goto Done;
546 *pBuf4 = Item.val.string.ptr;
547 *pBuf4Len = Item.val.string.len;
548
549 nReturn = 0;
550
551Done:
552 return(nReturn);
553}
554
555
556
Laurence Lundblade9e3651c2018-10-10 11:49:55 +0800557
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800558int SimpleArrayTest()
559{
560 uint8_t *pEncoded;
561 size_t nEncodedLen;
562
563 int64_t i1, i2;
564 size_t i3, i4;
565 const uint8_t *s3, *s4;
566
567
568 if(CreateSimpleArray(23, 6000, &pEncoded, &nEncodedLen) < 0) {
569 return(-1);
570 }
571
572 ParseOrderedArray(pEncoded, nEncodedLen, &i1, &i2, &s3, &i3, &s4, &i4);
573
574 if(i1 != 23 ||
575 i2 != 6000 ||
576 i3 != 8 ||
577 i4 != 11 ||
Laurence Lundbladea44d5062018-10-17 18:45:12 +0530578 memcmp("galactic", s3, 8) !=0 ||
579 memcmp("haven token", s4, 11) !=0) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800580 return(-1);
581 }
582
583 return(0);
584}
585
586
Laurence Lundblade9e3651c2018-10-10 11:49:55 +0800587
Laurence Lundbladeb836efb2018-10-28 20:09:58 +0700588static uint8_t spDeepArrays[] = {0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x80};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800589
590int ParseDeepArrayTest()
591{
592 QCBORDecodeContext DCtx;
593 int nReturn = 0;
594 int i;
595
Laurence Lundbladeb836efb2018-10-28 20:09:58 +0700596 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spDeepArrays), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800597
598 for(i = 0; i < 10; i++) {
599 QCBORItem Item;
600
601 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
602 Item.uDataType != QCBOR_TYPE_ARRAY ||
603 Item.uNestingLevel != i) {
604 nReturn = -1;
605 break;
606 }
607 }
608
609 return(nReturn);
610}
611
Laurence Lundblade972e59c2018-11-11 15:57:23 +0700612// Big enough to test nesting to the depth of 24
613static uint8_t spTooDeepArrays[] = {0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81,
614 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81,
615 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81,
616 0x80};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800617
618int ParseTooDeepArrayTest()
619{
620 QCBORDecodeContext DCtx;
621 int nReturn = 0;
622 int i;
623 QCBORItem Item;
624
625
Laurence Lundbladeb836efb2018-10-28 20:09:58 +0700626 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTooDeepArrays), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800627
Laurence Lundblade972e59c2018-11-11 15:57:23 +0700628 for(i = 0; i < QCBOR_MAX_ARRAY_NESTING1; i++) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800629
630 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
631 Item.uDataType != QCBOR_TYPE_ARRAY ||
632 Item.uNestingLevel != i) {
633 nReturn = -1;
634 break;
635 }
636 }
637
638 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_ARRAY_NESTING_TOO_DEEP)
639 nReturn = -1;
640
641 return(nReturn);
642}
643
644
645
646
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800647int ShortBufferParseTest()
648{
649 int nResult = 0;
650 QCBORDecodeContext DCtx;
651 int num;
652
Laurence Lundbladeb836efb2018-10-28 20:09:58 +0700653 for(num = sizeof(spExpectedEncodedInts)-1; num; num--) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800654 int n;
655
Laurence Lundbladeb836efb2018-10-28 20:09:58 +0700656 QCBORDecode_Init(&DCtx, (UsefulBufC){spExpectedEncodedInts, num}, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800657
658 n = IntegerValuesParseTestInternal(&DCtx);
659
660 //printf("Len %d, result: %d\n", num, n);
661
662 if(n != QCBOR_ERR_HIT_END) {
663 nResult = -1;
664 goto Done;
665 }
666 }
667Done:
668 return nResult;
669}
670
671
Laurence Lundblade9e3651c2018-10-10 11:49:55 +0800672
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800673int ShortBufferParseTest2()
674{
675 uint8_t *pEncoded;
676 int nReturn;
677 size_t nEncodedLen;
678
679 int64_t i1, i2;
680 size_t i3, i4;
681 const uint8_t *s3, *s4;
682
683 nReturn = 0;
684
685 if(CreateSimpleArray(23, 6000, &pEncoded, &nEncodedLen) < 0) {
686 return(-1);
687 }
688
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800689 for(nEncodedLen--; nEncodedLen; nEncodedLen--) {
690 int nResult = ParseOrderedArray(pEncoded, (uint32_t)nEncodedLen, &i1, &i2, &s3, &i3, &s4, &i4);
691 if(nResult == 0) {
692 nReturn = -1;
693 }
694 }
695
696 return(nReturn);
697}
698
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530699/*
700 Decode and thoroughly check a moderately complex
701 set of maps
702 */
Laurence Lundbladeea567ac2018-12-09 14:03:21 -0800703static int ParseMapTest1(QCBORDecodeMode nMode)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800704{
705 QCBORDecodeContext DCtx;
706 QCBORItem Item;
707 int nCBORError;
708
Laurence Lundbladeea567ac2018-12-09 14:03:21 -0800709 QCBORDecode_Init(&DCtx, (UsefulBufC){pValidMapEncoded, sizeof(pValidMapEncoded)}, nMode);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800710
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900711 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800712 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900713 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800714 if(Item.uDataType != QCBOR_TYPE_MAP ||
715 Item.val.uCount != 3)
716 return -1;
717
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900718 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800719 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900720 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800721 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800722 Item.uDataType != QCBOR_TYPE_INT64 ||
723 Item.val.int64 != 42 ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530724 Item.uDataAlloc ||
725 Item.uLabelAlloc ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900726 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("first integer"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800727 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900728 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800729
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900730 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800731 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900732 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800733 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530734 Item.uDataAlloc ||
735 Item.uLabelAlloc ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900736 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("an array of two strings")) ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800737 Item.uDataType != QCBOR_TYPE_ARRAY ||
738 Item.val.uCount != 2)
739 return -1;
740
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900741 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800742 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900743 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800744 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530745 Item.uDataAlloc ||
746 Item.uLabelAlloc ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900747 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("string1"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800748 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900749 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800750
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900751 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800752 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900753 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800754 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530755 Item.uDataAlloc ||
756 Item.uLabelAlloc ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900757 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("string2"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800758 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900759 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800760
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900761 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800762 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900763 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800764 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530765 Item.uDataAlloc ||
766 Item.uLabelAlloc ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900767 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("map in a map")) ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800768 Item.uDataType != QCBOR_TYPE_MAP ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900769 Item.val.uCount != 4) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800770 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900771 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800772
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900773 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800774 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900775 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800776 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900777 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("bytes 1"))||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800778 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530779 Item.uDataAlloc ||
780 Item.uLabelAlloc ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900781 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("xxxx"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800782 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900783 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800784
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900785 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800786 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900787 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800788 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900789 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("bytes 2")) ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800790 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530791 Item.uDataAlloc ||
792 Item.uLabelAlloc ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900793 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("yyyy"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800794 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900795 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800796
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900797 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800798 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900799 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800800 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530801 Item.uDataAlloc ||
802 Item.uLabelAlloc ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900803 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("another int")) ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800804 Item.uDataType != QCBOR_TYPE_INT64 ||
805 Item.val.int64 != 98)
806 return -1;
807
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900808 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800809 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900810 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800811 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900812 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("text 2"))||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800813 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530814 Item.uDataAlloc ||
815 Item.uLabelAlloc ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900816 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("lies, damn lies and statistics"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800817 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900818 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800819
820 return 0;
821}
822
823
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900824/*
825 Decode and thoroughly check a moderately complex
826 set of maps
827 */
828int ParseMapAsArrayTest()
829{
830 QCBORDecodeContext DCtx;
831 QCBORItem Item;
832 int nCBORError;
833
834 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), QCBOR_DECODE_MODE_MAP_AS_ARRAY);
835
836 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
837 return nCBORError;
838 }
Laurence Lundbladed61cbf32018-12-09 11:42:21 -0800839 if(Item.uDataType != QCBOR_TYPE_MAP_AS_ARRAY ||
840 Item.val.uCount != 6) {
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900841 return -1;
842 }
843
844 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
845 return nCBORError;
846 }
847 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
848 Item.uDataAlloc ||
849 Item.uLabelAlloc ||
850 Item.uLabelType != QCBOR_TYPE_NONE ||
851 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("first integer"))) {
852 return -2;
853 }
854
855 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
856 return nCBORError;
857 }
858 if(Item.uLabelType != QCBOR_TYPE_NONE ||
859 Item.uDataType != QCBOR_TYPE_INT64 ||
860 Item.val.int64 != 42 ||
861 Item.uDataAlloc ||
862 Item.uLabelAlloc) {
863 return -3;
864 }
865
866 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
867 return nCBORError;
868 }
869 if(Item.uLabelType != QCBOR_TYPE_NONE ||
870 Item.uDataAlloc ||
871 Item.uLabelAlloc ||
872 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("an array of two strings")) ||
873 Item.uDataType != QCBOR_TYPE_TEXT_STRING) {
874 return -4;
875 }
876
877 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
878 return nCBORError;
879 }
880 if(Item.uLabelType != QCBOR_TYPE_NONE ||
881 Item.uDataAlloc ||
882 Item.uLabelAlloc ||
883 Item.uDataType != QCBOR_TYPE_ARRAY ||
884 Item.val.uCount != 2) {
885 return -5;
886 }
887
888 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
889 return nCBORError;
890 }
891 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
892 Item.val.string.len != 7 ||
893 Item.uDataAlloc ||
894 Item.uLabelAlloc ||
895 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("string1"))) {
896 return -6;
897 }
898
899 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
900 return nCBORError;
901 }
902 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
903 Item.uDataAlloc ||
904 Item.uLabelAlloc ||
905 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("string2"))) {
906 return -7;
907 }
908
909
910 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
911 return nCBORError;
912 }
913 if(Item.uLabelType != QCBOR_TYPE_NONE ||
914 Item.uDataAlloc ||
915 Item.uLabelAlloc ||
916 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("map in a map"))) {
917 return -8;
918 }
919
920 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
921 return nCBORError;
922 }
923 if(Item.uLabelType != QCBOR_TYPE_NONE ||
924 Item.uDataAlloc ||
925 Item.uLabelAlloc ||
Laurence Lundbladed61cbf32018-12-09 11:42:21 -0800926 Item.uDataType != QCBOR_TYPE_MAP_AS_ARRAY ||
927 Item.val.uCount != 8) {
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900928 return -9;
929 }
930
931 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
932 return nCBORError;
933 }
934 if(Item.uLabelType != QCBOR_TYPE_NONE ||
935 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("bytes 1"))||
936 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
937 Item.uDataAlloc ||
938 Item.uLabelAlloc) {
939 return -10;
940 }
941
942 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
943 return nCBORError;
944 }
945 if(Item.uLabelType != QCBOR_TYPE_NONE ||
946 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
947 Item.uDataAlloc ||
948 Item.uLabelAlloc ||
949 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("xxxx"))) {
950 return -11;
951 }
952
953 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
954 return nCBORError;
955 }
956 if(Item.uLabelType != QCBOR_TYPE_NONE ||
957 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("bytes 2")) ||
958 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
959 Item.uDataAlloc ||
960 Item.uLabelAlloc) {
961 return -12;
962 }
963
964 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
965 return nCBORError;
966 }
967 if(Item.uLabelType != QCBOR_TYPE_NONE ||
968 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
969 Item.uDataAlloc ||
970 Item.uLabelAlloc ||
971 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("yyyy"))) {
972 return -13;
973 }
974
975 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
976 return nCBORError;
977 }
978 if(Item.uLabelType != QCBOR_TYPE_NONE ||
979 Item.uDataAlloc ||
980 Item.uLabelAlloc ||
981 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("another int")) ||
982 Item.uDataType != QCBOR_TYPE_TEXT_STRING) {
983 return -14;
984 }
985
986 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
987 return nCBORError;
988 }
989 if(Item.uLabelType != QCBOR_TYPE_NONE ||
990 Item.uDataAlloc ||
991 Item.uLabelAlloc ||
992 Item.uDataType != QCBOR_TYPE_INT64 ||
993 Item.val.int64 != 98) {
994 return -15;
995 }
996
997 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
998 return nCBORError;
999 }
1000 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1001 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("text 2"))||
1002 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1003 Item.uDataAlloc ||
1004 Item.uLabelAlloc) {
1005 return -16;
1006 }
1007
1008 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
1009 return nCBORError;
1010 }
1011 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1012 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1013 Item.uDataAlloc ||
1014 Item.uLabelAlloc ||
1015 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("lies, damn lies and statistics"))) {
1016 return -17;
1017 }
1018
1019 return 0;
1020}
1021
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001022
1023/*
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301024 Fully or partially decode pValidMapEncoded. When
1025 partially decoding check for the right error code.
1026 How much partial decoding depends on nLevel.
1027
1028 The partial decodes test error conditions of
1029 incomplete encoded input.
1030
1031 This could be combined with the above test
1032 and made prettier and maybe a little more
1033 thorough.
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001034 */
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001035static int ExtraBytesTest(int nLevel)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001036{
1037 QCBORDecodeContext DCtx;
1038 QCBORItem Item;
1039 int nCBORError;
1040
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001041 QCBORDecode_Init(&DCtx, (UsefulBufC){pValidMapEncoded, sizeof(pValidMapEncoded)}, QCBOR_DECODE_MODE_NORMAL);
1042
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001043 if(nLevel < 1) {
1044 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_EXTRA_BYTES) {
1045 return -1;
1046 } else {
1047 return 0;
1048 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001049 }
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301050
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001051
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001052 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001053 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001054 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001055 if(Item.uDataType != QCBOR_TYPE_MAP ||
1056 Item.val.uCount != 3)
1057 return -1;
1058
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001059 if(nLevel < 2) {
1060 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
1061 return -1;
1062 } else {
1063 return 0;
1064 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001065 }
1066
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001067
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001068 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001069 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001070 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001071 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001072 Item.uDataType != QCBOR_TYPE_INT64 ||
1073 Item.val.uCount != 42 ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001074 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("first integer"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001075 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001076 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001077
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001078 if(nLevel < 3) {
1079 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
1080 return -1;
1081 } else {
1082 return 0;
1083 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001084 }
1085
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001086 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001087 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001088 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001089 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001090 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("an array of two strings")) ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001091 Item.uDataType != QCBOR_TYPE_ARRAY ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001092 Item.val.uCount != 2) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001093 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001094 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001095
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001096
1097 if(nLevel < 4) {
1098 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
1099 return -1;
1100 } else {
1101 return 0;
1102 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001103 }
1104
1105
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001106 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001107 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001108 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001109 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001110 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("string1"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001111 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001112 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001113
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001114 if(nLevel < 5) {
1115 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
1116 return -1;
1117 } else {
1118 return 0;
1119 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001120 }
1121
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001122 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001123 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001124 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001125 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001126 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("string2"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001127 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001128 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001129
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001130 if(nLevel < 6) {
1131 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
1132 return -1;
1133 } else {
1134 return 0;
1135 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001136 }
1137
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001138 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001139 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001140 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001141 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001142 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("map in a map")) ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001143 Item.uDataType != QCBOR_TYPE_MAP ||
1144 Item.val.uCount != 4)
1145 return -1;
1146
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001147 if(nLevel < 7) {
1148 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
1149 return -1;
1150 } else {
1151 return 0;
1152 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001153 }
1154
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001155 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001156 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001157 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001158 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001159 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("bytes 1")) ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001160 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001161 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("xxxx"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001162 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001163 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001164
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001165 if(nLevel < 8) {
1166 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
1167 return -1;
1168 } else {
1169 return 0;
1170 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001171 }
1172
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001173 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001174 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001175 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001176 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001177 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("bytes 2")) ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001178 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001179 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("yyyy"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001180 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001181 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001182
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001183 if(nLevel < 9) {
1184 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
1185 return -1;
1186 } else {
1187 return 0;
1188 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001189 }
1190
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001191 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001192 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001193 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001194 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001195 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("another int")) ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001196 Item.uDataType != QCBOR_TYPE_INT64 ||
1197 Item.val.int64 != 98)
1198 return -1;
1199
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001200 if(nLevel < 10) {
1201 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
1202 return -1;
1203 } else {
1204 return 0;
1205 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001206 }
1207
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001208 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001209 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001210 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001211 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001212 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("text 2"))||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001213 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001214 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("lies, damn lies and statistics"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001215 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001216 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001217
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301218 if(QCBORDecode_Finish(&DCtx)) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001219 return -1;
1220 }
1221
1222 return 0;
1223}
1224
1225
1226
1227
1228int ParseMapTest()
1229{
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301230 // Parse a moderatly complex map structure very thoroughl
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08001231 int n = ParseMapTest1(QCBOR_DECODE_MODE_NORMAL);
1232
1233 n = ParseMapTest1(QCBOR_DECODE_MODE_MAP_STRINGS_ONLY);
1234
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001235 if(!n) {
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001236 for(int i = 0; i < 10; i++) {
1237 n = ExtraBytesTest(i);
1238 if(n) {
1239 break;
1240 }
1241 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001242 }
1243
1244 return(n);
1245}
1246
1247
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07001248static 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 +08001249
1250int ParseSimpleTest()
1251{
1252 QCBORDecodeContext DCtx;
1253 QCBORItem Item;
1254 int nCBORError;
1255
1256
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07001257 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSimpleValues), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001258
1259
1260 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1261 return nCBORError;
1262 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
1263 Item.val.uCount != 10)
1264 return -1;
1265
1266 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1267 return nCBORError;
1268 if(Item.uDataType != QCBOR_TYPE_FALSE)
1269 return -1;
1270
1271 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1272 return nCBORError;
1273 if(Item.uDataType != QCBOR_TYPE_TRUE)
1274 return -1;
1275
1276 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1277 return nCBORError;
1278 if(Item.uDataType != QCBOR_TYPE_NULL)
1279 return -1;
1280
1281 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1282 return nCBORError;
1283 if(Item.uDataType != QCBOR_TYPE_UNDEF)
1284 return -1;
1285
1286 // A break
Laurence Lundblade9e3651c2018-10-10 11:49:55 +08001287 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_BREAK)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001288 return -1;
1289
1290 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1291 return nCBORError;
1292 if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 0)
1293 return -1;
1294
1295 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1296 return nCBORError;
1297 if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 19)
1298 return -1;
1299
1300 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_INVALID_CBOR)
1301 return -1;
1302
1303 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_INVALID_CBOR)
1304 return -1;
1305
1306 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_INVALID_CBOR)
1307 return -1;
1308
1309 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1310 return nCBORError;
1311 if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 32)
1312 return -1;
1313
1314 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1315 return nCBORError;
1316 if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 255)
1317 return -1;
1318
1319 return 0;
1320
1321}
1322
1323
1324struct FailInput {
1325 UsefulBufC Input;
1326 int nError;
1327};
1328
1329
1330struct FailInput Failures[] = {
1331 { {(uint8_t[]){0x18}, 1}, QCBOR_ERR_HIT_END }, // 1 byte integer missing the byte
1332 { {(uint8_t[]){0x1c}, 1}, QCBOR_ERR_UNSUPPORTED }, // Reserved additional info = 28
1333 { {(uint8_t[]){0x1d}, 1}, QCBOR_ERR_UNSUPPORTED }, // Reserved additional info = 29
1334 { {(uint8_t[]){0x1e}, 1}, QCBOR_ERR_UNSUPPORTED }, // Reserved additional info = 30
1335 { {(uint8_t[]){0x1f}, 1}, QCBOR_ERR_UNSUPPORTED }, // Indefinite length integer
1336 { {(uint8_t[]){0x3c}, 1}, QCBOR_ERR_UNSUPPORTED }, // 1 byte integer missing the byte
1337 { {(uint8_t[]){0x3d}, 1}, QCBOR_ERR_UNSUPPORTED }, // 1 byte integer missing the byte
1338 { {(uint8_t[]){0x3e}, 1}, QCBOR_ERR_UNSUPPORTED }, // 1 byte integer missing the byte
1339 { {(uint8_t[]){0x3f}, 1}, QCBOR_ERR_UNSUPPORTED }, // Indefinite length negative integer
1340 { {(uint8_t[]){0x41}, 1}, QCBOR_ERR_HIT_END }, // Short byte string
1341 { {(uint8_t[]){0x5c}, 1}, QCBOR_ERR_UNSUPPORTED }, // Reserved additional info = 28
1342 { {(uint8_t[]){0x5f}, 1}, QCBOR_ERR_UNSUPPORTED }, // Indefinite length byte string
1343 { {(uint8_t[]){0x61}, 1}, QCBOR_ERR_HIT_END }, // Short UTF-8 string
1344 { {(uint8_t[]){0x7c}, 1}, QCBOR_ERR_UNSUPPORTED }, // Reserved additional info = 28
1345 { {(uint8_t[]){0x7f}, 1}, QCBOR_ERR_UNSUPPORTED }, // Indefinite length UTF-8 string
1346 { {(uint8_t[]){0xff}, 1}, QCBOR_ERR_UNSUPPORTED } , // break
1347 { {(uint8_t[]){0xf8, 0x00}, 2}, QCBOR_ERR_INVALID_CBOR }, // An invalid encoding of a simple type
1348 { {(uint8_t[]){0xf8, 0x1f}, 2}, QCBOR_ERR_INVALID_CBOR }, // An invalid encoding of a simple type
1349 { {(uint8_t[]){0xc0, 0x00}, 2}, QCBOR_ERR_BAD_OPT_TAG }, // Text-based date, with an integer
1350 { {(uint8_t[]){0xc1, 0x41, 0x33}, 3}, QCBOR_ERR_BAD_OPT_TAG }, // Epoch date, with an byte string
1351 { {(uint8_t[]){0xc1, 0xc0, 0x00}, 3}, QCBOR_ERR_BAD_OPT_TAG }, // tagged as both epoch and string dates
1352 { {(uint8_t[]){0xc2, 0x00}, 2}, QCBOR_ERR_BAD_OPT_TAG } // big num tagged an int, not a byte string
1353
1354};
1355
1356
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001357int FailureTests()
1358{
1359 int nResult = 0;
1360
1361 struct FailInput *pFEnd = &Failures[0] + sizeof(Failures)/sizeof(struct FailInput);
1362
1363 for(struct FailInput *pF = &Failures[0]; pF < pFEnd ;pF++) {
1364 QCBORDecodeContext DCtx;
1365 QCBORItem Item;
1366 int nCBORError;
1367
1368 QCBORDecode_Init(&DCtx, pF->Input, QCBOR_DECODE_MODE_NORMAL);
1369
1370 while(1) {
1371 nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
1372 if(QCBOR_ERR_HIT_END == nCBORError) {
1373 break;
1374 }
1375 if(nCBORError != pF->nError) {
1376 nResult = 1;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001377 break;
1378 }
1379 }
1380 }
1381
1382 {
1383 QCBORDecodeContext DCtx;
1384 QCBORItem Item;
1385 int nCBORError;
1386
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07001387 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSimpleValues), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001388
1389 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1390 return nCBORError;
1391 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
1392 Item.val.uCount != 10)
1393 return -1;
1394
1395 DCtx.InBuf.magic = 0; // Corrupt the UsefulInputBuf
1396
1397 nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
1398 if(nCBORError != QCBOR_ERR_HIT_END)
1399 return -1;
1400 }
1401
1402
1403 return nResult;
1404}
1405
1406
1407
1408
1409static void Recurser(uint8_t *pBuf, int nLen, int nLenMax)
1410{
1411
1412 if(nLen >= nLenMax) {
1413 return;
1414 }
1415
1416 //printf("__%d__%d__\n", nLen, nLenMax);
1417
1418 for(int i = 0; i < 256; i++) {
1419 pBuf[nLen] = i;
1420
1421 QCBORDecodeContext DCtx;
1422 QCBORItem Item;
1423 int nCBORError;
1424
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08001425 const UsefulBufC Input = {pBuf, nLen+1};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001426
1427 QCBORDecode_Init(&DCtx, Input, QCBOR_DECODE_MODE_NORMAL);
1428
1429 while(1) {
1430 nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
1431 if(QCBOR_ERR_HIT_END == nCBORError) {
1432 break;
1433 }
1434 if(nCBORError != QCBOR_SUCCESS) {
1435 if(nCBORError != QCBOR_ERR_UNSUPPORTED && nCBORError != QCBOR_ERR_HIT_END && nCBORError != QCBOR_ERR_INVALID_CBOR) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001436 }
1437 break;
1438 }
1439 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001440
1441
1442 Recurser(pBuf, nLen+1, nLenMax);
1443 }
1444}
1445
1446
1447/*
1448 Runs all possible input strings of a given length. This is set to 3 to make the test
1449 run in reasonable time.
1450 Main point of this test is to not crash.
1451 */
1452
1453int ComprehensiveInputTest()
1454{
1455 uint8_t pBuf[3]; // 3 keeps it running in reasonable time. 4 takes tens of minutes.
1456
1457 Recurser(pBuf, 0, sizeof(pBuf));
1458
1459 return 0;
1460}
1461
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07001462static uint8_t spDateTestInput[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001463 0xc0, // tag for string date
1464 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string
1465
1466 0xc1, // tag for epoch date
1467 0x1a, 0x53, 0x72, 0x4E, 0x00, // Epoch date 1400000000; Tue, 13 May 2014 16:53:20 GMT
1468
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001469 // CBOR_TAG_B64
1470 0xc1, 0xcf, 0xd8, 0x22, // 0xee, // Epoch date with extra tags TODO: fix this test
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001471 0x1a, 0x53, 0x72, 0x4E, 0x01,
1472
1473 0xc1, // tag for epoch date
1474 0x1b, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, // Too large integer
1475
1476 0xc1, // tag for epoch date
1477 0xfa, 0x3f, 0x8c, 0xcc, 0xcd, // double with value 1.1
1478
1479 0xc1, // tag for epoch date
1480 0xfa, 0x7f, 0x7f, 0xff, 0xff // 3.4028234663852886e+38 too large
1481
1482};
1483
1484
1485// have to check float expected only to within an epsilon
1486int CHECK_EXPECTED_DOUBLE(double val, double expected) {
1487
1488 double diff = val - expected;
1489
1490 diff = fabs(diff);
1491
1492 return diff > 0.0000001;
1493}
1494
1495
1496int DateParseTest()
1497{
1498 QCBORDecodeContext DCtx;
1499 QCBORItem Item;
1500 int nCBORError;
1501
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07001502 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spDateTestInput), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001503
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001504 const uint64_t uTags[] = {15};
1505 QCBORTagListIn TagList = {1, uTags};
1506
1507 QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TagList);
1508
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001509 // String date
1510 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1511 return -1;
1512 if(Item.uDataType != QCBOR_TYPE_DATE_STRING ||
Laurence Lundblade9e3651c2018-10-10 11:49:55 +08001513 UsefulBuf_Compare(Item.val.dateString, UsefulBuf_FromSZ("1985-04-12"))){
Laurence Lundblade67bd5512018-11-02 21:44:06 +07001514 return -2;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001515 }
1516
1517 // Epoch date
1518 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade67bd5512018-11-02 21:44:06 +07001519 return -3;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001520 if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH ||
1521 Item.val.epochDate.nSeconds != 1400000000 ||
1522 Item.val.epochDate.fSecondsFraction != 0 ) {
Laurence Lundblade67bd5512018-11-02 21:44:06 +07001523 return -4;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001524 }
1525
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001526 // Epoch date with extra CBOR_TAG_B64 tag that doesn't really mean anything
1527 // but want to be sure extra tag doesn't cause a problem
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001528 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade67bd5512018-11-02 21:44:06 +07001529 return -5;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001530 if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH ||
1531 Item.val.epochDate.nSeconds != 1400000001 ||
1532 Item.val.epochDate.fSecondsFraction != 0 ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001533 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_B64)) {
Laurence Lundblade67bd5512018-11-02 21:44:06 +07001534 return -6;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001535 }
1536
1537 // Epoch date that is too large for our representation
1538 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_DATE_OVERFLOW) {
Laurence Lundblade67bd5512018-11-02 21:44:06 +07001539 return -7;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001540 }
1541
1542 // Epoch date in float format with fractional seconds
1543 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade67bd5512018-11-02 21:44:06 +07001544 return -8;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001545 if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH ||
1546 Item.val.epochDate.nSeconds != 1 ||
1547 CHECK_EXPECTED_DOUBLE(Item.val.epochDate.fSecondsFraction, 0.1 )) {
Laurence Lundblade67bd5512018-11-02 21:44:06 +07001548 return -9;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001549 }
1550
1551 // Epoch date float that is too large for our representation
1552 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_DATE_OVERFLOW) {
Laurence Lundblade67bd5512018-11-02 21:44:06 +07001553 return -10;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001554 }
1555
1556 // TODO: could use a few more tests with float, double, and half precsion and negative (but coverage is still pretty good)
1557
1558 return 0;
1559}
1560
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001561// Really simple basic input for tagging test
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07001562static uint8_t spOptTestInput[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001563 0xd9, 0xd9, 0xf7, // CBOR magic number
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001564 0x81, // Array of one
1565 0xd8, 0x04, // non-preferred serialization of tag 4
1566 0x82, 0x01, 0x03}; // fraction 1/3
1567
1568static uint8_t spEncodedLargeTag[] = {0xdb, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x80};
1569
1570// 0x9192939495969798, 0x88, 0x01, 0x04
1571static uint8_t spLotsOfTags[] = {0xdb, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0xd8, 0x88, 0xc5, 0xc4, 0x80};
1572
1573/*
1574 The cbor.me parse of this.
1575 55799(55799(55799({6(7(-23)): 5859837686836516696(7({7(-20): 11({17(-18): 17(17(17("Organization"))),
1576 9(-17): 773("SSG"), -15: 4(5(6(7(8(9(10(11(12(13(14(15("Confusion")))))))))))), 17(-16): 17("San Diego"),
1577 17(-14): 17("US")}), 23(-19): 19({-11: 9({-9: -7}),
1578 90599561(90599561(90599561(-10))): 12(h'0102030405060708090A')})})),
1579 16(-22): 23({11(8(7(-5))): 8(-3)})})))
1580 */
1581static uint8_t spCSRWithTags[] = {
1582 0xd9, 0xd9, 0xf7, 0xd9, 0xd9, 0xf7, 0xd9, 0xd9, 0xf7, 0xa2,
1583 0xc6, 0xc7, 0x36,
1584 0xdb, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0xc7, 0xa2,
1585 0xda, 0x00, 0x00, 0x00, 0x07, 0x33,
1586 0xcb, 0xa5,
1587 0xd1, 0x31,
1588 0xd1, 0xd1, 0xd1, 0x6c,
1589 0x4f, 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74, 0x69, 0x6f, 0x6e,
1590 0xc9, 0x30,
1591 0xd9, 0x03, 0x05, 0x63,
1592 0x53, 0x53, 0x47,
1593 0x2e,
1594 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf, 0x69,
1595 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73, 0x69, 0x6f, 0x6e,
1596 0xd1, 0x2f,
1597 0xd1, 0x69,
1598 0x53, 0x61, 0x6e, 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f,
1599 0xd1, 0x2d,
1600 0xd1, 0x62,
1601 0x55, 0x53,
1602 0xd7, 0x32,
1603 0xd3, 0xa2,
1604 0x2a,
1605 0xc9, 0xa1,
1606 0x28,
1607 0x26,
1608 0xda, 0x05, 0x66, 0x70, 0x89, 0xda, 0x05, 0x66, 0x70, 0x89, 0xda, 0x05, 0x66, 0x70, 0x89, 0x29,
1609 0xcc, 0x4a,
1610 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,0x07, 0x08, 0x09, 0x0a,
1611 0xd0, 0x35,
1612 0xd7, 0xa1,
1613 0xcb, 0xc8, 0xc7, 0x24,
1614 0xc8, 0x22};
1615
1616static int CheckCSRMaps(QCBORDecodeContext *pDC);
1617
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001618
1619int OptTagParseTest()
1620{
1621 QCBORDecodeContext DCtx;
1622 QCBORItem Item;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001623
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07001624 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spOptTestInput), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001625
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001626 //-------------------------
1627 // This text matches the magic number tag and the fraction tag
1628 if(QCBORDecode_GetNext(&DCtx, &Item)) {
1629 return -2;
1630 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001631 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001632 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC)) {
1633 return -3;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001634 }
1635
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001636 if(QCBORDecode_GetNext(&DCtx, &Item)) {
1637 return -4;
1638 }
1639 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
1640 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_FRACTION) ||
1641 Item.val.uCount != 2) {
1642 return -5;
1643 }
1644
1645 // --------------------------------
1646 // This test decodes the very large tag, but it is not in
1647 // any list so it is ignored.
1648 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEncodedLargeTag), QCBOR_DECODE_MODE_NORMAL);
1649 if(QCBORDecode_GetNext(&DCtx, &Item)) {
1650 return -6;
1651 }
1652 if(Item.uTagBits) {
1653 return -7;
1654 }
1655
1656 // ----------------------------------
1657 // This test sets up a caller-config list that includes the very large tage and then matches it.
1658 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEncodedLargeTag), QCBOR_DECODE_MODE_NORMAL);
1659 const uint64_t puList[] = {0x9192939495969798, 257};
1660 const QCBORTagListIn TL = {2, puList};
1661 QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TL);
1662
1663 if(QCBORDecode_GetNext(&DCtx, &Item)) {
1664 return -8;
1665 }
1666 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
1667 !QCBORDecode_IsTagged(&DCtx, &Item, 0x9192939495969798) ||
1668 QCBORDecode_IsTagged(&DCtx, &Item, 257) ||
1669 QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_BIGFLOAT) ||
1670 Item.val.uCount != 0) {
1671 return -9;
1672 }
1673
1674 //------------------------
1675 // This test sets up a caller-configured list, and looks up something not in it
1676 const uint64_t puLongList[17] = {1,2,1};
1677 const QCBORTagListIn TLLong = {17, puLongList};
1678 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEncodedLargeTag), QCBOR_DECODE_MODE_NORMAL);
1679 QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TLLong);
1680 if(QCBORDecode_GetNext(&DCtx, &Item)) {
1681 return -11;
1682 }
1683
1684 // -----------------------
1685 // This tests retrievel of the full tag list
1686 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spLotsOfTags), QCBOR_DECODE_MODE_NORMAL);
1687 uint64_t puTags[16];
1688 QCBORTagListOut Out = {0, 4, puTags};
1689 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
1690 return -12;
1691 }
1692 if(puTags[0] != 0x9192939495969798 ||
1693 puTags[1] != 0x88 ||
1694 puTags[2] != 0x05 ||
1695 puTags[3] != 0x04) {
1696 return -13;
1697 }
1698
1699 // ----------------------
1700 // This text if too small of an out list
1701 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spLotsOfTags), QCBOR_DECODE_MODE_NORMAL);
1702 QCBORTagListOut OutSmall = {0, 3, puTags};
1703 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &OutSmall) != QCBOR_ERR_TOO_MANY_TAGS) {
1704 return -14;
1705 }
1706
1707 // ---------------
1708 // Parse a version of the "CSR" that has had a ton of tags randomly inserted
1709 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRWithTags), QCBOR_DECODE_MODE_NORMAL);
1710 int n = CheckCSRMaps(&DCtx);
1711 if(n) {
1712 return n-2000;
1713 }
1714
1715 Out = (QCBORTagListOut){0,16, puTags};
1716 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRWithTags), QCBOR_DECODE_MODE_NORMAL);
1717
1718 const uint64_t puTagList[] = {773, 1, 90599561};
1719 const QCBORTagListIn TagList = {3, puTagList};
1720 QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TagList);
1721
1722
1723 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
1724 return -100;
1725 }
1726 if(Item.uDataType != QCBOR_TYPE_MAP ||
1727 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC) ||
1728 QCBORDecode_IsTagged(&DCtx, &Item, 90599561) ||
1729 QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_DATE_EPOCH) ||
1730 Item.val.uCount != 2 ||
1731 puTags[0] != CBOR_TAG_CBOR_MAGIC ||
1732 puTags[1] != CBOR_TAG_CBOR_MAGIC ||
1733 puTags[2] != CBOR_TAG_CBOR_MAGIC ||
1734 Out.uNumUsed != 3) {
1735 return -101;
1736 }
1737
1738 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
1739 return -102;
1740 }
1741 if(Item.uDataType != QCBOR_TYPE_MAP ||
1742 QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC) ||
1743 QCBORDecode_IsTagged(&DCtx, &Item, 6) ||
1744 QCBORDecode_IsTagged(&DCtx, &Item, 7) || // item is tagged 7, but 7 is not configured to be recognized
1745 Item.val.uCount != 2 ||
1746 puTags[0] != 5859837686836516696 ||
1747 puTags[1] != 7 ||
1748 Out.uNumUsed != 2) {
1749 return -103;
1750 }
1751
1752 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
1753 return -104;
1754 }
1755 if(Item.uDataType != QCBOR_TYPE_MAP ||
1756 Item.uTagBits ||
1757 Item.val.uCount != 5 ||
1758 puTags[0] != 0x0b ||
1759 Out.uNumUsed != 1) {
1760 return -105;
1761 }
1762
1763 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
1764 return -106;
1765 }
1766 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1767 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_COSE_MAC0) ||
1768 Item.val.string.len != 12 ||
1769 puTags[0] != CBOR_TAG_COSE_MAC0 ||
1770 puTags[1] != CBOR_TAG_COSE_MAC0 ||
1771 puTags[2] != CBOR_TAG_COSE_MAC0 ||
1772 Out.uNumUsed != 3) {
1773 return -105;
1774 }
1775
1776 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
1777 return -107;
1778 }
1779 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1780 !QCBORDecode_IsTagged(&DCtx, &Item, 773) ||
1781 Item.val.string.len != 3 ||
1782 puTags[0] != 773 ||
1783 Out.uNumUsed != 1) {
1784 return -108;
1785 }
1786
1787 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
1788 return -109;
1789 }
1790 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1791 !QCBORDecode_IsTagged(&DCtx, &Item, 4) ||
1792 Item.val.string.len != 9 ||
1793 puTags[0] != 4 ||
1794 puTags[11] != 0x0f ||
1795 Out.uNumUsed != 12) {
1796 return -110;
1797 }
1798
1799 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
1800 return -111;
1801 }
1802 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1803 !QCBORDecode_IsTagged(&DCtx, &Item, 17) ||
1804 Item.val.string.len != 9 ||
1805 puTags[0] != 17 ||
1806 Out.uNumUsed != 1) {
1807 return -112;
1808 }
1809
1810 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
1811 return -111;
1812 }
1813 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1814 !QCBORDecode_IsTagged(&DCtx, &Item, 17) ||
1815 Item.val.string.len != 2 ||
1816 puTags[0] != 17 ||
1817 Out.uNumUsed != 1) {
1818 return -112;
1819 }
1820
1821 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
1822 return -113;
1823 }
1824 if(Item.uDataType != QCBOR_TYPE_MAP ||
1825 QCBORDecode_IsTagged(&DCtx, &Item, 19) ||
1826 Item.val.uCount != 2 ||
1827 puTags[0] != 19 ||
1828 Out.uNumUsed != 1) {
1829 return -114;
1830 }
1831
1832 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
1833 return -115;
1834 }
1835 if(Item.uDataType != QCBOR_TYPE_MAP ||
1836 QCBORDecode_IsTagged(&DCtx, &Item, 9) ||
1837 Item.uTagBits ||
1838 Item.val.uCount != 1 ||
1839 puTags[0] != 9 ||
1840 Out.uNumUsed != 1) {
1841 return -116;
1842 }
1843
1844 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
1845 return -116;
1846 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001847 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001848 Item.val.int64 != -7 ||
1849 Item.uTagBits ||
1850 Out.uNumUsed != 0) {
1851 return -117;
1852 }
1853
1854 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
1855 return -118;
1856 }
1857 if(Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
1858 Item.val.string.len != 10 ||
1859 Item.uTagBits ||
1860 puTags[0] != 12 ||
1861 Out.uNumUsed != 1) {
1862 return -119;
1863 }
1864
1865 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
1866 return -120;
1867 }
1868 if(Item.uDataType != QCBOR_TYPE_MAP ||
1869 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_ENC_AS_B16) ||
1870 Item.val.uCount != 1 ||
1871 puTags[0] != 0x17 ||
1872 Out.uNumUsed != 1) {
1873 return -121;
1874 }
1875
1876 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
1877 return -122;
1878 }
1879 if(Item.uDataType != QCBOR_TYPE_INT64 ||
1880 QCBORDecode_IsTagged(&DCtx, &Item, 8) ||
1881 Item.val.int64 != -3 ||
1882 puTags[0] != 8 ||
1883 Out.uNumUsed != 1) {
1884 return -123;
1885 }
1886
1887 if(QCBORDecode_Finish(&DCtx)) {
1888 return -124;
1889 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001890
1891 return 0;
1892}
1893
1894
1895
1896
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07001897static uint8_t spBigNumInput[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001898 0x83,
1899 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1900 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1901 0xA4,
1902 0x63, 0x42, 0x4E, 0x2B,
1903 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1904 0x18, 0x40,
1905 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1906 0x63, 0x42, 0x4E, 0x2D,
1907 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1908 0x38, 0x3F,
1909 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
1910
1911
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07001912static uint8_t spBigNum[] = {0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001913
1914
1915int BignumParseTest()
1916{
1917 QCBORDecodeContext DCtx;
1918 QCBORItem Item;
1919 int nCBORError;
1920
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07001921 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNumInput), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001922
1923
1924 //
1925 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1926 return -1;
1927 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
1928 return -1;
1929 }
1930
1931 //
1932 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1933 return -1;
1934 if(Item.uDataType != QCBOR_TYPE_POSBIGNUM ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07001935 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001936 return -1;
1937 }
1938
1939 //
1940 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1941 return -1;
1942 if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07001943 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001944 return -1;
1945 }
1946
1947 //
1948 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1949 return -1;
1950 if(Item.uDataType != QCBOR_TYPE_MAP) {
1951 return -1;
1952 }
1953
1954 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1955 return -1;
1956 if(Item.uDataType != QCBOR_TYPE_POSBIGNUM ||
1957 Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07001958 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001959 return -1;
1960 }
1961
1962 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1963 return -1;
1964 if(Item.uDataType != QCBOR_TYPE_POSBIGNUM ||
1965 Item.uLabelType != QCBOR_TYPE_INT64 ||
1966 Item.label.int64 != 64 ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07001967 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001968 return -1;
1969 }
1970
1971 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1972 return -1;
1973 if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM ||
1974 Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07001975 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001976 return -1;
1977 }
1978
1979 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1980 return -1;
1981 if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM ||
1982 Item.uLabelType != QCBOR_TYPE_INT64 ||
1983 Item.label.int64 != -64 ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07001984 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001985 return -1;
1986 }
1987
1988 return 0;
1989}
1990
1991
1992
Laurence Lundbladea44d5062018-10-17 18:45:12 +05301993static 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 +08001994{
1995 QCBORItem Item;
1996 int nCBORError;
1997
1998 if((nCBORError = QCBORDecode_GetNext(pCtx, &Item))) return -1;
1999 if(Item.uDataType != uDataType) return -1;
2000 if(uNestingLevel > 0) {
2001 if(Item.uLabelType != QCBOR_TYPE_INT64 && Item.uLabelType != QCBOR_TYPE_UINT64) return -1;
2002 if(Item.uLabelType == QCBOR_TYPE_INT64) {
2003 if(Item.label.int64 != nLabel) return -1;
2004 } else {
Laurence Lundblade570fab52018-10-13 18:28:27 +08002005 if(Item.label.uint64 != (uint64_t)nLabel) return -1;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002006 }
2007 }
2008 if(Item.uNestingLevel != uNestingLevel) return -1;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302009 if(Item.uNextNestLevel != uNextNest) return -1;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002010
2011 if(pItem) {
2012 *pItem = Item;
2013 }
2014 return 0;
2015}
2016
2017
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002018// Same code checks definite and indefinite length versions of the map
2019static int CheckCSRMaps(QCBORDecodeContext *pDC)
2020{
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302021 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 0, 1, 0, NULL)) return -1;
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002022
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302023 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 1, 2, -23, NULL)) return -1;
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002024
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302025 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 2, 3, -20, NULL)) return -1;
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002026
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302027 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -18, NULL)) return -1;
2028 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -17, NULL)) return -1;
2029 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -15, NULL)) return -1;
2030 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -16, NULL)) return -1;
2031 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 2, -14, NULL)) return -1;
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002032
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302033 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 2, 3, -19, NULL)) return -1;
2034 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 3, 4, -11, NULL)) return -1;
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002035
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302036 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_INT64, 4, 3, -9, NULL)) return -1;
2037 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_BYTE_STRING, 3, 1, -10, NULL)) return -1;
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002038
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302039 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 1, 2, -22, NULL)) return -1;
2040 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_INT64, 2, 0, -5, NULL)) return -1;
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002041
2042 if(QCBORDecode_Finish(pDC)) return -2;
2043
2044 return 0;
2045}
2046
2047
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002048/*
2049// cbor.me decoded output
2050{
2051 -23: {
2052 -20: {
2053 -18: "Organization",
2054 -17: "SSG",
2055 -15: "Confusion",
2056 -16: "San Diego",
2057 -14: "US"
2058 },
2059 -19: {
2060 -11: {
2061 -9: -7
2062 },
2063 -10: '\u0001\u0002\u0003\u0004\u0005\u0006\a\b\t\n'
2064 }
2065 },
2066 -22: {
2067 -5: -3
2068 }
2069}
2070 */
2071
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002072
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002073static uint8_t spCSRInput[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002074 0xa2, 0x36, 0xa2, 0x33, 0xa5, 0x31, 0x6c, 0x4f,
2075 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74,
2076 0x69, 0x6f, 0x6e, 0x30, 0x63, 0x53, 0x53, 0x47,
2077 0x2e, 0x69, 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73,
2078 0x69, 0x6f, 0x6e, 0x2f, 0x69, 0x53, 0x61, 0x6e,
2079 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f, 0x2d, 0x62,
2080 0x55, 0x53, 0x32, 0xa2, 0x2a, 0xa1, 0x28, 0x26,
2081 0x29, 0x4a, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
2082 0x07, 0x08, 0x09, 0x0a, 0x35, 0xa1, 0x24, 0x22};
2083
2084int NestedMapTest()
2085{
2086 QCBORDecodeContext DCtx;
2087
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002088 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002089
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002090 return CheckCSRMaps(&DCtx);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002091}
2092
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08002093
2094
2095int StringDecoderModeFailTest()
2096{
2097 QCBORDecodeContext DCtx;
2098
2099 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput), QCBOR_DECODE_MODE_MAP_STRINGS_ONLY);
2100
2101 QCBORItem Item;
2102 QCBORError nCBORError;
2103
2104 if(QCBORDecode_GetNext(&DCtx, &Item)) {
2105 return -1;
2106 }
2107 if(Item.uDataType != QCBOR_TYPE_MAP) {
2108 return -2;
2109 }
2110
2111 nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
2112 if(nCBORError != QCBOR_ERR_MAP_LABEL_TYPE) {
2113 return -3;
2114 }
2115
2116 return 0;
2117}
2118
2119
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002120// Same map as above, but using indefinite lengths
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002121static uint8_t spCSRInputIndefLen[] = {
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002122 0xbf, 0x36, 0xbf, 0x33, 0xbf, 0x31, 0x6c, 0x4f,
2123 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74,
2124 0x69, 0x6f, 0x6e, 0x30, 0x63, 0x53, 0x53, 0x47,
2125 0x2e, 0x69, 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73,
2126 0x69, 0x6f, 0x6e, 0x2f, 0x69, 0x53, 0x61, 0x6e,
2127 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f, 0x2d, 0x62,
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002128 0x55, 0x53, 0xff, 0x32, 0xbf, 0x2a, 0xbf, 0x28,
2129 0x26, 0xff, 0x29, 0x4a, 0x01, 0x02, 0x03, 0x04,
2130 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0xff, 0xff,
2131 0x35, 0xbf, 0x24, 0x22, 0xff, 0xff};
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002132
2133int NestedMapTestIndefLen()
2134{
2135 QCBORDecodeContext DCtx;
2136
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002137 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInputIndefLen), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002138
2139 return CheckCSRMaps(&DCtx);
2140}
2141
2142
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002143
Laurence Lundblade17ede402018-10-13 11:43:07 +08002144static UsefulBufC make_nested_indefinite_arrays(int n, UsefulBuf Storage)
2145{
2146 UsefulOutBuf UOB;
2147 UsefulOutBuf_Init(&UOB, Storage);
2148
2149 int i;
2150 for(i = 0; i < n; i++) {
2151 UsefulOutBuf_AppendByte(&UOB, 0x9f);
2152 }
2153
2154 for(i = 0; i < n; i++) {
2155 UsefulOutBuf_AppendByte(&UOB, 0xff);
2156 }
2157 return UsefulOutBuf_OutUBuf(&UOB);
2158}
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002159
2160
Laurence Lundblade17ede402018-10-13 11:43:07 +08002161static int parse_indeflen_nested(UsefulBufC Nested, int nNestLevel)
2162{
2163 QCBORDecodeContext DC;
2164 QCBORDecode_Init(&DC, Nested, 0);
2165
2166 int j;
2167 for(j = 0; j < nNestLevel; j++) {
2168 QCBORItem Item;
2169 int nReturn = QCBORDecode_GetNext(&DC, &Item);
2170 if(j >= QCBOR_MAX_ARRAY_NESTING) {
2171 // Should be in error
2172 if(nReturn != QCBOR_ERR_ARRAY_NESTING_TOO_DEEP) {
2173 return -4;
2174 } else {
2175 return 0; // Decoding doesn't recover after an error
2176 }
2177 } else {
2178 // Should be no error
2179 if(nReturn) {
2180 return -9; // Should not have got an error
2181 }
2182 }
2183 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
2184 return -7;
2185 }
2186 }
2187 int nReturn = QCBORDecode_Finish(&DC);
2188 if(nReturn) {
2189 return -3;
2190 }
2191 return 0;
2192}
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002193
2194
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302195int IndefiniteLengthNestTest()
Laurence Lundblade17ede402018-10-13 11:43:07 +08002196{
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05302197 UsefulBuf_MAKE_STACK_UB(Storage, 50);
Laurence Lundblade17ede402018-10-13 11:43:07 +08002198 int i;
Laurence Lundblade6de37062018-10-15 12:22:42 +05302199 for(i=1; i < QCBOR_MAX_ARRAY_NESTING+4; i++) {
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08002200 const UsefulBufC Nested = make_nested_indefinite_arrays(i, Storage);
Laurence Lundblade17ede402018-10-13 11:43:07 +08002201 int nReturn = parse_indeflen_nested(Nested, i);
2202 if(nReturn) {
2203 return nReturn;
2204 }
2205 }
2206 return 0;
2207}
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002208
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002209
Laurence Lundblade6de37062018-10-15 12:22:42 +05302210
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002211static const uint8_t spIndefiniteArray[] = {0x9f, 0x01, 0x82, 0x02, 0x03, 0xff}; // [1, [2, 3]]
2212static const uint8_t spIndefiniteArrayBad1[] = {0x9f}; // No closing break
2213static const uint8_t spIndefiniteArrayBad2[] = {0x9f, 0x9f, 0x02, 0xff}; // Not enough closing breaks
2214static const uint8_t spIndefiniteArrayBad3[] = {0x9f, 0x02, 0xff, 0xff}; // Too many closing breaks
2215static const uint8_t spIndefiniteArrayBad4[] = {0x81, 0x9f}; // Unclosed indeflen inside def len
2216static const uint8_t spIndefiniteArrayBad5[] = {0x9f, 0xd1, 0xff}; // confused tag
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002217
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302218int IndefiniteLengthArrayMapTest()
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002219{
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002220 int nResult;
2221 // --- first test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002222 UsefulBufC IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArray);
Laurence Lundblade17ede402018-10-13 11:43:07 +08002223
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002224 // Decode it and see if it is OK
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05302225 UsefulBuf_MAKE_STACK_UB(MemPool, 150);
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002226 QCBORDecodeContext DC;
2227 QCBORItem Item;
2228 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
2229
2230 QCBORDecode_SetMemPool(&DC, MemPool, false);
Laurence Lundblade6de37062018-10-15 12:22:42 +05302231
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002232 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05302233
2234 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
2235 Item.uNestingLevel != 0 ||
2236 Item.uNextNestLevel != 1) {
2237 return -111;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002238 }
2239
2240 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05302241 if(Item.uDataType != QCBOR_TYPE_INT64 ||
2242 Item.uNestingLevel != 1 ||
2243 Item.uNextNestLevel != 1) {
2244 return -2;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002245 }
2246
2247 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05302248 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
2249 Item.uNestingLevel != 1 ||
2250 Item.uNextNestLevel != 2) {
2251 return -3;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002252 }
2253
2254 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade12b495d2018-12-17 11:15:54 -08002255 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade6de37062018-10-15 12:22:42 +05302256 Item.uNestingLevel != 2 ||
2257 Item.uNextNestLevel != 2) {
2258 return -4;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002259 }
2260
2261 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade12b495d2018-12-17 11:15:54 -08002262 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade6de37062018-10-15 12:22:42 +05302263 Item.uNestingLevel != 2 ||
2264 Item.uNextNestLevel != 0) {
2265 return -5;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002266 }
2267
2268 if(QCBORDecode_Finish(&DC)) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05302269 return -6;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002270 }
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002271
2272 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002273 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad1);
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002274
2275 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
2276
2277 QCBORDecode_SetMemPool(&DC, MemPool, false);
2278
2279 nResult = QCBORDecode_GetNext(&DC, &Item);
2280 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05302281 return -7;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002282 }
2283
Laurence Lundblade570fab52018-10-13 18:28:27 +08002284 nResult = QCBORDecode_Finish(&DC);
Laurence Lundblade6de37062018-10-15 12:22:42 +05302285 if(nResult != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
2286 return -8;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002287 }
2288
2289
2290 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002291 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad2);
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002292
2293 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
2294
2295 QCBORDecode_SetMemPool(&DC, MemPool, false);
2296
2297 nResult = QCBORDecode_GetNext(&DC, &Item);
2298 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05302299 return -9;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002300 }
2301
2302 nResult = QCBORDecode_GetNext(&DC, &Item);
2303 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05302304 return -10;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002305 }
2306
2307 nResult = QCBORDecode_GetNext(&DC, &Item);
2308 if(nResult || Item.uDataType != QCBOR_TYPE_INT64) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05302309 return -11;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002310 }
2311
2312 nResult = QCBORDecode_Finish(&DC);
Laurence Lundblade6de37062018-10-15 12:22:42 +05302313 if(nResult != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
2314 return -12;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002315 }
2316
2317
2318 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002319 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad3);
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002320
2321 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
2322
2323 QCBORDecode_SetMemPool(&DC, MemPool, false);
2324
2325 nResult = QCBORDecode_GetNext(&DC, &Item);
2326 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05302327 return -13;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002328 }
2329
2330 nResult = QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05302331 if(nResult != QCBOR_ERR_BAD_BREAK) {
2332 return -14;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002333 }
Laurence Lundblade6de37062018-10-15 12:22:42 +05302334
Laurence Lundblade570fab52018-10-13 18:28:27 +08002335
2336 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002337 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad4);
Laurence Lundblade570fab52018-10-13 18:28:27 +08002338
2339 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
2340
2341 QCBORDecode_SetMemPool(&DC, MemPool, false);
2342
2343 nResult = QCBORDecode_GetNext(&DC, &Item);
2344 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05302345 return -15;
Laurence Lundblade570fab52018-10-13 18:28:27 +08002346 }
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302347
Laurence Lundblade570fab52018-10-13 18:28:27 +08002348 nResult = QCBORDecode_GetNext(&DC, &Item);
2349 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05302350 return -16;
Laurence Lundblade570fab52018-10-13 18:28:27 +08002351 }
2352
2353 nResult = QCBORDecode_Finish(&DC);
Laurence Lundblade6de37062018-10-15 12:22:42 +05302354 if(nResult != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
2355 return -17;
Laurence Lundblade570fab52018-10-13 18:28:27 +08002356 }
2357
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302358 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002359 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad5);
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302360
2361 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
2362
2363 QCBORDecode_SetMemPool(&DC, MemPool, false);
2364
2365 nResult = QCBORDecode_GetNext(&DC, &Item);
2366 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05302367 return -18;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302368 }
2369
2370 nResult = QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05302371 if(nResult != QCBOR_ERR_BAD_BREAK) {
2372 return -19;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302373 }
2374
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002375 return 0;
2376}
2377
Laurence Lundblade17ede402018-10-13 11:43:07 +08002378
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002379static const uint8_t spIndefiniteLenString[] = {
Laurence Lundblade17ede402018-10-13 11:43:07 +08002380 0x81, // Array of length one
2381 0x7f, // text string marked with indefinite length
2382 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment
2383 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment
2384 0xff // ending break
2385};
2386
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002387static const uint8_t spIndefiniteLenStringBad2[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302388 0x81, // Array of length one
2389 0x7f, // text string marked with indefinite length
2390 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment
2391 0x44, 0x6d, 0x69, 0x6e, 0x67, // second segment of wrong type
2392 0xff // ending break
2393};
2394
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002395static const uint8_t spIndefiniteLenStringBad3[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302396 0x81, // Array of length one
2397 0x7f, // text string marked with indefinite length
2398 0x01, 0x02, // Not a string
2399 0xff // ending break
2400};
2401
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002402static const uint8_t spIndefiniteLenStringBad4[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302403 0x81, // Array of length one
2404 0x7f, // text string marked with indefinite length
2405 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment
2406 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment
2407 // missing end of string
2408};
2409
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002410static const uint8_t spIndefiniteLenStringLabel[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302411 0xa1, // Array of length one
2412 0x7f, // text string marked with indefinite length
2413 0x65, 0x73, 0x74, 0x72, 0x75, 0x75, // first segment
2414 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment
2415 0xff, // ending break
2416 0x01 // integer being labeled.
2417};
2418
2419static UsefulBufC MakeIndefiniteBigBstr(UsefulBuf Storage)
2420{
2421 UsefulOutBuf UOB;
2422
2423 UsefulOutBuf_Init(&UOB, Storage);
2424 UsefulOutBuf_AppendByte(&UOB, 0x81);
2425 UsefulOutBuf_AppendByte(&UOB, 0x5f);
2426
2427 int i = 0;
2428 for(int nChunkSize = 1; nChunkSize <= 128; nChunkSize *= 2) {
2429 UsefulOutBuf_AppendByte(&UOB, 0x58);
2430 UsefulOutBuf_AppendByte(&UOB, (uint8_t)nChunkSize);
2431 for(int j = 0; j < nChunkSize; j++ ) {
2432 UsefulOutBuf_AppendByte(&UOB, i);
2433 i++;
2434 }
2435 }
2436 UsefulOutBuf_AppendByte(&UOB, 0xff);
2437
2438 return UsefulOutBuf_OutUBuf(&UOB);
2439}
2440
2441static int CheckBigString(UsefulBufC BigString)
2442{
2443 if(BigString.len != 255) {
2444 return 1;
2445 }
2446
2447 for(uint8_t i = 0; i < 255; i++){
2448 if(((const uint8_t *)BigString.ptr)[i] != i) {
2449 return 1;
2450 }
2451 }
2452 return 0;
2453}
2454
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05302455
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302456int IndefiniteLengthStringTest()
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302457{
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302458 QCBORDecodeContext DC;
2459 QCBORItem Item;
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05302460 // big enough for MakeIndefiniteBigBstr() + MemPool overhead
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05302461 UsefulBuf_MAKE_STACK_UB(MemPool, 320);
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302462
2463 // --- Simple normal indefinite length string ------
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002464 UsefulBufC IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenString);
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302465 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002466
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05302467 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302468 return -1;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302469 }
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002470
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302471 if(QCBORDecode_GetNext(&DC, &Item)) {
2472 return -2;
2473 }
2474 if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.uDataAlloc) {
2475 return -3;
2476 }
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002477
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302478 if(QCBORDecode_GetNext(&DC, &Item)) {
2479 return -4;
2480 }
2481 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || !Item.uDataAlloc) {
2482 return -5;
2483 }
2484 if(QCBORDecode_Finish(&DC)) {
2485 return -6;
2486 }
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302487
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302488 // ----- types mismatch ---
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002489 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad2), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302490
2491 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
2492 return -7;
2493 }
2494
2495 if(QCBORDecode_GetNext(&DC, &Item)) {
2496 return -8;
2497 }
2498 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
2499 return -9;
2500 }
2501
Laurence Lundblade30816f22018-11-10 13:40:22 +07002502 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_INDEFINITE_STRING_CHUNK) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302503 return -10;
2504 }
2505
2506 // ----- not a string ---
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002507 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad3), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302508
2509 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
2510 return -11;
2511 }
2512
2513 if(QCBORDecode_GetNext(&DC, &Item)) {
2514 return -12;
2515 }
2516 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
2517 return -13;
2518 }
2519
Laurence Lundblade30816f22018-11-10 13:40:22 +07002520 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_INDEFINITE_STRING_CHUNK) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302521 return -14;
2522 }
2523
2524 // ----- no end -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002525 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad4), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302526
2527 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
2528 return -15;
2529 }
2530
2531 if(QCBORDecode_GetNext(&DC, &Item)) {
2532 return -16;
2533 }
2534 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
2535 return -17;
2536 }
2537
2538 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_HIT_END) {
2539 return -18;
2540 }
2541
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302542 // ------ Don't set a string allocator and see an error -----
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302543 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
2544
2545 QCBORDecode_GetNext(&DC, &Item);
2546 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302547 return -19;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302548 }
2549
2550 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_NO_STRING_ALLOCATOR) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302551 return -20;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302552 }
2553
2554 // ----- Mempool is way too small -----
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05302555 UsefulBuf_MAKE_STACK_UB(MemPoolTooSmall, 20); // 20 is too small no matter what
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302556
2557 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
2558 if(!QCBORDecode_SetMemPool(&DC, MemPoolTooSmall, false)) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302559 return -21;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302560 }
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302561
2562 // ----- Mempool is way too small -----
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05302563 UsefulBuf_MAKE_STACK_UB(BigIndefBStrStorage, 290);
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08002564 const UsefulBufC BigIndefBStr = MakeIndefiniteBigBstr(BigIndefBStrStorage);
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302565
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05302566 UsefulBuf_MAKE_STACK_UB(MemPoolSmall, 80); // 80 is big enough for MemPool overhead, but not BigIndefBStr
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302567
2568 QCBORDecode_Init(&DC, BigIndefBStr, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302569 if(QCBORDecode_SetMemPool(&DC, MemPoolSmall, false)) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302570 return -22;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302571 }
2572
2573 QCBORDecode_GetNext(&DC, &Item);
2574 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302575 return -23;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302576 }
Laurence Lundblade30816f22018-11-10 13:40:22 +07002577 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_STRING_ALLOCATE) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302578 return -24;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302579 }
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302580
2581 // ---- big bstr -----
2582 QCBORDecode_Init(&DC, BigIndefBStr, QCBOR_DECODE_MODE_NORMAL);
2583
2584 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
2585 return -25;
2586 }
2587
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05302588 if(QCBORDecode_GetNext(&DC, &Item)) {
2589 return -26;
2590 }
2591 if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.uDataAlloc) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302592 return -26;
2593 }
2594
2595 if(QCBORDecode_GetNext(&DC, &Item)) {
2596 return -27;
2597 }
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05302598 if(Item.uDataType != QCBOR_TYPE_BYTE_STRING || !Item.uDataAlloc || Item.uNestingLevel != 1) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302599 return -28;
2600 }
2601 if(CheckBigString(Item.val.string)) {
2602 return -3;
2603 }
2604 if(QCBORDecode_Finish(&DC)) {
2605 return -29;
2606 }
2607
2608 // --- label is an indefinite length string ------
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002609 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringLabel), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302610
2611 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
2612 return -30;
2613 }
2614
2615 QCBORDecode_GetNext(&DC, &Item);
2616 if(Item.uDataType != QCBOR_TYPE_MAP) {
2617 return -31;
2618 }
2619
2620 if(QCBORDecode_GetNext(&DC, &Item)){
2621 return -32;
2622 }
2623 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || Item.uDataType != QCBOR_TYPE_INT64 ||
2624 Item.uDataAlloc || !Item.uLabelAlloc ||
2625 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("struuming"))) {
2626 return -33;
2627 }
2628
2629 if(QCBORDecode_Finish(&DC)) {
2630 return -34;
2631 }
2632
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002633 return 0;
2634}
2635
2636
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302637int AllocAllStringsTest()
2638{
2639 QCBORDecodeContext DC;
2640
2641 // First test, use the "CSRMap" as easy input and checking
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002642 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302643
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05302644 UsefulBuf_MAKE_STACK_UB(Pool, 300);
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302645
2646 QCBORDecode_SetMemPool(&DC, Pool, 1); // Turn on copying.
2647
2648 if(CheckCSRMaps(&DC)) {
2649 return -1;
2650 }
2651
2652 // Next parse, save pointers to a few strings, destroy original and see all is OK.
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05302653 UsefulBuf_MAKE_STACK_UB(CopyOfStorage, 160);
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08002654 const UsefulBufC CopyOf = UsefulBuf_Copy(CopyOfStorage, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded));
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002655
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302656 QCBORDecode_Init(&DC, CopyOf, QCBOR_DECODE_MODE_NORMAL);
2657 QCBORDecode_SetMemPool(&DC, Pool, 1); // Turn on copying.
2658
2659 int nCBORError;
2660 QCBORItem Item1, Item2, Item3, Item4;
2661 if((nCBORError = QCBORDecode_GetNext(&DC, &Item1)))
2662 return nCBORError;
2663 if(Item1.uDataType != QCBOR_TYPE_MAP ||
2664 Item1.val.uCount != 3)
2665 return -1;
2666 if((nCBORError = QCBORDecode_GetNext(&DC, &Item1)))
2667 return nCBORError;
2668 if((nCBORError = QCBORDecode_GetNext(&DC, &Item2)))
2669 return nCBORError;
2670 if((nCBORError = QCBORDecode_GetNext(&DC, &Item3)))
2671 return nCBORError;
2672 if((nCBORError = QCBORDecode_GetNext(&DC, &Item4)))
2673 return nCBORError;
2674
Laurence Lundblade05ec57b2018-10-21 01:50:03 +05302675 UsefulBuf_Set(CopyOfStorage, '_');
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302676
2677 if(Item1.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302678 Item1.uDataType != QCBOR_TYPE_INT64 ||
2679 Item1.val.int64 != 42 ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09002680 UsefulBuf_Compare(Item1.label.string, UsefulBuf_FromSZ("first integer"))) {
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302681 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09002682 }
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302683
2684
2685 if(Item2.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09002686 UsefulBuf_Compare(Item2.label.string, UsefulBuf_FromSZ("an array of two strings")) ||
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302687 Item2.uDataType != QCBOR_TYPE_ARRAY ||
2688 Item2.val.uCount != 2)
2689 return -1;
2690
2691 if(Item3.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09002692 UsefulBuf_Compare(Item3.val.string, UsefulBuf_FromSZ("string1"))) {
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302693 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09002694 }
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302695
2696 if(Item4.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09002697 UsefulBuf_Compare(Item4.val.string, UsefulBuf_FromSZ("string2"))) {
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302698 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09002699 }
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302700
2701 // Next parse with a pool that is too small
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05302702 UsefulBuf_MAKE_STACK_UB(SmallPool, 80);
2703 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302704 QCBORDecode_SetMemPool(&DC, SmallPool, 1); // Turn on copying.
2705 if((nCBORError = QCBORDecode_GetNext(&DC, &Item1)))
2706 return nCBORError;
2707 if(Item1.uDataType != QCBOR_TYPE_MAP ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09002708 Item1.val.uCount != 3) {
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302709 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09002710 }
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302711 if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item1))){
2712 if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item2))) {
2713 if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item3))) {
2714 nCBORError = QCBORDecode_GetNext(&DC, &Item4);
2715 }
2716 }
2717 }
Laurence Lundblade30816f22018-11-10 13:40:22 +07002718 if(nCBORError != QCBOR_ERR_STRING_ALLOCATE) {
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302719 return -5;
2720 }
2721
2722 return 0;
2723}
2724
Laurence Lundbladef6531662018-12-04 10:42:22 +09002725// Cheating declaration to get to the special test hook
2726size_t MemPoolTestHook_GetPoolSize(void *ctx);
2727
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302728
2729int MemPoolTest(void)
Laurence Lundblade0155b622018-10-12 20:04:37 +08002730{
Laurence Lundbladef6531662018-12-04 10:42:22 +09002731 // Set up the decoder with a tiny bit of CBOR to parse
2732 QCBORDecodeContext DC;
2733 const uint8_t pMinimalCBOR[] = {0xa0}; // One empty map
2734 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pMinimalCBOR),0);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002735
Laurence Lundbladef6531662018-12-04 10:42:22 +09002736 // Set up an memory pool of 100 bytes
2737 UsefulBuf_MAKE_STACK_UB(Pool, 100);
2738 QCBORDecode_SetMemPool(&DC, Pool, 0);
2739
2740 // Cheat a little to get to the string allocator object
2741 // so we can call it directly to test it
2742 QCBORStringAllocator *pAlloc = (QCBORStringAllocator *)DC.pStringAllocator;
2743 // Cheat some more to know exactly the
2744 size_t uAvailPool = MemPoolTestHook_GetPoolSize(pAlloc);
2745
2746 // First test -- ask for too much in one go
2747 UsefulBuf Allocated = (*pAlloc->fAllocate)(pAlloc->pAllocaterContext, NULL, uAvailPool+1);
2748 if(!UsefulBuf_IsNULL(Allocated)) {
2749 return -1;
2750 }
2751
2752
2753 // Re do the set up for the next test that will do a successful alloc,
2754 // a fail, a free and then success
2755 // This test should work on 32 and 64-bit machines if the compiler
2756 // does the expected thing with pointer sizes for the internal
2757 // MemPool implementation leaving 44 or 72 bytes of pool memory.
2758 QCBORDecode_SetMemPool(&DC, Pool, 0);
2759
2760 // Cheat a little to get to the string allocator object
2761 // so we can call it directly to test it
2762 pAlloc = (QCBORStringAllocator *)DC.pStringAllocator;
2763 // Cheat some more to know exactly the
2764 uAvailPool = MemPoolTestHook_GetPoolSize(pAlloc);
2765
2766 Allocated = (*pAlloc->fAllocate)(pAlloc->pAllocaterContext, NULL, uAvailPool-1);
2767 if(UsefulBuf_IsNULL(Allocated)) { // expected to succeed
2768 return -2;
2769 }
2770 UsefulBuf Allocated2 = (*pAlloc->fAllocate)(pAlloc->pAllocaterContext, NULL, uAvailPool/2);
2771 if(!UsefulBuf_IsNULL(Allocated2)) { // expected to fail
2772 return -3;
2773 }
2774 (*pAlloc->fFree)(pAlloc->pAllocaterContext, Allocated.ptr);
2775 Allocated = (*pAlloc->fAllocate)(pAlloc->pAllocaterContext, NULL, uAvailPool/2);
2776 if(UsefulBuf_IsNULL(Allocated)) { // succeed because of the free
2777 return -4;
2778 }
2779
2780
2781 // Re do set up for next test that involves a successful alloc,
2782 // and a successful realloc and a failed realloc
2783 QCBORDecode_SetMemPool(&DC, Pool, 0);
2784
2785 // Cheat a little to get to the string allocator object
2786 // so we can call it directly to test it
2787 pAlloc = (QCBORStringAllocator *)DC.pStringAllocator;
2788 Allocated = (*pAlloc->fAllocate)(pAlloc->pAllocaterContext, NULL, uAvailPool/2);
2789 if(UsefulBuf_IsNULL(Allocated)) { // expected to succeed
2790 return -5;
2791 }
2792 Allocated2 = (*pAlloc->fAllocate)(pAlloc->pAllocaterContext, Allocated.ptr, uAvailPool);
2793 if(UsefulBuf_IsNULL(Allocated2)) {
2794 return -6;
2795 }
2796 if(Allocated2.ptr != Allocated.ptr || Allocated2.len != uAvailPool) {
2797 return -7;
2798 }
2799 UsefulBuf Allocated3 = (*pAlloc->fAllocate)(pAlloc->pAllocaterContext, Allocated.ptr, uAvailPool+1);
2800 if(!UsefulBuf_IsNULL(Allocated3)) { // expected to fail
2801 return -8;
2802 }
2803
2804 return 0;
2805}
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002806