blob: 021c1507c7a1952bb0b589c82d99a5183bc890ae [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 Lundblade2ded3d92018-10-09 21:36:11 +0800570 int64_t i1, i2;
571 size_t i3, i4;
572 const uint8_t *s3, *s4;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800573
574
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800575 if(CreateSimpleArray(23, 6000, &pEncoded, &nEncodedLen) < 0) {
576 return(-1);
577 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800578
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800579 ParseOrderedArray(pEncoded, nEncodedLen, &i1, &i2, &s3, &i3, &s4, &i4);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800580
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800581 if(i1 != 23 ||
582 i2 != 6000 ||
583 i3 != 8 ||
584 i4 != 11 ||
Laurence Lundbladea44d5062018-10-17 18:45:12 +0530585 memcmp("galactic", s3, 8) !=0 ||
586 memcmp("haven token", s4, 11) !=0) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800587 return(-1);
588 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800589
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800590 return(0);
591}
592
593
Laurence Lundblade9e3651c2018-10-10 11:49:55 +0800594
Laurence Lundbladeb836efb2018-10-28 20:09:58 +0700595static uint8_t spDeepArrays[] = {0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x80};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800596
597int ParseDeepArrayTest()
598{
599 QCBORDecodeContext DCtx;
600 int nReturn = 0;
601 int i;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800602
Laurence Lundbladeb836efb2018-10-28 20:09:58 +0700603 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spDeepArrays), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800604
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800605 for(i = 0; i < 10; i++) {
606 QCBORItem Item;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800607
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800608 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
609 Item.uDataType != QCBOR_TYPE_ARRAY ||
610 Item.uNestingLevel != i) {
611 nReturn = -1;
612 break;
613 }
614 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800615
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800616 return(nReturn);
617}
618
Laurence Lundblade972e59c2018-11-11 15:57:23 +0700619// Big enough to test nesting to the depth of 24
620static uint8_t spTooDeepArrays[] = {0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81,
621 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81,
622 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81,
623 0x80};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800624
625int ParseTooDeepArrayTest()
626{
627 QCBORDecodeContext DCtx;
628 int nReturn = 0;
629 int i;
630 QCBORItem Item;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800631
632
Laurence Lundbladeb836efb2018-10-28 20:09:58 +0700633 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTooDeepArrays), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800634
Laurence Lundblade972e59c2018-11-11 15:57:23 +0700635 for(i = 0; i < QCBOR_MAX_ARRAY_NESTING1; i++) {
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800636
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800637 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
638 Item.uDataType != QCBOR_TYPE_ARRAY ||
639 Item.uNestingLevel != i) {
640 nReturn = -1;
641 break;
642 }
643 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800644
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800645 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_ARRAY_NESTING_TOO_DEEP)
646 nReturn = -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800647
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800648 return(nReturn);
649}
650
651
652
653
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800654int ShortBufferParseTest()
655{
656 int nResult = 0;
657 QCBORDecodeContext DCtx;
658 int num;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800659
Laurence Lundbladeb836efb2018-10-28 20:09:58 +0700660 for(num = sizeof(spExpectedEncodedInts)-1; num; num--) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800661 int n;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800662
Laurence Lundbladeb836efb2018-10-28 20:09:58 +0700663 QCBORDecode_Init(&DCtx, (UsefulBufC){spExpectedEncodedInts, num}, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800664
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800665 n = IntegerValuesParseTestInternal(&DCtx);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800666
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800667 //printf("Len %d, result: %d\n", num, n);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800668
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800669 if(n != QCBOR_ERR_HIT_END) {
670 nResult = -1;
671 goto Done;
672 }
673 }
674Done:
675 return nResult;
676}
677
678
Laurence Lundblade9e3651c2018-10-10 11:49:55 +0800679
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800680int ShortBufferParseTest2()
681{
682 uint8_t *pEncoded;
683 int nReturn;
684 size_t nEncodedLen;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800685
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800686 int64_t i1, i2;
687 size_t i3, i4;
688 const uint8_t *s3, *s4;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800689
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800690 nReturn = 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800691
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800692 if(CreateSimpleArray(23, 6000, &pEncoded, &nEncodedLen) < 0) {
693 return(-1);
694 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800695
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800696 for(nEncodedLen--; nEncodedLen; nEncodedLen--) {
697 int nResult = ParseOrderedArray(pEncoded, (uint32_t)nEncodedLen, &i1, &i2, &s3, &i3, &s4, &i4);
698 if(nResult == 0) {
699 nReturn = -1;
700 }
701 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800702
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800703 return(nReturn);
704}
705
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530706/*
707 Decode and thoroughly check a moderately complex
708 set of maps
709 */
Laurence Lundbladeea567ac2018-12-09 14:03:21 -0800710static int ParseMapTest1(QCBORDecodeMode nMode)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800711{
712 QCBORDecodeContext DCtx;
713 QCBORItem Item;
714 int nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800715
Laurence Lundbladeea567ac2018-12-09 14:03:21 -0800716 QCBORDecode_Init(&DCtx, (UsefulBufC){pValidMapEncoded, sizeof(pValidMapEncoded)}, nMode);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800717
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900718 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800719 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900720 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800721 if(Item.uDataType != QCBOR_TYPE_MAP ||
722 Item.val.uCount != 3)
723 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800724
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900725 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800726 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900727 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800728 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800729 Item.uDataType != QCBOR_TYPE_INT64 ||
730 Item.val.int64 != 42 ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530731 Item.uDataAlloc ||
732 Item.uLabelAlloc ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900733 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("first integer"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800734 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900735 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800736
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900737 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800738 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900739 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800740 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530741 Item.uDataAlloc ||
742 Item.uLabelAlloc ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900743 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("an array of two strings")) ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800744 Item.uDataType != QCBOR_TYPE_ARRAY ||
745 Item.val.uCount != 2)
746 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800747
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900748 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800749 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900750 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800751 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530752 Item.uDataAlloc ||
753 Item.uLabelAlloc ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900754 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("string1"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800755 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900756 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800757
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900758 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800759 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900760 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800761 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530762 Item.uDataAlloc ||
763 Item.uLabelAlloc ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900764 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("string2"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800765 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900766 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800767
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900768 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800769 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900770 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800771 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530772 Item.uDataAlloc ||
773 Item.uLabelAlloc ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900774 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("map in a map")) ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800775 Item.uDataType != QCBOR_TYPE_MAP ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900776 Item.val.uCount != 4) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800777 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900778 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800779
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900780 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800781 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900782 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800783 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900784 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("bytes 1"))||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800785 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530786 Item.uDataAlloc ||
787 Item.uLabelAlloc ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900788 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("xxxx"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800789 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900790 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800791
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900792 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800793 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900794 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800795 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900796 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("bytes 2")) ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800797 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530798 Item.uDataAlloc ||
799 Item.uLabelAlloc ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900800 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("yyyy"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800801 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900802 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800803
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900804 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800805 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900806 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800807 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530808 Item.uDataAlloc ||
809 Item.uLabelAlloc ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900810 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("another int")) ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800811 Item.uDataType != QCBOR_TYPE_INT64 ||
812 Item.val.int64 != 98)
813 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800814
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900815 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800816 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900817 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800818 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900819 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("text 2"))||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800820 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530821 Item.uDataAlloc ||
822 Item.uLabelAlloc ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900823 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("lies, damn lies and statistics"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800824 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900825 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800826
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800827 return 0;
828}
829
830
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900831/*
832 Decode and thoroughly check a moderately complex
833 set of maps
834 */
835int ParseMapAsArrayTest()
836{
837 QCBORDecodeContext DCtx;
838 QCBORItem Item;
839 int nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800840
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900841 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), QCBOR_DECODE_MODE_MAP_AS_ARRAY);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800842
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900843 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
844 return nCBORError;
845 }
Laurence Lundbladed61cbf32018-12-09 11:42:21 -0800846 if(Item.uDataType != QCBOR_TYPE_MAP_AS_ARRAY ||
847 Item.val.uCount != 6) {
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900848 return -1;
849 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800850
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900851 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
852 return nCBORError;
853 }
854 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
855 Item.uDataAlloc ||
856 Item.uLabelAlloc ||
857 Item.uLabelType != QCBOR_TYPE_NONE ||
858 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("first integer"))) {
859 return -2;
860 }
861
862 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
863 return nCBORError;
864 }
865 if(Item.uLabelType != QCBOR_TYPE_NONE ||
866 Item.uDataType != QCBOR_TYPE_INT64 ||
867 Item.val.int64 != 42 ||
868 Item.uDataAlloc ||
869 Item.uLabelAlloc) {
870 return -3;
871 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800872
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900873 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
874 return nCBORError;
875 }
876 if(Item.uLabelType != QCBOR_TYPE_NONE ||
877 Item.uDataAlloc ||
878 Item.uLabelAlloc ||
879 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("an array of two strings")) ||
880 Item.uDataType != QCBOR_TYPE_TEXT_STRING) {
881 return -4;
882 }
883
884 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
885 return nCBORError;
886 }
887 if(Item.uLabelType != QCBOR_TYPE_NONE ||
888 Item.uDataAlloc ||
889 Item.uLabelAlloc ||
890 Item.uDataType != QCBOR_TYPE_ARRAY ||
891 Item.val.uCount != 2) {
892 return -5;
893 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800894
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900895 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
896 return nCBORError;
897 }
898 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
899 Item.val.string.len != 7 ||
900 Item.uDataAlloc ||
901 Item.uLabelAlloc ||
902 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("string1"))) {
903 return -6;
904 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800905
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900906 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
907 return nCBORError;
908 }
909 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
910 Item.uDataAlloc ||
911 Item.uLabelAlloc ||
912 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("string2"))) {
913 return -7;
914 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800915
916
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900917 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
918 return nCBORError;
919 }
920 if(Item.uLabelType != QCBOR_TYPE_NONE ||
921 Item.uDataAlloc ||
922 Item.uLabelAlloc ||
923 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("map in a map"))) {
924 return -8;
925 }
926
927 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
928 return nCBORError;
929 }
930 if(Item.uLabelType != QCBOR_TYPE_NONE ||
931 Item.uDataAlloc ||
932 Item.uLabelAlloc ||
Laurence Lundbladed61cbf32018-12-09 11:42:21 -0800933 Item.uDataType != QCBOR_TYPE_MAP_AS_ARRAY ||
934 Item.val.uCount != 8) {
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900935 return -9;
936 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800937
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900938 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
939 return nCBORError;
940 }
941 if(Item.uLabelType != QCBOR_TYPE_NONE ||
942 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("bytes 1"))||
943 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
944 Item.uDataAlloc ||
945 Item.uLabelAlloc) {
946 return -10;
947 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800948
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900949 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
950 return nCBORError;
951 }
952 if(Item.uLabelType != QCBOR_TYPE_NONE ||
953 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
954 Item.uDataAlloc ||
955 Item.uLabelAlloc ||
956 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("xxxx"))) {
957 return -11;
958 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800959
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900960 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
961 return nCBORError;
962 }
963 if(Item.uLabelType != QCBOR_TYPE_NONE ||
964 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("bytes 2")) ||
965 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
966 Item.uDataAlloc ||
967 Item.uLabelAlloc) {
968 return -12;
969 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800970
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900971 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
972 return nCBORError;
973 }
974 if(Item.uLabelType != QCBOR_TYPE_NONE ||
975 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
976 Item.uDataAlloc ||
977 Item.uLabelAlloc ||
978 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("yyyy"))) {
979 return -13;
980 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800981
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900982 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
983 return nCBORError;
984 }
985 if(Item.uLabelType != QCBOR_TYPE_NONE ||
986 Item.uDataAlloc ||
987 Item.uLabelAlloc ||
988 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("another int")) ||
989 Item.uDataType != QCBOR_TYPE_TEXT_STRING) {
990 return -14;
991 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800992
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900993 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
994 return nCBORError;
995 }
996 if(Item.uLabelType != QCBOR_TYPE_NONE ||
997 Item.uDataAlloc ||
998 Item.uLabelAlloc ||
999 Item.uDataType != QCBOR_TYPE_INT64 ||
1000 Item.val.int64 != 98) {
1001 return -15;
1002 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001003
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001004 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
1005 return nCBORError;
1006 }
1007 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1008 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("text 2"))||
1009 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1010 Item.uDataAlloc ||
1011 Item.uLabelAlloc) {
1012 return -16;
1013 }
1014
1015 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
1016 return nCBORError;
1017 }
1018 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1019 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1020 Item.uDataAlloc ||
1021 Item.uLabelAlloc ||
1022 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("lies, damn lies and statistics"))) {
1023 return -17;
1024 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001025
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001026 return 0;
1027}
1028
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001029
1030/*
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301031 Fully or partially decode pValidMapEncoded. When
1032 partially decoding check for the right error code.
1033 How much partial decoding depends on nLevel.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001034
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301035 The partial decodes test error conditions of
1036 incomplete encoded input.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001037
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301038 This could be combined with the above test
1039 and made prettier and maybe a little more
1040 thorough.
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001041 */
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001042static int ExtraBytesTest(int nLevel)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001043{
1044 QCBORDecodeContext DCtx;
1045 QCBORItem Item;
1046 int nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001047
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001048 QCBORDecode_Init(&DCtx, (UsefulBufC){pValidMapEncoded, sizeof(pValidMapEncoded)}, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001049
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001050 if(nLevel < 1) {
1051 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_EXTRA_BYTES) {
1052 return -1;
1053 } else {
1054 return 0;
1055 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001056 }
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301057
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001058
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001059 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001060 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001061 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001062 if(Item.uDataType != QCBOR_TYPE_MAP ||
1063 Item.val.uCount != 3)
1064 return -1;
1065
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001066 if(nLevel < 2) {
1067 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
1068 return -1;
1069 } else {
1070 return 0;
1071 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001072 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001073
1074
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001075 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001076 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001077 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001078 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001079 Item.uDataType != QCBOR_TYPE_INT64 ||
1080 Item.val.uCount != 42 ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001081 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("first integer"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001082 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001083 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001084
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001085 if(nLevel < 3) {
1086 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
1087 return -1;
1088 } else {
1089 return 0;
1090 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001091 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001092
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001093 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001094 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001095 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001096 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001097 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("an array of two strings")) ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001098 Item.uDataType != QCBOR_TYPE_ARRAY ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001099 Item.val.uCount != 2) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001100 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001101 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001102
1103
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001104 if(nLevel < 4) {
1105 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
1106 return -1;
1107 } else {
1108 return 0;
1109 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001110 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001111
1112
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001113 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001114 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001115 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001116 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001117 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("string1"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001118 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001119 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001120
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001121 if(nLevel < 5) {
1122 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
1123 return -1;
1124 } else {
1125 return 0;
1126 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001127 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001128
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001129 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001130 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001131 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001132 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001133 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("string2"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001134 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001135 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001136
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001137 if(nLevel < 6) {
1138 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
1139 return -1;
1140 } else {
1141 return 0;
1142 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001143 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001144
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001145 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001146 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001147 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001148 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001149 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("map in a map")) ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001150 Item.uDataType != QCBOR_TYPE_MAP ||
1151 Item.val.uCount != 4)
1152 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001153
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001154 if(nLevel < 7) {
1155 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
1156 return -1;
1157 } else {
1158 return 0;
1159 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001160 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001161
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001162 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001163 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001164 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001165 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001166 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("bytes 1")) ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001167 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001168 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("xxxx"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001169 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001170 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001171
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001172 if(nLevel < 8) {
1173 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
1174 return -1;
1175 } else {
1176 return 0;
1177 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001178 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001179
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001180 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001181 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001182 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001183 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001184 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("bytes 2")) ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001185 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001186 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("yyyy"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001187 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001188 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001189
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001190 if(nLevel < 9) {
1191 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
1192 return -1;
1193 } else {
1194 return 0;
1195 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001196 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001197
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001198 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001199 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001200 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001201 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001202 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("another int")) ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001203 Item.uDataType != QCBOR_TYPE_INT64 ||
1204 Item.val.int64 != 98)
1205 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001206
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001207 if(nLevel < 10) {
1208 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
1209 return -1;
1210 } else {
1211 return 0;
1212 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001213 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001214
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001215 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001216 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001217 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001218 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001219 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("text 2"))||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001220 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001221 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("lies, damn lies and statistics"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001222 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001223 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001224
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301225 if(QCBORDecode_Finish(&DCtx)) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001226 return -1;
1227 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001228
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001229 return 0;
1230}
1231
1232
1233
1234
1235int ParseMapTest()
1236{
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301237 // Parse a moderatly complex map structure very thoroughl
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08001238 int n = ParseMapTest1(QCBOR_DECODE_MODE_NORMAL);
1239
1240 n = ParseMapTest1(QCBOR_DECODE_MODE_MAP_STRINGS_ONLY);
1241
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001242 if(!n) {
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001243 for(int i = 0; i < 10; i++) {
1244 n = ExtraBytesTest(i);
1245 if(n) {
1246 break;
1247 }
1248 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001249 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001250
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001251 return(n);
1252}
1253
1254
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07001255static 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 +08001256
1257int ParseSimpleTest()
1258{
1259 QCBORDecodeContext DCtx;
1260 QCBORItem Item;
1261 int nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001262
1263
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07001264 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSimpleValues), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001265
1266
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001267 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1268 return nCBORError;
1269 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
1270 Item.val.uCount != 10)
1271 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001272
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001273 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1274 return nCBORError;
1275 if(Item.uDataType != QCBOR_TYPE_FALSE)
1276 return -1;
1277
1278 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1279 return nCBORError;
1280 if(Item.uDataType != QCBOR_TYPE_TRUE)
1281 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001282
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001283 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1284 return nCBORError;
1285 if(Item.uDataType != QCBOR_TYPE_NULL)
1286 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001287
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001288 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1289 return nCBORError;
1290 if(Item.uDataType != QCBOR_TYPE_UNDEF)
1291 return -1;
1292
1293 // A break
Laurence Lundblade9e3651c2018-10-10 11:49:55 +08001294 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_BREAK)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001295 return -1;
1296
1297 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1298 return nCBORError;
1299 if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 0)
1300 return -1;
1301
1302 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1303 return nCBORError;
1304 if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 19)
1305 return -1;
1306
1307 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_INVALID_CBOR)
1308 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001309
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001310 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_INVALID_CBOR)
1311 return -1;
1312
1313 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_INVALID_CBOR)
1314 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001315
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001316 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1317 return nCBORError;
1318 if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 32)
1319 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001320
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001321 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1322 return nCBORError;
1323 if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 255)
1324 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001325
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001326 return 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001327
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001328}
1329
1330
1331struct FailInput {
1332 UsefulBufC Input;
1333 int nError;
1334};
1335
1336
1337struct FailInput Failures[] = {
1338 { {(uint8_t[]){0x18}, 1}, QCBOR_ERR_HIT_END }, // 1 byte integer missing the byte
1339 { {(uint8_t[]){0x1c}, 1}, QCBOR_ERR_UNSUPPORTED }, // Reserved additional info = 28
1340 { {(uint8_t[]){0x1d}, 1}, QCBOR_ERR_UNSUPPORTED }, // Reserved additional info = 29
1341 { {(uint8_t[]){0x1e}, 1}, QCBOR_ERR_UNSUPPORTED }, // Reserved additional info = 30
1342 { {(uint8_t[]){0x1f}, 1}, QCBOR_ERR_UNSUPPORTED }, // Indefinite length integer
1343 { {(uint8_t[]){0x3c}, 1}, QCBOR_ERR_UNSUPPORTED }, // 1 byte integer missing the byte
1344 { {(uint8_t[]){0x3d}, 1}, QCBOR_ERR_UNSUPPORTED }, // 1 byte integer missing the byte
1345 { {(uint8_t[]){0x3e}, 1}, QCBOR_ERR_UNSUPPORTED }, // 1 byte integer missing the byte
1346 { {(uint8_t[]){0x3f}, 1}, QCBOR_ERR_UNSUPPORTED }, // Indefinite length negative integer
1347 { {(uint8_t[]){0x41}, 1}, QCBOR_ERR_HIT_END }, // Short byte string
1348 { {(uint8_t[]){0x5c}, 1}, QCBOR_ERR_UNSUPPORTED }, // Reserved additional info = 28
1349 { {(uint8_t[]){0x5f}, 1}, QCBOR_ERR_UNSUPPORTED }, // Indefinite length byte string
1350 { {(uint8_t[]){0x61}, 1}, QCBOR_ERR_HIT_END }, // Short UTF-8 string
1351 { {(uint8_t[]){0x7c}, 1}, QCBOR_ERR_UNSUPPORTED }, // Reserved additional info = 28
1352 { {(uint8_t[]){0x7f}, 1}, QCBOR_ERR_UNSUPPORTED }, // Indefinite length UTF-8 string
1353 { {(uint8_t[]){0xff}, 1}, QCBOR_ERR_UNSUPPORTED } , // break
1354 { {(uint8_t[]){0xf8, 0x00}, 2}, QCBOR_ERR_INVALID_CBOR }, // An invalid encoding of a simple type
1355 { {(uint8_t[]){0xf8, 0x1f}, 2}, QCBOR_ERR_INVALID_CBOR }, // An invalid encoding of a simple type
1356 { {(uint8_t[]){0xc0, 0x00}, 2}, QCBOR_ERR_BAD_OPT_TAG }, // Text-based date, with an integer
1357 { {(uint8_t[]){0xc1, 0x41, 0x33}, 3}, QCBOR_ERR_BAD_OPT_TAG }, // Epoch date, with an byte string
1358 { {(uint8_t[]){0xc1, 0xc0, 0x00}, 3}, QCBOR_ERR_BAD_OPT_TAG }, // tagged as both epoch and string dates
1359 { {(uint8_t[]){0xc2, 0x00}, 2}, QCBOR_ERR_BAD_OPT_TAG } // big num tagged an int, not a byte string
1360
1361};
1362
1363
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001364int FailureTests()
1365{
1366 int nResult = 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001367
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001368 struct FailInput *pFEnd = &Failures[0] + sizeof(Failures)/sizeof(struct FailInput);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001369
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001370 for(struct FailInput *pF = &Failures[0]; pF < pFEnd ;pF++) {
1371 QCBORDecodeContext DCtx;
1372 QCBORItem Item;
1373 int nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001374
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001375 QCBORDecode_Init(&DCtx, pF->Input, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001376
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001377 while(1) {
1378 nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
1379 if(QCBOR_ERR_HIT_END == nCBORError) {
1380 break;
1381 }
1382 if(nCBORError != pF->nError) {
1383 nResult = 1;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001384 break;
1385 }
1386 }
1387 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001388
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001389 {
1390 QCBORDecodeContext DCtx;
1391 QCBORItem Item;
1392 int nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001393
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07001394 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSimpleValues), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001395
1396 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1397 return nCBORError;
1398 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
1399 Item.val.uCount != 10)
1400 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001401
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001402 DCtx.InBuf.magic = 0; // Corrupt the UsefulInputBuf
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001403
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001404 nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
1405 if(nCBORError != QCBOR_ERR_HIT_END)
1406 return -1;
1407 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001408
1409
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001410 return nResult;
1411}
1412
1413
Laurence Lundbladea2e29072018-12-30 09:20:06 -08001414/* Try all 256 values of the byte at nLen including recursing for
1415 each of the values to try values at nLen+1 ... up to nLenMax
1416 */
1417static void ComprehensiveInputRecurser(uint8_t *pBuf, int nLen, int nLenMax)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001418{
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001419 if(nLen >= nLenMax) {
1420 return;
1421 }
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08001422
Laurence Lundbladea2e29072018-12-30 09:20:06 -08001423 for(int inputByte = 0; inputByte < 256; inputByte++) {
1424 // Set up the input
1425 pBuf[nLen] = inputByte;
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08001426 const UsefulBufC Input = {pBuf, nLen+1};
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08001427
Laurence Lundbladea2e29072018-12-30 09:20:06 -08001428 // Get ready to parse
1429 QCBORDecodeContext DCtx;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001430 QCBORDecode_Init(&DCtx, Input, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001431
Laurence Lundbladea2e29072018-12-30 09:20:06 -08001432 // Parse by getting the next item until an error occurs
1433 // Just about every possible decoder error can occur here
1434 // The goal of this test is not to check for the correct
1435 // error since that is not really possible. It is to
1436 // see that there is no crash on hostile input.
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001437 while(1) {
Laurence Lundbladea2e29072018-12-30 09:20:06 -08001438 QCBORItem Item;
1439 QCBORError nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001440 if(nCBORError != QCBOR_SUCCESS) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001441 break;
1442 }
1443 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001444
Laurence Lundbladea2e29072018-12-30 09:20:06 -08001445 ComprehensiveInputRecurser(pBuf, nLen+1, nLenMax);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001446 }
1447}
1448
1449
1450/*
Laurence Lundbladea2e29072018-12-30 09:20:06 -08001451 Public function for initialization. See header qcbor.h
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001452 */
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001453int ComprehensiveInputTest()
1454{
Laurence Lundbladea2e29072018-12-30 09:20:06 -08001455 // Size 2 tests 64K inputs and runs quickly
1456 uint8_t pBuf[2];
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001457
Laurence Lundbladea2e29072018-12-30 09:20:06 -08001458 ComprehensiveInputRecurser(pBuf, 0, sizeof(pBuf));
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001459
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001460 return 0;
1461}
1462
Laurence Lundbladea2e29072018-12-30 09:20:06 -08001463
1464/*
1465 Public function for initialization. See header qcbor.h
1466 */
1467int BigComprehensiveInputTest()
1468{
1469 // size 3 tests 16 million inputs and runs OK
1470 // in seconds on fast machines. Size 4 takes
1471 // 10+ minutes and 5 half a day on fast
1472 // machines. This test is kept separate from
1473 // the others so as to no slow down the use
1474 // of them as a very frequent regression.
1475 uint8_t pBuf[3]; //
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08001476
Laurence Lundbladea2e29072018-12-30 09:20:06 -08001477 ComprehensiveInputRecurser(pBuf, 0, sizeof(pBuf));
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08001478
Laurence Lundbladea2e29072018-12-30 09:20:06 -08001479 return 0;
1480}
1481
1482
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07001483static uint8_t spDateTestInput[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001484 0xc0, // tag for string date
1485 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001486
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001487 0xc1, // tag for epoch date
1488 0x1a, 0x53, 0x72, 0x4E, 0x00, // Epoch date 1400000000; Tue, 13 May 2014 16:53:20 GMT
1489
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001490 // CBOR_TAG_B64
1491 0xc1, 0xcf, 0xd8, 0x22, // 0xee, // Epoch date with extra tags TODO: fix this test
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001492 0x1a, 0x53, 0x72, 0x4E, 0x01,
1493
1494 0xc1, // tag for epoch date
1495 0x1b, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, // Too large integer
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001496
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001497 0xc1, // tag for epoch date
1498 0xfa, 0x3f, 0x8c, 0xcc, 0xcd, // double with value 1.1
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001499
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001500 0xc1, // tag for epoch date
1501 0xfa, 0x7f, 0x7f, 0xff, 0xff // 3.4028234663852886e+38 too large
1502
1503};
1504
1505
1506// have to check float expected only to within an epsilon
1507int CHECK_EXPECTED_DOUBLE(double val, double expected) {
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001508
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001509 double diff = val - expected;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001510
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001511 diff = fabs(diff);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001512
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001513 return diff > 0.0000001;
1514}
1515
1516
1517int DateParseTest()
1518{
1519 QCBORDecodeContext DCtx;
1520 QCBORItem Item;
1521 int nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001522
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07001523 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spDateTestInput), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001524
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001525 const uint64_t uTags[] = {15};
1526 QCBORTagListIn TagList = {1, uTags};
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001527
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001528 QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TagList);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001529
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001530 // String date
1531 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1532 return -1;
1533 if(Item.uDataType != QCBOR_TYPE_DATE_STRING ||
Laurence Lundblade9e3651c2018-10-10 11:49:55 +08001534 UsefulBuf_Compare(Item.val.dateString, UsefulBuf_FromSZ("1985-04-12"))){
Laurence Lundblade67bd5512018-11-02 21:44:06 +07001535 return -2;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001536 }
1537
1538 // Epoch date
1539 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade67bd5512018-11-02 21:44:06 +07001540 return -3;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001541 if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH ||
1542 Item.val.epochDate.nSeconds != 1400000000 ||
1543 Item.val.epochDate.fSecondsFraction != 0 ) {
Laurence Lundblade67bd5512018-11-02 21:44:06 +07001544 return -4;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001545 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001546
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001547 // Epoch date with extra CBOR_TAG_B64 tag that doesn't really mean anything
1548 // but want to be sure extra tag doesn't cause a problem
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001549 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade67bd5512018-11-02 21:44:06 +07001550 return -5;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001551 if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH ||
1552 Item.val.epochDate.nSeconds != 1400000001 ||
1553 Item.val.epochDate.fSecondsFraction != 0 ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001554 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_B64)) {
Laurence Lundblade67bd5512018-11-02 21:44:06 +07001555 return -6;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001556 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001557
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001558 // Epoch date that is too large for our representation
1559 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_DATE_OVERFLOW) {
Laurence Lundblade67bd5512018-11-02 21:44:06 +07001560 return -7;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001561 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001562
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001563 // Epoch date in float format with fractional seconds
1564 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade67bd5512018-11-02 21:44:06 +07001565 return -8;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001566 if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH ||
1567 Item.val.epochDate.nSeconds != 1 ||
1568 CHECK_EXPECTED_DOUBLE(Item.val.epochDate.fSecondsFraction, 0.1 )) {
Laurence Lundblade67bd5512018-11-02 21:44:06 +07001569 return -9;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001570 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001571
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001572 // Epoch date float that is too large for our representation
1573 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_DATE_OVERFLOW) {
Laurence Lundblade67bd5512018-11-02 21:44:06 +07001574 return -10;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001575 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001576
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001577 // TODO: could use a few more tests with float, double, and half precsion and negative (but coverage is still pretty good)
1578
1579 return 0;
1580}
1581
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001582// Really simple basic input for tagging test
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07001583static uint8_t spOptTestInput[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001584 0xd9, 0xd9, 0xf7, // CBOR magic number
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001585 0x81, // Array of one
1586 0xd8, 0x04, // non-preferred serialization of tag 4
1587 0x82, 0x01, 0x03}; // fraction 1/3
1588
1589static uint8_t spEncodedLargeTag[] = {0xdb, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x80};
1590
1591// 0x9192939495969798, 0x88, 0x01, 0x04
1592static uint8_t spLotsOfTags[] = {0xdb, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0xd8, 0x88, 0xc5, 0xc4, 0x80};
1593
1594/*
1595 The cbor.me parse of this.
1596 55799(55799(55799({6(7(-23)): 5859837686836516696(7({7(-20): 11({17(-18): 17(17(17("Organization"))),
1597 9(-17): 773("SSG"), -15: 4(5(6(7(8(9(10(11(12(13(14(15("Confusion")))))))))))), 17(-16): 17("San Diego"),
1598 17(-14): 17("US")}), 23(-19): 19({-11: 9({-9: -7}),
1599 90599561(90599561(90599561(-10))): 12(h'0102030405060708090A')})})),
1600 16(-22): 23({11(8(7(-5))): 8(-3)})})))
1601 */
1602static uint8_t spCSRWithTags[] = {
1603 0xd9, 0xd9, 0xf7, 0xd9, 0xd9, 0xf7, 0xd9, 0xd9, 0xf7, 0xa2,
1604 0xc6, 0xc7, 0x36,
1605 0xdb, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0xc7, 0xa2,
1606 0xda, 0x00, 0x00, 0x00, 0x07, 0x33,
1607 0xcb, 0xa5,
1608 0xd1, 0x31,
1609 0xd1, 0xd1, 0xd1, 0x6c,
1610 0x4f, 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74, 0x69, 0x6f, 0x6e,
1611 0xc9, 0x30,
1612 0xd9, 0x03, 0x05, 0x63,
1613 0x53, 0x53, 0x47,
1614 0x2e,
1615 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf, 0x69,
1616 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73, 0x69, 0x6f, 0x6e,
1617 0xd1, 0x2f,
1618 0xd1, 0x69,
1619 0x53, 0x61, 0x6e, 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f,
1620 0xd1, 0x2d,
1621 0xd1, 0x62,
1622 0x55, 0x53,
1623 0xd7, 0x32,
1624 0xd3, 0xa2,
1625 0x2a,
1626 0xc9, 0xa1,
1627 0x28,
1628 0x26,
1629 0xda, 0x05, 0x66, 0x70, 0x89, 0xda, 0x05, 0x66, 0x70, 0x89, 0xda, 0x05, 0x66, 0x70, 0x89, 0x29,
1630 0xcc, 0x4a,
1631 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,0x07, 0x08, 0x09, 0x0a,
1632 0xd0, 0x35,
1633 0xd7, 0xa1,
1634 0xcb, 0xc8, 0xc7, 0x24,
1635 0xc8, 0x22};
1636
1637static int CheckCSRMaps(QCBORDecodeContext *pDC);
1638
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001639
1640int OptTagParseTest()
1641{
1642 QCBORDecodeContext DCtx;
1643 QCBORItem Item;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001644
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07001645 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spOptTestInput), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001646
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001647 //-------------------------
1648 // This text matches the magic number tag and the fraction tag
1649 if(QCBORDecode_GetNext(&DCtx, &Item)) {
1650 return -2;
1651 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001652 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001653 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC)) {
1654 return -3;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001655 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001656
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001657 if(QCBORDecode_GetNext(&DCtx, &Item)) {
1658 return -4;
1659 }
1660 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
1661 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_FRACTION) ||
1662 Item.val.uCount != 2) {
1663 return -5;
1664 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001665
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001666 // --------------------------------
1667 // This test decodes the very large tag, but it is not in
1668 // any list so it is ignored.
1669 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEncodedLargeTag), QCBOR_DECODE_MODE_NORMAL);
1670 if(QCBORDecode_GetNext(&DCtx, &Item)) {
1671 return -6;
1672 }
1673 if(Item.uTagBits) {
1674 return -7;
1675 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001676
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001677 // ----------------------------------
1678 // This test sets up a caller-config list that includes the very large tage and then matches it.
1679 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEncodedLargeTag), QCBOR_DECODE_MODE_NORMAL);
1680 const uint64_t puList[] = {0x9192939495969798, 257};
1681 const QCBORTagListIn TL = {2, puList};
1682 QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001683
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001684 if(QCBORDecode_GetNext(&DCtx, &Item)) {
1685 return -8;
1686 }
1687 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
1688 !QCBORDecode_IsTagged(&DCtx, &Item, 0x9192939495969798) ||
1689 QCBORDecode_IsTagged(&DCtx, &Item, 257) ||
1690 QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_BIGFLOAT) ||
1691 Item.val.uCount != 0) {
1692 return -9;
1693 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001694
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001695 //------------------------
1696 // This test sets up a caller-configured list, and looks up something not in it
1697 const uint64_t puLongList[17] = {1,2,1};
1698 const QCBORTagListIn TLLong = {17, puLongList};
1699 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEncodedLargeTag), QCBOR_DECODE_MODE_NORMAL);
1700 QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TLLong);
1701 if(QCBORDecode_GetNext(&DCtx, &Item)) {
1702 return -11;
1703 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001704
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001705 // -----------------------
1706 // This tests retrievel of the full tag list
1707 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spLotsOfTags), QCBOR_DECODE_MODE_NORMAL);
1708 uint64_t puTags[16];
1709 QCBORTagListOut Out = {0, 4, puTags};
1710 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
1711 return -12;
1712 }
1713 if(puTags[0] != 0x9192939495969798 ||
1714 puTags[1] != 0x88 ||
1715 puTags[2] != 0x05 ||
1716 puTags[3] != 0x04) {
1717 return -13;
1718 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001719
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001720 // ----------------------
1721 // This text if too small of an out list
1722 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spLotsOfTags), QCBOR_DECODE_MODE_NORMAL);
1723 QCBORTagListOut OutSmall = {0, 3, puTags};
1724 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &OutSmall) != QCBOR_ERR_TOO_MANY_TAGS) {
1725 return -14;
1726 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001727
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001728 // ---------------
1729 // Parse a version of the "CSR" that has had a ton of tags randomly inserted
1730 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRWithTags), QCBOR_DECODE_MODE_NORMAL);
1731 int n = CheckCSRMaps(&DCtx);
1732 if(n) {
1733 return n-2000;
1734 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001735
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001736 Out = (QCBORTagListOut){0,16, puTags};
1737 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRWithTags), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001738
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001739 const uint64_t puTagList[] = {773, 1, 90599561};
1740 const QCBORTagListIn TagList = {3, puTagList};
1741 QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TagList);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001742
1743
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001744 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
1745 return -100;
1746 }
1747 if(Item.uDataType != QCBOR_TYPE_MAP ||
1748 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC) ||
1749 QCBORDecode_IsTagged(&DCtx, &Item, 90599561) ||
1750 QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_DATE_EPOCH) ||
1751 Item.val.uCount != 2 ||
1752 puTags[0] != CBOR_TAG_CBOR_MAGIC ||
1753 puTags[1] != CBOR_TAG_CBOR_MAGIC ||
1754 puTags[2] != CBOR_TAG_CBOR_MAGIC ||
1755 Out.uNumUsed != 3) {
1756 return -101;
1757 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001758
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001759 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
1760 return -102;
1761 }
1762 if(Item.uDataType != QCBOR_TYPE_MAP ||
1763 QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC) ||
1764 QCBORDecode_IsTagged(&DCtx, &Item, 6) ||
1765 QCBORDecode_IsTagged(&DCtx, &Item, 7) || // item is tagged 7, but 7 is not configured to be recognized
1766 Item.val.uCount != 2 ||
1767 puTags[0] != 5859837686836516696 ||
1768 puTags[1] != 7 ||
1769 Out.uNumUsed != 2) {
1770 return -103;
1771 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001772
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001773 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
1774 return -104;
1775 }
1776 if(Item.uDataType != QCBOR_TYPE_MAP ||
1777 Item.uTagBits ||
1778 Item.val.uCount != 5 ||
1779 puTags[0] != 0x0b ||
1780 Out.uNumUsed != 1) {
1781 return -105;
1782 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001783
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001784 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
1785 return -106;
1786 }
1787 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1788 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_COSE_MAC0) ||
1789 Item.val.string.len != 12 ||
1790 puTags[0] != CBOR_TAG_COSE_MAC0 ||
1791 puTags[1] != CBOR_TAG_COSE_MAC0 ||
1792 puTags[2] != CBOR_TAG_COSE_MAC0 ||
1793 Out.uNumUsed != 3) {
1794 return -105;
1795 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001796
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001797 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
1798 return -107;
1799 }
1800 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1801 !QCBORDecode_IsTagged(&DCtx, &Item, 773) ||
1802 Item.val.string.len != 3 ||
1803 puTags[0] != 773 ||
1804 Out.uNumUsed != 1) {
1805 return -108;
1806 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001807
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001808 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
1809 return -109;
1810 }
1811 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1812 !QCBORDecode_IsTagged(&DCtx, &Item, 4) ||
1813 Item.val.string.len != 9 ||
1814 puTags[0] != 4 ||
1815 puTags[11] != 0x0f ||
1816 Out.uNumUsed != 12) {
1817 return -110;
1818 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001819
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001820 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
1821 return -111;
1822 }
1823 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1824 !QCBORDecode_IsTagged(&DCtx, &Item, 17) ||
1825 Item.val.string.len != 9 ||
1826 puTags[0] != 17 ||
1827 Out.uNumUsed != 1) {
1828 return -112;
1829 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001830
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001831 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
1832 return -111;
1833 }
1834 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1835 !QCBORDecode_IsTagged(&DCtx, &Item, 17) ||
1836 Item.val.string.len != 2 ||
1837 puTags[0] != 17 ||
1838 Out.uNumUsed != 1) {
1839 return -112;
1840 }
1841
1842 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
1843 return -113;
1844 }
1845 if(Item.uDataType != QCBOR_TYPE_MAP ||
1846 QCBORDecode_IsTagged(&DCtx, &Item, 19) ||
1847 Item.val.uCount != 2 ||
1848 puTags[0] != 19 ||
1849 Out.uNumUsed != 1) {
1850 return -114;
1851 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001852
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001853 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
1854 return -115;
1855 }
1856 if(Item.uDataType != QCBOR_TYPE_MAP ||
1857 QCBORDecode_IsTagged(&DCtx, &Item, 9) ||
1858 Item.uTagBits ||
1859 Item.val.uCount != 1 ||
1860 puTags[0] != 9 ||
1861 Out.uNumUsed != 1) {
1862 return -116;
1863 }
1864
1865 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
1866 return -116;
1867 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001868 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001869 Item.val.int64 != -7 ||
1870 Item.uTagBits ||
1871 Out.uNumUsed != 0) {
1872 return -117;
1873 }
1874
1875 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
1876 return -118;
1877 }
1878 if(Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
1879 Item.val.string.len != 10 ||
1880 Item.uTagBits ||
1881 puTags[0] != 12 ||
1882 Out.uNumUsed != 1) {
1883 return -119;
1884 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001885
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001886 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
1887 return -120;
1888 }
1889 if(Item.uDataType != QCBOR_TYPE_MAP ||
1890 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_ENC_AS_B16) ||
1891 Item.val.uCount != 1 ||
1892 puTags[0] != 0x17 ||
1893 Out.uNumUsed != 1) {
1894 return -121;
1895 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001896
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001897 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
1898 return -122;
1899 }
1900 if(Item.uDataType != QCBOR_TYPE_INT64 ||
1901 QCBORDecode_IsTagged(&DCtx, &Item, 8) ||
1902 Item.val.int64 != -3 ||
1903 puTags[0] != 8 ||
1904 Out.uNumUsed != 1) {
1905 return -123;
1906 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001907
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001908 if(QCBORDecode_Finish(&DCtx)) {
1909 return -124;
1910 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001911
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001912 return 0;
1913}
1914
1915
1916
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001917
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07001918static uint8_t spBigNumInput[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001919 0x83,
1920 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1921 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1922 0xA4,
1923 0x63, 0x42, 0x4E, 0x2B,
1924 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1925 0x18, 0x40,
1926 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1927 0x63, 0x42, 0x4E, 0x2D,
1928 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1929 0x38, 0x3F,
1930 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
1931
1932
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07001933static uint8_t spBigNum[] = {0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001934
1935
1936int BignumParseTest()
1937{
1938 QCBORDecodeContext DCtx;
1939 QCBORItem Item;
1940 int nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001941
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07001942 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNumInput), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001943
1944
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001945 //
1946 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1947 return -1;
1948 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
1949 return -1;
1950 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001951
1952 //
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001953 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1954 return -1;
1955 if(Item.uDataType != QCBOR_TYPE_POSBIGNUM ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07001956 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001957 return -1;
1958 }
1959
1960 //
1961 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1962 return -1;
1963 if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07001964 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001965 return -1;
1966 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001967
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001968 //
1969 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1970 return -1;
1971 if(Item.uDataType != QCBOR_TYPE_MAP) {
1972 return -1;
1973 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001974
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001975 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1976 return -1;
1977 if(Item.uDataType != QCBOR_TYPE_POSBIGNUM ||
1978 Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07001979 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001980 return -1;
1981 }
1982
1983 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1984 return -1;
1985 if(Item.uDataType != QCBOR_TYPE_POSBIGNUM ||
1986 Item.uLabelType != QCBOR_TYPE_INT64 ||
1987 Item.label.int64 != 64 ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07001988 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001989 return -1;
1990 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001991
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001992 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1993 return -1;
1994 if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM ||
1995 Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07001996 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001997 return -1;
1998 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001999
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002000 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
2001 return -1;
2002 if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM ||
2003 Item.uLabelType != QCBOR_TYPE_INT64 ||
2004 Item.label.int64 != -64 ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002005 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002006 return -1;
2007 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002008
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002009 return 0;
2010}
2011
2012
2013
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302014static 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 +08002015{
2016 QCBORItem Item;
2017 int nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002018
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002019 if((nCBORError = QCBORDecode_GetNext(pCtx, &Item))) return -1;
2020 if(Item.uDataType != uDataType) return -1;
2021 if(uNestingLevel > 0) {
2022 if(Item.uLabelType != QCBOR_TYPE_INT64 && Item.uLabelType != QCBOR_TYPE_UINT64) return -1;
2023 if(Item.uLabelType == QCBOR_TYPE_INT64) {
2024 if(Item.label.int64 != nLabel) return -1;
2025 } else {
Laurence Lundblade570fab52018-10-13 18:28:27 +08002026 if(Item.label.uint64 != (uint64_t)nLabel) return -1;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002027 }
2028 }
2029 if(Item.uNestingLevel != uNestingLevel) return -1;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302030 if(Item.uNextNestLevel != uNextNest) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002031
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002032 if(pItem) {
2033 *pItem = Item;
2034 }
2035 return 0;
2036}
2037
2038
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002039// Same code checks definite and indefinite length versions of the map
2040static int CheckCSRMaps(QCBORDecodeContext *pDC)
2041{
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302042 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 0, 1, 0, NULL)) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002043
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302044 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 1, 2, -23, NULL)) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002045
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302046 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 2, 3, -20, NULL)) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002047
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302048 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -18, NULL)) return -1;
2049 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -17, NULL)) return -1;
2050 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -15, NULL)) return -1;
2051 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -16, NULL)) return -1;
2052 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 2, -14, NULL)) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002053
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302054 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 2, 3, -19, NULL)) return -1;
2055 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 3, 4, -11, NULL)) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002056
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302057 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_INT64, 4, 3, -9, NULL)) return -1;
2058 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_BYTE_STRING, 3, 1, -10, NULL)) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002059
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302060 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 1, 2, -22, NULL)) return -1;
2061 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_INT64, 2, 0, -5, NULL)) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002062
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002063 if(QCBORDecode_Finish(pDC)) return -2;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002064
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002065 return 0;
2066}
2067
2068
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002069/*
2070// cbor.me decoded output
2071{
2072 -23: {
2073 -20: {
2074 -18: "Organization",
2075 -17: "SSG",
2076 -15: "Confusion",
2077 -16: "San Diego",
2078 -14: "US"
2079 },
2080 -19: {
2081 -11: {
2082 -9: -7
2083 },
2084 -10: '\u0001\u0002\u0003\u0004\u0005\u0006\a\b\t\n'
2085 }
2086 },
2087 -22: {
2088 -5: -3
2089 }
2090}
2091 */
2092
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002093
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002094static uint8_t spCSRInput[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002095 0xa2, 0x36, 0xa2, 0x33, 0xa5, 0x31, 0x6c, 0x4f,
2096 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74,
2097 0x69, 0x6f, 0x6e, 0x30, 0x63, 0x53, 0x53, 0x47,
2098 0x2e, 0x69, 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73,
2099 0x69, 0x6f, 0x6e, 0x2f, 0x69, 0x53, 0x61, 0x6e,
2100 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f, 0x2d, 0x62,
2101 0x55, 0x53, 0x32, 0xa2, 0x2a, 0xa1, 0x28, 0x26,
2102 0x29, 0x4a, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
2103 0x07, 0x08, 0x09, 0x0a, 0x35, 0xa1, 0x24, 0x22};
2104
2105int NestedMapTest()
2106{
2107 QCBORDecodeContext DCtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002108
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002109 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002110
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002111 return CheckCSRMaps(&DCtx);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002112}
2113
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08002114
2115
2116int StringDecoderModeFailTest()
2117{
2118 QCBORDecodeContext DCtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002119
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08002120 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput), QCBOR_DECODE_MODE_MAP_STRINGS_ONLY);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002121
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08002122 QCBORItem Item;
2123 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002124
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08002125 if(QCBORDecode_GetNext(&DCtx, &Item)) {
2126 return -1;
2127 }
2128 if(Item.uDataType != QCBOR_TYPE_MAP) {
2129 return -2;
2130 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002131
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08002132 nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
2133 if(nCBORError != QCBOR_ERR_MAP_LABEL_TYPE) {
2134 return -3;
2135 }
2136
2137 return 0;
2138}
2139
2140
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002141// Same map as above, but using indefinite lengths
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002142static uint8_t spCSRInputIndefLen[] = {
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002143 0xbf, 0x36, 0xbf, 0x33, 0xbf, 0x31, 0x6c, 0x4f,
2144 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74,
2145 0x69, 0x6f, 0x6e, 0x30, 0x63, 0x53, 0x53, 0x47,
2146 0x2e, 0x69, 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73,
2147 0x69, 0x6f, 0x6e, 0x2f, 0x69, 0x53, 0x61, 0x6e,
2148 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f, 0x2d, 0x62,
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002149 0x55, 0x53, 0xff, 0x32, 0xbf, 0x2a, 0xbf, 0x28,
2150 0x26, 0xff, 0x29, 0x4a, 0x01, 0x02, 0x03, 0x04,
2151 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0xff, 0xff,
2152 0x35, 0xbf, 0x24, 0x22, 0xff, 0xff};
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002153
2154int NestedMapTestIndefLen()
2155{
2156 QCBORDecodeContext DCtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002157
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002158 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInputIndefLen), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002159
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002160 return CheckCSRMaps(&DCtx);
2161}
2162
2163
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002164
Laurence Lundblade17ede402018-10-13 11:43:07 +08002165static UsefulBufC make_nested_indefinite_arrays(int n, UsefulBuf Storage)
2166{
2167 UsefulOutBuf UOB;
2168 UsefulOutBuf_Init(&UOB, Storage);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002169
Laurence Lundblade17ede402018-10-13 11:43:07 +08002170 int i;
2171 for(i = 0; i < n; i++) {
2172 UsefulOutBuf_AppendByte(&UOB, 0x9f);
2173 }
2174
2175 for(i = 0; i < n; i++) {
2176 UsefulOutBuf_AppendByte(&UOB, 0xff);
2177 }
2178 return UsefulOutBuf_OutUBuf(&UOB);
2179}
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002180
2181
Laurence Lundblade17ede402018-10-13 11:43:07 +08002182static int parse_indeflen_nested(UsefulBufC Nested, int nNestLevel)
2183{
2184 QCBORDecodeContext DC;
2185 QCBORDecode_Init(&DC, Nested, 0);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002186
Laurence Lundblade17ede402018-10-13 11:43:07 +08002187 int j;
2188 for(j = 0; j < nNestLevel; j++) {
2189 QCBORItem Item;
2190 int nReturn = QCBORDecode_GetNext(&DC, &Item);
2191 if(j >= QCBOR_MAX_ARRAY_NESTING) {
2192 // Should be in error
2193 if(nReturn != QCBOR_ERR_ARRAY_NESTING_TOO_DEEP) {
2194 return -4;
2195 } else {
2196 return 0; // Decoding doesn't recover after an error
2197 }
2198 } else {
2199 // Should be no error
2200 if(nReturn) {
2201 return -9; // Should not have got an error
2202 }
2203 }
2204 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
2205 return -7;
2206 }
2207 }
2208 int nReturn = QCBORDecode_Finish(&DC);
2209 if(nReturn) {
2210 return -3;
2211 }
2212 return 0;
2213}
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002214
2215
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302216int IndefiniteLengthNestTest()
Laurence Lundblade17ede402018-10-13 11:43:07 +08002217{
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05302218 UsefulBuf_MAKE_STACK_UB(Storage, 50);
Laurence Lundblade17ede402018-10-13 11:43:07 +08002219 int i;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002220 for(i=1; i < QCBOR_MAX_ARRAY_NESTING+4; i++) {
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08002221 const UsefulBufC Nested = make_nested_indefinite_arrays(i, Storage);
Laurence Lundblade17ede402018-10-13 11:43:07 +08002222 int nReturn = parse_indeflen_nested(Nested, i);
2223 if(nReturn) {
2224 return nReturn;
2225 }
2226 }
2227 return 0;
2228}
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002229
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002230
Laurence Lundblade6de37062018-10-15 12:22:42 +05302231
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002232static const uint8_t spIndefiniteArray[] = {0x9f, 0x01, 0x82, 0x02, 0x03, 0xff}; // [1, [2, 3]]
2233static const uint8_t spIndefiniteArrayBad1[] = {0x9f}; // No closing break
2234static const uint8_t spIndefiniteArrayBad2[] = {0x9f, 0x9f, 0x02, 0xff}; // Not enough closing breaks
2235static const uint8_t spIndefiniteArrayBad3[] = {0x9f, 0x02, 0xff, 0xff}; // Too many closing breaks
2236static const uint8_t spIndefiniteArrayBad4[] = {0x81, 0x9f}; // Unclosed indeflen inside def len
2237static const uint8_t spIndefiniteArrayBad5[] = {0x9f, 0xd1, 0xff}; // confused tag
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002238
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302239int IndefiniteLengthArrayMapTest()
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002240{
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002241 int nResult;
2242 // --- first test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002243 UsefulBufC IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArray);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002244
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002245 // Decode it and see if it is OK
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05302246 UsefulBuf_MAKE_STACK_UB(MemPool, 150);
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002247 QCBORDecodeContext DC;
2248 QCBORItem Item;
2249 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002250
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002251 QCBORDecode_SetMemPool(&DC, MemPool, false);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002252
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002253 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05302254
2255 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
2256 Item.uNestingLevel != 0 ||
2257 Item.uNextNestLevel != 1) {
2258 return -111;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002259 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002260
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002261 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05302262 if(Item.uDataType != QCBOR_TYPE_INT64 ||
2263 Item.uNestingLevel != 1 ||
2264 Item.uNextNestLevel != 1) {
2265 return -2;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002266 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002267
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002268 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05302269 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
2270 Item.uNestingLevel != 1 ||
2271 Item.uNextNestLevel != 2) {
2272 return -3;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002273 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002274
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002275 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade12b495d2018-12-17 11:15:54 -08002276 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade6de37062018-10-15 12:22:42 +05302277 Item.uNestingLevel != 2 ||
2278 Item.uNextNestLevel != 2) {
2279 return -4;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002280 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002281
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002282 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade12b495d2018-12-17 11:15:54 -08002283 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade6de37062018-10-15 12:22:42 +05302284 Item.uNestingLevel != 2 ||
2285 Item.uNextNestLevel != 0) {
2286 return -5;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002287 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002288
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002289 if(QCBORDecode_Finish(&DC)) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05302290 return -6;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002291 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002292
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002293 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002294 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad1);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002295
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002296 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002297
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002298 QCBORDecode_SetMemPool(&DC, MemPool, false);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002299
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002300 nResult = QCBORDecode_GetNext(&DC, &Item);
2301 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05302302 return -7;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002303 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002304
Laurence Lundblade570fab52018-10-13 18:28:27 +08002305 nResult = QCBORDecode_Finish(&DC);
Laurence Lundblade6de37062018-10-15 12:22:42 +05302306 if(nResult != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
2307 return -8;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002308 }
2309
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002310
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002311 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002312 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad2);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002313
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002314 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002315
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002316 QCBORDecode_SetMemPool(&DC, MemPool, false);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002317
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002318 nResult = QCBORDecode_GetNext(&DC, &Item);
2319 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05302320 return -9;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002321 }
2322
2323 nResult = QCBORDecode_GetNext(&DC, &Item);
2324 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05302325 return -10;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002326 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002327
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002328 nResult = QCBORDecode_GetNext(&DC, &Item);
2329 if(nResult || Item.uDataType != QCBOR_TYPE_INT64) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05302330 return -11;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002331 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002332
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002333 nResult = QCBORDecode_Finish(&DC);
Laurence Lundblade6de37062018-10-15 12:22:42 +05302334 if(nResult != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
2335 return -12;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002336 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002337
2338
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002339 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002340 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad3);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002341
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002342 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002343
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002344 QCBORDecode_SetMemPool(&DC, MemPool, false);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002345
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002346 nResult = QCBORDecode_GetNext(&DC, &Item);
2347 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05302348 return -13;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002349 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002350
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002351 nResult = QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05302352 if(nResult != QCBOR_ERR_BAD_BREAK) {
2353 return -14;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002354 }
Laurence Lundblade6de37062018-10-15 12:22:42 +05302355
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002356
Laurence Lundblade570fab52018-10-13 18:28:27 +08002357 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002358 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad4);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002359
Laurence Lundblade570fab52018-10-13 18:28:27 +08002360 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002361
Laurence Lundblade570fab52018-10-13 18:28:27 +08002362 QCBORDecode_SetMemPool(&DC, MemPool, false);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002363
Laurence Lundblade570fab52018-10-13 18:28:27 +08002364 nResult = QCBORDecode_GetNext(&DC, &Item);
2365 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05302366 return -15;
Laurence Lundblade570fab52018-10-13 18:28:27 +08002367 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002368
Laurence Lundblade570fab52018-10-13 18:28:27 +08002369 nResult = QCBORDecode_GetNext(&DC, &Item);
2370 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05302371 return -16;
Laurence Lundblade570fab52018-10-13 18:28:27 +08002372 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002373
Laurence Lundblade570fab52018-10-13 18:28:27 +08002374 nResult = QCBORDecode_Finish(&DC);
Laurence Lundblade6de37062018-10-15 12:22:42 +05302375 if(nResult != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
2376 return -17;
Laurence Lundblade570fab52018-10-13 18:28:27 +08002377 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002378
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302379 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002380 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad5);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002381
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302382 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002383
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302384 QCBORDecode_SetMemPool(&DC, MemPool, false);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002385
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302386 nResult = QCBORDecode_GetNext(&DC, &Item);
2387 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05302388 return -18;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302389 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002390
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302391 nResult = QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05302392 if(nResult != QCBOR_ERR_BAD_BREAK) {
2393 return -19;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302394 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002395
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002396 return 0;
2397}
2398
Laurence Lundblade17ede402018-10-13 11:43:07 +08002399
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002400static const uint8_t spIndefiniteLenString[] = {
Laurence Lundblade17ede402018-10-13 11:43:07 +08002401 0x81, // Array of length one
2402 0x7f, // text string marked with indefinite length
2403 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment
2404 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment
2405 0xff // ending break
2406};
2407
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002408static const uint8_t spIndefiniteLenStringBad2[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302409 0x81, // Array of length one
2410 0x7f, // text string marked with indefinite length
2411 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment
2412 0x44, 0x6d, 0x69, 0x6e, 0x67, // second segment of wrong type
2413 0xff // ending break
2414};
2415
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002416static const uint8_t spIndefiniteLenStringBad3[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302417 0x81, // Array of length one
2418 0x7f, // text string marked with indefinite length
2419 0x01, 0x02, // Not a string
2420 0xff // ending break
2421};
2422
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002423static const uint8_t spIndefiniteLenStringBad4[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302424 0x81, // Array of length one
2425 0x7f, // text string marked with indefinite length
2426 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment
2427 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment
2428 // missing end of string
2429};
2430
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002431static const uint8_t spIndefiniteLenStringLabel[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302432 0xa1, // Array of length one
2433 0x7f, // text string marked with indefinite length
2434 0x65, 0x73, 0x74, 0x72, 0x75, 0x75, // first segment
2435 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment
2436 0xff, // ending break
2437 0x01 // integer being labeled.
2438};
2439
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08002440static UsefulBufC MakeIndefiniteBigBstr(UsefulBuf Storage) // TODO: size this
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302441{
2442 UsefulOutBuf UOB;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002443
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302444 UsefulOutBuf_Init(&UOB, Storage);
2445 UsefulOutBuf_AppendByte(&UOB, 0x81);
2446 UsefulOutBuf_AppendByte(&UOB, 0x5f);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002447
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302448 int i = 0;
2449 for(int nChunkSize = 1; nChunkSize <= 128; nChunkSize *= 2) {
2450 UsefulOutBuf_AppendByte(&UOB, 0x58);
2451 UsefulOutBuf_AppendByte(&UOB, (uint8_t)nChunkSize);
2452 for(int j = 0; j < nChunkSize; j++ ) {
2453 UsefulOutBuf_AppendByte(&UOB, i);
2454 i++;
2455 }
2456 }
2457 UsefulOutBuf_AppendByte(&UOB, 0xff);
2458
2459 return UsefulOutBuf_OutUBuf(&UOB);
2460}
2461
2462static int CheckBigString(UsefulBufC BigString)
2463{
2464 if(BigString.len != 255) {
2465 return 1;
2466 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002467
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302468 for(uint8_t i = 0; i < 255; i++){
2469 if(((const uint8_t *)BigString.ptr)[i] != i) {
2470 return 1;
2471 }
2472 }
2473 return 0;
2474}
2475
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05302476
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302477int IndefiniteLengthStringTest()
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302478{
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302479 QCBORDecodeContext DC;
2480 QCBORItem Item;
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05302481 // big enough for MakeIndefiniteBigBstr() + MemPool overhead
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08002482 UsefulBuf_MAKE_STACK_UB(MemPool, 350);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002483
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302484 // --- Simple normal indefinite length string ------
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002485 UsefulBufC IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenString);
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302486 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002487
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05302488 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302489 return -1;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302490 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002491
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302492 if(QCBORDecode_GetNext(&DC, &Item)) {
2493 return -2;
2494 }
2495 if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.uDataAlloc) {
2496 return -3;
2497 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002498
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302499 if(QCBORDecode_GetNext(&DC, &Item)) {
2500 return -4;
2501 }
2502 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || !Item.uDataAlloc) {
2503 return -5;
2504 }
2505 if(QCBORDecode_Finish(&DC)) {
2506 return -6;
2507 }
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302508
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302509 // ----- types mismatch ---
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002510 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad2), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002511
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302512 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
2513 return -7;
2514 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002515
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302516 if(QCBORDecode_GetNext(&DC, &Item)) {
2517 return -8;
2518 }
2519 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
2520 return -9;
2521 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002522
Laurence Lundblade30816f22018-11-10 13:40:22 +07002523 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_INDEFINITE_STRING_CHUNK) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302524 return -10;
2525 }
2526
2527 // ----- not a string ---
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002528 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad3), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002529
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302530 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
2531 return -11;
2532 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002533
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302534 if(QCBORDecode_GetNext(&DC, &Item)) {
2535 return -12;
2536 }
2537 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
2538 return -13;
2539 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002540
Laurence Lundblade30816f22018-11-10 13:40:22 +07002541 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_INDEFINITE_STRING_CHUNK) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302542 return -14;
2543 }
2544
2545 // ----- no end -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002546 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad4), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002547
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302548 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
2549 return -15;
2550 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002551
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302552 if(QCBORDecode_GetNext(&DC, &Item)) {
2553 return -16;
2554 }
2555 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
2556 return -17;
2557 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002558
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302559 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_HIT_END) {
2560 return -18;
2561 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002562
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302563 // ------ Don't set a string allocator and see an error -----
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302564 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002565
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302566 QCBORDecode_GetNext(&DC, &Item);
2567 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302568 return -19;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302569 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002570
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302571 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_NO_STRING_ALLOCATOR) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302572 return -20;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302573 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002574
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302575 // ----- Mempool is way too small -----
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05302576 UsefulBuf_MAKE_STACK_UB(MemPoolTooSmall, 20); // 20 is too small no matter what
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302577
2578 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
2579 if(!QCBORDecode_SetMemPool(&DC, MemPoolTooSmall, false)) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302580 return -21;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302581 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002582
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302583 // ----- Mempool is way too small -----
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05302584 UsefulBuf_MAKE_STACK_UB(BigIndefBStrStorage, 290);
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08002585 const UsefulBufC BigIndefBStr = MakeIndefiniteBigBstr(BigIndefBStrStorage);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002586
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05302587 UsefulBuf_MAKE_STACK_UB(MemPoolSmall, 80); // 80 is big enough for MemPool overhead, but not BigIndefBStr
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002588
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302589 QCBORDecode_Init(&DC, BigIndefBStr, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302590 if(QCBORDecode_SetMemPool(&DC, MemPoolSmall, false)) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302591 return -22;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302592 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002593
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302594 QCBORDecode_GetNext(&DC, &Item);
2595 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302596 return -23;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302597 }
Laurence Lundblade30816f22018-11-10 13:40:22 +07002598 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_STRING_ALLOCATE) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302599 return -24;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302600 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002601
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302602 // ---- big bstr -----
2603 QCBORDecode_Init(&DC, BigIndefBStr, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002604
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302605 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
2606 return -25;
2607 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002608
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05302609 if(QCBORDecode_GetNext(&DC, &Item)) {
2610 return -26;
2611 }
2612 if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.uDataAlloc) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302613 return -26;
2614 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002615
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302616 if(QCBORDecode_GetNext(&DC, &Item)) {
2617 return -27;
2618 }
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05302619 if(Item.uDataType != QCBOR_TYPE_BYTE_STRING || !Item.uDataAlloc || Item.uNestingLevel != 1) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302620 return -28;
2621 }
2622 if(CheckBigString(Item.val.string)) {
2623 return -3;
2624 }
2625 if(QCBORDecode_Finish(&DC)) {
2626 return -29;
2627 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002628
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302629 // --- label is an indefinite length string ------
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002630 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringLabel), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002631
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302632 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
2633 return -30;
2634 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002635
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302636 QCBORDecode_GetNext(&DC, &Item);
2637 if(Item.uDataType != QCBOR_TYPE_MAP) {
2638 return -31;
2639 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002640
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302641 if(QCBORDecode_GetNext(&DC, &Item)){
2642 return -32;
2643 }
2644 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || Item.uDataType != QCBOR_TYPE_INT64 ||
2645 Item.uDataAlloc || !Item.uLabelAlloc ||
2646 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("struuming"))) {
2647 return -33;
2648 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002649
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302650 if(QCBORDecode_Finish(&DC)) {
2651 return -34;
2652 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002653
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002654 return 0;
2655}
2656
2657
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302658int AllocAllStringsTest()
2659{
2660 QCBORDecodeContext DC;
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08002661 QCBORError nCBORError;
2662
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002663
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302664 // First test, use the "CSRMap" as easy input and checking
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002665 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002666
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08002667 UsefulBuf_MAKE_STACK_UB(Pool, sizeof(spCSRInput) + QCBOR_DECODE_MIN_MEM_POOL_SIZE);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002668
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08002669 nCBORError = QCBORDecode_SetMemPool(&DC, Pool, 1); // Turn on copying.
2670 if(nCBORError) {
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302671 return -1;
2672 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002673
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08002674 if(CheckCSRMaps(&DC)) {
2675 return -2;
2676 }
2677
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302678 // Next parse, save pointers to a few strings, destroy original and see all is OK.
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08002679 UsefulBuf_MAKE_STACK_UB(CopyOfStorage, sizeof(pValidMapEncoded) + QCBOR_DECODE_MIN_MEM_POOL_SIZE);
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08002680 const UsefulBufC CopyOf = UsefulBuf_Copy(CopyOfStorage, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded));
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002681
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302682 QCBORDecode_Init(&DC, CopyOf, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08002683 UsefulBuf_Set(Pool, '/');
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302684 QCBORDecode_SetMemPool(&DC, Pool, 1); // Turn on copying.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002685
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302686 QCBORItem Item1, Item2, Item3, Item4;
2687 if((nCBORError = QCBORDecode_GetNext(&DC, &Item1)))
2688 return nCBORError;
2689 if(Item1.uDataType != QCBOR_TYPE_MAP ||
2690 Item1.val.uCount != 3)
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08002691 return -3;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302692 if((nCBORError = QCBORDecode_GetNext(&DC, &Item1)))
2693 return nCBORError;
2694 if((nCBORError = QCBORDecode_GetNext(&DC, &Item2)))
2695 return nCBORError;
2696 if((nCBORError = QCBORDecode_GetNext(&DC, &Item3)))
2697 return nCBORError;
2698 if((nCBORError = QCBORDecode_GetNext(&DC, &Item4)))
2699 return nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002700
Laurence Lundblade05ec57b2018-10-21 01:50:03 +05302701 UsefulBuf_Set(CopyOfStorage, '_');
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002702
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302703 if(Item1.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302704 Item1.uDataType != QCBOR_TYPE_INT64 ||
2705 Item1.val.int64 != 42 ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09002706 UsefulBuf_Compare(Item1.label.string, UsefulBuf_FromSZ("first integer"))) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08002707 return -4;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09002708 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002709
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302710
2711 if(Item2.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09002712 UsefulBuf_Compare(Item2.label.string, UsefulBuf_FromSZ("an array of two strings")) ||
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302713 Item2.uDataType != QCBOR_TYPE_ARRAY ||
2714 Item2.val.uCount != 2)
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08002715 return -5;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002716
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302717 if(Item3.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09002718 UsefulBuf_Compare(Item3.val.string, UsefulBuf_FromSZ("string1"))) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08002719 return -6;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09002720 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002721
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302722 if(Item4.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09002723 UsefulBuf_Compare(Item4.val.string, UsefulBuf_FromSZ("string2"))) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08002724 return -7;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09002725 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002726
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302727 // Next parse with a pool that is too small
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08002728 UsefulBuf_MAKE_STACK_UB(SmallPool, QCBOR_DECODE_MIN_MEM_POOL_SIZE + 1);
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05302729 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302730 QCBORDecode_SetMemPool(&DC, SmallPool, 1); // Turn on copying.
2731 if((nCBORError = QCBORDecode_GetNext(&DC, &Item1)))
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08002732 return -8;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302733 if(Item1.uDataType != QCBOR_TYPE_MAP ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09002734 Item1.val.uCount != 3) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08002735 return -9;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09002736 }
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302737 if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item1))){
2738 if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item2))) {
2739 if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item3))) {
2740 nCBORError = QCBORDecode_GetNext(&DC, &Item4);
2741 }
2742 }
2743 }
Laurence Lundblade30816f22018-11-10 13:40:22 +07002744 if(nCBORError != QCBOR_ERR_STRING_ALLOCATE) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08002745 return -10;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302746 }
2747
2748 return 0;
2749}
2750
Laurence Lundbladef6531662018-12-04 10:42:22 +09002751// Cheating declaration to get to the special test hook
2752size_t MemPoolTestHook_GetPoolSize(void *ctx);
2753
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302754
2755int MemPoolTest(void)
Laurence Lundblade0155b622018-10-12 20:04:37 +08002756{
Laurence Lundbladef6531662018-12-04 10:42:22 +09002757 // Set up the decoder with a tiny bit of CBOR to parse
2758 QCBORDecodeContext DC;
2759 const uint8_t pMinimalCBOR[] = {0xa0}; // One empty map
2760 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pMinimalCBOR),0);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002761
Laurence Lundbladef6531662018-12-04 10:42:22 +09002762 // Set up an memory pool of 100 bytes
2763 UsefulBuf_MAKE_STACK_UB(Pool, 100);
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08002764 QCBORError nError = QCBORDecode_SetMemPool(&DC, Pool, 0);
2765 if(nError) {
2766 return -9;
2767 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002768
Laurence Lundbladef6531662018-12-04 10:42:22 +09002769 // Cheat a little to get to the string allocator object
2770 // so we can call it directly to test it
2771 QCBORStringAllocator *pAlloc = (QCBORStringAllocator *)DC.pStringAllocator;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002772 // Cheat some more to know exactly the
Laurence Lundbladef6531662018-12-04 10:42:22 +09002773 size_t uAvailPool = MemPoolTestHook_GetPoolSize(pAlloc);
2774
2775 // First test -- ask for too much in one go
2776 UsefulBuf Allocated = (*pAlloc->fAllocate)(pAlloc->pAllocaterContext, NULL, uAvailPool+1);
2777 if(!UsefulBuf_IsNULL(Allocated)) {
2778 return -1;
2779 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002780
2781
Laurence Lundbladef6531662018-12-04 10:42:22 +09002782 // Re do the set up for the next test that will do a successful alloc,
2783 // a fail, a free and then success
2784 // This test should work on 32 and 64-bit machines if the compiler
2785 // does the expected thing with pointer sizes for the internal
2786 // MemPool implementation leaving 44 or 72 bytes of pool memory.
2787 QCBORDecode_SetMemPool(&DC, Pool, 0);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002788
Laurence Lundbladef6531662018-12-04 10:42:22 +09002789 // Cheat a little to get to the string allocator object
2790 // so we can call it directly to test it
2791 pAlloc = (QCBORStringAllocator *)DC.pStringAllocator;
2792 // Cheat some more to know exactly the
2793 uAvailPool = MemPoolTestHook_GetPoolSize(pAlloc);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002794
Laurence Lundbladef6531662018-12-04 10:42:22 +09002795 Allocated = (*pAlloc->fAllocate)(pAlloc->pAllocaterContext, NULL, uAvailPool-1);
2796 if(UsefulBuf_IsNULL(Allocated)) { // expected to succeed
2797 return -2;
2798 }
2799 UsefulBuf Allocated2 = (*pAlloc->fAllocate)(pAlloc->pAllocaterContext, NULL, uAvailPool/2);
2800 if(!UsefulBuf_IsNULL(Allocated2)) { // expected to fail
2801 return -3;
2802 }
2803 (*pAlloc->fFree)(pAlloc->pAllocaterContext, Allocated.ptr);
2804 Allocated = (*pAlloc->fAllocate)(pAlloc->pAllocaterContext, NULL, uAvailPool/2);
2805 if(UsefulBuf_IsNULL(Allocated)) { // succeed because of the free
2806 return -4;
2807 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002808
2809
Laurence Lundbladef6531662018-12-04 10:42:22 +09002810 // Re do set up for next test that involves a successful alloc,
2811 // and a successful realloc and a failed realloc
2812 QCBORDecode_SetMemPool(&DC, Pool, 0);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002813
Laurence Lundbladef6531662018-12-04 10:42:22 +09002814 // Cheat a little to get to the string allocator object
2815 // so we can call it directly to test it
2816 pAlloc = (QCBORStringAllocator *)DC.pStringAllocator;
2817 Allocated = (*pAlloc->fAllocate)(pAlloc->pAllocaterContext, NULL, uAvailPool/2);
2818 if(UsefulBuf_IsNULL(Allocated)) { // expected to succeed
2819 return -5;
2820 }
2821 Allocated2 = (*pAlloc->fAllocate)(pAlloc->pAllocaterContext, Allocated.ptr, uAvailPool);
2822 if(UsefulBuf_IsNULL(Allocated2)) {
2823 return -6;
2824 }
2825 if(Allocated2.ptr != Allocated.ptr || Allocated2.len != uAvailPool) {
2826 return -7;
2827 }
2828 UsefulBuf Allocated3 = (*pAlloc->fAllocate)(pAlloc->pAllocaterContext, Allocated.ptr, uAvailPool+1);
2829 if(!UsefulBuf_IsNULL(Allocated3)) { // expected to fail
2830 return -8;
2831 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002832
Laurence Lundbladef6531662018-12-04 10:42:22 +09002833 return 0;
2834}
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002835