blob: 3cdec8044fd9bea856e3804f31421421d48a753c [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 Lundblade20db9c92018-12-17 11:40:37 -080039#ifdef PRINT_FUNCTIONS_FOR_DEBUGGINGXX
40#include <stdio.h>
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080041static void printencoded(const char *szLabel, const uint8_t *pEncoded, size_t nLen)
42{
43 if(szLabel) {
44 printf("%s ", szLabel);
45 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -080046
Laurence Lundblade570fab52018-10-13 18:28:27 +080047 size_t i;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080048 for(i = 0; i < nLen; i++) {
49 uint8_t Z = pEncoded[i];
50 printf("%02x ", Z);
51 }
52 printf("\n");
53
54 fflush(stdout);
55}
Laurence Lundblade20db9c92018-12-17 11:40:37 -080056#endif
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080057
58
Laurence Lundbladeb836efb2018-10-28 20:09:58 +070059static const uint8_t spExpectedEncodedInts[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080060 0x98, 0x2f, 0x3b, 0x7f, 0xff, 0xff, 0xff, 0xff,
61 0xff, 0xff, 0xff, 0x3b, 0x00, 0x00, 0x00, 0x01,
62 0x00, 0x00, 0x00, 0x00, 0x3a, 0xff, 0xff, 0xff,
63 0xff, 0x3a, 0xff, 0xff, 0xff, 0xfe, 0x3a, 0xff,
64 0xff, 0xff, 0xfd, 0x3a, 0x7f, 0xff, 0xff, 0xff,
65 0x3a, 0x7f, 0xff, 0xff, 0xfe, 0x3a, 0x00, 0x01,
66 0x00, 0x01, 0x3a, 0x00, 0x01, 0x00, 0x00, 0x39,
67 0xff, 0xff, 0x39, 0xff, 0xfe, 0x39, 0xff, 0xfd,
68 0x39, 0x01, 0x00, 0x38, 0xff, 0x38, 0xfe, 0x38,
69 0xfd, 0x38, 0x18, 0x37, 0x36, 0x20, 0x00, 0x00,
70 0x01, 0x16, 0x17, 0x18, 0x18, 0x18, 0x19, 0x18,
71 0x1a, 0x18, 0xfe, 0x18, 0xff, 0x19, 0x01, 0x00,
72 0x19, 0x01, 0x01, 0x19, 0xff, 0xfe, 0x19, 0xff,
73 0xff, 0x1a, 0x00, 0x01, 0x00, 0x00, 0x1a, 0x00,
74 0x01, 0x00, 0x01, 0x1a, 0x00, 0x01, 0x00, 0x02,
75 0x1a, 0x7f, 0xff, 0xff, 0xff, 0x1a, 0x7f, 0xff,
76 0xff, 0xff, 0x1a, 0x80, 0x00, 0x00, 0x00, 0x1a,
77 0x80, 0x00, 0x00, 0x01, 0x1a, 0xff, 0xff, 0xff,
78 0xfe, 0x1a, 0xff, 0xff, 0xff, 0xff, 0x1b, 0x00,
79 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x1b,
80 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01,
81 0x1b, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
82 0xff, 0x1b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
83 0xff, 0xff};
84
85
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080086// return CBOR error or -1 if type of value doesn't match
87
88static int IntegerValuesParseTestInternal(QCBORDecodeContext *pDCtx)
89{
90 QCBORItem Item;
91 int nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -080092
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080093 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
94 return nCBORError;
95 if(Item.uDataType != QCBOR_TYPE_ARRAY)
96 return -1;
97
98 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
99 return nCBORError;
100 if(Item.uDataType != QCBOR_TYPE_INT64 || // Todo; fix this for 32-bit machines
Laurence Lundblade570fab52018-10-13 18:28:27 +0800101 Item.val.int64 != -9223372036854775807LL - 1)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800102 return -1;
103
104 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
105 return nCBORError;
106 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade570fab52018-10-13 18:28:27 +0800107 Item.val.int64 != -4294967297)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800108 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800109
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800110 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
111 return nCBORError;
112 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade570fab52018-10-13 18:28:27 +0800113 Item.val.int64 != -4294967296)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800114 return -1;
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 != -4294967295)
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 != -4294967294)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800126 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800127
128
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800129 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
130 return nCBORError;
131 if(Item.uDataType != QCBOR_TYPE_INT64 ||
132 Item.val.int64 != -2147483648)
133 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800134
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 != -2147483647)
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 != -65538)
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 != -65537)
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 != -65536)
157 return -1;
158
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800159
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800160 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
161 return nCBORError;
162 if(Item.uDataType != QCBOR_TYPE_INT64 ||
163 Item.val.int64 != -65535)
164 return -1;
165
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800166
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800167 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
168 return nCBORError;
169 if(Item.uDataType != QCBOR_TYPE_INT64 ||
170 Item.val.int64 != -65534)
171 return -1;
172
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800173
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800174 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
175 return nCBORError;
176 if(Item.uDataType != QCBOR_TYPE_INT64 ||
177 Item.val.int64 != -257)
178 return -1;
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 != -256)
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 != -255)
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 != -254)
196 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800197
198
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800199 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
200 return nCBORError;
201 if(Item.uDataType != QCBOR_TYPE_INT64 ||
202 Item.val.int64 != -25)
203 return -1;
204
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800205
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800206 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
207 return nCBORError;
208 if(Item.uDataType != QCBOR_TYPE_INT64 ||
209 Item.val.int64 != -24)
210 return -1;
211
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800212
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800213 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
214 return nCBORError;
215 if(Item.uDataType != QCBOR_TYPE_INT64 ||
216 Item.val.int64 != -23)
217 return -1;
218
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800219
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800220 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
221 return nCBORError;
222 if(Item.uDataType != QCBOR_TYPE_INT64 ||
223 Item.val.int64 != -1)
224 return -1;
225
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800226
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800227 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
228 return nCBORError;
229 if(Item.uDataType != QCBOR_TYPE_INT64 ||
230 Item.val.int64 != 0)
231 return -1;
232
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800233
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800234 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
235 return nCBORError;
236 if(Item.uDataType != QCBOR_TYPE_INT64 ||
237 Item.val.int64 != 0)
238 return -1;
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 != 1)
244 return -1;
245
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800246
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800247 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
248 return nCBORError;
249 if(Item.uDataType != QCBOR_TYPE_INT64 ||
250 Item.val.int64 != 22)
251 return -1;
252
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800253
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800254 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
255 return nCBORError;
256 if(Item.uDataType != QCBOR_TYPE_INT64 ||
257 Item.val.int64 != 23)
258 return -1;
259
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800260
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800261 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
262 return nCBORError;
263 if(Item.uDataType != QCBOR_TYPE_INT64 ||
264 Item.val.int64 != 24)
265 return -1;
266
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800267
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800268 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
269 return nCBORError;
270 if(Item.uDataType != QCBOR_TYPE_INT64 ||
271 Item.val.int64 != 25)
272 return -1;
273
274 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
275 return nCBORError;
276 if(Item.uDataType != QCBOR_TYPE_INT64 ||
277 Item.val.int64 != 26)
278 return -1;
279
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800280
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800281 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
282 return nCBORError;
283 if(Item.uDataType != QCBOR_TYPE_INT64 ||
284 Item.val.int64 != 254)
285 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800286
287
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800288 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
289 return nCBORError;
290 if(Item.uDataType != QCBOR_TYPE_INT64 ||
291 Item.val.int64 != 255)
292 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800293
294
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800295 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
296 return nCBORError;
297 if(Item.uDataType != QCBOR_TYPE_INT64 ||
298 Item.val.int64 != 256)
299 return -1;
300
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800301
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800302 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
303 return nCBORError;
304 if(Item.uDataType != QCBOR_TYPE_INT64 ||
305 Item.val.int64 != 257)
306 return -1;
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 != 65534)
312 return -1;
313
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800314
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800315 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
316 return nCBORError;
317 if(Item.uDataType != QCBOR_TYPE_INT64 ||
318 Item.val.int64 != 65535)
319 return -1;
320
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800321
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800322 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
323 return nCBORError;
324 if(Item.uDataType != QCBOR_TYPE_INT64 ||
325 Item.val.int64 != 65536)
326 return -1;
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 != 65537)
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 != 65538)
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 != 2147483647)
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 != 2147483648)
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 != 2147483649)
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 != 4294967294)
368 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800369
370
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800371 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
372 return nCBORError;
373 if(Item.uDataType != QCBOR_TYPE_INT64 ||
374 Item.val.int64 != 4294967295)
375 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800376
377
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800378 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
379 return nCBORError;
380 if(Item.uDataType != QCBOR_TYPE_INT64 ||
381 Item.val.int64 != 4294967296)
382 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800383
384
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800385 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
386 return nCBORError;
387 if(Item.uDataType != QCBOR_TYPE_INT64 ||
388 Item.val.int64 != 4294967297)
389 return -1;
390
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800391
392
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800393 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
394 return nCBORError;
395 if(Item.uDataType != QCBOR_TYPE_INT64 ||
396 Item.val.int64 != 9223372036854775807LL)
397 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800398
399
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800400 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
401 return nCBORError;
402 if(Item.uDataType != QCBOR_TYPE_UINT64 ||
403 Item.val.uint64 != 18446744073709551615ULL)
404 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800405
406
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800407 if(QCBORDecode_Finish(pDCtx) != QCBOR_SUCCESS) {
408 return -1;
409 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800410
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800411 return 0;
412}
413
414
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800415/*
416 Tests the decoding of lots of different integers sizes
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +0800417 and values.
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800418 */
419
420int IntegerValuesParseTest()
421{
422 int n;
423 QCBORDecodeContext DCtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800424
Laurence Lundbladeb836efb2018-10-28 20:09:58 +0700425 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedEncodedInts), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800426
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800427 n = IntegerValuesParseTestInternal(&DCtx);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800428
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800429 return(n);
430}
431
432
433/*
434 Creates a simple CBOR array and returns it in *pEncoded. The array is malloced
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800435 and needs to be freed. This is used by several tests.
436
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800437 Two of the inputs can be set. Two other items in the array are fixed.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800438
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800439 */
440
441static int CreateSimpleArray(int nInt1, int nInt2, uint8_t **pEncoded, size_t *pEncodedLen)
442{
443 QCBOREncodeContext ECtx;
444 int nReturn = -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800445
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800446 *pEncoded = NULL;
447 *pEncodedLen = INT32_MAX;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800448
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800449 // loop runs CBOR encoding twice. First with no buffer to
450 // calucate the length so buffer can be allocated correctly,
451 // and last with the buffer to do the actual encoding
452 do {
Laurence Lundblade0595e932018-11-02 22:22:47 +0700453 QCBOREncode_Init(&ECtx, (UsefulBuf){*pEncoded, *pEncodedLen});
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800454 QCBOREncode_OpenArray(&ECtx);
455 QCBOREncode_AddInt64(&ECtx, nInt1);
456 QCBOREncode_AddInt64(&ECtx, nInt2);
457 QCBOREncode_AddBytes(&ECtx, ((UsefulBufC) {"galactic", 8}));
458 QCBOREncode_AddBytes(&ECtx, ((UsefulBufC) {"haven token", 11}));
459 QCBOREncode_CloseArray(&ECtx);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800460
Laurence Lundblade0595e932018-11-02 22:22:47 +0700461 UsefulBufC Encoded;
462 if(QCBOREncode_Finish(&ECtx, &Encoded))
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800463 goto Done;
464
465 if(*pEncoded != NULL) {
Laurence Lundblade0595e932018-11-02 22:22:47 +0700466 *pEncodedLen = Encoded.len;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800467 nReturn = 0;
468 goto Done;
469 }
Laurence Lundblade0595e932018-11-02 22:22:47 +0700470 *pEncoded = malloc(Encoded.len);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800471 if(*pEncoded == NULL) {
472 nReturn = -1;
473 goto Done;
474 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800475
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800476 } while(1);
477Done:
478 return (nReturn);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800479
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800480}
481
482
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800483/*
484 {"first integer": 42,
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900485 "an array of two strings": [
486 "string1", "string2"
487 ],
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800488 "map in a map": {
489 "bytes 1": h'78787878',
490 "bytes 2": h'79797979',
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900491 "another int": 98,
492 "text 2": "lies, damn lies and statistics"
493 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800494 }
495 */
496
497static uint8_t pValidMapEncoded[] = {
Laurence Lundbladeb836efb2018-10-28 20:09:58 +0700498 0xa3, 0x6d, 0x66, 0x69, 0x72, 0x73, 0x74, 0x20, 0x69, 0x6e,
499 0x74, 0x65, 0x67, 0x65, 0x72, 0x18, 0x2a, 0x77, 0x61, 0x6e,
500 0x20, 0x61, 0x72, 0x72, 0x61, 0x79, 0x20, 0x6f, 0x66, 0x20,
501 0x74, 0x77, 0x6f, 0x20, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67,
502 0x73, 0x82, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x31,
503 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x32, 0x6c, 0x6d,
504 0x61, 0x70, 0x20, 0x69, 0x6e, 0x20, 0x61, 0x20, 0x6d, 0x61,
505 0x70, 0xa4, 0x67, 0x62, 0x79, 0x74, 0x65, 0x73, 0x20, 0x31,
506 0x44, 0x78, 0x78, 0x78, 0x78, 0x67, 0x62, 0x79, 0x74, 0x65,
507 0x73, 0x20, 0x32, 0x44, 0x79, 0x79, 0x79, 0x79, 0x6b, 0x61,
508 0x6e, 0x6f, 0x74, 0x68, 0x65, 0x72, 0x20, 0x69, 0x6e, 0x74,
509 0x18, 0x62, 0x66, 0x74, 0x65, 0x78, 0x74, 0x20, 0x32, 0x78,
510 0x1e, 0x6c, 0x69, 0x65, 0x73, 0x2c, 0x20, 0x64, 0x61, 0x6d,
511 0x6e, 0x20, 0x6c, 0x69, 0x65, 0x73, 0x20, 0x61, 0x6e, 0x64,
512 0x20, 0x73, 0x74, 0x61, 0x74, 0x69, 0x73, 0x74, 0x69, 0x63,
513 0x73 } ;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800514
515static int ParseOrderedArray(const uint8_t *pEncoded, size_t nLen, int64_t *pInt1, int64_t *pInt2, const uint8_t **pBuf3, size_t *pBuf3Len, const uint8_t **pBuf4, size_t *pBuf4Len)
516{
517 QCBORDecodeContext DCtx;
518 QCBORItem Item;
519 int nReturn = -1; // assume error until success
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800520
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800521 QCBORDecode_Init(&DCtx, (UsefulBufC){pEncoded, nLen}, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800522
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800523 // Make sure the first thing is a map
524 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || Item.uDataType != QCBOR_TYPE_ARRAY)
525 goto Done;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800526
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800527 // First integer
Laurence Lundblade12b495d2018-12-17 11:15:54 -0800528 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || Item.uDataType != QCBOR_TYPE_INT64)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800529 goto Done;
530 *pInt1 = Item.val.int64;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800531
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800532 // Second integer
533 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || Item.uDataType != QCBOR_TYPE_INT64)
534 goto Done;
535 *pInt2 = Item.val.int64;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800536
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800537 // First string
538 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || Item.uDataType != QCBOR_TYPE_BYTE_STRING)
539 goto Done;
540 *pBuf3 = Item.val.string.ptr;
541 *pBuf3Len = Item.val.string.len;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800542
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800543 // Second string
544 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || Item.uDataType != QCBOR_TYPE_BYTE_STRING)
545 goto Done;
546 *pBuf4 = Item.val.string.ptr;
547 *pBuf4Len = Item.val.string.len;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800548
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800549 nReturn = 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800550
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800551Done:
552 return(nReturn);
553}
554
555
556
Laurence Lundblade9e3651c2018-10-10 11:49:55 +0800557
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800558int SimpleArrayTest()
559{
560 uint8_t *pEncoded;
561 size_t nEncodedLen;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800562
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800563 int64_t i1, i2;
564 size_t i3, i4;
565 const uint8_t *s3, *s4;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800566
567
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800568 if(CreateSimpleArray(23, 6000, &pEncoded, &nEncodedLen) < 0) {
569 return(-1);
570 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800571
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800572 ParseOrderedArray(pEncoded, nEncodedLen, &i1, &i2, &s3, &i3, &s4, &i4);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800573
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800574 if(i1 != 23 ||
575 i2 != 6000 ||
576 i3 != 8 ||
577 i4 != 11 ||
Laurence Lundbladea44d5062018-10-17 18:45:12 +0530578 memcmp("galactic", s3, 8) !=0 ||
579 memcmp("haven token", s4, 11) !=0) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800580 return(-1);
581 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800582
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800583 return(0);
584}
585
586
Laurence Lundblade9e3651c2018-10-10 11:49:55 +0800587
Laurence Lundbladeb836efb2018-10-28 20:09:58 +0700588static uint8_t spDeepArrays[] = {0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x80};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800589
590int ParseDeepArrayTest()
591{
592 QCBORDecodeContext DCtx;
593 int nReturn = 0;
594 int i;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800595
Laurence Lundbladeb836efb2018-10-28 20:09:58 +0700596 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spDeepArrays), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800597
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800598 for(i = 0; i < 10; i++) {
599 QCBORItem Item;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800600
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800601 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
602 Item.uDataType != QCBOR_TYPE_ARRAY ||
603 Item.uNestingLevel != i) {
604 nReturn = -1;
605 break;
606 }
607 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800608
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800609 return(nReturn);
610}
611
Laurence Lundblade972e59c2018-11-11 15:57:23 +0700612// Big enough to test nesting to the depth of 24
613static uint8_t spTooDeepArrays[] = {0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81,
614 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81,
615 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81,
616 0x80};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800617
618int ParseTooDeepArrayTest()
619{
620 QCBORDecodeContext DCtx;
621 int nReturn = 0;
622 int i;
623 QCBORItem Item;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800624
625
Laurence Lundbladeb836efb2018-10-28 20:09:58 +0700626 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTooDeepArrays), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800627
Laurence Lundblade972e59c2018-11-11 15:57:23 +0700628 for(i = 0; i < QCBOR_MAX_ARRAY_NESTING1; i++) {
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800629
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800630 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
631 Item.uDataType != QCBOR_TYPE_ARRAY ||
632 Item.uNestingLevel != i) {
633 nReturn = -1;
634 break;
635 }
636 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800637
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800638 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_ARRAY_NESTING_TOO_DEEP)
639 nReturn = -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800640
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800641 return(nReturn);
642}
643
644
645
646
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800647int ShortBufferParseTest()
648{
649 int nResult = 0;
650 QCBORDecodeContext DCtx;
651 int num;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800652
Laurence Lundbladeb836efb2018-10-28 20:09:58 +0700653 for(num = sizeof(spExpectedEncodedInts)-1; num; num--) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800654 int n;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800655
Laurence Lundbladeb836efb2018-10-28 20:09:58 +0700656 QCBORDecode_Init(&DCtx, (UsefulBufC){spExpectedEncodedInts, num}, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800657
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800658 n = IntegerValuesParseTestInternal(&DCtx);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800659
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800660 //printf("Len %d, result: %d\n", num, n);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800661
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800662 if(n != QCBOR_ERR_HIT_END) {
663 nResult = -1;
664 goto Done;
665 }
666 }
667Done:
668 return nResult;
669}
670
671
Laurence Lundblade9e3651c2018-10-10 11:49:55 +0800672
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800673int ShortBufferParseTest2()
674{
675 uint8_t *pEncoded;
676 int nReturn;
677 size_t nEncodedLen;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800678
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800679 int64_t i1, i2;
680 size_t i3, i4;
681 const uint8_t *s3, *s4;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800682
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800683 nReturn = 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800684
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800685 if(CreateSimpleArray(23, 6000, &pEncoded, &nEncodedLen) < 0) {
686 return(-1);
687 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800688
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800689 for(nEncodedLen--; nEncodedLen; nEncodedLen--) {
690 int nResult = ParseOrderedArray(pEncoded, (uint32_t)nEncodedLen, &i1, &i2, &s3, &i3, &s4, &i4);
691 if(nResult == 0) {
692 nReturn = -1;
693 }
694 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800695
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800696 return(nReturn);
697}
698
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530699/*
700 Decode and thoroughly check a moderately complex
701 set of maps
702 */
Laurence Lundbladeea567ac2018-12-09 14:03:21 -0800703static int ParseMapTest1(QCBORDecodeMode nMode)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800704{
705 QCBORDecodeContext DCtx;
706 QCBORItem Item;
707 int nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800708
Laurence Lundbladeea567ac2018-12-09 14:03:21 -0800709 QCBORDecode_Init(&DCtx, (UsefulBufC){pValidMapEncoded, sizeof(pValidMapEncoded)}, nMode);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800710
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900711 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800712 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900713 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800714 if(Item.uDataType != QCBOR_TYPE_MAP ||
715 Item.val.uCount != 3)
716 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800717
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900718 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800719 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900720 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800721 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800722 Item.uDataType != QCBOR_TYPE_INT64 ||
723 Item.val.int64 != 42 ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530724 Item.uDataAlloc ||
725 Item.uLabelAlloc ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900726 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("first integer"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800727 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900728 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800729
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900730 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800731 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900732 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800733 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530734 Item.uDataAlloc ||
735 Item.uLabelAlloc ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900736 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("an array of two strings")) ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800737 Item.uDataType != QCBOR_TYPE_ARRAY ||
738 Item.val.uCount != 2)
739 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800740
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900741 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800742 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900743 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800744 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530745 Item.uDataAlloc ||
746 Item.uLabelAlloc ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900747 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("string1"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800748 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900749 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800750
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900751 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800752 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900753 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800754 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530755 Item.uDataAlloc ||
756 Item.uLabelAlloc ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900757 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("string2"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800758 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900759 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800760
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900761 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800762 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900763 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800764 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530765 Item.uDataAlloc ||
766 Item.uLabelAlloc ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900767 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("map in a map")) ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800768 Item.uDataType != QCBOR_TYPE_MAP ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900769 Item.val.uCount != 4) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800770 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900771 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800772
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900773 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800774 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900775 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800776 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900777 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("bytes 1"))||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800778 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530779 Item.uDataAlloc ||
780 Item.uLabelAlloc ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900781 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("xxxx"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800782 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900783 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800784
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900785 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800786 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900787 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800788 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900789 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("bytes 2")) ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800790 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530791 Item.uDataAlloc ||
792 Item.uLabelAlloc ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900793 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("yyyy"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800794 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900795 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800796
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900797 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800798 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900799 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800800 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530801 Item.uDataAlloc ||
802 Item.uLabelAlloc ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900803 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("another int")) ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800804 Item.uDataType != QCBOR_TYPE_INT64 ||
805 Item.val.int64 != 98)
806 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800807
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900808 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800809 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900810 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800811 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900812 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("text 2"))||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800813 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530814 Item.uDataAlloc ||
815 Item.uLabelAlloc ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900816 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("lies, damn lies and statistics"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800817 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900818 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800819
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800820 return 0;
821}
822
823
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900824/*
825 Decode and thoroughly check a moderately complex
826 set of maps
827 */
828int ParseMapAsArrayTest()
829{
830 QCBORDecodeContext DCtx;
831 QCBORItem Item;
832 int nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800833
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900834 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), QCBOR_DECODE_MODE_MAP_AS_ARRAY);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800835
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900836 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
837 return nCBORError;
838 }
Laurence Lundbladed61cbf32018-12-09 11:42:21 -0800839 if(Item.uDataType != QCBOR_TYPE_MAP_AS_ARRAY ||
840 Item.val.uCount != 6) {
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900841 return -1;
842 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800843
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900844 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
845 return nCBORError;
846 }
847 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
848 Item.uDataAlloc ||
849 Item.uLabelAlloc ||
850 Item.uLabelType != QCBOR_TYPE_NONE ||
851 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("first integer"))) {
852 return -2;
853 }
854
855 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
856 return nCBORError;
857 }
858 if(Item.uLabelType != QCBOR_TYPE_NONE ||
859 Item.uDataType != QCBOR_TYPE_INT64 ||
860 Item.val.int64 != 42 ||
861 Item.uDataAlloc ||
862 Item.uLabelAlloc) {
863 return -3;
864 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800865
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900866 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
867 return nCBORError;
868 }
869 if(Item.uLabelType != QCBOR_TYPE_NONE ||
870 Item.uDataAlloc ||
871 Item.uLabelAlloc ||
872 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("an array of two strings")) ||
873 Item.uDataType != QCBOR_TYPE_TEXT_STRING) {
874 return -4;
875 }
876
877 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
878 return nCBORError;
879 }
880 if(Item.uLabelType != QCBOR_TYPE_NONE ||
881 Item.uDataAlloc ||
882 Item.uLabelAlloc ||
883 Item.uDataType != QCBOR_TYPE_ARRAY ||
884 Item.val.uCount != 2) {
885 return -5;
886 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800887
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900888 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
889 return nCBORError;
890 }
891 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
892 Item.val.string.len != 7 ||
893 Item.uDataAlloc ||
894 Item.uLabelAlloc ||
895 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("string1"))) {
896 return -6;
897 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800898
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900899 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
900 return nCBORError;
901 }
902 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
903 Item.uDataAlloc ||
904 Item.uLabelAlloc ||
905 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("string2"))) {
906 return -7;
907 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800908
909
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900910 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
911 return nCBORError;
912 }
913 if(Item.uLabelType != QCBOR_TYPE_NONE ||
914 Item.uDataAlloc ||
915 Item.uLabelAlloc ||
916 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("map in a map"))) {
917 return -8;
918 }
919
920 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
921 return nCBORError;
922 }
923 if(Item.uLabelType != QCBOR_TYPE_NONE ||
924 Item.uDataAlloc ||
925 Item.uLabelAlloc ||
Laurence Lundbladed61cbf32018-12-09 11:42:21 -0800926 Item.uDataType != QCBOR_TYPE_MAP_AS_ARRAY ||
927 Item.val.uCount != 8) {
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900928 return -9;
929 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800930
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900931 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
932 return nCBORError;
933 }
934 if(Item.uLabelType != QCBOR_TYPE_NONE ||
935 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("bytes 1"))||
936 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
937 Item.uDataAlloc ||
938 Item.uLabelAlloc) {
939 return -10;
940 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800941
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900942 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
943 return nCBORError;
944 }
945 if(Item.uLabelType != QCBOR_TYPE_NONE ||
946 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
947 Item.uDataAlloc ||
948 Item.uLabelAlloc ||
949 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("xxxx"))) {
950 return -11;
951 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800952
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900953 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
954 return nCBORError;
955 }
956 if(Item.uLabelType != QCBOR_TYPE_NONE ||
957 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("bytes 2")) ||
958 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
959 Item.uDataAlloc ||
960 Item.uLabelAlloc) {
961 return -12;
962 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800963
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900964 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
965 return nCBORError;
966 }
967 if(Item.uLabelType != QCBOR_TYPE_NONE ||
968 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
969 Item.uDataAlloc ||
970 Item.uLabelAlloc ||
971 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("yyyy"))) {
972 return -13;
973 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800974
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900975 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
976 return nCBORError;
977 }
978 if(Item.uLabelType != QCBOR_TYPE_NONE ||
979 Item.uDataAlloc ||
980 Item.uLabelAlloc ||
981 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("another int")) ||
982 Item.uDataType != QCBOR_TYPE_TEXT_STRING) {
983 return -14;
984 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800985
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900986 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
987 return nCBORError;
988 }
989 if(Item.uLabelType != QCBOR_TYPE_NONE ||
990 Item.uDataAlloc ||
991 Item.uLabelAlloc ||
992 Item.uDataType != QCBOR_TYPE_INT64 ||
993 Item.val.int64 != 98) {
994 return -15;
995 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800996
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900997 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
998 return nCBORError;
999 }
1000 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1001 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("text 2"))||
1002 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1003 Item.uDataAlloc ||
1004 Item.uLabelAlloc) {
1005 return -16;
1006 }
1007
1008 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
1009 return nCBORError;
1010 }
1011 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1012 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1013 Item.uDataAlloc ||
1014 Item.uLabelAlloc ||
1015 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("lies, damn lies and statistics"))) {
1016 return -17;
1017 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001018
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001019 return 0;
1020}
1021
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001022
1023/*
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301024 Fully or partially decode pValidMapEncoded. When
1025 partially decoding check for the right error code.
1026 How much partial decoding depends on nLevel.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001027
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301028 The partial decodes test error conditions of
1029 incomplete encoded input.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001030
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301031 This could be combined with the above test
1032 and made prettier and maybe a little more
1033 thorough.
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001034 */
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001035static int ExtraBytesTest(int nLevel)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001036{
1037 QCBORDecodeContext DCtx;
1038 QCBORItem Item;
1039 int nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001040
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001041 QCBORDecode_Init(&DCtx, (UsefulBufC){pValidMapEncoded, sizeof(pValidMapEncoded)}, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001042
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001043 if(nLevel < 1) {
1044 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_EXTRA_BYTES) {
1045 return -1;
1046 } else {
1047 return 0;
1048 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001049 }
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301050
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001051
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001052 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001053 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001054 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001055 if(Item.uDataType != QCBOR_TYPE_MAP ||
1056 Item.val.uCount != 3)
1057 return -1;
1058
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001059 if(nLevel < 2) {
1060 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
1061 return -1;
1062 } else {
1063 return 0;
1064 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001065 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001066
1067
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001068 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001069 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001070 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001071 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001072 Item.uDataType != QCBOR_TYPE_INT64 ||
1073 Item.val.uCount != 42 ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001074 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("first integer"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001075 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001076 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001077
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001078 if(nLevel < 3) {
1079 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
1080 return -1;
1081 } else {
1082 return 0;
1083 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001084 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001085
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001086 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001087 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001088 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001089 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001090 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("an array of two strings")) ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001091 Item.uDataType != QCBOR_TYPE_ARRAY ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001092 Item.val.uCount != 2) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001093 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001094 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001095
1096
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001097 if(nLevel < 4) {
1098 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
1099 return -1;
1100 } else {
1101 return 0;
1102 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001103 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001104
1105
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001106 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001107 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001108 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001109 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001110 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("string1"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001111 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001112 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001113
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001114 if(nLevel < 5) {
1115 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
1116 return -1;
1117 } else {
1118 return 0;
1119 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001120 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001121
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001122 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001123 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001124 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001125 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001126 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("string2"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001127 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001128 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001129
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001130 if(nLevel < 6) {
1131 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
1132 return -1;
1133 } else {
1134 return 0;
1135 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001136 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001137
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001138 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001139 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001140 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001141 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001142 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("map in a map")) ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001143 Item.uDataType != QCBOR_TYPE_MAP ||
1144 Item.val.uCount != 4)
1145 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001146
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001147 if(nLevel < 7) {
1148 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
1149 return -1;
1150 } else {
1151 return 0;
1152 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001153 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001154
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001155 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001156 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001157 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001158 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001159 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("bytes 1")) ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001160 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001161 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("xxxx"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001162 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001163 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001164
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001165 if(nLevel < 8) {
1166 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
1167 return -1;
1168 } else {
1169 return 0;
1170 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001171 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001172
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001173 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001174 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001175 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001176 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001177 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("bytes 2")) ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001178 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001179 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("yyyy"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001180 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001181 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001182
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001183 if(nLevel < 9) {
1184 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
1185 return -1;
1186 } else {
1187 return 0;
1188 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001189 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001190
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001191 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001192 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001193 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001194 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001195 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("another int")) ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001196 Item.uDataType != QCBOR_TYPE_INT64 ||
1197 Item.val.int64 != 98)
1198 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001199
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001200 if(nLevel < 10) {
1201 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
1202 return -1;
1203 } else {
1204 return 0;
1205 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001206 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001207
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001208 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001209 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001210 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001211 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001212 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("text 2"))||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001213 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001214 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("lies, damn lies and statistics"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001215 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001216 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001217
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301218 if(QCBORDecode_Finish(&DCtx)) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001219 return -1;
1220 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001221
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001222 return 0;
1223}
1224
1225
1226
1227
1228int ParseMapTest()
1229{
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301230 // Parse a moderatly complex map structure very thoroughl
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08001231 int n = ParseMapTest1(QCBOR_DECODE_MODE_NORMAL);
1232
1233 n = ParseMapTest1(QCBOR_DECODE_MODE_MAP_STRINGS_ONLY);
1234
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001235 if(!n) {
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001236 for(int i = 0; i < 10; i++) {
1237 n = ExtraBytesTest(i);
1238 if(n) {
1239 break;
1240 }
1241 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001242 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001243
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001244 return(n);
1245}
1246
1247
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07001248static uint8_t spSimpleValues[] = {0x8a, 0xf4, 0xf5, 0xf6, 0xf7, 0xff, 0xe0, 0xf3, 0xf8, 0x00, 0xf8, 0x13, 0xf8, 0x1f, 0xf8, 0x20, 0xf8, 0xff};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001249
1250int ParseSimpleTest()
1251{
1252 QCBORDecodeContext DCtx;
1253 QCBORItem Item;
1254 int nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001255
1256
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07001257 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSimpleValues), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001258
1259
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001260 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1261 return nCBORError;
1262 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
1263 Item.val.uCount != 10)
1264 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001265
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001266 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1267 return nCBORError;
1268 if(Item.uDataType != QCBOR_TYPE_FALSE)
1269 return -1;
1270
1271 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1272 return nCBORError;
1273 if(Item.uDataType != QCBOR_TYPE_TRUE)
1274 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001275
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001276 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1277 return nCBORError;
1278 if(Item.uDataType != QCBOR_TYPE_NULL)
1279 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001280
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001281 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1282 return nCBORError;
1283 if(Item.uDataType != QCBOR_TYPE_UNDEF)
1284 return -1;
1285
1286 // A break
Laurence Lundblade9e3651c2018-10-10 11:49:55 +08001287 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_BREAK)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001288 return -1;
1289
1290 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1291 return nCBORError;
1292 if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 0)
1293 return -1;
1294
1295 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1296 return nCBORError;
1297 if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 19)
1298 return -1;
1299
1300 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_INVALID_CBOR)
1301 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001302
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001303 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_INVALID_CBOR)
1304 return -1;
1305
1306 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_INVALID_CBOR)
1307 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001308
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001309 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1310 return nCBORError;
1311 if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 32)
1312 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001313
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001314 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1315 return nCBORError;
1316 if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 255)
1317 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001318
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001319 return 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001320
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001321}
1322
1323
1324struct FailInput {
1325 UsefulBufC Input;
1326 int nError;
1327};
1328
1329
1330struct FailInput Failures[] = {
1331 { {(uint8_t[]){0x18}, 1}, QCBOR_ERR_HIT_END }, // 1 byte integer missing the byte
1332 { {(uint8_t[]){0x1c}, 1}, QCBOR_ERR_UNSUPPORTED }, // Reserved additional info = 28
1333 { {(uint8_t[]){0x1d}, 1}, QCBOR_ERR_UNSUPPORTED }, // Reserved additional info = 29
1334 { {(uint8_t[]){0x1e}, 1}, QCBOR_ERR_UNSUPPORTED }, // Reserved additional info = 30
1335 { {(uint8_t[]){0x1f}, 1}, QCBOR_ERR_UNSUPPORTED }, // Indefinite length integer
1336 { {(uint8_t[]){0x3c}, 1}, QCBOR_ERR_UNSUPPORTED }, // 1 byte integer missing the byte
1337 { {(uint8_t[]){0x3d}, 1}, QCBOR_ERR_UNSUPPORTED }, // 1 byte integer missing the byte
1338 { {(uint8_t[]){0x3e}, 1}, QCBOR_ERR_UNSUPPORTED }, // 1 byte integer missing the byte
1339 { {(uint8_t[]){0x3f}, 1}, QCBOR_ERR_UNSUPPORTED }, // Indefinite length negative integer
1340 { {(uint8_t[]){0x41}, 1}, QCBOR_ERR_HIT_END }, // Short byte string
1341 { {(uint8_t[]){0x5c}, 1}, QCBOR_ERR_UNSUPPORTED }, // Reserved additional info = 28
1342 { {(uint8_t[]){0x5f}, 1}, QCBOR_ERR_UNSUPPORTED }, // Indefinite length byte string
1343 { {(uint8_t[]){0x61}, 1}, QCBOR_ERR_HIT_END }, // Short UTF-8 string
1344 { {(uint8_t[]){0x7c}, 1}, QCBOR_ERR_UNSUPPORTED }, // Reserved additional info = 28
1345 { {(uint8_t[]){0x7f}, 1}, QCBOR_ERR_UNSUPPORTED }, // Indefinite length UTF-8 string
1346 { {(uint8_t[]){0xff}, 1}, QCBOR_ERR_UNSUPPORTED } , // break
1347 { {(uint8_t[]){0xf8, 0x00}, 2}, QCBOR_ERR_INVALID_CBOR }, // An invalid encoding of a simple type
1348 { {(uint8_t[]){0xf8, 0x1f}, 2}, QCBOR_ERR_INVALID_CBOR }, // An invalid encoding of a simple type
1349 { {(uint8_t[]){0xc0, 0x00}, 2}, QCBOR_ERR_BAD_OPT_TAG }, // Text-based date, with an integer
1350 { {(uint8_t[]){0xc1, 0x41, 0x33}, 3}, QCBOR_ERR_BAD_OPT_TAG }, // Epoch date, with an byte string
1351 { {(uint8_t[]){0xc1, 0xc0, 0x00}, 3}, QCBOR_ERR_BAD_OPT_TAG }, // tagged as both epoch and string dates
1352 { {(uint8_t[]){0xc2, 0x00}, 2}, QCBOR_ERR_BAD_OPT_TAG } // big num tagged an int, not a byte string
1353
1354};
1355
1356
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001357int FailureTests()
1358{
1359 int nResult = 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001360
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001361 struct FailInput *pFEnd = &Failures[0] + sizeof(Failures)/sizeof(struct FailInput);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001362
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001363 for(struct FailInput *pF = &Failures[0]; pF < pFEnd ;pF++) {
1364 QCBORDecodeContext DCtx;
1365 QCBORItem Item;
1366 int nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001367
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001368 QCBORDecode_Init(&DCtx, pF->Input, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001369
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001370 while(1) {
1371 nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
1372 if(QCBOR_ERR_HIT_END == nCBORError) {
1373 break;
1374 }
1375 if(nCBORError != pF->nError) {
1376 nResult = 1;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001377 break;
1378 }
1379 }
1380 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001381
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001382 {
1383 QCBORDecodeContext DCtx;
1384 QCBORItem Item;
1385 int nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001386
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07001387 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSimpleValues), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001388
1389 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1390 return nCBORError;
1391 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
1392 Item.val.uCount != 10)
1393 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001394
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001395 DCtx.InBuf.magic = 0; // Corrupt the UsefulInputBuf
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001396
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001397 nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
1398 if(nCBORError != QCBOR_ERR_HIT_END)
1399 return -1;
1400 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001401
1402
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001403 return nResult;
1404}
1405
1406
1407
1408
1409static void Recurser(uint8_t *pBuf, int nLen, int nLenMax)
1410{
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001411
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001412 if(nLen >= nLenMax) {
1413 return;
1414 }
1415
1416 //printf("__%d__%d__\n", nLen, nLenMax);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001417
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001418 for(int i = 0; i < 256; i++) {
1419 pBuf[nLen] = i;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001420
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001421 QCBORDecodeContext DCtx;
1422 QCBORItem Item;
1423 int nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001424
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08001425 const UsefulBufC Input = {pBuf, nLen+1};
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001426
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001427 QCBORDecode_Init(&DCtx, Input, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001428
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001429 while(1) {
1430 nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
1431 if(QCBOR_ERR_HIT_END == nCBORError) {
1432 break;
1433 }
1434 if(nCBORError != QCBOR_SUCCESS) {
1435 if(nCBORError != QCBOR_ERR_UNSUPPORTED && nCBORError != QCBOR_ERR_HIT_END && nCBORError != QCBOR_ERR_INVALID_CBOR) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001436 }
1437 break;
1438 }
1439 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001440
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001441
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001442 Recurser(pBuf, nLen+1, nLenMax);
1443 }
1444}
1445
1446
1447/*
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001448 Runs all possible input strings of a given length. This is set to 3 to make the test
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001449 run in reasonable time.
1450 Main point of this test is to not crash.
1451 */
1452
1453int ComprehensiveInputTest()
1454{
1455 uint8_t pBuf[3]; // 3 keeps it running in reasonable time. 4 takes tens of minutes.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001456
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001457 Recurser(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 Lundbladeb836efb2018-10-28 20:09:58 +07001462static uint8_t spDateTestInput[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001463 0xc0, // tag for string date
1464 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001465
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001466 0xc1, // tag for epoch date
1467 0x1a, 0x53, 0x72, 0x4E, 0x00, // Epoch date 1400000000; Tue, 13 May 2014 16:53:20 GMT
1468
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001469 // CBOR_TAG_B64
1470 0xc1, 0xcf, 0xd8, 0x22, // 0xee, // Epoch date with extra tags TODO: fix this test
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001471 0x1a, 0x53, 0x72, 0x4E, 0x01,
1472
1473 0xc1, // tag for epoch date
1474 0x1b, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, // Too large integer
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001475
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001476 0xc1, // tag for epoch date
1477 0xfa, 0x3f, 0x8c, 0xcc, 0xcd, // double with value 1.1
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001478
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001479 0xc1, // tag for epoch date
1480 0xfa, 0x7f, 0x7f, 0xff, 0xff // 3.4028234663852886e+38 too large
1481
1482};
1483
1484
1485// have to check float expected only to within an epsilon
1486int CHECK_EXPECTED_DOUBLE(double val, double expected) {
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001487
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001488 double diff = val - expected;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001489
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001490 diff = fabs(diff);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001491
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001492 return diff > 0.0000001;
1493}
1494
1495
1496int DateParseTest()
1497{
1498 QCBORDecodeContext DCtx;
1499 QCBORItem Item;
1500 int nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001501
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07001502 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spDateTestInput), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001503
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001504 const uint64_t uTags[] = {15};
1505 QCBORTagListIn TagList = {1, uTags};
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001506
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001507 QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TagList);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001508
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001509 // String date
1510 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1511 return -1;
1512 if(Item.uDataType != QCBOR_TYPE_DATE_STRING ||
Laurence Lundblade9e3651c2018-10-10 11:49:55 +08001513 UsefulBuf_Compare(Item.val.dateString, UsefulBuf_FromSZ("1985-04-12"))){
Laurence Lundblade67bd5512018-11-02 21:44:06 +07001514 return -2;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001515 }
1516
1517 // Epoch date
1518 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade67bd5512018-11-02 21:44:06 +07001519 return -3;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001520 if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH ||
1521 Item.val.epochDate.nSeconds != 1400000000 ||
1522 Item.val.epochDate.fSecondsFraction != 0 ) {
Laurence Lundblade67bd5512018-11-02 21:44:06 +07001523 return -4;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001524 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001525
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001526 // Epoch date with extra CBOR_TAG_B64 tag that doesn't really mean anything
1527 // but want to be sure extra tag doesn't cause a problem
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001528 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade67bd5512018-11-02 21:44:06 +07001529 return -5;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001530 if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH ||
1531 Item.val.epochDate.nSeconds != 1400000001 ||
1532 Item.val.epochDate.fSecondsFraction != 0 ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001533 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_B64)) {
Laurence Lundblade67bd5512018-11-02 21:44:06 +07001534 return -6;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001535 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001536
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001537 // Epoch date that is too large for our representation
1538 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_DATE_OVERFLOW) {
Laurence Lundblade67bd5512018-11-02 21:44:06 +07001539 return -7;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001540 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001541
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001542 // Epoch date in float format with fractional seconds
1543 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade67bd5512018-11-02 21:44:06 +07001544 return -8;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001545 if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH ||
1546 Item.val.epochDate.nSeconds != 1 ||
1547 CHECK_EXPECTED_DOUBLE(Item.val.epochDate.fSecondsFraction, 0.1 )) {
Laurence Lundblade67bd5512018-11-02 21:44:06 +07001548 return -9;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001549 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001550
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001551 // Epoch date float that is too large for our representation
1552 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_DATE_OVERFLOW) {
Laurence Lundblade67bd5512018-11-02 21:44:06 +07001553 return -10;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001554 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001555
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001556 // TODO: could use a few more tests with float, double, and half precsion and negative (but coverage is still pretty good)
1557
1558 return 0;
1559}
1560
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001561// Really simple basic input for tagging test
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07001562static uint8_t spOptTestInput[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001563 0xd9, 0xd9, 0xf7, // CBOR magic number
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001564 0x81, // Array of one
1565 0xd8, 0x04, // non-preferred serialization of tag 4
1566 0x82, 0x01, 0x03}; // fraction 1/3
1567
1568static uint8_t spEncodedLargeTag[] = {0xdb, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x80};
1569
1570// 0x9192939495969798, 0x88, 0x01, 0x04
1571static uint8_t spLotsOfTags[] = {0xdb, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0xd8, 0x88, 0xc5, 0xc4, 0x80};
1572
1573/*
1574 The cbor.me parse of this.
1575 55799(55799(55799({6(7(-23)): 5859837686836516696(7({7(-20): 11({17(-18): 17(17(17("Organization"))),
1576 9(-17): 773("SSG"), -15: 4(5(6(7(8(9(10(11(12(13(14(15("Confusion")))))))))))), 17(-16): 17("San Diego"),
1577 17(-14): 17("US")}), 23(-19): 19({-11: 9({-9: -7}),
1578 90599561(90599561(90599561(-10))): 12(h'0102030405060708090A')})})),
1579 16(-22): 23({11(8(7(-5))): 8(-3)})})))
1580 */
1581static uint8_t spCSRWithTags[] = {
1582 0xd9, 0xd9, 0xf7, 0xd9, 0xd9, 0xf7, 0xd9, 0xd9, 0xf7, 0xa2,
1583 0xc6, 0xc7, 0x36,
1584 0xdb, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0xc7, 0xa2,
1585 0xda, 0x00, 0x00, 0x00, 0x07, 0x33,
1586 0xcb, 0xa5,
1587 0xd1, 0x31,
1588 0xd1, 0xd1, 0xd1, 0x6c,
1589 0x4f, 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74, 0x69, 0x6f, 0x6e,
1590 0xc9, 0x30,
1591 0xd9, 0x03, 0x05, 0x63,
1592 0x53, 0x53, 0x47,
1593 0x2e,
1594 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf, 0x69,
1595 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73, 0x69, 0x6f, 0x6e,
1596 0xd1, 0x2f,
1597 0xd1, 0x69,
1598 0x53, 0x61, 0x6e, 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f,
1599 0xd1, 0x2d,
1600 0xd1, 0x62,
1601 0x55, 0x53,
1602 0xd7, 0x32,
1603 0xd3, 0xa2,
1604 0x2a,
1605 0xc9, 0xa1,
1606 0x28,
1607 0x26,
1608 0xda, 0x05, 0x66, 0x70, 0x89, 0xda, 0x05, 0x66, 0x70, 0x89, 0xda, 0x05, 0x66, 0x70, 0x89, 0x29,
1609 0xcc, 0x4a,
1610 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,0x07, 0x08, 0x09, 0x0a,
1611 0xd0, 0x35,
1612 0xd7, 0xa1,
1613 0xcb, 0xc8, 0xc7, 0x24,
1614 0xc8, 0x22};
1615
1616static int CheckCSRMaps(QCBORDecodeContext *pDC);
1617
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001618
1619int OptTagParseTest()
1620{
1621 QCBORDecodeContext DCtx;
1622 QCBORItem Item;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001623
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07001624 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spOptTestInput), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001625
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001626 //-------------------------
1627 // This text matches the magic number tag and the fraction tag
1628 if(QCBORDecode_GetNext(&DCtx, &Item)) {
1629 return -2;
1630 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001631 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001632 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC)) {
1633 return -3;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001634 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001635
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001636 if(QCBORDecode_GetNext(&DCtx, &Item)) {
1637 return -4;
1638 }
1639 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
1640 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_FRACTION) ||
1641 Item.val.uCount != 2) {
1642 return -5;
1643 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001644
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001645 // --------------------------------
1646 // This test decodes the very large tag, but it is not in
1647 // any list so it is ignored.
1648 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEncodedLargeTag), QCBOR_DECODE_MODE_NORMAL);
1649 if(QCBORDecode_GetNext(&DCtx, &Item)) {
1650 return -6;
1651 }
1652 if(Item.uTagBits) {
1653 return -7;
1654 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001655
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001656 // ----------------------------------
1657 // This test sets up a caller-config list that includes the very large tage and then matches it.
1658 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEncodedLargeTag), QCBOR_DECODE_MODE_NORMAL);
1659 const uint64_t puList[] = {0x9192939495969798, 257};
1660 const QCBORTagListIn TL = {2, puList};
1661 QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001662
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001663 if(QCBORDecode_GetNext(&DCtx, &Item)) {
1664 return -8;
1665 }
1666 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
1667 !QCBORDecode_IsTagged(&DCtx, &Item, 0x9192939495969798) ||
1668 QCBORDecode_IsTagged(&DCtx, &Item, 257) ||
1669 QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_BIGFLOAT) ||
1670 Item.val.uCount != 0) {
1671 return -9;
1672 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001673
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001674 //------------------------
1675 // This test sets up a caller-configured list, and looks up something not in it
1676 const uint64_t puLongList[17] = {1,2,1};
1677 const QCBORTagListIn TLLong = {17, puLongList};
1678 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEncodedLargeTag), QCBOR_DECODE_MODE_NORMAL);
1679 QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TLLong);
1680 if(QCBORDecode_GetNext(&DCtx, &Item)) {
1681 return -11;
1682 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001683
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001684 // -----------------------
1685 // This tests retrievel of the full tag list
1686 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spLotsOfTags), QCBOR_DECODE_MODE_NORMAL);
1687 uint64_t puTags[16];
1688 QCBORTagListOut Out = {0, 4, puTags};
1689 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
1690 return -12;
1691 }
1692 if(puTags[0] != 0x9192939495969798 ||
1693 puTags[1] != 0x88 ||
1694 puTags[2] != 0x05 ||
1695 puTags[3] != 0x04) {
1696 return -13;
1697 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001698
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001699 // ----------------------
1700 // This text if too small of an out list
1701 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spLotsOfTags), QCBOR_DECODE_MODE_NORMAL);
1702 QCBORTagListOut OutSmall = {0, 3, puTags};
1703 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &OutSmall) != QCBOR_ERR_TOO_MANY_TAGS) {
1704 return -14;
1705 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001706
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001707 // ---------------
1708 // Parse a version of the "CSR" that has had a ton of tags randomly inserted
1709 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRWithTags), QCBOR_DECODE_MODE_NORMAL);
1710 int n = CheckCSRMaps(&DCtx);
1711 if(n) {
1712 return n-2000;
1713 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001714
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001715 Out = (QCBORTagListOut){0,16, puTags};
1716 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRWithTags), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001717
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001718 const uint64_t puTagList[] = {773, 1, 90599561};
1719 const QCBORTagListIn TagList = {3, puTagList};
1720 QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TagList);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001721
1722
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001723 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
1724 return -100;
1725 }
1726 if(Item.uDataType != QCBOR_TYPE_MAP ||
1727 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC) ||
1728 QCBORDecode_IsTagged(&DCtx, &Item, 90599561) ||
1729 QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_DATE_EPOCH) ||
1730 Item.val.uCount != 2 ||
1731 puTags[0] != CBOR_TAG_CBOR_MAGIC ||
1732 puTags[1] != CBOR_TAG_CBOR_MAGIC ||
1733 puTags[2] != CBOR_TAG_CBOR_MAGIC ||
1734 Out.uNumUsed != 3) {
1735 return -101;
1736 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001737
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001738 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
1739 return -102;
1740 }
1741 if(Item.uDataType != QCBOR_TYPE_MAP ||
1742 QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC) ||
1743 QCBORDecode_IsTagged(&DCtx, &Item, 6) ||
1744 QCBORDecode_IsTagged(&DCtx, &Item, 7) || // item is tagged 7, but 7 is not configured to be recognized
1745 Item.val.uCount != 2 ||
1746 puTags[0] != 5859837686836516696 ||
1747 puTags[1] != 7 ||
1748 Out.uNumUsed != 2) {
1749 return -103;
1750 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001751
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001752 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
1753 return -104;
1754 }
1755 if(Item.uDataType != QCBOR_TYPE_MAP ||
1756 Item.uTagBits ||
1757 Item.val.uCount != 5 ||
1758 puTags[0] != 0x0b ||
1759 Out.uNumUsed != 1) {
1760 return -105;
1761 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001762
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001763 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
1764 return -106;
1765 }
1766 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1767 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_COSE_MAC0) ||
1768 Item.val.string.len != 12 ||
1769 puTags[0] != CBOR_TAG_COSE_MAC0 ||
1770 puTags[1] != CBOR_TAG_COSE_MAC0 ||
1771 puTags[2] != CBOR_TAG_COSE_MAC0 ||
1772 Out.uNumUsed != 3) {
1773 return -105;
1774 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001775
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001776 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
1777 return -107;
1778 }
1779 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1780 !QCBORDecode_IsTagged(&DCtx, &Item, 773) ||
1781 Item.val.string.len != 3 ||
1782 puTags[0] != 773 ||
1783 Out.uNumUsed != 1) {
1784 return -108;
1785 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001786
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001787 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
1788 return -109;
1789 }
1790 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1791 !QCBORDecode_IsTagged(&DCtx, &Item, 4) ||
1792 Item.val.string.len != 9 ||
1793 puTags[0] != 4 ||
1794 puTags[11] != 0x0f ||
1795 Out.uNumUsed != 12) {
1796 return -110;
1797 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001798
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001799 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
1800 return -111;
1801 }
1802 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1803 !QCBORDecode_IsTagged(&DCtx, &Item, 17) ||
1804 Item.val.string.len != 9 ||
1805 puTags[0] != 17 ||
1806 Out.uNumUsed != 1) {
1807 return -112;
1808 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001809
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001810 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
1811 return -111;
1812 }
1813 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1814 !QCBORDecode_IsTagged(&DCtx, &Item, 17) ||
1815 Item.val.string.len != 2 ||
1816 puTags[0] != 17 ||
1817 Out.uNumUsed != 1) {
1818 return -112;
1819 }
1820
1821 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
1822 return -113;
1823 }
1824 if(Item.uDataType != QCBOR_TYPE_MAP ||
1825 QCBORDecode_IsTagged(&DCtx, &Item, 19) ||
1826 Item.val.uCount != 2 ||
1827 puTags[0] != 19 ||
1828 Out.uNumUsed != 1) {
1829 return -114;
1830 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001831
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001832 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
1833 return -115;
1834 }
1835 if(Item.uDataType != QCBOR_TYPE_MAP ||
1836 QCBORDecode_IsTagged(&DCtx, &Item, 9) ||
1837 Item.uTagBits ||
1838 Item.val.uCount != 1 ||
1839 puTags[0] != 9 ||
1840 Out.uNumUsed != 1) {
1841 return -116;
1842 }
1843
1844 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
1845 return -116;
1846 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001847 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001848 Item.val.int64 != -7 ||
1849 Item.uTagBits ||
1850 Out.uNumUsed != 0) {
1851 return -117;
1852 }
1853
1854 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
1855 return -118;
1856 }
1857 if(Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
1858 Item.val.string.len != 10 ||
1859 Item.uTagBits ||
1860 puTags[0] != 12 ||
1861 Out.uNumUsed != 1) {
1862 return -119;
1863 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001864
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001865 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
1866 return -120;
1867 }
1868 if(Item.uDataType != QCBOR_TYPE_MAP ||
1869 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_ENC_AS_B16) ||
1870 Item.val.uCount != 1 ||
1871 puTags[0] != 0x17 ||
1872 Out.uNumUsed != 1) {
1873 return -121;
1874 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001875
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001876 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
1877 return -122;
1878 }
1879 if(Item.uDataType != QCBOR_TYPE_INT64 ||
1880 QCBORDecode_IsTagged(&DCtx, &Item, 8) ||
1881 Item.val.int64 != -3 ||
1882 puTags[0] != 8 ||
1883 Out.uNumUsed != 1) {
1884 return -123;
1885 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001886
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001887 if(QCBORDecode_Finish(&DCtx)) {
1888 return -124;
1889 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001890
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001891 return 0;
1892}
1893
1894
1895
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001896
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07001897static uint8_t spBigNumInput[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001898 0x83,
1899 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1900 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1901 0xA4,
1902 0x63, 0x42, 0x4E, 0x2B,
1903 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1904 0x18, 0x40,
1905 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1906 0x63, 0x42, 0x4E, 0x2D,
1907 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1908 0x38, 0x3F,
1909 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
1910
1911
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07001912static uint8_t spBigNum[] = {0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001913
1914
1915int BignumParseTest()
1916{
1917 QCBORDecodeContext DCtx;
1918 QCBORItem Item;
1919 int nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001920
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07001921 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNumInput), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001922
1923
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001924 //
1925 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1926 return -1;
1927 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
1928 return -1;
1929 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001930
1931 //
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001932 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1933 return -1;
1934 if(Item.uDataType != QCBOR_TYPE_POSBIGNUM ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07001935 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001936 return -1;
1937 }
1938
1939 //
1940 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1941 return -1;
1942 if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07001943 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001944 return -1;
1945 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001946
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001947 //
1948 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1949 return -1;
1950 if(Item.uDataType != QCBOR_TYPE_MAP) {
1951 return -1;
1952 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001953
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001954 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1955 return -1;
1956 if(Item.uDataType != QCBOR_TYPE_POSBIGNUM ||
1957 Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07001958 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001959 return -1;
1960 }
1961
1962 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1963 return -1;
1964 if(Item.uDataType != QCBOR_TYPE_POSBIGNUM ||
1965 Item.uLabelType != QCBOR_TYPE_INT64 ||
1966 Item.label.int64 != 64 ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07001967 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001968 return -1;
1969 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001970
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001971 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1972 return -1;
1973 if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM ||
1974 Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07001975 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001976 return -1;
1977 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001978
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001979 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1980 return -1;
1981 if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM ||
1982 Item.uLabelType != QCBOR_TYPE_INT64 ||
1983 Item.label.int64 != -64 ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07001984 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001985 return -1;
1986 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001987
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001988 return 0;
1989}
1990
1991
1992
Laurence Lundbladea44d5062018-10-17 18:45:12 +05301993static int CheckItemWithIntLabel(QCBORDecodeContext *pCtx, uint8_t uDataType, uint8_t uNestingLevel, uint8_t uNextNest, int64_t nLabel, QCBORItem *pItem)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001994{
1995 QCBORItem Item;
1996 int nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001997
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001998 if((nCBORError = QCBORDecode_GetNext(pCtx, &Item))) return -1;
1999 if(Item.uDataType != uDataType) return -1;
2000 if(uNestingLevel > 0) {
2001 if(Item.uLabelType != QCBOR_TYPE_INT64 && Item.uLabelType != QCBOR_TYPE_UINT64) return -1;
2002 if(Item.uLabelType == QCBOR_TYPE_INT64) {
2003 if(Item.label.int64 != nLabel) return -1;
2004 } else {
Laurence Lundblade570fab52018-10-13 18:28:27 +08002005 if(Item.label.uint64 != (uint64_t)nLabel) return -1;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002006 }
2007 }
2008 if(Item.uNestingLevel != uNestingLevel) return -1;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302009 if(Item.uNextNestLevel != uNextNest) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002010
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002011 if(pItem) {
2012 *pItem = Item;
2013 }
2014 return 0;
2015}
2016
2017
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002018// Same code checks definite and indefinite length versions of the map
2019static int CheckCSRMaps(QCBORDecodeContext *pDC)
2020{
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302021 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 0, 1, 0, NULL)) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002022
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302023 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 1, 2, -23, NULL)) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002024
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302025 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 2, 3, -20, NULL)) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002026
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302027 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -18, NULL)) return -1;
2028 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -17, NULL)) return -1;
2029 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -15, NULL)) return -1;
2030 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -16, NULL)) return -1;
2031 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 2, -14, NULL)) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002032
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302033 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 2, 3, -19, NULL)) return -1;
2034 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 3, 4, -11, NULL)) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002035
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302036 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_INT64, 4, 3, -9, NULL)) return -1;
2037 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_BYTE_STRING, 3, 1, -10, NULL)) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002038
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302039 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 1, 2, -22, NULL)) return -1;
2040 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_INT64, 2, 0, -5, NULL)) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002041
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002042 if(QCBORDecode_Finish(pDC)) return -2;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002043
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002044 return 0;
2045}
2046
2047
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002048/*
2049// cbor.me decoded output
2050{
2051 -23: {
2052 -20: {
2053 -18: "Organization",
2054 -17: "SSG",
2055 -15: "Confusion",
2056 -16: "San Diego",
2057 -14: "US"
2058 },
2059 -19: {
2060 -11: {
2061 -9: -7
2062 },
2063 -10: '\u0001\u0002\u0003\u0004\u0005\u0006\a\b\t\n'
2064 }
2065 },
2066 -22: {
2067 -5: -3
2068 }
2069}
2070 */
2071
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002072
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002073static uint8_t spCSRInput[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002074 0xa2, 0x36, 0xa2, 0x33, 0xa5, 0x31, 0x6c, 0x4f,
2075 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74,
2076 0x69, 0x6f, 0x6e, 0x30, 0x63, 0x53, 0x53, 0x47,
2077 0x2e, 0x69, 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73,
2078 0x69, 0x6f, 0x6e, 0x2f, 0x69, 0x53, 0x61, 0x6e,
2079 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f, 0x2d, 0x62,
2080 0x55, 0x53, 0x32, 0xa2, 0x2a, 0xa1, 0x28, 0x26,
2081 0x29, 0x4a, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
2082 0x07, 0x08, 0x09, 0x0a, 0x35, 0xa1, 0x24, 0x22};
2083
2084int NestedMapTest()
2085{
2086 QCBORDecodeContext DCtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002087
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002088 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002089
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002090 return CheckCSRMaps(&DCtx);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002091}
2092
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08002093
2094
2095int StringDecoderModeFailTest()
2096{
2097 QCBORDecodeContext DCtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002098
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08002099 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput), QCBOR_DECODE_MODE_MAP_STRINGS_ONLY);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002100
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08002101 QCBORItem Item;
2102 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002103
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08002104 if(QCBORDecode_GetNext(&DCtx, &Item)) {
2105 return -1;
2106 }
2107 if(Item.uDataType != QCBOR_TYPE_MAP) {
2108 return -2;
2109 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002110
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08002111 nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
2112 if(nCBORError != QCBOR_ERR_MAP_LABEL_TYPE) {
2113 return -3;
2114 }
2115
2116 return 0;
2117}
2118
2119
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002120// Same map as above, but using indefinite lengths
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002121static uint8_t spCSRInputIndefLen[] = {
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002122 0xbf, 0x36, 0xbf, 0x33, 0xbf, 0x31, 0x6c, 0x4f,
2123 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74,
2124 0x69, 0x6f, 0x6e, 0x30, 0x63, 0x53, 0x53, 0x47,
2125 0x2e, 0x69, 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73,
2126 0x69, 0x6f, 0x6e, 0x2f, 0x69, 0x53, 0x61, 0x6e,
2127 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f, 0x2d, 0x62,
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002128 0x55, 0x53, 0xff, 0x32, 0xbf, 0x2a, 0xbf, 0x28,
2129 0x26, 0xff, 0x29, 0x4a, 0x01, 0x02, 0x03, 0x04,
2130 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0xff, 0xff,
2131 0x35, 0xbf, 0x24, 0x22, 0xff, 0xff};
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002132
2133int NestedMapTestIndefLen()
2134{
2135 QCBORDecodeContext DCtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002136
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002137 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInputIndefLen), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002138
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002139 return CheckCSRMaps(&DCtx);
2140}
2141
2142
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002143
Laurence Lundblade17ede402018-10-13 11:43:07 +08002144static UsefulBufC make_nested_indefinite_arrays(int n, UsefulBuf Storage)
2145{
2146 UsefulOutBuf UOB;
2147 UsefulOutBuf_Init(&UOB, Storage);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002148
Laurence Lundblade17ede402018-10-13 11:43:07 +08002149 int i;
2150 for(i = 0; i < n; i++) {
2151 UsefulOutBuf_AppendByte(&UOB, 0x9f);
2152 }
2153
2154 for(i = 0; i < n; i++) {
2155 UsefulOutBuf_AppendByte(&UOB, 0xff);
2156 }
2157 return UsefulOutBuf_OutUBuf(&UOB);
2158}
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002159
2160
Laurence Lundblade17ede402018-10-13 11:43:07 +08002161static int parse_indeflen_nested(UsefulBufC Nested, int nNestLevel)
2162{
2163 QCBORDecodeContext DC;
2164 QCBORDecode_Init(&DC, Nested, 0);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002165
Laurence Lundblade17ede402018-10-13 11:43:07 +08002166 int j;
2167 for(j = 0; j < nNestLevel; j++) {
2168 QCBORItem Item;
2169 int nReturn = QCBORDecode_GetNext(&DC, &Item);
2170 if(j >= QCBOR_MAX_ARRAY_NESTING) {
2171 // Should be in error
2172 if(nReturn != QCBOR_ERR_ARRAY_NESTING_TOO_DEEP) {
2173 return -4;
2174 } else {
2175 return 0; // Decoding doesn't recover after an error
2176 }
2177 } else {
2178 // Should be no error
2179 if(nReturn) {
2180 return -9; // Should not have got an error
2181 }
2182 }
2183 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
2184 return -7;
2185 }
2186 }
2187 int nReturn = QCBORDecode_Finish(&DC);
2188 if(nReturn) {
2189 return -3;
2190 }
2191 return 0;
2192}
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002193
2194
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302195int IndefiniteLengthNestTest()
Laurence Lundblade17ede402018-10-13 11:43:07 +08002196{
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05302197 UsefulBuf_MAKE_STACK_UB(Storage, 50);
Laurence Lundblade17ede402018-10-13 11:43:07 +08002198 int i;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002199 for(i=1; i < QCBOR_MAX_ARRAY_NESTING+4; i++) {
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08002200 const UsefulBufC Nested = make_nested_indefinite_arrays(i, Storage);
Laurence Lundblade17ede402018-10-13 11:43:07 +08002201 int nReturn = parse_indeflen_nested(Nested, i);
2202 if(nReturn) {
2203 return nReturn;
2204 }
2205 }
2206 return 0;
2207}
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002208
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002209
Laurence Lundblade6de37062018-10-15 12:22:42 +05302210
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002211static const uint8_t spIndefiniteArray[] = {0x9f, 0x01, 0x82, 0x02, 0x03, 0xff}; // [1, [2, 3]]
2212static const uint8_t spIndefiniteArrayBad1[] = {0x9f}; // No closing break
2213static const uint8_t spIndefiniteArrayBad2[] = {0x9f, 0x9f, 0x02, 0xff}; // Not enough closing breaks
2214static const uint8_t spIndefiniteArrayBad3[] = {0x9f, 0x02, 0xff, 0xff}; // Too many closing breaks
2215static const uint8_t spIndefiniteArrayBad4[] = {0x81, 0x9f}; // Unclosed indeflen inside def len
2216static const uint8_t spIndefiniteArrayBad5[] = {0x9f, 0xd1, 0xff}; // confused tag
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002217
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302218int IndefiniteLengthArrayMapTest()
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002219{
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002220 int nResult;
2221 // --- first test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002222 UsefulBufC IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArray);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002223
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002224 // Decode it and see if it is OK
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05302225 UsefulBuf_MAKE_STACK_UB(MemPool, 150);
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002226 QCBORDecodeContext DC;
2227 QCBORItem Item;
2228 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002229
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002230 QCBORDecode_SetMemPool(&DC, MemPool, false);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002231
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002232 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05302233
2234 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
2235 Item.uNestingLevel != 0 ||
2236 Item.uNextNestLevel != 1) {
2237 return -111;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002238 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002239
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002240 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05302241 if(Item.uDataType != QCBOR_TYPE_INT64 ||
2242 Item.uNestingLevel != 1 ||
2243 Item.uNextNestLevel != 1) {
2244 return -2;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002245 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002246
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002247 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05302248 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
2249 Item.uNestingLevel != 1 ||
2250 Item.uNextNestLevel != 2) {
2251 return -3;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002252 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002253
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002254 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade12b495d2018-12-17 11:15:54 -08002255 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade6de37062018-10-15 12:22:42 +05302256 Item.uNestingLevel != 2 ||
2257 Item.uNextNestLevel != 2) {
2258 return -4;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002259 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002260
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002261 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade12b495d2018-12-17 11:15:54 -08002262 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade6de37062018-10-15 12:22:42 +05302263 Item.uNestingLevel != 2 ||
2264 Item.uNextNestLevel != 0) {
2265 return -5;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002266 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002267
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002268 if(QCBORDecode_Finish(&DC)) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05302269 return -6;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002270 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002271
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002272 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002273 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad1);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002274
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002275 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002276
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002277 QCBORDecode_SetMemPool(&DC, MemPool, false);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002278
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002279 nResult = QCBORDecode_GetNext(&DC, &Item);
2280 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05302281 return -7;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002282 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002283
Laurence Lundblade570fab52018-10-13 18:28:27 +08002284 nResult = QCBORDecode_Finish(&DC);
Laurence Lundblade6de37062018-10-15 12:22:42 +05302285 if(nResult != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
2286 return -8;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002287 }
2288
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002289
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002290 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002291 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad2);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002292
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002293 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002294
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002295 QCBORDecode_SetMemPool(&DC, MemPool, false);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002296
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002297 nResult = QCBORDecode_GetNext(&DC, &Item);
2298 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05302299 return -9;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002300 }
2301
2302 nResult = QCBORDecode_GetNext(&DC, &Item);
2303 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05302304 return -10;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002305 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002306
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002307 nResult = QCBORDecode_GetNext(&DC, &Item);
2308 if(nResult || Item.uDataType != QCBOR_TYPE_INT64) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05302309 return -11;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002310 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002311
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002312 nResult = QCBORDecode_Finish(&DC);
Laurence Lundblade6de37062018-10-15 12:22:42 +05302313 if(nResult != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
2314 return -12;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002315 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002316
2317
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002318 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002319 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad3);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002320
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002321 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002322
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002323 QCBORDecode_SetMemPool(&DC, MemPool, false);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002324
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002325 nResult = QCBORDecode_GetNext(&DC, &Item);
2326 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05302327 return -13;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002328 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002329
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002330 nResult = QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05302331 if(nResult != QCBOR_ERR_BAD_BREAK) {
2332 return -14;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002333 }
Laurence Lundblade6de37062018-10-15 12:22:42 +05302334
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002335
Laurence Lundblade570fab52018-10-13 18:28:27 +08002336 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002337 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad4);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002338
Laurence Lundblade570fab52018-10-13 18:28:27 +08002339 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002340
Laurence Lundblade570fab52018-10-13 18:28:27 +08002341 QCBORDecode_SetMemPool(&DC, MemPool, false);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002342
Laurence Lundblade570fab52018-10-13 18:28:27 +08002343 nResult = QCBORDecode_GetNext(&DC, &Item);
2344 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05302345 return -15;
Laurence Lundblade570fab52018-10-13 18:28:27 +08002346 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002347
Laurence Lundblade570fab52018-10-13 18:28:27 +08002348 nResult = QCBORDecode_GetNext(&DC, &Item);
2349 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05302350 return -16;
Laurence Lundblade570fab52018-10-13 18:28:27 +08002351 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002352
Laurence Lundblade570fab52018-10-13 18:28:27 +08002353 nResult = QCBORDecode_Finish(&DC);
Laurence Lundblade6de37062018-10-15 12:22:42 +05302354 if(nResult != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
2355 return -17;
Laurence Lundblade570fab52018-10-13 18:28:27 +08002356 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002357
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302358 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002359 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad5);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002360
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302361 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002362
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302363 QCBORDecode_SetMemPool(&DC, MemPool, false);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002364
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302365 nResult = QCBORDecode_GetNext(&DC, &Item);
2366 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05302367 return -18;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302368 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002369
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302370 nResult = QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05302371 if(nResult != QCBOR_ERR_BAD_BREAK) {
2372 return -19;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302373 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002374
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002375 return 0;
2376}
2377
Laurence Lundblade17ede402018-10-13 11:43:07 +08002378
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002379static const uint8_t spIndefiniteLenString[] = {
Laurence Lundblade17ede402018-10-13 11:43:07 +08002380 0x81, // Array of length one
2381 0x7f, // text string marked with indefinite length
2382 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment
2383 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment
2384 0xff // ending break
2385};
2386
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002387static const uint8_t spIndefiniteLenStringBad2[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302388 0x81, // Array of length one
2389 0x7f, // text string marked with indefinite length
2390 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment
2391 0x44, 0x6d, 0x69, 0x6e, 0x67, // second segment of wrong type
2392 0xff // ending break
2393};
2394
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002395static const uint8_t spIndefiniteLenStringBad3[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302396 0x81, // Array of length one
2397 0x7f, // text string marked with indefinite length
2398 0x01, 0x02, // Not a string
2399 0xff // ending break
2400};
2401
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002402static const uint8_t spIndefiniteLenStringBad4[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302403 0x81, // Array of length one
2404 0x7f, // text string marked with indefinite length
2405 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment
2406 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment
2407 // missing end of string
2408};
2409
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002410static const uint8_t spIndefiniteLenStringLabel[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302411 0xa1, // Array of length one
2412 0x7f, // text string marked with indefinite length
2413 0x65, 0x73, 0x74, 0x72, 0x75, 0x75, // first segment
2414 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment
2415 0xff, // ending break
2416 0x01 // integer being labeled.
2417};
2418
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08002419static UsefulBufC MakeIndefiniteBigBstr(UsefulBuf Storage) // TODO: size this
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302420{
2421 UsefulOutBuf UOB;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002422
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302423 UsefulOutBuf_Init(&UOB, Storage);
2424 UsefulOutBuf_AppendByte(&UOB, 0x81);
2425 UsefulOutBuf_AppendByte(&UOB, 0x5f);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002426
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302427 int i = 0;
2428 for(int nChunkSize = 1; nChunkSize <= 128; nChunkSize *= 2) {
2429 UsefulOutBuf_AppendByte(&UOB, 0x58);
2430 UsefulOutBuf_AppendByte(&UOB, (uint8_t)nChunkSize);
2431 for(int j = 0; j < nChunkSize; j++ ) {
2432 UsefulOutBuf_AppendByte(&UOB, i);
2433 i++;
2434 }
2435 }
2436 UsefulOutBuf_AppendByte(&UOB, 0xff);
2437
2438 return UsefulOutBuf_OutUBuf(&UOB);
2439}
2440
2441static int CheckBigString(UsefulBufC BigString)
2442{
2443 if(BigString.len != 255) {
2444 return 1;
2445 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002446
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302447 for(uint8_t i = 0; i < 255; i++){
2448 if(((const uint8_t *)BigString.ptr)[i] != i) {
2449 return 1;
2450 }
2451 }
2452 return 0;
2453}
2454
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05302455
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302456int IndefiniteLengthStringTest()
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302457{
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302458 QCBORDecodeContext DC;
2459 QCBORItem Item;
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05302460 // big enough for MakeIndefiniteBigBstr() + MemPool overhead
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08002461 UsefulBuf_MAKE_STACK_UB(MemPool, 350);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002462
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302463 // --- Simple normal indefinite length string ------
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002464 UsefulBufC IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenString);
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302465 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002466
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05302467 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302468 return -1;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302469 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002470
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302471 if(QCBORDecode_GetNext(&DC, &Item)) {
2472 return -2;
2473 }
2474 if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.uDataAlloc) {
2475 return -3;
2476 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002477
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302478 if(QCBORDecode_GetNext(&DC, &Item)) {
2479 return -4;
2480 }
2481 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || !Item.uDataAlloc) {
2482 return -5;
2483 }
2484 if(QCBORDecode_Finish(&DC)) {
2485 return -6;
2486 }
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302487
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302488 // ----- types mismatch ---
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002489 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad2), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002490
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302491 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
2492 return -7;
2493 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002494
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302495 if(QCBORDecode_GetNext(&DC, &Item)) {
2496 return -8;
2497 }
2498 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
2499 return -9;
2500 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002501
Laurence Lundblade30816f22018-11-10 13:40:22 +07002502 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_INDEFINITE_STRING_CHUNK) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302503 return -10;
2504 }
2505
2506 // ----- not a string ---
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002507 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad3), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002508
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302509 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
2510 return -11;
2511 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002512
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302513 if(QCBORDecode_GetNext(&DC, &Item)) {
2514 return -12;
2515 }
2516 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
2517 return -13;
2518 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002519
Laurence Lundblade30816f22018-11-10 13:40:22 +07002520 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_INDEFINITE_STRING_CHUNK) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302521 return -14;
2522 }
2523
2524 // ----- no end -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002525 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad4), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002526
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302527 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
2528 return -15;
2529 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002530
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302531 if(QCBORDecode_GetNext(&DC, &Item)) {
2532 return -16;
2533 }
2534 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
2535 return -17;
2536 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002537
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302538 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_HIT_END) {
2539 return -18;
2540 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002541
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302542 // ------ Don't set a string allocator and see an error -----
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302543 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002544
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302545 QCBORDecode_GetNext(&DC, &Item);
2546 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302547 return -19;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302548 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002549
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302550 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_NO_STRING_ALLOCATOR) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302551 return -20;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302552 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002553
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302554 // ----- Mempool is way too small -----
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05302555 UsefulBuf_MAKE_STACK_UB(MemPoolTooSmall, 20); // 20 is too small no matter what
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302556
2557 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
2558 if(!QCBORDecode_SetMemPool(&DC, MemPoolTooSmall, false)) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302559 return -21;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302560 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002561
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302562 // ----- Mempool is way too small -----
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05302563 UsefulBuf_MAKE_STACK_UB(BigIndefBStrStorage, 290);
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08002564 const UsefulBufC BigIndefBStr = MakeIndefiniteBigBstr(BigIndefBStrStorage);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002565
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05302566 UsefulBuf_MAKE_STACK_UB(MemPoolSmall, 80); // 80 is big enough for MemPool overhead, but not BigIndefBStr
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002567
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302568 QCBORDecode_Init(&DC, BigIndefBStr, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302569 if(QCBORDecode_SetMemPool(&DC, MemPoolSmall, false)) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302570 return -22;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302571 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002572
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302573 QCBORDecode_GetNext(&DC, &Item);
2574 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302575 return -23;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302576 }
Laurence Lundblade30816f22018-11-10 13:40:22 +07002577 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_STRING_ALLOCATE) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302578 return -24;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302579 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002580
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302581 // ---- big bstr -----
2582 QCBORDecode_Init(&DC, BigIndefBStr, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002583
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302584 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
2585 return -25;
2586 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002587
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05302588 if(QCBORDecode_GetNext(&DC, &Item)) {
2589 return -26;
2590 }
2591 if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.uDataAlloc) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302592 return -26;
2593 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002594
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302595 if(QCBORDecode_GetNext(&DC, &Item)) {
2596 return -27;
2597 }
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05302598 if(Item.uDataType != QCBOR_TYPE_BYTE_STRING || !Item.uDataAlloc || Item.uNestingLevel != 1) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302599 return -28;
2600 }
2601 if(CheckBigString(Item.val.string)) {
2602 return -3;
2603 }
2604 if(QCBORDecode_Finish(&DC)) {
2605 return -29;
2606 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002607
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302608 // --- label is an indefinite length string ------
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002609 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringLabel), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002610
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302611 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
2612 return -30;
2613 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002614
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302615 QCBORDecode_GetNext(&DC, &Item);
2616 if(Item.uDataType != QCBOR_TYPE_MAP) {
2617 return -31;
2618 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002619
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302620 if(QCBORDecode_GetNext(&DC, &Item)){
2621 return -32;
2622 }
2623 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || Item.uDataType != QCBOR_TYPE_INT64 ||
2624 Item.uDataAlloc || !Item.uLabelAlloc ||
2625 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("struuming"))) {
2626 return -33;
2627 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002628
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302629 if(QCBORDecode_Finish(&DC)) {
2630 return -34;
2631 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002632
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002633 return 0;
2634}
2635
2636
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302637int AllocAllStringsTest()
2638{
2639 QCBORDecodeContext DC;
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08002640 QCBORError nCBORError;
2641
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002642
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302643 // First test, use the "CSRMap" as easy input and checking
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002644 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002645
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08002646 UsefulBuf_MAKE_STACK_UB(Pool, sizeof(spCSRInput) + QCBOR_DECODE_MIN_MEM_POOL_SIZE);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002647
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08002648 nCBORError = QCBORDecode_SetMemPool(&DC, Pool, 1); // Turn on copying.
2649 if(nCBORError) {
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302650 return -1;
2651 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002652
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08002653 if(CheckCSRMaps(&DC)) {
2654 return -2;
2655 }
2656
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302657 // Next parse, save pointers to a few strings, destroy original and see all is OK.
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08002658 UsefulBuf_MAKE_STACK_UB(CopyOfStorage, sizeof(pValidMapEncoded) + QCBOR_DECODE_MIN_MEM_POOL_SIZE);
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08002659 const UsefulBufC CopyOf = UsefulBuf_Copy(CopyOfStorage, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded));
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002660
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302661 QCBORDecode_Init(&DC, CopyOf, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08002662 UsefulBuf_Set(Pool, '/');
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302663 QCBORDecode_SetMemPool(&DC, Pool, 1); // Turn on copying.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002664
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302665 QCBORItem Item1, Item2, Item3, Item4;
2666 if((nCBORError = QCBORDecode_GetNext(&DC, &Item1)))
2667 return nCBORError;
2668 if(Item1.uDataType != QCBOR_TYPE_MAP ||
2669 Item1.val.uCount != 3)
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08002670 return -3;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302671 if((nCBORError = QCBORDecode_GetNext(&DC, &Item1)))
2672 return nCBORError;
2673 if((nCBORError = QCBORDecode_GetNext(&DC, &Item2)))
2674 return nCBORError;
2675 if((nCBORError = QCBORDecode_GetNext(&DC, &Item3)))
2676 return nCBORError;
2677 if((nCBORError = QCBORDecode_GetNext(&DC, &Item4)))
2678 return nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002679
Laurence Lundblade05ec57b2018-10-21 01:50:03 +05302680 UsefulBuf_Set(CopyOfStorage, '_');
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002681
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302682 if(Item1.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302683 Item1.uDataType != QCBOR_TYPE_INT64 ||
2684 Item1.val.int64 != 42 ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09002685 UsefulBuf_Compare(Item1.label.string, UsefulBuf_FromSZ("first integer"))) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08002686 return -4;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09002687 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002688
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302689
2690 if(Item2.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09002691 UsefulBuf_Compare(Item2.label.string, UsefulBuf_FromSZ("an array of two strings")) ||
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302692 Item2.uDataType != QCBOR_TYPE_ARRAY ||
2693 Item2.val.uCount != 2)
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08002694 return -5;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002695
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302696 if(Item3.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09002697 UsefulBuf_Compare(Item3.val.string, UsefulBuf_FromSZ("string1"))) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08002698 return -6;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09002699 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002700
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302701 if(Item4.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09002702 UsefulBuf_Compare(Item4.val.string, UsefulBuf_FromSZ("string2"))) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08002703 return -7;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09002704 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002705
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302706 // Next parse with a pool that is too small
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08002707 UsefulBuf_MAKE_STACK_UB(SmallPool, QCBOR_DECODE_MIN_MEM_POOL_SIZE + 1);
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05302708 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302709 QCBORDecode_SetMemPool(&DC, SmallPool, 1); // Turn on copying.
2710 if((nCBORError = QCBORDecode_GetNext(&DC, &Item1)))
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08002711 return -8;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302712 if(Item1.uDataType != QCBOR_TYPE_MAP ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09002713 Item1.val.uCount != 3) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08002714 return -9;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09002715 }
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302716 if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item1))){
2717 if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item2))) {
2718 if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item3))) {
2719 nCBORError = QCBORDecode_GetNext(&DC, &Item4);
2720 }
2721 }
2722 }
Laurence Lundblade30816f22018-11-10 13:40:22 +07002723 if(nCBORError != QCBOR_ERR_STRING_ALLOCATE) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08002724 return -10;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302725 }
2726
2727 return 0;
2728}
2729
Laurence Lundbladef6531662018-12-04 10:42:22 +09002730// Cheating declaration to get to the special test hook
2731size_t MemPoolTestHook_GetPoolSize(void *ctx);
2732
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302733
2734int MemPoolTest(void)
Laurence Lundblade0155b622018-10-12 20:04:37 +08002735{
Laurence Lundbladef6531662018-12-04 10:42:22 +09002736 // Set up the decoder with a tiny bit of CBOR to parse
2737 QCBORDecodeContext DC;
2738 const uint8_t pMinimalCBOR[] = {0xa0}; // One empty map
2739 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pMinimalCBOR),0);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002740
Laurence Lundbladef6531662018-12-04 10:42:22 +09002741 // Set up an memory pool of 100 bytes
2742 UsefulBuf_MAKE_STACK_UB(Pool, 100);
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08002743 QCBORError nError = QCBORDecode_SetMemPool(&DC, Pool, 0);
2744 if(nError) {
2745 return -9;
2746 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002747
Laurence Lundbladef6531662018-12-04 10:42:22 +09002748 // Cheat a little to get to the string allocator object
2749 // so we can call it directly to test it
2750 QCBORStringAllocator *pAlloc = (QCBORStringAllocator *)DC.pStringAllocator;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002751 // Cheat some more to know exactly the
Laurence Lundbladef6531662018-12-04 10:42:22 +09002752 size_t uAvailPool = MemPoolTestHook_GetPoolSize(pAlloc);
2753
2754 // First test -- ask for too much in one go
2755 UsefulBuf Allocated = (*pAlloc->fAllocate)(pAlloc->pAllocaterContext, NULL, uAvailPool+1);
2756 if(!UsefulBuf_IsNULL(Allocated)) {
2757 return -1;
2758 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002759
2760
Laurence Lundbladef6531662018-12-04 10:42:22 +09002761 // Re do the set up for the next test that will do a successful alloc,
2762 // a fail, a free and then success
2763 // This test should work on 32 and 64-bit machines if the compiler
2764 // does the expected thing with pointer sizes for the internal
2765 // MemPool implementation leaving 44 or 72 bytes of pool memory.
2766 QCBORDecode_SetMemPool(&DC, Pool, 0);
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 pAlloc = (QCBORStringAllocator *)DC.pStringAllocator;
2771 // Cheat some more to know exactly the
2772 uAvailPool = MemPoolTestHook_GetPoolSize(pAlloc);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002773
Laurence Lundbladef6531662018-12-04 10:42:22 +09002774 Allocated = (*pAlloc->fAllocate)(pAlloc->pAllocaterContext, NULL, uAvailPool-1);
2775 if(UsefulBuf_IsNULL(Allocated)) { // expected to succeed
2776 return -2;
2777 }
2778 UsefulBuf Allocated2 = (*pAlloc->fAllocate)(pAlloc->pAllocaterContext, NULL, uAvailPool/2);
2779 if(!UsefulBuf_IsNULL(Allocated2)) { // expected to fail
2780 return -3;
2781 }
2782 (*pAlloc->fFree)(pAlloc->pAllocaterContext, Allocated.ptr);
2783 Allocated = (*pAlloc->fAllocate)(pAlloc->pAllocaterContext, NULL, uAvailPool/2);
2784 if(UsefulBuf_IsNULL(Allocated)) { // succeed because of the free
2785 return -4;
2786 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002787
2788
Laurence Lundbladef6531662018-12-04 10:42:22 +09002789 // Re do set up for next test that involves a successful alloc,
2790 // and a successful realloc and a failed realloc
2791 QCBORDecode_SetMemPool(&DC, Pool, 0);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002792
Laurence Lundbladef6531662018-12-04 10:42:22 +09002793 // Cheat a little to get to the string allocator object
2794 // so we can call it directly to test it
2795 pAlloc = (QCBORStringAllocator *)DC.pStringAllocator;
2796 Allocated = (*pAlloc->fAllocate)(pAlloc->pAllocaterContext, NULL, uAvailPool/2);
2797 if(UsefulBuf_IsNULL(Allocated)) { // expected to succeed
2798 return -5;
2799 }
2800 Allocated2 = (*pAlloc->fAllocate)(pAlloc->pAllocaterContext, Allocated.ptr, uAvailPool);
2801 if(UsefulBuf_IsNULL(Allocated2)) {
2802 return -6;
2803 }
2804 if(Allocated2.ptr != Allocated.ptr || Allocated2.len != uAvailPool) {
2805 return -7;
2806 }
2807 UsefulBuf Allocated3 = (*pAlloc->fAllocate)(pAlloc->pAllocaterContext, Allocated.ptr, uAvailPool+1);
2808 if(!UsefulBuf_IsNULL(Allocated3)) { // expected to fail
2809 return -8;
2810 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002811
Laurence Lundbladef6531662018-12-04 10:42:22 +09002812 return 0;
2813}
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002814