blob: 34c8b1c21c5b83b45a4e89dc6b450832e1f129b2 [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"
35#include <stdio.h>
36#include <strings.h>
Laurence Lundblade9e3651c2018-10-10 11:49:55 +080037#include <math.h> // for fabs()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080038#include <stdlib.h>
39
40
41// TODO: test other than the normal decoder mode
42
43static void printencoded(const char *szLabel, const uint8_t *pEncoded, size_t nLen)
44{
45 if(szLabel) {
46 printf("%s ", szLabel);
47 }
48
Laurence Lundblade570fab52018-10-13 18:28:27 +080049 size_t i;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080050 for(i = 0; i < nLen; i++) {
51 uint8_t Z = pEncoded[i];
52 printf("%02x ", Z);
53 }
54 printf("\n");
55
56 fflush(stdout);
57}
58
59
Laurence Lundbladeb836efb2018-10-28 20:09:58 +070060static const uint8_t spExpectedEncodedInts[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080061 0x98, 0x2f, 0x3b, 0x7f, 0xff, 0xff, 0xff, 0xff,
62 0xff, 0xff, 0xff, 0x3b, 0x00, 0x00, 0x00, 0x01,
63 0x00, 0x00, 0x00, 0x00, 0x3a, 0xff, 0xff, 0xff,
64 0xff, 0x3a, 0xff, 0xff, 0xff, 0xfe, 0x3a, 0xff,
65 0xff, 0xff, 0xfd, 0x3a, 0x7f, 0xff, 0xff, 0xff,
66 0x3a, 0x7f, 0xff, 0xff, 0xfe, 0x3a, 0x00, 0x01,
67 0x00, 0x01, 0x3a, 0x00, 0x01, 0x00, 0x00, 0x39,
68 0xff, 0xff, 0x39, 0xff, 0xfe, 0x39, 0xff, 0xfd,
69 0x39, 0x01, 0x00, 0x38, 0xff, 0x38, 0xfe, 0x38,
70 0xfd, 0x38, 0x18, 0x37, 0x36, 0x20, 0x00, 0x00,
71 0x01, 0x16, 0x17, 0x18, 0x18, 0x18, 0x19, 0x18,
72 0x1a, 0x18, 0xfe, 0x18, 0xff, 0x19, 0x01, 0x00,
73 0x19, 0x01, 0x01, 0x19, 0xff, 0xfe, 0x19, 0xff,
74 0xff, 0x1a, 0x00, 0x01, 0x00, 0x00, 0x1a, 0x00,
75 0x01, 0x00, 0x01, 0x1a, 0x00, 0x01, 0x00, 0x02,
76 0x1a, 0x7f, 0xff, 0xff, 0xff, 0x1a, 0x7f, 0xff,
77 0xff, 0xff, 0x1a, 0x80, 0x00, 0x00, 0x00, 0x1a,
78 0x80, 0x00, 0x00, 0x01, 0x1a, 0xff, 0xff, 0xff,
79 0xfe, 0x1a, 0xff, 0xff, 0xff, 0xff, 0x1b, 0x00,
80 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x1b,
81 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01,
82 0x1b, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
83 0xff, 0x1b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
84 0xff, 0xff};
85
86
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080087// return CBOR error or -1 if type of value doesn't match
88
89static int IntegerValuesParseTestInternal(QCBORDecodeContext *pDCtx)
90{
91 QCBORItem Item;
92 int nCBORError;
93
94 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
95 return nCBORError;
96 if(Item.uDataType != QCBOR_TYPE_ARRAY)
97 return -1;
98
99 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
100 return nCBORError;
101 if(Item.uDataType != QCBOR_TYPE_INT64 || // Todo; fix this for 32-bit machines
Laurence Lundblade570fab52018-10-13 18:28:27 +0800102 Item.val.int64 != -9223372036854775807LL - 1)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800103 return -1;
104
105 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
106 return nCBORError;
107 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade570fab52018-10-13 18:28:27 +0800108 Item.val.int64 != -4294967297)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800109 return -1;
110
111 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
112 return nCBORError;
113 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade570fab52018-10-13 18:28:27 +0800114 Item.val.int64 != -4294967296)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800115 return -1;
116
117 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
118 return nCBORError;
119 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade570fab52018-10-13 18:28:27 +0800120 Item.val.int64 != -4294967295)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800121 return -1;
122
123 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
124 return nCBORError;
125 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade570fab52018-10-13 18:28:27 +0800126 Item.val.int64 != -4294967294)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800127 return -1;
128
129
130 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
131 return nCBORError;
132 if(Item.uDataType != QCBOR_TYPE_INT64 ||
133 Item.val.int64 != -2147483648)
134 return -1;
135
136 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
137 return nCBORError;
138 if(Item.uDataType != QCBOR_TYPE_INT64 ||
139 Item.val.int64 != -2147483647)
140 return -1;
141
142 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
143 return nCBORError;
144 if(Item.uDataType != QCBOR_TYPE_INT64 ||
145 Item.val.int64 != -65538)
146 return -1;
147
148 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
149 return nCBORError;
150 if(Item.uDataType != QCBOR_TYPE_INT64 ||
151 Item.val.int64 != -65537)
152 return -1;
153
154 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
155 return nCBORError;
156 if(Item.uDataType != QCBOR_TYPE_INT64 ||
157 Item.val.int64 != -65536)
158 return -1;
159
160
161 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
162 return nCBORError;
163 if(Item.uDataType != QCBOR_TYPE_INT64 ||
164 Item.val.int64 != -65535)
165 return -1;
166
167
168 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
169 return nCBORError;
170 if(Item.uDataType != QCBOR_TYPE_INT64 ||
171 Item.val.int64 != -65534)
172 return -1;
173
174
175 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
176 return nCBORError;
177 if(Item.uDataType != QCBOR_TYPE_INT64 ||
178 Item.val.int64 != -257)
179 return -1;
180
181 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
182 return nCBORError;
183 if(Item.uDataType != QCBOR_TYPE_INT64 ||
184 Item.val.int64 != -256)
185 return -1;
186
187 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
188 return nCBORError;
189 if(Item.uDataType != QCBOR_TYPE_INT64 ||
190 Item.val.int64 != -255)
191 return -1;
192
193 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
194 return nCBORError;
195 if(Item.uDataType != QCBOR_TYPE_INT64 ||
196 Item.val.int64 != -254)
197 return -1;
198
199
200 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
201 return nCBORError;
202 if(Item.uDataType != QCBOR_TYPE_INT64 ||
203 Item.val.int64 != -25)
204 return -1;
205
206
207 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
208 return nCBORError;
209 if(Item.uDataType != QCBOR_TYPE_INT64 ||
210 Item.val.int64 != -24)
211 return -1;
212
213
214 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
215 return nCBORError;
216 if(Item.uDataType != QCBOR_TYPE_INT64 ||
217 Item.val.int64 != -23)
218 return -1;
219
220
221 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
222 return nCBORError;
223 if(Item.uDataType != QCBOR_TYPE_INT64 ||
224 Item.val.int64 != -1)
225 return -1;
226
227
228 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
229 return nCBORError;
230 if(Item.uDataType != QCBOR_TYPE_INT64 ||
231 Item.val.int64 != 0)
232 return -1;
233
234
235 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
236 return nCBORError;
237 if(Item.uDataType != QCBOR_TYPE_INT64 ||
238 Item.val.int64 != 0)
239 return -1;
240
241 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
242 return nCBORError;
243 if(Item.uDataType != QCBOR_TYPE_INT64 ||
244 Item.val.int64 != 1)
245 return -1;
246
247
248 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
249 return nCBORError;
250 if(Item.uDataType != QCBOR_TYPE_INT64 ||
251 Item.val.int64 != 22)
252 return -1;
253
254
255 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
256 return nCBORError;
257 if(Item.uDataType != QCBOR_TYPE_INT64 ||
258 Item.val.int64 != 23)
259 return -1;
260
261
262 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
263 return nCBORError;
264 if(Item.uDataType != QCBOR_TYPE_INT64 ||
265 Item.val.int64 != 24)
266 return -1;
267
268
269 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
270 return nCBORError;
271 if(Item.uDataType != QCBOR_TYPE_INT64 ||
272 Item.val.int64 != 25)
273 return -1;
274
275 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
276 return nCBORError;
277 if(Item.uDataType != QCBOR_TYPE_INT64 ||
278 Item.val.int64 != 26)
279 return -1;
280
281
282 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
283 return nCBORError;
284 if(Item.uDataType != QCBOR_TYPE_INT64 ||
285 Item.val.int64 != 254)
286 return -1;
287
288
289 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
290 return nCBORError;
291 if(Item.uDataType != QCBOR_TYPE_INT64 ||
292 Item.val.int64 != 255)
293 return -1;
294
295
296 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
297 return nCBORError;
298 if(Item.uDataType != QCBOR_TYPE_INT64 ||
299 Item.val.int64 != 256)
300 return -1;
301
302
303 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
304 return nCBORError;
305 if(Item.uDataType != QCBOR_TYPE_INT64 ||
306 Item.val.int64 != 257)
307 return -1;
308
309 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
310 return nCBORError;
311 if(Item.uDataType != QCBOR_TYPE_INT64 ||
312 Item.val.int64 != 65534)
313 return -1;
314
315
316 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
317 return nCBORError;
318 if(Item.uDataType != QCBOR_TYPE_INT64 ||
319 Item.val.int64 != 65535)
320 return -1;
321
322
323 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
324 return nCBORError;
325 if(Item.uDataType != QCBOR_TYPE_INT64 ||
326 Item.val.int64 != 65536)
327 return -1;
328
329 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
330 return nCBORError;
331 if(Item.uDataType != QCBOR_TYPE_INT64 ||
332 Item.val.int64 != 65537)
333 return -1;
334
335 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
336 return nCBORError;
337 if(Item.uDataType != QCBOR_TYPE_INT64 ||
338 Item.val.int64 != 65538)
339 return -1;
340
341 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
342 return nCBORError;
343 if(Item.uDataType != QCBOR_TYPE_INT64 ||
344 Item.val.int64 != 2147483647)
345 return -1;
346
347 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
348 return nCBORError;
349 if(Item.uDataType != QCBOR_TYPE_INT64 ||
350 Item.val.int64 != 2147483647)
351 return -1;
352
353 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
354 return nCBORError;
355 if(Item.uDataType != QCBOR_TYPE_INT64 ||
356 Item.val.int64 != 2147483648)
357 return -1;
358
359 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
360 return nCBORError;
361 if(Item.uDataType != QCBOR_TYPE_INT64 ||
362 Item.val.int64 != 2147483649)
363 return -1;
364
365 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
366 return nCBORError;
367 if(Item.uDataType != QCBOR_TYPE_INT64 ||
368 Item.val.int64 != 4294967294)
369 return -1;
370
371
372 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
373 return nCBORError;
374 if(Item.uDataType != QCBOR_TYPE_INT64 ||
375 Item.val.int64 != 4294967295)
376 return -1;
377
378
379 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
380 return nCBORError;
381 if(Item.uDataType != QCBOR_TYPE_INT64 ||
382 Item.val.int64 != 4294967296)
383 return -1;
384
385
386 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
387 return nCBORError;
388 if(Item.uDataType != QCBOR_TYPE_INT64 ||
389 Item.val.int64 != 4294967297)
390 return -1;
391
392
393
394 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
395 return nCBORError;
396 if(Item.uDataType != QCBOR_TYPE_INT64 ||
397 Item.val.int64 != 9223372036854775807LL)
398 return -1;
399
400
401 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
402 return nCBORError;
403 if(Item.uDataType != QCBOR_TYPE_UINT64 ||
404 Item.val.uint64 != 18446744073709551615ULL)
405 return -1;
406
407
408 if(QCBORDecode_Finish(pDCtx) != QCBOR_SUCCESS) {
409 return -1;
410 }
411
412 return 0;
413}
414
415
416/*
417 Tests the decoding of lots of different integers sizes
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +0800418 and values.
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800419 */
420
421int IntegerValuesParseTest()
422{
423 int n;
424 QCBORDecodeContext DCtx;
425
Laurence Lundbladeb836efb2018-10-28 20:09:58 +0700426 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedEncodedInts), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800427
428 n = IntegerValuesParseTestInternal(&DCtx);
429
430 return(n);
431}
432
433
434/*
435 Creates a simple CBOR array and returns it in *pEncoded. The array is malloced
436 and needs to be freed. This is used by several tests.
437
438 Two of the inputs can be set. Two other items in the array are fixed.
439
440 */
441
442static int CreateSimpleArray(int nInt1, int nInt2, uint8_t **pEncoded, size_t *pEncodedLen)
443{
444 QCBOREncodeContext ECtx;
445 int nReturn = -1;
446
447 *pEncoded = NULL;
448 *pEncodedLen = INT32_MAX;
449
450 // loop runs CBOR encoding twice. First with no buffer to
451 // calucate the length so buffer can be allocated correctly,
452 // and last with the buffer to do the actual encoding
453 do {
Laurence Lundblade0595e932018-11-02 22:22:47 +0700454 QCBOREncode_Init(&ECtx, (UsefulBuf){*pEncoded, *pEncodedLen});
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800455 QCBOREncode_OpenArray(&ECtx);
456 QCBOREncode_AddInt64(&ECtx, nInt1);
457 QCBOREncode_AddInt64(&ECtx, nInt2);
458 QCBOREncode_AddBytes(&ECtx, ((UsefulBufC) {"galactic", 8}));
459 QCBOREncode_AddBytes(&ECtx, ((UsefulBufC) {"haven token", 11}));
460 QCBOREncode_CloseArray(&ECtx);
461
Laurence Lundblade0595e932018-11-02 22:22:47 +0700462 UsefulBufC Encoded;
463 if(QCBOREncode_Finish(&ECtx, &Encoded))
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800464 goto Done;
465
466 if(*pEncoded != NULL) {
Laurence Lundblade0595e932018-11-02 22:22:47 +0700467 *pEncodedLen = Encoded.len;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800468 nReturn = 0;
469 goto Done;
470 }
Laurence Lundblade0595e932018-11-02 22:22:47 +0700471 *pEncoded = malloc(Encoded.len);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800472 if(*pEncoded == NULL) {
473 nReturn = -1;
474 goto Done;
475 }
476
477 } while(1);
478Done:
479 return (nReturn);
480
481}
482
483
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800484/*
485 {"first integer": 42,
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900486 "an array of two strings": [
487 "string1", "string2"
488 ],
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800489 "map in a map": {
490 "bytes 1": h'78787878',
491 "bytes 2": h'79797979',
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900492 "another int": 98,
493 "text 2": "lies, damn lies and statistics"
494 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800495 }
496 */
497
498static uint8_t pValidMapEncoded[] = {
Laurence Lundbladeb836efb2018-10-28 20:09:58 +0700499 0xa3, 0x6d, 0x66, 0x69, 0x72, 0x73, 0x74, 0x20, 0x69, 0x6e,
500 0x74, 0x65, 0x67, 0x65, 0x72, 0x18, 0x2a, 0x77, 0x61, 0x6e,
501 0x20, 0x61, 0x72, 0x72, 0x61, 0x79, 0x20, 0x6f, 0x66, 0x20,
502 0x74, 0x77, 0x6f, 0x20, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67,
503 0x73, 0x82, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x31,
504 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x32, 0x6c, 0x6d,
505 0x61, 0x70, 0x20, 0x69, 0x6e, 0x20, 0x61, 0x20, 0x6d, 0x61,
506 0x70, 0xa4, 0x67, 0x62, 0x79, 0x74, 0x65, 0x73, 0x20, 0x31,
507 0x44, 0x78, 0x78, 0x78, 0x78, 0x67, 0x62, 0x79, 0x74, 0x65,
508 0x73, 0x20, 0x32, 0x44, 0x79, 0x79, 0x79, 0x79, 0x6b, 0x61,
509 0x6e, 0x6f, 0x74, 0x68, 0x65, 0x72, 0x20, 0x69, 0x6e, 0x74,
510 0x18, 0x62, 0x66, 0x74, 0x65, 0x78, 0x74, 0x20, 0x32, 0x78,
511 0x1e, 0x6c, 0x69, 0x65, 0x73, 0x2c, 0x20, 0x64, 0x61, 0x6d,
512 0x6e, 0x20, 0x6c, 0x69, 0x65, 0x73, 0x20, 0x61, 0x6e, 0x64,
513 0x20, 0x73, 0x74, 0x61, 0x74, 0x69, 0x73, 0x74, 0x69, 0x63,
514 0x73 } ;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800515
516static 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)
517{
518 QCBORDecodeContext DCtx;
519 QCBORItem Item;
520 int nReturn = -1; // assume error until success
521
522 QCBORDecode_Init(&DCtx, (UsefulBufC){pEncoded, nLen}, QCBOR_DECODE_MODE_NORMAL);
523
524 // Make sure the first thing is a map
525 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || Item.uDataType != QCBOR_TYPE_ARRAY)
526 goto Done;
527
528 // First integer
529 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 | Item.uDataType != QCBOR_TYPE_INT64)
530 goto Done;
531 *pInt1 = Item.val.int64;
532
533 // Second integer
534 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || Item.uDataType != QCBOR_TYPE_INT64)
535 goto Done;
536 *pInt2 = Item.val.int64;
537
538 // First string
539 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || Item.uDataType != QCBOR_TYPE_BYTE_STRING)
540 goto Done;
541 *pBuf3 = Item.val.string.ptr;
542 *pBuf3Len = Item.val.string.len;
543
544 // Second string
545 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || Item.uDataType != QCBOR_TYPE_BYTE_STRING)
546 goto Done;
547 *pBuf4 = Item.val.string.ptr;
548 *pBuf4Len = Item.val.string.len;
549
550 nReturn = 0;
551
552Done:
553 return(nReturn);
554}
555
556
557
Laurence Lundblade9e3651c2018-10-10 11:49:55 +0800558
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800559int SimpleArrayTest()
560{
561 uint8_t *pEncoded;
562 size_t nEncodedLen;
563
564 int64_t i1, i2;
565 size_t i3, i4;
566 const uint8_t *s3, *s4;
567
568
569 if(CreateSimpleArray(23, 6000, &pEncoded, &nEncodedLen) < 0) {
570 return(-1);
571 }
572
573 ParseOrderedArray(pEncoded, nEncodedLen, &i1, &i2, &s3, &i3, &s4, &i4);
574
575 if(i1 != 23 ||
576 i2 != 6000 ||
577 i3 != 8 ||
578 i4 != 11 ||
Laurence Lundbladea44d5062018-10-17 18:45:12 +0530579 memcmp("galactic", s3, 8) !=0 ||
580 memcmp("haven token", s4, 11) !=0) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800581 printf("SimpleArraryTest Failed\n");
582 return(-1);
583 }
584
585 return(0);
586}
587
588
Laurence Lundblade9e3651c2018-10-10 11:49:55 +0800589
Laurence Lundbladeb836efb2018-10-28 20:09:58 +0700590static uint8_t spDeepArrays[] = {0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x80};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800591
592int ParseDeepArrayTest()
593{
594 QCBORDecodeContext DCtx;
595 int nReturn = 0;
596 int i;
597
Laurence Lundbladeb836efb2018-10-28 20:09:58 +0700598 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spDeepArrays), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800599
600 for(i = 0; i < 10; i++) {
601 QCBORItem Item;
602
603 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
604 Item.uDataType != QCBOR_TYPE_ARRAY ||
605 Item.uNestingLevel != i) {
606 nReturn = -1;
607 break;
608 }
609 }
610
611 return(nReturn);
612}
613
Laurence Lundblade972e59c2018-11-11 15:57:23 +0700614// Big enough to test nesting to the depth of 24
615static uint8_t spTooDeepArrays[] = {0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81,
616 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81,
617 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81,
618 0x80};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800619
620int ParseTooDeepArrayTest()
621{
622 QCBORDecodeContext DCtx;
623 int nReturn = 0;
624 int i;
625 QCBORItem Item;
626
627
Laurence Lundbladeb836efb2018-10-28 20:09:58 +0700628 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTooDeepArrays), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800629
Laurence Lundblade972e59c2018-11-11 15:57:23 +0700630 for(i = 0; i < QCBOR_MAX_ARRAY_NESTING1; i++) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800631
632 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
633 Item.uDataType != QCBOR_TYPE_ARRAY ||
634 Item.uNestingLevel != i) {
635 nReturn = -1;
636 break;
637 }
638 }
639
640 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_ARRAY_NESTING_TOO_DEEP)
641 nReturn = -1;
642
643 return(nReturn);
644}
645
646
647
648
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800649int ShortBufferParseTest()
650{
651 int nResult = 0;
652 QCBORDecodeContext DCtx;
653 int num;
654
Laurence Lundbladeb836efb2018-10-28 20:09:58 +0700655 for(num = sizeof(spExpectedEncodedInts)-1; num; num--) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800656 int n;
657
Laurence Lundbladeb836efb2018-10-28 20:09:58 +0700658 QCBORDecode_Init(&DCtx, (UsefulBufC){spExpectedEncodedInts, num}, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800659
660 n = IntegerValuesParseTestInternal(&DCtx);
661
662 //printf("Len %d, result: %d\n", num, n);
663
664 if(n != QCBOR_ERR_HIT_END) {
665 nResult = -1;
666 goto Done;
667 }
668 }
669Done:
670 return nResult;
671}
672
673
Laurence Lundblade9e3651c2018-10-10 11:49:55 +0800674
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800675int ShortBufferParseTest2()
676{
677 uint8_t *pEncoded;
678 int nReturn;
679 size_t nEncodedLen;
680
681 int64_t i1, i2;
682 size_t i3, i4;
683 const uint8_t *s3, *s4;
684
685 nReturn = 0;
686
687 if(CreateSimpleArray(23, 6000, &pEncoded, &nEncodedLen) < 0) {
688 return(-1);
689 }
690
691 //printencoded(pEncoded, nEncodedLen);
692
693 for(nEncodedLen--; nEncodedLen; nEncodedLen--) {
694 int nResult = ParseOrderedArray(pEncoded, (uint32_t)nEncodedLen, &i1, &i2, &s3, &i3, &s4, &i4);
695 if(nResult == 0) {
696 nReturn = -1;
697 }
698 }
699
700 return(nReturn);
701}
702
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530703/*
704 Decode and thoroughly check a moderately complex
705 set of maps
706 */
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800707static int ParseMapTest1()
708{
709 QCBORDecodeContext DCtx;
710 QCBORItem Item;
711 int nCBORError;
712
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800713 QCBORDecode_Init(&DCtx, (UsefulBufC){pValidMapEncoded, sizeof(pValidMapEncoded)}, QCBOR_DECODE_MODE_NORMAL);
714
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900715 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800716 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900717 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800718 if(Item.uDataType != QCBOR_TYPE_MAP ||
719 Item.val.uCount != 3)
720 return -1;
721
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900722 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800723 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900724 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800725 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800726 Item.uDataType != QCBOR_TYPE_INT64 ||
727 Item.val.int64 != 42 ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530728 Item.uDataAlloc ||
729 Item.uLabelAlloc ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900730 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("first integer"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800731 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900732 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800733
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900734 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800735 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900736 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800737 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530738 Item.uDataAlloc ||
739 Item.uLabelAlloc ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900740 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("an array of two strings")) ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800741 Item.uDataType != QCBOR_TYPE_ARRAY ||
742 Item.val.uCount != 2)
743 return -1;
744
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900745 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800746 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900747 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800748 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530749 Item.uDataAlloc ||
750 Item.uLabelAlloc ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900751 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("string1"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800752 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900753 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800754
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900755 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800756 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900757 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800758 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530759 Item.uDataAlloc ||
760 Item.uLabelAlloc ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900761 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("string2"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800762 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900763 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800764
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900765 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800766 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900767 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800768 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530769 Item.uDataAlloc ||
770 Item.uLabelAlloc ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900771 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("map in a map")) ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800772 Item.uDataType != QCBOR_TYPE_MAP ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900773 Item.val.uCount != 4) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800774 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900775 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800776
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900777 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800778 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900779 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800780 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900781 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("bytes 1"))||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800782 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530783 Item.uDataAlloc ||
784 Item.uLabelAlloc ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900785 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("xxxx"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800786 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900787 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800788
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900789 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800790 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900791 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800792 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900793 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("bytes 2")) ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800794 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530795 Item.uDataAlloc ||
796 Item.uLabelAlloc ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900797 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("yyyy"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800798 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900799 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800800
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900801 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800802 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900803 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800804 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530805 Item.uDataAlloc ||
806 Item.uLabelAlloc ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900807 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("another int")) ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800808 Item.uDataType != QCBOR_TYPE_INT64 ||
809 Item.val.int64 != 98)
810 return -1;
811
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900812 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800813 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900814 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800815 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900816 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("text 2"))||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800817 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530818 Item.uDataAlloc ||
819 Item.uLabelAlloc ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900820 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("lies, damn lies and statistics"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800821 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900822 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800823
824 return 0;
825}
826
827
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900828/*
829 Decode and thoroughly check a moderately complex
830 set of maps
831 */
832int ParseMapAsArrayTest()
833{
834 QCBORDecodeContext DCtx;
835 QCBORItem Item;
836 int nCBORError;
837
838 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), QCBOR_DECODE_MODE_MAP_AS_ARRAY);
839
840 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
841 return nCBORError;
842 }
Laurence Lundbladed61cbf32018-12-09 11:42:21 -0800843 if(Item.uDataType != QCBOR_TYPE_MAP_AS_ARRAY ||
844 Item.val.uCount != 6) {
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900845 return -1;
846 }
847
848 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
849 return nCBORError;
850 }
851 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
852 Item.uDataAlloc ||
853 Item.uLabelAlloc ||
854 Item.uLabelType != QCBOR_TYPE_NONE ||
855 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("first integer"))) {
856 return -2;
857 }
858
859 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
860 return nCBORError;
861 }
862 if(Item.uLabelType != QCBOR_TYPE_NONE ||
863 Item.uDataType != QCBOR_TYPE_INT64 ||
864 Item.val.int64 != 42 ||
865 Item.uDataAlloc ||
866 Item.uLabelAlloc) {
867 return -3;
868 }
869
870 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
871 return nCBORError;
872 }
873 if(Item.uLabelType != QCBOR_TYPE_NONE ||
874 Item.uDataAlloc ||
875 Item.uLabelAlloc ||
876 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("an array of two strings")) ||
877 Item.uDataType != QCBOR_TYPE_TEXT_STRING) {
878 return -4;
879 }
880
881 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
882 return nCBORError;
883 }
884 if(Item.uLabelType != QCBOR_TYPE_NONE ||
885 Item.uDataAlloc ||
886 Item.uLabelAlloc ||
887 Item.uDataType != QCBOR_TYPE_ARRAY ||
888 Item.val.uCount != 2) {
889 return -5;
890 }
891
892 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
893 return nCBORError;
894 }
895 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
896 Item.val.string.len != 7 ||
897 Item.uDataAlloc ||
898 Item.uLabelAlloc ||
899 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("string1"))) {
900 return -6;
901 }
902
903 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
904 return nCBORError;
905 }
906 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
907 Item.uDataAlloc ||
908 Item.uLabelAlloc ||
909 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("string2"))) {
910 return -7;
911 }
912
913
914 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
915 return nCBORError;
916 }
917 if(Item.uLabelType != QCBOR_TYPE_NONE ||
918 Item.uDataAlloc ||
919 Item.uLabelAlloc ||
920 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("map in a map"))) {
921 return -8;
922 }
923
924 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
925 return nCBORError;
926 }
927 if(Item.uLabelType != QCBOR_TYPE_NONE ||
928 Item.uDataAlloc ||
929 Item.uLabelAlloc ||
Laurence Lundbladed61cbf32018-12-09 11:42:21 -0800930 Item.uDataType != QCBOR_TYPE_MAP_AS_ARRAY ||
931 Item.val.uCount != 8) {
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900932 return -9;
933 }
934
935 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
936 return nCBORError;
937 }
938 if(Item.uLabelType != QCBOR_TYPE_NONE ||
939 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("bytes 1"))||
940 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
941 Item.uDataAlloc ||
942 Item.uLabelAlloc) {
943 return -10;
944 }
945
946 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
947 return nCBORError;
948 }
949 if(Item.uLabelType != QCBOR_TYPE_NONE ||
950 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
951 Item.uDataAlloc ||
952 Item.uLabelAlloc ||
953 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("xxxx"))) {
954 return -11;
955 }
956
957 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
958 return nCBORError;
959 }
960 if(Item.uLabelType != QCBOR_TYPE_NONE ||
961 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("bytes 2")) ||
962 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
963 Item.uDataAlloc ||
964 Item.uLabelAlloc) {
965 return -12;
966 }
967
968 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
969 return nCBORError;
970 }
971 if(Item.uLabelType != QCBOR_TYPE_NONE ||
972 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
973 Item.uDataAlloc ||
974 Item.uLabelAlloc ||
975 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("yyyy"))) {
976 return -13;
977 }
978
979 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
980 return nCBORError;
981 }
982 if(Item.uLabelType != QCBOR_TYPE_NONE ||
983 Item.uDataAlloc ||
984 Item.uLabelAlloc ||
985 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("another int")) ||
986 Item.uDataType != QCBOR_TYPE_TEXT_STRING) {
987 return -14;
988 }
989
990 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
991 return nCBORError;
992 }
993 if(Item.uLabelType != QCBOR_TYPE_NONE ||
994 Item.uDataAlloc ||
995 Item.uLabelAlloc ||
996 Item.uDataType != QCBOR_TYPE_INT64 ||
997 Item.val.int64 != 98) {
998 return -15;
999 }
1000
1001 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
1002 return nCBORError;
1003 }
1004 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1005 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("text 2"))||
1006 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1007 Item.uDataAlloc ||
1008 Item.uLabelAlloc) {
1009 return -16;
1010 }
1011
1012 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
1013 return nCBORError;
1014 }
1015 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1016 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1017 Item.uDataAlloc ||
1018 Item.uLabelAlloc ||
1019 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("lies, damn lies and statistics"))) {
1020 return -17;
1021 }
1022
1023 return 0;
1024}
1025
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001026
1027/*
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301028 Fully or partially decode pValidMapEncoded. When
1029 partially decoding check for the right error code.
1030 How much partial decoding depends on nLevel.
1031
1032 The partial decodes test error conditions of
1033 incomplete encoded input.
1034
1035 This could be combined with the above test
1036 and made prettier and maybe a little more
1037 thorough.
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001038 */
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001039static int ExtraBytesTest(int nLevel)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001040{
1041 QCBORDecodeContext DCtx;
1042 QCBORItem Item;
1043 int nCBORError;
1044
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001045 QCBORDecode_Init(&DCtx, (UsefulBufC){pValidMapEncoded, sizeof(pValidMapEncoded)}, QCBOR_DECODE_MODE_NORMAL);
1046
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001047 if(nLevel < 1) {
1048 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_EXTRA_BYTES) {
1049 return -1;
1050 } else {
1051 return 0;
1052 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001053 }
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301054
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001055
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001056 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001057 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001058 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001059 if(Item.uDataType != QCBOR_TYPE_MAP ||
1060 Item.val.uCount != 3)
1061 return -1;
1062
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001063 if(nLevel < 2) {
1064 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
1065 return -1;
1066 } else {
1067 return 0;
1068 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001069 }
1070
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001071
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001072 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001073 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001074 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001075 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001076 Item.uDataType != QCBOR_TYPE_INT64 ||
1077 Item.val.uCount != 42 ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001078 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("first integer"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001079 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001080 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001081
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001082 if(nLevel < 3) {
1083 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
1084 return -1;
1085 } else {
1086 return 0;
1087 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001088 }
1089
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001090 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001091 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001092 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001093 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001094 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("an array of two strings")) ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001095 Item.uDataType != QCBOR_TYPE_ARRAY ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001096 Item.val.uCount != 2) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001097 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001098 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001099
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001100
1101 if(nLevel < 4) {
1102 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
1103 return -1;
1104 } else {
1105 return 0;
1106 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001107 }
1108
1109
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001110 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001111 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001112 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001113 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001114 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("string1"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001115 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001116 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001117
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001118 if(nLevel < 5) {
1119 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
1120 return -1;
1121 } else {
1122 return 0;
1123 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001124 }
1125
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001126 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001127 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001128 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001129 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001130 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("string2"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001131 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001132 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001133
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001134 if(nLevel < 6) {
1135 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
1136 return -1;
1137 } else {
1138 return 0;
1139 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001140 }
1141
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001142 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001143 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001144 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001145 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001146 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("map in a map")) ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001147 Item.uDataType != QCBOR_TYPE_MAP ||
1148 Item.val.uCount != 4)
1149 return -1;
1150
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001151 if(nLevel < 7) {
1152 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
1153 return -1;
1154 } else {
1155 return 0;
1156 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001157 }
1158
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001159 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001160 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001161 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001162 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001163 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("bytes 1")) ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001164 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001165 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("xxxx"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001166 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001167 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001168
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001169 if(nLevel < 8) {
1170 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
1171 return -1;
1172 } else {
1173 return 0;
1174 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001175 }
1176
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001177 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001178 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001179 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001180 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001181 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("bytes 2")) ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001182 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001183 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("yyyy"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001184 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001185 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001186
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001187 if(nLevel < 9) {
1188 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
1189 return -1;
1190 } else {
1191 return 0;
1192 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001193 }
1194
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001195 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001196 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001197 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001198 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001199 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("another int")) ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001200 Item.uDataType != QCBOR_TYPE_INT64 ||
1201 Item.val.int64 != 98)
1202 return -1;
1203
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001204 if(nLevel < 10) {
1205 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
1206 return -1;
1207 } else {
1208 return 0;
1209 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001210 }
1211
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001212 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001213 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001214 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001215 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001216 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("text 2"))||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001217 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001218 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("lies, damn lies and statistics"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001219 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001220 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001221
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301222 if(QCBORDecode_Finish(&DCtx)) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001223 return -1;
1224 }
1225
1226 return 0;
1227}
1228
1229
1230
1231
1232int ParseMapTest()
1233{
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301234 // Parse a moderatly complex map structure very thoroughl
1235 int n = ParseMapTest1();
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001236
1237 if(!n) {
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001238 for(int i = 0; i < 10; i++) {
1239 n = ExtraBytesTest(i);
1240 if(n) {
1241 break;
1242 }
1243 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001244 }
1245
1246 return(n);
1247}
1248
1249
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07001250static 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 +08001251
1252int ParseSimpleTest()
1253{
1254 QCBORDecodeContext DCtx;
1255 QCBORItem Item;
1256 int nCBORError;
1257
1258
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07001259 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSimpleValues), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001260
1261
1262 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1263 return nCBORError;
1264 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
1265 Item.val.uCount != 10)
1266 return -1;
1267
1268 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1269 return nCBORError;
1270 if(Item.uDataType != QCBOR_TYPE_FALSE)
1271 return -1;
1272
1273 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1274 return nCBORError;
1275 if(Item.uDataType != QCBOR_TYPE_TRUE)
1276 return -1;
1277
1278 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1279 return nCBORError;
1280 if(Item.uDataType != QCBOR_TYPE_NULL)
1281 return -1;
1282
1283 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1284 return nCBORError;
1285 if(Item.uDataType != QCBOR_TYPE_UNDEF)
1286 return -1;
1287
1288 // A break
Laurence Lundblade9e3651c2018-10-10 11:49:55 +08001289 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_BREAK)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001290 return -1;
1291
1292 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1293 return nCBORError;
1294 if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 0)
1295 return -1;
1296
1297 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1298 return nCBORError;
1299 if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 19)
1300 return -1;
1301
1302 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_INVALID_CBOR)
1303 return -1;
1304
1305 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_INVALID_CBOR)
1306 return -1;
1307
1308 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_INVALID_CBOR)
1309 return -1;
1310
1311 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1312 return nCBORError;
1313 if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 32)
1314 return -1;
1315
1316 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1317 return nCBORError;
1318 if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 255)
1319 return -1;
1320
1321 return 0;
1322
1323}
1324
1325
1326struct FailInput {
1327 UsefulBufC Input;
1328 int nError;
1329};
1330
1331
1332struct FailInput Failures[] = {
1333 { {(uint8_t[]){0x18}, 1}, QCBOR_ERR_HIT_END }, // 1 byte integer missing the byte
1334 { {(uint8_t[]){0x1c}, 1}, QCBOR_ERR_UNSUPPORTED }, // Reserved additional info = 28
1335 { {(uint8_t[]){0x1d}, 1}, QCBOR_ERR_UNSUPPORTED }, // Reserved additional info = 29
1336 { {(uint8_t[]){0x1e}, 1}, QCBOR_ERR_UNSUPPORTED }, // Reserved additional info = 30
1337 { {(uint8_t[]){0x1f}, 1}, QCBOR_ERR_UNSUPPORTED }, // Indefinite length integer
1338 { {(uint8_t[]){0x3c}, 1}, QCBOR_ERR_UNSUPPORTED }, // 1 byte integer missing the byte
1339 { {(uint8_t[]){0x3d}, 1}, QCBOR_ERR_UNSUPPORTED }, // 1 byte integer missing the byte
1340 { {(uint8_t[]){0x3e}, 1}, QCBOR_ERR_UNSUPPORTED }, // 1 byte integer missing the byte
1341 { {(uint8_t[]){0x3f}, 1}, QCBOR_ERR_UNSUPPORTED }, // Indefinite length negative integer
1342 { {(uint8_t[]){0x41}, 1}, QCBOR_ERR_HIT_END }, // Short byte string
1343 { {(uint8_t[]){0x5c}, 1}, QCBOR_ERR_UNSUPPORTED }, // Reserved additional info = 28
1344 { {(uint8_t[]){0x5f}, 1}, QCBOR_ERR_UNSUPPORTED }, // Indefinite length byte string
1345 { {(uint8_t[]){0x61}, 1}, QCBOR_ERR_HIT_END }, // Short UTF-8 string
1346 { {(uint8_t[]){0x7c}, 1}, QCBOR_ERR_UNSUPPORTED }, // Reserved additional info = 28
1347 { {(uint8_t[]){0x7f}, 1}, QCBOR_ERR_UNSUPPORTED }, // Indefinite length UTF-8 string
1348 { {(uint8_t[]){0xff}, 1}, QCBOR_ERR_UNSUPPORTED } , // break
1349 { {(uint8_t[]){0xf8, 0x00}, 2}, QCBOR_ERR_INVALID_CBOR }, // An invalid encoding of a simple type
1350 { {(uint8_t[]){0xf8, 0x1f}, 2}, QCBOR_ERR_INVALID_CBOR }, // An invalid encoding of a simple type
1351 { {(uint8_t[]){0xc0, 0x00}, 2}, QCBOR_ERR_BAD_OPT_TAG }, // Text-based date, with an integer
1352 { {(uint8_t[]){0xc1, 0x41, 0x33}, 3}, QCBOR_ERR_BAD_OPT_TAG }, // Epoch date, with an byte string
1353 { {(uint8_t[]){0xc1, 0xc0, 0x00}, 3}, QCBOR_ERR_BAD_OPT_TAG }, // tagged as both epoch and string dates
1354 { {(uint8_t[]){0xc2, 0x00}, 2}, QCBOR_ERR_BAD_OPT_TAG } // big num tagged an int, not a byte string
1355
1356};
1357
1358
1359void Dump(UsefulBufC Input, int x)
1360{
1361 char label[10];
1362
1363 sprintf(label, "%d", x);
1364
1365 printencoded(label, Input.ptr, Input.len);
1366}
1367
1368
1369int FailureTests()
1370{
1371 int nResult = 0;
1372
1373 struct FailInput *pFEnd = &Failures[0] + sizeof(Failures)/sizeof(struct FailInput);
1374
1375 for(struct FailInput *pF = &Failures[0]; pF < pFEnd ;pF++) {
1376 QCBORDecodeContext DCtx;
1377 QCBORItem Item;
1378 int nCBORError;
1379
1380 QCBORDecode_Init(&DCtx, pF->Input, QCBOR_DECODE_MODE_NORMAL);
1381
1382 while(1) {
1383 nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
1384 if(QCBOR_ERR_HIT_END == nCBORError) {
1385 break;
1386 }
1387 if(nCBORError != pF->nError) {
1388 nResult = 1;
1389 // Dump(pF->Input, nCBORError);
1390 break;
1391 }
1392 }
1393 }
1394
1395 {
1396 QCBORDecodeContext DCtx;
1397 QCBORItem Item;
1398 int nCBORError;
1399
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07001400 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSimpleValues), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001401
1402 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1403 return nCBORError;
1404 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
1405 Item.val.uCount != 10)
1406 return -1;
1407
1408 DCtx.InBuf.magic = 0; // Corrupt the UsefulInputBuf
1409
1410 nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
1411 if(nCBORError != QCBOR_ERR_HIT_END)
1412 return -1;
1413 }
1414
1415
1416 return nResult;
1417}
1418
1419
1420
1421
1422static void Recurser(uint8_t *pBuf, int nLen, int nLenMax)
1423{
1424
1425 if(nLen >= nLenMax) {
1426 return;
1427 }
1428
1429 //printf("__%d__%d__\n", nLen, nLenMax);
1430
1431 for(int i = 0; i < 256; i++) {
1432 pBuf[nLen] = i;
1433
1434 QCBORDecodeContext DCtx;
1435 QCBORItem Item;
1436 int nCBORError;
1437
1438 UsefulBufC Input = {pBuf, nLen+1};
1439
1440 QCBORDecode_Init(&DCtx, Input, QCBOR_DECODE_MODE_NORMAL);
1441
1442 while(1) {
1443 nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
1444 if(QCBOR_ERR_HIT_END == nCBORError) {
1445 break;
1446 }
1447 if(nCBORError != QCBOR_SUCCESS) {
1448 if(nCBORError != QCBOR_ERR_UNSUPPORTED && nCBORError != QCBOR_ERR_HIT_END && nCBORError != QCBOR_ERR_INVALID_CBOR) {
1449 //Dump(Input, nCBORError);
1450 }
1451 break;
1452 }
1453 }
1454 //Dump(Input, -1);
1455
1456
1457 Recurser(pBuf, nLen+1, nLenMax);
1458 }
1459}
1460
1461
1462/*
1463 Runs all possible input strings of a given length. This is set to 3 to make the test
1464 run in reasonable time.
1465 Main point of this test is to not crash.
1466 */
1467
1468int ComprehensiveInputTest()
1469{
1470 uint8_t pBuf[3]; // 3 keeps it running in reasonable time. 4 takes tens of minutes.
1471
1472 Recurser(pBuf, 0, sizeof(pBuf));
1473
1474 return 0;
1475}
1476
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07001477static uint8_t spDateTestInput[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001478 0xc0, // tag for string date
1479 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string
1480
1481 0xc1, // tag for epoch date
1482 0x1a, 0x53, 0x72, 0x4E, 0x00, // Epoch date 1400000000; Tue, 13 May 2014 16:53:20 GMT
1483
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001484 // CBOR_TAG_B64
1485 0xc1, 0xcf, 0xd8, 0x22, // 0xee, // Epoch date with extra tags TODO: fix this test
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001486 0x1a, 0x53, 0x72, 0x4E, 0x01,
1487
1488 0xc1, // tag for epoch date
1489 0x1b, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, // Too large integer
1490
1491 0xc1, // tag for epoch date
1492 0xfa, 0x3f, 0x8c, 0xcc, 0xcd, // double with value 1.1
1493
1494 0xc1, // tag for epoch date
1495 0xfa, 0x7f, 0x7f, 0xff, 0xff // 3.4028234663852886e+38 too large
1496
1497};
1498
1499
1500// have to check float expected only to within an epsilon
1501int CHECK_EXPECTED_DOUBLE(double val, double expected) {
1502
1503 double diff = val - expected;
1504
1505 diff = fabs(diff);
1506
1507 return diff > 0.0000001;
1508}
1509
1510
1511int DateParseTest()
1512{
1513 QCBORDecodeContext DCtx;
1514 QCBORItem Item;
1515 int nCBORError;
1516
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07001517 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spDateTestInput), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001518
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001519 const uint64_t uTags[] = {15};
1520 QCBORTagListIn TagList = {1, uTags};
1521
1522 QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TagList);
1523
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001524 // String date
1525 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1526 return -1;
1527 if(Item.uDataType != QCBOR_TYPE_DATE_STRING ||
Laurence Lundblade9e3651c2018-10-10 11:49:55 +08001528 UsefulBuf_Compare(Item.val.dateString, UsefulBuf_FromSZ("1985-04-12"))){
Laurence Lundblade67bd5512018-11-02 21:44:06 +07001529 return -2;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001530 }
1531
1532 // Epoch date
1533 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade67bd5512018-11-02 21:44:06 +07001534 return -3;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001535 if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH ||
1536 Item.val.epochDate.nSeconds != 1400000000 ||
1537 Item.val.epochDate.fSecondsFraction != 0 ) {
Laurence Lundblade67bd5512018-11-02 21:44:06 +07001538 return -4;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001539 }
1540
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001541 // Epoch date with extra CBOR_TAG_B64 tag that doesn't really mean anything
1542 // but want to be sure extra tag doesn't cause a problem
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001543 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade67bd5512018-11-02 21:44:06 +07001544 return -5;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001545 if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH ||
1546 Item.val.epochDate.nSeconds != 1400000001 ||
1547 Item.val.epochDate.fSecondsFraction != 0 ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001548 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_B64)) {
Laurence Lundblade67bd5512018-11-02 21:44:06 +07001549 return -6;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001550 }
1551
1552 // Epoch date that is too large for our representation
1553 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_DATE_OVERFLOW) {
Laurence Lundblade67bd5512018-11-02 21:44:06 +07001554 return -7;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001555 }
1556
1557 // Epoch date in float format with fractional seconds
1558 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade67bd5512018-11-02 21:44:06 +07001559 return -8;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001560 if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH ||
1561 Item.val.epochDate.nSeconds != 1 ||
1562 CHECK_EXPECTED_DOUBLE(Item.val.epochDate.fSecondsFraction, 0.1 )) {
Laurence Lundblade67bd5512018-11-02 21:44:06 +07001563 return -9;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001564 }
1565
1566 // Epoch date float that is too large for our representation
1567 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_DATE_OVERFLOW) {
Laurence Lundblade67bd5512018-11-02 21:44:06 +07001568 return -10;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001569 }
1570
1571 // TODO: could use a few more tests with float, double, and half precsion and negative (but coverage is still pretty good)
1572
1573 return 0;
1574}
1575
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001576// Really simple basic input for tagging test
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07001577static uint8_t spOptTestInput[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001578 0xd9, 0xd9, 0xf7, // CBOR magic number
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001579 0x81, // Array of one
1580 0xd8, 0x04, // non-preferred serialization of tag 4
1581 0x82, 0x01, 0x03}; // fraction 1/3
1582
1583static uint8_t spEncodedLargeTag[] = {0xdb, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x80};
1584
1585// 0x9192939495969798, 0x88, 0x01, 0x04
1586static uint8_t spLotsOfTags[] = {0xdb, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0xd8, 0x88, 0xc5, 0xc4, 0x80};
1587
1588/*
1589 The cbor.me parse of this.
1590 55799(55799(55799({6(7(-23)): 5859837686836516696(7({7(-20): 11({17(-18): 17(17(17("Organization"))),
1591 9(-17): 773("SSG"), -15: 4(5(6(7(8(9(10(11(12(13(14(15("Confusion")))))))))))), 17(-16): 17("San Diego"),
1592 17(-14): 17("US")}), 23(-19): 19({-11: 9({-9: -7}),
1593 90599561(90599561(90599561(-10))): 12(h'0102030405060708090A')})})),
1594 16(-22): 23({11(8(7(-5))): 8(-3)})})))
1595 */
1596static uint8_t spCSRWithTags[] = {
1597 0xd9, 0xd9, 0xf7, 0xd9, 0xd9, 0xf7, 0xd9, 0xd9, 0xf7, 0xa2,
1598 0xc6, 0xc7, 0x36,
1599 0xdb, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0xc7, 0xa2,
1600 0xda, 0x00, 0x00, 0x00, 0x07, 0x33,
1601 0xcb, 0xa5,
1602 0xd1, 0x31,
1603 0xd1, 0xd1, 0xd1, 0x6c,
1604 0x4f, 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74, 0x69, 0x6f, 0x6e,
1605 0xc9, 0x30,
1606 0xd9, 0x03, 0x05, 0x63,
1607 0x53, 0x53, 0x47,
1608 0x2e,
1609 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf, 0x69,
1610 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73, 0x69, 0x6f, 0x6e,
1611 0xd1, 0x2f,
1612 0xd1, 0x69,
1613 0x53, 0x61, 0x6e, 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f,
1614 0xd1, 0x2d,
1615 0xd1, 0x62,
1616 0x55, 0x53,
1617 0xd7, 0x32,
1618 0xd3, 0xa2,
1619 0x2a,
1620 0xc9, 0xa1,
1621 0x28,
1622 0x26,
1623 0xda, 0x05, 0x66, 0x70, 0x89, 0xda, 0x05, 0x66, 0x70, 0x89, 0xda, 0x05, 0x66, 0x70, 0x89, 0x29,
1624 0xcc, 0x4a,
1625 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,0x07, 0x08, 0x09, 0x0a,
1626 0xd0, 0x35,
1627 0xd7, 0xa1,
1628 0xcb, 0xc8, 0xc7, 0x24,
1629 0xc8, 0x22};
1630
1631static int CheckCSRMaps(QCBORDecodeContext *pDC);
1632
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001633
1634int OptTagParseTest()
1635{
1636 QCBORDecodeContext DCtx;
1637 QCBORItem Item;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001638
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07001639 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spOptTestInput), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001640
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001641 //-------------------------
1642 // This text matches the magic number tag and the fraction tag
1643 if(QCBORDecode_GetNext(&DCtx, &Item)) {
1644 return -2;
1645 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001646 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001647 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC)) {
1648 return -3;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001649 }
1650
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001651 if(QCBORDecode_GetNext(&DCtx, &Item)) {
1652 return -4;
1653 }
1654 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
1655 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_FRACTION) ||
1656 Item.val.uCount != 2) {
1657 return -5;
1658 }
1659
1660 // --------------------------------
1661 // This test decodes the very large tag, but it is not in
1662 // any list so it is ignored.
1663 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEncodedLargeTag), QCBOR_DECODE_MODE_NORMAL);
1664 if(QCBORDecode_GetNext(&DCtx, &Item)) {
1665 return -6;
1666 }
1667 if(Item.uTagBits) {
1668 return -7;
1669 }
1670
1671 // ----------------------------------
1672 // This test sets up a caller-config list that includes the very large tage and then matches it.
1673 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEncodedLargeTag), QCBOR_DECODE_MODE_NORMAL);
1674 const uint64_t puList[] = {0x9192939495969798, 257};
1675 const QCBORTagListIn TL = {2, puList};
1676 QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TL);
1677
1678 if(QCBORDecode_GetNext(&DCtx, &Item)) {
1679 return -8;
1680 }
1681 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
1682 !QCBORDecode_IsTagged(&DCtx, &Item, 0x9192939495969798) ||
1683 QCBORDecode_IsTagged(&DCtx, &Item, 257) ||
1684 QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_BIGFLOAT) ||
1685 Item.val.uCount != 0) {
1686 return -9;
1687 }
1688
1689 //------------------------
1690 // This test sets up a caller-configured list, and looks up something not in it
1691 const uint64_t puLongList[17] = {1,2,1};
1692 const QCBORTagListIn TLLong = {17, puLongList};
1693 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEncodedLargeTag), QCBOR_DECODE_MODE_NORMAL);
1694 QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TLLong);
1695 if(QCBORDecode_GetNext(&DCtx, &Item)) {
1696 return -11;
1697 }
1698
1699 // -----------------------
1700 // This tests retrievel of the full tag list
1701 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spLotsOfTags), QCBOR_DECODE_MODE_NORMAL);
1702 uint64_t puTags[16];
1703 QCBORTagListOut Out = {0, 4, puTags};
1704 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
1705 return -12;
1706 }
1707 if(puTags[0] != 0x9192939495969798 ||
1708 puTags[1] != 0x88 ||
1709 puTags[2] != 0x05 ||
1710 puTags[3] != 0x04) {
1711 return -13;
1712 }
1713
1714 // ----------------------
1715 // This text if too small of an out list
1716 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spLotsOfTags), QCBOR_DECODE_MODE_NORMAL);
1717 QCBORTagListOut OutSmall = {0, 3, puTags};
1718 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &OutSmall) != QCBOR_ERR_TOO_MANY_TAGS) {
1719 return -14;
1720 }
1721
1722 // ---------------
1723 // Parse a version of the "CSR" that has had a ton of tags randomly inserted
1724 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRWithTags), QCBOR_DECODE_MODE_NORMAL);
1725 int n = CheckCSRMaps(&DCtx);
1726 if(n) {
1727 return n-2000;
1728 }
1729
1730 Out = (QCBORTagListOut){0,16, puTags};
1731 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRWithTags), QCBOR_DECODE_MODE_NORMAL);
1732
1733 const uint64_t puTagList[] = {773, 1, 90599561};
1734 const QCBORTagListIn TagList = {3, puTagList};
1735 QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TagList);
1736
1737
1738 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
1739 return -100;
1740 }
1741 if(Item.uDataType != QCBOR_TYPE_MAP ||
1742 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC) ||
1743 QCBORDecode_IsTagged(&DCtx, &Item, 90599561) ||
1744 QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_DATE_EPOCH) ||
1745 Item.val.uCount != 2 ||
1746 puTags[0] != CBOR_TAG_CBOR_MAGIC ||
1747 puTags[1] != CBOR_TAG_CBOR_MAGIC ||
1748 puTags[2] != CBOR_TAG_CBOR_MAGIC ||
1749 Out.uNumUsed != 3) {
1750 return -101;
1751 }
1752
1753 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
1754 return -102;
1755 }
1756 if(Item.uDataType != QCBOR_TYPE_MAP ||
1757 QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC) ||
1758 QCBORDecode_IsTagged(&DCtx, &Item, 6) ||
1759 QCBORDecode_IsTagged(&DCtx, &Item, 7) || // item is tagged 7, but 7 is not configured to be recognized
1760 Item.val.uCount != 2 ||
1761 puTags[0] != 5859837686836516696 ||
1762 puTags[1] != 7 ||
1763 Out.uNumUsed != 2) {
1764 return -103;
1765 }
1766
1767 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
1768 return -104;
1769 }
1770 if(Item.uDataType != QCBOR_TYPE_MAP ||
1771 Item.uTagBits ||
1772 Item.val.uCount != 5 ||
1773 puTags[0] != 0x0b ||
1774 Out.uNumUsed != 1) {
1775 return -105;
1776 }
1777
1778 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
1779 return -106;
1780 }
1781 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1782 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_COSE_MAC0) ||
1783 Item.val.string.len != 12 ||
1784 puTags[0] != CBOR_TAG_COSE_MAC0 ||
1785 puTags[1] != CBOR_TAG_COSE_MAC0 ||
1786 puTags[2] != CBOR_TAG_COSE_MAC0 ||
1787 Out.uNumUsed != 3) {
1788 return -105;
1789 }
1790
1791 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
1792 return -107;
1793 }
1794 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1795 !QCBORDecode_IsTagged(&DCtx, &Item, 773) ||
1796 Item.val.string.len != 3 ||
1797 puTags[0] != 773 ||
1798 Out.uNumUsed != 1) {
1799 return -108;
1800 }
1801
1802 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
1803 return -109;
1804 }
1805 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1806 !QCBORDecode_IsTagged(&DCtx, &Item, 4) ||
1807 Item.val.string.len != 9 ||
1808 puTags[0] != 4 ||
1809 puTags[11] != 0x0f ||
1810 Out.uNumUsed != 12) {
1811 return -110;
1812 }
1813
1814 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
1815 return -111;
1816 }
1817 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1818 !QCBORDecode_IsTagged(&DCtx, &Item, 17) ||
1819 Item.val.string.len != 9 ||
1820 puTags[0] != 17 ||
1821 Out.uNumUsed != 1) {
1822 return -112;
1823 }
1824
1825 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
1826 return -111;
1827 }
1828 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1829 !QCBORDecode_IsTagged(&DCtx, &Item, 17) ||
1830 Item.val.string.len != 2 ||
1831 puTags[0] != 17 ||
1832 Out.uNumUsed != 1) {
1833 return -112;
1834 }
1835
1836 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
1837 return -113;
1838 }
1839 if(Item.uDataType != QCBOR_TYPE_MAP ||
1840 QCBORDecode_IsTagged(&DCtx, &Item, 19) ||
1841 Item.val.uCount != 2 ||
1842 puTags[0] != 19 ||
1843 Out.uNumUsed != 1) {
1844 return -114;
1845 }
1846
1847 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
1848 return -115;
1849 }
1850 if(Item.uDataType != QCBOR_TYPE_MAP ||
1851 QCBORDecode_IsTagged(&DCtx, &Item, 9) ||
1852 Item.uTagBits ||
1853 Item.val.uCount != 1 ||
1854 puTags[0] != 9 ||
1855 Out.uNumUsed != 1) {
1856 return -116;
1857 }
1858
1859 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
1860 return -116;
1861 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001862 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001863 Item.val.int64 != -7 ||
1864 Item.uTagBits ||
1865 Out.uNumUsed != 0) {
1866 return -117;
1867 }
1868
1869 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
1870 return -118;
1871 }
1872 if(Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
1873 Item.val.string.len != 10 ||
1874 Item.uTagBits ||
1875 puTags[0] != 12 ||
1876 Out.uNumUsed != 1) {
1877 return -119;
1878 }
1879
1880 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
1881 return -120;
1882 }
1883 if(Item.uDataType != QCBOR_TYPE_MAP ||
1884 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_ENC_AS_B16) ||
1885 Item.val.uCount != 1 ||
1886 puTags[0] != 0x17 ||
1887 Out.uNumUsed != 1) {
1888 return -121;
1889 }
1890
1891 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
1892 return -122;
1893 }
1894 if(Item.uDataType != QCBOR_TYPE_INT64 ||
1895 QCBORDecode_IsTagged(&DCtx, &Item, 8) ||
1896 Item.val.int64 != -3 ||
1897 puTags[0] != 8 ||
1898 Out.uNumUsed != 1) {
1899 return -123;
1900 }
1901
1902 if(QCBORDecode_Finish(&DCtx)) {
1903 return -124;
1904 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001905
1906 return 0;
1907}
1908
1909
1910
1911
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07001912static uint8_t spBigNumInput[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001913 0x83,
1914 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1915 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1916 0xA4,
1917 0x63, 0x42, 0x4E, 0x2B,
1918 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1919 0x18, 0x40,
1920 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1921 0x63, 0x42, 0x4E, 0x2D,
1922 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1923 0x38, 0x3F,
1924 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
1925
1926
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07001927static uint8_t spBigNum[] = {0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001928
1929
1930int BignumParseTest()
1931{
1932 QCBORDecodeContext DCtx;
1933 QCBORItem Item;
1934 int nCBORError;
1935
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07001936 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNumInput), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001937
1938
1939 //
1940 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1941 return -1;
1942 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
1943 return -1;
1944 }
1945
1946 //
1947 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1948 return -1;
1949 if(Item.uDataType != QCBOR_TYPE_POSBIGNUM ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07001950 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001951 return -1;
1952 }
1953
1954 //
1955 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1956 return -1;
1957 if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM ||
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 //
1963 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1964 return -1;
1965 if(Item.uDataType != QCBOR_TYPE_MAP) {
1966 return -1;
1967 }
1968
1969 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1970 return -1;
1971 if(Item.uDataType != QCBOR_TYPE_POSBIGNUM ||
1972 Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07001973 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001974 return -1;
1975 }
1976
1977 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1978 return -1;
1979 if(Item.uDataType != QCBOR_TYPE_POSBIGNUM ||
1980 Item.uLabelType != QCBOR_TYPE_INT64 ||
1981 Item.label.int64 != 64 ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07001982 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001983 return -1;
1984 }
1985
1986 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1987 return -1;
1988 if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM ||
1989 Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07001990 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001991 return -1;
1992 }
1993
1994 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1995 return -1;
1996 if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM ||
1997 Item.uLabelType != QCBOR_TYPE_INT64 ||
1998 Item.label.int64 != -64 ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07001999 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002000 return -1;
2001 }
2002
2003 return 0;
2004}
2005
2006
2007
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302008static 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 +08002009{
2010 QCBORItem Item;
2011 int nCBORError;
2012
2013 if((nCBORError = QCBORDecode_GetNext(pCtx, &Item))) return -1;
2014 if(Item.uDataType != uDataType) return -1;
2015 if(uNestingLevel > 0) {
2016 if(Item.uLabelType != QCBOR_TYPE_INT64 && Item.uLabelType != QCBOR_TYPE_UINT64) return -1;
2017 if(Item.uLabelType == QCBOR_TYPE_INT64) {
2018 if(Item.label.int64 != nLabel) return -1;
2019 } else {
Laurence Lundblade570fab52018-10-13 18:28:27 +08002020 if(Item.label.uint64 != (uint64_t)nLabel) return -1;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002021 }
2022 }
2023 if(Item.uNestingLevel != uNestingLevel) return -1;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302024 if(Item.uNextNestLevel != uNextNest) return -1;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002025
2026 if(pItem) {
2027 *pItem = Item;
2028 }
2029 return 0;
2030}
2031
2032
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002033// Same code checks definite and indefinite length versions of the map
2034static int CheckCSRMaps(QCBORDecodeContext *pDC)
2035{
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302036 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 0, 1, 0, NULL)) return -1;
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002037
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302038 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 1, 2, -23, NULL)) return -1;
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002039
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302040 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 2, 3, -20, NULL)) return -1;
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002041
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302042 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -18, NULL)) return -1;
2043 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -17, NULL)) return -1;
2044 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -15, NULL)) return -1;
2045 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -16, NULL)) return -1;
2046 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 2, -14, NULL)) return -1;
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002047
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302048 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 2, 3, -19, NULL)) return -1;
2049 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 3, 4, -11, NULL)) return -1;
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002050
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302051 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_INT64, 4, 3, -9, NULL)) return -1;
2052 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_BYTE_STRING, 3, 1, -10, NULL)) return -1;
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002053
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302054 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 1, 2, -22, NULL)) return -1;
2055 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_INT64, 2, 0, -5, NULL)) return -1;
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002056
2057 if(QCBORDecode_Finish(pDC)) return -2;
2058
2059 return 0;
2060}
2061
2062
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002063/*
2064// cbor.me decoded output
2065{
2066 -23: {
2067 -20: {
2068 -18: "Organization",
2069 -17: "SSG",
2070 -15: "Confusion",
2071 -16: "San Diego",
2072 -14: "US"
2073 },
2074 -19: {
2075 -11: {
2076 -9: -7
2077 },
2078 -10: '\u0001\u0002\u0003\u0004\u0005\u0006\a\b\t\n'
2079 }
2080 },
2081 -22: {
2082 -5: -3
2083 }
2084}
2085 */
2086
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002087
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002088static uint8_t spCSRInput[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002089 0xa2, 0x36, 0xa2, 0x33, 0xa5, 0x31, 0x6c, 0x4f,
2090 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74,
2091 0x69, 0x6f, 0x6e, 0x30, 0x63, 0x53, 0x53, 0x47,
2092 0x2e, 0x69, 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73,
2093 0x69, 0x6f, 0x6e, 0x2f, 0x69, 0x53, 0x61, 0x6e,
2094 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f, 0x2d, 0x62,
2095 0x55, 0x53, 0x32, 0xa2, 0x2a, 0xa1, 0x28, 0x26,
2096 0x29, 0x4a, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
2097 0x07, 0x08, 0x09, 0x0a, 0x35, 0xa1, 0x24, 0x22};
2098
2099int NestedMapTest()
2100{
2101 QCBORDecodeContext DCtx;
2102
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002103 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002104
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002105 return CheckCSRMaps(&DCtx);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002106}
2107
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002108// Same map as above, but using indefinite lengths
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002109static uint8_t spCSRInputIndefLen[] = {
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002110 0xbf, 0x36, 0xbf, 0x33, 0xbf, 0x31, 0x6c, 0x4f,
2111 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74,
2112 0x69, 0x6f, 0x6e, 0x30, 0x63, 0x53, 0x53, 0x47,
2113 0x2e, 0x69, 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73,
2114 0x69, 0x6f, 0x6e, 0x2f, 0x69, 0x53, 0x61, 0x6e,
2115 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f, 0x2d, 0x62,
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002116 0x55, 0x53, 0xff, 0x32, 0xbf, 0x2a, 0xbf, 0x28,
2117 0x26, 0xff, 0x29, 0x4a, 0x01, 0x02, 0x03, 0x04,
2118 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0xff, 0xff,
2119 0x35, 0xbf, 0x24, 0x22, 0xff, 0xff};
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002120
2121int NestedMapTestIndefLen()
2122{
2123 QCBORDecodeContext DCtx;
2124
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002125 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInputIndefLen), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002126
2127 return CheckCSRMaps(&DCtx);
2128}
2129
2130
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002131
Laurence Lundblade17ede402018-10-13 11:43:07 +08002132static UsefulBufC make_nested_indefinite_arrays(int n, UsefulBuf Storage)
2133{
2134 UsefulOutBuf UOB;
2135 UsefulOutBuf_Init(&UOB, Storage);
2136
2137 int i;
2138 for(i = 0; i < n; i++) {
2139 UsefulOutBuf_AppendByte(&UOB, 0x9f);
2140 }
2141
2142 for(i = 0; i < n; i++) {
2143 UsefulOutBuf_AppendByte(&UOB, 0xff);
2144 }
2145 return UsefulOutBuf_OutUBuf(&UOB);
2146}
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002147
2148
Laurence Lundblade17ede402018-10-13 11:43:07 +08002149static int parse_indeflen_nested(UsefulBufC Nested, int nNestLevel)
2150{
2151 QCBORDecodeContext DC;
2152 QCBORDecode_Init(&DC, Nested, 0);
2153
2154 int j;
2155 for(j = 0; j < nNestLevel; j++) {
2156 QCBORItem Item;
2157 int nReturn = QCBORDecode_GetNext(&DC, &Item);
2158 if(j >= QCBOR_MAX_ARRAY_NESTING) {
2159 // Should be in error
2160 if(nReturn != QCBOR_ERR_ARRAY_NESTING_TOO_DEEP) {
2161 return -4;
2162 } else {
2163 return 0; // Decoding doesn't recover after an error
2164 }
2165 } else {
2166 // Should be no error
2167 if(nReturn) {
2168 return -9; // Should not have got an error
2169 }
2170 }
2171 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
2172 return -7;
2173 }
2174 }
2175 int nReturn = QCBORDecode_Finish(&DC);
2176 if(nReturn) {
2177 return -3;
2178 }
2179 return 0;
2180}
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002181
2182
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302183int IndefiniteLengthNestTest()
Laurence Lundblade17ede402018-10-13 11:43:07 +08002184{
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05302185 UsefulBuf_MAKE_STACK_UB(Storage, 50);
Laurence Lundblade17ede402018-10-13 11:43:07 +08002186 int i;
Laurence Lundblade6de37062018-10-15 12:22:42 +05302187 for(i=1; i < QCBOR_MAX_ARRAY_NESTING+4; i++) {
Laurence Lundblade17ede402018-10-13 11:43:07 +08002188 UsefulBufC Nested = make_nested_indefinite_arrays(i, Storage);
2189 int nReturn = parse_indeflen_nested(Nested, i);
2190 if(nReturn) {
2191 return nReturn;
2192 }
2193 }
2194 return 0;
2195}
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002196
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002197
Laurence Lundblade6de37062018-10-15 12:22:42 +05302198
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002199static const uint8_t spIndefiniteArray[] = {0x9f, 0x01, 0x82, 0x02, 0x03, 0xff}; // [1, [2, 3]]
2200static const uint8_t spIndefiniteArrayBad1[] = {0x9f}; // No closing break
2201static const uint8_t spIndefiniteArrayBad2[] = {0x9f, 0x9f, 0x02, 0xff}; // Not enough closing breaks
2202static const uint8_t spIndefiniteArrayBad3[] = {0x9f, 0x02, 0xff, 0xff}; // Too many closing breaks
2203static const uint8_t spIndefiniteArrayBad4[] = {0x81, 0x9f}; // Unclosed indeflen inside def len
2204static const uint8_t spIndefiniteArrayBad5[] = {0x9f, 0xd1, 0xff}; // confused tag
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002205
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302206int IndefiniteLengthArrayMapTest()
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002207{
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002208 int nResult;
2209 // --- first test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002210 UsefulBufC IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArray);
Laurence Lundblade17ede402018-10-13 11:43:07 +08002211
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002212 // Decode it and see if it is OK
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05302213 UsefulBuf_MAKE_STACK_UB(MemPool, 150);
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002214 QCBORDecodeContext DC;
2215 QCBORItem Item;
2216 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
2217
2218 QCBORDecode_SetMemPool(&DC, MemPool, false);
Laurence Lundblade6de37062018-10-15 12:22:42 +05302219
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002220 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05302221
2222 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
2223 Item.uNestingLevel != 0 ||
2224 Item.uNextNestLevel != 1) {
2225 return -111;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002226 }
2227
2228 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05302229 if(Item.uDataType != QCBOR_TYPE_INT64 ||
2230 Item.uNestingLevel != 1 ||
2231 Item.uNextNestLevel != 1) {
2232 return -2;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002233 }
2234
2235 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05302236 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
2237 Item.uNestingLevel != 1 ||
2238 Item.uNextNestLevel != 2) {
2239 return -3;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002240 }
2241
2242 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05302243 if(Item.uDataType != QCBOR_TYPE_INT64 |
2244 Item.uNestingLevel != 2 ||
2245 Item.uNextNestLevel != 2) {
2246 return -4;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002247 }
2248
2249 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05302250 if(Item.uDataType != QCBOR_TYPE_INT64 |
2251 Item.uNestingLevel != 2 ||
2252 Item.uNextNestLevel != 0) {
2253 return -5;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002254 }
2255
2256 if(QCBORDecode_Finish(&DC)) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05302257 return -6;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002258 }
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002259
2260 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002261 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad1);
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002262
2263 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
2264
2265 QCBORDecode_SetMemPool(&DC, MemPool, false);
2266
2267 nResult = QCBORDecode_GetNext(&DC, &Item);
2268 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05302269 return -7;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002270 }
2271
Laurence Lundblade570fab52018-10-13 18:28:27 +08002272 nResult = QCBORDecode_Finish(&DC);
Laurence Lundblade6de37062018-10-15 12:22:42 +05302273 if(nResult != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
2274 return -8;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002275 }
2276
2277
2278 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002279 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad2);
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002280
2281 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
2282
2283 QCBORDecode_SetMemPool(&DC, MemPool, false);
2284
2285 nResult = QCBORDecode_GetNext(&DC, &Item);
2286 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05302287 return -9;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002288 }
2289
2290 nResult = QCBORDecode_GetNext(&DC, &Item);
2291 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05302292 return -10;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002293 }
2294
2295 nResult = QCBORDecode_GetNext(&DC, &Item);
2296 if(nResult || Item.uDataType != QCBOR_TYPE_INT64) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05302297 return -11;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002298 }
2299
2300 nResult = QCBORDecode_Finish(&DC);
Laurence Lundblade6de37062018-10-15 12:22:42 +05302301 if(nResult != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
2302 return -12;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002303 }
2304
2305
2306 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002307 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad3);
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002308
2309 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
2310
2311 QCBORDecode_SetMemPool(&DC, MemPool, false);
2312
2313 nResult = QCBORDecode_GetNext(&DC, &Item);
2314 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05302315 return -13;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002316 }
2317
2318 nResult = QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05302319 if(nResult != QCBOR_ERR_BAD_BREAK) {
2320 return -14;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002321 }
Laurence Lundblade6de37062018-10-15 12:22:42 +05302322
Laurence Lundblade570fab52018-10-13 18:28:27 +08002323
2324 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002325 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad4);
Laurence Lundblade570fab52018-10-13 18:28:27 +08002326
2327 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
2328
2329 QCBORDecode_SetMemPool(&DC, MemPool, false);
2330
2331 nResult = QCBORDecode_GetNext(&DC, &Item);
2332 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05302333 return -15;
Laurence Lundblade570fab52018-10-13 18:28:27 +08002334 }
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302335
Laurence Lundblade570fab52018-10-13 18:28:27 +08002336 nResult = QCBORDecode_GetNext(&DC, &Item);
2337 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05302338 return -16;
Laurence Lundblade570fab52018-10-13 18:28:27 +08002339 }
2340
2341 nResult = QCBORDecode_Finish(&DC);
Laurence Lundblade6de37062018-10-15 12:22:42 +05302342 if(nResult != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
2343 return -17;
Laurence Lundblade570fab52018-10-13 18:28:27 +08002344 }
2345
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302346 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002347 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad5);
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302348
2349 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
2350
2351 QCBORDecode_SetMemPool(&DC, MemPool, false);
2352
2353 nResult = QCBORDecode_GetNext(&DC, &Item);
2354 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05302355 return -18;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302356 }
2357
2358 nResult = QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05302359 if(nResult != QCBOR_ERR_BAD_BREAK) {
2360 return -19;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302361 }
2362
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002363 return 0;
2364}
2365
Laurence Lundblade17ede402018-10-13 11:43:07 +08002366
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002367static const uint8_t spIndefiniteLenString[] = {
Laurence Lundblade17ede402018-10-13 11:43:07 +08002368 0x81, // Array of length one
2369 0x7f, // text string marked with indefinite length
2370 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment
2371 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment
2372 0xff // ending break
2373};
2374
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002375static const uint8_t spIndefiniteLenStringBad2[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302376 0x81, // Array of length one
2377 0x7f, // text string marked with indefinite length
2378 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment
2379 0x44, 0x6d, 0x69, 0x6e, 0x67, // second segment of wrong type
2380 0xff // ending break
2381};
2382
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002383static const uint8_t spIndefiniteLenStringBad3[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302384 0x81, // Array of length one
2385 0x7f, // text string marked with indefinite length
2386 0x01, 0x02, // Not a string
2387 0xff // ending break
2388};
2389
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002390static const uint8_t spIndefiniteLenStringBad4[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302391 0x81, // Array of length one
2392 0x7f, // text string marked with indefinite length
2393 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment
2394 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment
2395 // missing end of string
2396};
2397
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002398static const uint8_t spIndefiniteLenStringLabel[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302399 0xa1, // Array of length one
2400 0x7f, // text string marked with indefinite length
2401 0x65, 0x73, 0x74, 0x72, 0x75, 0x75, // first segment
2402 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment
2403 0xff, // ending break
2404 0x01 // integer being labeled.
2405};
2406
2407static UsefulBufC MakeIndefiniteBigBstr(UsefulBuf Storage)
2408{
2409 UsefulOutBuf UOB;
2410
2411 UsefulOutBuf_Init(&UOB, Storage);
2412 UsefulOutBuf_AppendByte(&UOB, 0x81);
2413 UsefulOutBuf_AppendByte(&UOB, 0x5f);
2414
2415 int i = 0;
2416 for(int nChunkSize = 1; nChunkSize <= 128; nChunkSize *= 2) {
2417 UsefulOutBuf_AppendByte(&UOB, 0x58);
2418 UsefulOutBuf_AppendByte(&UOB, (uint8_t)nChunkSize);
2419 for(int j = 0; j < nChunkSize; j++ ) {
2420 UsefulOutBuf_AppendByte(&UOB, i);
2421 i++;
2422 }
2423 }
2424 UsefulOutBuf_AppendByte(&UOB, 0xff);
2425
2426 return UsefulOutBuf_OutUBuf(&UOB);
2427}
2428
2429static int CheckBigString(UsefulBufC BigString)
2430{
2431 if(BigString.len != 255) {
2432 return 1;
2433 }
2434
2435 for(uint8_t i = 0; i < 255; i++){
2436 if(((const uint8_t *)BigString.ptr)[i] != i) {
2437 return 1;
2438 }
2439 }
2440 return 0;
2441}
2442
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05302443
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302444int IndefiniteLengthStringTest()
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302445{
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302446 QCBORDecodeContext DC;
2447 QCBORItem Item;
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05302448 // big enough for MakeIndefiniteBigBstr() + MemPool overhead
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05302449 UsefulBuf_MAKE_STACK_UB(MemPool, 320);
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302450
2451 // --- Simple normal indefinite length string ------
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002452 UsefulBufC IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenString);
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302453 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002454
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05302455 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302456 return -1;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302457 }
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002458
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302459 if(QCBORDecode_GetNext(&DC, &Item)) {
2460 return -2;
2461 }
2462 if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.uDataAlloc) {
2463 return -3;
2464 }
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002465
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302466 if(QCBORDecode_GetNext(&DC, &Item)) {
2467 return -4;
2468 }
2469 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || !Item.uDataAlloc) {
2470 return -5;
2471 }
2472 if(QCBORDecode_Finish(&DC)) {
2473 return -6;
2474 }
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302475
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302476 // ----- types mismatch ---
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002477 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad2), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302478
2479 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
2480 return -7;
2481 }
2482
2483 if(QCBORDecode_GetNext(&DC, &Item)) {
2484 return -8;
2485 }
2486 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
2487 return -9;
2488 }
2489
Laurence Lundblade30816f22018-11-10 13:40:22 +07002490 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_INDEFINITE_STRING_CHUNK) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302491 return -10;
2492 }
2493
2494 // ----- not a string ---
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002495 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad3), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302496
2497 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
2498 return -11;
2499 }
2500
2501 if(QCBORDecode_GetNext(&DC, &Item)) {
2502 return -12;
2503 }
2504 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
2505 return -13;
2506 }
2507
Laurence Lundblade30816f22018-11-10 13:40:22 +07002508 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_INDEFINITE_STRING_CHUNK) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302509 return -14;
2510 }
2511
2512 // ----- no end -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002513 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad4), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302514
2515 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
2516 return -15;
2517 }
2518
2519 if(QCBORDecode_GetNext(&DC, &Item)) {
2520 return -16;
2521 }
2522 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
2523 return -17;
2524 }
2525
2526 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_HIT_END) {
2527 return -18;
2528 }
2529
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302530 // ------ Don't set a string allocator and see an error -----
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302531 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
2532
2533 QCBORDecode_GetNext(&DC, &Item);
2534 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302535 return -19;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302536 }
2537
2538 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_NO_STRING_ALLOCATOR) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302539 return -20;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302540 }
2541
2542 // ----- Mempool is way too small -----
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05302543 UsefulBuf_MAKE_STACK_UB(MemPoolTooSmall, 20); // 20 is too small no matter what
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302544
2545 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
2546 if(!QCBORDecode_SetMemPool(&DC, MemPoolTooSmall, false)) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302547 return -21;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302548 }
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302549
2550 // ----- Mempool is way too small -----
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05302551 UsefulBuf_MAKE_STACK_UB(BigIndefBStrStorage, 290);
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302552 UsefulBufC BigIndefBStr = MakeIndefiniteBigBstr(BigIndefBStrStorage);
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302553
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05302554 UsefulBuf_MAKE_STACK_UB(MemPoolSmall, 80); // 80 is big enough for MemPool overhead, but not BigIndefBStr
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302555
2556 QCBORDecode_Init(&DC, BigIndefBStr, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302557 if(QCBORDecode_SetMemPool(&DC, MemPoolSmall, false)) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302558 return -22;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302559 }
2560
2561 QCBORDecode_GetNext(&DC, &Item);
2562 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302563 return -23;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302564 }
Laurence Lundblade30816f22018-11-10 13:40:22 +07002565 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_STRING_ALLOCATE) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302566 return -24;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302567 }
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302568
2569 // ---- big bstr -----
2570 QCBORDecode_Init(&DC, BigIndefBStr, QCBOR_DECODE_MODE_NORMAL);
2571
2572 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
2573 return -25;
2574 }
2575
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05302576 if(QCBORDecode_GetNext(&DC, &Item)) {
2577 return -26;
2578 }
2579 if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.uDataAlloc) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302580 return -26;
2581 }
2582
2583 if(QCBORDecode_GetNext(&DC, &Item)) {
2584 return -27;
2585 }
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05302586 if(Item.uDataType != QCBOR_TYPE_BYTE_STRING || !Item.uDataAlloc || Item.uNestingLevel != 1) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302587 return -28;
2588 }
2589 if(CheckBigString(Item.val.string)) {
2590 return -3;
2591 }
2592 if(QCBORDecode_Finish(&DC)) {
2593 return -29;
2594 }
2595
2596 // --- label is an indefinite length string ------
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002597 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringLabel), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302598
2599 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
2600 return -30;
2601 }
2602
2603 QCBORDecode_GetNext(&DC, &Item);
2604 if(Item.uDataType != QCBOR_TYPE_MAP) {
2605 return -31;
2606 }
2607
2608 if(QCBORDecode_GetNext(&DC, &Item)){
2609 return -32;
2610 }
2611 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || Item.uDataType != QCBOR_TYPE_INT64 ||
2612 Item.uDataAlloc || !Item.uLabelAlloc ||
2613 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("struuming"))) {
2614 return -33;
2615 }
2616
2617 if(QCBORDecode_Finish(&DC)) {
2618 return -34;
2619 }
2620
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002621 return 0;
2622}
2623
2624
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302625int AllocAllStringsTest()
2626{
2627 QCBORDecodeContext DC;
2628
2629 // First test, use the "CSRMap" as easy input and checking
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002630 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302631
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05302632 UsefulBuf_MAKE_STACK_UB(Pool, 300);
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302633
2634 QCBORDecode_SetMemPool(&DC, Pool, 1); // Turn on copying.
2635
2636 if(CheckCSRMaps(&DC)) {
2637 return -1;
2638 }
2639
2640 // Next parse, save pointers to a few strings, destroy original and see all is OK.
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05302641 UsefulBuf_MAKE_STACK_UB(CopyOfStorage, 160);
2642 UsefulBufC CopyOf = UsefulBuf_Copy(CopyOfStorage, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded));
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002643
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302644 QCBORDecode_Init(&DC, CopyOf, QCBOR_DECODE_MODE_NORMAL);
2645 QCBORDecode_SetMemPool(&DC, Pool, 1); // Turn on copying.
2646
2647 int nCBORError;
2648 QCBORItem Item1, Item2, Item3, Item4;
2649 if((nCBORError = QCBORDecode_GetNext(&DC, &Item1)))
2650 return nCBORError;
2651 if(Item1.uDataType != QCBOR_TYPE_MAP ||
2652 Item1.val.uCount != 3)
2653 return -1;
2654 if((nCBORError = QCBORDecode_GetNext(&DC, &Item1)))
2655 return nCBORError;
2656 if((nCBORError = QCBORDecode_GetNext(&DC, &Item2)))
2657 return nCBORError;
2658 if((nCBORError = QCBORDecode_GetNext(&DC, &Item3)))
2659 return nCBORError;
2660 if((nCBORError = QCBORDecode_GetNext(&DC, &Item4)))
2661 return nCBORError;
2662
Laurence Lundblade05ec57b2018-10-21 01:50:03 +05302663 UsefulBuf_Set(CopyOfStorage, '_');
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302664
2665 if(Item1.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302666 Item1.uDataType != QCBOR_TYPE_INT64 ||
2667 Item1.val.int64 != 42 ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09002668 UsefulBuf_Compare(Item1.label.string, UsefulBuf_FromSZ("first integer"))) {
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302669 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09002670 }
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302671
2672
2673 if(Item2.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09002674 UsefulBuf_Compare(Item2.label.string, UsefulBuf_FromSZ("an array of two strings")) ||
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302675 Item2.uDataType != QCBOR_TYPE_ARRAY ||
2676 Item2.val.uCount != 2)
2677 return -1;
2678
2679 if(Item3.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09002680 UsefulBuf_Compare(Item3.val.string, UsefulBuf_FromSZ("string1"))) {
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 if(Item4.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09002685 UsefulBuf_Compare(Item4.val.string, UsefulBuf_FromSZ("string2"))) {
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302686 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09002687 }
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302688
2689 // Next parse with a pool that is too small
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05302690 UsefulBuf_MAKE_STACK_UB(SmallPool, 80);
2691 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302692 QCBORDecode_SetMemPool(&DC, SmallPool, 1); // Turn on copying.
2693 if((nCBORError = QCBORDecode_GetNext(&DC, &Item1)))
2694 return nCBORError;
2695 if(Item1.uDataType != QCBOR_TYPE_MAP ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09002696 Item1.val.uCount != 3) {
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302697 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09002698 }
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302699 if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item1))){
2700 if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item2))) {
2701 if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item3))) {
2702 nCBORError = QCBORDecode_GetNext(&DC, &Item4);
2703 }
2704 }
2705 }
Laurence Lundblade30816f22018-11-10 13:40:22 +07002706 if(nCBORError != QCBOR_ERR_STRING_ALLOCATE) {
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302707 return -5;
2708 }
2709
2710 return 0;
2711}
2712
Laurence Lundbladef6531662018-12-04 10:42:22 +09002713// Cheating declaration to get to the special test hook
2714size_t MemPoolTestHook_GetPoolSize(void *ctx);
2715
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302716
2717int MemPoolTest(void)
Laurence Lundblade0155b622018-10-12 20:04:37 +08002718{
Laurence Lundbladef6531662018-12-04 10:42:22 +09002719 // Set up the decoder with a tiny bit of CBOR to parse
2720 QCBORDecodeContext DC;
2721 const uint8_t pMinimalCBOR[] = {0xa0}; // One empty map
2722 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pMinimalCBOR),0);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002723
Laurence Lundbladef6531662018-12-04 10:42:22 +09002724 // Set up an memory pool of 100 bytes
2725 UsefulBuf_MAKE_STACK_UB(Pool, 100);
2726 QCBORDecode_SetMemPool(&DC, Pool, 0);
2727
2728 // Cheat a little to get to the string allocator object
2729 // so we can call it directly to test it
2730 QCBORStringAllocator *pAlloc = (QCBORStringAllocator *)DC.pStringAllocator;
2731 // Cheat some more to know exactly the
2732 size_t uAvailPool = MemPoolTestHook_GetPoolSize(pAlloc);
2733
2734 // First test -- ask for too much in one go
2735 UsefulBuf Allocated = (*pAlloc->fAllocate)(pAlloc->pAllocaterContext, NULL, uAvailPool+1);
2736 if(!UsefulBuf_IsNULL(Allocated)) {
2737 return -1;
2738 }
2739
2740
2741 // Re do the set up for the next test that will do a successful alloc,
2742 // a fail, a free and then success
2743 // This test should work on 32 and 64-bit machines if the compiler
2744 // does the expected thing with pointer sizes for the internal
2745 // MemPool implementation leaving 44 or 72 bytes of pool memory.
2746 QCBORDecode_SetMemPool(&DC, Pool, 0);
2747
2748 // Cheat a little to get to the string allocator object
2749 // so we can call it directly to test it
2750 pAlloc = (QCBORStringAllocator *)DC.pStringAllocator;
2751 // Cheat some more to know exactly the
2752 uAvailPool = MemPoolTestHook_GetPoolSize(pAlloc);
2753
2754 Allocated = (*pAlloc->fAllocate)(pAlloc->pAllocaterContext, NULL, uAvailPool-1);
2755 if(UsefulBuf_IsNULL(Allocated)) { // expected to succeed
2756 return -2;
2757 }
2758 UsefulBuf Allocated2 = (*pAlloc->fAllocate)(pAlloc->pAllocaterContext, NULL, uAvailPool/2);
2759 if(!UsefulBuf_IsNULL(Allocated2)) { // expected to fail
2760 return -3;
2761 }
2762 (*pAlloc->fFree)(pAlloc->pAllocaterContext, Allocated.ptr);
2763 Allocated = (*pAlloc->fAllocate)(pAlloc->pAllocaterContext, NULL, uAvailPool/2);
2764 if(UsefulBuf_IsNULL(Allocated)) { // succeed because of the free
2765 return -4;
2766 }
2767
2768
2769 // Re do set up for next test that involves a successful alloc,
2770 // and a successful realloc and a failed realloc
2771 QCBORDecode_SetMemPool(&DC, Pool, 0);
2772
2773 // Cheat a little to get to the string allocator object
2774 // so we can call it directly to test it
2775 pAlloc = (QCBORStringAllocator *)DC.pStringAllocator;
2776 Allocated = (*pAlloc->fAllocate)(pAlloc->pAllocaterContext, NULL, uAvailPool/2);
2777 if(UsefulBuf_IsNULL(Allocated)) { // expected to succeed
2778 return -5;
2779 }
2780 Allocated2 = (*pAlloc->fAllocate)(pAlloc->pAllocaterContext, Allocated.ptr, uAvailPool);
2781 if(UsefulBuf_IsNULL(Allocated2)) {
2782 return -6;
2783 }
2784 if(Allocated2.ptr != Allocated.ptr || Allocated2.len != uAvailPool) {
2785 return -7;
2786 }
2787 UsefulBuf Allocated3 = (*pAlloc->fAllocate)(pAlloc->pAllocaterContext, Allocated.ptr, uAvailPool+1);
2788 if(!UsefulBuf_IsNULL(Allocated3)) { // expected to fail
2789 return -8;
2790 }
2791
2792 return 0;
2793}
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002794