blob: 8a79bd812ba5d82e07d9cfaecfdc8470749f119b [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 Lundblade9e3651c2018-10-10 11:49:55 +0800596
Laurence Lundbladeb836efb2018-10-28 20:09:58 +0700597static uint8_t spDeepArrays[] = {0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x80};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800598
599int ParseDeepArrayTest()
600{
601 QCBORDecodeContext DCtx;
602 int nReturn = 0;
603 int i;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800604
Laurence Lundbladeb836efb2018-10-28 20:09:58 +0700605 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spDeepArrays), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800606
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800607 for(i = 0; i < 10; i++) {
608 QCBORItem Item;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800609
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800610 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
611 Item.uDataType != QCBOR_TYPE_ARRAY ||
612 Item.uNestingLevel != i) {
613 nReturn = -1;
614 break;
615 }
616 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800617
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800618 return(nReturn);
619}
620
Laurence Lundblade972e59c2018-11-11 15:57:23 +0700621// Big enough to test nesting to the depth of 24
622static uint8_t spTooDeepArrays[] = {0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81,
623 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81,
624 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81,
625 0x80};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800626
627int ParseTooDeepArrayTest()
628{
629 QCBORDecodeContext DCtx;
630 int nReturn = 0;
631 int i;
632 QCBORItem Item;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800633
634
Laurence Lundbladeb836efb2018-10-28 20:09:58 +0700635 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTooDeepArrays), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800636
Laurence Lundblade972e59c2018-11-11 15:57:23 +0700637 for(i = 0; i < QCBOR_MAX_ARRAY_NESTING1; i++) {
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800638
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800639 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
640 Item.uDataType != QCBOR_TYPE_ARRAY ||
641 Item.uNestingLevel != i) {
642 nReturn = -1;
643 break;
644 }
645 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800646
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800647 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_ARRAY_NESTING_TOO_DEEP)
648 nReturn = -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800649
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800650 return(nReturn);
651}
652
653
654
655
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800656int ShortBufferParseTest()
657{
Laurence Lundbladebb1062e2019-08-12 23:28:54 -0700658 int nResult = 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800659
Laurence Lundbladebb1062e2019-08-12 23:28:54 -0700660 for(int nNum = sizeof(spExpectedEncodedInts)-1; nNum; nNum--) {
661 QCBORDecodeContext DCtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800662
Laurence Lundbladebb1062e2019-08-12 23:28:54 -0700663 QCBORDecode_Init(&DCtx, (UsefulBufC){spExpectedEncodedInts, nNum}, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800664
Laurence Lundbladebb1062e2019-08-12 23:28:54 -0700665 const QCBORError nErr = IntegerValuesParseTestInternal(&DCtx);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800666
Laurence Lundbladebb1062e2019-08-12 23:28:54 -0700667 if(nErr != QCBOR_ERR_HIT_END && nErr != QCBOR_ERR_NO_MORE_ITEMS) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800668 nResult = -1;
669 goto Done;
670 }
671 }
672Done:
673 return nResult;
674}
675
676
Laurence Lundblade9e3651c2018-10-10 11:49:55 +0800677
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800678int ShortBufferParseTest2()
679{
680 uint8_t *pEncoded;
681 int nReturn;
682 size_t nEncodedLen;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800683
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800684 int64_t i1, i2;
685 size_t i3, i4;
686 const uint8_t *s3, *s4;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800687
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800688 nReturn = 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800689
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800690 if(CreateSimpleArray(23, 6000, &pEncoded, &nEncodedLen) < 0) {
691 return(-1);
692 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800693
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800694 for(nEncodedLen--; nEncodedLen; nEncodedLen--) {
695 int nResult = ParseOrderedArray(pEncoded, (uint32_t)nEncodedLen, &i1, &i2, &s3, &i3, &s4, &i4);
696 if(nResult == 0) {
697 nReturn = -1;
698 }
699 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800700
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800701 return(nReturn);
702}
703
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530704/*
705 Decode and thoroughly check a moderately complex
706 set of maps
707 */
Laurence Lundbladeea567ac2018-12-09 14:03:21 -0800708static int ParseMapTest1(QCBORDecodeMode nMode)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800709{
710 QCBORDecodeContext DCtx;
711 QCBORItem Item;
712 int nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800713
Laurence Lundbladeea567ac2018-12-09 14:03:21 -0800714 QCBORDecode_Init(&DCtx, (UsefulBufC){pValidMapEncoded, sizeof(pValidMapEncoded)}, nMode);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800715
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900716 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800717 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900718 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800719 if(Item.uDataType != QCBOR_TYPE_MAP ||
720 Item.val.uCount != 3)
721 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800722
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900723 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800724 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900725 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800726 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800727 Item.uDataType != QCBOR_TYPE_INT64 ||
728 Item.val.int64 != 42 ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530729 Item.uDataAlloc ||
730 Item.uLabelAlloc ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900731 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("first integer"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800732 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900733 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800734
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900735 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800736 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900737 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800738 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530739 Item.uDataAlloc ||
740 Item.uLabelAlloc ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900741 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("an array of two strings")) ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800742 Item.uDataType != QCBOR_TYPE_ARRAY ||
743 Item.val.uCount != 2)
744 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800745
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900746 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800747 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900748 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800749 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530750 Item.uDataAlloc ||
751 Item.uLabelAlloc ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900752 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("string1"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800753 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900754 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800755
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900756 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800757 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900758 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800759 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530760 Item.uDataAlloc ||
761 Item.uLabelAlloc ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900762 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("string2"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800763 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900764 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800765
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900766 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800767 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900768 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800769 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530770 Item.uDataAlloc ||
771 Item.uLabelAlloc ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900772 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("map in a map")) ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800773 Item.uDataType != QCBOR_TYPE_MAP ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900774 Item.val.uCount != 4) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800775 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900776 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800777
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900778 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800779 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900780 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800781 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900782 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("bytes 1"))||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800783 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530784 Item.uDataAlloc ||
785 Item.uLabelAlloc ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900786 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("xxxx"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800787 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900788 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800789
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900790 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800791 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900792 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800793 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900794 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("bytes 2")) ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800795 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530796 Item.uDataAlloc ||
797 Item.uLabelAlloc ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900798 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("yyyy"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800799 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900800 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800801
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900802 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800803 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900804 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800805 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530806 Item.uDataAlloc ||
807 Item.uLabelAlloc ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900808 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("another int")) ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800809 Item.uDataType != QCBOR_TYPE_INT64 ||
810 Item.val.int64 != 98)
811 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800812
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900813 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800814 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900815 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800816 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900817 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("text 2"))||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800818 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530819 Item.uDataAlloc ||
820 Item.uLabelAlloc ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900821 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("lies, damn lies and statistics"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800822 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900823 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800824
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800825 return 0;
826}
827
828
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900829/*
830 Decode and thoroughly check a moderately complex
831 set of maps
832 */
833int ParseMapAsArrayTest()
834{
835 QCBORDecodeContext DCtx;
836 QCBORItem Item;
837 int nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800838
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900839 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), QCBOR_DECODE_MODE_MAP_AS_ARRAY);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800840
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900841 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
842 return nCBORError;
843 }
Laurence Lundbladed61cbf32018-12-09 11:42:21 -0800844 if(Item.uDataType != QCBOR_TYPE_MAP_AS_ARRAY ||
845 Item.val.uCount != 6) {
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900846 return -1;
847 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800848
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900849 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
850 return nCBORError;
851 }
852 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
853 Item.uDataAlloc ||
854 Item.uLabelAlloc ||
855 Item.uLabelType != QCBOR_TYPE_NONE ||
856 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("first integer"))) {
857 return -2;
858 }
859
860 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
861 return nCBORError;
862 }
863 if(Item.uLabelType != QCBOR_TYPE_NONE ||
864 Item.uDataType != QCBOR_TYPE_INT64 ||
865 Item.val.int64 != 42 ||
866 Item.uDataAlloc ||
867 Item.uLabelAlloc) {
868 return -3;
869 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800870
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900871 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
872 return nCBORError;
873 }
874 if(Item.uLabelType != QCBOR_TYPE_NONE ||
875 Item.uDataAlloc ||
876 Item.uLabelAlloc ||
877 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("an array of two strings")) ||
878 Item.uDataType != QCBOR_TYPE_TEXT_STRING) {
879 return -4;
880 }
881
882 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
883 return nCBORError;
884 }
885 if(Item.uLabelType != QCBOR_TYPE_NONE ||
886 Item.uDataAlloc ||
887 Item.uLabelAlloc ||
888 Item.uDataType != QCBOR_TYPE_ARRAY ||
889 Item.val.uCount != 2) {
890 return -5;
891 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800892
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900893 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
894 return nCBORError;
895 }
896 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
897 Item.val.string.len != 7 ||
898 Item.uDataAlloc ||
899 Item.uLabelAlloc ||
900 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("string1"))) {
901 return -6;
902 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800903
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900904 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
905 return nCBORError;
906 }
907 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
908 Item.uDataAlloc ||
909 Item.uLabelAlloc ||
910 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("string2"))) {
911 return -7;
912 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800913
914
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900915 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
916 return nCBORError;
917 }
918 if(Item.uLabelType != QCBOR_TYPE_NONE ||
919 Item.uDataAlloc ||
920 Item.uLabelAlloc ||
921 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("map in a map"))) {
922 return -8;
923 }
924
925 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
926 return nCBORError;
927 }
928 if(Item.uLabelType != QCBOR_TYPE_NONE ||
929 Item.uDataAlloc ||
930 Item.uLabelAlloc ||
Laurence Lundbladed61cbf32018-12-09 11:42:21 -0800931 Item.uDataType != QCBOR_TYPE_MAP_AS_ARRAY ||
932 Item.val.uCount != 8) {
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900933 return -9;
934 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800935
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900936 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
937 return nCBORError;
938 }
939 if(Item.uLabelType != QCBOR_TYPE_NONE ||
940 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("bytes 1"))||
941 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
942 Item.uDataAlloc ||
943 Item.uLabelAlloc) {
944 return -10;
945 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800946
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900947 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
948 return nCBORError;
949 }
950 if(Item.uLabelType != QCBOR_TYPE_NONE ||
951 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
952 Item.uDataAlloc ||
953 Item.uLabelAlloc ||
954 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("xxxx"))) {
955 return -11;
956 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800957
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900958 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
959 return nCBORError;
960 }
961 if(Item.uLabelType != QCBOR_TYPE_NONE ||
962 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("bytes 2")) ||
963 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
964 Item.uDataAlloc ||
965 Item.uLabelAlloc) {
966 return -12;
967 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800968
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900969 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
970 return nCBORError;
971 }
972 if(Item.uLabelType != QCBOR_TYPE_NONE ||
973 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
974 Item.uDataAlloc ||
975 Item.uLabelAlloc ||
976 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("yyyy"))) {
977 return -13;
978 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800979
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900980 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
981 return nCBORError;
982 }
983 if(Item.uLabelType != QCBOR_TYPE_NONE ||
984 Item.uDataAlloc ||
985 Item.uLabelAlloc ||
986 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("another int")) ||
987 Item.uDataType != QCBOR_TYPE_TEXT_STRING) {
988 return -14;
989 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800990
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900991 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
992 return nCBORError;
993 }
994 if(Item.uLabelType != QCBOR_TYPE_NONE ||
995 Item.uDataAlloc ||
996 Item.uLabelAlloc ||
997 Item.uDataType != QCBOR_TYPE_INT64 ||
998 Item.val.int64 != 98) {
999 return -15;
1000 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001001
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001002 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
1003 return nCBORError;
1004 }
1005 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1006 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("text 2"))||
1007 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1008 Item.uDataAlloc ||
1009 Item.uLabelAlloc) {
1010 return -16;
1011 }
1012
1013 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
1014 return nCBORError;
1015 }
1016 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1017 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1018 Item.uDataAlloc ||
1019 Item.uLabelAlloc ||
1020 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("lies, damn lies and statistics"))) {
1021 return -17;
1022 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001023
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001024 return 0;
1025}
1026
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001027
1028/*
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301029 Fully or partially decode pValidMapEncoded. When
1030 partially decoding check for the right error code.
1031 How much partial decoding depends on nLevel.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001032
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301033 The partial decodes test error conditions of
1034 incomplete encoded input.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001035
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301036 This could be combined with the above test
1037 and made prettier and maybe a little more
1038 thorough.
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001039 */
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001040static int ExtraBytesTest(int nLevel)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001041{
1042 QCBORDecodeContext DCtx;
1043 QCBORItem Item;
1044 int nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001045
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001046 QCBORDecode_Init(&DCtx, (UsefulBufC){pValidMapEncoded, sizeof(pValidMapEncoded)}, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001047
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001048 if(nLevel < 1) {
1049 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_EXTRA_BYTES) {
1050 return -1;
1051 } else {
1052 return 0;
1053 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001054 }
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301055
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001056
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001057 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001058 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001059 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001060 if(Item.uDataType != QCBOR_TYPE_MAP ||
1061 Item.val.uCount != 3)
1062 return -1;
1063
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001064 if(nLevel < 2) {
1065 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
1066 return -1;
1067 } else {
1068 return 0;
1069 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001070 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001071
1072
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001073 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001074 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001075 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001076 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001077 Item.uDataType != QCBOR_TYPE_INT64 ||
1078 Item.val.uCount != 42 ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001079 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("first integer"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001080 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001081 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001082
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001083 if(nLevel < 3) {
1084 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
1085 return -1;
1086 } else {
1087 return 0;
1088 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001089 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001090
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001091 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001092 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001093 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001094 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001095 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("an array of two strings")) ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001096 Item.uDataType != QCBOR_TYPE_ARRAY ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001097 Item.val.uCount != 2) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001098 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001099 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001100
1101
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001102 if(nLevel < 4) {
1103 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
1104 return -1;
1105 } else {
1106 return 0;
1107 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001108 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001109
1110
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001111 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001112 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001113 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001114 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001115 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("string1"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001116 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001117 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001118
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001119 if(nLevel < 5) {
1120 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
1121 return -1;
1122 } else {
1123 return 0;
1124 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001125 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001126
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001127 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001128 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001129 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001130 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001131 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("string2"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001132 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001133 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001134
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001135 if(nLevel < 6) {
1136 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
1137 return -1;
1138 } else {
1139 return 0;
1140 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001141 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001142
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001143 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001144 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001145 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001146 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001147 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("map in a map")) ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001148 Item.uDataType != QCBOR_TYPE_MAP ||
1149 Item.val.uCount != 4)
1150 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001151
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001152 if(nLevel < 7) {
1153 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
1154 return -1;
1155 } else {
1156 return 0;
1157 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001158 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001159
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001160 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001161 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001162 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001163 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001164 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("bytes 1")) ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001165 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001166 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("xxxx"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001167 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001168 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001169
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001170 if(nLevel < 8) {
1171 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
1172 return -1;
1173 } else {
1174 return 0;
1175 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001176 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001177
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001178 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001179 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001180 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001181 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001182 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("bytes 2")) ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001183 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001184 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("yyyy"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001185 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001186 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001187
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001188 if(nLevel < 9) {
1189 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
1190 return -1;
1191 } else {
1192 return 0;
1193 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001194 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001195
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001196 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001197 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001198 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001199 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001200 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("another int")) ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001201 Item.uDataType != QCBOR_TYPE_INT64 ||
1202 Item.val.int64 != 98)
1203 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001204
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001205 if(nLevel < 10) {
1206 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
1207 return -1;
1208 } else {
1209 return 0;
1210 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001211 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001212
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001213 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001214 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001215 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001216 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001217 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("text 2"))||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001218 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001219 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("lies, damn lies and statistics"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001220 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001221 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001222
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301223 if(QCBORDecode_Finish(&DCtx)) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001224 return -1;
1225 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001226
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001227 return 0;
1228}
1229
1230
1231
1232
1233int ParseMapTest()
1234{
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301235 // Parse a moderatly complex map structure very thoroughl
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08001236 int n = ParseMapTest1(QCBOR_DECODE_MODE_NORMAL);
1237
1238 n = ParseMapTest1(QCBOR_DECODE_MODE_MAP_STRINGS_ONLY);
1239
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001240 if(!n) {
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001241 for(int i = 0; i < 10; i++) {
1242 n = ExtraBytesTest(i);
1243 if(n) {
1244 break;
1245 }
1246 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001247 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001248
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001249 return(n);
1250}
1251
1252
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07001253static 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 +08001254
1255int ParseSimpleTest()
1256{
1257 QCBORDecodeContext DCtx;
1258 QCBORItem Item;
1259 int nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001260
1261
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07001262 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSimpleValues), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001263
1264
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001265 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1266 return nCBORError;
1267 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
1268 Item.val.uCount != 10)
1269 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001270
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001271 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1272 return nCBORError;
1273 if(Item.uDataType != QCBOR_TYPE_FALSE)
1274 return -1;
1275
1276 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1277 return nCBORError;
1278 if(Item.uDataType != QCBOR_TYPE_TRUE)
1279 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001280
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001281 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1282 return nCBORError;
1283 if(Item.uDataType != QCBOR_TYPE_NULL)
1284 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001285
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001286 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1287 return nCBORError;
1288 if(Item.uDataType != QCBOR_TYPE_UNDEF)
1289 return -1;
1290
1291 // A break
Laurence Lundblade9e3651c2018-10-10 11:49:55 +08001292 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_BREAK)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001293 return -1;
1294
1295 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1296 return nCBORError;
1297 if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 0)
1298 return -1;
1299
1300 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1301 return nCBORError;
1302 if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 19)
1303 return -1;
1304
Laurence Lundblade077475f2019-04-26 09:06:33 -07001305 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_TYPE_7)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001306 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001307
Laurence Lundblade077475f2019-04-26 09:06:33 -07001308 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_TYPE_7)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001309 return -1;
1310
Laurence Lundblade077475f2019-04-26 09:06:33 -07001311 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_TYPE_7)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001312 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001313
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001314 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1315 return nCBORError;
1316 if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 32)
1317 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001318
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001319 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1320 return nCBORError;
1321 if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 255)
1322 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001323
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001324 return 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001325
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001326}
1327
1328
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001329static int IsNotWellFormedError(QCBORError nErr)
1330{
1331 switch(nErr){
1332 case QCBOR_ERR_INDEFINITE_STRING_CHUNK:
1333 case QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN:
1334 case QCBOR_ERR_UNSUPPORTED:
1335 case QCBOR_ERR_HIT_END:
1336 case QCBOR_ERR_BAD_TYPE_7:
1337 case QCBOR_ERR_BAD_BREAK:
1338 case QCBOR_ERR_EXTRA_BYTES:
1339 case QCBOR_ERR_BAD_INT:
1340 return 1;
1341 default:
1342 return 0;
1343 }
1344}
1345
1346
1347int NotWellFormedTests()
1348{
1349 // Loop over all the not-well-formed instance of CBOR
1350 // that are test vectors in not_well_formed_cbor.h
1351 const uint16_t nArraySize = sizeof(paNotWellFormedCBOR)/sizeof(struct someBinaryBytes);
1352 for(uint16_t nIterate = 0; nIterate < nArraySize; nIterate++) {
1353 const struct someBinaryBytes *pBytes = &paNotWellFormedCBOR[nIterate];
1354 const UsefulBufC Input = (UsefulBufC){pBytes->p, pBytes->n};
1355
1356 // Set up decoder context. String allocator needed for indefinite string test cases
1357 QCBORDecodeContext DCtx;
1358 QCBORDecode_Init(&DCtx, Input, QCBOR_DECODE_MODE_NORMAL);
1359 UsefulBuf_MAKE_STACK_UB(Pool, 100);
1360 QCBORDecode_SetMemPool(&DCtx, Pool, 0);
1361
1362 // Loop getting items until no more to get
1363 QCBORError nCBORError;
1364 do {
1365 QCBORItem Item;
1366
1367 nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
1368 } while(nCBORError == QCBOR_SUCCESS);
1369
1370 // Every test vector must fail with
1371 // a not-well-formed error. If not
1372 // this test fails.
1373 if(!IsNotWellFormedError(nCBORError)) {
1374 // Return index of failure in the error code
1375 return 2000 + nIterate;
1376 }
1377 }
1378 return 0;
1379}
1380
1381
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001382struct FailInput {
Laurence Lundblade3a6042e2019-06-28 19:58:04 -07001383 UsefulBufC Input; // CBOR to decode
1384 QCBORError nError; // The error expected
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001385};
1386
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001387struct FailInput Failures[] = {
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001388 // Most of this is copied from not_well_formed.h. Here the error code
1389 // returned is also checked.
1390
1391 // Indefinite length strings must be closed off
1392 // An indefinite length byte string not closed off
1393 { {(uint8_t[]){0x5f, 0x41, 0x00}, 3}, QCBOR_ERR_HIT_END },
1394 // An indefinite length text string not closed off
1395 { {(uint8_t[]){0x7f, 0x61, 0x00}, 3}, QCBOR_ERR_HIT_END },
1396
1397
1398 // All the chunks in an indefinite length string must be of the type of indefinite length string
1399 // indefinite length byte string with text string chunk
1400 { {(uint8_t[]){0x5f, 0x61, 0x00, 0xff}, 4}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1401 // indefinite length text string with a byte string chunk
1402 { {(uint8_t[]){0x7f, 0x41, 0x00, 0xff}, 4}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1403 // indefinite length byte string with an positive integer chunk
1404 { {(uint8_t[]){0x5f, 0x00, 0xff}, 3}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1405 // indefinite length byte string with an negative integer chunk
1406 { {(uint8_t[]){0x5f, 0x21, 0xff}, 3}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1407 // indefinite length byte string with an array chunk
1408 { {(uint8_t[]){0x5f, 0x80, 0xff}, 3}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1409 // indefinite length byte string with an map chunk
1410 { {(uint8_t[]){0x5f, 0xa0, 0xff}, 3}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1411 // indefinite length byte string with tagged integer chunk
1412 { {(uint8_t[]){0x5f, 0xc0, 0x00, 0xff}, 4}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1413 // indefinite length byte string with an simple type chunk
1414 { {(uint8_t[]){0x5f, 0xe0, 0xff}, 3}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1415 { {(uint8_t[]){0x5f, 0x5f, 0x41, 0x00, 0xff, 0xff}, 6}, QCBOR_ERR_INDEFINITE_STRING_CHUNK},
1416 // indefinite length text string with indefinite string inside
1417 { {(uint8_t[]){0x7f, 0x7f, 0x61, 0x00, 0xff, 0xff}, 6}, QCBOR_ERR_INDEFINITE_STRING_CHUNK},
1418
1419
1420 // Definte length maps and arrays must be closed by having the right number of items
1421 // A definte length array that is supposed to have 1 item, but has none
1422 { {(uint8_t[]){0x81}, 1}, QCBOR_ERR_HIT_END },
1423 // A definte length array that is supposed to have 2 items, but has only 1
1424 { {(uint8_t[]){0x82, 0x00}, 2}, QCBOR_ERR_HIT_END },
1425 // A definte length array that is supposed to have 511 items, but has only 1
1426 { {(uint8_t[]){0x9a, 0x01, 0xff, 0x00}, 4}, QCBOR_ERR_HIT_END },
1427 // A definte length map that is supposed to have 1 item, but has none
1428 { {(uint8_t[]){0xa1}, 1}, QCBOR_ERR_HIT_END },
1429 // A definte length map that is supposed to have s item, but has only 1
1430 { {(uint8_t[]){0xa2, 0x01, 0x02}, 3}, QCBOR_ERR_HIT_END },
1431
1432
1433 // Indefinte length maps and arrays must be ended by a break
1434 // Indefinite length array with zero items and no break
1435 { {(uint8_t[]){0x9f}, 1}, QCBOR_ERR_HIT_END },
1436 // Indefinite length array with two items and no break
1437 { {(uint8_t[]){0x9f, 0x01, 0x02}, 3}, QCBOR_ERR_HIT_END },
1438 // Indefinite length map with zero items and no break
1439 { {(uint8_t[]){0xbf}, 1}, QCBOR_ERR_HIT_END },
1440 // Indefinite length map with two items and no break
1441 { {(uint8_t[]){0xbf, 0x01, 0x02, 0x01, 0x02}, 5}, QCBOR_ERR_HIT_END },
1442
1443
1444 // Nested maps and arrays must be closed off (some extra nested test vectors)
1445 // Unclosed indefinite array containing a close definite array
1446 { {(uint8_t[]){0x9f, 0x80, 0x00}, 3}, QCBOR_ERR_HIT_END },
1447 // Definite length array containing an unclosed indefinite array
1448 { {(uint8_t[]){0x81, 0x9f}, 2}, QCBOR_ERR_HIT_END },
1449 // Deeply nested definite length arrays with deepest one unclosed
1450 { {(uint8_t[]){0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81}, 9}, QCBOR_ERR_HIT_END },
1451 // Deeply nested indefinite length arrays with deepest one unclosed
1452 { {(uint8_t[]){0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0xff, 0xff, 0xff, 0xff}, 9}, QCBOR_ERR_HIT_END },
1453 // Mixed nesting with indefinite unclosed
1454 { {(uint8_t[]){0x9f, 0x81, 0x9f, 0x81, 0x9f, 0x9f, 0xff, 0xff, 0xff}, 9}, QCBOR_ERR_BAD_BREAK }, // TODO: think through this one
1455 // Mixed nesting with definite unclosed
1456 { {(uint8_t[]){0x9f, 0x82, 0x9f, 0x81, 0x9f, 0x9f, 0xff, 0xff, 0xff, 0xff}, 10}, QCBOR_ERR_BAD_BREAK }, // TODO: think through this one
1457
1458
1459 // The "argument" for the data item is incomplete
1460 // Positive integer missing 1 byte argument
1461 { {(uint8_t[]){0x18}, 1}, QCBOR_ERR_HIT_END },
1462 // Positive integer missing 2 byte argument
1463 { {(uint8_t[]){0x19}, 1}, QCBOR_ERR_HIT_END },
1464 // Positive integer missing 4 byte argument
1465 { {(uint8_t[]){0x1a}, 1}, QCBOR_ERR_HIT_END },
1466 // Positive integer missing 8 byte argument
1467 { {(uint8_t[]){0x1b}, 1}, QCBOR_ERR_HIT_END },
1468 // Positive integer missing 1 byte of 2 byte argument
1469 { {(uint8_t[]){0x19, 0x01}, 2}, QCBOR_ERR_HIT_END },
1470 // Positive integer missing 2 bytes of 4 byte argument
1471 { {(uint8_t[]){0x1a, 0x01, 0x02}, 3}, QCBOR_ERR_HIT_END },
1472 // Positive integer missing 1 bytes of 7 byte argument
1473 { {(uint8_t[]){0x1b, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07}, 8}, QCBOR_ERR_HIT_END },
1474 // Negative integer missing 1 byte argument
1475 { {(uint8_t[]){0x38}, 1}, QCBOR_ERR_HIT_END },
1476 // Binary string missing 1 byte argument
1477 { {(uint8_t[]){0x58}, 1}, QCBOR_ERR_HIT_END },
1478 // Text string missing 1 byte argument
1479 { {(uint8_t[]){0x78}, 1}, QCBOR_ERR_HIT_END },
1480 // Array missing 1 byte argument
1481 { {(uint8_t[]){0x98}, 1}, QCBOR_ERR_HIT_END },
1482 // Map missing 1 byte argument
1483 { {(uint8_t[]){0xb8}, 1}, QCBOR_ERR_HIT_END },
1484 // Tag missing 1 byte argument
1485 { {(uint8_t[]){0xd8}, 1}, QCBOR_ERR_HIT_END },
1486 // Simple missing 1 byte argument
1487 { {(uint8_t[]){0xf8}, 1}, QCBOR_ERR_HIT_END },
1488
1489
1490 // Breaks must not occur in definite length arrays and maps
1491 // Array of length 1 with sole member replaced by a break
1492 { {(uint8_t[]){0x81, 0xff}, 2}, QCBOR_ERR_BAD_BREAK },
1493 // Array of length 2 with 2nd member replaced by a break
1494 { {(uint8_t[]){0x82, 0x00, 0xff}, 3}, QCBOR_ERR_BAD_BREAK },
1495 // Map of length 1 with sole member label replaced by a break
1496 { {(uint8_t[]){0xa1, 0xff}, 2}, QCBOR_ERR_BAD_BREAK },
1497 // Map of length 1 with sole member label replaced by break
1498 // Alternate representation that some decoders handle difference
1499 { {(uint8_t[]){0xa1, 0xff, 0x00}, 3}, QCBOR_ERR_BAD_BREAK },
1500 // Array of length 1 with 2nd member value replaced by a break
1501 { {(uint8_t[]){0xa1, 0x00, 0xff}, 3}, QCBOR_ERR_BAD_BREAK },
1502 // Map of length 2 with 2nd member replaced by a break
1503 { {(uint8_t[]){0xa2, 0x00, 0x00, 0xff}, 4}, QCBOR_ERR_BAD_BREAK },
1504
1505
1506 // Breaks must not occur on their own out of an indefinite length data item
1507 // A bare break is not well formed
1508 { {(uint8_t[]){0xff}, 1}, QCBOR_ERR_BAD_BREAK },
1509 // A bare break after a zero length definite length array
1510 { {(uint8_t[]){0x80, 0xff}, 2}, QCBOR_ERR_BAD_BREAK },
1511 // A bare break after a zero length indefinite length map
1512 { {(uint8_t[]){0x9f, 0xff, 0xff}, 3}, QCBOR_ERR_BAD_BREAK },
1513
1514
1515 // Forbidden two byte encodings of simple types
1516 // Must use 0xe0 instead
1517 { {(uint8_t[]){0xf8, 0x00}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1518 // Should use 0xe1 instead
1519 { {(uint8_t[]){0xf8, 0x01}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1520 // Should use 0xe2 instead
1521 { {(uint8_t[]){0xf8, 0x02}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1522 // Should use 0xe3 instead
1523 { {(uint8_t[]){0xf8, 0x03}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1524 // Should use 0xe4 instead
1525 { {(uint8_t[]){0xf8, 0x04}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1526 // Should use 0xe5 instead
1527 { {(uint8_t[]){0xf8, 0x05}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1528 // Should use 0xe6 instead
1529 { {(uint8_t[]){0xf8, 0x06}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1530 // Should use 0xe7 instead
1531 { {(uint8_t[]){0xf8, 0x07}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1532 // Should use 0xe8 instead
1533 { {(uint8_t[]){0xf8, 0x08}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1534 // Should use 0xe9 instead
1535 { {(uint8_t[]){0xf8, 0x09}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1536 // Should use 0xea instead
1537 { {(uint8_t[]){0xf8, 0x0a}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1538 // Should use 0xeb instead
1539 { {(uint8_t[]){0xf8, 0x0b}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1540 // Should use 0xec instead
1541 { {(uint8_t[]){0xf8, 0x0c}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1542 // Should use 0xed instead
1543 { {(uint8_t[]){0xf8, 0x0d}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1544 // Should use 0xee instead
1545 { {(uint8_t[]){0xf8, 0x0e}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1546 // Should use 0xef instead
1547 { {(uint8_t[]){0xf8, 0x0f}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1548 // Should use 0xf0 instead
1549 { {(uint8_t[]){0xf8, 0x10}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1550 // Should use 0xf1 instead
1551 { {(uint8_t[]){0xf8, 0x11}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1552 // Should use 0xf2 instead
1553 { {(uint8_t[]){0xf8, 0x12}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1554 // Must use 0xf3 instead
1555 { {(uint8_t[]){0xf8, 0x13}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1556 // Must use 0xf4 instead
1557 { {(uint8_t[]){0xf8, 0x14}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1558 // Must use 0xf5 instead
1559 { {(uint8_t[]){0xf8, 0x15}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1560 // Must use 0xf6 instead
1561 { {(uint8_t[]){0xf8, 0x16}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1562 // Must use 0xf7 instead
1563 { {(uint8_t[]){0xf8, 0x17}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1564 // Must use 0xf8 instead
1565 { {(uint8_t[]){0xf8, 0x18}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1566
1567
1568 // Integers with additional info indefinite length
1569 // Positive integer with additional info indefinite length
1570 { {(uint8_t[]){0x1f}, 1}, QCBOR_ERR_BAD_INT },
1571 // Negative integer with additional info indefinite length
1572 { {(uint8_t[]){0x3f}, 1}, QCBOR_ERR_BAD_INT },
1573 // CBOR tag with "argument" an indefinite length
1574 { {(uint8_t[]){0xdf, 0x00}, 2}, QCBOR_ERR_BAD_INT },
1575 // CBOR tag with "argument" an indefinite length alternate vector
1576 { {(uint8_t[]){0xdf}, 1}, QCBOR_ERR_BAD_INT },
1577
1578
1579 // Missing bytes from a deterministic length string
1580 // A byte string is of length 1 without the 1 byte
1581 { {(uint8_t[]){0x41}, 1}, QCBOR_ERR_HIT_END },
1582 // A text string is of length 1 without the 1 byte
1583 { {(uint8_t[]){0x61}, 1}, QCBOR_ERR_HIT_END },
1584 // Byte string should have 2^32-1 bytes, but has one
1585 { {(uint8_t[]){0x5a, 0xff, 0xff, 0xff, 0xff, 0x00}, 6}, QCBOR_ERR_HIT_END },
1586 // Byte string should have 2^32-1 bytes, but has one
1587 { {(uint8_t[]){0x7a, 0xff, 0xff, 0xff, 0xff, 0x00}, 6}, QCBOR_ERR_HIT_END },
1588
1589
1590 // Use of unassigned additional information values
1591 // Major type positive integer with reserved value 28
1592 { {(uint8_t[]){0x1c}, 1}, QCBOR_ERR_UNSUPPORTED },
1593 // Major type positive integer with reserved value 29
1594 { {(uint8_t[]){0x1d}, 1}, QCBOR_ERR_UNSUPPORTED },
1595 // Major type positive integer with reserved value 30
1596 { {(uint8_t[]){0x1e}, 1}, QCBOR_ERR_UNSUPPORTED },
1597 // Major type negative integer with reserved value 28
1598 { {(uint8_t[]){0x3c}, 1}, QCBOR_ERR_UNSUPPORTED },
1599 // Major type negative integer with reserved value 29
1600 { {(uint8_t[]){0x3d}, 1}, QCBOR_ERR_UNSUPPORTED },
1601 // Major type negative integer with reserved value 30
1602 { {(uint8_t[]){0x3e}, 1}, QCBOR_ERR_UNSUPPORTED },
1603 // Major type byte string with reserved value 28 length
1604 { {(uint8_t[]){0x5c}, 1}, QCBOR_ERR_UNSUPPORTED },
1605 // Major type byte string with reserved value 29 length
1606 { {(uint8_t[]){0x5d}, 1}, QCBOR_ERR_UNSUPPORTED },
1607 // Major type byte string with reserved value 30 length
1608 { {(uint8_t[]){0x5e}, 1}, QCBOR_ERR_UNSUPPORTED },
1609 // Major type text string with reserved value 28 length
1610 { {(uint8_t[]){0x7c}, 1}, QCBOR_ERR_UNSUPPORTED },
1611 // Major type text string with reserved value 29 length
1612 { {(uint8_t[]){0x7d}, 1}, QCBOR_ERR_UNSUPPORTED },
1613 // Major type text string with reserved value 30 length
1614 { {(uint8_t[]){0x7e}, 1}, QCBOR_ERR_UNSUPPORTED },
1615 // Major type array with reserved value 28 length
1616 { {(uint8_t[]){0x9c}, 1}, QCBOR_ERR_UNSUPPORTED },
1617 // Major type array with reserved value 29 length
1618 { {(uint8_t[]){0x9d}, 1}, QCBOR_ERR_UNSUPPORTED },
1619 // Major type array with reserved value 30 length
1620 { {(uint8_t[]){0x9e}, 1}, QCBOR_ERR_UNSUPPORTED },
1621 // Major type map with reserved value 28 length
1622 { {(uint8_t[]){0xbc}, 1}, QCBOR_ERR_UNSUPPORTED },
1623 // Major type map with reserved value 29 length
1624 { {(uint8_t[]){0xbd}, 1}, QCBOR_ERR_UNSUPPORTED },
1625 // Major type map with reserved value 30 length
1626 { {(uint8_t[]){0xbe}, 1}, QCBOR_ERR_UNSUPPORTED },
1627 // Major type tag with reserved value 28 length
1628 { {(uint8_t[]){0xdc}, 1}, QCBOR_ERR_UNSUPPORTED },
1629 // Major type tag with reserved value 29 length
1630 { {(uint8_t[]){0xdd}, 1}, QCBOR_ERR_UNSUPPORTED },
1631 // Major type tag with reserved value 30 length
1632 { {(uint8_t[]){0xde}, 1}, QCBOR_ERR_UNSUPPORTED },
1633 // Major type simple with reserved value 28 length
1634 { {(uint8_t[]){0xfc}, 1}, QCBOR_ERR_UNSUPPORTED },
1635 // Major type simple with reserved value 29 length
1636 { {(uint8_t[]){0xfd}, 1}, QCBOR_ERR_UNSUPPORTED },
1637 // Major type simple with reserved value 30 length
1638 { {(uint8_t[]){0xfe}, 1}, QCBOR_ERR_UNSUPPORTED },
1639
1640
1641 // Maps must have an even number of data items (key & value)
1642 // Map with 1 item when it should have 2
1643 { {(uint8_t[]){0xa1, 0x00}, 2}, QCBOR_ERR_HIT_END },
1644 // Map with 3 item when it should have 4
1645 { {(uint8_t[]){0xa2, 0x00, 0x00, 0x00}, 2}, QCBOR_ERR_HIT_END },
1646 // Map with 1 item when it should have 2
1647 { {(uint8_t[]){0xbf, 0x00, 0xff}, 3}, QCBOR_ERR_BAD_BREAK },
1648 // Map with 3 item when it should have 4
1649 { {(uint8_t[]){0xbf, 0x00, 0x00, 0x00, 0xff}, 5}, QCBOR_ERR_BAD_BREAK },
1650
1651
1652 // In addition to not-well-formed, some invalid CBOR
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001653 { {(uint8_t[]){0xc0, 0x00}, 2}, QCBOR_ERR_BAD_OPT_TAG }, // Text-based date, with an integer
1654 { {(uint8_t[]){0xc1, 0x41, 0x33}, 3}, QCBOR_ERR_BAD_OPT_TAG }, // Epoch date, with an byte string
1655 { {(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 -07001656 { {(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 +08001657};
1658
Laurence Lundblade3a6042e2019-06-28 19:58:04 -07001659int DecodeFailureTests()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001660{
Laurence Lundblade3a6042e2019-06-28 19:58:04 -07001661 // Loop over the failures
1662 const struct FailInput * const pFEnd = &Failures[0] +
1663 sizeof(Failures)/sizeof(struct FailInput);
Laurence Lundblade3a6042e2019-06-28 19:58:04 -07001664 for(const struct FailInput *pF = &Failures[0]; pF < pFEnd ;pF++) {
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001665
1666 // Set up the decoding context including a mem pool so that
1667 // indefinite length items can be checked
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001668 QCBORDecodeContext DCtx;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001669 QCBORDecode_Init(&DCtx, pF->Input, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001670 UsefulBuf_MAKE_STACK_UB(Pool, 100);
1671 QCBORError nCBORError = QCBORDecode_SetMemPool(&DCtx, Pool, 0);
1672 if(nCBORError) {
1673 return -9;
1674 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001675
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001676 // Iterate until there is an error of some sort
1677 do {
1678 QCBORItem Item;
1679
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001680 nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001681 } while(nCBORError == QCBOR_SUCCESS);
Laurence Lundblade3a6042e2019-06-28 19:58:04 -07001682
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001683 // Must get the expected error or the this test fails
1684 if(nCBORError != pF->nError) {
1685 // return index of CBOR + 1000
1686 return 1000 + (int)(pF - &Failures[0]);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001687 }
1688 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001689
Laurence Lundblade3a6042e2019-06-28 19:58:04 -07001690 // Corrupt the UsefulInputBuf and see that
1691 // it reflected correctly for CBOR decoding
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001692 {
1693 QCBORDecodeContext DCtx;
Laurence Lundblade3a6042e2019-06-28 19:58:04 -07001694 QCBORItem Item;
1695 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001696
Laurence Lundblade3a6042e2019-06-28 19:58:04 -07001697 QCBORDecode_Init(&DCtx,
1698 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSimpleValues),
1699 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001700
1701 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1702 return nCBORError;
1703 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
Laurence Lundblade3a6042e2019-06-28 19:58:04 -07001704 Item.val.uCount != 10) {
1705 // This wasn't supposed to happen
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001706 return -1;
Laurence Lundblade3a6042e2019-06-28 19:58:04 -07001707 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001708
Laurence Lundblade3a6042e2019-06-28 19:58:04 -07001709 DCtx.InBuf.magic = 0; // Reach in and corrupt the UsefulInputBuf
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001710
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001711 nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundblade3a6042e2019-06-28 19:58:04 -07001712 if(nCBORError != QCBOR_ERR_HIT_END) {
1713 // Did not get back the error expected
1714 return -2;
1715 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001716 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001717
Laurence Lundblade3a6042e2019-06-28 19:58:04 -07001718 return 0;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001719}
1720
1721
Laurence Lundbladea2e29072018-12-30 09:20:06 -08001722/* Try all 256 values of the byte at nLen including recursing for
1723 each of the values to try values at nLen+1 ... up to nLenMax
1724 */
1725static void ComprehensiveInputRecurser(uint8_t *pBuf, int nLen, int nLenMax)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001726{
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001727 if(nLen >= nLenMax) {
1728 return;
1729 }
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08001730
Laurence Lundbladea2e29072018-12-30 09:20:06 -08001731 for(int inputByte = 0; inputByte < 256; inputByte++) {
1732 // Set up the input
1733 pBuf[nLen] = inputByte;
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08001734 const UsefulBufC Input = {pBuf, nLen+1};
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08001735
Laurence Lundbladea2e29072018-12-30 09:20:06 -08001736 // Get ready to parse
1737 QCBORDecodeContext DCtx;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001738 QCBORDecode_Init(&DCtx, Input, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001739
Laurence Lundbladea2e29072018-12-30 09:20:06 -08001740 // Parse by getting the next item until an error occurs
1741 // Just about every possible decoder error can occur here
1742 // The goal of this test is not to check for the correct
1743 // error since that is not really possible. It is to
1744 // see that there is no crash on hostile input.
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001745 while(1) {
Laurence Lundbladea2e29072018-12-30 09:20:06 -08001746 QCBORItem Item;
1747 QCBORError nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001748 if(nCBORError != QCBOR_SUCCESS) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001749 break;
1750 }
1751 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001752
Laurence Lundbladea2e29072018-12-30 09:20:06 -08001753 ComprehensiveInputRecurser(pBuf, nLen+1, nLenMax);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001754 }
1755}
1756
1757
1758/*
Laurence Lundbladea2e29072018-12-30 09:20:06 -08001759 Public function for initialization. See header qcbor.h
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001760 */
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001761int ComprehensiveInputTest()
1762{
Laurence Lundbladea2e29072018-12-30 09:20:06 -08001763 // Size 2 tests 64K inputs and runs quickly
1764 uint8_t pBuf[2];
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001765
Laurence Lundbladea2e29072018-12-30 09:20:06 -08001766 ComprehensiveInputRecurser(pBuf, 0, sizeof(pBuf));
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001767
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001768 return 0;
1769}
1770
Laurence Lundbladea2e29072018-12-30 09:20:06 -08001771
1772/*
1773 Public function for initialization. See header qcbor.h
1774 */
1775int BigComprehensiveInputTest()
1776{
1777 // size 3 tests 16 million inputs and runs OK
1778 // in seconds on fast machines. Size 4 takes
1779 // 10+ minutes and 5 half a day on fast
1780 // machines. This test is kept separate from
1781 // the others so as to no slow down the use
1782 // of them as a very frequent regression.
1783 uint8_t pBuf[3]; //
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08001784
Laurence Lundbladea2e29072018-12-30 09:20:06 -08001785 ComprehensiveInputRecurser(pBuf, 0, sizeof(pBuf));
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08001786
Laurence Lundbladea2e29072018-12-30 09:20:06 -08001787 return 0;
1788}
1789
1790
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07001791static uint8_t spDateTestInput[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001792 0xc0, // tag for string date
1793 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001794
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001795 0xc1, // tag for epoch date
1796 0x1a, 0x53, 0x72, 0x4E, 0x00, // Epoch date 1400000000; Tue, 13 May 2014 16:53:20 GMT
1797
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001798 // CBOR_TAG_B64
1799 0xc1, 0xcf, 0xd8, 0x22, // 0xee, // Epoch date with extra tags TODO: fix this test
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001800 0x1a, 0x53, 0x72, 0x4E, 0x01,
1801
1802 0xc1, // tag for epoch date
1803 0x1b, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, // Too large integer
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001804
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001805 0xc1, // tag for epoch date
1806 0xfa, 0x3f, 0x8c, 0xcc, 0xcd, // double with value 1.1
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001807
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001808 0xc1, // tag for epoch date
1809 0xfa, 0x7f, 0x7f, 0xff, 0xff // 3.4028234663852886e+38 too large
1810
1811};
1812
1813
1814// have to check float expected only to within an epsilon
1815int CHECK_EXPECTED_DOUBLE(double val, double expected) {
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001816
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001817 double diff = val - expected;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001818
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001819 diff = fabs(diff);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001820
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001821 return diff > 0.0000001;
1822}
1823
1824
1825int DateParseTest()
1826{
1827 QCBORDecodeContext DCtx;
1828 QCBORItem Item;
1829 int nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001830
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07001831 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spDateTestInput), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001832
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001833 const uint64_t uTags[] = {15};
1834 QCBORTagListIn TagList = {1, uTags};
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001835
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001836 QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TagList);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001837
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001838 // String date
1839 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1840 return -1;
1841 if(Item.uDataType != QCBOR_TYPE_DATE_STRING ||
Laurence Lundblade9e3651c2018-10-10 11:49:55 +08001842 UsefulBuf_Compare(Item.val.dateString, UsefulBuf_FromSZ("1985-04-12"))){
Laurence Lundblade67bd5512018-11-02 21:44:06 +07001843 return -2;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001844 }
1845
1846 // Epoch date
1847 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade67bd5512018-11-02 21:44:06 +07001848 return -3;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001849 if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH ||
1850 Item.val.epochDate.nSeconds != 1400000000 ||
1851 Item.val.epochDate.fSecondsFraction != 0 ) {
Laurence Lundblade67bd5512018-11-02 21:44:06 +07001852 return -4;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001853 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001854
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001855 // Epoch date with extra CBOR_TAG_B64 tag that doesn't really mean anything
1856 // but want to be sure extra tag doesn't cause a problem
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001857 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade67bd5512018-11-02 21:44:06 +07001858 return -5;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001859 if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH ||
1860 Item.val.epochDate.nSeconds != 1400000001 ||
1861 Item.val.epochDate.fSecondsFraction != 0 ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001862 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_B64)) {
Laurence Lundblade67bd5512018-11-02 21:44:06 +07001863 return -6;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001864 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001865
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001866 // Epoch date that is too large for our representation
1867 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_DATE_OVERFLOW) {
Laurence Lundblade67bd5512018-11-02 21:44:06 +07001868 return -7;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001869 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001870
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001871 // Epoch date in float format with fractional seconds
1872 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade67bd5512018-11-02 21:44:06 +07001873 return -8;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001874 if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH ||
1875 Item.val.epochDate.nSeconds != 1 ||
1876 CHECK_EXPECTED_DOUBLE(Item.val.epochDate.fSecondsFraction, 0.1 )) {
Laurence Lundblade67bd5512018-11-02 21:44:06 +07001877 return -9;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001878 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001879
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001880 // Epoch date float that is too large for our representation
1881 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_DATE_OVERFLOW) {
Laurence Lundblade67bd5512018-11-02 21:44:06 +07001882 return -10;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001883 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001884
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001885 // TODO: could use a few more tests with float, double, and half precsion and negative (but coverage is still pretty good)
1886
1887 return 0;
1888}
1889
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001890// Really simple basic input for tagging test
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07001891static uint8_t spOptTestInput[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001892 0xd9, 0xd9, 0xf7, // CBOR magic number
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001893 0x81, // Array of one
1894 0xd8, 0x04, // non-preferred serialization of tag 4
1895 0x82, 0x01, 0x03}; // fraction 1/3
1896
1897static uint8_t spEncodedLargeTag[] = {0xdb, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x80};
1898
1899// 0x9192939495969798, 0x88, 0x01, 0x04
1900static uint8_t spLotsOfTags[] = {0xdb, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0xd8, 0x88, 0xc5, 0xc4, 0x80};
1901
1902/*
1903 The cbor.me parse of this.
1904 55799(55799(55799({6(7(-23)): 5859837686836516696(7({7(-20): 11({17(-18): 17(17(17("Organization"))),
1905 9(-17): 773("SSG"), -15: 4(5(6(7(8(9(10(11(12(13(14(15("Confusion")))))))))))), 17(-16): 17("San Diego"),
1906 17(-14): 17("US")}), 23(-19): 19({-11: 9({-9: -7}),
1907 90599561(90599561(90599561(-10))): 12(h'0102030405060708090A')})})),
1908 16(-22): 23({11(8(7(-5))): 8(-3)})})))
1909 */
1910static uint8_t spCSRWithTags[] = {
1911 0xd9, 0xd9, 0xf7, 0xd9, 0xd9, 0xf7, 0xd9, 0xd9, 0xf7, 0xa2,
1912 0xc6, 0xc7, 0x36,
1913 0xdb, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0xc7, 0xa2,
1914 0xda, 0x00, 0x00, 0x00, 0x07, 0x33,
1915 0xcb, 0xa5,
1916 0xd1, 0x31,
1917 0xd1, 0xd1, 0xd1, 0x6c,
1918 0x4f, 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74, 0x69, 0x6f, 0x6e,
1919 0xc9, 0x30,
1920 0xd9, 0x03, 0x05, 0x63,
1921 0x53, 0x53, 0x47,
1922 0x2e,
1923 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf, 0x69,
1924 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73, 0x69, 0x6f, 0x6e,
1925 0xd1, 0x2f,
1926 0xd1, 0x69,
1927 0x53, 0x61, 0x6e, 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f,
1928 0xd1, 0x2d,
1929 0xd1, 0x62,
1930 0x55, 0x53,
1931 0xd7, 0x32,
1932 0xd3, 0xa2,
1933 0x2a,
1934 0xc9, 0xa1,
1935 0x28,
1936 0x26,
1937 0xda, 0x05, 0x66, 0x70, 0x89, 0xda, 0x05, 0x66, 0x70, 0x89, 0xda, 0x05, 0x66, 0x70, 0x89, 0x29,
1938 0xcc, 0x4a,
1939 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,0x07, 0x08, 0x09, 0x0a,
1940 0xd0, 0x35,
1941 0xd7, 0xa1,
1942 0xcb, 0xc8, 0xc7, 0x24,
1943 0xc8, 0x22};
1944
1945static int CheckCSRMaps(QCBORDecodeContext *pDC);
1946
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001947
1948int OptTagParseTest()
1949{
1950 QCBORDecodeContext DCtx;
1951 QCBORItem Item;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001952
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07001953 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spOptTestInput), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001954
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001955 //-------------------------
1956 // This text matches the magic number tag and the fraction tag
1957 if(QCBORDecode_GetNext(&DCtx, &Item)) {
1958 return -2;
1959 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001960 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001961 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC)) {
1962 return -3;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001963 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001964
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001965 if(QCBORDecode_GetNext(&DCtx, &Item)) {
1966 return -4;
1967 }
1968 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
1969 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_FRACTION) ||
1970 Item.val.uCount != 2) {
1971 return -5;
1972 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001973
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001974 // --------------------------------
1975 // This test decodes the very large tag, but it is not in
1976 // any list so it is ignored.
1977 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEncodedLargeTag), QCBOR_DECODE_MODE_NORMAL);
1978 if(QCBORDecode_GetNext(&DCtx, &Item)) {
1979 return -6;
1980 }
1981 if(Item.uTagBits) {
1982 return -7;
1983 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001984
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001985 // ----------------------------------
1986 // This test sets up a caller-config list that includes the very large tage and then matches it.
1987 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEncodedLargeTag), QCBOR_DECODE_MODE_NORMAL);
1988 const uint64_t puList[] = {0x9192939495969798, 257};
1989 const QCBORTagListIn TL = {2, puList};
1990 QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001991
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001992 if(QCBORDecode_GetNext(&DCtx, &Item)) {
1993 return -8;
1994 }
1995 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
1996 !QCBORDecode_IsTagged(&DCtx, &Item, 0x9192939495969798) ||
1997 QCBORDecode_IsTagged(&DCtx, &Item, 257) ||
1998 QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_BIGFLOAT) ||
1999 Item.val.uCount != 0) {
2000 return -9;
2001 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002002
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002003 //------------------------
2004 // This test sets up a caller-configured list, and looks up something not in it
2005 const uint64_t puLongList[17] = {1,2,1};
2006 const QCBORTagListIn TLLong = {17, puLongList};
2007 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEncodedLargeTag), QCBOR_DECODE_MODE_NORMAL);
2008 QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TLLong);
2009 if(QCBORDecode_GetNext(&DCtx, &Item)) {
2010 return -11;
2011 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002012
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002013 // -----------------------
2014 // This tests retrievel of the full tag list
2015 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spLotsOfTags), QCBOR_DECODE_MODE_NORMAL);
2016 uint64_t puTags[16];
2017 QCBORTagListOut Out = {0, 4, puTags};
2018 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2019 return -12;
2020 }
2021 if(puTags[0] != 0x9192939495969798 ||
2022 puTags[1] != 0x88 ||
2023 puTags[2] != 0x05 ||
2024 puTags[3] != 0x04) {
2025 return -13;
2026 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002027
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002028 // ----------------------
2029 // This text if too small of an out list
2030 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spLotsOfTags), QCBOR_DECODE_MODE_NORMAL);
2031 QCBORTagListOut OutSmall = {0, 3, puTags};
2032 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &OutSmall) != QCBOR_ERR_TOO_MANY_TAGS) {
2033 return -14;
2034 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002035
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002036 // ---------------
2037 // Parse a version of the "CSR" that has had a ton of tags randomly inserted
2038 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRWithTags), QCBOR_DECODE_MODE_NORMAL);
2039 int n = CheckCSRMaps(&DCtx);
2040 if(n) {
2041 return n-2000;
2042 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002043
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002044 Out = (QCBORTagListOut){0,16, puTags};
2045 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRWithTags), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002046
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002047 const uint64_t puTagList[] = {773, 1, 90599561};
2048 const QCBORTagListIn TagList = {3, puTagList};
2049 QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TagList);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002050
2051
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002052 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2053 return -100;
2054 }
2055 if(Item.uDataType != QCBOR_TYPE_MAP ||
2056 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC) ||
2057 QCBORDecode_IsTagged(&DCtx, &Item, 90599561) ||
2058 QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_DATE_EPOCH) ||
2059 Item.val.uCount != 2 ||
2060 puTags[0] != CBOR_TAG_CBOR_MAGIC ||
2061 puTags[1] != CBOR_TAG_CBOR_MAGIC ||
2062 puTags[2] != CBOR_TAG_CBOR_MAGIC ||
2063 Out.uNumUsed != 3) {
2064 return -101;
2065 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002066
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002067 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2068 return -102;
2069 }
2070 if(Item.uDataType != QCBOR_TYPE_MAP ||
2071 QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC) ||
2072 QCBORDecode_IsTagged(&DCtx, &Item, 6) ||
2073 QCBORDecode_IsTagged(&DCtx, &Item, 7) || // item is tagged 7, but 7 is not configured to be recognized
2074 Item.val.uCount != 2 ||
2075 puTags[0] != 5859837686836516696 ||
2076 puTags[1] != 7 ||
2077 Out.uNumUsed != 2) {
2078 return -103;
2079 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002080
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002081 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2082 return -104;
2083 }
2084 if(Item.uDataType != QCBOR_TYPE_MAP ||
2085 Item.uTagBits ||
2086 Item.val.uCount != 5 ||
2087 puTags[0] != 0x0b ||
2088 Out.uNumUsed != 1) {
2089 return -105;
2090 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002091
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002092 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2093 return -106;
2094 }
2095 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
2096 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_COSE_MAC0) ||
2097 Item.val.string.len != 12 ||
2098 puTags[0] != CBOR_TAG_COSE_MAC0 ||
2099 puTags[1] != CBOR_TAG_COSE_MAC0 ||
2100 puTags[2] != CBOR_TAG_COSE_MAC0 ||
2101 Out.uNumUsed != 3) {
2102 return -105;
2103 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002104
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002105 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2106 return -107;
2107 }
2108 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
2109 !QCBORDecode_IsTagged(&DCtx, &Item, 773) ||
2110 Item.val.string.len != 3 ||
2111 puTags[0] != 773 ||
2112 Out.uNumUsed != 1) {
2113 return -108;
2114 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002115
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002116 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2117 return -109;
2118 }
2119 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
2120 !QCBORDecode_IsTagged(&DCtx, &Item, 4) ||
2121 Item.val.string.len != 9 ||
2122 puTags[0] != 4 ||
2123 puTags[11] != 0x0f ||
2124 Out.uNumUsed != 12) {
2125 return -110;
2126 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002127
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002128 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2129 return -111;
2130 }
2131 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
2132 !QCBORDecode_IsTagged(&DCtx, &Item, 17) ||
2133 Item.val.string.len != 9 ||
2134 puTags[0] != 17 ||
2135 Out.uNumUsed != 1) {
2136 return -112;
2137 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002138
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002139 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2140 return -111;
2141 }
2142 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
2143 !QCBORDecode_IsTagged(&DCtx, &Item, 17) ||
2144 Item.val.string.len != 2 ||
2145 puTags[0] != 17 ||
2146 Out.uNumUsed != 1) {
2147 return -112;
2148 }
2149
2150 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2151 return -113;
2152 }
2153 if(Item.uDataType != QCBOR_TYPE_MAP ||
2154 QCBORDecode_IsTagged(&DCtx, &Item, 19) ||
2155 Item.val.uCount != 2 ||
2156 puTags[0] != 19 ||
2157 Out.uNumUsed != 1) {
2158 return -114;
2159 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002160
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002161 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2162 return -115;
2163 }
2164 if(Item.uDataType != QCBOR_TYPE_MAP ||
2165 QCBORDecode_IsTagged(&DCtx, &Item, 9) ||
2166 Item.uTagBits ||
2167 Item.val.uCount != 1 ||
2168 puTags[0] != 9 ||
2169 Out.uNumUsed != 1) {
2170 return -116;
2171 }
2172
2173 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2174 return -116;
2175 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002176 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002177 Item.val.int64 != -7 ||
2178 Item.uTagBits ||
2179 Out.uNumUsed != 0) {
2180 return -117;
2181 }
2182
2183 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2184 return -118;
2185 }
2186 if(Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
2187 Item.val.string.len != 10 ||
2188 Item.uTagBits ||
2189 puTags[0] != 12 ||
2190 Out.uNumUsed != 1) {
2191 return -119;
2192 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002193
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002194 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2195 return -120;
2196 }
2197 if(Item.uDataType != QCBOR_TYPE_MAP ||
2198 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_ENC_AS_B16) ||
2199 Item.val.uCount != 1 ||
2200 puTags[0] != 0x17 ||
2201 Out.uNumUsed != 1) {
2202 return -121;
2203 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002204
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002205 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2206 return -122;
2207 }
2208 if(Item.uDataType != QCBOR_TYPE_INT64 ||
2209 QCBORDecode_IsTagged(&DCtx, &Item, 8) ||
2210 Item.val.int64 != -3 ||
2211 puTags[0] != 8 ||
2212 Out.uNumUsed != 1) {
2213 return -123;
2214 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002215
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002216 if(QCBORDecode_Finish(&DCtx)) {
2217 return -124;
2218 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002219
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002220 return 0;
2221}
2222
2223
2224
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002225
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002226static uint8_t spBigNumInput[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002227 0x83,
2228 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2229 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2230 0xA4,
2231 0x63, 0x42, 0x4E, 0x2B,
2232 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2233 0x18, 0x40,
2234 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2235 0x63, 0x42, 0x4E, 0x2D,
2236 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2237 0x38, 0x3F,
2238 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
2239
2240
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002241static uint8_t spBigNum[] = {0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002242
2243
2244int BignumParseTest()
2245{
2246 QCBORDecodeContext DCtx;
2247 QCBORItem Item;
2248 int nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002249
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002250 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNumInput), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002251
2252
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002253 //
2254 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
2255 return -1;
2256 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
2257 return -1;
2258 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002259
2260 //
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002261 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
2262 return -1;
2263 if(Item.uDataType != QCBOR_TYPE_POSBIGNUM ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002264 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002265 return -1;
2266 }
2267
2268 //
2269 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
2270 return -1;
2271 if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002272 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002273 return -1;
2274 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002275
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002276 //
2277 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
2278 return -1;
2279 if(Item.uDataType != QCBOR_TYPE_MAP) {
2280 return -1;
2281 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002282
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002283 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
2284 return -1;
2285 if(Item.uDataType != QCBOR_TYPE_POSBIGNUM ||
2286 Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002287 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002288 return -1;
2289 }
2290
2291 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
2292 return -1;
2293 if(Item.uDataType != QCBOR_TYPE_POSBIGNUM ||
2294 Item.uLabelType != QCBOR_TYPE_INT64 ||
2295 Item.label.int64 != 64 ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002296 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002297 return -1;
2298 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002299
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002300 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
2301 return -1;
2302 if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM ||
2303 Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002304 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002305 return -1;
2306 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002307
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002308 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
2309 return -1;
2310 if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM ||
2311 Item.uLabelType != QCBOR_TYPE_INT64 ||
2312 Item.label.int64 != -64 ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002313 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002314 return -1;
2315 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002316
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002317 return 0;
2318}
2319
2320
2321
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302322static 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 +08002323{
2324 QCBORItem Item;
2325 int nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002326
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002327 if((nCBORError = QCBORDecode_GetNext(pCtx, &Item))) return -1;
2328 if(Item.uDataType != uDataType) return -1;
2329 if(uNestingLevel > 0) {
2330 if(Item.uLabelType != QCBOR_TYPE_INT64 && Item.uLabelType != QCBOR_TYPE_UINT64) return -1;
2331 if(Item.uLabelType == QCBOR_TYPE_INT64) {
2332 if(Item.label.int64 != nLabel) return -1;
2333 } else {
Laurence Lundblade570fab52018-10-13 18:28:27 +08002334 if(Item.label.uint64 != (uint64_t)nLabel) return -1;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002335 }
2336 }
2337 if(Item.uNestingLevel != uNestingLevel) return -1;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302338 if(Item.uNextNestLevel != uNextNest) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002339
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002340 if(pItem) {
2341 *pItem = Item;
2342 }
2343 return 0;
2344}
2345
2346
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002347// Same code checks definite and indefinite length versions of the map
2348static int CheckCSRMaps(QCBORDecodeContext *pDC)
2349{
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302350 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 0, 1, 0, NULL)) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002351
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302352 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 1, 2, -23, NULL)) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002353
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302354 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 2, 3, -20, NULL)) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002355
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302356 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -18, NULL)) return -1;
2357 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -17, NULL)) return -1;
2358 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -15, NULL)) return -1;
2359 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -16, NULL)) return -1;
2360 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 2, -14, NULL)) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002361
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302362 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 2, 3, -19, NULL)) return -1;
2363 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 3, 4, -11, NULL)) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002364
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302365 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_INT64, 4, 3, -9, NULL)) return -1;
2366 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_BYTE_STRING, 3, 1, -10, NULL)) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002367
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302368 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 1, 2, -22, NULL)) return -1;
2369 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_INT64, 2, 0, -5, NULL)) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002370
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002371 if(QCBORDecode_Finish(pDC)) return -2;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002372
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002373 return 0;
2374}
2375
2376
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002377/*
2378// cbor.me decoded output
2379{
2380 -23: {
2381 -20: {
2382 -18: "Organization",
2383 -17: "SSG",
2384 -15: "Confusion",
2385 -16: "San Diego",
2386 -14: "US"
2387 },
2388 -19: {
2389 -11: {
2390 -9: -7
2391 },
2392 -10: '\u0001\u0002\u0003\u0004\u0005\u0006\a\b\t\n'
2393 }
2394 },
2395 -22: {
2396 -5: -3
2397 }
2398}
2399 */
2400
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002401
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002402static uint8_t spCSRInput[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002403 0xa2, 0x36, 0xa2, 0x33, 0xa5, 0x31, 0x6c, 0x4f,
2404 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74,
2405 0x69, 0x6f, 0x6e, 0x30, 0x63, 0x53, 0x53, 0x47,
2406 0x2e, 0x69, 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73,
2407 0x69, 0x6f, 0x6e, 0x2f, 0x69, 0x53, 0x61, 0x6e,
2408 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f, 0x2d, 0x62,
2409 0x55, 0x53, 0x32, 0xa2, 0x2a, 0xa1, 0x28, 0x26,
2410 0x29, 0x4a, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
2411 0x07, 0x08, 0x09, 0x0a, 0x35, 0xa1, 0x24, 0x22};
2412
2413int NestedMapTest()
2414{
2415 QCBORDecodeContext DCtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002416
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002417 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002418
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002419 return CheckCSRMaps(&DCtx);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002420}
2421
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08002422
2423
2424int StringDecoderModeFailTest()
2425{
2426 QCBORDecodeContext DCtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002427
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08002428 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput), QCBOR_DECODE_MODE_MAP_STRINGS_ONLY);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002429
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08002430 QCBORItem Item;
2431 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002432
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08002433 if(QCBORDecode_GetNext(&DCtx, &Item)) {
2434 return -1;
2435 }
2436 if(Item.uDataType != QCBOR_TYPE_MAP) {
2437 return -2;
2438 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002439
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08002440 nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
2441 if(nCBORError != QCBOR_ERR_MAP_LABEL_TYPE) {
2442 return -3;
2443 }
2444
2445 return 0;
2446}
2447
2448
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002449// Same map as above, but using indefinite lengths
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002450static uint8_t spCSRInputIndefLen[] = {
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002451 0xbf, 0x36, 0xbf, 0x33, 0xbf, 0x31, 0x6c, 0x4f,
2452 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74,
2453 0x69, 0x6f, 0x6e, 0x30, 0x63, 0x53, 0x53, 0x47,
2454 0x2e, 0x69, 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73,
2455 0x69, 0x6f, 0x6e, 0x2f, 0x69, 0x53, 0x61, 0x6e,
2456 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f, 0x2d, 0x62,
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002457 0x55, 0x53, 0xff, 0x32, 0xbf, 0x2a, 0xbf, 0x28,
2458 0x26, 0xff, 0x29, 0x4a, 0x01, 0x02, 0x03, 0x04,
2459 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0xff, 0xff,
2460 0x35, 0xbf, 0x24, 0x22, 0xff, 0xff};
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002461
2462int NestedMapTestIndefLen()
2463{
2464 QCBORDecodeContext DCtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002465
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002466 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInputIndefLen), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002467
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002468 return CheckCSRMaps(&DCtx);
2469}
2470
2471
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002472
Laurence Lundblade17ede402018-10-13 11:43:07 +08002473static UsefulBufC make_nested_indefinite_arrays(int n, UsefulBuf Storage)
2474{
2475 UsefulOutBuf UOB;
2476 UsefulOutBuf_Init(&UOB, Storage);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002477
Laurence Lundblade17ede402018-10-13 11:43:07 +08002478 int i;
2479 for(i = 0; i < n; i++) {
2480 UsefulOutBuf_AppendByte(&UOB, 0x9f);
2481 }
2482
2483 for(i = 0; i < n; i++) {
2484 UsefulOutBuf_AppendByte(&UOB, 0xff);
2485 }
2486 return UsefulOutBuf_OutUBuf(&UOB);
2487}
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002488
2489
Laurence Lundblade17ede402018-10-13 11:43:07 +08002490static int parse_indeflen_nested(UsefulBufC Nested, int nNestLevel)
2491{
2492 QCBORDecodeContext DC;
2493 QCBORDecode_Init(&DC, Nested, 0);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002494
Laurence Lundblade17ede402018-10-13 11:43:07 +08002495 int j;
2496 for(j = 0; j < nNestLevel; j++) {
2497 QCBORItem Item;
2498 int nReturn = QCBORDecode_GetNext(&DC, &Item);
2499 if(j >= QCBOR_MAX_ARRAY_NESTING) {
2500 // Should be in error
2501 if(nReturn != QCBOR_ERR_ARRAY_NESTING_TOO_DEEP) {
2502 return -4;
2503 } else {
2504 return 0; // Decoding doesn't recover after an error
2505 }
2506 } else {
2507 // Should be no error
2508 if(nReturn) {
2509 return -9; // Should not have got an error
2510 }
2511 }
2512 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
2513 return -7;
2514 }
2515 }
2516 int nReturn = QCBORDecode_Finish(&DC);
2517 if(nReturn) {
2518 return -3;
2519 }
2520 return 0;
2521}
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002522
2523
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302524int IndefiniteLengthNestTest()
Laurence Lundblade17ede402018-10-13 11:43:07 +08002525{
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05302526 UsefulBuf_MAKE_STACK_UB(Storage, 50);
Laurence Lundblade17ede402018-10-13 11:43:07 +08002527 int i;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002528 for(i=1; i < QCBOR_MAX_ARRAY_NESTING+4; i++) {
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08002529 const UsefulBufC Nested = make_nested_indefinite_arrays(i, Storage);
Laurence Lundblade17ede402018-10-13 11:43:07 +08002530 int nReturn = parse_indeflen_nested(Nested, i);
2531 if(nReturn) {
2532 return nReturn;
2533 }
2534 }
2535 return 0;
2536}
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002537
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002538
Laurence Lundblade6de37062018-10-15 12:22:42 +05302539
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002540static const uint8_t spIndefiniteArray[] = {0x9f, 0x01, 0x82, 0x02, 0x03, 0xff}; // [1, [2, 3]]
2541static const uint8_t spIndefiniteArrayBad1[] = {0x9f}; // No closing break
2542static const uint8_t spIndefiniteArrayBad2[] = {0x9f, 0x9f, 0x02, 0xff}; // Not enough closing breaks
2543static const uint8_t spIndefiniteArrayBad3[] = {0x9f, 0x02, 0xff, 0xff}; // Too many closing breaks
2544static const uint8_t spIndefiniteArrayBad4[] = {0x81, 0x9f}; // Unclosed indeflen inside def len
2545static const uint8_t spIndefiniteArrayBad5[] = {0x9f, 0xd1, 0xff}; // confused tag
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002546
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302547int IndefiniteLengthArrayMapTest()
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002548{
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002549 int nResult;
2550 // --- first test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002551 UsefulBufC IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArray);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002552
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002553 // Decode it and see if it is OK
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05302554 UsefulBuf_MAKE_STACK_UB(MemPool, 150);
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002555 QCBORDecodeContext DC;
2556 QCBORItem Item;
2557 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002558
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002559 QCBORDecode_SetMemPool(&DC, MemPool, false);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002560
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002561 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05302562
2563 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
2564 Item.uNestingLevel != 0 ||
2565 Item.uNextNestLevel != 1) {
2566 return -111;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002567 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002568
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002569 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05302570 if(Item.uDataType != QCBOR_TYPE_INT64 ||
2571 Item.uNestingLevel != 1 ||
2572 Item.uNextNestLevel != 1) {
2573 return -2;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002574 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002575
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002576 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05302577 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
2578 Item.uNestingLevel != 1 ||
2579 Item.uNextNestLevel != 2) {
2580 return -3;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002581 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002582
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002583 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade12b495d2018-12-17 11:15:54 -08002584 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade6de37062018-10-15 12:22:42 +05302585 Item.uNestingLevel != 2 ||
2586 Item.uNextNestLevel != 2) {
2587 return -4;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002588 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002589
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002590 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade12b495d2018-12-17 11:15:54 -08002591 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade6de37062018-10-15 12:22:42 +05302592 Item.uNestingLevel != 2 ||
2593 Item.uNextNestLevel != 0) {
2594 return -5;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002595 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002596
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002597 if(QCBORDecode_Finish(&DC)) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05302598 return -6;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002599 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002600
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002601 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002602 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad1);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002603
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002604 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002605
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002606 QCBORDecode_SetMemPool(&DC, MemPool, false);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002607
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002608 nResult = QCBORDecode_GetNext(&DC, &Item);
2609 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05302610 return -7;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002611 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002612
Laurence Lundblade570fab52018-10-13 18:28:27 +08002613 nResult = QCBORDecode_Finish(&DC);
Laurence Lundblade6de37062018-10-15 12:22:42 +05302614 if(nResult != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
2615 return -8;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002616 }
2617
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002618
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002619 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002620 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad2);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002621
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002622 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002623
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002624 QCBORDecode_SetMemPool(&DC, MemPool, false);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002625
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002626 nResult = QCBORDecode_GetNext(&DC, &Item);
2627 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05302628 return -9;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002629 }
2630
2631 nResult = QCBORDecode_GetNext(&DC, &Item);
2632 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05302633 return -10;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002634 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002635
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002636 nResult = QCBORDecode_GetNext(&DC, &Item);
2637 if(nResult || Item.uDataType != QCBOR_TYPE_INT64) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05302638 return -11;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002639 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002640
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002641 nResult = QCBORDecode_Finish(&DC);
Laurence Lundblade6de37062018-10-15 12:22:42 +05302642 if(nResult != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
2643 return -12;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002644 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002645
2646
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002647 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002648 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad3);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002649
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002650 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002651
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002652 QCBORDecode_SetMemPool(&DC, MemPool, false);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002653
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002654 nResult = QCBORDecode_GetNext(&DC, &Item);
2655 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05302656 return -13;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002657 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002658
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002659 nResult = QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05302660 if(nResult != QCBOR_ERR_BAD_BREAK) {
2661 return -14;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002662 }
Laurence Lundblade6de37062018-10-15 12:22:42 +05302663
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002664
Laurence Lundblade570fab52018-10-13 18:28:27 +08002665 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002666 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad4);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002667
Laurence Lundblade570fab52018-10-13 18:28:27 +08002668 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002669
Laurence Lundblade570fab52018-10-13 18:28:27 +08002670 QCBORDecode_SetMemPool(&DC, MemPool, false);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002671
Laurence Lundblade570fab52018-10-13 18:28:27 +08002672 nResult = QCBORDecode_GetNext(&DC, &Item);
2673 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05302674 return -15;
Laurence Lundblade570fab52018-10-13 18:28:27 +08002675 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002676
Laurence Lundblade570fab52018-10-13 18:28:27 +08002677 nResult = QCBORDecode_GetNext(&DC, &Item);
2678 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05302679 return -16;
Laurence Lundblade570fab52018-10-13 18:28:27 +08002680 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002681
Laurence Lundblade570fab52018-10-13 18:28:27 +08002682 nResult = QCBORDecode_Finish(&DC);
Laurence Lundblade6de37062018-10-15 12:22:42 +05302683 if(nResult != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
2684 return -17;
Laurence Lundblade570fab52018-10-13 18:28:27 +08002685 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002686
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302687 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002688 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad5);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002689
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302690 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002691
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302692 QCBORDecode_SetMemPool(&DC, MemPool, false);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002693
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302694 nResult = QCBORDecode_GetNext(&DC, &Item);
2695 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05302696 return -18;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302697 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002698
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302699 nResult = QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05302700 if(nResult != QCBOR_ERR_BAD_BREAK) {
2701 return -19;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302702 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002703
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002704 return 0;
2705}
2706
Laurence Lundblade17ede402018-10-13 11:43:07 +08002707
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002708static const uint8_t spIndefiniteLenString[] = {
Laurence Lundblade17ede402018-10-13 11:43:07 +08002709 0x81, // Array of length one
2710 0x7f, // text string marked with indefinite length
2711 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment
2712 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment
2713 0xff // ending break
2714};
2715
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002716static const uint8_t spIndefiniteLenStringBad2[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302717 0x81, // Array of length one
2718 0x7f, // text string marked with indefinite length
2719 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment
2720 0x44, 0x6d, 0x69, 0x6e, 0x67, // second segment of wrong type
2721 0xff // ending break
2722};
2723
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002724static const uint8_t spIndefiniteLenStringBad3[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302725 0x81, // Array of length one
2726 0x7f, // text string marked with indefinite length
2727 0x01, 0x02, // Not a string
2728 0xff // ending break
2729};
2730
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002731static const uint8_t spIndefiniteLenStringBad4[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302732 0x81, // Array of length one
2733 0x7f, // text string marked with indefinite length
2734 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment
2735 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment
2736 // missing end of string
2737};
2738
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002739static const uint8_t spIndefiniteLenStringLabel[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302740 0xa1, // Array of length one
2741 0x7f, // text string marked with indefinite length
2742 0x65, 0x73, 0x74, 0x72, 0x75, 0x75, // first segment
2743 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment
2744 0xff, // ending break
2745 0x01 // integer being labeled.
2746};
2747
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08002748static UsefulBufC MakeIndefiniteBigBstr(UsefulBuf Storage) // TODO: size this
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302749{
2750 UsefulOutBuf UOB;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002751
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302752 UsefulOutBuf_Init(&UOB, Storage);
2753 UsefulOutBuf_AppendByte(&UOB, 0x81);
2754 UsefulOutBuf_AppendByte(&UOB, 0x5f);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002755
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302756 int i = 0;
2757 for(int nChunkSize = 1; nChunkSize <= 128; nChunkSize *= 2) {
2758 UsefulOutBuf_AppendByte(&UOB, 0x58);
2759 UsefulOutBuf_AppendByte(&UOB, (uint8_t)nChunkSize);
2760 for(int j = 0; j < nChunkSize; j++ ) {
2761 UsefulOutBuf_AppendByte(&UOB, i);
2762 i++;
2763 }
2764 }
2765 UsefulOutBuf_AppendByte(&UOB, 0xff);
2766
2767 return UsefulOutBuf_OutUBuf(&UOB);
2768}
2769
2770static int CheckBigString(UsefulBufC BigString)
2771{
2772 if(BigString.len != 255) {
2773 return 1;
2774 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002775
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302776 for(uint8_t i = 0; i < 255; i++){
2777 if(((const uint8_t *)BigString.ptr)[i] != i) {
2778 return 1;
2779 }
2780 }
2781 return 0;
2782}
2783
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05302784
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302785int IndefiniteLengthStringTest()
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302786{
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302787 QCBORDecodeContext DC;
2788 QCBORItem Item;
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05302789 // big enough for MakeIndefiniteBigBstr() + MemPool overhead
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08002790 UsefulBuf_MAKE_STACK_UB(MemPool, 350);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002791
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302792 // --- Simple normal indefinite length string ------
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002793 UsefulBufC IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenString);
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302794 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002795
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05302796 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302797 return -1;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302798 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002799
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302800 if(QCBORDecode_GetNext(&DC, &Item)) {
2801 return -2;
2802 }
2803 if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.uDataAlloc) {
2804 return -3;
2805 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002806
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302807 if(QCBORDecode_GetNext(&DC, &Item)) {
2808 return -4;
2809 }
2810 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || !Item.uDataAlloc) {
2811 return -5;
2812 }
2813 if(QCBORDecode_Finish(&DC)) {
2814 return -6;
2815 }
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302816
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302817 // ----- types mismatch ---
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002818 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad2), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002819
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302820 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
2821 return -7;
2822 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002823
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302824 if(QCBORDecode_GetNext(&DC, &Item)) {
2825 return -8;
2826 }
2827 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
2828 return -9;
2829 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002830
Laurence Lundblade30816f22018-11-10 13:40:22 +07002831 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_INDEFINITE_STRING_CHUNK) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302832 return -10;
2833 }
2834
2835 // ----- not a string ---
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002836 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad3), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002837
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302838 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
2839 return -11;
2840 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002841
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302842 if(QCBORDecode_GetNext(&DC, &Item)) {
2843 return -12;
2844 }
2845 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
2846 return -13;
2847 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002848
Laurence Lundblade30816f22018-11-10 13:40:22 +07002849 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_INDEFINITE_STRING_CHUNK) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302850 return -14;
2851 }
2852
2853 // ----- no end -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002854 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad4), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002855
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302856 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
2857 return -15;
2858 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002859
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302860 if(QCBORDecode_GetNext(&DC, &Item)) {
2861 return -16;
2862 }
2863 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
2864 return -17;
2865 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002866
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302867 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_HIT_END) {
2868 return -18;
2869 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002870
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302871 // ------ Don't set a string allocator and see an error -----
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302872 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002873
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302874 QCBORDecode_GetNext(&DC, &Item);
2875 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302876 return -19;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302877 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002878
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302879 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_NO_STRING_ALLOCATOR) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302880 return -20;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302881 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002882
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302883 // ----- Mempool is way too small -----
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08002884 UsefulBuf_MAKE_STACK_UB(MemPoolTooSmall, QCBOR_DECODE_MIN_MEM_POOL_SIZE-1);
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302885
2886 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
2887 if(!QCBORDecode_SetMemPool(&DC, MemPoolTooSmall, false)) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302888 return -21;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302889 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002890
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302891 // ----- Mempool is way too small -----
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05302892 UsefulBuf_MAKE_STACK_UB(BigIndefBStrStorage, 290);
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08002893 const UsefulBufC BigIndefBStr = MakeIndefiniteBigBstr(BigIndefBStrStorage);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002894
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05302895 UsefulBuf_MAKE_STACK_UB(MemPoolSmall, 80); // 80 is big enough for MemPool overhead, but not BigIndefBStr
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002896
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302897 QCBORDecode_Init(&DC, BigIndefBStr, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302898 if(QCBORDecode_SetMemPool(&DC, MemPoolSmall, false)) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302899 return -22;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302900 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002901
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302902 QCBORDecode_GetNext(&DC, &Item);
2903 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302904 return -23;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302905 }
Laurence Lundblade30816f22018-11-10 13:40:22 +07002906 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_STRING_ALLOCATE) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302907 return -24;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302908 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002909
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302910 // ---- big bstr -----
2911 QCBORDecode_Init(&DC, BigIndefBStr, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002912
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302913 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
2914 return -25;
2915 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002916
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05302917 if(QCBORDecode_GetNext(&DC, &Item)) {
2918 return -26;
2919 }
2920 if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.uDataAlloc) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302921 return -26;
2922 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002923
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302924 if(QCBORDecode_GetNext(&DC, &Item)) {
2925 return -27;
2926 }
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05302927 if(Item.uDataType != QCBOR_TYPE_BYTE_STRING || !Item.uDataAlloc || Item.uNestingLevel != 1) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302928 return -28;
2929 }
2930 if(CheckBigString(Item.val.string)) {
2931 return -3;
2932 }
2933 if(QCBORDecode_Finish(&DC)) {
2934 return -29;
2935 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002936
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302937 // --- label is an indefinite length string ------
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002938 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringLabel), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002939
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302940 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
2941 return -30;
2942 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002943
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302944 QCBORDecode_GetNext(&DC, &Item);
2945 if(Item.uDataType != QCBOR_TYPE_MAP) {
2946 return -31;
2947 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002948
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302949 if(QCBORDecode_GetNext(&DC, &Item)){
2950 return -32;
2951 }
2952 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || Item.uDataType != QCBOR_TYPE_INT64 ||
2953 Item.uDataAlloc || !Item.uLabelAlloc ||
2954 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("struuming"))) {
2955 return -33;
2956 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002957
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302958 if(QCBORDecode_Finish(&DC)) {
2959 return -34;
2960 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002961
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08002962 return 0;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002963}
2964
2965
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302966int AllocAllStringsTest()
2967{
2968 QCBORDecodeContext DC;
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08002969 QCBORError nCBORError;
2970
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002971
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302972 // First test, use the "CSRMap" as easy input and checking
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002973 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002974
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08002975 UsefulBuf_MAKE_STACK_UB(Pool, sizeof(spCSRInput) + QCBOR_DECODE_MIN_MEM_POOL_SIZE);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002976
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08002977 nCBORError = QCBORDecode_SetMemPool(&DC, Pool, 1); // Turn on copying.
2978 if(nCBORError) {
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302979 return -1;
2980 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002981
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08002982 if(CheckCSRMaps(&DC)) {
2983 return -2;
2984 }
2985
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302986 // Next parse, save pointers to a few strings, destroy original and see all is OK.
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08002987 UsefulBuf_MAKE_STACK_UB(CopyOfStorage, sizeof(pValidMapEncoded) + QCBOR_DECODE_MIN_MEM_POOL_SIZE);
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08002988 const UsefulBufC CopyOf = UsefulBuf_Copy(CopyOfStorage, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded));
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002989
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302990 QCBORDecode_Init(&DC, CopyOf, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08002991 UsefulBuf_Set(Pool, '/');
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302992 QCBORDecode_SetMemPool(&DC, Pool, 1); // Turn on copying.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002993
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302994 QCBORItem Item1, Item2, Item3, Item4;
2995 if((nCBORError = QCBORDecode_GetNext(&DC, &Item1)))
2996 return nCBORError;
2997 if(Item1.uDataType != QCBOR_TYPE_MAP ||
2998 Item1.val.uCount != 3)
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08002999 return -3;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303000 if((nCBORError = QCBORDecode_GetNext(&DC, &Item1)))
3001 return nCBORError;
3002 if((nCBORError = QCBORDecode_GetNext(&DC, &Item2)))
3003 return nCBORError;
3004 if((nCBORError = QCBORDecode_GetNext(&DC, &Item3)))
3005 return nCBORError;
3006 if((nCBORError = QCBORDecode_GetNext(&DC, &Item4)))
3007 return nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003008
Laurence Lundblade05ec57b2018-10-21 01:50:03 +05303009 UsefulBuf_Set(CopyOfStorage, '_');
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003010
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303011 if(Item1.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303012 Item1.uDataType != QCBOR_TYPE_INT64 ||
3013 Item1.val.int64 != 42 ||
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003014 Item1.uDataAlloc != 0 ||
3015 Item1.uLabelAlloc == 0 ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09003016 UsefulBuf_Compare(Item1.label.string, UsefulBuf_FromSZ("first integer"))) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003017 return -4;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09003018 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003019
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303020
3021 if(Item2.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09003022 UsefulBuf_Compare(Item2.label.string, UsefulBuf_FromSZ("an array of two strings")) ||
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303023 Item2.uDataType != QCBOR_TYPE_ARRAY ||
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003024 Item2.uDataAlloc != 0 ||
3025 Item2.uLabelAlloc == 0 ||
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303026 Item2.val.uCount != 2)
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003027 return -5;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003028
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303029 if(Item3.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003030 Item3.uDataAlloc == 0 ||
3031 Item3.uLabelAlloc != 0 ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09003032 UsefulBuf_Compare(Item3.val.string, UsefulBuf_FromSZ("string1"))) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003033 return -6;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09003034 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003035
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303036 if(Item4.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003037 Item4.uDataAlloc == 0 ||
3038 Item4.uLabelAlloc != 0 ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09003039 UsefulBuf_Compare(Item4.val.string, UsefulBuf_FromSZ("string2"))) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003040 return -7;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09003041 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003042
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303043 // Next parse with a pool that is too small
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003044 UsefulBuf_MAKE_STACK_UB(SmallPool, QCBOR_DECODE_MIN_MEM_POOL_SIZE + 1);
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05303045 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303046 QCBORDecode_SetMemPool(&DC, SmallPool, 1); // Turn on copying.
3047 if((nCBORError = QCBORDecode_GetNext(&DC, &Item1)))
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003048 return -8;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303049 if(Item1.uDataType != QCBOR_TYPE_MAP ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09003050 Item1.val.uCount != 3) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003051 return -9;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09003052 }
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303053 if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item1))){
3054 if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item2))) {
3055 if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item3))) {
3056 nCBORError = QCBORDecode_GetNext(&DC, &Item4);
3057 }
3058 }
3059 }
Laurence Lundblade30816f22018-11-10 13:40:22 +07003060 if(nCBORError != QCBOR_ERR_STRING_ALLOCATE) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003061 return -10;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303062 }
3063
3064 return 0;
3065}
3066
Laurence Lundbladef6531662018-12-04 10:42:22 +09003067
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303068
3069int MemPoolTest(void)
Laurence Lundblade0155b622018-10-12 20:04:37 +08003070{
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003071 // Set up the decoder with a tiny bit of CBOR to parse because
3072 // nothing can be done with it unless that is set up.
Laurence Lundbladef6531662018-12-04 10:42:22 +09003073 QCBORDecodeContext DC;
3074 const uint8_t pMinimalCBOR[] = {0xa0}; // One empty map
3075 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pMinimalCBOR),0);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003076
Laurence Lundbladef6531662018-12-04 10:42:22 +09003077 // Set up an memory pool of 100 bytes
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003078 // Then fish into the internals of the decode context
3079 // to get the allocator function so it can be called directly.
3080 // Also figure out how much pool is available for use
3081 // buy subtracting out the overhead.
Laurence Lundbladef6531662018-12-04 10:42:22 +09003082 UsefulBuf_MAKE_STACK_UB(Pool, 100);
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003083 QCBORError nError = QCBORDecode_SetMemPool(&DC, Pool, 0);
3084 if(nError) {
3085 return -9;
3086 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003087 QCBORStringAllocate pAlloc = DC.StringAllocator.pfAllocator;
3088 void *pAllocCtx = DC.StringAllocator.pAllocateCxt;
3089 size_t uAvailPool = Pool.len - QCBOR_DECODE_MIN_MEM_POOL_SIZE;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003090
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003091 // First test -- ask for one more byte than available and see failure
3092 UsefulBuf Allocated = (*pAlloc)(pAllocCtx, NULL, uAvailPool+1);
Laurence Lundbladef6531662018-12-04 10:42:22 +09003093 if(!UsefulBuf_IsNULL(Allocated)) {
3094 return -1;
3095 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003096
Laurence Lundbladef6531662018-12-04 10:42:22 +09003097 // Re do the set up for the next test that will do a successful alloc,
3098 // a fail, a free and then success
Laurence Lundbladef6531662018-12-04 10:42:22 +09003099 QCBORDecode_SetMemPool(&DC, Pool, 0);
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003100 pAlloc = DC.StringAllocator.pfAllocator;
3101 pAllocCtx = DC.StringAllocator.pAllocateCxt;
3102 uAvailPool = Pool.len - QCBOR_DECODE_MIN_MEM_POOL_SIZE;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003103
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003104 // Allocate one byte less than available and see success
3105 Allocated = (pAlloc)(pAllocCtx, NULL, uAvailPool-1);
Laurence Lundbladef6531662018-12-04 10:42:22 +09003106 if(UsefulBuf_IsNULL(Allocated)) { // expected to succeed
3107 return -2;
3108 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003109 // Ask for some more and see failure
3110 UsefulBuf Allocated2 = (*pAlloc)(pAllocCtx, NULL, uAvailPool/2);
Laurence Lundbladef6531662018-12-04 10:42:22 +09003111 if(!UsefulBuf_IsNULL(Allocated2)) { // expected to fail
3112 return -3;
3113 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003114 // Free the first allocate, retry the second and see success
3115 (*pAlloc)(pAllocCtx, Allocated.ptr, 0); // Free
3116 Allocated = (*pAlloc)(pAllocCtx, NULL, uAvailPool/2);
Laurence Lundbladef6531662018-12-04 10:42:22 +09003117 if(UsefulBuf_IsNULL(Allocated)) { // succeed because of the free
3118 return -4;
3119 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003120
Laurence Lundbladef6531662018-12-04 10:42:22 +09003121 // Re do set up for next test that involves a successful alloc,
3122 // and a successful realloc and a failed realloc
3123 QCBORDecode_SetMemPool(&DC, Pool, 0);
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003124 pAlloc = DC.StringAllocator.pfAllocator;
3125 pAllocCtx = DC.StringAllocator.pAllocateCxt;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003126
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003127 // Allocate half the pool and see success
3128 Allocated = (*pAlloc)(pAllocCtx, NULL, uAvailPool/2);
Laurence Lundbladef6531662018-12-04 10:42:22 +09003129 if(UsefulBuf_IsNULL(Allocated)) { // expected to succeed
3130 return -5;
3131 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003132 // Reallocate to take up the whole pool and see success
3133 Allocated2 = (*pAlloc)(pAllocCtx, Allocated.ptr, uAvailPool);
Laurence Lundbladef6531662018-12-04 10:42:22 +09003134 if(UsefulBuf_IsNULL(Allocated2)) {
3135 return -6;
3136 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003137 // Make sure its the same pointer and the size is right
Laurence Lundbladef6531662018-12-04 10:42:22 +09003138 if(Allocated2.ptr != Allocated.ptr || Allocated2.len != uAvailPool) {
3139 return -7;
3140 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003141 // Try to allocate more to be sure there is failure after a realloc
3142 UsefulBuf Allocated3 = (*pAlloc)(pAllocCtx, Allocated.ptr, uAvailPool+1);
3143 if(!UsefulBuf_IsNULL(Allocated3)) {
Laurence Lundbladef6531662018-12-04 10:42:22 +09003144 return -8;
3145 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003146
Laurence Lundbladef6531662018-12-04 10:42:22 +09003147 return 0;
3148}
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003149
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003150
3151/* Just enough of an allocator to test configuration of one */
3152static UsefulBuf AllocateTestFunction(void *pCtx, void *pOldMem, size_t uNewSize)
3153{
3154 (void)pOldMem; // unused variable
3155
3156 if(uNewSize) {
3157 // Assumes the context pointer is the buffer and
3158 // nothing too big will ever be asked for.
3159 // This is only good for this basic test!
3160 return (UsefulBuf) {pCtx, uNewSize};
3161 } else {
3162 return NULLUsefulBuf;
3163 }
3164}
3165
3166
3167int SetUpAllocatorTest(void)
3168{
3169 // Set up the decoder with a tiny bit of CBOR to parse because
3170 // nothing can be done with it unless that is set up.
3171 QCBORDecodeContext DC;
3172 const uint8_t pMinimalCBOR[] = {0x62, 0x48, 0x69}; // "Hi"
3173 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pMinimalCBOR),0);
3174
3175 uint8_t pAllocatorBuffer[50];
3176
3177 // This is really just to test that this call works.
3178 // The full functionality of string allocators is tested
3179 // elsewhere with the MemPool internal allocator.
3180 QCBORDecode_SetUpAllocator(&DC, AllocateTestFunction, pAllocatorBuffer, 1);
3181
3182 QCBORItem Item;
3183 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_SUCCESS) {
3184 return -1;
3185 }
3186
3187 if(Item.uDataAlloc == 0 ||
3188 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
3189 Item.val.string.ptr != pAllocatorBuffer) {
3190 return -2;
3191 }
3192
3193 if(QCBORDecode_Finish(&DC) != QCBOR_SUCCESS) {
3194 return -3;
3195 }
3196
3197 return 0;
3198}
3199