blob: 1009e168b60d347d7f971723f81c5e4bc5a295f7 [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 Lundbladebb1062e2019-08-12 23:28:54 -0700838 for(int nNum = sizeof(spExpectedEncodedInts)-1; nNum; nNum--) {
839 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 Lundbladebb1062e2019-08-12 23:28:54 -0700845 const QCBORError 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) {
1618 // return index of CBOR + 1000
1619 return (int)(pF - pFailInputs) * 100 + nCBORError;
1620 }
1621 }
1622
1623 return 0;
1624}
1625
1626
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001627struct FailInput Failures[] = {
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001628 // Most of this is copied from not_well_formed.h. Here the error code
1629 // returned is also checked.
1630
1631 // Indefinite length strings must be closed off
1632 // An indefinite length byte string not closed off
1633 { {(uint8_t[]){0x5f, 0x41, 0x00}, 3}, QCBOR_ERR_HIT_END },
1634 // An indefinite length text string not closed off
1635 { {(uint8_t[]){0x7f, 0x61, 0x00}, 3}, QCBOR_ERR_HIT_END },
1636
1637
1638 // All the chunks in an indefinite length string must be of the type of indefinite length string
1639 // indefinite length byte string with text string chunk
1640 { {(uint8_t[]){0x5f, 0x61, 0x00, 0xff}, 4}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1641 // indefinite length text string with a byte string chunk
1642 { {(uint8_t[]){0x7f, 0x41, 0x00, 0xff}, 4}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1643 // indefinite length byte string with an positive integer chunk
1644 { {(uint8_t[]){0x5f, 0x00, 0xff}, 3}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1645 // indefinite length byte string with an negative integer chunk
1646 { {(uint8_t[]){0x5f, 0x21, 0xff}, 3}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1647 // indefinite length byte string with an array chunk
1648 { {(uint8_t[]){0x5f, 0x80, 0xff}, 3}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1649 // indefinite length byte string with an map chunk
1650 { {(uint8_t[]){0x5f, 0xa0, 0xff}, 3}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1651 // indefinite length byte string with tagged integer chunk
1652 { {(uint8_t[]){0x5f, 0xc0, 0x00, 0xff}, 4}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1653 // indefinite length byte string with an simple type chunk
1654 { {(uint8_t[]){0x5f, 0xe0, 0xff}, 3}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1655 { {(uint8_t[]){0x5f, 0x5f, 0x41, 0x00, 0xff, 0xff}, 6}, QCBOR_ERR_INDEFINITE_STRING_CHUNK},
1656 // indefinite length text string with indefinite string inside
1657 { {(uint8_t[]){0x7f, 0x7f, 0x61, 0x00, 0xff, 0xff}, 6}, QCBOR_ERR_INDEFINITE_STRING_CHUNK},
1658
1659
1660 // Definte length maps and arrays must be closed by having the right number of items
1661 // A definte length array that is supposed to have 1 item, but has none
1662 { {(uint8_t[]){0x81}, 1}, QCBOR_ERR_HIT_END },
1663 // A definte length array that is supposed to have 2 items, but has only 1
1664 { {(uint8_t[]){0x82, 0x00}, 2}, QCBOR_ERR_HIT_END },
1665 // A definte length array that is supposed to have 511 items, but has only 1
1666 { {(uint8_t[]){0x9a, 0x01, 0xff, 0x00}, 4}, QCBOR_ERR_HIT_END },
1667 // A definte length map that is supposed to have 1 item, but has none
1668 { {(uint8_t[]){0xa1}, 1}, QCBOR_ERR_HIT_END },
1669 // A definte length map that is supposed to have s item, but has only 1
1670 { {(uint8_t[]){0xa2, 0x01, 0x02}, 3}, QCBOR_ERR_HIT_END },
1671
1672
1673 // Indefinte length maps and arrays must be ended by a break
1674 // Indefinite length array with zero items and no break
1675 { {(uint8_t[]){0x9f}, 1}, QCBOR_ERR_HIT_END },
1676 // Indefinite length array with two items and no break
1677 { {(uint8_t[]){0x9f, 0x01, 0x02}, 3}, QCBOR_ERR_HIT_END },
1678 // Indefinite length map with zero items and no break
1679 { {(uint8_t[]){0xbf}, 1}, QCBOR_ERR_HIT_END },
1680 // Indefinite length map with two items and no break
1681 { {(uint8_t[]){0xbf, 0x01, 0x02, 0x01, 0x02}, 5}, QCBOR_ERR_HIT_END },
1682
1683
1684 // Nested maps and arrays must be closed off (some extra nested test vectors)
1685 // Unclosed indefinite array containing a close definite array
1686 { {(uint8_t[]){0x9f, 0x80, 0x00}, 3}, QCBOR_ERR_HIT_END },
1687 // Definite length array containing an unclosed indefinite array
1688 { {(uint8_t[]){0x81, 0x9f}, 2}, QCBOR_ERR_HIT_END },
1689 // Deeply nested definite length arrays with deepest one unclosed
1690 { {(uint8_t[]){0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81}, 9}, QCBOR_ERR_HIT_END },
1691 // Deeply nested indefinite length arrays with deepest one unclosed
1692 { {(uint8_t[]){0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0xff, 0xff, 0xff, 0xff}, 9}, QCBOR_ERR_HIT_END },
1693 // Mixed nesting with indefinite unclosed
Laurence Lundbladeee851742020-01-08 08:37:05 -08001694 // TODO: think through this one
1695 { {(uint8_t[]){0x9f, 0x81, 0x9f, 0x81, 0x9f, 0x9f, 0xff, 0xff, 0xff}, 9}, QCBOR_ERR_BAD_BREAK },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001696 // Mixed nesting with definite unclosed
Laurence Lundbladeee851742020-01-08 08:37:05 -08001697 // TODO: think through this one
1698 { {(uint8_t[]){0x9f, 0x82, 0x9f, 0x81, 0x9f, 0x9f, 0xff, 0xff, 0xff, 0xff}, 10}, QCBOR_ERR_BAD_BREAK },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001699
1700
1701 // The "argument" for the data item is incomplete
1702 // Positive integer missing 1 byte argument
1703 { {(uint8_t[]){0x18}, 1}, QCBOR_ERR_HIT_END },
1704 // Positive integer missing 2 byte argument
1705 { {(uint8_t[]){0x19}, 1}, QCBOR_ERR_HIT_END },
1706 // Positive integer missing 4 byte argument
1707 { {(uint8_t[]){0x1a}, 1}, QCBOR_ERR_HIT_END },
1708 // Positive integer missing 8 byte argument
1709 { {(uint8_t[]){0x1b}, 1}, QCBOR_ERR_HIT_END },
1710 // Positive integer missing 1 byte of 2 byte argument
1711 { {(uint8_t[]){0x19, 0x01}, 2}, QCBOR_ERR_HIT_END },
1712 // Positive integer missing 2 bytes of 4 byte argument
1713 { {(uint8_t[]){0x1a, 0x01, 0x02}, 3}, QCBOR_ERR_HIT_END },
1714 // Positive integer missing 1 bytes of 7 byte argument
1715 { {(uint8_t[]){0x1b, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07}, 8}, QCBOR_ERR_HIT_END },
1716 // Negative integer missing 1 byte argument
1717 { {(uint8_t[]){0x38}, 1}, QCBOR_ERR_HIT_END },
1718 // Binary string missing 1 byte argument
1719 { {(uint8_t[]){0x58}, 1}, QCBOR_ERR_HIT_END },
1720 // Text string missing 1 byte argument
1721 { {(uint8_t[]){0x78}, 1}, QCBOR_ERR_HIT_END },
1722 // Array missing 1 byte argument
1723 { {(uint8_t[]){0x98}, 1}, QCBOR_ERR_HIT_END },
1724 // Map missing 1 byte argument
1725 { {(uint8_t[]){0xb8}, 1}, QCBOR_ERR_HIT_END },
1726 // Tag missing 1 byte argument
1727 { {(uint8_t[]){0xd8}, 1}, QCBOR_ERR_HIT_END },
1728 // Simple missing 1 byte argument
1729 { {(uint8_t[]){0xf8}, 1}, QCBOR_ERR_HIT_END },
1730
1731
1732 // Breaks must not occur in definite length arrays and maps
1733 // Array of length 1 with sole member replaced by a break
1734 { {(uint8_t[]){0x81, 0xff}, 2}, QCBOR_ERR_BAD_BREAK },
1735 // Array of length 2 with 2nd member replaced by a break
1736 { {(uint8_t[]){0x82, 0x00, 0xff}, 3}, QCBOR_ERR_BAD_BREAK },
1737 // Map of length 1 with sole member label replaced by a break
1738 { {(uint8_t[]){0xa1, 0xff}, 2}, QCBOR_ERR_BAD_BREAK },
1739 // Map of length 1 with sole member label replaced by break
1740 // Alternate representation that some decoders handle difference
1741 { {(uint8_t[]){0xa1, 0xff, 0x00}, 3}, QCBOR_ERR_BAD_BREAK },
1742 // Array of length 1 with 2nd member value replaced by a break
1743 { {(uint8_t[]){0xa1, 0x00, 0xff}, 3}, QCBOR_ERR_BAD_BREAK },
1744 // Map of length 2 with 2nd member replaced by a break
1745 { {(uint8_t[]){0xa2, 0x00, 0x00, 0xff}, 4}, QCBOR_ERR_BAD_BREAK },
1746
1747
1748 // Breaks must not occur on their own out of an indefinite length data item
1749 // A bare break is not well formed
1750 { {(uint8_t[]){0xff}, 1}, QCBOR_ERR_BAD_BREAK },
1751 // A bare break after a zero length definite length array
1752 { {(uint8_t[]){0x80, 0xff}, 2}, QCBOR_ERR_BAD_BREAK },
1753 // A bare break after a zero length indefinite length map
1754 { {(uint8_t[]){0x9f, 0xff, 0xff}, 3}, QCBOR_ERR_BAD_BREAK },
1755
1756
1757 // Forbidden two byte encodings of simple types
1758 // Must use 0xe0 instead
1759 { {(uint8_t[]){0xf8, 0x00}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1760 // Should use 0xe1 instead
1761 { {(uint8_t[]){0xf8, 0x01}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1762 // Should use 0xe2 instead
1763 { {(uint8_t[]){0xf8, 0x02}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1764 // Should use 0xe3 instead
1765 { {(uint8_t[]){0xf8, 0x03}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1766 // Should use 0xe4 instead
1767 { {(uint8_t[]){0xf8, 0x04}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1768 // Should use 0xe5 instead
1769 { {(uint8_t[]){0xf8, 0x05}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1770 // Should use 0xe6 instead
1771 { {(uint8_t[]){0xf8, 0x06}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1772 // Should use 0xe7 instead
1773 { {(uint8_t[]){0xf8, 0x07}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1774 // Should use 0xe8 instead
1775 { {(uint8_t[]){0xf8, 0x08}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1776 // Should use 0xe9 instead
1777 { {(uint8_t[]){0xf8, 0x09}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1778 // Should use 0xea instead
1779 { {(uint8_t[]){0xf8, 0x0a}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1780 // Should use 0xeb instead
1781 { {(uint8_t[]){0xf8, 0x0b}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1782 // Should use 0xec instead
1783 { {(uint8_t[]){0xf8, 0x0c}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1784 // Should use 0xed instead
1785 { {(uint8_t[]){0xf8, 0x0d}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1786 // Should use 0xee instead
1787 { {(uint8_t[]){0xf8, 0x0e}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1788 // Should use 0xef instead
1789 { {(uint8_t[]){0xf8, 0x0f}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1790 // Should use 0xf0 instead
1791 { {(uint8_t[]){0xf8, 0x10}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1792 // Should use 0xf1 instead
1793 { {(uint8_t[]){0xf8, 0x11}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1794 // Should use 0xf2 instead
1795 { {(uint8_t[]){0xf8, 0x12}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1796 // Must use 0xf3 instead
1797 { {(uint8_t[]){0xf8, 0x13}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1798 // Must use 0xf4 instead
1799 { {(uint8_t[]){0xf8, 0x14}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1800 // Must use 0xf5 instead
1801 { {(uint8_t[]){0xf8, 0x15}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1802 // Must use 0xf6 instead
1803 { {(uint8_t[]){0xf8, 0x16}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1804 // Must use 0xf7 instead
1805 { {(uint8_t[]){0xf8, 0x17}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1806 // Must use 0xf8 instead
1807 { {(uint8_t[]){0xf8, 0x18}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1808
1809
1810 // Integers with additional info indefinite length
1811 // Positive integer with additional info indefinite length
1812 { {(uint8_t[]){0x1f}, 1}, QCBOR_ERR_BAD_INT },
1813 // Negative integer with additional info indefinite length
1814 { {(uint8_t[]){0x3f}, 1}, QCBOR_ERR_BAD_INT },
1815 // CBOR tag with "argument" an indefinite length
1816 { {(uint8_t[]){0xdf, 0x00}, 2}, QCBOR_ERR_BAD_INT },
1817 // CBOR tag with "argument" an indefinite length alternate vector
1818 { {(uint8_t[]){0xdf}, 1}, QCBOR_ERR_BAD_INT },
1819
1820
1821 // Missing bytes from a deterministic length string
1822 // A byte string is of length 1 without the 1 byte
1823 { {(uint8_t[]){0x41}, 1}, QCBOR_ERR_HIT_END },
1824 // A text string is of length 1 without the 1 byte
1825 { {(uint8_t[]){0x61}, 1}, QCBOR_ERR_HIT_END },
1826 // Byte string should have 2^32-1 bytes, but has one
1827 { {(uint8_t[]){0x5a, 0xff, 0xff, 0xff, 0xff, 0x00}, 6}, QCBOR_ERR_HIT_END },
1828 // Byte string should have 2^32-1 bytes, but has one
1829 { {(uint8_t[]){0x7a, 0xff, 0xff, 0xff, 0xff, 0x00}, 6}, QCBOR_ERR_HIT_END },
1830
1831
1832 // Use of unassigned additional information values
1833 // Major type positive integer with reserved value 28
1834 { {(uint8_t[]){0x1c}, 1}, QCBOR_ERR_UNSUPPORTED },
1835 // Major type positive integer with reserved value 29
1836 { {(uint8_t[]){0x1d}, 1}, QCBOR_ERR_UNSUPPORTED },
1837 // Major type positive integer with reserved value 30
1838 { {(uint8_t[]){0x1e}, 1}, QCBOR_ERR_UNSUPPORTED },
1839 // Major type negative integer with reserved value 28
1840 { {(uint8_t[]){0x3c}, 1}, QCBOR_ERR_UNSUPPORTED },
1841 // Major type negative integer with reserved value 29
1842 { {(uint8_t[]){0x3d}, 1}, QCBOR_ERR_UNSUPPORTED },
1843 // Major type negative integer with reserved value 30
1844 { {(uint8_t[]){0x3e}, 1}, QCBOR_ERR_UNSUPPORTED },
1845 // Major type byte string with reserved value 28 length
1846 { {(uint8_t[]){0x5c}, 1}, QCBOR_ERR_UNSUPPORTED },
1847 // Major type byte string with reserved value 29 length
1848 { {(uint8_t[]){0x5d}, 1}, QCBOR_ERR_UNSUPPORTED },
1849 // Major type byte string with reserved value 30 length
1850 { {(uint8_t[]){0x5e}, 1}, QCBOR_ERR_UNSUPPORTED },
1851 // Major type text string with reserved value 28 length
1852 { {(uint8_t[]){0x7c}, 1}, QCBOR_ERR_UNSUPPORTED },
1853 // Major type text string with reserved value 29 length
1854 { {(uint8_t[]){0x7d}, 1}, QCBOR_ERR_UNSUPPORTED },
1855 // Major type text string with reserved value 30 length
1856 { {(uint8_t[]){0x7e}, 1}, QCBOR_ERR_UNSUPPORTED },
1857 // Major type array with reserved value 28 length
1858 { {(uint8_t[]){0x9c}, 1}, QCBOR_ERR_UNSUPPORTED },
1859 // Major type array with reserved value 29 length
1860 { {(uint8_t[]){0x9d}, 1}, QCBOR_ERR_UNSUPPORTED },
1861 // Major type array with reserved value 30 length
1862 { {(uint8_t[]){0x9e}, 1}, QCBOR_ERR_UNSUPPORTED },
1863 // Major type map with reserved value 28 length
1864 { {(uint8_t[]){0xbc}, 1}, QCBOR_ERR_UNSUPPORTED },
1865 // Major type map with reserved value 29 length
1866 { {(uint8_t[]){0xbd}, 1}, QCBOR_ERR_UNSUPPORTED },
1867 // Major type map with reserved value 30 length
1868 { {(uint8_t[]){0xbe}, 1}, QCBOR_ERR_UNSUPPORTED },
1869 // Major type tag with reserved value 28 length
1870 { {(uint8_t[]){0xdc}, 1}, QCBOR_ERR_UNSUPPORTED },
1871 // Major type tag with reserved value 29 length
1872 { {(uint8_t[]){0xdd}, 1}, QCBOR_ERR_UNSUPPORTED },
1873 // Major type tag with reserved value 30 length
1874 { {(uint8_t[]){0xde}, 1}, QCBOR_ERR_UNSUPPORTED },
1875 // Major type simple with reserved value 28 length
1876 { {(uint8_t[]){0xfc}, 1}, QCBOR_ERR_UNSUPPORTED },
1877 // Major type simple with reserved value 29 length
1878 { {(uint8_t[]){0xfd}, 1}, QCBOR_ERR_UNSUPPORTED },
1879 // Major type simple with reserved value 30 length
1880 { {(uint8_t[]){0xfe}, 1}, QCBOR_ERR_UNSUPPORTED },
1881
1882
1883 // Maps must have an even number of data items (key & value)
1884 // Map with 1 item when it should have 2
1885 { {(uint8_t[]){0xa1, 0x00}, 2}, QCBOR_ERR_HIT_END },
1886 // Map with 3 item when it should have 4
1887 { {(uint8_t[]){0xa2, 0x00, 0x00, 0x00}, 2}, QCBOR_ERR_HIT_END },
1888 // Map with 1 item when it should have 2
1889 { {(uint8_t[]){0xbf, 0x00, 0xff}, 3}, QCBOR_ERR_BAD_BREAK },
1890 // Map with 3 item when it should have 4
1891 { {(uint8_t[]){0xbf, 0x00, 0x00, 0x00, 0xff}, 5}, QCBOR_ERR_BAD_BREAK },
1892
1893
1894 // In addition to not-well-formed, some invalid CBOR
Laurence Lundbladeee851742020-01-08 08:37:05 -08001895 // Text-based date, with an integer
1896 { {(uint8_t[]){0xc0, 0x00}, 2}, QCBOR_ERR_BAD_OPT_TAG },
1897 // Epoch date, with an byte string
1898 { {(uint8_t[]){0xc1, 0x41, 0x33}, 3}, QCBOR_ERR_BAD_OPT_TAG },
1899 // tagged as both epoch and string dates
1900 { {(uint8_t[]){0xc1, 0xc0, 0x00}, 3}, QCBOR_ERR_BAD_OPT_TAG },
1901 // big num tagged an int, not a byte string
1902 { {(uint8_t[]){0xc2, 0x00}, 2}, QCBOR_ERR_BAD_OPT_TAG },
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001903};
1904
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001905int32_t DecodeFailureTests()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001906{
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001907 int32_t nResult;
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001908
Laurence Lundblade59289e52019-12-30 13:44:37 -08001909 nResult = ProcessFailures(Failures, sizeof(Failures)/sizeof(struct FailInput));
1910 if(nResult) {
1911 return nResult;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001912 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001913
Laurence Lundblade3a6042e2019-06-28 19:58:04 -07001914 // Corrupt the UsefulInputBuf and see that
1915 // it reflected correctly for CBOR decoding
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001916 {
1917 QCBORDecodeContext DCtx;
Laurence Lundblade3a6042e2019-06-28 19:58:04 -07001918 QCBORItem Item;
1919 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001920
Laurence Lundblade3a6042e2019-06-28 19:58:04 -07001921 QCBORDecode_Init(&DCtx,
1922 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSimpleValues),
1923 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001924
1925 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1926 return nCBORError;
1927 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
Laurence Lundblade3a6042e2019-06-28 19:58:04 -07001928 Item.val.uCount != 10) {
1929 // This wasn't supposed to happen
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001930 return -1;
Laurence Lundblade3a6042e2019-06-28 19:58:04 -07001931 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001932
Laurence Lundblade3a6042e2019-06-28 19:58:04 -07001933 DCtx.InBuf.magic = 0; // Reach in and corrupt the UsefulInputBuf
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001934
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001935 nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundblade3a6042e2019-06-28 19:58:04 -07001936 if(nCBORError != QCBOR_ERR_HIT_END) {
1937 // Did not get back the error expected
1938 return -2;
1939 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001940 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001941
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001942/*
1943 This test is disabled until QCBOREncode_EncodeHead() is brought in so
1944 the size encoded can be tied to SIZE_MAX and work for all size CPUs.
1945
1946 This relies on the largest string allowed being SIZE_MAX -4 rather than
1947 SIZE_MAX. That way the test can be performed.
1948 {
1949 QCBORDecodeContext DCtx;
1950 QCBORItem Item;
1951
1952 static uint8_t foo[] = {0x5b, 0xff, 0xff, 0xff, 0xff,
1953 0xff, 0xff, 0xff, 0xff};
1954
1955 QCBORDecode_Init(&DCtx,
1956 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(foo),
1957 QCBOR_DECODE_MODE_NORMAL);
1958
1959 if(QCBOR_ERR_STRING_TOO_LONG != QCBORDecode_GetNext(&DCtx, &Item)) {
1960 return -4;
1961 }
1962 }
1963*/
1964
Laurence Lundblade3a6042e2019-06-28 19:58:04 -07001965 return 0;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001966}
1967
1968
Laurence Lundbladea2e29072018-12-30 09:20:06 -08001969/* Try all 256 values of the byte at nLen including recursing for
1970 each of the values to try values at nLen+1 ... up to nLenMax
1971 */
1972static void ComprehensiveInputRecurser(uint8_t *pBuf, int nLen, int nLenMax)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001973{
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001974 if(nLen >= nLenMax) {
1975 return;
1976 }
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08001977
Laurence Lundbladea2e29072018-12-30 09:20:06 -08001978 for(int inputByte = 0; inputByte < 256; inputByte++) {
1979 // Set up the input
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001980 pBuf[nLen] = (uint8_t)inputByte;
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08001981 const UsefulBufC Input = {pBuf, nLen+1};
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08001982
Laurence Lundbladea2e29072018-12-30 09:20:06 -08001983 // Get ready to parse
1984 QCBORDecodeContext DCtx;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001985 QCBORDecode_Init(&DCtx, Input, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001986
Laurence Lundbladea2e29072018-12-30 09:20:06 -08001987 // Parse by getting the next item until an error occurs
1988 // Just about every possible decoder error can occur here
1989 // The goal of this test is not to check for the correct
1990 // error since that is not really possible. It is to
1991 // see that there is no crash on hostile input.
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001992 while(1) {
Laurence Lundbladea2e29072018-12-30 09:20:06 -08001993 QCBORItem Item;
1994 QCBORError nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001995 if(nCBORError != QCBOR_SUCCESS) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001996 break;
1997 }
1998 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001999
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002000 ComprehensiveInputRecurser(pBuf, nLen+1, nLenMax);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002001 }
2002}
2003
2004
2005/*
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002006 Public function for initialization. See header qcbor.h
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002007 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002008int32_t ComprehensiveInputTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002009{
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002010 // Size 2 tests 64K inputs and runs quickly
2011 uint8_t pBuf[2];
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002012
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002013 ComprehensiveInputRecurser(pBuf, 0, sizeof(pBuf));
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002014
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002015 return 0;
2016}
2017
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002018
2019/*
2020 Public function for initialization. See header qcbor.h
2021 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002022int32_t BigComprehensiveInputTest()
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002023{
2024 // size 3 tests 16 million inputs and runs OK
2025 // in seconds on fast machines. Size 4 takes
2026 // 10+ minutes and 5 half a day on fast
2027 // machines. This test is kept separate from
2028 // the others so as to no slow down the use
2029 // of them as a very frequent regression.
2030 uint8_t pBuf[3]; //
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08002031
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002032 ComprehensiveInputRecurser(pBuf, 0, sizeof(pBuf));
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08002033
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002034 return 0;
2035}
2036
2037
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002038static uint8_t spDateTestInput[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002039 0xc0, // tag for string date
2040 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002041
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002042 0xc1, // tag for epoch date
2043 0x1a, 0x53, 0x72, 0x4E, 0x00, // Epoch date 1400000000; Tue, 13 May 2014 16:53:20 GMT
2044
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002045 // CBOR_TAG_B64
2046 0xc1, 0xcf, 0xd8, 0x22, // 0xee, // Epoch date with extra tags TODO: fix this test
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002047 0x1a, 0x53, 0x72, 0x4E, 0x01,
2048
2049 0xc1, // tag for epoch date
2050 0x1b, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, // Too large integer
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002051
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002052 0xc1, // tag for epoch date
2053 0xfa, 0x3f, 0x8c, 0xcc, 0xcd, // double with value 1.1
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002054
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002055 0xc1, // tag for epoch date
Laurence Lundbladea1ad8782019-11-08 00:12:11 -08002056 0xfa, 0x7f, 0x7f, 0xff, 0xff, // 3.4028234663852886e+38 too large
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002057
Laurence Lundbladea1ad8782019-11-08 00:12:11 -08002058 0xc1, // tag for epoch date
2059 0xfb, 0x43, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 9223372036854775808.000000 just barely too large
2060 //0xfa, 0x7f, 0x7f, 0xff, 0xff // 3.4028234663852886e+38 too large
2061
2062 0xc1, // tag for epoch date
2063 0xfb, 0x43, 0xdf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe // 9223372036854773760 largest supported
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002064};
2065
2066
2067// have to check float expected only to within an epsilon
2068int CHECK_EXPECTED_DOUBLE(double val, double expected) {
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002069
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002070 double diff = val - expected;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002071
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002072 diff = fabs(diff);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002073
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002074 return diff > 0.0000001;
2075}
2076
2077
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002078int32_t DateParseTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002079{
2080 QCBORDecodeContext DCtx;
2081 QCBORItem Item;
2082 int nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002083
Laurence Lundbladeee851742020-01-08 08:37:05 -08002084 QCBORDecode_Init(&DCtx,
2085 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spDateTestInput),
2086 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002087
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002088 const uint64_t uTags[] = {15};
2089 QCBORTagListIn TagList = {1, uTags};
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002090
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002091 QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TagList);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002092
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002093 // String date
2094 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
2095 return -1;
2096 if(Item.uDataType != QCBOR_TYPE_DATE_STRING ||
Laurence Lundblade9e3651c2018-10-10 11:49:55 +08002097 UsefulBuf_Compare(Item.val.dateString, UsefulBuf_FromSZ("1985-04-12"))){
Laurence Lundblade67bd5512018-11-02 21:44:06 +07002098 return -2;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002099 }
2100
2101 // Epoch date
2102 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade67bd5512018-11-02 21:44:06 +07002103 return -3;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002104 if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH ||
2105 Item.val.epochDate.nSeconds != 1400000000 ||
2106 Item.val.epochDate.fSecondsFraction != 0 ) {
Laurence Lundblade67bd5512018-11-02 21:44:06 +07002107 return -4;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002108 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002109
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002110 // Epoch date with extra CBOR_TAG_B64 tag that doesn't really mean anything
2111 // but want to be sure extra tag doesn't cause a problem
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002112 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade67bd5512018-11-02 21:44:06 +07002113 return -5;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002114 if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH ||
2115 Item.val.epochDate.nSeconds != 1400000001 ||
2116 Item.val.epochDate.fSecondsFraction != 0 ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002117 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_B64)) {
Laurence Lundblade67bd5512018-11-02 21:44:06 +07002118 return -6;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002119 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002120
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002121 // Epoch date that is too large for our representation
2122 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_DATE_OVERFLOW) {
Laurence Lundblade67bd5512018-11-02 21:44:06 +07002123 return -7;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002124 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002125
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002126 // Epoch date in float format with fractional seconds
2127 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade67bd5512018-11-02 21:44:06 +07002128 return -8;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002129 if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH ||
2130 Item.val.epochDate.nSeconds != 1 ||
2131 CHECK_EXPECTED_DOUBLE(Item.val.epochDate.fSecondsFraction, 0.1 )) {
Laurence Lundblade67bd5512018-11-02 21:44:06 +07002132 return -9;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002133 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002134
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002135 // Epoch date float that is too large for our representation
2136 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_DATE_OVERFLOW) {
Laurence Lundblade67bd5512018-11-02 21:44:06 +07002137 return -10;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002138 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002139
Laurence Lundbladea1ad8782019-11-08 00:12:11 -08002140 // Epoch date double that is just slightly too large
2141 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_DATE_OVERFLOW) {
2142 return -11;
2143 }
2144
2145 // Largest double epoch date supported
2146 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_SUCCESS ||
2147 Item.uDataType != QCBOR_TYPE_DATE_EPOCH ||
2148 Item.val.epochDate.nSeconds != 9223372036854773760 ||
2149 Item.val.epochDate.nSeconds == 0) {
2150 return -12;
2151 }
Laurence Lundbladeee851742020-01-08 08:37:05 -08002152 // TODO: could use a few more tests with float, double, and half precsion
2153 // and negative (but coverage is still pretty good)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002154
2155 return 0;
2156}
2157
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002158// Really simple basic input for tagging test
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002159static uint8_t spOptTestInput[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002160 0xd9, 0xd9, 0xf7, // CBOR magic number
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002161 0x81, // Array of one
2162 0xd8, 0x04, // non-preferred serialization of tag 4
2163 0x82, 0x01, 0x03}; // fraction 1/3
2164
Laurence Lundblade59289e52019-12-30 13:44:37 -08002165/*
2166 DB 9192939495969798 # tag(10489608748473423768)
2167 80 # array(0)
2168 */
Laurence Lundbladeee851742020-01-08 08:37:05 -08002169static uint8_t spEncodedLargeTag[] = {0xdb, 0x91, 0x92, 0x93, 0x94, 0x95,
2170 0x96, 0x97, 0x98, 0x80};
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002171
Laurence Lundblade59289e52019-12-30 13:44:37 -08002172/*
2173DB 9192939495969798 # tag(10489608748473423768)
2174 D8 88 # tag(136)
2175 C6 # tag(6)
2176 C7 # tag(7)
2177 80 # array(0)
2178*/
Laurence Lundbladeee851742020-01-08 08:37:05 -08002179static uint8_t spLotsOfTags[] = {0xdb, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96,
2180 0x97, 0x98, 0xd8, 0x88, 0xc6, 0xc7, 0x80};
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002181
2182/*
2183 The cbor.me parse of this.
2184 55799(55799(55799({6(7(-23)): 5859837686836516696(7({7(-20): 11({17(-18): 17(17(17("Organization"))),
Laurence Lundblade59289e52019-12-30 13:44:37 -08002185 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 +07002186 17(-14): 17("US")}), 23(-19): 19({-11: 9({-9: -7}),
2187 90599561(90599561(90599561(-10))): 12(h'0102030405060708090A')})})),
2188 16(-22): 23({11(8(7(-5))): 8(-3)})})))
2189 */
2190static uint8_t spCSRWithTags[] = {
2191 0xd9, 0xd9, 0xf7, 0xd9, 0xd9, 0xf7, 0xd9, 0xd9, 0xf7, 0xa2,
2192 0xc6, 0xc7, 0x36,
2193 0xdb, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0xc7, 0xa2,
2194 0xda, 0x00, 0x00, 0x00, 0x07, 0x33,
2195 0xcb, 0xa5,
2196 0xd1, 0x31,
2197 0xd1, 0xd1, 0xd1, 0x6c,
2198 0x4f, 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74, 0x69, 0x6f, 0x6e,
2199 0xc9, 0x30,
2200 0xd9, 0x03, 0x05, 0x63,
2201 0x53, 0x53, 0x47,
2202 0x2e,
Laurence Lundblade59289e52019-12-30 13:44:37 -08002203 0xd0, 0xd1, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf, 0x69,
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002204 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73, 0x69, 0x6f, 0x6e,
2205 0xd1, 0x2f,
2206 0xd1, 0x69,
2207 0x53, 0x61, 0x6e, 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f,
2208 0xd1, 0x2d,
2209 0xd1, 0x62,
2210 0x55, 0x53,
2211 0xd7, 0x32,
2212 0xd3, 0xa2,
2213 0x2a,
2214 0xc9, 0xa1,
2215 0x28,
2216 0x26,
2217 0xda, 0x05, 0x66, 0x70, 0x89, 0xda, 0x05, 0x66, 0x70, 0x89, 0xda, 0x05, 0x66, 0x70, 0x89, 0x29,
2218 0xcc, 0x4a,
2219 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,0x07, 0x08, 0x09, 0x0a,
2220 0xd0, 0x35,
2221 0xd7, 0xa1,
2222 0xcb, 0xc8, 0xc7, 0x24,
2223 0xc8, 0x22};
2224
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002225static int32_t CheckCSRMaps(QCBORDecodeContext *pDC);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002226
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002227
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002228int32_t OptTagParseTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002229{
2230 QCBORDecodeContext DCtx;
2231 QCBORItem Item;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002232
Laurence Lundbladeee851742020-01-08 08:37:05 -08002233 QCBORDecode_Init(&DCtx,
2234 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spOptTestInput),
2235 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002236
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002237 //-------------------------
2238 // This text matches the magic number tag and the fraction tag
2239 if(QCBORDecode_GetNext(&DCtx, &Item)) {
2240 return -2;
2241 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002242 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002243 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC)) {
2244 return -3;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002245 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002246
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002247 if(QCBORDecode_GetNext(&DCtx, &Item)) {
2248 return -4;
2249 }
Laurence Lundblade59289e52019-12-30 13:44:37 -08002250
2251#ifdef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002252 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
Laurence Lundblade59289e52019-12-30 13:44:37 -08002253 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_DECIMAL_FRACTION) ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002254 Item.val.uCount != 2) {
2255 return -5;
2256 }
Laurence Lundblade59289e52019-12-30 13:44:37 -08002257#else
2258 if(Item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION) {
2259 return -6;
2260 }
2261#endif
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002262
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002263 // --------------------------------
2264 // This test decodes the very large tag, but it is not in
2265 // any list so it is ignored.
Laurence Lundbladeee851742020-01-08 08:37:05 -08002266 QCBORDecode_Init(&DCtx,
2267 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEncodedLargeTag),
2268 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002269 if(QCBORDecode_GetNext(&DCtx, &Item)) {
2270 return -6;
2271 }
2272 if(Item.uTagBits) {
2273 return -7;
2274 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002275
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002276 // ----------------------------------
Laurence Lundbladeee851742020-01-08 08:37:05 -08002277 // This test sets up a caller-config list that includes the very large
2278 // tage and then matches it.
2279 QCBORDecode_Init(&DCtx,
2280 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEncodedLargeTag),
2281 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002282 const uint64_t puList[] = {0x9192939495969798, 257};
2283 const QCBORTagListIn TL = {2, puList};
2284 QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002285
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002286 if(QCBORDecode_GetNext(&DCtx, &Item)) {
2287 return -8;
2288 }
2289 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
2290 !QCBORDecode_IsTagged(&DCtx, &Item, 0x9192939495969798) ||
2291 QCBORDecode_IsTagged(&DCtx, &Item, 257) ||
2292 QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_BIGFLOAT) ||
2293 Item.val.uCount != 0) {
2294 return -9;
2295 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002296
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002297 //------------------------
Laurence Lundbladeee851742020-01-08 08:37:05 -08002298 // Sets up a caller-configured list and look up something not in it
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002299 const uint64_t puLongList[17] = {1,2,1};
2300 const QCBORTagListIn TLLong = {17, puLongList};
Laurence Lundbladeee851742020-01-08 08:37:05 -08002301 QCBORDecode_Init(&DCtx,
2302 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEncodedLargeTag),
2303 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002304 QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TLLong);
2305 if(QCBORDecode_GetNext(&DCtx, &Item)) {
2306 return -11;
2307 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002308
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002309 // -----------------------
2310 // This tests retrievel of the full tag list
Laurence Lundbladeee851742020-01-08 08:37:05 -08002311 QCBORDecode_Init(&DCtx,
2312 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spLotsOfTags),
2313 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002314 uint64_t puTags[16];
2315 QCBORTagListOut Out = {0, 4, puTags};
2316 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2317 return -12;
2318 }
2319 if(puTags[0] != 0x9192939495969798 ||
2320 puTags[1] != 0x88 ||
Laurence Lundblade59289e52019-12-30 13:44:37 -08002321 puTags[2] != 0x06 ||
2322 puTags[3] != 0x07) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002323 return -13;
2324 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002325
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002326 // ----------------------
2327 // This text if too small of an out list
Laurence Lundbladeee851742020-01-08 08:37:05 -08002328 QCBORDecode_Init(&DCtx,
2329 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spLotsOfTags),
2330 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002331 QCBORTagListOut OutSmall = {0, 3, puTags};
2332 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &OutSmall) != QCBOR_ERR_TOO_MANY_TAGS) {
2333 return -14;
2334 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002335
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002336 // ---------------
2337 // Parse a version of the "CSR" that has had a ton of tags randomly inserted
Laurence Lundbladeee851742020-01-08 08:37:05 -08002338 QCBORDecode_Init(&DCtx,
2339 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRWithTags),
2340 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002341 int n = CheckCSRMaps(&DCtx);
2342 if(n) {
2343 return n-2000;
2344 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002345
Laurence Lundblade59289e52019-12-30 13:44:37 -08002346 Out = (QCBORTagListOut){0, 16, puTags};
Laurence Lundbladeee851742020-01-08 08:37:05 -08002347 QCBORDecode_Init(&DCtx,
2348 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRWithTags),
2349 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002350
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002351 const uint64_t puTagList[] = {773, 1, 90599561};
2352 const QCBORTagListIn TagList = {3, puTagList};
2353 QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TagList);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002354
2355
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002356 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2357 return -100;
2358 }
2359 if(Item.uDataType != QCBOR_TYPE_MAP ||
2360 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC) ||
2361 QCBORDecode_IsTagged(&DCtx, &Item, 90599561) ||
2362 QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_DATE_EPOCH) ||
2363 Item.val.uCount != 2 ||
2364 puTags[0] != CBOR_TAG_CBOR_MAGIC ||
2365 puTags[1] != CBOR_TAG_CBOR_MAGIC ||
2366 puTags[2] != CBOR_TAG_CBOR_MAGIC ||
2367 Out.uNumUsed != 3) {
2368 return -101;
2369 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002370
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002371 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2372 return -102;
2373 }
2374 if(Item.uDataType != QCBOR_TYPE_MAP ||
2375 QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC) ||
2376 QCBORDecode_IsTagged(&DCtx, &Item, 6) ||
2377 QCBORDecode_IsTagged(&DCtx, &Item, 7) || // item is tagged 7, but 7 is not configured to be recognized
2378 Item.val.uCount != 2 ||
2379 puTags[0] != 5859837686836516696 ||
2380 puTags[1] != 7 ||
2381 Out.uNumUsed != 2) {
2382 return -103;
2383 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002384
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002385 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2386 return -104;
2387 }
2388 if(Item.uDataType != QCBOR_TYPE_MAP ||
2389 Item.uTagBits ||
2390 Item.val.uCount != 5 ||
2391 puTags[0] != 0x0b ||
2392 Out.uNumUsed != 1) {
2393 return -105;
2394 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002395
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002396 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2397 return -106;
2398 }
2399 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
2400 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_COSE_MAC0) ||
2401 Item.val.string.len != 12 ||
2402 puTags[0] != CBOR_TAG_COSE_MAC0 ||
2403 puTags[1] != CBOR_TAG_COSE_MAC0 ||
2404 puTags[2] != CBOR_TAG_COSE_MAC0 ||
2405 Out.uNumUsed != 3) {
2406 return -105;
2407 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002408
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002409 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2410 return -107;
2411 }
2412 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
2413 !QCBORDecode_IsTagged(&DCtx, &Item, 773) ||
2414 Item.val.string.len != 3 ||
2415 puTags[0] != 773 ||
2416 Out.uNumUsed != 1) {
2417 return -108;
2418 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002419
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002420 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2421 return -109;
2422 }
2423 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade59289e52019-12-30 13:44:37 -08002424 !QCBORDecode_IsTagged(&DCtx, &Item, 16) ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002425 Item.val.string.len != 9 ||
Laurence Lundblade59289e52019-12-30 13:44:37 -08002426 puTags[0] != 16 ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002427 puTags[11] != 0x0f ||
2428 Out.uNumUsed != 12) {
2429 return -110;
2430 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002431
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002432 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2433 return -111;
2434 }
2435 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
2436 !QCBORDecode_IsTagged(&DCtx, &Item, 17) ||
2437 Item.val.string.len != 9 ||
2438 puTags[0] != 17 ||
2439 Out.uNumUsed != 1) {
2440 return -112;
2441 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002442
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002443 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2444 return -111;
2445 }
2446 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
2447 !QCBORDecode_IsTagged(&DCtx, &Item, 17) ||
2448 Item.val.string.len != 2 ||
2449 puTags[0] != 17 ||
2450 Out.uNumUsed != 1) {
2451 return -112;
2452 }
2453
2454 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2455 return -113;
2456 }
2457 if(Item.uDataType != QCBOR_TYPE_MAP ||
2458 QCBORDecode_IsTagged(&DCtx, &Item, 19) ||
2459 Item.val.uCount != 2 ||
2460 puTags[0] != 19 ||
2461 Out.uNumUsed != 1) {
2462 return -114;
2463 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002464
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002465 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2466 return -115;
2467 }
2468 if(Item.uDataType != QCBOR_TYPE_MAP ||
2469 QCBORDecode_IsTagged(&DCtx, &Item, 9) ||
2470 Item.uTagBits ||
2471 Item.val.uCount != 1 ||
2472 puTags[0] != 9 ||
2473 Out.uNumUsed != 1) {
2474 return -116;
2475 }
2476
2477 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2478 return -116;
2479 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002480 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002481 Item.val.int64 != -7 ||
2482 Item.uTagBits ||
2483 Out.uNumUsed != 0) {
2484 return -117;
2485 }
2486
2487 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2488 return -118;
2489 }
2490 if(Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
2491 Item.val.string.len != 10 ||
2492 Item.uTagBits ||
2493 puTags[0] != 12 ||
2494 Out.uNumUsed != 1) {
2495 return -119;
2496 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002497
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002498 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2499 return -120;
2500 }
2501 if(Item.uDataType != QCBOR_TYPE_MAP ||
2502 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_ENC_AS_B16) ||
2503 Item.val.uCount != 1 ||
2504 puTags[0] != 0x17 ||
2505 Out.uNumUsed != 1) {
2506 return -121;
2507 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002508
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002509 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2510 return -122;
2511 }
2512 if(Item.uDataType != QCBOR_TYPE_INT64 ||
2513 QCBORDecode_IsTagged(&DCtx, &Item, 8) ||
2514 Item.val.int64 != -3 ||
2515 puTags[0] != 8 ||
2516 Out.uNumUsed != 1) {
2517 return -123;
2518 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002519
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002520 if(QCBORDecode_Finish(&DCtx)) {
2521 return -124;
2522 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002523
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002524 return 0;
2525}
2526
2527
2528
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002529
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002530static uint8_t spBigNumInput[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002531 0x83,
2532 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2533 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2534 0xA4,
2535 0x63, 0x42, 0x4E, 0x2B,
2536 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2537 0x18, 0x40,
2538 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2539 0x63, 0x42, 0x4E, 0x2D,
2540 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2541 0x38, 0x3F,
2542 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
2543
2544
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002545static uint8_t spBigNum[] = {0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002546
2547
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002548int32_t BignumParseTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002549{
2550 QCBORDecodeContext DCtx;
2551 QCBORItem Item;
2552 int nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002553
Laurence Lundbladeee851742020-01-08 08:37:05 -08002554 QCBORDecode_Init(&DCtx,
2555 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNumInput),
2556 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002557
2558
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002559 //
2560 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
2561 return -1;
2562 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
2563 return -1;
2564 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002565
2566 //
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002567 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
2568 return -1;
2569 if(Item.uDataType != QCBOR_TYPE_POSBIGNUM ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002570 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002571 return -1;
2572 }
2573
2574 //
2575 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
2576 return -1;
2577 if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002578 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002579 return -1;
2580 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002581
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002582 //
2583 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
2584 return -1;
2585 if(Item.uDataType != QCBOR_TYPE_MAP) {
2586 return -1;
2587 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002588
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002589 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
2590 return -1;
2591 if(Item.uDataType != QCBOR_TYPE_POSBIGNUM ||
2592 Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002593 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002594 return -1;
2595 }
2596
2597 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
2598 return -1;
2599 if(Item.uDataType != QCBOR_TYPE_POSBIGNUM ||
2600 Item.uLabelType != QCBOR_TYPE_INT64 ||
2601 Item.label.int64 != 64 ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002602 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002603 return -1;
2604 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002605
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002606 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
2607 return -1;
2608 if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM ||
2609 Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002610 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002611 return -1;
2612 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002613
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002614 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
2615 return -1;
2616 if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM ||
2617 Item.uLabelType != QCBOR_TYPE_INT64 ||
2618 Item.label.int64 != -64 ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002619 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002620 return -1;
2621 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002622
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002623 return 0;
2624}
2625
2626
2627
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002628static int32_t CheckItemWithIntLabel(QCBORDecodeContext *pCtx,
Laurence Lundbladeee851742020-01-08 08:37:05 -08002629 uint8_t uDataType,
2630 uint8_t uNestingLevel,
2631 uint8_t uNextNest,
2632 int64_t nLabel,
2633 QCBORItem *pItem)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002634{
2635 QCBORItem Item;
2636 int nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002637
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002638 if((nCBORError = QCBORDecode_GetNext(pCtx, &Item))) return -1;
2639 if(Item.uDataType != uDataType) return -1;
2640 if(uNestingLevel > 0) {
Laurence Lundbladeee851742020-01-08 08:37:05 -08002641 if(Item.uLabelType != QCBOR_TYPE_INT64 &&
2642 Item.uLabelType != QCBOR_TYPE_UINT64) {
2643 return -1;
2644 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002645 if(Item.uLabelType == QCBOR_TYPE_INT64) {
2646 if(Item.label.int64 != nLabel) return -1;
2647 } else {
Laurence Lundblade570fab52018-10-13 18:28:27 +08002648 if(Item.label.uint64 != (uint64_t)nLabel) return -1;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002649 }
2650 }
2651 if(Item.uNestingLevel != uNestingLevel) return -1;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302652 if(Item.uNextNestLevel != uNextNest) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002653
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002654 if(pItem) {
2655 *pItem = Item;
2656 }
2657 return 0;
2658}
2659
2660
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002661// Same code checks definite and indefinite length versions of the map
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002662static int32_t CheckCSRMaps(QCBORDecodeContext *pDC)
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002663{
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302664 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 0, 1, 0, NULL)) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002665
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302666 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 1, 2, -23, NULL)) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002667
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302668 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 2, 3, -20, NULL)) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002669
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302670 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -18, NULL)) return -1;
2671 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -17, NULL)) return -1;
2672 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -15, NULL)) return -1;
2673 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -16, NULL)) return -1;
2674 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 2, -14, NULL)) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002675
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302676 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 2, 3, -19, NULL)) return -1;
2677 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 3, 4, -11, NULL)) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002678
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302679 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_INT64, 4, 3, -9, NULL)) return -1;
2680 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_BYTE_STRING, 3, 1, -10, NULL)) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002681
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302682 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 1, 2, -22, NULL)) return -1;
2683 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_INT64, 2, 0, -5, NULL)) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002684
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002685 if(QCBORDecode_Finish(pDC)) return -2;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002686
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002687 return 0;
2688}
2689
2690
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002691/*
2692// cbor.me decoded output
2693{
2694 -23: {
2695 -20: {
2696 -18: "Organization",
2697 -17: "SSG",
2698 -15: "Confusion",
2699 -16: "San Diego",
2700 -14: "US"
2701 },
2702 -19: {
2703 -11: {
2704 -9: -7
2705 },
2706 -10: '\u0001\u0002\u0003\u0004\u0005\u0006\a\b\t\n'
2707 }
2708 },
2709 -22: {
2710 -5: -3
2711 }
2712}
2713 */
2714
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002715
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002716static uint8_t spCSRInput[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002717 0xa2, 0x36, 0xa2, 0x33, 0xa5, 0x31, 0x6c, 0x4f,
2718 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74,
2719 0x69, 0x6f, 0x6e, 0x30, 0x63, 0x53, 0x53, 0x47,
2720 0x2e, 0x69, 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73,
2721 0x69, 0x6f, 0x6e, 0x2f, 0x69, 0x53, 0x61, 0x6e,
2722 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f, 0x2d, 0x62,
2723 0x55, 0x53, 0x32, 0xa2, 0x2a, 0xa1, 0x28, 0x26,
2724 0x29, 0x4a, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
2725 0x07, 0x08, 0x09, 0x0a, 0x35, 0xa1, 0x24, 0x22};
2726
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002727int32_t NestedMapTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002728{
2729 QCBORDecodeContext DCtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002730
Laurence Lundbladeee851742020-01-08 08:37:05 -08002731 QCBORDecode_Init(&DCtx,
2732 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput),
2733 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002734
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002735 return CheckCSRMaps(&DCtx);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002736}
2737
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08002738
2739
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002740int32_t StringDecoderModeFailTest()
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08002741{
2742 QCBORDecodeContext DCtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002743
Laurence Lundbladeee851742020-01-08 08:37:05 -08002744 QCBORDecode_Init(&DCtx,
2745 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput),
2746 QCBOR_DECODE_MODE_MAP_STRINGS_ONLY);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002747
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08002748 QCBORItem Item;
2749 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002750
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08002751 if(QCBORDecode_GetNext(&DCtx, &Item)) {
2752 return -1;
2753 }
2754 if(Item.uDataType != QCBOR_TYPE_MAP) {
2755 return -2;
2756 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002757
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08002758 nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
2759 if(nCBORError != QCBOR_ERR_MAP_LABEL_TYPE) {
2760 return -3;
2761 }
2762
2763 return 0;
2764}
2765
2766
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002767// Same map as above, but using indefinite lengths
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002768static uint8_t spCSRInputIndefLen[] = {
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002769 0xbf, 0x36, 0xbf, 0x33, 0xbf, 0x31, 0x6c, 0x4f,
2770 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74,
2771 0x69, 0x6f, 0x6e, 0x30, 0x63, 0x53, 0x53, 0x47,
2772 0x2e, 0x69, 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73,
2773 0x69, 0x6f, 0x6e, 0x2f, 0x69, 0x53, 0x61, 0x6e,
2774 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f, 0x2d, 0x62,
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002775 0x55, 0x53, 0xff, 0x32, 0xbf, 0x2a, 0xbf, 0x28,
2776 0x26, 0xff, 0x29, 0x4a, 0x01, 0x02, 0x03, 0x04,
2777 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0xff, 0xff,
2778 0x35, 0xbf, 0x24, 0x22, 0xff, 0xff};
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002779
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002780int32_t NestedMapTestIndefLen()
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002781{
2782 QCBORDecodeContext DCtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002783
Laurence Lundbladeee851742020-01-08 08:37:05 -08002784 QCBORDecode_Init(&DCtx,
2785 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInputIndefLen),
2786 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002787
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002788 return CheckCSRMaps(&DCtx);
2789}
2790
2791
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002792
Laurence Lundblade17ede402018-10-13 11:43:07 +08002793static UsefulBufC make_nested_indefinite_arrays(int n, UsefulBuf Storage)
2794{
2795 UsefulOutBuf UOB;
2796 UsefulOutBuf_Init(&UOB, Storage);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002797
Laurence Lundblade17ede402018-10-13 11:43:07 +08002798 int i;
2799 for(i = 0; i < n; i++) {
2800 UsefulOutBuf_AppendByte(&UOB, 0x9f);
2801 }
2802
2803 for(i = 0; i < n; i++) {
2804 UsefulOutBuf_AppendByte(&UOB, 0xff);
2805 }
2806 return UsefulOutBuf_OutUBuf(&UOB);
2807}
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002808
2809
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002810static int32_t parse_indeflen_nested(UsefulBufC Nested, int nNestLevel)
Laurence Lundblade17ede402018-10-13 11:43:07 +08002811{
2812 QCBORDecodeContext DC;
2813 QCBORDecode_Init(&DC, Nested, 0);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002814
Laurence Lundblade17ede402018-10-13 11:43:07 +08002815 int j;
2816 for(j = 0; j < nNestLevel; j++) {
2817 QCBORItem Item;
2818 int nReturn = QCBORDecode_GetNext(&DC, &Item);
2819 if(j >= QCBOR_MAX_ARRAY_NESTING) {
2820 // Should be in error
2821 if(nReturn != QCBOR_ERR_ARRAY_NESTING_TOO_DEEP) {
2822 return -4;
2823 } else {
2824 return 0; // Decoding doesn't recover after an error
2825 }
2826 } else {
2827 // Should be no error
2828 if(nReturn) {
2829 return -9; // Should not have got an error
2830 }
2831 }
2832 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
2833 return -7;
2834 }
2835 }
2836 int nReturn = QCBORDecode_Finish(&DC);
2837 if(nReturn) {
2838 return -3;
2839 }
2840 return 0;
2841}
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002842
2843
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002844int32_t IndefiniteLengthNestTest()
Laurence Lundblade17ede402018-10-13 11:43:07 +08002845{
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05302846 UsefulBuf_MAKE_STACK_UB(Storage, 50);
Laurence Lundblade17ede402018-10-13 11:43:07 +08002847 int i;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002848 for(i=1; i < QCBOR_MAX_ARRAY_NESTING+4; i++) {
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08002849 const UsefulBufC Nested = make_nested_indefinite_arrays(i, Storage);
Laurence Lundblade17ede402018-10-13 11:43:07 +08002850 int nReturn = parse_indeflen_nested(Nested, i);
2851 if(nReturn) {
2852 return nReturn;
2853 }
2854 }
2855 return 0;
2856}
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002857
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002858
Laurence Lundbladeee851742020-01-08 08:37:05 -08002859// [1, [2, 3]]
2860static const uint8_t spIndefiniteArray[] = {0x9f, 0x01, 0x82, 0x02, 0x03, 0xff};
2861// No closing break
2862static const uint8_t spIndefiniteArrayBad1[] = {0x9f};
2863// Not enough closing breaks
2864static const uint8_t spIndefiniteArrayBad2[] = {0x9f, 0x9f, 0x02, 0xff};
2865// Too many closing breaks
2866static const uint8_t spIndefiniteArrayBad3[] = {0x9f, 0x02, 0xff, 0xff};
2867// Unclosed indeflen inside def len
2868static const uint8_t spIndefiniteArrayBad4[] = {0x81, 0x9f};
2869// confused tag
2870static const uint8_t spIndefiniteArrayBad5[] = {0x9f, 0xd1, 0xff};
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002871
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002872int32_t IndefiniteLengthArrayMapTest()
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002873{
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002874 int nResult;
2875 // --- first test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002876 UsefulBufC IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArray);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002877
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002878 // Decode it and see if it is OK
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05302879 UsefulBuf_MAKE_STACK_UB(MemPool, 150);
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002880 QCBORDecodeContext DC;
2881 QCBORItem Item;
2882 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002883
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002884 QCBORDecode_SetMemPool(&DC, MemPool, false);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002885
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002886 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05302887
2888 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
2889 Item.uNestingLevel != 0 ||
2890 Item.uNextNestLevel != 1) {
2891 return -111;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002892 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002893
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002894 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05302895 if(Item.uDataType != QCBOR_TYPE_INT64 ||
2896 Item.uNestingLevel != 1 ||
2897 Item.uNextNestLevel != 1) {
2898 return -2;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002899 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002900
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002901 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05302902 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
2903 Item.uNestingLevel != 1 ||
2904 Item.uNextNestLevel != 2) {
2905 return -3;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002906 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002907
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002908 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade12b495d2018-12-17 11:15:54 -08002909 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade6de37062018-10-15 12:22:42 +05302910 Item.uNestingLevel != 2 ||
2911 Item.uNextNestLevel != 2) {
2912 return -4;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002913 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002914
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002915 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade12b495d2018-12-17 11:15:54 -08002916 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade6de37062018-10-15 12:22:42 +05302917 Item.uNestingLevel != 2 ||
2918 Item.uNextNestLevel != 0) {
2919 return -5;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002920 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002921
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002922 if(QCBORDecode_Finish(&DC)) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05302923 return -6;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002924 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002925
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002926 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002927 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad1);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002928
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002929 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002930
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002931 QCBORDecode_SetMemPool(&DC, MemPool, false);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002932
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002933 nResult = QCBORDecode_GetNext(&DC, &Item);
2934 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05302935 return -7;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002936 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002937
Laurence Lundblade570fab52018-10-13 18:28:27 +08002938 nResult = QCBORDecode_Finish(&DC);
Laurence Lundblade6de37062018-10-15 12:22:42 +05302939 if(nResult != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
2940 return -8;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002941 }
2942
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002943
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002944 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002945 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad2);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002946
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002947 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002948
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002949 QCBORDecode_SetMemPool(&DC, MemPool, false);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002950
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002951 nResult = QCBORDecode_GetNext(&DC, &Item);
2952 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05302953 return -9;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002954 }
2955
2956 nResult = QCBORDecode_GetNext(&DC, &Item);
2957 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05302958 return -10;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002959 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002960
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002961 nResult = QCBORDecode_GetNext(&DC, &Item);
2962 if(nResult || Item.uDataType != QCBOR_TYPE_INT64) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05302963 return -11;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002964 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002965
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002966 nResult = QCBORDecode_Finish(&DC);
Laurence Lundblade6de37062018-10-15 12:22:42 +05302967 if(nResult != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
2968 return -12;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002969 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002970
2971
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002972 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002973 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad3);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002974
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002975 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002976
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002977 QCBORDecode_SetMemPool(&DC, MemPool, false);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002978
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002979 nResult = QCBORDecode_GetNext(&DC, &Item);
2980 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05302981 return -13;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002982 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002983
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002984 nResult = QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05302985 if(nResult != QCBOR_ERR_BAD_BREAK) {
2986 return -14;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002987 }
Laurence Lundblade6de37062018-10-15 12:22:42 +05302988
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002989
Laurence Lundblade570fab52018-10-13 18:28:27 +08002990 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002991 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad4);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002992
Laurence Lundblade570fab52018-10-13 18:28:27 +08002993 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002994
Laurence Lundblade570fab52018-10-13 18:28:27 +08002995 QCBORDecode_SetMemPool(&DC, MemPool, false);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002996
Laurence Lundblade570fab52018-10-13 18:28:27 +08002997 nResult = QCBORDecode_GetNext(&DC, &Item);
2998 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05302999 return -15;
Laurence Lundblade570fab52018-10-13 18:28:27 +08003000 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003001
Laurence Lundblade570fab52018-10-13 18:28:27 +08003002 nResult = QCBORDecode_GetNext(&DC, &Item);
3003 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303004 return -16;
Laurence Lundblade570fab52018-10-13 18:28:27 +08003005 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003006
Laurence Lundblade570fab52018-10-13 18:28:27 +08003007 nResult = QCBORDecode_Finish(&DC);
Laurence Lundblade6de37062018-10-15 12:22:42 +05303008 if(nResult != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
3009 return -17;
Laurence Lundblade570fab52018-10-13 18:28:27 +08003010 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003011
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303012 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003013 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad5);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003014
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303015 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003016
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303017 QCBORDecode_SetMemPool(&DC, MemPool, false);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003018
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303019 nResult = QCBORDecode_GetNext(&DC, &Item);
3020 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303021 return -18;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303022 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003023
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303024 nResult = QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05303025 if(nResult != QCBOR_ERR_BAD_BREAK) {
3026 return -19;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303027 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003028
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003029 return 0;
3030}
3031
Laurence Lundblade17ede402018-10-13 11:43:07 +08003032
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003033static const uint8_t spIndefiniteLenString[] = {
Laurence Lundblade17ede402018-10-13 11:43:07 +08003034 0x81, // Array of length one
3035 0x7f, // text string marked with indefinite length
3036 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment
3037 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment
3038 0xff // ending break
3039};
3040
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003041static const uint8_t spIndefiniteLenStringBad2[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303042 0x81, // Array of length one
3043 0x7f, // text string marked with indefinite length
3044 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment
3045 0x44, 0x6d, 0x69, 0x6e, 0x67, // second segment of wrong type
3046 0xff // ending break
3047};
3048
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003049static const uint8_t spIndefiniteLenStringBad3[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303050 0x81, // Array of length one
3051 0x7f, // text string marked with indefinite length
3052 0x01, 0x02, // Not a string
3053 0xff // ending break
3054};
3055
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003056static const uint8_t spIndefiniteLenStringBad4[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303057 0x81, // Array of length one
3058 0x7f, // text string marked with indefinite length
3059 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment
3060 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment
3061 // missing end of string
3062};
3063
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003064static const uint8_t spIndefiniteLenStringLabel[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303065 0xa1, // Array of length one
3066 0x7f, // text string marked with indefinite length
3067 0x65, 0x73, 0x74, 0x72, 0x75, 0x75, // first segment
3068 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment
3069 0xff, // ending break
3070 0x01 // integer being labeled.
3071};
3072
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003073/**
3074 Make an indefinite length string
3075
3076 @param Storage Storage for string, must be 144 bytes in size
3077 @return The indefinite length string
3078
3079 This makes an array with one indefinite length string that has 7 chunks
3080 from size of 1 byte up to 64 bytes.
3081 */
3082static UsefulBufC MakeIndefiniteBigBstr(UsefulBuf Storage)
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303083{
3084 UsefulOutBuf UOB;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003085
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303086 UsefulOutBuf_Init(&UOB, Storage);
3087 UsefulOutBuf_AppendByte(&UOB, 0x81);
3088 UsefulOutBuf_AppendByte(&UOB, 0x5f);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003089
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003090 uint8_t uStringByte = 0;
3091 // Use of type int is intentional
3092 for(int uChunkSize = 1; uChunkSize <= 128; uChunkSize *= 2) {
3093 // Not using preferred encoding here, but that is OK.
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303094 UsefulOutBuf_AppendByte(&UOB, 0x58);
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003095 UsefulOutBuf_AppendByte(&UOB, (uint8_t)uChunkSize);
3096 for(int j = 0; j < uChunkSize; j++) {
3097 UsefulOutBuf_AppendByte(&UOB, uStringByte);
3098 uStringByte++;
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303099 }
3100 }
3101 UsefulOutBuf_AppendByte(&UOB, 0xff);
3102
3103 return UsefulOutBuf_OutUBuf(&UOB);
3104}
3105
3106static int CheckBigString(UsefulBufC BigString)
3107{
3108 if(BigString.len != 255) {
3109 return 1;
3110 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003111
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303112 for(uint8_t i = 0; i < 255; i++){
3113 if(((const uint8_t *)BigString.ptr)[i] != i) {
3114 return 1;
3115 }
3116 }
3117 return 0;
3118}
3119
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05303120
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003121int32_t IndefiniteLengthStringTest()
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303122{
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303123 QCBORDecodeContext DC;
3124 QCBORItem Item;
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05303125 // big enough for MakeIndefiniteBigBstr() + MemPool overhead
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003126 UsefulBuf_MAKE_STACK_UB(MemPool, 350);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003127
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303128 // --- Simple normal indefinite length string ------
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003129 UsefulBufC IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenString);
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303130 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003131
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05303132 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303133 return -1;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303134 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003135
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303136 if(QCBORDecode_GetNext(&DC, &Item)) {
3137 return -2;
3138 }
3139 if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.uDataAlloc) {
3140 return -3;
3141 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003142
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303143 if(QCBORDecode_GetNext(&DC, &Item)) {
3144 return -4;
3145 }
3146 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || !Item.uDataAlloc) {
3147 return -5;
3148 }
3149 if(QCBORDecode_Finish(&DC)) {
3150 return -6;
3151 }
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303152
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303153 // ----- types mismatch ---
Laurence Lundbladeee851742020-01-08 08:37:05 -08003154 QCBORDecode_Init(&DC,
3155 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad2),
3156 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003157
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303158 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
3159 return -7;
3160 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003161
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303162 if(QCBORDecode_GetNext(&DC, &Item)) {
3163 return -8;
3164 }
3165 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
3166 return -9;
3167 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003168
Laurence Lundblade30816f22018-11-10 13:40:22 +07003169 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_INDEFINITE_STRING_CHUNK) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303170 return -10;
3171 }
3172
3173 // ----- not a string ---
Laurence Lundbladeee851742020-01-08 08:37:05 -08003174 QCBORDecode_Init(&DC,
3175 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad3),
3176 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003177
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303178 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
3179 return -11;
3180 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003181
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303182 if(QCBORDecode_GetNext(&DC, &Item)) {
3183 return -12;
3184 }
3185 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
3186 return -13;
3187 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003188
Laurence Lundblade30816f22018-11-10 13:40:22 +07003189 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_INDEFINITE_STRING_CHUNK) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303190 return -14;
3191 }
3192
3193 // ----- no end -----
Laurence Lundbladeee851742020-01-08 08:37:05 -08003194 QCBORDecode_Init(&DC,
3195 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad4),
3196 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003197
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303198 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
3199 return -15;
3200 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003201
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303202 if(QCBORDecode_GetNext(&DC, &Item)) {
3203 return -16;
3204 }
3205 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
3206 return -17;
3207 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003208
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303209 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_HIT_END) {
3210 return -18;
3211 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003212
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303213 // ------ Don't set a string allocator and see an error -----
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303214 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003215
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303216 QCBORDecode_GetNext(&DC, &Item);
3217 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303218 return -19;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303219 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003220
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303221 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_NO_STRING_ALLOCATOR) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303222 return -20;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303223 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003224
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303225 // ----- Mempool is way too small -----
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003226 UsefulBuf_MAKE_STACK_UB(MemPoolTooSmall, QCBOR_DECODE_MIN_MEM_POOL_SIZE-1);
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303227
3228 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
3229 if(!QCBORDecode_SetMemPool(&DC, MemPoolTooSmall, false)) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303230 return -21;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303231 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003232
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303233 // ----- Mempool is way too small -----
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05303234 UsefulBuf_MAKE_STACK_UB(BigIndefBStrStorage, 290);
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08003235 const UsefulBufC BigIndefBStr = MakeIndefiniteBigBstr(BigIndefBStrStorage);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003236
Laurence Lundbladeee851742020-01-08 08:37:05 -08003237 // 80 is big enough for MemPool overhead, but not BigIndefBStr
3238 UsefulBuf_MAKE_STACK_UB(MemPoolSmall, 80);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003239
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303240 QCBORDecode_Init(&DC, BigIndefBStr, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303241 if(QCBORDecode_SetMemPool(&DC, MemPoolSmall, false)) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303242 return -22;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303243 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003244
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303245 QCBORDecode_GetNext(&DC, &Item);
3246 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303247 return -23;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303248 }
Laurence Lundblade30816f22018-11-10 13:40:22 +07003249 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_STRING_ALLOCATE) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303250 return -24;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303251 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003252
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303253 // ---- big bstr -----
3254 QCBORDecode_Init(&DC, BigIndefBStr, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003255
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303256 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
3257 return -25;
3258 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003259
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05303260 if(QCBORDecode_GetNext(&DC, &Item)) {
3261 return -26;
3262 }
3263 if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.uDataAlloc) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303264 return -26;
3265 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003266
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303267 if(QCBORDecode_GetNext(&DC, &Item)) {
3268 return -27;
3269 }
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05303270 if(Item.uDataType != QCBOR_TYPE_BYTE_STRING || !Item.uDataAlloc || Item.uNestingLevel != 1) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303271 return -28;
3272 }
3273 if(CheckBigString(Item.val.string)) {
3274 return -3;
3275 }
3276 if(QCBORDecode_Finish(&DC)) {
3277 return -29;
3278 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003279
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303280 // --- label is an indefinite length string ------
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003281 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringLabel), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003282
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303283 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
3284 return -30;
3285 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003286
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303287 QCBORDecode_GetNext(&DC, &Item);
3288 if(Item.uDataType != QCBOR_TYPE_MAP) {
3289 return -31;
3290 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003291
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303292 if(QCBORDecode_GetNext(&DC, &Item)){
3293 return -32;
3294 }
Laurence Lundbladeee851742020-01-08 08:37:05 -08003295 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
3296 Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303297 Item.uDataAlloc || !Item.uLabelAlloc ||
3298 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("struuming"))) {
3299 return -33;
3300 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003301
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303302 if(QCBORDecode_Finish(&DC)) {
3303 return -34;
3304 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003305
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003306 return 0;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003307}
3308
3309
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003310int32_t AllocAllStringsTest()
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303311{
3312 QCBORDecodeContext DC;
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003313 QCBORError nCBORError;
3314
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003315
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303316 // First test, use the "CSRMap" as easy input and checking
Laurence Lundbladeee851742020-01-08 08:37:05 -08003317 QCBORDecode_Init(&DC,
3318 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput),
3319 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003320
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003321 UsefulBuf_MAKE_STACK_UB(Pool, sizeof(spCSRInput) + QCBOR_DECODE_MIN_MEM_POOL_SIZE);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003322
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003323 nCBORError = QCBORDecode_SetMemPool(&DC, Pool, 1); // Turn on copying.
3324 if(nCBORError) {
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303325 return -1;
3326 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003327
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003328 if(CheckCSRMaps(&DC)) {
3329 return -2;
3330 }
3331
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303332 // Next parse, save pointers to a few strings, destroy original and see all is OK.
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003333 UsefulBuf_MAKE_STACK_UB(CopyOfStorage, sizeof(pValidMapEncoded) + QCBOR_DECODE_MIN_MEM_POOL_SIZE);
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08003334 const UsefulBufC CopyOf = UsefulBuf_Copy(CopyOfStorage, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded));
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003335
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303336 QCBORDecode_Init(&DC, CopyOf, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08003337 UsefulBuf_Set(Pool, '/');
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303338 QCBORDecode_SetMemPool(&DC, Pool, 1); // Turn on copying.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003339
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303340 QCBORItem Item1, Item2, Item3, Item4;
3341 if((nCBORError = QCBORDecode_GetNext(&DC, &Item1)))
3342 return nCBORError;
3343 if(Item1.uDataType != QCBOR_TYPE_MAP ||
3344 Item1.val.uCount != 3)
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003345 return -3;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303346 if((nCBORError = QCBORDecode_GetNext(&DC, &Item1)))
3347 return nCBORError;
3348 if((nCBORError = QCBORDecode_GetNext(&DC, &Item2)))
3349 return nCBORError;
3350 if((nCBORError = QCBORDecode_GetNext(&DC, &Item3)))
3351 return nCBORError;
3352 if((nCBORError = QCBORDecode_GetNext(&DC, &Item4)))
3353 return nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003354
Laurence Lundblade05ec57b2018-10-21 01:50:03 +05303355 UsefulBuf_Set(CopyOfStorage, '_');
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003356
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303357 if(Item1.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303358 Item1.uDataType != QCBOR_TYPE_INT64 ||
3359 Item1.val.int64 != 42 ||
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003360 Item1.uDataAlloc != 0 ||
3361 Item1.uLabelAlloc == 0 ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09003362 UsefulBuf_Compare(Item1.label.string, UsefulBuf_FromSZ("first integer"))) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003363 return -4;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09003364 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003365
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303366
3367 if(Item2.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09003368 UsefulBuf_Compare(Item2.label.string, UsefulBuf_FromSZ("an array of two strings")) ||
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303369 Item2.uDataType != QCBOR_TYPE_ARRAY ||
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003370 Item2.uDataAlloc != 0 ||
3371 Item2.uLabelAlloc == 0 ||
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303372 Item2.val.uCount != 2)
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003373 return -5;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003374
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303375 if(Item3.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003376 Item3.uDataAlloc == 0 ||
3377 Item3.uLabelAlloc != 0 ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09003378 UsefulBuf_Compare(Item3.val.string, UsefulBuf_FromSZ("string1"))) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003379 return -6;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09003380 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003381
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303382 if(Item4.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003383 Item4.uDataAlloc == 0 ||
3384 Item4.uLabelAlloc != 0 ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09003385 UsefulBuf_Compare(Item4.val.string, UsefulBuf_FromSZ("string2"))) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003386 return -7;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09003387 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003388
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303389 // Next parse with a pool that is too small
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003390 UsefulBuf_MAKE_STACK_UB(SmallPool, QCBOR_DECODE_MIN_MEM_POOL_SIZE + 1);
Laurence Lundbladeee851742020-01-08 08:37:05 -08003391 QCBORDecode_Init(&DC,
3392 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded),
3393 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303394 QCBORDecode_SetMemPool(&DC, SmallPool, 1); // Turn on copying.
3395 if((nCBORError = QCBORDecode_GetNext(&DC, &Item1)))
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003396 return -8;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303397 if(Item1.uDataType != QCBOR_TYPE_MAP ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09003398 Item1.val.uCount != 3) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003399 return -9;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09003400 }
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303401 if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item1))){
3402 if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item2))) {
3403 if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item3))) {
3404 nCBORError = QCBORDecode_GetNext(&DC, &Item4);
3405 }
3406 }
3407 }
Laurence Lundblade30816f22018-11-10 13:40:22 +07003408 if(nCBORError != QCBOR_ERR_STRING_ALLOCATE) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003409 return -10;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303410 }
3411
3412 return 0;
3413}
3414
Laurence Lundbladef6531662018-12-04 10:42:22 +09003415
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303416
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003417int32_t MemPoolTest(void)
Laurence Lundblade0155b622018-10-12 20:04:37 +08003418{
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003419 // Set up the decoder with a tiny bit of CBOR to parse because
3420 // nothing can be done with it unless that is set up.
Laurence Lundbladef6531662018-12-04 10:42:22 +09003421 QCBORDecodeContext DC;
3422 const uint8_t pMinimalCBOR[] = {0xa0}; // One empty map
3423 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pMinimalCBOR),0);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003424
Laurence Lundbladef6531662018-12-04 10:42:22 +09003425 // Set up an memory pool of 100 bytes
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003426 // Then fish into the internals of the decode context
3427 // to get the allocator function so it can be called directly.
3428 // Also figure out how much pool is available for use
3429 // buy subtracting out the overhead.
Laurence Lundbladef6531662018-12-04 10:42:22 +09003430 UsefulBuf_MAKE_STACK_UB(Pool, 100);
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003431 QCBORError nError = QCBORDecode_SetMemPool(&DC, Pool, 0);
3432 if(nError) {
3433 return -9;
3434 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003435 QCBORStringAllocate pAlloc = DC.StringAllocator.pfAllocator;
3436 void *pAllocCtx = DC.StringAllocator.pAllocateCxt;
3437 size_t uAvailPool = Pool.len - QCBOR_DECODE_MIN_MEM_POOL_SIZE;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003438
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003439 // First test -- ask for one more byte than available and see failure
3440 UsefulBuf Allocated = (*pAlloc)(pAllocCtx, NULL, uAvailPool+1);
Laurence Lundbladef6531662018-12-04 10:42:22 +09003441 if(!UsefulBuf_IsNULL(Allocated)) {
3442 return -1;
3443 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003444
Laurence Lundbladef6531662018-12-04 10:42:22 +09003445 // Re do the set up for the next test that will do a successful alloc,
3446 // a fail, a free and then success
Laurence Lundbladef6531662018-12-04 10:42:22 +09003447 QCBORDecode_SetMemPool(&DC, Pool, 0);
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003448 pAlloc = DC.StringAllocator.pfAllocator;
3449 pAllocCtx = DC.StringAllocator.pAllocateCxt;
3450 uAvailPool = Pool.len - QCBOR_DECODE_MIN_MEM_POOL_SIZE;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003451
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003452 // Allocate one byte less than available and see success
3453 Allocated = (pAlloc)(pAllocCtx, NULL, uAvailPool-1);
Laurence Lundbladef6531662018-12-04 10:42:22 +09003454 if(UsefulBuf_IsNULL(Allocated)) { // expected to succeed
3455 return -2;
3456 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003457 // Ask for some more and see failure
3458 UsefulBuf Allocated2 = (*pAlloc)(pAllocCtx, NULL, uAvailPool/2);
Laurence Lundbladef6531662018-12-04 10:42:22 +09003459 if(!UsefulBuf_IsNULL(Allocated2)) { // expected to fail
3460 return -3;
3461 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003462 // Free the first allocate, retry the second and see success
3463 (*pAlloc)(pAllocCtx, Allocated.ptr, 0); // Free
3464 Allocated = (*pAlloc)(pAllocCtx, NULL, uAvailPool/2);
Laurence Lundbladef6531662018-12-04 10:42:22 +09003465 if(UsefulBuf_IsNULL(Allocated)) { // succeed because of the free
3466 return -4;
3467 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003468
Laurence Lundbladef6531662018-12-04 10:42:22 +09003469 // Re do set up for next test that involves a successful alloc,
3470 // and a successful realloc and a failed realloc
3471 QCBORDecode_SetMemPool(&DC, Pool, 0);
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003472 pAlloc = DC.StringAllocator.pfAllocator;
3473 pAllocCtx = DC.StringAllocator.pAllocateCxt;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003474
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003475 // Allocate half the pool and see success
3476 Allocated = (*pAlloc)(pAllocCtx, NULL, uAvailPool/2);
Laurence Lundbladef6531662018-12-04 10:42:22 +09003477 if(UsefulBuf_IsNULL(Allocated)) { // expected to succeed
3478 return -5;
3479 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003480 // Reallocate to take up the whole pool and see success
3481 Allocated2 = (*pAlloc)(pAllocCtx, Allocated.ptr, uAvailPool);
Laurence Lundbladef6531662018-12-04 10:42:22 +09003482 if(UsefulBuf_IsNULL(Allocated2)) {
3483 return -6;
3484 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003485 // Make sure its the same pointer and the size is right
Laurence Lundbladef6531662018-12-04 10:42:22 +09003486 if(Allocated2.ptr != Allocated.ptr || Allocated2.len != uAvailPool) {
3487 return -7;
3488 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003489 // Try to allocate more to be sure there is failure after a realloc
3490 UsefulBuf Allocated3 = (*pAlloc)(pAllocCtx, Allocated.ptr, uAvailPool+1);
3491 if(!UsefulBuf_IsNULL(Allocated3)) {
Laurence Lundbladef6531662018-12-04 10:42:22 +09003492 return -8;
3493 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003494
Laurence Lundbladef6531662018-12-04 10:42:22 +09003495 return 0;
3496}
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003497
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003498
3499/* Just enough of an allocator to test configuration of one */
3500static UsefulBuf AllocateTestFunction(void *pCtx, void *pOldMem, size_t uNewSize)
3501{
3502 (void)pOldMem; // unused variable
3503
3504 if(uNewSize) {
3505 // Assumes the context pointer is the buffer and
3506 // nothing too big will ever be asked for.
3507 // This is only good for this basic test!
3508 return (UsefulBuf) {pCtx, uNewSize};
3509 } else {
3510 return NULLUsefulBuf;
3511 }
3512}
3513
3514
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003515int32_t SetUpAllocatorTest(void)
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003516{
3517 // Set up the decoder with a tiny bit of CBOR to parse because
3518 // nothing can be done with it unless that is set up.
3519 QCBORDecodeContext DC;
3520 const uint8_t pMinimalCBOR[] = {0x62, 0x48, 0x69}; // "Hi"
3521 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pMinimalCBOR),0);
3522
3523 uint8_t pAllocatorBuffer[50];
3524
3525 // This is really just to test that this call works.
3526 // The full functionality of string allocators is tested
3527 // elsewhere with the MemPool internal allocator.
3528 QCBORDecode_SetUpAllocator(&DC, AllocateTestFunction, pAllocatorBuffer, 1);
3529
3530 QCBORItem Item;
3531 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_SUCCESS) {
3532 return -1;
3533 }
3534
3535 if(Item.uDataAlloc == 0 ||
3536 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
3537 Item.val.string.ptr != pAllocatorBuffer) {
3538 return -2;
3539 }
3540
3541 if(QCBORDecode_Finish(&DC) != QCBOR_SUCCESS) {
3542 return -3;
3543 }
3544
3545 return 0;
3546}
3547
Laurence Lundblade59289e52019-12-30 13:44:37 -08003548
3549#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
3550/*
3551 [
3552 4([-1, 3]),
3553 4([-20, 4759477275222530853136]),
3554 4([9223372036854775807, -4759477275222530853137]),
3555 5([300, 100]),
3556 5([-20, 4759477275222530853136]),
3557 5([-9223372036854775807, -4759477275222530853137])
3558 5([9223372036854775806, -4759477275222530853137])
3559 5([9223372036854775806, 9223372036854775806])]
3560 ]
3561 */
3562
3563static const uint8_t spExpectedExponentsAndMantissas[] = {
3564 0x87,
3565 0xC4, 0x82, 0x20,
3566 0x03,
3567 0xC4, 0x82, 0x33,
3568 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,
3569 0xC4, 0x82, 0x1B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
3570 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,
3571 0xC5, 0x82, 0x19, 0x01, 0x2C,
3572 0x18, 0x64,
3573 0xC5, 0x82, 0x33,
3574 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,
3575 0xC5, 0x82, 0x3B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE,
3576 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,
3577 0xC5, 0x82, 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE,
3578 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE
3579};
3580
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003581int32_t ExponentAndMantissaDecodeTests(void)
Laurence Lundblade59289e52019-12-30 13:44:37 -08003582{
3583 QCBORDecodeContext DC;
3584 QCBORError nCBORError;
3585 QCBORItem item;
3586
Laurence Lundblade17af4902020-01-07 19:11:55 -08003587 static const uint8_t spBigNumMantissa[] = {0x01, 0x02, 0x03, 0x04, 0x05,
3588 0x06, 0x07, 0x08, 0x09, 0x010};
3589 UsefulBufC BN = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNumMantissa);
Laurence Lundblade59289e52019-12-30 13:44:37 -08003590
3591
3592 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedExponentsAndMantissas), QCBOR_DECODE_MODE_NORMAL);
3593
3594 nCBORError = QCBORDecode_GetNext(&DC, &item);
3595 if(nCBORError != QCBOR_SUCCESS) {
3596 return 1;
3597 }
3598
3599 if(item.uDataType != QCBOR_TYPE_ARRAY) {
3600 return 2;
3601 }
3602
3603 nCBORError = QCBORDecode_GetNext(&DC, &item);
3604 if(nCBORError != QCBOR_SUCCESS) {
3605 return 3;
3606 }
3607
3608 if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION ||
3609 item.val.expAndMantissa.Mantissa.nInt != 3 ||
3610 item.val.expAndMantissa.nExponent != -1) {
3611 return 4;
3612 }
3613
3614 nCBORError = QCBORDecode_GetNext(&DC, &item);
3615 if(nCBORError != QCBOR_SUCCESS) {
3616 return 5;
3617 }
3618
3619 if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM ||
3620 item.val.expAndMantissa.nExponent != -20 ||
3621 UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) {
3622 return 6;
3623 }
3624
3625 nCBORError = QCBORDecode_GetNext(&DC, &item);
3626 if(nCBORError != QCBOR_SUCCESS) {
3627 return 7;
3628 }
3629
3630 if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM ||
3631 item.val.expAndMantissa.nExponent != 9223372036854775807 ||
3632 UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) {
3633 return 8;
3634 }
3635
3636 nCBORError = QCBORDecode_GetNext(&DC, &item);
3637 if(nCBORError != QCBOR_SUCCESS) {
3638 return 9;
3639 }
3640
3641 if(item.uDataType != QCBOR_TYPE_BIGFLOAT ||
3642 item.val.expAndMantissa.Mantissa.nInt != 100 ||
3643 item.val.expAndMantissa.nExponent != 300) {
3644 return 10;
3645 }
3646
3647 nCBORError = QCBORDecode_GetNext(&DC, &item);
3648 if(nCBORError != QCBOR_SUCCESS) {
3649 return 11;
3650 }
3651
3652 if(item.uDataType != QCBOR_TYPE_BIGFLOAT_POS_BIGNUM ||
3653 item.val.expAndMantissa.nExponent != -20 ||
3654 UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) {
3655 return 12;
3656 }
3657
3658 nCBORError = QCBORDecode_GetNext(&DC, &item);
3659 if(nCBORError != QCBOR_SUCCESS) {
3660 return 13;
3661 }
3662
3663 if(item.uDataType != QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM ||
3664 item.val.expAndMantissa.nExponent != -9223372036854775807 ||
3665 UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) {
3666 return 14;
3667 }
3668
3669 nCBORError = QCBORDecode_GetNext(&DC, &item);
3670 if(nCBORError != QCBOR_SUCCESS) {
3671 return 15;
3672 }
3673
3674 if(item.uDataType != QCBOR_TYPE_BIGFLOAT ||
3675 item.val.expAndMantissa.nExponent != 9223372036854775806 ||
3676 item.val.expAndMantissa.Mantissa.nInt!= 9223372036854775806 ) {
3677 return 16;
3678 }
3679
3680 /* Now encode some stuff and then decode it */
3681 uint8_t pBuf[40];
3682 QCBOREncodeContext EC;
3683 UsefulBufC Encoded;
3684
3685 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(pBuf));
3686 QCBOREncode_OpenArray(&EC);
3687 QCBOREncode_AddDecimalFraction(&EC, 999, 1000); // 999 * (10 ^ 1000)
3688 QCBOREncode_AddBigFloat(&EC, 100, INT32_MIN);
3689 QCBOREncode_AddDecimalFractionBigNum(&EC, BN, false, INT32_MAX);
3690 QCBOREncode_CloseArray(&EC);
3691 QCBOREncode_Finish(&EC, &Encoded);
3692
3693
3694 QCBORDecode_Init(&DC, Encoded, QCBOR_DECODE_MODE_NORMAL);
3695 nCBORError = QCBORDecode_GetNext(&DC, &item);
3696 if(nCBORError != QCBOR_SUCCESS) {
3697 return 13;
3698 }
3699
3700 nCBORError = QCBORDecode_GetNext(&DC, &item);
3701 if(nCBORError != QCBOR_SUCCESS) {
3702 return 13;
3703 }
3704
3705 if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION ||
3706 item.val.expAndMantissa.nExponent != 1000 ||
3707 item.val.expAndMantissa.Mantissa.nInt != 999) {
3708 return 15;
3709 }
3710
3711 nCBORError = QCBORDecode_GetNext(&DC, &item);
3712 if(nCBORError != QCBOR_SUCCESS) {
3713 return 13;
3714 }
3715
3716 if(item.uDataType != QCBOR_TYPE_BIGFLOAT ||
3717 item.val.expAndMantissa.nExponent != INT32_MIN ||
3718 item.val.expAndMantissa.Mantissa.nInt != 100) {
3719 return 15;
3720 }
3721
3722 nCBORError = QCBORDecode_GetNext(&DC, &item);
3723 if(nCBORError != QCBOR_SUCCESS) {
3724 return 13;
3725 }
3726
3727 if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM ||
3728 item.val.expAndMantissa.nExponent != INT32_MAX ||
3729 UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) {
3730 return 12;
3731 }
3732
3733 return 0;
3734}
3735
3736
3737static struct FailInput ExponentAndMantissaFailures[] = {
3738 // Exponent > INT64_MAX
3739 { {(uint8_t[]){0xC4, 0x82, 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
3740 0xFF, 0xFF, 0x1B, 0x80, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
3741 0xFF, 0xFF,}, 20}, QCBOR_ERR_BAD_EXP_AND_MANTISSA},
3742 // Mantissa > INT64_MAX
3743 { {(uint8_t[]){0xC4, 0x82, 0x1B, 0x80, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
3744 0xFF, 0xFF, 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05,
3745 0x06, 0x07, 0x08, 0x09, 0x10}, 23}, QCBOR_ERR_BAD_EXP_AND_MANTISSA},
3746 // End of input
3747 { {(uint8_t[]){0xC4, 0x82}, 2}, QCBOR_ERR_HIT_END},
3748 // End of input
3749 { {(uint8_t[]){0xC4, 0x82, 0x01}, 3}, QCBOR_ERR_HIT_END},
3750 // bad content for big num
3751 { {(uint8_t[]){0xC4, 0x82, 0x01, 0xc3, 0x01}, 5}, QCBOR_ERR_BAD_OPT_TAG},
3752 // bad content for big num
3753 { {(uint8_t[]){0xC4, 0x82, 0xc2, 0x01, 0x1f}, 5}, QCBOR_ERR_BAD_INT},
3754 // Bad integer for exponent
3755 { {(uint8_t[]){0xC4, 0x82, 0x01, 0x1f}, 4}, QCBOR_ERR_BAD_INT},
3756 // Bad integer for mantissa
3757 { {(uint8_t[]){0xC4, 0x82, 0x1f, 0x01}, 4}, QCBOR_ERR_BAD_INT},
3758 // 3 items in array
3759 { {(uint8_t[]){0xC4, 0x83, 0x03, 0x01, 02}, 5}, QCBOR_ERR_BAD_EXP_AND_MANTISSA},
3760 // unterminated indefinite length array
3761 { {(uint8_t[]){0xC4, 0x9f, 0x03, 0x01, 0x02}, 5}, QCBOR_ERR_BAD_EXP_AND_MANTISSA},
3762 // Empty array
3763 { {(uint8_t[]){0xC4, 0x80}, 2}, QCBOR_ERR_NO_MORE_ITEMS},
3764 // Second is not an integer
3765 { {(uint8_t[]){0xC4, 0x82, 0x03, 0x40}, 4}, QCBOR_ERR_BAD_EXP_AND_MANTISSA},
3766 // First is not an integer
3767 { {(uint8_t[]){0xC4, 0x82, 0x40}, 3}, QCBOR_ERR_BAD_EXP_AND_MANTISSA},
3768 // Not an array
3769 { {(uint8_t[]){0xC4, 0xa2}, 2}, QCBOR_ERR_BAD_EXP_AND_MANTISSA}
3770};
3771
3772
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003773int32_t ExponentAndMantissaDecodeFailTests()
Laurence Lundblade59289e52019-12-30 13:44:37 -08003774{
3775 return ProcessFailures(ExponentAndMantissaFailures,
3776 sizeof(ExponentAndMantissaFailures)/sizeof(struct FailInput));
3777}
3778
3779#endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */