blob: 2cadf7fae72c87d0cdf0089c1fd936dc0c89561b [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 Lundblade21d1d812019-09-28 22:47:49 -1000422// The largest negative int possible in CBOR.
423// Not possible in C.
424static const uint8_t spTooBigNegative[] = {
425 0x3b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff
426};
427
428
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800429/*
430 Tests the decoding of lots of different integers sizes
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +0800431 and values.
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800432 */
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800433int IntegerValuesParseTest()
434{
Laurence Lundblade21d1d812019-09-28 22:47:49 -1000435 int nReturn;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800436 QCBORDecodeContext DCtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800437
Laurence Lundblade21d1d812019-09-28 22:47:49 -1000438 QCBORDecode_Init(&DCtx,
439 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedEncodedInts),
440 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800441
Laurence Lundblade21d1d812019-09-28 22:47:49 -1000442 // The really big test of all successes
443 nReturn = IntegerValuesParseTestInternal(&DCtx);
444 if(nReturn) {
445 return nReturn;
446 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800447
Laurence Lundblade21d1d812019-09-28 22:47:49 -1000448 // The one large negative integer that can be parsed
449 QCBORDecode_Init(&DCtx,
450 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTooBigNegative),
451 QCBOR_DECODE_MODE_NORMAL);
452
453 QCBORItem item;
454 if(QCBORDecode_GetNext(&DCtx, &item) != QCBOR_ERR_INT_OVERFLOW) {
455 nReturn = -4000;
456 }
457
458 return(nReturn);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800459}
460
461
462/*
463 Creates a simple CBOR array and returns it in *pEncoded. The array is malloced
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800464 and needs to be freed. This is used by several tests.
465
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800466 Two of the inputs can be set. Two other items in the array are fixed.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800467
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800468 */
469
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -0800470static uint8_t spSimpleArrayBuffer[50];
471
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800472static int CreateSimpleArray(int nInt1, int nInt2, uint8_t **pEncoded, size_t *pEncodedLen)
473{
474 QCBOREncodeContext ECtx;
475 int nReturn = -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800476
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800477 *pEncoded = NULL;
478 *pEncodedLen = INT32_MAX;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800479
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800480 // loop runs CBOR encoding twice. First with no buffer to
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -0800481 // calculate the length so buffer can be allocated correctly,
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800482 // and last with the buffer to do the actual encoding
483 do {
Laurence Lundblade0595e932018-11-02 22:22:47 +0700484 QCBOREncode_Init(&ECtx, (UsefulBuf){*pEncoded, *pEncodedLen});
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800485 QCBOREncode_OpenArray(&ECtx);
486 QCBOREncode_AddInt64(&ECtx, nInt1);
487 QCBOREncode_AddInt64(&ECtx, nInt2);
488 QCBOREncode_AddBytes(&ECtx, ((UsefulBufC) {"galactic", 8}));
489 QCBOREncode_AddBytes(&ECtx, ((UsefulBufC) {"haven token", 11}));
490 QCBOREncode_CloseArray(&ECtx);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800491
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -0800492 if(QCBOREncode_FinishGetSize(&ECtx, pEncodedLen))
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800493 goto Done;
494
495 if(*pEncoded != NULL) {
496 nReturn = 0;
497 goto Done;
498 }
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -0800499
500 // Use static buffer to avoid dependency on malloc()
501 if(*pEncodedLen > sizeof(spSimpleArrayBuffer)) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800502 goto Done;
503 }
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -0800504 *pEncoded = spSimpleArrayBuffer;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800505
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800506 } while(1);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800507
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -0800508Done:
509 return nReturn;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800510}
511
512
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800513/*
514 {"first integer": 42,
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900515 "an array of two strings": [
516 "string1", "string2"
517 ],
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800518 "map in a map": {
519 "bytes 1": h'78787878',
520 "bytes 2": h'79797979',
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900521 "another int": 98,
522 "text 2": "lies, damn lies and statistics"
523 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800524 }
525 */
526
527static uint8_t pValidMapEncoded[] = {
Laurence Lundbladeb836efb2018-10-28 20:09:58 +0700528 0xa3, 0x6d, 0x66, 0x69, 0x72, 0x73, 0x74, 0x20, 0x69, 0x6e,
529 0x74, 0x65, 0x67, 0x65, 0x72, 0x18, 0x2a, 0x77, 0x61, 0x6e,
530 0x20, 0x61, 0x72, 0x72, 0x61, 0x79, 0x20, 0x6f, 0x66, 0x20,
531 0x74, 0x77, 0x6f, 0x20, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67,
532 0x73, 0x82, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x31,
533 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x32, 0x6c, 0x6d,
534 0x61, 0x70, 0x20, 0x69, 0x6e, 0x20, 0x61, 0x20, 0x6d, 0x61,
535 0x70, 0xa4, 0x67, 0x62, 0x79, 0x74, 0x65, 0x73, 0x20, 0x31,
536 0x44, 0x78, 0x78, 0x78, 0x78, 0x67, 0x62, 0x79, 0x74, 0x65,
537 0x73, 0x20, 0x32, 0x44, 0x79, 0x79, 0x79, 0x79, 0x6b, 0x61,
538 0x6e, 0x6f, 0x74, 0x68, 0x65, 0x72, 0x20, 0x69, 0x6e, 0x74,
539 0x18, 0x62, 0x66, 0x74, 0x65, 0x78, 0x74, 0x20, 0x32, 0x78,
540 0x1e, 0x6c, 0x69, 0x65, 0x73, 0x2c, 0x20, 0x64, 0x61, 0x6d,
541 0x6e, 0x20, 0x6c, 0x69, 0x65, 0x73, 0x20, 0x61, 0x6e, 0x64,
542 0x20, 0x73, 0x74, 0x61, 0x74, 0x69, 0x73, 0x74, 0x69, 0x63,
543 0x73 } ;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800544
545static 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)
546{
547 QCBORDecodeContext DCtx;
548 QCBORItem Item;
549 int nReturn = -1; // assume error until success
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800550
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800551 QCBORDecode_Init(&DCtx, (UsefulBufC){pEncoded, nLen}, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800552
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800553 // Make sure the first thing is a map
554 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || Item.uDataType != QCBOR_TYPE_ARRAY)
555 goto Done;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800556
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800557 // First integer
Laurence Lundblade12b495d2018-12-17 11:15:54 -0800558 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || Item.uDataType != QCBOR_TYPE_INT64)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800559 goto Done;
560 *pInt1 = Item.val.int64;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800561
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800562 // Second integer
563 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || Item.uDataType != QCBOR_TYPE_INT64)
564 goto Done;
565 *pInt2 = Item.val.int64;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800566
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800567 // First string
568 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || Item.uDataType != QCBOR_TYPE_BYTE_STRING)
569 goto Done;
570 *pBuf3 = Item.val.string.ptr;
571 *pBuf3Len = Item.val.string.len;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800572
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800573 // Second string
574 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 || Item.uDataType != QCBOR_TYPE_BYTE_STRING)
575 goto Done;
576 *pBuf4 = Item.val.string.ptr;
577 *pBuf4Len = Item.val.string.len;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800578
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800579 nReturn = 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800580
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800581Done:
582 return(nReturn);
583}
584
585
586
Laurence Lundblade9e3651c2018-10-10 11:49:55 +0800587
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800588int SimpleArrayTest()
589{
590 uint8_t *pEncoded;
591 size_t nEncodedLen;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800592
Laurence Lundblade5e390822019-01-06 12:35:01 -0800593 int64_t i1=0, i2=0;
594 size_t i3=0, i4=0;
595 const uint8_t *s3= (uint8_t *)"";
596 const uint8_t *s4= (uint8_t *)"";
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800597
598
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800599 if(CreateSimpleArray(23, 6000, &pEncoded, &nEncodedLen) < 0) {
600 return(-1);
601 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800602
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800603 ParseOrderedArray(pEncoded, nEncodedLen, &i1, &i2, &s3, &i3, &s4, &i4);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800604
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800605 if(i1 != 23 ||
606 i2 != 6000 ||
607 i3 != 8 ||
608 i4 != 11 ||
Laurence Lundbladea44d5062018-10-17 18:45:12 +0530609 memcmp("galactic", s3, 8) !=0 ||
610 memcmp("haven token", s4, 11) !=0) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800611 return(-1);
612 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800613
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800614 return(0);
615}
616
617
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700618/*
619 [
620 0,
621 [],
622 [
623 [],
624 [
625 0
626 ],
627 {},
628 {
629 1: {},
630 2: {},
631 3: []
632 }
633 ]
634 ]
635 */
636static uint8_t sEmpties[] = {0x83, 0x00, 0x80, 0x84, 0x80, 0x81, 0x00, 0xa0,
637 0xa3, 0x01, 0xa0, 0x02, 0xa0, 0x03, 0x80};
638
639int EmptyMapsAndArraysTest()
640{
641 QCBORDecodeContext DCtx;
642 QCBORItem Item;
643
644 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(sEmpties), QCBOR_DECODE_MODE_NORMAL);
645
646 // Array with 3 items
647 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
648 Item.uDataType != QCBOR_TYPE_ARRAY ||
649 Item.uNestingLevel != 0 ||
650 Item.uNextNestLevel != 1 ||
651 Item.val.uCount != 3) {
652 return -1;
653 }
654
655 // An integer 0
656 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
657 Item.uDataType != QCBOR_TYPE_INT64 ||
658 Item.uNestingLevel != 1 ||
659 Item.uNextNestLevel != 1 ||
660 Item.val.uint64 != 0) {
661 return -2;
662 }
663
664 // An empty array
665 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
666 Item.uDataType != QCBOR_TYPE_ARRAY ||
667 Item.uNestingLevel != 1 ||
668 Item.uNextNestLevel != 1 ||
669 Item.val.uCount != 0) {
670 return -3;
671 }
672
673 // An array with 4 items
674 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
675 Item.uDataType != QCBOR_TYPE_ARRAY ||
676 Item.uNestingLevel != 1 ||
677 Item.uNextNestLevel != 2 ||
678 Item.val.uCount != 4) {
679 return -4;
680 }
681
682 // An empty array
683 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
684 Item.uDataType != QCBOR_TYPE_ARRAY ||
685 Item.uNestingLevel != 2 ||
686 Item.uNextNestLevel != 2 ||
687 Item.val.uCount != 0) {
688 return -5;
689 }
690
691 // An array with 1 item
692 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
693 Item.uDataType != QCBOR_TYPE_ARRAY ||
694 Item.uNestingLevel != 2 ||
695 Item.uNextNestLevel != 3 ||
696 Item.val.uCount != 1) {
697 return -6;
698 }
699
700 // An integer 0
701 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
702 Item.uDataType != QCBOR_TYPE_INT64 ||
703 Item.uNestingLevel != 3 ||
704 Item.uNextNestLevel != 2 ||
705 Item.val.uint64 != 0) {
706 return -7;
707 }
708
709 // An empty map
710 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
711 Item.uDataType != QCBOR_TYPE_MAP ||
712 Item.uNestingLevel != 2 ||
713 Item.uNextNestLevel != 2 ||
714 Item.val.uCount != 0) {
715 return -8;
716 }
717
718 // An map with 3 items
719 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
720 Item.uDataType != QCBOR_TYPE_MAP ||
721 Item.uNestingLevel != 2 ||
722 Item.uNextNestLevel != 3 ||
723 Item.val.uCount != 3) {
724 return -9;
725 }
726
727 // An empty map
728 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
729 Item.uDataType != QCBOR_TYPE_MAP ||
730 Item.uNestingLevel != 3 ||
731 Item.uNextNestLevel != 3 ||
732 Item.val.uCount != 0) {
733 return -10;
734 }
735
736 // An empty map
737 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
738 Item.uDataType != QCBOR_TYPE_MAP ||
739 Item.uNestingLevel != 3 ||
740 Item.uNextNestLevel != 3 ||
741 Item.val.uCount != 0) {
742 return -11;
743 }
744
745 // An empty array
746 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
747 Item.uDataType != QCBOR_TYPE_ARRAY ||
748 Item.uNestingLevel != 3 ||
749 Item.uNextNestLevel != 0 ||
750 Item.val.uCount != 0) {
751 return -12;
752 }
753
754 if(QCBORDecode_Finish(&DCtx) != QCBOR_SUCCESS) {
755 return -13;
756 }
757
758 return 0;
759}
760
Laurence Lundblade9e3651c2018-10-10 11:49:55 +0800761
Laurence Lundbladeb836efb2018-10-28 20:09:58 +0700762static uint8_t spDeepArrays[] = {0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x80};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800763
764int ParseDeepArrayTest()
765{
766 QCBORDecodeContext DCtx;
767 int nReturn = 0;
768 int i;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800769
Laurence Lundbladeb836efb2018-10-28 20:09:58 +0700770 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spDeepArrays), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800771
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800772 for(i = 0; i < 10; i++) {
773 QCBORItem Item;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800774
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800775 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
776 Item.uDataType != QCBOR_TYPE_ARRAY ||
777 Item.uNestingLevel != i) {
778 nReturn = -1;
779 break;
780 }
781 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800782
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800783 return(nReturn);
784}
785
Laurence Lundblade972e59c2018-11-11 15:57:23 +0700786// Big enough to test nesting to the depth of 24
787static uint8_t spTooDeepArrays[] = {0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81,
788 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81,
789 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81,
790 0x80};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800791
792int ParseTooDeepArrayTest()
793{
794 QCBORDecodeContext DCtx;
795 int nReturn = 0;
796 int i;
797 QCBORItem Item;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800798
799
Laurence Lundbladeb836efb2018-10-28 20:09:58 +0700800 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTooDeepArrays), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800801
Laurence Lundblade972e59c2018-11-11 15:57:23 +0700802 for(i = 0; i < QCBOR_MAX_ARRAY_NESTING1; i++) {
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800803
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800804 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
805 Item.uDataType != QCBOR_TYPE_ARRAY ||
806 Item.uNestingLevel != i) {
807 nReturn = -1;
808 break;
809 }
810 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800811
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800812 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_ARRAY_NESTING_TOO_DEEP)
813 nReturn = -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800814
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800815 return(nReturn);
816}
817
818
819
820
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800821int ShortBufferParseTest()
822{
Laurence Lundbladebb1062e2019-08-12 23:28:54 -0700823 int nResult = 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800824
Laurence Lundbladebb1062e2019-08-12 23:28:54 -0700825 for(int nNum = sizeof(spExpectedEncodedInts)-1; nNum; nNum--) {
826 QCBORDecodeContext DCtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800827
Laurence Lundbladebb1062e2019-08-12 23:28:54 -0700828 QCBORDecode_Init(&DCtx, (UsefulBufC){spExpectedEncodedInts, nNum}, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800829
Laurence Lundbladebb1062e2019-08-12 23:28:54 -0700830 const QCBORError nErr = IntegerValuesParseTestInternal(&DCtx);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800831
Laurence Lundbladebb1062e2019-08-12 23:28:54 -0700832 if(nErr != QCBOR_ERR_HIT_END && nErr != QCBOR_ERR_NO_MORE_ITEMS) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800833 nResult = -1;
834 goto Done;
835 }
836 }
837Done:
838 return nResult;
839}
840
841
Laurence Lundblade9e3651c2018-10-10 11:49:55 +0800842
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800843int ShortBufferParseTest2()
844{
845 uint8_t *pEncoded;
846 int nReturn;
847 size_t nEncodedLen;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800848
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800849 int64_t i1, i2;
850 size_t i3, i4;
851 const uint8_t *s3, *s4;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800852
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800853 nReturn = 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800854
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800855 if(CreateSimpleArray(23, 6000, &pEncoded, &nEncodedLen) < 0) {
856 return(-1);
857 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800858
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800859 for(nEncodedLen--; nEncodedLen; nEncodedLen--) {
860 int nResult = ParseOrderedArray(pEncoded, (uint32_t)nEncodedLen, &i1, &i2, &s3, &i3, &s4, &i4);
861 if(nResult == 0) {
862 nReturn = -1;
863 }
864 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800865
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800866 return(nReturn);
867}
868
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530869/*
870 Decode and thoroughly check a moderately complex
871 set of maps
872 */
Laurence Lundbladeea567ac2018-12-09 14:03:21 -0800873static int ParseMapTest1(QCBORDecodeMode nMode)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800874{
875 QCBORDecodeContext DCtx;
876 QCBORItem Item;
877 int nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800878
Laurence Lundbladeea567ac2018-12-09 14:03:21 -0800879 QCBORDecode_Init(&DCtx, (UsefulBufC){pValidMapEncoded, sizeof(pValidMapEncoded)}, nMode);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800880
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900881 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800882 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900883 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800884 if(Item.uDataType != QCBOR_TYPE_MAP ||
885 Item.val.uCount != 3)
886 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800887
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900888 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800889 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900890 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800891 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800892 Item.uDataType != QCBOR_TYPE_INT64 ||
893 Item.val.int64 != 42 ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530894 Item.uDataAlloc ||
895 Item.uLabelAlloc ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900896 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("first integer"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800897 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900898 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800899
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900900 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800901 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900902 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800903 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530904 Item.uDataAlloc ||
905 Item.uLabelAlloc ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900906 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("an array of two strings")) ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800907 Item.uDataType != QCBOR_TYPE_ARRAY ||
908 Item.val.uCount != 2)
909 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800910
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900911 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800912 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900913 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800914 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530915 Item.uDataAlloc ||
916 Item.uLabelAlloc ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900917 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("string1"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800918 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900919 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800920
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900921 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800922 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900923 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800924 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530925 Item.uDataAlloc ||
926 Item.uLabelAlloc ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900927 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("string2"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800928 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900929 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800930
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900931 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800932 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900933 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800934 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530935 Item.uDataAlloc ||
936 Item.uLabelAlloc ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900937 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("map in a map")) ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800938 Item.uDataType != QCBOR_TYPE_MAP ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900939 Item.val.uCount != 4) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800940 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900941 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800942
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900943 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800944 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900945 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800946 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900947 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("bytes 1"))||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800948 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530949 Item.uDataAlloc ||
950 Item.uLabelAlloc ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900951 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("xxxx"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800952 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900953 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800954
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900955 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800956 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900957 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800958 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900959 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("bytes 2")) ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800960 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530961 Item.uDataAlloc ||
962 Item.uLabelAlloc ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900963 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("yyyy"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800964 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900965 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800966
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900967 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800968 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900969 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800970 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530971 Item.uDataAlloc ||
972 Item.uLabelAlloc ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900973 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("another int")) ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800974 Item.uDataType != QCBOR_TYPE_INT64 ||
975 Item.val.int64 != 98)
976 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800977
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900978 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800979 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900980 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800981 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900982 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("text 2"))||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800983 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +0530984 Item.uDataAlloc ||
985 Item.uLabelAlloc ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900986 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("lies, damn lies and statistics"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800987 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900988 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800989
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800990 return 0;
991}
992
993
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900994/*
995 Decode and thoroughly check a moderately complex
996 set of maps
997 */
998int ParseMapAsArrayTest()
999{
1000 QCBORDecodeContext DCtx;
1001 QCBORItem Item;
1002 int nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001003
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001004 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), QCBOR_DECODE_MODE_MAP_AS_ARRAY);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001005
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001006 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
1007 return nCBORError;
1008 }
Laurence Lundbladed61cbf32018-12-09 11:42:21 -08001009 if(Item.uDataType != QCBOR_TYPE_MAP_AS_ARRAY ||
1010 Item.val.uCount != 6) {
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001011 return -1;
1012 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001013
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001014 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
1015 return nCBORError;
1016 }
1017 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1018 Item.uDataAlloc ||
1019 Item.uLabelAlloc ||
1020 Item.uLabelType != QCBOR_TYPE_NONE ||
1021 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("first integer"))) {
1022 return -2;
1023 }
1024
1025 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
1026 return nCBORError;
1027 }
1028 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1029 Item.uDataType != QCBOR_TYPE_INT64 ||
1030 Item.val.int64 != 42 ||
1031 Item.uDataAlloc ||
1032 Item.uLabelAlloc) {
1033 return -3;
1034 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001035
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001036 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
1037 return nCBORError;
1038 }
1039 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1040 Item.uDataAlloc ||
1041 Item.uLabelAlloc ||
1042 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("an array of two strings")) ||
1043 Item.uDataType != QCBOR_TYPE_TEXT_STRING) {
1044 return -4;
1045 }
1046
1047 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
1048 return nCBORError;
1049 }
1050 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1051 Item.uDataAlloc ||
1052 Item.uLabelAlloc ||
1053 Item.uDataType != QCBOR_TYPE_ARRAY ||
1054 Item.val.uCount != 2) {
1055 return -5;
1056 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001057
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001058 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
1059 return nCBORError;
1060 }
1061 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1062 Item.val.string.len != 7 ||
1063 Item.uDataAlloc ||
1064 Item.uLabelAlloc ||
1065 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("string1"))) {
1066 return -6;
1067 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001068
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001069 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
1070 return nCBORError;
1071 }
1072 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1073 Item.uDataAlloc ||
1074 Item.uLabelAlloc ||
1075 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("string2"))) {
1076 return -7;
1077 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001078
1079
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001080 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
1081 return nCBORError;
1082 }
1083 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1084 Item.uDataAlloc ||
1085 Item.uLabelAlloc ||
1086 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("map in a map"))) {
1087 return -8;
1088 }
1089
1090 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
1091 return nCBORError;
1092 }
1093 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1094 Item.uDataAlloc ||
1095 Item.uLabelAlloc ||
Laurence Lundbladed61cbf32018-12-09 11:42:21 -08001096 Item.uDataType != QCBOR_TYPE_MAP_AS_ARRAY ||
1097 Item.val.uCount != 8) {
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001098 return -9;
1099 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001100
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001101 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
1102 return nCBORError;
1103 }
1104 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1105 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("bytes 1"))||
1106 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1107 Item.uDataAlloc ||
1108 Item.uLabelAlloc) {
1109 return -10;
1110 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001111
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001112 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
1113 return nCBORError;
1114 }
1115 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1116 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
1117 Item.uDataAlloc ||
1118 Item.uLabelAlloc ||
1119 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("xxxx"))) {
1120 return -11;
1121 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001122
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001123 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
1124 return nCBORError;
1125 }
1126 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1127 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("bytes 2")) ||
1128 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1129 Item.uDataAlloc ||
1130 Item.uLabelAlloc) {
1131 return -12;
1132 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001133
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001134 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
1135 return nCBORError;
1136 }
1137 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1138 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
1139 Item.uDataAlloc ||
1140 Item.uLabelAlloc ||
1141 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("yyyy"))) {
1142 return -13;
1143 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001144
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001145 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
1146 return nCBORError;
1147 }
1148 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1149 Item.uDataAlloc ||
1150 Item.uLabelAlloc ||
1151 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("another int")) ||
1152 Item.uDataType != QCBOR_TYPE_TEXT_STRING) {
1153 return -14;
1154 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001155
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001156 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
1157 return nCBORError;
1158 }
1159 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1160 Item.uDataAlloc ||
1161 Item.uLabelAlloc ||
1162 Item.uDataType != QCBOR_TYPE_INT64 ||
1163 Item.val.int64 != 98) {
1164 return -15;
1165 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001166
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001167 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
1168 return nCBORError;
1169 }
1170 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1171 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("text 2"))||
1172 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1173 Item.uDataAlloc ||
1174 Item.uLabelAlloc) {
1175 return -16;
1176 }
1177
1178 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
1179 return nCBORError;
1180 }
1181 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1182 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1183 Item.uDataAlloc ||
1184 Item.uLabelAlloc ||
1185 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("lies, damn lies and statistics"))) {
1186 return -17;
1187 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001188
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001189 return 0;
1190}
1191
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001192
1193/*
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301194 Fully or partially decode pValidMapEncoded. When
1195 partially decoding check for the right error code.
1196 How much partial decoding depends on nLevel.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001197
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301198 The partial decodes test error conditions of
1199 incomplete encoded input.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001200
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301201 This could be combined with the above test
1202 and made prettier and maybe a little more
1203 thorough.
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001204 */
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001205static int ExtraBytesTest(int nLevel)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001206{
1207 QCBORDecodeContext DCtx;
1208 QCBORItem Item;
1209 int nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001210
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001211 QCBORDecode_Init(&DCtx, (UsefulBufC){pValidMapEncoded, sizeof(pValidMapEncoded)}, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001212
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001213 if(nLevel < 1) {
1214 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_EXTRA_BYTES) {
1215 return -1;
1216 } else {
1217 return 0;
1218 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001219 }
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301220
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001221
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001222 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001223 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001224 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001225 if(Item.uDataType != QCBOR_TYPE_MAP ||
1226 Item.val.uCount != 3)
1227 return -1;
1228
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001229 if(nLevel < 2) {
1230 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
1231 return -1;
1232 } else {
1233 return 0;
1234 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001235 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001236
1237
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001238 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001239 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001240 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001241 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001242 Item.uDataType != QCBOR_TYPE_INT64 ||
1243 Item.val.uCount != 42 ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001244 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("first integer"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001245 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001246 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001247
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001248 if(nLevel < 3) {
1249 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
1250 return -1;
1251 } else {
1252 return 0;
1253 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001254 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001255
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001256 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001257 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001258 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001259 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001260 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("an array of two strings")) ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001261 Item.uDataType != QCBOR_TYPE_ARRAY ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001262 Item.val.uCount != 2) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001263 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001264 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001265
1266
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001267 if(nLevel < 4) {
1268 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
1269 return -1;
1270 } else {
1271 return 0;
1272 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001273 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001274
1275
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001276 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001277 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001278 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001279 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001280 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("string1"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001281 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001282 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001283
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001284 if(nLevel < 5) {
1285 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
1286 return -1;
1287 } else {
1288 return 0;
1289 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001290 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001291
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001292 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001293 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001294 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001295 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001296 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("string2"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001297 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001298 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001299
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001300 if(nLevel < 6) {
1301 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
1302 return -1;
1303 } else {
1304 return 0;
1305 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001306 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001307
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001308 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001309 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001310 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001311 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001312 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("map in a map")) ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001313 Item.uDataType != QCBOR_TYPE_MAP ||
1314 Item.val.uCount != 4)
1315 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001316
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001317 if(nLevel < 7) {
1318 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
1319 return -1;
1320 } else {
1321 return 0;
1322 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001323 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001324
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001325 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001326 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001327 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001328 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001329 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("bytes 1")) ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001330 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001331 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("xxxx"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001332 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001333 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001334
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001335 if(nLevel < 8) {
1336 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
1337 return -1;
1338 } else {
1339 return 0;
1340 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001341 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001342
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001343 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001344 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001345 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001346 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001347 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("bytes 2")) ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001348 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001349 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("yyyy"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001350 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001351 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001352
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001353 if(nLevel < 9) {
1354 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
1355 return -1;
1356 } else {
1357 return 0;
1358 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001359 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001360
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001361 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001362 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001363 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001364 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001365 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("another int")) ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001366 Item.uDataType != QCBOR_TYPE_INT64 ||
1367 Item.val.int64 != 98)
1368 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001369
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001370 if(nLevel < 10) {
1371 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
1372 return -1;
1373 } else {
1374 return 0;
1375 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001376 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001377
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001378 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001379 return nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001380 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001381 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001382 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("text 2"))||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001383 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001384 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("lies, damn lies and statistics"))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001385 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001386 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001387
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301388 if(QCBORDecode_Finish(&DCtx)) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001389 return -1;
1390 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001391
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001392 return 0;
1393}
1394
1395
1396
1397
1398int ParseMapTest()
1399{
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301400 // Parse a moderatly complex map structure very thoroughl
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08001401 int n = ParseMapTest1(QCBOR_DECODE_MODE_NORMAL);
1402
1403 n = ParseMapTest1(QCBOR_DECODE_MODE_MAP_STRINGS_ONLY);
1404
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001405 if(!n) {
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001406 for(int i = 0; i < 10; i++) {
1407 n = ExtraBytesTest(i);
1408 if(n) {
1409 break;
1410 }
1411 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001412 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001413
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001414 return(n);
1415}
1416
1417
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07001418static 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 +08001419
1420int ParseSimpleTest()
1421{
1422 QCBORDecodeContext DCtx;
1423 QCBORItem Item;
1424 int nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001425
1426
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07001427 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSimpleValues), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001428
1429
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001430 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1431 return nCBORError;
1432 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
1433 Item.val.uCount != 10)
1434 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001435
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001436 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1437 return nCBORError;
1438 if(Item.uDataType != QCBOR_TYPE_FALSE)
1439 return -1;
1440
1441 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1442 return nCBORError;
1443 if(Item.uDataType != QCBOR_TYPE_TRUE)
1444 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001445
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001446 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1447 return nCBORError;
1448 if(Item.uDataType != QCBOR_TYPE_NULL)
1449 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001450
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001451 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1452 return nCBORError;
1453 if(Item.uDataType != QCBOR_TYPE_UNDEF)
1454 return -1;
1455
1456 // A break
Laurence Lundblade9e3651c2018-10-10 11:49:55 +08001457 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_BREAK)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001458 return -1;
1459
1460 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1461 return nCBORError;
1462 if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 0)
1463 return -1;
1464
1465 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1466 return nCBORError;
1467 if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 19)
1468 return -1;
1469
Laurence Lundblade077475f2019-04-26 09:06:33 -07001470 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_TYPE_7)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001471 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001472
Laurence Lundblade077475f2019-04-26 09:06:33 -07001473 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_TYPE_7)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001474 return -1;
1475
Laurence Lundblade077475f2019-04-26 09:06:33 -07001476 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_TYPE_7)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001477 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001478
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001479 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1480 return nCBORError;
1481 if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 32)
1482 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001483
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001484 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1485 return nCBORError;
1486 if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 255)
1487 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001488
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001489 return 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001490
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001491}
1492
1493
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001494static int IsNotWellFormedError(QCBORError nErr)
1495{
1496 switch(nErr){
1497 case QCBOR_ERR_INDEFINITE_STRING_CHUNK:
1498 case QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN:
1499 case QCBOR_ERR_UNSUPPORTED:
1500 case QCBOR_ERR_HIT_END:
1501 case QCBOR_ERR_BAD_TYPE_7:
1502 case QCBOR_ERR_BAD_BREAK:
1503 case QCBOR_ERR_EXTRA_BYTES:
1504 case QCBOR_ERR_BAD_INT:
1505 return 1;
1506 default:
1507 return 0;
1508 }
1509}
1510
1511
1512int NotWellFormedTests()
1513{
1514 // Loop over all the not-well-formed instance of CBOR
1515 // that are test vectors in not_well_formed_cbor.h
1516 const uint16_t nArraySize = sizeof(paNotWellFormedCBOR)/sizeof(struct someBinaryBytes);
1517 for(uint16_t nIterate = 0; nIterate < nArraySize; nIterate++) {
1518 const struct someBinaryBytes *pBytes = &paNotWellFormedCBOR[nIterate];
1519 const UsefulBufC Input = (UsefulBufC){pBytes->p, pBytes->n};
1520
1521 // Set up decoder context. String allocator needed for indefinite string test cases
1522 QCBORDecodeContext DCtx;
1523 QCBORDecode_Init(&DCtx, Input, QCBOR_DECODE_MODE_NORMAL);
1524 UsefulBuf_MAKE_STACK_UB(Pool, 100);
1525 QCBORDecode_SetMemPool(&DCtx, Pool, 0);
1526
1527 // Loop getting items until no more to get
1528 QCBORError nCBORError;
1529 do {
1530 QCBORItem Item;
1531
1532 nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
1533 } while(nCBORError == QCBOR_SUCCESS);
1534
1535 // Every test vector must fail with
1536 // a not-well-formed error. If not
1537 // this test fails.
1538 if(!IsNotWellFormedError(nCBORError)) {
1539 // Return index of failure in the error code
1540 return 2000 + nIterate;
1541 }
1542 }
1543 return 0;
1544}
1545
1546
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001547struct FailInput {
Laurence Lundblade3a6042e2019-06-28 19:58:04 -07001548 UsefulBufC Input; // CBOR to decode
1549 QCBORError nError; // The error expected
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001550};
1551
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001552struct FailInput Failures[] = {
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001553 // Most of this is copied from not_well_formed.h. Here the error code
1554 // returned is also checked.
1555
1556 // Indefinite length strings must be closed off
1557 // An indefinite length byte string not closed off
1558 { {(uint8_t[]){0x5f, 0x41, 0x00}, 3}, QCBOR_ERR_HIT_END },
1559 // An indefinite length text string not closed off
1560 { {(uint8_t[]){0x7f, 0x61, 0x00}, 3}, QCBOR_ERR_HIT_END },
1561
1562
1563 // All the chunks in an indefinite length string must be of the type of indefinite length string
1564 // indefinite length byte string with text string chunk
1565 { {(uint8_t[]){0x5f, 0x61, 0x00, 0xff}, 4}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1566 // indefinite length text string with a byte string chunk
1567 { {(uint8_t[]){0x7f, 0x41, 0x00, 0xff}, 4}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1568 // indefinite length byte string with an positive integer chunk
1569 { {(uint8_t[]){0x5f, 0x00, 0xff}, 3}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1570 // indefinite length byte string with an negative integer chunk
1571 { {(uint8_t[]){0x5f, 0x21, 0xff}, 3}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1572 // indefinite length byte string with an array chunk
1573 { {(uint8_t[]){0x5f, 0x80, 0xff}, 3}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1574 // indefinite length byte string with an map chunk
1575 { {(uint8_t[]){0x5f, 0xa0, 0xff}, 3}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1576 // indefinite length byte string with tagged integer chunk
1577 { {(uint8_t[]){0x5f, 0xc0, 0x00, 0xff}, 4}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1578 // indefinite length byte string with an simple type chunk
1579 { {(uint8_t[]){0x5f, 0xe0, 0xff}, 3}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1580 { {(uint8_t[]){0x5f, 0x5f, 0x41, 0x00, 0xff, 0xff}, 6}, QCBOR_ERR_INDEFINITE_STRING_CHUNK},
1581 // indefinite length text string with indefinite string inside
1582 { {(uint8_t[]){0x7f, 0x7f, 0x61, 0x00, 0xff, 0xff}, 6}, QCBOR_ERR_INDEFINITE_STRING_CHUNK},
1583
1584
1585 // Definte length maps and arrays must be closed by having the right number of items
1586 // A definte length array that is supposed to have 1 item, but has none
1587 { {(uint8_t[]){0x81}, 1}, QCBOR_ERR_HIT_END },
1588 // A definte length array that is supposed to have 2 items, but has only 1
1589 { {(uint8_t[]){0x82, 0x00}, 2}, QCBOR_ERR_HIT_END },
1590 // A definte length array that is supposed to have 511 items, but has only 1
1591 { {(uint8_t[]){0x9a, 0x01, 0xff, 0x00}, 4}, QCBOR_ERR_HIT_END },
1592 // A definte length map that is supposed to have 1 item, but has none
1593 { {(uint8_t[]){0xa1}, 1}, QCBOR_ERR_HIT_END },
1594 // A definte length map that is supposed to have s item, but has only 1
1595 { {(uint8_t[]){0xa2, 0x01, 0x02}, 3}, QCBOR_ERR_HIT_END },
1596
1597
1598 // Indefinte length maps and arrays must be ended by a break
1599 // Indefinite length array with zero items and no break
1600 { {(uint8_t[]){0x9f}, 1}, QCBOR_ERR_HIT_END },
1601 // Indefinite length array with two items and no break
1602 { {(uint8_t[]){0x9f, 0x01, 0x02}, 3}, QCBOR_ERR_HIT_END },
1603 // Indefinite length map with zero items and no break
1604 { {(uint8_t[]){0xbf}, 1}, QCBOR_ERR_HIT_END },
1605 // Indefinite length map with two items and no break
1606 { {(uint8_t[]){0xbf, 0x01, 0x02, 0x01, 0x02}, 5}, QCBOR_ERR_HIT_END },
1607
1608
1609 // Nested maps and arrays must be closed off (some extra nested test vectors)
1610 // Unclosed indefinite array containing a close definite array
1611 { {(uint8_t[]){0x9f, 0x80, 0x00}, 3}, QCBOR_ERR_HIT_END },
1612 // Definite length array containing an unclosed indefinite array
1613 { {(uint8_t[]){0x81, 0x9f}, 2}, QCBOR_ERR_HIT_END },
1614 // Deeply nested definite length arrays with deepest one unclosed
1615 { {(uint8_t[]){0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81}, 9}, QCBOR_ERR_HIT_END },
1616 // Deeply nested indefinite length arrays with deepest one unclosed
1617 { {(uint8_t[]){0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0xff, 0xff, 0xff, 0xff}, 9}, QCBOR_ERR_HIT_END },
1618 // Mixed nesting with indefinite unclosed
1619 { {(uint8_t[]){0x9f, 0x81, 0x9f, 0x81, 0x9f, 0x9f, 0xff, 0xff, 0xff}, 9}, QCBOR_ERR_BAD_BREAK }, // TODO: think through this one
1620 // Mixed nesting with definite unclosed
1621 { {(uint8_t[]){0x9f, 0x82, 0x9f, 0x81, 0x9f, 0x9f, 0xff, 0xff, 0xff, 0xff}, 10}, QCBOR_ERR_BAD_BREAK }, // TODO: think through this one
1622
1623
1624 // The "argument" for the data item is incomplete
1625 // Positive integer missing 1 byte argument
1626 { {(uint8_t[]){0x18}, 1}, QCBOR_ERR_HIT_END },
1627 // Positive integer missing 2 byte argument
1628 { {(uint8_t[]){0x19}, 1}, QCBOR_ERR_HIT_END },
1629 // Positive integer missing 4 byte argument
1630 { {(uint8_t[]){0x1a}, 1}, QCBOR_ERR_HIT_END },
1631 // Positive integer missing 8 byte argument
1632 { {(uint8_t[]){0x1b}, 1}, QCBOR_ERR_HIT_END },
1633 // Positive integer missing 1 byte of 2 byte argument
1634 { {(uint8_t[]){0x19, 0x01}, 2}, QCBOR_ERR_HIT_END },
1635 // Positive integer missing 2 bytes of 4 byte argument
1636 { {(uint8_t[]){0x1a, 0x01, 0x02}, 3}, QCBOR_ERR_HIT_END },
1637 // Positive integer missing 1 bytes of 7 byte argument
1638 { {(uint8_t[]){0x1b, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07}, 8}, QCBOR_ERR_HIT_END },
1639 // Negative integer missing 1 byte argument
1640 { {(uint8_t[]){0x38}, 1}, QCBOR_ERR_HIT_END },
1641 // Binary string missing 1 byte argument
1642 { {(uint8_t[]){0x58}, 1}, QCBOR_ERR_HIT_END },
1643 // Text string missing 1 byte argument
1644 { {(uint8_t[]){0x78}, 1}, QCBOR_ERR_HIT_END },
1645 // Array missing 1 byte argument
1646 { {(uint8_t[]){0x98}, 1}, QCBOR_ERR_HIT_END },
1647 // Map missing 1 byte argument
1648 { {(uint8_t[]){0xb8}, 1}, QCBOR_ERR_HIT_END },
1649 // Tag missing 1 byte argument
1650 { {(uint8_t[]){0xd8}, 1}, QCBOR_ERR_HIT_END },
1651 // Simple missing 1 byte argument
1652 { {(uint8_t[]){0xf8}, 1}, QCBOR_ERR_HIT_END },
1653
1654
1655 // Breaks must not occur in definite length arrays and maps
1656 // Array of length 1 with sole member replaced by a break
1657 { {(uint8_t[]){0x81, 0xff}, 2}, QCBOR_ERR_BAD_BREAK },
1658 // Array of length 2 with 2nd member replaced by a break
1659 { {(uint8_t[]){0x82, 0x00, 0xff}, 3}, QCBOR_ERR_BAD_BREAK },
1660 // Map of length 1 with sole member label replaced by a break
1661 { {(uint8_t[]){0xa1, 0xff}, 2}, QCBOR_ERR_BAD_BREAK },
1662 // Map of length 1 with sole member label replaced by break
1663 // Alternate representation that some decoders handle difference
1664 { {(uint8_t[]){0xa1, 0xff, 0x00}, 3}, QCBOR_ERR_BAD_BREAK },
1665 // Array of length 1 with 2nd member value replaced by a break
1666 { {(uint8_t[]){0xa1, 0x00, 0xff}, 3}, QCBOR_ERR_BAD_BREAK },
1667 // Map of length 2 with 2nd member replaced by a break
1668 { {(uint8_t[]){0xa2, 0x00, 0x00, 0xff}, 4}, QCBOR_ERR_BAD_BREAK },
1669
1670
1671 // Breaks must not occur on their own out of an indefinite length data item
1672 // A bare break is not well formed
1673 { {(uint8_t[]){0xff}, 1}, QCBOR_ERR_BAD_BREAK },
1674 // A bare break after a zero length definite length array
1675 { {(uint8_t[]){0x80, 0xff}, 2}, QCBOR_ERR_BAD_BREAK },
1676 // A bare break after a zero length indefinite length map
1677 { {(uint8_t[]){0x9f, 0xff, 0xff}, 3}, QCBOR_ERR_BAD_BREAK },
1678
1679
1680 // Forbidden two byte encodings of simple types
1681 // Must use 0xe0 instead
1682 { {(uint8_t[]){0xf8, 0x00}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1683 // Should use 0xe1 instead
1684 { {(uint8_t[]){0xf8, 0x01}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1685 // Should use 0xe2 instead
1686 { {(uint8_t[]){0xf8, 0x02}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1687 // Should use 0xe3 instead
1688 { {(uint8_t[]){0xf8, 0x03}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1689 // Should use 0xe4 instead
1690 { {(uint8_t[]){0xf8, 0x04}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1691 // Should use 0xe5 instead
1692 { {(uint8_t[]){0xf8, 0x05}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1693 // Should use 0xe6 instead
1694 { {(uint8_t[]){0xf8, 0x06}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1695 // Should use 0xe7 instead
1696 { {(uint8_t[]){0xf8, 0x07}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1697 // Should use 0xe8 instead
1698 { {(uint8_t[]){0xf8, 0x08}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1699 // Should use 0xe9 instead
1700 { {(uint8_t[]){0xf8, 0x09}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1701 // Should use 0xea instead
1702 { {(uint8_t[]){0xf8, 0x0a}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1703 // Should use 0xeb instead
1704 { {(uint8_t[]){0xf8, 0x0b}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1705 // Should use 0xec instead
1706 { {(uint8_t[]){0xf8, 0x0c}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1707 // Should use 0xed instead
1708 { {(uint8_t[]){0xf8, 0x0d}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1709 // Should use 0xee instead
1710 { {(uint8_t[]){0xf8, 0x0e}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1711 // Should use 0xef instead
1712 { {(uint8_t[]){0xf8, 0x0f}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1713 // Should use 0xf0 instead
1714 { {(uint8_t[]){0xf8, 0x10}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1715 // Should use 0xf1 instead
1716 { {(uint8_t[]){0xf8, 0x11}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1717 // Should use 0xf2 instead
1718 { {(uint8_t[]){0xf8, 0x12}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1719 // Must use 0xf3 instead
1720 { {(uint8_t[]){0xf8, 0x13}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1721 // Must use 0xf4 instead
1722 { {(uint8_t[]){0xf8, 0x14}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1723 // Must use 0xf5 instead
1724 { {(uint8_t[]){0xf8, 0x15}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1725 // Must use 0xf6 instead
1726 { {(uint8_t[]){0xf8, 0x16}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1727 // Must use 0xf7 instead
1728 { {(uint8_t[]){0xf8, 0x17}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1729 // Must use 0xf8 instead
1730 { {(uint8_t[]){0xf8, 0x18}, 2}, QCBOR_ERR_BAD_TYPE_7 },
1731
1732
1733 // Integers with additional info indefinite length
1734 // Positive integer with additional info indefinite length
1735 { {(uint8_t[]){0x1f}, 1}, QCBOR_ERR_BAD_INT },
1736 // Negative integer with additional info indefinite length
1737 { {(uint8_t[]){0x3f}, 1}, QCBOR_ERR_BAD_INT },
1738 // CBOR tag with "argument" an indefinite length
1739 { {(uint8_t[]){0xdf, 0x00}, 2}, QCBOR_ERR_BAD_INT },
1740 // CBOR tag with "argument" an indefinite length alternate vector
1741 { {(uint8_t[]){0xdf}, 1}, QCBOR_ERR_BAD_INT },
1742
1743
1744 // Missing bytes from a deterministic length string
1745 // A byte string is of length 1 without the 1 byte
1746 { {(uint8_t[]){0x41}, 1}, QCBOR_ERR_HIT_END },
1747 // A text string is of length 1 without the 1 byte
1748 { {(uint8_t[]){0x61}, 1}, QCBOR_ERR_HIT_END },
1749 // Byte string should have 2^32-1 bytes, but has one
1750 { {(uint8_t[]){0x5a, 0xff, 0xff, 0xff, 0xff, 0x00}, 6}, QCBOR_ERR_HIT_END },
1751 // Byte string should have 2^32-1 bytes, but has one
1752 { {(uint8_t[]){0x7a, 0xff, 0xff, 0xff, 0xff, 0x00}, 6}, QCBOR_ERR_HIT_END },
1753
1754
1755 // Use of unassigned additional information values
1756 // Major type positive integer with reserved value 28
1757 { {(uint8_t[]){0x1c}, 1}, QCBOR_ERR_UNSUPPORTED },
1758 // Major type positive integer with reserved value 29
1759 { {(uint8_t[]){0x1d}, 1}, QCBOR_ERR_UNSUPPORTED },
1760 // Major type positive integer with reserved value 30
1761 { {(uint8_t[]){0x1e}, 1}, QCBOR_ERR_UNSUPPORTED },
1762 // Major type negative integer with reserved value 28
1763 { {(uint8_t[]){0x3c}, 1}, QCBOR_ERR_UNSUPPORTED },
1764 // Major type negative integer with reserved value 29
1765 { {(uint8_t[]){0x3d}, 1}, QCBOR_ERR_UNSUPPORTED },
1766 // Major type negative integer with reserved value 30
1767 { {(uint8_t[]){0x3e}, 1}, QCBOR_ERR_UNSUPPORTED },
1768 // Major type byte string with reserved value 28 length
1769 { {(uint8_t[]){0x5c}, 1}, QCBOR_ERR_UNSUPPORTED },
1770 // Major type byte string with reserved value 29 length
1771 { {(uint8_t[]){0x5d}, 1}, QCBOR_ERR_UNSUPPORTED },
1772 // Major type byte string with reserved value 30 length
1773 { {(uint8_t[]){0x5e}, 1}, QCBOR_ERR_UNSUPPORTED },
1774 // Major type text string with reserved value 28 length
1775 { {(uint8_t[]){0x7c}, 1}, QCBOR_ERR_UNSUPPORTED },
1776 // Major type text string with reserved value 29 length
1777 { {(uint8_t[]){0x7d}, 1}, QCBOR_ERR_UNSUPPORTED },
1778 // Major type text string with reserved value 30 length
1779 { {(uint8_t[]){0x7e}, 1}, QCBOR_ERR_UNSUPPORTED },
1780 // Major type array with reserved value 28 length
1781 { {(uint8_t[]){0x9c}, 1}, QCBOR_ERR_UNSUPPORTED },
1782 // Major type array with reserved value 29 length
1783 { {(uint8_t[]){0x9d}, 1}, QCBOR_ERR_UNSUPPORTED },
1784 // Major type array with reserved value 30 length
1785 { {(uint8_t[]){0x9e}, 1}, QCBOR_ERR_UNSUPPORTED },
1786 // Major type map with reserved value 28 length
1787 { {(uint8_t[]){0xbc}, 1}, QCBOR_ERR_UNSUPPORTED },
1788 // Major type map with reserved value 29 length
1789 { {(uint8_t[]){0xbd}, 1}, QCBOR_ERR_UNSUPPORTED },
1790 // Major type map with reserved value 30 length
1791 { {(uint8_t[]){0xbe}, 1}, QCBOR_ERR_UNSUPPORTED },
1792 // Major type tag with reserved value 28 length
1793 { {(uint8_t[]){0xdc}, 1}, QCBOR_ERR_UNSUPPORTED },
1794 // Major type tag with reserved value 29 length
1795 { {(uint8_t[]){0xdd}, 1}, QCBOR_ERR_UNSUPPORTED },
1796 // Major type tag with reserved value 30 length
1797 { {(uint8_t[]){0xde}, 1}, QCBOR_ERR_UNSUPPORTED },
1798 // Major type simple with reserved value 28 length
1799 { {(uint8_t[]){0xfc}, 1}, QCBOR_ERR_UNSUPPORTED },
1800 // Major type simple with reserved value 29 length
1801 { {(uint8_t[]){0xfd}, 1}, QCBOR_ERR_UNSUPPORTED },
1802 // Major type simple with reserved value 30 length
1803 { {(uint8_t[]){0xfe}, 1}, QCBOR_ERR_UNSUPPORTED },
1804
1805
1806 // Maps must have an even number of data items (key & value)
1807 // Map with 1 item when it should have 2
1808 { {(uint8_t[]){0xa1, 0x00}, 2}, QCBOR_ERR_HIT_END },
1809 // Map with 3 item when it should have 4
1810 { {(uint8_t[]){0xa2, 0x00, 0x00, 0x00}, 2}, QCBOR_ERR_HIT_END },
1811 // Map with 1 item when it should have 2
1812 { {(uint8_t[]){0xbf, 0x00, 0xff}, 3}, QCBOR_ERR_BAD_BREAK },
1813 // Map with 3 item when it should have 4
1814 { {(uint8_t[]){0xbf, 0x00, 0x00, 0x00, 0xff}, 5}, QCBOR_ERR_BAD_BREAK },
1815
1816
1817 // In addition to not-well-formed, some invalid CBOR
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001818 { {(uint8_t[]){0xc0, 0x00}, 2}, QCBOR_ERR_BAD_OPT_TAG }, // Text-based date, with an integer
1819 { {(uint8_t[]){0xc1, 0x41, 0x33}, 3}, QCBOR_ERR_BAD_OPT_TAG }, // Epoch date, with an byte string
1820 { {(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 -07001821 { {(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 +08001822};
1823
Laurence Lundblade3a6042e2019-06-28 19:58:04 -07001824int DecodeFailureTests()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001825{
Laurence Lundblade3a6042e2019-06-28 19:58:04 -07001826 // Loop over the failures
1827 const struct FailInput * const pFEnd = &Failures[0] +
1828 sizeof(Failures)/sizeof(struct FailInput);
Laurence Lundblade3a6042e2019-06-28 19:58:04 -07001829 for(const struct FailInput *pF = &Failures[0]; pF < pFEnd ;pF++) {
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001830
1831 // Set up the decoding context including a mem pool so that
1832 // indefinite length items can be checked
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001833 QCBORDecodeContext DCtx;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001834 QCBORDecode_Init(&DCtx, pF->Input, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001835 UsefulBuf_MAKE_STACK_UB(Pool, 100);
1836 QCBORError nCBORError = QCBORDecode_SetMemPool(&DCtx, Pool, 0);
1837 if(nCBORError) {
1838 return -9;
1839 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001840
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001841 // Iterate until there is an error of some sort
1842 do {
1843 QCBORItem Item;
1844
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001845 nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001846 } while(nCBORError == QCBOR_SUCCESS);
Laurence Lundblade3a6042e2019-06-28 19:58:04 -07001847
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001848 // Must get the expected error or the this test fails
1849 if(nCBORError != pF->nError) {
1850 // return index of CBOR + 1000
1851 return 1000 + (int)(pF - &Failures[0]);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001852 }
1853 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001854
Laurence Lundblade3a6042e2019-06-28 19:58:04 -07001855 // Corrupt the UsefulInputBuf and see that
1856 // it reflected correctly for CBOR decoding
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001857 {
1858 QCBORDecodeContext DCtx;
Laurence Lundblade3a6042e2019-06-28 19:58:04 -07001859 QCBORItem Item;
1860 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001861
Laurence Lundblade3a6042e2019-06-28 19:58:04 -07001862 QCBORDecode_Init(&DCtx,
1863 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSimpleValues),
1864 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001865
1866 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
1867 return nCBORError;
1868 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
Laurence Lundblade3a6042e2019-06-28 19:58:04 -07001869 Item.val.uCount != 10) {
1870 // This wasn't supposed to happen
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001871 return -1;
Laurence Lundblade3a6042e2019-06-28 19:58:04 -07001872 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001873
Laurence Lundblade3a6042e2019-06-28 19:58:04 -07001874 DCtx.InBuf.magic = 0; // Reach in and corrupt the UsefulInputBuf
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001875
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001876 nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundblade3a6042e2019-06-28 19:58:04 -07001877 if(nCBORError != QCBOR_ERR_HIT_END) {
1878 // Did not get back the error expected
1879 return -2;
1880 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001881 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001882
Laurence Lundblade3a6042e2019-06-28 19:58:04 -07001883 return 0;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001884}
1885
1886
Laurence Lundbladea2e29072018-12-30 09:20:06 -08001887/* Try all 256 values of the byte at nLen including recursing for
1888 each of the values to try values at nLen+1 ... up to nLenMax
1889 */
1890static void ComprehensiveInputRecurser(uint8_t *pBuf, int nLen, int nLenMax)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001891{
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001892 if(nLen >= nLenMax) {
1893 return;
1894 }
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08001895
Laurence Lundbladea2e29072018-12-30 09:20:06 -08001896 for(int inputByte = 0; inputByte < 256; inputByte++) {
1897 // Set up the input
1898 pBuf[nLen] = inputByte;
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08001899 const UsefulBufC Input = {pBuf, nLen+1};
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08001900
Laurence Lundbladea2e29072018-12-30 09:20:06 -08001901 // Get ready to parse
1902 QCBORDecodeContext DCtx;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001903 QCBORDecode_Init(&DCtx, Input, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001904
Laurence Lundbladea2e29072018-12-30 09:20:06 -08001905 // Parse by getting the next item until an error occurs
1906 // Just about every possible decoder error can occur here
1907 // The goal of this test is not to check for the correct
1908 // error since that is not really possible. It is to
1909 // see that there is no crash on hostile input.
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001910 while(1) {
Laurence Lundbladea2e29072018-12-30 09:20:06 -08001911 QCBORItem Item;
1912 QCBORError nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001913 if(nCBORError != QCBOR_SUCCESS) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001914 break;
1915 }
1916 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001917
Laurence Lundbladea2e29072018-12-30 09:20:06 -08001918 ComprehensiveInputRecurser(pBuf, nLen+1, nLenMax);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001919 }
1920}
1921
1922
1923/*
Laurence Lundbladea2e29072018-12-30 09:20:06 -08001924 Public function for initialization. See header qcbor.h
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001925 */
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001926int ComprehensiveInputTest()
1927{
Laurence Lundbladea2e29072018-12-30 09:20:06 -08001928 // Size 2 tests 64K inputs and runs quickly
1929 uint8_t pBuf[2];
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001930
Laurence Lundbladea2e29072018-12-30 09:20:06 -08001931 ComprehensiveInputRecurser(pBuf, 0, sizeof(pBuf));
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001932
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001933 return 0;
1934}
1935
Laurence Lundbladea2e29072018-12-30 09:20:06 -08001936
1937/*
1938 Public function for initialization. See header qcbor.h
1939 */
1940int BigComprehensiveInputTest()
1941{
1942 // size 3 tests 16 million inputs and runs OK
1943 // in seconds on fast machines. Size 4 takes
1944 // 10+ minutes and 5 half a day on fast
1945 // machines. This test is kept separate from
1946 // the others so as to no slow down the use
1947 // of them as a very frequent regression.
1948 uint8_t pBuf[3]; //
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08001949
Laurence Lundbladea2e29072018-12-30 09:20:06 -08001950 ComprehensiveInputRecurser(pBuf, 0, sizeof(pBuf));
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08001951
Laurence Lundbladea2e29072018-12-30 09:20:06 -08001952 return 0;
1953}
1954
1955
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07001956static uint8_t spDateTestInput[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001957 0xc0, // tag for string date
1958 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001959
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001960 0xc1, // tag for epoch date
1961 0x1a, 0x53, 0x72, 0x4E, 0x00, // Epoch date 1400000000; Tue, 13 May 2014 16:53:20 GMT
1962
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001963 // CBOR_TAG_B64
1964 0xc1, 0xcf, 0xd8, 0x22, // 0xee, // Epoch date with extra tags TODO: fix this test
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001965 0x1a, 0x53, 0x72, 0x4E, 0x01,
1966
1967 0xc1, // tag for epoch date
1968 0x1b, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, // Too large integer
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001969
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001970 0xc1, // tag for epoch date
1971 0xfa, 0x3f, 0x8c, 0xcc, 0xcd, // double with value 1.1
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001972
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001973 0xc1, // tag for epoch date
1974 0xfa, 0x7f, 0x7f, 0xff, 0xff // 3.4028234663852886e+38 too large
1975
1976};
1977
1978
1979// have to check float expected only to within an epsilon
1980int CHECK_EXPECTED_DOUBLE(double val, double expected) {
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001981
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001982 double diff = val - expected;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001983
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001984 diff = fabs(diff);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001985
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001986 return diff > 0.0000001;
1987}
1988
1989
1990int DateParseTest()
1991{
1992 QCBORDecodeContext DCtx;
1993 QCBORItem Item;
1994 int nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001995
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07001996 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spDateTestInput), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001997
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07001998 const uint64_t uTags[] = {15};
1999 QCBORTagListIn TagList = {1, uTags};
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002000
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002001 QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TagList);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002002
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002003 // String date
2004 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
2005 return -1;
2006 if(Item.uDataType != QCBOR_TYPE_DATE_STRING ||
Laurence Lundblade9e3651c2018-10-10 11:49:55 +08002007 UsefulBuf_Compare(Item.val.dateString, UsefulBuf_FromSZ("1985-04-12"))){
Laurence Lundblade67bd5512018-11-02 21:44:06 +07002008 return -2;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002009 }
2010
2011 // Epoch date
2012 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade67bd5512018-11-02 21:44:06 +07002013 return -3;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002014 if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH ||
2015 Item.val.epochDate.nSeconds != 1400000000 ||
2016 Item.val.epochDate.fSecondsFraction != 0 ) {
Laurence Lundblade67bd5512018-11-02 21:44:06 +07002017 return -4;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002018 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002019
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002020 // Epoch date with extra CBOR_TAG_B64 tag that doesn't really mean anything
2021 // but want to be sure extra tag doesn't cause a problem
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002022 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade67bd5512018-11-02 21:44:06 +07002023 return -5;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002024 if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH ||
2025 Item.val.epochDate.nSeconds != 1400000001 ||
2026 Item.val.epochDate.fSecondsFraction != 0 ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002027 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_B64)) {
Laurence Lundblade67bd5512018-11-02 21:44:06 +07002028 return -6;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002029 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002030
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002031 // Epoch date that is too large for our representation
2032 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_DATE_OVERFLOW) {
Laurence Lundblade67bd5512018-11-02 21:44:06 +07002033 return -7;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002034 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002035
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002036 // Epoch date in float format with fractional seconds
2037 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade67bd5512018-11-02 21:44:06 +07002038 return -8;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002039 if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH ||
2040 Item.val.epochDate.nSeconds != 1 ||
2041 CHECK_EXPECTED_DOUBLE(Item.val.epochDate.fSecondsFraction, 0.1 )) {
Laurence Lundblade67bd5512018-11-02 21:44:06 +07002042 return -9;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002043 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002044
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002045 // Epoch date float that is too large for our representation
2046 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_DATE_OVERFLOW) {
Laurence Lundblade67bd5512018-11-02 21:44:06 +07002047 return -10;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002048 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002049
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002050 // TODO: could use a few more tests with float, double, and half precsion and negative (but coverage is still pretty good)
2051
2052 return 0;
2053}
2054
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002055// Really simple basic input for tagging test
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002056static uint8_t spOptTestInput[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002057 0xd9, 0xd9, 0xf7, // CBOR magic number
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002058 0x81, // Array of one
2059 0xd8, 0x04, // non-preferred serialization of tag 4
2060 0x82, 0x01, 0x03}; // fraction 1/3
2061
2062static uint8_t spEncodedLargeTag[] = {0xdb, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0x80};
2063
2064// 0x9192939495969798, 0x88, 0x01, 0x04
2065static uint8_t spLotsOfTags[] = {0xdb, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98, 0xd8, 0x88, 0xc5, 0xc4, 0x80};
2066
2067/*
2068 The cbor.me parse of this.
2069 55799(55799(55799({6(7(-23)): 5859837686836516696(7({7(-20): 11({17(-18): 17(17(17("Organization"))),
2070 9(-17): 773("SSG"), -15: 4(5(6(7(8(9(10(11(12(13(14(15("Confusion")))))))))))), 17(-16): 17("San Diego"),
2071 17(-14): 17("US")}), 23(-19): 19({-11: 9({-9: -7}),
2072 90599561(90599561(90599561(-10))): 12(h'0102030405060708090A')})})),
2073 16(-22): 23({11(8(7(-5))): 8(-3)})})))
2074 */
2075static uint8_t spCSRWithTags[] = {
2076 0xd9, 0xd9, 0xf7, 0xd9, 0xd9, 0xf7, 0xd9, 0xd9, 0xf7, 0xa2,
2077 0xc6, 0xc7, 0x36,
2078 0xdb, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0xc7, 0xa2,
2079 0xda, 0x00, 0x00, 0x00, 0x07, 0x33,
2080 0xcb, 0xa5,
2081 0xd1, 0x31,
2082 0xd1, 0xd1, 0xd1, 0x6c,
2083 0x4f, 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74, 0x69, 0x6f, 0x6e,
2084 0xc9, 0x30,
2085 0xd9, 0x03, 0x05, 0x63,
2086 0x53, 0x53, 0x47,
2087 0x2e,
2088 0xc4, 0xc5, 0xc6, 0xc7, 0xc8, 0xc9, 0xca, 0xcb, 0xcc, 0xcd, 0xce, 0xcf, 0x69,
2089 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73, 0x69, 0x6f, 0x6e,
2090 0xd1, 0x2f,
2091 0xd1, 0x69,
2092 0x53, 0x61, 0x6e, 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f,
2093 0xd1, 0x2d,
2094 0xd1, 0x62,
2095 0x55, 0x53,
2096 0xd7, 0x32,
2097 0xd3, 0xa2,
2098 0x2a,
2099 0xc9, 0xa1,
2100 0x28,
2101 0x26,
2102 0xda, 0x05, 0x66, 0x70, 0x89, 0xda, 0x05, 0x66, 0x70, 0x89, 0xda, 0x05, 0x66, 0x70, 0x89, 0x29,
2103 0xcc, 0x4a,
2104 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,0x07, 0x08, 0x09, 0x0a,
2105 0xd0, 0x35,
2106 0xd7, 0xa1,
2107 0xcb, 0xc8, 0xc7, 0x24,
2108 0xc8, 0x22};
2109
2110static int CheckCSRMaps(QCBORDecodeContext *pDC);
2111
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002112
2113int OptTagParseTest()
2114{
2115 QCBORDecodeContext DCtx;
2116 QCBORItem Item;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002117
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002118 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spOptTestInput), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002119
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002120 //-------------------------
2121 // This text matches the magic number tag and the fraction tag
2122 if(QCBORDecode_GetNext(&DCtx, &Item)) {
2123 return -2;
2124 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002125 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002126 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC)) {
2127 return -3;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002128 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002129
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002130 if(QCBORDecode_GetNext(&DCtx, &Item)) {
2131 return -4;
2132 }
2133 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
2134 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_FRACTION) ||
2135 Item.val.uCount != 2) {
2136 return -5;
2137 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002138
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002139 // --------------------------------
2140 // This test decodes the very large tag, but it is not in
2141 // any list so it is ignored.
2142 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEncodedLargeTag), QCBOR_DECODE_MODE_NORMAL);
2143 if(QCBORDecode_GetNext(&DCtx, &Item)) {
2144 return -6;
2145 }
2146 if(Item.uTagBits) {
2147 return -7;
2148 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002149
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002150 // ----------------------------------
2151 // This test sets up a caller-config list that includes the very large tage and then matches it.
2152 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEncodedLargeTag), QCBOR_DECODE_MODE_NORMAL);
2153 const uint64_t puList[] = {0x9192939495969798, 257};
2154 const QCBORTagListIn TL = {2, puList};
2155 QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002156
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002157 if(QCBORDecode_GetNext(&DCtx, &Item)) {
2158 return -8;
2159 }
2160 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
2161 !QCBORDecode_IsTagged(&DCtx, &Item, 0x9192939495969798) ||
2162 QCBORDecode_IsTagged(&DCtx, &Item, 257) ||
2163 QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_BIGFLOAT) ||
2164 Item.val.uCount != 0) {
2165 return -9;
2166 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002167
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002168 //------------------------
2169 // This test sets up a caller-configured list, and looks up something not in it
2170 const uint64_t puLongList[17] = {1,2,1};
2171 const QCBORTagListIn TLLong = {17, puLongList};
2172 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEncodedLargeTag), QCBOR_DECODE_MODE_NORMAL);
2173 QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TLLong);
2174 if(QCBORDecode_GetNext(&DCtx, &Item)) {
2175 return -11;
2176 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002177
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002178 // -----------------------
2179 // This tests retrievel of the full tag list
2180 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spLotsOfTags), QCBOR_DECODE_MODE_NORMAL);
2181 uint64_t puTags[16];
2182 QCBORTagListOut Out = {0, 4, puTags};
2183 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2184 return -12;
2185 }
2186 if(puTags[0] != 0x9192939495969798 ||
2187 puTags[1] != 0x88 ||
2188 puTags[2] != 0x05 ||
2189 puTags[3] != 0x04) {
2190 return -13;
2191 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002192
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002193 // ----------------------
2194 // This text if too small of an out list
2195 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spLotsOfTags), QCBOR_DECODE_MODE_NORMAL);
2196 QCBORTagListOut OutSmall = {0, 3, puTags};
2197 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &OutSmall) != QCBOR_ERR_TOO_MANY_TAGS) {
2198 return -14;
2199 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002200
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002201 // ---------------
2202 // Parse a version of the "CSR" that has had a ton of tags randomly inserted
2203 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRWithTags), QCBOR_DECODE_MODE_NORMAL);
2204 int n = CheckCSRMaps(&DCtx);
2205 if(n) {
2206 return n-2000;
2207 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002208
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002209 Out = (QCBORTagListOut){0,16, puTags};
2210 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRWithTags), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002211
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002212 const uint64_t puTagList[] = {773, 1, 90599561};
2213 const QCBORTagListIn TagList = {3, puTagList};
2214 QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TagList);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002215
2216
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002217 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2218 return -100;
2219 }
2220 if(Item.uDataType != QCBOR_TYPE_MAP ||
2221 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC) ||
2222 QCBORDecode_IsTagged(&DCtx, &Item, 90599561) ||
2223 QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_DATE_EPOCH) ||
2224 Item.val.uCount != 2 ||
2225 puTags[0] != CBOR_TAG_CBOR_MAGIC ||
2226 puTags[1] != CBOR_TAG_CBOR_MAGIC ||
2227 puTags[2] != CBOR_TAG_CBOR_MAGIC ||
2228 Out.uNumUsed != 3) {
2229 return -101;
2230 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002231
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002232 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2233 return -102;
2234 }
2235 if(Item.uDataType != QCBOR_TYPE_MAP ||
2236 QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC) ||
2237 QCBORDecode_IsTagged(&DCtx, &Item, 6) ||
2238 QCBORDecode_IsTagged(&DCtx, &Item, 7) || // item is tagged 7, but 7 is not configured to be recognized
2239 Item.val.uCount != 2 ||
2240 puTags[0] != 5859837686836516696 ||
2241 puTags[1] != 7 ||
2242 Out.uNumUsed != 2) {
2243 return -103;
2244 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002245
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002246 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2247 return -104;
2248 }
2249 if(Item.uDataType != QCBOR_TYPE_MAP ||
2250 Item.uTagBits ||
2251 Item.val.uCount != 5 ||
2252 puTags[0] != 0x0b ||
2253 Out.uNumUsed != 1) {
2254 return -105;
2255 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002256
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002257 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2258 return -106;
2259 }
2260 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
2261 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_COSE_MAC0) ||
2262 Item.val.string.len != 12 ||
2263 puTags[0] != CBOR_TAG_COSE_MAC0 ||
2264 puTags[1] != CBOR_TAG_COSE_MAC0 ||
2265 puTags[2] != CBOR_TAG_COSE_MAC0 ||
2266 Out.uNumUsed != 3) {
2267 return -105;
2268 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002269
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002270 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2271 return -107;
2272 }
2273 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
2274 !QCBORDecode_IsTagged(&DCtx, &Item, 773) ||
2275 Item.val.string.len != 3 ||
2276 puTags[0] != 773 ||
2277 Out.uNumUsed != 1) {
2278 return -108;
2279 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002280
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002281 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2282 return -109;
2283 }
2284 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
2285 !QCBORDecode_IsTagged(&DCtx, &Item, 4) ||
2286 Item.val.string.len != 9 ||
2287 puTags[0] != 4 ||
2288 puTags[11] != 0x0f ||
2289 Out.uNumUsed != 12) {
2290 return -110;
2291 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002292
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002293 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2294 return -111;
2295 }
2296 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
2297 !QCBORDecode_IsTagged(&DCtx, &Item, 17) ||
2298 Item.val.string.len != 9 ||
2299 puTags[0] != 17 ||
2300 Out.uNumUsed != 1) {
2301 return -112;
2302 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002303
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002304 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2305 return -111;
2306 }
2307 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
2308 !QCBORDecode_IsTagged(&DCtx, &Item, 17) ||
2309 Item.val.string.len != 2 ||
2310 puTags[0] != 17 ||
2311 Out.uNumUsed != 1) {
2312 return -112;
2313 }
2314
2315 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2316 return -113;
2317 }
2318 if(Item.uDataType != QCBOR_TYPE_MAP ||
2319 QCBORDecode_IsTagged(&DCtx, &Item, 19) ||
2320 Item.val.uCount != 2 ||
2321 puTags[0] != 19 ||
2322 Out.uNumUsed != 1) {
2323 return -114;
2324 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002325
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002326 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2327 return -115;
2328 }
2329 if(Item.uDataType != QCBOR_TYPE_MAP ||
2330 QCBORDecode_IsTagged(&DCtx, &Item, 9) ||
2331 Item.uTagBits ||
2332 Item.val.uCount != 1 ||
2333 puTags[0] != 9 ||
2334 Out.uNumUsed != 1) {
2335 return -116;
2336 }
2337
2338 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2339 return -116;
2340 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002341 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002342 Item.val.int64 != -7 ||
2343 Item.uTagBits ||
2344 Out.uNumUsed != 0) {
2345 return -117;
2346 }
2347
2348 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2349 return -118;
2350 }
2351 if(Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
2352 Item.val.string.len != 10 ||
2353 Item.uTagBits ||
2354 puTags[0] != 12 ||
2355 Out.uNumUsed != 1) {
2356 return -119;
2357 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002358
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002359 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2360 return -120;
2361 }
2362 if(Item.uDataType != QCBOR_TYPE_MAP ||
2363 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_ENC_AS_B16) ||
2364 Item.val.uCount != 1 ||
2365 puTags[0] != 0x17 ||
2366 Out.uNumUsed != 1) {
2367 return -121;
2368 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002369
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002370 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
2371 return -122;
2372 }
2373 if(Item.uDataType != QCBOR_TYPE_INT64 ||
2374 QCBORDecode_IsTagged(&DCtx, &Item, 8) ||
2375 Item.val.int64 != -3 ||
2376 puTags[0] != 8 ||
2377 Out.uNumUsed != 1) {
2378 return -123;
2379 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002380
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002381 if(QCBORDecode_Finish(&DCtx)) {
2382 return -124;
2383 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002384
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002385 return 0;
2386}
2387
2388
2389
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002390
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002391static uint8_t spBigNumInput[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002392 0x83,
2393 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2394 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2395 0xA4,
2396 0x63, 0x42, 0x4E, 0x2B,
2397 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2398 0x18, 0x40,
2399 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2400 0x63, 0x42, 0x4E, 0x2D,
2401 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
2402 0x38, 0x3F,
2403 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
2404
2405
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002406static uint8_t spBigNum[] = {0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002407
2408
2409int BignumParseTest()
2410{
2411 QCBORDecodeContext DCtx;
2412 QCBORItem Item;
2413 int nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002414
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002415 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNumInput), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002416
2417
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002418 //
2419 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
2420 return -1;
2421 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
2422 return -1;
2423 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002424
2425 //
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002426 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
2427 return -1;
2428 if(Item.uDataType != QCBOR_TYPE_POSBIGNUM ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002429 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002430 return -1;
2431 }
2432
2433 //
2434 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
2435 return -1;
2436 if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002437 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002438 return -1;
2439 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002440
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002441 //
2442 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
2443 return -1;
2444 if(Item.uDataType != QCBOR_TYPE_MAP) {
2445 return -1;
2446 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002447
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002448 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
2449 return -1;
2450 if(Item.uDataType != QCBOR_TYPE_POSBIGNUM ||
2451 Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002452 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002453 return -1;
2454 }
2455
2456 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
2457 return -1;
2458 if(Item.uDataType != QCBOR_TYPE_POSBIGNUM ||
2459 Item.uLabelType != QCBOR_TYPE_INT64 ||
2460 Item.label.int64 != 64 ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002461 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002462 return -1;
2463 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002464
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002465 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
2466 return -1;
2467 if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM ||
2468 Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002469 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002470 return -1;
2471 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002472
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002473 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
2474 return -1;
2475 if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM ||
2476 Item.uLabelType != QCBOR_TYPE_INT64 ||
2477 Item.label.int64 != -64 ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002478 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002479 return -1;
2480 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002481
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002482 return 0;
2483}
2484
2485
2486
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302487static 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 +08002488{
2489 QCBORItem Item;
2490 int nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002491
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002492 if((nCBORError = QCBORDecode_GetNext(pCtx, &Item))) return -1;
2493 if(Item.uDataType != uDataType) return -1;
2494 if(uNestingLevel > 0) {
2495 if(Item.uLabelType != QCBOR_TYPE_INT64 && Item.uLabelType != QCBOR_TYPE_UINT64) return -1;
2496 if(Item.uLabelType == QCBOR_TYPE_INT64) {
2497 if(Item.label.int64 != nLabel) return -1;
2498 } else {
Laurence Lundblade570fab52018-10-13 18:28:27 +08002499 if(Item.label.uint64 != (uint64_t)nLabel) return -1;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002500 }
2501 }
2502 if(Item.uNestingLevel != uNestingLevel) return -1;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302503 if(Item.uNextNestLevel != uNextNest) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002504
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002505 if(pItem) {
2506 *pItem = Item;
2507 }
2508 return 0;
2509}
2510
2511
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002512// Same code checks definite and indefinite length versions of the map
2513static int CheckCSRMaps(QCBORDecodeContext *pDC)
2514{
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302515 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 0, 1, 0, NULL)) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002516
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302517 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 1, 2, -23, NULL)) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002518
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302519 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 2, 3, -20, NULL)) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002520
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302521 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -18, NULL)) return -1;
2522 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -17, NULL)) return -1;
2523 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -15, NULL)) return -1;
2524 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -16, NULL)) return -1;
2525 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 2, -14, NULL)) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002526
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302527 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 2, 3, -19, NULL)) return -1;
2528 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 3, 4, -11, NULL)) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002529
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302530 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_INT64, 4, 3, -9, NULL)) return -1;
2531 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_BYTE_STRING, 3, 1, -10, NULL)) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002532
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302533 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 1, 2, -22, NULL)) return -1;
2534 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_INT64, 2, 0, -5, NULL)) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002535
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002536 if(QCBORDecode_Finish(pDC)) return -2;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002537
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002538 return 0;
2539}
2540
2541
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002542/*
2543// cbor.me decoded output
2544{
2545 -23: {
2546 -20: {
2547 -18: "Organization",
2548 -17: "SSG",
2549 -15: "Confusion",
2550 -16: "San Diego",
2551 -14: "US"
2552 },
2553 -19: {
2554 -11: {
2555 -9: -7
2556 },
2557 -10: '\u0001\u0002\u0003\u0004\u0005\u0006\a\b\t\n'
2558 }
2559 },
2560 -22: {
2561 -5: -3
2562 }
2563}
2564 */
2565
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002566
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002567static uint8_t spCSRInput[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002568 0xa2, 0x36, 0xa2, 0x33, 0xa5, 0x31, 0x6c, 0x4f,
2569 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74,
2570 0x69, 0x6f, 0x6e, 0x30, 0x63, 0x53, 0x53, 0x47,
2571 0x2e, 0x69, 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73,
2572 0x69, 0x6f, 0x6e, 0x2f, 0x69, 0x53, 0x61, 0x6e,
2573 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f, 0x2d, 0x62,
2574 0x55, 0x53, 0x32, 0xa2, 0x2a, 0xa1, 0x28, 0x26,
2575 0x29, 0x4a, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
2576 0x07, 0x08, 0x09, 0x0a, 0x35, 0xa1, 0x24, 0x22};
2577
2578int NestedMapTest()
2579{
2580 QCBORDecodeContext DCtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002581
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002582 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002583
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002584 return CheckCSRMaps(&DCtx);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002585}
2586
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08002587
2588
2589int StringDecoderModeFailTest()
2590{
2591 QCBORDecodeContext DCtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002592
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08002593 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput), QCBOR_DECODE_MODE_MAP_STRINGS_ONLY);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002594
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08002595 QCBORItem Item;
2596 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002597
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08002598 if(QCBORDecode_GetNext(&DCtx, &Item)) {
2599 return -1;
2600 }
2601 if(Item.uDataType != QCBOR_TYPE_MAP) {
2602 return -2;
2603 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002604
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08002605 nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
2606 if(nCBORError != QCBOR_ERR_MAP_LABEL_TYPE) {
2607 return -3;
2608 }
2609
2610 return 0;
2611}
2612
2613
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002614// Same map as above, but using indefinite lengths
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002615static uint8_t spCSRInputIndefLen[] = {
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002616 0xbf, 0x36, 0xbf, 0x33, 0xbf, 0x31, 0x6c, 0x4f,
2617 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74,
2618 0x69, 0x6f, 0x6e, 0x30, 0x63, 0x53, 0x53, 0x47,
2619 0x2e, 0x69, 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73,
2620 0x69, 0x6f, 0x6e, 0x2f, 0x69, 0x53, 0x61, 0x6e,
2621 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f, 0x2d, 0x62,
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002622 0x55, 0x53, 0xff, 0x32, 0xbf, 0x2a, 0xbf, 0x28,
2623 0x26, 0xff, 0x29, 0x4a, 0x01, 0x02, 0x03, 0x04,
2624 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0xff, 0xff,
2625 0x35, 0xbf, 0x24, 0x22, 0xff, 0xff};
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002626
2627int NestedMapTestIndefLen()
2628{
2629 QCBORDecodeContext DCtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002630
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002631 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInputIndefLen), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002632
Laurence Lundblade742df4a2018-10-13 20:07:17 +08002633 return CheckCSRMaps(&DCtx);
2634}
2635
2636
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002637
Laurence Lundblade17ede402018-10-13 11:43:07 +08002638static UsefulBufC make_nested_indefinite_arrays(int n, UsefulBuf Storage)
2639{
2640 UsefulOutBuf UOB;
2641 UsefulOutBuf_Init(&UOB, Storage);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002642
Laurence Lundblade17ede402018-10-13 11:43:07 +08002643 int i;
2644 for(i = 0; i < n; i++) {
2645 UsefulOutBuf_AppendByte(&UOB, 0x9f);
2646 }
2647
2648 for(i = 0; i < n; i++) {
2649 UsefulOutBuf_AppendByte(&UOB, 0xff);
2650 }
2651 return UsefulOutBuf_OutUBuf(&UOB);
2652}
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002653
2654
Laurence Lundblade17ede402018-10-13 11:43:07 +08002655static int parse_indeflen_nested(UsefulBufC Nested, int nNestLevel)
2656{
2657 QCBORDecodeContext DC;
2658 QCBORDecode_Init(&DC, Nested, 0);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002659
Laurence Lundblade17ede402018-10-13 11:43:07 +08002660 int j;
2661 for(j = 0; j < nNestLevel; j++) {
2662 QCBORItem Item;
2663 int nReturn = QCBORDecode_GetNext(&DC, &Item);
2664 if(j >= QCBOR_MAX_ARRAY_NESTING) {
2665 // Should be in error
2666 if(nReturn != QCBOR_ERR_ARRAY_NESTING_TOO_DEEP) {
2667 return -4;
2668 } else {
2669 return 0; // Decoding doesn't recover after an error
2670 }
2671 } else {
2672 // Should be no error
2673 if(nReturn) {
2674 return -9; // Should not have got an error
2675 }
2676 }
2677 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
2678 return -7;
2679 }
2680 }
2681 int nReturn = QCBORDecode_Finish(&DC);
2682 if(nReturn) {
2683 return -3;
2684 }
2685 return 0;
2686}
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002687
2688
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302689int IndefiniteLengthNestTest()
Laurence Lundblade17ede402018-10-13 11:43:07 +08002690{
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05302691 UsefulBuf_MAKE_STACK_UB(Storage, 50);
Laurence Lundblade17ede402018-10-13 11:43:07 +08002692 int i;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002693 for(i=1; i < QCBOR_MAX_ARRAY_NESTING+4; i++) {
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08002694 const UsefulBufC Nested = make_nested_indefinite_arrays(i, Storage);
Laurence Lundblade17ede402018-10-13 11:43:07 +08002695 int nReturn = parse_indeflen_nested(Nested, i);
2696 if(nReturn) {
2697 return nReturn;
2698 }
2699 }
2700 return 0;
2701}
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002702
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002703
Laurence Lundblade6de37062018-10-15 12:22:42 +05302704
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002705static const uint8_t spIndefiniteArray[] = {0x9f, 0x01, 0x82, 0x02, 0x03, 0xff}; // [1, [2, 3]]
2706static const uint8_t spIndefiniteArrayBad1[] = {0x9f}; // No closing break
2707static const uint8_t spIndefiniteArrayBad2[] = {0x9f, 0x9f, 0x02, 0xff}; // Not enough closing breaks
2708static const uint8_t spIndefiniteArrayBad3[] = {0x9f, 0x02, 0xff, 0xff}; // Too many closing breaks
2709static const uint8_t spIndefiniteArrayBad4[] = {0x81, 0x9f}; // Unclosed indeflen inside def len
2710static const uint8_t spIndefiniteArrayBad5[] = {0x9f, 0xd1, 0xff}; // confused tag
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002711
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302712int IndefiniteLengthArrayMapTest()
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002713{
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002714 int nResult;
2715 // --- first test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002716 UsefulBufC IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArray);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002717
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002718 // Decode it and see if it is OK
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05302719 UsefulBuf_MAKE_STACK_UB(MemPool, 150);
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002720 QCBORDecodeContext DC;
2721 QCBORItem Item;
2722 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002723
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002724 QCBORDecode_SetMemPool(&DC, MemPool, false);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002725
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002726 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05302727
2728 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
2729 Item.uNestingLevel != 0 ||
2730 Item.uNextNestLevel != 1) {
2731 return -111;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002732 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002733
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002734 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05302735 if(Item.uDataType != QCBOR_TYPE_INT64 ||
2736 Item.uNestingLevel != 1 ||
2737 Item.uNextNestLevel != 1) {
2738 return -2;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002739 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002740
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002741 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05302742 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
2743 Item.uNestingLevel != 1 ||
2744 Item.uNextNestLevel != 2) {
2745 return -3;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002746 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002747
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002748 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade12b495d2018-12-17 11:15:54 -08002749 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade6de37062018-10-15 12:22:42 +05302750 Item.uNestingLevel != 2 ||
2751 Item.uNextNestLevel != 2) {
2752 return -4;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002753 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002754
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002755 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade12b495d2018-12-17 11:15:54 -08002756 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade6de37062018-10-15 12:22:42 +05302757 Item.uNestingLevel != 2 ||
2758 Item.uNextNestLevel != 0) {
2759 return -5;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002760 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002761
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002762 if(QCBORDecode_Finish(&DC)) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05302763 return -6;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002764 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002765
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002766 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002767 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad1);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002768
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002769 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002770
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002771 QCBORDecode_SetMemPool(&DC, MemPool, false);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002772
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002773 nResult = QCBORDecode_GetNext(&DC, &Item);
2774 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05302775 return -7;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002776 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002777
Laurence Lundblade570fab52018-10-13 18:28:27 +08002778 nResult = QCBORDecode_Finish(&DC);
Laurence Lundblade6de37062018-10-15 12:22:42 +05302779 if(nResult != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
2780 return -8;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002781 }
2782
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002783
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002784 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002785 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad2);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002786
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002787 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002788
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002789 QCBORDecode_SetMemPool(&DC, MemPool, false);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002790
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002791 nResult = QCBORDecode_GetNext(&DC, &Item);
2792 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05302793 return -9;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002794 }
2795
2796 nResult = QCBORDecode_GetNext(&DC, &Item);
2797 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05302798 return -10;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002799 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002800
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002801 nResult = QCBORDecode_GetNext(&DC, &Item);
2802 if(nResult || Item.uDataType != QCBOR_TYPE_INT64) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05302803 return -11;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002804 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002805
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002806 nResult = QCBORDecode_Finish(&DC);
Laurence Lundblade6de37062018-10-15 12:22:42 +05302807 if(nResult != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
2808 return -12;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002809 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002810
2811
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002812 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002813 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad3);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002814
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002815 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002816
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002817 QCBORDecode_SetMemPool(&DC, MemPool, false);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002818
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002819 nResult = QCBORDecode_GetNext(&DC, &Item);
2820 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05302821 return -13;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002822 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002823
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002824 nResult = QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05302825 if(nResult != QCBOR_ERR_BAD_BREAK) {
2826 return -14;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08002827 }
Laurence Lundblade6de37062018-10-15 12:22:42 +05302828
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002829
Laurence Lundblade570fab52018-10-13 18:28:27 +08002830 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002831 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad4);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002832
Laurence Lundblade570fab52018-10-13 18:28:27 +08002833 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002834
Laurence Lundblade570fab52018-10-13 18:28:27 +08002835 QCBORDecode_SetMemPool(&DC, MemPool, false);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002836
Laurence Lundblade570fab52018-10-13 18:28:27 +08002837 nResult = QCBORDecode_GetNext(&DC, &Item);
2838 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05302839 return -15;
Laurence Lundblade570fab52018-10-13 18:28:27 +08002840 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002841
Laurence Lundblade570fab52018-10-13 18:28:27 +08002842 nResult = QCBORDecode_GetNext(&DC, &Item);
2843 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05302844 return -16;
Laurence Lundblade570fab52018-10-13 18:28:27 +08002845 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002846
Laurence Lundblade570fab52018-10-13 18:28:27 +08002847 nResult = QCBORDecode_Finish(&DC);
Laurence Lundblade6de37062018-10-15 12:22:42 +05302848 if(nResult != QCBOR_ERR_ARRAY_OR_MAP_STILL_OPEN) {
2849 return -17;
Laurence Lundblade570fab52018-10-13 18:28:27 +08002850 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002851
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302852 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002853 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad5);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002854
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302855 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002856
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302857 QCBORDecode_SetMemPool(&DC, MemPool, false);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002858
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302859 nResult = QCBORDecode_GetNext(&DC, &Item);
2860 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05302861 return -18;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302862 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002863
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302864 nResult = QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05302865 if(nResult != QCBOR_ERR_BAD_BREAK) {
2866 return -19;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302867 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002868
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08002869 return 0;
2870}
2871
Laurence Lundblade17ede402018-10-13 11:43:07 +08002872
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002873static const uint8_t spIndefiniteLenString[] = {
Laurence Lundblade17ede402018-10-13 11:43:07 +08002874 0x81, // Array of length one
2875 0x7f, // text string marked with indefinite length
2876 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment
2877 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment
2878 0xff // ending break
2879};
2880
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002881static const uint8_t spIndefiniteLenStringBad2[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302882 0x81, // Array of length one
2883 0x7f, // text string marked with indefinite length
2884 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment
2885 0x44, 0x6d, 0x69, 0x6e, 0x67, // second segment of wrong type
2886 0xff // ending break
2887};
2888
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002889static const uint8_t spIndefiniteLenStringBad3[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302890 0x81, // Array of length one
2891 0x7f, // text string marked with indefinite length
2892 0x01, 0x02, // Not a string
2893 0xff // ending break
2894};
2895
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002896static const uint8_t spIndefiniteLenStringBad4[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302897 0x81, // Array of length one
2898 0x7f, // text string marked with indefinite length
2899 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment
2900 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment
2901 // missing end of string
2902};
2903
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002904static const uint8_t spIndefiniteLenStringLabel[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302905 0xa1, // Array of length one
2906 0x7f, // text string marked with indefinite length
2907 0x65, 0x73, 0x74, 0x72, 0x75, 0x75, // first segment
2908 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment
2909 0xff, // ending break
2910 0x01 // integer being labeled.
2911};
2912
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08002913static UsefulBufC MakeIndefiniteBigBstr(UsefulBuf Storage) // TODO: size this
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302914{
2915 UsefulOutBuf UOB;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002916
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302917 UsefulOutBuf_Init(&UOB, Storage);
2918 UsefulOutBuf_AppendByte(&UOB, 0x81);
2919 UsefulOutBuf_AppendByte(&UOB, 0x5f);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002920
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302921 int i = 0;
2922 for(int nChunkSize = 1; nChunkSize <= 128; nChunkSize *= 2) {
2923 UsefulOutBuf_AppendByte(&UOB, 0x58);
2924 UsefulOutBuf_AppendByte(&UOB, (uint8_t)nChunkSize);
2925 for(int j = 0; j < nChunkSize; j++ ) {
2926 UsefulOutBuf_AppendByte(&UOB, i);
2927 i++;
2928 }
2929 }
2930 UsefulOutBuf_AppendByte(&UOB, 0xff);
2931
2932 return UsefulOutBuf_OutUBuf(&UOB);
2933}
2934
2935static int CheckBigString(UsefulBufC BigString)
2936{
2937 if(BigString.len != 255) {
2938 return 1;
2939 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002940
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302941 for(uint8_t i = 0; i < 255; i++){
2942 if(((const uint8_t *)BigString.ptr)[i] != i) {
2943 return 1;
2944 }
2945 }
2946 return 0;
2947}
2948
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05302949
Laurence Lundbladea44d5062018-10-17 18:45:12 +05302950int IndefiniteLengthStringTest()
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302951{
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302952 QCBORDecodeContext DC;
2953 QCBORItem Item;
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05302954 // big enough for MakeIndefiniteBigBstr() + MemPool overhead
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08002955 UsefulBuf_MAKE_STACK_UB(MemPool, 350);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002956
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302957 // --- Simple normal indefinite length string ------
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002958 UsefulBufC IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenString);
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302959 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002960
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05302961 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302962 return -1;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302963 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002964
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302965 if(QCBORDecode_GetNext(&DC, &Item)) {
2966 return -2;
2967 }
2968 if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.uDataAlloc) {
2969 return -3;
2970 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002971
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302972 if(QCBORDecode_GetNext(&DC, &Item)) {
2973 return -4;
2974 }
2975 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || !Item.uDataAlloc) {
2976 return -5;
2977 }
2978 if(QCBORDecode_Finish(&DC)) {
2979 return -6;
2980 }
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05302981
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302982 // ----- types mismatch ---
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07002983 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad2), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002984
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302985 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
2986 return -7;
2987 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002988
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302989 if(QCBORDecode_GetNext(&DC, &Item)) {
2990 return -8;
2991 }
2992 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
2993 return -9;
2994 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002995
Laurence Lundblade30816f22018-11-10 13:40:22 +07002996 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_INDEFINITE_STRING_CHUNK) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05302997 return -10;
2998 }
2999
3000 // ----- not a string ---
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003001 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad3), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003002
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303003 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
3004 return -11;
3005 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003006
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303007 if(QCBORDecode_GetNext(&DC, &Item)) {
3008 return -12;
3009 }
3010 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
3011 return -13;
3012 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003013
Laurence Lundblade30816f22018-11-10 13:40:22 +07003014 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_INDEFINITE_STRING_CHUNK) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303015 return -14;
3016 }
3017
3018 // ----- no end -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003019 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad4), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003020
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303021 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
3022 return -15;
3023 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003024
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303025 if(QCBORDecode_GetNext(&DC, &Item)) {
3026 return -16;
3027 }
3028 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
3029 return -17;
3030 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003031
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303032 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_HIT_END) {
3033 return -18;
3034 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003035
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303036 // ------ Don't set a string allocator and see an error -----
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303037 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003038
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303039 QCBORDecode_GetNext(&DC, &Item);
3040 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303041 return -19;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303042 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003043
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303044 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_NO_STRING_ALLOCATOR) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303045 return -20;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303046 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003047
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303048 // ----- Mempool is way too small -----
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003049 UsefulBuf_MAKE_STACK_UB(MemPoolTooSmall, QCBOR_DECODE_MIN_MEM_POOL_SIZE-1);
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303050
3051 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
3052 if(!QCBORDecode_SetMemPool(&DC, MemPoolTooSmall, false)) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303053 return -21;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303054 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003055
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303056 // ----- Mempool is way too small -----
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05303057 UsefulBuf_MAKE_STACK_UB(BigIndefBStrStorage, 290);
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08003058 const UsefulBufC BigIndefBStr = MakeIndefiniteBigBstr(BigIndefBStrStorage);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003059
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05303060 UsefulBuf_MAKE_STACK_UB(MemPoolSmall, 80); // 80 is big enough for MemPool overhead, but not BigIndefBStr
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003061
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303062 QCBORDecode_Init(&DC, BigIndefBStr, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303063 if(QCBORDecode_SetMemPool(&DC, MemPoolSmall, false)) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303064 return -22;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303065 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003066
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303067 QCBORDecode_GetNext(&DC, &Item);
3068 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303069 return -23;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303070 }
Laurence Lundblade30816f22018-11-10 13:40:22 +07003071 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_STRING_ALLOCATE) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303072 return -24;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303073 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003074
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303075 // ---- big bstr -----
3076 QCBORDecode_Init(&DC, BigIndefBStr, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003077
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303078 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
3079 return -25;
3080 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003081
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05303082 if(QCBORDecode_GetNext(&DC, &Item)) {
3083 return -26;
3084 }
3085 if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.uDataAlloc) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303086 return -26;
3087 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003088
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303089 if(QCBORDecode_GetNext(&DC, &Item)) {
3090 return -27;
3091 }
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05303092 if(Item.uDataType != QCBOR_TYPE_BYTE_STRING || !Item.uDataAlloc || Item.uNestingLevel != 1) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303093 return -28;
3094 }
3095 if(CheckBigString(Item.val.string)) {
3096 return -3;
3097 }
3098 if(QCBORDecode_Finish(&DC)) {
3099 return -29;
3100 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003101
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303102 // --- label is an indefinite length string ------
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003103 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringLabel), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003104
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303105 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
3106 return -30;
3107 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003108
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303109 QCBORDecode_GetNext(&DC, &Item);
3110 if(Item.uDataType != QCBOR_TYPE_MAP) {
3111 return -31;
3112 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003113
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303114 if(QCBORDecode_GetNext(&DC, &Item)){
3115 return -32;
3116 }
3117 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING || Item.uDataType != QCBOR_TYPE_INT64 ||
3118 Item.uDataAlloc || !Item.uLabelAlloc ||
3119 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("struuming"))) {
3120 return -33;
3121 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003122
Laurence Lundblade57dd1442018-10-15 20:26:28 +05303123 if(QCBORDecode_Finish(&DC)) {
3124 return -34;
3125 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003126
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003127 return 0;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003128}
3129
3130
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303131int AllocAllStringsTest()
3132{
3133 QCBORDecodeContext DC;
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003134 QCBORError nCBORError;
3135
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003136
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303137 // First test, use the "CSRMap" as easy input and checking
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003138 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003139
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003140 UsefulBuf_MAKE_STACK_UB(Pool, sizeof(spCSRInput) + QCBOR_DECODE_MIN_MEM_POOL_SIZE);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003141
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003142 nCBORError = QCBORDecode_SetMemPool(&DC, Pool, 1); // Turn on copying.
3143 if(nCBORError) {
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303144 return -1;
3145 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003146
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003147 if(CheckCSRMaps(&DC)) {
3148 return -2;
3149 }
3150
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303151 // Next parse, save pointers to a few strings, destroy original and see all is OK.
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003152 UsefulBuf_MAKE_STACK_UB(CopyOfStorage, sizeof(pValidMapEncoded) + QCBOR_DECODE_MIN_MEM_POOL_SIZE);
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08003153 const UsefulBufC CopyOf = UsefulBuf_Copy(CopyOfStorage, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded));
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003154
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303155 QCBORDecode_Init(&DC, CopyOf, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08003156 UsefulBuf_Set(Pool, '/');
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303157 QCBORDecode_SetMemPool(&DC, Pool, 1); // Turn on copying.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003158
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303159 QCBORItem Item1, Item2, Item3, Item4;
3160 if((nCBORError = QCBORDecode_GetNext(&DC, &Item1)))
3161 return nCBORError;
3162 if(Item1.uDataType != QCBOR_TYPE_MAP ||
3163 Item1.val.uCount != 3)
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003164 return -3;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303165 if((nCBORError = QCBORDecode_GetNext(&DC, &Item1)))
3166 return nCBORError;
3167 if((nCBORError = QCBORDecode_GetNext(&DC, &Item2)))
3168 return nCBORError;
3169 if((nCBORError = QCBORDecode_GetNext(&DC, &Item3)))
3170 return nCBORError;
3171 if((nCBORError = QCBORDecode_GetNext(&DC, &Item4)))
3172 return nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003173
Laurence Lundblade05ec57b2018-10-21 01:50:03 +05303174 UsefulBuf_Set(CopyOfStorage, '_');
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003175
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303176 if(Item1.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303177 Item1.uDataType != QCBOR_TYPE_INT64 ||
3178 Item1.val.int64 != 42 ||
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003179 Item1.uDataAlloc != 0 ||
3180 Item1.uLabelAlloc == 0 ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09003181 UsefulBuf_Compare(Item1.label.string, UsefulBuf_FromSZ("first integer"))) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003182 return -4;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09003183 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003184
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303185
3186 if(Item2.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09003187 UsefulBuf_Compare(Item2.label.string, UsefulBuf_FromSZ("an array of two strings")) ||
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303188 Item2.uDataType != QCBOR_TYPE_ARRAY ||
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003189 Item2.uDataAlloc != 0 ||
3190 Item2.uLabelAlloc == 0 ||
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303191 Item2.val.uCount != 2)
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003192 return -5;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003193
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303194 if(Item3.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003195 Item3.uDataAlloc == 0 ||
3196 Item3.uLabelAlloc != 0 ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09003197 UsefulBuf_Compare(Item3.val.string, UsefulBuf_FromSZ("string1"))) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003198 return -6;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09003199 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003200
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303201 if(Item4.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003202 Item4.uDataAlloc == 0 ||
3203 Item4.uLabelAlloc != 0 ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09003204 UsefulBuf_Compare(Item4.val.string, UsefulBuf_FromSZ("string2"))) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003205 return -7;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09003206 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003207
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303208 // Next parse with a pool that is too small
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003209 UsefulBuf_MAKE_STACK_UB(SmallPool, QCBOR_DECODE_MIN_MEM_POOL_SIZE + 1);
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05303210 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303211 QCBORDecode_SetMemPool(&DC, SmallPool, 1); // Turn on copying.
3212 if((nCBORError = QCBORDecode_GetNext(&DC, &Item1)))
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003213 return -8;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303214 if(Item1.uDataType != QCBOR_TYPE_MAP ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09003215 Item1.val.uCount != 3) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003216 return -9;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09003217 }
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303218 if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item1))){
3219 if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item2))) {
3220 if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item3))) {
3221 nCBORError = QCBORDecode_GetNext(&DC, &Item4);
3222 }
3223 }
3224 }
Laurence Lundblade30816f22018-11-10 13:40:22 +07003225 if(nCBORError != QCBOR_ERR_STRING_ALLOCATE) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003226 return -10;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303227 }
3228
3229 return 0;
3230}
3231
Laurence Lundbladef6531662018-12-04 10:42:22 +09003232
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303233
3234int MemPoolTest(void)
Laurence Lundblade0155b622018-10-12 20:04:37 +08003235{
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003236 // Set up the decoder with a tiny bit of CBOR to parse because
3237 // nothing can be done with it unless that is set up.
Laurence Lundbladef6531662018-12-04 10:42:22 +09003238 QCBORDecodeContext DC;
3239 const uint8_t pMinimalCBOR[] = {0xa0}; // One empty map
3240 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pMinimalCBOR),0);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003241
Laurence Lundbladef6531662018-12-04 10:42:22 +09003242 // Set up an memory pool of 100 bytes
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003243 // Then fish into the internals of the decode context
3244 // to get the allocator function so it can be called directly.
3245 // Also figure out how much pool is available for use
3246 // buy subtracting out the overhead.
Laurence Lundbladef6531662018-12-04 10:42:22 +09003247 UsefulBuf_MAKE_STACK_UB(Pool, 100);
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08003248 QCBORError nError = QCBORDecode_SetMemPool(&DC, Pool, 0);
3249 if(nError) {
3250 return -9;
3251 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003252 QCBORStringAllocate pAlloc = DC.StringAllocator.pfAllocator;
3253 void *pAllocCtx = DC.StringAllocator.pAllocateCxt;
3254 size_t uAvailPool = Pool.len - QCBOR_DECODE_MIN_MEM_POOL_SIZE;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003255
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003256 // First test -- ask for one more byte than available and see failure
3257 UsefulBuf Allocated = (*pAlloc)(pAllocCtx, NULL, uAvailPool+1);
Laurence Lundbladef6531662018-12-04 10:42:22 +09003258 if(!UsefulBuf_IsNULL(Allocated)) {
3259 return -1;
3260 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003261
Laurence Lundbladef6531662018-12-04 10:42:22 +09003262 // Re do the set up for the next test that will do a successful alloc,
3263 // a fail, a free and then success
Laurence Lundbladef6531662018-12-04 10:42:22 +09003264 QCBORDecode_SetMemPool(&DC, Pool, 0);
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003265 pAlloc = DC.StringAllocator.pfAllocator;
3266 pAllocCtx = DC.StringAllocator.pAllocateCxt;
3267 uAvailPool = Pool.len - QCBOR_DECODE_MIN_MEM_POOL_SIZE;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003268
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003269 // Allocate one byte less than available and see success
3270 Allocated = (pAlloc)(pAllocCtx, NULL, uAvailPool-1);
Laurence Lundbladef6531662018-12-04 10:42:22 +09003271 if(UsefulBuf_IsNULL(Allocated)) { // expected to succeed
3272 return -2;
3273 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003274 // Ask for some more and see failure
3275 UsefulBuf Allocated2 = (*pAlloc)(pAllocCtx, NULL, uAvailPool/2);
Laurence Lundbladef6531662018-12-04 10:42:22 +09003276 if(!UsefulBuf_IsNULL(Allocated2)) { // expected to fail
3277 return -3;
3278 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003279 // Free the first allocate, retry the second and see success
3280 (*pAlloc)(pAllocCtx, Allocated.ptr, 0); // Free
3281 Allocated = (*pAlloc)(pAllocCtx, NULL, uAvailPool/2);
Laurence Lundbladef6531662018-12-04 10:42:22 +09003282 if(UsefulBuf_IsNULL(Allocated)) { // succeed because of the free
3283 return -4;
3284 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003285
Laurence Lundbladef6531662018-12-04 10:42:22 +09003286 // Re do set up for next test that involves a successful alloc,
3287 // and a successful realloc and a failed realloc
3288 QCBORDecode_SetMemPool(&DC, Pool, 0);
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003289 pAlloc = DC.StringAllocator.pfAllocator;
3290 pAllocCtx = DC.StringAllocator.pAllocateCxt;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003291
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003292 // Allocate half the pool and see success
3293 Allocated = (*pAlloc)(pAllocCtx, NULL, uAvailPool/2);
Laurence Lundbladef6531662018-12-04 10:42:22 +09003294 if(UsefulBuf_IsNULL(Allocated)) { // expected to succeed
3295 return -5;
3296 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003297 // Reallocate to take up the whole pool and see success
3298 Allocated2 = (*pAlloc)(pAllocCtx, Allocated.ptr, uAvailPool);
Laurence Lundbladef6531662018-12-04 10:42:22 +09003299 if(UsefulBuf_IsNULL(Allocated2)) {
3300 return -6;
3301 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003302 // Make sure its the same pointer and the size is right
Laurence Lundbladef6531662018-12-04 10:42:22 +09003303 if(Allocated2.ptr != Allocated.ptr || Allocated2.len != uAvailPool) {
3304 return -7;
3305 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003306 // Try to allocate more to be sure there is failure after a realloc
3307 UsefulBuf Allocated3 = (*pAlloc)(pAllocCtx, Allocated.ptr, uAvailPool+1);
3308 if(!UsefulBuf_IsNULL(Allocated3)) {
Laurence Lundbladef6531662018-12-04 10:42:22 +09003309 return -8;
3310 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003311
Laurence Lundbladef6531662018-12-04 10:42:22 +09003312 return 0;
3313}
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003314
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08003315
3316/* Just enough of an allocator to test configuration of one */
3317static UsefulBuf AllocateTestFunction(void *pCtx, void *pOldMem, size_t uNewSize)
3318{
3319 (void)pOldMem; // unused variable
3320
3321 if(uNewSize) {
3322 // Assumes the context pointer is the buffer and
3323 // nothing too big will ever be asked for.
3324 // This is only good for this basic test!
3325 return (UsefulBuf) {pCtx, uNewSize};
3326 } else {
3327 return NULLUsefulBuf;
3328 }
3329}
3330
3331
3332int SetUpAllocatorTest(void)
3333{
3334 // Set up the decoder with a tiny bit of CBOR to parse because
3335 // nothing can be done with it unless that is set up.
3336 QCBORDecodeContext DC;
3337 const uint8_t pMinimalCBOR[] = {0x62, 0x48, 0x69}; // "Hi"
3338 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pMinimalCBOR),0);
3339
3340 uint8_t pAllocatorBuffer[50];
3341
3342 // This is really just to test that this call works.
3343 // The full functionality of string allocators is tested
3344 // elsewhere with the MemPool internal allocator.
3345 QCBORDecode_SetUpAllocator(&DC, AllocateTestFunction, pAllocatorBuffer, 1);
3346
3347 QCBORItem Item;
3348 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_SUCCESS) {
3349 return -1;
3350 }
3351
3352 if(Item.uDataAlloc == 0 ||
3353 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
3354 Item.val.string.ptr != pAllocatorBuffer) {
3355 return -2;
3356 }
3357
3358 if(QCBORDecode_Finish(&DC) != QCBOR_SUCCESS) {
3359 return -3;
3360 }
3361
3362 return 0;
3363}
3364