blob: 86c62bb8ecd63bf278d150e60d46e25417a2af0a [file] [log] [blame]
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001/*==============================================================================
Laurence Lundbladed92a6162018-11-01 11:38:35 +07002 Copyright (c) 2016-2018, The Linux Foundation.
3 Copyright (c) 2018, Laurence Lundblade.
4 All rights reserved.
Laurence Lundblade9e3651c2018-10-10 11:49:55 +08005
Laurence Lundblade0dbc9172018-11-01 14:17:21 +07006Redistribution and use in source and binary forms, with or without
7modification, are permitted provided that the following conditions are
8met:
9 * Redistributions of source code must retain the above copyright
10 notice, this list of conditions and the following disclaimer.
11 * Redistributions in binary form must reproduce the above
12 copyright notice, this list of conditions and the following
13 disclaimer in the documentation and/or other materials provided
14 with the distribution.
15 * Neither the name of The Linux Foundation nor the names of its
16 contributors, nor the name "Laurence Lundblade" may be used to
17 endorse or promote products derived from this software without
18 specific prior written permission.
Laurence Lundblade9e3651c2018-10-10 11:49:55 +080019
Laurence Lundblade0dbc9172018-11-01 14:17:21 +070020THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
21WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
22MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
23ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
24BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
27BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
29OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
30IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Laurence Lundblade9e3651c2018-10-10 11:49:55 +080031 ==============================================================================*/
32
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080033#include "qcbor.h"
34#include "qcbor_decode_tests.h"
35#include <stdio.h>
36#include <strings.h>
Laurence Lundblade9e3651c2018-10-10 11:49:55 +080037#include <math.h> // for fabs()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080038#include <stdlib.h>
39
40
41// TODO: test other than the normal decoder mode
42
43static void printencoded(const char *szLabel, const uint8_t *pEncoded, size_t nLen)
44{
45 if(szLabel) {
46 printf("%s ", szLabel);
47 }
48
Laurence Lundblade570fab52018-10-13 18:28:27 +080049 size_t i;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080050 for(i = 0; i < nLen; i++) {
51 uint8_t Z = pEncoded[i];
52 printf("%02x ", Z);
53 }
54 printf("\n");
55
56 fflush(stdout);
57}
58
59
60// TODO: -- add a test for counting the top level items and adding it back in with AddRaw()
61
62
Laurence Lundbladeb836efb2018-10-28 20:09:58 +070063static const uint8_t spExpectedEncodedInts[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080064 0x98, 0x2f, 0x3b, 0x7f, 0xff, 0xff, 0xff, 0xff,
65 0xff, 0xff, 0xff, 0x3b, 0x00, 0x00, 0x00, 0x01,
66 0x00, 0x00, 0x00, 0x00, 0x3a, 0xff, 0xff, 0xff,
67 0xff, 0x3a, 0xff, 0xff, 0xff, 0xfe, 0x3a, 0xff,
68 0xff, 0xff, 0xfd, 0x3a, 0x7f, 0xff, 0xff, 0xff,
69 0x3a, 0x7f, 0xff, 0xff, 0xfe, 0x3a, 0x00, 0x01,
70 0x00, 0x01, 0x3a, 0x00, 0x01, 0x00, 0x00, 0x39,
71 0xff, 0xff, 0x39, 0xff, 0xfe, 0x39, 0xff, 0xfd,
72 0x39, 0x01, 0x00, 0x38, 0xff, 0x38, 0xfe, 0x38,
73 0xfd, 0x38, 0x18, 0x37, 0x36, 0x20, 0x00, 0x00,
74 0x01, 0x16, 0x17, 0x18, 0x18, 0x18, 0x19, 0x18,
75 0x1a, 0x18, 0xfe, 0x18, 0xff, 0x19, 0x01, 0x00,
76 0x19, 0x01, 0x01, 0x19, 0xff, 0xfe, 0x19, 0xff,
77 0xff, 0x1a, 0x00, 0x01, 0x00, 0x00, 0x1a, 0x00,
78 0x01, 0x00, 0x01, 0x1a, 0x00, 0x01, 0x00, 0x02,
79 0x1a, 0x7f, 0xff, 0xff, 0xff, 0x1a, 0x7f, 0xff,
80 0xff, 0xff, 0x1a, 0x80, 0x00, 0x00, 0x00, 0x1a,
81 0x80, 0x00, 0x00, 0x01, 0x1a, 0xff, 0xff, 0xff,
82 0xfe, 0x1a, 0xff, 0xff, 0xff, 0xff, 0x1b, 0x00,
83 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x1b,
84 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01,
85 0x1b, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
86 0xff, 0x1b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
87 0xff, 0xff};
88
89
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080090// return CBOR error or -1 if type of value doesn't match
91
92static int IntegerValuesParseTestInternal(QCBORDecodeContext *pDCtx)
93{
94 QCBORItem Item;
95 int nCBORError;
96
97 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
98 return nCBORError;
99 if(Item.uDataType != QCBOR_TYPE_ARRAY)
100 return -1;
101
102 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
103 return nCBORError;
104 if(Item.uDataType != QCBOR_TYPE_INT64 || // Todo; fix this for 32-bit machines
Laurence Lundblade570fab52018-10-13 18:28:27 +0800105 Item.val.int64 != -9223372036854775807LL - 1)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800106 return -1;
107
108 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
109 return nCBORError;
110 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade570fab52018-10-13 18:28:27 +0800111 Item.val.int64 != -4294967297)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800112 return -1;
113
114 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
115 return nCBORError;
116 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade570fab52018-10-13 18:28:27 +0800117 Item.val.int64 != -4294967296)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800118 return -1;
119
120 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
121 return nCBORError;
122 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade570fab52018-10-13 18:28:27 +0800123 Item.val.int64 != -4294967295)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800124 return -1;
125
126 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
127 return nCBORError;
128 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade570fab52018-10-13 18:28:27 +0800129 Item.val.int64 != -4294967294)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800130 return -1;
131
132
133 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
134 return nCBORError;
135 if(Item.uDataType != QCBOR_TYPE_INT64 ||
136 Item.val.int64 != -2147483648)
137 return -1;
138
139 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
140 return nCBORError;
141 if(Item.uDataType != QCBOR_TYPE_INT64 ||
142 Item.val.int64 != -2147483647)
143 return -1;
144
145 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
146 return nCBORError;
147 if(Item.uDataType != QCBOR_TYPE_INT64 ||
148 Item.val.int64 != -65538)
149 return -1;
150
151 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
152 return nCBORError;
153 if(Item.uDataType != QCBOR_TYPE_INT64 ||
154 Item.val.int64 != -65537)
155 return -1;
156
157 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
158 return nCBORError;
159 if(Item.uDataType != QCBOR_TYPE_INT64 ||
160 Item.val.int64 != -65536)
161 return -1;
162
163
164 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
165 return nCBORError;
166 if(Item.uDataType != QCBOR_TYPE_INT64 ||
167 Item.val.int64 != -65535)
168 return -1;
169
170
171 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
172 return nCBORError;
173 if(Item.uDataType != QCBOR_TYPE_INT64 ||
174 Item.val.int64 != -65534)
175 return -1;
176
177
178 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
179 return nCBORError;
180 if(Item.uDataType != QCBOR_TYPE_INT64 ||
181 Item.val.int64 != -257)
182 return -1;
183
184 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
185 return nCBORError;
186 if(Item.uDataType != QCBOR_TYPE_INT64 ||
187 Item.val.int64 != -256)
188 return -1;
189
190 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
191 return nCBORError;
192 if(Item.uDataType != QCBOR_TYPE_INT64 ||
193 Item.val.int64 != -255)
194 return -1;
195
196 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
197 return nCBORError;
198 if(Item.uDataType != QCBOR_TYPE_INT64 ||
199 Item.val.int64 != -254)
200 return -1;
201
202
203 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
204 return nCBORError;
205 if(Item.uDataType != QCBOR_TYPE_INT64 ||
206 Item.val.int64 != -25)
207 return -1;
208
209
210 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
211 return nCBORError;
212 if(Item.uDataType != QCBOR_TYPE_INT64 ||
213 Item.val.int64 != -24)
214 return -1;
215
216
217 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
218 return nCBORError;
219 if(Item.uDataType != QCBOR_TYPE_INT64 ||
220 Item.val.int64 != -23)
221 return -1;
222
223
224 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
225 return nCBORError;
226 if(Item.uDataType != QCBOR_TYPE_INT64 ||
227 Item.val.int64 != -1)
228 return -1;
229
230
231 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
232 return nCBORError;
233 if(Item.uDataType != QCBOR_TYPE_INT64 ||
234 Item.val.int64 != 0)
235 return -1;
236
237
238 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
239 return nCBORError;
240 if(Item.uDataType != QCBOR_TYPE_INT64 ||
241 Item.val.int64 != 0)
242 return -1;
243
244 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
245 return nCBORError;
246 if(Item.uDataType != QCBOR_TYPE_INT64 ||
247 Item.val.int64 != 1)
248 return -1;
249
250
251 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
252 return nCBORError;
253 if(Item.uDataType != QCBOR_TYPE_INT64 ||
254 Item.val.int64 != 22)
255 return -1;
256
257
258 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
259 return nCBORError;
260 if(Item.uDataType != QCBOR_TYPE_INT64 ||
261 Item.val.int64 != 23)
262 return -1;
263
264
265 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
266 return nCBORError;
267 if(Item.uDataType != QCBOR_TYPE_INT64 ||
268 Item.val.int64 != 24)
269 return -1;
270
271
272 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
273 return nCBORError;
274 if(Item.uDataType != QCBOR_TYPE_INT64 ||
275 Item.val.int64 != 25)
276 return -1;
277
278 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
279 return nCBORError;
280 if(Item.uDataType != QCBOR_TYPE_INT64 ||
281 Item.val.int64 != 26)
282 return -1;
283
284
285 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
286 return nCBORError;
287 if(Item.uDataType != QCBOR_TYPE_INT64 ||
288 Item.val.int64 != 254)
289 return -1;
290
291
292 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
293 return nCBORError;
294 if(Item.uDataType != QCBOR_TYPE_INT64 ||
295 Item.val.int64 != 255)
296 return -1;
297
298
299 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
300 return nCBORError;
301 if(Item.uDataType != QCBOR_TYPE_INT64 ||
302 Item.val.int64 != 256)
303 return -1;
304
305
306 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
307 return nCBORError;
308 if(Item.uDataType != QCBOR_TYPE_INT64 ||
309 Item.val.int64 != 257)
310 return -1;
311
312 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
313 return nCBORError;
314 if(Item.uDataType != QCBOR_TYPE_INT64 ||
315 Item.val.int64 != 65534)
316 return -1;
317
318
319 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
320 return nCBORError;
321 if(Item.uDataType != QCBOR_TYPE_INT64 ||
322 Item.val.int64 != 65535)
323 return -1;
324
325
326 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
327 return nCBORError;
328 if(Item.uDataType != QCBOR_TYPE_INT64 ||
329 Item.val.int64 != 65536)
330 return -1;
331
332 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
333 return nCBORError;
334 if(Item.uDataType != QCBOR_TYPE_INT64 ||
335 Item.val.int64 != 65537)
336 return -1;
337
338 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
339 return nCBORError;
340 if(Item.uDataType != QCBOR_TYPE_INT64 ||
341 Item.val.int64 != 65538)
342 return -1;
343
344 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
345 return nCBORError;
346 if(Item.uDataType != QCBOR_TYPE_INT64 ||
347 Item.val.int64 != 2147483647)
348 return -1;
349
350 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
351 return nCBORError;
352 if(Item.uDataType != QCBOR_TYPE_INT64 ||
353 Item.val.int64 != 2147483647)
354 return -1;
355
356 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
357 return nCBORError;
358 if(Item.uDataType != QCBOR_TYPE_INT64 ||
359 Item.val.int64 != 2147483648)
360 return -1;
361
362 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
363 return nCBORError;
364 if(Item.uDataType != QCBOR_TYPE_INT64 ||
365 Item.val.int64 != 2147483649)
366 return -1;
367
368 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
369 return nCBORError;
370 if(Item.uDataType != QCBOR_TYPE_INT64 ||
371 Item.val.int64 != 4294967294)
372 return -1;
373
374
375 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
376 return nCBORError;
377 if(Item.uDataType != QCBOR_TYPE_INT64 ||
378 Item.val.int64 != 4294967295)
379 return -1;
380
381
382 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
383 return nCBORError;
384 if(Item.uDataType != QCBOR_TYPE_INT64 ||
385 Item.val.int64 != 4294967296)
386 return -1;
387
388
389 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
390 return nCBORError;
391 if(Item.uDataType != QCBOR_TYPE_INT64 ||
392 Item.val.int64 != 4294967297)
393 return -1;
394
395
396
397 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
398 return nCBORError;
399 if(Item.uDataType != QCBOR_TYPE_INT64 ||
400 Item.val.int64 != 9223372036854775807LL)
401 return -1;
402
403
404 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
405 return nCBORError;
406 if(Item.uDataType != QCBOR_TYPE_UINT64 ||
407 Item.val.uint64 != 18446744073709551615ULL)
408 return -1;
409
410
411 if(QCBORDecode_Finish(pDCtx) != QCBOR_SUCCESS) {
412 return -1;
413 }
414
415 return 0;
416}
417
418
419/*
420 Tests the decoding of lots of different integers sizes
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +0800421 and values.
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800422 */
423
424int IntegerValuesParseTest()
425{
426 int n;
427 QCBORDecodeContext DCtx;
428
Laurence Lundbladeb836efb2018-10-28 20:09:58 +0700429 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedEncodedInts), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800430
431 n = IntegerValuesParseTestInternal(&DCtx);
432
433 return(n);
434}
435
436
437/*
438 Creates a simple CBOR array and returns it in *pEncoded. The array is malloced
439 and needs to be freed. This is used by several tests.
440
441 Two of the inputs can be set. Two other items in the array are fixed.
442
443 */
444
445static int CreateSimpleArray(int nInt1, int nInt2, uint8_t **pEncoded, size_t *pEncodedLen)
446{
447 QCBOREncodeContext ECtx;
448 int nReturn = -1;
449
450 *pEncoded = NULL;
451 *pEncodedLen = INT32_MAX;
452
453 // loop runs CBOR encoding twice. First with no buffer to
454 // calucate the length so buffer can be allocated correctly,
455 // and last with the buffer to do the actual encoding
456 do {
Laurence Lundblade0595e932018-11-02 22:22:47 +0700457 QCBOREncode_Init(&ECtx, (UsefulBuf){*pEncoded, *pEncodedLen});
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800458 QCBOREncode_OpenArray(&ECtx);
459 QCBOREncode_AddInt64(&ECtx, nInt1);
460 QCBOREncode_AddInt64(&ECtx, nInt2);
461 QCBOREncode_AddBytes(&ECtx, ((UsefulBufC) {"galactic", 8}));
462 QCBOREncode_AddBytes(&ECtx, ((UsefulBufC) {"haven token", 11}));
463 QCBOREncode_CloseArray(&ECtx);
464
Laurence Lundblade0595e932018-11-02 22:22:47 +0700465 UsefulBufC Encoded;
466 if(QCBOREncode_Finish(&ECtx, &Encoded))
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800467 goto Done;
468
469 if(*pEncoded != NULL) {
Laurence Lundblade0595e932018-11-02 22:22:47 +0700470 *pEncodedLen = Encoded.len;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800471 nReturn = 0;
472 goto Done;
473 }
Laurence Lundblade0595e932018-11-02 22:22:47 +0700474 *pEncoded = malloc(Encoded.len);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800475 if(*pEncoded == NULL) {
476 nReturn = -1;
477 goto Done;
478 }
479
480 } while(1);
481Done:
482 return (nReturn);
483
484}
485
486
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800487/*
488 {"first integer": 42,
489 "an array of two strings": ["string1", "string2"],
490 "map in a map": {
491 "bytes 1": h'78787878',
492 "bytes 2": h'79797979',
493 "another int": 98, "text 2":
494 "lies, damn lies and statistics"}
495 }
496 */
497
498static uint8_t pValidMapEncoded[] = {
Laurence Lundbladeb836efb2018-10-28 20:09:58 +0700499 0xa3, 0x6d, 0x66, 0x69, 0x72, 0x73, 0x74, 0x20, 0x69, 0x6e,
500 0x74, 0x65, 0x67, 0x65, 0x72, 0x18, 0x2a, 0x77, 0x61, 0x6e,
501 0x20, 0x61, 0x72, 0x72, 0x61, 0x79, 0x20, 0x6f, 0x66, 0x20,
502 0x74, 0x77, 0x6f, 0x20, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67,
503 0x73, 0x82, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x31,
504 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x32, 0x6c, 0x6d,
505 0x61, 0x70, 0x20, 0x69, 0x6e, 0x20, 0x61, 0x20, 0x6d, 0x61,
506 0x70, 0xa4, 0x67, 0x62, 0x79, 0x74, 0x65, 0x73, 0x20, 0x31,
507 0x44, 0x78, 0x78, 0x78, 0x78, 0x67, 0x62, 0x79, 0x74, 0x65,
508 0x73, 0x20, 0x32, 0x44, 0x79, 0x79, 0x79, 0x79, 0x6b, 0x61,
509 0x6e, 0x6f, 0x74, 0x68, 0x65, 0x72, 0x20, 0x69, 0x6e, 0x74,
510 0x18, 0x62, 0x66, 0x74, 0x65, 0x78, 0x74, 0x20, 0x32, 0x78,
511 0x1e, 0x6c, 0x69, 0x65, 0x73, 0x2c, 0x20, 0x64, 0x61, 0x6d,
512 0x6e, 0x20, 0x6c, 0x69, 0x65, 0x73, 0x20, 0x61, 0x6e, 0x64,
513 0x20, 0x73, 0x74, 0x61, 0x74, 0x69, 0x73, 0x74, 0x69, 0x63,
514 0x73 } ;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800515
516static int ParseOrderedArray(const uint8_t *pEncoded, size_t nLen, int64_t *pInt1, int64_t *pInt2, const uint8_t **pBuf3, size_t *pBuf3Len, const uint8_t **pBuf4, size_t *pBuf4Len)
517{
518 QCBORDecodeContext DCtx;
519 QCBORItem Item;
520 int nReturn = -1; // assume error until success
521
522 QCBORDecode_Init(&DCtx, (UsefulBufC){pEncoded, nLen}, QCBOR_DECODE_MODE_NORMAL);
523
524 // Make sure the first thing is a map
525 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || Item.uDataType != QCBOR_TYPE_ARRAY)
526 goto Done;
527
528 // First integer
529 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 | Item.uDataType != QCBOR_TYPE_INT64)
530 goto Done;
531 *pInt1 = Item.val.int64;
532
533 // Second integer
534 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || Item.uDataType != QCBOR_TYPE_INT64)
535 goto Done;
536 *pInt2 = Item.val.int64;
537
538 // First string
539 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || Item.uDataType != QCBOR_TYPE_BYTE_STRING)
540 goto Done;
541 *pBuf3 = Item.val.string.ptr;
542 *pBuf3Len = Item.val.string.len;
543
544 // Second string
545 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || Item.uDataType != QCBOR_TYPE_BYTE_STRING)
546 goto Done;
547 *pBuf4 = Item.val.string.ptr;
548 *pBuf4Len = Item.val.string.len;
549
550 nReturn = 0;
551
552Done:
553 return(nReturn);
554}
555
556
557
Laurence Lundblade9e3651c2018-10-10 11:49:55 +0800558
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800559int SimpleArrayTest()
560{
561 uint8_t *pEncoded;
562 size_t nEncodedLen;
563
564 int64_t i1, i2;
565 size_t i3, i4;
566 const uint8_t *s3, *s4;
567
568
569 if(CreateSimpleArray(23, 6000, &pEncoded, &nEncodedLen) < 0) {
570 return(-1);
571 }
572
573 ParseOrderedArray(pEncoded, nEncodedLen, &i1, &i2, &s3, &i3, &s4, &i4);
574
575 if(i1 != 23 ||
576 i2 != 6000 ||
577 i3 != 8 ||
578 i4 != 11 ||
Laurence Lundbladea44d5062018-10-17 18:45:12 +0530579 memcmp("galactic", s3, 8) !=0 ||
580 memcmp("haven token", s4, 11) !=0) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800581 printf("SimpleArraryTest Failed\n");
582 return(-1);
583 }
584
585 return(0);
586}
587
588
Laurence Lundblade9e3651c2018-10-10 11:49:55 +0800589
Laurence Lundbladeb836efb2018-10-28 20:09:58 +0700590static uint8_t spDeepArrays[] = {0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x80};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800591
592int ParseDeepArrayTest()
593{
594 QCBORDecodeContext DCtx;
595 int nReturn = 0;
596 int i;
597
Laurence Lundbladeb836efb2018-10-28 20:09:58 +0700598 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spDeepArrays), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800599
600 for(i = 0; i < 10; i++) {
601 QCBORItem Item;
602
603 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
604 Item.uDataType != QCBOR_TYPE_ARRAY ||
605 Item.uNestingLevel != i) {
606 nReturn = -1;
607 break;
608 }
609 }
610
611 return(nReturn);
612}
613
614
Laurence Lundbladeb836efb2018-10-28 20:09:58 +0700615static uint8_t spTooDeepArrays[] = {0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x80};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800616
617int ParseTooDeepArrayTest()
618{
619 QCBORDecodeContext DCtx;
620 int nReturn = 0;
621 int i;
622 QCBORItem Item;
623
624
Laurence Lundbladeb836efb2018-10-28 20:09:58 +0700625 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTooDeepArrays), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800626
627 for(i = 0; i < 10; i++) {
628
629 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
630 Item.uDataType != QCBOR_TYPE_ARRAY ||
631 Item.uNestingLevel != i) {
632 nReturn = -1;
633 break;
634 }
635 }
636
637 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_ARRAY_NESTING_TOO_DEEP)
638 nReturn = -1;
639
640 return(nReturn);
641}
642
643
644
645
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800646int ShortBufferParseTest()
647{
648 int nResult = 0;
649 QCBORDecodeContext DCtx;
650 int num;
651
Laurence Lundbladeb836efb2018-10-28 20:09:58 +0700652 for(num = sizeof(spExpectedEncodedInts)-1; num; num--) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800653 int n;
654
Laurence Lundbladeb836efb2018-10-28 20:09:58 +0700655 QCBORDecode_Init(&DCtx, (UsefulBufC){spExpectedEncodedInts, num}, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800656
657 n = IntegerValuesParseTestInternal(&DCtx);
658
659 //printf("Len %d, result: %d\n", num, n);
660
661 if(n != QCBOR_ERR_HIT_END) {
662 nResult = -1;
663 goto Done;
664 }
665 }
666Done:
667 return nResult;
668}
669
670
Laurence Lundblade9e3651c2018-10-10 11:49:55 +0800671
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800672int ShortBufferParseTest2()
673{
674 uint8_t *pEncoded;
675 int nReturn;
676 size_t nEncodedLen;
677
678 int64_t i1, i2;
679 size_t i3, i4;
680 const uint8_t *s3, *s4;
681
682 nReturn = 0;
683
684 if(CreateSimpleArray(23, 6000, &pEncoded, &nEncodedLen) < 0) {
685 return(-1);
686 }
687
688 //printencoded(pEncoded, nEncodedLen);
689
690 for(nEncodedLen--; nEncodedLen; nEncodedLen--) {
691 int nResult = ParseOrderedArray(pEncoded, (uint32_t)nEncodedLen, &i1, &i2, &s3, &i3, &s4, &i4);
692 if(nResult == 0) {
693 nReturn = -1;
694 }
695 }
696
697 return(nReturn);
698}
699
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530700/*
701 Decode and thoroughly check a moderately complex
702 set of maps
703 */
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800704static int ParseMapTest1()
705{
706 QCBORDecodeContext DCtx;
707 QCBORItem Item;
708 int nCBORError;
709
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800710 QCBORDecode_Init(&DCtx, (UsefulBufC){pValidMapEncoded, sizeof(pValidMapEncoded)}, QCBOR_DECODE_MODE_NORMAL);
711
712 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
713 return nCBORError;
714 if(Item.uDataType != QCBOR_TYPE_MAP ||
715 Item.val.uCount != 3)
716 return -1;
717
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800718 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
719 return nCBORError;
720 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
721 Item.label.string.len != 13 ||
722 Item.uDataType != QCBOR_TYPE_INT64 ||
723 Item.val.int64 != 42 ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530724 Item.uDataAlloc ||
725 Item.uLabelAlloc ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800726 memcmp(Item.label.string.ptr, "first integer", 13))
727 return -1;
728
729 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
730 return nCBORError;
731 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
732 Item.label.string.len != 23 ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530733 Item.uDataAlloc ||
734 Item.uLabelAlloc ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800735 memcmp(Item.label.string.ptr, "an array of two strings", 23) ||
736 Item.uDataType != QCBOR_TYPE_ARRAY ||
737 Item.val.uCount != 2)
738 return -1;
739
740 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
741 return nCBORError;
742 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
743 Item.val.string.len != 7 ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530744 Item.uDataAlloc ||
745 Item.uLabelAlloc ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800746 memcmp(Item.val.string.ptr, "string1", 7))
747 return -1;
748
749 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
750 return nCBORError;
751 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
752 Item.val.string.len != 7 ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530753 Item.uDataAlloc ||
754 Item.uLabelAlloc ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800755 memcmp(Item.val.string.ptr, "string2", 7))
756 return -1;
757
758 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
759 return nCBORError;
760 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
761 Item.label.string.len != 12 ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530762 Item.uDataAlloc ||
763 Item.uLabelAlloc ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800764 memcmp(Item.label.string.ptr, "map in a map", 12) ||
765 Item.uDataType != QCBOR_TYPE_MAP ||
766 Item.val.uCount != 4)
767 return -1;
768
769 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
770 return nCBORError;
771 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
772 Item.label.string.len != 7 ||
773 memcmp(Item.label.string.ptr, "bytes 1", 7)||
774 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
775 Item.val.string.len != 4 ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530776 Item.uDataAlloc ||
777 Item.uLabelAlloc ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800778 memcmp(Item.val.string.ptr, "xxxx", 4))
779 return -1;
780
781 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
782 return nCBORError;
783 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
784 Item.label.string.len != 7 ||
785 memcmp(Item.label.string.ptr, "bytes 2", 7) ||
786 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
787 Item.val.string.len != 4 ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530788 Item.uDataAlloc ||
789 Item.uLabelAlloc ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800790 memcmp(Item.val.string.ptr, "yyyy", 4))
791 return -1;
792
793 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
794 return nCBORError;
795 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
796 Item.label.string.len != 11 ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530797 Item.uDataAlloc ||
798 Item.uLabelAlloc ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800799 memcmp(Item.label.string.ptr, "another int", 11) ||
800 Item.uDataType != QCBOR_TYPE_INT64 ||
801 Item.val.int64 != 98)
802 return -1;
803
804 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
805 return nCBORError;
806 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
807 Item.label.string.len != 6 ||
808 memcmp(Item.label.string.ptr, "text 2", 6)||
809 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
810 Item.val.string.len != 30 ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530811 Item.uDataAlloc ||
812 Item.uLabelAlloc ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800813 memcmp(Item.val.string.ptr, "lies, damn lies and statistics", 30))
814 return -1;
815
816 return 0;
817}
818
819
820
821/*
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530822 Fully or partially decode pValidMapEncoded. When
823 partially decoding check for the right error code.
824 How much partial decoding depends on nLevel.
825
826 The partial decodes test error conditions of
827 incomplete encoded input.
828
829 This could be combined with the above test
830 and made prettier and maybe a little more
831 thorough.
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800832 */
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +0800833static int ExtraBytesTest(int nLevel)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800834{
835 QCBORDecodeContext DCtx;
836 QCBORItem Item;
837 int nCBORError;
838
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800839 QCBORDecode_Init(&DCtx, (UsefulBufC){pValidMapEncoded, sizeof(pValidMapEncoded)}, QCBOR_DECODE_MODE_NORMAL);
840
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +0800841 if(nLevel < 1) {
842 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_EXTRA_BYTES) {
843 return -1;
844 } else {
845 return 0;
846 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800847 }
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530848
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +0800849
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800850 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
851 return nCBORError;
852 if(Item.uDataType != QCBOR_TYPE_MAP ||
853 Item.val.uCount != 3)
854 return -1;
855
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +0800856 if(nLevel < 2) {
857 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
858 return -1;
859 } else {
860 return 0;
861 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800862 }
863
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +0800864
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800865 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
866 return nCBORError;
867 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
868 Item.label.string.len != 13 ||
869 Item.uDataType != QCBOR_TYPE_INT64 ||
870 Item.val.uCount != 42 ||
871 memcmp(Item.label.string.ptr, "first integer", 13))
872 return -1;
873
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +0800874 if(nLevel < 3) {
875 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
876 return -1;
877 } else {
878 return 0;
879 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800880 }
881
882 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
883 return nCBORError;
884 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
885 Item.label.string.len != 23 ||
886 memcmp(Item.label.string.ptr, "an array of two strings", 23) ||
887 Item.uDataType != QCBOR_TYPE_ARRAY ||
888 Item.val.uCount != 2)
889 return -1;
890
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +0800891
892 if(nLevel < 4) {
893 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
894 return -1;
895 } else {
896 return 0;
897 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800898 }
899
900
901 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
902 return nCBORError;
903 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
904 Item.val.string.len != 7 ||
905 memcmp(Item.val.string.ptr, "string1", 7))
906 return -1;
907
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +0800908 if(nLevel < 5) {
909 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
910 return -1;
911 } else {
912 return 0;
913 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800914 }
915
916 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
917 return nCBORError;
918 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
919 Item.val.string.len != 7 ||
920 memcmp(Item.val.string.ptr, "string2", 7))
921 return -1;
922
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +0800923 if(nLevel < 6) {
924 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
925 return -1;
926 } else {
927 return 0;
928 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800929 }
930
931 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
932 return nCBORError;
933 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
934 Item.label.string.len != 12 ||
935 memcmp(Item.label.string.ptr, "map in a map", 12) ||
936 Item.uDataType != QCBOR_TYPE_MAP ||
937 Item.val.uCount != 4)
938 return -1;
939
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +0800940 if(nLevel < 7) {
941 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
942 return -1;
943 } else {
944 return 0;
945 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800946 }
947
948 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
949 return nCBORError;
950 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
951 Item.label.string.len != 7 ||
952 memcmp(Item.label.string.ptr, "bytes 1", 7)||
953 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
954 Item.val.string.len != 4 ||
955 memcmp(Item.val.string.ptr, "xxxx", 4))
956 return -1;
957
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +0800958 if(nLevel < 8) {
959 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
960 return -1;
961 } else {
962 return 0;
963 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800964 }
965
966 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
967 return nCBORError;
968 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
969 Item.label.string.len != 7 ||
970 memcmp(Item.label.string.ptr, "bytes 2", 7) ||
971 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
972 Item.val.string.len != 4 ||
973 memcmp(Item.val.string.ptr, "yyyy", 4))
974 return -1;
975
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +0800976 if(nLevel < 9) {
977 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
978 return -1;
979 } else {
980 return 0;
981 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800982 }
983
984 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
985 return nCBORError;
986 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
987 Item.label.string.len != 11 ||
988 memcmp(Item.label.string.ptr, "another int", 11) ||
989 Item.uDataType != QCBOR_TYPE_INT64 ||
990 Item.val.int64 != 98)
991 return -1;
992
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +0800993 if(nLevel < 10) {
994 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
995 return -1;
996 } else {
997 return 0;
998 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800999 }
1000
1001 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1002 return nCBORError;
1003 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
1004 Item.label.string.len != 6 ||
1005 memcmp(Item.label.string.ptr, "text 2", 6)||
1006 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1007 Item.val.string.len != 30 ||
1008 memcmp(Item.val.string.ptr, "lies, damn lies and statistics", 30))
1009 return -1;
1010
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301011 if(QCBORDecode_Finish(&DCtx)) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001012 return -1;
1013 }
1014
1015 return 0;
1016}
1017
1018
1019
1020
1021int ParseMapTest()
1022{
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301023 // Parse a moderatly complex map structure very thoroughl
1024 int n = ParseMapTest1();
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001025
1026 if(!n) {
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001027 for(int i = 0; i < 10; i++) {
1028 n = ExtraBytesTest(i);
1029 if(n) {
1030 break;
1031 }
1032 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001033 }
1034
1035 return(n);
1036}
1037
1038
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07001039static 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 +08001040
1041int ParseSimpleTest()
1042{
1043 QCBORDecodeContext DCtx;
1044 QCBORItem Item;
1045 int nCBORError;
1046
1047
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07001048 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSimpleValues), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001049
1050
1051 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1052 return nCBORError;
1053 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
1054 Item.val.uCount != 10)
1055 return -1;
1056
1057 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1058 return nCBORError;
1059 if(Item.uDataType != QCBOR_TYPE_FALSE)
1060 return -1;
1061
1062 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1063 return nCBORError;
1064 if(Item.uDataType != QCBOR_TYPE_TRUE)
1065 return -1;
1066
1067 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1068 return nCBORError;
1069 if(Item.uDataType != QCBOR_TYPE_NULL)
1070 return -1;
1071
1072 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1073 return nCBORError;
1074 if(Item.uDataType != QCBOR_TYPE_UNDEF)
1075 return -1;
1076
1077 // A break
Laurence Lundblade9e3651c2018-10-10 11:49:55 +08001078 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_BREAK)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001079 return -1;
1080
1081 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1082 return nCBORError;
1083 if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 0)
1084 return -1;
1085
1086 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1087 return nCBORError;
1088 if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 19)
1089 return -1;
1090
1091 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_INVALID_CBOR)
1092 return -1;
1093
1094 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_INVALID_CBOR)
1095 return -1;
1096
1097 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_INVALID_CBOR)
1098 return -1;
1099
1100 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1101 return nCBORError;
1102 if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 32)
1103 return -1;
1104
1105 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1106 return nCBORError;
1107 if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 255)
1108 return -1;
1109
1110 return 0;
1111
1112}
1113
1114
1115struct FailInput {
1116 UsefulBufC Input;
1117 int nError;
1118};
1119
1120
1121struct FailInput Failures[] = {
1122 { {(uint8_t[]){0x18}, 1}, QCBOR_ERR_HIT_END }, // 1 byte integer missing the byte
1123 { {(uint8_t[]){0x1c}, 1}, QCBOR_ERR_UNSUPPORTED }, // Reserved additional info = 28
1124 { {(uint8_t[]){0x1d}, 1}, QCBOR_ERR_UNSUPPORTED }, // Reserved additional info = 29
1125 { {(uint8_t[]){0x1e}, 1}, QCBOR_ERR_UNSUPPORTED }, // Reserved additional info = 30
1126 { {(uint8_t[]){0x1f}, 1}, QCBOR_ERR_UNSUPPORTED }, // Indefinite length integer
1127 { {(uint8_t[]){0x3c}, 1}, QCBOR_ERR_UNSUPPORTED }, // 1 byte integer missing the byte
1128 { {(uint8_t[]){0x3d}, 1}, QCBOR_ERR_UNSUPPORTED }, // 1 byte integer missing the byte
1129 { {(uint8_t[]){0x3e}, 1}, QCBOR_ERR_UNSUPPORTED }, // 1 byte integer missing the byte
1130 { {(uint8_t[]){0x3f}, 1}, QCBOR_ERR_UNSUPPORTED }, // Indefinite length negative integer
1131 { {(uint8_t[]){0x41}, 1}, QCBOR_ERR_HIT_END }, // Short byte string
1132 { {(uint8_t[]){0x5c}, 1}, QCBOR_ERR_UNSUPPORTED }, // Reserved additional info = 28
1133 { {(uint8_t[]){0x5f}, 1}, QCBOR_ERR_UNSUPPORTED }, // Indefinite length byte string
1134 { {(uint8_t[]){0x61}, 1}, QCBOR_ERR_HIT_END }, // Short UTF-8 string
1135 { {(uint8_t[]){0x7c}, 1}, QCBOR_ERR_UNSUPPORTED }, // Reserved additional info = 28
1136 { {(uint8_t[]){0x7f}, 1}, QCBOR_ERR_UNSUPPORTED }, // Indefinite length UTF-8 string
1137 { {(uint8_t[]){0xff}, 1}, QCBOR_ERR_UNSUPPORTED } , // break
1138 { {(uint8_t[]){0xf8, 0x00}, 2}, QCBOR_ERR_INVALID_CBOR }, // An invalid encoding of a simple type
1139 { {(uint8_t[]){0xf8, 0x1f}, 2}, QCBOR_ERR_INVALID_CBOR }, // An invalid encoding of a simple type
1140 { {(uint8_t[]){0xc0, 0x00}, 2}, QCBOR_ERR_BAD_OPT_TAG }, // Text-based date, with an integer
1141 { {(uint8_t[]){0xc1, 0x41, 0x33}, 3}, QCBOR_ERR_BAD_OPT_TAG }, // Epoch date, with an byte string
1142 { {(uint8_t[]){0xc1, 0xc0, 0x00}, 3}, QCBOR_ERR_BAD_OPT_TAG }, // tagged as both epoch and string dates
1143 { {(uint8_t[]){0xc2, 0x00}, 2}, QCBOR_ERR_BAD_OPT_TAG } // big num tagged an int, not a byte string
1144
1145};
1146
1147
1148void Dump(UsefulBufC Input, int x)
1149{
1150 char label[10];
1151
1152 sprintf(label, "%d", x);
1153
1154 printencoded(label, Input.ptr, Input.len);
1155}
1156
1157
1158int FailureTests()
1159{
1160 int nResult = 0;
1161
1162 struct FailInput *pFEnd = &Failures[0] + sizeof(Failures)/sizeof(struct FailInput);
1163
1164 for(struct FailInput *pF = &Failures[0]; pF < pFEnd ;pF++) {
1165 QCBORDecodeContext DCtx;
1166 QCBORItem Item;
1167 int nCBORError;
1168
1169 QCBORDecode_Init(&DCtx, pF->Input, QCBOR_DECODE_MODE_NORMAL);
1170
1171 while(1) {
1172 nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
1173 if(QCBOR_ERR_HIT_END == nCBORError) {
1174 break;
1175 }
1176 if(nCBORError != pF->nError) {
1177 nResult = 1;
1178 // Dump(pF->Input, nCBORError);
1179 break;
1180 }
1181 }
1182 }
1183
1184 {
1185 QCBORDecodeContext DCtx;
1186 QCBORItem Item;
1187 int nCBORError;
1188
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07001189 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSimpleValues), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001190
1191 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1192 return nCBORError;
1193 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
1194 Item.val.uCount != 10)
1195 return -1;
1196
1197 DCtx.InBuf.magic = 0; // Corrupt the UsefulInputBuf
1198
1199 nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
1200 if(nCBORError != QCBOR_ERR_HIT_END)
1201 return -1;
1202 }
1203
1204
1205 return nResult;
1206}
1207
1208
1209
1210
1211static void Recurser(uint8_t *pBuf, int nLen, int nLenMax)
1212{
1213
1214 if(nLen >= nLenMax) {
1215 return;
1216 }
1217
1218 //printf("__%d__%d__\n", nLen, nLenMax);
1219
1220 for(int i = 0; i < 256; i++) {
1221 pBuf[nLen] = i;
1222
1223 QCBORDecodeContext DCtx;
1224 QCBORItem Item;
1225 int nCBORError;
1226
1227 UsefulBufC Input = {pBuf, nLen+1};
1228
1229 QCBORDecode_Init(&DCtx, Input, QCBOR_DECODE_MODE_NORMAL);
1230
1231 while(1) {
1232 nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
1233 if(QCBOR_ERR_HIT_END == nCBORError) {
1234 break;
1235 }
1236 if(nCBORError != QCBOR_SUCCESS) {
1237 if(nCBORError != QCBOR_ERR_UNSUPPORTED && nCBORError != QCBOR_ERR_HIT_END && nCBORError != QCBOR_ERR_INVALID_CBOR) {
1238 //Dump(Input, nCBORError);
1239 }
1240 break;
1241 }
1242 }
1243 //Dump(Input, -1);
1244
1245
1246 Recurser(pBuf, nLen+1, nLenMax);
1247 }
1248}
1249
1250
1251/*
1252 Runs all possible input strings of a given length. This is set to 3 to make the test
1253 run in reasonable time.
1254 Main point of this test is to not crash.
1255 */
1256
1257int ComprehensiveInputTest()
1258{
1259 uint8_t pBuf[3]; // 3 keeps it running in reasonable time. 4 takes tens of minutes.
1260
1261 Recurser(pBuf, 0, sizeof(pBuf));
1262
1263 return 0;
1264}
1265
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07001266static uint8_t spDateTestInput[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001267 0xc0, // tag for string date
1268 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string
1269
1270 0xc1, // tag for epoch date
1271 0x1a, 0x53, 0x72, 0x4E, 0x00, // Epoch date 1400000000; Tue, 13 May 2014 16:53:20 GMT
1272
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001273 // CBOR_TAG_B64
1274 0xc1, 0xcf, 0xd8, 0x22, // 0xee, // Epoch date with extra tags TODO: fix this test
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001275 0x1a, 0x53, 0x72, 0x4E, 0x01,
1276
1277 0xc1, // tag for epoch date
1278 0x1b, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, // Too large integer
1279
1280 0xc1, // tag for epoch date
1281 0xfa, 0x3f, 0x8c, 0xcc, 0xcd, // double with value 1.1
1282
1283 0xc1, // tag for epoch date
1284 0xfa, 0x7f, 0x7f, 0xff, 0xff // 3.4028234663852886e+38 too large
1285
1286};
1287
1288
1289// have to check float expected only to within an epsilon
1290int CHECK_EXPECTED_DOUBLE(double val, double expected) {
1291
1292 double diff = val - expected;
1293
1294 diff = fabs(diff);
1295
1296 return diff > 0.0000001;
1297}
1298
1299
1300int DateParseTest()
1301{
1302 QCBORDecodeContext DCtx;
1303 QCBORItem Item;
1304 int nCBORError;
1305
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07001306 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spDateTestInput), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001307
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001308 const uint64_t uTags[] = {15};
1309 QCBORTagListIn TagList = {1, uTags};
1310
1311 QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TagList);
1312
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001313 // String date
1314 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1315 return -1;
1316 if(Item.uDataType != QCBOR_TYPE_DATE_STRING ||
Laurence Lundblade9e3651c2018-10-10 11:49:55 +08001317 UsefulBuf_Compare(Item.val.dateString, UsefulBuf_FromSZ("1985-04-12"))){
Laurence Lundblade67bd5512018-11-02 21:44:06 +07001318 return -2;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001319 }
1320
1321 // Epoch date
1322 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade67bd5512018-11-02 21:44:06 +07001323 return -3;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001324 if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH ||
1325 Item.val.epochDate.nSeconds != 1400000000 ||
1326 Item.val.epochDate.fSecondsFraction != 0 ) {
Laurence Lundblade67bd5512018-11-02 21:44:06 +07001327 return -4;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001328 }
1329
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001330 // Epoch date with extra CBOR_TAG_B64 tag that doesn't really mean anything
1331 // but want to be sure extra tag doesn't cause a problem
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001332 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade67bd5512018-11-02 21:44:06 +07001333 return -5;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001334 if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH ||
1335 Item.val.epochDate.nSeconds != 1400000001 ||
1336 Item.val.epochDate.fSecondsFraction != 0 ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001337 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_B64)) {
Laurence Lundblade67bd5512018-11-02 21:44:06 +07001338 return -6;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001339 }
1340
1341 // Epoch date that is too large for our representation
1342 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_DATE_OVERFLOW) {
Laurence Lundblade67bd5512018-11-02 21:44:06 +07001343 return -7;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001344 }
1345
1346 // Epoch date in float format with fractional seconds
1347 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade67bd5512018-11-02 21:44:06 +07001348 return -8;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001349 if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH ||
1350 Item.val.epochDate.nSeconds != 1 ||
1351 CHECK_EXPECTED_DOUBLE(Item.val.epochDate.fSecondsFraction, 0.1 )) {
Laurence Lundblade67bd5512018-11-02 21:44:06 +07001352 return -9;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001353 }
1354
1355 // Epoch date float that is too large for our representation
1356 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_DATE_OVERFLOW) {
Laurence Lundblade67bd5512018-11-02 21:44:06 +07001357 return -10;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001358 }
1359
1360 // TODO: could use a few more tests with float, double, and half precsion and negative (but coverage is still pretty good)
1361
1362 return 0;
1363}
1364
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001365// Really simple basic input for tagging test
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07001366static uint8_t spOptTestInput[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001367 0xd9, 0xd9, 0xf7, // CBOR magic number
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001368 0x81, // Array of one
1369 0xd8, 0x04, // non-preferred serialization of tag 4
1370 0x82, 0x01, 0x03}; // fraction 1/3
1371
1372static uint8_t spEncodedLargeTag[] = {0xdb, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x80};
1373
1374// 0x9192939495969798, 0x88, 0x01, 0x04
1375static uint8_t spLotsOfTags[] = {0xdb, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0xd8, 0x88, 0xc5, 0xc4, 0x80};
1376
1377/*
1378 The cbor.me parse of this.
1379 55799(55799(55799({6(7(-23)): 5859837686836516696(7({7(-20): 11({17(-18): 17(17(17("Organization"))),
1380 9(-17): 773("SSG"), -15: 4(5(6(7(8(9(10(11(12(13(14(15("Confusion")))))))))))), 17(-16): 17("San Diego"),
1381 17(-14): 17("US")}), 23(-19): 19({-11: 9({-9: -7}),
1382 90599561(90599561(90599561(-10))): 12(h'0102030405060708090A')})})),
1383 16(-22): 23({11(8(7(-5))): 8(-3)})})))
1384 */
1385static uint8_t spCSRWithTags[] = {
1386 0xd9, 0xd9, 0xf7, 0xd9, 0xd9, 0xf7, 0xd9, 0xd9, 0xf7, 0xa2,
1387 0xc6, 0xc7, 0x36,
1388 0xdb, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0xc7, 0xa2,
1389 0xda, 0x00, 0x00, 0x00, 0x07, 0x33,
1390 0xcb, 0xa5,
1391 0xd1, 0x31,
1392 0xd1, 0xd1, 0xd1, 0x6c,
1393 0x4f, 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74, 0x69, 0x6f, 0x6e,
1394 0xc9, 0x30,
1395 0xd9, 0x03, 0x05, 0x63,
1396 0x53, 0x53, 0x47,
1397 0x2e,
1398 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf, 0x69,
1399 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73, 0x69, 0x6f, 0x6e,
1400 0xd1, 0x2f,
1401 0xd1, 0x69,
1402 0x53, 0x61, 0x6e, 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f,
1403 0xd1, 0x2d,
1404 0xd1, 0x62,
1405 0x55, 0x53,
1406 0xd7, 0x32,
1407 0xd3, 0xa2,
1408 0x2a,
1409 0xc9, 0xa1,
1410 0x28,
1411 0x26,
1412 0xda, 0x05, 0x66, 0x70, 0x89, 0xda, 0x05, 0x66, 0x70, 0x89, 0xda, 0x05, 0x66, 0x70, 0x89, 0x29,
1413 0xcc, 0x4a,
1414 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,0x07, 0x08, 0x09, 0x0a,
1415 0xd0, 0x35,
1416 0xd7, 0xa1,
1417 0xcb, 0xc8, 0xc7, 0x24,
1418 0xc8, 0x22};
1419
1420static int CheckCSRMaps(QCBORDecodeContext *pDC);
1421
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001422
1423int OptTagParseTest()
1424{
1425 QCBORDecodeContext DCtx;
1426 QCBORItem Item;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001427
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07001428 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spOptTestInput), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001429
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001430 //-------------------------
1431 // This text matches the magic number tag and the fraction tag
1432 if(QCBORDecode_GetNext(&DCtx, &Item)) {
1433 return -2;
1434 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001435 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001436 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC)) {
1437 return -3;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001438 }
1439
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001440 if(QCBORDecode_GetNext(&DCtx, &Item)) {
1441 return -4;
1442 }
1443 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
1444 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_FRACTION) ||
1445 Item.val.uCount != 2) {
1446 return -5;
1447 }
1448
1449 // --------------------------------
1450 // This test decodes the very large tag, but it is not in
1451 // any list so it is ignored.
1452 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEncodedLargeTag), QCBOR_DECODE_MODE_NORMAL);
1453 if(QCBORDecode_GetNext(&DCtx, &Item)) {
1454 return -6;
1455 }
1456 if(Item.uTagBits) {
1457 return -7;
1458 }
1459
1460 // ----------------------------------
1461 // This test sets up a caller-config list that includes the very large tage and then matches it.
1462 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEncodedLargeTag), QCBOR_DECODE_MODE_NORMAL);
1463 const uint64_t puList[] = {0x9192939495969798, 257};
1464 const QCBORTagListIn TL = {2, puList};
1465 QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TL);
1466
1467 if(QCBORDecode_GetNext(&DCtx, &Item)) {
1468 return -8;
1469 }
1470 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
1471 !QCBORDecode_IsTagged(&DCtx, &Item, 0x9192939495969798) ||
1472 QCBORDecode_IsTagged(&DCtx, &Item, 257) ||
1473 QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_BIGFLOAT) ||
1474 Item.val.uCount != 0) {
1475 return -9;
1476 }
1477
1478 //------------------------
1479 // This test sets up a caller-configured list, and looks up something not in it
1480 const uint64_t puLongList[17] = {1,2,1};
1481 const QCBORTagListIn TLLong = {17, puLongList};
1482 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEncodedLargeTag), QCBOR_DECODE_MODE_NORMAL);
1483 QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TLLong);
1484 if(QCBORDecode_GetNext(&DCtx, &Item)) {
1485 return -11;
1486 }
1487
1488 // -----------------------
1489 // This tests retrievel of the full tag list
1490 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spLotsOfTags), QCBOR_DECODE_MODE_NORMAL);
1491 uint64_t puTags[16];
1492 QCBORTagListOut Out = {0, 4, puTags};
1493 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
1494 return -12;
1495 }
1496 if(puTags[0] != 0x9192939495969798 ||
1497 puTags[1] != 0x88 ||
1498 puTags[2] != 0x05 ||
1499 puTags[3] != 0x04) {
1500 return -13;
1501 }
1502
1503 // ----------------------
1504 // This text if too small of an out list
1505 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spLotsOfTags), QCBOR_DECODE_MODE_NORMAL);
1506 QCBORTagListOut OutSmall = {0, 3, puTags};
1507 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &OutSmall) != QCBOR_ERR_TOO_MANY_TAGS) {
1508 return -14;
1509 }
1510
1511 // ---------------
1512 // Parse a version of the "CSR" that has had a ton of tags randomly inserted
1513 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRWithTags), QCBOR_DECODE_MODE_NORMAL);
1514 int n = CheckCSRMaps(&DCtx);
1515 if(n) {
1516 return n-2000;
1517 }
1518
1519 Out = (QCBORTagListOut){0,16, puTags};
1520 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRWithTags), QCBOR_DECODE_MODE_NORMAL);
1521
1522 const uint64_t puTagList[] = {773, 1, 90599561};
1523 const QCBORTagListIn TagList = {3, puTagList};
1524 QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TagList);
1525
1526
1527 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
1528 return -100;
1529 }
1530 if(Item.uDataType != QCBOR_TYPE_MAP ||
1531 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC) ||
1532 QCBORDecode_IsTagged(&DCtx, &Item, 90599561) ||
1533 QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_DATE_EPOCH) ||
1534 Item.val.uCount != 2 ||
1535 puTags[0] != CBOR_TAG_CBOR_MAGIC ||
1536 puTags[1] != CBOR_TAG_CBOR_MAGIC ||
1537 puTags[2] != CBOR_TAG_CBOR_MAGIC ||
1538 Out.uNumUsed != 3) {
1539 return -101;
1540 }
1541
1542 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
1543 return -102;
1544 }
1545 if(Item.uDataType != QCBOR_TYPE_MAP ||
1546 QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC) ||
1547 QCBORDecode_IsTagged(&DCtx, &Item, 6) ||
1548 QCBORDecode_IsTagged(&DCtx, &Item, 7) || // item is tagged 7, but 7 is not configured to be recognized
1549 Item.val.uCount != 2 ||
1550 puTags[0] != 5859837686836516696 ||
1551 puTags[1] != 7 ||
1552 Out.uNumUsed != 2) {
1553 return -103;
1554 }
1555
1556 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
1557 return -104;
1558 }
1559 if(Item.uDataType != QCBOR_TYPE_MAP ||
1560 Item.uTagBits ||
1561 Item.val.uCount != 5 ||
1562 puTags[0] != 0x0b ||
1563 Out.uNumUsed != 1) {
1564 return -105;
1565 }
1566
1567 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
1568 return -106;
1569 }
1570 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1571 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_COSE_MAC0) ||
1572 Item.val.string.len != 12 ||
1573 puTags[0] != CBOR_TAG_COSE_MAC0 ||
1574 puTags[1] != CBOR_TAG_COSE_MAC0 ||
1575 puTags[2] != CBOR_TAG_COSE_MAC0 ||
1576 Out.uNumUsed != 3) {
1577 return -105;
1578 }
1579
1580 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
1581 return -107;
1582 }
1583 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1584 !QCBORDecode_IsTagged(&DCtx, &Item, 773) ||
1585 Item.val.string.len != 3 ||
1586 puTags[0] != 773 ||
1587 Out.uNumUsed != 1) {
1588 return -108;
1589 }
1590
1591 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
1592 return -109;
1593 }
1594 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1595 !QCBORDecode_IsTagged(&DCtx, &Item, 4) ||
1596 Item.val.string.len != 9 ||
1597 puTags[0] != 4 ||
1598 puTags[11] != 0x0f ||
1599 Out.uNumUsed != 12) {
1600 return -110;
1601 }
1602
1603 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
1604 return -111;
1605 }
1606 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1607 !QCBORDecode_IsTagged(&DCtx, &Item, 17) ||
1608 Item.val.string.len != 9 ||
1609 puTags[0] != 17 ||
1610 Out.uNumUsed != 1) {
1611 return -112;
1612 }
1613
1614 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
1615 return -111;
1616 }
1617 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1618 !QCBORDecode_IsTagged(&DCtx, &Item, 17) ||
1619 Item.val.string.len != 2 ||
1620 puTags[0] != 17 ||
1621 Out.uNumUsed != 1) {
1622 return -112;
1623 }
1624
1625 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
1626 return -113;
1627 }
1628 if(Item.uDataType != QCBOR_TYPE_MAP ||
1629 QCBORDecode_IsTagged(&DCtx, &Item, 19) ||
1630 Item.val.uCount != 2 ||
1631 puTags[0] != 19 ||
1632 Out.uNumUsed != 1) {
1633 return -114;
1634 }
1635
1636 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
1637 return -115;
1638 }
1639 if(Item.uDataType != QCBOR_TYPE_MAP ||
1640 QCBORDecode_IsTagged(&DCtx, &Item, 9) ||
1641 Item.uTagBits ||
1642 Item.val.uCount != 1 ||
1643 puTags[0] != 9 ||
1644 Out.uNumUsed != 1) {
1645 return -116;
1646 }
1647
1648 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
1649 return -116;
1650 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001651 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001652 Item.val.int64 != -7 ||
1653 Item.uTagBits ||
1654 Out.uNumUsed != 0) {
1655 return -117;
1656 }
1657
1658 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
1659 return -118;
1660 }
1661 if(Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
1662 Item.val.string.len != 10 ||
1663 Item.uTagBits ||
1664 puTags[0] != 12 ||
1665 Out.uNumUsed != 1) {
1666 return -119;
1667 }
1668
1669 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
1670 return -120;
1671 }
1672 if(Item.uDataType != QCBOR_TYPE_MAP ||
1673 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_ENC_AS_B16) ||
1674 Item.val.uCount != 1 ||
1675 puTags[0] != 0x17 ||
1676 Out.uNumUsed != 1) {
1677 return -121;
1678 }
1679
1680 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
1681 return -122;
1682 }
1683 if(Item.uDataType != QCBOR_TYPE_INT64 ||
1684 QCBORDecode_IsTagged(&DCtx, &Item, 8) ||
1685 Item.val.int64 != -3 ||
1686 puTags[0] != 8 ||
1687 Out.uNumUsed != 1) {
1688 return -123;
1689 }
1690
1691 if(QCBORDecode_Finish(&DCtx)) {
1692 return -124;
1693 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001694
1695 return 0;
1696}
1697
1698
1699
1700
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07001701static uint8_t spBigNumInput[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001702 0x83,
1703 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1704 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1705 0xA4,
1706 0x63, 0x42, 0x4E, 0x2B,
1707 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1708 0x18, 0x40,
1709 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1710 0x63, 0x42, 0x4E, 0x2D,
1711 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1712 0x38, 0x3F,
1713 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
1714
1715
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07001716static uint8_t spBigNum[] = {0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001717
1718
1719int BignumParseTest()
1720{
1721 QCBORDecodeContext DCtx;
1722 QCBORItem Item;
1723 int nCBORError;
1724
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07001725 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNumInput), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001726
1727
1728 //
1729 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1730 return -1;
1731 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
1732 return -1;
1733 }
1734
1735 //
1736 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1737 return -1;
1738 if(Item.uDataType != QCBOR_TYPE_POSBIGNUM ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07001739 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001740 return -1;
1741 }
1742
1743 //
1744 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1745 return -1;
1746 if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07001747 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001748 return -1;
1749 }
1750
1751 //
1752 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1753 return -1;
1754 if(Item.uDataType != QCBOR_TYPE_MAP) {
1755 return -1;
1756 }
1757
1758 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1759 return -1;
1760 if(Item.uDataType != QCBOR_TYPE_POSBIGNUM ||
1761 Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07001762 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001763 return -1;
1764 }
1765
1766 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1767 return -1;
1768 if(Item.uDataType != QCBOR_TYPE_POSBIGNUM ||
1769 Item.uLabelType != QCBOR_TYPE_INT64 ||
1770 Item.label.int64 != 64 ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07001771 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001772 return -1;
1773 }
1774
1775 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1776 return -1;
1777 if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM ||
1778 Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07001779 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001780 return -1;
1781 }
1782
1783 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1784 return -1;
1785 if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM ||
1786 Item.uLabelType != QCBOR_TYPE_INT64 ||
1787 Item.label.int64 != -64 ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07001788 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001789 return -1;
1790 }
1791
1792 return 0;
1793}
1794
1795
1796
Laurence Lundbladea44d5062018-10-17 18:45:12 +05301797static 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 +08001798{
1799 QCBORItem Item;
1800 int nCBORError;
1801
1802 if((nCBORError = QCBORDecode_GetNext(pCtx, &Item))) return -1;
1803 if(Item.uDataType != uDataType) return -1;
1804 if(uNestingLevel > 0) {
1805 if(Item.uLabelType != QCBOR_TYPE_INT64 && Item.uLabelType != QCBOR_TYPE_UINT64) return -1;
1806 if(Item.uLabelType == QCBOR_TYPE_INT64) {
1807 if(Item.label.int64 != nLabel) return -1;
1808 } else {
Laurence Lundblade570fab52018-10-13 18:28:27 +08001809 if(Item.label.uint64 != (uint64_t)nLabel) return -1;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001810 }
1811 }
1812 if(Item.uNestingLevel != uNestingLevel) return -1;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05301813 if(Item.uNextNestLevel != uNextNest) return -1;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001814
1815 if(pItem) {
1816 *pItem = Item;
1817 }
1818 return 0;
1819}
1820
1821
Laurence Lundblade742df4a2018-10-13 20:07:17 +08001822// Same code checks definite and indefinite length versions of the map
1823static int CheckCSRMaps(QCBORDecodeContext *pDC)
1824{
Laurence Lundbladea44d5062018-10-17 18:45:12 +05301825 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 0, 1, 0, NULL)) return -1;
Laurence Lundblade742df4a2018-10-13 20:07:17 +08001826
Laurence Lundbladea44d5062018-10-17 18:45:12 +05301827 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 1, 2, -23, NULL)) return -1;
Laurence Lundblade742df4a2018-10-13 20:07:17 +08001828
Laurence Lundbladea44d5062018-10-17 18:45:12 +05301829 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 2, 3, -20, NULL)) return -1;
Laurence Lundblade742df4a2018-10-13 20:07:17 +08001830
Laurence Lundbladea44d5062018-10-17 18:45:12 +05301831 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -18, NULL)) return -1;
1832 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -17, NULL)) return -1;
1833 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -15, NULL)) return -1;
1834 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -16, NULL)) return -1;
1835 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 2, -14, NULL)) return -1;
Laurence Lundblade742df4a2018-10-13 20:07:17 +08001836
Laurence Lundbladea44d5062018-10-17 18:45:12 +05301837 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 2, 3, -19, NULL)) return -1;
1838 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 3, 4, -11, NULL)) return -1;
Laurence Lundblade742df4a2018-10-13 20:07:17 +08001839
Laurence Lundbladea44d5062018-10-17 18:45:12 +05301840 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_INT64, 4, 3, -9, NULL)) return -1;
1841 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_BYTE_STRING, 3, 1, -10, NULL)) return -1;
Laurence Lundblade742df4a2018-10-13 20:07:17 +08001842
Laurence Lundbladea44d5062018-10-17 18:45:12 +05301843 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 1, 2, -22, NULL)) return -1;
1844 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_INT64, 2, 0, -5, NULL)) return -1;
Laurence Lundblade742df4a2018-10-13 20:07:17 +08001845
1846 if(QCBORDecode_Finish(pDC)) return -2;
1847
1848 return 0;
1849}
1850
1851
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001852/*
1853// cbor.me decoded output
1854{
1855 -23: {
1856 -20: {
1857 -18: "Organization",
1858 -17: "SSG",
1859 -15: "Confusion",
1860 -16: "San Diego",
1861 -14: "US"
1862 },
1863 -19: {
1864 -11: {
1865 -9: -7
1866 },
1867 -10: '\u0001\u0002\u0003\u0004\u0005\u0006\a\b\t\n'
1868 }
1869 },
1870 -22: {
1871 -5: -3
1872 }
1873}
1874 */
1875
Laurence Lundblade742df4a2018-10-13 20:07:17 +08001876
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07001877static uint8_t spCSRInput[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001878 0xa2, 0x36, 0xa2, 0x33, 0xa5, 0x31, 0x6c, 0x4f,
1879 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74,
1880 0x69, 0x6f, 0x6e, 0x30, 0x63, 0x53, 0x53, 0x47,
1881 0x2e, 0x69, 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73,
1882 0x69, 0x6f, 0x6e, 0x2f, 0x69, 0x53, 0x61, 0x6e,
1883 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f, 0x2d, 0x62,
1884 0x55, 0x53, 0x32, 0xa2, 0x2a, 0xa1, 0x28, 0x26,
1885 0x29, 0x4a, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
1886 0x07, 0x08, 0x09, 0x0a, 0x35, 0xa1, 0x24, 0x22};
1887
1888int NestedMapTest()
1889{
1890 QCBORDecodeContext DCtx;
1891
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07001892 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001893
Laurence Lundblade742df4a2018-10-13 20:07:17 +08001894 return CheckCSRMaps(&DCtx);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001895}
1896
Laurence Lundblade742df4a2018-10-13 20:07:17 +08001897// Same map as above, but using indefinite lengths
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07001898static uint8_t spCSRInputIndefLen[] = {
Laurence Lundblade742df4a2018-10-13 20:07:17 +08001899 0xbf, 0x36, 0xbf, 0x33, 0xbf, 0x31, 0x6c, 0x4f,
1900 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74,
1901 0x69, 0x6f, 0x6e, 0x30, 0x63, 0x53, 0x53, 0x47,
1902 0x2e, 0x69, 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73,
1903 0x69, 0x6f, 0x6e, 0x2f, 0x69, 0x53, 0x61, 0x6e,
1904 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f, 0x2d, 0x62,
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07001905 0x55, 0x53, 0xff, 0x32, 0xbf, 0x2a, 0xbf, 0x28,
1906 0x26, 0xff, 0x29, 0x4a, 0x01, 0x02, 0x03, 0x04,
1907 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0xff, 0xff,
1908 0x35, 0xbf, 0x24, 0x22, 0xff, 0xff};
Laurence Lundblade742df4a2018-10-13 20:07:17 +08001909
1910int NestedMapTestIndefLen()
1911{
1912 QCBORDecodeContext DCtx;
1913
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07001914 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInputIndefLen), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade742df4a2018-10-13 20:07:17 +08001915
1916 return CheckCSRMaps(&DCtx);
1917}
1918
1919
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001920
Laurence Lundblade17ede402018-10-13 11:43:07 +08001921static UsefulBufC make_nested_indefinite_arrays(int n, UsefulBuf Storage)
1922{
1923 UsefulOutBuf UOB;
1924 UsefulOutBuf_Init(&UOB, Storage);
1925
1926 int i;
1927 for(i = 0; i < n; i++) {
1928 UsefulOutBuf_AppendByte(&UOB, 0x9f);
1929 }
1930
1931 for(i = 0; i < n; i++) {
1932 UsefulOutBuf_AppendByte(&UOB, 0xff);
1933 }
1934 return UsefulOutBuf_OutUBuf(&UOB);
1935}
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08001936
1937
Laurence Lundblade17ede402018-10-13 11:43:07 +08001938static int parse_indeflen_nested(UsefulBufC Nested, int nNestLevel)
1939{
1940 QCBORDecodeContext DC;
1941 QCBORDecode_Init(&DC, Nested, 0);
1942
1943 int j;
1944 for(j = 0; j < nNestLevel; j++) {
1945 QCBORItem Item;
1946 int nReturn = QCBORDecode_GetNext(&DC, &Item);
1947 if(j >= QCBOR_MAX_ARRAY_NESTING) {
1948 // Should be in error
1949 if(nReturn != QCBOR_ERR_ARRAY_NESTING_TOO_DEEP) {
1950 return -4;
1951 } else {
1952 return 0; // Decoding doesn't recover after an error
1953 }
1954 } else {
1955 // Should be no error
1956 if(nReturn) {
1957 return -9; // Should not have got an error
1958 }
1959 }
1960 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
1961 return -7;
1962 }
1963 }
1964 int nReturn = QCBORDecode_Finish(&DC);
1965 if(nReturn) {
1966 return -3;
1967 }
1968 return 0;
1969}
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08001970
1971
Laurence Lundbladea44d5062018-10-17 18:45:12 +05301972int IndefiniteLengthNestTest()
Laurence Lundblade17ede402018-10-13 11:43:07 +08001973{
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05301974 UsefulBuf_MAKE_STACK_UB(Storage, 50);
Laurence Lundblade17ede402018-10-13 11:43:07 +08001975 int i;
Laurence Lundblade6de37062018-10-15 12:22:42 +05301976 for(i=1; i < QCBOR_MAX_ARRAY_NESTING+4; i++) {
Laurence Lundblade17ede402018-10-13 11:43:07 +08001977 UsefulBufC Nested = make_nested_indefinite_arrays(i, Storage);
1978 int nReturn = parse_indeflen_nested(Nested, i);
1979 if(nReturn) {
1980 return nReturn;
1981 }
1982 }
1983 return 0;
1984}
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08001985
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08001986
Laurence Lundblade6de37062018-10-15 12:22:42 +05301987
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07001988static const uint8_t spIndefiniteArray[] = {0x9f, 0x01, 0x82, 0x02, 0x03, 0xff}; // [1, [2, 3]]
1989static const uint8_t spIndefiniteArrayBad1[] = {0x9f}; // No closing break
1990static const uint8_t spIndefiniteArrayBad2[] = {0x9f, 0x9f, 0x02, 0xff}; // Not enough closing breaks
1991static const uint8_t spIndefiniteArrayBad3[] = {0x9f, 0x02, 0xff, 0xff}; // Too many closing breaks
1992static const uint8_t spIndefiniteArrayBad4[] = {0x81, 0x9f}; // Unclosed indeflen inside def len
1993static const uint8_t spIndefiniteArrayBad5[] = {0x9f, 0xd1, 0xff}; // confused tag
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08001994
Laurence Lundbladea44d5062018-10-17 18:45:12 +05301995int IndefiniteLengthArrayMapTest()
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08001996{
Laurence Lundblade19e0c802018-10-13 12:19:55 +08001997 int nResult;
1998 // --- first test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07001999 UsefulBufC IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArray);
Laurence Lundblade17ede402018-10-13 11:43:07 +08002000
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002001 // Decode it and see if it is OK
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05302002 UsefulBuf_MAKE_STACK_UB(MemPool, 150);
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002003 QCBORDecodeContext DC;
2004 QCBORItem Item;
2005 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
2006
2007 QCBORDecode_SetMemPool(&DC, MemPool, false);
Laurence Lundblade6de37062018-10-15 12:22:42 +05302008
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002009 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05302010
2011 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
2012 Item.uNestingLevel != 0 ||
2013 Item.uNextNestLevel != 1) {
2014 return -111;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002015 }
2016
2017 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05302018 if(Item.uDataType != QCBOR_TYPE_INT64 ||
2019 Item.uNestingLevel != 1 ||
2020 Item.uNextNestLevel != 1) {
2021 return -2;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002022 }
2023
2024 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05302025 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
2026 Item.uNestingLevel != 1 ||
2027 Item.uNextNestLevel != 2) {
2028 return -3;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002029 }
2030
2031 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05302032 if(Item.uDataType != QCBOR_TYPE_INT64 |
2033 Item.uNestingLevel != 2 ||
2034 Item.uNextNestLevel != 2) {
2035 return -4;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002036 }
2037
2038 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05302039 if(Item.uDataType != QCBOR_TYPE_INT64 |
2040 Item.uNestingLevel != 2 ||
2041 Item.uNextNestLevel != 0) {
2042 return -5;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002043 }
2044
2045 if(QCBORDecode_Finish(&DC)) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05302046 return -6;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002047 }
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002048
2049 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002050 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad1);
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002051
2052 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
2053
2054 QCBORDecode_SetMemPool(&DC, MemPool, false);
2055
2056 nResult = QCBORDecode_GetNext(&DC, &Item);
2057 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05302058 return -7;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002059 }
2060
Laurence Lundblade570fab52018-10-13 18:28:27 +08002061 nResult = QCBORDecode_Finish(&DC);
Laurence Lundblade6de37062018-10-15 12:22:42 +05302062 if(nResult != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
2063 return -8;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002064 }
2065
2066
2067 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002068 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad2);
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002069
2070 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
2071
2072 QCBORDecode_SetMemPool(&DC, MemPool, false);
2073
2074 nResult = QCBORDecode_GetNext(&DC, &Item);
2075 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05302076 return -9;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002077 }
2078
2079 nResult = QCBORDecode_GetNext(&DC, &Item);
2080 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05302081 return -10;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002082 }
2083
2084 nResult = QCBORDecode_GetNext(&DC, &Item);
2085 if(nResult || Item.uDataType != QCBOR_TYPE_INT64) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05302086 return -11;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002087 }
2088
2089 nResult = QCBORDecode_Finish(&DC);
Laurence Lundblade6de37062018-10-15 12:22:42 +05302090 if(nResult != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
2091 return -12;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002092 }
2093
2094
2095 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002096 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad3);
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002097
2098 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
2099
2100 QCBORDecode_SetMemPool(&DC, MemPool, false);
2101
2102 nResult = QCBORDecode_GetNext(&DC, &Item);
2103 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05302104 return -13;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002105 }
2106
2107 nResult = QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05302108 if(nResult != QCBOR_ERR_BAD_BREAK) {
2109 return -14;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002110 }
Laurence Lundblade6de37062018-10-15 12:22:42 +05302111
Laurence Lundblade570fab52018-10-13 18:28:27 +08002112
2113 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002114 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad4);
Laurence Lundblade570fab52018-10-13 18:28:27 +08002115
2116 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
2117
2118 QCBORDecode_SetMemPool(&DC, MemPool, false);
2119
2120 nResult = QCBORDecode_GetNext(&DC, &Item);
2121 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05302122 return -15;
Laurence Lundblade570fab52018-10-13 18:28:27 +08002123 }
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302124
Laurence Lundblade570fab52018-10-13 18:28:27 +08002125 nResult = QCBORDecode_GetNext(&DC, &Item);
2126 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05302127 return -16;
Laurence Lundblade570fab52018-10-13 18:28:27 +08002128 }
2129
2130 nResult = QCBORDecode_Finish(&DC);
Laurence Lundblade6de37062018-10-15 12:22:42 +05302131 if(nResult != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
2132 return -17;
Laurence Lundblade570fab52018-10-13 18:28:27 +08002133 }
2134
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302135 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002136 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad5);
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302137
2138 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
2139
2140 QCBORDecode_SetMemPool(&DC, MemPool, false);
2141
2142 nResult = QCBORDecode_GetNext(&DC, &Item);
2143 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05302144 return -18;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302145 }
2146
2147 nResult = QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05302148 if(nResult != QCBOR_ERR_BAD_BREAK) {
2149 return -19;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302150 }
2151
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002152 return 0;
2153}
2154
Laurence Lundblade17ede402018-10-13 11:43:07 +08002155
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002156static const uint8_t spIndefiniteLenString[] = {
Laurence Lundblade17ede402018-10-13 11:43:07 +08002157 0x81, // Array of length one
2158 0x7f, // text string marked with indefinite length
2159 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment
2160 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment
2161 0xff // ending break
2162};
2163
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002164static const uint8_t spIndefiniteLenStringBad2[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302165 0x81, // Array of length one
2166 0x7f, // text string marked with indefinite length
2167 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment
2168 0x44, 0x6d, 0x69, 0x6e, 0x67, // second segment of wrong type
2169 0xff // ending break
2170};
2171
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002172static const uint8_t spIndefiniteLenStringBad3[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302173 0x81, // Array of length one
2174 0x7f, // text string marked with indefinite length
2175 0x01, 0x02, // Not a string
2176 0xff // ending break
2177};
2178
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002179static const uint8_t spIndefiniteLenStringBad4[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302180 0x81, // Array of length one
2181 0x7f, // text string marked with indefinite length
2182 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment
2183 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment
2184 // missing end of string
2185};
2186
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002187static const uint8_t spIndefiniteLenStringLabel[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302188 0xa1, // Array of length one
2189 0x7f, // text string marked with indefinite length
2190 0x65, 0x73, 0x74, 0x72, 0x75, 0x75, // first segment
2191 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment
2192 0xff, // ending break
2193 0x01 // integer being labeled.
2194};
2195
2196static UsefulBufC MakeIndefiniteBigBstr(UsefulBuf Storage)
2197{
2198 UsefulOutBuf UOB;
2199
2200 UsefulOutBuf_Init(&UOB, Storage);
2201 UsefulOutBuf_AppendByte(&UOB, 0x81);
2202 UsefulOutBuf_AppendByte(&UOB, 0x5f);
2203
2204 int i = 0;
2205 for(int nChunkSize = 1; nChunkSize <= 128; nChunkSize *= 2) {
2206 UsefulOutBuf_AppendByte(&UOB, 0x58);
2207 UsefulOutBuf_AppendByte(&UOB, (uint8_t)nChunkSize);
2208 for(int j = 0; j < nChunkSize; j++ ) {
2209 UsefulOutBuf_AppendByte(&UOB, i);
2210 i++;
2211 }
2212 }
2213 UsefulOutBuf_AppendByte(&UOB, 0xff);
2214
2215 return UsefulOutBuf_OutUBuf(&UOB);
2216}
2217
2218static int CheckBigString(UsefulBufC BigString)
2219{
2220 if(BigString.len != 255) {
2221 return 1;
2222 }
2223
2224 for(uint8_t i = 0; i < 255; i++){
2225 if(((const uint8_t *)BigString.ptr)[i] != i) {
2226 return 1;
2227 }
2228 }
2229 return 0;
2230}
2231
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05302232
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302233int IndefiniteLengthStringTest()
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302234{
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302235 QCBORDecodeContext DC;
2236 QCBORItem Item;
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05302237 // big enough for MakeIndefiniteBigBstr() + MemPool overhead
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05302238 UsefulBuf_MAKE_STACK_UB(MemPool, 320);
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302239
2240 // --- Simple normal indefinite length string ------
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002241 UsefulBufC IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenString);
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302242 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002243
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05302244 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302245 return -1;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302246 }
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002247
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302248 if(QCBORDecode_GetNext(&DC, &Item)) {
2249 return -2;
2250 }
2251 if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.uDataAlloc) {
2252 return -3;
2253 }
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002254
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302255 if(QCBORDecode_GetNext(&DC, &Item)) {
2256 return -4;
2257 }
2258 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || !Item.uDataAlloc) {
2259 return -5;
2260 }
2261 if(QCBORDecode_Finish(&DC)) {
2262 return -6;
2263 }
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302264
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302265 // ----- types mismatch ---
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002266 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad2), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302267
2268 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
2269 return -7;
2270 }
2271
2272 if(QCBORDecode_GetNext(&DC, &Item)) {
2273 return -8;
2274 }
2275 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
2276 return -9;
2277 }
2278
Laurence Lundblade30816f22018-11-10 13:40:22 +07002279 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_INDEFINITE_STRING_CHUNK) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302280 return -10;
2281 }
2282
2283 // ----- not a string ---
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002284 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad3), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302285
2286 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
2287 return -11;
2288 }
2289
2290 if(QCBORDecode_GetNext(&DC, &Item)) {
2291 return -12;
2292 }
2293 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
2294 return -13;
2295 }
2296
Laurence Lundblade30816f22018-11-10 13:40:22 +07002297 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_INDEFINITE_STRING_CHUNK) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302298 return -14;
2299 }
2300
2301 // ----- no end -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002302 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad4), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302303
2304 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
2305 return -15;
2306 }
2307
2308 if(QCBORDecode_GetNext(&DC, &Item)) {
2309 return -16;
2310 }
2311 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
2312 return -17;
2313 }
2314
2315 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_HIT_END) {
2316 return -18;
2317 }
2318
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302319 // ------ Don't set a string allocator and see an error -----
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302320 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
2321
2322 QCBORDecode_GetNext(&DC, &Item);
2323 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302324 return -19;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302325 }
2326
2327 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_NO_STRING_ALLOCATOR) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302328 return -20;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302329 }
2330
2331 // ----- Mempool is way too small -----
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05302332 UsefulBuf_MAKE_STACK_UB(MemPoolTooSmall, 20); // 20 is too small no matter what
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302333
2334 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
2335 if(!QCBORDecode_SetMemPool(&DC, MemPoolTooSmall, false)) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302336 return -21;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302337 }
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302338
2339 // ----- Mempool is way too small -----
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05302340 UsefulBuf_MAKE_STACK_UB(BigIndefBStrStorage, 290);
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302341 UsefulBufC BigIndefBStr = MakeIndefiniteBigBstr(BigIndefBStrStorage);
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302342
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05302343 UsefulBuf_MAKE_STACK_UB(MemPoolSmall, 80); // 80 is big enough for MemPool overhead, but not BigIndefBStr
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302344
2345 QCBORDecode_Init(&DC, BigIndefBStr, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302346 if(QCBORDecode_SetMemPool(&DC, MemPoolSmall, false)) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302347 return -22;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302348 }
2349
2350 QCBORDecode_GetNext(&DC, &Item);
2351 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302352 return -23;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302353 }
Laurence Lundblade30816f22018-11-10 13:40:22 +07002354 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_STRING_ALLOCATE) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302355 return -24;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302356 }
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302357
2358 // ---- big bstr -----
2359 QCBORDecode_Init(&DC, BigIndefBStr, QCBOR_DECODE_MODE_NORMAL);
2360
2361 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
2362 return -25;
2363 }
2364
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05302365 if(QCBORDecode_GetNext(&DC, &Item)) {
2366 return -26;
2367 }
2368 if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.uDataAlloc) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302369 return -26;
2370 }
2371
2372 if(QCBORDecode_GetNext(&DC, &Item)) {
2373 return -27;
2374 }
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05302375 if(Item.uDataType != QCBOR_TYPE_BYTE_STRING || !Item.uDataAlloc || Item.uNestingLevel != 1) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302376 return -28;
2377 }
2378 if(CheckBigString(Item.val.string)) {
2379 return -3;
2380 }
2381 if(QCBORDecode_Finish(&DC)) {
2382 return -29;
2383 }
2384
2385 // --- label is an indefinite length string ------
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002386 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringLabel), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302387
2388 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
2389 return -30;
2390 }
2391
2392 QCBORDecode_GetNext(&DC, &Item);
2393 if(Item.uDataType != QCBOR_TYPE_MAP) {
2394 return -31;
2395 }
2396
2397 if(QCBORDecode_GetNext(&DC, &Item)){
2398 return -32;
2399 }
2400 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || Item.uDataType != QCBOR_TYPE_INT64 ||
2401 Item.uDataAlloc || !Item.uLabelAlloc ||
2402 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("struuming"))) {
2403 return -33;
2404 }
2405
2406 if(QCBORDecode_Finish(&DC)) {
2407 return -34;
2408 }
2409
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002410 return 0;
2411}
2412
2413
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302414int AllocAllStringsTest()
2415{
2416 QCBORDecodeContext DC;
2417
2418 // First test, use the "CSRMap" as easy input and checking
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002419 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302420
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05302421 UsefulBuf_MAKE_STACK_UB(Pool, 300);
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302422
2423 QCBORDecode_SetMemPool(&DC, Pool, 1); // Turn on copying.
2424
2425 if(CheckCSRMaps(&DC)) {
2426 return -1;
2427 }
2428
2429 // Next parse, save pointers to a few strings, destroy original and see all is OK.
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05302430 UsefulBuf_MAKE_STACK_UB(CopyOfStorage, 160);
2431 UsefulBufC CopyOf = UsefulBuf_Copy(CopyOfStorage, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded));
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002432
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302433 QCBORDecode_Init(&DC, CopyOf, QCBOR_DECODE_MODE_NORMAL);
2434 QCBORDecode_SetMemPool(&DC, Pool, 1); // Turn on copying.
2435
2436 int nCBORError;
2437 QCBORItem Item1, Item2, Item3, Item4;
2438 if((nCBORError = QCBORDecode_GetNext(&DC, &Item1)))
2439 return nCBORError;
2440 if(Item1.uDataType != QCBOR_TYPE_MAP ||
2441 Item1.val.uCount != 3)
2442 return -1;
2443 if((nCBORError = QCBORDecode_GetNext(&DC, &Item1)))
2444 return nCBORError;
2445 if((nCBORError = QCBORDecode_GetNext(&DC, &Item2)))
2446 return nCBORError;
2447 if((nCBORError = QCBORDecode_GetNext(&DC, &Item3)))
2448 return nCBORError;
2449 if((nCBORError = QCBORDecode_GetNext(&DC, &Item4)))
2450 return nCBORError;
2451
Laurence Lundblade05ec57b2018-10-21 01:50:03 +05302452 UsefulBuf_Set(CopyOfStorage, '_');
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302453
2454 if(Item1.uLabelType != QCBOR_TYPE_TEXT_STRING ||
2455 Item1.label.string.len != 13 ||
2456 Item1.uDataType != QCBOR_TYPE_INT64 ||
2457 Item1.val.int64 != 42 ||
2458 memcmp(Item1.label.string.ptr, "first integer", 13))
2459 return -1;
2460
2461
2462 if(Item2.uLabelType != QCBOR_TYPE_TEXT_STRING ||
2463 Item2.label.string.len != 23 ||
2464 memcmp(Item2.label.string.ptr, "an array of two strings", 23) ||
2465 Item2.uDataType != QCBOR_TYPE_ARRAY ||
2466 Item2.val.uCount != 2)
2467 return -1;
2468
2469 if(Item3.uDataType != QCBOR_TYPE_TEXT_STRING ||
2470 Item3.val.string.len != 7 ||
2471 memcmp(Item3.val.string.ptr, "string1", 7))
2472 return -1;
2473
2474 if(Item4.uDataType != QCBOR_TYPE_TEXT_STRING ||
2475 Item4.val.string.len != 7 ||
2476 memcmp(Item4.val.string.ptr, "string2", 7))
2477 return -1;
2478
2479 // Next parse with a pool that is too small
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05302480 UsefulBuf_MAKE_STACK_UB(SmallPool, 80);
2481 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302482 QCBORDecode_SetMemPool(&DC, SmallPool, 1); // Turn on copying.
2483 if((nCBORError = QCBORDecode_GetNext(&DC, &Item1)))
2484 return nCBORError;
2485 if(Item1.uDataType != QCBOR_TYPE_MAP ||
2486 Item1.val.uCount != 3)
2487 return -1;
2488 if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item1))){
2489 if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item2))) {
2490 if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item3))) {
2491 nCBORError = QCBORDecode_GetNext(&DC, &Item4);
2492 }
2493 }
2494 }
Laurence Lundblade30816f22018-11-10 13:40:22 +07002495 if(nCBORError != QCBOR_ERR_STRING_ALLOCATE) {
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302496 return -5;
2497 }
2498
2499 return 0;
2500}
2501
2502
2503int MemPoolTest(void)
Laurence Lundblade0155b622018-10-12 20:04:37 +08002504{
2505 QCBORDecodeContext DC;
2506
2507 const uint8_t pMinimalCBOR[] = {0xa0}; // One empty map
2508
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05302509 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pMinimalCBOR),0);
Laurence Lundblade0155b622018-10-12 20:04:37 +08002510
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05302511 UsefulBuf_MAKE_STACK_UB(Pool, 100);
Laurence Lundblade0155b622018-10-12 20:04:37 +08002512
2513 QCBORDecode_SetMemPool(&DC, Pool, 0);
2514
2515 // Cheat a little to get to the string allocator object
2516 // so we can call it directly to test it
2517 QCBORStringAllocator *pAlloc = (QCBORStringAllocator *)DC.pStringAllocator;
2518
2519 // Ask for too much in one go
2520 // 90 < 100, but there is some overhead taken out of the 100
2521 UsefulBuf Allocated = (*pAlloc->fAllocate)(pAlloc->pAllocaterContext, NULL, 90);
2522 if(!UsefulBuf_IsNULL(Allocated)) {
2523 return -1;
2524 }
2525
2526
2527
2528 QCBORDecode_SetMemPool(&DC, Pool, 0);
2529
2530 // Cheat a little to get to the string allocator object
2531 // so we can call it directly to test it
2532 pAlloc = (QCBORStringAllocator *)DC.pStringAllocator;
2533
2534 Allocated = (*pAlloc->fAllocate)(pAlloc->pAllocaterContext, NULL, 30);
2535 if(UsefulBuf_IsNULL(Allocated)) { // expected to succeed
2536 return -1;
2537 }
2538 UsefulBuf Allocated2 = (*pAlloc->fAllocate)(pAlloc->pAllocaterContext, NULL, 30);
2539 if(!UsefulBuf_IsNULL(Allocated2)) { // expected to fail
2540 return -1;
2541 }
2542 (*pAlloc->fFree)(pAlloc->pAllocaterContext, Allocated.ptr);
2543 Allocated = (*pAlloc->fAllocate)(pAlloc->pAllocaterContext, NULL, 30);
2544 if(UsefulBuf_IsNULL(Allocated)) { // succeed because of the free
2545 return -1;
2546 }
2547
2548
2549 QCBORDecode_SetMemPool(&DC, Pool, 0);
2550
2551 // Cheat a little to get to the string allocator object
2552 // so we can call it directly to test it
2553 pAlloc = (QCBORStringAllocator *)DC.pStringAllocator;
2554 Allocated = (*pAlloc->fAllocate)(pAlloc->pAllocaterContext, NULL, 20);
2555 if(UsefulBuf_IsNULL(Allocated)) { // expected to succeed
2556 return -1;
2557 }
2558 Allocated2 = (*pAlloc->fAllocate)(pAlloc->pAllocaterContext, Allocated.ptr, 25);
2559 if(UsefulBuf_IsNULL(Allocated2)) { // expected to fail
2560 return -1;
2561 }
2562 if(Allocated2.ptr != Allocated.ptr || Allocated2.len != 25) {
2563 return -1;
2564 }
2565
Laurence Lundblade0155b622018-10-12 20:04:37 +08002566 return 0;
2567}
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002568
2569
2570
Laurence Lundblade9e3651c2018-10-10 11:49:55 +08002571
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002572
2573