blob: 3c17d0afc5a3e74fc501f0727b00a828520c84c6 [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 Lundbladef0ea5f32019-01-11 20:10:26 -08003 Copyright (c) 2018-2019, 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 Lundblade9e3651c2018-10-10 11:49:55 +080031 ==============================================================================*/
32
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080033#include "qcbor_decode_tests.h"
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -080034#include "qcbor.h"
Laurence Lundbladed4728fd2018-12-17 15:15:56 -080035#include <string.h>
Laurence Lundblade9e3651c2018-10-10 11:49:55 +080036#include <math.h> // for fabs()
Laurence Lundbladebb1062e2019-08-12 23:28:54 -070037#include "not_well_formed_cbor.h"
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -080038
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080039
Laurence Lundbladea2e29072018-12-30 09:20:06 -080040#ifdef PRINT_FUNCTIONS_FOR_DEBUGGING
Laurence Lundblade20db9c92018-12-17 11:40:37 -080041#include <stdio.h>
Laurence Lundbladea2e29072018-12-30 09:20:06 -080042
43static void PrintUsefulBufC(const char *szLabel, UsefulBufC Buf)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080044{
45 if(szLabel) {
46 printf("%s ", szLabel);
47 }
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -080048
Laurence Lundblade570fab52018-10-13 18:28:27 +080049 size_t i;
Laurence Lundbladea2e29072018-12-30 09:20:06 -080050 for(i = 0; i < Buf.len; i++) {
51 uint8_t Z = ((uint8_t *)Buf.ptr)[i];
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080052 printf("%02x ", Z);
53 }
54 printf("\n");
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -080055
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080056 fflush(stdout);
57}
Laurence Lundbladea2e29072018-12-30 09:20:06 -080058
59/*static void printencoded(const char *szLabel, const uint8_t *pEncoded, size_t nLen)
60{
61 PrintUsefulBufC(szLabel, (UsefulBufC){pEncoded, nLen});
62}*/
Laurence Lundblade20db9c92018-12-17 11:40:37 -080063#endif
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080064
65
Laurence Lundbladeb836efb2018-10-28 20:09:58 +070066static const uint8_t spExpectedEncodedInts[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080067 0x98, 0x2f, 0x3b, 0x7f, 0xff, 0xff, 0xff, 0xff,
68 0xff, 0xff, 0xff, 0x3b, 0x00, 0x00, 0x00, 0x01,
69 0x00, 0x00, 0x00, 0x00, 0x3a, 0xff, 0xff, 0xff,
70 0xff, 0x3a, 0xff, 0xff, 0xff, 0xfe, 0x3a, 0xff,
71 0xff, 0xff, 0xfd, 0x3a, 0x7f, 0xff, 0xff, 0xff,
72 0x3a, 0x7f, 0xff, 0xff, 0xfe, 0x3a, 0x00, 0x01,
73 0x00, 0x01, 0x3a, 0x00, 0x01, 0x00, 0x00, 0x39,
74 0xff, 0xff, 0x39, 0xff, 0xfe, 0x39, 0xff, 0xfd,
75 0x39, 0x01, 0x00, 0x38, 0xff, 0x38, 0xfe, 0x38,
76 0xfd, 0x38, 0x18, 0x37, 0x36, 0x20, 0x00, 0x00,
77 0x01, 0x16, 0x17, 0x18, 0x18, 0x18, 0x19, 0x18,
78 0x1a, 0x18, 0xfe, 0x18, 0xff, 0x19, 0x01, 0x00,
79 0x19, 0x01, 0x01, 0x19, 0xff, 0xfe, 0x19, 0xff,
80 0xff, 0x1a, 0x00, 0x01, 0x00, 0x00, 0x1a, 0x00,
81 0x01, 0x00, 0x01, 0x1a, 0x00, 0x01, 0x00, 0x02,
82 0x1a, 0x7f, 0xff, 0xff, 0xff, 0x1a, 0x7f, 0xff,
83 0xff, 0xff, 0x1a, 0x80, 0x00, 0x00, 0x00, 0x1a,
84 0x80, 0x00, 0x00, 0x01, 0x1a, 0xff, 0xff, 0xff,
85 0xfe, 0x1a, 0xff, 0xff, 0xff, 0xff, 0x1b, 0x00,
86 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x1b,
87 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01,
88 0x1b, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
89 0xff, 0x1b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
90 0xff, 0xff};
91
92
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080093// return CBOR error or -1 if type of value doesn't match
94
95static int IntegerValuesParseTestInternal(QCBORDecodeContext *pDCtx)
96{
97 QCBORItem Item;
98 int nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -080099
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800100 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
101 return nCBORError;
102 if(Item.uDataType != QCBOR_TYPE_ARRAY)
103 return -1;
104
105 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
106 return nCBORError;
107 if(Item.uDataType != QCBOR_TYPE_INT64 || // Todo; fix this for 32-bit machines
Laurence Lundblade570fab52018-10-13 18:28:27 +0800108 Item.val.int64 != -9223372036854775807LL - 1)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800109 return -1;
110
111 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
112 return nCBORError;
113 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade570fab52018-10-13 18:28:27 +0800114 Item.val.int64 != -4294967297)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800115 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800116
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800117 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
118 return nCBORError;
119 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade570fab52018-10-13 18:28:27 +0800120 Item.val.int64 != -4294967296)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800121 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800122
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800123 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
124 return nCBORError;
125 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade570fab52018-10-13 18:28:27 +0800126 Item.val.int64 != -4294967295)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800127 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800128
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800129 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
130 return nCBORError;
131 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade570fab52018-10-13 18:28:27 +0800132 Item.val.int64 != -4294967294)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800133 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800134
135
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800136 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
137 return nCBORError;
138 if(Item.uDataType != QCBOR_TYPE_INT64 ||
139 Item.val.int64 != -2147483648)
140 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800141
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800142 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
143 return nCBORError;
144 if(Item.uDataType != QCBOR_TYPE_INT64 ||
145 Item.val.int64 != -2147483647)
146 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800147
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800148 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
149 return nCBORError;
150 if(Item.uDataType != QCBOR_TYPE_INT64 ||
151 Item.val.int64 != -65538)
152 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800153
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800154 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
155 return nCBORError;
156 if(Item.uDataType != QCBOR_TYPE_INT64 ||
157 Item.val.int64 != -65537)
158 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800159
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800160 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
161 return nCBORError;
162 if(Item.uDataType != QCBOR_TYPE_INT64 ||
163 Item.val.int64 != -65536)
164 return -1;
165
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800166
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800167 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
168 return nCBORError;
169 if(Item.uDataType != QCBOR_TYPE_INT64 ||
170 Item.val.int64 != -65535)
171 return -1;
172
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800173
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800174 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
175 return nCBORError;
176 if(Item.uDataType != QCBOR_TYPE_INT64 ||
177 Item.val.int64 != -65534)
178 return -1;
179
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800180
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800181 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
182 return nCBORError;
183 if(Item.uDataType != QCBOR_TYPE_INT64 ||
184 Item.val.int64 != -257)
185 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800186
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800187 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
188 return nCBORError;
189 if(Item.uDataType != QCBOR_TYPE_INT64 ||
190 Item.val.int64 != -256)
191 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800192
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800193 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
194 return nCBORError;
195 if(Item.uDataType != QCBOR_TYPE_INT64 ||
196 Item.val.int64 != -255)
197 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800198
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800199 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
200 return nCBORError;
201 if(Item.uDataType != QCBOR_TYPE_INT64 ||
202 Item.val.int64 != -254)
203 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800204
205
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800206 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
207 return nCBORError;
208 if(Item.uDataType != QCBOR_TYPE_INT64 ||
209 Item.val.int64 != -25)
210 return -1;
211
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800212
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800213 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
214 return nCBORError;
215 if(Item.uDataType != QCBOR_TYPE_INT64 ||
216 Item.val.int64 != -24)
217 return -1;
218
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800219
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800220 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
221 return nCBORError;
222 if(Item.uDataType != QCBOR_TYPE_INT64 ||
223 Item.val.int64 != -23)
224 return -1;
225
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800226
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800227 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
228 return nCBORError;
229 if(Item.uDataType != QCBOR_TYPE_INT64 ||
230 Item.val.int64 != -1)
231 return -1;
232
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800233
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800234 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
235 return nCBORError;
236 if(Item.uDataType != QCBOR_TYPE_INT64 ||
237 Item.val.int64 != 0)
238 return -1;
239
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800240
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800241 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
242 return nCBORError;
243 if(Item.uDataType != QCBOR_TYPE_INT64 ||
244 Item.val.int64 != 0)
245 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800246
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800247 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
248 return nCBORError;
249 if(Item.uDataType != QCBOR_TYPE_INT64 ||
250 Item.val.int64 != 1)
251 return -1;
252
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800253
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800254 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
255 return nCBORError;
256 if(Item.uDataType != QCBOR_TYPE_INT64 ||
257 Item.val.int64 != 22)
258 return -1;
259
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800260
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800261 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
262 return nCBORError;
263 if(Item.uDataType != QCBOR_TYPE_INT64 ||
264 Item.val.int64 != 23)
265 return -1;
266
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800267
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800268 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
269 return nCBORError;
270 if(Item.uDataType != QCBOR_TYPE_INT64 ||
271 Item.val.int64 != 24)
272 return -1;
273
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800274
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800275 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
276 return nCBORError;
277 if(Item.uDataType != QCBOR_TYPE_INT64 ||
278 Item.val.int64 != 25)
279 return -1;
280
281 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
282 return nCBORError;
283 if(Item.uDataType != QCBOR_TYPE_INT64 ||
284 Item.val.int64 != 26)
285 return -1;
286
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800287
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800288 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
289 return nCBORError;
290 if(Item.uDataType != QCBOR_TYPE_INT64 ||
291 Item.val.int64 != 254)
292 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800293
294
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800295 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
296 return nCBORError;
297 if(Item.uDataType != QCBOR_TYPE_INT64 ||
298 Item.val.int64 != 255)
299 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800300
301
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800302 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
303 return nCBORError;
304 if(Item.uDataType != QCBOR_TYPE_INT64 ||
305 Item.val.int64 != 256)
306 return -1;
307
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800308
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800309 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
310 return nCBORError;
311 if(Item.uDataType != QCBOR_TYPE_INT64 ||
312 Item.val.int64 != 257)
313 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800314
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800315 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
316 return nCBORError;
317 if(Item.uDataType != QCBOR_TYPE_INT64 ||
318 Item.val.int64 != 65534)
319 return -1;
320
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800321
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800322 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
323 return nCBORError;
324 if(Item.uDataType != QCBOR_TYPE_INT64 ||
325 Item.val.int64 != 65535)
326 return -1;
327
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800328
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800329 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
330 return nCBORError;
331 if(Item.uDataType != QCBOR_TYPE_INT64 ||
332 Item.val.int64 != 65536)
333 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800334
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800335 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
336 return nCBORError;
337 if(Item.uDataType != QCBOR_TYPE_INT64 ||
338 Item.val.int64 != 65537)
339 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800340
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800341 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
342 return nCBORError;
343 if(Item.uDataType != QCBOR_TYPE_INT64 ||
344 Item.val.int64 != 65538)
345 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800346
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800347 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
348 return nCBORError;
349 if(Item.uDataType != QCBOR_TYPE_INT64 ||
350 Item.val.int64 != 2147483647)
351 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800352
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800353 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
354 return nCBORError;
355 if(Item.uDataType != QCBOR_TYPE_INT64 ||
356 Item.val.int64 != 2147483647)
357 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800358
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800359 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
360 return nCBORError;
361 if(Item.uDataType != QCBOR_TYPE_INT64 ||
362 Item.val.int64 != 2147483648)
363 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800364
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800365 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
366 return nCBORError;
367 if(Item.uDataType != QCBOR_TYPE_INT64 ||
368 Item.val.int64 != 2147483649)
369 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800370
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800371 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
372 return nCBORError;
373 if(Item.uDataType != QCBOR_TYPE_INT64 ||
374 Item.val.int64 != 4294967294)
375 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800376
377
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800378 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
379 return nCBORError;
380 if(Item.uDataType != QCBOR_TYPE_INT64 ||
381 Item.val.int64 != 4294967295)
382 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800383
384
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800385 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
386 return nCBORError;
387 if(Item.uDataType != QCBOR_TYPE_INT64 ||
388 Item.val.int64 != 4294967296)
389 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800390
391
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800392 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
393 return nCBORError;
394 if(Item.uDataType != QCBOR_TYPE_INT64 ||
395 Item.val.int64 != 4294967297)
396 return -1;
397
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800398
399
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800400 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
401 return nCBORError;
402 if(Item.uDataType != QCBOR_TYPE_INT64 ||
403 Item.val.int64 != 9223372036854775807LL)
404 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800405
406
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800407 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
408 return nCBORError;
409 if(Item.uDataType != QCBOR_TYPE_UINT64 ||
410 Item.val.uint64 != 18446744073709551615ULL)
411 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800412
413
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800414 if(QCBORDecode_Finish(pDCtx) != QCBOR_SUCCESS) {
415 return -1;
416 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800417
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800418 return 0;
419}
420
421
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800422/*
423 Tests the decoding of lots of different integers sizes
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +0800424 and values.
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800425 */
426
427int IntegerValuesParseTest()
428{
429 int n;
430 QCBORDecodeContext DCtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800431
Laurence Lundbladeb836efb2018-10-28 20:09:58 +0700432 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedEncodedInts), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800433
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800434 n = IntegerValuesParseTestInternal(&DCtx);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800435
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800436 return(n);
437}
438
439
440/*
441 Creates a simple CBOR array and returns it in *pEncoded. The array is malloced
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800442 and needs to be freed. This is used by several tests.
443
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800444 Two of the inputs can be set. Two other items in the array are fixed.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800445
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800446 */
447
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -0800448static uint8_t spSimpleArrayBuffer[50];
449
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800450static int CreateSimpleArray(int nInt1, int nInt2, uint8_t **pEncoded, size_t *pEncodedLen)
451{
452 QCBOREncodeContext ECtx;
453 int nReturn = -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800454
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800455 *pEncoded = NULL;
456 *pEncodedLen = INT32_MAX;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800457
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800458 // loop runs CBOR encoding twice. First with no buffer to
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -0800459 // calculate the length so buffer can be allocated correctly,
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800460 // and last with the buffer to do the actual encoding
461 do {
Laurence Lundblade0595e932018-11-02 22:22:47 +0700462 QCBOREncode_Init(&ECtx, (UsefulBuf){*pEncoded, *pEncodedLen});
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800463 QCBOREncode_OpenArray(&ECtx);
464 QCBOREncode_AddInt64(&ECtx, nInt1);
465 QCBOREncode_AddInt64(&ECtx, nInt2);
466 QCBOREncode_AddBytes(&ECtx, ((UsefulBufC) {"galactic", 8}));
467 QCBOREncode_AddBytes(&ECtx, ((UsefulBufC) {"haven token", 11}));
468 QCBOREncode_CloseArray(&ECtx);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800469
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -0800470 if(QCBOREncode_FinishGetSize(&ECtx, pEncodedLen))
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800471 goto Done;
472
473 if(*pEncoded != NULL) {
474 nReturn = 0;
475 goto Done;
476 }
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -0800477
478 // Use static buffer to avoid dependency on malloc()
479 if(*pEncodedLen > sizeof(spSimpleArrayBuffer)) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800480 goto Done;
481 }
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -0800482 *pEncoded = spSimpleArrayBuffer;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800483
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800484 } while(1);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800485
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -0800486Done:
487 return nReturn;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800488}
489
490
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800491/*
492 {"first integer": 42,
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900493 "an array of two strings": [
494 "string1", "string2"
495 ],
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800496 "map in a map": {
497 "bytes 1": h'78787878',
498 "bytes 2": h'79797979',
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900499 "another int": 98,
500 "text 2": "lies, damn lies and statistics"
501 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800502 }
503 */
504
505static uint8_t pValidMapEncoded[] = {
Laurence Lundbladeb836efb2018-10-28 20:09:58 +0700506 0xa3, 0x6d, 0x66, 0x69, 0x72, 0x73, 0x74, 0x20, 0x69, 0x6e,
507 0x74, 0x65, 0x67, 0x65, 0x72, 0x18, 0x2a, 0x77, 0x61, 0x6e,
508 0x20, 0x61, 0x72, 0x72, 0x61, 0x79, 0x20, 0x6f, 0x66, 0x20,
509 0x74, 0x77, 0x6f, 0x20, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67,
510 0x73, 0x82, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x31,
511 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x32, 0x6c, 0x6d,
512 0x61, 0x70, 0x20, 0x69, 0x6e, 0x20, 0x61, 0x20, 0x6d, 0x61,
513 0x70, 0xa4, 0x67, 0x62, 0x79, 0x74, 0x65, 0x73, 0x20, 0x31,
514 0x44, 0x78, 0x78, 0x78, 0x78, 0x67, 0x62, 0x79, 0x74, 0x65,
515 0x73, 0x20, 0x32, 0x44, 0x79, 0x79, 0x79, 0x79, 0x6b, 0x61,
516 0x6e, 0x6f, 0x74, 0x68, 0x65, 0x72, 0x20, 0x69, 0x6e, 0x74,
517 0x18, 0x62, 0x66, 0x74, 0x65, 0x78, 0x74, 0x20, 0x32, 0x78,
518 0x1e, 0x6c, 0x69, 0x65, 0x73, 0x2c, 0x20, 0x64, 0x61, 0x6d,
519 0x6e, 0x20, 0x6c, 0x69, 0x65, 0x73, 0x20, 0x61, 0x6e, 0x64,
520 0x20, 0x73, 0x74, 0x61, 0x74, 0x69, 0x73, 0x74, 0x69, 0x63,
521 0x73 } ;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800522
523static int ParseOrderedArray(const uint8_t *pEncoded, size_t nLen, int64_t *pInt1, int64_t *pInt2, const uint8_t **pBuf3, size_t *pBuf3Len, const uint8_t **pBuf4, size_t *pBuf4Len)
524{
525 QCBORDecodeContext DCtx;
526 QCBORItem Item;
527 int nReturn = -1; // assume error until success
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800528
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800529 QCBORDecode_Init(&DCtx, (UsefulBufC){pEncoded, nLen}, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800530
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800531 // Make sure the first thing is a map
532 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || Item.uDataType != QCBOR_TYPE_ARRAY)
533 goto Done;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800534
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800535 // First integer
Laurence Lundblade12b495d2018-12-17 11:15:54 -0800536 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || Item.uDataType != QCBOR_TYPE_INT64)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800537 goto Done;
538 *pInt1 = Item.val.int64;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800539
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800540 // Second integer
541 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || Item.uDataType != QCBOR_TYPE_INT64)
542 goto Done;
543 *pInt2 = Item.val.int64;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800544
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800545 // First string
546 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || Item.uDataType != QCBOR_TYPE_BYTE_STRING)
547 goto Done;
548 *pBuf3 = Item.val.string.ptr;
549 *pBuf3Len = Item.val.string.len;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800550
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800551 // Second string
552 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || Item.uDataType != QCBOR_TYPE_BYTE_STRING)
553 goto Done;
554 *pBuf4 = Item.val.string.ptr;
555 *pBuf4Len = Item.val.string.len;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800556
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800557 nReturn = 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800558
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800559Done:
560 return(nReturn);
561}
562
563
564
Laurence Lundblade9e3651c2018-10-10 11:49:55 +0800565
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800566int SimpleArrayTest()
567{
568 uint8_t *pEncoded;
569 size_t nEncodedLen;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800570
Laurence Lundblade5e390822019-01-06 12:35:01 -0800571 int64_t i1=0, i2=0;
572 size_t i3=0, i4=0;
573 const uint8_t *s3= (uint8_t *)"";
574 const uint8_t *s4= (uint8_t *)"";
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800575
576
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800577 if(CreateSimpleArray(23, 6000, &pEncoded, &nEncodedLen) < 0) {
578 return(-1);
579 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800580
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800581 ParseOrderedArray(pEncoded, nEncodedLen, &i1, &i2, &s3, &i3, &s4, &i4);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800582
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800583 if(i1 != 23 ||
584 i2 != 6000 ||
585 i3 != 8 ||
586 i4 != 11 ||
Laurence Lundbladea44d5062018-10-17 18:45:12 +0530587 memcmp("galactic", s3, 8) !=0 ||
588 memcmp("haven token", s4, 11) !=0) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800589 return(-1);
590 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800591
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800592 return(0);
593}
594
595
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700596/*
597 [
598 0,
599 [],
600 [
601 [],
602 [
603 0
604 ],
605 {},
606 {
607 1: {},
608 2: {},
609 3: []
610 }
611 ]
612 ]
613 */
614static uint8_t sEmpties[] = {0x83, 0x00, 0x80, 0x84, 0x80, 0x81, 0x00, 0xa0,
615 0xa3, 0x01, 0xa0, 0x02, 0xa0, 0x03, 0x80};
616
617int EmptyMapsAndArraysTest()
618{
619 QCBORDecodeContext DCtx;
620 QCBORItem Item;
621
622 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(sEmpties), QCBOR_DECODE_MODE_NORMAL);
623
624 // Array with 3 items
625 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
626 Item.uDataType != QCBOR_TYPE_ARRAY ||
627 Item.uNestingLevel != 0 ||
628 Item.uNextNestLevel != 1 ||
629 Item.val.uCount != 3) {
630 return -1;
631 }
632
633 // An integer 0
634 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
635 Item.uDataType != QCBOR_TYPE_INT64 ||
636 Item.uNestingLevel != 1 ||
637 Item.uNextNestLevel != 1 ||
638 Item.val.uint64 != 0) {
639 return -2;
640 }
641
642 // An empty array
643 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
644 Item.uDataType != QCBOR_TYPE_ARRAY ||
645 Item.uNestingLevel != 1 ||
646 Item.uNextNestLevel != 1 ||
647 Item.val.uCount != 0) {
648 return -3;
649 }
650
651 // An array with 4 items
652 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
653 Item.uDataType != QCBOR_TYPE_ARRAY ||
654 Item.uNestingLevel != 1 ||
655 Item.uNextNestLevel != 2 ||
656 Item.val.uCount != 4) {
657 return -4;
658 }
659
660 // An empty array
661 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
662 Item.uDataType != QCBOR_TYPE_ARRAY ||
663 Item.uNestingLevel != 2 ||
664 Item.uNextNestLevel != 2 ||
665 Item.val.uCount != 0) {
666 return -5;
667 }
668
669 // An array with 1 item
670 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
671 Item.uDataType != QCBOR_TYPE_ARRAY ||
672 Item.uNestingLevel != 2 ||
673 Item.uNextNestLevel != 3 ||
674 Item.val.uCount != 1) {
675 return -6;
676 }
677
678 // An integer 0
679 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
680 Item.uDataType != QCBOR_TYPE_INT64 ||
681 Item.uNestingLevel != 3 ||
682 Item.uNextNestLevel != 2 ||
683 Item.val.uint64 != 0) {
684 return -7;
685 }
686
687 // An empty map
688 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
689 Item.uDataType != QCBOR_TYPE_MAP ||
690 Item.uNestingLevel != 2 ||
691 Item.uNextNestLevel != 2 ||
692 Item.val.uCount != 0) {
693 return -8;
694 }
695
696 // An map with 3 items
697 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
698 Item.uDataType != QCBOR_TYPE_MAP ||
699 Item.uNestingLevel != 2 ||
700 Item.uNextNestLevel != 3 ||
701 Item.val.uCount != 3) {
702 return -9;
703 }
704
705 // An empty map
706 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
707 Item.uDataType != QCBOR_TYPE_MAP ||
708 Item.uNestingLevel != 3 ||
709 Item.uNextNestLevel != 3 ||
710 Item.val.uCount != 0) {
711 return -10;
712 }
713
714 // An empty map
715 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
716 Item.uDataType != QCBOR_TYPE_MAP ||
717 Item.uNestingLevel != 3 ||
718 Item.uNextNestLevel != 3 ||
719 Item.val.uCount != 0) {
720 return -11;
721 }
722
723 // An empty array
724 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
725 Item.uDataType != QCBOR_TYPE_ARRAY ||
726 Item.uNestingLevel != 3 ||
727 Item.uNextNestLevel != 0 ||
728 Item.val.uCount != 0) {
729 return -12;
730 }
731
732 if(QCBORDecode_Finish(&DCtx) != QCBOR_SUCCESS) {
733 return -13;
734 }
735
736 return 0;
737}
738
Laurence Lundblade9e3651c2018-10-10 11:49:55 +0800739
Laurence Lundbladeb836efb2018-10-28 20:09:58 +0700740static uint8_t spDeepArrays[] = {0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x80};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800741
742int ParseDeepArrayTest()
743{
744 QCBORDecodeContext DCtx;
745 int nReturn = 0;
746 int i;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800747
Laurence Lundbladeb836efb2018-10-28 20:09:58 +0700748 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spDeepArrays), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800749
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800750 for(i = 0; i < 10; i++) {
751 QCBORItem Item;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800752
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800753 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
754 Item.uDataType != QCBOR_TYPE_ARRAY ||
755 Item.uNestingLevel != i) {
756 nReturn = -1;
757 break;
758 }
759 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800760
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800761 return(nReturn);
762}
763
Laurence Lundblade972e59c2018-11-11 15:57:23 +0700764// Big enough to test nesting to the depth of 24
765static uint8_t spTooDeepArrays[] = {0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81,
766 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81,
767 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81,
768 0x80};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800769
770int ParseTooDeepArrayTest()
771{
772 QCBORDecodeContext DCtx;
773 int nReturn = 0;
774 int i;
775 QCBORItem Item;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800776
777
Laurence Lundbladeb836efb2018-10-28 20:09:58 +0700778 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTooDeepArrays), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800779
Laurence Lundblade972e59c2018-11-11 15:57:23 +0700780 for(i = 0; i < QCBOR_MAX_ARRAY_NESTING1; i++) {
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800781
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800782 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
783 Item.uDataType != QCBOR_TYPE_ARRAY ||
784 Item.uNestingLevel != i) {
785 nReturn = -1;
786 break;
787 }
788 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800789
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800790 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_ARRAY_NESTING_TOO_DEEP)
791 nReturn = -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800792
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800793 return(nReturn);
794}
795
796
797
798
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800799int ShortBufferParseTest()
800{
Laurence Lundbladebb1062e2019-08-12 23:28:54 -0700801 int nResult = 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800802
Laurence Lundbladebb1062e2019-08-12 23:28:54 -0700803 for(int nNum = sizeof(spExpectedEncodedInts)-1; nNum; nNum--) {
804 QCBORDecodeContext DCtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800805
Laurence Lundbladebb1062e2019-08-12 23:28:54 -0700806 QCBORDecode_Init(&DCtx, (UsefulBufC){spExpectedEncodedInts, nNum}, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800807
Laurence Lundbladebb1062e2019-08-12 23:28:54 -0700808 const QCBORError nErr = IntegerValuesParseTestInternal(&DCtx);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800809
Laurence Lundbladebb1062e2019-08-12 23:28:54 -0700810 if(nErr != QCBOR_ERR_HIT_END && nErr != QCBOR_ERR_NO_MORE_ITEMS) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800811 nResult = -1;
812 goto Done;
813 }
814 }
815Done:
816 return nResult;
817}
818
819
Laurence Lundblade9e3651c2018-10-10 11:49:55 +0800820
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800821int ShortBufferParseTest2()
822{
823 uint8_t *pEncoded;
824 int nReturn;
825 size_t nEncodedLen;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800826
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800827 int64_t i1, i2;
828 size_t i3, i4;
829 const uint8_t *s3, *s4;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800830
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800831 nReturn = 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800832
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800833 if(CreateSimpleArray(23, 6000, &pEncoded, &nEncodedLen) < 0) {
834 return(-1);
835 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800836
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800837 for(nEncodedLen--; nEncodedLen; nEncodedLen--) {
838 int nResult = ParseOrderedArray(pEncoded, (uint32_t)nEncodedLen, &i1, &i2, &s3, &i3, &s4, &i4);
839 if(nResult == 0) {
840 nReturn = -1;
841 }
842 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800843
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800844 return(nReturn);
845}
846
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530847/*
848 Decode and thoroughly check a moderately complex
849 set of maps
850 */
Laurence Lundbladeea567ac2018-12-09 14:03:21 -0800851static int ParseMapTest1(QCBORDecodeMode nMode)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800852{
853 QCBORDecodeContext DCtx;
854 QCBORItem Item;
855 int nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800856
Laurence Lundbladeea567ac2018-12-09 14:03:21 -0800857 QCBORDecode_Init(&DCtx, (UsefulBufC){pValidMapEncoded, sizeof(pValidMapEncoded)}, nMode);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800858
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900859 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800860 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900861 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800862 if(Item.uDataType != QCBOR_TYPE_MAP ||
863 Item.val.uCount != 3)
864 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800865
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900866 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800867 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900868 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800869 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800870 Item.uDataType != QCBOR_TYPE_INT64 ||
871 Item.val.int64 != 42 ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530872 Item.uDataAlloc ||
873 Item.uLabelAlloc ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900874 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("first integer"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800875 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900876 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800877
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900878 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800879 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900880 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800881 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530882 Item.uDataAlloc ||
883 Item.uLabelAlloc ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900884 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("an array of two strings")) ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800885 Item.uDataType != QCBOR_TYPE_ARRAY ||
886 Item.val.uCount != 2)
887 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800888
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900889 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800890 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900891 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800892 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530893 Item.uDataAlloc ||
894 Item.uLabelAlloc ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900895 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("string1"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800896 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900897 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800898
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900899 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800900 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900901 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800902 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530903 Item.uDataAlloc ||
904 Item.uLabelAlloc ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900905 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("string2"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800906 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900907 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800908
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900909 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800910 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900911 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800912 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
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("map in a map")) ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800916 Item.uDataType != QCBOR_TYPE_MAP ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900917 Item.val.uCount != 4) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800918 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900919 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800920
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900921 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800922 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900923 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800924 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900925 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("bytes 1"))||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800926 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530927 Item.uDataAlloc ||
928 Item.uLabelAlloc ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900929 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("xxxx"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800930 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900931 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800932
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900933 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800934 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900935 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800936 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900937 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("bytes 2")) ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800938 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530939 Item.uDataAlloc ||
940 Item.uLabelAlloc ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900941 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("yyyy"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800942 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900943 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800944
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900945 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800946 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900947 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800948 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530949 Item.uDataAlloc ||
950 Item.uLabelAlloc ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900951 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("another int")) ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800952 Item.uDataType != QCBOR_TYPE_INT64 ||
953 Item.val.int64 != 98)
954 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800955
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900956 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800957 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900958 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800959 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900960 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("text 2"))||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800961 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530962 Item.uDataAlloc ||
963 Item.uLabelAlloc ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900964 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("lies, damn lies and statistics"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800965 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900966 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800967
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800968 return 0;
969}
970
971
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900972/*
973 Decode and thoroughly check a moderately complex
974 set of maps
975 */
976int ParseMapAsArrayTest()
977{
978 QCBORDecodeContext DCtx;
979 QCBORItem Item;
980 int nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800981
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900982 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), QCBOR_DECODE_MODE_MAP_AS_ARRAY);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800983
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900984 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
985 return nCBORError;
986 }
Laurence Lundbladed61cbf32018-12-09 11:42:21 -0800987 if(Item.uDataType != QCBOR_TYPE_MAP_AS_ARRAY ||
988 Item.val.uCount != 6) {
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900989 return -1;
990 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800991
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900992 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
993 return nCBORError;
994 }
995 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
996 Item.uDataAlloc ||
997 Item.uLabelAlloc ||
998 Item.uLabelType != QCBOR_TYPE_NONE ||
999 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("first integer"))) {
1000 return -2;
1001 }
1002
1003 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
1004 return nCBORError;
1005 }
1006 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1007 Item.uDataType != QCBOR_TYPE_INT64 ||
1008 Item.val.int64 != 42 ||
1009 Item.uDataAlloc ||
1010 Item.uLabelAlloc) {
1011 return -3;
1012 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001013
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001014 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
1015 return nCBORError;
1016 }
1017 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1018 Item.uDataAlloc ||
1019 Item.uLabelAlloc ||
1020 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("an array of two strings")) ||
1021 Item.uDataType != QCBOR_TYPE_TEXT_STRING) {
1022 return -4;
1023 }
1024
1025 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
1026 return nCBORError;
1027 }
1028 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1029 Item.uDataAlloc ||
1030 Item.uLabelAlloc ||
1031 Item.uDataType != QCBOR_TYPE_ARRAY ||
1032 Item.val.uCount != 2) {
1033 return -5;
1034 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001035
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001036 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
1037 return nCBORError;
1038 }
1039 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1040 Item.val.string.len != 7 ||
1041 Item.uDataAlloc ||
1042 Item.uLabelAlloc ||
1043 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("string1"))) {
1044 return -6;
1045 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001046
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001047 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
1048 return nCBORError;
1049 }
1050 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1051 Item.uDataAlloc ||
1052 Item.uLabelAlloc ||
1053 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("string2"))) {
1054 return -7;
1055 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001056
1057
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001058 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
1059 return nCBORError;
1060 }
1061 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1062 Item.uDataAlloc ||
1063 Item.uLabelAlloc ||
1064 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("map in a map"))) {
1065 return -8;
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 ||
Laurence Lundbladed61cbf32018-12-09 11:42:21 -08001074 Item.uDataType != QCBOR_TYPE_MAP_AS_ARRAY ||
1075 Item.val.uCount != 8) {
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001076 return -9;
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.uLabelType != QCBOR_TYPE_NONE ||
1083 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("bytes 1"))||
1084 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1085 Item.uDataAlloc ||
1086 Item.uLabelAlloc) {
1087 return -10;
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.uLabelType != QCBOR_TYPE_NONE ||
1094 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
1095 Item.uDataAlloc ||
1096 Item.uLabelAlloc ||
1097 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("xxxx"))) {
1098 return -11;
1099 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001100
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 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("bytes 2")) ||
1106 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1107 Item.uDataAlloc ||
1108 Item.uLabelAlloc) {
1109 return -12;
1110 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001111
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001112 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
1113 return nCBORError;
1114 }
1115 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1116 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
1117 Item.uDataAlloc ||
1118 Item.uLabelAlloc ||
1119 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("yyyy"))) {
1120 return -13;
1121 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001122
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001123 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
1124 return nCBORError;
1125 }
1126 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1127 Item.uDataAlloc ||
1128 Item.uLabelAlloc ||
1129 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("another int")) ||
1130 Item.uDataType != QCBOR_TYPE_TEXT_STRING) {
1131 return -14;
1132 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001133
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001134 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
1135 return nCBORError;
1136 }
1137 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1138 Item.uDataAlloc ||
1139 Item.uLabelAlloc ||
1140 Item.uDataType != QCBOR_TYPE_INT64 ||
1141 Item.val.int64 != 98) {
1142 return -15;
1143 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001144
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001145 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
1146 return nCBORError;
1147 }
1148 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1149 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("text 2"))||
1150 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1151 Item.uDataAlloc ||
1152 Item.uLabelAlloc) {
1153 return -16;
1154 }
1155
1156 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
1157 return nCBORError;
1158 }
1159 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1160 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1161 Item.uDataAlloc ||
1162 Item.uLabelAlloc ||
1163 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("lies, damn lies and statistics"))) {
1164 return -17;
1165 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001166
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001167 return 0;
1168}
1169
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001170
1171/*
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301172 Fully or partially decode pValidMapEncoded. When
1173 partially decoding check for the right error code.
1174 How much partial decoding depends on nLevel.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001175
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301176 The partial decodes test error conditions of
1177 incomplete encoded input.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001178
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301179 This could be combined with the above test
1180 and made prettier and maybe a little more
1181 thorough.
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001182 */
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001183static int ExtraBytesTest(int nLevel)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001184{
1185 QCBORDecodeContext DCtx;
1186 QCBORItem Item;
1187 int nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001188
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001189 QCBORDecode_Init(&DCtx, (UsefulBufC){pValidMapEncoded, sizeof(pValidMapEncoded)}, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001190
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001191 if(nLevel < 1) {
1192 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_EXTRA_BYTES) {
1193 return -1;
1194 } else {
1195 return 0;
1196 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001197 }
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301198
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001199
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001200 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001201 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001202 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001203 if(Item.uDataType != QCBOR_TYPE_MAP ||
1204 Item.val.uCount != 3)
1205 return -1;
1206
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001207 if(nLevel < 2) {
1208 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
1209 return -1;
1210 } else {
1211 return 0;
1212 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001213 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001214
1215
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001216 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001217 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001218 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001219 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001220 Item.uDataType != QCBOR_TYPE_INT64 ||
1221 Item.val.uCount != 42 ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001222 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("first integer"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001223 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001224 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001225
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001226 if(nLevel < 3) {
1227 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
1228 return -1;
1229 } else {
1230 return 0;
1231 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001232 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001233
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001234 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001235 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001236 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001237 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001238 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("an array of two strings")) ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001239 Item.uDataType != QCBOR_TYPE_ARRAY ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001240 Item.val.uCount != 2) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001241 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001242 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001243
1244
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001245 if(nLevel < 4) {
1246 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
1247 return -1;
1248 } else {
1249 return 0;
1250 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001251 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001252
1253
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001254 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001255 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001256 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001257 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001258 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("string1"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001259 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001260 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001261
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001262 if(nLevel < 5) {
1263 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
1264 return -1;
1265 } else {
1266 return 0;
1267 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001268 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001269
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001270 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001271 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001272 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001273 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001274 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("string2"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001275 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001276 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001277
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001278 if(nLevel < 6) {
1279 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
1280 return -1;
1281 } else {
1282 return 0;
1283 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001284 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001285
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001286 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001287 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001288 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001289 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001290 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("map in a map")) ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001291 Item.uDataType != QCBOR_TYPE_MAP ||
1292 Item.val.uCount != 4)
1293 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001294
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001295 if(nLevel < 7) {
1296 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
1297 return -1;
1298 } else {
1299 return 0;
1300 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001301 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001302
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001303 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001304 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001305 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001306 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001307 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("bytes 1")) ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001308 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001309 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("xxxx"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001310 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001311 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001312
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001313 if(nLevel < 8) {
1314 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
1315 return -1;
1316 } else {
1317 return 0;
1318 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001319 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001320
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001321 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001322 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001323 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001324 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001325 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("bytes 2")) ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001326 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001327 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("yyyy"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001328 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001329 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001330
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001331 if(nLevel < 9) {
1332 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
1333 return -1;
1334 } else {
1335 return 0;
1336 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001337 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001338
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001339 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001340 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001341 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001342 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001343 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("another int")) ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001344 Item.uDataType != QCBOR_TYPE_INT64 ||
1345 Item.val.int64 != 98)
1346 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001347
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001348 if(nLevel < 10) {
1349 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
1350 return -1;
1351 } else {
1352 return 0;
1353 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001354 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001355
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001356 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001357 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001358 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001359 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001360 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("text 2"))||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001361 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001362 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("lies, damn lies and statistics"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001363 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001364 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001365
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301366 if(QCBORDecode_Finish(&DCtx)) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001367 return -1;
1368 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001369
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001370 return 0;
1371}
1372
1373
1374
1375
1376int ParseMapTest()
1377{
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301378 // Parse a moderatly complex map structure very thoroughl
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08001379 int n = ParseMapTest1(QCBOR_DECODE_MODE_NORMAL);
1380
1381 n = ParseMapTest1(QCBOR_DECODE_MODE_MAP_STRINGS_ONLY);
1382
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001383 if(!n) {
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001384 for(int i = 0; i < 10; i++) {
1385 n = ExtraBytesTest(i);
1386 if(n) {
1387 break;
1388 }
1389 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001390 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001391
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001392 return(n);
1393}
1394
1395
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07001396static uint8_t spSimpleValues[] = {0x8a, 0xf4, 0xf5, 0xf6, 0xf7, 0xff, 0xe0, 0xf3, 0xf8, 0x00, 0xf8, 0x13, 0xf8, 0x1f, 0xf8, 0x20, 0xf8, 0xff};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001397
1398int ParseSimpleTest()
1399{
1400 QCBORDecodeContext DCtx;
1401 QCBORItem Item;
1402 int nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001403
1404
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07001405 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSimpleValues), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001406
1407
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001408 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1409 return nCBORError;
1410 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
1411 Item.val.uCount != 10)
1412 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001413
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001414 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1415 return nCBORError;
1416 if(Item.uDataType != QCBOR_TYPE_FALSE)
1417 return -1;
1418
1419 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1420 return nCBORError;
1421 if(Item.uDataType != QCBOR_TYPE_TRUE)
1422 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001423
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001424 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1425 return nCBORError;
1426 if(Item.uDataType != QCBOR_TYPE_NULL)
1427 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001428
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001429 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1430 return nCBORError;
1431 if(Item.uDataType != QCBOR_TYPE_UNDEF)
1432 return -1;
1433
1434 // A break
Laurence Lundblade9e3651c2018-10-10 11:49:55 +08001435 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_BREAK)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001436 return -1;
1437
1438 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1439 return nCBORError;
1440 if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 0)
1441 return -1;
1442
1443 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1444 return nCBORError;
1445 if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 19)
1446 return -1;
1447
Laurence Lundblade077475f2019-04-26 09:06:33 -07001448 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_TYPE_7)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001449 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001450
Laurence Lundblade077475f2019-04-26 09:06:33 -07001451 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_TYPE_7)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001452 return -1;
1453
Laurence Lundblade077475f2019-04-26 09:06:33 -07001454 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_TYPE_7)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001455 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001456
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001457 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1458 return nCBORError;
1459 if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 32)
1460 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001461
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001462 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1463 return nCBORError;
1464 if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 255)
1465 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001466
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001467 return 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001468
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001469}
1470
1471
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001472static int IsNotWellFormedError(QCBORError nErr)
1473{
1474 switch(nErr){
1475 case QCBOR_ERR_INDEFINITE_STRING_CHUNK:
1476 case QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN:
1477 case QCBOR_ERR_UNSUPPORTED:
1478 case QCBOR_ERR_HIT_END:
1479 case QCBOR_ERR_BAD_TYPE_7:
1480 case QCBOR_ERR_BAD_BREAK:
1481 case QCBOR_ERR_EXTRA_BYTES:
1482 case QCBOR_ERR_BAD_INT:
1483 return 1;
1484 default:
1485 return 0;
1486 }
1487}
1488
1489
1490int NotWellFormedTests()
1491{
1492 // Loop over all the not-well-formed instance of CBOR
1493 // that are test vectors in not_well_formed_cbor.h
1494 const uint16_t nArraySize = sizeof(paNotWellFormedCBOR)/sizeof(struct someBinaryBytes);
1495 for(uint16_t nIterate = 0; nIterate < nArraySize; nIterate++) {
1496 const struct someBinaryBytes *pBytes = &paNotWellFormedCBOR[nIterate];
1497 const UsefulBufC Input = (UsefulBufC){pBytes->p, pBytes->n};
1498
1499 // Set up decoder context. String allocator needed for indefinite string test cases
1500 QCBORDecodeContext DCtx;
1501 QCBORDecode_Init(&DCtx, Input, QCBOR_DECODE_MODE_NORMAL);
1502 UsefulBuf_MAKE_STACK_UB(Pool, 100);
1503 QCBORDecode_SetMemPool(&DCtx, Pool, 0);
1504
1505 // Loop getting items until no more to get
1506 QCBORError nCBORError;
1507 do {
1508 QCBORItem Item;
1509
1510 nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
1511 } while(nCBORError == QCBOR_SUCCESS);
1512
1513 // Every test vector must fail with
1514 // a not-well-formed error. If not
1515 // this test fails.
1516 if(!IsNotWellFormedError(nCBORError)) {
1517 // Return index of failure in the error code
1518 return 2000 + nIterate;
1519 }
1520 }
1521 return 0;
1522}
1523
1524
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001525struct FailInput {
Laurence Lundblade3a6042e2019-06-28 19:58:04 -07001526 UsefulBufC Input; // CBOR to decode
1527 QCBORError nError; // The error expected
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001528};
1529
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001530struct FailInput Failures[] = {
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001531 // Most of this is copied from not_well_formed.h. Here the error code
1532 // returned is also checked.
1533
1534 // Indefinite length strings must be closed off
1535 // An indefinite length byte string not closed off
1536 { {(uint8_t[]){0x5f, 0x41, 0x00}, 3}, QCBOR_ERR_HIT_END },
1537 // An indefinite length text string not closed off
1538 { {(uint8_t[]){0x7f, 0x61, 0x00}, 3}, QCBOR_ERR_HIT_END },
1539
1540
1541 // All the chunks in an indefinite length string must be of the type of indefinite length string
1542 // indefinite length byte string with text string chunk
1543 { {(uint8_t[]){0x5f, 0x61, 0x00, 0xff}, 4}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1544 // indefinite length text string with a byte string chunk
1545 { {(uint8_t[]){0x7f, 0x41, 0x00, 0xff}, 4}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1546 // indefinite length byte string with an positive integer chunk
1547 { {(uint8_t[]){0x5f, 0x00, 0xff}, 3}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1548 // indefinite length byte string with an negative integer chunk
1549 { {(uint8_t[]){0x5f, 0x21, 0xff}, 3}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1550 // indefinite length byte string with an array chunk
1551 { {(uint8_t[]){0x5f, 0x80, 0xff}, 3}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1552 // indefinite length byte string with an map chunk
1553 { {(uint8_t[]){0x5f, 0xa0, 0xff}, 3}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1554 // indefinite length byte string with tagged integer chunk
1555 { {(uint8_t[]){0x5f, 0xc0, 0x00, 0xff}, 4}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1556 // indefinite length byte string with an simple type chunk
1557 { {(uint8_t[]){0x5f, 0xe0, 0xff}, 3}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1558 { {(uint8_t[]){0x5f, 0x5f, 0x41, 0x00, 0xff, 0xff}, 6}, QCBOR_ERR_INDEFINITE_STRING_CHUNK},
1559 // indefinite length text string with indefinite string inside
1560 { {(uint8_t[]){0x7f, 0x7f, 0x61, 0x00, 0xff, 0xff}, 6}, QCBOR_ERR_INDEFINITE_STRING_CHUNK},
1561
1562
1563 // Definte length maps and arrays must be closed by having the right number of items
1564 // A definte length array that is supposed to have 1 item, but has none
1565 { {(uint8_t[]){0x81}, 1}, QCBOR_ERR_HIT_END },
1566 // A definte length array that is supposed to have 2 items, but has only 1
1567 { {(uint8_t[]){0x82, 0x00}, 2}, QCBOR_ERR_HIT_END },
1568 // A definte length array that is supposed to have 511 items, but has only 1
1569 { {(uint8_t[]){0x9a, 0x01, 0xff, 0x00}, 4}, QCBOR_ERR_HIT_END },
1570 // A definte length map that is supposed to have 1 item, but has none
1571 { {(uint8_t[]){0xa1}, 1}, QCBOR_ERR_HIT_END },
1572 // A definte length map that is supposed to have s item, but has only 1
1573 { {(uint8_t[]){0xa2, 0x01, 0x02}, 3}, QCBOR_ERR_HIT_END },
1574
1575
1576 // Indefinte length maps and arrays must be ended by a break
1577 // Indefinite length array with zero items and no break
1578 { {(uint8_t[]){0x9f}, 1}, QCBOR_ERR_HIT_END },
1579 // Indefinite length array with two items and no break
1580 { {(uint8_t[]){0x9f, 0x01, 0x02}, 3}, QCBOR_ERR_HIT_END },
1581 // Indefinite length map with zero items and no break
1582 { {(uint8_t[]){0xbf}, 1}, QCBOR_ERR_HIT_END },
1583 // Indefinite length map with two items and no break
1584 { {(uint8_t[]){0xbf, 0x01, 0x02, 0x01, 0x02}, 5}, QCBOR_ERR_HIT_END },
1585
1586
1587 // Nested maps and arrays must be closed off (some extra nested test vectors)
1588 // Unclosed indefinite array containing a close definite array
1589 { {(uint8_t[]){0x9f, 0x80, 0x00}, 3}, QCBOR_ERR_HIT_END },
1590 // Definite length array containing an unclosed indefinite array
1591 { {(uint8_t[]){0x81, 0x9f}, 2}, QCBOR_ERR_HIT_END },
1592 // Deeply nested definite length arrays with deepest one unclosed
1593 { {(uint8_t[]){0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81}, 9}, QCBOR_ERR_HIT_END },
1594 // Deeply nested indefinite length arrays with deepest one unclosed
1595 { {(uint8_t[]){0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0xff, 0xff, 0xff, 0xff}, 9}, QCBOR_ERR_HIT_END },
1596 // Mixed nesting with indefinite unclosed
1597 { {(uint8_t[]){0x9f, 0x81, 0x9f, 0x81, 0x9f, 0x9f, 0xff, 0xff, 0xff}, 9}, QCBOR_ERR_BAD_BREAK }, // TODO: think through this one
1598 // Mixed nesting with definite unclosed
1599 { {(uint8_t[]){0x9f, 0x82, 0x9f, 0x81, 0x9f, 0x9f, 0xff, 0xff, 0xff, 0xff}, 10}, QCBOR_ERR_BAD_BREAK }, // TODO: think through this one
1600
1601
1602 // The "argument" for the data item is incomplete
1603 // Positive integer missing 1 byte argument
1604 { {(uint8_t[]){0x18}, 1}, QCBOR_ERR_HIT_END },
1605 // Positive integer missing 2 byte argument
1606 { {(uint8_t[]){0x19}, 1}, QCBOR_ERR_HIT_END },
1607 // Positive integer missing 4 byte argument
1608 { {(uint8_t[]){0x1a}, 1}, QCBOR_ERR_HIT_END },
1609 // Positive integer missing 8 byte argument
1610 { {(uint8_t[]){0x1b}, 1}, QCBOR_ERR_HIT_END },
1611 // Positive integer missing 1 byte of 2 byte argument
1612 { {(uint8_t[]){0x19, 0x01}, 2}, QCBOR_ERR_HIT_END },
1613 // Positive integer missing 2 bytes of 4 byte argument
1614 { {(uint8_t[]){0x1a, 0x01, 0x02}, 3}, QCBOR_ERR_HIT_END },
1615 // Positive integer missing 1 bytes of 7 byte argument
1616 { {(uint8_t[]){0x1b, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07}, 8}, QCBOR_ERR_HIT_END },
1617 // Negative integer missing 1 byte argument
1618 { {(uint8_t[]){0x38}, 1}, QCBOR_ERR_HIT_END },
1619 // Binary string missing 1 byte argument
1620 { {(uint8_t[]){0x58}, 1}, QCBOR_ERR_HIT_END },
1621 // Text string missing 1 byte argument
1622 { {(uint8_t[]){0x78}, 1}, QCBOR_ERR_HIT_END },
1623 // Array missing 1 byte argument
1624 { {(uint8_t[]){0x98}, 1}, QCBOR_ERR_HIT_END },
1625 // Map missing 1 byte argument
1626 { {(uint8_t[]){0xb8}, 1}, QCBOR_ERR_HIT_END },
1627 // Tag missing 1 byte argument
1628 { {(uint8_t[]){0xd8}, 1}, QCBOR_ERR_HIT_END },
1629 // Simple missing 1 byte argument
1630 { {(uint8_t[]){0xf8}, 1}, QCBOR_ERR_HIT_END },
1631
1632
1633 // Breaks must not occur in definite length arrays and maps
1634 // Array of length 1 with sole member replaced by a break
1635 { {(uint8_t[]){0x81, 0xff}, 2}, QCBOR_ERR_BAD_BREAK },
1636 // Array of length 2 with 2nd member replaced by a break
1637 { {(uint8_t[]){0x82, 0x00, 0xff}, 3}, QCBOR_ERR_BAD_BREAK },
1638 // Map of length 1 with sole member label replaced by a break
1639 { {(uint8_t[]){0xa1, 0xff}, 2}, QCBOR_ERR_BAD_BREAK },
1640 // Map of length 1 with sole member label replaced by break
1641 // Alternate representation that some decoders handle difference
1642 { {(uint8_t[]){0xa1, 0xff, 0x00}, 3}, QCBOR_ERR_BAD_BREAK },
1643 // Array of length 1 with 2nd member value replaced by a break
1644 { {(uint8_t[]){0xa1, 0x00, 0xff}, 3}, QCBOR_ERR_BAD_BREAK },
1645 // Map of length 2 with 2nd member replaced by a break
1646 { {(uint8_t[]){0xa2, 0x00, 0x00, 0xff}, 4}, QCBOR_ERR_BAD_BREAK },
1647
1648
1649 // Breaks must not occur on their own out of an indefinite length data item
1650 // A bare break is not well formed
1651 { {(uint8_t[]){0xff}, 1}, QCBOR_ERR_BAD_BREAK },
1652 // A bare break after a zero length definite length array
1653 { {(uint8_t[]){0x80, 0xff}, 2}, QCBOR_ERR_BAD_BREAK },
1654 // A bare break after a zero length indefinite length map
1655 { {(uint8_t[]){0x9f, 0xff, 0xff}, 3}, QCBOR_ERR_BAD_BREAK },
1656
1657
1658 // Forbidden two byte encodings of simple types
1659 // Must use 0xe0 instead
1660 { {(uint8_t[]){0xf8, 0x00}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1661 // Should use 0xe1 instead
1662 { {(uint8_t[]){0xf8, 0x01}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1663 // Should use 0xe2 instead
1664 { {(uint8_t[]){0xf8, 0x02}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1665 // Should use 0xe3 instead
1666 { {(uint8_t[]){0xf8, 0x03}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1667 // Should use 0xe4 instead
1668 { {(uint8_t[]){0xf8, 0x04}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1669 // Should use 0xe5 instead
1670 { {(uint8_t[]){0xf8, 0x05}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1671 // Should use 0xe6 instead
1672 { {(uint8_t[]){0xf8, 0x06}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1673 // Should use 0xe7 instead
1674 { {(uint8_t[]){0xf8, 0x07}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1675 // Should use 0xe8 instead
1676 { {(uint8_t[]){0xf8, 0x08}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1677 // Should use 0xe9 instead
1678 { {(uint8_t[]){0xf8, 0x09}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1679 // Should use 0xea instead
1680 { {(uint8_t[]){0xf8, 0x0a}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1681 // Should use 0xeb instead
1682 { {(uint8_t[]){0xf8, 0x0b}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1683 // Should use 0xec instead
1684 { {(uint8_t[]){0xf8, 0x0c}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1685 // Should use 0xed instead
1686 { {(uint8_t[]){0xf8, 0x0d}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1687 // Should use 0xee instead
1688 { {(uint8_t[]){0xf8, 0x0e}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1689 // Should use 0xef instead
1690 { {(uint8_t[]){0xf8, 0x0f}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1691 // Should use 0xf0 instead
1692 { {(uint8_t[]){0xf8, 0x10}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1693 // Should use 0xf1 instead
1694 { {(uint8_t[]){0xf8, 0x11}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1695 // Should use 0xf2 instead
1696 { {(uint8_t[]){0xf8, 0x12}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1697 // Must use 0xf3 instead
1698 { {(uint8_t[]){0xf8, 0x13}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1699 // Must use 0xf4 instead
1700 { {(uint8_t[]){0xf8, 0x14}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1701 // Must use 0xf5 instead
1702 { {(uint8_t[]){0xf8, 0x15}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1703 // Must use 0xf6 instead
1704 { {(uint8_t[]){0xf8, 0x16}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1705 // Must use 0xf7 instead
1706 { {(uint8_t[]){0xf8, 0x17}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1707 // Must use 0xf8 instead
1708 { {(uint8_t[]){0xf8, 0x18}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1709
1710
1711 // Integers with additional info indefinite length
1712 // Positive integer with additional info indefinite length
1713 { {(uint8_t[]){0x1f}, 1}, QCBOR_ERR_BAD_INT },
1714 // Negative integer with additional info indefinite length
1715 { {(uint8_t[]){0x3f}, 1}, QCBOR_ERR_BAD_INT },
1716 // CBOR tag with "argument" an indefinite length
1717 { {(uint8_t[]){0xdf, 0x00}, 2}, QCBOR_ERR_BAD_INT },
1718 // CBOR tag with "argument" an indefinite length alternate vector
1719 { {(uint8_t[]){0xdf}, 1}, QCBOR_ERR_BAD_INT },
1720
1721
1722 // Missing bytes from a deterministic length string
1723 // A byte string is of length 1 without the 1 byte
1724 { {(uint8_t[]){0x41}, 1}, QCBOR_ERR_HIT_END },
1725 // A text string is of length 1 without the 1 byte
1726 { {(uint8_t[]){0x61}, 1}, QCBOR_ERR_HIT_END },
1727 // Byte string should have 2^32-1 bytes, but has one
1728 { {(uint8_t[]){0x5a, 0xff, 0xff, 0xff, 0xff, 0x00}, 6}, QCBOR_ERR_HIT_END },
1729 // Byte string should have 2^32-1 bytes, but has one
1730 { {(uint8_t[]){0x7a, 0xff, 0xff, 0xff, 0xff, 0x00}, 6}, QCBOR_ERR_HIT_END },
1731
1732
1733 // Use of unassigned additional information values
1734 // Major type positive integer with reserved value 28
1735 { {(uint8_t[]){0x1c}, 1}, QCBOR_ERR_UNSUPPORTED },
1736 // Major type positive integer with reserved value 29
1737 { {(uint8_t[]){0x1d}, 1}, QCBOR_ERR_UNSUPPORTED },
1738 // Major type positive integer with reserved value 30
1739 { {(uint8_t[]){0x1e}, 1}, QCBOR_ERR_UNSUPPORTED },
1740 // Major type negative integer with reserved value 28
1741 { {(uint8_t[]){0x3c}, 1}, QCBOR_ERR_UNSUPPORTED },
1742 // Major type negative integer with reserved value 29
1743 { {(uint8_t[]){0x3d}, 1}, QCBOR_ERR_UNSUPPORTED },
1744 // Major type negative integer with reserved value 30
1745 { {(uint8_t[]){0x3e}, 1}, QCBOR_ERR_UNSUPPORTED },
1746 // Major type byte string with reserved value 28 length
1747 { {(uint8_t[]){0x5c}, 1}, QCBOR_ERR_UNSUPPORTED },
1748 // Major type byte string with reserved value 29 length
1749 { {(uint8_t[]){0x5d}, 1}, QCBOR_ERR_UNSUPPORTED },
1750 // Major type byte string with reserved value 30 length
1751 { {(uint8_t[]){0x5e}, 1}, QCBOR_ERR_UNSUPPORTED },
1752 // Major type text string with reserved value 28 length
1753 { {(uint8_t[]){0x7c}, 1}, QCBOR_ERR_UNSUPPORTED },
1754 // Major type text string with reserved value 29 length
1755 { {(uint8_t[]){0x7d}, 1}, QCBOR_ERR_UNSUPPORTED },
1756 // Major type text string with reserved value 30 length
1757 { {(uint8_t[]){0x7e}, 1}, QCBOR_ERR_UNSUPPORTED },
1758 // Major type array with reserved value 28 length
1759 { {(uint8_t[]){0x9c}, 1}, QCBOR_ERR_UNSUPPORTED },
1760 // Major type array with reserved value 29 length
1761 { {(uint8_t[]){0x9d}, 1}, QCBOR_ERR_UNSUPPORTED },
1762 // Major type array with reserved value 30 length
1763 { {(uint8_t[]){0x9e}, 1}, QCBOR_ERR_UNSUPPORTED },
1764 // Major type map with reserved value 28 length
1765 { {(uint8_t[]){0xbc}, 1}, QCBOR_ERR_UNSUPPORTED },
1766 // Major type map with reserved value 29 length
1767 { {(uint8_t[]){0xbd}, 1}, QCBOR_ERR_UNSUPPORTED },
1768 // Major type map with reserved value 30 length
1769 { {(uint8_t[]){0xbe}, 1}, QCBOR_ERR_UNSUPPORTED },
1770 // Major type tag with reserved value 28 length
1771 { {(uint8_t[]){0xdc}, 1}, QCBOR_ERR_UNSUPPORTED },
1772 // Major type tag with reserved value 29 length
1773 { {(uint8_t[]){0xdd}, 1}, QCBOR_ERR_UNSUPPORTED },
1774 // Major type tag with reserved value 30 length
1775 { {(uint8_t[]){0xde}, 1}, QCBOR_ERR_UNSUPPORTED },
1776 // Major type simple with reserved value 28 length
1777 { {(uint8_t[]){0xfc}, 1}, QCBOR_ERR_UNSUPPORTED },
1778 // Major type simple with reserved value 29 length
1779 { {(uint8_t[]){0xfd}, 1}, QCBOR_ERR_UNSUPPORTED },
1780 // Major type simple with reserved value 30 length
1781 { {(uint8_t[]){0xfe}, 1}, QCBOR_ERR_UNSUPPORTED },
1782
1783
1784 // Maps must have an even number of data items (key & value)
1785 // Map with 1 item when it should have 2
1786 { {(uint8_t[]){0xa1, 0x00}, 2}, QCBOR_ERR_HIT_END },
1787 // Map with 3 item when it should have 4
1788 { {(uint8_t[]){0xa2, 0x00, 0x00, 0x00}, 2}, QCBOR_ERR_HIT_END },
1789 // Map with 1 item when it should have 2
1790 { {(uint8_t[]){0xbf, 0x00, 0xff}, 3}, QCBOR_ERR_BAD_BREAK },
1791 // Map with 3 item when it should have 4
1792 { {(uint8_t[]){0xbf, 0x00, 0x00, 0x00, 0xff}, 5}, QCBOR_ERR_BAD_BREAK },
1793
1794
1795 // In addition to not-well-formed, some invalid CBOR
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001796 { {(uint8_t[]){0xc0, 0x00}, 2}, QCBOR_ERR_BAD_OPT_TAG }, // Text-based date, with an integer
1797 { {(uint8_t[]){0xc1, 0x41, 0x33}, 3}, QCBOR_ERR_BAD_OPT_TAG }, // Epoch date, with an byte string
1798 { {(uint8_t[]){0xc1, 0xc0, 0x00}, 3}, QCBOR_ERR_BAD_OPT_TAG }, // tagged as both epoch and string dates
Laurence Lundblade3a6042e2019-06-28 19:58:04 -07001799 { {(uint8_t[]){0xc2, 0x00}, 2}, QCBOR_ERR_BAD_OPT_TAG }, // big num tagged an int, not a byte string
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001800};
1801
Laurence Lundblade3a6042e2019-06-28 19:58:04 -07001802int DecodeFailureTests()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001803{
Laurence Lundblade3a6042e2019-06-28 19:58:04 -07001804 // Loop over the failures
1805 const struct FailInput * const pFEnd = &Failures[0] +
1806 sizeof(Failures)/sizeof(struct FailInput);
Laurence Lundblade3a6042e2019-06-28 19:58:04 -07001807 for(const struct FailInput *pF = &Failures[0]; pF < pFEnd ;pF++) {
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001808
1809 // Set up the decoding context including a mem pool so that
1810 // indefinite length items can be checked
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001811 QCBORDecodeContext DCtx;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001812 QCBORDecode_Init(&DCtx, pF->Input, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001813 UsefulBuf_MAKE_STACK_UB(Pool, 100);
1814 QCBORError nCBORError = QCBORDecode_SetMemPool(&DCtx, Pool, 0);
1815 if(nCBORError) {
1816 return -9;
1817 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001818
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001819 // Iterate until there is an error of some sort
1820 do {
1821 QCBORItem Item;
1822
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001823 nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001824 } while(nCBORError == QCBOR_SUCCESS);
Laurence Lundblade3a6042e2019-06-28 19:58:04 -07001825
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001826 // Must get the expected error or the this test fails
1827 if(nCBORError != pF->nError) {
1828 // return index of CBOR + 1000
1829 return 1000 + (int)(pF - &Failures[0]);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001830 }
1831 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001832
Laurence Lundblade3a6042e2019-06-28 19:58:04 -07001833 // Corrupt the UsefulInputBuf and see that
1834 // it reflected correctly for CBOR decoding
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001835 {
1836 QCBORDecodeContext DCtx;
Laurence Lundblade3a6042e2019-06-28 19:58:04 -07001837 QCBORItem Item;
1838 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001839
Laurence Lundblade3a6042e2019-06-28 19:58:04 -07001840 QCBORDecode_Init(&DCtx,
1841 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSimpleValues),
1842 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001843
1844 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1845 return nCBORError;
1846 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
Laurence Lundblade3a6042e2019-06-28 19:58:04 -07001847 Item.val.uCount != 10) {
1848 // This wasn't supposed to happen
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001849 return -1;
Laurence Lundblade3a6042e2019-06-28 19:58:04 -07001850 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001851
Laurence Lundblade3a6042e2019-06-28 19:58:04 -07001852 DCtx.InBuf.magic = 0; // Reach in and corrupt the UsefulInputBuf
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001853
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001854 nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundblade3a6042e2019-06-28 19:58:04 -07001855 if(nCBORError != QCBOR_ERR_HIT_END) {
1856 // Did not get back the error expected
1857 return -2;
1858 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001859 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001860
Laurence Lundblade3a6042e2019-06-28 19:58:04 -07001861 return 0;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001862}
1863
1864
Laurence Lundbladea2e29072018-12-30 09:20:06 -08001865/* Try all 256 values of the byte at nLen including recursing for
1866 each of the values to try values at nLen+1 ... up to nLenMax
1867 */
1868static void ComprehensiveInputRecurser(uint8_t *pBuf, int nLen, int nLenMax)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001869{
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001870 if(nLen >= nLenMax) {
1871 return;
1872 }
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08001873
Laurence Lundbladea2e29072018-12-30 09:20:06 -08001874 for(int inputByte = 0; inputByte < 256; inputByte++) {
1875 // Set up the input
1876 pBuf[nLen] = inputByte;
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08001877 const UsefulBufC Input = {pBuf, nLen+1};
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08001878
Laurence Lundbladea2e29072018-12-30 09:20:06 -08001879 // Get ready to parse
1880 QCBORDecodeContext DCtx;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001881 QCBORDecode_Init(&DCtx, Input, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001882
Laurence Lundbladea2e29072018-12-30 09:20:06 -08001883 // Parse by getting the next item until an error occurs
1884 // Just about every possible decoder error can occur here
1885 // The goal of this test is not to check for the correct
1886 // error since that is not really possible. It is to
1887 // see that there is no crash on hostile input.
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001888 while(1) {
Laurence Lundbladea2e29072018-12-30 09:20:06 -08001889 QCBORItem Item;
1890 QCBORError nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001891 if(nCBORError != QCBOR_SUCCESS) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001892 break;
1893 }
1894 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001895
Laurence Lundbladea2e29072018-12-30 09:20:06 -08001896 ComprehensiveInputRecurser(pBuf, nLen+1, nLenMax);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001897 }
1898}
1899
1900
1901/*
Laurence Lundbladea2e29072018-12-30 09:20:06 -08001902 Public function for initialization. See header qcbor.h
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001903 */
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001904int ComprehensiveInputTest()
1905{
Laurence Lundbladea2e29072018-12-30 09:20:06 -08001906 // Size 2 tests 64K inputs and runs quickly
1907 uint8_t pBuf[2];
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001908
Laurence Lundbladea2e29072018-12-30 09:20:06 -08001909 ComprehensiveInputRecurser(pBuf, 0, sizeof(pBuf));
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001910
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001911 return 0;
1912}
1913
Laurence Lundbladea2e29072018-12-30 09:20:06 -08001914
1915/*
1916 Public function for initialization. See header qcbor.h
1917 */
1918int BigComprehensiveInputTest()
1919{
1920 // size 3 tests 16 million inputs and runs OK
1921 // in seconds on fast machines. Size 4 takes
1922 // 10+ minutes and 5 half a day on fast
1923 // machines. This test is kept separate from
1924 // the others so as to no slow down the use
1925 // of them as a very frequent regression.
1926 uint8_t pBuf[3]; //
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08001927
Laurence Lundbladea2e29072018-12-30 09:20:06 -08001928 ComprehensiveInputRecurser(pBuf, 0, sizeof(pBuf));
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08001929
Laurence Lundbladea2e29072018-12-30 09:20:06 -08001930 return 0;
1931}
1932
1933
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07001934static uint8_t spDateTestInput[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001935 0xc0, // tag for string date
1936 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001937
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001938 0xc1, // tag for epoch date
1939 0x1a, 0x53, 0x72, 0x4E, 0x00, // Epoch date 1400000000; Tue, 13 May 2014 16:53:20 GMT
1940
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001941 // CBOR_TAG_B64
1942 0xc1, 0xcf, 0xd8, 0x22, // 0xee, // Epoch date with extra tags TODO: fix this test
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001943 0x1a, 0x53, 0x72, 0x4E, 0x01,
1944
1945 0xc1, // tag for epoch date
1946 0x1b, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, // Too large integer
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001947
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001948 0xc1, // tag for epoch date
1949 0xfa, 0x3f, 0x8c, 0xcc, 0xcd, // double with value 1.1
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001950
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001951 0xc1, // tag for epoch date
1952 0xfa, 0x7f, 0x7f, 0xff, 0xff // 3.4028234663852886e+38 too large
1953
1954};
1955
1956
1957// have to check float expected only to within an epsilon
1958int CHECK_EXPECTED_DOUBLE(double val, double expected) {
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001959
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001960 double diff = val - expected;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001961
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001962 diff = fabs(diff);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001963
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001964 return diff > 0.0000001;
1965}
1966
1967
1968int DateParseTest()
1969{
1970 QCBORDecodeContext DCtx;
1971 QCBORItem Item;
1972 int nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001973
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07001974 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spDateTestInput), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001975
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001976 const uint64_t uTags[] = {15};
1977 QCBORTagListIn TagList = {1, uTags};
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001978
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001979 QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TagList);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001980
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001981 // String date
1982 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1983 return -1;
1984 if(Item.uDataType != QCBOR_TYPE_DATE_STRING ||
Laurence Lundblade9e3651c2018-10-10 11:49:55 +08001985 UsefulBuf_Compare(Item.val.dateString, UsefulBuf_FromSZ("1985-04-12"))){
Laurence Lundblade67bd5512018-11-02 21:44:06 +07001986 return -2;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001987 }
1988
1989 // Epoch date
1990 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade67bd5512018-11-02 21:44:06 +07001991 return -3;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001992 if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH ||
1993 Item.val.epochDate.nSeconds != 1400000000 ||
1994 Item.val.epochDate.fSecondsFraction != 0 ) {
Laurence Lundblade67bd5512018-11-02 21:44:06 +07001995 return -4;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001996 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001997
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001998 // Epoch date with extra CBOR_TAG_B64 tag that doesn't really mean anything
1999 // but want to be sure extra tag doesn't cause a problem
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002000 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade67bd5512018-11-02 21:44:06 +07002001 return -5;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002002 if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH ||
2003 Item.val.epochDate.nSeconds != 1400000001 ||
2004 Item.val.epochDate.fSecondsFraction != 0 ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002005 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_B64)) {
Laurence Lundblade67bd5512018-11-02 21:44:06 +07002006 return -6;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002007 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002008
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002009 // Epoch date that is too large for our representation
2010 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_DATE_OVERFLOW) {
Laurence Lundblade67bd5512018-11-02 21:44:06 +07002011 return -7;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002012 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002013
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002014 // Epoch date in float format with fractional seconds
2015 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade67bd5512018-11-02 21:44:06 +07002016 return -8;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002017 if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH ||
2018 Item.val.epochDate.nSeconds != 1 ||
2019 CHECK_EXPECTED_DOUBLE(Item.val.epochDate.fSecondsFraction, 0.1 )) {
Laurence Lundblade67bd5512018-11-02 21:44:06 +07002020 return -9;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002021 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002022
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002023 // Epoch date float that is too large for our representation
2024 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_DATE_OVERFLOW) {
Laurence Lundblade67bd5512018-11-02 21:44:06 +07002025 return -10;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002026 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002027
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002028 // TODO: could use a few more tests with float, double, and half precsion and negative (but coverage is still pretty good)
2029
2030 return 0;
2031}
2032
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002033// Really simple basic input for tagging test
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002034static uint8_t spOptTestInput[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002035 0xd9, 0xd9, 0xf7, // CBOR magic number
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002036 0x81, // Array of one
2037 0xd8, 0x04, // non-preferred serialization of tag 4
2038 0x82, 0x01, 0x03}; // fraction 1/3
2039
2040static uint8_t spEncodedLargeTag[] = {0xdb, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x80};
2041
2042// 0x9192939495969798, 0x88, 0x01, 0x04
2043static uint8_t spLotsOfTags[] = {0xdb, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0xd8, 0x88, 0xc5, 0xc4, 0x80};
2044
2045/*
2046 The cbor.me parse of this.
2047 55799(55799(55799({6(7(-23)): 5859837686836516696(7({7(-20): 11({17(-18): 17(17(17("Organization"))),
2048 9(-17): 773("SSG"), -15: 4(5(6(7(8(9(10(11(12(13(14(15("Confusion")))))))))))), 17(-16): 17("San Diego"),
2049 17(-14): 17("US")}), 23(-19): 19({-11: 9({-9: -7}),
2050 90599561(90599561(90599561(-10))): 12(h'0102030405060708090A')})})),
2051 16(-22): 23({11(8(7(-5))): 8(-3)})})))
2052 */
2053static uint8_t spCSRWithTags[] = {
2054 0xd9, 0xd9, 0xf7, 0xd9, 0xd9, 0xf7, 0xd9, 0xd9, 0xf7, 0xa2,
2055 0xc6, 0xc7, 0x36,
2056 0xdb, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0xc7, 0xa2,
2057 0xda, 0x00, 0x00, 0x00, 0x07, 0x33,
2058 0xcb, 0xa5,
2059 0xd1, 0x31,
2060 0xd1, 0xd1, 0xd1, 0x6c,
2061 0x4f, 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74, 0x69, 0x6f, 0x6e,
2062 0xc9, 0x30,
2063 0xd9, 0x03, 0x05, 0x63,
2064 0x53, 0x53, 0x47,
2065 0x2e,
2066 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf, 0x69,
2067 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73, 0x69, 0x6f, 0x6e,
2068 0xd1, 0x2f,
2069 0xd1, 0x69,
2070 0x53, 0x61, 0x6e, 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f,
2071 0xd1, 0x2d,
2072 0xd1, 0x62,
2073 0x55, 0x53,
2074 0xd7, 0x32,
2075 0xd3, 0xa2,
2076 0x2a,
2077 0xc9, 0xa1,
2078 0x28,
2079 0x26,
2080 0xda, 0x05, 0x66, 0x70, 0x89, 0xda, 0x05, 0x66, 0x70, 0x89, 0xda, 0x05, 0x66, 0x70, 0x89, 0x29,
2081 0xcc, 0x4a,
2082 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,0x07, 0x08, 0x09, 0x0a,
2083 0xd0, 0x35,
2084 0xd7, 0xa1,
2085 0xcb, 0xc8, 0xc7, 0x24,
2086 0xc8, 0x22};
2087
2088static int CheckCSRMaps(QCBORDecodeContext *pDC);
2089
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002090
2091int OptTagParseTest()
2092{
2093 QCBORDecodeContext DCtx;
2094 QCBORItem Item;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002095
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002096 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spOptTestInput), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002097
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002098 //-------------------------
2099 // This text matches the magic number tag and the fraction tag
2100 if(QCBORDecode_GetNext(&DCtx, &Item)) {
2101 return -2;
2102 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002103 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002104 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC)) {
2105 return -3;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002106 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002107
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002108 if(QCBORDecode_GetNext(&DCtx, &Item)) {
2109 return -4;
2110 }
2111 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
2112 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_FRACTION) ||
2113 Item.val.uCount != 2) {
2114 return -5;
2115 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002116
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002117 // --------------------------------
2118 // This test decodes the very large tag, but it is not in
2119 // any list so it is ignored.
2120 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEncodedLargeTag), QCBOR_DECODE_MODE_NORMAL);
2121 if(QCBORDecode_GetNext(&DCtx, &Item)) {
2122 return -6;
2123 }
2124 if(Item.uTagBits) {
2125 return -7;
2126 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002127
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002128 // ----------------------------------
2129 // This test sets up a caller-config list that includes the very large tage and then matches it.
2130 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEncodedLargeTag), QCBOR_DECODE_MODE_NORMAL);
2131 const uint64_t puList[] = {0x9192939495969798, 257};
2132 const QCBORTagListIn TL = {2, puList};
2133 QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002134
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002135 if(QCBORDecode_GetNext(&DCtx, &Item)) {
2136 return -8;
2137 }
2138 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
2139 !QCBORDecode_IsTagged(&DCtx, &Item, 0x9192939495969798) ||
2140 QCBORDecode_IsTagged(&DCtx, &Item, 257) ||
2141 QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_BIGFLOAT) ||
2142 Item.val.uCount != 0) {
2143 return -9;
2144 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002145
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002146 //------------------------
2147 // This test sets up a caller-configured list, and looks up something not in it
2148 const uint64_t puLongList[17] = {1,2,1};
2149 const QCBORTagListIn TLLong = {17, puLongList};
2150 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEncodedLargeTag), QCBOR_DECODE_MODE_NORMAL);
2151 QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TLLong);
2152 if(QCBORDecode_GetNext(&DCtx, &Item)) {
2153 return -11;
2154 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002155
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002156 // -----------------------
2157 // This tests retrievel of the full tag list
2158 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spLotsOfTags), QCBOR_DECODE_MODE_NORMAL);
2159 uint64_t puTags[16];
2160 QCBORTagListOut Out = {0, 4, puTags};
2161 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2162 return -12;
2163 }
2164 if(puTags[0] != 0x9192939495969798 ||
2165 puTags[1] != 0x88 ||
2166 puTags[2] != 0x05 ||
2167 puTags[3] != 0x04) {
2168 return -13;
2169 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002170
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002171 // ----------------------
2172 // This text if too small of an out list
2173 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spLotsOfTags), QCBOR_DECODE_MODE_NORMAL);
2174 QCBORTagListOut OutSmall = {0, 3, puTags};
2175 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &OutSmall) != QCBOR_ERR_TOO_MANY_TAGS) {
2176 return -14;
2177 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002178
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002179 // ---------------
2180 // Parse a version of the "CSR" that has had a ton of tags randomly inserted
2181 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRWithTags), QCBOR_DECODE_MODE_NORMAL);
2182 int n = CheckCSRMaps(&DCtx);
2183 if(n) {
2184 return n-2000;
2185 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002186
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002187 Out = (QCBORTagListOut){0,16, puTags};
2188 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRWithTags), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002189
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002190 const uint64_t puTagList[] = {773, 1, 90599561};
2191 const QCBORTagListIn TagList = {3, puTagList};
2192 QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TagList);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002193
2194
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002195 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2196 return -100;
2197 }
2198 if(Item.uDataType != QCBOR_TYPE_MAP ||
2199 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC) ||
2200 QCBORDecode_IsTagged(&DCtx, &Item, 90599561) ||
2201 QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_DATE_EPOCH) ||
2202 Item.val.uCount != 2 ||
2203 puTags[0] != CBOR_TAG_CBOR_MAGIC ||
2204 puTags[1] != CBOR_TAG_CBOR_MAGIC ||
2205 puTags[2] != CBOR_TAG_CBOR_MAGIC ||
2206 Out.uNumUsed != 3) {
2207 return -101;
2208 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002209
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002210 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2211 return -102;
2212 }
2213 if(Item.uDataType != QCBOR_TYPE_MAP ||
2214 QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC) ||
2215 QCBORDecode_IsTagged(&DCtx, &Item, 6) ||
2216 QCBORDecode_IsTagged(&DCtx, &Item, 7) || // item is tagged 7, but 7 is not configured to be recognized
2217 Item.val.uCount != 2 ||
2218 puTags[0] != 5859837686836516696 ||
2219 puTags[1] != 7 ||
2220 Out.uNumUsed != 2) {
2221 return -103;
2222 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002223
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002224 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2225 return -104;
2226 }
2227 if(Item.uDataType != QCBOR_TYPE_MAP ||
2228 Item.uTagBits ||
2229 Item.val.uCount != 5 ||
2230 puTags[0] != 0x0b ||
2231 Out.uNumUsed != 1) {
2232 return -105;
2233 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002234
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002235 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2236 return -106;
2237 }
2238 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
2239 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_COSE_MAC0) ||
2240 Item.val.string.len != 12 ||
2241 puTags[0] != CBOR_TAG_COSE_MAC0 ||
2242 puTags[1] != CBOR_TAG_COSE_MAC0 ||
2243 puTags[2] != CBOR_TAG_COSE_MAC0 ||
2244 Out.uNumUsed != 3) {
2245 return -105;
2246 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002247
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002248 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2249 return -107;
2250 }
2251 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
2252 !QCBORDecode_IsTagged(&DCtx, &Item, 773) ||
2253 Item.val.string.len != 3 ||
2254 puTags[0] != 773 ||
2255 Out.uNumUsed != 1) {
2256 return -108;
2257 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002258
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002259 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2260 return -109;
2261 }
2262 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
2263 !QCBORDecode_IsTagged(&DCtx, &Item, 4) ||
2264 Item.val.string.len != 9 ||
2265 puTags[0] != 4 ||
2266 puTags[11] != 0x0f ||
2267 Out.uNumUsed != 12) {
2268 return -110;
2269 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002270
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002271 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2272 return -111;
2273 }
2274 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
2275 !QCBORDecode_IsTagged(&DCtx, &Item, 17) ||
2276 Item.val.string.len != 9 ||
2277 puTags[0] != 17 ||
2278 Out.uNumUsed != 1) {
2279 return -112;
2280 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002281
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002282 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2283 return -111;
2284 }
2285 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
2286 !QCBORDecode_IsTagged(&DCtx, &Item, 17) ||
2287 Item.val.string.len != 2 ||
2288 puTags[0] != 17 ||
2289 Out.uNumUsed != 1) {
2290 return -112;
2291 }
2292
2293 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2294 return -113;
2295 }
2296 if(Item.uDataType != QCBOR_TYPE_MAP ||
2297 QCBORDecode_IsTagged(&DCtx, &Item, 19) ||
2298 Item.val.uCount != 2 ||
2299 puTags[0] != 19 ||
2300 Out.uNumUsed != 1) {
2301 return -114;
2302 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002303
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002304 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2305 return -115;
2306 }
2307 if(Item.uDataType != QCBOR_TYPE_MAP ||
2308 QCBORDecode_IsTagged(&DCtx, &Item, 9) ||
2309 Item.uTagBits ||
2310 Item.val.uCount != 1 ||
2311 puTags[0] != 9 ||
2312 Out.uNumUsed != 1) {
2313 return -116;
2314 }
2315
2316 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2317 return -116;
2318 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002319 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002320 Item.val.int64 != -7 ||
2321 Item.uTagBits ||
2322 Out.uNumUsed != 0) {
2323 return -117;
2324 }
2325
2326 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2327 return -118;
2328 }
2329 if(Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
2330 Item.val.string.len != 10 ||
2331 Item.uTagBits ||
2332 puTags[0] != 12 ||
2333 Out.uNumUsed != 1) {
2334 return -119;
2335 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002336
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002337 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2338 return -120;
2339 }
2340 if(Item.uDataType != QCBOR_TYPE_MAP ||
2341 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_ENC_AS_B16) ||
2342 Item.val.uCount != 1 ||
2343 puTags[0] != 0x17 ||
2344 Out.uNumUsed != 1) {
2345 return -121;
2346 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002347
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002348 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2349 return -122;
2350 }
2351 if(Item.uDataType != QCBOR_TYPE_INT64 ||
2352 QCBORDecode_IsTagged(&DCtx, &Item, 8) ||
2353 Item.val.int64 != -3 ||
2354 puTags[0] != 8 ||
2355 Out.uNumUsed != 1) {
2356 return -123;
2357 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002358
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002359 if(QCBORDecode_Finish(&DCtx)) {
2360 return -124;
2361 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002362
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002363 return 0;
2364}
2365
2366
2367
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002368
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002369static uint8_t spBigNumInput[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002370 0x83,
2371 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2372 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2373 0xA4,
2374 0x63, 0x42, 0x4E, 0x2B,
2375 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2376 0x18, 0x40,
2377 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2378 0x63, 0x42, 0x4E, 0x2D,
2379 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2380 0x38, 0x3F,
2381 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
2382
2383
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002384static uint8_t spBigNum[] = {0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002385
2386
2387int BignumParseTest()
2388{
2389 QCBORDecodeContext DCtx;
2390 QCBORItem Item;
2391 int nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002392
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002393 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNumInput), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002394
2395
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002396 //
2397 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
2398 return -1;
2399 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
2400 return -1;
2401 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002402
2403 //
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002404 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
2405 return -1;
2406 if(Item.uDataType != QCBOR_TYPE_POSBIGNUM ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002407 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002408 return -1;
2409 }
2410
2411 //
2412 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
2413 return -1;
2414 if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002415 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002416 return -1;
2417 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002418
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002419 //
2420 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
2421 return -1;
2422 if(Item.uDataType != QCBOR_TYPE_MAP) {
2423 return -1;
2424 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002425
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002426 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
2427 return -1;
2428 if(Item.uDataType != QCBOR_TYPE_POSBIGNUM ||
2429 Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002430 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002431 return -1;
2432 }
2433
2434 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
2435 return -1;
2436 if(Item.uDataType != QCBOR_TYPE_POSBIGNUM ||
2437 Item.uLabelType != QCBOR_TYPE_INT64 ||
2438 Item.label.int64 != 64 ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002439 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002440 return -1;
2441 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002442
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002443 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
2444 return -1;
2445 if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM ||
2446 Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002447 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002448 return -1;
2449 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002450
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002451 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
2452 return -1;
2453 if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM ||
2454 Item.uLabelType != QCBOR_TYPE_INT64 ||
2455 Item.label.int64 != -64 ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002456 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002457 return -1;
2458 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002459
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002460 return 0;
2461}
2462
2463
2464
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302465static int CheckItemWithIntLabel(QCBORDecodeContext *pCtx, uint8_t uDataType, uint8_t uNestingLevel, uint8_t uNextNest, int64_t nLabel, QCBORItem *pItem)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002466{
2467 QCBORItem Item;
2468 int nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002469
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002470 if((nCBORError = QCBORDecode_GetNext(pCtx, &Item))) return -1;
2471 if(Item.uDataType != uDataType) return -1;
2472 if(uNestingLevel > 0) {
2473 if(Item.uLabelType != QCBOR_TYPE_INT64 && Item.uLabelType != QCBOR_TYPE_UINT64) return -1;
2474 if(Item.uLabelType == QCBOR_TYPE_INT64) {
2475 if(Item.label.int64 != nLabel) return -1;
2476 } else {
Laurence Lundblade570fab52018-10-13 18:28:27 +08002477 if(Item.label.uint64 != (uint64_t)nLabel) return -1;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002478 }
2479 }
2480 if(Item.uNestingLevel != uNestingLevel) return -1;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302481 if(Item.uNextNestLevel != uNextNest) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002482
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002483 if(pItem) {
2484 *pItem = Item;
2485 }
2486 return 0;
2487}
2488
2489
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002490// Same code checks definite and indefinite length versions of the map
2491static int CheckCSRMaps(QCBORDecodeContext *pDC)
2492{
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302493 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 0, 1, 0, NULL)) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002494
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302495 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 1, 2, -23, NULL)) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002496
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302497 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 2, 3, -20, NULL)) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002498
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302499 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -18, NULL)) return -1;
2500 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -17, NULL)) return -1;
2501 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -15, NULL)) return -1;
2502 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -16, NULL)) return -1;
2503 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 2, -14, NULL)) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002504
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302505 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 2, 3, -19, NULL)) return -1;
2506 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 3, 4, -11, NULL)) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002507
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302508 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_INT64, 4, 3, -9, NULL)) return -1;
2509 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_BYTE_STRING, 3, 1, -10, NULL)) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002510
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302511 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 1, 2, -22, NULL)) return -1;
2512 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_INT64, 2, 0, -5, NULL)) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002513
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002514 if(QCBORDecode_Finish(pDC)) return -2;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002515
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002516 return 0;
2517}
2518
2519
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002520/*
2521// cbor.me decoded output
2522{
2523 -23: {
2524 -20: {
2525 -18: "Organization",
2526 -17: "SSG",
2527 -15: "Confusion",
2528 -16: "San Diego",
2529 -14: "US"
2530 },
2531 -19: {
2532 -11: {
2533 -9: -7
2534 },
2535 -10: '\u0001\u0002\u0003\u0004\u0005\u0006\a\b\t\n'
2536 }
2537 },
2538 -22: {
2539 -5: -3
2540 }
2541}
2542 */
2543
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002544
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002545static uint8_t spCSRInput[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002546 0xa2, 0x36, 0xa2, 0x33, 0xa5, 0x31, 0x6c, 0x4f,
2547 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74,
2548 0x69, 0x6f, 0x6e, 0x30, 0x63, 0x53, 0x53, 0x47,
2549 0x2e, 0x69, 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73,
2550 0x69, 0x6f, 0x6e, 0x2f, 0x69, 0x53, 0x61, 0x6e,
2551 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f, 0x2d, 0x62,
2552 0x55, 0x53, 0x32, 0xa2, 0x2a, 0xa1, 0x28, 0x26,
2553 0x29, 0x4a, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
2554 0x07, 0x08, 0x09, 0x0a, 0x35, 0xa1, 0x24, 0x22};
2555
2556int NestedMapTest()
2557{
2558 QCBORDecodeContext DCtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002559
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002560 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002561
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002562 return CheckCSRMaps(&DCtx);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002563}
2564
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08002565
2566
2567int StringDecoderModeFailTest()
2568{
2569 QCBORDecodeContext DCtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002570
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08002571 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput), QCBOR_DECODE_MODE_MAP_STRINGS_ONLY);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002572
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08002573 QCBORItem Item;
2574 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002575
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08002576 if(QCBORDecode_GetNext(&DCtx, &Item)) {
2577 return -1;
2578 }
2579 if(Item.uDataType != QCBOR_TYPE_MAP) {
2580 return -2;
2581 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002582
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08002583 nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
2584 if(nCBORError != QCBOR_ERR_MAP_LABEL_TYPE) {
2585 return -3;
2586 }
2587
2588 return 0;
2589}
2590
2591
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002592// Same map as above, but using indefinite lengths
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002593static uint8_t spCSRInputIndefLen[] = {
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002594 0xbf, 0x36, 0xbf, 0x33, 0xbf, 0x31, 0x6c, 0x4f,
2595 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74,
2596 0x69, 0x6f, 0x6e, 0x30, 0x63, 0x53, 0x53, 0x47,
2597 0x2e, 0x69, 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73,
2598 0x69, 0x6f, 0x6e, 0x2f, 0x69, 0x53, 0x61, 0x6e,
2599 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f, 0x2d, 0x62,
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002600 0x55, 0x53, 0xff, 0x32, 0xbf, 0x2a, 0xbf, 0x28,
2601 0x26, 0xff, 0x29, 0x4a, 0x01, 0x02, 0x03, 0x04,
2602 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0xff, 0xff,
2603 0x35, 0xbf, 0x24, 0x22, 0xff, 0xff};
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002604
2605int NestedMapTestIndefLen()
2606{
2607 QCBORDecodeContext DCtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002608
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002609 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInputIndefLen), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002610
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002611 return CheckCSRMaps(&DCtx);
2612}
2613
2614
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002615
Laurence Lundblade17ede402018-10-13 11:43:07 +08002616static UsefulBufC make_nested_indefinite_arrays(int n, UsefulBuf Storage)
2617{
2618 UsefulOutBuf UOB;
2619 UsefulOutBuf_Init(&UOB, Storage);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002620
Laurence Lundblade17ede402018-10-13 11:43:07 +08002621 int i;
2622 for(i = 0; i < n; i++) {
2623 UsefulOutBuf_AppendByte(&UOB, 0x9f);
2624 }
2625
2626 for(i = 0; i < n; i++) {
2627 UsefulOutBuf_AppendByte(&UOB, 0xff);
2628 }
2629 return UsefulOutBuf_OutUBuf(&UOB);
2630}
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002631
2632
Laurence Lundblade17ede402018-10-13 11:43:07 +08002633static int parse_indeflen_nested(UsefulBufC Nested, int nNestLevel)
2634{
2635 QCBORDecodeContext DC;
2636 QCBORDecode_Init(&DC, Nested, 0);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002637
Laurence Lundblade17ede402018-10-13 11:43:07 +08002638 int j;
2639 for(j = 0; j < nNestLevel; j++) {
2640 QCBORItem Item;
2641 int nReturn = QCBORDecode_GetNext(&DC, &Item);
2642 if(j >= QCBOR_MAX_ARRAY_NESTING) {
2643 // Should be in error
2644 if(nReturn != QCBOR_ERR_ARRAY_NESTING_TOO_DEEP) {
2645 return -4;
2646 } else {
2647 return 0; // Decoding doesn't recover after an error
2648 }
2649 } else {
2650 // Should be no error
2651 if(nReturn) {
2652 return -9; // Should not have got an error
2653 }
2654 }
2655 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
2656 return -7;
2657 }
2658 }
2659 int nReturn = QCBORDecode_Finish(&DC);
2660 if(nReturn) {
2661 return -3;
2662 }
2663 return 0;
2664}
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002665
2666
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302667int IndefiniteLengthNestTest()
Laurence Lundblade17ede402018-10-13 11:43:07 +08002668{
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05302669 UsefulBuf_MAKE_STACK_UB(Storage, 50);
Laurence Lundblade17ede402018-10-13 11:43:07 +08002670 int i;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002671 for(i=1; i < QCBOR_MAX_ARRAY_NESTING+4; i++) {
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08002672 const UsefulBufC Nested = make_nested_indefinite_arrays(i, Storage);
Laurence Lundblade17ede402018-10-13 11:43:07 +08002673 int nReturn = parse_indeflen_nested(Nested, i);
2674 if(nReturn) {
2675 return nReturn;
2676 }
2677 }
2678 return 0;
2679}
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002680
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002681
Laurence Lundblade6de37062018-10-15 12:22:42 +05302682
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002683static const uint8_t spIndefiniteArray[] = {0x9f, 0x01, 0x82, 0x02, 0x03, 0xff}; // [1, [2, 3]]
2684static const uint8_t spIndefiniteArrayBad1[] = {0x9f}; // No closing break
2685static const uint8_t spIndefiniteArrayBad2[] = {0x9f, 0x9f, 0x02, 0xff}; // Not enough closing breaks
2686static const uint8_t spIndefiniteArrayBad3[] = {0x9f, 0x02, 0xff, 0xff}; // Too many closing breaks
2687static const uint8_t spIndefiniteArrayBad4[] = {0x81, 0x9f}; // Unclosed indeflen inside def len
2688static const uint8_t spIndefiniteArrayBad5[] = {0x9f, 0xd1, 0xff}; // confused tag
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002689
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302690int IndefiniteLengthArrayMapTest()
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002691{
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002692 int nResult;
2693 // --- first test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002694 UsefulBufC IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArray);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002695
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002696 // Decode it and see if it is OK
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05302697 UsefulBuf_MAKE_STACK_UB(MemPool, 150);
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002698 QCBORDecodeContext DC;
2699 QCBORItem Item;
2700 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002701
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002702 QCBORDecode_SetMemPool(&DC, MemPool, false);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002703
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002704 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05302705
2706 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
2707 Item.uNestingLevel != 0 ||
2708 Item.uNextNestLevel != 1) {
2709 return -111;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002710 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002711
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002712 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05302713 if(Item.uDataType != QCBOR_TYPE_INT64 ||
2714 Item.uNestingLevel != 1 ||
2715 Item.uNextNestLevel != 1) {
2716 return -2;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002717 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002718
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002719 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05302720 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
2721 Item.uNestingLevel != 1 ||
2722 Item.uNextNestLevel != 2) {
2723 return -3;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002724 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002725
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002726 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade12b495d2018-12-17 11:15:54 -08002727 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade6de37062018-10-15 12:22:42 +05302728 Item.uNestingLevel != 2 ||
2729 Item.uNextNestLevel != 2) {
2730 return -4;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002731 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002732
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002733 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade12b495d2018-12-17 11:15:54 -08002734 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade6de37062018-10-15 12:22:42 +05302735 Item.uNestingLevel != 2 ||
2736 Item.uNextNestLevel != 0) {
2737 return -5;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002738 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002739
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002740 if(QCBORDecode_Finish(&DC)) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05302741 return -6;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002742 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002743
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002744 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002745 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad1);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002746
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002747 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002748
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002749 QCBORDecode_SetMemPool(&DC, MemPool, false);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002750
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002751 nResult = QCBORDecode_GetNext(&DC, &Item);
2752 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05302753 return -7;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002754 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002755
Laurence Lundblade570fab52018-10-13 18:28:27 +08002756 nResult = QCBORDecode_Finish(&DC);
Laurence Lundblade6de37062018-10-15 12:22:42 +05302757 if(nResult != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
2758 return -8;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002759 }
2760
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002761
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002762 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002763 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad2);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002764
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002765 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002766
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002767 QCBORDecode_SetMemPool(&DC, MemPool, false);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002768
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002769 nResult = QCBORDecode_GetNext(&DC, &Item);
2770 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05302771 return -9;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002772 }
2773
2774 nResult = QCBORDecode_GetNext(&DC, &Item);
2775 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05302776 return -10;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002777 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002778
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002779 nResult = QCBORDecode_GetNext(&DC, &Item);
2780 if(nResult || Item.uDataType != QCBOR_TYPE_INT64) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05302781 return -11;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002782 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002783
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002784 nResult = QCBORDecode_Finish(&DC);
Laurence Lundblade6de37062018-10-15 12:22:42 +05302785 if(nResult != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
2786 return -12;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002787 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002788
2789
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002790 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002791 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad3);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002792
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002793 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002794
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002795 QCBORDecode_SetMemPool(&DC, MemPool, false);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002796
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002797 nResult = QCBORDecode_GetNext(&DC, &Item);
2798 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05302799 return -13;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002800 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002801
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002802 nResult = QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05302803 if(nResult != QCBOR_ERR_BAD_BREAK) {
2804 return -14;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002805 }
Laurence Lundblade6de37062018-10-15 12:22:42 +05302806
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002807
Laurence Lundblade570fab52018-10-13 18:28:27 +08002808 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002809 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad4);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002810
Laurence Lundblade570fab52018-10-13 18:28:27 +08002811 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002812
Laurence Lundblade570fab52018-10-13 18:28:27 +08002813 QCBORDecode_SetMemPool(&DC, MemPool, false);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002814
Laurence Lundblade570fab52018-10-13 18:28:27 +08002815 nResult = QCBORDecode_GetNext(&DC, &Item);
2816 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05302817 return -15;
Laurence Lundblade570fab52018-10-13 18:28:27 +08002818 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002819
Laurence Lundblade570fab52018-10-13 18:28:27 +08002820 nResult = QCBORDecode_GetNext(&DC, &Item);
2821 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05302822 return -16;
Laurence Lundblade570fab52018-10-13 18:28:27 +08002823 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002824
Laurence Lundblade570fab52018-10-13 18:28:27 +08002825 nResult = QCBORDecode_Finish(&DC);
Laurence Lundblade6de37062018-10-15 12:22:42 +05302826 if(nResult != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
2827 return -17;
Laurence Lundblade570fab52018-10-13 18:28:27 +08002828 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002829
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302830 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002831 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad5);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002832
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302833 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002834
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302835 QCBORDecode_SetMemPool(&DC, MemPool, false);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002836
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302837 nResult = QCBORDecode_GetNext(&DC, &Item);
2838 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05302839 return -18;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302840 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002841
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302842 nResult = QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05302843 if(nResult != QCBOR_ERR_BAD_BREAK) {
2844 return -19;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302845 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002846
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002847 return 0;
2848}
2849
Laurence Lundblade17ede402018-10-13 11:43:07 +08002850
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002851static const uint8_t spIndefiniteLenString[] = {
Laurence Lundblade17ede402018-10-13 11:43:07 +08002852 0x81, // Array of length one
2853 0x7f, // text string marked with indefinite length
2854 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment
2855 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment
2856 0xff // ending break
2857};
2858
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002859static const uint8_t spIndefiniteLenStringBad2[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302860 0x81, // Array of length one
2861 0x7f, // text string marked with indefinite length
2862 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment
2863 0x44, 0x6d, 0x69, 0x6e, 0x67, // second segment of wrong type
2864 0xff // ending break
2865};
2866
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002867static const uint8_t spIndefiniteLenStringBad3[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302868 0x81, // Array of length one
2869 0x7f, // text string marked with indefinite length
2870 0x01, 0x02, // Not a string
2871 0xff // ending break
2872};
2873
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002874static const uint8_t spIndefiniteLenStringBad4[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302875 0x81, // Array of length one
2876 0x7f, // text string marked with indefinite length
2877 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment
2878 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment
2879 // missing end of string
2880};
2881
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002882static const uint8_t spIndefiniteLenStringLabel[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302883 0xa1, // Array of length one
2884 0x7f, // text string marked with indefinite length
2885 0x65, 0x73, 0x74, 0x72, 0x75, 0x75, // first segment
2886 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment
2887 0xff, // ending break
2888 0x01 // integer being labeled.
2889};
2890
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08002891static UsefulBufC MakeIndefiniteBigBstr(UsefulBuf Storage) // TODO: size this
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302892{
2893 UsefulOutBuf UOB;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002894
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302895 UsefulOutBuf_Init(&UOB, Storage);
2896 UsefulOutBuf_AppendByte(&UOB, 0x81);
2897 UsefulOutBuf_AppendByte(&UOB, 0x5f);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002898
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302899 int i = 0;
2900 for(int nChunkSize = 1; nChunkSize <= 128; nChunkSize *= 2) {
2901 UsefulOutBuf_AppendByte(&UOB, 0x58);
2902 UsefulOutBuf_AppendByte(&UOB, (uint8_t)nChunkSize);
2903 for(int j = 0; j < nChunkSize; j++ ) {
2904 UsefulOutBuf_AppendByte(&UOB, i);
2905 i++;
2906 }
2907 }
2908 UsefulOutBuf_AppendByte(&UOB, 0xff);
2909
2910 return UsefulOutBuf_OutUBuf(&UOB);
2911}
2912
2913static int CheckBigString(UsefulBufC BigString)
2914{
2915 if(BigString.len != 255) {
2916 return 1;
2917 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002918
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302919 for(uint8_t i = 0; i < 255; i++){
2920 if(((const uint8_t *)BigString.ptr)[i] != i) {
2921 return 1;
2922 }
2923 }
2924 return 0;
2925}
2926
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05302927
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302928int IndefiniteLengthStringTest()
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302929{
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302930 QCBORDecodeContext DC;
2931 QCBORItem Item;
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05302932 // big enough for MakeIndefiniteBigBstr() + MemPool overhead
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08002933 UsefulBuf_MAKE_STACK_UB(MemPool, 350);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002934
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302935 // --- Simple normal indefinite length string ------
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002936 UsefulBufC IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenString);
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302937 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002938
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05302939 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302940 return -1;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302941 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002942
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302943 if(QCBORDecode_GetNext(&DC, &Item)) {
2944 return -2;
2945 }
2946 if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.uDataAlloc) {
2947 return -3;
2948 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002949
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302950 if(QCBORDecode_GetNext(&DC, &Item)) {
2951 return -4;
2952 }
2953 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || !Item.uDataAlloc) {
2954 return -5;
2955 }
2956 if(QCBORDecode_Finish(&DC)) {
2957 return -6;
2958 }
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302959
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302960 // ----- types mismatch ---
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002961 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad2), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002962
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302963 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
2964 return -7;
2965 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002966
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302967 if(QCBORDecode_GetNext(&DC, &Item)) {
2968 return -8;
2969 }
2970 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
2971 return -9;
2972 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002973
Laurence Lundblade30816f22018-11-10 13:40:22 +07002974 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_INDEFINITE_STRING_CHUNK) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302975 return -10;
2976 }
2977
2978 // ----- not a string ---
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002979 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad3), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002980
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302981 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
2982 return -11;
2983 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002984
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302985 if(QCBORDecode_GetNext(&DC, &Item)) {
2986 return -12;
2987 }
2988 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
2989 return -13;
2990 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002991
Laurence Lundblade30816f22018-11-10 13:40:22 +07002992 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_INDEFINITE_STRING_CHUNK) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302993 return -14;
2994 }
2995
2996 // ----- no end -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002997 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad4), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002998
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302999 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
3000 return -15;
3001 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003002
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303003 if(QCBORDecode_GetNext(&DC, &Item)) {
3004 return -16;
3005 }
3006 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
3007 return -17;
3008 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003009
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303010 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_HIT_END) {
3011 return -18;
3012 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003013
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303014 // ------ Don't set a string allocator and see an error -----
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303015 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003016
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303017 QCBORDecode_GetNext(&DC, &Item);
3018 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303019 return -19;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303020 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003021
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303022 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_NO_STRING_ALLOCATOR) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303023 return -20;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303024 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003025
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303026 // ----- Mempool is way too small -----
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003027 UsefulBuf_MAKE_STACK_UB(MemPoolTooSmall, QCBOR_DECODE_MIN_MEM_POOL_SIZE-1);
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303028
3029 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
3030 if(!QCBORDecode_SetMemPool(&DC, MemPoolTooSmall, false)) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303031 return -21;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303032 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003033
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303034 // ----- Mempool is way too small -----
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05303035 UsefulBuf_MAKE_STACK_UB(BigIndefBStrStorage, 290);
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08003036 const UsefulBufC BigIndefBStr = MakeIndefiniteBigBstr(BigIndefBStrStorage);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003037
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05303038 UsefulBuf_MAKE_STACK_UB(MemPoolSmall, 80); // 80 is big enough for MemPool overhead, but not BigIndefBStr
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003039
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303040 QCBORDecode_Init(&DC, BigIndefBStr, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303041 if(QCBORDecode_SetMemPool(&DC, MemPoolSmall, false)) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303042 return -22;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303043 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003044
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303045 QCBORDecode_GetNext(&DC, &Item);
3046 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303047 return -23;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303048 }
Laurence Lundblade30816f22018-11-10 13:40:22 +07003049 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_STRING_ALLOCATE) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303050 return -24;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303051 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003052
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303053 // ---- big bstr -----
3054 QCBORDecode_Init(&DC, BigIndefBStr, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003055
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303056 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
3057 return -25;
3058 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003059
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05303060 if(QCBORDecode_GetNext(&DC, &Item)) {
3061 return -26;
3062 }
3063 if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.uDataAlloc) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303064 return -26;
3065 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003066
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303067 if(QCBORDecode_GetNext(&DC, &Item)) {
3068 return -27;
3069 }
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05303070 if(Item.uDataType != QCBOR_TYPE_BYTE_STRING || !Item.uDataAlloc || Item.uNestingLevel != 1) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303071 return -28;
3072 }
3073 if(CheckBigString(Item.val.string)) {
3074 return -3;
3075 }
3076 if(QCBORDecode_Finish(&DC)) {
3077 return -29;
3078 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003079
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303080 // --- label is an indefinite length string ------
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003081 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringLabel), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003082
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303083 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
3084 return -30;
3085 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003086
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303087 QCBORDecode_GetNext(&DC, &Item);
3088 if(Item.uDataType != QCBOR_TYPE_MAP) {
3089 return -31;
3090 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003091
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303092 if(QCBORDecode_GetNext(&DC, &Item)){
3093 return -32;
3094 }
3095 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || Item.uDataType != QCBOR_TYPE_INT64 ||
3096 Item.uDataAlloc || !Item.uLabelAlloc ||
3097 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("struuming"))) {
3098 return -33;
3099 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003100
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303101 if(QCBORDecode_Finish(&DC)) {
3102 return -34;
3103 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003104
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003105 return 0;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003106}
3107
3108
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303109int AllocAllStringsTest()
3110{
3111 QCBORDecodeContext DC;
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003112 QCBORError nCBORError;
3113
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003114
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303115 // First test, use the "CSRMap" as easy input and checking
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003116 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003117
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003118 UsefulBuf_MAKE_STACK_UB(Pool, sizeof(spCSRInput) + QCBOR_DECODE_MIN_MEM_POOL_SIZE);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003119
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003120 nCBORError = QCBORDecode_SetMemPool(&DC, Pool, 1); // Turn on copying.
3121 if(nCBORError) {
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303122 return -1;
3123 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003124
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003125 if(CheckCSRMaps(&DC)) {
3126 return -2;
3127 }
3128
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303129 // Next parse, save pointers to a few strings, destroy original and see all is OK.
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003130 UsefulBuf_MAKE_STACK_UB(CopyOfStorage, sizeof(pValidMapEncoded) + QCBOR_DECODE_MIN_MEM_POOL_SIZE);
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08003131 const UsefulBufC CopyOf = UsefulBuf_Copy(CopyOfStorage, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded));
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003132
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303133 QCBORDecode_Init(&DC, CopyOf, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08003134 UsefulBuf_Set(Pool, '/');
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303135 QCBORDecode_SetMemPool(&DC, Pool, 1); // Turn on copying.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003136
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303137 QCBORItem Item1, Item2, Item3, Item4;
3138 if((nCBORError = QCBORDecode_GetNext(&DC, &Item1)))
3139 return nCBORError;
3140 if(Item1.uDataType != QCBOR_TYPE_MAP ||
3141 Item1.val.uCount != 3)
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003142 return -3;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303143 if((nCBORError = QCBORDecode_GetNext(&DC, &Item1)))
3144 return nCBORError;
3145 if((nCBORError = QCBORDecode_GetNext(&DC, &Item2)))
3146 return nCBORError;
3147 if((nCBORError = QCBORDecode_GetNext(&DC, &Item3)))
3148 return nCBORError;
3149 if((nCBORError = QCBORDecode_GetNext(&DC, &Item4)))
3150 return nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003151
Laurence Lundblade05ec57b2018-10-21 01:50:03 +05303152 UsefulBuf_Set(CopyOfStorage, '_');
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003153
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303154 if(Item1.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303155 Item1.uDataType != QCBOR_TYPE_INT64 ||
3156 Item1.val.int64 != 42 ||
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003157 Item1.uDataAlloc != 0 ||
3158 Item1.uLabelAlloc == 0 ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09003159 UsefulBuf_Compare(Item1.label.string, UsefulBuf_FromSZ("first integer"))) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003160 return -4;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09003161 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003162
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303163
3164 if(Item2.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09003165 UsefulBuf_Compare(Item2.label.string, UsefulBuf_FromSZ("an array of two strings")) ||
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303166 Item2.uDataType != QCBOR_TYPE_ARRAY ||
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003167 Item2.uDataAlloc != 0 ||
3168 Item2.uLabelAlloc == 0 ||
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303169 Item2.val.uCount != 2)
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003170 return -5;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003171
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303172 if(Item3.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003173 Item3.uDataAlloc == 0 ||
3174 Item3.uLabelAlloc != 0 ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09003175 UsefulBuf_Compare(Item3.val.string, UsefulBuf_FromSZ("string1"))) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003176 return -6;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09003177 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003178
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303179 if(Item4.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003180 Item4.uDataAlloc == 0 ||
3181 Item4.uLabelAlloc != 0 ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09003182 UsefulBuf_Compare(Item4.val.string, UsefulBuf_FromSZ("string2"))) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003183 return -7;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09003184 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003185
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303186 // Next parse with a pool that is too small
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003187 UsefulBuf_MAKE_STACK_UB(SmallPool, QCBOR_DECODE_MIN_MEM_POOL_SIZE + 1);
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05303188 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303189 QCBORDecode_SetMemPool(&DC, SmallPool, 1); // Turn on copying.
3190 if((nCBORError = QCBORDecode_GetNext(&DC, &Item1)))
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003191 return -8;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303192 if(Item1.uDataType != QCBOR_TYPE_MAP ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09003193 Item1.val.uCount != 3) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003194 return -9;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09003195 }
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303196 if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item1))){
3197 if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item2))) {
3198 if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item3))) {
3199 nCBORError = QCBORDecode_GetNext(&DC, &Item4);
3200 }
3201 }
3202 }
Laurence Lundblade30816f22018-11-10 13:40:22 +07003203 if(nCBORError != QCBOR_ERR_STRING_ALLOCATE) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003204 return -10;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303205 }
3206
3207 return 0;
3208}
3209
Laurence Lundbladef6531662018-12-04 10:42:22 +09003210
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303211
3212int MemPoolTest(void)
Laurence Lundblade0155b622018-10-12 20:04:37 +08003213{
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003214 // Set up the decoder with a tiny bit of CBOR to parse because
3215 // nothing can be done with it unless that is set up.
Laurence Lundbladef6531662018-12-04 10:42:22 +09003216 QCBORDecodeContext DC;
3217 const uint8_t pMinimalCBOR[] = {0xa0}; // One empty map
3218 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pMinimalCBOR),0);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003219
Laurence Lundbladef6531662018-12-04 10:42:22 +09003220 // Set up an memory pool of 100 bytes
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003221 // Then fish into the internals of the decode context
3222 // to get the allocator function so it can be called directly.
3223 // Also figure out how much pool is available for use
3224 // buy subtracting out the overhead.
Laurence Lundbladef6531662018-12-04 10:42:22 +09003225 UsefulBuf_MAKE_STACK_UB(Pool, 100);
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003226 QCBORError nError = QCBORDecode_SetMemPool(&DC, Pool, 0);
3227 if(nError) {
3228 return -9;
3229 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003230 QCBORStringAllocate pAlloc = DC.StringAllocator.pfAllocator;
3231 void *pAllocCtx = DC.StringAllocator.pAllocateCxt;
3232 size_t uAvailPool = Pool.len - QCBOR_DECODE_MIN_MEM_POOL_SIZE;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003233
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003234 // First test -- ask for one more byte than available and see failure
3235 UsefulBuf Allocated = (*pAlloc)(pAllocCtx, NULL, uAvailPool+1);
Laurence Lundbladef6531662018-12-04 10:42:22 +09003236 if(!UsefulBuf_IsNULL(Allocated)) {
3237 return -1;
3238 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003239
Laurence Lundbladef6531662018-12-04 10:42:22 +09003240 // Re do the set up for the next test that will do a successful alloc,
3241 // a fail, a free and then success
Laurence Lundbladef6531662018-12-04 10:42:22 +09003242 QCBORDecode_SetMemPool(&DC, Pool, 0);
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003243 pAlloc = DC.StringAllocator.pfAllocator;
3244 pAllocCtx = DC.StringAllocator.pAllocateCxt;
3245 uAvailPool = Pool.len - QCBOR_DECODE_MIN_MEM_POOL_SIZE;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003246
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003247 // Allocate one byte less than available and see success
3248 Allocated = (pAlloc)(pAllocCtx, NULL, uAvailPool-1);
Laurence Lundbladef6531662018-12-04 10:42:22 +09003249 if(UsefulBuf_IsNULL(Allocated)) { // expected to succeed
3250 return -2;
3251 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003252 // Ask for some more and see failure
3253 UsefulBuf Allocated2 = (*pAlloc)(pAllocCtx, NULL, uAvailPool/2);
Laurence Lundbladef6531662018-12-04 10:42:22 +09003254 if(!UsefulBuf_IsNULL(Allocated2)) { // expected to fail
3255 return -3;
3256 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003257 // Free the first allocate, retry the second and see success
3258 (*pAlloc)(pAllocCtx, Allocated.ptr, 0); // Free
3259 Allocated = (*pAlloc)(pAllocCtx, NULL, uAvailPool/2);
Laurence Lundbladef6531662018-12-04 10:42:22 +09003260 if(UsefulBuf_IsNULL(Allocated)) { // succeed because of the free
3261 return -4;
3262 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003263
Laurence Lundbladef6531662018-12-04 10:42:22 +09003264 // Re do set up for next test that involves a successful alloc,
3265 // and a successful realloc and a failed realloc
3266 QCBORDecode_SetMemPool(&DC, Pool, 0);
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003267 pAlloc = DC.StringAllocator.pfAllocator;
3268 pAllocCtx = DC.StringAllocator.pAllocateCxt;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003269
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003270 // Allocate half the pool and see success
3271 Allocated = (*pAlloc)(pAllocCtx, NULL, uAvailPool/2);
Laurence Lundbladef6531662018-12-04 10:42:22 +09003272 if(UsefulBuf_IsNULL(Allocated)) { // expected to succeed
3273 return -5;
3274 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003275 // Reallocate to take up the whole pool and see success
3276 Allocated2 = (*pAlloc)(pAllocCtx, Allocated.ptr, uAvailPool);
Laurence Lundbladef6531662018-12-04 10:42:22 +09003277 if(UsefulBuf_IsNULL(Allocated2)) {
3278 return -6;
3279 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003280 // Make sure its the same pointer and the size is right
Laurence Lundbladef6531662018-12-04 10:42:22 +09003281 if(Allocated2.ptr != Allocated.ptr || Allocated2.len != uAvailPool) {
3282 return -7;
3283 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003284 // Try to allocate more to be sure there is failure after a realloc
3285 UsefulBuf Allocated3 = (*pAlloc)(pAllocCtx, Allocated.ptr, uAvailPool+1);
3286 if(!UsefulBuf_IsNULL(Allocated3)) {
Laurence Lundbladef6531662018-12-04 10:42:22 +09003287 return -8;
3288 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003289
Laurence Lundbladef6531662018-12-04 10:42:22 +09003290 return 0;
3291}
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003292
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003293
3294/* Just enough of an allocator to test configuration of one */
3295static UsefulBuf AllocateTestFunction(void *pCtx, void *pOldMem, size_t uNewSize)
3296{
3297 (void)pOldMem; // unused variable
3298
3299 if(uNewSize) {
3300 // Assumes the context pointer is the buffer and
3301 // nothing too big will ever be asked for.
3302 // This is only good for this basic test!
3303 return (UsefulBuf) {pCtx, uNewSize};
3304 } else {
3305 return NULLUsefulBuf;
3306 }
3307}
3308
3309
3310int SetUpAllocatorTest(void)
3311{
3312 // Set up the decoder with a tiny bit of CBOR to parse because
3313 // nothing can be done with it unless that is set up.
3314 QCBORDecodeContext DC;
3315 const uint8_t pMinimalCBOR[] = {0x62, 0x48, 0x69}; // "Hi"
3316 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pMinimalCBOR),0);
3317
3318 uint8_t pAllocatorBuffer[50];
3319
3320 // This is really just to test that this call works.
3321 // The full functionality of string allocators is tested
3322 // elsewhere with the MemPool internal allocator.
3323 QCBORDecode_SetUpAllocator(&DC, AllocateTestFunction, pAllocatorBuffer, 1);
3324
3325 QCBORItem Item;
3326 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_SUCCESS) {
3327 return -1;
3328 }
3329
3330 if(Item.uDataAlloc == 0 ||
3331 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
3332 Item.val.string.ptr != pAllocatorBuffer) {
3333 return -2;
3334 }
3335
3336 if(QCBORDecode_Finish(&DC) != QCBOR_SUCCESS) {
3337 return -3;
3338 }
3339
3340 return 0;
3341}
3342