blob: 125560edcdb9a1cbd184777fc8c46c444af4b63b [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.
Laurence Lundbladeee851742020-01-08 08:37:05 -08003 Copyright (c) 2018-2020, Laurence Lundblade.
Laurence Lundbladed92a6162018-11-01 11:38:35 +07004 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 Lundbladeee851742020-01-08 08:37:05 -080031 =============================================================================*/
Laurence Lundblade9e3651c2018-10-10 11:49:55 +080032
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 Lundbladebb1062e2019-08-12 23:28:54 -070037#include "not_well_formed_cbor.h"
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -080038
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080039
Laurence Lundbladea2e29072018-12-30 09:20:06 -080040#ifdef PRINT_FUNCTIONS_FOR_DEBUGGING
Laurence Lundblade20db9c92018-12-17 11:40:37 -080041#include <stdio.h>
Laurence Lundbladea2e29072018-12-30 09:20:06 -080042
43static void PrintUsefulBufC(const char *szLabel, UsefulBufC Buf)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080044{
45 if(szLabel) {
46 printf("%s ", szLabel);
47 }
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -080048
Laurence Lundblade570fab52018-10-13 18:28:27 +080049 size_t i;
Laurence Lundbladea2e29072018-12-30 09:20:06 -080050 for(i = 0; i < Buf.len; i++) {
51 uint8_t Z = ((uint8_t *)Buf.ptr)[i];
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080052 printf("%02x ", Z);
53 }
54 printf("\n");
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -080055
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080056 fflush(stdout);
57}
Laurence Lundblade20db9c92018-12-17 11:40:37 -080058#endif
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080059
60
Laurence Lundbladeb836efb2018-10-28 20:09:58 +070061static const uint8_t spExpectedEncodedInts[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080062 0x98, 0x2f, 0x3b, 0x7f, 0xff, 0xff, 0xff, 0xff,
63 0xff, 0xff, 0xff, 0x3b, 0x00, 0x00, 0x00, 0x01,
64 0x00, 0x00, 0x00, 0x00, 0x3a, 0xff, 0xff, 0xff,
65 0xff, 0x3a, 0xff, 0xff, 0xff, 0xfe, 0x3a, 0xff,
66 0xff, 0xff, 0xfd, 0x3a, 0x7f, 0xff, 0xff, 0xff,
67 0x3a, 0x7f, 0xff, 0xff, 0xfe, 0x3a, 0x00, 0x01,
68 0x00, 0x01, 0x3a, 0x00, 0x01, 0x00, 0x00, 0x39,
69 0xff, 0xff, 0x39, 0xff, 0xfe, 0x39, 0xff, 0xfd,
70 0x39, 0x01, 0x00, 0x38, 0xff, 0x38, 0xfe, 0x38,
71 0xfd, 0x38, 0x18, 0x37, 0x36, 0x20, 0x00, 0x00,
72 0x01, 0x16, 0x17, 0x18, 0x18, 0x18, 0x19, 0x18,
73 0x1a, 0x18, 0xfe, 0x18, 0xff, 0x19, 0x01, 0x00,
74 0x19, 0x01, 0x01, 0x19, 0xff, 0xfe, 0x19, 0xff,
75 0xff, 0x1a, 0x00, 0x01, 0x00, 0x00, 0x1a, 0x00,
76 0x01, 0x00, 0x01, 0x1a, 0x00, 0x01, 0x00, 0x02,
77 0x1a, 0x7f, 0xff, 0xff, 0xff, 0x1a, 0x7f, 0xff,
78 0xff, 0xff, 0x1a, 0x80, 0x00, 0x00, 0x00, 0x1a,
79 0x80, 0x00, 0x00, 0x01, 0x1a, 0xff, 0xff, 0xff,
80 0xfe, 0x1a, 0xff, 0xff, 0xff, 0xff, 0x1b, 0x00,
81 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x1b,
82 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01,
83 0x1b, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
84 0xff, 0x1b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
85 0xff, 0xff};
86
87
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080088// return CBOR error or -1 if type of value doesn't match
89
Laurence Lundbladec5fef682020-01-25 11:38:45 -080090static int32_t IntegerValuesParseTestInternal(QCBORDecodeContext *pDCtx)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080091{
92 QCBORItem Item;
93 int nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -080094
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080095 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
96 return nCBORError;
97 if(Item.uDataType != QCBOR_TYPE_ARRAY)
98 return -1;
99
100 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
101 return nCBORError;
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800102 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade570fab52018-10-13 18:28:27 +0800103 Item.val.int64 != -9223372036854775807LL - 1)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800104 return -1;
105
106 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
107 return nCBORError;
108 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade570fab52018-10-13 18:28:27 +0800109 Item.val.int64 != -4294967297)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800110 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800111
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800112 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
113 return nCBORError;
114 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade570fab52018-10-13 18:28:27 +0800115 Item.val.int64 != -4294967296)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800116 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800117
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800118 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
119 return nCBORError;
120 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade570fab52018-10-13 18:28:27 +0800121 Item.val.int64 != -4294967295)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800122 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800123
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800124 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
125 return nCBORError;
126 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade570fab52018-10-13 18:28:27 +0800127 Item.val.int64 != -4294967294)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800128 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800129
130
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800131 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
132 return nCBORError;
133 if(Item.uDataType != QCBOR_TYPE_INT64 ||
134 Item.val.int64 != -2147483648)
135 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800136
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800137 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
138 return nCBORError;
139 if(Item.uDataType != QCBOR_TYPE_INT64 ||
140 Item.val.int64 != -2147483647)
141 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800142
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800143 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
144 return nCBORError;
145 if(Item.uDataType != QCBOR_TYPE_INT64 ||
146 Item.val.int64 != -65538)
147 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800148
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800149 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
150 return nCBORError;
151 if(Item.uDataType != QCBOR_TYPE_INT64 ||
152 Item.val.int64 != -65537)
153 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800154
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800155 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
156 return nCBORError;
157 if(Item.uDataType != QCBOR_TYPE_INT64 ||
158 Item.val.int64 != -65536)
159 return -1;
160
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800161
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800162 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
163 return nCBORError;
164 if(Item.uDataType != QCBOR_TYPE_INT64 ||
165 Item.val.int64 != -65535)
166 return -1;
167
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800168
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800169 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
170 return nCBORError;
171 if(Item.uDataType != QCBOR_TYPE_INT64 ||
172 Item.val.int64 != -65534)
173 return -1;
174
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800175
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800176 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
177 return nCBORError;
178 if(Item.uDataType != QCBOR_TYPE_INT64 ||
179 Item.val.int64 != -257)
180 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800181
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800182 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
183 return nCBORError;
184 if(Item.uDataType != QCBOR_TYPE_INT64 ||
185 Item.val.int64 != -256)
186 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800187
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800188 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
189 return nCBORError;
190 if(Item.uDataType != QCBOR_TYPE_INT64 ||
191 Item.val.int64 != -255)
192 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800193
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800194 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
195 return nCBORError;
196 if(Item.uDataType != QCBOR_TYPE_INT64 ||
197 Item.val.int64 != -254)
198 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800199
200
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800201 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
202 return nCBORError;
203 if(Item.uDataType != QCBOR_TYPE_INT64 ||
204 Item.val.int64 != -25)
205 return -1;
206
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800207
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800208 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
209 return nCBORError;
210 if(Item.uDataType != QCBOR_TYPE_INT64 ||
211 Item.val.int64 != -24)
212 return -1;
213
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800214
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800215 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
216 return nCBORError;
217 if(Item.uDataType != QCBOR_TYPE_INT64 ||
218 Item.val.int64 != -23)
219 return -1;
220
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800221
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800222 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
223 return nCBORError;
224 if(Item.uDataType != QCBOR_TYPE_INT64 ||
225 Item.val.int64 != -1)
226 return -1;
227
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800228
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800229 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
230 return nCBORError;
231 if(Item.uDataType != QCBOR_TYPE_INT64 ||
232 Item.val.int64 != 0)
233 return -1;
234
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800235
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800236 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
237 return nCBORError;
238 if(Item.uDataType != QCBOR_TYPE_INT64 ||
239 Item.val.int64 != 0)
240 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800241
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800242 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
243 return nCBORError;
244 if(Item.uDataType != QCBOR_TYPE_INT64 ||
245 Item.val.int64 != 1)
246 return -1;
247
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800248
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800249 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
250 return nCBORError;
251 if(Item.uDataType != QCBOR_TYPE_INT64 ||
252 Item.val.int64 != 22)
253 return -1;
254
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800255
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800256 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
257 return nCBORError;
258 if(Item.uDataType != QCBOR_TYPE_INT64 ||
259 Item.val.int64 != 23)
260 return -1;
261
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800262
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800263 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
264 return nCBORError;
265 if(Item.uDataType != QCBOR_TYPE_INT64 ||
266 Item.val.int64 != 24)
267 return -1;
268
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800269
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800270 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
271 return nCBORError;
272 if(Item.uDataType != QCBOR_TYPE_INT64 ||
273 Item.val.int64 != 25)
274 return -1;
275
276 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
277 return nCBORError;
278 if(Item.uDataType != QCBOR_TYPE_INT64 ||
279 Item.val.int64 != 26)
280 return -1;
281
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800282
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800283 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
284 return nCBORError;
285 if(Item.uDataType != QCBOR_TYPE_INT64 ||
286 Item.val.int64 != 254)
287 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800288
289
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800290 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
291 return nCBORError;
292 if(Item.uDataType != QCBOR_TYPE_INT64 ||
293 Item.val.int64 != 255)
294 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800295
296
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800297 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
298 return nCBORError;
299 if(Item.uDataType != QCBOR_TYPE_INT64 ||
300 Item.val.int64 != 256)
301 return -1;
302
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800303
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800304 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
305 return nCBORError;
306 if(Item.uDataType != QCBOR_TYPE_INT64 ||
307 Item.val.int64 != 257)
308 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800309
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800310 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
311 return nCBORError;
312 if(Item.uDataType != QCBOR_TYPE_INT64 ||
313 Item.val.int64 != 65534)
314 return -1;
315
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800316
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800317 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
318 return nCBORError;
319 if(Item.uDataType != QCBOR_TYPE_INT64 ||
320 Item.val.int64 != 65535)
321 return -1;
322
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800323
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800324 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
325 return nCBORError;
326 if(Item.uDataType != QCBOR_TYPE_INT64 ||
327 Item.val.int64 != 65536)
328 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800329
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800330 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
331 return nCBORError;
332 if(Item.uDataType != QCBOR_TYPE_INT64 ||
333 Item.val.int64 != 65537)
334 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800335
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800336 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
337 return nCBORError;
338 if(Item.uDataType != QCBOR_TYPE_INT64 ||
339 Item.val.int64 != 65538)
340 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800341
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800342 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
343 return nCBORError;
344 if(Item.uDataType != QCBOR_TYPE_INT64 ||
345 Item.val.int64 != 2147483647)
346 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800347
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800348 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
349 return nCBORError;
350 if(Item.uDataType != QCBOR_TYPE_INT64 ||
351 Item.val.int64 != 2147483647)
352 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800353
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800354 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
355 return nCBORError;
356 if(Item.uDataType != QCBOR_TYPE_INT64 ||
357 Item.val.int64 != 2147483648)
358 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800359
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800360 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
361 return nCBORError;
362 if(Item.uDataType != QCBOR_TYPE_INT64 ||
363 Item.val.int64 != 2147483649)
364 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800365
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800366 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
367 return nCBORError;
368 if(Item.uDataType != QCBOR_TYPE_INT64 ||
369 Item.val.int64 != 4294967294)
370 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800371
372
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800373 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
374 return nCBORError;
375 if(Item.uDataType != QCBOR_TYPE_INT64 ||
376 Item.val.int64 != 4294967295)
377 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800378
379
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800380 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
381 return nCBORError;
382 if(Item.uDataType != QCBOR_TYPE_INT64 ||
383 Item.val.int64 != 4294967296)
384 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800385
386
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800387 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
388 return nCBORError;
389 if(Item.uDataType != QCBOR_TYPE_INT64 ||
390 Item.val.int64 != 4294967297)
391 return -1;
392
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800393
394
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800395 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
396 return nCBORError;
397 if(Item.uDataType != QCBOR_TYPE_INT64 ||
398 Item.val.int64 != 9223372036854775807LL)
399 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800400
401
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800402 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
403 return nCBORError;
404 if(Item.uDataType != QCBOR_TYPE_UINT64 ||
405 Item.val.uint64 != 18446744073709551615ULL)
406 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800407
408
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800409 if(QCBORDecode_Finish(pDCtx) != QCBOR_SUCCESS) {
410 return -1;
411 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800412
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800413 return 0;
414}
415
416
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800417// One less than the smallest negative integer allowed in C. Decoding
418// this should fail.
419static const uint8_t spTooSmallNegative[] = {
420 0x3b, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
Laurence Lundblade21d1d812019-09-28 22:47:49 -1000421};
422
423
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800424/*
425 Tests the decoding of lots of different integers sizes
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +0800426 and values.
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800427 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800428int32_t IntegerValuesParseTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800429{
Laurence Lundblade21d1d812019-09-28 22:47:49 -1000430 int nReturn;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800431 QCBORDecodeContext DCtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800432
Laurence Lundblade21d1d812019-09-28 22:47:49 -1000433 QCBORDecode_Init(&DCtx,
434 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedEncodedInts),
435 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800436
Laurence Lundblade21d1d812019-09-28 22:47:49 -1000437 // The really big test of all successes
438 nReturn = IntegerValuesParseTestInternal(&DCtx);
439 if(nReturn) {
440 return nReturn;
441 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800442
Laurence Lundblade21d1d812019-09-28 22:47:49 -1000443 // The one large negative integer that can be parsed
444 QCBORDecode_Init(&DCtx,
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800445 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTooSmallNegative),
Laurence Lundblade21d1d812019-09-28 22:47:49 -1000446 QCBOR_DECODE_MODE_NORMAL);
447
448 QCBORItem item;
449 if(QCBORDecode_GetNext(&DCtx, &item) != QCBOR_ERR_INT_OVERFLOW) {
450 nReturn = -4000;
451 }
452
453 return(nReturn);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800454}
455
456
457/*
Laurence Lundbladeee851742020-01-08 08:37:05 -0800458 Creates a simple CBOR array and returns it in *pEncoded. The array is
459 malloced and needs to be freed. This is used by several tests.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800460
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800461 Two of the inputs can be set. Two other items in the array are fixed.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800462
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800463 */
464
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -0800465static uint8_t spSimpleArrayBuffer[50];
466
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800467static int32_t CreateSimpleArray(int nInt1, int nInt2, uint8_t **pEncoded, size_t *pEncodedLen)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800468{
469 QCBOREncodeContext ECtx;
470 int nReturn = -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800471
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800472 *pEncoded = NULL;
473 *pEncodedLen = INT32_MAX;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800474
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800475 // loop runs CBOR encoding twice. First with no buffer to
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -0800476 // calculate the length so buffer can be allocated correctly,
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800477 // and last with the buffer to do the actual encoding
478 do {
Laurence Lundblade0595e932018-11-02 22:22:47 +0700479 QCBOREncode_Init(&ECtx, (UsefulBuf){*pEncoded, *pEncodedLen});
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800480 QCBOREncode_OpenArray(&ECtx);
481 QCBOREncode_AddInt64(&ECtx, nInt1);
482 QCBOREncode_AddInt64(&ECtx, nInt2);
483 QCBOREncode_AddBytes(&ECtx, ((UsefulBufC) {"galactic", 8}));
484 QCBOREncode_AddBytes(&ECtx, ((UsefulBufC) {"haven token", 11}));
485 QCBOREncode_CloseArray(&ECtx);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800486
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -0800487 if(QCBOREncode_FinishGetSize(&ECtx, pEncodedLen))
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800488 goto Done;
489
490 if(*pEncoded != NULL) {
491 nReturn = 0;
492 goto Done;
493 }
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -0800494
495 // Use static buffer to avoid dependency on malloc()
496 if(*pEncodedLen > sizeof(spSimpleArrayBuffer)) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800497 goto Done;
498 }
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -0800499 *pEncoded = spSimpleArrayBuffer;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800500
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800501 } while(1);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800502
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -0800503Done:
504 return nReturn;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800505}
506
507
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800508/*
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800509 Some basic CBOR with map and array used in a lot of tests.
510 The map labels are all strings
511
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800512 {"first integer": 42,
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900513 "an array of two strings": [
514 "string1", "string2"
515 ],
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800516 "map in a map": {
517 "bytes 1": h'78787878',
518 "bytes 2": h'79797979',
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900519 "another int": 98,
520 "text 2": "lies, damn lies and statistics"
521 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800522 }
523 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800524static const uint8_t pValidMapEncoded[] = {
Laurence Lundbladeb836efb2018-10-28 20:09:58 +0700525 0xa3, 0x6d, 0x66, 0x69, 0x72, 0x73, 0x74, 0x20, 0x69, 0x6e,
526 0x74, 0x65, 0x67, 0x65, 0x72, 0x18, 0x2a, 0x77, 0x61, 0x6e,
527 0x20, 0x61, 0x72, 0x72, 0x61, 0x79, 0x20, 0x6f, 0x66, 0x20,
528 0x74, 0x77, 0x6f, 0x20, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67,
529 0x73, 0x82, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x31,
530 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x32, 0x6c, 0x6d,
531 0x61, 0x70, 0x20, 0x69, 0x6e, 0x20, 0x61, 0x20, 0x6d, 0x61,
532 0x70, 0xa4, 0x67, 0x62, 0x79, 0x74, 0x65, 0x73, 0x20, 0x31,
533 0x44, 0x78, 0x78, 0x78, 0x78, 0x67, 0x62, 0x79, 0x74, 0x65,
534 0x73, 0x20, 0x32, 0x44, 0x79, 0x79, 0x79, 0x79, 0x6b, 0x61,
535 0x6e, 0x6f, 0x74, 0x68, 0x65, 0x72, 0x20, 0x69, 0x6e, 0x74,
536 0x18, 0x62, 0x66, 0x74, 0x65, 0x78, 0x74, 0x20, 0x32, 0x78,
537 0x1e, 0x6c, 0x69, 0x65, 0x73, 0x2c, 0x20, 0x64, 0x61, 0x6d,
538 0x6e, 0x20, 0x6c, 0x69, 0x65, 0x73, 0x20, 0x61, 0x6e, 0x64,
539 0x20, 0x73, 0x74, 0x61, 0x74, 0x69, 0x73, 0x74, 0x69, 0x63,
540 0x73 } ;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800541
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800542static int32_t ParseOrderedArray(const uint8_t *pEncoded,
Laurence Lundbladeee851742020-01-08 08:37:05 -0800543 size_t nLen,
544 int64_t *pInt1,
545 int64_t *pInt2,
546 const uint8_t **pBuf3,
547 size_t *pBuf3Len,
548 const uint8_t **pBuf4,
549 size_t *pBuf4Len)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800550{
551 QCBORDecodeContext DCtx;
552 QCBORItem Item;
553 int nReturn = -1; // assume error until success
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800554
Laurence Lundbladeee851742020-01-08 08:37:05 -0800555 QCBORDecode_Init(&DCtx,
556 (UsefulBufC){pEncoded, nLen},
557 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800558
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800559 // Make sure the first thing is a map
560 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || Item.uDataType != QCBOR_TYPE_ARRAY)
561 goto Done;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800562
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800563 // First integer
Laurence Lundblade12b495d2018-12-17 11:15:54 -0800564 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || Item.uDataType != QCBOR_TYPE_INT64)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800565 goto Done;
566 *pInt1 = Item.val.int64;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800567
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800568 // Second integer
569 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || Item.uDataType != QCBOR_TYPE_INT64)
570 goto Done;
571 *pInt2 = Item.val.int64;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800572
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800573 // First string
574 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || Item.uDataType != QCBOR_TYPE_BYTE_STRING)
575 goto Done;
576 *pBuf3 = Item.val.string.ptr;
577 *pBuf3Len = Item.val.string.len;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800578
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800579 // Second string
580 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || Item.uDataType != QCBOR_TYPE_BYTE_STRING)
581 goto Done;
582 *pBuf4 = Item.val.string.ptr;
583 *pBuf4Len = Item.val.string.len;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800584
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800585 nReturn = 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800586
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800587Done:
588 return(nReturn);
589}
590
591
592
Laurence Lundblade9e3651c2018-10-10 11:49:55 +0800593
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800594int32_t SimpleArrayTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800595{
596 uint8_t *pEncoded;
597 size_t nEncodedLen;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800598
Laurence Lundblade5e390822019-01-06 12:35:01 -0800599 int64_t i1=0, i2=0;
600 size_t i3=0, i4=0;
601 const uint8_t *s3= (uint8_t *)"";
602 const uint8_t *s4= (uint8_t *)"";
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800603
604
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800605 if(CreateSimpleArray(23, 6000, &pEncoded, &nEncodedLen) < 0) {
606 return(-1);
607 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800608
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800609 ParseOrderedArray(pEncoded, nEncodedLen, &i1, &i2, &s3, &i3, &s4, &i4);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800610
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800611 if(i1 != 23 ||
612 i2 != 6000 ||
613 i3 != 8 ||
614 i4 != 11 ||
Laurence Lundbladea44d5062018-10-17 18:45:12 +0530615 memcmp("galactic", s3, 8) !=0 ||
616 memcmp("haven token", s4, 11) !=0) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800617 return(-1);
618 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800619
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800620 return(0);
621}
622
623
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700624/*
625 [
626 0,
627 [],
628 [
629 [],
630 [
631 0
632 ],
633 {},
634 {
635 1: {},
636 2: {},
637 3: []
638 }
639 ]
640 ]
641 */
642static uint8_t sEmpties[] = {0x83, 0x00, 0x80, 0x84, 0x80, 0x81, 0x00, 0xa0,
643 0xa3, 0x01, 0xa0, 0x02, 0xa0, 0x03, 0x80};
644
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800645int32_t EmptyMapsAndArraysTest()
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700646{
647 QCBORDecodeContext DCtx;
648 QCBORItem Item;
649
Laurence Lundbladeee851742020-01-08 08:37:05 -0800650 QCBORDecode_Init(&DCtx,
651 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(sEmpties),
652 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700653
654 // Array with 3 items
655 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
656 Item.uDataType != QCBOR_TYPE_ARRAY ||
657 Item.uNestingLevel != 0 ||
658 Item.uNextNestLevel != 1 ||
659 Item.val.uCount != 3) {
660 return -1;
661 }
662
663 // An integer 0
664 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
665 Item.uDataType != QCBOR_TYPE_INT64 ||
666 Item.uNestingLevel != 1 ||
667 Item.uNextNestLevel != 1 ||
668 Item.val.uint64 != 0) {
669 return -2;
670 }
671
672 // An empty array
673 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
674 Item.uDataType != QCBOR_TYPE_ARRAY ||
675 Item.uNestingLevel != 1 ||
676 Item.uNextNestLevel != 1 ||
677 Item.val.uCount != 0) {
678 return -3;
679 }
680
681 // An array with 4 items
682 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
683 Item.uDataType != QCBOR_TYPE_ARRAY ||
684 Item.uNestingLevel != 1 ||
685 Item.uNextNestLevel != 2 ||
686 Item.val.uCount != 4) {
687 return -4;
688 }
689
690 // An empty array
691 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
692 Item.uDataType != QCBOR_TYPE_ARRAY ||
693 Item.uNestingLevel != 2 ||
694 Item.uNextNestLevel != 2 ||
695 Item.val.uCount != 0) {
696 return -5;
697 }
698
699 // An array with 1 item
700 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
701 Item.uDataType != QCBOR_TYPE_ARRAY ||
702 Item.uNestingLevel != 2 ||
703 Item.uNextNestLevel != 3 ||
704 Item.val.uCount != 1) {
705 return -6;
706 }
707
708 // An integer 0
709 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
710 Item.uDataType != QCBOR_TYPE_INT64 ||
711 Item.uNestingLevel != 3 ||
712 Item.uNextNestLevel != 2 ||
713 Item.val.uint64 != 0) {
714 return -7;
715 }
716
717 // An empty map
718 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
719 Item.uDataType != QCBOR_TYPE_MAP ||
720 Item.uNestingLevel != 2 ||
721 Item.uNextNestLevel != 2 ||
722 Item.val.uCount != 0) {
723 return -8;
724 }
725
726 // An map with 3 items
727 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
728 Item.uDataType != QCBOR_TYPE_MAP ||
729 Item.uNestingLevel != 2 ||
730 Item.uNextNestLevel != 3 ||
731 Item.val.uCount != 3) {
732 return -9;
733 }
734
735 // An empty map
736 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
737 Item.uDataType != QCBOR_TYPE_MAP ||
738 Item.uNestingLevel != 3 ||
739 Item.uNextNestLevel != 3 ||
740 Item.val.uCount != 0) {
741 return -10;
742 }
743
744 // An empty map
745 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
746 Item.uDataType != QCBOR_TYPE_MAP ||
747 Item.uNestingLevel != 3 ||
748 Item.uNextNestLevel != 3 ||
749 Item.val.uCount != 0) {
750 return -11;
751 }
752
753 // An empty array
754 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
755 Item.uDataType != QCBOR_TYPE_ARRAY ||
756 Item.uNestingLevel != 3 ||
757 Item.uNextNestLevel != 0 ||
758 Item.val.uCount != 0) {
759 return -12;
760 }
761
762 if(QCBORDecode_Finish(&DCtx) != QCBOR_SUCCESS) {
763 return -13;
764 }
765
766 return 0;
767}
768
Laurence Lundblade9e3651c2018-10-10 11:49:55 +0800769
Laurence Lundbladeee851742020-01-08 08:37:05 -0800770static uint8_t spDeepArrays[] = {0x81, 0x81, 0x81, 0x81, 0x81, 0x81,
771 0x81, 0x81, 0x81, 0x80};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800772
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800773int32_t ParseDeepArrayTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800774{
775 QCBORDecodeContext DCtx;
776 int nReturn = 0;
777 int i;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800778
Laurence Lundbladeee851742020-01-08 08:37:05 -0800779 QCBORDecode_Init(&DCtx,
780 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spDeepArrays),
781 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800782
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800783 for(i = 0; i < 10; i++) {
784 QCBORItem Item;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800785
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800786 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
787 Item.uDataType != QCBOR_TYPE_ARRAY ||
788 Item.uNestingLevel != i) {
789 nReturn = -1;
790 break;
791 }
792 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800793
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800794 return(nReturn);
795}
796
Laurence Lundblade972e59c2018-11-11 15:57:23 +0700797// Big enough to test nesting to the depth of 24
Laurence Lundbladeee851742020-01-08 08:37:05 -0800798static uint8_t spTooDeepArrays[] = {0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81,
799 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81,
800 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81,
801 0x81, 0x81, 0x81, 0x80};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800802
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800803int32_t ParseTooDeepArrayTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800804{
805 QCBORDecodeContext DCtx;
806 int nReturn = 0;
807 int i;
808 QCBORItem Item;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800809
810
Laurence Lundbladeee851742020-01-08 08:37:05 -0800811 QCBORDecode_Init(&DCtx,
812 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTooDeepArrays),
813 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800814
Laurence Lundblade972e59c2018-11-11 15:57:23 +0700815 for(i = 0; i < QCBOR_MAX_ARRAY_NESTING1; i++) {
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800816
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800817 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
818 Item.uDataType != QCBOR_TYPE_ARRAY ||
819 Item.uNestingLevel != i) {
820 nReturn = -1;
821 break;
822 }
823 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800824
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800825 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_ARRAY_NESTING_TOO_DEEP)
826 nReturn = -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800827
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800828 return(nReturn);
829}
830
831
832
833
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800834int32_t ShortBufferParseTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800835{
Laurence Lundbladebb1062e2019-08-12 23:28:54 -0700836 int nResult = 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800837
Laurence Lundblade06350ea2020-01-27 19:32:40 -0800838 for(size_t nNum = sizeof(spExpectedEncodedInts)-1; nNum; nNum--) {
Laurence Lundbladebb1062e2019-08-12 23:28:54 -0700839 QCBORDecodeContext DCtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800840
Laurence Lundbladeee851742020-01-08 08:37:05 -0800841 QCBORDecode_Init(&DCtx,
842 (UsefulBufC){spExpectedEncodedInts, nNum},
843 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800844
Laurence Lundblade06350ea2020-01-27 19:32:40 -0800845 const int nErr = IntegerValuesParseTestInternal(&DCtx);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800846
Laurence Lundbladebb1062e2019-08-12 23:28:54 -0700847 if(nErr != QCBOR_ERR_HIT_END && nErr != QCBOR_ERR_NO_MORE_ITEMS) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800848 nResult = -1;
849 goto Done;
850 }
851 }
852Done:
853 return nResult;
854}
855
856
Laurence Lundblade9e3651c2018-10-10 11:49:55 +0800857
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800858int32_t ShortBufferParseTest2()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800859{
860 uint8_t *pEncoded;
861 int nReturn;
862 size_t nEncodedLen;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800863
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800864 int64_t i1, i2;
865 size_t i3, i4;
866 const uint8_t *s3, *s4;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800867
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800868 nReturn = 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800869
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800870 if(CreateSimpleArray(23, 6000, &pEncoded, &nEncodedLen) < 0) {
871 return(-1);
872 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800873
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800874 for(nEncodedLen--; nEncodedLen; nEncodedLen--) {
875 int nResult = ParseOrderedArray(pEncoded, (uint32_t)nEncodedLen, &i1, &i2, &s3, &i3, &s4, &i4);
876 if(nResult == 0) {
877 nReturn = -1;
878 }
879 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800880
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800881 return(nReturn);
882}
883
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530884/*
885 Decode and thoroughly check a moderately complex
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800886 set of maps. Can be run in QCBOR_DECODE_MODE_NORMAL or in
887 QCBOR_DECODE_MODE_MAP_STRINGS_ONLY.
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530888 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800889static int32_t ParseMapTest1(QCBORDecodeMode nMode)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800890{
891 QCBORDecodeContext DCtx;
892 QCBORItem Item;
893 int nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800894
Laurence Lundbladeee851742020-01-08 08:37:05 -0800895 QCBORDecode_Init(&DCtx,
896 (UsefulBufC){pValidMapEncoded, sizeof(pValidMapEncoded)},
897 nMode);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800898
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900899 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800900 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900901 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800902 if(Item.uDataType != QCBOR_TYPE_MAP ||
903 Item.val.uCount != 3)
904 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800905
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900906 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800907 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900908 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800909 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800910 Item.uDataType != QCBOR_TYPE_INT64 ||
911 Item.val.int64 != 42 ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530912 Item.uDataAlloc ||
913 Item.uLabelAlloc ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900914 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("first integer"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800915 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900916 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800917
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900918 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800919 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900920 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800921 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530922 Item.uDataAlloc ||
923 Item.uLabelAlloc ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900924 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("an array of two strings")) ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800925 Item.uDataType != QCBOR_TYPE_ARRAY ||
926 Item.val.uCount != 2)
927 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800928
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900929 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800930 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900931 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800932 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530933 Item.uDataAlloc ||
934 Item.uLabelAlloc ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900935 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("string1"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800936 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900937 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800938
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900939 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800940 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900941 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800942 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530943 Item.uDataAlloc ||
944 Item.uLabelAlloc ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900945 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("string2"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800946 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900947 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800948
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900949 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800950 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900951 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800952 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530953 Item.uDataAlloc ||
954 Item.uLabelAlloc ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900955 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("map in a map")) ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800956 Item.uDataType != QCBOR_TYPE_MAP ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900957 Item.val.uCount != 4) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800958 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900959 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800960
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900961 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800962 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900963 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800964 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900965 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("bytes 1"))||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800966 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530967 Item.uDataAlloc ||
968 Item.uLabelAlloc ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900969 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("xxxx"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800970 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900971 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800972
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900973 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800974 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900975 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800976 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900977 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("bytes 2")) ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800978 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530979 Item.uDataAlloc ||
980 Item.uLabelAlloc ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900981 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("yyyy"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800982 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900983 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800984
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900985 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800986 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900987 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800988 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530989 Item.uDataAlloc ||
990 Item.uLabelAlloc ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900991 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("another int")) ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800992 Item.uDataType != QCBOR_TYPE_INT64 ||
993 Item.val.int64 != 98)
994 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800995
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900996 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800997 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900998 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800999 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001000 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("text 2"))||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001001 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301002 Item.uDataAlloc ||
1003 Item.uLabelAlloc ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001004 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("lies, damn lies and statistics"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001005 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001006 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001007
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001008 return 0;
1009}
1010
1011
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001012/*
1013 Decode and thoroughly check a moderately complex
1014 set of maps
1015 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001016int32_t ParseMapAsArrayTest()
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001017{
1018 QCBORDecodeContext DCtx;
1019 QCBORItem Item;
1020 int nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001021
Laurence Lundbladeee851742020-01-08 08:37:05 -08001022 QCBORDecode_Init(&DCtx,
1023 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded),
1024 QCBOR_DECODE_MODE_MAP_AS_ARRAY);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001025
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001026 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
1027 return nCBORError;
1028 }
Laurence Lundbladed61cbf32018-12-09 11:42:21 -08001029 if(Item.uDataType != QCBOR_TYPE_MAP_AS_ARRAY ||
1030 Item.val.uCount != 6) {
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001031 return -1;
1032 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001033
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001034 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
1035 return nCBORError;
1036 }
1037 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1038 Item.uDataAlloc ||
1039 Item.uLabelAlloc ||
1040 Item.uLabelType != QCBOR_TYPE_NONE ||
1041 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("first integer"))) {
1042 return -2;
1043 }
1044
1045 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
1046 return nCBORError;
1047 }
1048 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1049 Item.uDataType != QCBOR_TYPE_INT64 ||
1050 Item.val.int64 != 42 ||
1051 Item.uDataAlloc ||
1052 Item.uLabelAlloc) {
1053 return -3;
1054 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001055
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001056 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
1057 return nCBORError;
1058 }
1059 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1060 Item.uDataAlloc ||
1061 Item.uLabelAlloc ||
1062 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("an array of two strings")) ||
1063 Item.uDataType != QCBOR_TYPE_TEXT_STRING) {
1064 return -4;
1065 }
1066
1067 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
1068 return nCBORError;
1069 }
1070 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1071 Item.uDataAlloc ||
1072 Item.uLabelAlloc ||
1073 Item.uDataType != QCBOR_TYPE_ARRAY ||
1074 Item.val.uCount != 2) {
1075 return -5;
1076 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001077
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001078 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
1079 return nCBORError;
1080 }
1081 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1082 Item.val.string.len != 7 ||
1083 Item.uDataAlloc ||
1084 Item.uLabelAlloc ||
1085 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("string1"))) {
1086 return -6;
1087 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001088
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001089 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
1090 return nCBORError;
1091 }
1092 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1093 Item.uDataAlloc ||
1094 Item.uLabelAlloc ||
1095 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("string2"))) {
1096 return -7;
1097 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001098
1099
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001100 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
1101 return nCBORError;
1102 }
1103 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1104 Item.uDataAlloc ||
1105 Item.uLabelAlloc ||
1106 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("map in a map"))) {
1107 return -8;
1108 }
1109
1110 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
1111 return nCBORError;
1112 }
1113 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1114 Item.uDataAlloc ||
1115 Item.uLabelAlloc ||
Laurence Lundbladed61cbf32018-12-09 11:42:21 -08001116 Item.uDataType != QCBOR_TYPE_MAP_AS_ARRAY ||
1117 Item.val.uCount != 8) {
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001118 return -9;
1119 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001120
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001121 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
1122 return nCBORError;
1123 }
1124 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1125 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("bytes 1"))||
1126 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1127 Item.uDataAlloc ||
1128 Item.uLabelAlloc) {
1129 return -10;
1130 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001131
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001132 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
1133 return nCBORError;
1134 }
1135 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1136 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
1137 Item.uDataAlloc ||
1138 Item.uLabelAlloc ||
1139 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("xxxx"))) {
1140 return -11;
1141 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001142
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001143 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
1144 return nCBORError;
1145 }
1146 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1147 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("bytes 2")) ||
1148 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1149 Item.uDataAlloc ||
1150 Item.uLabelAlloc) {
1151 return -12;
1152 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001153
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001154 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
1155 return nCBORError;
1156 }
1157 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1158 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
1159 Item.uDataAlloc ||
1160 Item.uLabelAlloc ||
1161 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("yyyy"))) {
1162 return -13;
1163 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001164
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001165 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
1166 return nCBORError;
1167 }
1168 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1169 Item.uDataAlloc ||
1170 Item.uLabelAlloc ||
1171 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("another int")) ||
1172 Item.uDataType != QCBOR_TYPE_TEXT_STRING) {
1173 return -14;
1174 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001175
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001176 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
1177 return nCBORError;
1178 }
1179 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1180 Item.uDataAlloc ||
1181 Item.uLabelAlloc ||
1182 Item.uDataType != QCBOR_TYPE_INT64 ||
1183 Item.val.int64 != 98) {
1184 return -15;
1185 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001186
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001187 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
1188 return nCBORError;
1189 }
1190 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1191 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("text 2"))||
1192 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1193 Item.uDataAlloc ||
1194 Item.uLabelAlloc) {
1195 return -16;
1196 }
1197
1198 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
1199 return nCBORError;
1200 }
1201 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1202 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1203 Item.uDataAlloc ||
1204 Item.uLabelAlloc ||
1205 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("lies, damn lies and statistics"))) {
1206 return -17;
1207 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001208
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001209 return 0;
1210}
1211
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001212
1213/*
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301214 Fully or partially decode pValidMapEncoded. When
1215 partially decoding check for the right error code.
1216 How much partial decoding depends on nLevel.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001217
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301218 The partial decodes test error conditions of
1219 incomplete encoded input.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001220
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301221 This could be combined with the above test
1222 and made prettier and maybe a little more
1223 thorough.
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001224 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001225static int32_t ExtraBytesTest(int nLevel)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001226{
1227 QCBORDecodeContext DCtx;
1228 QCBORItem Item;
1229 int nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001230
Laurence Lundbladeee851742020-01-08 08:37:05 -08001231 QCBORDecode_Init(&DCtx,
1232 (UsefulBufC){pValidMapEncoded, sizeof(pValidMapEncoded)},
1233 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001234
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001235 if(nLevel < 1) {
1236 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_EXTRA_BYTES) {
1237 return -1;
1238 } else {
1239 return 0;
1240 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001241 }
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301242
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001243
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001244 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001245 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001246 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001247 if(Item.uDataType != QCBOR_TYPE_MAP ||
1248 Item.val.uCount != 3)
1249 return -1;
1250
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001251 if(nLevel < 2) {
1252 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
1253 return -1;
1254 } else {
1255 return 0;
1256 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001257 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001258
1259
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001260 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001261 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001262 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001263 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001264 Item.uDataType != QCBOR_TYPE_INT64 ||
1265 Item.val.uCount != 42 ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001266 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("first integer"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001267 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001268 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001269
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001270 if(nLevel < 3) {
1271 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
1272 return -1;
1273 } else {
1274 return 0;
1275 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001276 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001277
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001278 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001279 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001280 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001281 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001282 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("an array of two strings")) ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001283 Item.uDataType != QCBOR_TYPE_ARRAY ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001284 Item.val.uCount != 2) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001285 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001286 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001287
1288
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001289 if(nLevel < 4) {
1290 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
1291 return -1;
1292 } else {
1293 return 0;
1294 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001295 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001296
1297
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001298 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001299 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001300 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001301 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001302 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("string1"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001303 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001304 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001305
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001306 if(nLevel < 5) {
1307 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
1308 return -1;
1309 } else {
1310 return 0;
1311 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001312 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001313
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001314 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001315 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001316 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001317 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001318 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("string2"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001319 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001320 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001321
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001322 if(nLevel < 6) {
1323 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
1324 return -1;
1325 } else {
1326 return 0;
1327 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001328 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001329
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001330 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001331 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001332 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001333 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001334 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("map in a map")) ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001335 Item.uDataType != QCBOR_TYPE_MAP ||
1336 Item.val.uCount != 4)
1337 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001338
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001339 if(nLevel < 7) {
1340 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
1341 return -1;
1342 } else {
1343 return 0;
1344 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001345 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001346
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001347 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001348 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001349 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001350 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001351 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("bytes 1")) ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001352 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001353 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("xxxx"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001354 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001355 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001356
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001357 if(nLevel < 8) {
1358 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
1359 return -1;
1360 } else {
1361 return 0;
1362 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001363 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001364
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001365 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001366 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001367 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001368 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001369 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("bytes 2")) ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001370 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001371 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("yyyy"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001372 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001373 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001374
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001375 if(nLevel < 9) {
1376 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
1377 return -1;
1378 } else {
1379 return 0;
1380 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001381 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001382
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001383 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001384 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001385 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001386 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001387 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("another int")) ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001388 Item.uDataType != QCBOR_TYPE_INT64 ||
1389 Item.val.int64 != 98)
1390 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001391
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001392 if(nLevel < 10) {
1393 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
1394 return -1;
1395 } else {
1396 return 0;
1397 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001398 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001399
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001400 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001401 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001402 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001403 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001404 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("text 2"))||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001405 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001406 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("lies, damn lies and statistics"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001407 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001408 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001409
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301410 if(QCBORDecode_Finish(&DCtx)) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001411 return -1;
1412 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001413
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001414 return 0;
1415}
1416
1417
1418
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001419/*
1420 Public function for initialization. See header qcbor.h
1421 */
1422int32_t ParseMapTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001423{
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001424 // Parse a moderatly complex map structure very thoroughly
1425 int32_t nResult = ParseMapTest1(QCBOR_DECODE_MODE_NORMAL);
1426 if(nResult) {
1427 return nResult;
1428 }
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08001429
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001430 // Again, but in strings-only mode. It should succeed since the input
1431 // map has only string labels.
1432 nResult = ParseMapTest1(QCBOR_DECODE_MODE_MAP_STRINGS_ONLY);
1433 if(nResult) {
1434 return nResult;
1435 }
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08001436
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001437 // Again, but try to finish the decoding before the end of the
1438 // input at 10 different place and see that the right error code
1439 // is returned.
1440 for(int i = 0; i < 10; i++) {
1441 nResult = ExtraBytesTest(i);
1442 if(nResult) {
1443 break;
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001444 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001445 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001446
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001447 return nResult;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001448}
1449
1450
Laurence Lundbladeee851742020-01-08 08:37:05 -08001451static uint8_t spSimpleValues[] = {0x8a, 0xf4, 0xf5, 0xf6, 0xf7, 0xff,
1452 0xe0, 0xf3, 0xf8, 0x00, 0xf8, 0x13,
1453 0xf8, 0x1f, 0xf8, 0x20, 0xf8, 0xff};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001454
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001455int32_t ParseSimpleTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001456{
1457 QCBORDecodeContext DCtx;
1458 QCBORItem Item;
1459 int nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001460
1461
Laurence Lundbladeee851742020-01-08 08:37:05 -08001462 QCBORDecode_Init(&DCtx,
1463 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSimpleValues),
1464 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001465
1466
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001467 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1468 return nCBORError;
1469 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
1470 Item.val.uCount != 10)
1471 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001472
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001473 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1474 return nCBORError;
1475 if(Item.uDataType != QCBOR_TYPE_FALSE)
1476 return -1;
1477
1478 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1479 return nCBORError;
1480 if(Item.uDataType != QCBOR_TYPE_TRUE)
1481 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001482
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001483 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1484 return nCBORError;
1485 if(Item.uDataType != QCBOR_TYPE_NULL)
1486 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001487
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001488 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1489 return nCBORError;
1490 if(Item.uDataType != QCBOR_TYPE_UNDEF)
1491 return -1;
1492
1493 // A break
Laurence Lundblade9e3651c2018-10-10 11:49:55 +08001494 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_BREAK)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001495 return -1;
1496
1497 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1498 return nCBORError;
1499 if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 0)
1500 return -1;
1501
1502 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1503 return nCBORError;
1504 if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 19)
1505 return -1;
1506
Laurence Lundblade077475f2019-04-26 09:06:33 -07001507 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_TYPE_7)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001508 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001509
Laurence Lundblade077475f2019-04-26 09:06:33 -07001510 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_TYPE_7)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001511 return -1;
1512
Laurence Lundblade077475f2019-04-26 09:06:33 -07001513 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_TYPE_7)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001514 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001515
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001516 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1517 return nCBORError;
1518 if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 32)
1519 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001520
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001521 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1522 return nCBORError;
1523 if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 255)
1524 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001525
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001526 return 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001527
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001528}
1529
1530
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001531static int IsNotWellFormedError(QCBORError nErr)
1532{
1533 switch(nErr){
1534 case QCBOR_ERR_INDEFINITE_STRING_CHUNK:
1535 case QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN:
1536 case QCBOR_ERR_UNSUPPORTED:
1537 case QCBOR_ERR_HIT_END:
1538 case QCBOR_ERR_BAD_TYPE_7:
1539 case QCBOR_ERR_BAD_BREAK:
1540 case QCBOR_ERR_EXTRA_BYTES:
1541 case QCBOR_ERR_BAD_INT:
1542 return 1;
1543 default:
1544 return 0;
1545 }
1546}
1547
1548
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001549int32_t NotWellFormedTests()
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001550{
1551 // Loop over all the not-well-formed instance of CBOR
1552 // that are test vectors in not_well_formed_cbor.h
1553 const uint16_t nArraySize = sizeof(paNotWellFormedCBOR)/sizeof(struct someBinaryBytes);
1554 for(uint16_t nIterate = 0; nIterate < nArraySize; nIterate++) {
1555 const struct someBinaryBytes *pBytes = &paNotWellFormedCBOR[nIterate];
1556 const UsefulBufC Input = (UsefulBufC){pBytes->p, pBytes->n};
1557
Laurence Lundbladeee851742020-01-08 08:37:05 -08001558 // Set up decoder context. String allocator needed for indefinite
1559 // string test cases
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001560 QCBORDecodeContext DCtx;
1561 QCBORDecode_Init(&DCtx, Input, QCBOR_DECODE_MODE_NORMAL);
1562 UsefulBuf_MAKE_STACK_UB(Pool, 100);
1563 QCBORDecode_SetMemPool(&DCtx, Pool, 0);
1564
1565 // Loop getting items until no more to get
1566 QCBORError nCBORError;
1567 do {
1568 QCBORItem Item;
1569
1570 nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
1571 } while(nCBORError == QCBOR_SUCCESS);
1572
1573 // Every test vector must fail with
1574 // a not-well-formed error. If not
1575 // this test fails.
1576 if(!IsNotWellFormedError(nCBORError)) {
1577 // Return index of failure in the error code
1578 return 2000 + nIterate;
1579 }
1580 }
1581 return 0;
1582}
1583
1584
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001585struct FailInput {
Laurence Lundblade59289e52019-12-30 13:44:37 -08001586 UsefulBufC Input;
1587 QCBORError nError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001588};
1589
Laurence Lundblade59289e52019-12-30 13:44:37 -08001590
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001591static int32_t ProcessFailures(struct FailInput *pFailInputs, size_t nNumFails)
Laurence Lundblade59289e52019-12-30 13:44:37 -08001592{
1593 for(struct FailInput *pF = pFailInputs; pF < pFailInputs + nNumFails; pF++) {
1594 // Set up the decoding context including a memory pool so that
1595 // indefinite length items can be checked
1596 QCBORDecodeContext DCtx;
1597 QCBORDecode_Init(&DCtx, pF->Input, QCBOR_DECODE_MODE_NORMAL);
1598 UsefulBuf_MAKE_STACK_UB(Pool, 100);
1599 QCBORError nCBORError = QCBORDecode_SetMemPool(&DCtx, Pool, 0);
1600 if(nCBORError) {
1601 return -9;
1602 }
1603
1604 // Iterate until there is an error of some sort error
1605 QCBORItem Item;
1606 do {
1607 // Set to something none-zero other than QCBOR_TYPE_NONE
1608 memset(&Item, 0x33, sizeof(Item));
1609
1610 nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
1611 } while(nCBORError == QCBOR_SUCCESS);
1612
1613 // Must get the expected error or the this test fails
1614 // The data and label type must also be QCBOR_TYPE_NONE
1615 if(nCBORError != pF->nError ||
1616 Item.uDataType != QCBOR_TYPE_NONE ||
1617 Item.uLabelType != QCBOR_TYPE_NONE) {
Laurence Lundblade06350ea2020-01-27 19:32:40 -08001618 // return index of CBOR + 100
1619 const size_t nIndex = (size_t)(pF - pFailInputs)/sizeof(struct FailInput);
1620 return (int32_t)(nIndex * 100 + nCBORError);
Laurence Lundblade59289e52019-12-30 13:44:37 -08001621 }
1622 }
1623
1624 return 0;
1625}
1626
1627
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001628struct FailInput Failures[] = {
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001629 // Most of this is copied from not_well_formed.h. Here the error code
1630 // returned is also checked.
1631
1632 // Indefinite length strings must be closed off
1633 // An indefinite length byte string not closed off
1634 { {(uint8_t[]){0x5f, 0x41, 0x00}, 3}, QCBOR_ERR_HIT_END },
1635 // An indefinite length text string not closed off
1636 { {(uint8_t[]){0x7f, 0x61, 0x00}, 3}, QCBOR_ERR_HIT_END },
1637
1638
1639 // All the chunks in an indefinite length string must be of the type of indefinite length string
1640 // indefinite length byte string with text string chunk
1641 { {(uint8_t[]){0x5f, 0x61, 0x00, 0xff}, 4}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1642 // indefinite length text string with a byte string chunk
1643 { {(uint8_t[]){0x7f, 0x41, 0x00, 0xff}, 4}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1644 // indefinite length byte string with an positive integer chunk
1645 { {(uint8_t[]){0x5f, 0x00, 0xff}, 3}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1646 // indefinite length byte string with an negative integer chunk
1647 { {(uint8_t[]){0x5f, 0x21, 0xff}, 3}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1648 // indefinite length byte string with an array chunk
1649 { {(uint8_t[]){0x5f, 0x80, 0xff}, 3}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1650 // indefinite length byte string with an map chunk
1651 { {(uint8_t[]){0x5f, 0xa0, 0xff}, 3}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1652 // indefinite length byte string with tagged integer chunk
1653 { {(uint8_t[]){0x5f, 0xc0, 0x00, 0xff}, 4}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1654 // indefinite length byte string with an simple type chunk
1655 { {(uint8_t[]){0x5f, 0xe0, 0xff}, 3}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1656 { {(uint8_t[]){0x5f, 0x5f, 0x41, 0x00, 0xff, 0xff}, 6}, QCBOR_ERR_INDEFINITE_STRING_CHUNK},
1657 // indefinite length text string with indefinite string inside
1658 { {(uint8_t[]){0x7f, 0x7f, 0x61, 0x00, 0xff, 0xff}, 6}, QCBOR_ERR_INDEFINITE_STRING_CHUNK},
1659
1660
1661 // Definte length maps and arrays must be closed by having the right number of items
1662 // A definte length array that is supposed to have 1 item, but has none
1663 { {(uint8_t[]){0x81}, 1}, QCBOR_ERR_HIT_END },
1664 // A definte length array that is supposed to have 2 items, but has only 1
1665 { {(uint8_t[]){0x82, 0x00}, 2}, QCBOR_ERR_HIT_END },
1666 // A definte length array that is supposed to have 511 items, but has only 1
1667 { {(uint8_t[]){0x9a, 0x01, 0xff, 0x00}, 4}, QCBOR_ERR_HIT_END },
1668 // A definte length map that is supposed to have 1 item, but has none
1669 { {(uint8_t[]){0xa1}, 1}, QCBOR_ERR_HIT_END },
1670 // A definte length map that is supposed to have s item, but has only 1
1671 { {(uint8_t[]){0xa2, 0x01, 0x02}, 3}, QCBOR_ERR_HIT_END },
1672
1673
1674 // Indefinte length maps and arrays must be ended by a break
1675 // Indefinite length array with zero items and no break
1676 { {(uint8_t[]){0x9f}, 1}, QCBOR_ERR_HIT_END },
1677 // Indefinite length array with two items and no break
1678 { {(uint8_t[]){0x9f, 0x01, 0x02}, 3}, QCBOR_ERR_HIT_END },
1679 // Indefinite length map with zero items and no break
1680 { {(uint8_t[]){0xbf}, 1}, QCBOR_ERR_HIT_END },
1681 // Indefinite length map with two items and no break
1682 { {(uint8_t[]){0xbf, 0x01, 0x02, 0x01, 0x02}, 5}, QCBOR_ERR_HIT_END },
1683
1684
1685 // Nested maps and arrays must be closed off (some extra nested test vectors)
1686 // Unclosed indefinite array containing a close definite array
1687 { {(uint8_t[]){0x9f, 0x80, 0x00}, 3}, QCBOR_ERR_HIT_END },
1688 // Definite length array containing an unclosed indefinite array
1689 { {(uint8_t[]){0x81, 0x9f}, 2}, QCBOR_ERR_HIT_END },
1690 // Deeply nested definite length arrays with deepest one unclosed
1691 { {(uint8_t[]){0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81}, 9}, QCBOR_ERR_HIT_END },
1692 // Deeply nested indefinite length arrays with deepest one unclosed
1693 { {(uint8_t[]){0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0xff, 0xff, 0xff, 0xff}, 9}, QCBOR_ERR_HIT_END },
1694 // Mixed nesting with indefinite unclosed
Laurence Lundbladeee851742020-01-08 08:37:05 -08001695 // TODO: think through this one
1696 { {(uint8_t[]){0x9f, 0x81, 0x9f, 0x81, 0x9f, 0x9f, 0xff, 0xff, 0xff}, 9}, QCBOR_ERR_BAD_BREAK },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001697 // Mixed nesting with definite unclosed
Laurence Lundbladeee851742020-01-08 08:37:05 -08001698 // TODO: think through this one
1699 { {(uint8_t[]){0x9f, 0x82, 0x9f, 0x81, 0x9f, 0x9f, 0xff, 0xff, 0xff, 0xff}, 10}, QCBOR_ERR_BAD_BREAK },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001700
1701
1702 // The "argument" for the data item is incomplete
1703 // Positive integer missing 1 byte argument
1704 { {(uint8_t[]){0x18}, 1}, QCBOR_ERR_HIT_END },
1705 // Positive integer missing 2 byte argument
1706 { {(uint8_t[]){0x19}, 1}, QCBOR_ERR_HIT_END },
1707 // Positive integer missing 4 byte argument
1708 { {(uint8_t[]){0x1a}, 1}, QCBOR_ERR_HIT_END },
1709 // Positive integer missing 8 byte argument
1710 { {(uint8_t[]){0x1b}, 1}, QCBOR_ERR_HIT_END },
1711 // Positive integer missing 1 byte of 2 byte argument
1712 { {(uint8_t[]){0x19, 0x01}, 2}, QCBOR_ERR_HIT_END },
1713 // Positive integer missing 2 bytes of 4 byte argument
1714 { {(uint8_t[]){0x1a, 0x01, 0x02}, 3}, QCBOR_ERR_HIT_END },
1715 // Positive integer missing 1 bytes of 7 byte argument
1716 { {(uint8_t[]){0x1b, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07}, 8}, QCBOR_ERR_HIT_END },
1717 // Negative integer missing 1 byte argument
1718 { {(uint8_t[]){0x38}, 1}, QCBOR_ERR_HIT_END },
1719 // Binary string missing 1 byte argument
1720 { {(uint8_t[]){0x58}, 1}, QCBOR_ERR_HIT_END },
1721 // Text string missing 1 byte argument
1722 { {(uint8_t[]){0x78}, 1}, QCBOR_ERR_HIT_END },
1723 // Array missing 1 byte argument
1724 { {(uint8_t[]){0x98}, 1}, QCBOR_ERR_HIT_END },
1725 // Map missing 1 byte argument
1726 { {(uint8_t[]){0xb8}, 1}, QCBOR_ERR_HIT_END },
1727 // Tag missing 1 byte argument
1728 { {(uint8_t[]){0xd8}, 1}, QCBOR_ERR_HIT_END },
1729 // Simple missing 1 byte argument
1730 { {(uint8_t[]){0xf8}, 1}, QCBOR_ERR_HIT_END },
1731
1732
1733 // Breaks must not occur in definite length arrays and maps
1734 // Array of length 1 with sole member replaced by a break
1735 { {(uint8_t[]){0x81, 0xff}, 2}, QCBOR_ERR_BAD_BREAK },
1736 // Array of length 2 with 2nd member replaced by a break
1737 { {(uint8_t[]){0x82, 0x00, 0xff}, 3}, QCBOR_ERR_BAD_BREAK },
1738 // Map of length 1 with sole member label replaced by a break
1739 { {(uint8_t[]){0xa1, 0xff}, 2}, QCBOR_ERR_BAD_BREAK },
1740 // Map of length 1 with sole member label replaced by break
1741 // Alternate representation that some decoders handle difference
1742 { {(uint8_t[]){0xa1, 0xff, 0x00}, 3}, QCBOR_ERR_BAD_BREAK },
1743 // Array of length 1 with 2nd member value replaced by a break
1744 { {(uint8_t[]){0xa1, 0x00, 0xff}, 3}, QCBOR_ERR_BAD_BREAK },
1745 // Map of length 2 with 2nd member replaced by a break
1746 { {(uint8_t[]){0xa2, 0x00, 0x00, 0xff}, 4}, QCBOR_ERR_BAD_BREAK },
1747
1748
1749 // Breaks must not occur on their own out of an indefinite length data item
1750 // A bare break is not well formed
1751 { {(uint8_t[]){0xff}, 1}, QCBOR_ERR_BAD_BREAK },
1752 // A bare break after a zero length definite length array
1753 { {(uint8_t[]){0x80, 0xff}, 2}, QCBOR_ERR_BAD_BREAK },
1754 // A bare break after a zero length indefinite length map
1755 { {(uint8_t[]){0x9f, 0xff, 0xff}, 3}, QCBOR_ERR_BAD_BREAK },
1756
1757
1758 // Forbidden two byte encodings of simple types
1759 // Must use 0xe0 instead
1760 { {(uint8_t[]){0xf8, 0x00}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1761 // Should use 0xe1 instead
1762 { {(uint8_t[]){0xf8, 0x01}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1763 // Should use 0xe2 instead
1764 { {(uint8_t[]){0xf8, 0x02}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1765 // Should use 0xe3 instead
1766 { {(uint8_t[]){0xf8, 0x03}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1767 // Should use 0xe4 instead
1768 { {(uint8_t[]){0xf8, 0x04}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1769 // Should use 0xe5 instead
1770 { {(uint8_t[]){0xf8, 0x05}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1771 // Should use 0xe6 instead
1772 { {(uint8_t[]){0xf8, 0x06}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1773 // Should use 0xe7 instead
1774 { {(uint8_t[]){0xf8, 0x07}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1775 // Should use 0xe8 instead
1776 { {(uint8_t[]){0xf8, 0x08}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1777 // Should use 0xe9 instead
1778 { {(uint8_t[]){0xf8, 0x09}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1779 // Should use 0xea instead
1780 { {(uint8_t[]){0xf8, 0x0a}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1781 // Should use 0xeb instead
1782 { {(uint8_t[]){0xf8, 0x0b}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1783 // Should use 0xec instead
1784 { {(uint8_t[]){0xf8, 0x0c}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1785 // Should use 0xed instead
1786 { {(uint8_t[]){0xf8, 0x0d}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1787 // Should use 0xee instead
1788 { {(uint8_t[]){0xf8, 0x0e}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1789 // Should use 0xef instead
1790 { {(uint8_t[]){0xf8, 0x0f}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1791 // Should use 0xf0 instead
1792 { {(uint8_t[]){0xf8, 0x10}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1793 // Should use 0xf1 instead
1794 { {(uint8_t[]){0xf8, 0x11}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1795 // Should use 0xf2 instead
1796 { {(uint8_t[]){0xf8, 0x12}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1797 // Must use 0xf3 instead
1798 { {(uint8_t[]){0xf8, 0x13}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1799 // Must use 0xf4 instead
1800 { {(uint8_t[]){0xf8, 0x14}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1801 // Must use 0xf5 instead
1802 { {(uint8_t[]){0xf8, 0x15}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1803 // Must use 0xf6 instead
1804 { {(uint8_t[]){0xf8, 0x16}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1805 // Must use 0xf7 instead
1806 { {(uint8_t[]){0xf8, 0x17}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1807 // Must use 0xf8 instead
1808 { {(uint8_t[]){0xf8, 0x18}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1809
1810
1811 // Integers with additional info indefinite length
1812 // Positive integer with additional info indefinite length
1813 { {(uint8_t[]){0x1f}, 1}, QCBOR_ERR_BAD_INT },
1814 // Negative integer with additional info indefinite length
1815 { {(uint8_t[]){0x3f}, 1}, QCBOR_ERR_BAD_INT },
1816 // CBOR tag with "argument" an indefinite length
1817 { {(uint8_t[]){0xdf, 0x00}, 2}, QCBOR_ERR_BAD_INT },
1818 // CBOR tag with "argument" an indefinite length alternate vector
1819 { {(uint8_t[]){0xdf}, 1}, QCBOR_ERR_BAD_INT },
1820
1821
1822 // Missing bytes from a deterministic length string
1823 // A byte string is of length 1 without the 1 byte
1824 { {(uint8_t[]){0x41}, 1}, QCBOR_ERR_HIT_END },
1825 // A text string is of length 1 without the 1 byte
1826 { {(uint8_t[]){0x61}, 1}, QCBOR_ERR_HIT_END },
1827 // Byte string should have 2^32-1 bytes, but has one
1828 { {(uint8_t[]){0x5a, 0xff, 0xff, 0xff, 0xff, 0x00}, 6}, QCBOR_ERR_HIT_END },
1829 // Byte string should have 2^32-1 bytes, but has one
1830 { {(uint8_t[]){0x7a, 0xff, 0xff, 0xff, 0xff, 0x00}, 6}, QCBOR_ERR_HIT_END },
1831
1832
1833 // Use of unassigned additional information values
1834 // Major type positive integer with reserved value 28
1835 { {(uint8_t[]){0x1c}, 1}, QCBOR_ERR_UNSUPPORTED },
1836 // Major type positive integer with reserved value 29
1837 { {(uint8_t[]){0x1d}, 1}, QCBOR_ERR_UNSUPPORTED },
1838 // Major type positive integer with reserved value 30
1839 { {(uint8_t[]){0x1e}, 1}, QCBOR_ERR_UNSUPPORTED },
1840 // Major type negative integer with reserved value 28
1841 { {(uint8_t[]){0x3c}, 1}, QCBOR_ERR_UNSUPPORTED },
1842 // Major type negative integer with reserved value 29
1843 { {(uint8_t[]){0x3d}, 1}, QCBOR_ERR_UNSUPPORTED },
1844 // Major type negative integer with reserved value 30
1845 { {(uint8_t[]){0x3e}, 1}, QCBOR_ERR_UNSUPPORTED },
1846 // Major type byte string with reserved value 28 length
1847 { {(uint8_t[]){0x5c}, 1}, QCBOR_ERR_UNSUPPORTED },
1848 // Major type byte string with reserved value 29 length
1849 { {(uint8_t[]){0x5d}, 1}, QCBOR_ERR_UNSUPPORTED },
1850 // Major type byte string with reserved value 30 length
1851 { {(uint8_t[]){0x5e}, 1}, QCBOR_ERR_UNSUPPORTED },
1852 // Major type text string with reserved value 28 length
1853 { {(uint8_t[]){0x7c}, 1}, QCBOR_ERR_UNSUPPORTED },
1854 // Major type text string with reserved value 29 length
1855 { {(uint8_t[]){0x7d}, 1}, QCBOR_ERR_UNSUPPORTED },
1856 // Major type text string with reserved value 30 length
1857 { {(uint8_t[]){0x7e}, 1}, QCBOR_ERR_UNSUPPORTED },
1858 // Major type array with reserved value 28 length
1859 { {(uint8_t[]){0x9c}, 1}, QCBOR_ERR_UNSUPPORTED },
1860 // Major type array with reserved value 29 length
1861 { {(uint8_t[]){0x9d}, 1}, QCBOR_ERR_UNSUPPORTED },
1862 // Major type array with reserved value 30 length
1863 { {(uint8_t[]){0x9e}, 1}, QCBOR_ERR_UNSUPPORTED },
1864 // Major type map with reserved value 28 length
1865 { {(uint8_t[]){0xbc}, 1}, QCBOR_ERR_UNSUPPORTED },
1866 // Major type map with reserved value 29 length
1867 { {(uint8_t[]){0xbd}, 1}, QCBOR_ERR_UNSUPPORTED },
1868 // Major type map with reserved value 30 length
1869 { {(uint8_t[]){0xbe}, 1}, QCBOR_ERR_UNSUPPORTED },
1870 // Major type tag with reserved value 28 length
1871 { {(uint8_t[]){0xdc}, 1}, QCBOR_ERR_UNSUPPORTED },
1872 // Major type tag with reserved value 29 length
1873 { {(uint8_t[]){0xdd}, 1}, QCBOR_ERR_UNSUPPORTED },
1874 // Major type tag with reserved value 30 length
1875 { {(uint8_t[]){0xde}, 1}, QCBOR_ERR_UNSUPPORTED },
1876 // Major type simple with reserved value 28 length
1877 { {(uint8_t[]){0xfc}, 1}, QCBOR_ERR_UNSUPPORTED },
1878 // Major type simple with reserved value 29 length
1879 { {(uint8_t[]){0xfd}, 1}, QCBOR_ERR_UNSUPPORTED },
1880 // Major type simple with reserved value 30 length
1881 { {(uint8_t[]){0xfe}, 1}, QCBOR_ERR_UNSUPPORTED },
1882
1883
1884 // Maps must have an even number of data items (key & value)
1885 // Map with 1 item when it should have 2
1886 { {(uint8_t[]){0xa1, 0x00}, 2}, QCBOR_ERR_HIT_END },
1887 // Map with 3 item when it should have 4
1888 { {(uint8_t[]){0xa2, 0x00, 0x00, 0x00}, 2}, QCBOR_ERR_HIT_END },
1889 // Map with 1 item when it should have 2
1890 { {(uint8_t[]){0xbf, 0x00, 0xff}, 3}, QCBOR_ERR_BAD_BREAK },
1891 // Map with 3 item when it should have 4
1892 { {(uint8_t[]){0xbf, 0x00, 0x00, 0x00, 0xff}, 5}, QCBOR_ERR_BAD_BREAK },
1893
1894
1895 // In addition to not-well-formed, some invalid CBOR
Laurence Lundbladeee851742020-01-08 08:37:05 -08001896 // Text-based date, with an integer
1897 { {(uint8_t[]){0xc0, 0x00}, 2}, QCBOR_ERR_BAD_OPT_TAG },
1898 // Epoch date, with an byte string
1899 { {(uint8_t[]){0xc1, 0x41, 0x33}, 3}, QCBOR_ERR_BAD_OPT_TAG },
1900 // tagged as both epoch and string dates
1901 { {(uint8_t[]){0xc1, 0xc0, 0x00}, 3}, QCBOR_ERR_BAD_OPT_TAG },
1902 // big num tagged an int, not a byte string
1903 { {(uint8_t[]){0xc2, 0x00}, 2}, QCBOR_ERR_BAD_OPT_TAG },
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001904};
1905
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001906int32_t DecodeFailureTests()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001907{
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001908 int32_t nResult;
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001909
Laurence Lundblade59289e52019-12-30 13:44:37 -08001910 nResult = ProcessFailures(Failures, sizeof(Failures)/sizeof(struct FailInput));
1911 if(nResult) {
1912 return nResult;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001913 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001914
Laurence Lundblade3a6042e2019-06-28 19:58:04 -07001915 // Corrupt the UsefulInputBuf and see that
1916 // it reflected correctly for CBOR decoding
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001917 {
1918 QCBORDecodeContext DCtx;
Laurence Lundblade3a6042e2019-06-28 19:58:04 -07001919 QCBORItem Item;
1920 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001921
Laurence Lundblade3a6042e2019-06-28 19:58:04 -07001922 QCBORDecode_Init(&DCtx,
1923 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSimpleValues),
1924 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001925
1926 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1927 return nCBORError;
1928 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
Laurence Lundblade3a6042e2019-06-28 19:58:04 -07001929 Item.val.uCount != 10) {
1930 // This wasn't supposed to happen
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001931 return -1;
Laurence Lundblade3a6042e2019-06-28 19:58:04 -07001932 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001933
Laurence Lundblade3a6042e2019-06-28 19:58:04 -07001934 DCtx.InBuf.magic = 0; // Reach in and corrupt the UsefulInputBuf
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001935
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001936 nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundblade3a6042e2019-06-28 19:58:04 -07001937 if(nCBORError != QCBOR_ERR_HIT_END) {
1938 // Did not get back the error expected
1939 return -2;
1940 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001941 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001942
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001943/*
1944 This test is disabled until QCBOREncode_EncodeHead() is brought in so
1945 the size encoded can be tied to SIZE_MAX and work for all size CPUs.
1946
1947 This relies on the largest string allowed being SIZE_MAX -4 rather than
1948 SIZE_MAX. That way the test can be performed.
1949 {
1950 QCBORDecodeContext DCtx;
1951 QCBORItem Item;
1952
1953 static uint8_t foo[] = {0x5b, 0xff, 0xff, 0xff, 0xff,
1954 0xff, 0xff, 0xff, 0xff};
1955
1956 QCBORDecode_Init(&DCtx,
1957 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(foo),
1958 QCBOR_DECODE_MODE_NORMAL);
1959
1960 if(QCBOR_ERR_STRING_TOO_LONG != QCBORDecode_GetNext(&DCtx, &Item)) {
1961 return -4;
1962 }
1963 }
1964*/
1965
Laurence Lundblade3a6042e2019-06-28 19:58:04 -07001966 return 0;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001967}
1968
1969
Laurence Lundbladea2e29072018-12-30 09:20:06 -08001970/* Try all 256 values of the byte at nLen including recursing for
1971 each of the values to try values at nLen+1 ... up to nLenMax
1972 */
Laurence Lundblade06350ea2020-01-27 19:32:40 -08001973static void ComprehensiveInputRecurser(uint8_t *pBuf, size_t nLen, size_t nLenMax)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001974{
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001975 if(nLen >= nLenMax) {
1976 return;
1977 }
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08001978
Laurence Lundbladea2e29072018-12-30 09:20:06 -08001979 for(int inputByte = 0; inputByte < 256; inputByte++) {
1980 // Set up the input
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001981 pBuf[nLen] = (uint8_t)inputByte;
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08001982 const UsefulBufC Input = {pBuf, nLen+1};
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08001983
Laurence Lundbladea2e29072018-12-30 09:20:06 -08001984 // Get ready to parse
1985 QCBORDecodeContext DCtx;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001986 QCBORDecode_Init(&DCtx, Input, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001987
Laurence Lundbladea2e29072018-12-30 09:20:06 -08001988 // Parse by getting the next item until an error occurs
1989 // Just about every possible decoder error can occur here
1990 // The goal of this test is not to check for the correct
1991 // error since that is not really possible. It is to
1992 // see that there is no crash on hostile input.
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001993 while(1) {
Laurence Lundbladea2e29072018-12-30 09:20:06 -08001994 QCBORItem Item;
1995 QCBORError nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001996 if(nCBORError != QCBOR_SUCCESS) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001997 break;
1998 }
1999 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002000
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002001 ComprehensiveInputRecurser(pBuf, nLen+1, nLenMax);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002002 }
2003}
2004
2005
2006/*
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002007 Public function for initialization. See header qcbor.h
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002008 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002009int32_t ComprehensiveInputTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002010{
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002011 // Size 2 tests 64K inputs and runs quickly
2012 uint8_t pBuf[2];
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002013
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002014 ComprehensiveInputRecurser(pBuf, 0, sizeof(pBuf));
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002015
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002016 return 0;
2017}
2018
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002019
2020/*
2021 Public function for initialization. See header qcbor.h
2022 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002023int32_t BigComprehensiveInputTest()
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002024{
2025 // size 3 tests 16 million inputs and runs OK
2026 // in seconds on fast machines. Size 4 takes
2027 // 10+ minutes and 5 half a day on fast
2028 // machines. This test is kept separate from
2029 // the others so as to no slow down the use
2030 // of them as a very frequent regression.
2031 uint8_t pBuf[3]; //
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08002032
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002033 ComprehensiveInputRecurser(pBuf, 0, sizeof(pBuf));
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08002034
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002035 return 0;
2036}
2037
2038
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002039static uint8_t spDateTestInput[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002040 0xc0, // tag for string date
2041 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002042
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002043 0xc1, // tag for epoch date
2044 0x1a, 0x53, 0x72, 0x4E, 0x00, // Epoch date 1400000000; Tue, 13 May 2014 16:53:20 GMT
2045
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002046 // CBOR_TAG_B64
2047 0xc1, 0xcf, 0xd8, 0x22, // 0xee, // Epoch date with extra tags TODO: fix this test
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002048 0x1a, 0x53, 0x72, 0x4E, 0x01,
2049
2050 0xc1, // tag for epoch date
2051 0x1b, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, // Too large integer
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002052
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002053 0xc1, // tag for epoch date
2054 0xfa, 0x3f, 0x8c, 0xcc, 0xcd, // double with value 1.1
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002055
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002056 0xc1, // tag for epoch date
Laurence Lundbladea1ad8782019-11-08 00:12:11 -08002057 0xfa, 0x7f, 0x7f, 0xff, 0xff, // 3.4028234663852886e+38 too large
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002058
Laurence Lundbladea1ad8782019-11-08 00:12:11 -08002059 0xc1, // tag for epoch date
2060 0xfb, 0x43, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 9223372036854775808.000000 just barely too large
2061 //0xfa, 0x7f, 0x7f, 0xff, 0xff // 3.4028234663852886e+38 too large
2062
2063 0xc1, // tag for epoch date
2064 0xfb, 0x43, 0xdf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe // 9223372036854773760 largest supported
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002065};
2066
2067
2068// have to check float expected only to within an epsilon
2069int CHECK_EXPECTED_DOUBLE(double val, double expected) {
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002070
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002071 double diff = val - expected;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002072
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002073 diff = fabs(diff);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002074
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002075 return diff > 0.0000001;
2076}
2077
2078
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002079int32_t DateParseTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002080{
2081 QCBORDecodeContext DCtx;
2082 QCBORItem Item;
2083 int nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002084
Laurence Lundbladeee851742020-01-08 08:37:05 -08002085 QCBORDecode_Init(&DCtx,
2086 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spDateTestInput),
2087 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002088
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002089 const uint64_t uTags[] = {15};
2090 QCBORTagListIn TagList = {1, uTags};
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002091
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002092 QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TagList);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002093
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002094 // String date
2095 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
2096 return -1;
2097 if(Item.uDataType != QCBOR_TYPE_DATE_STRING ||
Laurence Lundblade9e3651c2018-10-10 11:49:55 +08002098 UsefulBuf_Compare(Item.val.dateString, UsefulBuf_FromSZ("1985-04-12"))){
Laurence Lundblade67bd5512018-11-02 21:44:06 +07002099 return -2;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002100 }
2101
2102 // Epoch date
2103 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade67bd5512018-11-02 21:44:06 +07002104 return -3;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002105 if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH ||
2106 Item.val.epochDate.nSeconds != 1400000000 ||
2107 Item.val.epochDate.fSecondsFraction != 0 ) {
Laurence Lundblade67bd5512018-11-02 21:44:06 +07002108 return -4;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002109 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002110
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002111 // Epoch date with extra CBOR_TAG_B64 tag that doesn't really mean anything
2112 // but want to be sure extra tag doesn't cause a problem
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002113 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade67bd5512018-11-02 21:44:06 +07002114 return -5;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002115 if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH ||
2116 Item.val.epochDate.nSeconds != 1400000001 ||
2117 Item.val.epochDate.fSecondsFraction != 0 ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002118 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_B64)) {
Laurence Lundblade67bd5512018-11-02 21:44:06 +07002119 return -6;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002120 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002121
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002122 // Epoch date that is too large for our representation
2123 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_DATE_OVERFLOW) {
Laurence Lundblade67bd5512018-11-02 21:44:06 +07002124 return -7;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002125 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002126
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002127 // Epoch date in float format with fractional seconds
2128 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade67bd5512018-11-02 21:44:06 +07002129 return -8;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002130 if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH ||
2131 Item.val.epochDate.nSeconds != 1 ||
2132 CHECK_EXPECTED_DOUBLE(Item.val.epochDate.fSecondsFraction, 0.1 )) {
Laurence Lundblade67bd5512018-11-02 21:44:06 +07002133 return -9;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002134 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002135
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002136 // Epoch date float that is too large for our representation
2137 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_DATE_OVERFLOW) {
Laurence Lundblade67bd5512018-11-02 21:44:06 +07002138 return -10;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002139 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002140
Laurence Lundbladea1ad8782019-11-08 00:12:11 -08002141 // Epoch date double that is just slightly too large
2142 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_DATE_OVERFLOW) {
2143 return -11;
2144 }
2145
2146 // Largest double epoch date supported
2147 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_SUCCESS ||
2148 Item.uDataType != QCBOR_TYPE_DATE_EPOCH ||
2149 Item.val.epochDate.nSeconds != 9223372036854773760 ||
2150 Item.val.epochDate.nSeconds == 0) {
2151 return -12;
2152 }
Laurence Lundbladeee851742020-01-08 08:37:05 -08002153 // TODO: could use a few more tests with float, double, and half precsion
2154 // and negative (but coverage is still pretty good)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002155
2156 return 0;
2157}
2158
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002159// Really simple basic input for tagging test
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002160static uint8_t spOptTestInput[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002161 0xd9, 0xd9, 0xf7, // CBOR magic number
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002162 0x81, // Array of one
2163 0xd8, 0x04, // non-preferred serialization of tag 4
2164 0x82, 0x01, 0x03}; // fraction 1/3
2165
Laurence Lundblade59289e52019-12-30 13:44:37 -08002166/*
2167 DB 9192939495969798 # tag(10489608748473423768)
2168 80 # array(0)
2169 */
Laurence Lundbladeee851742020-01-08 08:37:05 -08002170static uint8_t spEncodedLargeTag[] = {0xdb, 0x91, 0x92, 0x93, 0x94, 0x95,
2171 0x96, 0x97, 0x98, 0x80};
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002172
Laurence Lundblade59289e52019-12-30 13:44:37 -08002173/*
2174DB 9192939495969798 # tag(10489608748473423768)
2175 D8 88 # tag(136)
2176 C6 # tag(6)
2177 C7 # tag(7)
2178 80 # array(0)
2179*/
Laurence Lundbladeee851742020-01-08 08:37:05 -08002180static uint8_t spLotsOfTags[] = {0xdb, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96,
2181 0x97, 0x98, 0xd8, 0x88, 0xc6, 0xc7, 0x80};
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002182
2183/*
2184 The cbor.me parse of this.
2185 55799(55799(55799({6(7(-23)): 5859837686836516696(7({7(-20): 11({17(-18): 17(17(17("Organization"))),
Laurence Lundblade59289e52019-12-30 13:44:37 -08002186 9(-17): 773("SSG"), -15: 16(17(6(7(8(9(10(11(12(13(14(15("Confusion")))))))))))), 17(-16): 17("San Diego"),
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002187 17(-14): 17("US")}), 23(-19): 19({-11: 9({-9: -7}),
2188 90599561(90599561(90599561(-10))): 12(h'0102030405060708090A')})})),
2189 16(-22): 23({11(8(7(-5))): 8(-3)})})))
2190 */
2191static uint8_t spCSRWithTags[] = {
2192 0xd9, 0xd9, 0xf7, 0xd9, 0xd9, 0xf7, 0xd9, 0xd9, 0xf7, 0xa2,
2193 0xc6, 0xc7, 0x36,
2194 0xdb, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0xc7, 0xa2,
2195 0xda, 0x00, 0x00, 0x00, 0x07, 0x33,
2196 0xcb, 0xa5,
2197 0xd1, 0x31,
2198 0xd1, 0xd1, 0xd1, 0x6c,
2199 0x4f, 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74, 0x69, 0x6f, 0x6e,
2200 0xc9, 0x30,
2201 0xd9, 0x03, 0x05, 0x63,
2202 0x53, 0x53, 0x47,
2203 0x2e,
Laurence Lundblade59289e52019-12-30 13:44:37 -08002204 0xd0, 0xd1, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf, 0x69,
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002205 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73, 0x69, 0x6f, 0x6e,
2206 0xd1, 0x2f,
2207 0xd1, 0x69,
2208 0x53, 0x61, 0x6e, 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f,
2209 0xd1, 0x2d,
2210 0xd1, 0x62,
2211 0x55, 0x53,
2212 0xd7, 0x32,
2213 0xd3, 0xa2,
2214 0x2a,
2215 0xc9, 0xa1,
2216 0x28,
2217 0x26,
2218 0xda, 0x05, 0x66, 0x70, 0x89, 0xda, 0x05, 0x66, 0x70, 0x89, 0xda, 0x05, 0x66, 0x70, 0x89, 0x29,
2219 0xcc, 0x4a,
2220 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,0x07, 0x08, 0x09, 0x0a,
2221 0xd0, 0x35,
2222 0xd7, 0xa1,
2223 0xcb, 0xc8, 0xc7, 0x24,
2224 0xc8, 0x22};
2225
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002226static int32_t CheckCSRMaps(QCBORDecodeContext *pDC);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002227
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002228
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002229int32_t OptTagParseTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002230{
2231 QCBORDecodeContext DCtx;
2232 QCBORItem Item;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002233
Laurence Lundbladeee851742020-01-08 08:37:05 -08002234 QCBORDecode_Init(&DCtx,
2235 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spOptTestInput),
2236 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002237
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002238 //-------------------------
2239 // This text matches the magic number tag and the fraction tag
2240 if(QCBORDecode_GetNext(&DCtx, &Item)) {
2241 return -2;
2242 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002243 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002244 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC)) {
2245 return -3;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002246 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002247
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002248 if(QCBORDecode_GetNext(&DCtx, &Item)) {
2249 return -4;
2250 }
Laurence Lundblade59289e52019-12-30 13:44:37 -08002251
2252#ifdef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002253 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
Laurence Lundblade59289e52019-12-30 13:44:37 -08002254 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_DECIMAL_FRACTION) ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002255 Item.val.uCount != 2) {
2256 return -5;
2257 }
Laurence Lundblade59289e52019-12-30 13:44:37 -08002258#else
2259 if(Item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION) {
2260 return -6;
2261 }
2262#endif
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002263
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002264 // --------------------------------
2265 // This test decodes the very large tag, but it is not in
2266 // any list so it is ignored.
Laurence Lundbladeee851742020-01-08 08:37:05 -08002267 QCBORDecode_Init(&DCtx,
2268 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEncodedLargeTag),
2269 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002270 if(QCBORDecode_GetNext(&DCtx, &Item)) {
2271 return -6;
2272 }
2273 if(Item.uTagBits) {
2274 return -7;
2275 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002276
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002277 // ----------------------------------
Laurence Lundbladeee851742020-01-08 08:37:05 -08002278 // This test sets up a caller-config list that includes the very large
2279 // tage and then matches it.
2280 QCBORDecode_Init(&DCtx,
2281 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEncodedLargeTag),
2282 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002283 const uint64_t puList[] = {0x9192939495969798, 257};
2284 const QCBORTagListIn TL = {2, puList};
2285 QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002286
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002287 if(QCBORDecode_GetNext(&DCtx, &Item)) {
2288 return -8;
2289 }
2290 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
2291 !QCBORDecode_IsTagged(&DCtx, &Item, 0x9192939495969798) ||
2292 QCBORDecode_IsTagged(&DCtx, &Item, 257) ||
2293 QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_BIGFLOAT) ||
2294 Item.val.uCount != 0) {
2295 return -9;
2296 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002297
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002298 //------------------------
Laurence Lundbladeee851742020-01-08 08:37:05 -08002299 // Sets up a caller-configured list and look up something not in it
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002300 const uint64_t puLongList[17] = {1,2,1};
2301 const QCBORTagListIn TLLong = {17, puLongList};
Laurence Lundbladeee851742020-01-08 08:37:05 -08002302 QCBORDecode_Init(&DCtx,
2303 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEncodedLargeTag),
2304 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002305 QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TLLong);
2306 if(QCBORDecode_GetNext(&DCtx, &Item)) {
2307 return -11;
2308 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002309
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002310 // -----------------------
2311 // This tests retrievel of the full tag list
Laurence Lundbladeee851742020-01-08 08:37:05 -08002312 QCBORDecode_Init(&DCtx,
2313 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spLotsOfTags),
2314 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002315 uint64_t puTags[16];
2316 QCBORTagListOut Out = {0, 4, puTags};
2317 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2318 return -12;
2319 }
2320 if(puTags[0] != 0x9192939495969798 ||
2321 puTags[1] != 0x88 ||
Laurence Lundblade59289e52019-12-30 13:44:37 -08002322 puTags[2] != 0x06 ||
2323 puTags[3] != 0x07) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002324 return -13;
2325 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002326
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002327 // ----------------------
2328 // This text if too small of an out list
Laurence Lundbladeee851742020-01-08 08:37:05 -08002329 QCBORDecode_Init(&DCtx,
2330 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spLotsOfTags),
2331 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002332 QCBORTagListOut OutSmall = {0, 3, puTags};
2333 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &OutSmall) != QCBOR_ERR_TOO_MANY_TAGS) {
2334 return -14;
2335 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002336
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002337 // ---------------
2338 // Parse a version of the "CSR" that has had a ton of tags randomly inserted
Laurence Lundbladeee851742020-01-08 08:37:05 -08002339 QCBORDecode_Init(&DCtx,
2340 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRWithTags),
2341 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002342 int n = CheckCSRMaps(&DCtx);
2343 if(n) {
2344 return n-2000;
2345 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002346
Laurence Lundblade59289e52019-12-30 13:44:37 -08002347 Out = (QCBORTagListOut){0, 16, puTags};
Laurence Lundbladeee851742020-01-08 08:37:05 -08002348 QCBORDecode_Init(&DCtx,
2349 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRWithTags),
2350 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002351
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002352 const uint64_t puTagList[] = {773, 1, 90599561};
2353 const QCBORTagListIn TagList = {3, puTagList};
2354 QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TagList);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002355
2356
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002357 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2358 return -100;
2359 }
2360 if(Item.uDataType != QCBOR_TYPE_MAP ||
2361 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC) ||
2362 QCBORDecode_IsTagged(&DCtx, &Item, 90599561) ||
2363 QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_DATE_EPOCH) ||
2364 Item.val.uCount != 2 ||
2365 puTags[0] != CBOR_TAG_CBOR_MAGIC ||
2366 puTags[1] != CBOR_TAG_CBOR_MAGIC ||
2367 puTags[2] != CBOR_TAG_CBOR_MAGIC ||
2368 Out.uNumUsed != 3) {
2369 return -101;
2370 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002371
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002372 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2373 return -102;
2374 }
2375 if(Item.uDataType != QCBOR_TYPE_MAP ||
2376 QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC) ||
2377 QCBORDecode_IsTagged(&DCtx, &Item, 6) ||
2378 QCBORDecode_IsTagged(&DCtx, &Item, 7) || // item is tagged 7, but 7 is not configured to be recognized
2379 Item.val.uCount != 2 ||
2380 puTags[0] != 5859837686836516696 ||
2381 puTags[1] != 7 ||
2382 Out.uNumUsed != 2) {
2383 return -103;
2384 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002385
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002386 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2387 return -104;
2388 }
2389 if(Item.uDataType != QCBOR_TYPE_MAP ||
2390 Item.uTagBits ||
2391 Item.val.uCount != 5 ||
2392 puTags[0] != 0x0b ||
2393 Out.uNumUsed != 1) {
2394 return -105;
2395 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002396
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002397 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2398 return -106;
2399 }
2400 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
2401 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_COSE_MAC0) ||
2402 Item.val.string.len != 12 ||
2403 puTags[0] != CBOR_TAG_COSE_MAC0 ||
2404 puTags[1] != CBOR_TAG_COSE_MAC0 ||
2405 puTags[2] != CBOR_TAG_COSE_MAC0 ||
2406 Out.uNumUsed != 3) {
2407 return -105;
2408 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002409
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002410 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2411 return -107;
2412 }
2413 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
2414 !QCBORDecode_IsTagged(&DCtx, &Item, 773) ||
2415 Item.val.string.len != 3 ||
2416 puTags[0] != 773 ||
2417 Out.uNumUsed != 1) {
2418 return -108;
2419 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002420
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002421 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2422 return -109;
2423 }
2424 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade59289e52019-12-30 13:44:37 -08002425 !QCBORDecode_IsTagged(&DCtx, &Item, 16) ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002426 Item.val.string.len != 9 ||
Laurence Lundblade59289e52019-12-30 13:44:37 -08002427 puTags[0] != 16 ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002428 puTags[11] != 0x0f ||
2429 Out.uNumUsed != 12) {
2430 return -110;
2431 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002432
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002433 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2434 return -111;
2435 }
2436 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
2437 !QCBORDecode_IsTagged(&DCtx, &Item, 17) ||
2438 Item.val.string.len != 9 ||
2439 puTags[0] != 17 ||
2440 Out.uNumUsed != 1) {
2441 return -112;
2442 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002443
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002444 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2445 return -111;
2446 }
2447 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
2448 !QCBORDecode_IsTagged(&DCtx, &Item, 17) ||
2449 Item.val.string.len != 2 ||
2450 puTags[0] != 17 ||
2451 Out.uNumUsed != 1) {
2452 return -112;
2453 }
2454
2455 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2456 return -113;
2457 }
2458 if(Item.uDataType != QCBOR_TYPE_MAP ||
2459 QCBORDecode_IsTagged(&DCtx, &Item, 19) ||
2460 Item.val.uCount != 2 ||
2461 puTags[0] != 19 ||
2462 Out.uNumUsed != 1) {
2463 return -114;
2464 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002465
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002466 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2467 return -115;
2468 }
2469 if(Item.uDataType != QCBOR_TYPE_MAP ||
2470 QCBORDecode_IsTagged(&DCtx, &Item, 9) ||
2471 Item.uTagBits ||
2472 Item.val.uCount != 1 ||
2473 puTags[0] != 9 ||
2474 Out.uNumUsed != 1) {
2475 return -116;
2476 }
2477
2478 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2479 return -116;
2480 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002481 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002482 Item.val.int64 != -7 ||
2483 Item.uTagBits ||
2484 Out.uNumUsed != 0) {
2485 return -117;
2486 }
2487
2488 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2489 return -118;
2490 }
2491 if(Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
2492 Item.val.string.len != 10 ||
2493 Item.uTagBits ||
2494 puTags[0] != 12 ||
2495 Out.uNumUsed != 1) {
2496 return -119;
2497 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002498
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002499 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2500 return -120;
2501 }
2502 if(Item.uDataType != QCBOR_TYPE_MAP ||
2503 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_ENC_AS_B16) ||
2504 Item.val.uCount != 1 ||
2505 puTags[0] != 0x17 ||
2506 Out.uNumUsed != 1) {
2507 return -121;
2508 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002509
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002510 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2511 return -122;
2512 }
2513 if(Item.uDataType != QCBOR_TYPE_INT64 ||
2514 QCBORDecode_IsTagged(&DCtx, &Item, 8) ||
2515 Item.val.int64 != -3 ||
2516 puTags[0] != 8 ||
2517 Out.uNumUsed != 1) {
2518 return -123;
2519 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002520
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002521 if(QCBORDecode_Finish(&DCtx)) {
2522 return -124;
2523 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002524
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002525 return 0;
2526}
2527
2528
2529
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002530
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002531static uint8_t spBigNumInput[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002532 0x83,
2533 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2534 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2535 0xA4,
2536 0x63, 0x42, 0x4E, 0x2B,
2537 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2538 0x18, 0x40,
2539 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2540 0x63, 0x42, 0x4E, 0x2D,
2541 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2542 0x38, 0x3F,
2543 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
2544
2545
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002546static uint8_t spBigNum[] = {0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002547
2548
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002549int32_t BignumParseTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002550{
2551 QCBORDecodeContext DCtx;
2552 QCBORItem Item;
2553 int nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002554
Laurence Lundbladeee851742020-01-08 08:37:05 -08002555 QCBORDecode_Init(&DCtx,
2556 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNumInput),
2557 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002558
2559
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002560 //
2561 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
2562 return -1;
2563 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
2564 return -1;
2565 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002566
2567 //
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002568 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
2569 return -1;
2570 if(Item.uDataType != QCBOR_TYPE_POSBIGNUM ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002571 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002572 return -1;
2573 }
2574
2575 //
2576 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
2577 return -1;
2578 if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002579 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002580 return -1;
2581 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002582
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002583 //
2584 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
2585 return -1;
2586 if(Item.uDataType != QCBOR_TYPE_MAP) {
2587 return -1;
2588 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002589
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002590 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
2591 return -1;
2592 if(Item.uDataType != QCBOR_TYPE_POSBIGNUM ||
2593 Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002594 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002595 return -1;
2596 }
2597
2598 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
2599 return -1;
2600 if(Item.uDataType != QCBOR_TYPE_POSBIGNUM ||
2601 Item.uLabelType != QCBOR_TYPE_INT64 ||
2602 Item.label.int64 != 64 ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002603 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002604 return -1;
2605 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002606
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002607 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
2608 return -1;
2609 if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM ||
2610 Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002611 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002612 return -1;
2613 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002614
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002615 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
2616 return -1;
2617 if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM ||
2618 Item.uLabelType != QCBOR_TYPE_INT64 ||
2619 Item.label.int64 != -64 ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002620 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002621 return -1;
2622 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002623
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002624 return 0;
2625}
2626
2627
2628
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002629static int32_t CheckItemWithIntLabel(QCBORDecodeContext *pCtx,
Laurence Lundbladeee851742020-01-08 08:37:05 -08002630 uint8_t uDataType,
2631 uint8_t uNestingLevel,
2632 uint8_t uNextNest,
2633 int64_t nLabel,
2634 QCBORItem *pItem)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002635{
2636 QCBORItem Item;
2637 int nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002638
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002639 if((nCBORError = QCBORDecode_GetNext(pCtx, &Item))) return -1;
2640 if(Item.uDataType != uDataType) return -1;
2641 if(uNestingLevel > 0) {
Laurence Lundbladeee851742020-01-08 08:37:05 -08002642 if(Item.uLabelType != QCBOR_TYPE_INT64 &&
2643 Item.uLabelType != QCBOR_TYPE_UINT64) {
2644 return -1;
2645 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002646 if(Item.uLabelType == QCBOR_TYPE_INT64) {
2647 if(Item.label.int64 != nLabel) return -1;
2648 } else {
Laurence Lundblade570fab52018-10-13 18:28:27 +08002649 if(Item.label.uint64 != (uint64_t)nLabel) return -1;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002650 }
2651 }
2652 if(Item.uNestingLevel != uNestingLevel) return -1;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302653 if(Item.uNextNestLevel != uNextNest) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002654
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002655 if(pItem) {
2656 *pItem = Item;
2657 }
2658 return 0;
2659}
2660
2661
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002662// Same code checks definite and indefinite length versions of the map
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002663static int32_t CheckCSRMaps(QCBORDecodeContext *pDC)
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002664{
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302665 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 0, 1, 0, NULL)) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002666
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302667 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 1, 2, -23, NULL)) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002668
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302669 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 2, 3, -20, NULL)) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002670
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302671 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -18, NULL)) return -1;
2672 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -17, NULL)) return -1;
2673 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -15, NULL)) return -1;
2674 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -16, NULL)) return -1;
2675 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 2, -14, NULL)) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002676
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302677 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 2, 3, -19, NULL)) return -1;
2678 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 3, 4, -11, NULL)) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002679
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302680 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_INT64, 4, 3, -9, NULL)) return -1;
2681 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_BYTE_STRING, 3, 1, -10, NULL)) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002682
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302683 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 1, 2, -22, NULL)) return -1;
2684 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_INT64, 2, 0, -5, NULL)) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002685
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002686 if(QCBORDecode_Finish(pDC)) return -2;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002687
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002688 return 0;
2689}
2690
2691
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002692/*
2693// cbor.me decoded output
2694{
2695 -23: {
2696 -20: {
2697 -18: "Organization",
2698 -17: "SSG",
2699 -15: "Confusion",
2700 -16: "San Diego",
2701 -14: "US"
2702 },
2703 -19: {
2704 -11: {
2705 -9: -7
2706 },
2707 -10: '\u0001\u0002\u0003\u0004\u0005\u0006\a\b\t\n'
2708 }
2709 },
2710 -22: {
2711 -5: -3
2712 }
2713}
2714 */
2715
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002716
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002717static uint8_t spCSRInput[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002718 0xa2, 0x36, 0xa2, 0x33, 0xa5, 0x31, 0x6c, 0x4f,
2719 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74,
2720 0x69, 0x6f, 0x6e, 0x30, 0x63, 0x53, 0x53, 0x47,
2721 0x2e, 0x69, 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73,
2722 0x69, 0x6f, 0x6e, 0x2f, 0x69, 0x53, 0x61, 0x6e,
2723 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f, 0x2d, 0x62,
2724 0x55, 0x53, 0x32, 0xa2, 0x2a, 0xa1, 0x28, 0x26,
2725 0x29, 0x4a, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
2726 0x07, 0x08, 0x09, 0x0a, 0x35, 0xa1, 0x24, 0x22};
2727
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002728int32_t NestedMapTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002729{
2730 QCBORDecodeContext DCtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002731
Laurence Lundbladeee851742020-01-08 08:37:05 -08002732 QCBORDecode_Init(&DCtx,
2733 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput),
2734 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002735
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002736 return CheckCSRMaps(&DCtx);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002737}
2738
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08002739
2740
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002741int32_t StringDecoderModeFailTest()
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08002742{
2743 QCBORDecodeContext DCtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002744
Laurence Lundbladeee851742020-01-08 08:37:05 -08002745 QCBORDecode_Init(&DCtx,
2746 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput),
2747 QCBOR_DECODE_MODE_MAP_STRINGS_ONLY);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002748
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08002749 QCBORItem Item;
2750 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002751
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08002752 if(QCBORDecode_GetNext(&DCtx, &Item)) {
2753 return -1;
2754 }
2755 if(Item.uDataType != QCBOR_TYPE_MAP) {
2756 return -2;
2757 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002758
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08002759 nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
2760 if(nCBORError != QCBOR_ERR_MAP_LABEL_TYPE) {
2761 return -3;
2762 }
2763
2764 return 0;
2765}
2766
2767
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002768// Same map as above, but using indefinite lengths
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002769static uint8_t spCSRInputIndefLen[] = {
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002770 0xbf, 0x36, 0xbf, 0x33, 0xbf, 0x31, 0x6c, 0x4f,
2771 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74,
2772 0x69, 0x6f, 0x6e, 0x30, 0x63, 0x53, 0x53, 0x47,
2773 0x2e, 0x69, 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73,
2774 0x69, 0x6f, 0x6e, 0x2f, 0x69, 0x53, 0x61, 0x6e,
2775 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f, 0x2d, 0x62,
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002776 0x55, 0x53, 0xff, 0x32, 0xbf, 0x2a, 0xbf, 0x28,
2777 0x26, 0xff, 0x29, 0x4a, 0x01, 0x02, 0x03, 0x04,
2778 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0xff, 0xff,
2779 0x35, 0xbf, 0x24, 0x22, 0xff, 0xff};
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002780
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002781int32_t NestedMapTestIndefLen()
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002782{
2783 QCBORDecodeContext DCtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002784
Laurence Lundbladeee851742020-01-08 08:37:05 -08002785 QCBORDecode_Init(&DCtx,
2786 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInputIndefLen),
2787 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002788
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002789 return CheckCSRMaps(&DCtx);
2790}
2791
2792
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002793
Laurence Lundblade17ede402018-10-13 11:43:07 +08002794static UsefulBufC make_nested_indefinite_arrays(int n, UsefulBuf Storage)
2795{
2796 UsefulOutBuf UOB;
2797 UsefulOutBuf_Init(&UOB, Storage);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002798
Laurence Lundblade17ede402018-10-13 11:43:07 +08002799 int i;
2800 for(i = 0; i < n; i++) {
2801 UsefulOutBuf_AppendByte(&UOB, 0x9f);
2802 }
2803
2804 for(i = 0; i < n; i++) {
2805 UsefulOutBuf_AppendByte(&UOB, 0xff);
2806 }
2807 return UsefulOutBuf_OutUBuf(&UOB);
2808}
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002809
2810
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002811static int32_t parse_indeflen_nested(UsefulBufC Nested, int nNestLevel)
Laurence Lundblade17ede402018-10-13 11:43:07 +08002812{
2813 QCBORDecodeContext DC;
2814 QCBORDecode_Init(&DC, Nested, 0);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002815
Laurence Lundblade17ede402018-10-13 11:43:07 +08002816 int j;
2817 for(j = 0; j < nNestLevel; j++) {
2818 QCBORItem Item;
2819 int nReturn = QCBORDecode_GetNext(&DC, &Item);
2820 if(j >= QCBOR_MAX_ARRAY_NESTING) {
2821 // Should be in error
2822 if(nReturn != QCBOR_ERR_ARRAY_NESTING_TOO_DEEP) {
2823 return -4;
2824 } else {
2825 return 0; // Decoding doesn't recover after an error
2826 }
2827 } else {
2828 // Should be no error
2829 if(nReturn) {
2830 return -9; // Should not have got an error
2831 }
2832 }
2833 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
2834 return -7;
2835 }
2836 }
2837 int nReturn = QCBORDecode_Finish(&DC);
2838 if(nReturn) {
2839 return -3;
2840 }
2841 return 0;
2842}
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002843
2844
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002845int32_t IndefiniteLengthNestTest()
Laurence Lundblade17ede402018-10-13 11:43:07 +08002846{
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05302847 UsefulBuf_MAKE_STACK_UB(Storage, 50);
Laurence Lundblade17ede402018-10-13 11:43:07 +08002848 int i;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002849 for(i=1; i < QCBOR_MAX_ARRAY_NESTING+4; i++) {
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08002850 const UsefulBufC Nested = make_nested_indefinite_arrays(i, Storage);
Laurence Lundblade17ede402018-10-13 11:43:07 +08002851 int nReturn = parse_indeflen_nested(Nested, i);
2852 if(nReturn) {
2853 return nReturn;
2854 }
2855 }
2856 return 0;
2857}
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002858
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002859
Laurence Lundbladeee851742020-01-08 08:37:05 -08002860// [1, [2, 3]]
2861static const uint8_t spIndefiniteArray[] = {0x9f, 0x01, 0x82, 0x02, 0x03, 0xff};
2862// No closing break
2863static const uint8_t spIndefiniteArrayBad1[] = {0x9f};
2864// Not enough closing breaks
2865static const uint8_t spIndefiniteArrayBad2[] = {0x9f, 0x9f, 0x02, 0xff};
2866// Too many closing breaks
2867static const uint8_t spIndefiniteArrayBad3[] = {0x9f, 0x02, 0xff, 0xff};
2868// Unclosed indeflen inside def len
2869static const uint8_t spIndefiniteArrayBad4[] = {0x81, 0x9f};
2870// confused tag
2871static const uint8_t spIndefiniteArrayBad5[] = {0x9f, 0xd1, 0xff};
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002872
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002873int32_t IndefiniteLengthArrayMapTest()
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002874{
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002875 int nResult;
2876 // --- first test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002877 UsefulBufC IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArray);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002878
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002879 // Decode it and see if it is OK
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05302880 UsefulBuf_MAKE_STACK_UB(MemPool, 150);
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002881 QCBORDecodeContext DC;
2882 QCBORItem Item;
2883 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002884
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002885 QCBORDecode_SetMemPool(&DC, MemPool, false);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002886
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002887 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05302888
2889 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
2890 Item.uNestingLevel != 0 ||
2891 Item.uNextNestLevel != 1) {
2892 return -111;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002893 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002894
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002895 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05302896 if(Item.uDataType != QCBOR_TYPE_INT64 ||
2897 Item.uNestingLevel != 1 ||
2898 Item.uNextNestLevel != 1) {
2899 return -2;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002900 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002901
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002902 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05302903 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
2904 Item.uNestingLevel != 1 ||
2905 Item.uNextNestLevel != 2) {
2906 return -3;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002907 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002908
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002909 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade12b495d2018-12-17 11:15:54 -08002910 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade6de37062018-10-15 12:22:42 +05302911 Item.uNestingLevel != 2 ||
2912 Item.uNextNestLevel != 2) {
2913 return -4;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002914 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002915
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002916 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade12b495d2018-12-17 11:15:54 -08002917 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade6de37062018-10-15 12:22:42 +05302918 Item.uNestingLevel != 2 ||
2919 Item.uNextNestLevel != 0) {
2920 return -5;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002921 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002922
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002923 if(QCBORDecode_Finish(&DC)) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05302924 return -6;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002925 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002926
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002927 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002928 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad1);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002929
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002930 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002931
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002932 QCBORDecode_SetMemPool(&DC, MemPool, false);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002933
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002934 nResult = QCBORDecode_GetNext(&DC, &Item);
2935 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05302936 return -7;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002937 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002938
Laurence Lundblade570fab52018-10-13 18:28:27 +08002939 nResult = QCBORDecode_Finish(&DC);
Laurence Lundblade6de37062018-10-15 12:22:42 +05302940 if(nResult != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
2941 return -8;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002942 }
2943
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002944
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002945 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002946 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad2);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002947
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002948 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002949
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002950 QCBORDecode_SetMemPool(&DC, MemPool, false);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002951
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002952 nResult = QCBORDecode_GetNext(&DC, &Item);
2953 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05302954 return -9;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002955 }
2956
2957 nResult = QCBORDecode_GetNext(&DC, &Item);
2958 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05302959 return -10;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002960 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002961
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002962 nResult = QCBORDecode_GetNext(&DC, &Item);
2963 if(nResult || Item.uDataType != QCBOR_TYPE_INT64) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05302964 return -11;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002965 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002966
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002967 nResult = QCBORDecode_Finish(&DC);
Laurence Lundblade6de37062018-10-15 12:22:42 +05302968 if(nResult != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
2969 return -12;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002970 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002971
2972
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002973 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002974 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad3);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002975
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002976 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002977
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002978 QCBORDecode_SetMemPool(&DC, MemPool, false);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002979
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002980 nResult = QCBORDecode_GetNext(&DC, &Item);
2981 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05302982 return -13;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002983 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002984
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002985 nResult = QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05302986 if(nResult != QCBOR_ERR_BAD_BREAK) {
2987 return -14;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002988 }
Laurence Lundblade6de37062018-10-15 12:22:42 +05302989
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002990
Laurence Lundblade570fab52018-10-13 18:28:27 +08002991 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002992 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad4);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002993
Laurence Lundblade570fab52018-10-13 18:28:27 +08002994 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002995
Laurence Lundblade570fab52018-10-13 18:28:27 +08002996 QCBORDecode_SetMemPool(&DC, MemPool, false);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002997
Laurence Lundblade570fab52018-10-13 18:28:27 +08002998 nResult = QCBORDecode_GetNext(&DC, &Item);
2999 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303000 return -15;
Laurence Lundblade570fab52018-10-13 18:28:27 +08003001 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003002
Laurence Lundblade570fab52018-10-13 18:28:27 +08003003 nResult = QCBORDecode_GetNext(&DC, &Item);
3004 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303005 return -16;
Laurence Lundblade570fab52018-10-13 18:28:27 +08003006 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003007
Laurence Lundblade570fab52018-10-13 18:28:27 +08003008 nResult = QCBORDecode_Finish(&DC);
Laurence Lundblade6de37062018-10-15 12:22:42 +05303009 if(nResult != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
3010 return -17;
Laurence Lundblade570fab52018-10-13 18:28:27 +08003011 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003012
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303013 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003014 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad5);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003015
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303016 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003017
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303018 QCBORDecode_SetMemPool(&DC, MemPool, false);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003019
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303020 nResult = QCBORDecode_GetNext(&DC, &Item);
3021 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303022 return -18;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303023 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003024
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303025 nResult = QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05303026 if(nResult != QCBOR_ERR_BAD_BREAK) {
3027 return -19;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303028 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003029
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003030 return 0;
3031}
3032
Laurence Lundblade17ede402018-10-13 11:43:07 +08003033
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003034static const uint8_t spIndefiniteLenString[] = {
Laurence Lundblade17ede402018-10-13 11:43:07 +08003035 0x81, // Array of length one
3036 0x7f, // text string marked with indefinite length
3037 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment
3038 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment
3039 0xff // ending break
3040};
3041
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003042static const uint8_t spIndefiniteLenStringBad2[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303043 0x81, // Array of length one
3044 0x7f, // text string marked with indefinite length
3045 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment
3046 0x44, 0x6d, 0x69, 0x6e, 0x67, // second segment of wrong type
3047 0xff // ending break
3048};
3049
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003050static const uint8_t spIndefiniteLenStringBad3[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303051 0x81, // Array of length one
3052 0x7f, // text string marked with indefinite length
3053 0x01, 0x02, // Not a string
3054 0xff // ending break
3055};
3056
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003057static const uint8_t spIndefiniteLenStringBad4[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303058 0x81, // Array of length one
3059 0x7f, // text string marked with indefinite length
3060 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment
3061 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment
3062 // missing end of string
3063};
3064
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003065static const uint8_t spIndefiniteLenStringLabel[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303066 0xa1, // Array of length one
3067 0x7f, // text string marked with indefinite length
3068 0x65, 0x73, 0x74, 0x72, 0x75, 0x75, // first segment
3069 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment
3070 0xff, // ending break
3071 0x01 // integer being labeled.
3072};
3073
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003074/**
3075 Make an indefinite length string
3076
3077 @param Storage Storage for string, must be 144 bytes in size
3078 @return The indefinite length string
3079
3080 This makes an array with one indefinite length string that has 7 chunks
3081 from size of 1 byte up to 64 bytes.
3082 */
3083static UsefulBufC MakeIndefiniteBigBstr(UsefulBuf Storage)
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303084{
3085 UsefulOutBuf UOB;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003086
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303087 UsefulOutBuf_Init(&UOB, Storage);
3088 UsefulOutBuf_AppendByte(&UOB, 0x81);
3089 UsefulOutBuf_AppendByte(&UOB, 0x5f);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003090
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003091 uint8_t uStringByte = 0;
3092 // Use of type int is intentional
3093 for(int uChunkSize = 1; uChunkSize <= 128; uChunkSize *= 2) {
3094 // Not using preferred encoding here, but that is OK.
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303095 UsefulOutBuf_AppendByte(&UOB, 0x58);
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003096 UsefulOutBuf_AppendByte(&UOB, (uint8_t)uChunkSize);
3097 for(int j = 0; j < uChunkSize; j++) {
3098 UsefulOutBuf_AppendByte(&UOB, uStringByte);
3099 uStringByte++;
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303100 }
3101 }
3102 UsefulOutBuf_AppendByte(&UOB, 0xff);
3103
3104 return UsefulOutBuf_OutUBuf(&UOB);
3105}
3106
3107static int CheckBigString(UsefulBufC BigString)
3108{
3109 if(BigString.len != 255) {
3110 return 1;
3111 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003112
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303113 for(uint8_t i = 0; i < 255; i++){
3114 if(((const uint8_t *)BigString.ptr)[i] != i) {
3115 return 1;
3116 }
3117 }
3118 return 0;
3119}
3120
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05303121
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003122int32_t IndefiniteLengthStringTest()
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303123{
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303124 QCBORDecodeContext DC;
3125 QCBORItem Item;
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05303126 // big enough for MakeIndefiniteBigBstr() + MemPool overhead
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003127 UsefulBuf_MAKE_STACK_UB(MemPool, 350);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003128
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303129 // --- Simple normal indefinite length string ------
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003130 UsefulBufC IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenString);
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303131 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003132
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05303133 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303134 return -1;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303135 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003136
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303137 if(QCBORDecode_GetNext(&DC, &Item)) {
3138 return -2;
3139 }
3140 if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.uDataAlloc) {
3141 return -3;
3142 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003143
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303144 if(QCBORDecode_GetNext(&DC, &Item)) {
3145 return -4;
3146 }
3147 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || !Item.uDataAlloc) {
3148 return -5;
3149 }
3150 if(QCBORDecode_Finish(&DC)) {
3151 return -6;
3152 }
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303153
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303154 // ----- types mismatch ---
Laurence Lundbladeee851742020-01-08 08:37:05 -08003155 QCBORDecode_Init(&DC,
3156 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad2),
3157 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003158
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303159 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
3160 return -7;
3161 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003162
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303163 if(QCBORDecode_GetNext(&DC, &Item)) {
3164 return -8;
3165 }
3166 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
3167 return -9;
3168 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003169
Laurence Lundblade30816f22018-11-10 13:40:22 +07003170 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_INDEFINITE_STRING_CHUNK) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303171 return -10;
3172 }
3173
3174 // ----- not a string ---
Laurence Lundbladeee851742020-01-08 08:37:05 -08003175 QCBORDecode_Init(&DC,
3176 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad3),
3177 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003178
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303179 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
3180 return -11;
3181 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003182
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303183 if(QCBORDecode_GetNext(&DC, &Item)) {
3184 return -12;
3185 }
3186 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
3187 return -13;
3188 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003189
Laurence Lundblade30816f22018-11-10 13:40:22 +07003190 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_INDEFINITE_STRING_CHUNK) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303191 return -14;
3192 }
3193
3194 // ----- no end -----
Laurence Lundbladeee851742020-01-08 08:37:05 -08003195 QCBORDecode_Init(&DC,
3196 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad4),
3197 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003198
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303199 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
3200 return -15;
3201 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003202
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303203 if(QCBORDecode_GetNext(&DC, &Item)) {
3204 return -16;
3205 }
3206 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
3207 return -17;
3208 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003209
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303210 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_HIT_END) {
3211 return -18;
3212 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003213
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303214 // ------ Don't set a string allocator and see an error -----
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303215 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003216
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303217 QCBORDecode_GetNext(&DC, &Item);
3218 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303219 return -19;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303220 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003221
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303222 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_NO_STRING_ALLOCATOR) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303223 return -20;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303224 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003225
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303226 // ----- Mempool is way too small -----
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003227 UsefulBuf_MAKE_STACK_UB(MemPoolTooSmall, QCBOR_DECODE_MIN_MEM_POOL_SIZE-1);
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303228
3229 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
3230 if(!QCBORDecode_SetMemPool(&DC, MemPoolTooSmall, false)) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303231 return -21;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303232 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003233
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303234 // ----- Mempool is way too small -----
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05303235 UsefulBuf_MAKE_STACK_UB(BigIndefBStrStorage, 290);
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08003236 const UsefulBufC BigIndefBStr = MakeIndefiniteBigBstr(BigIndefBStrStorage);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003237
Laurence Lundbladeee851742020-01-08 08:37:05 -08003238 // 80 is big enough for MemPool overhead, but not BigIndefBStr
3239 UsefulBuf_MAKE_STACK_UB(MemPoolSmall, 80);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003240
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303241 QCBORDecode_Init(&DC, BigIndefBStr, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303242 if(QCBORDecode_SetMemPool(&DC, MemPoolSmall, false)) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303243 return -22;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303244 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003245
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303246 QCBORDecode_GetNext(&DC, &Item);
3247 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303248 return -23;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303249 }
Laurence Lundblade30816f22018-11-10 13:40:22 +07003250 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_STRING_ALLOCATE) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303251 return -24;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303252 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003253
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303254 // ---- big bstr -----
3255 QCBORDecode_Init(&DC, BigIndefBStr, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003256
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303257 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
3258 return -25;
3259 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003260
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05303261 if(QCBORDecode_GetNext(&DC, &Item)) {
3262 return -26;
3263 }
3264 if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.uDataAlloc) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303265 return -26;
3266 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003267
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303268 if(QCBORDecode_GetNext(&DC, &Item)) {
3269 return -27;
3270 }
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05303271 if(Item.uDataType != QCBOR_TYPE_BYTE_STRING || !Item.uDataAlloc || Item.uNestingLevel != 1) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303272 return -28;
3273 }
3274 if(CheckBigString(Item.val.string)) {
3275 return -3;
3276 }
3277 if(QCBORDecode_Finish(&DC)) {
3278 return -29;
3279 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003280
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303281 // --- label is an indefinite length string ------
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003282 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringLabel), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003283
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303284 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
3285 return -30;
3286 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003287
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303288 QCBORDecode_GetNext(&DC, &Item);
3289 if(Item.uDataType != QCBOR_TYPE_MAP) {
3290 return -31;
3291 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003292
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303293 if(QCBORDecode_GetNext(&DC, &Item)){
3294 return -32;
3295 }
Laurence Lundbladeee851742020-01-08 08:37:05 -08003296 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
3297 Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303298 Item.uDataAlloc || !Item.uLabelAlloc ||
3299 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("struuming"))) {
3300 return -33;
3301 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003302
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303303 if(QCBORDecode_Finish(&DC)) {
3304 return -34;
3305 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003306
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003307 return 0;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003308}
3309
3310
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003311int32_t AllocAllStringsTest()
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303312{
3313 QCBORDecodeContext DC;
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003314 QCBORError nCBORError;
3315
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003316
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303317 // First test, use the "CSRMap" as easy input and checking
Laurence Lundbladeee851742020-01-08 08:37:05 -08003318 QCBORDecode_Init(&DC,
3319 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput),
3320 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003321
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003322 UsefulBuf_MAKE_STACK_UB(Pool, sizeof(spCSRInput) + QCBOR_DECODE_MIN_MEM_POOL_SIZE);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003323
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003324 nCBORError = QCBORDecode_SetMemPool(&DC, Pool, 1); // Turn on copying.
3325 if(nCBORError) {
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303326 return -1;
3327 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003328
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003329 if(CheckCSRMaps(&DC)) {
3330 return -2;
3331 }
3332
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303333 // Next parse, save pointers to a few strings, destroy original and see all is OK.
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003334 UsefulBuf_MAKE_STACK_UB(CopyOfStorage, sizeof(pValidMapEncoded) + QCBOR_DECODE_MIN_MEM_POOL_SIZE);
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08003335 const UsefulBufC CopyOf = UsefulBuf_Copy(CopyOfStorage, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded));
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003336
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303337 QCBORDecode_Init(&DC, CopyOf, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08003338 UsefulBuf_Set(Pool, '/');
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303339 QCBORDecode_SetMemPool(&DC, Pool, 1); // Turn on copying.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003340
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303341 QCBORItem Item1, Item2, Item3, Item4;
3342 if((nCBORError = QCBORDecode_GetNext(&DC, &Item1)))
3343 return nCBORError;
3344 if(Item1.uDataType != QCBOR_TYPE_MAP ||
3345 Item1.val.uCount != 3)
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003346 return -3;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303347 if((nCBORError = QCBORDecode_GetNext(&DC, &Item1)))
3348 return nCBORError;
3349 if((nCBORError = QCBORDecode_GetNext(&DC, &Item2)))
3350 return nCBORError;
3351 if((nCBORError = QCBORDecode_GetNext(&DC, &Item3)))
3352 return nCBORError;
3353 if((nCBORError = QCBORDecode_GetNext(&DC, &Item4)))
3354 return nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003355
Laurence Lundblade05ec57b2018-10-21 01:50:03 +05303356 UsefulBuf_Set(CopyOfStorage, '_');
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003357
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303358 if(Item1.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303359 Item1.uDataType != QCBOR_TYPE_INT64 ||
3360 Item1.val.int64 != 42 ||
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003361 Item1.uDataAlloc != 0 ||
3362 Item1.uLabelAlloc == 0 ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09003363 UsefulBuf_Compare(Item1.label.string, UsefulBuf_FromSZ("first integer"))) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003364 return -4;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09003365 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003366
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303367
3368 if(Item2.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09003369 UsefulBuf_Compare(Item2.label.string, UsefulBuf_FromSZ("an array of two strings")) ||
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303370 Item2.uDataType != QCBOR_TYPE_ARRAY ||
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003371 Item2.uDataAlloc != 0 ||
3372 Item2.uLabelAlloc == 0 ||
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303373 Item2.val.uCount != 2)
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003374 return -5;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003375
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303376 if(Item3.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003377 Item3.uDataAlloc == 0 ||
3378 Item3.uLabelAlloc != 0 ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09003379 UsefulBuf_Compare(Item3.val.string, UsefulBuf_FromSZ("string1"))) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003380 return -6;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09003381 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003382
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303383 if(Item4.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003384 Item4.uDataAlloc == 0 ||
3385 Item4.uLabelAlloc != 0 ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09003386 UsefulBuf_Compare(Item4.val.string, UsefulBuf_FromSZ("string2"))) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003387 return -7;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09003388 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003389
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303390 // Next parse with a pool that is too small
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003391 UsefulBuf_MAKE_STACK_UB(SmallPool, QCBOR_DECODE_MIN_MEM_POOL_SIZE + 1);
Laurence Lundbladeee851742020-01-08 08:37:05 -08003392 QCBORDecode_Init(&DC,
3393 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded),
3394 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303395 QCBORDecode_SetMemPool(&DC, SmallPool, 1); // Turn on copying.
3396 if((nCBORError = QCBORDecode_GetNext(&DC, &Item1)))
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003397 return -8;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303398 if(Item1.uDataType != QCBOR_TYPE_MAP ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09003399 Item1.val.uCount != 3) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003400 return -9;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09003401 }
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303402 if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item1))){
3403 if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item2))) {
3404 if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item3))) {
3405 nCBORError = QCBORDecode_GetNext(&DC, &Item4);
3406 }
3407 }
3408 }
Laurence Lundblade30816f22018-11-10 13:40:22 +07003409 if(nCBORError != QCBOR_ERR_STRING_ALLOCATE) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003410 return -10;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303411 }
3412
3413 return 0;
3414}
3415
Laurence Lundbladef6531662018-12-04 10:42:22 +09003416
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303417
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003418int32_t MemPoolTest(void)
Laurence Lundblade0155b622018-10-12 20:04:37 +08003419{
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003420 // Set up the decoder with a tiny bit of CBOR to parse because
3421 // nothing can be done with it unless that is set up.
Laurence Lundbladef6531662018-12-04 10:42:22 +09003422 QCBORDecodeContext DC;
3423 const uint8_t pMinimalCBOR[] = {0xa0}; // One empty map
3424 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pMinimalCBOR),0);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003425
Laurence Lundbladef6531662018-12-04 10:42:22 +09003426 // Set up an memory pool of 100 bytes
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003427 // Then fish into the internals of the decode context
3428 // to get the allocator function so it can be called directly.
3429 // Also figure out how much pool is available for use
3430 // buy subtracting out the overhead.
Laurence Lundbladef6531662018-12-04 10:42:22 +09003431 UsefulBuf_MAKE_STACK_UB(Pool, 100);
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003432 QCBORError nError = QCBORDecode_SetMemPool(&DC, Pool, 0);
3433 if(nError) {
3434 return -9;
3435 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003436 QCBORStringAllocate pAlloc = DC.StringAllocator.pfAllocator;
3437 void *pAllocCtx = DC.StringAllocator.pAllocateCxt;
3438 size_t uAvailPool = Pool.len - QCBOR_DECODE_MIN_MEM_POOL_SIZE;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003439
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003440 // First test -- ask for one more byte than available and see failure
3441 UsefulBuf Allocated = (*pAlloc)(pAllocCtx, NULL, uAvailPool+1);
Laurence Lundbladef6531662018-12-04 10:42:22 +09003442 if(!UsefulBuf_IsNULL(Allocated)) {
3443 return -1;
3444 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003445
Laurence Lundbladef6531662018-12-04 10:42:22 +09003446 // Re do the set up for the next test that will do a successful alloc,
3447 // a fail, a free and then success
Laurence Lundbladef6531662018-12-04 10:42:22 +09003448 QCBORDecode_SetMemPool(&DC, Pool, 0);
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003449 pAlloc = DC.StringAllocator.pfAllocator;
3450 pAllocCtx = DC.StringAllocator.pAllocateCxt;
3451 uAvailPool = Pool.len - QCBOR_DECODE_MIN_MEM_POOL_SIZE;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003452
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003453 // Allocate one byte less than available and see success
3454 Allocated = (pAlloc)(pAllocCtx, NULL, uAvailPool-1);
Laurence Lundbladef6531662018-12-04 10:42:22 +09003455 if(UsefulBuf_IsNULL(Allocated)) { // expected to succeed
3456 return -2;
3457 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003458 // Ask for some more and see failure
3459 UsefulBuf Allocated2 = (*pAlloc)(pAllocCtx, NULL, uAvailPool/2);
Laurence Lundbladef6531662018-12-04 10:42:22 +09003460 if(!UsefulBuf_IsNULL(Allocated2)) { // expected to fail
3461 return -3;
3462 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003463 // Free the first allocate, retry the second and see success
3464 (*pAlloc)(pAllocCtx, Allocated.ptr, 0); // Free
3465 Allocated = (*pAlloc)(pAllocCtx, NULL, uAvailPool/2);
Laurence Lundbladef6531662018-12-04 10:42:22 +09003466 if(UsefulBuf_IsNULL(Allocated)) { // succeed because of the free
3467 return -4;
3468 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003469
Laurence Lundbladef6531662018-12-04 10:42:22 +09003470 // Re do set up for next test that involves a successful alloc,
3471 // and a successful realloc and a failed realloc
3472 QCBORDecode_SetMemPool(&DC, Pool, 0);
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003473 pAlloc = DC.StringAllocator.pfAllocator;
3474 pAllocCtx = DC.StringAllocator.pAllocateCxt;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003475
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003476 // Allocate half the pool and see success
3477 Allocated = (*pAlloc)(pAllocCtx, NULL, uAvailPool/2);
Laurence Lundbladef6531662018-12-04 10:42:22 +09003478 if(UsefulBuf_IsNULL(Allocated)) { // expected to succeed
3479 return -5;
3480 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003481 // Reallocate to take up the whole pool and see success
3482 Allocated2 = (*pAlloc)(pAllocCtx, Allocated.ptr, uAvailPool);
Laurence Lundbladef6531662018-12-04 10:42:22 +09003483 if(UsefulBuf_IsNULL(Allocated2)) {
3484 return -6;
3485 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003486 // Make sure its the same pointer and the size is right
Laurence Lundbladef6531662018-12-04 10:42:22 +09003487 if(Allocated2.ptr != Allocated.ptr || Allocated2.len != uAvailPool) {
3488 return -7;
3489 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003490 // Try to allocate more to be sure there is failure after a realloc
3491 UsefulBuf Allocated3 = (*pAlloc)(pAllocCtx, Allocated.ptr, uAvailPool+1);
3492 if(!UsefulBuf_IsNULL(Allocated3)) {
Laurence Lundbladef6531662018-12-04 10:42:22 +09003493 return -8;
3494 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003495
Laurence Lundbladef6531662018-12-04 10:42:22 +09003496 return 0;
3497}
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003498
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003499
3500/* Just enough of an allocator to test configuration of one */
3501static UsefulBuf AllocateTestFunction(void *pCtx, void *pOldMem, size_t uNewSize)
3502{
3503 (void)pOldMem; // unused variable
3504
3505 if(uNewSize) {
3506 // Assumes the context pointer is the buffer and
3507 // nothing too big will ever be asked for.
3508 // This is only good for this basic test!
3509 return (UsefulBuf) {pCtx, uNewSize};
3510 } else {
3511 return NULLUsefulBuf;
3512 }
3513}
3514
3515
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003516int32_t SetUpAllocatorTest(void)
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003517{
3518 // Set up the decoder with a tiny bit of CBOR to parse because
3519 // nothing can be done with it unless that is set up.
3520 QCBORDecodeContext DC;
3521 const uint8_t pMinimalCBOR[] = {0x62, 0x48, 0x69}; // "Hi"
3522 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pMinimalCBOR),0);
3523
3524 uint8_t pAllocatorBuffer[50];
3525
3526 // This is really just to test that this call works.
3527 // The full functionality of string allocators is tested
3528 // elsewhere with the MemPool internal allocator.
3529 QCBORDecode_SetUpAllocator(&DC, AllocateTestFunction, pAllocatorBuffer, 1);
3530
3531 QCBORItem Item;
3532 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_SUCCESS) {
3533 return -1;
3534 }
3535
3536 if(Item.uDataAlloc == 0 ||
3537 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
3538 Item.val.string.ptr != pAllocatorBuffer) {
3539 return -2;
3540 }
3541
3542 if(QCBORDecode_Finish(&DC) != QCBOR_SUCCESS) {
3543 return -3;
3544 }
3545
3546 return 0;
3547}
3548
Laurence Lundblade59289e52019-12-30 13:44:37 -08003549
3550#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
3551/*
3552 [
3553 4([-1, 3]),
3554 4([-20, 4759477275222530853136]),
3555 4([9223372036854775807, -4759477275222530853137]),
3556 5([300, 100]),
3557 5([-20, 4759477275222530853136]),
3558 5([-9223372036854775807, -4759477275222530853137])
3559 5([9223372036854775806, -4759477275222530853137])
3560 5([9223372036854775806, 9223372036854775806])]
3561 ]
3562 */
3563
3564static const uint8_t spExpectedExponentsAndMantissas[] = {
3565 0x87,
3566 0xC4, 0x82, 0x20,
3567 0x03,
3568 0xC4, 0x82, 0x33,
3569 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,
3570 0xC4, 0x82, 0x1B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
3571 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,
3572 0xC5, 0x82, 0x19, 0x01, 0x2C,
3573 0x18, 0x64,
3574 0xC5, 0x82, 0x33,
3575 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,
3576 0xC5, 0x82, 0x3B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE,
3577 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,
3578 0xC5, 0x82, 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE,
3579 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE
3580};
3581
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003582int32_t ExponentAndMantissaDecodeTests(void)
Laurence Lundblade59289e52019-12-30 13:44:37 -08003583{
3584 QCBORDecodeContext DC;
3585 QCBORError nCBORError;
3586 QCBORItem item;
3587
Laurence Lundblade17af4902020-01-07 19:11:55 -08003588 static const uint8_t spBigNumMantissa[] = {0x01, 0x02, 0x03, 0x04, 0x05,
3589 0x06, 0x07, 0x08, 0x09, 0x010};
3590 UsefulBufC BN = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNumMantissa);
Laurence Lundblade59289e52019-12-30 13:44:37 -08003591
3592
3593 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedExponentsAndMantissas), QCBOR_DECODE_MODE_NORMAL);
3594
3595 nCBORError = QCBORDecode_GetNext(&DC, &item);
3596 if(nCBORError != QCBOR_SUCCESS) {
3597 return 1;
3598 }
3599
3600 if(item.uDataType != QCBOR_TYPE_ARRAY) {
3601 return 2;
3602 }
3603
3604 nCBORError = QCBORDecode_GetNext(&DC, &item);
3605 if(nCBORError != QCBOR_SUCCESS) {
3606 return 3;
3607 }
3608
3609 if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION ||
3610 item.val.expAndMantissa.Mantissa.nInt != 3 ||
3611 item.val.expAndMantissa.nExponent != -1) {
3612 return 4;
3613 }
3614
3615 nCBORError = QCBORDecode_GetNext(&DC, &item);
3616 if(nCBORError != QCBOR_SUCCESS) {
3617 return 5;
3618 }
3619
3620 if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM ||
3621 item.val.expAndMantissa.nExponent != -20 ||
3622 UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) {
3623 return 6;
3624 }
3625
3626 nCBORError = QCBORDecode_GetNext(&DC, &item);
3627 if(nCBORError != QCBOR_SUCCESS) {
3628 return 7;
3629 }
3630
3631 if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM ||
3632 item.val.expAndMantissa.nExponent != 9223372036854775807 ||
3633 UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) {
3634 return 8;
3635 }
3636
3637 nCBORError = QCBORDecode_GetNext(&DC, &item);
3638 if(nCBORError != QCBOR_SUCCESS) {
3639 return 9;
3640 }
3641
3642 if(item.uDataType != QCBOR_TYPE_BIGFLOAT ||
3643 item.val.expAndMantissa.Mantissa.nInt != 100 ||
3644 item.val.expAndMantissa.nExponent != 300) {
3645 return 10;
3646 }
3647
3648 nCBORError = QCBORDecode_GetNext(&DC, &item);
3649 if(nCBORError != QCBOR_SUCCESS) {
3650 return 11;
3651 }
3652
3653 if(item.uDataType != QCBOR_TYPE_BIGFLOAT_POS_BIGNUM ||
3654 item.val.expAndMantissa.nExponent != -20 ||
3655 UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) {
3656 return 12;
3657 }
3658
3659 nCBORError = QCBORDecode_GetNext(&DC, &item);
3660 if(nCBORError != QCBOR_SUCCESS) {
3661 return 13;
3662 }
3663
3664 if(item.uDataType != QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM ||
3665 item.val.expAndMantissa.nExponent != -9223372036854775807 ||
3666 UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) {
3667 return 14;
3668 }
3669
3670 nCBORError = QCBORDecode_GetNext(&DC, &item);
3671 if(nCBORError != QCBOR_SUCCESS) {
3672 return 15;
3673 }
3674
3675 if(item.uDataType != QCBOR_TYPE_BIGFLOAT ||
3676 item.val.expAndMantissa.nExponent != 9223372036854775806 ||
3677 item.val.expAndMantissa.Mantissa.nInt!= 9223372036854775806 ) {
3678 return 16;
3679 }
3680
3681 /* Now encode some stuff and then decode it */
3682 uint8_t pBuf[40];
3683 QCBOREncodeContext EC;
3684 UsefulBufC Encoded;
3685
3686 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(pBuf));
3687 QCBOREncode_OpenArray(&EC);
3688 QCBOREncode_AddDecimalFraction(&EC, 999, 1000); // 999 * (10 ^ 1000)
3689 QCBOREncode_AddBigFloat(&EC, 100, INT32_MIN);
3690 QCBOREncode_AddDecimalFractionBigNum(&EC, BN, false, INT32_MAX);
3691 QCBOREncode_CloseArray(&EC);
3692 QCBOREncode_Finish(&EC, &Encoded);
3693
3694
3695 QCBORDecode_Init(&DC, Encoded, QCBOR_DECODE_MODE_NORMAL);
3696 nCBORError = QCBORDecode_GetNext(&DC, &item);
3697 if(nCBORError != QCBOR_SUCCESS) {
3698 return 13;
3699 }
3700
3701 nCBORError = QCBORDecode_GetNext(&DC, &item);
3702 if(nCBORError != QCBOR_SUCCESS) {
3703 return 13;
3704 }
3705
3706 if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION ||
3707 item.val.expAndMantissa.nExponent != 1000 ||
3708 item.val.expAndMantissa.Mantissa.nInt != 999) {
3709 return 15;
3710 }
3711
3712 nCBORError = QCBORDecode_GetNext(&DC, &item);
3713 if(nCBORError != QCBOR_SUCCESS) {
3714 return 13;
3715 }
3716
3717 if(item.uDataType != QCBOR_TYPE_BIGFLOAT ||
3718 item.val.expAndMantissa.nExponent != INT32_MIN ||
3719 item.val.expAndMantissa.Mantissa.nInt != 100) {
3720 return 15;
3721 }
3722
3723 nCBORError = QCBORDecode_GetNext(&DC, &item);
3724 if(nCBORError != QCBOR_SUCCESS) {
3725 return 13;
3726 }
3727
3728 if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM ||
3729 item.val.expAndMantissa.nExponent != INT32_MAX ||
3730 UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) {
3731 return 12;
3732 }
3733
3734 return 0;
3735}
3736
3737
3738static struct FailInput ExponentAndMantissaFailures[] = {
3739 // Exponent > INT64_MAX
3740 { {(uint8_t[]){0xC4, 0x82, 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
3741 0xFF, 0xFF, 0x1B, 0x80, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
3742 0xFF, 0xFF,}, 20}, QCBOR_ERR_BAD_EXP_AND_MANTISSA},
3743 // Mantissa > INT64_MAX
3744 { {(uint8_t[]){0xC4, 0x82, 0x1B, 0x80, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
3745 0xFF, 0xFF, 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05,
3746 0x06, 0x07, 0x08, 0x09, 0x10}, 23}, QCBOR_ERR_BAD_EXP_AND_MANTISSA},
3747 // End of input
3748 { {(uint8_t[]){0xC4, 0x82}, 2}, QCBOR_ERR_HIT_END},
3749 // End of input
3750 { {(uint8_t[]){0xC4, 0x82, 0x01}, 3}, QCBOR_ERR_HIT_END},
3751 // bad content for big num
3752 { {(uint8_t[]){0xC4, 0x82, 0x01, 0xc3, 0x01}, 5}, QCBOR_ERR_BAD_OPT_TAG},
3753 // bad content for big num
3754 { {(uint8_t[]){0xC4, 0x82, 0xc2, 0x01, 0x1f}, 5}, QCBOR_ERR_BAD_INT},
3755 // Bad integer for exponent
3756 { {(uint8_t[]){0xC4, 0x82, 0x01, 0x1f}, 4}, QCBOR_ERR_BAD_INT},
3757 // Bad integer for mantissa
3758 { {(uint8_t[]){0xC4, 0x82, 0x1f, 0x01}, 4}, QCBOR_ERR_BAD_INT},
3759 // 3 items in array
3760 { {(uint8_t[]){0xC4, 0x83, 0x03, 0x01, 02}, 5}, QCBOR_ERR_BAD_EXP_AND_MANTISSA},
3761 // unterminated indefinite length array
3762 { {(uint8_t[]){0xC4, 0x9f, 0x03, 0x01, 0x02}, 5}, QCBOR_ERR_BAD_EXP_AND_MANTISSA},
3763 // Empty array
3764 { {(uint8_t[]){0xC4, 0x80}, 2}, QCBOR_ERR_NO_MORE_ITEMS},
3765 // Second is not an integer
3766 { {(uint8_t[]){0xC4, 0x82, 0x03, 0x40}, 4}, QCBOR_ERR_BAD_EXP_AND_MANTISSA},
3767 // First is not an integer
3768 { {(uint8_t[]){0xC4, 0x82, 0x40}, 3}, QCBOR_ERR_BAD_EXP_AND_MANTISSA},
3769 // Not an array
3770 { {(uint8_t[]){0xC4, 0xa2}, 2}, QCBOR_ERR_BAD_EXP_AND_MANTISSA}
3771};
3772
3773
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003774int32_t ExponentAndMantissaDecodeFailTests()
Laurence Lundblade59289e52019-12-30 13:44:37 -08003775{
3776 return ProcessFailures(ExponentAndMantissaFailures,
3777 sizeof(ExponentAndMantissaFailures)/sizeof(struct FailInput));
3778}
3779
3780#endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */