blob: 999a9b71f5739be8c4292ce62c55acc11699295d [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
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080041static void printencoded(const char *szLabel, const uint8_t *pEncoded, size_t nLen)
42{
43 if(szLabel) {
44 printf("%s ", szLabel);
45 }
46
Laurence Lundblade570fab52018-10-13 18:28:27 +080047 size_t i;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080048 for(i = 0; i < nLen; i++) {
49 uint8_t Z = pEncoded[i];
50 printf("%02x ", Z);
51 }
52 printf("\n");
53
54 fflush(stdout);
55}
56
57
Laurence Lundbladeb836efb2018-10-28 20:09:58 +070058static const uint8_t spExpectedEncodedInts[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080059 0x98, 0x2f, 0x3b, 0x7f, 0xff, 0xff, 0xff, 0xff,
60 0xff, 0xff, 0xff, 0x3b, 0x00, 0x00, 0x00, 0x01,
61 0x00, 0x00, 0x00, 0x00, 0x3a, 0xff, 0xff, 0xff,
62 0xff, 0x3a, 0xff, 0xff, 0xff, 0xfe, 0x3a, 0xff,
63 0xff, 0xff, 0xfd, 0x3a, 0x7f, 0xff, 0xff, 0xff,
64 0x3a, 0x7f, 0xff, 0xff, 0xfe, 0x3a, 0x00, 0x01,
65 0x00, 0x01, 0x3a, 0x00, 0x01, 0x00, 0x00, 0x39,
66 0xff, 0xff, 0x39, 0xff, 0xfe, 0x39, 0xff, 0xfd,
67 0x39, 0x01, 0x00, 0x38, 0xff, 0x38, 0xfe, 0x38,
68 0xfd, 0x38, 0x18, 0x37, 0x36, 0x20, 0x00, 0x00,
69 0x01, 0x16, 0x17, 0x18, 0x18, 0x18, 0x19, 0x18,
70 0x1a, 0x18, 0xfe, 0x18, 0xff, 0x19, 0x01, 0x00,
71 0x19, 0x01, 0x01, 0x19, 0xff, 0xfe, 0x19, 0xff,
72 0xff, 0x1a, 0x00, 0x01, 0x00, 0x00, 0x1a, 0x00,
73 0x01, 0x00, 0x01, 0x1a, 0x00, 0x01, 0x00, 0x02,
74 0x1a, 0x7f, 0xff, 0xff, 0xff, 0x1a, 0x7f, 0xff,
75 0xff, 0xff, 0x1a, 0x80, 0x00, 0x00, 0x00, 0x1a,
76 0x80, 0x00, 0x00, 0x01, 0x1a, 0xff, 0xff, 0xff,
77 0xfe, 0x1a, 0xff, 0xff, 0xff, 0xff, 0x1b, 0x00,
78 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x1b,
79 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01,
80 0x1b, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
81 0xff, 0x1b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
82 0xff, 0xff};
83
84
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080085// return CBOR error or -1 if type of value doesn't match
86
87static int IntegerValuesParseTestInternal(QCBORDecodeContext *pDCtx)
88{
89 QCBORItem Item;
90 int nCBORError;
91
92 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
93 return nCBORError;
94 if(Item.uDataType != QCBOR_TYPE_ARRAY)
95 return -1;
96
97 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
98 return nCBORError;
99 if(Item.uDataType != QCBOR_TYPE_INT64 || // Todo; fix this for 32-bit machines
Laurence Lundblade570fab52018-10-13 18:28:27 +0800100 Item.val.int64 != -9223372036854775807LL - 1)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800101 return -1;
102
103 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
104 return nCBORError;
105 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade570fab52018-10-13 18:28:27 +0800106 Item.val.int64 != -4294967297)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800107 return -1;
108
109 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
110 return nCBORError;
111 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade570fab52018-10-13 18:28:27 +0800112 Item.val.int64 != -4294967296)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800113 return -1;
114
115 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
116 return nCBORError;
117 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade570fab52018-10-13 18:28:27 +0800118 Item.val.int64 != -4294967295)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800119 return -1;
120
121 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
122 return nCBORError;
123 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade570fab52018-10-13 18:28:27 +0800124 Item.val.int64 != -4294967294)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800125 return -1;
126
127
128 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
129 return nCBORError;
130 if(Item.uDataType != QCBOR_TYPE_INT64 ||
131 Item.val.int64 != -2147483648)
132 return -1;
133
134 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
135 return nCBORError;
136 if(Item.uDataType != QCBOR_TYPE_INT64 ||
137 Item.val.int64 != -2147483647)
138 return -1;
139
140 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
141 return nCBORError;
142 if(Item.uDataType != QCBOR_TYPE_INT64 ||
143 Item.val.int64 != -65538)
144 return -1;
145
146 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
147 return nCBORError;
148 if(Item.uDataType != QCBOR_TYPE_INT64 ||
149 Item.val.int64 != -65537)
150 return -1;
151
152 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
153 return nCBORError;
154 if(Item.uDataType != QCBOR_TYPE_INT64 ||
155 Item.val.int64 != -65536)
156 return -1;
157
158
159 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
160 return nCBORError;
161 if(Item.uDataType != QCBOR_TYPE_INT64 ||
162 Item.val.int64 != -65535)
163 return -1;
164
165
166 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
167 return nCBORError;
168 if(Item.uDataType != QCBOR_TYPE_INT64 ||
169 Item.val.int64 != -65534)
170 return -1;
171
172
173 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
174 return nCBORError;
175 if(Item.uDataType != QCBOR_TYPE_INT64 ||
176 Item.val.int64 != -257)
177 return -1;
178
179 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
180 return nCBORError;
181 if(Item.uDataType != QCBOR_TYPE_INT64 ||
182 Item.val.int64 != -256)
183 return -1;
184
185 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
186 return nCBORError;
187 if(Item.uDataType != QCBOR_TYPE_INT64 ||
188 Item.val.int64 != -255)
189 return -1;
190
191 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
192 return nCBORError;
193 if(Item.uDataType != QCBOR_TYPE_INT64 ||
194 Item.val.int64 != -254)
195 return -1;
196
197
198 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
199 return nCBORError;
200 if(Item.uDataType != QCBOR_TYPE_INT64 ||
201 Item.val.int64 != -25)
202 return -1;
203
204
205 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
206 return nCBORError;
207 if(Item.uDataType != QCBOR_TYPE_INT64 ||
208 Item.val.int64 != -24)
209 return -1;
210
211
212 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
213 return nCBORError;
214 if(Item.uDataType != QCBOR_TYPE_INT64 ||
215 Item.val.int64 != -23)
216 return -1;
217
218
219 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
220 return nCBORError;
221 if(Item.uDataType != QCBOR_TYPE_INT64 ||
222 Item.val.int64 != -1)
223 return -1;
224
225
226 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
227 return nCBORError;
228 if(Item.uDataType != QCBOR_TYPE_INT64 ||
229 Item.val.int64 != 0)
230 return -1;
231
232
233 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
234 return nCBORError;
235 if(Item.uDataType != QCBOR_TYPE_INT64 ||
236 Item.val.int64 != 0)
237 return -1;
238
239 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
240 return nCBORError;
241 if(Item.uDataType != QCBOR_TYPE_INT64 ||
242 Item.val.int64 != 1)
243 return -1;
244
245
246 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
247 return nCBORError;
248 if(Item.uDataType != QCBOR_TYPE_INT64 ||
249 Item.val.int64 != 22)
250 return -1;
251
252
253 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
254 return nCBORError;
255 if(Item.uDataType != QCBOR_TYPE_INT64 ||
256 Item.val.int64 != 23)
257 return -1;
258
259
260 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
261 return nCBORError;
262 if(Item.uDataType != QCBOR_TYPE_INT64 ||
263 Item.val.int64 != 24)
264 return -1;
265
266
267 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
268 return nCBORError;
269 if(Item.uDataType != QCBOR_TYPE_INT64 ||
270 Item.val.int64 != 25)
271 return -1;
272
273 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
274 return nCBORError;
275 if(Item.uDataType != QCBOR_TYPE_INT64 ||
276 Item.val.int64 != 26)
277 return -1;
278
279
280 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
281 return nCBORError;
282 if(Item.uDataType != QCBOR_TYPE_INT64 ||
283 Item.val.int64 != 254)
284 return -1;
285
286
287 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
288 return nCBORError;
289 if(Item.uDataType != QCBOR_TYPE_INT64 ||
290 Item.val.int64 != 255)
291 return -1;
292
293
294 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
295 return nCBORError;
296 if(Item.uDataType != QCBOR_TYPE_INT64 ||
297 Item.val.int64 != 256)
298 return -1;
299
300
301 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
302 return nCBORError;
303 if(Item.uDataType != QCBOR_TYPE_INT64 ||
304 Item.val.int64 != 257)
305 return -1;
306
307 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
308 return nCBORError;
309 if(Item.uDataType != QCBOR_TYPE_INT64 ||
310 Item.val.int64 != 65534)
311 return -1;
312
313
314 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
315 return nCBORError;
316 if(Item.uDataType != QCBOR_TYPE_INT64 ||
317 Item.val.int64 != 65535)
318 return -1;
319
320
321 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
322 return nCBORError;
323 if(Item.uDataType != QCBOR_TYPE_INT64 ||
324 Item.val.int64 != 65536)
325 return -1;
326
327 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
328 return nCBORError;
329 if(Item.uDataType != QCBOR_TYPE_INT64 ||
330 Item.val.int64 != 65537)
331 return -1;
332
333 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
334 return nCBORError;
335 if(Item.uDataType != QCBOR_TYPE_INT64 ||
336 Item.val.int64 != 65538)
337 return -1;
338
339 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
340 return nCBORError;
341 if(Item.uDataType != QCBOR_TYPE_INT64 ||
342 Item.val.int64 != 2147483647)
343 return -1;
344
345 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
346 return nCBORError;
347 if(Item.uDataType != QCBOR_TYPE_INT64 ||
348 Item.val.int64 != 2147483647)
349 return -1;
350
351 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
352 return nCBORError;
353 if(Item.uDataType != QCBOR_TYPE_INT64 ||
354 Item.val.int64 != 2147483648)
355 return -1;
356
357 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
358 return nCBORError;
359 if(Item.uDataType != QCBOR_TYPE_INT64 ||
360 Item.val.int64 != 2147483649)
361 return -1;
362
363 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
364 return nCBORError;
365 if(Item.uDataType != QCBOR_TYPE_INT64 ||
366 Item.val.int64 != 4294967294)
367 return -1;
368
369
370 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
371 return nCBORError;
372 if(Item.uDataType != QCBOR_TYPE_INT64 ||
373 Item.val.int64 != 4294967295)
374 return -1;
375
376
377 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
378 return nCBORError;
379 if(Item.uDataType != QCBOR_TYPE_INT64 ||
380 Item.val.int64 != 4294967296)
381 return -1;
382
383
384 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
385 return nCBORError;
386 if(Item.uDataType != QCBOR_TYPE_INT64 ||
387 Item.val.int64 != 4294967297)
388 return -1;
389
390
391
392 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
393 return nCBORError;
394 if(Item.uDataType != QCBOR_TYPE_INT64 ||
395 Item.val.int64 != 9223372036854775807LL)
396 return -1;
397
398
399 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
400 return nCBORError;
401 if(Item.uDataType != QCBOR_TYPE_UINT64 ||
402 Item.val.uint64 != 18446744073709551615ULL)
403 return -1;
404
405
406 if(QCBORDecode_Finish(pDCtx) != QCBOR_SUCCESS) {
407 return -1;
408 }
409
410 return 0;
411}
412
413
414/*
415 Tests the decoding of lots of different integers sizes
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +0800416 and values.
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800417 */
418
419int IntegerValuesParseTest()
420{
421 int n;
422 QCBORDecodeContext DCtx;
423
Laurence Lundbladeb836efb2018-10-28 20:09:58 +0700424 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedEncodedInts), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800425
426 n = IntegerValuesParseTestInternal(&DCtx);
427
428 return(n);
429}
430
431
432/*
433 Creates a simple CBOR array and returns it in *pEncoded. The array is malloced
434 and needs to be freed. This is used by several tests.
435
436 Two of the inputs can be set. Two other items in the array are fixed.
437
438 */
439
440static int CreateSimpleArray(int nInt1, int nInt2, uint8_t **pEncoded, size_t *pEncodedLen)
441{
442 QCBOREncodeContext ECtx;
443 int nReturn = -1;
444
445 *pEncoded = NULL;
446 *pEncodedLen = INT32_MAX;
447
448 // loop runs CBOR encoding twice. First with no buffer to
449 // calucate the length so buffer can be allocated correctly,
450 // and last with the buffer to do the actual encoding
451 do {
Laurence Lundblade0595e932018-11-02 22:22:47 +0700452 QCBOREncode_Init(&ECtx, (UsefulBuf){*pEncoded, *pEncodedLen});
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800453 QCBOREncode_OpenArray(&ECtx);
454 QCBOREncode_AddInt64(&ECtx, nInt1);
455 QCBOREncode_AddInt64(&ECtx, nInt2);
456 QCBOREncode_AddBytes(&ECtx, ((UsefulBufC) {"galactic", 8}));
457 QCBOREncode_AddBytes(&ECtx, ((UsefulBufC) {"haven token", 11}));
458 QCBOREncode_CloseArray(&ECtx);
459
Laurence Lundblade0595e932018-11-02 22:22:47 +0700460 UsefulBufC Encoded;
461 if(QCBOREncode_Finish(&ECtx, &Encoded))
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800462 goto Done;
463
464 if(*pEncoded != NULL) {
Laurence Lundblade0595e932018-11-02 22:22:47 +0700465 *pEncodedLen = Encoded.len;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800466 nReturn = 0;
467 goto Done;
468 }
Laurence Lundblade0595e932018-11-02 22:22:47 +0700469 *pEncoded = malloc(Encoded.len);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800470 if(*pEncoded == NULL) {
471 nReturn = -1;
472 goto Done;
473 }
474
475 } while(1);
476Done:
477 return (nReturn);
478
479}
480
481
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800482/*
483 {"first integer": 42,
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900484 "an array of two strings": [
485 "string1", "string2"
486 ],
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800487 "map in a map": {
488 "bytes 1": h'78787878',
489 "bytes 2": h'79797979',
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900490 "another int": 98,
491 "text 2": "lies, damn lies and statistics"
492 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800493 }
494 */
495
496static uint8_t pValidMapEncoded[] = {
Laurence Lundbladeb836efb2018-10-28 20:09:58 +0700497 0xa3, 0x6d, 0x66, 0x69, 0x72, 0x73, 0x74, 0x20, 0x69, 0x6e,
498 0x74, 0x65, 0x67, 0x65, 0x72, 0x18, 0x2a, 0x77, 0x61, 0x6e,
499 0x20, 0x61, 0x72, 0x72, 0x61, 0x79, 0x20, 0x6f, 0x66, 0x20,
500 0x74, 0x77, 0x6f, 0x20, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67,
501 0x73, 0x82, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x31,
502 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x32, 0x6c, 0x6d,
503 0x61, 0x70, 0x20, 0x69, 0x6e, 0x20, 0x61, 0x20, 0x6d, 0x61,
504 0x70, 0xa4, 0x67, 0x62, 0x79, 0x74, 0x65, 0x73, 0x20, 0x31,
505 0x44, 0x78, 0x78, 0x78, 0x78, 0x67, 0x62, 0x79, 0x74, 0x65,
506 0x73, 0x20, 0x32, 0x44, 0x79, 0x79, 0x79, 0x79, 0x6b, 0x61,
507 0x6e, 0x6f, 0x74, 0x68, 0x65, 0x72, 0x20, 0x69, 0x6e, 0x74,
508 0x18, 0x62, 0x66, 0x74, 0x65, 0x78, 0x74, 0x20, 0x32, 0x78,
509 0x1e, 0x6c, 0x69, 0x65, 0x73, 0x2c, 0x20, 0x64, 0x61, 0x6d,
510 0x6e, 0x20, 0x6c, 0x69, 0x65, 0x73, 0x20, 0x61, 0x6e, 0x64,
511 0x20, 0x73, 0x74, 0x61, 0x74, 0x69, 0x73, 0x74, 0x69, 0x63,
512 0x73 } ;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800513
514static 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)
515{
516 QCBORDecodeContext DCtx;
517 QCBORItem Item;
518 int nReturn = -1; // assume error until success
519
520 QCBORDecode_Init(&DCtx, (UsefulBufC){pEncoded, nLen}, QCBOR_DECODE_MODE_NORMAL);
521
522 // Make sure the first thing is a map
523 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || Item.uDataType != QCBOR_TYPE_ARRAY)
524 goto Done;
525
526 // First integer
Laurence Lundblade12b495d2018-12-17 11:15:54 -0800527 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || Item.uDataType != QCBOR_TYPE_INT64)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800528 goto Done;
529 *pInt1 = Item.val.int64;
530
531 // Second integer
532 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || Item.uDataType != QCBOR_TYPE_INT64)
533 goto Done;
534 *pInt2 = Item.val.int64;
535
536 // First string
537 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || Item.uDataType != QCBOR_TYPE_BYTE_STRING)
538 goto Done;
539 *pBuf3 = Item.val.string.ptr;
540 *pBuf3Len = Item.val.string.len;
541
542 // Second string
543 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || Item.uDataType != QCBOR_TYPE_BYTE_STRING)
544 goto Done;
545 *pBuf4 = Item.val.string.ptr;
546 *pBuf4Len = Item.val.string.len;
547
548 nReturn = 0;
549
550Done:
551 return(nReturn);
552}
553
554
555
Laurence Lundblade9e3651c2018-10-10 11:49:55 +0800556
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800557int SimpleArrayTest()
558{
559 uint8_t *pEncoded;
560 size_t nEncodedLen;
561
562 int64_t i1, i2;
563 size_t i3, i4;
564 const uint8_t *s3, *s4;
565
566
567 if(CreateSimpleArray(23, 6000, &pEncoded, &nEncodedLen) < 0) {
568 return(-1);
569 }
570
571 ParseOrderedArray(pEncoded, nEncodedLen, &i1, &i2, &s3, &i3, &s4, &i4);
572
573 if(i1 != 23 ||
574 i2 != 6000 ||
575 i3 != 8 ||
576 i4 != 11 ||
Laurence Lundbladea44d5062018-10-17 18:45:12 +0530577 memcmp("galactic", s3, 8) !=0 ||
578 memcmp("haven token", s4, 11) !=0) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800579 printf("SimpleArraryTest Failed\n");
580 return(-1);
581 }
582
583 return(0);
584}
585
586
Laurence Lundblade9e3651c2018-10-10 11:49:55 +0800587
Laurence Lundbladeb836efb2018-10-28 20:09:58 +0700588static uint8_t spDeepArrays[] = {0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x80};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800589
590int ParseDeepArrayTest()
591{
592 QCBORDecodeContext DCtx;
593 int nReturn = 0;
594 int i;
595
Laurence Lundbladeb836efb2018-10-28 20:09:58 +0700596 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spDeepArrays), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800597
598 for(i = 0; i < 10; i++) {
599 QCBORItem Item;
600
601 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
602 Item.uDataType != QCBOR_TYPE_ARRAY ||
603 Item.uNestingLevel != i) {
604 nReturn = -1;
605 break;
606 }
607 }
608
609 return(nReturn);
610}
611
Laurence Lundblade972e59c2018-11-11 15:57:23 +0700612// Big enough to test nesting to the depth of 24
613static uint8_t spTooDeepArrays[] = {0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81,
614 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81,
615 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81,
616 0x80};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800617
618int ParseTooDeepArrayTest()
619{
620 QCBORDecodeContext DCtx;
621 int nReturn = 0;
622 int i;
623 QCBORItem Item;
624
625
Laurence Lundbladeb836efb2018-10-28 20:09:58 +0700626 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTooDeepArrays), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800627
Laurence Lundblade972e59c2018-11-11 15:57:23 +0700628 for(i = 0; i < QCBOR_MAX_ARRAY_NESTING1; i++) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800629
630 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
631 Item.uDataType != QCBOR_TYPE_ARRAY ||
632 Item.uNestingLevel != i) {
633 nReturn = -1;
634 break;
635 }
636 }
637
638 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_ARRAY_NESTING_TOO_DEEP)
639 nReturn = -1;
640
641 return(nReturn);
642}
643
644
645
646
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800647int ShortBufferParseTest()
648{
649 int nResult = 0;
650 QCBORDecodeContext DCtx;
651 int num;
652
Laurence Lundbladeb836efb2018-10-28 20:09:58 +0700653 for(num = sizeof(spExpectedEncodedInts)-1; num; num--) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800654 int n;
655
Laurence Lundbladeb836efb2018-10-28 20:09:58 +0700656 QCBORDecode_Init(&DCtx, (UsefulBufC){spExpectedEncodedInts, num}, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800657
658 n = IntegerValuesParseTestInternal(&DCtx);
659
660 //printf("Len %d, result: %d\n", num, n);
661
662 if(n != QCBOR_ERR_HIT_END) {
663 nResult = -1;
664 goto Done;
665 }
666 }
667Done:
668 return nResult;
669}
670
671
Laurence Lundblade9e3651c2018-10-10 11:49:55 +0800672
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800673int ShortBufferParseTest2()
674{
675 uint8_t *pEncoded;
676 int nReturn;
677 size_t nEncodedLen;
678
679 int64_t i1, i2;
680 size_t i3, i4;
681 const uint8_t *s3, *s4;
682
683 nReturn = 0;
684
685 if(CreateSimpleArray(23, 6000, &pEncoded, &nEncodedLen) < 0) {
686 return(-1);
687 }
688
689 //printencoded(pEncoded, nEncodedLen);
690
691 for(nEncodedLen--; nEncodedLen; nEncodedLen--) {
692 int nResult = ParseOrderedArray(pEncoded, (uint32_t)nEncodedLen, &i1, &i2, &s3, &i3, &s4, &i4);
693 if(nResult == 0) {
694 nReturn = -1;
695 }
696 }
697
698 return(nReturn);
699}
700
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530701/*
702 Decode and thoroughly check a moderately complex
703 set of maps
704 */
Laurence Lundbladeea567ac2018-12-09 14:03:21 -0800705static int ParseMapTest1(QCBORDecodeMode nMode)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800706{
707 QCBORDecodeContext DCtx;
708 QCBORItem Item;
709 int nCBORError;
710
Laurence Lundbladeea567ac2018-12-09 14:03:21 -0800711 QCBORDecode_Init(&DCtx, (UsefulBufC){pValidMapEncoded, sizeof(pValidMapEncoded)}, nMode);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800712
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900713 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800714 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900715 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800716 if(Item.uDataType != QCBOR_TYPE_MAP ||
717 Item.val.uCount != 3)
718 return -1;
719
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900720 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800721 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900722 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800723 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800724 Item.uDataType != QCBOR_TYPE_INT64 ||
725 Item.val.int64 != 42 ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530726 Item.uDataAlloc ||
727 Item.uLabelAlloc ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900728 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("first integer"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800729 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900730 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800731
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900732 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800733 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900734 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800735 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530736 Item.uDataAlloc ||
737 Item.uLabelAlloc ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900738 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("an array of two strings")) ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800739 Item.uDataType != QCBOR_TYPE_ARRAY ||
740 Item.val.uCount != 2)
741 return -1;
742
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900743 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800744 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900745 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800746 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530747 Item.uDataAlloc ||
748 Item.uLabelAlloc ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900749 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("string1"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800750 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900751 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800752
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900753 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800754 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900755 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800756 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530757 Item.uDataAlloc ||
758 Item.uLabelAlloc ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900759 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("string2"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800760 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900761 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800762
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900763 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800764 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900765 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800766 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530767 Item.uDataAlloc ||
768 Item.uLabelAlloc ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900769 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("map in a map")) ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800770 Item.uDataType != QCBOR_TYPE_MAP ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900771 Item.val.uCount != 4) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800772 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900773 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800774
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900775 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800776 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900777 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800778 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900779 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("bytes 1"))||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800780 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530781 Item.uDataAlloc ||
782 Item.uLabelAlloc ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900783 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("xxxx"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800784 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900785 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800786
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900787 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800788 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900789 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800790 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900791 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("bytes 2")) ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800792 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530793 Item.uDataAlloc ||
794 Item.uLabelAlloc ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900795 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("yyyy"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800796 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900797 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800798
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900799 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800800 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900801 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800802 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530803 Item.uDataAlloc ||
804 Item.uLabelAlloc ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900805 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("another int")) ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800806 Item.uDataType != QCBOR_TYPE_INT64 ||
807 Item.val.int64 != 98)
808 return -1;
809
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900810 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800811 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900812 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800813 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900814 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("text 2"))||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800815 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530816 Item.uDataAlloc ||
817 Item.uLabelAlloc ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900818 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("lies, damn lies and statistics"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800819 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900820 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800821
822 return 0;
823}
824
825
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900826/*
827 Decode and thoroughly check a moderately complex
828 set of maps
829 */
830int ParseMapAsArrayTest()
831{
832 QCBORDecodeContext DCtx;
833 QCBORItem Item;
834 int nCBORError;
835
836 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), QCBOR_DECODE_MODE_MAP_AS_ARRAY);
837
838 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
839 return nCBORError;
840 }
Laurence Lundbladed61cbf32018-12-09 11:42:21 -0800841 if(Item.uDataType != QCBOR_TYPE_MAP_AS_ARRAY ||
842 Item.val.uCount != 6) {
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900843 return -1;
844 }
845
846 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
847 return nCBORError;
848 }
849 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
850 Item.uDataAlloc ||
851 Item.uLabelAlloc ||
852 Item.uLabelType != QCBOR_TYPE_NONE ||
853 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("first integer"))) {
854 return -2;
855 }
856
857 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
858 return nCBORError;
859 }
860 if(Item.uLabelType != QCBOR_TYPE_NONE ||
861 Item.uDataType != QCBOR_TYPE_INT64 ||
862 Item.val.int64 != 42 ||
863 Item.uDataAlloc ||
864 Item.uLabelAlloc) {
865 return -3;
866 }
867
868 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
869 return nCBORError;
870 }
871 if(Item.uLabelType != QCBOR_TYPE_NONE ||
872 Item.uDataAlloc ||
873 Item.uLabelAlloc ||
874 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("an array of two strings")) ||
875 Item.uDataType != QCBOR_TYPE_TEXT_STRING) {
876 return -4;
877 }
878
879 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
880 return nCBORError;
881 }
882 if(Item.uLabelType != QCBOR_TYPE_NONE ||
883 Item.uDataAlloc ||
884 Item.uLabelAlloc ||
885 Item.uDataType != QCBOR_TYPE_ARRAY ||
886 Item.val.uCount != 2) {
887 return -5;
888 }
889
890 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
891 return nCBORError;
892 }
893 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
894 Item.val.string.len != 7 ||
895 Item.uDataAlloc ||
896 Item.uLabelAlloc ||
897 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("string1"))) {
898 return -6;
899 }
900
901 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
902 return nCBORError;
903 }
904 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
905 Item.uDataAlloc ||
906 Item.uLabelAlloc ||
907 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("string2"))) {
908 return -7;
909 }
910
911
912 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
913 return nCBORError;
914 }
915 if(Item.uLabelType != QCBOR_TYPE_NONE ||
916 Item.uDataAlloc ||
917 Item.uLabelAlloc ||
918 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("map in a map"))) {
919 return -8;
920 }
921
922 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
923 return nCBORError;
924 }
925 if(Item.uLabelType != QCBOR_TYPE_NONE ||
926 Item.uDataAlloc ||
927 Item.uLabelAlloc ||
Laurence Lundbladed61cbf32018-12-09 11:42:21 -0800928 Item.uDataType != QCBOR_TYPE_MAP_AS_ARRAY ||
929 Item.val.uCount != 8) {
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900930 return -9;
931 }
932
933 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
934 return nCBORError;
935 }
936 if(Item.uLabelType != QCBOR_TYPE_NONE ||
937 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("bytes 1"))||
938 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
939 Item.uDataAlloc ||
940 Item.uLabelAlloc) {
941 return -10;
942 }
943
944 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
945 return nCBORError;
946 }
947 if(Item.uLabelType != QCBOR_TYPE_NONE ||
948 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
949 Item.uDataAlloc ||
950 Item.uLabelAlloc ||
951 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("xxxx"))) {
952 return -11;
953 }
954
955 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
956 return nCBORError;
957 }
958 if(Item.uLabelType != QCBOR_TYPE_NONE ||
959 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("bytes 2")) ||
960 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
961 Item.uDataAlloc ||
962 Item.uLabelAlloc) {
963 return -12;
964 }
965
966 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
967 return nCBORError;
968 }
969 if(Item.uLabelType != QCBOR_TYPE_NONE ||
970 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
971 Item.uDataAlloc ||
972 Item.uLabelAlloc ||
973 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("yyyy"))) {
974 return -13;
975 }
976
977 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
978 return nCBORError;
979 }
980 if(Item.uLabelType != QCBOR_TYPE_NONE ||
981 Item.uDataAlloc ||
982 Item.uLabelAlloc ||
983 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("another int")) ||
984 Item.uDataType != QCBOR_TYPE_TEXT_STRING) {
985 return -14;
986 }
987
988 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
989 return nCBORError;
990 }
991 if(Item.uLabelType != QCBOR_TYPE_NONE ||
992 Item.uDataAlloc ||
993 Item.uLabelAlloc ||
994 Item.uDataType != QCBOR_TYPE_INT64 ||
995 Item.val.int64 != 98) {
996 return -15;
997 }
998
999 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
1000 return nCBORError;
1001 }
1002 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1003 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("text 2"))||
1004 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1005 Item.uDataAlloc ||
1006 Item.uLabelAlloc) {
1007 return -16;
1008 }
1009
1010 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
1011 return nCBORError;
1012 }
1013 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1014 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1015 Item.uDataAlloc ||
1016 Item.uLabelAlloc ||
1017 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("lies, damn lies and statistics"))) {
1018 return -17;
1019 }
1020
1021 return 0;
1022}
1023
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001024
1025/*
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301026 Fully or partially decode pValidMapEncoded. When
1027 partially decoding check for the right error code.
1028 How much partial decoding depends on nLevel.
1029
1030 The partial decodes test error conditions of
1031 incomplete encoded input.
1032
1033 This could be combined with the above test
1034 and made prettier and maybe a little more
1035 thorough.
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001036 */
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001037static int ExtraBytesTest(int nLevel)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001038{
1039 QCBORDecodeContext DCtx;
1040 QCBORItem Item;
1041 int nCBORError;
1042
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001043 QCBORDecode_Init(&DCtx, (UsefulBufC){pValidMapEncoded, sizeof(pValidMapEncoded)}, QCBOR_DECODE_MODE_NORMAL);
1044
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001045 if(nLevel < 1) {
1046 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_EXTRA_BYTES) {
1047 return -1;
1048 } else {
1049 return 0;
1050 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001051 }
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301052
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001053
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001054 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001055 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001056 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001057 if(Item.uDataType != QCBOR_TYPE_MAP ||
1058 Item.val.uCount != 3)
1059 return -1;
1060
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001061 if(nLevel < 2) {
1062 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
1063 return -1;
1064 } else {
1065 return 0;
1066 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001067 }
1068
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001069
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001070 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001071 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001072 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001073 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001074 Item.uDataType != QCBOR_TYPE_INT64 ||
1075 Item.val.uCount != 42 ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001076 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("first integer"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001077 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001078 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001079
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001080 if(nLevel < 3) {
1081 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
1082 return -1;
1083 } else {
1084 return 0;
1085 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001086 }
1087
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001088 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001089 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001090 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001091 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001092 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("an array of two strings")) ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001093 Item.uDataType != QCBOR_TYPE_ARRAY ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001094 Item.val.uCount != 2) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001095 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001096 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001097
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001098
1099 if(nLevel < 4) {
1100 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
1101 return -1;
1102 } else {
1103 return 0;
1104 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001105 }
1106
1107
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001108 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001109 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001110 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001111 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001112 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("string1"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001113 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001114 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001115
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001116 if(nLevel < 5) {
1117 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
1118 return -1;
1119 } else {
1120 return 0;
1121 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001122 }
1123
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001124 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001125 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001126 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001127 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001128 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("string2"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001129 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001130 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001131
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001132 if(nLevel < 6) {
1133 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
1134 return -1;
1135 } else {
1136 return 0;
1137 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001138 }
1139
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001140 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001141 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001142 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001143 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001144 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("map in a map")) ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001145 Item.uDataType != QCBOR_TYPE_MAP ||
1146 Item.val.uCount != 4)
1147 return -1;
1148
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001149 if(nLevel < 7) {
1150 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
1151 return -1;
1152 } else {
1153 return 0;
1154 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001155 }
1156
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001157 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001158 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001159 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001160 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001161 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("bytes 1")) ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001162 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001163 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("xxxx"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001164 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001165 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001166
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001167 if(nLevel < 8) {
1168 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
1169 return -1;
1170 } else {
1171 return 0;
1172 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001173 }
1174
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001175 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001176 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001177 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001178 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001179 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("bytes 2")) ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001180 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001181 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("yyyy"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001182 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001183 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001184
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001185 if(nLevel < 9) {
1186 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
1187 return -1;
1188 } else {
1189 return 0;
1190 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001191 }
1192
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001193 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001194 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001195 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001196 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001197 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("another int")) ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001198 Item.uDataType != QCBOR_TYPE_INT64 ||
1199 Item.val.int64 != 98)
1200 return -1;
1201
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001202 if(nLevel < 10) {
1203 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
1204 return -1;
1205 } else {
1206 return 0;
1207 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001208 }
1209
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001210 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001211 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001212 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001213 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001214 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("text 2"))||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001215 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001216 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("lies, damn lies and statistics"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001217 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001218 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001219
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301220 if(QCBORDecode_Finish(&DCtx)) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001221 return -1;
1222 }
1223
1224 return 0;
1225}
1226
1227
1228
1229
1230int ParseMapTest()
1231{
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301232 // Parse a moderatly complex map structure very thoroughl
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08001233 int n = ParseMapTest1(QCBOR_DECODE_MODE_NORMAL);
1234
1235 n = ParseMapTest1(QCBOR_DECODE_MODE_MAP_STRINGS_ONLY);
1236
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001237 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 Lundbladeea567ac2018-12-09 14:03:21 -08002108
2109
2110int StringDecoderModeFailTest()
2111{
2112 QCBORDecodeContext DCtx;
2113
2114 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput), QCBOR_DECODE_MODE_MAP_STRINGS_ONLY);
2115
2116 QCBORItem Item;
2117 QCBORError nCBORError;
2118
2119 if(QCBORDecode_GetNext(&DCtx, &Item)) {
2120 return -1;
2121 }
2122 if(Item.uDataType != QCBOR_TYPE_MAP) {
2123 return -2;
2124 }
2125
2126 nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
2127 if(nCBORError != QCBOR_ERR_MAP_LABEL_TYPE) {
2128 return -3;
2129 }
2130
2131 return 0;
2132}
2133
2134
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002135// Same map as above, but using indefinite lengths
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002136static uint8_t spCSRInputIndefLen[] = {
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002137 0xbf, 0x36, 0xbf, 0x33, 0xbf, 0x31, 0x6c, 0x4f,
2138 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74,
2139 0x69, 0x6f, 0x6e, 0x30, 0x63, 0x53, 0x53, 0x47,
2140 0x2e, 0x69, 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73,
2141 0x69, 0x6f, 0x6e, 0x2f, 0x69, 0x53, 0x61, 0x6e,
2142 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f, 0x2d, 0x62,
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002143 0x55, 0x53, 0xff, 0x32, 0xbf, 0x2a, 0xbf, 0x28,
2144 0x26, 0xff, 0x29, 0x4a, 0x01, 0x02, 0x03, 0x04,
2145 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0xff, 0xff,
2146 0x35, 0xbf, 0x24, 0x22, 0xff, 0xff};
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002147
2148int NestedMapTestIndefLen()
2149{
2150 QCBORDecodeContext DCtx;
2151
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002152 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInputIndefLen), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002153
2154 return CheckCSRMaps(&DCtx);
2155}
2156
2157
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002158
Laurence Lundblade17ede402018-10-13 11:43:07 +08002159static UsefulBufC make_nested_indefinite_arrays(int n, UsefulBuf Storage)
2160{
2161 UsefulOutBuf UOB;
2162 UsefulOutBuf_Init(&UOB, Storage);
2163
2164 int i;
2165 for(i = 0; i < n; i++) {
2166 UsefulOutBuf_AppendByte(&UOB, 0x9f);
2167 }
2168
2169 for(i = 0; i < n; i++) {
2170 UsefulOutBuf_AppendByte(&UOB, 0xff);
2171 }
2172 return UsefulOutBuf_OutUBuf(&UOB);
2173}
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002174
2175
Laurence Lundblade17ede402018-10-13 11:43:07 +08002176static int parse_indeflen_nested(UsefulBufC Nested, int nNestLevel)
2177{
2178 QCBORDecodeContext DC;
2179 QCBORDecode_Init(&DC, Nested, 0);
2180
2181 int j;
2182 for(j = 0; j < nNestLevel; j++) {
2183 QCBORItem Item;
2184 int nReturn = QCBORDecode_GetNext(&DC, &Item);
2185 if(j >= QCBOR_MAX_ARRAY_NESTING) {
2186 // Should be in error
2187 if(nReturn != QCBOR_ERR_ARRAY_NESTING_TOO_DEEP) {
2188 return -4;
2189 } else {
2190 return 0; // Decoding doesn't recover after an error
2191 }
2192 } else {
2193 // Should be no error
2194 if(nReturn) {
2195 return -9; // Should not have got an error
2196 }
2197 }
2198 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
2199 return -7;
2200 }
2201 }
2202 int nReturn = QCBORDecode_Finish(&DC);
2203 if(nReturn) {
2204 return -3;
2205 }
2206 return 0;
2207}
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002208
2209
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302210int IndefiniteLengthNestTest()
Laurence Lundblade17ede402018-10-13 11:43:07 +08002211{
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05302212 UsefulBuf_MAKE_STACK_UB(Storage, 50);
Laurence Lundblade17ede402018-10-13 11:43:07 +08002213 int i;
Laurence Lundblade6de37062018-10-15 12:22:42 +05302214 for(i=1; i < QCBOR_MAX_ARRAY_NESTING+4; i++) {
Laurence Lundblade17ede402018-10-13 11:43:07 +08002215 UsefulBufC Nested = make_nested_indefinite_arrays(i, Storage);
2216 int nReturn = parse_indeflen_nested(Nested, i);
2217 if(nReturn) {
2218 return nReturn;
2219 }
2220 }
2221 return 0;
2222}
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002223
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002224
Laurence Lundblade6de37062018-10-15 12:22:42 +05302225
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002226static const uint8_t spIndefiniteArray[] = {0x9f, 0x01, 0x82, 0x02, 0x03, 0xff}; // [1, [2, 3]]
2227static const uint8_t spIndefiniteArrayBad1[] = {0x9f}; // No closing break
2228static const uint8_t spIndefiniteArrayBad2[] = {0x9f, 0x9f, 0x02, 0xff}; // Not enough closing breaks
2229static const uint8_t spIndefiniteArrayBad3[] = {0x9f, 0x02, 0xff, 0xff}; // Too many closing breaks
2230static const uint8_t spIndefiniteArrayBad4[] = {0x81, 0x9f}; // Unclosed indeflen inside def len
2231static const uint8_t spIndefiniteArrayBad5[] = {0x9f, 0xd1, 0xff}; // confused tag
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002232
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302233int IndefiniteLengthArrayMapTest()
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002234{
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002235 int nResult;
2236 // --- first test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002237 UsefulBufC IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArray);
Laurence Lundblade17ede402018-10-13 11:43:07 +08002238
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002239 // Decode it and see if it is OK
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05302240 UsefulBuf_MAKE_STACK_UB(MemPool, 150);
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002241 QCBORDecodeContext DC;
2242 QCBORItem Item;
2243 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
2244
2245 QCBORDecode_SetMemPool(&DC, MemPool, false);
Laurence Lundblade6de37062018-10-15 12:22:42 +05302246
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002247 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05302248
2249 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
2250 Item.uNestingLevel != 0 ||
2251 Item.uNextNestLevel != 1) {
2252 return -111;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002253 }
2254
2255 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05302256 if(Item.uDataType != QCBOR_TYPE_INT64 ||
2257 Item.uNestingLevel != 1 ||
2258 Item.uNextNestLevel != 1) {
2259 return -2;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002260 }
2261
2262 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05302263 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
2264 Item.uNestingLevel != 1 ||
2265 Item.uNextNestLevel != 2) {
2266 return -3;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002267 }
2268
2269 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade12b495d2018-12-17 11:15:54 -08002270 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade6de37062018-10-15 12:22:42 +05302271 Item.uNestingLevel != 2 ||
2272 Item.uNextNestLevel != 2) {
2273 return -4;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002274 }
2275
2276 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade12b495d2018-12-17 11:15:54 -08002277 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade6de37062018-10-15 12:22:42 +05302278 Item.uNestingLevel != 2 ||
2279 Item.uNextNestLevel != 0) {
2280 return -5;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002281 }
2282
2283 if(QCBORDecode_Finish(&DC)) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05302284 return -6;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002285 }
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002286
2287 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002288 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad1);
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002289
2290 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
2291
2292 QCBORDecode_SetMemPool(&DC, MemPool, false);
2293
2294 nResult = QCBORDecode_GetNext(&DC, &Item);
2295 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05302296 return -7;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002297 }
2298
Laurence Lundblade570fab52018-10-13 18:28:27 +08002299 nResult = QCBORDecode_Finish(&DC);
Laurence Lundblade6de37062018-10-15 12:22:42 +05302300 if(nResult != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
2301 return -8;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002302 }
2303
2304
2305 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002306 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad2);
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002307
2308 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
2309
2310 QCBORDecode_SetMemPool(&DC, MemPool, false);
2311
2312 nResult = QCBORDecode_GetNext(&DC, &Item);
2313 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05302314 return -9;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002315 }
2316
2317 nResult = QCBORDecode_GetNext(&DC, &Item);
2318 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05302319 return -10;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002320 }
2321
2322 nResult = QCBORDecode_GetNext(&DC, &Item);
2323 if(nResult || Item.uDataType != QCBOR_TYPE_INT64) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05302324 return -11;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002325 }
2326
2327 nResult = QCBORDecode_Finish(&DC);
Laurence Lundblade6de37062018-10-15 12:22:42 +05302328 if(nResult != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
2329 return -12;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002330 }
2331
2332
2333 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002334 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad3);
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002335
2336 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
2337
2338 QCBORDecode_SetMemPool(&DC, MemPool, false);
2339
2340 nResult = QCBORDecode_GetNext(&DC, &Item);
2341 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05302342 return -13;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002343 }
2344
2345 nResult = QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05302346 if(nResult != QCBOR_ERR_BAD_BREAK) {
2347 return -14;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002348 }
Laurence Lundblade6de37062018-10-15 12:22:42 +05302349
Laurence Lundblade570fab52018-10-13 18:28:27 +08002350
2351 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002352 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad4);
Laurence Lundblade570fab52018-10-13 18:28:27 +08002353
2354 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
2355
2356 QCBORDecode_SetMemPool(&DC, MemPool, false);
2357
2358 nResult = QCBORDecode_GetNext(&DC, &Item);
2359 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05302360 return -15;
Laurence Lundblade570fab52018-10-13 18:28:27 +08002361 }
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302362
Laurence Lundblade570fab52018-10-13 18:28:27 +08002363 nResult = QCBORDecode_GetNext(&DC, &Item);
2364 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05302365 return -16;
Laurence Lundblade570fab52018-10-13 18:28:27 +08002366 }
2367
2368 nResult = QCBORDecode_Finish(&DC);
Laurence Lundblade6de37062018-10-15 12:22:42 +05302369 if(nResult != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
2370 return -17;
Laurence Lundblade570fab52018-10-13 18:28:27 +08002371 }
2372
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302373 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002374 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad5);
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302375
2376 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
2377
2378 QCBORDecode_SetMemPool(&DC, MemPool, false);
2379
2380 nResult = QCBORDecode_GetNext(&DC, &Item);
2381 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05302382 return -18;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302383 }
2384
2385 nResult = QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05302386 if(nResult != QCBOR_ERR_BAD_BREAK) {
2387 return -19;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302388 }
2389
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002390 return 0;
2391}
2392
Laurence Lundblade17ede402018-10-13 11:43:07 +08002393
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002394static const uint8_t spIndefiniteLenString[] = {
Laurence Lundblade17ede402018-10-13 11:43:07 +08002395 0x81, // Array of length one
2396 0x7f, // text string marked with indefinite length
2397 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment
2398 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment
2399 0xff // ending break
2400};
2401
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002402static const uint8_t spIndefiniteLenStringBad2[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302403 0x81, // Array of length one
2404 0x7f, // text string marked with indefinite length
2405 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment
2406 0x44, 0x6d, 0x69, 0x6e, 0x67, // second segment of wrong type
2407 0xff // ending break
2408};
2409
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002410static const uint8_t spIndefiniteLenStringBad3[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302411 0x81, // Array of length one
2412 0x7f, // text string marked with indefinite length
2413 0x01, 0x02, // Not a string
2414 0xff // ending break
2415};
2416
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002417static const uint8_t spIndefiniteLenStringBad4[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302418 0x81, // Array of length one
2419 0x7f, // text string marked with indefinite length
2420 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment
2421 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment
2422 // missing end of string
2423};
2424
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002425static const uint8_t spIndefiniteLenStringLabel[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302426 0xa1, // Array of length one
2427 0x7f, // text string marked with indefinite length
2428 0x65, 0x73, 0x74, 0x72, 0x75, 0x75, // first segment
2429 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment
2430 0xff, // ending break
2431 0x01 // integer being labeled.
2432};
2433
2434static UsefulBufC MakeIndefiniteBigBstr(UsefulBuf Storage)
2435{
2436 UsefulOutBuf UOB;
2437
2438 UsefulOutBuf_Init(&UOB, Storage);
2439 UsefulOutBuf_AppendByte(&UOB, 0x81);
2440 UsefulOutBuf_AppendByte(&UOB, 0x5f);
2441
2442 int i = 0;
2443 for(int nChunkSize = 1; nChunkSize <= 128; nChunkSize *= 2) {
2444 UsefulOutBuf_AppendByte(&UOB, 0x58);
2445 UsefulOutBuf_AppendByte(&UOB, (uint8_t)nChunkSize);
2446 for(int j = 0; j < nChunkSize; j++ ) {
2447 UsefulOutBuf_AppendByte(&UOB, i);
2448 i++;
2449 }
2450 }
2451 UsefulOutBuf_AppendByte(&UOB, 0xff);
2452
2453 return UsefulOutBuf_OutUBuf(&UOB);
2454}
2455
2456static int CheckBigString(UsefulBufC BigString)
2457{
2458 if(BigString.len != 255) {
2459 return 1;
2460 }
2461
2462 for(uint8_t i = 0; i < 255; i++){
2463 if(((const uint8_t *)BigString.ptr)[i] != i) {
2464 return 1;
2465 }
2466 }
2467 return 0;
2468}
2469
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05302470
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302471int IndefiniteLengthStringTest()
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302472{
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302473 QCBORDecodeContext DC;
2474 QCBORItem Item;
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05302475 // big enough for MakeIndefiniteBigBstr() + MemPool overhead
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05302476 UsefulBuf_MAKE_STACK_UB(MemPool, 320);
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302477
2478 // --- Simple normal indefinite length string ------
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002479 UsefulBufC IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenString);
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302480 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002481
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05302482 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302483 return -1;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302484 }
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002485
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302486 if(QCBORDecode_GetNext(&DC, &Item)) {
2487 return -2;
2488 }
2489 if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.uDataAlloc) {
2490 return -3;
2491 }
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002492
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302493 if(QCBORDecode_GetNext(&DC, &Item)) {
2494 return -4;
2495 }
2496 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || !Item.uDataAlloc) {
2497 return -5;
2498 }
2499 if(QCBORDecode_Finish(&DC)) {
2500 return -6;
2501 }
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302502
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302503 // ----- types mismatch ---
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002504 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad2), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302505
2506 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
2507 return -7;
2508 }
2509
2510 if(QCBORDecode_GetNext(&DC, &Item)) {
2511 return -8;
2512 }
2513 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
2514 return -9;
2515 }
2516
Laurence Lundblade30816f22018-11-10 13:40:22 +07002517 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_INDEFINITE_STRING_CHUNK) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302518 return -10;
2519 }
2520
2521 // ----- not a string ---
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002522 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad3), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302523
2524 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
2525 return -11;
2526 }
2527
2528 if(QCBORDecode_GetNext(&DC, &Item)) {
2529 return -12;
2530 }
2531 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
2532 return -13;
2533 }
2534
Laurence Lundblade30816f22018-11-10 13:40:22 +07002535 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_INDEFINITE_STRING_CHUNK) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302536 return -14;
2537 }
2538
2539 // ----- no end -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002540 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad4), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302541
2542 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
2543 return -15;
2544 }
2545
2546 if(QCBORDecode_GetNext(&DC, &Item)) {
2547 return -16;
2548 }
2549 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
2550 return -17;
2551 }
2552
2553 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_HIT_END) {
2554 return -18;
2555 }
2556
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302557 // ------ Don't set a string allocator and see an error -----
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302558 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
2559
2560 QCBORDecode_GetNext(&DC, &Item);
2561 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302562 return -19;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302563 }
2564
2565 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_NO_STRING_ALLOCATOR) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302566 return -20;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302567 }
2568
2569 // ----- Mempool is way too small -----
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05302570 UsefulBuf_MAKE_STACK_UB(MemPoolTooSmall, 20); // 20 is too small no matter what
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302571
2572 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
2573 if(!QCBORDecode_SetMemPool(&DC, MemPoolTooSmall, false)) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302574 return -21;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302575 }
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302576
2577 // ----- Mempool is way too small -----
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05302578 UsefulBuf_MAKE_STACK_UB(BigIndefBStrStorage, 290);
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302579 UsefulBufC BigIndefBStr = MakeIndefiniteBigBstr(BigIndefBStrStorage);
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302580
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05302581 UsefulBuf_MAKE_STACK_UB(MemPoolSmall, 80); // 80 is big enough for MemPool overhead, but not BigIndefBStr
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302582
2583 QCBORDecode_Init(&DC, BigIndefBStr, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302584 if(QCBORDecode_SetMemPool(&DC, MemPoolSmall, false)) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302585 return -22;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302586 }
2587
2588 QCBORDecode_GetNext(&DC, &Item);
2589 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302590 return -23;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302591 }
Laurence Lundblade30816f22018-11-10 13:40:22 +07002592 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_STRING_ALLOCATE) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302593 return -24;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302594 }
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302595
2596 // ---- big bstr -----
2597 QCBORDecode_Init(&DC, BigIndefBStr, QCBOR_DECODE_MODE_NORMAL);
2598
2599 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
2600 return -25;
2601 }
2602
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05302603 if(QCBORDecode_GetNext(&DC, &Item)) {
2604 return -26;
2605 }
2606 if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.uDataAlloc) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302607 return -26;
2608 }
2609
2610 if(QCBORDecode_GetNext(&DC, &Item)) {
2611 return -27;
2612 }
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05302613 if(Item.uDataType != QCBOR_TYPE_BYTE_STRING || !Item.uDataAlloc || Item.uNestingLevel != 1) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302614 return -28;
2615 }
2616 if(CheckBigString(Item.val.string)) {
2617 return -3;
2618 }
2619 if(QCBORDecode_Finish(&DC)) {
2620 return -29;
2621 }
2622
2623 // --- label is an indefinite length string ------
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002624 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringLabel), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302625
2626 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
2627 return -30;
2628 }
2629
2630 QCBORDecode_GetNext(&DC, &Item);
2631 if(Item.uDataType != QCBOR_TYPE_MAP) {
2632 return -31;
2633 }
2634
2635 if(QCBORDecode_GetNext(&DC, &Item)){
2636 return -32;
2637 }
2638 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || Item.uDataType != QCBOR_TYPE_INT64 ||
2639 Item.uDataAlloc || !Item.uLabelAlloc ||
2640 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("struuming"))) {
2641 return -33;
2642 }
2643
2644 if(QCBORDecode_Finish(&DC)) {
2645 return -34;
2646 }
2647
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002648 return 0;
2649}
2650
2651
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302652int AllocAllStringsTest()
2653{
2654 QCBORDecodeContext DC;
2655
2656 // First test, use the "CSRMap" as easy input and checking
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002657 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302658
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05302659 UsefulBuf_MAKE_STACK_UB(Pool, 300);
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302660
2661 QCBORDecode_SetMemPool(&DC, Pool, 1); // Turn on copying.
2662
2663 if(CheckCSRMaps(&DC)) {
2664 return -1;
2665 }
2666
2667 // Next parse, save pointers to a few strings, destroy original and see all is OK.
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05302668 UsefulBuf_MAKE_STACK_UB(CopyOfStorage, 160);
2669 UsefulBufC CopyOf = UsefulBuf_Copy(CopyOfStorage, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded));
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002670
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302671 QCBORDecode_Init(&DC, CopyOf, QCBOR_DECODE_MODE_NORMAL);
2672 QCBORDecode_SetMemPool(&DC, Pool, 1); // Turn on copying.
2673
2674 int nCBORError;
2675 QCBORItem Item1, Item2, Item3, Item4;
2676 if((nCBORError = QCBORDecode_GetNext(&DC, &Item1)))
2677 return nCBORError;
2678 if(Item1.uDataType != QCBOR_TYPE_MAP ||
2679 Item1.val.uCount != 3)
2680 return -1;
2681 if((nCBORError = QCBORDecode_GetNext(&DC, &Item1)))
2682 return nCBORError;
2683 if((nCBORError = QCBORDecode_GetNext(&DC, &Item2)))
2684 return nCBORError;
2685 if((nCBORError = QCBORDecode_GetNext(&DC, &Item3)))
2686 return nCBORError;
2687 if((nCBORError = QCBORDecode_GetNext(&DC, &Item4)))
2688 return nCBORError;
2689
Laurence Lundblade05ec57b2018-10-21 01:50:03 +05302690 UsefulBuf_Set(CopyOfStorage, '_');
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302691
2692 if(Item1.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302693 Item1.uDataType != QCBOR_TYPE_INT64 ||
2694 Item1.val.int64 != 42 ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09002695 UsefulBuf_Compare(Item1.label.string, UsefulBuf_FromSZ("first integer"))) {
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302696 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09002697 }
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302698
2699
2700 if(Item2.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09002701 UsefulBuf_Compare(Item2.label.string, UsefulBuf_FromSZ("an array of two strings")) ||
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302702 Item2.uDataType != QCBOR_TYPE_ARRAY ||
2703 Item2.val.uCount != 2)
2704 return -1;
2705
2706 if(Item3.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09002707 UsefulBuf_Compare(Item3.val.string, UsefulBuf_FromSZ("string1"))) {
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302708 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09002709 }
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302710
2711 if(Item4.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09002712 UsefulBuf_Compare(Item4.val.string, UsefulBuf_FromSZ("string2"))) {
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302713 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09002714 }
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302715
2716 // Next parse with a pool that is too small
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05302717 UsefulBuf_MAKE_STACK_UB(SmallPool, 80);
2718 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302719 QCBORDecode_SetMemPool(&DC, SmallPool, 1); // Turn on copying.
2720 if((nCBORError = QCBORDecode_GetNext(&DC, &Item1)))
2721 return nCBORError;
2722 if(Item1.uDataType != QCBOR_TYPE_MAP ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09002723 Item1.val.uCount != 3) {
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302724 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09002725 }
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302726 if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item1))){
2727 if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item2))) {
2728 if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item3))) {
2729 nCBORError = QCBORDecode_GetNext(&DC, &Item4);
2730 }
2731 }
2732 }
Laurence Lundblade30816f22018-11-10 13:40:22 +07002733 if(nCBORError != QCBOR_ERR_STRING_ALLOCATE) {
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302734 return -5;
2735 }
2736
2737 return 0;
2738}
2739
Laurence Lundbladef6531662018-12-04 10:42:22 +09002740// Cheating declaration to get to the special test hook
2741size_t MemPoolTestHook_GetPoolSize(void *ctx);
2742
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302743
2744int MemPoolTest(void)
Laurence Lundblade0155b622018-10-12 20:04:37 +08002745{
Laurence Lundbladef6531662018-12-04 10:42:22 +09002746 // Set up the decoder with a tiny bit of CBOR to parse
2747 QCBORDecodeContext DC;
2748 const uint8_t pMinimalCBOR[] = {0xa0}; // One empty map
2749 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pMinimalCBOR),0);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002750
Laurence Lundbladef6531662018-12-04 10:42:22 +09002751 // Set up an memory pool of 100 bytes
2752 UsefulBuf_MAKE_STACK_UB(Pool, 100);
2753 QCBORDecode_SetMemPool(&DC, Pool, 0);
2754
2755 // Cheat a little to get to the string allocator object
2756 // so we can call it directly to test it
2757 QCBORStringAllocator *pAlloc = (QCBORStringAllocator *)DC.pStringAllocator;
2758 // Cheat some more to know exactly the
2759 size_t uAvailPool = MemPoolTestHook_GetPoolSize(pAlloc);
2760
2761 // First test -- ask for too much in one go
2762 UsefulBuf Allocated = (*pAlloc->fAllocate)(pAlloc->pAllocaterContext, NULL, uAvailPool+1);
2763 if(!UsefulBuf_IsNULL(Allocated)) {
2764 return -1;
2765 }
2766
2767
2768 // Re do the set up for the next test that will do a successful alloc,
2769 // a fail, a free and then success
2770 // This test should work on 32 and 64-bit machines if the compiler
2771 // does the expected thing with pointer sizes for the internal
2772 // MemPool implementation leaving 44 or 72 bytes of pool memory.
2773 QCBORDecode_SetMemPool(&DC, Pool, 0);
2774
2775 // Cheat a little to get to the string allocator object
2776 // so we can call it directly to test it
2777 pAlloc = (QCBORStringAllocator *)DC.pStringAllocator;
2778 // Cheat some more to know exactly the
2779 uAvailPool = MemPoolTestHook_GetPoolSize(pAlloc);
2780
2781 Allocated = (*pAlloc->fAllocate)(pAlloc->pAllocaterContext, NULL, uAvailPool-1);
2782 if(UsefulBuf_IsNULL(Allocated)) { // expected to succeed
2783 return -2;
2784 }
2785 UsefulBuf Allocated2 = (*pAlloc->fAllocate)(pAlloc->pAllocaterContext, NULL, uAvailPool/2);
2786 if(!UsefulBuf_IsNULL(Allocated2)) { // expected to fail
2787 return -3;
2788 }
2789 (*pAlloc->fFree)(pAlloc->pAllocaterContext, Allocated.ptr);
2790 Allocated = (*pAlloc->fAllocate)(pAlloc->pAllocaterContext, NULL, uAvailPool/2);
2791 if(UsefulBuf_IsNULL(Allocated)) { // succeed because of the free
2792 return -4;
2793 }
2794
2795
2796 // Re do set up for next test that involves a successful alloc,
2797 // and a successful realloc and a failed realloc
2798 QCBORDecode_SetMemPool(&DC, Pool, 0);
2799
2800 // Cheat a little to get to the string allocator object
2801 // so we can call it directly to test it
2802 pAlloc = (QCBORStringAllocator *)DC.pStringAllocator;
2803 Allocated = (*pAlloc->fAllocate)(pAlloc->pAllocaterContext, NULL, uAvailPool/2);
2804 if(UsefulBuf_IsNULL(Allocated)) { // expected to succeed
2805 return -5;
2806 }
2807 Allocated2 = (*pAlloc->fAllocate)(pAlloc->pAllocaterContext, Allocated.ptr, uAvailPool);
2808 if(UsefulBuf_IsNULL(Allocated2)) {
2809 return -6;
2810 }
2811 if(Allocated2.ptr != Allocated.ptr || Allocated2.len != uAvailPool) {
2812 return -7;
2813 }
2814 UsefulBuf Allocated3 = (*pAlloc->fAllocate)(pAlloc->pAllocaterContext, Allocated.ptr, uAvailPool+1);
2815 if(!UsefulBuf_IsNULL(Allocated3)) { // expected to fail
2816 return -8;
2817 }
2818
2819 return 0;
2820}
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002821