blob: 9bcaa463445da87c36d0281286ccf39bf1026bc2 [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 Lundblade844bb5c2020-03-01 17:27:25 -080034#include "qcbor/qcbor_encode.h"
35#include "qcbor/qcbor_decode.h"
Laurence Lundbladed4728fd2018-12-17 15:15:56 -080036#include <string.h>
Laurence Lundblade9e3651c2018-10-10 11:49:55 +080037#include <math.h> // for fabs()
Laurence Lundbladebb1062e2019-08-12 23:28:54 -070038#include "not_well_formed_cbor.h"
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -080039
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080040
Laurence Lundbladea2e29072018-12-30 09:20:06 -080041#ifdef PRINT_FUNCTIONS_FOR_DEBUGGING
Laurence Lundblade20db9c92018-12-17 11:40:37 -080042#include <stdio.h>
Laurence Lundbladea2e29072018-12-30 09:20:06 -080043
44static void PrintUsefulBufC(const char *szLabel, UsefulBufC Buf)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080045{
46 if(szLabel) {
47 printf("%s ", szLabel);
48 }
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -080049
Laurence Lundblade570fab52018-10-13 18:28:27 +080050 size_t i;
Laurence Lundbladea2e29072018-12-30 09:20:06 -080051 for(i = 0; i < Buf.len; i++) {
52 uint8_t Z = ((uint8_t *)Buf.ptr)[i];
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080053 printf("%02x ", Z);
54 }
55 printf("\n");
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -080056
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080057 fflush(stdout);
58}
Laurence Lundblade20db9c92018-12-17 11:40:37 -080059#endif
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080060
61
Laurence Lundbladeb836efb2018-10-28 20:09:58 +070062static const uint8_t spExpectedEncodedInts[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080063 0x98, 0x2f, 0x3b, 0x7f, 0xff, 0xff, 0xff, 0xff,
64 0xff, 0xff, 0xff, 0x3b, 0x00, 0x00, 0x00, 0x01,
65 0x00, 0x00, 0x00, 0x00, 0x3a, 0xff, 0xff, 0xff,
66 0xff, 0x3a, 0xff, 0xff, 0xff, 0xfe, 0x3a, 0xff,
67 0xff, 0xff, 0xfd, 0x3a, 0x7f, 0xff, 0xff, 0xff,
68 0x3a, 0x7f, 0xff, 0xff, 0xfe, 0x3a, 0x00, 0x01,
69 0x00, 0x01, 0x3a, 0x00, 0x01, 0x00, 0x00, 0x39,
70 0xff, 0xff, 0x39, 0xff, 0xfe, 0x39, 0xff, 0xfd,
71 0x39, 0x01, 0x00, 0x38, 0xff, 0x38, 0xfe, 0x38,
72 0xfd, 0x38, 0x18, 0x37, 0x36, 0x20, 0x00, 0x00,
73 0x01, 0x16, 0x17, 0x18, 0x18, 0x18, 0x19, 0x18,
74 0x1a, 0x18, 0xfe, 0x18, 0xff, 0x19, 0x01, 0x00,
75 0x19, 0x01, 0x01, 0x19, 0xff, 0xfe, 0x19, 0xff,
76 0xff, 0x1a, 0x00, 0x01, 0x00, 0x00, 0x1a, 0x00,
77 0x01, 0x00, 0x01, 0x1a, 0x00, 0x01, 0x00, 0x02,
78 0x1a, 0x7f, 0xff, 0xff, 0xff, 0x1a, 0x7f, 0xff,
79 0xff, 0xff, 0x1a, 0x80, 0x00, 0x00, 0x00, 0x1a,
80 0x80, 0x00, 0x00, 0x01, 0x1a, 0xff, 0xff, 0xff,
81 0xfe, 0x1a, 0xff, 0xff, 0xff, 0xff, 0x1b, 0x00,
82 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x1b,
83 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01,
84 0x1b, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
85 0xff, 0x1b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
86 0xff, 0xff};
87
88
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080089// return CBOR error or -1 if type of value doesn't match
90
Laurence Lundbladec5fef682020-01-25 11:38:45 -080091static int32_t IntegerValuesParseTestInternal(QCBORDecodeContext *pDCtx)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080092{
93 QCBORItem Item;
94 int nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -080095
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080096 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
97 return nCBORError;
98 if(Item.uDataType != QCBOR_TYPE_ARRAY)
99 return -1;
100
101 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
102 return nCBORError;
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800103 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade570fab52018-10-13 18:28:27 +0800104 Item.val.int64 != -9223372036854775807LL - 1)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800105 return -1;
106
107 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
108 return nCBORError;
109 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade570fab52018-10-13 18:28:27 +0800110 Item.val.int64 != -4294967297)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800111 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800112
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800113 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
114 return nCBORError;
115 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade570fab52018-10-13 18:28:27 +0800116 Item.val.int64 != -4294967296)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800117 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800118
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800119 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
120 return nCBORError;
121 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade570fab52018-10-13 18:28:27 +0800122 Item.val.int64 != -4294967295)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800123 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800124
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800125 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
126 return nCBORError;
127 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade570fab52018-10-13 18:28:27 +0800128 Item.val.int64 != -4294967294)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800129 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800130
131
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800132 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
133 return nCBORError;
134 if(Item.uDataType != QCBOR_TYPE_INT64 ||
135 Item.val.int64 != -2147483648)
136 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800137
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800138 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
139 return nCBORError;
140 if(Item.uDataType != QCBOR_TYPE_INT64 ||
141 Item.val.int64 != -2147483647)
142 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800143
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800144 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
145 return nCBORError;
146 if(Item.uDataType != QCBOR_TYPE_INT64 ||
147 Item.val.int64 != -65538)
148 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800149
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800150 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
151 return nCBORError;
152 if(Item.uDataType != QCBOR_TYPE_INT64 ||
153 Item.val.int64 != -65537)
154 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800155
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800156 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
157 return nCBORError;
158 if(Item.uDataType != QCBOR_TYPE_INT64 ||
159 Item.val.int64 != -65536)
160 return -1;
161
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800162
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800163 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
164 return nCBORError;
165 if(Item.uDataType != QCBOR_TYPE_INT64 ||
166 Item.val.int64 != -65535)
167 return -1;
168
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800169
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800170 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
171 return nCBORError;
172 if(Item.uDataType != QCBOR_TYPE_INT64 ||
173 Item.val.int64 != -65534)
174 return -1;
175
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800176
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800177 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
178 return nCBORError;
179 if(Item.uDataType != QCBOR_TYPE_INT64 ||
180 Item.val.int64 != -257)
181 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800182
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800183 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
184 return nCBORError;
185 if(Item.uDataType != QCBOR_TYPE_INT64 ||
186 Item.val.int64 != -256)
187 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800188
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800189 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
190 return nCBORError;
191 if(Item.uDataType != QCBOR_TYPE_INT64 ||
192 Item.val.int64 != -255)
193 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800194
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800195 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
196 return nCBORError;
197 if(Item.uDataType != QCBOR_TYPE_INT64 ||
198 Item.val.int64 != -254)
199 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800200
201
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800202 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
203 return nCBORError;
204 if(Item.uDataType != QCBOR_TYPE_INT64 ||
205 Item.val.int64 != -25)
206 return -1;
207
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800208
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800209 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
210 return nCBORError;
211 if(Item.uDataType != QCBOR_TYPE_INT64 ||
212 Item.val.int64 != -24)
213 return -1;
214
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800215
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800216 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
217 return nCBORError;
218 if(Item.uDataType != QCBOR_TYPE_INT64 ||
219 Item.val.int64 != -23)
220 return -1;
221
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800222
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800223 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
224 return nCBORError;
225 if(Item.uDataType != QCBOR_TYPE_INT64 ||
226 Item.val.int64 != -1)
227 return -1;
228
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800229
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800230 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
231 return nCBORError;
232 if(Item.uDataType != QCBOR_TYPE_INT64 ||
233 Item.val.int64 != 0)
234 return -1;
235
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800236
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800237 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
238 return nCBORError;
239 if(Item.uDataType != QCBOR_TYPE_INT64 ||
240 Item.val.int64 != 0)
241 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800242
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800243 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
244 return nCBORError;
245 if(Item.uDataType != QCBOR_TYPE_INT64 ||
246 Item.val.int64 != 1)
247 return -1;
248
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800249
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800250 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
251 return nCBORError;
252 if(Item.uDataType != QCBOR_TYPE_INT64 ||
253 Item.val.int64 != 22)
254 return -1;
255
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800256
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800257 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
258 return nCBORError;
259 if(Item.uDataType != QCBOR_TYPE_INT64 ||
260 Item.val.int64 != 23)
261 return -1;
262
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800263
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800264 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
265 return nCBORError;
266 if(Item.uDataType != QCBOR_TYPE_INT64 ||
267 Item.val.int64 != 24)
268 return -1;
269
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800270
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800271 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
272 return nCBORError;
273 if(Item.uDataType != QCBOR_TYPE_INT64 ||
274 Item.val.int64 != 25)
275 return -1;
276
277 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
278 return nCBORError;
279 if(Item.uDataType != QCBOR_TYPE_INT64 ||
280 Item.val.int64 != 26)
281 return -1;
282
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800283
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800284 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
285 return nCBORError;
286 if(Item.uDataType != QCBOR_TYPE_INT64 ||
287 Item.val.int64 != 254)
288 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800289
290
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800291 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
292 return nCBORError;
293 if(Item.uDataType != QCBOR_TYPE_INT64 ||
294 Item.val.int64 != 255)
295 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800296
297
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800298 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
299 return nCBORError;
300 if(Item.uDataType != QCBOR_TYPE_INT64 ||
301 Item.val.int64 != 256)
302 return -1;
303
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800304
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800305 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
306 return nCBORError;
307 if(Item.uDataType != QCBOR_TYPE_INT64 ||
308 Item.val.int64 != 257)
309 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800310
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800311 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
312 return nCBORError;
313 if(Item.uDataType != QCBOR_TYPE_INT64 ||
314 Item.val.int64 != 65534)
315 return -1;
316
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800317
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800318 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
319 return nCBORError;
320 if(Item.uDataType != QCBOR_TYPE_INT64 ||
321 Item.val.int64 != 65535)
322 return -1;
323
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800324
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800325 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
326 return nCBORError;
327 if(Item.uDataType != QCBOR_TYPE_INT64 ||
328 Item.val.int64 != 65536)
329 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800330
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800331 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
332 return nCBORError;
333 if(Item.uDataType != QCBOR_TYPE_INT64 ||
334 Item.val.int64 != 65537)
335 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800336
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800337 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
338 return nCBORError;
339 if(Item.uDataType != QCBOR_TYPE_INT64 ||
340 Item.val.int64 != 65538)
341 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800342
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800343 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
344 return nCBORError;
345 if(Item.uDataType != QCBOR_TYPE_INT64 ||
346 Item.val.int64 != 2147483647)
347 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800348
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800349 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
350 return nCBORError;
351 if(Item.uDataType != QCBOR_TYPE_INT64 ||
352 Item.val.int64 != 2147483647)
353 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800354
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800355 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
356 return nCBORError;
357 if(Item.uDataType != QCBOR_TYPE_INT64 ||
358 Item.val.int64 != 2147483648)
359 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800360
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800361 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
362 return nCBORError;
363 if(Item.uDataType != QCBOR_TYPE_INT64 ||
364 Item.val.int64 != 2147483649)
365 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800366
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800367 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
368 return nCBORError;
369 if(Item.uDataType != QCBOR_TYPE_INT64 ||
370 Item.val.int64 != 4294967294)
371 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800372
373
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800374 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
375 return nCBORError;
376 if(Item.uDataType != QCBOR_TYPE_INT64 ||
377 Item.val.int64 != 4294967295)
378 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800379
380
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800381 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
382 return nCBORError;
383 if(Item.uDataType != QCBOR_TYPE_INT64 ||
384 Item.val.int64 != 4294967296)
385 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800386
387
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800388 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
389 return nCBORError;
390 if(Item.uDataType != QCBOR_TYPE_INT64 ||
391 Item.val.int64 != 4294967297)
392 return -1;
393
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800394
395
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800396 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
397 return nCBORError;
398 if(Item.uDataType != QCBOR_TYPE_INT64 ||
399 Item.val.int64 != 9223372036854775807LL)
400 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800401
402
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800403 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
404 return nCBORError;
405 if(Item.uDataType != QCBOR_TYPE_UINT64 ||
406 Item.val.uint64 != 18446744073709551615ULL)
407 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800408
409
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800410 if(QCBORDecode_Finish(pDCtx) != QCBOR_SUCCESS) {
411 return -1;
412 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800413
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800414 return 0;
415}
416
417
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800418// One less than the smallest negative integer allowed in C. Decoding
419// this should fail.
420static const uint8_t spTooSmallNegative[] = {
421 0x3b, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
Laurence Lundblade21d1d812019-09-28 22:47:49 -1000422};
423
424
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800425/*
426 Tests the decoding of lots of different integers sizes
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +0800427 and values.
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800428 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800429int32_t IntegerValuesParseTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800430{
Laurence Lundblade21d1d812019-09-28 22:47:49 -1000431 int nReturn;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800432 QCBORDecodeContext DCtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800433
Laurence Lundblade21d1d812019-09-28 22:47:49 -1000434 QCBORDecode_Init(&DCtx,
435 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedEncodedInts),
436 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800437
Laurence Lundblade21d1d812019-09-28 22:47:49 -1000438 // The really big test of all successes
439 nReturn = IntegerValuesParseTestInternal(&DCtx);
440 if(nReturn) {
441 return nReturn;
442 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800443
Laurence Lundblade21d1d812019-09-28 22:47:49 -1000444 // The one large negative integer that can be parsed
445 QCBORDecode_Init(&DCtx,
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800446 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTooSmallNegative),
Laurence Lundblade21d1d812019-09-28 22:47:49 -1000447 QCBOR_DECODE_MODE_NORMAL);
448
449 QCBORItem item;
450 if(QCBORDecode_GetNext(&DCtx, &item) != QCBOR_ERR_INT_OVERFLOW) {
451 nReturn = -4000;
452 }
453
454 return(nReturn);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800455}
456
457
458/*
Laurence Lundbladeee851742020-01-08 08:37:05 -0800459 Creates a simple CBOR array and returns it in *pEncoded. The array is
460 malloced and needs to be freed. This is used by several tests.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800461
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800462 Two of the inputs can be set. Two other items in the array are fixed.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800463
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800464 */
465
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -0800466static uint8_t spSimpleArrayBuffer[50];
467
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800468static int32_t CreateSimpleArray(int nInt1, int nInt2, uint8_t **pEncoded, size_t *pEncodedLen)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800469{
470 QCBOREncodeContext ECtx;
471 int nReturn = -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800472
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800473 *pEncoded = NULL;
474 *pEncodedLen = INT32_MAX;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800475
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800476 // loop runs CBOR encoding twice. First with no buffer to
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -0800477 // calculate the length so buffer can be allocated correctly,
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800478 // and last with the buffer to do the actual encoding
479 do {
Laurence Lundblade0595e932018-11-02 22:22:47 +0700480 QCBOREncode_Init(&ECtx, (UsefulBuf){*pEncoded, *pEncodedLen});
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800481 QCBOREncode_OpenArray(&ECtx);
482 QCBOREncode_AddInt64(&ECtx, nInt1);
483 QCBOREncode_AddInt64(&ECtx, nInt2);
484 QCBOREncode_AddBytes(&ECtx, ((UsefulBufC) {"galactic", 8}));
485 QCBOREncode_AddBytes(&ECtx, ((UsefulBufC) {"haven token", 11}));
486 QCBOREncode_CloseArray(&ECtx);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800487
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -0800488 if(QCBOREncode_FinishGetSize(&ECtx, pEncodedLen))
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800489 goto Done;
490
491 if(*pEncoded != NULL) {
492 nReturn = 0;
493 goto Done;
494 }
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -0800495
496 // Use static buffer to avoid dependency on malloc()
497 if(*pEncodedLen > sizeof(spSimpleArrayBuffer)) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800498 goto Done;
499 }
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -0800500 *pEncoded = spSimpleArrayBuffer;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800501
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800502 } while(1);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800503
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -0800504Done:
505 return nReturn;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800506}
507
508
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800509/*
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800510 Some basic CBOR with map and array used in a lot of tests.
511 The map labels are all strings
512
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800513 {"first integer": 42,
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900514 "an array of two strings": [
515 "string1", "string2"
516 ],
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800517 "map in a map": {
518 "bytes 1": h'78787878',
519 "bytes 2": h'79797979',
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900520 "another int": 98,
521 "text 2": "lies, damn lies and statistics"
522 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800523 }
524 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800525static const uint8_t pValidMapEncoded[] = {
Laurence Lundbladeb836efb2018-10-28 20:09:58 +0700526 0xa3, 0x6d, 0x66, 0x69, 0x72, 0x73, 0x74, 0x20, 0x69, 0x6e,
527 0x74, 0x65, 0x67, 0x65, 0x72, 0x18, 0x2a, 0x77, 0x61, 0x6e,
528 0x20, 0x61, 0x72, 0x72, 0x61, 0x79, 0x20, 0x6f, 0x66, 0x20,
529 0x74, 0x77, 0x6f, 0x20, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67,
530 0x73, 0x82, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x31,
531 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x32, 0x6c, 0x6d,
532 0x61, 0x70, 0x20, 0x69, 0x6e, 0x20, 0x61, 0x20, 0x6d, 0x61,
533 0x70, 0xa4, 0x67, 0x62, 0x79, 0x74, 0x65, 0x73, 0x20, 0x31,
534 0x44, 0x78, 0x78, 0x78, 0x78, 0x67, 0x62, 0x79, 0x74, 0x65,
535 0x73, 0x20, 0x32, 0x44, 0x79, 0x79, 0x79, 0x79, 0x6b, 0x61,
536 0x6e, 0x6f, 0x74, 0x68, 0x65, 0x72, 0x20, 0x69, 0x6e, 0x74,
537 0x18, 0x62, 0x66, 0x74, 0x65, 0x78, 0x74, 0x20, 0x32, 0x78,
538 0x1e, 0x6c, 0x69, 0x65, 0x73, 0x2c, 0x20, 0x64, 0x61, 0x6d,
539 0x6e, 0x20, 0x6c, 0x69, 0x65, 0x73, 0x20, 0x61, 0x6e, 0x64,
540 0x20, 0x73, 0x74, 0x61, 0x74, 0x69, 0x73, 0x74, 0x69, 0x63,
541 0x73 } ;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800542
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800543static int32_t ParseOrderedArray(const uint8_t *pEncoded,
Laurence Lundbladeee851742020-01-08 08:37:05 -0800544 size_t nLen,
545 int64_t *pInt1,
546 int64_t *pInt2,
547 const uint8_t **pBuf3,
548 size_t *pBuf3Len,
549 const uint8_t **pBuf4,
550 size_t *pBuf4Len)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800551{
552 QCBORDecodeContext DCtx;
553 QCBORItem Item;
554 int nReturn = -1; // assume error until success
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800555
Laurence Lundbladeee851742020-01-08 08:37:05 -0800556 QCBORDecode_Init(&DCtx,
557 (UsefulBufC){pEncoded, nLen},
558 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800559
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800560 // Make sure the first thing is a map
561 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || Item.uDataType != QCBOR_TYPE_ARRAY)
562 goto Done;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800563
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800564 // First integer
Laurence Lundblade12b495d2018-12-17 11:15:54 -0800565 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || Item.uDataType != QCBOR_TYPE_INT64)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800566 goto Done;
567 *pInt1 = Item.val.int64;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800568
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800569 // Second integer
570 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || Item.uDataType != QCBOR_TYPE_INT64)
571 goto Done;
572 *pInt2 = Item.val.int64;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800573
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800574 // First string
575 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || Item.uDataType != QCBOR_TYPE_BYTE_STRING)
576 goto Done;
577 *pBuf3 = Item.val.string.ptr;
578 *pBuf3Len = Item.val.string.len;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800579
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800580 // Second string
581 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || Item.uDataType != QCBOR_TYPE_BYTE_STRING)
582 goto Done;
583 *pBuf4 = Item.val.string.ptr;
584 *pBuf4Len = Item.val.string.len;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800585
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800586 nReturn = 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800587
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800588Done:
589 return(nReturn);
590}
591
592
593
Laurence Lundblade9e3651c2018-10-10 11:49:55 +0800594
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800595int32_t SimpleArrayTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800596{
597 uint8_t *pEncoded;
598 size_t nEncodedLen;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800599
Laurence Lundblade5e390822019-01-06 12:35:01 -0800600 int64_t i1=0, i2=0;
601 size_t i3=0, i4=0;
602 const uint8_t *s3= (uint8_t *)"";
603 const uint8_t *s4= (uint8_t *)"";
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800604
605
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800606 if(CreateSimpleArray(23, 6000, &pEncoded, &nEncodedLen) < 0) {
607 return(-1);
608 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800609
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800610 ParseOrderedArray(pEncoded, nEncodedLen, &i1, &i2, &s3, &i3, &s4, &i4);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800611
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800612 if(i1 != 23 ||
613 i2 != 6000 ||
614 i3 != 8 ||
615 i4 != 11 ||
Laurence Lundbladea44d5062018-10-17 18:45:12 +0530616 memcmp("galactic", s3, 8) !=0 ||
617 memcmp("haven token", s4, 11) !=0) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800618 return(-1);
619 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800620
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800621 return(0);
622}
623
624
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700625/*
626 [
627 0,
628 [],
629 [
630 [],
631 [
632 0
633 ],
634 {},
635 {
636 1: {},
637 2: {},
638 3: []
639 }
640 ]
641 ]
642 */
643static uint8_t sEmpties[] = {0x83, 0x00, 0x80, 0x84, 0x80, 0x81, 0x00, 0xa0,
644 0xa3, 0x01, 0xa0, 0x02, 0xa0, 0x03, 0x80};
645
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800646int32_t EmptyMapsAndArraysTest()
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700647{
648 QCBORDecodeContext DCtx;
649 QCBORItem Item;
650
Laurence Lundbladeee851742020-01-08 08:37:05 -0800651 QCBORDecode_Init(&DCtx,
652 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(sEmpties),
653 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700654
655 // Array with 3 items
656 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
657 Item.uDataType != QCBOR_TYPE_ARRAY ||
658 Item.uNestingLevel != 0 ||
659 Item.uNextNestLevel != 1 ||
660 Item.val.uCount != 3) {
661 return -1;
662 }
663
664 // An integer 0
665 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
666 Item.uDataType != QCBOR_TYPE_INT64 ||
667 Item.uNestingLevel != 1 ||
668 Item.uNextNestLevel != 1 ||
669 Item.val.uint64 != 0) {
670 return -2;
671 }
672
673 // An empty array
674 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
675 Item.uDataType != QCBOR_TYPE_ARRAY ||
676 Item.uNestingLevel != 1 ||
677 Item.uNextNestLevel != 1 ||
678 Item.val.uCount != 0) {
679 return -3;
680 }
681
682 // An array with 4 items
683 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
684 Item.uDataType != QCBOR_TYPE_ARRAY ||
685 Item.uNestingLevel != 1 ||
686 Item.uNextNestLevel != 2 ||
687 Item.val.uCount != 4) {
688 return -4;
689 }
690
691 // An empty array
692 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
693 Item.uDataType != QCBOR_TYPE_ARRAY ||
694 Item.uNestingLevel != 2 ||
695 Item.uNextNestLevel != 2 ||
696 Item.val.uCount != 0) {
697 return -5;
698 }
699
700 // An array with 1 item
701 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
702 Item.uDataType != QCBOR_TYPE_ARRAY ||
703 Item.uNestingLevel != 2 ||
704 Item.uNextNestLevel != 3 ||
705 Item.val.uCount != 1) {
706 return -6;
707 }
708
709 // An integer 0
710 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
711 Item.uDataType != QCBOR_TYPE_INT64 ||
712 Item.uNestingLevel != 3 ||
713 Item.uNextNestLevel != 2 ||
714 Item.val.uint64 != 0) {
715 return -7;
716 }
717
718 // An empty map
719 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
720 Item.uDataType != QCBOR_TYPE_MAP ||
721 Item.uNestingLevel != 2 ||
722 Item.uNextNestLevel != 2 ||
723 Item.val.uCount != 0) {
724 return -8;
725 }
726
727 // An map with 3 items
728 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
729 Item.uDataType != QCBOR_TYPE_MAP ||
730 Item.uNestingLevel != 2 ||
731 Item.uNextNestLevel != 3 ||
732 Item.val.uCount != 3) {
733 return -9;
734 }
735
736 // An empty map
737 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
738 Item.uDataType != QCBOR_TYPE_MAP ||
739 Item.uNestingLevel != 3 ||
740 Item.uNextNestLevel != 3 ||
741 Item.val.uCount != 0) {
742 return -10;
743 }
744
745 // An empty map
746 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
747 Item.uDataType != QCBOR_TYPE_MAP ||
748 Item.uNestingLevel != 3 ||
749 Item.uNextNestLevel != 3 ||
750 Item.val.uCount != 0) {
751 return -11;
752 }
753
754 // An empty array
755 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
756 Item.uDataType != QCBOR_TYPE_ARRAY ||
757 Item.uNestingLevel != 3 ||
758 Item.uNextNestLevel != 0 ||
759 Item.val.uCount != 0) {
760 return -12;
761 }
762
763 if(QCBORDecode_Finish(&DCtx) != QCBOR_SUCCESS) {
764 return -13;
765 }
766
767 return 0;
768}
769
Laurence Lundblade9e3651c2018-10-10 11:49:55 +0800770
Laurence Lundbladeee851742020-01-08 08:37:05 -0800771static uint8_t spDeepArrays[] = {0x81, 0x81, 0x81, 0x81, 0x81, 0x81,
772 0x81, 0x81, 0x81, 0x80};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800773
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800774int32_t ParseDeepArrayTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800775{
776 QCBORDecodeContext DCtx;
777 int nReturn = 0;
778 int i;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800779
Laurence Lundbladeee851742020-01-08 08:37:05 -0800780 QCBORDecode_Init(&DCtx,
781 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spDeepArrays),
782 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800783
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800784 for(i = 0; i < 10; i++) {
785 QCBORItem Item;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800786
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800787 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
788 Item.uDataType != QCBOR_TYPE_ARRAY ||
789 Item.uNestingLevel != i) {
790 nReturn = -1;
791 break;
792 }
793 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800794
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800795 return(nReturn);
796}
797
Laurence Lundblade972e59c2018-11-11 15:57:23 +0700798// Big enough to test nesting to the depth of 24
Laurence Lundbladeee851742020-01-08 08:37:05 -0800799static uint8_t spTooDeepArrays[] = {0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81,
800 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81,
801 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81,
802 0x81, 0x81, 0x81, 0x80};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800803
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800804int32_t ParseTooDeepArrayTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800805{
806 QCBORDecodeContext DCtx;
807 int nReturn = 0;
808 int i;
809 QCBORItem Item;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800810
811
Laurence Lundbladeee851742020-01-08 08:37:05 -0800812 QCBORDecode_Init(&DCtx,
813 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTooDeepArrays),
814 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800815
Laurence Lundblade972e59c2018-11-11 15:57:23 +0700816 for(i = 0; i < QCBOR_MAX_ARRAY_NESTING1; i++) {
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800817
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800818 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
819 Item.uDataType != QCBOR_TYPE_ARRAY ||
820 Item.uNestingLevel != i) {
821 nReturn = -1;
822 break;
823 }
824 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800825
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800826 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_ARRAY_NESTING_TOO_DEEP)
827 nReturn = -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800828
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800829 return(nReturn);
830}
831
832
833
834
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800835int32_t ShortBufferParseTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800836{
Laurence Lundbladebb1062e2019-08-12 23:28:54 -0700837 int nResult = 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800838
Laurence Lundblade06350ea2020-01-27 19:32:40 -0800839 for(size_t nNum = sizeof(spExpectedEncodedInts)-1; nNum; nNum--) {
Laurence Lundbladebb1062e2019-08-12 23:28:54 -0700840 QCBORDecodeContext DCtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800841
Laurence Lundbladeee851742020-01-08 08:37:05 -0800842 QCBORDecode_Init(&DCtx,
843 (UsefulBufC){spExpectedEncodedInts, nNum},
844 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800845
Laurence Lundblade06350ea2020-01-27 19:32:40 -0800846 const int nErr = IntegerValuesParseTestInternal(&DCtx);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800847
Laurence Lundbladebb1062e2019-08-12 23:28:54 -0700848 if(nErr != QCBOR_ERR_HIT_END && nErr != QCBOR_ERR_NO_MORE_ITEMS) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800849 nResult = -1;
850 goto Done;
851 }
852 }
853Done:
854 return nResult;
855}
856
857
Laurence Lundblade9e3651c2018-10-10 11:49:55 +0800858
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800859int32_t ShortBufferParseTest2()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800860{
861 uint8_t *pEncoded;
862 int nReturn;
863 size_t nEncodedLen;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800864
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800865 int64_t i1, i2;
866 size_t i3, i4;
867 const uint8_t *s3, *s4;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800868
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800869 nReturn = 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800870
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800871 if(CreateSimpleArray(23, 6000, &pEncoded, &nEncodedLen) < 0) {
872 return(-1);
873 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800874
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800875 for(nEncodedLen--; nEncodedLen; nEncodedLen--) {
876 int nResult = ParseOrderedArray(pEncoded, (uint32_t)nEncodedLen, &i1, &i2, &s3, &i3, &s4, &i4);
877 if(nResult == 0) {
878 nReturn = -1;
879 }
880 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800881
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800882 return(nReturn);
883}
884
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530885/*
886 Decode and thoroughly check a moderately complex
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800887 set of maps. Can be run in QCBOR_DECODE_MODE_NORMAL or in
888 QCBOR_DECODE_MODE_MAP_STRINGS_ONLY.
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530889 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800890static int32_t ParseMapTest1(QCBORDecodeMode nMode)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800891{
892 QCBORDecodeContext DCtx;
893 QCBORItem Item;
894 int nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800895
Laurence Lundbladeee851742020-01-08 08:37:05 -0800896 QCBORDecode_Init(&DCtx,
897 (UsefulBufC){pValidMapEncoded, sizeof(pValidMapEncoded)},
898 nMode);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800899
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900900 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800901 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900902 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800903 if(Item.uDataType != QCBOR_TYPE_MAP ||
904 Item.val.uCount != 3)
905 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800906
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900907 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800908 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900909 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800910 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800911 Item.uDataType != QCBOR_TYPE_INT64 ||
912 Item.val.int64 != 42 ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530913 Item.uDataAlloc ||
914 Item.uLabelAlloc ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900915 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("first integer"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800916 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900917 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800918
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900919 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800920 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900921 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800922 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530923 Item.uDataAlloc ||
924 Item.uLabelAlloc ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900925 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("an array of two strings")) ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800926 Item.uDataType != QCBOR_TYPE_ARRAY ||
927 Item.val.uCount != 2)
928 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800929
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900930 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800931 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900932 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800933 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530934 Item.uDataAlloc ||
935 Item.uLabelAlloc ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900936 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("string1"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800937 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900938 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800939
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900940 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800941 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900942 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800943 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530944 Item.uDataAlloc ||
945 Item.uLabelAlloc ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900946 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("string2"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800947 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900948 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800949
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900950 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800951 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900952 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800953 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530954 Item.uDataAlloc ||
955 Item.uLabelAlloc ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900956 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("map in a map")) ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800957 Item.uDataType != QCBOR_TYPE_MAP ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900958 Item.val.uCount != 4) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800959 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900960 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800961
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900962 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800963 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900964 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800965 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900966 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("bytes 1"))||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800967 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530968 Item.uDataAlloc ||
969 Item.uLabelAlloc ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900970 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("xxxx"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800971 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900972 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800973
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900974 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800975 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900976 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800977 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900978 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("bytes 2")) ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800979 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530980 Item.uDataAlloc ||
981 Item.uLabelAlloc ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900982 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("yyyy"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800983 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900984 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800985
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900986 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800987 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900988 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800989 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530990 Item.uDataAlloc ||
991 Item.uLabelAlloc ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900992 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("another int")) ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800993 Item.uDataType != QCBOR_TYPE_INT64 ||
994 Item.val.int64 != 98)
995 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800996
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900997 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800998 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900999 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001000 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001001 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("text 2"))||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001002 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301003 Item.uDataAlloc ||
1004 Item.uLabelAlloc ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001005 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("lies, damn lies and statistics"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001006 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001007 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001008
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001009 return 0;
1010}
1011
1012
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001013/*
1014 Decode and thoroughly check a moderately complex
1015 set of maps
1016 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001017int32_t ParseMapAsArrayTest()
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001018{
1019 QCBORDecodeContext DCtx;
1020 QCBORItem Item;
1021 int nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001022
Laurence Lundbladeee851742020-01-08 08:37:05 -08001023 QCBORDecode_Init(&DCtx,
1024 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded),
1025 QCBOR_DECODE_MODE_MAP_AS_ARRAY);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001026
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001027 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
1028 return nCBORError;
1029 }
Laurence Lundbladed61cbf32018-12-09 11:42:21 -08001030 if(Item.uDataType != QCBOR_TYPE_MAP_AS_ARRAY ||
1031 Item.val.uCount != 6) {
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001032 return -1;
1033 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001034
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001035 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
1036 return nCBORError;
1037 }
1038 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1039 Item.uDataAlloc ||
1040 Item.uLabelAlloc ||
1041 Item.uLabelType != QCBOR_TYPE_NONE ||
1042 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("first integer"))) {
1043 return -2;
1044 }
1045
1046 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
1047 return nCBORError;
1048 }
1049 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1050 Item.uDataType != QCBOR_TYPE_INT64 ||
1051 Item.val.int64 != 42 ||
1052 Item.uDataAlloc ||
1053 Item.uLabelAlloc) {
1054 return -3;
1055 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001056
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001057 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
1058 return nCBORError;
1059 }
1060 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1061 Item.uDataAlloc ||
1062 Item.uLabelAlloc ||
1063 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("an array of two strings")) ||
1064 Item.uDataType != QCBOR_TYPE_TEXT_STRING) {
1065 return -4;
1066 }
1067
1068 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
1069 return nCBORError;
1070 }
1071 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1072 Item.uDataAlloc ||
1073 Item.uLabelAlloc ||
1074 Item.uDataType != QCBOR_TYPE_ARRAY ||
1075 Item.val.uCount != 2) {
1076 return -5;
1077 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001078
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001079 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
1080 return nCBORError;
1081 }
1082 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1083 Item.val.string.len != 7 ||
1084 Item.uDataAlloc ||
1085 Item.uLabelAlloc ||
1086 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("string1"))) {
1087 return -6;
1088 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001089
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001090 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
1091 return nCBORError;
1092 }
1093 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1094 Item.uDataAlloc ||
1095 Item.uLabelAlloc ||
1096 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("string2"))) {
1097 return -7;
1098 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001099
1100
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001101 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
1102 return nCBORError;
1103 }
1104 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1105 Item.uDataAlloc ||
1106 Item.uLabelAlloc ||
1107 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("map in a map"))) {
1108 return -8;
1109 }
1110
1111 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
1112 return nCBORError;
1113 }
1114 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1115 Item.uDataAlloc ||
1116 Item.uLabelAlloc ||
Laurence Lundbladed61cbf32018-12-09 11:42:21 -08001117 Item.uDataType != QCBOR_TYPE_MAP_AS_ARRAY ||
1118 Item.val.uCount != 8) {
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001119 return -9;
1120 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001121
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001122 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
1123 return nCBORError;
1124 }
1125 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1126 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("bytes 1"))||
1127 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1128 Item.uDataAlloc ||
1129 Item.uLabelAlloc) {
1130 return -10;
1131 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001132
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001133 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
1134 return nCBORError;
1135 }
1136 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1137 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
1138 Item.uDataAlloc ||
1139 Item.uLabelAlloc ||
1140 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("xxxx"))) {
1141 return -11;
1142 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001143
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001144 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
1145 return nCBORError;
1146 }
1147 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1148 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("bytes 2")) ||
1149 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1150 Item.uDataAlloc ||
1151 Item.uLabelAlloc) {
1152 return -12;
1153 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001154
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001155 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
1156 return nCBORError;
1157 }
1158 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1159 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
1160 Item.uDataAlloc ||
1161 Item.uLabelAlloc ||
1162 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("yyyy"))) {
1163 return -13;
1164 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001165
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001166 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
1167 return nCBORError;
1168 }
1169 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1170 Item.uDataAlloc ||
1171 Item.uLabelAlloc ||
1172 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("another int")) ||
1173 Item.uDataType != QCBOR_TYPE_TEXT_STRING) {
1174 return -14;
1175 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001176
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001177 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
1178 return nCBORError;
1179 }
1180 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1181 Item.uDataAlloc ||
1182 Item.uLabelAlloc ||
1183 Item.uDataType != QCBOR_TYPE_INT64 ||
1184 Item.val.int64 != 98) {
1185 return -15;
1186 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001187
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001188 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
1189 return nCBORError;
1190 }
1191 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1192 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("text 2"))||
1193 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1194 Item.uDataAlloc ||
1195 Item.uLabelAlloc) {
1196 return -16;
1197 }
1198
1199 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
1200 return nCBORError;
1201 }
1202 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1203 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1204 Item.uDataAlloc ||
1205 Item.uLabelAlloc ||
1206 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("lies, damn lies and statistics"))) {
1207 return -17;
1208 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001209
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001210 return 0;
1211}
1212
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001213
1214/*
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301215 Fully or partially decode pValidMapEncoded. When
1216 partially decoding check for the right error code.
1217 How much partial decoding depends on nLevel.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001218
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301219 The partial decodes test error conditions of
1220 incomplete encoded input.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001221
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301222 This could be combined with the above test
1223 and made prettier and maybe a little more
1224 thorough.
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001225 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001226static int32_t ExtraBytesTest(int nLevel)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001227{
1228 QCBORDecodeContext DCtx;
1229 QCBORItem Item;
1230 int nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001231
Laurence Lundbladeee851742020-01-08 08:37:05 -08001232 QCBORDecode_Init(&DCtx,
1233 (UsefulBufC){pValidMapEncoded, sizeof(pValidMapEncoded)},
1234 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001235
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001236 if(nLevel < 1) {
1237 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_EXTRA_BYTES) {
1238 return -1;
1239 } else {
1240 return 0;
1241 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001242 }
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301243
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001244
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001245 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001246 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001247 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001248 if(Item.uDataType != QCBOR_TYPE_MAP ||
1249 Item.val.uCount != 3)
1250 return -1;
1251
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001252 if(nLevel < 2) {
1253 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
1254 return -1;
1255 } else {
1256 return 0;
1257 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001258 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001259
1260
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001261 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001262 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001263 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001264 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001265 Item.uDataType != QCBOR_TYPE_INT64 ||
1266 Item.val.uCount != 42 ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001267 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("first integer"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001268 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001269 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001270
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001271 if(nLevel < 3) {
1272 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
1273 return -1;
1274 } else {
1275 return 0;
1276 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001277 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001278
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001279 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001280 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001281 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001282 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001283 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("an array of two strings")) ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001284 Item.uDataType != QCBOR_TYPE_ARRAY ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001285 Item.val.uCount != 2) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001286 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001287 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001288
1289
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001290 if(nLevel < 4) {
1291 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
1292 return -1;
1293 } else {
1294 return 0;
1295 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001296 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001297
1298
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001299 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001300 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001301 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001302 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001303 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("string1"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001304 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001305 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001306
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001307 if(nLevel < 5) {
1308 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
1309 return -1;
1310 } else {
1311 return 0;
1312 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001313 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001314
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001315 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001316 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001317 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001318 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001319 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("string2"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001320 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001321 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001322
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001323 if(nLevel < 6) {
1324 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
1325 return -1;
1326 } else {
1327 return 0;
1328 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001329 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001330
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001331 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001332 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001333 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001334 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001335 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("map in a map")) ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001336 Item.uDataType != QCBOR_TYPE_MAP ||
1337 Item.val.uCount != 4)
1338 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001339
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001340 if(nLevel < 7) {
1341 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
1342 return -1;
1343 } else {
1344 return 0;
1345 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001346 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001347
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001348 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001349 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001350 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001351 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001352 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("bytes 1")) ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001353 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001354 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("xxxx"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001355 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001356 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001357
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001358 if(nLevel < 8) {
1359 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
1360 return -1;
1361 } else {
1362 return 0;
1363 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001364 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001365
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001366 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001367 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001368 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001369 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001370 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("bytes 2")) ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001371 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001372 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("yyyy"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001373 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001374 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001375
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001376 if(nLevel < 9) {
1377 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
1378 return -1;
1379 } else {
1380 return 0;
1381 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001382 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001383
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001384 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001385 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001386 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001387 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001388 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("another int")) ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001389 Item.uDataType != QCBOR_TYPE_INT64 ||
1390 Item.val.int64 != 98)
1391 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001392
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001393 if(nLevel < 10) {
1394 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
1395 return -1;
1396 } else {
1397 return 0;
1398 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001399 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001400
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001401 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001402 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001403 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001404 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001405 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("text 2"))||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001406 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001407 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("lies, damn lies and statistics"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001408 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001409 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001410
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301411 if(QCBORDecode_Finish(&DCtx)) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001412 return -1;
1413 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001414
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001415 return 0;
1416}
1417
1418
1419
Laurence Lundblade844bb5c2020-03-01 17:27:25 -08001420
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001421int32_t ParseMapTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001422{
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001423 // Parse a moderatly complex map structure very thoroughly
1424 int32_t nResult = ParseMapTest1(QCBOR_DECODE_MODE_NORMAL);
1425 if(nResult) {
1426 return nResult;
1427 }
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08001428
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001429 // Again, but in strings-only mode. It should succeed since the input
1430 // map has only string labels.
1431 nResult = ParseMapTest1(QCBOR_DECODE_MODE_MAP_STRINGS_ONLY);
1432 if(nResult) {
1433 return nResult;
1434 }
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08001435
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001436 // Again, but try to finish the decoding before the end of the
1437 // input at 10 different place and see that the right error code
1438 // is returned.
1439 for(int i = 0; i < 10; i++) {
1440 nResult = ExtraBytesTest(i);
1441 if(nResult) {
1442 break;
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001443 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001444 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001445
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001446 return nResult;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001447}
1448
1449
Laurence Lundbladeee851742020-01-08 08:37:05 -08001450static uint8_t spSimpleValues[] = {0x8a, 0xf4, 0xf5, 0xf6, 0xf7, 0xff,
1451 0xe0, 0xf3, 0xf8, 0x00, 0xf8, 0x13,
1452 0xf8, 0x1f, 0xf8, 0x20, 0xf8, 0xff};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001453
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001454int32_t ParseSimpleTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001455{
1456 QCBORDecodeContext DCtx;
1457 QCBORItem Item;
1458 int nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001459
1460
Laurence Lundbladeee851742020-01-08 08:37:05 -08001461 QCBORDecode_Init(&DCtx,
1462 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSimpleValues),
1463 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001464
1465
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001466 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1467 return nCBORError;
1468 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
1469 Item.val.uCount != 10)
1470 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001471
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001472 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1473 return nCBORError;
1474 if(Item.uDataType != QCBOR_TYPE_FALSE)
1475 return -1;
1476
1477 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1478 return nCBORError;
1479 if(Item.uDataType != QCBOR_TYPE_TRUE)
1480 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001481
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001482 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1483 return nCBORError;
1484 if(Item.uDataType != QCBOR_TYPE_NULL)
1485 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001486
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001487 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1488 return nCBORError;
1489 if(Item.uDataType != QCBOR_TYPE_UNDEF)
1490 return -1;
1491
1492 // A break
Laurence Lundblade9e3651c2018-10-10 11:49:55 +08001493 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_BREAK)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001494 return -1;
1495
1496 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1497 return nCBORError;
1498 if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 0)
1499 return -1;
1500
1501 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1502 return nCBORError;
1503 if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 19)
1504 return -1;
1505
Laurence Lundblade077475f2019-04-26 09:06:33 -07001506 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_TYPE_7)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001507 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001508
Laurence Lundblade077475f2019-04-26 09:06:33 -07001509 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_TYPE_7)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001510 return -1;
1511
Laurence Lundblade077475f2019-04-26 09:06:33 -07001512 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_TYPE_7)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001513 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001514
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001515 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1516 return nCBORError;
1517 if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 32)
1518 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001519
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001520 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1521 return nCBORError;
1522 if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 255)
1523 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001524
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001525 return 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001526
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001527}
1528
1529
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001530static int IsNotWellFormedError(QCBORError nErr)
1531{
1532 switch(nErr){
1533 case QCBOR_ERR_INDEFINITE_STRING_CHUNK:
1534 case QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN:
1535 case QCBOR_ERR_UNSUPPORTED:
1536 case QCBOR_ERR_HIT_END:
1537 case QCBOR_ERR_BAD_TYPE_7:
1538 case QCBOR_ERR_BAD_BREAK:
1539 case QCBOR_ERR_EXTRA_BYTES:
1540 case QCBOR_ERR_BAD_INT:
1541 return 1;
1542 default:
1543 return 0;
1544 }
1545}
1546
1547
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001548int32_t NotWellFormedTests()
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001549{
1550 // Loop over all the not-well-formed instance of CBOR
1551 // that are test vectors in not_well_formed_cbor.h
1552 const uint16_t nArraySize = sizeof(paNotWellFormedCBOR)/sizeof(struct someBinaryBytes);
1553 for(uint16_t nIterate = 0; nIterate < nArraySize; nIterate++) {
1554 const struct someBinaryBytes *pBytes = &paNotWellFormedCBOR[nIterate];
1555 const UsefulBufC Input = (UsefulBufC){pBytes->p, pBytes->n};
1556
Laurence Lundbladeee851742020-01-08 08:37:05 -08001557 // Set up decoder context. String allocator needed for indefinite
1558 // string test cases
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001559 QCBORDecodeContext DCtx;
1560 QCBORDecode_Init(&DCtx, Input, QCBOR_DECODE_MODE_NORMAL);
1561 UsefulBuf_MAKE_STACK_UB(Pool, 100);
1562 QCBORDecode_SetMemPool(&DCtx, Pool, 0);
1563
1564 // Loop getting items until no more to get
1565 QCBORError nCBORError;
1566 do {
1567 QCBORItem Item;
1568
1569 nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
1570 } while(nCBORError == QCBOR_SUCCESS);
1571
1572 // Every test vector must fail with
1573 // a not-well-formed error. If not
1574 // this test fails.
1575 if(!IsNotWellFormedError(nCBORError)) {
1576 // Return index of failure in the error code
1577 return 2000 + nIterate;
1578 }
1579 }
1580 return 0;
1581}
1582
1583
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001584struct FailInput {
Laurence Lundblade59289e52019-12-30 13:44:37 -08001585 UsefulBufC Input;
1586 QCBORError nError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001587};
1588
Laurence Lundblade59289e52019-12-30 13:44:37 -08001589
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001590static int32_t ProcessFailures(struct FailInput *pFailInputs, size_t nNumFails)
Laurence Lundblade59289e52019-12-30 13:44:37 -08001591{
1592 for(struct FailInput *pF = pFailInputs; pF < pFailInputs + nNumFails; pF++) {
1593 // Set up the decoding context including a memory pool so that
1594 // indefinite length items can be checked
1595 QCBORDecodeContext DCtx;
1596 QCBORDecode_Init(&DCtx, pF->Input, QCBOR_DECODE_MODE_NORMAL);
1597 UsefulBuf_MAKE_STACK_UB(Pool, 100);
1598 QCBORError nCBORError = QCBORDecode_SetMemPool(&DCtx, Pool, 0);
1599 if(nCBORError) {
1600 return -9;
1601 }
1602
1603 // Iterate until there is an error of some sort error
1604 QCBORItem Item;
1605 do {
1606 // Set to something none-zero other than QCBOR_TYPE_NONE
1607 memset(&Item, 0x33, sizeof(Item));
1608
1609 nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
1610 } while(nCBORError == QCBOR_SUCCESS);
1611
1612 // Must get the expected error or the this test fails
1613 // The data and label type must also be QCBOR_TYPE_NONE
1614 if(nCBORError != pF->nError ||
1615 Item.uDataType != QCBOR_TYPE_NONE ||
1616 Item.uLabelType != QCBOR_TYPE_NONE) {
Laurence Lundblade06350ea2020-01-27 19:32:40 -08001617 // return index of CBOR + 100
1618 const size_t nIndex = (size_t)(pF - pFailInputs)/sizeof(struct FailInput);
1619 return (int32_t)(nIndex * 100 + nCBORError);
Laurence Lundblade59289e52019-12-30 13:44:37 -08001620 }
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 },
Laurence Lundblade42272e42020-01-31 07:50:53 -08001826 // Byte string should have 2^32-15 bytes, but has one
1827 { {(uint8_t[]){0x5a, 0xff, 0xff, 0xff, 0xf0, 0x00}, 6}, QCBOR_ERR_HIT_END },
1828 // Byte string should have 2^32-15 bytes, but has one
1829 { {(uint8_t[]){0x7a, 0xff, 0xff, 0xff, 0xf0, 0x00}, 6}, QCBOR_ERR_HIT_END },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001830
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 */
Laurence Lundblade06350ea2020-01-27 19:32:40 -08001972static void ComprehensiveInputRecurser(uint8_t *pBuf, size_t nLen, size_t 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
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002005int32_t ComprehensiveInputTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002006{
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002007 // Size 2 tests 64K inputs and runs quickly
2008 uint8_t pBuf[2];
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002009
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002010 ComprehensiveInputRecurser(pBuf, 0, sizeof(pBuf));
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002011
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002012 return 0;
2013}
2014
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002015
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002016int32_t BigComprehensiveInputTest()
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002017{
2018 // size 3 tests 16 million inputs and runs OK
2019 // in seconds on fast machines. Size 4 takes
2020 // 10+ minutes and 5 half a day on fast
2021 // machines. This test is kept separate from
2022 // the others so as to no slow down the use
2023 // of them as a very frequent regression.
2024 uint8_t pBuf[3]; //
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08002025
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002026 ComprehensiveInputRecurser(pBuf, 0, sizeof(pBuf));
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08002027
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002028 return 0;
2029}
2030
2031
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002032static uint8_t spDateTestInput[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002033 0xc0, // tag for string date
2034 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002035
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002036 0xc1, // tag for epoch date
2037 0x1a, 0x53, 0x72, 0x4E, 0x00, // Epoch date 1400000000; Tue, 13 May 2014 16:53:20 GMT
2038
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002039 // CBOR_TAG_B64
2040 0xc1, 0xcf, 0xd8, 0x22, // 0xee, // Epoch date with extra tags TODO: fix this test
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002041 0x1a, 0x53, 0x72, 0x4E, 0x01,
2042
2043 0xc1, // tag for epoch date
2044 0x1b, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, // Too large integer
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002045
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002046 0xc1, // tag for epoch date
2047 0xfa, 0x3f, 0x8c, 0xcc, 0xcd, // double with value 1.1
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002048
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002049 0xc1, // tag for epoch date
Laurence Lundbladea1ad8782019-11-08 00:12:11 -08002050 0xfa, 0x7f, 0x7f, 0xff, 0xff, // 3.4028234663852886e+38 too large
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002051
Laurence Lundbladea1ad8782019-11-08 00:12:11 -08002052 0xc1, // tag for epoch date
2053 0xfb, 0x43, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 9223372036854775808.000000 just barely too large
2054 //0xfa, 0x7f, 0x7f, 0xff, 0xff // 3.4028234663852886e+38 too large
2055
2056 0xc1, // tag for epoch date
2057 0xfb, 0x43, 0xdf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe // 9223372036854773760 largest supported
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002058};
2059
2060
2061// have to check float expected only to within an epsilon
2062int CHECK_EXPECTED_DOUBLE(double val, double expected) {
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002063
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002064 double diff = val - expected;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002065
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002066 diff = fabs(diff);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002067
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002068 return diff > 0.0000001;
2069}
2070
2071
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002072int32_t DateParseTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002073{
2074 QCBORDecodeContext DCtx;
2075 QCBORItem Item;
2076 int nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002077
Laurence Lundbladeee851742020-01-08 08:37:05 -08002078 QCBORDecode_Init(&DCtx,
2079 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spDateTestInput),
2080 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002081
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002082 const uint64_t uTags[] = {15};
2083 QCBORTagListIn TagList = {1, uTags};
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002084
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002085 QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TagList);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002086
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002087 // String date
2088 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
2089 return -1;
2090 if(Item.uDataType != QCBOR_TYPE_DATE_STRING ||
Laurence Lundblade9e3651c2018-10-10 11:49:55 +08002091 UsefulBuf_Compare(Item.val.dateString, UsefulBuf_FromSZ("1985-04-12"))){
Laurence Lundblade67bd5512018-11-02 21:44:06 +07002092 return -2;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002093 }
2094
2095 // Epoch date
2096 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade67bd5512018-11-02 21:44:06 +07002097 return -3;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002098 if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH ||
2099 Item.val.epochDate.nSeconds != 1400000000 ||
2100 Item.val.epochDate.fSecondsFraction != 0 ) {
Laurence Lundblade67bd5512018-11-02 21:44:06 +07002101 return -4;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002102 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002103
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002104 // Epoch date with extra CBOR_TAG_B64 tag that doesn't really mean anything
2105 // but want to be sure extra tag doesn't cause a problem
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002106 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade67bd5512018-11-02 21:44:06 +07002107 return -5;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002108 if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH ||
2109 Item.val.epochDate.nSeconds != 1400000001 ||
2110 Item.val.epochDate.fSecondsFraction != 0 ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002111 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_B64)) {
Laurence Lundblade67bd5512018-11-02 21:44:06 +07002112 return -6;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002113 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002114
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002115 // Epoch date that is too large for our representation
2116 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_DATE_OVERFLOW) {
Laurence Lundblade67bd5512018-11-02 21:44:06 +07002117 return -7;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002118 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002119
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002120 // Epoch date in float format with fractional seconds
2121 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade67bd5512018-11-02 21:44:06 +07002122 return -8;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002123 if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH ||
2124 Item.val.epochDate.nSeconds != 1 ||
2125 CHECK_EXPECTED_DOUBLE(Item.val.epochDate.fSecondsFraction, 0.1 )) {
Laurence Lundblade67bd5512018-11-02 21:44:06 +07002126 return -9;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002127 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002128
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002129 // Epoch date float that is too large for our representation
2130 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_DATE_OVERFLOW) {
Laurence Lundblade67bd5512018-11-02 21:44:06 +07002131 return -10;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002132 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002133
Laurence Lundbladea1ad8782019-11-08 00:12:11 -08002134 // Epoch date double that is just slightly too large
2135 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_DATE_OVERFLOW) {
2136 return -11;
2137 }
2138
2139 // Largest double epoch date supported
2140 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_SUCCESS ||
2141 Item.uDataType != QCBOR_TYPE_DATE_EPOCH ||
2142 Item.val.epochDate.nSeconds != 9223372036854773760 ||
2143 Item.val.epochDate.nSeconds == 0) {
2144 return -12;
2145 }
Laurence Lundbladeee851742020-01-08 08:37:05 -08002146 // TODO: could use a few more tests with float, double, and half precsion
2147 // and negative (but coverage is still pretty good)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002148
2149 return 0;
2150}
2151
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002152// Really simple basic input for tagging test
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002153static uint8_t spOptTestInput[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002154 0xd9, 0xd9, 0xf7, // CBOR magic number
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002155 0x81, // Array of one
2156 0xd8, 0x04, // non-preferred serialization of tag 4
2157 0x82, 0x01, 0x03}; // fraction 1/3
2158
Laurence Lundblade59289e52019-12-30 13:44:37 -08002159/*
2160 DB 9192939495969798 # tag(10489608748473423768)
2161 80 # array(0)
2162 */
Laurence Lundbladeee851742020-01-08 08:37:05 -08002163static uint8_t spEncodedLargeTag[] = {0xdb, 0x91, 0x92, 0x93, 0x94, 0x95,
2164 0x96, 0x97, 0x98, 0x80};
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002165
Laurence Lundblade59289e52019-12-30 13:44:37 -08002166/*
2167DB 9192939495969798 # tag(10489608748473423768)
2168 D8 88 # tag(136)
2169 C6 # tag(6)
2170 C7 # tag(7)
2171 80 # array(0)
2172*/
Laurence Lundbladeee851742020-01-08 08:37:05 -08002173static uint8_t spLotsOfTags[] = {0xdb, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96,
2174 0x97, 0x98, 0xd8, 0x88, 0xc6, 0xc7, 0x80};
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002175
2176/*
2177 The cbor.me parse of this.
2178 55799(55799(55799({6(7(-23)): 5859837686836516696(7({7(-20): 11({17(-18): 17(17(17("Organization"))),
Laurence Lundblade59289e52019-12-30 13:44:37 -08002179 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 +07002180 17(-14): 17("US")}), 23(-19): 19({-11: 9({-9: -7}),
2181 90599561(90599561(90599561(-10))): 12(h'0102030405060708090A')})})),
2182 16(-22): 23({11(8(7(-5))): 8(-3)})})))
2183 */
2184static uint8_t spCSRWithTags[] = {
2185 0xd9, 0xd9, 0xf7, 0xd9, 0xd9, 0xf7, 0xd9, 0xd9, 0xf7, 0xa2,
2186 0xc6, 0xc7, 0x36,
2187 0xdb, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0xc7, 0xa2,
2188 0xda, 0x00, 0x00, 0x00, 0x07, 0x33,
2189 0xcb, 0xa5,
2190 0xd1, 0x31,
2191 0xd1, 0xd1, 0xd1, 0x6c,
2192 0x4f, 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74, 0x69, 0x6f, 0x6e,
2193 0xc9, 0x30,
2194 0xd9, 0x03, 0x05, 0x63,
2195 0x53, 0x53, 0x47,
2196 0x2e,
Laurence Lundblade59289e52019-12-30 13:44:37 -08002197 0xd0, 0xd1, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf, 0x69,
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002198 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73, 0x69, 0x6f, 0x6e,
2199 0xd1, 0x2f,
2200 0xd1, 0x69,
2201 0x53, 0x61, 0x6e, 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f,
2202 0xd1, 0x2d,
2203 0xd1, 0x62,
2204 0x55, 0x53,
2205 0xd7, 0x32,
2206 0xd3, 0xa2,
2207 0x2a,
2208 0xc9, 0xa1,
2209 0x28,
2210 0x26,
2211 0xda, 0x05, 0x66, 0x70, 0x89, 0xda, 0x05, 0x66, 0x70, 0x89, 0xda, 0x05, 0x66, 0x70, 0x89, 0x29,
2212 0xcc, 0x4a,
2213 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,0x07, 0x08, 0x09, 0x0a,
2214 0xd0, 0x35,
2215 0xd7, 0xa1,
2216 0xcb, 0xc8, 0xc7, 0x24,
2217 0xc8, 0x22};
2218
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002219static int32_t CheckCSRMaps(QCBORDecodeContext *pDC);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002220
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002221
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002222int32_t OptTagParseTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002223{
2224 QCBORDecodeContext DCtx;
2225 QCBORItem Item;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002226
Laurence Lundbladeee851742020-01-08 08:37:05 -08002227 QCBORDecode_Init(&DCtx,
2228 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spOptTestInput),
2229 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002230
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002231 //-------------------------
2232 // This text matches the magic number tag and the fraction tag
2233 if(QCBORDecode_GetNext(&DCtx, &Item)) {
2234 return -2;
2235 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002236 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002237 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC)) {
2238 return -3;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002239 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002240
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002241 if(QCBORDecode_GetNext(&DCtx, &Item)) {
2242 return -4;
2243 }
Laurence Lundblade59289e52019-12-30 13:44:37 -08002244
2245#ifdef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002246 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
Laurence Lundblade59289e52019-12-30 13:44:37 -08002247 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_DECIMAL_FRACTION) ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002248 Item.val.uCount != 2) {
2249 return -5;
2250 }
Laurence Lundblade59289e52019-12-30 13:44:37 -08002251#else
2252 if(Item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION) {
2253 return -6;
2254 }
2255#endif
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002256
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002257 // --------------------------------
2258 // This test decodes the very large tag, but it is not in
2259 // any list so it is ignored.
Laurence Lundbladeee851742020-01-08 08:37:05 -08002260 QCBORDecode_Init(&DCtx,
2261 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEncodedLargeTag),
2262 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002263 if(QCBORDecode_GetNext(&DCtx, &Item)) {
2264 return -6;
2265 }
2266 if(Item.uTagBits) {
2267 return -7;
2268 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002269
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002270 // ----------------------------------
Laurence Lundbladeee851742020-01-08 08:37:05 -08002271 // This test sets up a caller-config list that includes the very large
2272 // tage and then matches it.
2273 QCBORDecode_Init(&DCtx,
2274 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEncodedLargeTag),
2275 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002276 const uint64_t puList[] = {0x9192939495969798, 257};
2277 const QCBORTagListIn TL = {2, puList};
2278 QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002279
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002280 if(QCBORDecode_GetNext(&DCtx, &Item)) {
2281 return -8;
2282 }
2283 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
2284 !QCBORDecode_IsTagged(&DCtx, &Item, 0x9192939495969798) ||
2285 QCBORDecode_IsTagged(&DCtx, &Item, 257) ||
2286 QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_BIGFLOAT) ||
2287 Item.val.uCount != 0) {
2288 return -9;
2289 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002290
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002291 //------------------------
Laurence Lundbladeee851742020-01-08 08:37:05 -08002292 // Sets up a caller-configured list and look up something not in it
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002293 const uint64_t puLongList[17] = {1,2,1};
2294 const QCBORTagListIn TLLong = {17, puLongList};
Laurence Lundbladeee851742020-01-08 08:37:05 -08002295 QCBORDecode_Init(&DCtx,
2296 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEncodedLargeTag),
2297 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002298 QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TLLong);
2299 if(QCBORDecode_GetNext(&DCtx, &Item)) {
2300 return -11;
2301 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002302
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002303 // -----------------------
2304 // This tests retrievel of the full tag list
Laurence Lundbladeee851742020-01-08 08:37:05 -08002305 QCBORDecode_Init(&DCtx,
2306 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spLotsOfTags),
2307 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002308 uint64_t puTags[16];
2309 QCBORTagListOut Out = {0, 4, puTags};
2310 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2311 return -12;
2312 }
2313 if(puTags[0] != 0x9192939495969798 ||
2314 puTags[1] != 0x88 ||
Laurence Lundblade59289e52019-12-30 13:44:37 -08002315 puTags[2] != 0x06 ||
2316 puTags[3] != 0x07) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002317 return -13;
2318 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002319
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002320 // ----------------------
2321 // This text if too small of an out list
Laurence Lundbladeee851742020-01-08 08:37:05 -08002322 QCBORDecode_Init(&DCtx,
2323 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spLotsOfTags),
2324 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002325 QCBORTagListOut OutSmall = {0, 3, puTags};
2326 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &OutSmall) != QCBOR_ERR_TOO_MANY_TAGS) {
2327 return -14;
2328 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002329
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002330 // ---------------
2331 // Parse a version of the "CSR" that has had a ton of tags randomly inserted
Laurence Lundbladeee851742020-01-08 08:37:05 -08002332 QCBORDecode_Init(&DCtx,
2333 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRWithTags),
2334 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002335 int n = CheckCSRMaps(&DCtx);
2336 if(n) {
2337 return n-2000;
2338 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002339
Laurence Lundblade59289e52019-12-30 13:44:37 -08002340 Out = (QCBORTagListOut){0, 16, puTags};
Laurence Lundbladeee851742020-01-08 08:37:05 -08002341 QCBORDecode_Init(&DCtx,
2342 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRWithTags),
2343 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002344
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002345 const uint64_t puTagList[] = {773, 1, 90599561};
2346 const QCBORTagListIn TagList = {3, puTagList};
2347 QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TagList);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002348
2349
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002350 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2351 return -100;
2352 }
2353 if(Item.uDataType != QCBOR_TYPE_MAP ||
2354 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC) ||
2355 QCBORDecode_IsTagged(&DCtx, &Item, 90599561) ||
2356 QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_DATE_EPOCH) ||
2357 Item.val.uCount != 2 ||
2358 puTags[0] != CBOR_TAG_CBOR_MAGIC ||
2359 puTags[1] != CBOR_TAG_CBOR_MAGIC ||
2360 puTags[2] != CBOR_TAG_CBOR_MAGIC ||
2361 Out.uNumUsed != 3) {
2362 return -101;
2363 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002364
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002365 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2366 return -102;
2367 }
2368 if(Item.uDataType != QCBOR_TYPE_MAP ||
2369 QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC) ||
2370 QCBORDecode_IsTagged(&DCtx, &Item, 6) ||
2371 QCBORDecode_IsTagged(&DCtx, &Item, 7) || // item is tagged 7, but 7 is not configured to be recognized
2372 Item.val.uCount != 2 ||
2373 puTags[0] != 5859837686836516696 ||
2374 puTags[1] != 7 ||
2375 Out.uNumUsed != 2) {
2376 return -103;
2377 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002378
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002379 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2380 return -104;
2381 }
2382 if(Item.uDataType != QCBOR_TYPE_MAP ||
2383 Item.uTagBits ||
2384 Item.val.uCount != 5 ||
2385 puTags[0] != 0x0b ||
2386 Out.uNumUsed != 1) {
2387 return -105;
2388 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002389
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002390 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2391 return -106;
2392 }
2393 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
2394 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_COSE_MAC0) ||
2395 Item.val.string.len != 12 ||
2396 puTags[0] != CBOR_TAG_COSE_MAC0 ||
2397 puTags[1] != CBOR_TAG_COSE_MAC0 ||
2398 puTags[2] != CBOR_TAG_COSE_MAC0 ||
2399 Out.uNumUsed != 3) {
2400 return -105;
2401 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002402
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002403 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2404 return -107;
2405 }
2406 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
2407 !QCBORDecode_IsTagged(&DCtx, &Item, 773) ||
2408 Item.val.string.len != 3 ||
2409 puTags[0] != 773 ||
2410 Out.uNumUsed != 1) {
2411 return -108;
2412 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002413
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002414 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2415 return -109;
2416 }
2417 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade59289e52019-12-30 13:44:37 -08002418 !QCBORDecode_IsTagged(&DCtx, &Item, 16) ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002419 Item.val.string.len != 9 ||
Laurence Lundblade59289e52019-12-30 13:44:37 -08002420 puTags[0] != 16 ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002421 puTags[11] != 0x0f ||
2422 Out.uNumUsed != 12) {
2423 return -110;
2424 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002425
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002426 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2427 return -111;
2428 }
2429 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
2430 !QCBORDecode_IsTagged(&DCtx, &Item, 17) ||
2431 Item.val.string.len != 9 ||
2432 puTags[0] != 17 ||
2433 Out.uNumUsed != 1) {
2434 return -112;
2435 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002436
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002437 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2438 return -111;
2439 }
2440 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
2441 !QCBORDecode_IsTagged(&DCtx, &Item, 17) ||
2442 Item.val.string.len != 2 ||
2443 puTags[0] != 17 ||
2444 Out.uNumUsed != 1) {
2445 return -112;
2446 }
2447
2448 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2449 return -113;
2450 }
2451 if(Item.uDataType != QCBOR_TYPE_MAP ||
2452 QCBORDecode_IsTagged(&DCtx, &Item, 19) ||
2453 Item.val.uCount != 2 ||
2454 puTags[0] != 19 ||
2455 Out.uNumUsed != 1) {
2456 return -114;
2457 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002458
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002459 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2460 return -115;
2461 }
2462 if(Item.uDataType != QCBOR_TYPE_MAP ||
2463 QCBORDecode_IsTagged(&DCtx, &Item, 9) ||
2464 Item.uTagBits ||
2465 Item.val.uCount != 1 ||
2466 puTags[0] != 9 ||
2467 Out.uNumUsed != 1) {
2468 return -116;
2469 }
2470
2471 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2472 return -116;
2473 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002474 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002475 Item.val.int64 != -7 ||
2476 Item.uTagBits ||
2477 Out.uNumUsed != 0) {
2478 return -117;
2479 }
2480
2481 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2482 return -118;
2483 }
2484 if(Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
2485 Item.val.string.len != 10 ||
2486 Item.uTagBits ||
2487 puTags[0] != 12 ||
2488 Out.uNumUsed != 1) {
2489 return -119;
2490 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002491
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002492 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2493 return -120;
2494 }
2495 if(Item.uDataType != QCBOR_TYPE_MAP ||
2496 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_ENC_AS_B16) ||
2497 Item.val.uCount != 1 ||
2498 puTags[0] != 0x17 ||
2499 Out.uNumUsed != 1) {
2500 return -121;
2501 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002502
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002503 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2504 return -122;
2505 }
2506 if(Item.uDataType != QCBOR_TYPE_INT64 ||
2507 QCBORDecode_IsTagged(&DCtx, &Item, 8) ||
2508 Item.val.int64 != -3 ||
2509 puTags[0] != 8 ||
2510 Out.uNumUsed != 1) {
2511 return -123;
2512 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002513
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002514 if(QCBORDecode_Finish(&DCtx)) {
2515 return -124;
2516 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002517
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002518 return 0;
2519}
2520
2521
2522
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002523
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002524static uint8_t spBigNumInput[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002525 0x83,
2526 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2527 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2528 0xA4,
2529 0x63, 0x42, 0x4E, 0x2B,
2530 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2531 0x18, 0x40,
2532 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2533 0x63, 0x42, 0x4E, 0x2D,
2534 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2535 0x38, 0x3F,
2536 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
2537
2538
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002539static uint8_t spBigNum[] = {0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002540
2541
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002542int32_t BignumParseTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002543{
2544 QCBORDecodeContext DCtx;
2545 QCBORItem Item;
2546 int nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002547
Laurence Lundbladeee851742020-01-08 08:37:05 -08002548 QCBORDecode_Init(&DCtx,
2549 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNumInput),
2550 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002551
2552
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002553 //
2554 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
2555 return -1;
2556 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
2557 return -1;
2558 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002559
2560 //
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002561 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
2562 return -1;
2563 if(Item.uDataType != QCBOR_TYPE_POSBIGNUM ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002564 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002565 return -1;
2566 }
2567
2568 //
2569 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
2570 return -1;
2571 if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002572 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002573 return -1;
2574 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002575
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002576 //
2577 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
2578 return -1;
2579 if(Item.uDataType != QCBOR_TYPE_MAP) {
2580 return -1;
2581 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002582
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002583 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
2584 return -1;
2585 if(Item.uDataType != QCBOR_TYPE_POSBIGNUM ||
2586 Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002587 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002588 return -1;
2589 }
2590
2591 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
2592 return -1;
2593 if(Item.uDataType != QCBOR_TYPE_POSBIGNUM ||
2594 Item.uLabelType != QCBOR_TYPE_INT64 ||
2595 Item.label.int64 != 64 ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002596 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002597 return -1;
2598 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002599
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002600 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
2601 return -1;
2602 if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM ||
2603 Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002604 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002605 return -1;
2606 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002607
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002608 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
2609 return -1;
2610 if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM ||
2611 Item.uLabelType != QCBOR_TYPE_INT64 ||
2612 Item.label.int64 != -64 ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002613 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002614 return -1;
2615 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002616
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002617 return 0;
2618}
2619
2620
2621
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002622static int32_t CheckItemWithIntLabel(QCBORDecodeContext *pCtx,
Laurence Lundbladeee851742020-01-08 08:37:05 -08002623 uint8_t uDataType,
2624 uint8_t uNestingLevel,
2625 uint8_t uNextNest,
2626 int64_t nLabel,
2627 QCBORItem *pItem)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002628{
2629 QCBORItem Item;
2630 int nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002631
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002632 if((nCBORError = QCBORDecode_GetNext(pCtx, &Item))) return -1;
2633 if(Item.uDataType != uDataType) return -1;
2634 if(uNestingLevel > 0) {
Laurence Lundbladeee851742020-01-08 08:37:05 -08002635 if(Item.uLabelType != QCBOR_TYPE_INT64 &&
2636 Item.uLabelType != QCBOR_TYPE_UINT64) {
2637 return -1;
2638 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002639 if(Item.uLabelType == QCBOR_TYPE_INT64) {
2640 if(Item.label.int64 != nLabel) return -1;
2641 } else {
Laurence Lundblade570fab52018-10-13 18:28:27 +08002642 if(Item.label.uint64 != (uint64_t)nLabel) return -1;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002643 }
2644 }
2645 if(Item.uNestingLevel != uNestingLevel) return -1;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302646 if(Item.uNextNestLevel != uNextNest) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002647
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002648 if(pItem) {
2649 *pItem = Item;
2650 }
2651 return 0;
2652}
2653
2654
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002655// Same code checks definite and indefinite length versions of the map
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002656static int32_t CheckCSRMaps(QCBORDecodeContext *pDC)
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002657{
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302658 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 0, 1, 0, NULL)) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002659
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302660 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 1, 2, -23, NULL)) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002661
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302662 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 2, 3, -20, NULL)) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002663
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302664 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -18, NULL)) return -1;
2665 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -17, NULL)) return -1;
2666 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -15, NULL)) return -1;
2667 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -16, NULL)) return -1;
2668 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 2, -14, NULL)) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002669
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302670 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 2, 3, -19, NULL)) return -1;
2671 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 3, 4, -11, NULL)) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002672
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302673 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_INT64, 4, 3, -9, NULL)) return -1;
2674 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_BYTE_STRING, 3, 1, -10, 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, 1, 2, -22, NULL)) return -1;
2677 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_INT64, 2, 0, -5, NULL)) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002678
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002679 if(QCBORDecode_Finish(pDC)) return -2;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002680
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002681 return 0;
2682}
2683
2684
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002685/*
2686// cbor.me decoded output
2687{
2688 -23: {
2689 -20: {
2690 -18: "Organization",
2691 -17: "SSG",
2692 -15: "Confusion",
2693 -16: "San Diego",
2694 -14: "US"
2695 },
2696 -19: {
2697 -11: {
2698 -9: -7
2699 },
2700 -10: '\u0001\u0002\u0003\u0004\u0005\u0006\a\b\t\n'
2701 }
2702 },
2703 -22: {
2704 -5: -3
2705 }
2706}
2707 */
2708
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002709
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002710static uint8_t spCSRInput[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002711 0xa2, 0x36, 0xa2, 0x33, 0xa5, 0x31, 0x6c, 0x4f,
2712 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74,
2713 0x69, 0x6f, 0x6e, 0x30, 0x63, 0x53, 0x53, 0x47,
2714 0x2e, 0x69, 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73,
2715 0x69, 0x6f, 0x6e, 0x2f, 0x69, 0x53, 0x61, 0x6e,
2716 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f, 0x2d, 0x62,
2717 0x55, 0x53, 0x32, 0xa2, 0x2a, 0xa1, 0x28, 0x26,
2718 0x29, 0x4a, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
2719 0x07, 0x08, 0x09, 0x0a, 0x35, 0xa1, 0x24, 0x22};
2720
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002721int32_t NestedMapTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002722{
2723 QCBORDecodeContext DCtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002724
Laurence Lundbladeee851742020-01-08 08:37:05 -08002725 QCBORDecode_Init(&DCtx,
2726 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput),
2727 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002728
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002729 return CheckCSRMaps(&DCtx);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002730}
2731
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08002732
2733
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002734int32_t StringDecoderModeFailTest()
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08002735{
2736 QCBORDecodeContext DCtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002737
Laurence Lundbladeee851742020-01-08 08:37:05 -08002738 QCBORDecode_Init(&DCtx,
2739 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput),
2740 QCBOR_DECODE_MODE_MAP_STRINGS_ONLY);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002741
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08002742 QCBORItem Item;
2743 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002744
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08002745 if(QCBORDecode_GetNext(&DCtx, &Item)) {
2746 return -1;
2747 }
2748 if(Item.uDataType != QCBOR_TYPE_MAP) {
2749 return -2;
2750 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002751
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08002752 nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
2753 if(nCBORError != QCBOR_ERR_MAP_LABEL_TYPE) {
2754 return -3;
2755 }
2756
2757 return 0;
2758}
2759
2760
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002761// Same map as above, but using indefinite lengths
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002762static uint8_t spCSRInputIndefLen[] = {
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002763 0xbf, 0x36, 0xbf, 0x33, 0xbf, 0x31, 0x6c, 0x4f,
2764 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74,
2765 0x69, 0x6f, 0x6e, 0x30, 0x63, 0x53, 0x53, 0x47,
2766 0x2e, 0x69, 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73,
2767 0x69, 0x6f, 0x6e, 0x2f, 0x69, 0x53, 0x61, 0x6e,
2768 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f, 0x2d, 0x62,
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002769 0x55, 0x53, 0xff, 0x32, 0xbf, 0x2a, 0xbf, 0x28,
2770 0x26, 0xff, 0x29, 0x4a, 0x01, 0x02, 0x03, 0x04,
2771 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0xff, 0xff,
2772 0x35, 0xbf, 0x24, 0x22, 0xff, 0xff};
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002773
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002774int32_t NestedMapTestIndefLen()
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002775{
2776 QCBORDecodeContext DCtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002777
Laurence Lundbladeee851742020-01-08 08:37:05 -08002778 QCBORDecode_Init(&DCtx,
2779 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInputIndefLen),
2780 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002781
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002782 return CheckCSRMaps(&DCtx);
2783}
2784
2785
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002786
Laurence Lundblade17ede402018-10-13 11:43:07 +08002787static UsefulBufC make_nested_indefinite_arrays(int n, UsefulBuf Storage)
2788{
2789 UsefulOutBuf UOB;
2790 UsefulOutBuf_Init(&UOB, Storage);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002791
Laurence Lundblade17ede402018-10-13 11:43:07 +08002792 int i;
2793 for(i = 0; i < n; i++) {
2794 UsefulOutBuf_AppendByte(&UOB, 0x9f);
2795 }
2796
2797 for(i = 0; i < n; i++) {
2798 UsefulOutBuf_AppendByte(&UOB, 0xff);
2799 }
2800 return UsefulOutBuf_OutUBuf(&UOB);
2801}
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002802
2803
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002804static int32_t parse_indeflen_nested(UsefulBufC Nested, int nNestLevel)
Laurence Lundblade17ede402018-10-13 11:43:07 +08002805{
2806 QCBORDecodeContext DC;
2807 QCBORDecode_Init(&DC, Nested, 0);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002808
Laurence Lundblade17ede402018-10-13 11:43:07 +08002809 int j;
2810 for(j = 0; j < nNestLevel; j++) {
2811 QCBORItem Item;
2812 int nReturn = QCBORDecode_GetNext(&DC, &Item);
2813 if(j >= QCBOR_MAX_ARRAY_NESTING) {
2814 // Should be in error
2815 if(nReturn != QCBOR_ERR_ARRAY_NESTING_TOO_DEEP) {
2816 return -4;
2817 } else {
2818 return 0; // Decoding doesn't recover after an error
2819 }
2820 } else {
2821 // Should be no error
2822 if(nReturn) {
2823 return -9; // Should not have got an error
2824 }
2825 }
2826 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
2827 return -7;
2828 }
2829 }
2830 int nReturn = QCBORDecode_Finish(&DC);
2831 if(nReturn) {
2832 return -3;
2833 }
2834 return 0;
2835}
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002836
2837
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002838int32_t IndefiniteLengthNestTest()
Laurence Lundblade17ede402018-10-13 11:43:07 +08002839{
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05302840 UsefulBuf_MAKE_STACK_UB(Storage, 50);
Laurence Lundblade17ede402018-10-13 11:43:07 +08002841 int i;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002842 for(i=1; i < QCBOR_MAX_ARRAY_NESTING+4; i++) {
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08002843 const UsefulBufC Nested = make_nested_indefinite_arrays(i, Storage);
Laurence Lundblade17ede402018-10-13 11:43:07 +08002844 int nReturn = parse_indeflen_nested(Nested, i);
2845 if(nReturn) {
2846 return nReturn;
2847 }
2848 }
2849 return 0;
2850}
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002851
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002852
Laurence Lundbladeee851742020-01-08 08:37:05 -08002853// [1, [2, 3]]
2854static const uint8_t spIndefiniteArray[] = {0x9f, 0x01, 0x82, 0x02, 0x03, 0xff};
2855// No closing break
2856static const uint8_t spIndefiniteArrayBad1[] = {0x9f};
2857// Not enough closing breaks
2858static const uint8_t spIndefiniteArrayBad2[] = {0x9f, 0x9f, 0x02, 0xff};
2859// Too many closing breaks
2860static const uint8_t spIndefiniteArrayBad3[] = {0x9f, 0x02, 0xff, 0xff};
2861// Unclosed indeflen inside def len
2862static const uint8_t spIndefiniteArrayBad4[] = {0x81, 0x9f};
2863// confused tag
2864static const uint8_t spIndefiniteArrayBad5[] = {0x9f, 0xd1, 0xff};
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002865
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002866int32_t IndefiniteLengthArrayMapTest()
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002867{
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002868 int nResult;
2869 // --- first test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002870 UsefulBufC IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArray);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002871
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002872 // Decode it and see if it is OK
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05302873 UsefulBuf_MAKE_STACK_UB(MemPool, 150);
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002874 QCBORDecodeContext DC;
2875 QCBORItem Item;
2876 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002877
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002878 QCBORDecode_SetMemPool(&DC, MemPool, false);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002879
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002880 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05302881
2882 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
2883 Item.uNestingLevel != 0 ||
2884 Item.uNextNestLevel != 1) {
2885 return -111;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002886 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002887
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002888 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05302889 if(Item.uDataType != QCBOR_TYPE_INT64 ||
2890 Item.uNestingLevel != 1 ||
2891 Item.uNextNestLevel != 1) {
2892 return -2;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002893 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002894
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002895 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05302896 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
2897 Item.uNestingLevel != 1 ||
2898 Item.uNextNestLevel != 2) {
2899 return -3;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002900 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002901
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002902 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade12b495d2018-12-17 11:15:54 -08002903 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade6de37062018-10-15 12:22:42 +05302904 Item.uNestingLevel != 2 ||
2905 Item.uNextNestLevel != 2) {
2906 return -4;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002907 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002908
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002909 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade12b495d2018-12-17 11:15:54 -08002910 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade6de37062018-10-15 12:22:42 +05302911 Item.uNestingLevel != 2 ||
2912 Item.uNextNestLevel != 0) {
2913 return -5;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002914 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002915
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002916 if(QCBORDecode_Finish(&DC)) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05302917 return -6;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002918 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002919
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002920 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002921 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad1);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002922
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002923 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002924
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002925 QCBORDecode_SetMemPool(&DC, MemPool, false);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002926
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002927 nResult = QCBORDecode_GetNext(&DC, &Item);
2928 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05302929 return -7;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002930 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002931
Laurence Lundblade570fab52018-10-13 18:28:27 +08002932 nResult = QCBORDecode_Finish(&DC);
Laurence Lundblade6de37062018-10-15 12:22:42 +05302933 if(nResult != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
2934 return -8;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002935 }
2936
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002937
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002938 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002939 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad2);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002940
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002941 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002942
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002943 QCBORDecode_SetMemPool(&DC, MemPool, false);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002944
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002945 nResult = QCBORDecode_GetNext(&DC, &Item);
2946 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05302947 return -9;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002948 }
2949
2950 nResult = QCBORDecode_GetNext(&DC, &Item);
2951 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05302952 return -10;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002953 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002954
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002955 nResult = QCBORDecode_GetNext(&DC, &Item);
2956 if(nResult || Item.uDataType != QCBOR_TYPE_INT64) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05302957 return -11;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002958 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002959
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002960 nResult = QCBORDecode_Finish(&DC);
Laurence Lundblade6de37062018-10-15 12:22:42 +05302961 if(nResult != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
2962 return -12;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002963 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002964
2965
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002966 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002967 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad3);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002968
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002969 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002970
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002971 QCBORDecode_SetMemPool(&DC, MemPool, false);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002972
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002973 nResult = QCBORDecode_GetNext(&DC, &Item);
2974 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05302975 return -13;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002976 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002977
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002978 nResult = QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05302979 if(nResult != QCBOR_ERR_BAD_BREAK) {
2980 return -14;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002981 }
Laurence Lundblade6de37062018-10-15 12:22:42 +05302982
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002983
Laurence Lundblade570fab52018-10-13 18:28:27 +08002984 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002985 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad4);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002986
Laurence Lundblade570fab52018-10-13 18:28:27 +08002987 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002988
Laurence Lundblade570fab52018-10-13 18:28:27 +08002989 QCBORDecode_SetMemPool(&DC, MemPool, false);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002990
Laurence Lundblade570fab52018-10-13 18:28:27 +08002991 nResult = QCBORDecode_GetNext(&DC, &Item);
2992 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05302993 return -15;
Laurence Lundblade570fab52018-10-13 18:28:27 +08002994 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002995
Laurence Lundblade570fab52018-10-13 18:28:27 +08002996 nResult = QCBORDecode_GetNext(&DC, &Item);
2997 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05302998 return -16;
Laurence Lundblade570fab52018-10-13 18:28:27 +08002999 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003000
Laurence Lundblade570fab52018-10-13 18:28:27 +08003001 nResult = QCBORDecode_Finish(&DC);
Laurence Lundblade6de37062018-10-15 12:22:42 +05303002 if(nResult != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
3003 return -17;
Laurence Lundblade570fab52018-10-13 18:28:27 +08003004 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003005
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303006 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003007 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad5);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003008
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303009 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003010
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303011 QCBORDecode_SetMemPool(&DC, MemPool, false);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003012
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303013 nResult = QCBORDecode_GetNext(&DC, &Item);
3014 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303015 return -18;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303016 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003017
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303018 nResult = QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05303019 if(nResult != QCBOR_ERR_BAD_BREAK) {
3020 return -19;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303021 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003022
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003023 return 0;
3024}
3025
Laurence Lundblade17ede402018-10-13 11:43:07 +08003026
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003027static const uint8_t spIndefiniteLenString[] = {
Laurence Lundblade17ede402018-10-13 11:43:07 +08003028 0x81, // Array of length one
3029 0x7f, // text string marked with indefinite length
3030 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment
3031 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment
3032 0xff // ending break
3033};
3034
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003035static const uint8_t spIndefiniteLenStringBad2[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303036 0x81, // Array of length one
3037 0x7f, // text string marked with indefinite length
3038 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment
3039 0x44, 0x6d, 0x69, 0x6e, 0x67, // second segment of wrong type
3040 0xff // ending break
3041};
3042
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003043static const uint8_t spIndefiniteLenStringBad3[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303044 0x81, // Array of length one
3045 0x7f, // text string marked with indefinite length
3046 0x01, 0x02, // Not a string
3047 0xff // ending break
3048};
3049
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003050static const uint8_t spIndefiniteLenStringBad4[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303051 0x81, // Array of length one
3052 0x7f, // text string marked with indefinite length
3053 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment
3054 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment
3055 // missing end of string
3056};
3057
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003058static const uint8_t spIndefiniteLenStringLabel[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303059 0xa1, // Array of length one
3060 0x7f, // text string marked with indefinite length
3061 0x65, 0x73, 0x74, 0x72, 0x75, 0x75, // first segment
3062 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment
3063 0xff, // ending break
3064 0x01 // integer being labeled.
3065};
3066
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003067/**
3068 Make an indefinite length string
3069
3070 @param Storage Storage for string, must be 144 bytes in size
3071 @return The indefinite length string
3072
3073 This makes an array with one indefinite length string that has 7 chunks
3074 from size of 1 byte up to 64 bytes.
3075 */
3076static UsefulBufC MakeIndefiniteBigBstr(UsefulBuf Storage)
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303077{
3078 UsefulOutBuf UOB;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003079
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303080 UsefulOutBuf_Init(&UOB, Storage);
3081 UsefulOutBuf_AppendByte(&UOB, 0x81);
3082 UsefulOutBuf_AppendByte(&UOB, 0x5f);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003083
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003084 uint8_t uStringByte = 0;
3085 // Use of type int is intentional
3086 for(int uChunkSize = 1; uChunkSize <= 128; uChunkSize *= 2) {
3087 // Not using preferred encoding here, but that is OK.
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303088 UsefulOutBuf_AppendByte(&UOB, 0x58);
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003089 UsefulOutBuf_AppendByte(&UOB, (uint8_t)uChunkSize);
3090 for(int j = 0; j < uChunkSize; j++) {
3091 UsefulOutBuf_AppendByte(&UOB, uStringByte);
3092 uStringByte++;
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303093 }
3094 }
3095 UsefulOutBuf_AppendByte(&UOB, 0xff);
3096
3097 return UsefulOutBuf_OutUBuf(&UOB);
3098}
3099
3100static int CheckBigString(UsefulBufC BigString)
3101{
3102 if(BigString.len != 255) {
3103 return 1;
3104 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003105
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303106 for(uint8_t i = 0; i < 255; i++){
3107 if(((const uint8_t *)BigString.ptr)[i] != i) {
3108 return 1;
3109 }
3110 }
3111 return 0;
3112}
3113
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05303114
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003115int32_t IndefiniteLengthStringTest()
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303116{
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303117 QCBORDecodeContext DC;
3118 QCBORItem Item;
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05303119 // big enough for MakeIndefiniteBigBstr() + MemPool overhead
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003120 UsefulBuf_MAKE_STACK_UB(MemPool, 350);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003121
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303122 // --- Simple normal indefinite length string ------
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003123 UsefulBufC IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenString);
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303124 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003125
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05303126 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303127 return -1;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303128 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003129
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303130 if(QCBORDecode_GetNext(&DC, &Item)) {
3131 return -2;
3132 }
3133 if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.uDataAlloc) {
3134 return -3;
3135 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003136
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303137 if(QCBORDecode_GetNext(&DC, &Item)) {
3138 return -4;
3139 }
3140 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || !Item.uDataAlloc) {
3141 return -5;
3142 }
3143 if(QCBORDecode_Finish(&DC)) {
3144 return -6;
3145 }
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303146
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303147 // ----- types mismatch ---
Laurence Lundbladeee851742020-01-08 08:37:05 -08003148 QCBORDecode_Init(&DC,
3149 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad2),
3150 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003151
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303152 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
3153 return -7;
3154 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003155
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303156 if(QCBORDecode_GetNext(&DC, &Item)) {
3157 return -8;
3158 }
3159 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
3160 return -9;
3161 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003162
Laurence Lundblade30816f22018-11-10 13:40:22 +07003163 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_INDEFINITE_STRING_CHUNK) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303164 return -10;
3165 }
3166
3167 // ----- not a string ---
Laurence Lundbladeee851742020-01-08 08:37:05 -08003168 QCBORDecode_Init(&DC,
3169 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad3),
3170 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003171
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303172 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
3173 return -11;
3174 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003175
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303176 if(QCBORDecode_GetNext(&DC, &Item)) {
3177 return -12;
3178 }
3179 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
3180 return -13;
3181 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003182
Laurence Lundblade30816f22018-11-10 13:40:22 +07003183 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_INDEFINITE_STRING_CHUNK) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303184 return -14;
3185 }
3186
3187 // ----- no end -----
Laurence Lundbladeee851742020-01-08 08:37:05 -08003188 QCBORDecode_Init(&DC,
3189 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad4),
3190 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003191
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303192 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
3193 return -15;
3194 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003195
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303196 if(QCBORDecode_GetNext(&DC, &Item)) {
3197 return -16;
3198 }
3199 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
3200 return -17;
3201 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003202
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303203 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_HIT_END) {
3204 return -18;
3205 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003206
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303207 // ------ Don't set a string allocator and see an error -----
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303208 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003209
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303210 QCBORDecode_GetNext(&DC, &Item);
3211 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303212 return -19;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303213 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003214
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303215 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_NO_STRING_ALLOCATOR) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303216 return -20;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303217 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003218
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303219 // ----- Mempool is way too small -----
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003220 UsefulBuf_MAKE_STACK_UB(MemPoolTooSmall, QCBOR_DECODE_MIN_MEM_POOL_SIZE-1);
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303221
3222 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
3223 if(!QCBORDecode_SetMemPool(&DC, MemPoolTooSmall, false)) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303224 return -21;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303225 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003226
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303227 // ----- Mempool is way too small -----
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05303228 UsefulBuf_MAKE_STACK_UB(BigIndefBStrStorage, 290);
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08003229 const UsefulBufC BigIndefBStr = MakeIndefiniteBigBstr(BigIndefBStrStorage);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003230
Laurence Lundbladeee851742020-01-08 08:37:05 -08003231 // 80 is big enough for MemPool overhead, but not BigIndefBStr
3232 UsefulBuf_MAKE_STACK_UB(MemPoolSmall, 80);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003233
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303234 QCBORDecode_Init(&DC, BigIndefBStr, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303235 if(QCBORDecode_SetMemPool(&DC, MemPoolSmall, false)) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303236 return -22;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303237 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003238
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303239 QCBORDecode_GetNext(&DC, &Item);
3240 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303241 return -23;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303242 }
Laurence Lundblade30816f22018-11-10 13:40:22 +07003243 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_STRING_ALLOCATE) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303244 return -24;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303245 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003246
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303247 // ---- big bstr -----
3248 QCBORDecode_Init(&DC, BigIndefBStr, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003249
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303250 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
3251 return -25;
3252 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003253
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05303254 if(QCBORDecode_GetNext(&DC, &Item)) {
3255 return -26;
3256 }
3257 if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.uDataAlloc) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303258 return -26;
3259 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003260
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303261 if(QCBORDecode_GetNext(&DC, &Item)) {
3262 return -27;
3263 }
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05303264 if(Item.uDataType != QCBOR_TYPE_BYTE_STRING || !Item.uDataAlloc || Item.uNestingLevel != 1) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303265 return -28;
3266 }
3267 if(CheckBigString(Item.val.string)) {
3268 return -3;
3269 }
3270 if(QCBORDecode_Finish(&DC)) {
3271 return -29;
3272 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003273
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303274 // --- label is an indefinite length string ------
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003275 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringLabel), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003276
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303277 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
3278 return -30;
3279 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003280
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303281 QCBORDecode_GetNext(&DC, &Item);
3282 if(Item.uDataType != QCBOR_TYPE_MAP) {
3283 return -31;
3284 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003285
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303286 if(QCBORDecode_GetNext(&DC, &Item)){
3287 return -32;
3288 }
Laurence Lundbladeee851742020-01-08 08:37:05 -08003289 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
3290 Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303291 Item.uDataAlloc || !Item.uLabelAlloc ||
3292 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("struuming"))) {
3293 return -33;
3294 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003295
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303296 if(QCBORDecode_Finish(&DC)) {
3297 return -34;
3298 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003299
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003300 return 0;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003301}
3302
3303
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003304int32_t AllocAllStringsTest()
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303305{
3306 QCBORDecodeContext DC;
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003307 QCBORError nCBORError;
3308
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003309
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303310 // First test, use the "CSRMap" as easy input and checking
Laurence Lundbladeee851742020-01-08 08:37:05 -08003311 QCBORDecode_Init(&DC,
3312 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput),
3313 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003314
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003315 UsefulBuf_MAKE_STACK_UB(Pool, sizeof(spCSRInput) + QCBOR_DECODE_MIN_MEM_POOL_SIZE);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003316
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003317 nCBORError = QCBORDecode_SetMemPool(&DC, Pool, 1); // Turn on copying.
3318 if(nCBORError) {
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303319 return -1;
3320 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003321
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003322 if(CheckCSRMaps(&DC)) {
3323 return -2;
3324 }
3325
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303326 // Next parse, save pointers to a few strings, destroy original and see all is OK.
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003327 UsefulBuf_MAKE_STACK_UB(CopyOfStorage, sizeof(pValidMapEncoded) + QCBOR_DECODE_MIN_MEM_POOL_SIZE);
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08003328 const UsefulBufC CopyOf = UsefulBuf_Copy(CopyOfStorage, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded));
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003329
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303330 QCBORDecode_Init(&DC, CopyOf, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08003331 UsefulBuf_Set(Pool, '/');
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303332 QCBORDecode_SetMemPool(&DC, Pool, 1); // Turn on copying.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003333
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303334 QCBORItem Item1, Item2, Item3, Item4;
3335 if((nCBORError = QCBORDecode_GetNext(&DC, &Item1)))
3336 return nCBORError;
3337 if(Item1.uDataType != QCBOR_TYPE_MAP ||
3338 Item1.val.uCount != 3)
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003339 return -3;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303340 if((nCBORError = QCBORDecode_GetNext(&DC, &Item1)))
3341 return nCBORError;
3342 if((nCBORError = QCBORDecode_GetNext(&DC, &Item2)))
3343 return nCBORError;
3344 if((nCBORError = QCBORDecode_GetNext(&DC, &Item3)))
3345 return nCBORError;
3346 if((nCBORError = QCBORDecode_GetNext(&DC, &Item4)))
3347 return nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003348
Laurence Lundblade05ec57b2018-10-21 01:50:03 +05303349 UsefulBuf_Set(CopyOfStorage, '_');
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003350
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303351 if(Item1.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303352 Item1.uDataType != QCBOR_TYPE_INT64 ||
3353 Item1.val.int64 != 42 ||
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003354 Item1.uDataAlloc != 0 ||
3355 Item1.uLabelAlloc == 0 ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09003356 UsefulBuf_Compare(Item1.label.string, UsefulBuf_FromSZ("first integer"))) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003357 return -4;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09003358 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003359
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303360
3361 if(Item2.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09003362 UsefulBuf_Compare(Item2.label.string, UsefulBuf_FromSZ("an array of two strings")) ||
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303363 Item2.uDataType != QCBOR_TYPE_ARRAY ||
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003364 Item2.uDataAlloc != 0 ||
3365 Item2.uLabelAlloc == 0 ||
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303366 Item2.val.uCount != 2)
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003367 return -5;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003368
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303369 if(Item3.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003370 Item3.uDataAlloc == 0 ||
3371 Item3.uLabelAlloc != 0 ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09003372 UsefulBuf_Compare(Item3.val.string, UsefulBuf_FromSZ("string1"))) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003373 return -6;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09003374 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003375
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303376 if(Item4.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003377 Item4.uDataAlloc == 0 ||
3378 Item4.uLabelAlloc != 0 ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09003379 UsefulBuf_Compare(Item4.val.string, UsefulBuf_FromSZ("string2"))) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003380 return -7;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09003381 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003382
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303383 // Next parse with a pool that is too small
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003384 UsefulBuf_MAKE_STACK_UB(SmallPool, QCBOR_DECODE_MIN_MEM_POOL_SIZE + 1);
Laurence Lundbladeee851742020-01-08 08:37:05 -08003385 QCBORDecode_Init(&DC,
3386 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded),
3387 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303388 QCBORDecode_SetMemPool(&DC, SmallPool, 1); // Turn on copying.
3389 if((nCBORError = QCBORDecode_GetNext(&DC, &Item1)))
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003390 return -8;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303391 if(Item1.uDataType != QCBOR_TYPE_MAP ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09003392 Item1.val.uCount != 3) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003393 return -9;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09003394 }
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303395 if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item1))){
3396 if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item2))) {
3397 if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item3))) {
3398 nCBORError = QCBORDecode_GetNext(&DC, &Item4);
3399 }
3400 }
3401 }
Laurence Lundblade30816f22018-11-10 13:40:22 +07003402 if(nCBORError != QCBOR_ERR_STRING_ALLOCATE) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003403 return -10;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303404 }
3405
3406 return 0;
3407}
3408
Laurence Lundbladef6531662018-12-04 10:42:22 +09003409
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303410
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003411int32_t MemPoolTest(void)
Laurence Lundblade0155b622018-10-12 20:04:37 +08003412{
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003413 // Set up the decoder with a tiny bit of CBOR to parse because
3414 // nothing can be done with it unless that is set up.
Laurence Lundbladef6531662018-12-04 10:42:22 +09003415 QCBORDecodeContext DC;
3416 const uint8_t pMinimalCBOR[] = {0xa0}; // One empty map
3417 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pMinimalCBOR),0);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003418
Laurence Lundbladef6531662018-12-04 10:42:22 +09003419 // Set up an memory pool of 100 bytes
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003420 // Then fish into the internals of the decode context
3421 // to get the allocator function so it can be called directly.
3422 // Also figure out how much pool is available for use
3423 // buy subtracting out the overhead.
Laurence Lundbladef6531662018-12-04 10:42:22 +09003424 UsefulBuf_MAKE_STACK_UB(Pool, 100);
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003425 QCBORError nError = QCBORDecode_SetMemPool(&DC, Pool, 0);
3426 if(nError) {
3427 return -9;
3428 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003429 QCBORStringAllocate pAlloc = DC.StringAllocator.pfAllocator;
3430 void *pAllocCtx = DC.StringAllocator.pAllocateCxt;
3431 size_t uAvailPool = Pool.len - QCBOR_DECODE_MIN_MEM_POOL_SIZE;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003432
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003433 // First test -- ask for one more byte than available and see failure
3434 UsefulBuf Allocated = (*pAlloc)(pAllocCtx, NULL, uAvailPool+1);
Laurence Lundbladef6531662018-12-04 10:42:22 +09003435 if(!UsefulBuf_IsNULL(Allocated)) {
3436 return -1;
3437 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003438
Laurence Lundbladef6531662018-12-04 10:42:22 +09003439 // Re do the set up for the next test that will do a successful alloc,
3440 // a fail, a free and then success
Laurence Lundbladef6531662018-12-04 10:42:22 +09003441 QCBORDecode_SetMemPool(&DC, Pool, 0);
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003442 pAlloc = DC.StringAllocator.pfAllocator;
3443 pAllocCtx = DC.StringAllocator.pAllocateCxt;
3444 uAvailPool = Pool.len - QCBOR_DECODE_MIN_MEM_POOL_SIZE;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003445
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003446 // Allocate one byte less than available and see success
3447 Allocated = (pAlloc)(pAllocCtx, NULL, uAvailPool-1);
Laurence Lundbladef6531662018-12-04 10:42:22 +09003448 if(UsefulBuf_IsNULL(Allocated)) { // expected to succeed
3449 return -2;
3450 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003451 // Ask for some more and see failure
3452 UsefulBuf Allocated2 = (*pAlloc)(pAllocCtx, NULL, uAvailPool/2);
Laurence Lundbladef6531662018-12-04 10:42:22 +09003453 if(!UsefulBuf_IsNULL(Allocated2)) { // expected to fail
3454 return -3;
3455 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003456 // Free the first allocate, retry the second and see success
3457 (*pAlloc)(pAllocCtx, Allocated.ptr, 0); // Free
3458 Allocated = (*pAlloc)(pAllocCtx, NULL, uAvailPool/2);
Laurence Lundbladef6531662018-12-04 10:42:22 +09003459 if(UsefulBuf_IsNULL(Allocated)) { // succeed because of the free
3460 return -4;
3461 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003462
Laurence Lundbladef6531662018-12-04 10:42:22 +09003463 // Re do set up for next test that involves a successful alloc,
3464 // and a successful realloc and a failed realloc
3465 QCBORDecode_SetMemPool(&DC, Pool, 0);
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003466 pAlloc = DC.StringAllocator.pfAllocator;
3467 pAllocCtx = DC.StringAllocator.pAllocateCxt;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003468
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003469 // Allocate half the pool and see success
3470 Allocated = (*pAlloc)(pAllocCtx, NULL, uAvailPool/2);
Laurence Lundbladef6531662018-12-04 10:42:22 +09003471 if(UsefulBuf_IsNULL(Allocated)) { // expected to succeed
3472 return -5;
3473 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003474 // Reallocate to take up the whole pool and see success
3475 Allocated2 = (*pAlloc)(pAllocCtx, Allocated.ptr, uAvailPool);
Laurence Lundbladef6531662018-12-04 10:42:22 +09003476 if(UsefulBuf_IsNULL(Allocated2)) {
3477 return -6;
3478 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003479 // Make sure its the same pointer and the size is right
Laurence Lundbladef6531662018-12-04 10:42:22 +09003480 if(Allocated2.ptr != Allocated.ptr || Allocated2.len != uAvailPool) {
3481 return -7;
3482 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003483 // Try to allocate more to be sure there is failure after a realloc
3484 UsefulBuf Allocated3 = (*pAlloc)(pAllocCtx, Allocated.ptr, uAvailPool+1);
3485 if(!UsefulBuf_IsNULL(Allocated3)) {
Laurence Lundbladef6531662018-12-04 10:42:22 +09003486 return -8;
3487 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003488
Laurence Lundbladef6531662018-12-04 10:42:22 +09003489 return 0;
3490}
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003491
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003492
3493/* Just enough of an allocator to test configuration of one */
3494static UsefulBuf AllocateTestFunction(void *pCtx, void *pOldMem, size_t uNewSize)
3495{
3496 (void)pOldMem; // unused variable
3497
3498 if(uNewSize) {
3499 // Assumes the context pointer is the buffer and
3500 // nothing too big will ever be asked for.
3501 // This is only good for this basic test!
3502 return (UsefulBuf) {pCtx, uNewSize};
3503 } else {
3504 return NULLUsefulBuf;
3505 }
3506}
3507
3508
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003509int32_t SetUpAllocatorTest(void)
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003510{
3511 // Set up the decoder with a tiny bit of CBOR to parse because
3512 // nothing can be done with it unless that is set up.
3513 QCBORDecodeContext DC;
3514 const uint8_t pMinimalCBOR[] = {0x62, 0x48, 0x69}; // "Hi"
3515 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pMinimalCBOR),0);
3516
3517 uint8_t pAllocatorBuffer[50];
3518
3519 // This is really just to test that this call works.
3520 // The full functionality of string allocators is tested
3521 // elsewhere with the MemPool internal allocator.
3522 QCBORDecode_SetUpAllocator(&DC, AllocateTestFunction, pAllocatorBuffer, 1);
3523
3524 QCBORItem Item;
3525 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_SUCCESS) {
3526 return -1;
3527 }
3528
3529 if(Item.uDataAlloc == 0 ||
3530 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
3531 Item.val.string.ptr != pAllocatorBuffer) {
3532 return -2;
3533 }
3534
3535 if(QCBORDecode_Finish(&DC) != QCBOR_SUCCESS) {
3536 return -3;
3537 }
3538
3539 return 0;
3540}
3541
Laurence Lundblade59289e52019-12-30 13:44:37 -08003542
3543#ifndef QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA
3544/*
3545 [
3546 4([-1, 3]),
3547 4([-20, 4759477275222530853136]),
3548 4([9223372036854775807, -4759477275222530853137]),
3549 5([300, 100]),
3550 5([-20, 4759477275222530853136]),
3551 5([-9223372036854775807, -4759477275222530853137])
3552 5([9223372036854775806, -4759477275222530853137])
3553 5([9223372036854775806, 9223372036854775806])]
3554 ]
3555 */
3556
3557static const uint8_t spExpectedExponentsAndMantissas[] = {
3558 0x87,
3559 0xC4, 0x82, 0x20,
3560 0x03,
3561 0xC4, 0x82, 0x33,
3562 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,
3563 0xC4, 0x82, 0x1B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
3564 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,
3565 0xC5, 0x82, 0x19, 0x01, 0x2C,
3566 0x18, 0x64,
3567 0xC5, 0x82, 0x33,
3568 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,
3569 0xC5, 0x82, 0x3B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE,
3570 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,
3571 0xC5, 0x82, 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE,
3572 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE
3573};
3574
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003575int32_t ExponentAndMantissaDecodeTests(void)
Laurence Lundblade59289e52019-12-30 13:44:37 -08003576{
3577 QCBORDecodeContext DC;
3578 QCBORError nCBORError;
3579 QCBORItem item;
3580
Laurence Lundblade17af4902020-01-07 19:11:55 -08003581 static const uint8_t spBigNumMantissa[] = {0x01, 0x02, 0x03, 0x04, 0x05,
3582 0x06, 0x07, 0x08, 0x09, 0x010};
3583 UsefulBufC BN = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNumMantissa);
Laurence Lundblade59289e52019-12-30 13:44:37 -08003584
3585
3586 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedExponentsAndMantissas), QCBOR_DECODE_MODE_NORMAL);
3587
3588 nCBORError = QCBORDecode_GetNext(&DC, &item);
3589 if(nCBORError != QCBOR_SUCCESS) {
3590 return 1;
3591 }
3592
3593 if(item.uDataType != QCBOR_TYPE_ARRAY) {
3594 return 2;
3595 }
3596
3597 nCBORError = QCBORDecode_GetNext(&DC, &item);
3598 if(nCBORError != QCBOR_SUCCESS) {
3599 return 3;
3600 }
3601
3602 if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION ||
3603 item.val.expAndMantissa.Mantissa.nInt != 3 ||
3604 item.val.expAndMantissa.nExponent != -1) {
3605 return 4;
3606 }
3607
3608 nCBORError = QCBORDecode_GetNext(&DC, &item);
3609 if(nCBORError != QCBOR_SUCCESS) {
3610 return 5;
3611 }
3612
3613 if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM ||
3614 item.val.expAndMantissa.nExponent != -20 ||
3615 UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) {
3616 return 6;
3617 }
3618
3619 nCBORError = QCBORDecode_GetNext(&DC, &item);
3620 if(nCBORError != QCBOR_SUCCESS) {
3621 return 7;
3622 }
3623
3624 if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM ||
3625 item.val.expAndMantissa.nExponent != 9223372036854775807 ||
3626 UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) {
3627 return 8;
3628 }
3629
3630 nCBORError = QCBORDecode_GetNext(&DC, &item);
3631 if(nCBORError != QCBOR_SUCCESS) {
3632 return 9;
3633 }
3634
3635 if(item.uDataType != QCBOR_TYPE_BIGFLOAT ||
3636 item.val.expAndMantissa.Mantissa.nInt != 100 ||
3637 item.val.expAndMantissa.nExponent != 300) {
3638 return 10;
3639 }
3640
3641 nCBORError = QCBORDecode_GetNext(&DC, &item);
3642 if(nCBORError != QCBOR_SUCCESS) {
3643 return 11;
3644 }
3645
3646 if(item.uDataType != QCBOR_TYPE_BIGFLOAT_POS_BIGNUM ||
3647 item.val.expAndMantissa.nExponent != -20 ||
3648 UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) {
3649 return 12;
3650 }
3651
3652 nCBORError = QCBORDecode_GetNext(&DC, &item);
3653 if(nCBORError != QCBOR_SUCCESS) {
3654 return 13;
3655 }
3656
3657 if(item.uDataType != QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM ||
3658 item.val.expAndMantissa.nExponent != -9223372036854775807 ||
3659 UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) {
3660 return 14;
3661 }
3662
3663 nCBORError = QCBORDecode_GetNext(&DC, &item);
3664 if(nCBORError != QCBOR_SUCCESS) {
3665 return 15;
3666 }
3667
3668 if(item.uDataType != QCBOR_TYPE_BIGFLOAT ||
3669 item.val.expAndMantissa.nExponent != 9223372036854775806 ||
3670 item.val.expAndMantissa.Mantissa.nInt!= 9223372036854775806 ) {
3671 return 16;
3672 }
3673
3674 /* Now encode some stuff and then decode it */
3675 uint8_t pBuf[40];
3676 QCBOREncodeContext EC;
3677 UsefulBufC Encoded;
3678
3679 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(pBuf));
3680 QCBOREncode_OpenArray(&EC);
3681 QCBOREncode_AddDecimalFraction(&EC, 999, 1000); // 999 * (10 ^ 1000)
3682 QCBOREncode_AddBigFloat(&EC, 100, INT32_MIN);
3683 QCBOREncode_AddDecimalFractionBigNum(&EC, BN, false, INT32_MAX);
3684 QCBOREncode_CloseArray(&EC);
3685 QCBOREncode_Finish(&EC, &Encoded);
3686
3687
3688 QCBORDecode_Init(&DC, Encoded, QCBOR_DECODE_MODE_NORMAL);
3689 nCBORError = QCBORDecode_GetNext(&DC, &item);
3690 if(nCBORError != QCBOR_SUCCESS) {
3691 return 13;
3692 }
3693
3694 nCBORError = QCBORDecode_GetNext(&DC, &item);
3695 if(nCBORError != QCBOR_SUCCESS) {
3696 return 13;
3697 }
3698
3699 if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION ||
3700 item.val.expAndMantissa.nExponent != 1000 ||
3701 item.val.expAndMantissa.Mantissa.nInt != 999) {
3702 return 15;
3703 }
3704
3705 nCBORError = QCBORDecode_GetNext(&DC, &item);
3706 if(nCBORError != QCBOR_SUCCESS) {
3707 return 13;
3708 }
3709
3710 if(item.uDataType != QCBOR_TYPE_BIGFLOAT ||
3711 item.val.expAndMantissa.nExponent != INT32_MIN ||
3712 item.val.expAndMantissa.Mantissa.nInt != 100) {
3713 return 15;
3714 }
3715
3716 nCBORError = QCBORDecode_GetNext(&DC, &item);
3717 if(nCBORError != QCBOR_SUCCESS) {
3718 return 13;
3719 }
3720
3721 if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM ||
3722 item.val.expAndMantissa.nExponent != INT32_MAX ||
3723 UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) {
3724 return 12;
3725 }
3726
3727 return 0;
3728}
3729
3730
3731static struct FailInput ExponentAndMantissaFailures[] = {
3732 // Exponent > INT64_MAX
3733 { {(uint8_t[]){0xC4, 0x82, 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
3734 0xFF, 0xFF, 0x1B, 0x80, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
3735 0xFF, 0xFF,}, 20}, QCBOR_ERR_BAD_EXP_AND_MANTISSA},
3736 // Mantissa > INT64_MAX
3737 { {(uint8_t[]){0xC4, 0x82, 0x1B, 0x80, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
3738 0xFF, 0xFF, 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05,
3739 0x06, 0x07, 0x08, 0x09, 0x10}, 23}, QCBOR_ERR_BAD_EXP_AND_MANTISSA},
3740 // End of input
3741 { {(uint8_t[]){0xC4, 0x82}, 2}, QCBOR_ERR_HIT_END},
3742 // End of input
3743 { {(uint8_t[]){0xC4, 0x82, 0x01}, 3}, QCBOR_ERR_HIT_END},
3744 // bad content for big num
3745 { {(uint8_t[]){0xC4, 0x82, 0x01, 0xc3, 0x01}, 5}, QCBOR_ERR_BAD_OPT_TAG},
3746 // bad content for big num
3747 { {(uint8_t[]){0xC4, 0x82, 0xc2, 0x01, 0x1f}, 5}, QCBOR_ERR_BAD_INT},
3748 // Bad integer for exponent
3749 { {(uint8_t[]){0xC4, 0x82, 0x01, 0x1f}, 4}, QCBOR_ERR_BAD_INT},
3750 // Bad integer for mantissa
3751 { {(uint8_t[]){0xC4, 0x82, 0x1f, 0x01}, 4}, QCBOR_ERR_BAD_INT},
3752 // 3 items in array
3753 { {(uint8_t[]){0xC4, 0x83, 0x03, 0x01, 02}, 5}, QCBOR_ERR_BAD_EXP_AND_MANTISSA},
3754 // unterminated indefinite length array
3755 { {(uint8_t[]){0xC4, 0x9f, 0x03, 0x01, 0x02}, 5}, QCBOR_ERR_BAD_EXP_AND_MANTISSA},
3756 // Empty array
3757 { {(uint8_t[]){0xC4, 0x80}, 2}, QCBOR_ERR_NO_MORE_ITEMS},
3758 // Second is not an integer
3759 { {(uint8_t[]){0xC4, 0x82, 0x03, 0x40}, 4}, QCBOR_ERR_BAD_EXP_AND_MANTISSA},
3760 // First is not an integer
3761 { {(uint8_t[]){0xC4, 0x82, 0x40}, 3}, QCBOR_ERR_BAD_EXP_AND_MANTISSA},
3762 // Not an array
3763 { {(uint8_t[]){0xC4, 0xa2}, 2}, QCBOR_ERR_BAD_EXP_AND_MANTISSA}
3764};
3765
3766
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003767int32_t ExponentAndMantissaDecodeFailTests()
Laurence Lundblade59289e52019-12-30 13:44:37 -08003768{
3769 return ProcessFailures(ExponentAndMantissaFailures,
3770 sizeof(ExponentAndMantissaFailures)/sizeof(struct FailInput));
3771}
3772
3773#endif /* QCBOR_CONFIG_DISABLE_EXP_AND_MANTISSA */