blob: d71ebd5b1203e3eb454037d9684afc866e7fb370 [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_decode_tests.h"
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -080034#include "qcbor.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 Lundblade1f8b5b02019-01-01 22:27:38 -080037
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080038
Laurence Lundbladea2e29072018-12-30 09:20:06 -080039#ifdef PRINT_FUNCTIONS_FOR_DEBUGGING
Laurence Lundblade20db9c92018-12-17 11:40:37 -080040#include <stdio.h>
Laurence Lundbladea2e29072018-12-30 09:20:06 -080041
42static void PrintUsefulBufC(const char *szLabel, UsefulBufC Buf)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080043{
44 if(szLabel) {
45 printf("%s ", szLabel);
46 }
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -080047
Laurence Lundblade570fab52018-10-13 18:28:27 +080048 size_t i;
Laurence Lundbladea2e29072018-12-30 09:20:06 -080049 for(i = 0; i < Buf.len; i++) {
50 uint8_t Z = ((uint8_t *)Buf.ptr)[i];
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080051 printf("%02x ", Z);
52 }
53 printf("\n");
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -080054
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080055 fflush(stdout);
56}
Laurence Lundbladea2e29072018-12-30 09:20:06 -080057
58/*static void printencoded(const char *szLabel, const uint8_t *pEncoded, size_t nLen)
59{
60 PrintUsefulBufC(szLabel, (UsefulBufC){pEncoded, nLen});
61}*/
Laurence Lundblade20db9c92018-12-17 11:40:37 -080062#endif
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080063
64
Laurence Lundbladeb836efb2018-10-28 20:09:58 +070065static const uint8_t spExpectedEncodedInts[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080066 0x98, 0x2f, 0x3b, 0x7f, 0xff, 0xff, 0xff, 0xff,
67 0xff, 0xff, 0xff, 0x3b, 0x00, 0x00, 0x00, 0x01,
68 0x00, 0x00, 0x00, 0x00, 0x3a, 0xff, 0xff, 0xff,
69 0xff, 0x3a, 0xff, 0xff, 0xff, 0xfe, 0x3a, 0xff,
70 0xff, 0xff, 0xfd, 0x3a, 0x7f, 0xff, 0xff, 0xff,
71 0x3a, 0x7f, 0xff, 0xff, 0xfe, 0x3a, 0x00, 0x01,
72 0x00, 0x01, 0x3a, 0x00, 0x01, 0x00, 0x00, 0x39,
73 0xff, 0xff, 0x39, 0xff, 0xfe, 0x39, 0xff, 0xfd,
74 0x39, 0x01, 0x00, 0x38, 0xff, 0x38, 0xfe, 0x38,
75 0xfd, 0x38, 0x18, 0x37, 0x36, 0x20, 0x00, 0x00,
76 0x01, 0x16, 0x17, 0x18, 0x18, 0x18, 0x19, 0x18,
77 0x1a, 0x18, 0xfe, 0x18, 0xff, 0x19, 0x01, 0x00,
78 0x19, 0x01, 0x01, 0x19, 0xff, 0xfe, 0x19, 0xff,
79 0xff, 0x1a, 0x00, 0x01, 0x00, 0x00, 0x1a, 0x00,
80 0x01, 0x00, 0x01, 0x1a, 0x00, 0x01, 0x00, 0x02,
81 0x1a, 0x7f, 0xff, 0xff, 0xff, 0x1a, 0x7f, 0xff,
82 0xff, 0xff, 0x1a, 0x80, 0x00, 0x00, 0x00, 0x1a,
83 0x80, 0x00, 0x00, 0x01, 0x1a, 0xff, 0xff, 0xff,
84 0xfe, 0x1a, 0xff, 0xff, 0xff, 0xff, 0x1b, 0x00,
85 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x1b,
86 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01,
87 0x1b, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
88 0xff, 0x1b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
89 0xff, 0xff};
90
91
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080092// return CBOR error or -1 if type of value doesn't match
93
94static int IntegerValuesParseTestInternal(QCBORDecodeContext *pDCtx)
95{
96 QCBORItem Item;
97 int nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -080098
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080099 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
100 return nCBORError;
101 if(Item.uDataType != QCBOR_TYPE_ARRAY)
102 return -1;
103
104 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
105 return nCBORError;
106 if(Item.uDataType != QCBOR_TYPE_INT64 || // Todo; fix this for 32-bit machines
Laurence Lundblade570fab52018-10-13 18:28:27 +0800107 Item.val.int64 != -9223372036854775807LL - 1)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800108 return -1;
109
110 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
111 return nCBORError;
112 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade570fab52018-10-13 18:28:27 +0800113 Item.val.int64 != -4294967297)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800114 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800115
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800116 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
117 return nCBORError;
118 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade570fab52018-10-13 18:28:27 +0800119 Item.val.int64 != -4294967296)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800120 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800121
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800122 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
123 return nCBORError;
124 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade570fab52018-10-13 18:28:27 +0800125 Item.val.int64 != -4294967295)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800126 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800127
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800128 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
129 return nCBORError;
130 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade570fab52018-10-13 18:28:27 +0800131 Item.val.int64 != -4294967294)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800132 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800133
134
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800135 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
136 return nCBORError;
137 if(Item.uDataType != QCBOR_TYPE_INT64 ||
138 Item.val.int64 != -2147483648)
139 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800140
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800141 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
142 return nCBORError;
143 if(Item.uDataType != QCBOR_TYPE_INT64 ||
144 Item.val.int64 != -2147483647)
145 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800146
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800147 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
148 return nCBORError;
149 if(Item.uDataType != QCBOR_TYPE_INT64 ||
150 Item.val.int64 != -65538)
151 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800152
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800153 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
154 return nCBORError;
155 if(Item.uDataType != QCBOR_TYPE_INT64 ||
156 Item.val.int64 != -65537)
157 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800158
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800159 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
160 return nCBORError;
161 if(Item.uDataType != QCBOR_TYPE_INT64 ||
162 Item.val.int64 != -65536)
163 return -1;
164
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800165
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800166 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
167 return nCBORError;
168 if(Item.uDataType != QCBOR_TYPE_INT64 ||
169 Item.val.int64 != -65535)
170 return -1;
171
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800172
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800173 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
174 return nCBORError;
175 if(Item.uDataType != QCBOR_TYPE_INT64 ||
176 Item.val.int64 != -65534)
177 return -1;
178
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800179
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800180 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
181 return nCBORError;
182 if(Item.uDataType != QCBOR_TYPE_INT64 ||
183 Item.val.int64 != -257)
184 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800185
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800186 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
187 return nCBORError;
188 if(Item.uDataType != QCBOR_TYPE_INT64 ||
189 Item.val.int64 != -256)
190 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800191
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800192 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
193 return nCBORError;
194 if(Item.uDataType != QCBOR_TYPE_INT64 ||
195 Item.val.int64 != -255)
196 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800197
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800198 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
199 return nCBORError;
200 if(Item.uDataType != QCBOR_TYPE_INT64 ||
201 Item.val.int64 != -254)
202 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800203
204
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800205 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
206 return nCBORError;
207 if(Item.uDataType != QCBOR_TYPE_INT64 ||
208 Item.val.int64 != -25)
209 return -1;
210
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800211
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800212 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
213 return nCBORError;
214 if(Item.uDataType != QCBOR_TYPE_INT64 ||
215 Item.val.int64 != -24)
216 return -1;
217
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800218
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800219 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
220 return nCBORError;
221 if(Item.uDataType != QCBOR_TYPE_INT64 ||
222 Item.val.int64 != -23)
223 return -1;
224
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800225
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800226 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
227 return nCBORError;
228 if(Item.uDataType != QCBOR_TYPE_INT64 ||
229 Item.val.int64 != -1)
230 return -1;
231
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800232
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800233 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
234 return nCBORError;
235 if(Item.uDataType != QCBOR_TYPE_INT64 ||
236 Item.val.int64 != 0)
237 return -1;
238
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800239
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800240 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
241 return nCBORError;
242 if(Item.uDataType != QCBOR_TYPE_INT64 ||
243 Item.val.int64 != 0)
244 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800245
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800246 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
247 return nCBORError;
248 if(Item.uDataType != QCBOR_TYPE_INT64 ||
249 Item.val.int64 != 1)
250 return -1;
251
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800252
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800253 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
254 return nCBORError;
255 if(Item.uDataType != QCBOR_TYPE_INT64 ||
256 Item.val.int64 != 22)
257 return -1;
258
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800259
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800260 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
261 return nCBORError;
262 if(Item.uDataType != QCBOR_TYPE_INT64 ||
263 Item.val.int64 != 23)
264 return -1;
265
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800266
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800267 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
268 return nCBORError;
269 if(Item.uDataType != QCBOR_TYPE_INT64 ||
270 Item.val.int64 != 24)
271 return -1;
272
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800273
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800274 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
275 return nCBORError;
276 if(Item.uDataType != QCBOR_TYPE_INT64 ||
277 Item.val.int64 != 25)
278 return -1;
279
280 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
281 return nCBORError;
282 if(Item.uDataType != QCBOR_TYPE_INT64 ||
283 Item.val.int64 != 26)
284 return -1;
285
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800286
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800287 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
288 return nCBORError;
289 if(Item.uDataType != QCBOR_TYPE_INT64 ||
290 Item.val.int64 != 254)
291 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800292
293
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800294 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
295 return nCBORError;
296 if(Item.uDataType != QCBOR_TYPE_INT64 ||
297 Item.val.int64 != 255)
298 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800299
300
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800301 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
302 return nCBORError;
303 if(Item.uDataType != QCBOR_TYPE_INT64 ||
304 Item.val.int64 != 256)
305 return -1;
306
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800307
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800308 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
309 return nCBORError;
310 if(Item.uDataType != QCBOR_TYPE_INT64 ||
311 Item.val.int64 != 257)
312 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800313
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800314 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
315 return nCBORError;
316 if(Item.uDataType != QCBOR_TYPE_INT64 ||
317 Item.val.int64 != 65534)
318 return -1;
319
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800320
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800321 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
322 return nCBORError;
323 if(Item.uDataType != QCBOR_TYPE_INT64 ||
324 Item.val.int64 != 65535)
325 return -1;
326
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800327
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800328 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
329 return nCBORError;
330 if(Item.uDataType != QCBOR_TYPE_INT64 ||
331 Item.val.int64 != 65536)
332 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800333
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800334 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
335 return nCBORError;
336 if(Item.uDataType != QCBOR_TYPE_INT64 ||
337 Item.val.int64 != 65537)
338 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800339
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800340 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
341 return nCBORError;
342 if(Item.uDataType != QCBOR_TYPE_INT64 ||
343 Item.val.int64 != 65538)
344 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800345
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800346 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
347 return nCBORError;
348 if(Item.uDataType != QCBOR_TYPE_INT64 ||
349 Item.val.int64 != 2147483647)
350 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800351
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800352 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
353 return nCBORError;
354 if(Item.uDataType != QCBOR_TYPE_INT64 ||
355 Item.val.int64 != 2147483647)
356 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800357
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800358 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
359 return nCBORError;
360 if(Item.uDataType != QCBOR_TYPE_INT64 ||
361 Item.val.int64 != 2147483648)
362 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800363
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800364 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
365 return nCBORError;
366 if(Item.uDataType != QCBOR_TYPE_INT64 ||
367 Item.val.int64 != 2147483649)
368 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800369
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800370 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
371 return nCBORError;
372 if(Item.uDataType != QCBOR_TYPE_INT64 ||
373 Item.val.int64 != 4294967294)
374 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800375
376
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800377 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
378 return nCBORError;
379 if(Item.uDataType != QCBOR_TYPE_INT64 ||
380 Item.val.int64 != 4294967295)
381 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800382
383
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800384 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
385 return nCBORError;
386 if(Item.uDataType != QCBOR_TYPE_INT64 ||
387 Item.val.int64 != 4294967296)
388 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800389
390
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800391 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
392 return nCBORError;
393 if(Item.uDataType != QCBOR_TYPE_INT64 ||
394 Item.val.int64 != 4294967297)
395 return -1;
396
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800397
398
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800399 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
400 return nCBORError;
401 if(Item.uDataType != QCBOR_TYPE_INT64 ||
402 Item.val.int64 != 9223372036854775807LL)
403 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800404
405
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800406 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
407 return nCBORError;
408 if(Item.uDataType != QCBOR_TYPE_UINT64 ||
409 Item.val.uint64 != 18446744073709551615ULL)
410 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800411
412
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800413 if(QCBORDecode_Finish(pDCtx) != QCBOR_SUCCESS) {
414 return -1;
415 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800416
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800417 return 0;
418}
419
420
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800421/*
422 Tests the decoding of lots of different integers sizes
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +0800423 and values.
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800424 */
425
426int IntegerValuesParseTest()
427{
428 int n;
429 QCBORDecodeContext DCtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800430
Laurence Lundbladeb836efb2018-10-28 20:09:58 +0700431 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedEncodedInts), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800432
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800433 n = IntegerValuesParseTestInternal(&DCtx);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800434
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800435 return(n);
436}
437
438
439/*
440 Creates a simple CBOR array and returns it in *pEncoded. The array is malloced
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800441 and needs to be freed. This is used by several tests.
442
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800443 Two of the inputs can be set. Two other items in the array are fixed.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800444
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800445 */
446
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -0800447static uint8_t spSimpleArrayBuffer[50];
448
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800449static int CreateSimpleArray(int nInt1, int nInt2, uint8_t **pEncoded, size_t *pEncodedLen)
450{
451 QCBOREncodeContext ECtx;
452 int nReturn = -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800453
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800454 *pEncoded = NULL;
455 *pEncodedLen = INT32_MAX;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800456
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800457 // loop runs CBOR encoding twice. First with no buffer to
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -0800458 // calculate the length so buffer can be allocated correctly,
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800459 // and last with the buffer to do the actual encoding
460 do {
Laurence Lundblade0595e932018-11-02 22:22:47 +0700461 QCBOREncode_Init(&ECtx, (UsefulBuf){*pEncoded, *pEncodedLen});
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800462 QCBOREncode_OpenArray(&ECtx);
463 QCBOREncode_AddInt64(&ECtx, nInt1);
464 QCBOREncode_AddInt64(&ECtx, nInt2);
465 QCBOREncode_AddBytes(&ECtx, ((UsefulBufC) {"galactic", 8}));
466 QCBOREncode_AddBytes(&ECtx, ((UsefulBufC) {"haven token", 11}));
467 QCBOREncode_CloseArray(&ECtx);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800468
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -0800469 if(QCBOREncode_FinishGetSize(&ECtx, pEncodedLen))
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800470 goto Done;
471
472 if(*pEncoded != NULL) {
473 nReturn = 0;
474 goto Done;
475 }
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -0800476
477 // Use static buffer to avoid dependency on malloc()
478 if(*pEncodedLen > sizeof(spSimpleArrayBuffer)) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800479 goto Done;
480 }
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -0800481 *pEncoded = spSimpleArrayBuffer;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800482
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800483 } while(1);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800484
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -0800485Done:
486 return nReturn;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800487}
488
489
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800490/*
491 {"first integer": 42,
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900492 "an array of two strings": [
493 "string1", "string2"
494 ],
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800495 "map in a map": {
496 "bytes 1": h'78787878',
497 "bytes 2": h'79797979',
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900498 "another int": 98,
499 "text 2": "lies, damn lies and statistics"
500 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800501 }
502 */
503
504static uint8_t pValidMapEncoded[] = {
Laurence Lundbladeb836efb2018-10-28 20:09:58 +0700505 0xa3, 0x6d, 0x66, 0x69, 0x72, 0x73, 0x74, 0x20, 0x69, 0x6e,
506 0x74, 0x65, 0x67, 0x65, 0x72, 0x18, 0x2a, 0x77, 0x61, 0x6e,
507 0x20, 0x61, 0x72, 0x72, 0x61, 0x79, 0x20, 0x6f, 0x66, 0x20,
508 0x74, 0x77, 0x6f, 0x20, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67,
509 0x73, 0x82, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x31,
510 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x32, 0x6c, 0x6d,
511 0x61, 0x70, 0x20, 0x69, 0x6e, 0x20, 0x61, 0x20, 0x6d, 0x61,
512 0x70, 0xa4, 0x67, 0x62, 0x79, 0x74, 0x65, 0x73, 0x20, 0x31,
513 0x44, 0x78, 0x78, 0x78, 0x78, 0x67, 0x62, 0x79, 0x74, 0x65,
514 0x73, 0x20, 0x32, 0x44, 0x79, 0x79, 0x79, 0x79, 0x6b, 0x61,
515 0x6e, 0x6f, 0x74, 0x68, 0x65, 0x72, 0x20, 0x69, 0x6e, 0x74,
516 0x18, 0x62, 0x66, 0x74, 0x65, 0x78, 0x74, 0x20, 0x32, 0x78,
517 0x1e, 0x6c, 0x69, 0x65, 0x73, 0x2c, 0x20, 0x64, 0x61, 0x6d,
518 0x6e, 0x20, 0x6c, 0x69, 0x65, 0x73, 0x20, 0x61, 0x6e, 0x64,
519 0x20, 0x73, 0x74, 0x61, 0x74, 0x69, 0x73, 0x74, 0x69, 0x63,
520 0x73 } ;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800521
522static 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)
523{
524 QCBORDecodeContext DCtx;
525 QCBORItem Item;
526 int nReturn = -1; // assume error until success
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800527
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800528 QCBORDecode_Init(&DCtx, (UsefulBufC){pEncoded, nLen}, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800529
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800530 // Make sure the first thing is a map
531 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || Item.uDataType != QCBOR_TYPE_ARRAY)
532 goto Done;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800533
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800534 // First integer
Laurence Lundblade12b495d2018-12-17 11:15:54 -0800535 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || Item.uDataType != QCBOR_TYPE_INT64)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800536 goto Done;
537 *pInt1 = Item.val.int64;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800538
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800539 // Second integer
540 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || Item.uDataType != QCBOR_TYPE_INT64)
541 goto Done;
542 *pInt2 = Item.val.int64;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800543
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800544 // First string
545 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || Item.uDataType != QCBOR_TYPE_BYTE_STRING)
546 goto Done;
547 *pBuf3 = Item.val.string.ptr;
548 *pBuf3Len = Item.val.string.len;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800549
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800550 // Second string
551 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || Item.uDataType != QCBOR_TYPE_BYTE_STRING)
552 goto Done;
553 *pBuf4 = Item.val.string.ptr;
554 *pBuf4Len = Item.val.string.len;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800555
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800556 nReturn = 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800557
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800558Done:
559 return(nReturn);
560}
561
562
563
Laurence Lundblade9e3651c2018-10-10 11:49:55 +0800564
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800565int SimpleArrayTest()
566{
567 uint8_t *pEncoded;
568 size_t nEncodedLen;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800569
Laurence Lundblade5e390822019-01-06 12:35:01 -0800570 int64_t i1=0, i2=0;
571 size_t i3=0, i4=0;
572 const uint8_t *s3= (uint8_t *)"";
573 const uint8_t *s4= (uint8_t *)"";
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800574
575
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800576 if(CreateSimpleArray(23, 6000, &pEncoded, &nEncodedLen) < 0) {
577 return(-1);
578 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800579
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800580 ParseOrderedArray(pEncoded, nEncodedLen, &i1, &i2, &s3, &i3, &s4, &i4);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800581
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800582 if(i1 != 23 ||
583 i2 != 6000 ||
584 i3 != 8 ||
585 i4 != 11 ||
Laurence Lundbladea44d5062018-10-17 18:45:12 +0530586 memcmp("galactic", s3, 8) !=0 ||
587 memcmp("haven token", s4, 11) !=0) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800588 return(-1);
589 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800590
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800591 return(0);
592}
593
594
Laurence Lundblade9e3651c2018-10-10 11:49:55 +0800595
Laurence Lundbladeb836efb2018-10-28 20:09:58 +0700596static uint8_t spDeepArrays[] = {0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x80};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800597
598int ParseDeepArrayTest()
599{
600 QCBORDecodeContext DCtx;
601 int nReturn = 0;
602 int i;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800603
Laurence Lundbladeb836efb2018-10-28 20:09:58 +0700604 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spDeepArrays), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800605
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800606 for(i = 0; i < 10; i++) {
607 QCBORItem Item;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800608
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800609 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
610 Item.uDataType != QCBOR_TYPE_ARRAY ||
611 Item.uNestingLevel != i) {
612 nReturn = -1;
613 break;
614 }
615 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800616
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800617 return(nReturn);
618}
619
Laurence Lundblade972e59c2018-11-11 15:57:23 +0700620// Big enough to test nesting to the depth of 24
621static uint8_t spTooDeepArrays[] = {0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81,
622 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81,
623 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81,
624 0x80};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800625
626int ParseTooDeepArrayTest()
627{
628 QCBORDecodeContext DCtx;
629 int nReturn = 0;
630 int i;
631 QCBORItem Item;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800632
633
Laurence Lundbladeb836efb2018-10-28 20:09:58 +0700634 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTooDeepArrays), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800635
Laurence Lundblade972e59c2018-11-11 15:57:23 +0700636 for(i = 0; i < QCBOR_MAX_ARRAY_NESTING1; i++) {
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800637
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800638 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
639 Item.uDataType != QCBOR_TYPE_ARRAY ||
640 Item.uNestingLevel != i) {
641 nReturn = -1;
642 break;
643 }
644 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800645
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800646 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_ARRAY_NESTING_TOO_DEEP)
647 nReturn = -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800648
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800649 return(nReturn);
650}
651
652
653
654
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800655int ShortBufferParseTest()
656{
657 int nResult = 0;
658 QCBORDecodeContext DCtx;
659 int num;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800660
Laurence Lundbladeb836efb2018-10-28 20:09:58 +0700661 for(num = sizeof(spExpectedEncodedInts)-1; num; num--) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800662 int n;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800663
Laurence Lundbladeb836efb2018-10-28 20:09:58 +0700664 QCBORDecode_Init(&DCtx, (UsefulBufC){spExpectedEncodedInts, num}, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800665
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800666 n = IntegerValuesParseTestInternal(&DCtx);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800667
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800668 //printf("Len %d, result: %d\n", num, n);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800669
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800670 if(n != QCBOR_ERR_HIT_END) {
671 nResult = -1;
672 goto Done;
673 }
674 }
675Done:
676 return nResult;
677}
678
679
Laurence Lundblade9e3651c2018-10-10 11:49:55 +0800680
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800681int ShortBufferParseTest2()
682{
683 uint8_t *pEncoded;
684 int nReturn;
685 size_t nEncodedLen;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800686
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800687 int64_t i1, i2;
688 size_t i3, i4;
689 const uint8_t *s3, *s4;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800690
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800691 nReturn = 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800692
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800693 if(CreateSimpleArray(23, 6000, &pEncoded, &nEncodedLen) < 0) {
694 return(-1);
695 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800696
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800697 for(nEncodedLen--; nEncodedLen; nEncodedLen--) {
698 int nResult = ParseOrderedArray(pEncoded, (uint32_t)nEncodedLen, &i1, &i2, &s3, &i3, &s4, &i4);
699 if(nResult == 0) {
700 nReturn = -1;
701 }
702 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800703
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800704 return(nReturn);
705}
706
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530707/*
708 Decode and thoroughly check a moderately complex
709 set of maps
710 */
Laurence Lundbladeea567ac2018-12-09 14:03:21 -0800711static int ParseMapTest1(QCBORDecodeMode nMode)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800712{
713 QCBORDecodeContext DCtx;
714 QCBORItem Item;
715 int nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800716
Laurence Lundbladeea567ac2018-12-09 14:03:21 -0800717 QCBORDecode_Init(&DCtx, (UsefulBufC){pValidMapEncoded, sizeof(pValidMapEncoded)}, nMode);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800718
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900719 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800720 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900721 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800722 if(Item.uDataType != QCBOR_TYPE_MAP ||
723 Item.val.uCount != 3)
724 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800725
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900726 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800727 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900728 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800729 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800730 Item.uDataType != QCBOR_TYPE_INT64 ||
731 Item.val.int64 != 42 ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530732 Item.uDataAlloc ||
733 Item.uLabelAlloc ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900734 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("first integer"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800735 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900736 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800737
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900738 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800739 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900740 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800741 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530742 Item.uDataAlloc ||
743 Item.uLabelAlloc ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900744 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("an array of two strings")) ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800745 Item.uDataType != QCBOR_TYPE_ARRAY ||
746 Item.val.uCount != 2)
747 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800748
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900749 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800750 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900751 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800752 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530753 Item.uDataAlloc ||
754 Item.uLabelAlloc ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900755 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("string1"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800756 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900757 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800758
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900759 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800760 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900761 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800762 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530763 Item.uDataAlloc ||
764 Item.uLabelAlloc ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900765 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("string2"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800766 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900767 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800768
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900769 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800770 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900771 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800772 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530773 Item.uDataAlloc ||
774 Item.uLabelAlloc ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900775 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("map in a map")) ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800776 Item.uDataType != QCBOR_TYPE_MAP ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900777 Item.val.uCount != 4) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800778 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900779 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800780
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900781 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800782 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900783 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800784 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900785 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("bytes 1"))||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800786 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530787 Item.uDataAlloc ||
788 Item.uLabelAlloc ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900789 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("xxxx"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800790 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900791 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800792
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900793 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800794 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900795 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800796 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900797 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("bytes 2")) ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800798 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530799 Item.uDataAlloc ||
800 Item.uLabelAlloc ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900801 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("yyyy"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800802 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900803 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800804
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900805 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800806 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900807 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800808 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530809 Item.uDataAlloc ||
810 Item.uLabelAlloc ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900811 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("another int")) ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800812 Item.uDataType != QCBOR_TYPE_INT64 ||
813 Item.val.int64 != 98)
814 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800815
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900816 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800817 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900818 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800819 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900820 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("text 2"))||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800821 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530822 Item.uDataAlloc ||
823 Item.uLabelAlloc ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900824 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("lies, damn lies and statistics"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800825 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900826 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800827
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800828 return 0;
829}
830
831
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900832/*
833 Decode and thoroughly check a moderately complex
834 set of maps
835 */
836int ParseMapAsArrayTest()
837{
838 QCBORDecodeContext DCtx;
839 QCBORItem Item;
840 int nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800841
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900842 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), QCBOR_DECODE_MODE_MAP_AS_ARRAY);
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 }
Laurence Lundbladed61cbf32018-12-09 11:42:21 -0800847 if(Item.uDataType != QCBOR_TYPE_MAP_AS_ARRAY ||
848 Item.val.uCount != 6) {
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900849 return -1;
850 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800851
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900852 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
853 return nCBORError;
854 }
855 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
856 Item.uDataAlloc ||
857 Item.uLabelAlloc ||
858 Item.uLabelType != QCBOR_TYPE_NONE ||
859 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("first integer"))) {
860 return -2;
861 }
862
863 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
864 return nCBORError;
865 }
866 if(Item.uLabelType != QCBOR_TYPE_NONE ||
867 Item.uDataType != QCBOR_TYPE_INT64 ||
868 Item.val.int64 != 42 ||
869 Item.uDataAlloc ||
870 Item.uLabelAlloc) {
871 return -3;
872 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800873
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900874 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
875 return nCBORError;
876 }
877 if(Item.uLabelType != QCBOR_TYPE_NONE ||
878 Item.uDataAlloc ||
879 Item.uLabelAlloc ||
880 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("an array of two strings")) ||
881 Item.uDataType != QCBOR_TYPE_TEXT_STRING) {
882 return -4;
883 }
884
885 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
886 return nCBORError;
887 }
888 if(Item.uLabelType != QCBOR_TYPE_NONE ||
889 Item.uDataAlloc ||
890 Item.uLabelAlloc ||
891 Item.uDataType != QCBOR_TYPE_ARRAY ||
892 Item.val.uCount != 2) {
893 return -5;
894 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800895
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900896 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
897 return nCBORError;
898 }
899 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
900 Item.val.string.len != 7 ||
901 Item.uDataAlloc ||
902 Item.uLabelAlloc ||
903 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("string1"))) {
904 return -6;
905 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800906
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900907 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
908 return nCBORError;
909 }
910 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
911 Item.uDataAlloc ||
912 Item.uLabelAlloc ||
913 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("string2"))) {
914 return -7;
915 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800916
917
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900918 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
919 return nCBORError;
920 }
921 if(Item.uLabelType != QCBOR_TYPE_NONE ||
922 Item.uDataAlloc ||
923 Item.uLabelAlloc ||
924 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("map in a map"))) {
925 return -8;
926 }
927
928 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
929 return nCBORError;
930 }
931 if(Item.uLabelType != QCBOR_TYPE_NONE ||
932 Item.uDataAlloc ||
933 Item.uLabelAlloc ||
Laurence Lundbladed61cbf32018-12-09 11:42:21 -0800934 Item.uDataType != QCBOR_TYPE_MAP_AS_ARRAY ||
935 Item.val.uCount != 8) {
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900936 return -9;
937 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800938
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900939 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
940 return nCBORError;
941 }
942 if(Item.uLabelType != QCBOR_TYPE_NONE ||
943 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("bytes 1"))||
944 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
945 Item.uDataAlloc ||
946 Item.uLabelAlloc) {
947 return -10;
948 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800949
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900950 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
951 return nCBORError;
952 }
953 if(Item.uLabelType != QCBOR_TYPE_NONE ||
954 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
955 Item.uDataAlloc ||
956 Item.uLabelAlloc ||
957 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("xxxx"))) {
958 return -11;
959 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800960
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900961 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
962 return nCBORError;
963 }
964 if(Item.uLabelType != QCBOR_TYPE_NONE ||
965 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("bytes 2")) ||
966 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
967 Item.uDataAlloc ||
968 Item.uLabelAlloc) {
969 return -12;
970 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800971
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900972 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
973 return nCBORError;
974 }
975 if(Item.uLabelType != QCBOR_TYPE_NONE ||
976 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
977 Item.uDataAlloc ||
978 Item.uLabelAlloc ||
979 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("yyyy"))) {
980 return -13;
981 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800982
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900983 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
984 return nCBORError;
985 }
986 if(Item.uLabelType != QCBOR_TYPE_NONE ||
987 Item.uDataAlloc ||
988 Item.uLabelAlloc ||
989 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("another int")) ||
990 Item.uDataType != QCBOR_TYPE_TEXT_STRING) {
991 return -14;
992 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800993
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900994 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
995 return nCBORError;
996 }
997 if(Item.uLabelType != QCBOR_TYPE_NONE ||
998 Item.uDataAlloc ||
999 Item.uLabelAlloc ||
1000 Item.uDataType != QCBOR_TYPE_INT64 ||
1001 Item.val.int64 != 98) {
1002 return -15;
1003 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001004
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001005 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
1006 return nCBORError;
1007 }
1008 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1009 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("text 2"))||
1010 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1011 Item.uDataAlloc ||
1012 Item.uLabelAlloc) {
1013 return -16;
1014 }
1015
1016 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
1017 return nCBORError;
1018 }
1019 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1020 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1021 Item.uDataAlloc ||
1022 Item.uLabelAlloc ||
1023 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("lies, damn lies and statistics"))) {
1024 return -17;
1025 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001026
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001027 return 0;
1028}
1029
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001030
1031/*
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301032 Fully or partially decode pValidMapEncoded. When
1033 partially decoding check for the right error code.
1034 How much partial decoding depends on nLevel.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001035
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301036 The partial decodes test error conditions of
1037 incomplete encoded input.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001038
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301039 This could be combined with the above test
1040 and made prettier and maybe a little more
1041 thorough.
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001042 */
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001043static int ExtraBytesTest(int nLevel)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001044{
1045 QCBORDecodeContext DCtx;
1046 QCBORItem Item;
1047 int nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001048
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001049 QCBORDecode_Init(&DCtx, (UsefulBufC){pValidMapEncoded, sizeof(pValidMapEncoded)}, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001050
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001051 if(nLevel < 1) {
1052 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_EXTRA_BYTES) {
1053 return -1;
1054 } else {
1055 return 0;
1056 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001057 }
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301058
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001059
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001060 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001061 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001062 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001063 if(Item.uDataType != QCBOR_TYPE_MAP ||
1064 Item.val.uCount != 3)
1065 return -1;
1066
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001067 if(nLevel < 2) {
1068 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
1069 return -1;
1070 } else {
1071 return 0;
1072 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001073 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001074
1075
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001076 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001077 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001078 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001079 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001080 Item.uDataType != QCBOR_TYPE_INT64 ||
1081 Item.val.uCount != 42 ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001082 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("first integer"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001083 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001084 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001085
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001086 if(nLevel < 3) {
1087 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
1088 return -1;
1089 } else {
1090 return 0;
1091 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001092 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001093
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001094 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001095 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001096 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001097 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001098 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("an array of two strings")) ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001099 Item.uDataType != QCBOR_TYPE_ARRAY ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001100 Item.val.uCount != 2) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001101 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001102 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001103
1104
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001105 if(nLevel < 4) {
1106 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
1107 return -1;
1108 } else {
1109 return 0;
1110 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001111 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001112
1113
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001114 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001115 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001116 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001117 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001118 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("string1"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001119 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001120 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001121
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001122 if(nLevel < 5) {
1123 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
1124 return -1;
1125 } else {
1126 return 0;
1127 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001128 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001129
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001130 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001131 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001132 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001133 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001134 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("string2"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001135 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001136 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001137
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001138 if(nLevel < 6) {
1139 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
1140 return -1;
1141 } else {
1142 return 0;
1143 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001144 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001145
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001146 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001147 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001148 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001149 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001150 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("map in a map")) ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001151 Item.uDataType != QCBOR_TYPE_MAP ||
1152 Item.val.uCount != 4)
1153 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001154
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001155 if(nLevel < 7) {
1156 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
1157 return -1;
1158 } else {
1159 return 0;
1160 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001161 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001162
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001163 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001164 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001165 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001166 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001167 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("bytes 1")) ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001168 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001169 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("xxxx"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001170 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001171 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001172
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001173 if(nLevel < 8) {
1174 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
1175 return -1;
1176 } else {
1177 return 0;
1178 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001179 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001180
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001181 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001182 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001183 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001184 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001185 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("bytes 2")) ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001186 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001187 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("yyyy"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001188 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001189 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001190
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001191 if(nLevel < 9) {
1192 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
1193 return -1;
1194 } else {
1195 return 0;
1196 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001197 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001198
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001199 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001200 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001201 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001202 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001203 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("another int")) ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001204 Item.uDataType != QCBOR_TYPE_INT64 ||
1205 Item.val.int64 != 98)
1206 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001207
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001208 if(nLevel < 10) {
1209 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
1210 return -1;
1211 } else {
1212 return 0;
1213 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001214 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001215
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001216 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001217 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001218 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001219 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001220 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("text 2"))||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001221 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001222 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("lies, damn lies and statistics"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001223 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001224 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001225
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301226 if(QCBORDecode_Finish(&DCtx)) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001227 return -1;
1228 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001229
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001230 return 0;
1231}
1232
1233
1234
1235
1236int ParseMapTest()
1237{
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301238 // Parse a moderatly complex map structure very thoroughl
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08001239 int n = ParseMapTest1(QCBOR_DECODE_MODE_NORMAL);
1240
1241 n = ParseMapTest1(QCBOR_DECODE_MODE_MAP_STRINGS_ONLY);
1242
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001243 if(!n) {
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001244 for(int i = 0; i < 10; i++) {
1245 n = ExtraBytesTest(i);
1246 if(n) {
1247 break;
1248 }
1249 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001250 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001251
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001252 return(n);
1253}
1254
1255
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07001256static 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 +08001257
1258int ParseSimpleTest()
1259{
1260 QCBORDecodeContext DCtx;
1261 QCBORItem Item;
1262 int nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001263
1264
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07001265 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSimpleValues), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001266
1267
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001268 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1269 return nCBORError;
1270 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
1271 Item.val.uCount != 10)
1272 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001273
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001274 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1275 return nCBORError;
1276 if(Item.uDataType != QCBOR_TYPE_FALSE)
1277 return -1;
1278
1279 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1280 return nCBORError;
1281 if(Item.uDataType != QCBOR_TYPE_TRUE)
1282 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001283
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001284 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1285 return nCBORError;
1286 if(Item.uDataType != QCBOR_TYPE_NULL)
1287 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001288
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001289 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1290 return nCBORError;
1291 if(Item.uDataType != QCBOR_TYPE_UNDEF)
1292 return -1;
1293
1294 // A break
Laurence Lundblade9e3651c2018-10-10 11:49:55 +08001295 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_BREAK)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001296 return -1;
1297
1298 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1299 return nCBORError;
1300 if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 0)
1301 return -1;
1302
1303 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1304 return nCBORError;
1305 if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 19)
1306 return -1;
1307
1308 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_INVALID_CBOR)
1309 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001310
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001311 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_INVALID_CBOR)
1312 return -1;
1313
1314 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_INVALID_CBOR)
1315 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001316
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001317 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1318 return nCBORError;
1319 if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 32)
1320 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001321
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001322 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1323 return nCBORError;
1324 if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 255)
1325 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001326
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001327 return 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001328
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001329}
1330
1331
1332struct FailInput {
1333 UsefulBufC Input;
1334 int nError;
1335};
1336
1337
1338struct FailInput Failures[] = {
1339 { {(uint8_t[]){0x18}, 1}, QCBOR_ERR_HIT_END }, // 1 byte integer missing the byte
1340 { {(uint8_t[]){0x1c}, 1}, QCBOR_ERR_UNSUPPORTED }, // Reserved additional info = 28
1341 { {(uint8_t[]){0x1d}, 1}, QCBOR_ERR_UNSUPPORTED }, // Reserved additional info = 29
1342 { {(uint8_t[]){0x1e}, 1}, QCBOR_ERR_UNSUPPORTED }, // Reserved additional info = 30
1343 { {(uint8_t[]){0x1f}, 1}, QCBOR_ERR_UNSUPPORTED }, // Indefinite length integer
1344 { {(uint8_t[]){0x3c}, 1}, QCBOR_ERR_UNSUPPORTED }, // 1 byte integer missing the byte
1345 { {(uint8_t[]){0x3d}, 1}, QCBOR_ERR_UNSUPPORTED }, // 1 byte integer missing the byte
1346 { {(uint8_t[]){0x3e}, 1}, QCBOR_ERR_UNSUPPORTED }, // 1 byte integer missing the byte
1347 { {(uint8_t[]){0x3f}, 1}, QCBOR_ERR_UNSUPPORTED }, // Indefinite length negative integer
1348 { {(uint8_t[]){0x41}, 1}, QCBOR_ERR_HIT_END }, // Short byte string
1349 { {(uint8_t[]){0x5c}, 1}, QCBOR_ERR_UNSUPPORTED }, // Reserved additional info = 28
1350 { {(uint8_t[]){0x5f}, 1}, QCBOR_ERR_UNSUPPORTED }, // Indefinite length byte string
1351 { {(uint8_t[]){0x61}, 1}, QCBOR_ERR_HIT_END }, // Short UTF-8 string
1352 { {(uint8_t[]){0x7c}, 1}, QCBOR_ERR_UNSUPPORTED }, // Reserved additional info = 28
1353 { {(uint8_t[]){0x7f}, 1}, QCBOR_ERR_UNSUPPORTED }, // Indefinite length UTF-8 string
1354 { {(uint8_t[]){0xff}, 1}, QCBOR_ERR_UNSUPPORTED } , // break
1355 { {(uint8_t[]){0xf8, 0x00}, 2}, QCBOR_ERR_INVALID_CBOR }, // An invalid encoding of a simple type
1356 { {(uint8_t[]){0xf8, 0x1f}, 2}, QCBOR_ERR_INVALID_CBOR }, // An invalid encoding of a simple type
1357 { {(uint8_t[]){0xc0, 0x00}, 2}, QCBOR_ERR_BAD_OPT_TAG }, // Text-based date, with an integer
1358 { {(uint8_t[]){0xc1, 0x41, 0x33}, 3}, QCBOR_ERR_BAD_OPT_TAG }, // Epoch date, with an byte string
1359 { {(uint8_t[]){0xc1, 0xc0, 0x00}, 3}, QCBOR_ERR_BAD_OPT_TAG }, // tagged as both epoch and string dates
1360 { {(uint8_t[]){0xc2, 0x00}, 2}, QCBOR_ERR_BAD_OPT_TAG } // big num tagged an int, not a byte string
1361
1362};
1363
1364
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001365int FailureTests()
1366{
1367 int nResult = 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001368
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001369 struct FailInput *pFEnd = &Failures[0] + sizeof(Failures)/sizeof(struct FailInput);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001370
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001371 for(struct FailInput *pF = &Failures[0]; pF < pFEnd ;pF++) {
1372 QCBORDecodeContext DCtx;
1373 QCBORItem Item;
1374 int nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001375
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001376 QCBORDecode_Init(&DCtx, pF->Input, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001377
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001378 while(1) {
1379 nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
1380 if(QCBOR_ERR_HIT_END == nCBORError) {
1381 break;
1382 }
1383 if(nCBORError != pF->nError) {
1384 nResult = 1;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001385 break;
1386 }
1387 }
1388 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001389
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001390 {
1391 QCBORDecodeContext DCtx;
1392 QCBORItem Item;
1393 int nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001394
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07001395 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSimpleValues), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001396
1397 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1398 return nCBORError;
1399 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
1400 Item.val.uCount != 10)
1401 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001402
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001403 DCtx.InBuf.magic = 0; // Corrupt the UsefulInputBuf
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001404
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001405 nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
1406 if(nCBORError != QCBOR_ERR_HIT_END)
1407 return -1;
1408 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001409
1410
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001411 return nResult;
1412}
1413
1414
Laurence Lundbladea2e29072018-12-30 09:20:06 -08001415/* Try all 256 values of the byte at nLen including recursing for
1416 each of the values to try values at nLen+1 ... up to nLenMax
1417 */
1418static void ComprehensiveInputRecurser(uint8_t *pBuf, int nLen, int nLenMax)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001419{
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001420 if(nLen >= nLenMax) {
1421 return;
1422 }
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08001423
Laurence Lundbladea2e29072018-12-30 09:20:06 -08001424 for(int inputByte = 0; inputByte < 256; inputByte++) {
1425 // Set up the input
1426 pBuf[nLen] = inputByte;
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08001427 const UsefulBufC Input = {pBuf, nLen+1};
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08001428
Laurence Lundbladea2e29072018-12-30 09:20:06 -08001429 // Get ready to parse
1430 QCBORDecodeContext DCtx;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001431 QCBORDecode_Init(&DCtx, Input, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001432
Laurence Lundbladea2e29072018-12-30 09:20:06 -08001433 // Parse by getting the next item until an error occurs
1434 // Just about every possible decoder error can occur here
1435 // The goal of this test is not to check for the correct
1436 // error since that is not really possible. It is to
1437 // see that there is no crash on hostile input.
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001438 while(1) {
Laurence Lundbladea2e29072018-12-30 09:20:06 -08001439 QCBORItem Item;
1440 QCBORError nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001441 if(nCBORError != QCBOR_SUCCESS) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001442 break;
1443 }
1444 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001445
Laurence Lundbladea2e29072018-12-30 09:20:06 -08001446 ComprehensiveInputRecurser(pBuf, nLen+1, nLenMax);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001447 }
1448}
1449
1450
1451/*
Laurence Lundbladea2e29072018-12-30 09:20:06 -08001452 Public function for initialization. See header qcbor.h
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001453 */
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001454int ComprehensiveInputTest()
1455{
Laurence Lundbladea2e29072018-12-30 09:20:06 -08001456 // Size 2 tests 64K inputs and runs quickly
1457 uint8_t pBuf[2];
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001458
Laurence Lundbladea2e29072018-12-30 09:20:06 -08001459 ComprehensiveInputRecurser(pBuf, 0, sizeof(pBuf));
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001460
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001461 return 0;
1462}
1463
Laurence Lundbladea2e29072018-12-30 09:20:06 -08001464
1465/*
1466 Public function for initialization. See header qcbor.h
1467 */
1468int BigComprehensiveInputTest()
1469{
1470 // size 3 tests 16 million inputs and runs OK
1471 // in seconds on fast machines. Size 4 takes
1472 // 10+ minutes and 5 half a day on fast
1473 // machines. This test is kept separate from
1474 // the others so as to no slow down the use
1475 // of them as a very frequent regression.
1476 uint8_t pBuf[3]; //
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08001477
Laurence Lundbladea2e29072018-12-30 09:20:06 -08001478 ComprehensiveInputRecurser(pBuf, 0, sizeof(pBuf));
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08001479
Laurence Lundbladea2e29072018-12-30 09:20:06 -08001480 return 0;
1481}
1482
1483
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07001484static uint8_t spDateTestInput[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001485 0xc0, // tag for string date
1486 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001487
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001488 0xc1, // tag for epoch date
1489 0x1a, 0x53, 0x72, 0x4E, 0x00, // Epoch date 1400000000; Tue, 13 May 2014 16:53:20 GMT
1490
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001491 // CBOR_TAG_B64
1492 0xc1, 0xcf, 0xd8, 0x22, // 0xee, // Epoch date with extra tags TODO: fix this test
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001493 0x1a, 0x53, 0x72, 0x4E, 0x01,
1494
1495 0xc1, // tag for epoch date
1496 0x1b, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, // Too large integer
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001497
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001498 0xc1, // tag for epoch date
1499 0xfa, 0x3f, 0x8c, 0xcc, 0xcd, // double with value 1.1
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001500
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001501 0xc1, // tag for epoch date
1502 0xfa, 0x7f, 0x7f, 0xff, 0xff // 3.4028234663852886e+38 too large
1503
1504};
1505
1506
1507// have to check float expected only to within an epsilon
1508int CHECK_EXPECTED_DOUBLE(double val, double expected) {
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001509
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001510 double diff = val - expected;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001511
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001512 diff = fabs(diff);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001513
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001514 return diff > 0.0000001;
1515}
1516
1517
1518int DateParseTest()
1519{
1520 QCBORDecodeContext DCtx;
1521 QCBORItem Item;
1522 int nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001523
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07001524 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spDateTestInput), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001525
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001526 const uint64_t uTags[] = {15};
1527 QCBORTagListIn TagList = {1, uTags};
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001528
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001529 QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TagList);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001530
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001531 // String date
1532 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1533 return -1;
1534 if(Item.uDataType != QCBOR_TYPE_DATE_STRING ||
Laurence Lundblade9e3651c2018-10-10 11:49:55 +08001535 UsefulBuf_Compare(Item.val.dateString, UsefulBuf_FromSZ("1985-04-12"))){
Laurence Lundblade67bd5512018-11-02 21:44:06 +07001536 return -2;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001537 }
1538
1539 // Epoch date
1540 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade67bd5512018-11-02 21:44:06 +07001541 return -3;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001542 if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH ||
1543 Item.val.epochDate.nSeconds != 1400000000 ||
1544 Item.val.epochDate.fSecondsFraction != 0 ) {
Laurence Lundblade67bd5512018-11-02 21:44:06 +07001545 return -4;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001546 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001547
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001548 // Epoch date with extra CBOR_TAG_B64 tag that doesn't really mean anything
1549 // but want to be sure extra tag doesn't cause a problem
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001550 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade67bd5512018-11-02 21:44:06 +07001551 return -5;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001552 if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH ||
1553 Item.val.epochDate.nSeconds != 1400000001 ||
1554 Item.val.epochDate.fSecondsFraction != 0 ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001555 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_B64)) {
Laurence Lundblade67bd5512018-11-02 21:44:06 +07001556 return -6;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001557 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001558
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001559 // Epoch date that is too large for our representation
1560 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_DATE_OVERFLOW) {
Laurence Lundblade67bd5512018-11-02 21:44:06 +07001561 return -7;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001562 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001563
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001564 // Epoch date in float format with fractional seconds
1565 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade67bd5512018-11-02 21:44:06 +07001566 return -8;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001567 if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH ||
1568 Item.val.epochDate.nSeconds != 1 ||
1569 CHECK_EXPECTED_DOUBLE(Item.val.epochDate.fSecondsFraction, 0.1 )) {
Laurence Lundblade67bd5512018-11-02 21:44:06 +07001570 return -9;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001571 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001572
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001573 // Epoch date float that is too large for our representation
1574 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_DATE_OVERFLOW) {
Laurence Lundblade67bd5512018-11-02 21:44:06 +07001575 return -10;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001576 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001577
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001578 // TODO: could use a few more tests with float, double, and half precsion and negative (but coverage is still pretty good)
1579
1580 return 0;
1581}
1582
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001583// Really simple basic input for tagging test
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07001584static uint8_t spOptTestInput[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001585 0xd9, 0xd9, 0xf7, // CBOR magic number
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001586 0x81, // Array of one
1587 0xd8, 0x04, // non-preferred serialization of tag 4
1588 0x82, 0x01, 0x03}; // fraction 1/3
1589
1590static uint8_t spEncodedLargeTag[] = {0xdb, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x80};
1591
1592// 0x9192939495969798, 0x88, 0x01, 0x04
1593static uint8_t spLotsOfTags[] = {0xdb, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0xd8, 0x88, 0xc5, 0xc4, 0x80};
1594
1595/*
1596 The cbor.me parse of this.
1597 55799(55799(55799({6(7(-23)): 5859837686836516696(7({7(-20): 11({17(-18): 17(17(17("Organization"))),
1598 9(-17): 773("SSG"), -15: 4(5(6(7(8(9(10(11(12(13(14(15("Confusion")))))))))))), 17(-16): 17("San Diego"),
1599 17(-14): 17("US")}), 23(-19): 19({-11: 9({-9: -7}),
1600 90599561(90599561(90599561(-10))): 12(h'0102030405060708090A')})})),
1601 16(-22): 23({11(8(7(-5))): 8(-3)})})))
1602 */
1603static uint8_t spCSRWithTags[] = {
1604 0xd9, 0xd9, 0xf7, 0xd9, 0xd9, 0xf7, 0xd9, 0xd9, 0xf7, 0xa2,
1605 0xc6, 0xc7, 0x36,
1606 0xdb, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0xc7, 0xa2,
1607 0xda, 0x00, 0x00, 0x00, 0x07, 0x33,
1608 0xcb, 0xa5,
1609 0xd1, 0x31,
1610 0xd1, 0xd1, 0xd1, 0x6c,
1611 0x4f, 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74, 0x69, 0x6f, 0x6e,
1612 0xc9, 0x30,
1613 0xd9, 0x03, 0x05, 0x63,
1614 0x53, 0x53, 0x47,
1615 0x2e,
1616 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf, 0x69,
1617 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73, 0x69, 0x6f, 0x6e,
1618 0xd1, 0x2f,
1619 0xd1, 0x69,
1620 0x53, 0x61, 0x6e, 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f,
1621 0xd1, 0x2d,
1622 0xd1, 0x62,
1623 0x55, 0x53,
1624 0xd7, 0x32,
1625 0xd3, 0xa2,
1626 0x2a,
1627 0xc9, 0xa1,
1628 0x28,
1629 0x26,
1630 0xda, 0x05, 0x66, 0x70, 0x89, 0xda, 0x05, 0x66, 0x70, 0x89, 0xda, 0x05, 0x66, 0x70, 0x89, 0x29,
1631 0xcc, 0x4a,
1632 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,0x07, 0x08, 0x09, 0x0a,
1633 0xd0, 0x35,
1634 0xd7, 0xa1,
1635 0xcb, 0xc8, 0xc7, 0x24,
1636 0xc8, 0x22};
1637
1638static int CheckCSRMaps(QCBORDecodeContext *pDC);
1639
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001640
1641int OptTagParseTest()
1642{
1643 QCBORDecodeContext DCtx;
1644 QCBORItem Item;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001645
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07001646 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spOptTestInput), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001647
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001648 //-------------------------
1649 // This text matches the magic number tag and the fraction tag
1650 if(QCBORDecode_GetNext(&DCtx, &Item)) {
1651 return -2;
1652 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001653 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001654 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC)) {
1655 return -3;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001656 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001657
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001658 if(QCBORDecode_GetNext(&DCtx, &Item)) {
1659 return -4;
1660 }
1661 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
1662 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_FRACTION) ||
1663 Item.val.uCount != 2) {
1664 return -5;
1665 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001666
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001667 // --------------------------------
1668 // This test decodes the very large tag, but it is not in
1669 // any list so it is ignored.
1670 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEncodedLargeTag), QCBOR_DECODE_MODE_NORMAL);
1671 if(QCBORDecode_GetNext(&DCtx, &Item)) {
1672 return -6;
1673 }
1674 if(Item.uTagBits) {
1675 return -7;
1676 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001677
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001678 // ----------------------------------
1679 // This test sets up a caller-config list that includes the very large tage and then matches it.
1680 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEncodedLargeTag), QCBOR_DECODE_MODE_NORMAL);
1681 const uint64_t puList[] = {0x9192939495969798, 257};
1682 const QCBORTagListIn TL = {2, puList};
1683 QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001684
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001685 if(QCBORDecode_GetNext(&DCtx, &Item)) {
1686 return -8;
1687 }
1688 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
1689 !QCBORDecode_IsTagged(&DCtx, &Item, 0x9192939495969798) ||
1690 QCBORDecode_IsTagged(&DCtx, &Item, 257) ||
1691 QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_BIGFLOAT) ||
1692 Item.val.uCount != 0) {
1693 return -9;
1694 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001695
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001696 //------------------------
1697 // This test sets up a caller-configured list, and looks up something not in it
1698 const uint64_t puLongList[17] = {1,2,1};
1699 const QCBORTagListIn TLLong = {17, puLongList};
1700 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEncodedLargeTag), QCBOR_DECODE_MODE_NORMAL);
1701 QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TLLong);
1702 if(QCBORDecode_GetNext(&DCtx, &Item)) {
1703 return -11;
1704 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001705
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001706 // -----------------------
1707 // This tests retrievel of the full tag list
1708 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spLotsOfTags), QCBOR_DECODE_MODE_NORMAL);
1709 uint64_t puTags[16];
1710 QCBORTagListOut Out = {0, 4, puTags};
1711 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
1712 return -12;
1713 }
1714 if(puTags[0] != 0x9192939495969798 ||
1715 puTags[1] != 0x88 ||
1716 puTags[2] != 0x05 ||
1717 puTags[3] != 0x04) {
1718 return -13;
1719 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001720
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001721 // ----------------------
1722 // This text if too small of an out list
1723 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spLotsOfTags), QCBOR_DECODE_MODE_NORMAL);
1724 QCBORTagListOut OutSmall = {0, 3, puTags};
1725 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &OutSmall) != QCBOR_ERR_TOO_MANY_TAGS) {
1726 return -14;
1727 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001728
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001729 // ---------------
1730 // Parse a version of the "CSR" that has had a ton of tags randomly inserted
1731 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRWithTags), QCBOR_DECODE_MODE_NORMAL);
1732 int n = CheckCSRMaps(&DCtx);
1733 if(n) {
1734 return n-2000;
1735 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001736
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001737 Out = (QCBORTagListOut){0,16, puTags};
1738 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRWithTags), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001739
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001740 const uint64_t puTagList[] = {773, 1, 90599561};
1741 const QCBORTagListIn TagList = {3, puTagList};
1742 QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TagList);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001743
1744
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001745 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
1746 return -100;
1747 }
1748 if(Item.uDataType != QCBOR_TYPE_MAP ||
1749 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC) ||
1750 QCBORDecode_IsTagged(&DCtx, &Item, 90599561) ||
1751 QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_DATE_EPOCH) ||
1752 Item.val.uCount != 2 ||
1753 puTags[0] != CBOR_TAG_CBOR_MAGIC ||
1754 puTags[1] != CBOR_TAG_CBOR_MAGIC ||
1755 puTags[2] != CBOR_TAG_CBOR_MAGIC ||
1756 Out.uNumUsed != 3) {
1757 return -101;
1758 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001759
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001760 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
1761 return -102;
1762 }
1763 if(Item.uDataType != QCBOR_TYPE_MAP ||
1764 QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC) ||
1765 QCBORDecode_IsTagged(&DCtx, &Item, 6) ||
1766 QCBORDecode_IsTagged(&DCtx, &Item, 7) || // item is tagged 7, but 7 is not configured to be recognized
1767 Item.val.uCount != 2 ||
1768 puTags[0] != 5859837686836516696 ||
1769 puTags[1] != 7 ||
1770 Out.uNumUsed != 2) {
1771 return -103;
1772 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001773
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001774 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
1775 return -104;
1776 }
1777 if(Item.uDataType != QCBOR_TYPE_MAP ||
1778 Item.uTagBits ||
1779 Item.val.uCount != 5 ||
1780 puTags[0] != 0x0b ||
1781 Out.uNumUsed != 1) {
1782 return -105;
1783 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001784
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001785 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
1786 return -106;
1787 }
1788 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1789 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_COSE_MAC0) ||
1790 Item.val.string.len != 12 ||
1791 puTags[0] != CBOR_TAG_COSE_MAC0 ||
1792 puTags[1] != CBOR_TAG_COSE_MAC0 ||
1793 puTags[2] != CBOR_TAG_COSE_MAC0 ||
1794 Out.uNumUsed != 3) {
1795 return -105;
1796 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001797
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001798 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
1799 return -107;
1800 }
1801 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1802 !QCBORDecode_IsTagged(&DCtx, &Item, 773) ||
1803 Item.val.string.len != 3 ||
1804 puTags[0] != 773 ||
1805 Out.uNumUsed != 1) {
1806 return -108;
1807 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001808
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001809 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
1810 return -109;
1811 }
1812 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1813 !QCBORDecode_IsTagged(&DCtx, &Item, 4) ||
1814 Item.val.string.len != 9 ||
1815 puTags[0] != 4 ||
1816 puTags[11] != 0x0f ||
1817 Out.uNumUsed != 12) {
1818 return -110;
1819 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001820
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001821 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
1822 return -111;
1823 }
1824 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1825 !QCBORDecode_IsTagged(&DCtx, &Item, 17) ||
1826 Item.val.string.len != 9 ||
1827 puTags[0] != 17 ||
1828 Out.uNumUsed != 1) {
1829 return -112;
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 -111;
1834 }
1835 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1836 !QCBORDecode_IsTagged(&DCtx, &Item, 17) ||
1837 Item.val.string.len != 2 ||
1838 puTags[0] != 17 ||
1839 Out.uNumUsed != 1) {
1840 return -112;
1841 }
1842
1843 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
1844 return -113;
1845 }
1846 if(Item.uDataType != QCBOR_TYPE_MAP ||
1847 QCBORDecode_IsTagged(&DCtx, &Item, 19) ||
1848 Item.val.uCount != 2 ||
1849 puTags[0] != 19 ||
1850 Out.uNumUsed != 1) {
1851 return -114;
1852 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001853
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001854 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
1855 return -115;
1856 }
1857 if(Item.uDataType != QCBOR_TYPE_MAP ||
1858 QCBORDecode_IsTagged(&DCtx, &Item, 9) ||
1859 Item.uTagBits ||
1860 Item.val.uCount != 1 ||
1861 puTags[0] != 9 ||
1862 Out.uNumUsed != 1) {
1863 return -116;
1864 }
1865
1866 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
1867 return -116;
1868 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001869 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001870 Item.val.int64 != -7 ||
1871 Item.uTagBits ||
1872 Out.uNumUsed != 0) {
1873 return -117;
1874 }
1875
1876 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
1877 return -118;
1878 }
1879 if(Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
1880 Item.val.string.len != 10 ||
1881 Item.uTagBits ||
1882 puTags[0] != 12 ||
1883 Out.uNumUsed != 1) {
1884 return -119;
1885 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001886
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001887 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
1888 return -120;
1889 }
1890 if(Item.uDataType != QCBOR_TYPE_MAP ||
1891 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_ENC_AS_B16) ||
1892 Item.val.uCount != 1 ||
1893 puTags[0] != 0x17 ||
1894 Out.uNumUsed != 1) {
1895 return -121;
1896 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001897
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001898 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
1899 return -122;
1900 }
1901 if(Item.uDataType != QCBOR_TYPE_INT64 ||
1902 QCBORDecode_IsTagged(&DCtx, &Item, 8) ||
1903 Item.val.int64 != -3 ||
1904 puTags[0] != 8 ||
1905 Out.uNumUsed != 1) {
1906 return -123;
1907 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001908
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001909 if(QCBORDecode_Finish(&DCtx)) {
1910 return -124;
1911 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001912
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001913 return 0;
1914}
1915
1916
1917
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001918
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07001919static uint8_t spBigNumInput[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001920 0x83,
1921 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1922 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1923 0xA4,
1924 0x63, 0x42, 0x4E, 0x2B,
1925 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1926 0x18, 0x40,
1927 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1928 0x63, 0x42, 0x4E, 0x2D,
1929 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1930 0x38, 0x3F,
1931 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
1932
1933
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07001934static uint8_t spBigNum[] = {0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001935
1936
1937int BignumParseTest()
1938{
1939 QCBORDecodeContext DCtx;
1940 QCBORItem Item;
1941 int nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001942
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07001943 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNumInput), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001944
1945
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001946 //
1947 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1948 return -1;
1949 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
1950 return -1;
1951 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001952
1953 //
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001954 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1955 return -1;
1956 if(Item.uDataType != QCBOR_TYPE_POSBIGNUM ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07001957 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001958 return -1;
1959 }
1960
1961 //
1962 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1963 return -1;
1964 if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07001965 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001966 return -1;
1967 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001968
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001969 //
1970 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1971 return -1;
1972 if(Item.uDataType != QCBOR_TYPE_MAP) {
1973 return -1;
1974 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001975
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001976 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1977 return -1;
1978 if(Item.uDataType != QCBOR_TYPE_POSBIGNUM ||
1979 Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07001980 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001981 return -1;
1982 }
1983
1984 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1985 return -1;
1986 if(Item.uDataType != QCBOR_TYPE_POSBIGNUM ||
1987 Item.uLabelType != QCBOR_TYPE_INT64 ||
1988 Item.label.int64 != 64 ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07001989 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001990 return -1;
1991 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001992
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001993 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1994 return -1;
1995 if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM ||
1996 Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07001997 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001998 return -1;
1999 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002000
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002001 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
2002 return -1;
2003 if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM ||
2004 Item.uLabelType != QCBOR_TYPE_INT64 ||
2005 Item.label.int64 != -64 ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002006 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002007 return -1;
2008 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002009
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002010 return 0;
2011}
2012
2013
2014
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302015static 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 +08002016{
2017 QCBORItem Item;
2018 int nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002019
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002020 if((nCBORError = QCBORDecode_GetNext(pCtx, &Item))) return -1;
2021 if(Item.uDataType != uDataType) return -1;
2022 if(uNestingLevel > 0) {
2023 if(Item.uLabelType != QCBOR_TYPE_INT64 && Item.uLabelType != QCBOR_TYPE_UINT64) return -1;
2024 if(Item.uLabelType == QCBOR_TYPE_INT64) {
2025 if(Item.label.int64 != nLabel) return -1;
2026 } else {
Laurence Lundblade570fab52018-10-13 18:28:27 +08002027 if(Item.label.uint64 != (uint64_t)nLabel) return -1;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002028 }
2029 }
2030 if(Item.uNestingLevel != uNestingLevel) return -1;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302031 if(Item.uNextNestLevel != uNextNest) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002032
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002033 if(pItem) {
2034 *pItem = Item;
2035 }
2036 return 0;
2037}
2038
2039
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002040// Same code checks definite and indefinite length versions of the map
2041static int CheckCSRMaps(QCBORDecodeContext *pDC)
2042{
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302043 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 0, 1, 0, NULL)) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002044
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302045 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 1, 2, -23, NULL)) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002046
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302047 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 2, 3, -20, NULL)) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002048
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302049 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -18, NULL)) return -1;
2050 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -17, NULL)) return -1;
2051 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -15, NULL)) return -1;
2052 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -16, NULL)) return -1;
2053 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 2, -14, NULL)) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002054
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302055 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 2, 3, -19, NULL)) return -1;
2056 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 3, 4, -11, NULL)) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002057
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302058 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_INT64, 4, 3, -9, NULL)) return -1;
2059 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_BYTE_STRING, 3, 1, -10, NULL)) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002060
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302061 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 1, 2, -22, NULL)) return -1;
2062 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_INT64, 2, 0, -5, NULL)) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002063
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002064 if(QCBORDecode_Finish(pDC)) return -2;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002065
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002066 return 0;
2067}
2068
2069
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002070/*
2071// cbor.me decoded output
2072{
2073 -23: {
2074 -20: {
2075 -18: "Organization",
2076 -17: "SSG",
2077 -15: "Confusion",
2078 -16: "San Diego",
2079 -14: "US"
2080 },
2081 -19: {
2082 -11: {
2083 -9: -7
2084 },
2085 -10: '\u0001\u0002\u0003\u0004\u0005\u0006\a\b\t\n'
2086 }
2087 },
2088 -22: {
2089 -5: -3
2090 }
2091}
2092 */
2093
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002094
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002095static uint8_t spCSRInput[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002096 0xa2, 0x36, 0xa2, 0x33, 0xa5, 0x31, 0x6c, 0x4f,
2097 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74,
2098 0x69, 0x6f, 0x6e, 0x30, 0x63, 0x53, 0x53, 0x47,
2099 0x2e, 0x69, 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73,
2100 0x69, 0x6f, 0x6e, 0x2f, 0x69, 0x53, 0x61, 0x6e,
2101 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f, 0x2d, 0x62,
2102 0x55, 0x53, 0x32, 0xa2, 0x2a, 0xa1, 0x28, 0x26,
2103 0x29, 0x4a, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
2104 0x07, 0x08, 0x09, 0x0a, 0x35, 0xa1, 0x24, 0x22};
2105
2106int NestedMapTest()
2107{
2108 QCBORDecodeContext DCtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002109
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002110 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002111
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002112 return CheckCSRMaps(&DCtx);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002113}
2114
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08002115
2116
2117int StringDecoderModeFailTest()
2118{
2119 QCBORDecodeContext DCtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002120
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08002121 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput), QCBOR_DECODE_MODE_MAP_STRINGS_ONLY);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002122
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08002123 QCBORItem Item;
2124 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002125
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08002126 if(QCBORDecode_GetNext(&DCtx, &Item)) {
2127 return -1;
2128 }
2129 if(Item.uDataType != QCBOR_TYPE_MAP) {
2130 return -2;
2131 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002132
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08002133 nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
2134 if(nCBORError != QCBOR_ERR_MAP_LABEL_TYPE) {
2135 return -3;
2136 }
2137
2138 return 0;
2139}
2140
2141
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002142// Same map as above, but using indefinite lengths
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002143static uint8_t spCSRInputIndefLen[] = {
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002144 0xbf, 0x36, 0xbf, 0x33, 0xbf, 0x31, 0x6c, 0x4f,
2145 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74,
2146 0x69, 0x6f, 0x6e, 0x30, 0x63, 0x53, 0x53, 0x47,
2147 0x2e, 0x69, 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73,
2148 0x69, 0x6f, 0x6e, 0x2f, 0x69, 0x53, 0x61, 0x6e,
2149 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f, 0x2d, 0x62,
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002150 0x55, 0x53, 0xff, 0x32, 0xbf, 0x2a, 0xbf, 0x28,
2151 0x26, 0xff, 0x29, 0x4a, 0x01, 0x02, 0x03, 0x04,
2152 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0xff, 0xff,
2153 0x35, 0xbf, 0x24, 0x22, 0xff, 0xff};
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002154
2155int NestedMapTestIndefLen()
2156{
2157 QCBORDecodeContext DCtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002158
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002159 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInputIndefLen), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002160
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002161 return CheckCSRMaps(&DCtx);
2162}
2163
2164
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002165
Laurence Lundblade17ede402018-10-13 11:43:07 +08002166static UsefulBufC make_nested_indefinite_arrays(int n, UsefulBuf Storage)
2167{
2168 UsefulOutBuf UOB;
2169 UsefulOutBuf_Init(&UOB, Storage);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002170
Laurence Lundblade17ede402018-10-13 11:43:07 +08002171 int i;
2172 for(i = 0; i < n; i++) {
2173 UsefulOutBuf_AppendByte(&UOB, 0x9f);
2174 }
2175
2176 for(i = 0; i < n; i++) {
2177 UsefulOutBuf_AppendByte(&UOB, 0xff);
2178 }
2179 return UsefulOutBuf_OutUBuf(&UOB);
2180}
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002181
2182
Laurence Lundblade17ede402018-10-13 11:43:07 +08002183static int parse_indeflen_nested(UsefulBufC Nested, int nNestLevel)
2184{
2185 QCBORDecodeContext DC;
2186 QCBORDecode_Init(&DC, Nested, 0);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002187
Laurence Lundblade17ede402018-10-13 11:43:07 +08002188 int j;
2189 for(j = 0; j < nNestLevel; j++) {
2190 QCBORItem Item;
2191 int nReturn = QCBORDecode_GetNext(&DC, &Item);
2192 if(j >= QCBOR_MAX_ARRAY_NESTING) {
2193 // Should be in error
2194 if(nReturn != QCBOR_ERR_ARRAY_NESTING_TOO_DEEP) {
2195 return -4;
2196 } else {
2197 return 0; // Decoding doesn't recover after an error
2198 }
2199 } else {
2200 // Should be no error
2201 if(nReturn) {
2202 return -9; // Should not have got an error
2203 }
2204 }
2205 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
2206 return -7;
2207 }
2208 }
2209 int nReturn = QCBORDecode_Finish(&DC);
2210 if(nReturn) {
2211 return -3;
2212 }
2213 return 0;
2214}
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002215
2216
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302217int IndefiniteLengthNestTest()
Laurence Lundblade17ede402018-10-13 11:43:07 +08002218{
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05302219 UsefulBuf_MAKE_STACK_UB(Storage, 50);
Laurence Lundblade17ede402018-10-13 11:43:07 +08002220 int i;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002221 for(i=1; i < QCBOR_MAX_ARRAY_NESTING+4; i++) {
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08002222 const UsefulBufC Nested = make_nested_indefinite_arrays(i, Storage);
Laurence Lundblade17ede402018-10-13 11:43:07 +08002223 int nReturn = parse_indeflen_nested(Nested, i);
2224 if(nReturn) {
2225 return nReturn;
2226 }
2227 }
2228 return 0;
2229}
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002230
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002231
Laurence Lundblade6de37062018-10-15 12:22:42 +05302232
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002233static const uint8_t spIndefiniteArray[] = {0x9f, 0x01, 0x82, 0x02, 0x03, 0xff}; // [1, [2, 3]]
2234static const uint8_t spIndefiniteArrayBad1[] = {0x9f}; // No closing break
2235static const uint8_t spIndefiniteArrayBad2[] = {0x9f, 0x9f, 0x02, 0xff}; // Not enough closing breaks
2236static const uint8_t spIndefiniteArrayBad3[] = {0x9f, 0x02, 0xff, 0xff}; // Too many closing breaks
2237static const uint8_t spIndefiniteArrayBad4[] = {0x81, 0x9f}; // Unclosed indeflen inside def len
2238static const uint8_t spIndefiniteArrayBad5[] = {0x9f, 0xd1, 0xff}; // confused tag
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002239
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302240int IndefiniteLengthArrayMapTest()
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002241{
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002242 int nResult;
2243 // --- first test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002244 UsefulBufC IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArray);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002245
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002246 // Decode it and see if it is OK
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05302247 UsefulBuf_MAKE_STACK_UB(MemPool, 150);
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002248 QCBORDecodeContext DC;
2249 QCBORItem Item;
2250 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002251
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002252 QCBORDecode_SetMemPool(&DC, MemPool, false);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002253
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002254 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05302255
2256 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
2257 Item.uNestingLevel != 0 ||
2258 Item.uNextNestLevel != 1) {
2259 return -111;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002260 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002261
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002262 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05302263 if(Item.uDataType != QCBOR_TYPE_INT64 ||
2264 Item.uNestingLevel != 1 ||
2265 Item.uNextNestLevel != 1) {
2266 return -2;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002267 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002268
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002269 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05302270 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
2271 Item.uNestingLevel != 1 ||
2272 Item.uNextNestLevel != 2) {
2273 return -3;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002274 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002275
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002276 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade12b495d2018-12-17 11:15:54 -08002277 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade6de37062018-10-15 12:22:42 +05302278 Item.uNestingLevel != 2 ||
2279 Item.uNextNestLevel != 2) {
2280 return -4;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002281 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002282
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002283 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade12b495d2018-12-17 11:15:54 -08002284 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade6de37062018-10-15 12:22:42 +05302285 Item.uNestingLevel != 2 ||
2286 Item.uNextNestLevel != 0) {
2287 return -5;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002288 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002289
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002290 if(QCBORDecode_Finish(&DC)) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05302291 return -6;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002292 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002293
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002294 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002295 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad1);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002296
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002297 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002298
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002299 QCBORDecode_SetMemPool(&DC, MemPool, false);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002300
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002301 nResult = QCBORDecode_GetNext(&DC, &Item);
2302 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05302303 return -7;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002304 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002305
Laurence Lundblade570fab52018-10-13 18:28:27 +08002306 nResult = QCBORDecode_Finish(&DC);
Laurence Lundblade6de37062018-10-15 12:22:42 +05302307 if(nResult != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
2308 return -8;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002309 }
2310
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002311
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002312 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002313 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad2);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002314
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002315 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002316
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002317 QCBORDecode_SetMemPool(&DC, MemPool, false);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002318
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002319 nResult = QCBORDecode_GetNext(&DC, &Item);
2320 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05302321 return -9;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002322 }
2323
2324 nResult = QCBORDecode_GetNext(&DC, &Item);
2325 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05302326 return -10;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002327 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002328
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002329 nResult = QCBORDecode_GetNext(&DC, &Item);
2330 if(nResult || Item.uDataType != QCBOR_TYPE_INT64) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05302331 return -11;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002332 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002333
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002334 nResult = QCBORDecode_Finish(&DC);
Laurence Lundblade6de37062018-10-15 12:22:42 +05302335 if(nResult != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
2336 return -12;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002337 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002338
2339
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002340 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002341 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad3);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002342
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002343 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002344
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002345 QCBORDecode_SetMemPool(&DC, MemPool, false);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002346
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002347 nResult = QCBORDecode_GetNext(&DC, &Item);
2348 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05302349 return -13;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002350 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002351
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002352 nResult = QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05302353 if(nResult != QCBOR_ERR_BAD_BREAK) {
2354 return -14;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002355 }
Laurence Lundblade6de37062018-10-15 12:22:42 +05302356
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002357
Laurence Lundblade570fab52018-10-13 18:28:27 +08002358 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002359 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad4);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002360
Laurence Lundblade570fab52018-10-13 18:28:27 +08002361 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002362
Laurence Lundblade570fab52018-10-13 18:28:27 +08002363 QCBORDecode_SetMemPool(&DC, MemPool, false);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002364
Laurence Lundblade570fab52018-10-13 18:28:27 +08002365 nResult = QCBORDecode_GetNext(&DC, &Item);
2366 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05302367 return -15;
Laurence Lundblade570fab52018-10-13 18:28:27 +08002368 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002369
Laurence Lundblade570fab52018-10-13 18:28:27 +08002370 nResult = QCBORDecode_GetNext(&DC, &Item);
2371 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05302372 return -16;
Laurence Lundblade570fab52018-10-13 18:28:27 +08002373 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002374
Laurence Lundblade570fab52018-10-13 18:28:27 +08002375 nResult = QCBORDecode_Finish(&DC);
Laurence Lundblade6de37062018-10-15 12:22:42 +05302376 if(nResult != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
2377 return -17;
Laurence Lundblade570fab52018-10-13 18:28:27 +08002378 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002379
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302380 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002381 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad5);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002382
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302383 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002384
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302385 QCBORDecode_SetMemPool(&DC, MemPool, false);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002386
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302387 nResult = QCBORDecode_GetNext(&DC, &Item);
2388 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05302389 return -18;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302390 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002391
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302392 nResult = QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05302393 if(nResult != QCBOR_ERR_BAD_BREAK) {
2394 return -19;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302395 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002396
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002397 return 0;
2398}
2399
Laurence Lundblade17ede402018-10-13 11:43:07 +08002400
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002401static const uint8_t spIndefiniteLenString[] = {
Laurence Lundblade17ede402018-10-13 11:43:07 +08002402 0x81, // Array of length one
2403 0x7f, // text string marked with indefinite length
2404 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment
2405 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment
2406 0xff // ending break
2407};
2408
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002409static const uint8_t spIndefiniteLenStringBad2[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302410 0x81, // Array of length one
2411 0x7f, // text string marked with indefinite length
2412 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment
2413 0x44, 0x6d, 0x69, 0x6e, 0x67, // second segment of wrong type
2414 0xff // ending break
2415};
2416
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002417static const uint8_t spIndefiniteLenStringBad3[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302418 0x81, // Array of length one
2419 0x7f, // text string marked with indefinite length
2420 0x01, 0x02, // Not a string
2421 0xff // ending break
2422};
2423
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002424static const uint8_t spIndefiniteLenStringBad4[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302425 0x81, // Array of length one
2426 0x7f, // text string marked with indefinite length
2427 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment
2428 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment
2429 // missing end of string
2430};
2431
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002432static const uint8_t spIndefiniteLenStringLabel[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302433 0xa1, // Array of length one
2434 0x7f, // text string marked with indefinite length
2435 0x65, 0x73, 0x74, 0x72, 0x75, 0x75, // first segment
2436 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment
2437 0xff, // ending break
2438 0x01 // integer being labeled.
2439};
2440
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08002441static UsefulBufC MakeIndefiniteBigBstr(UsefulBuf Storage) // TODO: size this
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302442{
2443 UsefulOutBuf UOB;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002444
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302445 UsefulOutBuf_Init(&UOB, Storage);
2446 UsefulOutBuf_AppendByte(&UOB, 0x81);
2447 UsefulOutBuf_AppendByte(&UOB, 0x5f);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002448
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302449 int i = 0;
2450 for(int nChunkSize = 1; nChunkSize <= 128; nChunkSize *= 2) {
2451 UsefulOutBuf_AppendByte(&UOB, 0x58);
2452 UsefulOutBuf_AppendByte(&UOB, (uint8_t)nChunkSize);
2453 for(int j = 0; j < nChunkSize; j++ ) {
2454 UsefulOutBuf_AppendByte(&UOB, i);
2455 i++;
2456 }
2457 }
2458 UsefulOutBuf_AppendByte(&UOB, 0xff);
2459
2460 return UsefulOutBuf_OutUBuf(&UOB);
2461}
2462
2463static int CheckBigString(UsefulBufC BigString)
2464{
2465 if(BigString.len != 255) {
2466 return 1;
2467 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002468
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302469 for(uint8_t i = 0; i < 255; i++){
2470 if(((const uint8_t *)BigString.ptr)[i] != i) {
2471 return 1;
2472 }
2473 }
2474 return 0;
2475}
2476
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05302477
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302478int IndefiniteLengthStringTest()
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302479{
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302480 QCBORDecodeContext DC;
2481 QCBORItem Item;
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05302482 // big enough for MakeIndefiniteBigBstr() + MemPool overhead
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08002483 UsefulBuf_MAKE_STACK_UB(MemPool, 350);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002484
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302485 // --- Simple normal indefinite length string ------
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002486 UsefulBufC IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenString);
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302487 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002488
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05302489 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302490 return -1;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302491 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002492
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302493 if(QCBORDecode_GetNext(&DC, &Item)) {
2494 return -2;
2495 }
2496 if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.uDataAlloc) {
2497 return -3;
2498 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002499
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302500 if(QCBORDecode_GetNext(&DC, &Item)) {
2501 return -4;
2502 }
2503 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || !Item.uDataAlloc) {
2504 return -5;
2505 }
2506 if(QCBORDecode_Finish(&DC)) {
2507 return -6;
2508 }
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302509
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302510 // ----- types mismatch ---
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002511 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad2), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002512
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302513 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
2514 return -7;
2515 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002516
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302517 if(QCBORDecode_GetNext(&DC, &Item)) {
2518 return -8;
2519 }
2520 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
2521 return -9;
2522 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002523
Laurence Lundblade30816f22018-11-10 13:40:22 +07002524 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_INDEFINITE_STRING_CHUNK) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302525 return -10;
2526 }
2527
2528 // ----- not a string ---
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002529 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad3), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002530
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302531 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
2532 return -11;
2533 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002534
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302535 if(QCBORDecode_GetNext(&DC, &Item)) {
2536 return -12;
2537 }
2538 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
2539 return -13;
2540 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002541
Laurence Lundblade30816f22018-11-10 13:40:22 +07002542 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_INDEFINITE_STRING_CHUNK) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302543 return -14;
2544 }
2545
2546 // ----- no end -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002547 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad4), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002548
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302549 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
2550 return -15;
2551 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002552
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302553 if(QCBORDecode_GetNext(&DC, &Item)) {
2554 return -16;
2555 }
2556 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
2557 return -17;
2558 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002559
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302560 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_HIT_END) {
2561 return -18;
2562 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002563
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302564 // ------ Don't set a string allocator and see an error -----
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302565 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002566
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302567 QCBORDecode_GetNext(&DC, &Item);
2568 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302569 return -19;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302570 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002571
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302572 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_NO_STRING_ALLOCATOR) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302573 return -20;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302574 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002575
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302576 // ----- Mempool is way too small -----
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05302577 UsefulBuf_MAKE_STACK_UB(MemPoolTooSmall, 20); // 20 is too small no matter what
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302578
2579 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
2580 if(!QCBORDecode_SetMemPool(&DC, MemPoolTooSmall, false)) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302581 return -21;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302582 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002583
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302584 // ----- Mempool is way too small -----
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05302585 UsefulBuf_MAKE_STACK_UB(BigIndefBStrStorage, 290);
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08002586 const UsefulBufC BigIndefBStr = MakeIndefiniteBigBstr(BigIndefBStrStorage);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002587
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05302588 UsefulBuf_MAKE_STACK_UB(MemPoolSmall, 80); // 80 is big enough for MemPool overhead, but not BigIndefBStr
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002589
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302590 QCBORDecode_Init(&DC, BigIndefBStr, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302591 if(QCBORDecode_SetMemPool(&DC, MemPoolSmall, false)) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302592 return -22;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302593 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002594
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302595 QCBORDecode_GetNext(&DC, &Item);
2596 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302597 return -23;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302598 }
Laurence Lundblade30816f22018-11-10 13:40:22 +07002599 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_STRING_ALLOCATE) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302600 return -24;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302601 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002602
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302603 // ---- big bstr -----
2604 QCBORDecode_Init(&DC, BigIndefBStr, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002605
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302606 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
2607 return -25;
2608 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002609
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05302610 if(QCBORDecode_GetNext(&DC, &Item)) {
2611 return -26;
2612 }
2613 if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.uDataAlloc) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302614 return -26;
2615 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002616
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302617 if(QCBORDecode_GetNext(&DC, &Item)) {
2618 return -27;
2619 }
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05302620 if(Item.uDataType != QCBOR_TYPE_BYTE_STRING || !Item.uDataAlloc || Item.uNestingLevel != 1) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302621 return -28;
2622 }
2623 if(CheckBigString(Item.val.string)) {
2624 return -3;
2625 }
2626 if(QCBORDecode_Finish(&DC)) {
2627 return -29;
2628 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002629
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302630 // --- label is an indefinite length string ------
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002631 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringLabel), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002632
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302633 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
2634 return -30;
2635 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002636
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302637 QCBORDecode_GetNext(&DC, &Item);
2638 if(Item.uDataType != QCBOR_TYPE_MAP) {
2639 return -31;
2640 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002641
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302642 if(QCBORDecode_GetNext(&DC, &Item)){
2643 return -32;
2644 }
2645 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || Item.uDataType != QCBOR_TYPE_INT64 ||
2646 Item.uDataAlloc || !Item.uLabelAlloc ||
2647 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("struuming"))) {
2648 return -33;
2649 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002650
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302651 if(QCBORDecode_Finish(&DC)) {
2652 return -34;
2653 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002654
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002655 return 0;
2656}
2657
2658
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302659int AllocAllStringsTest()
2660{
2661 QCBORDecodeContext DC;
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08002662 QCBORError nCBORError;
2663
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002664
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302665 // First test, use the "CSRMap" as easy input and checking
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002666 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002667
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08002668 UsefulBuf_MAKE_STACK_UB(Pool, sizeof(spCSRInput) + QCBOR_DECODE_MIN_MEM_POOL_SIZE);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002669
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08002670 nCBORError = QCBORDecode_SetMemPool(&DC, Pool, 1); // Turn on copying.
2671 if(nCBORError) {
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302672 return -1;
2673 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002674
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08002675 if(CheckCSRMaps(&DC)) {
2676 return -2;
2677 }
2678
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302679 // Next parse, save pointers to a few strings, destroy original and see all is OK.
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08002680 UsefulBuf_MAKE_STACK_UB(CopyOfStorage, sizeof(pValidMapEncoded) + QCBOR_DECODE_MIN_MEM_POOL_SIZE);
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08002681 const UsefulBufC CopyOf = UsefulBuf_Copy(CopyOfStorage, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded));
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002682
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302683 QCBORDecode_Init(&DC, CopyOf, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08002684 UsefulBuf_Set(Pool, '/');
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302685 QCBORDecode_SetMemPool(&DC, Pool, 1); // Turn on copying.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002686
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302687 QCBORItem Item1, Item2, Item3, Item4;
2688 if((nCBORError = QCBORDecode_GetNext(&DC, &Item1)))
2689 return nCBORError;
2690 if(Item1.uDataType != QCBOR_TYPE_MAP ||
2691 Item1.val.uCount != 3)
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08002692 return -3;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302693 if((nCBORError = QCBORDecode_GetNext(&DC, &Item1)))
2694 return nCBORError;
2695 if((nCBORError = QCBORDecode_GetNext(&DC, &Item2)))
2696 return nCBORError;
2697 if((nCBORError = QCBORDecode_GetNext(&DC, &Item3)))
2698 return nCBORError;
2699 if((nCBORError = QCBORDecode_GetNext(&DC, &Item4)))
2700 return nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002701
Laurence Lundblade05ec57b2018-10-21 01:50:03 +05302702 UsefulBuf_Set(CopyOfStorage, '_');
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002703
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302704 if(Item1.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302705 Item1.uDataType != QCBOR_TYPE_INT64 ||
2706 Item1.val.int64 != 42 ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09002707 UsefulBuf_Compare(Item1.label.string, UsefulBuf_FromSZ("first integer"))) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08002708 return -4;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09002709 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002710
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302711
2712 if(Item2.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09002713 UsefulBuf_Compare(Item2.label.string, UsefulBuf_FromSZ("an array of two strings")) ||
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302714 Item2.uDataType != QCBOR_TYPE_ARRAY ||
2715 Item2.val.uCount != 2)
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08002716 return -5;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002717
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302718 if(Item3.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09002719 UsefulBuf_Compare(Item3.val.string, UsefulBuf_FromSZ("string1"))) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08002720 return -6;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09002721 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002722
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302723 if(Item4.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09002724 UsefulBuf_Compare(Item4.val.string, UsefulBuf_FromSZ("string2"))) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08002725 return -7;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09002726 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002727
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302728 // Next parse with a pool that is too small
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08002729 UsefulBuf_MAKE_STACK_UB(SmallPool, QCBOR_DECODE_MIN_MEM_POOL_SIZE + 1);
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05302730 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302731 QCBORDecode_SetMemPool(&DC, SmallPool, 1); // Turn on copying.
2732 if((nCBORError = QCBORDecode_GetNext(&DC, &Item1)))
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08002733 return -8;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302734 if(Item1.uDataType != QCBOR_TYPE_MAP ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09002735 Item1.val.uCount != 3) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08002736 return -9;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09002737 }
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302738 if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item1))){
2739 if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item2))) {
2740 if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item3))) {
2741 nCBORError = QCBORDecode_GetNext(&DC, &Item4);
2742 }
2743 }
2744 }
Laurence Lundblade30816f22018-11-10 13:40:22 +07002745 if(nCBORError != QCBOR_ERR_STRING_ALLOCATE) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08002746 return -10;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302747 }
2748
2749 return 0;
2750}
2751
Laurence Lundbladef6531662018-12-04 10:42:22 +09002752// Cheating declaration to get to the special test hook
2753size_t MemPoolTestHook_GetPoolSize(void *ctx);
2754
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302755
2756int MemPoolTest(void)
Laurence Lundblade0155b622018-10-12 20:04:37 +08002757{
Laurence Lundbladef6531662018-12-04 10:42:22 +09002758 // Set up the decoder with a tiny bit of CBOR to parse
2759 QCBORDecodeContext DC;
2760 const uint8_t pMinimalCBOR[] = {0xa0}; // One empty map
2761 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pMinimalCBOR),0);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002762
Laurence Lundbladef6531662018-12-04 10:42:22 +09002763 // Set up an memory pool of 100 bytes
2764 UsefulBuf_MAKE_STACK_UB(Pool, 100);
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08002765 QCBORError nError = QCBORDecode_SetMemPool(&DC, Pool, 0);
2766 if(nError) {
2767 return -9;
2768 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002769
Laurence Lundbladef6531662018-12-04 10:42:22 +09002770 // Cheat a little to get to the string allocator object
2771 // so we can call it directly to test it
2772 QCBORStringAllocator *pAlloc = (QCBORStringAllocator *)DC.pStringAllocator;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002773 // Cheat some more to know exactly the
Laurence Lundbladef6531662018-12-04 10:42:22 +09002774 size_t uAvailPool = MemPoolTestHook_GetPoolSize(pAlloc);
2775
2776 // First test -- ask for too much in one go
2777 UsefulBuf Allocated = (*pAlloc->fAllocate)(pAlloc->pAllocaterContext, NULL, uAvailPool+1);
2778 if(!UsefulBuf_IsNULL(Allocated)) {
2779 return -1;
2780 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002781
2782
Laurence Lundbladef6531662018-12-04 10:42:22 +09002783 // Re do the set up for the next test that will do a successful alloc,
2784 // a fail, a free and then success
2785 // This test should work on 32 and 64-bit machines if the compiler
2786 // does the expected thing with pointer sizes for the internal
2787 // MemPool implementation leaving 44 or 72 bytes of pool memory.
2788 QCBORDecode_SetMemPool(&DC, Pool, 0);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002789
Laurence Lundbladef6531662018-12-04 10:42:22 +09002790 // Cheat a little to get to the string allocator object
2791 // so we can call it directly to test it
2792 pAlloc = (QCBORStringAllocator *)DC.pStringAllocator;
2793 // Cheat some more to know exactly the
2794 uAvailPool = MemPoolTestHook_GetPoolSize(pAlloc);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002795
Laurence Lundbladef6531662018-12-04 10:42:22 +09002796 Allocated = (*pAlloc->fAllocate)(pAlloc->pAllocaterContext, NULL, uAvailPool-1);
2797 if(UsefulBuf_IsNULL(Allocated)) { // expected to succeed
2798 return -2;
2799 }
2800 UsefulBuf Allocated2 = (*pAlloc->fAllocate)(pAlloc->pAllocaterContext, NULL, uAvailPool/2);
2801 if(!UsefulBuf_IsNULL(Allocated2)) { // expected to fail
2802 return -3;
2803 }
2804 (*pAlloc->fFree)(pAlloc->pAllocaterContext, Allocated.ptr);
2805 Allocated = (*pAlloc->fAllocate)(pAlloc->pAllocaterContext, NULL, uAvailPool/2);
2806 if(UsefulBuf_IsNULL(Allocated)) { // succeed because of the free
2807 return -4;
2808 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002809
2810
Laurence Lundbladef6531662018-12-04 10:42:22 +09002811 // Re do set up for next test that involves a successful alloc,
2812 // and a successful realloc and a failed realloc
2813 QCBORDecode_SetMemPool(&DC, Pool, 0);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002814
Laurence Lundbladef6531662018-12-04 10:42:22 +09002815 // Cheat a little to get to the string allocator object
2816 // so we can call it directly to test it
2817 pAlloc = (QCBORStringAllocator *)DC.pStringAllocator;
2818 Allocated = (*pAlloc->fAllocate)(pAlloc->pAllocaterContext, NULL, uAvailPool/2);
2819 if(UsefulBuf_IsNULL(Allocated)) { // expected to succeed
2820 return -5;
2821 }
2822 Allocated2 = (*pAlloc->fAllocate)(pAlloc->pAllocaterContext, Allocated.ptr, uAvailPool);
2823 if(UsefulBuf_IsNULL(Allocated2)) {
2824 return -6;
2825 }
2826 if(Allocated2.ptr != Allocated.ptr || Allocated2.len != uAvailPool) {
2827 return -7;
2828 }
2829 UsefulBuf Allocated3 = (*pAlloc->fAllocate)(pAlloc->pAllocaterContext, Allocated.ptr, uAvailPool+1);
2830 if(!UsefulBuf_IsNULL(Allocated3)) { // expected to fail
2831 return -8;
2832 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002833
Laurence Lundbladef6531662018-12-04 10:42:22 +09002834 return 0;
2835}
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002836