blob: 3fe705ce76d1ce9c815a4579def6115d206002d8 [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 Lundblade3aee3a32018-12-17 16:17:45 -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 Lundblade3aee3a32018-12-17 16:17:45 -080019
Laurence Lundblade0dbc9172018-11-01 14:17:21 +070020THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
21WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
22MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
23ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
24BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
27BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
29OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
30IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Laurence Lundblade9e3651c2018-10-10 11:49:55 +080031 ==============================================================================*/
32
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080033#include "qcbor.h"
34#include "qcbor_decode_tests.h"
Laurence Lundbladed4728fd2018-12-17 15:15:56 -080035#include <string.h>
Laurence Lundblade9e3651c2018-10-10 11:49:55 +080036#include <math.h> // for fabs()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080037#include <stdlib.h>
38
Laurence Lundbladea2e29072018-12-30 09:20:06 -080039#ifdef PRINT_FUNCTIONS_FOR_DEBUGGING
Laurence Lundblade20db9c92018-12-17 11:40:37 -080040#include <stdio.h>
Laurence Lundbladea2e29072018-12-30 09:20:06 -080041
42static void PrintUsefulBufC(const char *szLabel, UsefulBufC Buf)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080043{
44 if(szLabel) {
45 printf("%s ", szLabel);
46 }
Laurence Lundbladea2e29072018-12-30 09:20:06 -080047
Laurence Lundblade570fab52018-10-13 18:28:27 +080048 size_t i;
Laurence Lundbladea2e29072018-12-30 09:20:06 -080049 for(i = 0; i < Buf.len; i++) {
50 uint8_t Z = ((uint8_t *)Buf.ptr)[i];
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080051 printf("%02x ", Z);
52 }
53 printf("\n");
Laurence Lundbladea2e29072018-12-30 09:20:06 -080054
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080055 fflush(stdout);
56}
Laurence Lundbladea2e29072018-12-30 09:20:06 -080057
58/*static void printencoded(const char *szLabel, const uint8_t *pEncoded, size_t nLen)
59{
60 PrintUsefulBufC(szLabel, (UsefulBufC){pEncoded, nLen});
61}*/
Laurence Lundblade20db9c92018-12-17 11:40:37 -080062#endif
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080063
64
Laurence Lundbladeb836efb2018-10-28 20:09:58 +070065static const uint8_t spExpectedEncodedInts[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080066 0x98, 0x2f, 0x3b, 0x7f, 0xff, 0xff, 0xff, 0xff,
67 0xff, 0xff, 0xff, 0x3b, 0x00, 0x00, 0x00, 0x01,
68 0x00, 0x00, 0x00, 0x00, 0x3a, 0xff, 0xff, 0xff,
69 0xff, 0x3a, 0xff, 0xff, 0xff, 0xfe, 0x3a, 0xff,
70 0xff, 0xff, 0xfd, 0x3a, 0x7f, 0xff, 0xff, 0xff,
71 0x3a, 0x7f, 0xff, 0xff, 0xfe, 0x3a, 0x00, 0x01,
72 0x00, 0x01, 0x3a, 0x00, 0x01, 0x00, 0x00, 0x39,
73 0xff, 0xff, 0x39, 0xff, 0xfe, 0x39, 0xff, 0xfd,
74 0x39, 0x01, 0x00, 0x38, 0xff, 0x38, 0xfe, 0x38,
75 0xfd, 0x38, 0x18, 0x37, 0x36, 0x20, 0x00, 0x00,
76 0x01, 0x16, 0x17, 0x18, 0x18, 0x18, 0x19, 0x18,
77 0x1a, 0x18, 0xfe, 0x18, 0xff, 0x19, 0x01, 0x00,
78 0x19, 0x01, 0x01, 0x19, 0xff, 0xfe, 0x19, 0xff,
79 0xff, 0x1a, 0x00, 0x01, 0x00, 0x00, 0x1a, 0x00,
80 0x01, 0x00, 0x01, 0x1a, 0x00, 0x01, 0x00, 0x02,
81 0x1a, 0x7f, 0xff, 0xff, 0xff, 0x1a, 0x7f, 0xff,
82 0xff, 0xff, 0x1a, 0x80, 0x00, 0x00, 0x00, 0x1a,
83 0x80, 0x00, 0x00, 0x01, 0x1a, 0xff, 0xff, 0xff,
84 0xfe, 0x1a, 0xff, 0xff, 0xff, 0xff, 0x1b, 0x00,
85 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x1b,
86 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01,
87 0x1b, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
88 0xff, 0x1b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
89 0xff, 0xff};
90
91
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080092// return CBOR error or -1 if type of value doesn't match
93
94static int IntegerValuesParseTestInternal(QCBORDecodeContext *pDCtx)
95{
96 QCBORItem Item;
97 int nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -080098
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080099 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
100 return nCBORError;
101 if(Item.uDataType != QCBOR_TYPE_ARRAY)
102 return -1;
103
104 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
105 return nCBORError;
106 if(Item.uDataType != QCBOR_TYPE_INT64 || // Todo; fix this for 32-bit machines
Laurence Lundblade570fab52018-10-13 18:28:27 +0800107 Item.val.int64 != -9223372036854775807LL - 1)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800108 return -1;
109
110 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
111 return nCBORError;
112 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade570fab52018-10-13 18:28:27 +0800113 Item.val.int64 != -4294967297)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800114 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800115
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800116 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
117 return nCBORError;
118 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade570fab52018-10-13 18:28:27 +0800119 Item.val.int64 != -4294967296)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800120 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800121
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800122 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
123 return nCBORError;
124 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade570fab52018-10-13 18:28:27 +0800125 Item.val.int64 != -4294967295)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800126 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800127
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800128 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
129 return nCBORError;
130 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade570fab52018-10-13 18:28:27 +0800131 Item.val.int64 != -4294967294)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800132 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800133
134
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800135 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
136 return nCBORError;
137 if(Item.uDataType != QCBOR_TYPE_INT64 ||
138 Item.val.int64 != -2147483648)
139 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800140
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800141 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
142 return nCBORError;
143 if(Item.uDataType != QCBOR_TYPE_INT64 ||
144 Item.val.int64 != -2147483647)
145 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800146
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800147 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
148 return nCBORError;
149 if(Item.uDataType != QCBOR_TYPE_INT64 ||
150 Item.val.int64 != -65538)
151 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800152
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800153 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
154 return nCBORError;
155 if(Item.uDataType != QCBOR_TYPE_INT64 ||
156 Item.val.int64 != -65537)
157 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800158
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800159 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
160 return nCBORError;
161 if(Item.uDataType != QCBOR_TYPE_INT64 ||
162 Item.val.int64 != -65536)
163 return -1;
164
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800165
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800166 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
167 return nCBORError;
168 if(Item.uDataType != QCBOR_TYPE_INT64 ||
169 Item.val.int64 != -65535)
170 return -1;
171
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800172
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800173 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
174 return nCBORError;
175 if(Item.uDataType != QCBOR_TYPE_INT64 ||
176 Item.val.int64 != -65534)
177 return -1;
178
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800179
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800180 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
181 return nCBORError;
182 if(Item.uDataType != QCBOR_TYPE_INT64 ||
183 Item.val.int64 != -257)
184 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800185
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800186 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
187 return nCBORError;
188 if(Item.uDataType != QCBOR_TYPE_INT64 ||
189 Item.val.int64 != -256)
190 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800191
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800192 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
193 return nCBORError;
194 if(Item.uDataType != QCBOR_TYPE_INT64 ||
195 Item.val.int64 != -255)
196 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800197
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800198 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
199 return nCBORError;
200 if(Item.uDataType != QCBOR_TYPE_INT64 ||
201 Item.val.int64 != -254)
202 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800203
204
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800205 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
206 return nCBORError;
207 if(Item.uDataType != QCBOR_TYPE_INT64 ||
208 Item.val.int64 != -25)
209 return -1;
210
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800211
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800212 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
213 return nCBORError;
214 if(Item.uDataType != QCBOR_TYPE_INT64 ||
215 Item.val.int64 != -24)
216 return -1;
217
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800218
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800219 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
220 return nCBORError;
221 if(Item.uDataType != QCBOR_TYPE_INT64 ||
222 Item.val.int64 != -23)
223 return -1;
224
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800225
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800226 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
227 return nCBORError;
228 if(Item.uDataType != QCBOR_TYPE_INT64 ||
229 Item.val.int64 != -1)
230 return -1;
231
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800232
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800233 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
234 return nCBORError;
235 if(Item.uDataType != QCBOR_TYPE_INT64 ||
236 Item.val.int64 != 0)
237 return -1;
238
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800239
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800240 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
241 return nCBORError;
242 if(Item.uDataType != QCBOR_TYPE_INT64 ||
243 Item.val.int64 != 0)
244 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800245
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800246 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
247 return nCBORError;
248 if(Item.uDataType != QCBOR_TYPE_INT64 ||
249 Item.val.int64 != 1)
250 return -1;
251
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800252
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800253 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
254 return nCBORError;
255 if(Item.uDataType != QCBOR_TYPE_INT64 ||
256 Item.val.int64 != 22)
257 return -1;
258
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800259
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800260 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
261 return nCBORError;
262 if(Item.uDataType != QCBOR_TYPE_INT64 ||
263 Item.val.int64 != 23)
264 return -1;
265
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800266
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800267 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
268 return nCBORError;
269 if(Item.uDataType != QCBOR_TYPE_INT64 ||
270 Item.val.int64 != 24)
271 return -1;
272
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800273
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800274 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
275 return nCBORError;
276 if(Item.uDataType != QCBOR_TYPE_INT64 ||
277 Item.val.int64 != 25)
278 return -1;
279
280 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
281 return nCBORError;
282 if(Item.uDataType != QCBOR_TYPE_INT64 ||
283 Item.val.int64 != 26)
284 return -1;
285
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800286
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800287 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
288 return nCBORError;
289 if(Item.uDataType != QCBOR_TYPE_INT64 ||
290 Item.val.int64 != 254)
291 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800292
293
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800294 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
295 return nCBORError;
296 if(Item.uDataType != QCBOR_TYPE_INT64 ||
297 Item.val.int64 != 255)
298 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800299
300
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800301 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
302 return nCBORError;
303 if(Item.uDataType != QCBOR_TYPE_INT64 ||
304 Item.val.int64 != 256)
305 return -1;
306
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800307
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800308 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
309 return nCBORError;
310 if(Item.uDataType != QCBOR_TYPE_INT64 ||
311 Item.val.int64 != 257)
312 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800313
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800314 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
315 return nCBORError;
316 if(Item.uDataType != QCBOR_TYPE_INT64 ||
317 Item.val.int64 != 65534)
318 return -1;
319
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800320
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800321 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
322 return nCBORError;
323 if(Item.uDataType != QCBOR_TYPE_INT64 ||
324 Item.val.int64 != 65535)
325 return -1;
326
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800327
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800328 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
329 return nCBORError;
330 if(Item.uDataType != QCBOR_TYPE_INT64 ||
331 Item.val.int64 != 65536)
332 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800333
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800334 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
335 return nCBORError;
336 if(Item.uDataType != QCBOR_TYPE_INT64 ||
337 Item.val.int64 != 65537)
338 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800339
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800340 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
341 return nCBORError;
342 if(Item.uDataType != QCBOR_TYPE_INT64 ||
343 Item.val.int64 != 65538)
344 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800345
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800346 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
347 return nCBORError;
348 if(Item.uDataType != QCBOR_TYPE_INT64 ||
349 Item.val.int64 != 2147483647)
350 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800351
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800352 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
353 return nCBORError;
354 if(Item.uDataType != QCBOR_TYPE_INT64 ||
355 Item.val.int64 != 2147483647)
356 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800357
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800358 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
359 return nCBORError;
360 if(Item.uDataType != QCBOR_TYPE_INT64 ||
361 Item.val.int64 != 2147483648)
362 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800363
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800364 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
365 return nCBORError;
366 if(Item.uDataType != QCBOR_TYPE_INT64 ||
367 Item.val.int64 != 2147483649)
368 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800369
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800370 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
371 return nCBORError;
372 if(Item.uDataType != QCBOR_TYPE_INT64 ||
373 Item.val.int64 != 4294967294)
374 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800375
376
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800377 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
378 return nCBORError;
379 if(Item.uDataType != QCBOR_TYPE_INT64 ||
380 Item.val.int64 != 4294967295)
381 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800382
383
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800384 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
385 return nCBORError;
386 if(Item.uDataType != QCBOR_TYPE_INT64 ||
387 Item.val.int64 != 4294967296)
388 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800389
390
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800391 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
392 return nCBORError;
393 if(Item.uDataType != QCBOR_TYPE_INT64 ||
394 Item.val.int64 != 4294967297)
395 return -1;
396
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800397
398
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800399 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
400 return nCBORError;
401 if(Item.uDataType != QCBOR_TYPE_INT64 ||
402 Item.val.int64 != 9223372036854775807LL)
403 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800404
405
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800406 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
407 return nCBORError;
408 if(Item.uDataType != QCBOR_TYPE_UINT64 ||
409 Item.val.uint64 != 18446744073709551615ULL)
410 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800411
412
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800413 if(QCBORDecode_Finish(pDCtx) != QCBOR_SUCCESS) {
414 return -1;
415 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800416
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800417 return 0;
418}
419
420
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800421/*
422 Tests the decoding of lots of different integers sizes
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +0800423 and values.
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800424 */
425
426int IntegerValuesParseTest()
427{
428 int n;
429 QCBORDecodeContext DCtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800430
Laurence Lundbladeb836efb2018-10-28 20:09:58 +0700431 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedEncodedInts), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800432
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800433 n = IntegerValuesParseTestInternal(&DCtx);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800434
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800435 return(n);
436}
437
438
439/*
440 Creates a simple CBOR array and returns it in *pEncoded. The array is malloced
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800441 and needs to be freed. This is used by several tests.
442
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800443 Two of the inputs can be set. Two other items in the array are fixed.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800444
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800445 */
446
447static int CreateSimpleArray(int nInt1, int nInt2, uint8_t **pEncoded, size_t *pEncodedLen)
448{
449 QCBOREncodeContext ECtx;
450 int nReturn = -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800451
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800452 *pEncoded = NULL;
453 *pEncodedLen = INT32_MAX;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800454
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800455 // loop runs CBOR encoding twice. First with no buffer to
456 // calucate the length so buffer can be allocated correctly,
457 // and last with the buffer to do the actual encoding
458 do {
Laurence Lundblade0595e932018-11-02 22:22:47 +0700459 QCBOREncode_Init(&ECtx, (UsefulBuf){*pEncoded, *pEncodedLen});
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800460 QCBOREncode_OpenArray(&ECtx);
461 QCBOREncode_AddInt64(&ECtx, nInt1);
462 QCBOREncode_AddInt64(&ECtx, nInt2);
463 QCBOREncode_AddBytes(&ECtx, ((UsefulBufC) {"galactic", 8}));
464 QCBOREncode_AddBytes(&ECtx, ((UsefulBufC) {"haven token", 11}));
465 QCBOREncode_CloseArray(&ECtx);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800466
Laurence Lundblade0595e932018-11-02 22:22:47 +0700467 UsefulBufC Encoded;
468 if(QCBOREncode_Finish(&ECtx, &Encoded))
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800469 goto Done;
470
471 if(*pEncoded != NULL) {
Laurence Lundblade0595e932018-11-02 22:22:47 +0700472 *pEncodedLen = Encoded.len;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800473 nReturn = 0;
474 goto Done;
475 }
Laurence Lundblade0595e932018-11-02 22:22:47 +0700476 *pEncoded = malloc(Encoded.len);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800477 if(*pEncoded == NULL) {
478 nReturn = -1;
479 goto Done;
480 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800481
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800482 } while(1);
483Done:
484 return (nReturn);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800485
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800486}
487
488
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800489/*
490 {"first integer": 42,
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900491 "an array of two strings": [
492 "string1", "string2"
493 ],
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800494 "map in a map": {
495 "bytes 1": h'78787878',
496 "bytes 2": h'79797979',
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900497 "another int": 98,
498 "text 2": "lies, damn lies and statistics"
499 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800500 }
501 */
502
503static uint8_t pValidMapEncoded[] = {
Laurence Lundbladeb836efb2018-10-28 20:09:58 +0700504 0xa3, 0x6d, 0x66, 0x69, 0x72, 0x73, 0x74, 0x20, 0x69, 0x6e,
505 0x74, 0x65, 0x67, 0x65, 0x72, 0x18, 0x2a, 0x77, 0x61, 0x6e,
506 0x20, 0x61, 0x72, 0x72, 0x61, 0x79, 0x20, 0x6f, 0x66, 0x20,
507 0x74, 0x77, 0x6f, 0x20, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67,
508 0x73, 0x82, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x31,
509 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x32, 0x6c, 0x6d,
510 0x61, 0x70, 0x20, 0x69, 0x6e, 0x20, 0x61, 0x20, 0x6d, 0x61,
511 0x70, 0xa4, 0x67, 0x62, 0x79, 0x74, 0x65, 0x73, 0x20, 0x31,
512 0x44, 0x78, 0x78, 0x78, 0x78, 0x67, 0x62, 0x79, 0x74, 0x65,
513 0x73, 0x20, 0x32, 0x44, 0x79, 0x79, 0x79, 0x79, 0x6b, 0x61,
514 0x6e, 0x6f, 0x74, 0x68, 0x65, 0x72, 0x20, 0x69, 0x6e, 0x74,
515 0x18, 0x62, 0x66, 0x74, 0x65, 0x78, 0x74, 0x20, 0x32, 0x78,
516 0x1e, 0x6c, 0x69, 0x65, 0x73, 0x2c, 0x20, 0x64, 0x61, 0x6d,
517 0x6e, 0x20, 0x6c, 0x69, 0x65, 0x73, 0x20, 0x61, 0x6e, 0x64,
518 0x20, 0x73, 0x74, 0x61, 0x74, 0x69, 0x73, 0x74, 0x69, 0x63,
519 0x73 } ;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800520
521static 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)
522{
523 QCBORDecodeContext DCtx;
524 QCBORItem Item;
525 int nReturn = -1; // assume error until success
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800526
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800527 QCBORDecode_Init(&DCtx, (UsefulBufC){pEncoded, nLen}, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800528
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800529 // Make sure the first thing is a map
530 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || Item.uDataType != QCBOR_TYPE_ARRAY)
531 goto Done;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800532
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800533 // First integer
Laurence Lundblade12b495d2018-12-17 11:15:54 -0800534 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || Item.uDataType != QCBOR_TYPE_INT64)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800535 goto Done;
536 *pInt1 = Item.val.int64;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800537
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800538 // Second integer
539 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || Item.uDataType != QCBOR_TYPE_INT64)
540 goto Done;
541 *pInt2 = Item.val.int64;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800542
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800543 // First string
544 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || Item.uDataType != QCBOR_TYPE_BYTE_STRING)
545 goto Done;
546 *pBuf3 = Item.val.string.ptr;
547 *pBuf3Len = Item.val.string.len;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800548
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800549 // Second string
550 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || Item.uDataType != QCBOR_TYPE_BYTE_STRING)
551 goto Done;
552 *pBuf4 = Item.val.string.ptr;
553 *pBuf4Len = Item.val.string.len;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800554
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800555 nReturn = 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800556
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800557Done:
558 return(nReturn);
559}
560
561
562
Laurence Lundblade9e3651c2018-10-10 11:49:55 +0800563
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800564int SimpleArrayTest()
565{
566 uint8_t *pEncoded;
567 size_t nEncodedLen;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800568
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800569 int64_t i1, i2;
570 size_t i3, i4;
571 const uint8_t *s3, *s4;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800572
573
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800574 if(CreateSimpleArray(23, 6000, &pEncoded, &nEncodedLen) < 0) {
575 return(-1);
576 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800577
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800578 ParseOrderedArray(pEncoded, nEncodedLen, &i1, &i2, &s3, &i3, &s4, &i4);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800579
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800580 if(i1 != 23 ||
581 i2 != 6000 ||
582 i3 != 8 ||
583 i4 != 11 ||
Laurence Lundbladea44d5062018-10-17 18:45:12 +0530584 memcmp("galactic", s3, 8) !=0 ||
585 memcmp("haven token", s4, 11) !=0) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800586 return(-1);
587 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800588
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800589 return(0);
590}
591
592
Laurence Lundblade9e3651c2018-10-10 11:49:55 +0800593
Laurence Lundbladeb836efb2018-10-28 20:09:58 +0700594static uint8_t spDeepArrays[] = {0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x80};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800595
596int ParseDeepArrayTest()
597{
598 QCBORDecodeContext DCtx;
599 int nReturn = 0;
600 int i;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800601
Laurence Lundbladeb836efb2018-10-28 20:09:58 +0700602 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spDeepArrays), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800603
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800604 for(i = 0; i < 10; i++) {
605 QCBORItem Item;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800606
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800607 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
608 Item.uDataType != QCBOR_TYPE_ARRAY ||
609 Item.uNestingLevel != i) {
610 nReturn = -1;
611 break;
612 }
613 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800614
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800615 return(nReturn);
616}
617
Laurence Lundblade972e59c2018-11-11 15:57:23 +0700618// Big enough to test nesting to the depth of 24
619static uint8_t spTooDeepArrays[] = {0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81,
620 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81,
621 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81,
622 0x80};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800623
624int ParseTooDeepArrayTest()
625{
626 QCBORDecodeContext DCtx;
627 int nReturn = 0;
628 int i;
629 QCBORItem Item;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800630
631
Laurence Lundbladeb836efb2018-10-28 20:09:58 +0700632 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTooDeepArrays), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800633
Laurence Lundblade972e59c2018-11-11 15:57:23 +0700634 for(i = 0; i < QCBOR_MAX_ARRAY_NESTING1; i++) {
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800635
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800636 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
637 Item.uDataType != QCBOR_TYPE_ARRAY ||
638 Item.uNestingLevel != i) {
639 nReturn = -1;
640 break;
641 }
642 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800643
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800644 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_ARRAY_NESTING_TOO_DEEP)
645 nReturn = -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800646
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800647 return(nReturn);
648}
649
650
651
652
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800653int ShortBufferParseTest()
654{
655 int nResult = 0;
656 QCBORDecodeContext DCtx;
657 int num;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800658
Laurence Lundbladeb836efb2018-10-28 20:09:58 +0700659 for(num = sizeof(spExpectedEncodedInts)-1; num; num--) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800660 int n;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800661
Laurence Lundbladeb836efb2018-10-28 20:09:58 +0700662 QCBORDecode_Init(&DCtx, (UsefulBufC){spExpectedEncodedInts, num}, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800663
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800664 n = IntegerValuesParseTestInternal(&DCtx);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800665
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800666 //printf("Len %d, result: %d\n", num, n);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800667
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800668 if(n != QCBOR_ERR_HIT_END) {
669 nResult = -1;
670 goto Done;
671 }
672 }
673Done:
674 return nResult;
675}
676
677
Laurence Lundblade9e3651c2018-10-10 11:49:55 +0800678
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800679int ShortBufferParseTest2()
680{
681 uint8_t *pEncoded;
682 int nReturn;
683 size_t nEncodedLen;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800684
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800685 int64_t i1, i2;
686 size_t i3, i4;
687 const uint8_t *s3, *s4;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800688
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800689 nReturn = 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800690
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800691 if(CreateSimpleArray(23, 6000, &pEncoded, &nEncodedLen) < 0) {
692 return(-1);
693 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800694
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800695 for(nEncodedLen--; nEncodedLen; nEncodedLen--) {
696 int nResult = ParseOrderedArray(pEncoded, (uint32_t)nEncodedLen, &i1, &i2, &s3, &i3, &s4, &i4);
697 if(nResult == 0) {
698 nReturn = -1;
699 }
700 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800701
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800702 return(nReturn);
703}
704
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530705/*
706 Decode and thoroughly check a moderately complex
707 set of maps
708 */
Laurence Lundbladeea567ac2018-12-09 14:03:21 -0800709static int ParseMapTest1(QCBORDecodeMode nMode)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800710{
711 QCBORDecodeContext DCtx;
712 QCBORItem Item;
713 int nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800714
Laurence Lundbladeea567ac2018-12-09 14:03:21 -0800715 QCBORDecode_Init(&DCtx, (UsefulBufC){pValidMapEncoded, sizeof(pValidMapEncoded)}, nMode);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800716
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900717 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800718 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900719 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800720 if(Item.uDataType != QCBOR_TYPE_MAP ||
721 Item.val.uCount != 3)
722 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800723
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900724 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800725 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900726 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800727 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800728 Item.uDataType != QCBOR_TYPE_INT64 ||
729 Item.val.int64 != 42 ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530730 Item.uDataAlloc ||
731 Item.uLabelAlloc ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900732 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("first integer"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800733 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900734 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800735
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900736 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800737 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900738 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800739 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530740 Item.uDataAlloc ||
741 Item.uLabelAlloc ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900742 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("an array of two strings")) ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800743 Item.uDataType != QCBOR_TYPE_ARRAY ||
744 Item.val.uCount != 2)
745 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800746
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900747 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800748 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900749 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800750 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530751 Item.uDataAlloc ||
752 Item.uLabelAlloc ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900753 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("string1"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800754 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900755 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800756
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900757 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800758 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900759 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800760 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530761 Item.uDataAlloc ||
762 Item.uLabelAlloc ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900763 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("string2"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800764 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900765 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800766
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900767 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800768 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900769 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800770 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530771 Item.uDataAlloc ||
772 Item.uLabelAlloc ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900773 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("map in a map")) ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800774 Item.uDataType != QCBOR_TYPE_MAP ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900775 Item.val.uCount != 4) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800776 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900777 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800778
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900779 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800780 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900781 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800782 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900783 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("bytes 1"))||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800784 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530785 Item.uDataAlloc ||
786 Item.uLabelAlloc ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900787 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("xxxx"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800788 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900789 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800790
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900791 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800792 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900793 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800794 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900795 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("bytes 2")) ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800796 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530797 Item.uDataAlloc ||
798 Item.uLabelAlloc ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900799 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("yyyy"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800800 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900801 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800802
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900803 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800804 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900805 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800806 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530807 Item.uDataAlloc ||
808 Item.uLabelAlloc ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900809 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("another int")) ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800810 Item.uDataType != QCBOR_TYPE_INT64 ||
811 Item.val.int64 != 98)
812 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800813
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900814 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800815 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900816 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800817 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900818 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("text 2"))||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800819 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530820 Item.uDataAlloc ||
821 Item.uLabelAlloc ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900822 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("lies, damn lies and statistics"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800823 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900824 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800825
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800826 return 0;
827}
828
829
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900830/*
831 Decode and thoroughly check a moderately complex
832 set of maps
833 */
834int ParseMapAsArrayTest()
835{
836 QCBORDecodeContext DCtx;
837 QCBORItem Item;
838 int nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800839
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900840 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), QCBOR_DECODE_MODE_MAP_AS_ARRAY);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800841
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900842 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
843 return nCBORError;
844 }
Laurence Lundbladed61cbf32018-12-09 11:42:21 -0800845 if(Item.uDataType != QCBOR_TYPE_MAP_AS_ARRAY ||
846 Item.val.uCount != 6) {
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900847 return -1;
848 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800849
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900850 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
851 return nCBORError;
852 }
853 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
854 Item.uDataAlloc ||
855 Item.uLabelAlloc ||
856 Item.uLabelType != QCBOR_TYPE_NONE ||
857 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("first integer"))) {
858 return -2;
859 }
860
861 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
862 return nCBORError;
863 }
864 if(Item.uLabelType != QCBOR_TYPE_NONE ||
865 Item.uDataType != QCBOR_TYPE_INT64 ||
866 Item.val.int64 != 42 ||
867 Item.uDataAlloc ||
868 Item.uLabelAlloc) {
869 return -3;
870 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800871
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900872 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
873 return nCBORError;
874 }
875 if(Item.uLabelType != QCBOR_TYPE_NONE ||
876 Item.uDataAlloc ||
877 Item.uLabelAlloc ||
878 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("an array of two strings")) ||
879 Item.uDataType != QCBOR_TYPE_TEXT_STRING) {
880 return -4;
881 }
882
883 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
884 return nCBORError;
885 }
886 if(Item.uLabelType != QCBOR_TYPE_NONE ||
887 Item.uDataAlloc ||
888 Item.uLabelAlloc ||
889 Item.uDataType != QCBOR_TYPE_ARRAY ||
890 Item.val.uCount != 2) {
891 return -5;
892 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800893
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900894 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
895 return nCBORError;
896 }
897 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
898 Item.val.string.len != 7 ||
899 Item.uDataAlloc ||
900 Item.uLabelAlloc ||
901 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("string1"))) {
902 return -6;
903 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800904
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900905 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
906 return nCBORError;
907 }
908 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
909 Item.uDataAlloc ||
910 Item.uLabelAlloc ||
911 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("string2"))) {
912 return -7;
913 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800914
915
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900916 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
917 return nCBORError;
918 }
919 if(Item.uLabelType != QCBOR_TYPE_NONE ||
920 Item.uDataAlloc ||
921 Item.uLabelAlloc ||
922 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("map in a map"))) {
923 return -8;
924 }
925
926 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
927 return nCBORError;
928 }
929 if(Item.uLabelType != QCBOR_TYPE_NONE ||
930 Item.uDataAlloc ||
931 Item.uLabelAlloc ||
Laurence Lundbladed61cbf32018-12-09 11:42:21 -0800932 Item.uDataType != QCBOR_TYPE_MAP_AS_ARRAY ||
933 Item.val.uCount != 8) {
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900934 return -9;
935 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800936
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900937 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
938 return nCBORError;
939 }
940 if(Item.uLabelType != QCBOR_TYPE_NONE ||
941 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("bytes 1"))||
942 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
943 Item.uDataAlloc ||
944 Item.uLabelAlloc) {
945 return -10;
946 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800947
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900948 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
949 return nCBORError;
950 }
951 if(Item.uLabelType != QCBOR_TYPE_NONE ||
952 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
953 Item.uDataAlloc ||
954 Item.uLabelAlloc ||
955 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("xxxx"))) {
956 return -11;
957 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800958
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900959 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
960 return nCBORError;
961 }
962 if(Item.uLabelType != QCBOR_TYPE_NONE ||
963 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("bytes 2")) ||
964 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
965 Item.uDataAlloc ||
966 Item.uLabelAlloc) {
967 return -12;
968 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800969
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900970 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
971 return nCBORError;
972 }
973 if(Item.uLabelType != QCBOR_TYPE_NONE ||
974 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
975 Item.uDataAlloc ||
976 Item.uLabelAlloc ||
977 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("yyyy"))) {
978 return -13;
979 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800980
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900981 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
982 return nCBORError;
983 }
984 if(Item.uLabelType != QCBOR_TYPE_NONE ||
985 Item.uDataAlloc ||
986 Item.uLabelAlloc ||
987 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("another int")) ||
988 Item.uDataType != QCBOR_TYPE_TEXT_STRING) {
989 return -14;
990 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800991
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900992 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
993 return nCBORError;
994 }
995 if(Item.uLabelType != QCBOR_TYPE_NONE ||
996 Item.uDataAlloc ||
997 Item.uLabelAlloc ||
998 Item.uDataType != QCBOR_TYPE_INT64 ||
999 Item.val.int64 != 98) {
1000 return -15;
1001 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001002
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001003 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
1004 return nCBORError;
1005 }
1006 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1007 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("text 2"))||
1008 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1009 Item.uDataAlloc ||
1010 Item.uLabelAlloc) {
1011 return -16;
1012 }
1013
1014 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
1015 return nCBORError;
1016 }
1017 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1018 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1019 Item.uDataAlloc ||
1020 Item.uLabelAlloc ||
1021 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("lies, damn lies and statistics"))) {
1022 return -17;
1023 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001024
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001025 return 0;
1026}
1027
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001028
1029/*
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301030 Fully or partially decode pValidMapEncoded. When
1031 partially decoding check for the right error code.
1032 How much partial decoding depends on nLevel.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001033
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301034 The partial decodes test error conditions of
1035 incomplete encoded input.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001036
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301037 This could be combined with the above test
1038 and made prettier and maybe a little more
1039 thorough.
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001040 */
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001041static int ExtraBytesTest(int nLevel)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001042{
1043 QCBORDecodeContext DCtx;
1044 QCBORItem Item;
1045 int nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001046
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001047 QCBORDecode_Init(&DCtx, (UsefulBufC){pValidMapEncoded, sizeof(pValidMapEncoded)}, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001048
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001049 if(nLevel < 1) {
1050 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_EXTRA_BYTES) {
1051 return -1;
1052 } else {
1053 return 0;
1054 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001055 }
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301056
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001057
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001058 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001059 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001060 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001061 if(Item.uDataType != QCBOR_TYPE_MAP ||
1062 Item.val.uCount != 3)
1063 return -1;
1064
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001065 if(nLevel < 2) {
1066 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
1067 return -1;
1068 } else {
1069 return 0;
1070 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001071 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001072
1073
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001074 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001075 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001076 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001077 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001078 Item.uDataType != QCBOR_TYPE_INT64 ||
1079 Item.val.uCount != 42 ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001080 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("first integer"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001081 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001082 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001083
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001084 if(nLevel < 3) {
1085 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
1086 return -1;
1087 } else {
1088 return 0;
1089 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001090 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001091
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001092 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001093 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001094 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001095 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001096 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("an array of two strings")) ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001097 Item.uDataType != QCBOR_TYPE_ARRAY ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001098 Item.val.uCount != 2) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001099 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001100 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001101
1102
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001103 if(nLevel < 4) {
1104 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
1105 return -1;
1106 } else {
1107 return 0;
1108 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001109 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001110
1111
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001112 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001113 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001114 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001115 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001116 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("string1"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001117 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001118 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001119
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001120 if(nLevel < 5) {
1121 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
1122 return -1;
1123 } else {
1124 return 0;
1125 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001126 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001127
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001128 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001129 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001130 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001131 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001132 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("string2"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001133 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001134 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001135
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001136 if(nLevel < 6) {
1137 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
1138 return -1;
1139 } else {
1140 return 0;
1141 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001142 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001143
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001144 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001145 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001146 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001147 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001148 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("map in a map")) ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001149 Item.uDataType != QCBOR_TYPE_MAP ||
1150 Item.val.uCount != 4)
1151 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001152
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001153 if(nLevel < 7) {
1154 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
1155 return -1;
1156 } else {
1157 return 0;
1158 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001159 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001160
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001161 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001162 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001163 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001164 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001165 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("bytes 1")) ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001166 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001167 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("xxxx"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001168 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001169 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001170
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001171 if(nLevel < 8) {
1172 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
1173 return -1;
1174 } else {
1175 return 0;
1176 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001177 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001178
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001179 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001180 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001181 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001182 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001183 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("bytes 2")) ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001184 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001185 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("yyyy"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001186 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001187 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001188
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001189 if(nLevel < 9) {
1190 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
1191 return -1;
1192 } else {
1193 return 0;
1194 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001195 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001196
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001197 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001198 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001199 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001200 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001201 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("another int")) ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001202 Item.uDataType != QCBOR_TYPE_INT64 ||
1203 Item.val.int64 != 98)
1204 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001205
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001206 if(nLevel < 10) {
1207 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
1208 return -1;
1209 } else {
1210 return 0;
1211 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001212 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001213
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001214 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001215 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001216 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001217 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001218 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("text 2"))||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001219 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001220 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("lies, damn lies and statistics"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001221 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001222 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001223
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301224 if(QCBORDecode_Finish(&DCtx)) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001225 return -1;
1226 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001227
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001228 return 0;
1229}
1230
1231
1232
1233
1234int ParseMapTest()
1235{
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301236 // Parse a moderatly complex map structure very thoroughl
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08001237 int n = ParseMapTest1(QCBOR_DECODE_MODE_NORMAL);
1238
1239 n = ParseMapTest1(QCBOR_DECODE_MODE_MAP_STRINGS_ONLY);
1240
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001241 if(!n) {
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001242 for(int i = 0; i < 10; i++) {
1243 n = ExtraBytesTest(i);
1244 if(n) {
1245 break;
1246 }
1247 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001248 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001249
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001250 return(n);
1251}
1252
1253
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07001254static 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 +08001255
1256int ParseSimpleTest()
1257{
1258 QCBORDecodeContext DCtx;
1259 QCBORItem Item;
1260 int nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001261
1262
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07001263 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSimpleValues), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001264
1265
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001266 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1267 return nCBORError;
1268 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
1269 Item.val.uCount != 10)
1270 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001271
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001272 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1273 return nCBORError;
1274 if(Item.uDataType != QCBOR_TYPE_FALSE)
1275 return -1;
1276
1277 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1278 return nCBORError;
1279 if(Item.uDataType != QCBOR_TYPE_TRUE)
1280 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001281
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001282 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1283 return nCBORError;
1284 if(Item.uDataType != QCBOR_TYPE_NULL)
1285 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001286
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001287 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1288 return nCBORError;
1289 if(Item.uDataType != QCBOR_TYPE_UNDEF)
1290 return -1;
1291
1292 // A break
Laurence Lundblade9e3651c2018-10-10 11:49:55 +08001293 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_BREAK)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001294 return -1;
1295
1296 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1297 return nCBORError;
1298 if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 0)
1299 return -1;
1300
1301 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1302 return nCBORError;
1303 if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 19)
1304 return -1;
1305
1306 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_INVALID_CBOR)
1307 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001308
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001309 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_INVALID_CBOR)
1310 return -1;
1311
1312 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_INVALID_CBOR)
1313 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001314
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001315 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1316 return nCBORError;
1317 if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 32)
1318 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001319
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001320 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1321 return nCBORError;
1322 if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 255)
1323 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001324
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001325 return 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001326
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001327}
1328
1329
1330struct FailInput {
1331 UsefulBufC Input;
1332 int nError;
1333};
1334
1335
1336struct FailInput Failures[] = {
1337 { {(uint8_t[]){0x18}, 1}, QCBOR_ERR_HIT_END }, // 1 byte integer missing the byte
1338 { {(uint8_t[]){0x1c}, 1}, QCBOR_ERR_UNSUPPORTED }, // Reserved additional info = 28
1339 { {(uint8_t[]){0x1d}, 1}, QCBOR_ERR_UNSUPPORTED }, // Reserved additional info = 29
1340 { {(uint8_t[]){0x1e}, 1}, QCBOR_ERR_UNSUPPORTED }, // Reserved additional info = 30
1341 { {(uint8_t[]){0x1f}, 1}, QCBOR_ERR_UNSUPPORTED }, // Indefinite length integer
1342 { {(uint8_t[]){0x3c}, 1}, QCBOR_ERR_UNSUPPORTED }, // 1 byte integer missing the byte
1343 { {(uint8_t[]){0x3d}, 1}, QCBOR_ERR_UNSUPPORTED }, // 1 byte integer missing the byte
1344 { {(uint8_t[]){0x3e}, 1}, QCBOR_ERR_UNSUPPORTED }, // 1 byte integer missing the byte
1345 { {(uint8_t[]){0x3f}, 1}, QCBOR_ERR_UNSUPPORTED }, // Indefinite length negative integer
1346 { {(uint8_t[]){0x41}, 1}, QCBOR_ERR_HIT_END }, // Short byte string
1347 { {(uint8_t[]){0x5c}, 1}, QCBOR_ERR_UNSUPPORTED }, // Reserved additional info = 28
1348 { {(uint8_t[]){0x5f}, 1}, QCBOR_ERR_UNSUPPORTED }, // Indefinite length byte string
1349 { {(uint8_t[]){0x61}, 1}, QCBOR_ERR_HIT_END }, // Short UTF-8 string
1350 { {(uint8_t[]){0x7c}, 1}, QCBOR_ERR_UNSUPPORTED }, // Reserved additional info = 28
1351 { {(uint8_t[]){0x7f}, 1}, QCBOR_ERR_UNSUPPORTED }, // Indefinite length UTF-8 string
1352 { {(uint8_t[]){0xff}, 1}, QCBOR_ERR_UNSUPPORTED } , // break
1353 { {(uint8_t[]){0xf8, 0x00}, 2}, QCBOR_ERR_INVALID_CBOR }, // An invalid encoding of a simple type
1354 { {(uint8_t[]){0xf8, 0x1f}, 2}, QCBOR_ERR_INVALID_CBOR }, // An invalid encoding of a simple type
1355 { {(uint8_t[]){0xc0, 0x00}, 2}, QCBOR_ERR_BAD_OPT_TAG }, // Text-based date, with an integer
1356 { {(uint8_t[]){0xc1, 0x41, 0x33}, 3}, QCBOR_ERR_BAD_OPT_TAG }, // Epoch date, with an byte string
1357 { {(uint8_t[]){0xc1, 0xc0, 0x00}, 3}, QCBOR_ERR_BAD_OPT_TAG }, // tagged as both epoch and string dates
1358 { {(uint8_t[]){0xc2, 0x00}, 2}, QCBOR_ERR_BAD_OPT_TAG } // big num tagged an int, not a byte string
1359
1360};
1361
1362
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001363int FailureTests()
1364{
1365 int nResult = 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001366
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001367 struct FailInput *pFEnd = &Failures[0] + sizeof(Failures)/sizeof(struct FailInput);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001368
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001369 for(struct FailInput *pF = &Failures[0]; pF < pFEnd ;pF++) {
1370 QCBORDecodeContext DCtx;
1371 QCBORItem Item;
1372 int nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001373
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001374 QCBORDecode_Init(&DCtx, pF->Input, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001375
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001376 while(1) {
1377 nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
1378 if(QCBOR_ERR_HIT_END == nCBORError) {
1379 break;
1380 }
1381 if(nCBORError != pF->nError) {
1382 nResult = 1;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001383 break;
1384 }
1385 }
1386 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001387
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001388 {
1389 QCBORDecodeContext DCtx;
1390 QCBORItem Item;
1391 int nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001392
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07001393 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSimpleValues), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001394
1395 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1396 return nCBORError;
1397 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
1398 Item.val.uCount != 10)
1399 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001400
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001401 DCtx.InBuf.magic = 0; // Corrupt the UsefulInputBuf
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001402
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001403 nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
1404 if(nCBORError != QCBOR_ERR_HIT_END)
1405 return -1;
1406 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001407
1408
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001409 return nResult;
1410}
1411
1412
Laurence Lundbladea2e29072018-12-30 09:20:06 -08001413/* Try all 256 values of the byte at nLen including recursing for
1414 each of the values to try values at nLen+1 ... up to nLenMax
1415 */
1416static void ComprehensiveInputRecurser(uint8_t *pBuf, int nLen, int nLenMax)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001417{
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001418 if(nLen >= nLenMax) {
1419 return;
1420 }
Laurence Lundbladea2e29072018-12-30 09:20:06 -08001421
1422 for(int inputByte = 0; inputByte < 256; inputByte++) {
1423 // Set up the input
1424 pBuf[nLen] = inputByte;
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08001425 const UsefulBufC Input = {pBuf, nLen+1};
Laurence Lundbladea2e29072018-12-30 09:20:06 -08001426
1427 // Get ready to parse
1428 QCBORDecodeContext DCtx;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001429 QCBORDecode_Init(&DCtx, Input, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001430
Laurence Lundbladea2e29072018-12-30 09:20:06 -08001431 // Parse by getting the next item until an error occurs
1432 // Just about every possible decoder error can occur here
1433 // The goal of this test is not to check for the correct
1434 // error since that is not really possible. It is to
1435 // see that there is no crash on hostile input.
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001436 while(1) {
Laurence Lundbladea2e29072018-12-30 09:20:06 -08001437 QCBORItem Item;
1438 QCBORError nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001439 if(nCBORError != QCBOR_SUCCESS) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001440 break;
1441 }
1442 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001443
Laurence Lundbladea2e29072018-12-30 09:20:06 -08001444 ComprehensiveInputRecurser(pBuf, nLen+1, nLenMax);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001445 }
1446}
1447
1448
1449/*
Laurence Lundbladea2e29072018-12-30 09:20:06 -08001450 Public function for initialization. See header qcbor.h
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001451 */
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001452int ComprehensiveInputTest()
1453{
Laurence Lundbladea2e29072018-12-30 09:20:06 -08001454 // Size 2 tests 64K inputs and runs quickly
1455 uint8_t pBuf[2];
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001456
Laurence Lundbladea2e29072018-12-30 09:20:06 -08001457 ComprehensiveInputRecurser(pBuf, 0, sizeof(pBuf));
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001458
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001459 return 0;
1460}
1461
Laurence Lundbladea2e29072018-12-30 09:20:06 -08001462
1463/*
1464 Public function for initialization. See header qcbor.h
1465 */
1466int BigComprehensiveInputTest()
1467{
1468 // size 3 tests 16 million inputs and runs OK
1469 // in seconds on fast machines. Size 4 takes
1470 // 10+ minutes and 5 half a day on fast
1471 // machines. This test is kept separate from
1472 // the others so as to no slow down the use
1473 // of them as a very frequent regression.
1474 uint8_t pBuf[3]; //
1475
1476 ComprehensiveInputRecurser(pBuf, 0, sizeof(pBuf));
1477
1478 return 0;
1479}
1480
1481
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07001482static uint8_t spDateTestInput[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001483 0xc0, // tag for string date
1484 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001485
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001486 0xc1, // tag for epoch date
1487 0x1a, 0x53, 0x72, 0x4E, 0x00, // Epoch date 1400000000; Tue, 13 May 2014 16:53:20 GMT
1488
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001489 // CBOR_TAG_B64
1490 0xc1, 0xcf, 0xd8, 0x22, // 0xee, // Epoch date with extra tags TODO: fix this test
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001491 0x1a, 0x53, 0x72, 0x4E, 0x01,
1492
1493 0xc1, // tag for epoch date
1494 0x1b, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, // Too large integer
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001495
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001496 0xc1, // tag for epoch date
1497 0xfa, 0x3f, 0x8c, 0xcc, 0xcd, // double with value 1.1
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001498
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001499 0xc1, // tag for epoch date
1500 0xfa, 0x7f, 0x7f, 0xff, 0xff // 3.4028234663852886e+38 too large
1501
1502};
1503
1504
1505// have to check float expected only to within an epsilon
1506int CHECK_EXPECTED_DOUBLE(double val, double expected) {
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001507
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001508 double diff = val - expected;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001509
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001510 diff = fabs(diff);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001511
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001512 return diff > 0.0000001;
1513}
1514
1515
1516int DateParseTest()
1517{
1518 QCBORDecodeContext DCtx;
1519 QCBORItem Item;
1520 int nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001521
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07001522 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spDateTestInput), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001523
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001524 const uint64_t uTags[] = {15};
1525 QCBORTagListIn TagList = {1, uTags};
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001526
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001527 QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TagList);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001528
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001529 // String date
1530 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1531 return -1;
1532 if(Item.uDataType != QCBOR_TYPE_DATE_STRING ||
Laurence Lundblade9e3651c2018-10-10 11:49:55 +08001533 UsefulBuf_Compare(Item.val.dateString, UsefulBuf_FromSZ("1985-04-12"))){
Laurence Lundblade67bd5512018-11-02 21:44:06 +07001534 return -2;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001535 }
1536
1537 // Epoch date
1538 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade67bd5512018-11-02 21:44:06 +07001539 return -3;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001540 if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH ||
1541 Item.val.epochDate.nSeconds != 1400000000 ||
1542 Item.val.epochDate.fSecondsFraction != 0 ) {
Laurence Lundblade67bd5512018-11-02 21:44:06 +07001543 return -4;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001544 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001545
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001546 // Epoch date with extra CBOR_TAG_B64 tag that doesn't really mean anything
1547 // but want to be sure extra tag doesn't cause a problem
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001548 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade67bd5512018-11-02 21:44:06 +07001549 return -5;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001550 if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH ||
1551 Item.val.epochDate.nSeconds != 1400000001 ||
1552 Item.val.epochDate.fSecondsFraction != 0 ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001553 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_B64)) {
Laurence Lundblade67bd5512018-11-02 21:44:06 +07001554 return -6;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001555 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001556
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001557 // Epoch date that is too large for our representation
1558 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_DATE_OVERFLOW) {
Laurence Lundblade67bd5512018-11-02 21:44:06 +07001559 return -7;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001560 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001561
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001562 // Epoch date in float format with fractional seconds
1563 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade67bd5512018-11-02 21:44:06 +07001564 return -8;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001565 if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH ||
1566 Item.val.epochDate.nSeconds != 1 ||
1567 CHECK_EXPECTED_DOUBLE(Item.val.epochDate.fSecondsFraction, 0.1 )) {
Laurence Lundblade67bd5512018-11-02 21:44:06 +07001568 return -9;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001569 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001570
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001571 // Epoch date float that is too large for our representation
1572 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_DATE_OVERFLOW) {
Laurence Lundblade67bd5512018-11-02 21:44:06 +07001573 return -10;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001574 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001575
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001576 // TODO: could use a few more tests with float, double, and half precsion and negative (but coverage is still pretty good)
1577
1578 return 0;
1579}
1580
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001581// Really simple basic input for tagging test
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07001582static uint8_t spOptTestInput[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001583 0xd9, 0xd9, 0xf7, // CBOR magic number
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001584 0x81, // Array of one
1585 0xd8, 0x04, // non-preferred serialization of tag 4
1586 0x82, 0x01, 0x03}; // fraction 1/3
1587
1588static uint8_t spEncodedLargeTag[] = {0xdb, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x80};
1589
1590// 0x9192939495969798, 0x88, 0x01, 0x04
1591static uint8_t spLotsOfTags[] = {0xdb, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0xd8, 0x88, 0xc5, 0xc4, 0x80};
1592
1593/*
1594 The cbor.me parse of this.
1595 55799(55799(55799({6(7(-23)): 5859837686836516696(7({7(-20): 11({17(-18): 17(17(17("Organization"))),
1596 9(-17): 773("SSG"), -15: 4(5(6(7(8(9(10(11(12(13(14(15("Confusion")))))))))))), 17(-16): 17("San Diego"),
1597 17(-14): 17("US")}), 23(-19): 19({-11: 9({-9: -7}),
1598 90599561(90599561(90599561(-10))): 12(h'0102030405060708090A')})})),
1599 16(-22): 23({11(8(7(-5))): 8(-3)})})))
1600 */
1601static uint8_t spCSRWithTags[] = {
1602 0xd9, 0xd9, 0xf7, 0xd9, 0xd9, 0xf7, 0xd9, 0xd9, 0xf7, 0xa2,
1603 0xc6, 0xc7, 0x36,
1604 0xdb, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0xc7, 0xa2,
1605 0xda, 0x00, 0x00, 0x00, 0x07, 0x33,
1606 0xcb, 0xa5,
1607 0xd1, 0x31,
1608 0xd1, 0xd1, 0xd1, 0x6c,
1609 0x4f, 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74, 0x69, 0x6f, 0x6e,
1610 0xc9, 0x30,
1611 0xd9, 0x03, 0x05, 0x63,
1612 0x53, 0x53, 0x47,
1613 0x2e,
1614 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf, 0x69,
1615 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73, 0x69, 0x6f, 0x6e,
1616 0xd1, 0x2f,
1617 0xd1, 0x69,
1618 0x53, 0x61, 0x6e, 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f,
1619 0xd1, 0x2d,
1620 0xd1, 0x62,
1621 0x55, 0x53,
1622 0xd7, 0x32,
1623 0xd3, 0xa2,
1624 0x2a,
1625 0xc9, 0xa1,
1626 0x28,
1627 0x26,
1628 0xda, 0x05, 0x66, 0x70, 0x89, 0xda, 0x05, 0x66, 0x70, 0x89, 0xda, 0x05, 0x66, 0x70, 0x89, 0x29,
1629 0xcc, 0x4a,
1630 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,0x07, 0x08, 0x09, 0x0a,
1631 0xd0, 0x35,
1632 0xd7, 0xa1,
1633 0xcb, 0xc8, 0xc7, 0x24,
1634 0xc8, 0x22};
1635
1636static int CheckCSRMaps(QCBORDecodeContext *pDC);
1637
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001638
1639int OptTagParseTest()
1640{
1641 QCBORDecodeContext DCtx;
1642 QCBORItem Item;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001643
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07001644 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spOptTestInput), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001645
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001646 //-------------------------
1647 // This text matches the magic number tag and the fraction tag
1648 if(QCBORDecode_GetNext(&DCtx, &Item)) {
1649 return -2;
1650 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001651 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001652 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC)) {
1653 return -3;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001654 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001655
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001656 if(QCBORDecode_GetNext(&DCtx, &Item)) {
1657 return -4;
1658 }
1659 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
1660 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_FRACTION) ||
1661 Item.val.uCount != 2) {
1662 return -5;
1663 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001664
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001665 // --------------------------------
1666 // This test decodes the very large tag, but it is not in
1667 // any list so it is ignored.
1668 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEncodedLargeTag), QCBOR_DECODE_MODE_NORMAL);
1669 if(QCBORDecode_GetNext(&DCtx, &Item)) {
1670 return -6;
1671 }
1672 if(Item.uTagBits) {
1673 return -7;
1674 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001675
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001676 // ----------------------------------
1677 // This test sets up a caller-config list that includes the very large tage and then matches it.
1678 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEncodedLargeTag), QCBOR_DECODE_MODE_NORMAL);
1679 const uint64_t puList[] = {0x9192939495969798, 257};
1680 const QCBORTagListIn TL = {2, puList};
1681 QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001682
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001683 if(QCBORDecode_GetNext(&DCtx, &Item)) {
1684 return -8;
1685 }
1686 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
1687 !QCBORDecode_IsTagged(&DCtx, &Item, 0x9192939495969798) ||
1688 QCBORDecode_IsTagged(&DCtx, &Item, 257) ||
1689 QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_BIGFLOAT) ||
1690 Item.val.uCount != 0) {
1691 return -9;
1692 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001693
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001694 //------------------------
1695 // This test sets up a caller-configured list, and looks up something not in it
1696 const uint64_t puLongList[17] = {1,2,1};
1697 const QCBORTagListIn TLLong = {17, puLongList};
1698 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEncodedLargeTag), QCBOR_DECODE_MODE_NORMAL);
1699 QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TLLong);
1700 if(QCBORDecode_GetNext(&DCtx, &Item)) {
1701 return -11;
1702 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001703
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001704 // -----------------------
1705 // This tests retrievel of the full tag list
1706 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spLotsOfTags), QCBOR_DECODE_MODE_NORMAL);
1707 uint64_t puTags[16];
1708 QCBORTagListOut Out = {0, 4, puTags};
1709 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
1710 return -12;
1711 }
1712 if(puTags[0] != 0x9192939495969798 ||
1713 puTags[1] != 0x88 ||
1714 puTags[2] != 0x05 ||
1715 puTags[3] != 0x04) {
1716 return -13;
1717 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001718
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001719 // ----------------------
1720 // This text if too small of an out list
1721 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spLotsOfTags), QCBOR_DECODE_MODE_NORMAL);
1722 QCBORTagListOut OutSmall = {0, 3, puTags};
1723 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &OutSmall) != QCBOR_ERR_TOO_MANY_TAGS) {
1724 return -14;
1725 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001726
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001727 // ---------------
1728 // Parse a version of the "CSR" that has had a ton of tags randomly inserted
1729 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRWithTags), QCBOR_DECODE_MODE_NORMAL);
1730 int n = CheckCSRMaps(&DCtx);
1731 if(n) {
1732 return n-2000;
1733 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001734
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001735 Out = (QCBORTagListOut){0,16, puTags};
1736 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRWithTags), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001737
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001738 const uint64_t puTagList[] = {773, 1, 90599561};
1739 const QCBORTagListIn TagList = {3, puTagList};
1740 QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TagList);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001741
1742
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001743 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
1744 return -100;
1745 }
1746 if(Item.uDataType != QCBOR_TYPE_MAP ||
1747 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC) ||
1748 QCBORDecode_IsTagged(&DCtx, &Item, 90599561) ||
1749 QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_DATE_EPOCH) ||
1750 Item.val.uCount != 2 ||
1751 puTags[0] != CBOR_TAG_CBOR_MAGIC ||
1752 puTags[1] != CBOR_TAG_CBOR_MAGIC ||
1753 puTags[2] != CBOR_TAG_CBOR_MAGIC ||
1754 Out.uNumUsed != 3) {
1755 return -101;
1756 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001757
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001758 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
1759 return -102;
1760 }
1761 if(Item.uDataType != QCBOR_TYPE_MAP ||
1762 QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC) ||
1763 QCBORDecode_IsTagged(&DCtx, &Item, 6) ||
1764 QCBORDecode_IsTagged(&DCtx, &Item, 7) || // item is tagged 7, but 7 is not configured to be recognized
1765 Item.val.uCount != 2 ||
1766 puTags[0] != 5859837686836516696 ||
1767 puTags[1] != 7 ||
1768 Out.uNumUsed != 2) {
1769 return -103;
1770 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001771
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001772 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
1773 return -104;
1774 }
1775 if(Item.uDataType != QCBOR_TYPE_MAP ||
1776 Item.uTagBits ||
1777 Item.val.uCount != 5 ||
1778 puTags[0] != 0x0b ||
1779 Out.uNumUsed != 1) {
1780 return -105;
1781 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001782
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001783 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
1784 return -106;
1785 }
1786 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1787 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_COSE_MAC0) ||
1788 Item.val.string.len != 12 ||
1789 puTags[0] != CBOR_TAG_COSE_MAC0 ||
1790 puTags[1] != CBOR_TAG_COSE_MAC0 ||
1791 puTags[2] != CBOR_TAG_COSE_MAC0 ||
1792 Out.uNumUsed != 3) {
1793 return -105;
1794 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001795
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001796 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
1797 return -107;
1798 }
1799 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1800 !QCBORDecode_IsTagged(&DCtx, &Item, 773) ||
1801 Item.val.string.len != 3 ||
1802 puTags[0] != 773 ||
1803 Out.uNumUsed != 1) {
1804 return -108;
1805 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001806
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001807 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
1808 return -109;
1809 }
1810 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1811 !QCBORDecode_IsTagged(&DCtx, &Item, 4) ||
1812 Item.val.string.len != 9 ||
1813 puTags[0] != 4 ||
1814 puTags[11] != 0x0f ||
1815 Out.uNumUsed != 12) {
1816 return -110;
1817 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001818
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001819 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
1820 return -111;
1821 }
1822 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1823 !QCBORDecode_IsTagged(&DCtx, &Item, 17) ||
1824 Item.val.string.len != 9 ||
1825 puTags[0] != 17 ||
1826 Out.uNumUsed != 1) {
1827 return -112;
1828 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001829
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001830 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
1831 return -111;
1832 }
1833 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1834 !QCBORDecode_IsTagged(&DCtx, &Item, 17) ||
1835 Item.val.string.len != 2 ||
1836 puTags[0] != 17 ||
1837 Out.uNumUsed != 1) {
1838 return -112;
1839 }
1840
1841 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
1842 return -113;
1843 }
1844 if(Item.uDataType != QCBOR_TYPE_MAP ||
1845 QCBORDecode_IsTagged(&DCtx, &Item, 19) ||
1846 Item.val.uCount != 2 ||
1847 puTags[0] != 19 ||
1848 Out.uNumUsed != 1) {
1849 return -114;
1850 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001851
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001852 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
1853 return -115;
1854 }
1855 if(Item.uDataType != QCBOR_TYPE_MAP ||
1856 QCBORDecode_IsTagged(&DCtx, &Item, 9) ||
1857 Item.uTagBits ||
1858 Item.val.uCount != 1 ||
1859 puTags[0] != 9 ||
1860 Out.uNumUsed != 1) {
1861 return -116;
1862 }
1863
1864 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
1865 return -116;
1866 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001867 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001868 Item.val.int64 != -7 ||
1869 Item.uTagBits ||
1870 Out.uNumUsed != 0) {
1871 return -117;
1872 }
1873
1874 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
1875 return -118;
1876 }
1877 if(Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
1878 Item.val.string.len != 10 ||
1879 Item.uTagBits ||
1880 puTags[0] != 12 ||
1881 Out.uNumUsed != 1) {
1882 return -119;
1883 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001884
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001885 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
1886 return -120;
1887 }
1888 if(Item.uDataType != QCBOR_TYPE_MAP ||
1889 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_ENC_AS_B16) ||
1890 Item.val.uCount != 1 ||
1891 puTags[0] != 0x17 ||
1892 Out.uNumUsed != 1) {
1893 return -121;
1894 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001895
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001896 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
1897 return -122;
1898 }
1899 if(Item.uDataType != QCBOR_TYPE_INT64 ||
1900 QCBORDecode_IsTagged(&DCtx, &Item, 8) ||
1901 Item.val.int64 != -3 ||
1902 puTags[0] != 8 ||
1903 Out.uNumUsed != 1) {
1904 return -123;
1905 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001906
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001907 if(QCBORDecode_Finish(&DCtx)) {
1908 return -124;
1909 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001910
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001911 return 0;
1912}
1913
1914
1915
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001916
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07001917static uint8_t spBigNumInput[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001918 0x83,
1919 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1920 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1921 0xA4,
1922 0x63, 0x42, 0x4E, 0x2B,
1923 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1924 0x18, 0x40,
1925 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1926 0x63, 0x42, 0x4E, 0x2D,
1927 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1928 0x38, 0x3F,
1929 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
1930
1931
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07001932static uint8_t spBigNum[] = {0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001933
1934
1935int BignumParseTest()
1936{
1937 QCBORDecodeContext DCtx;
1938 QCBORItem Item;
1939 int nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001940
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07001941 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNumInput), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001942
1943
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001944 //
1945 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1946 return -1;
1947 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
1948 return -1;
1949 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001950
1951 //
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001952 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1953 return -1;
1954 if(Item.uDataType != QCBOR_TYPE_POSBIGNUM ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07001955 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001956 return -1;
1957 }
1958
1959 //
1960 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1961 return -1;
1962 if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07001963 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001964 return -1;
1965 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001966
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001967 //
1968 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1969 return -1;
1970 if(Item.uDataType != QCBOR_TYPE_MAP) {
1971 return -1;
1972 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001973
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001974 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1975 return -1;
1976 if(Item.uDataType != QCBOR_TYPE_POSBIGNUM ||
1977 Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07001978 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001979 return -1;
1980 }
1981
1982 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1983 return -1;
1984 if(Item.uDataType != QCBOR_TYPE_POSBIGNUM ||
1985 Item.uLabelType != QCBOR_TYPE_INT64 ||
1986 Item.label.int64 != 64 ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07001987 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001988 return -1;
1989 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001990
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001991 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1992 return -1;
1993 if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM ||
1994 Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07001995 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001996 return -1;
1997 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001998
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001999 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
2000 return -1;
2001 if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM ||
2002 Item.uLabelType != QCBOR_TYPE_INT64 ||
2003 Item.label.int64 != -64 ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002004 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002005 return -1;
2006 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002007
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002008 return 0;
2009}
2010
2011
2012
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302013static 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 +08002014{
2015 QCBORItem Item;
2016 int nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002017
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002018 if((nCBORError = QCBORDecode_GetNext(pCtx, &Item))) return -1;
2019 if(Item.uDataType != uDataType) return -1;
2020 if(uNestingLevel > 0) {
2021 if(Item.uLabelType != QCBOR_TYPE_INT64 && Item.uLabelType != QCBOR_TYPE_UINT64) return -1;
2022 if(Item.uLabelType == QCBOR_TYPE_INT64) {
2023 if(Item.label.int64 != nLabel) return -1;
2024 } else {
Laurence Lundblade570fab52018-10-13 18:28:27 +08002025 if(Item.label.uint64 != (uint64_t)nLabel) return -1;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002026 }
2027 }
2028 if(Item.uNestingLevel != uNestingLevel) return -1;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302029 if(Item.uNextNestLevel != uNextNest) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002030
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002031 if(pItem) {
2032 *pItem = Item;
2033 }
2034 return 0;
2035}
2036
2037
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002038// Same code checks definite and indefinite length versions of the map
2039static int CheckCSRMaps(QCBORDecodeContext *pDC)
2040{
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302041 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 0, 1, 0, NULL)) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002042
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302043 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 1, 2, -23, NULL)) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002044
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302045 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 2, 3, -20, NULL)) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002046
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302047 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -18, NULL)) return -1;
2048 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -17, NULL)) return -1;
2049 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -15, NULL)) return -1;
2050 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -16, NULL)) return -1;
2051 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 2, -14, NULL)) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002052
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302053 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 2, 3, -19, NULL)) return -1;
2054 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 3, 4, -11, NULL)) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002055
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302056 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_INT64, 4, 3, -9, NULL)) return -1;
2057 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_BYTE_STRING, 3, 1, -10, NULL)) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002058
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302059 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 1, 2, -22, NULL)) return -1;
2060 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_INT64, 2, 0, -5, NULL)) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002061
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002062 if(QCBORDecode_Finish(pDC)) return -2;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002063
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002064 return 0;
2065}
2066
2067
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002068/*
2069// cbor.me decoded output
2070{
2071 -23: {
2072 -20: {
2073 -18: "Organization",
2074 -17: "SSG",
2075 -15: "Confusion",
2076 -16: "San Diego",
2077 -14: "US"
2078 },
2079 -19: {
2080 -11: {
2081 -9: -7
2082 },
2083 -10: '\u0001\u0002\u0003\u0004\u0005\u0006\a\b\t\n'
2084 }
2085 },
2086 -22: {
2087 -5: -3
2088 }
2089}
2090 */
2091
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002092
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002093static uint8_t spCSRInput[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002094 0xa2, 0x36, 0xa2, 0x33, 0xa5, 0x31, 0x6c, 0x4f,
2095 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74,
2096 0x69, 0x6f, 0x6e, 0x30, 0x63, 0x53, 0x53, 0x47,
2097 0x2e, 0x69, 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73,
2098 0x69, 0x6f, 0x6e, 0x2f, 0x69, 0x53, 0x61, 0x6e,
2099 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f, 0x2d, 0x62,
2100 0x55, 0x53, 0x32, 0xa2, 0x2a, 0xa1, 0x28, 0x26,
2101 0x29, 0x4a, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
2102 0x07, 0x08, 0x09, 0x0a, 0x35, 0xa1, 0x24, 0x22};
2103
2104int NestedMapTest()
2105{
2106 QCBORDecodeContext DCtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002107
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002108 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002109
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002110 return CheckCSRMaps(&DCtx);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002111}
2112
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08002113
2114
2115int StringDecoderModeFailTest()
2116{
2117 QCBORDecodeContext DCtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002118
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08002119 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput), QCBOR_DECODE_MODE_MAP_STRINGS_ONLY);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002120
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08002121 QCBORItem Item;
2122 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002123
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08002124 if(QCBORDecode_GetNext(&DCtx, &Item)) {
2125 return -1;
2126 }
2127 if(Item.uDataType != QCBOR_TYPE_MAP) {
2128 return -2;
2129 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002130
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08002131 nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
2132 if(nCBORError != QCBOR_ERR_MAP_LABEL_TYPE) {
2133 return -3;
2134 }
2135
2136 return 0;
2137}
2138
2139
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002140// Same map as above, but using indefinite lengths
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002141static uint8_t spCSRInputIndefLen[] = {
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002142 0xbf, 0x36, 0xbf, 0x33, 0xbf, 0x31, 0x6c, 0x4f,
2143 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74,
2144 0x69, 0x6f, 0x6e, 0x30, 0x63, 0x53, 0x53, 0x47,
2145 0x2e, 0x69, 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73,
2146 0x69, 0x6f, 0x6e, 0x2f, 0x69, 0x53, 0x61, 0x6e,
2147 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f, 0x2d, 0x62,
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002148 0x55, 0x53, 0xff, 0x32, 0xbf, 0x2a, 0xbf, 0x28,
2149 0x26, 0xff, 0x29, 0x4a, 0x01, 0x02, 0x03, 0x04,
2150 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0xff, 0xff,
2151 0x35, 0xbf, 0x24, 0x22, 0xff, 0xff};
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002152
2153int NestedMapTestIndefLen()
2154{
2155 QCBORDecodeContext DCtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002156
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002157 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInputIndefLen), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002158
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002159 return CheckCSRMaps(&DCtx);
2160}
2161
2162
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002163
Laurence Lundblade17ede402018-10-13 11:43:07 +08002164static UsefulBufC make_nested_indefinite_arrays(int n, UsefulBuf Storage)
2165{
2166 UsefulOutBuf UOB;
2167 UsefulOutBuf_Init(&UOB, Storage);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002168
Laurence Lundblade17ede402018-10-13 11:43:07 +08002169 int i;
2170 for(i = 0; i < n; i++) {
2171 UsefulOutBuf_AppendByte(&UOB, 0x9f);
2172 }
2173
2174 for(i = 0; i < n; i++) {
2175 UsefulOutBuf_AppendByte(&UOB, 0xff);
2176 }
2177 return UsefulOutBuf_OutUBuf(&UOB);
2178}
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002179
2180
Laurence Lundblade17ede402018-10-13 11:43:07 +08002181static int parse_indeflen_nested(UsefulBufC Nested, int nNestLevel)
2182{
2183 QCBORDecodeContext DC;
2184 QCBORDecode_Init(&DC, Nested, 0);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002185
Laurence Lundblade17ede402018-10-13 11:43:07 +08002186 int j;
2187 for(j = 0; j < nNestLevel; j++) {
2188 QCBORItem Item;
2189 int nReturn = QCBORDecode_GetNext(&DC, &Item);
2190 if(j >= QCBOR_MAX_ARRAY_NESTING) {
2191 // Should be in error
2192 if(nReturn != QCBOR_ERR_ARRAY_NESTING_TOO_DEEP) {
2193 return -4;
2194 } else {
2195 return 0; // Decoding doesn't recover after an error
2196 }
2197 } else {
2198 // Should be no error
2199 if(nReturn) {
2200 return -9; // Should not have got an error
2201 }
2202 }
2203 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
2204 return -7;
2205 }
2206 }
2207 int nReturn = QCBORDecode_Finish(&DC);
2208 if(nReturn) {
2209 return -3;
2210 }
2211 return 0;
2212}
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002213
2214
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302215int IndefiniteLengthNestTest()
Laurence Lundblade17ede402018-10-13 11:43:07 +08002216{
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05302217 UsefulBuf_MAKE_STACK_UB(Storage, 50);
Laurence Lundblade17ede402018-10-13 11:43:07 +08002218 int i;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002219 for(i=1; i < QCBOR_MAX_ARRAY_NESTING+4; i++) {
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08002220 const UsefulBufC Nested = make_nested_indefinite_arrays(i, Storage);
Laurence Lundblade17ede402018-10-13 11:43:07 +08002221 int nReturn = parse_indeflen_nested(Nested, i);
2222 if(nReturn) {
2223 return nReturn;
2224 }
2225 }
2226 return 0;
2227}
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002228
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002229
Laurence Lundblade6de37062018-10-15 12:22:42 +05302230
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002231static const uint8_t spIndefiniteArray[] = {0x9f, 0x01, 0x82, 0x02, 0x03, 0xff}; // [1, [2, 3]]
2232static const uint8_t spIndefiniteArrayBad1[] = {0x9f}; // No closing break
2233static const uint8_t spIndefiniteArrayBad2[] = {0x9f, 0x9f, 0x02, 0xff}; // Not enough closing breaks
2234static const uint8_t spIndefiniteArrayBad3[] = {0x9f, 0x02, 0xff, 0xff}; // Too many closing breaks
2235static const uint8_t spIndefiniteArrayBad4[] = {0x81, 0x9f}; // Unclosed indeflen inside def len
2236static const uint8_t spIndefiniteArrayBad5[] = {0x9f, 0xd1, 0xff}; // confused tag
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002237
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302238int IndefiniteLengthArrayMapTest()
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002239{
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002240 int nResult;
2241 // --- first test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002242 UsefulBufC IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArray);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002243
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002244 // Decode it and see if it is OK
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05302245 UsefulBuf_MAKE_STACK_UB(MemPool, 150);
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002246 QCBORDecodeContext DC;
2247 QCBORItem Item;
2248 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002249
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002250 QCBORDecode_SetMemPool(&DC, MemPool, false);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002251
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002252 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05302253
2254 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
2255 Item.uNestingLevel != 0 ||
2256 Item.uNextNestLevel != 1) {
2257 return -111;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002258 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002259
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002260 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05302261 if(Item.uDataType != QCBOR_TYPE_INT64 ||
2262 Item.uNestingLevel != 1 ||
2263 Item.uNextNestLevel != 1) {
2264 return -2;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002265 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002266
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002267 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05302268 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
2269 Item.uNestingLevel != 1 ||
2270 Item.uNextNestLevel != 2) {
2271 return -3;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002272 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002273
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002274 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade12b495d2018-12-17 11:15:54 -08002275 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade6de37062018-10-15 12:22:42 +05302276 Item.uNestingLevel != 2 ||
2277 Item.uNextNestLevel != 2) {
2278 return -4;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002279 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002280
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002281 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade12b495d2018-12-17 11:15:54 -08002282 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade6de37062018-10-15 12:22:42 +05302283 Item.uNestingLevel != 2 ||
2284 Item.uNextNestLevel != 0) {
2285 return -5;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002286 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002287
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002288 if(QCBORDecode_Finish(&DC)) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05302289 return -6;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002290 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002291
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002292 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002293 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad1);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002294
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002295 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002296
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002297 QCBORDecode_SetMemPool(&DC, MemPool, false);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002298
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002299 nResult = QCBORDecode_GetNext(&DC, &Item);
2300 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05302301 return -7;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002302 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002303
Laurence Lundblade570fab52018-10-13 18:28:27 +08002304 nResult = QCBORDecode_Finish(&DC);
Laurence Lundblade6de37062018-10-15 12:22:42 +05302305 if(nResult != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
2306 return -8;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002307 }
2308
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002309
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002310 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002311 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad2);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002312
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002313 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002314
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002315 QCBORDecode_SetMemPool(&DC, MemPool, false);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002316
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002317 nResult = QCBORDecode_GetNext(&DC, &Item);
2318 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05302319 return -9;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002320 }
2321
2322 nResult = QCBORDecode_GetNext(&DC, &Item);
2323 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05302324 return -10;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002325 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002326
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002327 nResult = QCBORDecode_GetNext(&DC, &Item);
2328 if(nResult || Item.uDataType != QCBOR_TYPE_INT64) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05302329 return -11;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002330 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002331
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002332 nResult = QCBORDecode_Finish(&DC);
Laurence Lundblade6de37062018-10-15 12:22:42 +05302333 if(nResult != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
2334 return -12;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002335 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002336
2337
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002338 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002339 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad3);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002340
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002341 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002342
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002343 QCBORDecode_SetMemPool(&DC, MemPool, false);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002344
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002345 nResult = QCBORDecode_GetNext(&DC, &Item);
2346 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05302347 return -13;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002348 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002349
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002350 nResult = QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05302351 if(nResult != QCBOR_ERR_BAD_BREAK) {
2352 return -14;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002353 }
Laurence Lundblade6de37062018-10-15 12:22:42 +05302354
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002355
Laurence Lundblade570fab52018-10-13 18:28:27 +08002356 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002357 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad4);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002358
Laurence Lundblade570fab52018-10-13 18:28:27 +08002359 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002360
Laurence Lundblade570fab52018-10-13 18:28:27 +08002361 QCBORDecode_SetMemPool(&DC, MemPool, false);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002362
Laurence Lundblade570fab52018-10-13 18:28:27 +08002363 nResult = QCBORDecode_GetNext(&DC, &Item);
2364 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05302365 return -15;
Laurence Lundblade570fab52018-10-13 18:28:27 +08002366 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002367
Laurence Lundblade570fab52018-10-13 18:28:27 +08002368 nResult = QCBORDecode_GetNext(&DC, &Item);
2369 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05302370 return -16;
Laurence Lundblade570fab52018-10-13 18:28:27 +08002371 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002372
Laurence Lundblade570fab52018-10-13 18:28:27 +08002373 nResult = QCBORDecode_Finish(&DC);
Laurence Lundblade6de37062018-10-15 12:22:42 +05302374 if(nResult != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
2375 return -17;
Laurence Lundblade570fab52018-10-13 18:28:27 +08002376 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002377
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302378 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002379 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad5);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002380
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302381 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002382
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302383 QCBORDecode_SetMemPool(&DC, MemPool, false);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002384
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302385 nResult = QCBORDecode_GetNext(&DC, &Item);
2386 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05302387 return -18;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302388 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002389
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302390 nResult = QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05302391 if(nResult != QCBOR_ERR_BAD_BREAK) {
2392 return -19;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302393 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002394
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002395 return 0;
2396}
2397
Laurence Lundblade17ede402018-10-13 11:43:07 +08002398
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002399static const uint8_t spIndefiniteLenString[] = {
Laurence Lundblade17ede402018-10-13 11:43:07 +08002400 0x81, // Array of length one
2401 0x7f, // text string marked with indefinite length
2402 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment
2403 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment
2404 0xff // ending break
2405};
2406
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002407static const uint8_t spIndefiniteLenStringBad2[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302408 0x81, // Array of length one
2409 0x7f, // text string marked with indefinite length
2410 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment
2411 0x44, 0x6d, 0x69, 0x6e, 0x67, // second segment of wrong type
2412 0xff // ending break
2413};
2414
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002415static const uint8_t spIndefiniteLenStringBad3[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302416 0x81, // Array of length one
2417 0x7f, // text string marked with indefinite length
2418 0x01, 0x02, // Not a string
2419 0xff // ending break
2420};
2421
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002422static const uint8_t spIndefiniteLenStringBad4[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302423 0x81, // Array of length one
2424 0x7f, // text string marked with indefinite length
2425 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment
2426 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment
2427 // missing end of string
2428};
2429
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002430static const uint8_t spIndefiniteLenStringLabel[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302431 0xa1, // Array of length one
2432 0x7f, // text string marked with indefinite length
2433 0x65, 0x73, 0x74, 0x72, 0x75, 0x75, // first segment
2434 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment
2435 0xff, // ending break
2436 0x01 // integer being labeled.
2437};
2438
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08002439static UsefulBufC MakeIndefiniteBigBstr(UsefulBuf Storage) // TODO: size this
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302440{
2441 UsefulOutBuf UOB;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002442
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302443 UsefulOutBuf_Init(&UOB, Storage);
2444 UsefulOutBuf_AppendByte(&UOB, 0x81);
2445 UsefulOutBuf_AppendByte(&UOB, 0x5f);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002446
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302447 int i = 0;
2448 for(int nChunkSize = 1; nChunkSize <= 128; nChunkSize *= 2) {
2449 UsefulOutBuf_AppendByte(&UOB, 0x58);
2450 UsefulOutBuf_AppendByte(&UOB, (uint8_t)nChunkSize);
2451 for(int j = 0; j < nChunkSize; j++ ) {
2452 UsefulOutBuf_AppendByte(&UOB, i);
2453 i++;
2454 }
2455 }
2456 UsefulOutBuf_AppendByte(&UOB, 0xff);
2457
2458 return UsefulOutBuf_OutUBuf(&UOB);
2459}
2460
2461static int CheckBigString(UsefulBufC BigString)
2462{
2463 if(BigString.len != 255) {
2464 return 1;
2465 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002466
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302467 for(uint8_t i = 0; i < 255; i++){
2468 if(((const uint8_t *)BigString.ptr)[i] != i) {
2469 return 1;
2470 }
2471 }
2472 return 0;
2473}
2474
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05302475
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302476int IndefiniteLengthStringTest()
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302477{
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302478 QCBORDecodeContext DC;
2479 QCBORItem Item;
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05302480 // big enough for MakeIndefiniteBigBstr() + MemPool overhead
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08002481 UsefulBuf_MAKE_STACK_UB(MemPool, 350);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002482
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302483 // --- Simple normal indefinite length string ------
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002484 UsefulBufC IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenString);
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302485 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002486
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05302487 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302488 return -1;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302489 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002490
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302491 if(QCBORDecode_GetNext(&DC, &Item)) {
2492 return -2;
2493 }
2494 if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.uDataAlloc) {
2495 return -3;
2496 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002497
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302498 if(QCBORDecode_GetNext(&DC, &Item)) {
2499 return -4;
2500 }
2501 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || !Item.uDataAlloc) {
2502 return -5;
2503 }
2504 if(QCBORDecode_Finish(&DC)) {
2505 return -6;
2506 }
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302507
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302508 // ----- types mismatch ---
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002509 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad2), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002510
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302511 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
2512 return -7;
2513 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002514
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302515 if(QCBORDecode_GetNext(&DC, &Item)) {
2516 return -8;
2517 }
2518 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
2519 return -9;
2520 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002521
Laurence Lundblade30816f22018-11-10 13:40:22 +07002522 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_INDEFINITE_STRING_CHUNK) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302523 return -10;
2524 }
2525
2526 // ----- not a string ---
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002527 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad3), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002528
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302529 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
2530 return -11;
2531 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002532
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302533 if(QCBORDecode_GetNext(&DC, &Item)) {
2534 return -12;
2535 }
2536 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
2537 return -13;
2538 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002539
Laurence Lundblade30816f22018-11-10 13:40:22 +07002540 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_INDEFINITE_STRING_CHUNK) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302541 return -14;
2542 }
2543
2544 // ----- no end -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002545 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad4), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002546
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302547 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
2548 return -15;
2549 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002550
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302551 if(QCBORDecode_GetNext(&DC, &Item)) {
2552 return -16;
2553 }
2554 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
2555 return -17;
2556 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002557
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302558 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_HIT_END) {
2559 return -18;
2560 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002561
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302562 // ------ Don't set a string allocator and see an error -----
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302563 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002564
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302565 QCBORDecode_GetNext(&DC, &Item);
2566 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302567 return -19;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302568 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002569
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302570 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_NO_STRING_ALLOCATOR) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302571 return -20;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302572 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002573
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302574 // ----- Mempool is way too small -----
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05302575 UsefulBuf_MAKE_STACK_UB(MemPoolTooSmall, 20); // 20 is too small no matter what
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302576
2577 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
2578 if(!QCBORDecode_SetMemPool(&DC, MemPoolTooSmall, false)) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302579 return -21;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302580 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002581
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302582 // ----- Mempool is way too small -----
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05302583 UsefulBuf_MAKE_STACK_UB(BigIndefBStrStorage, 290);
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08002584 const UsefulBufC BigIndefBStr = MakeIndefiniteBigBstr(BigIndefBStrStorage);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002585
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05302586 UsefulBuf_MAKE_STACK_UB(MemPoolSmall, 80); // 80 is big enough for MemPool overhead, but not BigIndefBStr
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002587
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302588 QCBORDecode_Init(&DC, BigIndefBStr, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302589 if(QCBORDecode_SetMemPool(&DC, MemPoolSmall, false)) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302590 return -22;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302591 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002592
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302593 QCBORDecode_GetNext(&DC, &Item);
2594 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302595 return -23;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302596 }
Laurence Lundblade30816f22018-11-10 13:40:22 +07002597 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_STRING_ALLOCATE) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302598 return -24;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302599 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002600
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302601 // ---- big bstr -----
2602 QCBORDecode_Init(&DC, BigIndefBStr, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002603
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302604 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
2605 return -25;
2606 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002607
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05302608 if(QCBORDecode_GetNext(&DC, &Item)) {
2609 return -26;
2610 }
2611 if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.uDataAlloc) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302612 return -26;
2613 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002614
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302615 if(QCBORDecode_GetNext(&DC, &Item)) {
2616 return -27;
2617 }
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05302618 if(Item.uDataType != QCBOR_TYPE_BYTE_STRING || !Item.uDataAlloc || Item.uNestingLevel != 1) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302619 return -28;
2620 }
2621 if(CheckBigString(Item.val.string)) {
2622 return -3;
2623 }
2624 if(QCBORDecode_Finish(&DC)) {
2625 return -29;
2626 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002627
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302628 // --- label is an indefinite length string ------
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002629 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringLabel), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002630
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302631 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
2632 return -30;
2633 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002634
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302635 QCBORDecode_GetNext(&DC, &Item);
2636 if(Item.uDataType != QCBOR_TYPE_MAP) {
2637 return -31;
2638 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002639
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302640 if(QCBORDecode_GetNext(&DC, &Item)){
2641 return -32;
2642 }
2643 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || Item.uDataType != QCBOR_TYPE_INT64 ||
2644 Item.uDataAlloc || !Item.uLabelAlloc ||
2645 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("struuming"))) {
2646 return -33;
2647 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002648
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302649 if(QCBORDecode_Finish(&DC)) {
2650 return -34;
2651 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002652
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002653 return 0;
2654}
2655
2656
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302657int AllocAllStringsTest()
2658{
2659 QCBORDecodeContext DC;
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08002660 QCBORError nCBORError;
2661
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002662
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302663 // First test, use the "CSRMap" as easy input and checking
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002664 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002665
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08002666 UsefulBuf_MAKE_STACK_UB(Pool, sizeof(spCSRInput) + QCBOR_DECODE_MIN_MEM_POOL_SIZE);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002667
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08002668 nCBORError = QCBORDecode_SetMemPool(&DC, Pool, 1); // Turn on copying.
2669 if(nCBORError) {
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302670 return -1;
2671 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002672
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08002673 if(CheckCSRMaps(&DC)) {
2674 return -2;
2675 }
2676
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302677 // Next parse, save pointers to a few strings, destroy original and see all is OK.
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08002678 UsefulBuf_MAKE_STACK_UB(CopyOfStorage, sizeof(pValidMapEncoded) + QCBOR_DECODE_MIN_MEM_POOL_SIZE);
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08002679 const UsefulBufC CopyOf = UsefulBuf_Copy(CopyOfStorage, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded));
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002680
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302681 QCBORDecode_Init(&DC, CopyOf, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08002682 UsefulBuf_Set(Pool, '/');
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302683 QCBORDecode_SetMemPool(&DC, Pool, 1); // Turn on copying.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002684
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302685 QCBORItem Item1, Item2, Item3, Item4;
2686 if((nCBORError = QCBORDecode_GetNext(&DC, &Item1)))
2687 return nCBORError;
2688 if(Item1.uDataType != QCBOR_TYPE_MAP ||
2689 Item1.val.uCount != 3)
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08002690 return -3;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302691 if((nCBORError = QCBORDecode_GetNext(&DC, &Item1)))
2692 return nCBORError;
2693 if((nCBORError = QCBORDecode_GetNext(&DC, &Item2)))
2694 return nCBORError;
2695 if((nCBORError = QCBORDecode_GetNext(&DC, &Item3)))
2696 return nCBORError;
2697 if((nCBORError = QCBORDecode_GetNext(&DC, &Item4)))
2698 return nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002699
Laurence Lundblade05ec57b2018-10-21 01:50:03 +05302700 UsefulBuf_Set(CopyOfStorage, '_');
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002701
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302702 if(Item1.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302703 Item1.uDataType != QCBOR_TYPE_INT64 ||
2704 Item1.val.int64 != 42 ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09002705 UsefulBuf_Compare(Item1.label.string, UsefulBuf_FromSZ("first integer"))) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08002706 return -4;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09002707 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002708
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302709
2710 if(Item2.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09002711 UsefulBuf_Compare(Item2.label.string, UsefulBuf_FromSZ("an array of two strings")) ||
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302712 Item2.uDataType != QCBOR_TYPE_ARRAY ||
2713 Item2.val.uCount != 2)
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08002714 return -5;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002715
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302716 if(Item3.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09002717 UsefulBuf_Compare(Item3.val.string, UsefulBuf_FromSZ("string1"))) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08002718 return -6;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09002719 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002720
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302721 if(Item4.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09002722 UsefulBuf_Compare(Item4.val.string, UsefulBuf_FromSZ("string2"))) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08002723 return -7;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09002724 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002725
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302726 // Next parse with a pool that is too small
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08002727 UsefulBuf_MAKE_STACK_UB(SmallPool, QCBOR_DECODE_MIN_MEM_POOL_SIZE + 1);
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05302728 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302729 QCBORDecode_SetMemPool(&DC, SmallPool, 1); // Turn on copying.
2730 if((nCBORError = QCBORDecode_GetNext(&DC, &Item1)))
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08002731 return -8;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302732 if(Item1.uDataType != QCBOR_TYPE_MAP ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09002733 Item1.val.uCount != 3) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08002734 return -9;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09002735 }
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302736 if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item1))){
2737 if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item2))) {
2738 if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item3))) {
2739 nCBORError = QCBORDecode_GetNext(&DC, &Item4);
2740 }
2741 }
2742 }
Laurence Lundblade30816f22018-11-10 13:40:22 +07002743 if(nCBORError != QCBOR_ERR_STRING_ALLOCATE) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08002744 return -10;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302745 }
2746
2747 return 0;
2748}
2749
Laurence Lundbladef6531662018-12-04 10:42:22 +09002750// Cheating declaration to get to the special test hook
2751size_t MemPoolTestHook_GetPoolSize(void *ctx);
2752
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302753
2754int MemPoolTest(void)
Laurence Lundblade0155b622018-10-12 20:04:37 +08002755{
Laurence Lundbladef6531662018-12-04 10:42:22 +09002756 // Set up the decoder with a tiny bit of CBOR to parse
2757 QCBORDecodeContext DC;
2758 const uint8_t pMinimalCBOR[] = {0xa0}; // One empty map
2759 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pMinimalCBOR),0);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002760
Laurence Lundbladef6531662018-12-04 10:42:22 +09002761 // Set up an memory pool of 100 bytes
2762 UsefulBuf_MAKE_STACK_UB(Pool, 100);
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08002763 QCBORError nError = QCBORDecode_SetMemPool(&DC, Pool, 0);
2764 if(nError) {
2765 return -9;
2766 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002767
Laurence Lundbladef6531662018-12-04 10:42:22 +09002768 // Cheat a little to get to the string allocator object
2769 // so we can call it directly to test it
2770 QCBORStringAllocator *pAlloc = (QCBORStringAllocator *)DC.pStringAllocator;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002771 // Cheat some more to know exactly the
Laurence Lundbladef6531662018-12-04 10:42:22 +09002772 size_t uAvailPool = MemPoolTestHook_GetPoolSize(pAlloc);
2773
2774 // First test -- ask for too much in one go
2775 UsefulBuf Allocated = (*pAlloc->fAllocate)(pAlloc->pAllocaterContext, NULL, uAvailPool+1);
2776 if(!UsefulBuf_IsNULL(Allocated)) {
2777 return -1;
2778 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002779
2780
Laurence Lundbladef6531662018-12-04 10:42:22 +09002781 // Re do the set up for the next test that will do a successful alloc,
2782 // a fail, a free and then success
2783 // This test should work on 32 and 64-bit machines if the compiler
2784 // does the expected thing with pointer sizes for the internal
2785 // MemPool implementation leaving 44 or 72 bytes of pool memory.
2786 QCBORDecode_SetMemPool(&DC, Pool, 0);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002787
Laurence Lundbladef6531662018-12-04 10:42:22 +09002788 // Cheat a little to get to the string allocator object
2789 // so we can call it directly to test it
2790 pAlloc = (QCBORStringAllocator *)DC.pStringAllocator;
2791 // Cheat some more to know exactly the
2792 uAvailPool = MemPoolTestHook_GetPoolSize(pAlloc);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002793
Laurence Lundbladef6531662018-12-04 10:42:22 +09002794 Allocated = (*pAlloc->fAllocate)(pAlloc->pAllocaterContext, NULL, uAvailPool-1);
2795 if(UsefulBuf_IsNULL(Allocated)) { // expected to succeed
2796 return -2;
2797 }
2798 UsefulBuf Allocated2 = (*pAlloc->fAllocate)(pAlloc->pAllocaterContext, NULL, uAvailPool/2);
2799 if(!UsefulBuf_IsNULL(Allocated2)) { // expected to fail
2800 return -3;
2801 }
2802 (*pAlloc->fFree)(pAlloc->pAllocaterContext, Allocated.ptr);
2803 Allocated = (*pAlloc->fAllocate)(pAlloc->pAllocaterContext, NULL, uAvailPool/2);
2804 if(UsefulBuf_IsNULL(Allocated)) { // succeed because of the free
2805 return -4;
2806 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002807
2808
Laurence Lundbladef6531662018-12-04 10:42:22 +09002809 // Re do set up for next test that involves a successful alloc,
2810 // and a successful realloc and a failed realloc
2811 QCBORDecode_SetMemPool(&DC, Pool, 0);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002812
Laurence Lundbladef6531662018-12-04 10:42:22 +09002813 // Cheat a little to get to the string allocator object
2814 // so we can call it directly to test it
2815 pAlloc = (QCBORStringAllocator *)DC.pStringAllocator;
2816 Allocated = (*pAlloc->fAllocate)(pAlloc->pAllocaterContext, NULL, uAvailPool/2);
2817 if(UsefulBuf_IsNULL(Allocated)) { // expected to succeed
2818 return -5;
2819 }
2820 Allocated2 = (*pAlloc->fAllocate)(pAlloc->pAllocaterContext, Allocated.ptr, uAvailPool);
2821 if(UsefulBuf_IsNULL(Allocated2)) {
2822 return -6;
2823 }
2824 if(Allocated2.ptr != Allocated.ptr || Allocated2.len != uAvailPool) {
2825 return -7;
2826 }
2827 UsefulBuf Allocated3 = (*pAlloc->fAllocate)(pAlloc->pAllocaterContext, Allocated.ptr, uAvailPool+1);
2828 if(!UsefulBuf_IsNULL(Allocated3)) { // expected to fail
2829 return -8;
2830 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002831
Laurence Lundbladef6531662018-12-04 10:42:22 +09002832 return 0;
2833}
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002834