blob: b79772df807ca8503a5134231a255dd8816d00fc [file] [log] [blame]
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001/*==============================================================================
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002 * Copyright (c) 2016-2018, The Linux Foundation.
3 * Copyright (c) 2018-2024, Laurence Lundblade.
4 * Copyright (c) 2021, Arm Limited.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are
9 * met:
Laurence Lundblade0dbc9172018-11-01 14:17:21 +070010 * Redistributions of source code must retain the above copyright
11 notice, this list of conditions and the following disclaimer.
12 * Redistributions in binary form must reproduce the above
13 copyright notice, this list of conditions and the following
14 disclaimer in the documentation and/or other materials provided
15 with the distribution.
16 * Neither the name of The Linux Foundation nor the names of its
17 contributors, nor the name "Laurence Lundblade" may be used to
18 endorse or promote products derived from this software without
19 specific prior written permission.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -080020
Laurence Lundblade0dbc9172018-11-01 14:17:21 +070021THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
22WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
23MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
24ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
25BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
28BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
30OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
31IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Laurence Lundbladeee851742020-01-08 08:37:05 -080032 =============================================================================*/
Laurence Lundblade9e3651c2018-10-10 11:49:55 +080033
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080034#include "qcbor_decode_tests.h"
Laurence Lundblade844bb5c2020-03-01 17:27:25 -080035#include "qcbor/qcbor_encode.h"
36#include "qcbor/qcbor_decode.h"
Laurence Lundblade67257dc2020-07-27 03:33:37 -070037#include "qcbor/qcbor_spiffy_decode.h"
Laurence Lundbladed4728fd2018-12-17 15:15:56 -080038#include <string.h>
Laurence Lundblade9e3651c2018-10-10 11:49:55 +080039#include <math.h> // for fabs()
Laurence Lundbladebb1062e2019-08-12 23:28:54 -070040#include "not_well_formed_cbor.h"
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -080041
Laurence Lundblade9b334962020-08-27 10:55:53 -070042// Handy macro to compare a UsefulBuf to a C string
43#define UsefulBufCompareToSZ(x, y) \
44 UsefulBuf_Compare(x, UsefulBuf_FromSZ(y))
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080045
Laurence Lundbladea2e29072018-12-30 09:20:06 -080046#ifdef PRINT_FUNCTIONS_FOR_DEBUGGING
Laurence Lundblade20db9c92018-12-17 11:40:37 -080047#include <stdio.h>
Laurence Lundbladea2e29072018-12-30 09:20:06 -080048
49static void PrintUsefulBufC(const char *szLabel, UsefulBufC Buf)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080050{
51 if(szLabel) {
52 printf("%s ", szLabel);
53 }
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -080054
Laurence Lundblade570fab52018-10-13 18:28:27 +080055 size_t i;
Laurence Lundbladea2e29072018-12-30 09:20:06 -080056 for(i = 0; i < Buf.len; i++) {
57 uint8_t Z = ((uint8_t *)Buf.ptr)[i];
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080058 printf("%02x ", Z);
59 }
60 printf("\n");
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -080061
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080062 fflush(stdout);
63}
Laurence Lundbladee2c893c2020-12-26 17:41:53 -080064#endif /* PRINT_FUNCTIONS_FOR_DEBUGGING */
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080065
Laurence Lundblade5db34da2024-05-30 03:14:35 -070066
67/* Make a test results code that includes three components. Return code
68 * is xxxyyyzzz where zz is the error code, yy is the test number and
69 * zz is check being performed
70 */
71static int32_t
72MakeTestResultCode(uint32_t uTestCase,
73 uint32_t uTestNumber,
74 QCBORError uErrorCode)
75{
76 uint32_t uCode = (uTestCase * 1000000) +
77 (uTestNumber * 1000) +
78 (uint32_t)uErrorCode;
79 return (int32_t)uCode;
80}
81
Laurence Lundbladecc7da412020-12-27 00:09:07 -080082/*
83 [
84 -9223372036854775808,
85 -4294967297,
86 -4294967296,
87 -4294967295,
88 -4294967294,
89 -2147483648,
90 -2147483647,
91 -65538,
92 -65537,
93 -65536,
94 -65535,
95 -65534,
96 -257,
97 -256,
98 -255,
99 -254,
100 -25,
101 -24,
102 -23,
103 -1,
104 0,
105 0,
106 1,
107 22,
108 23,
109 24,
110 25,
111 26,
112 254,
113 255,
114 256,
115 257,
116 65534,
117 65535,
118 65536,
119 65537,
120 65538,
121 2147483647,
122 2147483647,
123 2147483648,
124 2147483649,
125 4294967294,
126 4294967295,
127 4294967296,
128 4294967297,
129 9223372036854775807,
130 18446744073709551615
131 ]
132 */
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800133
Laurence Lundbladeb836efb2018-10-28 20:09:58 +0700134static const uint8_t spExpectedEncodedInts[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800135 0x98, 0x2f, 0x3b, 0x7f, 0xff, 0xff, 0xff, 0xff,
136 0xff, 0xff, 0xff, 0x3b, 0x00, 0x00, 0x00, 0x01,
137 0x00, 0x00, 0x00, 0x00, 0x3a, 0xff, 0xff, 0xff,
138 0xff, 0x3a, 0xff, 0xff, 0xff, 0xfe, 0x3a, 0xff,
139 0xff, 0xff, 0xfd, 0x3a, 0x7f, 0xff, 0xff, 0xff,
140 0x3a, 0x7f, 0xff, 0xff, 0xfe, 0x3a, 0x00, 0x01,
141 0x00, 0x01, 0x3a, 0x00, 0x01, 0x00, 0x00, 0x39,
142 0xff, 0xff, 0x39, 0xff, 0xfe, 0x39, 0xff, 0xfd,
143 0x39, 0x01, 0x00, 0x38, 0xff, 0x38, 0xfe, 0x38,
144 0xfd, 0x38, 0x18, 0x37, 0x36, 0x20, 0x00, 0x00,
145 0x01, 0x16, 0x17, 0x18, 0x18, 0x18, 0x19, 0x18,
146 0x1a, 0x18, 0xfe, 0x18, 0xff, 0x19, 0x01, 0x00,
147 0x19, 0x01, 0x01, 0x19, 0xff, 0xfe, 0x19, 0xff,
148 0xff, 0x1a, 0x00, 0x01, 0x00, 0x00, 0x1a, 0x00,
149 0x01, 0x00, 0x01, 0x1a, 0x00, 0x01, 0x00, 0x02,
150 0x1a, 0x7f, 0xff, 0xff, 0xff, 0x1a, 0x7f, 0xff,
151 0xff, 0xff, 0x1a, 0x80, 0x00, 0x00, 0x00, 0x1a,
152 0x80, 0x00, 0x00, 0x01, 0x1a, 0xff, 0xff, 0xff,
153 0xfe, 0x1a, 0xff, 0xff, 0xff, 0xff, 0x1b, 0x00,
154 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x1b,
155 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01,
156 0x1b, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
157 0xff, 0x1b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
158 0xff, 0xff};
159
160
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800161// return CBOR error or -1 if type of value doesn't match
162
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800163static int32_t IntegerValuesParseTestInternal(QCBORDecodeContext *pDCtx)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800164{
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700165 QCBORItem Item;
166 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800167
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800168 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700169 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800170 if(Item.uDataType != QCBOR_TYPE_ARRAY)
171 return -1;
172
173 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700174 return (int32_t)nCBORError;
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800175 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade570fab52018-10-13 18:28:27 +0800176 Item.val.int64 != -9223372036854775807LL - 1)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800177 return -1;
178
179 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700180 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800181 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade570fab52018-10-13 18:28:27 +0800182 Item.val.int64 != -4294967297)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800183 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800184
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800185 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700186 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800187 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade570fab52018-10-13 18:28:27 +0800188 Item.val.int64 != -4294967296)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800189 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800190
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800191 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700192 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800193 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade570fab52018-10-13 18:28:27 +0800194 Item.val.int64 != -4294967295)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800195 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800196
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800197 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700198 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800199 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade570fab52018-10-13 18:28:27 +0800200 Item.val.int64 != -4294967294)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800201 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800202
203
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800204 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700205 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800206 if(Item.uDataType != QCBOR_TYPE_INT64 ||
207 Item.val.int64 != -2147483648)
208 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800209
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800210 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700211 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800212 if(Item.uDataType != QCBOR_TYPE_INT64 ||
213 Item.val.int64 != -2147483647)
214 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800215
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800216 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700217 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800218 if(Item.uDataType != QCBOR_TYPE_INT64 ||
219 Item.val.int64 != -65538)
220 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800221
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800222 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700223 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800224 if(Item.uDataType != QCBOR_TYPE_INT64 ||
225 Item.val.int64 != -65537)
226 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800227
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800228 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700229 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800230 if(Item.uDataType != QCBOR_TYPE_INT64 ||
231 Item.val.int64 != -65536)
232 return -1;
233
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800234
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800235 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700236 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800237 if(Item.uDataType != QCBOR_TYPE_INT64 ||
238 Item.val.int64 != -65535)
239 return -1;
240
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800241
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800242 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700243 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800244 if(Item.uDataType != QCBOR_TYPE_INT64 ||
245 Item.val.int64 != -65534)
246 return -1;
247
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800248
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800249 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700250 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800251 if(Item.uDataType != QCBOR_TYPE_INT64 ||
252 Item.val.int64 != -257)
253 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800254
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800255 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700256 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800257 if(Item.uDataType != QCBOR_TYPE_INT64 ||
258 Item.val.int64 != -256)
259 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800260
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800261 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700262 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800263 if(Item.uDataType != QCBOR_TYPE_INT64 ||
264 Item.val.int64 != -255)
265 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800266
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800267 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700268 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800269 if(Item.uDataType != QCBOR_TYPE_INT64 ||
270 Item.val.int64 != -254)
271 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800272
273
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800274 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700275 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800276 if(Item.uDataType != QCBOR_TYPE_INT64 ||
277 Item.val.int64 != -25)
278 return -1;
279
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800280
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800281 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700282 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800283 if(Item.uDataType != QCBOR_TYPE_INT64 ||
284 Item.val.int64 != -24)
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)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700289 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800290 if(Item.uDataType != QCBOR_TYPE_INT64 ||
291 Item.val.int64 != -23)
292 return -1;
293
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800294
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800295 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700296 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800297 if(Item.uDataType != QCBOR_TYPE_INT64 ||
298 Item.val.int64 != -1)
299 return -1;
300
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800301
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800302 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700303 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800304 if(Item.uDataType != QCBOR_TYPE_INT64 ||
305 Item.val.int64 != 0)
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)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700310 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800311 if(Item.uDataType != QCBOR_TYPE_INT64 ||
312 Item.val.int64 != 0)
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)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700316 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800317 if(Item.uDataType != QCBOR_TYPE_INT64 ||
318 Item.val.int64 != 1)
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)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700323 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800324 if(Item.uDataType != QCBOR_TYPE_INT64 ||
325 Item.val.int64 != 22)
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)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700330 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800331 if(Item.uDataType != QCBOR_TYPE_INT64 ||
332 Item.val.int64 != 23)
333 return -1;
334
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800335
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800336 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700337 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800338 if(Item.uDataType != QCBOR_TYPE_INT64 ||
339 Item.val.int64 != 24)
340 return -1;
341
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800342
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800343 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700344 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800345 if(Item.uDataType != QCBOR_TYPE_INT64 ||
346 Item.val.int64 != 25)
347 return -1;
348
349 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700350 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800351 if(Item.uDataType != QCBOR_TYPE_INT64 ||
352 Item.val.int64 != 26)
353 return -1;
354
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800355
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800356 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700357 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800358 if(Item.uDataType != QCBOR_TYPE_INT64 ||
359 Item.val.int64 != 254)
360 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800361
362
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800363 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700364 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800365 if(Item.uDataType != QCBOR_TYPE_INT64 ||
366 Item.val.int64 != 255)
367 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800368
369
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800370 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700371 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800372 if(Item.uDataType != QCBOR_TYPE_INT64 ||
373 Item.val.int64 != 256)
374 return -1;
375
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800376
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800377 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700378 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800379 if(Item.uDataType != QCBOR_TYPE_INT64 ||
380 Item.val.int64 != 257)
381 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800382
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800383 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700384 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800385 if(Item.uDataType != QCBOR_TYPE_INT64 ||
386 Item.val.int64 != 65534)
387 return -1;
388
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800389
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800390 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700391 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800392 if(Item.uDataType != QCBOR_TYPE_INT64 ||
393 Item.val.int64 != 65535)
394 return -1;
395
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800396
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800397 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700398 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800399 if(Item.uDataType != QCBOR_TYPE_INT64 ||
400 Item.val.int64 != 65536)
401 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800402
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800403 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700404 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800405 if(Item.uDataType != QCBOR_TYPE_INT64 ||
406 Item.val.int64 != 65537)
407 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800408
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800409 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700410 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800411 if(Item.uDataType != QCBOR_TYPE_INT64 ||
412 Item.val.int64 != 65538)
413 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800414
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800415 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700416 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800417 if(Item.uDataType != QCBOR_TYPE_INT64 ||
418 Item.val.int64 != 2147483647)
419 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800420
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800421 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700422 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800423 if(Item.uDataType != QCBOR_TYPE_INT64 ||
424 Item.val.int64 != 2147483647)
425 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800426
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800427 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700428 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800429 if(Item.uDataType != QCBOR_TYPE_INT64 ||
430 Item.val.int64 != 2147483648)
431 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800432
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800433 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700434 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800435 if(Item.uDataType != QCBOR_TYPE_INT64 ||
436 Item.val.int64 != 2147483649)
437 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800438
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800439 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700440 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800441 if(Item.uDataType != QCBOR_TYPE_INT64 ||
442 Item.val.int64 != 4294967294)
443 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800444
445
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800446 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700447 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800448 if(Item.uDataType != QCBOR_TYPE_INT64 ||
449 Item.val.int64 != 4294967295)
450 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800451
452
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800453 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700454 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800455 if(Item.uDataType != QCBOR_TYPE_INT64 ||
456 Item.val.int64 != 4294967296)
457 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800458
459
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800460 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700461 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800462 if(Item.uDataType != QCBOR_TYPE_INT64 ||
463 Item.val.int64 != 4294967297)
464 return -1;
465
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800466
467
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800468 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700469 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800470 if(Item.uDataType != QCBOR_TYPE_INT64 ||
471 Item.val.int64 != 9223372036854775807LL)
472 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800473
474
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800475 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700476 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800477 if(Item.uDataType != QCBOR_TYPE_UINT64 ||
478 Item.val.uint64 != 18446744073709551615ULL)
479 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800480
481
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800482 if(QCBORDecode_Finish(pDCtx) != QCBOR_SUCCESS) {
483 return -1;
484 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800485
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800486 return 0;
487}
488
489
Laurence Lundbladecc7da412020-12-27 00:09:07 -0800490/* One less than the smallest negative integer allowed in C. Decoding
491 this should fail.
492 -9223372036854775809
493 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800494static const uint8_t spTooSmallNegative[] = {
495 0x3b, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
Laurence Lundblade21d1d812019-09-28 22:47:49 -1000496};
497
498
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800499/*
500 Tests the decoding of lots of different integers sizes
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +0800501 and values.
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800502 */
Maxim Zhukovd538f0a2022-12-20 20:40:38 +0300503int32_t IntegerValuesParseTest(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800504{
Laurence Lundblade21d1d812019-09-28 22:47:49 -1000505 int nReturn;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800506 QCBORDecodeContext DCtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800507
Laurence Lundblade21d1d812019-09-28 22:47:49 -1000508 QCBORDecode_Init(&DCtx,
509 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedEncodedInts),
510 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800511
Laurence Lundblade21d1d812019-09-28 22:47:49 -1000512 // The really big test of all successes
513 nReturn = IntegerValuesParseTestInternal(&DCtx);
514 if(nReturn) {
515 return nReturn;
516 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800517
Laurence Lundblade21d1d812019-09-28 22:47:49 -1000518 // The one large negative integer that can be parsed
519 QCBORDecode_Init(&DCtx,
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800520 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTooSmallNegative),
Laurence Lundblade21d1d812019-09-28 22:47:49 -1000521 QCBOR_DECODE_MODE_NORMAL);
522
523 QCBORItem item;
524 if(QCBORDecode_GetNext(&DCtx, &item) != QCBOR_ERR_INT_OVERFLOW) {
525 nReturn = -4000;
526 }
527
528 return(nReturn);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800529}
530
531
532/*
Laurence Lundbladeee851742020-01-08 08:37:05 -0800533 Creates a simple CBOR array and returns it in *pEncoded. The array is
534 malloced and needs to be freed. This is used by several tests.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800535
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800536 Two of the inputs can be set. Two other items in the array are fixed.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800537
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800538 */
539
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -0800540static uint8_t spSimpleArrayBuffer[50];
541
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800542static int32_t CreateSimpleArray(int nInt1, int nInt2, uint8_t **pEncoded, size_t *pEncodedLen)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800543{
544 QCBOREncodeContext ECtx;
545 int nReturn = -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800546
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800547 *pEncoded = NULL;
548 *pEncodedLen = INT32_MAX;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800549
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800550 // loop runs CBOR encoding twice. First with no buffer to
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -0800551 // calculate the length so buffer can be allocated correctly,
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800552 // and last with the buffer to do the actual encoding
553 do {
Laurence Lundblade0595e932018-11-02 22:22:47 +0700554 QCBOREncode_Init(&ECtx, (UsefulBuf){*pEncoded, *pEncodedLen});
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800555 QCBOREncode_OpenArray(&ECtx);
556 QCBOREncode_AddInt64(&ECtx, nInt1);
557 QCBOREncode_AddInt64(&ECtx, nInt2);
558 QCBOREncode_AddBytes(&ECtx, ((UsefulBufC) {"galactic", 8}));
559 QCBOREncode_AddBytes(&ECtx, ((UsefulBufC) {"haven token", 11}));
560 QCBOREncode_CloseArray(&ECtx);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800561
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -0800562 if(QCBOREncode_FinishGetSize(&ECtx, pEncodedLen))
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800563 goto Done;
564
565 if(*pEncoded != NULL) {
566 nReturn = 0;
567 goto Done;
568 }
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -0800569
570 // Use static buffer to avoid dependency on malloc()
571 if(*pEncodedLen > sizeof(spSimpleArrayBuffer)) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800572 goto Done;
573 }
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -0800574 *pEncoded = spSimpleArrayBuffer;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800575
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800576 } while(1);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800577
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -0800578Done:
579 return nReturn;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800580}
581
582
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800583/*
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800584 Some basic CBOR with map and array used in a lot of tests.
585 The map labels are all strings
586
Laurence Lundbladecc7da412020-12-27 00:09:07 -0800587 {
588 "first integer": 42,
589 "an array of two strings": [
590 "string1", "string2"
591 ],
592 "map in a map": {
593 "bytes 1": h'78787878',
594 "bytes 2": h'79797979',
595 "another int": 98,
596 "text 2": "lies, damn lies and statistics"
597 }
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900598 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800599 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800600static const uint8_t pValidMapEncoded[] = {
Laurence Lundbladecc7da412020-12-27 00:09:07 -0800601 0xa3, 0x6d, 0x66, 0x69, 0x72, 0x73, 0x74, 0x20,
602 0x69, 0x6e, 0x74, 0x65, 0x67, 0x65, 0x72, 0x18,
603 0x2a, 0x77, 0x61, 0x6e, 0x20, 0x61, 0x72, 0x72,
604 0x61, 0x79, 0x20, 0x6f, 0x66, 0x20, 0x74, 0x77,
605 0x6f, 0x20, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67,
606 0x73, 0x82, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e,
607 0x67, 0x31, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e,
608 0x67, 0x32, 0x6c, 0x6d, 0x61, 0x70, 0x20, 0x69,
609 0x6e, 0x20, 0x61, 0x20, 0x6d, 0x61, 0x70, 0xa4,
610 0x67, 0x62, 0x79, 0x74, 0x65, 0x73, 0x20, 0x31,
611 0x44, 0x78, 0x78, 0x78, 0x78, 0x67, 0x62, 0x79,
612 0x74, 0x65, 0x73, 0x20, 0x32, 0x44, 0x79, 0x79,
613 0x79, 0x79, 0x6b, 0x61, 0x6e, 0x6f, 0x74, 0x68,
614 0x65, 0x72, 0x20, 0x69, 0x6e, 0x74, 0x18, 0x62,
615 0x66, 0x74, 0x65, 0x78, 0x74, 0x20, 0x32, 0x78,
616 0x1e, 0x6c, 0x69, 0x65, 0x73, 0x2c, 0x20, 0x64,
617 0x61, 0x6d, 0x6e, 0x20, 0x6c, 0x69, 0x65, 0x73,
618 0x20, 0x61, 0x6e, 0x64, 0x20, 0x73, 0x74, 0x61,
619 0x74, 0x69, 0x73, 0x74, 0x69, 0x63, 0x73 };
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800620
Laurence Lundbladee2c893c2020-12-26 17:41:53 -0800621
622#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundblade2c1faf92020-06-26 22:43:56 -0700623// Same as above, but with indefinite lengths.
624static const uint8_t pValidMapIndefEncoded[] = {
Laurence Lundbladecc7da412020-12-27 00:09:07 -0800625 0xbf, 0x6d, 0x66, 0x69, 0x72, 0x73, 0x74, 0x20,
626 0x69, 0x6e, 0x74, 0x65, 0x67, 0x65, 0x72, 0x18,
627 0x2a, 0x77, 0x61, 0x6e, 0x20, 0x61, 0x72, 0x72,
628 0x61, 0x79, 0x20, 0x6f, 0x66, 0x20, 0x74, 0x77,
629 0x6f, 0x20, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67,
630 0x73, 0x9f, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e,
631 0x67, 0x31, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e,
632 0x67, 0x32, 0xff, 0x6c, 0x6d, 0x61, 0x70, 0x20,
633 0x69, 0x6e, 0x20, 0x61, 0x20, 0x6d, 0x61, 0x70,
634 0xbf, 0x67, 0x62, 0x79, 0x74, 0x65, 0x73, 0x20,
635 0x31, 0x44, 0x78, 0x78, 0x78, 0x78, 0x67, 0x62,
636 0x79, 0x74, 0x65, 0x73, 0x20, 0x32, 0x44, 0x79,
637 0x79, 0x79, 0x79, 0x6b, 0x61, 0x6e, 0x6f, 0x74,
638 0x68, 0x65, 0x72, 0x20, 0x69, 0x6e, 0x74, 0x18,
639 0x62, 0x66, 0x74, 0x65, 0x78, 0x74, 0x20, 0x32,
640 0x78, 0x1e, 0x6c, 0x69, 0x65, 0x73, 0x2c, 0x20,
641 0x64, 0x61, 0x6d, 0x6e, 0x20, 0x6c, 0x69, 0x65,
642 0x73, 0x20, 0x61, 0x6e, 0x64, 0x20, 0x73, 0x74,
643 0x61, 0x74, 0x69, 0x73, 0x74, 0x69, 0x63, 0x73,
644 0xff, 0xff};
Laurence Lundbladee2c893c2020-12-26 17:41:53 -0800645#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
Laurence Lundblade2c1faf92020-06-26 22:43:56 -0700646
647
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800648static int32_t ParseOrderedArray(const uint8_t *pEncoded,
Laurence Lundblade02625d42020-06-25 14:41:41 -0700649 size_t nLen,
650 int64_t *pInt1,
651 int64_t *pInt2,
652 const uint8_t **pBuf3,
653 size_t *pBuf3Len,
654 const uint8_t **pBuf4,
655 size_t *pBuf4Len)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800656{
657 QCBORDecodeContext DCtx;
658 QCBORItem Item;
659 int nReturn = -1; // assume error until success
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800660
Laurence Lundbladeee851742020-01-08 08:37:05 -0800661 QCBORDecode_Init(&DCtx,
662 (UsefulBufC){pEncoded, nLen},
663 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800664
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800665 // Make sure the first thing is a map
Laurence Lundblade9b334962020-08-27 10:55:53 -0700666 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
667 Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800668 goto Done;
Laurence Lundblade9b334962020-08-27 10:55:53 -0700669 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800670
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800671 // First integer
Laurence Lundblade9b334962020-08-27 10:55:53 -0700672 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
673 Item.uDataType != QCBOR_TYPE_INT64) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800674 goto Done;
Laurence Lundblade9b334962020-08-27 10:55:53 -0700675 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800676 *pInt1 = Item.val.int64;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800677
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800678 // Second integer
Laurence Lundblade9b334962020-08-27 10:55:53 -0700679 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
680 Item.uDataType != QCBOR_TYPE_INT64) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800681 goto Done;
Laurence Lundblade9b334962020-08-27 10:55:53 -0700682 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800683 *pInt2 = Item.val.int64;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800684
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800685 // First string
Laurence Lundblade9b334962020-08-27 10:55:53 -0700686 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
687 Item.uDataType != QCBOR_TYPE_BYTE_STRING) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800688 goto Done;
Laurence Lundblade9b334962020-08-27 10:55:53 -0700689 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800690 *pBuf3 = Item.val.string.ptr;
691 *pBuf3Len = Item.val.string.len;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800692
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800693 // Second string
Laurence Lundblade9b334962020-08-27 10:55:53 -0700694 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
695 Item.uDataType != QCBOR_TYPE_BYTE_STRING) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800696 goto Done;
Laurence Lundblade9b334962020-08-27 10:55:53 -0700697 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800698 *pBuf4 = Item.val.string.ptr;
699 *pBuf4Len = Item.val.string.len;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800700
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800701 nReturn = 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800702
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800703Done:
704 return(nReturn);
705}
706
707
708
Laurence Lundblade9e3651c2018-10-10 11:49:55 +0800709
Maxim Zhukovd538f0a2022-12-20 20:40:38 +0300710int32_t SimpleArrayTest(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800711{
712 uint8_t *pEncoded;
713 size_t nEncodedLen;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800714
Laurence Lundblade5e390822019-01-06 12:35:01 -0800715 int64_t i1=0, i2=0;
716 size_t i3=0, i4=0;
717 const uint8_t *s3= (uint8_t *)"";
718 const uint8_t *s4= (uint8_t *)"";
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800719
720
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800721 if(CreateSimpleArray(23, 6000, &pEncoded, &nEncodedLen) < 0) {
722 return(-1);
723 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800724
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800725 ParseOrderedArray(pEncoded, nEncodedLen, &i1, &i2, &s3, &i3, &s4, &i4);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800726
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800727 if(i1 != 23 ||
728 i2 != 6000 ||
729 i3 != 8 ||
730 i4 != 11 ||
Laurence Lundbladea44d5062018-10-17 18:45:12 +0530731 memcmp("galactic", s3, 8) !=0 ||
732 memcmp("haven token", s4, 11) !=0) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800733 return(-1);
734 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800735
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800736 return(0);
737}
738
739
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700740/*
741 [
742 0,
743 [],
744 [
745 [],
746 [
747 0
748 ],
749 {},
750 {
751 1: {},
752 2: {},
753 3: []
754 }
755 ]
756 ]
757 */
Laurence Lundbladecc7da412020-12-27 00:09:07 -0800758static uint8_t sEmpties[] = {
759 0x83, 0x00, 0x80, 0x84, 0x80, 0x81, 0x00, 0xa0,
760 0xa3, 0x01, 0xa0, 0x02, 0xa0, 0x03, 0x80};
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700761
Laurence Lundbladee2c893c2020-12-26 17:41:53 -0800762#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundblade02625d42020-06-25 14:41:41 -0700763/* Same as above, but with indefinte lengths */
Laurence Lundbladecc7da412020-12-27 00:09:07 -0800764static const uint8_t sEmptiesIndef[] = {
Laurence Lundblade02625d42020-06-25 14:41:41 -07007650x9F,
Laurence Lundblade2c1faf92020-06-26 22:43:56 -0700766 0x00,
767 0x9F,
768 0xFF,
769 0x9F,
770 0x9F,
771 0xFF,
772 0x9F,
773 0x00,
774 0xFF,
775 0xBF,
776 0xFF,
777 0xBF,
778 0x01,
779 0xBF,
780 0xFF,
781 0x02,
782 0xBF,
783 0xFF,
784 0x03,
785 0x9F,
786 0xFF,
787 0xFF,
788 0xFF,
Laurence Lundblade02625d42020-06-25 14:41:41 -0700789 0xFF};
Laurence Lundbladee2c893c2020-12-26 17:41:53 -0800790#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
Laurence Lundblade02625d42020-06-25 14:41:41 -0700791
792
793static int32_t CheckEmpties(UsefulBufC input, bool bCheckCounts)
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700794{
795 QCBORDecodeContext DCtx;
796 QCBORItem Item;
797
Laurence Lundbladeee851742020-01-08 08:37:05 -0800798 QCBORDecode_Init(&DCtx,
Laurence Lundblade02625d42020-06-25 14:41:41 -0700799 input,
Laurence Lundbladeee851742020-01-08 08:37:05 -0800800 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700801
802 // Array with 3 items
803 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
804 Item.uDataType != QCBOR_TYPE_ARRAY ||
805 Item.uNestingLevel != 0 ||
806 Item.uNextNestLevel != 1 ||
Laurence Lundblade02625d42020-06-25 14:41:41 -0700807 (bCheckCounts && Item.val.uCount != 3)) {
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700808 return -1;
809 }
810
811 // An integer 0
812 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
813 Item.uDataType != QCBOR_TYPE_INT64 ||
814 Item.uNestingLevel != 1 ||
815 Item.uNextNestLevel != 1 ||
816 Item.val.uint64 != 0) {
817 return -2;
818 }
819
820 // An empty array
821 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
822 Item.uDataType != QCBOR_TYPE_ARRAY ||
823 Item.uNestingLevel != 1 ||
824 Item.uNextNestLevel != 1 ||
Laurence Lundblade02625d42020-06-25 14:41:41 -0700825 (bCheckCounts && Item.val.uCount != 0)) {
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700826 return -3;
827 }
828
829 // An array with 4 items
830 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
831 Item.uDataType != QCBOR_TYPE_ARRAY ||
832 Item.uNestingLevel != 1 ||
833 Item.uNextNestLevel != 2 ||
Laurence Lundblade02625d42020-06-25 14:41:41 -0700834 (bCheckCounts && Item.val.uCount != 4)) {
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700835 return -4;
836 }
837
838 // An empty array
839 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
840 Item.uDataType != QCBOR_TYPE_ARRAY ||
841 Item.uNestingLevel != 2 ||
842 Item.uNextNestLevel != 2 ||
Laurence Lundblade02625d42020-06-25 14:41:41 -0700843 (bCheckCounts && Item.val.uCount != 0)) {
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700844 return -5;
845 }
846
847 // An array with 1 item
848 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
849 Item.uDataType != QCBOR_TYPE_ARRAY ||
850 Item.uNestingLevel != 2 ||
851 Item.uNextNestLevel != 3 ||
Laurence Lundblade02625d42020-06-25 14:41:41 -0700852 (bCheckCounts && Item.val.uCount != 1)) {
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700853 return -6;
854 }
855
856 // An integer 0
857 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
858 Item.uDataType != QCBOR_TYPE_INT64 ||
859 Item.uNestingLevel != 3 ||
860 Item.uNextNestLevel != 2 ||
861 Item.val.uint64 != 0) {
862 return -7;
863 }
864
865 // An empty map
866 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
867 Item.uDataType != QCBOR_TYPE_MAP ||
868 Item.uNestingLevel != 2 ||
869 Item.uNextNestLevel != 2 ||
Laurence Lundblade02625d42020-06-25 14:41:41 -0700870 (bCheckCounts && Item.val.uCount != 0)) {
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700871 return -8;
872 }
873
Laurence Lundblade5e87da62020-06-07 03:24:28 -0700874 // A map with 3 items
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700875 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
876 Item.uDataType != QCBOR_TYPE_MAP ||
877 Item.uNestingLevel != 2 ||
878 Item.uNextNestLevel != 3 ||
Laurence Lundblade02625d42020-06-25 14:41:41 -0700879 (bCheckCounts && Item.val.uCount != 3)) {
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700880 return -9;
881 }
882
883 // An empty map
884 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
885 Item.uDataType != QCBOR_TYPE_MAP ||
886 Item.uNestingLevel != 3 ||
887 Item.uNextNestLevel != 3 ||
Laurence Lundblade02625d42020-06-25 14:41:41 -0700888 (bCheckCounts && Item.val.uCount != 0)) {
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700889 return -10;
890 }
891
892 // An empty map
893 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
894 Item.uDataType != QCBOR_TYPE_MAP ||
895 Item.uNestingLevel != 3 ||
896 Item.uNextNestLevel != 3 ||
Laurence Lundblade02625d42020-06-25 14:41:41 -0700897 (bCheckCounts && Item.val.uCount != 0)) {
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700898 return -11;
899 }
900
901 // An empty array
902 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
903 Item.uDataType != QCBOR_TYPE_ARRAY ||
904 Item.uNestingLevel != 3 ||
905 Item.uNextNestLevel != 0 ||
Laurence Lundblade02625d42020-06-25 14:41:41 -0700906 (bCheckCounts && Item.val.uCount != 0)) {
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700907 return -12;
908 }
909
910 if(QCBORDecode_Finish(&DCtx) != QCBOR_SUCCESS) {
911 return -13;
912 }
Laurence Lundblade02625d42020-06-25 14:41:41 -0700913 return 0;
914}
915
916
Maxim Zhukovd538f0a2022-12-20 20:40:38 +0300917int32_t EmptyMapsAndArraysTest(void)
Laurence Lundblade02625d42020-06-25 14:41:41 -0700918{
919 int nResult;
920 nResult = CheckEmpties(UsefulBuf_FROM_BYTE_ARRAY_LITERAL(sEmpties),
921 true);
922 if(nResult) {
923 return nResult;
924 }
925
Laurence Lundbladee2c893c2020-12-26 17:41:53 -0800926#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundblade02625d42020-06-25 14:41:41 -0700927 nResult = CheckEmpties(UsefulBuf_FROM_BYTE_ARRAY_LITERAL(sEmptiesIndef),
928 false);
929
930 if(nResult) {
931 return nResult -100;
932 }
Laurence Lundbladee2c893c2020-12-26 17:41:53 -0800933#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700934
935 return 0;
936}
937
Laurence Lundblade4c532ca2021-02-18 21:31:49 -0700938
939static const uint8_t sEmptyMap[] = {
Michael Richardson87de9af2021-02-18 23:13:31 -0500940 0xA1, //# map(1)
941 0x02, //# unsigned(2)
942 0xA0, //# map(0)
943};
944
945int32_t ParseEmptyMapInMapTest(void)
946{
947 QCBORDecodeContext DCtx;
948 QCBORItem Item;
949 int nReturn = 0;
Laurence Lundblade4c532ca2021-02-18 21:31:49 -0700950 QCBORError uErr;
Michael Richardson87de9af2021-02-18 23:13:31 -0500951
952 QCBORDecode_Init(&DCtx,
953 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(sEmptyMap),
954 QCBOR_DECODE_MODE_NORMAL);
955
956 /* now open the first Map */
Laurence Lundblade4c532ca2021-02-18 21:31:49 -0700957 uErr = QCBORDecode_GetNext(&DCtx, &Item);
958 if(uErr != QCBOR_SUCCESS ||
Michael Richardson87de9af2021-02-18 23:13:31 -0500959 Item.uDataType != QCBOR_TYPE_MAP) {
960 nReturn = -3;
961 goto done;
962 }
963
964 if(QCBORDecode_GetNext(&DCtx, &Item) != 0) {
965 nReturn = -1;
966 goto done;
967 }
968 if(Item.uDataType != QCBOR_TYPE_MAP ||
969 Item.uNestingLevel != 1 ||
970 Item.label.int64 != 2) {
971 nReturn = -2;
972 goto done;
973 }
974
975 done:
976 return(nReturn);
977}
978
Laurence Lundblade4c532ca2021-02-18 21:31:49 -0700979
Michael Richardson87de9af2021-02-18 23:13:31 -0500980/* [[[[[[[[[[]]]]]]]]]] */
Laurence Lundbladecc7da412020-12-27 00:09:07 -0800981static const uint8_t spDeepArrays[] = {
982 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81,
983 0x81, 0x80};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800984
Maxim Zhukovd538f0a2022-12-20 20:40:38 +0300985int32_t ParseDeepArrayTest(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800986{
987 QCBORDecodeContext DCtx;
988 int nReturn = 0;
989 int i;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800990
Laurence Lundbladeee851742020-01-08 08:37:05 -0800991 QCBORDecode_Init(&DCtx,
992 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spDeepArrays),
993 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800994
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800995 for(i = 0; i < 10; i++) {
996 QCBORItem Item;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800997
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800998 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
999 Item.uDataType != QCBOR_TYPE_ARRAY ||
1000 Item.uNestingLevel != i) {
1001 nReturn = -1;
1002 break;
1003 }
1004 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001005
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001006 return(nReturn);
1007}
1008
Laurence Lundbladecc7da412020-12-27 00:09:07 -08001009/* Big enough to test nesting to the depth of 24
1010 [[[[[[[[[[[[[[[[[[[[[[[[[]]]]]]]]]]]]]]]]]]]]]]]]]
1011 */
1012static const uint8_t spTooDeepArrays[] = {
1013 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81,
1014 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81,
1015 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81,
1016 0x80};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001017
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03001018int32_t ParseTooDeepArrayTest(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001019{
1020 QCBORDecodeContext DCtx;
1021 int nReturn = 0;
1022 int i;
1023 QCBORItem Item;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001024
1025
Laurence Lundbladeee851742020-01-08 08:37:05 -08001026 QCBORDecode_Init(&DCtx,
1027 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTooDeepArrays),
1028 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001029
Laurence Lundblade3e0b2302023-12-04 14:02:38 -07001030 for(i = 0; i < QCBOR_MAX_ARRAY_NESTING; i++) {
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001031
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001032 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
1033 Item.uDataType != QCBOR_TYPE_ARRAY ||
1034 Item.uNestingLevel != i) {
1035 nReturn = -1;
1036 break;
1037 }
1038 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001039
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001040 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_ARRAY_DECODE_NESTING_TOO_DEEP)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001041 nReturn = -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001042
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001043 return(nReturn);
1044}
1045
1046
1047
1048
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03001049int32_t ShortBufferParseTest(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001050{
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001051 int nResult = 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001052
Laurence Lundblade06350ea2020-01-27 19:32:40 -08001053 for(size_t nNum = sizeof(spExpectedEncodedInts)-1; nNum; nNum--) {
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001054 QCBORDecodeContext DCtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001055
Laurence Lundbladeee851742020-01-08 08:37:05 -08001056 QCBORDecode_Init(&DCtx,
1057 (UsefulBufC){spExpectedEncodedInts, nNum},
1058 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001059
Laurence Lundblade06350ea2020-01-27 19:32:40 -08001060 const int nErr = IntegerValuesParseTestInternal(&DCtx);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001061
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001062 if(nErr != QCBOR_ERR_HIT_END && nErr != QCBOR_ERR_NO_MORE_ITEMS) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001063 nResult = -1;
1064 goto Done;
1065 }
1066 }
1067Done:
1068 return nResult;
1069}
1070
1071
Laurence Lundblade9e3651c2018-10-10 11:49:55 +08001072
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03001073int32_t ShortBufferParseTest2(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001074{
1075 uint8_t *pEncoded;
1076 int nReturn;
1077 size_t nEncodedLen;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001078
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001079 int64_t i1, i2;
1080 size_t i3, i4;
1081 const uint8_t *s3, *s4;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001082
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001083 nReturn = 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001084
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001085 if(CreateSimpleArray(23, 6000, &pEncoded, &nEncodedLen) < 0) {
1086 return(-1);
1087 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001088
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001089 for(nEncodedLen--; nEncodedLen; nEncodedLen--) {
Laurence Lundblade9b334962020-08-27 10:55:53 -07001090 int nResult = ParseOrderedArray(pEncoded, (uint32_t)nEncodedLen, &i1,
1091 &i2, &s3, &i3, &s4, &i4);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001092 if(nResult == 0) {
1093 nReturn = -1;
1094 }
1095 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001096
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001097 return(nReturn);
1098}
1099
Laurence Lundblade5db34da2024-05-30 03:14:35 -07001100
1101/* This test requires indef strings, HW float and preferred float,... */
1102#if !defined(QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS) && \
1103 !defined(QCBOR_DISABLE_FLOAT_HW_USE) && \
1104 !defined(QCBOR_DISABLE_PREFERRED_FLOAT) && \
1105 !defined(QCBOR_DISABLE_TAGS) && \
1106 !defined(QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS)
1107
1108static const uint8_t pPerverseLabels[] = {
1109 0xae,
1110
1111 0xf5, 0x61, 0x61,
1112
1113 0xf6, 0x61, 0x62,
1114
1115 0xf8, 0xff, 0x61, 0x63,
1116
1117 0xf9, 0x7e, 0x00, 0x61, 0x64,
1118
1119 0xfa, 0x7f, 0x7f, 0xff, 0xff, 0x61, 0x65,
1120
1121 0xfb, 0xff, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x61, 0x66,
1122
1123 0xa1, 0x19, 0x03, 0xe8, 0x10, 0x61, 0x67,
1124
1125 0x81, 0x81, 0x81, 0x80, 0x61, 0x68,
1126
1127 0xc1, 0x09, 0x61, 0x69,
1128
1129 0x82, 0x05, 0xa2, 0x01, 0x02, 0x03, 0x04, 0x61, 0x6a,
1130
1131 0xbf, 0xff, 0x61, 0x6b,
1132
1133 0x9f, 0x11, 0x12, 0x13, 0xff, 0x61, 0x6c,
1134
1135 0x7f, 0x62, 0x41, 0x42, 0x62, 0x43, 0x44, 0xff, 0x61, 0x6d,
1136
1137 0xd9, 0x01, 0x02, 0xbf, 0x7f, 0x61, 0x4a, 0x61, 0x4b, 0xff, 0x00, 0xf4, 0xd7, 0x80 ,0xff, 0x61, 0x6e
1138};
1139#endif
1140
1141
Laurence Lundbladeec290b82024-06-10 11:10:54 -07001142#ifndef QCBOR_DISABLE_NON_INTEGER_LABELS
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301143/*
1144 Decode and thoroughly check a moderately complex
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001145 set of maps. Can be run in QCBOR_DECODE_MODE_NORMAL or in
1146 QCBOR_DECODE_MODE_MAP_STRINGS_ONLY.
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301147 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001148static int32_t ParseMapTest1(QCBORDecodeMode nMode)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001149{
1150 QCBORDecodeContext DCtx;
1151 QCBORItem Item;
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001152 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001153
Laurence Lundbladeee851742020-01-08 08:37:05 -08001154 QCBORDecode_Init(&DCtx,
1155 (UsefulBufC){pValidMapEncoded, sizeof(pValidMapEncoded)},
1156 nMode);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001157
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001158 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001159 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001160 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001161 if(Item.uDataType != QCBOR_TYPE_MAP ||
1162 Item.val.uCount != 3)
1163 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001164
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001165 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001166 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001167 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07001168
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001169 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001170 Item.uDataType != QCBOR_TYPE_INT64 ||
1171 Item.val.int64 != 42 ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301172 Item.uDataAlloc ||
1173 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001174 UsefulBufCompareToSZ(Item.label.string, "first integer")) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001175 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001176 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001177
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001178 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001179 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001180 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001181 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301182 Item.uDataAlloc ||
1183 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001184 UsefulBufCompareToSZ(Item.label.string, "an array of two strings") ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001185 Item.uDataType != QCBOR_TYPE_ARRAY ||
1186 Item.val.uCount != 2)
1187 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001188
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001189 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001190 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001191 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001192 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301193 Item.uDataAlloc ||
1194 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001195 UsefulBufCompareToSZ(Item.val.string, "string1")) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001196 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001197 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001198
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001199 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001200 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001201 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001202 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301203 Item.uDataAlloc ||
1204 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001205 UsefulBufCompareToSZ(Item.val.string, "string2")) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001206 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001207 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001208
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001209 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001210 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001211 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001212 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301213 Item.uDataAlloc ||
1214 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001215 UsefulBufCompareToSZ(Item.label.string, "map in a map") ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001216 Item.uDataType != QCBOR_TYPE_MAP ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001217 Item.val.uCount != 4) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001218 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001219 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001220
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001221 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001222 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001223 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001224 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001225 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("bytes 1"))||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001226 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301227 Item.uDataAlloc ||
1228 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001229 UsefulBufCompareToSZ(Item.val.string, "xxxx")) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001230 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001231 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001232
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001233 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001234 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001235 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001236 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001237 UsefulBufCompareToSZ(Item.label.string, "bytes 2") ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001238 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301239 Item.uDataAlloc ||
1240 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001241 UsefulBufCompareToSZ(Item.val.string, "yyyy")) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001242 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001243 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001244
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001245 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001246 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001247 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001248 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301249 Item.uDataAlloc ||
1250 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001251 UsefulBufCompareToSZ(Item.label.string, "another int") ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001252 Item.uDataType != QCBOR_TYPE_INT64 ||
1253 Item.val.int64 != 98)
1254 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001255
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001256 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001257 return (int32_t)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("text 2"))||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001261 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301262 Item.uDataAlloc ||
1263 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001264 UsefulBufCompareToSZ(Item.val.string, "lies, damn lies and statistics")) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001265 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001266 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001267
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001268 return 0;
1269}
Laurence Lundbladeec290b82024-06-10 11:10:54 -07001270#endif /* ! QCBOR_DISABLE_NON_INTEGER_LABELS */
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001271
Laurence Lundblade5db34da2024-05-30 03:14:35 -07001272/* This test requires indef strings, HW float and preferred float,... */
1273#if !defined(QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS) && \
1274 !defined(QCBOR_DISABLE_FLOAT_HW_USE) && \
1275 !defined(QCBOR_DISABLE_PREFERRED_FLOAT) && \
1276 !defined(QCBOR_DISABLE_TAGS) && \
1277 !defined(QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS)
1278
1279/* Utility to decode a one byte string and match to letter. */
1280static QCBORError
1281CheckOneLetterString(QCBORDecodeContext *pDecode, uint8_t letter)
1282{
1283 UsefulBufC Text;
1284 QCBORError uErr;
1285
1286 QCBORDecode_GetTextString(pDecode, &Text);
1287 uErr = QCBORDecode_GetError(pDecode);
1288 if(uErr) {
1289 return uErr;
1290 }
1291
1292 if(Text.len != 1) {
1293 return QCBOR_ERR_FIRST_USER_DEFINED;
1294 }
1295
1296 if(*(const uint8_t *)Text.ptr != letter) {
1297 return QCBOR_ERR_FIRST_USER_DEFINED;
1298 }
1299
1300 return QCBOR_SUCCESS;
1301}
1302#endif
1303
1304
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001305/*
1306 Decode and thoroughly check a moderately complex
Laurence Lundbladed4cc1032020-10-12 04:19:47 -07001307 set of maps in the QCBOR_DECODE_MODE_MAP_AS_ARRAY mode.
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001308 */
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03001309int32_t ParseMapAsArrayTest(void)
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001310{
1311 QCBORDecodeContext DCtx;
Laurence Lundblade5db34da2024-05-30 03:14:35 -07001312 QCBORItem Item;
1313 QCBORError uErr;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001314
Laurence Lundbladeee851742020-01-08 08:37:05 -08001315 QCBORDecode_Init(&DCtx,
1316 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded),
1317 QCBOR_DECODE_MODE_MAP_AS_ARRAY);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001318
Laurence Lundblade5db34da2024-05-30 03:14:35 -07001319 if((uErr = QCBORDecode_GetNext(&DCtx, &Item))) {
1320 return MakeTestResultCode(1, 1, uErr);
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001321 }
Laurence Lundbladed61cbf32018-12-09 11:42:21 -08001322 if(Item.uDataType != QCBOR_TYPE_MAP_AS_ARRAY ||
1323 Item.val.uCount != 6) {
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001324 return -1;
1325 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001326
Laurence Lundblade5db34da2024-05-30 03:14:35 -07001327 if((uErr = QCBORDecode_GetNext(&DCtx, &Item))) {
1328 return (int32_t)uErr;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001329 }
1330 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1331 Item.uDataAlloc ||
1332 Item.uLabelAlloc ||
1333 Item.uLabelType != QCBOR_TYPE_NONE ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001334 UsefulBufCompareToSZ(Item.val.string, "first integer")) {
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001335 return -2;
1336 }
1337
Laurence Lundblade5db34da2024-05-30 03:14:35 -07001338 if((uErr = QCBORDecode_GetNext(&DCtx, &Item))) {
1339 return (int32_t)uErr;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001340 }
1341 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1342 Item.uDataType != QCBOR_TYPE_INT64 ||
1343 Item.val.int64 != 42 ||
1344 Item.uDataAlloc ||
1345 Item.uLabelAlloc) {
1346 return -3;
1347 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001348
Laurence Lundblade5db34da2024-05-30 03:14:35 -07001349 if((uErr = QCBORDecode_GetNext(&DCtx, &Item))) {
1350 return (int32_t)uErr;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001351 }
1352 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1353 Item.uDataAlloc ||
1354 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001355 UsefulBufCompareToSZ(Item.val.string, "an array of two strings") ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001356 Item.uDataType != QCBOR_TYPE_TEXT_STRING) {
1357 return -4;
1358 }
1359
Laurence Lundblade5db34da2024-05-30 03:14:35 -07001360 if((uErr = QCBORDecode_GetNext(&DCtx, &Item))) {
1361 return (int32_t)uErr;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001362 }
1363 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1364 Item.uDataAlloc ||
1365 Item.uLabelAlloc ||
1366 Item.uDataType != QCBOR_TYPE_ARRAY ||
1367 Item.val.uCount != 2) {
1368 return -5;
1369 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001370
Laurence Lundblade5db34da2024-05-30 03:14:35 -07001371 if((uErr = QCBORDecode_GetNext(&DCtx, &Item))) {
1372 return (int32_t)uErr;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001373 }
1374 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1375 Item.val.string.len != 7 ||
1376 Item.uDataAlloc ||
1377 Item.uLabelAlloc ||
1378 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("string1"))) {
1379 return -6;
1380 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001381
Laurence Lundblade5db34da2024-05-30 03:14:35 -07001382 if((uErr = QCBORDecode_GetNext(&DCtx, &Item))) {
1383 return (int32_t)uErr;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001384 }
1385 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1386 Item.uDataAlloc ||
1387 Item.uLabelAlloc ||
1388 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("string2"))) {
1389 return -7;
1390 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001391
1392
Laurence Lundblade5db34da2024-05-30 03:14:35 -07001393 if((uErr = QCBORDecode_GetNext(&DCtx, &Item))) {
1394 return (int32_t)uErr;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001395 }
1396 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1397 Item.uDataAlloc ||
1398 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001399 UsefulBufCompareToSZ(Item.val.string, "map in a map")) {
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001400 return -8;
1401 }
1402
Laurence Lundblade5db34da2024-05-30 03:14:35 -07001403 if((uErr = QCBORDecode_GetNext(&DCtx, &Item))) {
1404 return (int32_t)uErr;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001405 }
1406 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1407 Item.uDataAlloc ||
1408 Item.uLabelAlloc ||
Laurence Lundbladed61cbf32018-12-09 11:42:21 -08001409 Item.uDataType != QCBOR_TYPE_MAP_AS_ARRAY ||
1410 Item.val.uCount != 8) {
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001411 return -9;
1412 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001413
Laurence Lundblade5db34da2024-05-30 03:14:35 -07001414 if((uErr = QCBORDecode_GetNext(&DCtx, &Item))) {
1415 return (int32_t)uErr;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001416 }
1417 if(Item.uLabelType != QCBOR_TYPE_NONE ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001418 UsefulBufCompareToSZ(Item.val.string, "bytes 1") ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001419 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1420 Item.uDataAlloc ||
1421 Item.uLabelAlloc) {
1422 return -10;
1423 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001424
Laurence Lundblade5db34da2024-05-30 03:14:35 -07001425 if((uErr = QCBORDecode_GetNext(&DCtx, &Item))) {
1426 return (int32_t)uErr;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001427 }
1428 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1429 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
1430 Item.uDataAlloc ||
1431 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001432 UsefulBufCompareToSZ(Item.val.string, "xxxx")) {
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001433 return -11;
1434 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001435
Laurence Lundblade5db34da2024-05-30 03:14:35 -07001436 if((uErr = QCBORDecode_GetNext(&DCtx, &Item))) {
1437 return (int32_t)uErr;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001438 }
1439 if(Item.uLabelType != QCBOR_TYPE_NONE ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001440 UsefulBufCompareToSZ(Item.val.string, "bytes 2") ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001441 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1442 Item.uDataAlloc ||
1443 Item.uLabelAlloc) {
1444 return -12;
1445 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001446
Laurence Lundblade5db34da2024-05-30 03:14:35 -07001447 if((uErr = QCBORDecode_GetNext(&DCtx, &Item))) {
1448 return (int32_t)uErr;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001449 }
1450 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1451 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
1452 Item.uDataAlloc ||
1453 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001454 UsefulBufCompareToSZ(Item.val.string, "yyyy")) {
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001455 return -13;
1456 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001457
Laurence Lundblade5db34da2024-05-30 03:14:35 -07001458 if((uErr = QCBORDecode_GetNext(&DCtx, &Item))) {
1459 return (int32_t)uErr;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001460 }
1461 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1462 Item.uDataAlloc ||
1463 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001464 UsefulBufCompareToSZ(Item.val.string, "another int") ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001465 Item.uDataType != QCBOR_TYPE_TEXT_STRING) {
1466 return -14;
1467 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001468
Laurence Lundblade5db34da2024-05-30 03:14:35 -07001469 if((uErr = QCBORDecode_GetNext(&DCtx, &Item))) {
1470 return (int32_t)uErr;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001471 }
1472 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1473 Item.uDataAlloc ||
1474 Item.uLabelAlloc ||
1475 Item.uDataType != QCBOR_TYPE_INT64 ||
1476 Item.val.int64 != 98) {
1477 return -15;
1478 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001479
Laurence Lundblade5db34da2024-05-30 03:14:35 -07001480 if((uErr = QCBORDecode_GetNext(&DCtx, &Item))) {
1481 return (int32_t)uErr;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001482 }
1483 if(Item.uLabelType != QCBOR_TYPE_NONE ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001484 UsefulBufCompareToSZ(Item.val.string, "text 2") ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001485 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1486 Item.uDataAlloc ||
1487 Item.uLabelAlloc) {
1488 return -16;
1489 }
1490
Laurence Lundblade5db34da2024-05-30 03:14:35 -07001491 if((uErr = QCBORDecode_GetNext(&DCtx, &Item))) {
1492 return (int32_t)uErr;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001493 }
1494 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1495 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1496 Item.uDataAlloc ||
1497 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001498 UsefulBufCompareToSZ(Item.val.string, "lies, damn lies and statistics")) {
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001499 return -17;
1500 }
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07001501
1502
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001503 /*
1504 Test with map that nearly QCBOR_MAX_ITEMS_IN_ARRAY items in a
1505 map that when interpreted as an array will be too many. Test
1506 data just has the start of the map, not all the items in the map.
1507 */
1508 static const uint8_t pTooLargeMap[] = {0xb9, 0xff, 0xfd};
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07001509
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001510 QCBORDecode_Init(&DCtx,
1511 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pTooLargeMap),
1512 QCBOR_DECODE_MODE_MAP_AS_ARRAY);
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07001513
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001514 if((QCBOR_ERR_ARRAY_DECODE_TOO_LONG != QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001515 return -50;
1516 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001517
Laurence Lundblade5db34da2024-05-30 03:14:35 -07001518 /* This test requires indef strings, HW float and preferred float,... */
1519#if !defined(QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS) && \
1520 !defined(QCBOR_DISABLE_FLOAT_HW_USE) && \
1521 !defined(QCBOR_DISABLE_PREFERRED_FLOAT) && \
1522 !defined(QCBOR_DISABLE_TAGS) && \
1523 !defined(QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS)
1524
1525 UsefulBufC Encoded;
1526
1527 /* Big decode of a map with a wide variety or labels */
1528 QCBORDecode_Init(&DCtx,
1529 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pPerverseLabels),
1530 QCBOR_DECODE_MODE_MAP_AS_ARRAY);
1531 UsefulBuf_MAKE_STACK_UB(Pool, 100);
1532 QCBORDecode_SetMemPool(&DCtx, Pool, 0);
1533
1534 uErr = QCBORDecode_GetNext(&DCtx, &Item);
1535 if(uErr) {
1536 return MakeTestResultCode(10, 1, uErr);
1537 }
1538 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1539 Item.uDataType != QCBOR_TYPE_MAP_AS_ARRAY) {
1540 return MakeTestResultCode(10, 2, 0);
1541 }
1542
1543 uErr = QCBORDecode_GetNext(&DCtx, &Item);
1544 if(uErr) {
1545 return MakeTestResultCode(10, 3, uErr);
1546 }
1547 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1548 Item.uDataType != QCBOR_TYPE_TRUE) {
1549 return MakeTestResultCode(10, 4, 0);
1550 }
1551
1552 uErr = QCBORDecode_GetNext(&DCtx, &Item);
1553 if(uErr) {
1554 return MakeTestResultCode(10, 5, uErr);
1555 }
1556 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1557 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1558 ((const char *)Item.val.string.ptr)[0] != 'a') {
1559 return MakeTestResultCode(10, 6, 0);
1560 }
1561
1562 uErr = QCBORDecode_GetNext(&DCtx, &Item);
1563 if(uErr) {
1564 return MakeTestResultCode(10, 7, uErr);
1565 }
1566 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1567 Item.uDataType != QCBOR_TYPE_NULL) {
1568 return MakeTestResultCode(10, 8, 0);
1569 }
1570
1571 uErr = QCBORDecode_GetNext(&DCtx, &Item);
1572 if(uErr) {
1573 return MakeTestResultCode(10, 9, uErr);
1574 }
1575 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1576 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1577 ((const char *)Item.val.string.ptr)[0] != 'b') {
1578 return MakeTestResultCode(10, 10, 0);
1579 }
1580
1581 uErr = QCBORDecode_GetNext(&DCtx, &Item);
1582 if(uErr) {
1583 return MakeTestResultCode(10, 11, uErr);
1584 }
1585 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1586 Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE ||
1587 Item.val.int64 != 255) {
1588 return MakeTestResultCode(10, 12, 0);
1589 }
1590
1591 uErr = QCBORDecode_GetNext(&DCtx, &Item);
1592 if(uErr) {
1593 return MakeTestResultCode(10, 13, uErr);
1594 }
1595 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1596 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1597 ((const char *)Item.val.string.ptr)[0] != 'c') {
1598 return MakeTestResultCode(10, 14, 0);
1599 }
1600
1601 uErr = QCBORDecode_GetNext(&DCtx, &Item);
1602 if(uErr) {
1603 return MakeTestResultCode(10, 15, uErr);
1604 }
1605 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1606 Item.uDataType != QCBOR_TYPE_DOUBLE ||
1607 !isnan(Item.val.dfnum)) {
1608 return MakeTestResultCode(10, 16, 0);
1609 }
1610
1611 uErr = QCBORDecode_GetNext(&DCtx, &Item);
1612 if(uErr) {
1613 return MakeTestResultCode(10, 17, uErr);
1614 }
1615 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1616 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1617 ((const char *)Item.val.string.ptr)[0] != 'd') {
1618 return MakeTestResultCode(10, 18, 0);
1619 }
1620
1621
1622 uErr = QCBORDecode_GetNext(&DCtx, &Item);
1623 if(uErr) {
1624 return MakeTestResultCode(10, 19, uErr);
1625 }
1626 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1627 Item.uDataType != QCBOR_TYPE_DOUBLE ||
1628 Item.val.dfnum != 3.4028234663852886E+38) {
1629 return MakeTestResultCode(10, 20, 0);
1630 }
1631
1632 uErr = QCBORDecode_GetNext(&DCtx, &Item);
1633 if(uErr) {
1634 return MakeTestResultCode(10, 21, uErr);
1635 }
1636 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1637 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1638 ((const char *)Item.val.string.ptr)[0] != 'e') {
1639 return MakeTestResultCode(10, 22, 0);
1640 }
1641
1642 uErr = QCBORDecode_GetNext(&DCtx, &Item);
1643 if(uErr) {
1644 return MakeTestResultCode(10, 23, uErr);
1645 }
1646 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1647 Item.uDataType != QCBOR_TYPE_DOUBLE ||
1648 Item.val.dfnum != -INFINITY) {
1649 return MakeTestResultCode(10, 24, 0);
1650 }
1651
1652 uErr = QCBORDecode_GetNext(&DCtx, &Item);
1653 if(uErr) {
1654 return MakeTestResultCode(10, 25, uErr);
1655 }
1656 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1657 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1658 ((const char *)Item.val.string.ptr)[0] != 'f') {
1659 return MakeTestResultCode(10, 26, 0);
1660 }
1661
1662 uErr = QCBORDecode_GetNext(&DCtx, &Item);
1663 if(uErr) {
1664 return MakeTestResultCode(10, 26, uErr);
1665 }
1666 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1667 Item.uDataType != QCBOR_TYPE_MAP_AS_ARRAY ||
1668 Item.val.uCount != 2) {
1669 return MakeTestResultCode(10, 27, 0);
1670 }
1671
1672 uErr = QCBORDecode_GetNext(&DCtx, &Item);
1673 if(uErr) {
1674 return MakeTestResultCode(10, 28, uErr);
1675 }
1676 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1677 Item.uDataType != QCBOR_TYPE_INT64 ||
1678 Item.val.int64 != 1000) {
1679 return MakeTestResultCode(10, 29, 0);
1680 }
1681
1682 uErr = QCBORDecode_GetNext(&DCtx, &Item);
1683 if(uErr) {
1684 return MakeTestResultCode(10, 30, uErr);
1685 }
1686 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1687 Item.uDataType != QCBOR_TYPE_INT64 ||
1688 Item.val.int64 != 16) {
1689 return MakeTestResultCode(10, 31, 0);
1690 }
1691
1692 uErr = QCBORDecode_GetNext(&DCtx, &Item);
1693 if(uErr) {
1694 return MakeTestResultCode(10, 32, uErr);
1695 }
1696 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1697 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1698 ((const char *)Item.val.string.ptr)[0] != 'g') {
1699 return MakeTestResultCode(10, 33, 0);
1700 }
1701
1702 for(int i = 0 ; i < 4; i++) {
1703 uErr = QCBORDecode_GetNext(&DCtx, &Item);
1704 if(uErr) {
1705 return MakeTestResultCode(10, 34, uErr);
1706 }
1707 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1708 Item.uDataType != QCBOR_TYPE_ARRAY) {
1709 return MakeTestResultCode(10, 35, 0);
1710 }
1711 if(i != 3) {
1712 if(Item.val.uCount != 1) {
1713 return MakeTestResultCode(10, 35, 0);
1714 }
1715 }
1716 }
1717 uErr = QCBORDecode_GetNext(&DCtx, &Item);
1718 if(uErr) {
1719 return MakeTestResultCode(10, 36, uErr);
1720 }
1721 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1722 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1723 ((const char *)Item.val.string.ptr)[0] != 'h') {
1724 return MakeTestResultCode(10, 37, 0);
1725 }
1726
1727 uErr = QCBORDecode_GetNext(&DCtx, &Item);
1728 if(uErr) {
1729 return MakeTestResultCode(10, 38, uErr);
1730 }
1731 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1732 Item.uDataType != QCBOR_TYPE_DATE_EPOCH) {
1733 return MakeTestResultCode(10, 39, 0);
1734 }
1735
1736 uErr = QCBORDecode_GetNext(&DCtx, &Item);
1737 if(uErr) {
1738 return MakeTestResultCode(10, 40, uErr);
1739 }
1740 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1741 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1742 ((const char *)Item.val.string.ptr)[0] != 'i') {
1743 return MakeTestResultCode(10, 41, 0);
1744 }
1745
1746 uErr = QCBORDecode_GetNext(&DCtx, &Item);
1747 if(uErr) {
1748 return MakeTestResultCode(10, 42, uErr);
1749 }
1750 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1751 Item.uDataType != QCBOR_TYPE_ARRAY ||
1752 Item.val.uCount != 2) {
1753 return MakeTestResultCode(10, 31, 0);
1754 }
1755
1756 uErr = QCBORDecode_GetNext(&DCtx, &Item);
1757 if(uErr) {
1758 return MakeTestResultCode(10, 43, uErr);
1759 }
1760 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1761 Item.uDataType != QCBOR_TYPE_INT64) {
1762 return MakeTestResultCode(10, 31, 0);
1763 }
1764
1765 uErr = QCBORDecode_GetNext(&DCtx, &Item);
1766 if(uErr) {
1767 return MakeTestResultCode(10, 44, uErr);
1768 }
1769 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1770 Item.uDataType != QCBOR_TYPE_MAP_AS_ARRAY ||
1771 Item.val.uCount != 4) {
1772 return MakeTestResultCode(10, 45, 0);
1773 }
1774
1775 for(int i = 0 ; i < 4; i++) {
1776 uErr = QCBORDecode_GetNext(&DCtx, &Item);
1777 if(uErr) {
1778 return MakeTestResultCode(10, 46, uErr);
1779 }
1780 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1781 Item.uDataType != QCBOR_TYPE_INT64) {
1782 return MakeTestResultCode(10, 47, 0);
1783 }
1784 }
1785 uErr = QCBORDecode_GetNext(&DCtx, &Item);
1786 if(uErr) {
1787 return MakeTestResultCode(10, 48, uErr);
1788 }
1789 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1790 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1791 ((const char *)Item.val.string.ptr)[0] != 'j') {
1792 return MakeTestResultCode(10, 49, 0);
1793 }
1794
1795 uErr = QCBORDecode_GetNext(&DCtx, &Item);
1796 if(uErr) {
1797 return MakeTestResultCode(10, 50, uErr);
1798 }
1799 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1800 Item.uDataType != QCBOR_TYPE_MAP_AS_ARRAY ||
1801 Item.val.uCount != UINT16_MAX) {
1802 return MakeTestResultCode(10, 51, 0);
1803 }
1804
1805 uErr = QCBORDecode_GetNext(&DCtx, &Item);
1806 if(uErr) {
1807 return MakeTestResultCode(10, 52, uErr);
1808 }
1809 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1810 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1811 ((const char *)Item.val.string.ptr)[0] != 'k') {
1812 return MakeTestResultCode(10, 53, 0);
1813 }
1814
1815 uErr = QCBORDecode_GetNext(&DCtx, &Item);
1816 if(uErr) {
1817 return MakeTestResultCode(10, 54, uErr);
1818 }
1819 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1820 Item.uDataType != QCBOR_TYPE_ARRAY ||
1821 Item.val.uCount != UINT16_MAX) {
1822 return MakeTestResultCode(10, 55, 0);
1823 }
1824
1825 for(int i = 0 ; i < 3; i++) {
1826 uErr = QCBORDecode_GetNext(&DCtx, &Item);
1827 if(uErr) {
1828 return MakeTestResultCode(10, 56, uErr);
1829 }
1830 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1831 Item.uDataType != QCBOR_TYPE_INT64) {
1832 return MakeTestResultCode(10, 57, 0);
1833 }
1834 }
1835 uErr = QCBORDecode_GetNext(&DCtx, &Item);
1836 if(uErr) {
1837 return MakeTestResultCode(10, 58, uErr);
1838 }
1839 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1840 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1841 ((const char *)Item.val.string.ptr)[0] != 'l') {
1842 return MakeTestResultCode(10, 59, 0);
1843 }
1844
1845 uErr = QCBORDecode_GetNext(&DCtx, &Item);
1846 if(uErr) {
1847 return MakeTestResultCode(10, 60, uErr);
1848 }
1849 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1850 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1851 Item.val.string.len != 4) {
1852 return MakeTestResultCode(10, 61, 0);
1853 }
1854
1855 uErr = QCBORDecode_GetNext(&DCtx, &Item);
1856 if(uErr) {
1857 return MakeTestResultCode(10, 62, uErr);
1858 }
1859 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1860 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1861 ((const char *)Item.val.string.ptr)[0] != 'm') {
1862 return MakeTestResultCode(10, 63, 0);
1863 }
1864
1865 uErr = QCBORDecode_GetNext(&DCtx, &Item);
1866 if(uErr) {
1867 return MakeTestResultCode(10, 64, uErr);
1868 }
1869 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1870 Item.uDataType != QCBOR_TYPE_MAP_AS_ARRAY ||
1871 !QCBORDecode_IsTagged(&DCtx, &Item, 258) ||
1872 Item.val.uCount != UINT16_MAX) {
1873 return MakeTestResultCode(10, 65, 0);
1874 }
1875
1876 uErr = QCBORDecode_GetNext(&DCtx, &Item);
1877 if(uErr) {
1878 return MakeTestResultCode(10, 66, uErr);
1879 }
1880 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1881 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1882 Item.val.string.len != 2) {
1883 return MakeTestResultCode(10, 67, 0);
1884 }
1885
1886 uErr = QCBORDecode_GetNext(&DCtx, &Item);
1887 if(uErr) {
1888 return MakeTestResultCode(10, 68, uErr);
1889 }
1890 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1891 Item.uDataType != QCBOR_TYPE_INT64 ||
1892 Item.val.int64 != 0) {
1893 return MakeTestResultCode(10, 69, 0);
1894 }
1895
1896 uErr = QCBORDecode_GetNext(&DCtx, &Item);
1897 if(uErr) {
1898 return MakeTestResultCode(10, 70, uErr);
1899 }
1900 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1901 Item.uDataType != QCBOR_TYPE_FALSE) {
1902 return MakeTestResultCode(10, 71, 0);
1903 }
1904
1905 uErr = QCBORDecode_GetNext(&DCtx, &Item);
1906 if(uErr) {
1907 return MakeTestResultCode(10, 72, uErr);
1908 }
1909 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1910 Item.uDataType != QCBOR_TYPE_ARRAY ||
1911 !QCBORDecode_IsTagged(&DCtx, &Item, 23) ||
1912 Item.val.uCount != 0) {
1913 return MakeTestResultCode(10, 73, 0);
1914 }
1915
1916 uErr = QCBORDecode_GetNext(&DCtx, &Item);
1917 if(uErr) {
1918 return MakeTestResultCode(10, 74, uErr);
1919 }
1920 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1921 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1922 ((const char *)Item.val.string.ptr)[0] != 'n') {
1923 return MakeTestResultCode(10, 75, 0);
1924 }
1925
1926
1927 /* Big decode of a map with a wide variety or labels */
1928 QCBORDecode_Init(&DCtx,
1929 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pPerverseLabels),
1930 QCBOR_DECODE_MODE_MAP_AS_ARRAY);
1931 QCBORDecode_SetMemPool(&DCtx, Pool, 0);
1932
1933 QCBORDecode_EnterArray(&DCtx, &Item);
1934 bool b;
1935 QCBORDecode_GetBool(&DCtx, &b);
1936
1937 uErr = CheckOneLetterString(&DCtx, 'a');
1938 if(uErr) {
1939 return MakeTestResultCode(11, 1, uErr);
1940 }
1941
1942 QCBORDecode_GetNull(&DCtx);
1943 uErr = CheckOneLetterString(&DCtx, 'b');
1944 if(uErr) {
1945 return MakeTestResultCode(11, 2, uErr);
1946 }
1947
1948 QCBORDecode_VGetNext(&DCtx, &Item);
1949 uErr = CheckOneLetterString(&DCtx, 'c');
1950 if(uErr) {
1951 return MakeTestResultCode(11, 3, uErr);
1952 }
1953
1954 double dNum;
1955 QCBORDecode_GetDouble(&DCtx, &dNum);
1956 if(!isnan(dNum)) {
1957 return MakeTestResultCode(11, 4, 0);
1958 }
1959 uErr = CheckOneLetterString(&DCtx, 'd');
1960 if(uErr) {
1961 return MakeTestResultCode(11, 5, uErr);
1962 }
1963
1964 QCBORDecode_GetDouble(&DCtx, &dNum);
1965 if( dNum != 3.4028234663852886E+38 ) {
1966 return MakeTestResultCode(11, 6, 0);
1967 }
1968 uErr = CheckOneLetterString(&DCtx, 'e');
1969 if(uErr) {
1970 return MakeTestResultCode(11, 7, uErr);
1971 }
1972
1973 QCBORDecode_GetDouble(&DCtx, &dNum);
1974 if(dNum != -INFINITY) {
1975 return MakeTestResultCode(11, 8, 0);
1976 }
1977 uErr = CheckOneLetterString(&DCtx, 'f');
1978 if(uErr) {
1979 return MakeTestResultCode(11, 9, uErr);
1980 }
1981
1982 int64_t nInt;
1983 QCBORDecode_EnterArray(&DCtx, &Item);
1984 QCBORDecode_GetInt64(&DCtx, &nInt);
1985 QCBORDecode_GetInt64(&DCtx, &nInt);
1986 QCBORDecode_ExitArray(&DCtx);
1987 uErr = CheckOneLetterString(&DCtx, 'g');
1988 if(uErr) {
1989 return MakeTestResultCode(11, 10, uErr);
1990 }
1991
1992 QCBORDecode_EnterArray(&DCtx, &Item);
1993 QCBORDecode_EnterArray(&DCtx, &Item);
1994 QCBORDecode_EnterArray(&DCtx, &Item);
1995 QCBORDecode_EnterArray(&DCtx, &Item);
1996 QCBORDecode_ExitArray(&DCtx);
1997 QCBORDecode_ExitArray(&DCtx);
1998 QCBORDecode_ExitArray(&DCtx);
1999 QCBORDecode_ExitArray(&DCtx);
2000 uErr = CheckOneLetterString(&DCtx, 'h');
2001 if(uErr) {
2002 return MakeTestResultCode(11, 11, uErr);
2003 }
2004 QCBORDecode_GetEpochDate(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &nInt);
2005 uErr = CheckOneLetterString(&DCtx, 'i');
2006 if(uErr) {
2007 return MakeTestResultCode(11, 12, uErr);
2008 }
2009
2010 QCBORDecode_EnterArray(&DCtx, &Item);
2011 QCBORDecode_GetInt64(&DCtx, &nInt);
2012 QCBORDecode_EnterArray(&DCtx, &Item);
2013 QCBORDecode_GetInt64(&DCtx, &nInt);
2014 QCBORDecode_GetInt64(&DCtx, &nInt);
2015 QCBORDecode_GetInt64(&DCtx, &nInt);
2016 QCBORDecode_GetInt64(&DCtx, &nInt);
2017 QCBORDecode_ExitArray(&DCtx);
2018 QCBORDecode_ExitArray(&DCtx);
2019 uErr = CheckOneLetterString(&DCtx, 'j');
2020 if(uErr) {
2021 return MakeTestResultCode(11, 13, uErr);
2022 }
2023
2024 QCBORDecode_GetArray(&DCtx, &Item, &Encoded);
2025 uErr = CheckOneLetterString(&DCtx, 'k');
2026 if(uErr) {
2027 return MakeTestResultCode(11, 14, uErr);
2028 }
2029
2030 QCBORDecode_EnterArray(&DCtx, &Item);
2031 QCBORDecode_GetInt64(&DCtx, &nInt);
2032 QCBORDecode_GetInt64(&DCtx, &nInt);
2033 QCBORDecode_GetInt64(&DCtx, &nInt);
2034 QCBORDecode_ExitArray(&DCtx);
2035 uErr = CheckOneLetterString(&DCtx, 'l');
2036 if(uErr) {
2037 return MakeTestResultCode(11, 15, uErr);
2038 }
2039
2040 QCBORDecode_GetTextString(&DCtx, &Encoded);
2041 uErr = CheckOneLetterString(&DCtx, 'm');
2042 if(uErr) {
2043 return MakeTestResultCode(11, 16, uErr);
2044 }
2045
2046 QCBORDecode_EnterArray(&DCtx, &Item);
2047 if(!QCBORDecode_IsTagged(&DCtx, &Item, 258)) {
2048 return MakeTestResultCode(11, 17, 0);
2049 }
2050 if(Item.uDataType != QCBOR_TYPE_MAP_AS_ARRAY) {
2051 return MakeTestResultCode(11, 18, 0);
2052 }
2053 if(Item.val.uCount != UINT16_MAX) {
2054 return MakeTestResultCode(11, 19, 0);
2055 }
2056 QCBORDecode_GetTextString(&DCtx, &Encoded);
2057 if(Encoded.len != 2) {
2058 return MakeTestResultCode(11, 20, 0);
2059 } QCBORDecode_GetInt64(&DCtx, &nInt);
2060 QCBORDecode_GetBool(&DCtx, &b);
2061 if(b != false) {
2062 return MakeTestResultCode(11, 21, 0);
2063 }
2064 QCBORDecode_EnterArray(&DCtx, &Item);
2065 if(!QCBORDecode_IsTagged(&DCtx, &Item, 23)) {
2066 return MakeTestResultCode(11, 22, 0);
2067 }
2068 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
2069 return MakeTestResultCode(11, 23, 0);
2070 }
2071 if(Item.val.uCount != 0) {
2072 return MakeTestResultCode(11, 24, 0);
2073 }
2074 QCBORDecode_ExitArray(&DCtx);
2075 QCBORDecode_ExitArray(&DCtx);
2076 uErr = CheckOneLetterString(&DCtx, 'n');
2077 if(uErr) {
2078 return MakeTestResultCode(11, 25, uErr);
2079 }
2080
2081 QCBORDecode_ExitArray(&DCtx);
2082 uErr = QCBORDecode_Finish(&DCtx);
2083 if(uErr) {
2084 return MakeTestResultCode(11, 26, uErr);
2085 }
2086#endif /* QCBOR_DISABLE_... */
Laurence Lundbladed4cc1032020-10-12 04:19:47 -07002087
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09002088 return 0;
2089}
2090
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002091
Laurence Lundblade5db34da2024-05-30 03:14:35 -07002092
Laurence Lundbladeec290b82024-06-10 11:10:54 -07002093#ifndef QCBOR_DISABLE_NON_INTEGER_LABELS
Laurence Lundblade5db34da2024-05-30 03:14:35 -07002094
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002095/*
Laurence Lundbladefab1b522018-10-19 13:40:52 +05302096 Fully or partially decode pValidMapEncoded. When
2097 partially decoding check for the right error code.
2098 How much partial decoding depends on nLevel.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002099
Laurence Lundbladefab1b522018-10-19 13:40:52 +05302100 The partial decodes test error conditions of
2101 incomplete encoded input.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002102
Laurence Lundbladefab1b522018-10-19 13:40:52 +05302103 This could be combined with the above test
2104 and made prettier and maybe a little more
2105 thorough.
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002106 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002107static int32_t ExtraBytesTest(int nLevel)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002108{
2109 QCBORDecodeContext DCtx;
2110 QCBORItem Item;
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07002111 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002112
Laurence Lundbladeee851742020-01-08 08:37:05 -08002113 QCBORDecode_Init(&DCtx,
2114 (UsefulBufC){pValidMapEncoded, sizeof(pValidMapEncoded)},
2115 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002116
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08002117 if(nLevel < 1) {
2118 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_EXTRA_BYTES) {
2119 return -1;
2120 } else {
2121 return 0;
2122 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002123 }
Laurence Lundbladefab1b522018-10-19 13:40:52 +05302124
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002125
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09002126 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07002127 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09002128 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002129 if(Item.uDataType != QCBOR_TYPE_MAP ||
2130 Item.val.uCount != 3)
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002131 return -2;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002132
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08002133 if(nLevel < 2) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002134 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
2135 return -3;
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08002136 } else {
2137 return 0;
2138 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002139 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002140
2141
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09002142 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07002143 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09002144 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002145 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002146 Item.uDataType != QCBOR_TYPE_INT64 ||
2147 Item.val.uCount != 42 ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07002148 UsefulBufCompareToSZ(Item.label.string, "first integer")) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002149 return -4;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09002150 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002151
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08002152 if(nLevel < 3) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002153 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
2154 return -5;
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08002155 } else {
2156 return 0;
2157 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002158 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002159
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09002160 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07002161 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09002162 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002163 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07002164 UsefulBufCompareToSZ(Item.label.string, "an array of two strings") ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002165 Item.uDataType != QCBOR_TYPE_ARRAY ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09002166 Item.val.uCount != 2) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002167 return -6;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09002168 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002169
2170
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08002171 if(nLevel < 4) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002172 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
2173 return -7;
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08002174 } else {
2175 return 0;
2176 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002177 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002178
2179
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09002180 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07002181 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09002182 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002183 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07002184 UsefulBufCompareToSZ(Item.val.string, "string1")) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002185 return -8;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09002186 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002187
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08002188 if(nLevel < 5) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002189 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
2190 return -9;
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08002191 } else {
2192 return 0;
2193 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002194 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002195
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09002196 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07002197 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09002198 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002199 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07002200 UsefulBufCompareToSZ(Item.val.string, "string2")) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002201 return -10;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09002202 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002203
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08002204 if(nLevel < 6) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002205 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
2206 return -11;
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08002207 } else {
2208 return 0;
2209 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002210 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002211
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09002212 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07002213 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09002214 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002215 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07002216 UsefulBufCompareToSZ(Item.label.string, "map in a map") ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002217 Item.uDataType != QCBOR_TYPE_MAP ||
2218 Item.val.uCount != 4)
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002219 return -12;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002220
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08002221 if(nLevel < 7) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002222 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
2223 return -13;
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08002224 } else {
2225 return 0;
2226 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002227 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002228
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09002229 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07002230 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09002231 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002232 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07002233 UsefulBufCompareToSZ(Item.label.string, "bytes 1") ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002234 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07002235 UsefulBufCompareToSZ(Item.val.string, "xxxx")) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002236 return -14;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09002237 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002238
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08002239 if(nLevel < 8) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002240 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
2241 return -15;
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08002242 } else {
2243 return 0;
2244 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002245 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002246
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09002247 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07002248 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09002249 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002250 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07002251 UsefulBufCompareToSZ(Item.label.string, "bytes 2") ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002252 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07002253 UsefulBufCompareToSZ(Item.val.string, "yyyy")) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002254 return -16;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09002255 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002256
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08002257 if(nLevel < 9) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002258 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
2259 return -17;
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08002260 } else {
2261 return 0;
2262 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002263 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002264
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09002265 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07002266 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09002267 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002268 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07002269 UsefulBufCompareToSZ(Item.label.string, "another int") ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002270 Item.uDataType != QCBOR_TYPE_INT64 ||
2271 Item.val.int64 != 98)
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002272 return -18;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002273
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08002274 if(nLevel < 10) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002275 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
2276 return -19;
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08002277 } else {
2278 return 0;
2279 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002280 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002281
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09002282 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07002283 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09002284 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002285 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09002286 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("text 2"))||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002287 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07002288 UsefulBufCompareToSZ(Item.val.string, "lies, damn lies and statistics")) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002289 return -20;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09002290 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002291
Laurence Lundbladefab1b522018-10-19 13:40:52 +05302292 if(QCBORDecode_Finish(&DCtx)) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002293 return -21;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002294 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002295
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002296 return 0;
2297}
2298
2299
2300
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03002301int32_t ParseMapTest(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002302{
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002303 // Parse a moderatly complex map structure very thoroughly
2304 int32_t nResult = ParseMapTest1(QCBOR_DECODE_MODE_NORMAL);
2305 if(nResult) {
2306 return nResult;
2307 }
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08002308
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002309 // Again, but in strings-only mode. It should succeed since the input
2310 // map has only string labels.
2311 nResult = ParseMapTest1(QCBOR_DECODE_MODE_MAP_STRINGS_ONLY);
2312 if(nResult) {
2313 return nResult;
2314 }
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08002315
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002316 // Again, but try to finish the decoding before the end of the
2317 // input at 10 different place and see that the right error code
2318 // is returned.
2319 for(int i = 0; i < 10; i++) {
2320 nResult = ExtraBytesTest(i);
2321 if(nResult) {
2322 break;
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08002323 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002324 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002325
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002326 return nResult;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002327}
Laurence Lundbladeec290b82024-06-10 11:10:54 -07002328#endif /* ! QCBOR_DISABLE_NON_INTEGER_LABELS */
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002329
2330
Laurence Lundbladecc7da412020-12-27 00:09:07 -08002331/* The simple-values including some not well formed */
2332static const uint8_t spSimpleValues[] = {
2333 0x8a, 0xf4, 0xf5, 0xf6, 0xf7, 0xff, 0xe0, 0xf3,
2334 0xf8, 0x00, 0xf8, 0x13, 0xf8, 0x1f, 0xf8, 0x20,
2335 0xf8, 0xff};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002336
Laurence Lundblade3888f002024-06-12 21:20:56 -07002337/* A map of good simple values, plus one well-formed integer */
2338static const uint8_t spGoodSimpleValues[] = {
2339 0xa9, 0x01, 0xf4, 0x02, 0xf5, 0x03, 0xf6, 0x04, 0xf7,
2340 0x05, 0xe0, 0x06, 0xf3, 0x07, 0xf8, 0x20, 0x61, 0x40,
2341 0xf8, 0xff, 0x0f, 0x0f};
2342
2343int32_t SimpleValueDecodeTests(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002344{
2345 QCBORDecodeContext DCtx;
Laurence Lundblade3888f002024-06-12 21:20:56 -07002346 QCBORItem Item;
2347 QCBORError uErr;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002348
Laurence Lundbladeee851742020-01-08 08:37:05 -08002349 QCBORDecode_Init(&DCtx,
2350 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSimpleValues),
2351 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002352
2353
Laurence Lundblade3888f002024-06-12 21:20:56 -07002354 if((uErr = QCBORDecode_GetNext(&DCtx, &Item)))
2355 return (int32_t)uErr;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002356 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
2357 Item.val.uCount != 10)
Laurence Lundblade3888f002024-06-12 21:20:56 -07002358 return 1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002359
Laurence Lundblade3888f002024-06-12 21:20:56 -07002360 if((uErr = QCBORDecode_GetNext(&DCtx, &Item)))
2361 return (int32_t)uErr;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002362 if(Item.uDataType != QCBOR_TYPE_FALSE)
Laurence Lundblade3888f002024-06-12 21:20:56 -07002363 return 2;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002364
Laurence Lundblade3888f002024-06-12 21:20:56 -07002365 if((uErr = QCBORDecode_GetNext(&DCtx, &Item)))
2366 return (int32_t)uErr;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002367 if(Item.uDataType != QCBOR_TYPE_TRUE)
Laurence Lundblade3888f002024-06-12 21:20:56 -07002368 return 3;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002369
Laurence Lundblade3888f002024-06-12 21:20:56 -07002370 if((uErr = QCBORDecode_GetNext(&DCtx, &Item)))
2371 return (int32_t)uErr;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002372 if(Item.uDataType != QCBOR_TYPE_NULL)
Laurence Lundblade3888f002024-06-12 21:20:56 -07002373 return 4;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002374
Laurence Lundblade3888f002024-06-12 21:20:56 -07002375 if((uErr = QCBORDecode_GetNext(&DCtx, &Item)))
2376 return (int32_t)uErr;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002377 if(Item.uDataType != QCBOR_TYPE_UNDEF)
Laurence Lundblade3888f002024-06-12 21:20:56 -07002378 return 5;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002379
2380 // A break
Laurence Lundblade9e3651c2018-10-10 11:49:55 +08002381 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_BREAK)
Laurence Lundblade3888f002024-06-12 21:20:56 -07002382 return 6;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002383
Laurence Lundblade3888f002024-06-12 21:20:56 -07002384 if((uErr = QCBORDecode_GetNext(&DCtx, &Item)))
2385 return (int32_t)uErr;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002386 if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 0)
Laurence Lundblade3888f002024-06-12 21:20:56 -07002387 return 7;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002388
Laurence Lundblade3888f002024-06-12 21:20:56 -07002389 if((uErr = QCBORDecode_GetNext(&DCtx, &Item)))
2390 return (int32_t)uErr;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002391 if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 19)
Laurence Lundblade3888f002024-06-12 21:20:56 -07002392 return 8;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002393
Laurence Lundblade077475f2019-04-26 09:06:33 -07002394 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_TYPE_7)
Laurence Lundblade3888f002024-06-12 21:20:56 -07002395 return 9;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002396
Laurence Lundblade077475f2019-04-26 09:06:33 -07002397 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_TYPE_7)
Laurence Lundblade3888f002024-06-12 21:20:56 -07002398 return 10;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002399
Laurence Lundblade077475f2019-04-26 09:06:33 -07002400 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_TYPE_7)
Laurence Lundblade3888f002024-06-12 21:20:56 -07002401 return 11;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002402
Laurence Lundblade3888f002024-06-12 21:20:56 -07002403 if((uErr = QCBORDecode_GetNext(&DCtx, &Item)))
2404 return (int32_t)uErr;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002405 if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 32)
Laurence Lundblade3888f002024-06-12 21:20:56 -07002406 return 12;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002407
Laurence Lundblade3888f002024-06-12 21:20:56 -07002408 if((uErr = QCBORDecode_GetNext(&DCtx, &Item)))
2409 return (int32_t)uErr;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002410 if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 255)
Laurence Lundblade3888f002024-06-12 21:20:56 -07002411 return 13;
2412
2413
2414 QCBORDecode_Init(&DCtx,
2415 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spGoodSimpleValues),
2416 QCBOR_DECODE_MODE_NORMAL);
2417
2418 uint8_t uSimple;
2419
2420 QCBORDecode_EnterMap(&DCtx, &Item);
2421 QCBORDecode_GetSimple(&DCtx, &uSimple);
2422 if(QCBORDecode_GetError(&DCtx) || uSimple != CBOR_SIMPLEV_FALSE) {
2423 return 20;
2424 }
2425 QCBORDecode_GetSimple(&DCtx, &uSimple);
2426 if(QCBORDecode_GetError(&DCtx) || uSimple != CBOR_SIMPLEV_TRUE) {
2427 return 21;
2428 }
2429 QCBORDecode_GetSimple(&DCtx, &uSimple);
2430 if(QCBORDecode_GetError(&DCtx) || uSimple != CBOR_SIMPLEV_NULL) {
2431 return 22;
2432 }
2433 QCBORDecode_GetSimple(&DCtx, &uSimple);
2434 if(QCBORDecode_GetError(&DCtx) || uSimple != CBOR_SIMPLEV_UNDEF) {
2435 return 23;
2436 }
2437 QCBORDecode_GetSimple(&DCtx, &uSimple);
2438 if(QCBORDecode_GetError(&DCtx) || uSimple != 0) {
2439 return 24;
2440 }
2441 QCBORDecode_GetSimple(&DCtx, &uSimple);
2442 if(QCBORDecode_GetError(&DCtx) || uSimple != 19) {
2443 return 25;
2444 }
2445 QCBORDecode_GetSimple(&DCtx, &uSimple);
2446 if(QCBORDecode_GetError(&DCtx) || uSimple != 32) {
2447 return 26;
2448 }
2449 QCBORDecode_GetSimple(&DCtx, &uSimple);
2450 if(QCBORDecode_GetError(&DCtx) || uSimple != 255) {
2451 return 27;
2452 }
2453 QCBORDecode_VGetNext(&DCtx, &Item);
2454 QCBORDecode_GetSimple(&DCtx, &uSimple);
2455 if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_NO_MORE_ITEMS) {
2456 return 28;
2457 }
2458
2459 QCBORDecode_Rewind(&DCtx);
2460
2461 QCBORDecode_GetSimpleInMapN(&DCtx, 6, &uSimple);
2462 if(QCBORDecode_GetError(&DCtx) || uSimple != 19) {
2463 return 30;
2464 }
2465
2466 QCBORDecode_GetSimpleInMapSZ(&DCtx, "@", &uSimple);
2467 if(QCBORDecode_GetError(&DCtx) || uSimple != 255) {
2468 return 31;
2469 }
2470
2471 QCBORDecode_GetSimpleInMapN(&DCtx, 99, &uSimple);
2472 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_LABEL_NOT_FOUND) {
2473 return 32;
2474 }
2475
2476 QCBORDecode_GetSimpleInMapSZ(&DCtx, "xx", &uSimple);
2477 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_LABEL_NOT_FOUND) {
2478 return 33;
2479 }
2480
2481 QCBORDecode_GetSimpleInMapN(&DCtx, 15, &uSimple);
2482 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
2483 return 34;
2484 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002485
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002486 return 0;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002487}
2488
2489
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03002490int32_t NotWellFormedTests(void)
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002491{
2492 // Loop over all the not-well-formed instance of CBOR
2493 // that are test vectors in not_well_formed_cbor.h
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08002494 const uint16_t nArraySize = C_ARRAY_COUNT(paNotWellFormedCBOR,
2495 struct someBinaryBytes);
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002496 for(uint16_t nIterate = 0; nIterate < nArraySize; nIterate++) {
2497 const struct someBinaryBytes *pBytes = &paNotWellFormedCBOR[nIterate];
2498 const UsefulBufC Input = (UsefulBufC){pBytes->p, pBytes->n};
2499
Laurence Lundbladeee851742020-01-08 08:37:05 -08002500 // Set up decoder context. String allocator needed for indefinite
2501 // string test cases
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002502 QCBORDecodeContext DCtx;
2503 QCBORDecode_Init(&DCtx, Input, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08002504#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002505 UsefulBuf_MAKE_STACK_UB(Pool, 100);
2506 QCBORDecode_SetMemPool(&DCtx, Pool, 0);
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08002507#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002508
2509 // Loop getting items until no more to get
Laurence Lundbladef71e1622020-08-06 18:52:13 -07002510 QCBORError uCBORError;
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002511 do {
2512 QCBORItem Item;
2513
Laurence Lundbladef71e1622020-08-06 18:52:13 -07002514 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
2515 } while(uCBORError == QCBOR_SUCCESS);
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002516
2517 // Every test vector must fail with
2518 // a not-well-formed error. If not
2519 // this test fails.
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002520 if(!QCBORDecode_IsNotWellFormedError(uCBORError) &&
Laurence Lundbladef71e1622020-08-06 18:52:13 -07002521 uCBORError != QCBOR_ERR_NO_MORE_ITEMS) {
Laurence Lundblade4bf6e712020-12-10 11:53:21 -08002522 /* Return index of failure and QCBOR error in the result */
2523 return (int32_t)(nIterate * 100 + uCBORError);
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002524 }
2525 }
2526 return 0;
2527}
2528
2529
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002530struct DecodeFailTestInput {
2531 const char *szDescription; /* Description of the test */
2532 QCBORDecodeMode DecoderMode; /* The QCBOR Decoder Mode for test */
2533 UsefulBufC Input; /* Chunk of CBOR that cases error */
2534 QCBORError nError; /* The expected error */
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002535};
2536
Laurence Lundblade59289e52019-12-30 13:44:37 -08002537
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002538static int32_t
2539ProcessDecodeFailures(const struct DecodeFailTestInput *pFailInputs, const int nNumFails)
Laurence Lundblade59289e52019-12-30 13:44:37 -08002540{
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002541 int nIndex;
2542 QCBORDecodeContext DCtx;
2543 QCBORError uCBORError;
2544 QCBORItem Item;
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08002545
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002546 for(nIndex = 0; nIndex < nNumFails; nIndex++) {
2547 const struct DecodeFailTestInput *pF = &pFailInputs[nIndex];
2548
2549 QCBORDecode_Init(&DCtx, pF->Input, pF->DecoderMode);
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08002550
2551#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002552 /* Set up the decoding context including a memory pool so that
2553 * indefinite length items can be checked.
2554 */
Laurence Lundblade59289e52019-12-30 13:44:37 -08002555 UsefulBuf_MAKE_STACK_UB(Pool, 100);
Laurence Lundblade830fbf92020-05-31 17:22:33 -07002556
Laurence Lundblade4bf6e712020-12-10 11:53:21 -08002557 uCBORError = QCBORDecode_SetMemPool(&DCtx, Pool, 0);
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002558 if(uCBORError != QCBOR_SUCCESS) {
2559 return -1;
Laurence Lundblade59289e52019-12-30 13:44:37 -08002560 }
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08002561#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
2562
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002563 if(nIndex == 8) {
2564 uCBORError = 9; /* For setting break points */
Laurence Lundblade37286c02022-09-03 10:05:02 -07002565 }
2566
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002567 /* Iterate until there is an error of some sort of error */
Laurence Lundblade59289e52019-12-30 13:44:37 -08002568 do {
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002569 /* Set to something none-zero, something other than QCBOR_TYPE_NONE */
Laurence Lundblade59289e52019-12-30 13:44:37 -08002570 memset(&Item, 0x33, sizeof(Item));
2571
Laurence Lundblade4bf6e712020-12-10 11:53:21 -08002572 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
2573 } while(uCBORError == QCBOR_SUCCESS);
2574
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002575 /* Must get the expected error or the this test fails
2576 * The data and label type must also be QCBOR_TYPE_NONE.
2577 */
Laurence Lundblade4bf6e712020-12-10 11:53:21 -08002578 if(uCBORError != pF->nError ||
Laurence Lundblade59289e52019-12-30 13:44:37 -08002579 Item.uDataType != QCBOR_TYPE_NONE ||
2580 Item.uLabelType != QCBOR_TYPE_NONE) {
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002581 return (int32_t)(nIndex * 1000 + (int)uCBORError);
Laurence Lundblade59289e52019-12-30 13:44:37 -08002582 }
2583 }
2584
2585 return 0;
2586}
2587
2588
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002589static const struct DecodeFailTestInput Failures[] = {
2590 /* Most of this is copied from not_well_formed.h. Here the error
2591 * code returned is also checked.
2592 */
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002593
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08002594#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002595 /* Indefinite length strings must be closed off */
2596 { "An indefinite length byte string not closed off",
2597 QCBOR_DECODE_MODE_NORMAL,
2598 {"0x5f\x41\x00", 3},
2599 QCBOR_ERR_HIT_END
2600 },
2601 { "An indefinite length text string not closed off",
2602 QCBOR_DECODE_MODE_NORMAL,
2603 {"\x7f\x61\x00", 3},
2604 QCBOR_ERR_HIT_END
2605 },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002606
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002607 /* All the chunks in an indefinite length string must be of the
2608 * type of indefinite length string
2609 */
2610 { "Indefinite length byte string with text string chunk",
2611 QCBOR_DECODE_MODE_NORMAL,
2612 {"\x5f\x61\x00\xff", 4},
2613 QCBOR_ERR_INDEFINITE_STRING_CHUNK
2614 },
2615 { "Indefinite length text string with a byte string chunk",
2616 QCBOR_DECODE_MODE_NORMAL,
2617 {"\x7f\x41\x00\xff", 4},
2618 QCBOR_ERR_INDEFINITE_STRING_CHUNK
2619 },
2620 { "Indefinite length byte string with a positive integer chunk",
2621 QCBOR_DECODE_MODE_NORMAL,
2622 {"\x5f\x00\xff", 3},
2623 QCBOR_ERR_INDEFINITE_STRING_CHUNK
2624 },
2625 { "Indefinite length byte string with an negative integer chunk",
2626 QCBOR_DECODE_MODE_NORMAL,
2627 {"\x5f\x21\xff", 3},
2628 QCBOR_ERR_INDEFINITE_STRING_CHUNK
2629 },
2630 { "Indefinite length byte string with an array chunk",
2631 QCBOR_DECODE_MODE_NORMAL,
2632 {"\x5f\x80\xff", 3},
2633 QCBOR_ERR_INDEFINITE_STRING_CHUNK
2634 },
2635 { "Indefinite length byte string with an map chunk",
2636 QCBOR_DECODE_MODE_NORMAL,
2637 {"\x5f\xa0\xff", 3},
2638 QCBOR_ERR_INDEFINITE_STRING_CHUNK
2639 },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002640
Laurence Lundblade37286c02022-09-03 10:05:02 -07002641#ifndef QCBOR_DISABLE_TAGS
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002642 { "Indefinite length byte string with tagged integer chunk",
2643 QCBOR_DECODE_MODE_NORMAL,
2644 {"\x5f\xc0\x00\xff", 4},
2645 QCBOR_ERR_INDEFINITE_STRING_CHUNK
2646 },
Laurence Lundblade37286c02022-09-03 10:05:02 -07002647#else
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002648 { "Indefinite length byte string with tagged integer chunk",
2649 QCBOR_DECODE_MODE_NORMAL,
2650 {"\x5f\xc0\x00\xff", 4},
2651 QCBOR_ERR_TAGS_DISABLED
2652 },
Laurence Lundblade37286c02022-09-03 10:05:02 -07002653#endif /* QCBOR_DISABLE_TAGS */
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002654
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002655 { "Indefinite length byte string with an simple type chunk",
2656 QCBOR_DECODE_MODE_NORMAL,
2657 {"\x5f\xe0\xff", 3},
2658 QCBOR_ERR_INDEFINITE_STRING_CHUNK
2659 },
2660 { "???",
2661 QCBOR_DECODE_MODE_NORMAL,
2662 {"\x5f\x5f\x41\x00\xff\xff", 6},
2663 QCBOR_ERR_INDEFINITE_STRING_CHUNK
2664 },
2665 { "indefinite length text string with indefinite string inside",
2666 QCBOR_DECODE_MODE_NORMAL,
2667 {"\x7f\x7f\x61\x00\xff\xff", 6},
2668 QCBOR_ERR_INDEFINITE_STRING_CHUNK
2669 },
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08002670#else /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
2671
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08002672#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
2673
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002674 /* Definte length maps and arrays must be closed by having the right number of items */
2675 { "A definte length array that is supposed to have 1 item, but has none",
2676 QCBOR_DECODE_MODE_NORMAL,
2677 {"\x81", 1},
2678 QCBOR_ERR_NO_MORE_ITEMS
2679 },
2680 { "A definte length array that is supposed to have 2 items, but has only 1",
2681 QCBOR_DECODE_MODE_NORMAL,
2682 {"\x82\x00", 2},
2683 QCBOR_ERR_NO_MORE_ITEMS
2684 },
2685 { "A definte length array that is supposed to have 511 items, but has only 1",
2686 QCBOR_DECODE_MODE_NORMAL,
2687 {"\x9a\x01\xff\x00", 4},
2688 QCBOR_ERR_HIT_END
2689 },
2690 { "A definte length map that is supposed to have 1 item, but has none",
2691 QCBOR_DECODE_MODE_NORMAL,
2692 {"\xa1", 1},
2693 QCBOR_ERR_NO_MORE_ITEMS
2694 },
2695 { "A definte length map that is supposed to have s item, but has only 1",
2696 QCBOR_DECODE_MODE_NORMAL,
2697 {"\xa2\x01\x02", 3},
2698 QCBOR_ERR_NO_MORE_ITEMS
2699 },
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08002700#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002701 /* Indefinte length maps and arrays must be ended by a break */
2702 { "Indefinite length array with zero items and no break",
2703 QCBOR_DECODE_MODE_NORMAL,
2704 {"\x9f", 1},
2705 QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002706
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002707 { "Indefinite length array with two items and no break",
2708 QCBOR_DECODE_MODE_NORMAL,
2709 {"\x9\x01\x02", 3},
2710 QCBOR_ERR_NO_MORE_ITEMS
2711 },
2712 { "Indefinite length map with zero items and no break",
2713 QCBOR_DECODE_MODE_NORMAL,
2714 {"\xbf", 1},
2715 QCBOR_ERR_NO_MORE_ITEMS
2716 },
2717 { "Indefinite length map with two items and no break",
2718 QCBOR_DECODE_MODE_NORMAL,
2719 {"\xbf\x01\x02\x01\x02", 5},
2720 QCBOR_ERR_NO_MORE_ITEMS
2721 },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002722
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002723 /* Nested maps and arrays must be closed off (some extra nested test vectors) */
2724 { "Unclosed indefinite array containing a closed definite length array",
2725 QCBOR_DECODE_MODE_NORMAL,
2726 {"\x9f\x80\x00", 3},
2727 QCBOR_ERR_NO_MORE_ITEMS
2728 },
2729
2730 { "Definite length array containing an unclosed indefinite length array",
2731 QCBOR_DECODE_MODE_NORMAL,
2732 {"\x81\x9f", 2},
2733 QCBOR_ERR_NO_MORE_ITEMS
2734 },
2735 { "Unclosed indefinite map containing a closed definite length array",
2736 QCBOR_DECODE_MODE_NORMAL,
2737 {"\xbf\x01\x80\x00\xa0", 5},
2738 QCBOR_ERR_NO_MORE_ITEMS
2739 },
2740 { "Definite length map containing an unclosed indefinite length array",
2741 QCBOR_DECODE_MODE_NORMAL,
2742 {"\xa1\x02\x9f", 3},
2743 QCBOR_ERR_NO_MORE_ITEMS
2744 },
2745 { "Deeply nested definite length arrays with deepest one unclosed",
2746 QCBOR_DECODE_MODE_NORMAL,
2747 {"\x81\x81\x81\x81\x81\x81\x81\x81\x81", 9},
2748 QCBOR_ERR_NO_MORE_ITEMS
2749 },
2750 { "Deeply nested indefinite length arrays with deepest one unclosed",
2751 QCBOR_DECODE_MODE_NORMAL,
2752 {"\x9f\x9f\x9f\x9f\x9f\xff\xff\xff\xff", 9},
2753 QCBOR_ERR_NO_MORE_ITEMS
2754 },
2755 { "Mixed nesting with indefinite unclosed",
2756 QCBOR_DECODE_MODE_NORMAL,
2757 {"\x9f\x81\x9f\x81\x9f\x9f\xff\xff\xff", 9},
2758 QCBOR_ERR_NO_MORE_ITEMS },
2759 { "Mixed nesting with definite unclosed",
2760 QCBOR_DECODE_MODE_NORMAL,
2761 {"\x9f\x82\x9f\x81\x9f\x9f\xff\xff\xff\xff", 10},
2762 QCBOR_ERR_BAD_BREAK
2763 },
2764 { "Unclosed indefinite length map in definite length maps",
2765 QCBOR_DECODE_MODE_NORMAL,
2766 {"\xa1\x01\xa2\x02\xbf\xff\x02\xbf", 8},
2767 QCBOR_ERR_NO_MORE_ITEMS
2768 },
2769 { "Unclosed definite length map in indefinite length maps",
2770 QCBOR_DECODE_MODE_NORMAL,
2771 {"\xbf\x01\xbf\x02\xa1", 5},
2772 QCBOR_ERR_NO_MORE_ITEMS
2773 },
2774 { "Unclosed indefinite length array in definite length maps",
2775 QCBOR_DECODE_MODE_NORMAL,
2776 {"\xa1\x01\xa2\x02\x9f\xff\x02\x9f", 8},
2777 QCBOR_ERR_NO_MORE_ITEMS
2778 },
2779 { "Unclosed definite length array in indefinite length maps",
2780 QCBOR_DECODE_MODE_NORMAL,
2781 {"\xbf\x01\xbf\x02\x81", 5},
2782 QCBOR_ERR_NO_MORE_ITEMS
2783 },
2784 { "Unclosed indefinite length map in definite length arrays",
2785 QCBOR_DECODE_MODE_NORMAL,
2786 {"\x81\x82\xbf\xff\xbf", 5},
2787 QCBOR_ERR_NO_MORE_ITEMS
2788 },
2789 { "Unclosed definite length map in indefinite length arrays",
2790 QCBOR_DECODE_MODE_NORMAL,
2791 {"\x9f\x9f\xa1", 3},
2792 QCBOR_ERR_NO_MORE_ITEMS
2793 },
2794
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08002795#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002796
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002797 /* The "argument" for the data item is incomplete */
2798 { "Positive integer missing 1 byte argument",
2799 QCBOR_DECODE_MODE_NORMAL,
2800 {"\x18", 1},
2801 QCBOR_ERR_HIT_END
2802 },
2803 { "Positive integer missing 2 byte argument",
2804 QCBOR_DECODE_MODE_NORMAL,
2805 {"\x19", 1},
2806 QCBOR_ERR_HIT_END
2807 },
2808 { "Positive integer missing 4 byte argument",
2809 QCBOR_DECODE_MODE_NORMAL,
2810 {"\x1a", 1},
2811 QCBOR_ERR_HIT_END
2812 },
2813 { "Positive integer missing 8 byte argument",
2814 QCBOR_DECODE_MODE_NORMAL,
2815 {"\x1b", 1},
2816 QCBOR_ERR_HIT_END
2817 },
2818 { "Positive integer missing 1 byte of 2 byte argument",
2819 QCBOR_DECODE_MODE_NORMAL,
2820 {"\x19\x01", 2},
2821 QCBOR_ERR_HIT_END
2822 },
2823 { "Positive integer missing 2 bytes of 4 byte argument",
2824 QCBOR_DECODE_MODE_NORMAL,
2825 {"\x1a\x01\x02", 3},
2826 QCBOR_ERR_HIT_END
2827 },
2828 { "Positive integer missing 1 bytes of 7 byte argument",
2829 QCBOR_DECODE_MODE_NORMAL,
2830 {"\x1b\x01\x02\x03\x04\x05\x06\x07", 8},
2831 QCBOR_ERR_HIT_END
2832 },
2833 { "Negative integer missing 1 byte argument",
2834 QCBOR_DECODE_MODE_NORMAL,
2835 {"\x38", 1},
2836 QCBOR_ERR_HIT_END
2837 },
2838 { "Binary string missing 1 byte argument",
2839 QCBOR_DECODE_MODE_NORMAL,
2840 {"\x58", 1},
2841 QCBOR_ERR_HIT_END
2842 },
2843 { "Text string missing 1 byte argument",
2844 QCBOR_DECODE_MODE_NORMAL,
2845 {"\x78", 1},
2846 QCBOR_ERR_HIT_END
2847 },
2848 { "Array missing 1 byte argument",
2849 QCBOR_DECODE_MODE_NORMAL,
2850 {"\x98", 1},
2851 QCBOR_ERR_HIT_END
2852 },
2853 { "Map missing 1 byte argument",
2854 QCBOR_DECODE_MODE_NORMAL,
2855 {"\xb8", 1},
2856 QCBOR_ERR_HIT_END
2857 },
2858 { "Tag missing 1 byte argument",
2859 QCBOR_DECODE_MODE_NORMAL,
2860 {"\xd8", 1},
2861 QCBOR_ERR_HIT_END
2862 },
2863 { "Simple missing 1 byte argument",
2864 QCBOR_DECODE_MODE_NORMAL,
2865 {"\xf8", 1},
2866 QCBOR_ERR_HIT_END
2867 },
2868 { "half-precision with 1 byte argument",
2869 QCBOR_DECODE_MODE_NORMAL,
2870 {"\xf9\x00", 2},
2871 QCBOR_ERR_HIT_END
2872 },
2873 { "single-precision with 2 byte argument",
2874 QCBOR_DECODE_MODE_NORMAL,
2875 {"\0xfa\x00\x00", 3},
2876 QCBOR_ERR_HIT_END
2877 },
2878 { "double-precision with 3 byte argument",
2879 QCBOR_DECODE_MODE_NORMAL,
2880 {"\xfb\x00\x00\x00", 4},
2881 QCBOR_ERR_HIT_END
2882 },
Laurence Lundblade2f467f92020-10-09 17:50:11 -07002883
Laurence Lundblade37286c02022-09-03 10:05:02 -07002884#ifndef QCBOR_DISABLE_TAGS
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002885 { "Tag with no content",
2886 QCBOR_DECODE_MODE_NORMAL,
2887 {"\xc0", 1},
2888 QCBOR_ERR_HIT_END
2889 },
Laurence Lundblade37286c02022-09-03 10:05:02 -07002890#else /* QCBOR_DISABLE_TAGS */
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002891 { "Tag with no content",
2892 QCBOR_DECODE_MODE_NORMAL,
2893 {"\xc0", 1},
2894 QCBOR_ERR_TAGS_DISABLED
2895 },
Laurence Lundblade37286c02022-09-03 10:05:02 -07002896#endif /* QCBOR_DISABLE_TAGS */
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002897
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002898 /* Breaks must not occur in definite length arrays and maps */
2899 { "Array of length 1 with sole member replaced by a break",
2900 QCBOR_DECODE_MODE_NORMAL,
2901 {"\x81\xff", 2},
2902 QCBOR_ERR_BAD_BREAK
2903 },
2904 { "Array of length 2 with 2nd member replaced by a break",
2905 QCBOR_DECODE_MODE_NORMAL,
2906 {"\x82\x00\xff", 3},
2907 QCBOR_ERR_BAD_BREAK
2908 },
2909 { "Map of length 1 with sole member label replaced by a break",
2910 QCBOR_DECODE_MODE_NORMAL,
2911 {"\xa1\xff", 2},
2912 QCBOR_ERR_BAD_BREAK
2913 },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002914
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002915 /* Map of length 1 with sole member label replaced by break */
2916 { "Alternate representation that some decoders handle differently",
2917 QCBOR_DECODE_MODE_NORMAL,
2918 {"\xa1\xff\x00", 3},
2919 QCBOR_ERR_BAD_BREAK
2920 },
2921 { "Array of length 1 with 2nd member value replaced by a break",
2922 QCBOR_DECODE_MODE_NORMAL,
2923 {"\xa1\x00\xff", 3},
2924 QCBOR_ERR_BAD_BREAK
2925 },
Laurence Lundbladeec290b82024-06-10 11:10:54 -07002926 { "Map of length 2 with 2nd entry label replaced by a break",
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002927 QCBOR_DECODE_MODE_NORMAL,
Laurence Lundbladeec290b82024-06-10 11:10:54 -07002928 {"\xa2\x00\x00\xff\x00", 5},
2929 QCBOR_ERR_BAD_BREAK
2930 },
2931 { "Map of length 2 with 2nd entry value replaced by a break",
2932 QCBOR_DECODE_MODE_NORMAL,
2933 {"\xa2\x00\x00\x01\xff", 5},
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002934 QCBOR_ERR_BAD_BREAK
2935 },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002936
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002937 /* Breaks must not occur on their own out of an indefinite length data item */
2938 { "A bare break is not well formed",
2939 QCBOR_DECODE_MODE_NORMAL,
2940 {"\xff", 1},
2941 QCBOR_ERR_BAD_BREAK
2942 },
2943 { "A bare break after a zero length definite length array",
2944 QCBOR_DECODE_MODE_NORMAL,
2945 {"\x80\xff", 2},
2946 QCBOR_ERR_BAD_BREAK
2947 },
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08002948#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002949 { "A bare break after a zero length indefinite length map",
2950 QCBOR_DECODE_MODE_NORMAL,
2951 {"\x9f\xff\xff", 3},
2952 QCBOR_ERR_BAD_BREAK
2953 },
2954 { "A break inside a definite length array inside an indefenite length array",
2955 QCBOR_DECODE_MODE_NORMAL,
2956 {"\x9f\x81\xff", 3},
2957 QCBOR_ERR_BAD_BREAK
2958 },
2959 { "Complicated mixed nesting with break outside indefinite length array",
2960 QCBOR_DECODE_MODE_NORMAL,
2961 {"\x9f\x82\x9f\x81\x9f\x9f\xff\xff\xff\xff", 10},
2962 QCBOR_ERR_BAD_BREAK },
2963#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
2964
2965 /* Forbidden two byte encodings of simple types */
2966 { "Must use 0xe0 instead",
2967 QCBOR_DECODE_MODE_NORMAL,
2968 {"\xf8\x00", 2},
2969 QCBOR_ERR_BAD_TYPE_7
2970 },
2971 { "Should use 0xe1 instead",
2972 QCBOR_DECODE_MODE_NORMAL,
2973 {"\xf8\x01", 2},
2974 QCBOR_ERR_BAD_TYPE_7
2975 },
2976 { "Should use 0xe2 instead",
2977 QCBOR_DECODE_MODE_NORMAL,
2978 {"\xf8\x02", 2},
2979 QCBOR_ERR_BAD_TYPE_7
2980 }, { "Should use 0xe3 instead",
2981 QCBOR_DECODE_MODE_NORMAL,
2982 {"\xf8\x03", 2},
2983 QCBOR_ERR_BAD_TYPE_7
2984 },
2985 { "Should use 0xe4 instead",
2986 QCBOR_DECODE_MODE_NORMAL,
2987 {"\xf8\x04", 2},
2988 QCBOR_ERR_BAD_TYPE_7
2989 },
2990 { "Should use 0xe5 instead",
2991 QCBOR_DECODE_MODE_NORMAL,
2992 {"\xf8\x05", 2},
2993 QCBOR_ERR_BAD_TYPE_7
2994 },
2995 { "Should use 0xe6 instead",
2996 QCBOR_DECODE_MODE_NORMAL,
2997 {"\xf8\x06", 2},
2998 QCBOR_ERR_BAD_TYPE_7
2999 },
3000 { "Should use 0xe7 instead",
3001 QCBOR_DECODE_MODE_NORMAL,
3002 {"\xf8\x07", 2},
3003 QCBOR_ERR_BAD_TYPE_7
3004 },
3005 { "Should use 0xe8 instead",
3006 QCBOR_DECODE_MODE_NORMAL,
3007 {"\xf8\x08", 2},
3008 QCBOR_ERR_BAD_TYPE_7
3009 },
3010 { "Should use 0xe9 instead",
3011 QCBOR_DECODE_MODE_NORMAL,
3012 {"\xf8\x09", 2},
3013 QCBOR_ERR_BAD_TYPE_7
3014 },
3015 { "Should use 0xea instead",
3016 QCBOR_DECODE_MODE_NORMAL,
3017 {"\xf8\x0a", 2},
3018 QCBOR_ERR_BAD_TYPE_7
3019 },
3020 { "Should use 0xeb instead",
3021 QCBOR_DECODE_MODE_NORMAL,
3022 {"\xf8\x0b", 2},
3023 QCBOR_ERR_BAD_TYPE_7
3024 },
3025 { "Should use 0xec instead",
3026 QCBOR_DECODE_MODE_NORMAL,
3027 {"\xf8\x0c", 2},
3028 QCBOR_ERR_BAD_TYPE_7
3029 },
3030 { "Should use 0xed instead",
3031 QCBOR_DECODE_MODE_NORMAL,
3032 {"\xf8\x0d", 2},
3033 QCBOR_ERR_BAD_TYPE_7
3034 },
3035 { "Should use 0xee instead",
3036 QCBOR_DECODE_MODE_NORMAL,
3037 {"\xf8\x0e", 2},
3038 QCBOR_ERR_BAD_TYPE_7
3039 },
3040 { "Should use 0xef instead",
3041 QCBOR_DECODE_MODE_NORMAL,
3042 {"\xf8\x0f", 2},
3043 QCBOR_ERR_BAD_TYPE_7
3044 },
3045 { "Should use 0xf0 instead",
3046 QCBOR_DECODE_MODE_NORMAL,
3047 {"\xf8\x10", 2},
3048 QCBOR_ERR_BAD_TYPE_7
3049 },
3050 { "Should use 0xf1 instead",
3051 QCBOR_DECODE_MODE_NORMAL,
3052 {"\xf8\x11", 2},
3053 QCBOR_ERR_BAD_TYPE_7
3054 },
3055 { "Should use 0xf2 instead",
3056 QCBOR_DECODE_MODE_NORMAL,
3057 {"\xf8\x12", 2},
3058 QCBOR_ERR_BAD_TYPE_7
3059 },
3060 { "Should use 0xf3 instead",
3061 QCBOR_DECODE_MODE_NORMAL,
3062 {"\xf8\x13", 2},
3063 QCBOR_ERR_BAD_TYPE_7
3064 },
3065 { "Should use 0xf4 instead",
3066 QCBOR_DECODE_MODE_NORMAL,
3067 {"\xf8\x14", 2},
3068 QCBOR_ERR_BAD_TYPE_7
3069 },
3070 { "Should use 0xf5 instead",
3071 QCBOR_DECODE_MODE_NORMAL,
3072 {"\xf8\x15", 2},
3073 QCBOR_ERR_BAD_TYPE_7
3074 },
3075 { "Should use 0xf6 instead",
3076 QCBOR_DECODE_MODE_NORMAL,
3077 {"\xf8\x16", 2},
3078 QCBOR_ERR_BAD_TYPE_7
3079 },
3080 { "Should use 0xef7 instead",
3081 QCBOR_DECODE_MODE_NORMAL,
3082 {"\xf8\x17", 2},
3083 QCBOR_ERR_BAD_TYPE_7
3084 },
3085 { "Should use 0xef8 instead",
3086 QCBOR_DECODE_MODE_NORMAL,
3087 {"\xf8\x18", 2},
3088 QCBOR_ERR_BAD_TYPE_7
3089 },
3090 { "Reserved",
3091 QCBOR_DECODE_MODE_NORMAL,
3092 {"\xf8\x18", 2},
3093 QCBOR_ERR_BAD_TYPE_7
3094 },
3095
3096 /* Maps must have an even number of data items (key & value) */
3097 { "Map with 1 item when it should have 2",
3098 QCBOR_DECODE_MODE_NORMAL,
3099 {"\xa1\x00", 2},
3100 QCBOR_ERR_HIT_END
3101 },
3102 { "Map with 3 item when it should have 4",
3103 QCBOR_DECODE_MODE_NORMAL,
3104 {"\xa2\x00\x00\x00", 2},
3105 QCBOR_ERR_HIT_END
3106 },
3107#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
3108 { "Map with 1 item when it should have 2",
3109 QCBOR_DECODE_MODE_NORMAL,
3110 {"\xbf\x00\xff", 3},
3111 QCBOR_ERR_BAD_BREAK
3112 },
3113 { "Map with 3 item when it should have 4",
3114 QCBOR_DECODE_MODE_NORMAL,
3115 {"\xbf\x00\x00\x00\xff", 5},
3116 QCBOR_ERR_BAD_BREAK
3117 },
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08003118#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07003119
3120
Laurence Lundblade37286c02022-09-03 10:05:02 -07003121#ifndef QCBOR_DISABLE_TAGS
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08003122 /* In addition to not-well-formed, some invalid CBOR */
3123 { "Text-based date, with an integer",
3124 QCBOR_DECODE_MODE_NORMAL,
3125 {"\xc0\x00", 2},
3126 QCBOR_ERR_BAD_OPT_TAG
3127 },
3128 { "Epoch date, with an byte string",
3129 QCBOR_DECODE_MODE_NORMAL,
3130 {"\xc1\x41\x33", 3},
3131 QCBOR_ERR_BAD_OPT_TAG
3132 },
3133 { "tagged as both epoch and string dates",
3134 QCBOR_DECODE_MODE_NORMAL,
3135 {"\xc1\xc0\x00", 3},
3136 QCBOR_ERR_BAD_OPT_TAG
3137 },
3138 { "big num tagged an int, not a byte string",
3139 QCBOR_DECODE_MODE_NORMAL,
3140 {"\xc2\x00", 2},
3141 QCBOR_ERR_BAD_OPT_TAG
3142 },
Laurence Lundblade37286c02022-09-03 10:05:02 -07003143#else /* QCBOR_DISABLE_TAGS */
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08003144 /* In addition to not-well-formed, some invalid CBOR */
3145 { "Text-based date, with an integer",
3146 QCBOR_DECODE_MODE_NORMAL,
3147 {"\xc0\x00", 2},
3148 QCBOR_ERR_TAGS_DISABLED
3149 },
3150 { "Epoch date, with an byte string",
3151 QCBOR_DECODE_MODE_NORMAL,
3152 {"\xc1\x41\x33", 3},
3153 QCBOR_ERR_TAGS_DISABLED
3154 },
3155 { "tagged as both epoch and string dates",
3156 QCBOR_DECODE_MODE_NORMAL,
3157 {"\xc1\xc0\x00", 3},
3158 QCBOR_ERR_TAGS_DISABLED
3159 },
3160 { "big num tagged an int, not a byte string",
3161 QCBOR_DECODE_MODE_NORMAL,
3162 {"\xc2\x00", 2},
3163 QCBOR_ERR_TAGS_DISABLED
3164 },
Laurence Lundblade37286c02022-09-03 10:05:02 -07003165#endif /* QCBOR_DISABLE_TAGS */
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003166};
3167
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08003168
3169
3170int32_t
3171DecodeFailureTests(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003172{
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003173 int32_t nResult;
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07003174
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08003175 nResult = ProcessDecodeFailures(Failures ,C_ARRAY_COUNT(Failures, struct DecodeFailTestInput));
Laurence Lundblade59289e52019-12-30 13:44:37 -08003176 if(nResult) {
3177 return nResult;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003178 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003179
Laurence Lundblade3a6042e2019-06-28 19:58:04 -07003180 // Corrupt the UsefulInputBuf and see that
3181 // it reflected correctly for CBOR decoding
Laurence Lundbladee6f15112020-07-23 18:44:16 -07003182 QCBORDecodeContext DCtx;
3183 QCBORItem Item;
3184 QCBORError uQCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003185
Laurence Lundbladee6f15112020-07-23 18:44:16 -07003186 QCBORDecode_Init(&DCtx,
3187 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSimpleValues),
3188 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003189
Laurence Lundbladee6f15112020-07-23 18:44:16 -07003190 if((uQCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
3191 return (int32_t)uQCBORError;
3192 }
3193 if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.val.uCount != 10) {
3194 // This wasn't supposed to happen
3195 return -1;
3196 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003197
Laurence Lundbladee6f15112020-07-23 18:44:16 -07003198 DCtx.InBuf.magic = 0; // Reach in and corrupt the UsefulInputBuf
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003199
Laurence Lundbladee6f15112020-07-23 18:44:16 -07003200 uQCBORError = QCBORDecode_GetNext(&DCtx, &Item);
3201 if(uQCBORError != QCBOR_ERR_NO_MORE_ITEMS) {
3202 // Did not get back the error expected
3203 return -2;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003204 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003205
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003206
Laurence Lundblade98427e92020-09-28 21:33:23 -07003207 /*
3208 The max size of a string for QCBOR is SIZE_MAX - 4 so this
3209 tests here can be performed to see that the max length
3210 error check works correctly. See DecodeBytes(). If the max
3211 size was SIZE_MAX, it wouldn't be possible to test this.
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003212
Laurence Lundblade98427e92020-09-28 21:33:23 -07003213 This test will automatocally adapt the all CPU sizes
3214 through the use of SIZE_MAX.
3215 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003216
Laurence Lundblade8510f8c2020-12-01 11:31:16 -08003217 UsefulBuf_MAKE_STACK_UB( HeadBuf, QCBOR_HEAD_BUFFER_SIZE);
Laurence Lundblade98427e92020-09-28 21:33:23 -07003218 UsefulBufC EncodedHead;
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003219
Laurence Lundblade98427e92020-09-28 21:33:23 -07003220 // This makes a CBOR head with a text string that is very long
3221 // but doesn't fill in the bytes of the text string as that is
3222 // not needed to test this part of QCBOR.
3223 EncodedHead = QCBOREncode_EncodeHead(HeadBuf, CBOR_MAJOR_TYPE_TEXT_STRING, 0, SIZE_MAX);
3224
3225 QCBORDecode_Init(&DCtx, EncodedHead, QCBOR_DECODE_MODE_NORMAL);
3226
3227 if(QCBOR_ERR_STRING_TOO_LONG != QCBORDecode_GetNext(&DCtx, &Item)) {
3228 return -4;
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003229 }
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003230
Laurence Lundblade3a6042e2019-06-28 19:58:04 -07003231 return 0;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003232}
3233
3234
Laurence Lundbladea2e29072018-12-30 09:20:06 -08003235/* Try all 256 values of the byte at nLen including recursing for
3236 each of the values to try values at nLen+1 ... up to nLenMax
3237 */
Laurence Lundblade06350ea2020-01-27 19:32:40 -08003238static void ComprehensiveInputRecurser(uint8_t *pBuf, size_t nLen, size_t nLenMax)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003239{
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003240 if(nLen >= nLenMax) {
3241 return;
3242 }
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08003243
Laurence Lundbladea2e29072018-12-30 09:20:06 -08003244 for(int inputByte = 0; inputByte < 256; inputByte++) {
3245 // Set up the input
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003246 pBuf[nLen] = (uint8_t)inputByte;
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08003247 const UsefulBufC Input = {pBuf, nLen+1};
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08003248
Laurence Lundbladea2e29072018-12-30 09:20:06 -08003249 // Get ready to parse
3250 QCBORDecodeContext DCtx;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003251 QCBORDecode_Init(&DCtx, Input, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003252
Laurence Lundbladea2e29072018-12-30 09:20:06 -08003253 // Parse by getting the next item until an error occurs
3254 // Just about every possible decoder error can occur here
3255 // The goal of this test is not to check for the correct
3256 // error since that is not really possible. It is to
3257 // see that there is no crash on hostile input.
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003258 while(1) {
Laurence Lundbladea2e29072018-12-30 09:20:06 -08003259 QCBORItem Item;
3260 QCBORError nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003261 if(nCBORError != QCBOR_SUCCESS) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003262 break;
3263 }
3264 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003265
Laurence Lundbladea2e29072018-12-30 09:20:06 -08003266 ComprehensiveInputRecurser(pBuf, nLen+1, nLenMax);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003267 }
3268}
3269
3270
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03003271int32_t ComprehensiveInputTest(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003272{
Laurence Lundbladea2e29072018-12-30 09:20:06 -08003273 // Size 2 tests 64K inputs and runs quickly
3274 uint8_t pBuf[2];
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003275
Laurence Lundbladea2e29072018-12-30 09:20:06 -08003276 ComprehensiveInputRecurser(pBuf, 0, sizeof(pBuf));
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003277
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003278 return 0;
3279}
3280
Laurence Lundbladea2e29072018-12-30 09:20:06 -08003281
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03003282int32_t BigComprehensiveInputTest(void)
Laurence Lundbladea2e29072018-12-30 09:20:06 -08003283{
3284 // size 3 tests 16 million inputs and runs OK
3285 // in seconds on fast machines. Size 4 takes
3286 // 10+ minutes and 5 half a day on fast
3287 // machines. This test is kept separate from
3288 // the others so as to no slow down the use
3289 // of them as a very frequent regression.
3290 uint8_t pBuf[3]; //
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08003291
Laurence Lundbladea2e29072018-12-30 09:20:06 -08003292 ComprehensiveInputRecurser(pBuf, 0, sizeof(pBuf));
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08003293
Laurence Lundbladea2e29072018-12-30 09:20:06 -08003294 return 0;
3295}
3296
3297
Laurence Lundbladecc7da412020-12-27 00:09:07 -08003298static const uint8_t spDateTestInput[] = {
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003299 /* 1. The valid date string "1985-04-12" */
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003300 0xc0, // tag for string date
3301 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003302
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003303 /* 2. An invalid date string due to wrong tag content type */
Laurence Lundbladec7114722020-08-13 05:11:40 -07003304 0xc0, // tag for string date
3305 0x00, // Wrong type for a string date
3306
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003307 /* 3. A valid epoch date, 1400000000; Tue, 13 May 2014 16:53:20 GMT */
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003308 0xc1, // tag for epoch date
3309 0x1a, 0x53, 0x72, 0x4E, 0x00, // Epoch date 1400000000; Tue, 13 May 2014 16:53:20 GMT
3310
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003311 /* 4. An invalid epoch date due to wrong tag content type */
Laurence Lundbladec7114722020-08-13 05:11:40 -07003312 0xc1,
3313 0x62, 'h', 'i', // wrong type tagged
3314
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003315 /* 5. Valid epoch date tag as content for a two other nested tags */
Laurence Lundblade99615302020-11-29 11:19:47 -08003316 // CBOR_TAG_ENC_AS_B64
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003317 0xcf, 0xd8, 0x16, 0xc1, // Epoch date with extra tags
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003318 0x1a, 0x53, 0x72, 0x4E, 0x01,
3319
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003320 /* 6. Epoch date with value to large to fit into int64 */
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003321 0xc1, // tag for epoch date
3322 0x1b, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, // Too large integer
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003323
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003324 /* 7. Epoch date with single-precision value of 1.1. */
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003325 0xc1, // tag for epoch date
Laurence Lundblade3ed0bca2020-07-14 22:50:10 -07003326 0xfa, 0x3f, 0x8c, 0xcc, 0xcd, // single with value 1.1
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003327
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003328 /* 8. Epoch date with too-large single precision float */
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003329 0xc1, // tag for epoch date
Laurence Lundbladea1ad8782019-11-08 00:12:11 -08003330 0xfa, 0x7f, 0x7f, 0xff, 0xff, // 3.4028234663852886e+38 too large
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003331
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003332 /* 9. Epoch date with slightly too-large double precision value */
Laurence Lundbladea1ad8782019-11-08 00:12:11 -08003333 0xc1, // tag for epoch date
3334 0xfb, 0x43, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 9223372036854775808.000000 just barely too large
3335 //0xfa, 0x7f, 0x7f, 0xff, 0xff // 3.4028234663852886e+38 too large
3336
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003337 /* 10. Epoch date with largest supported double precision value */
Laurence Lundbladea1ad8782019-11-08 00:12:11 -08003338 0xc1, // tag for epoch date
Laurence Lundbladec7114722020-08-13 05:11:40 -07003339 0xfb, 0x43, 0xdf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, // 9223372036854773760 largest supported
3340
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003341 /* 11. Epoch date with single-precision NaN */
Laurence Lundbladec7114722020-08-13 05:11:40 -07003342 0xc1, // tag for epoch date
3343 0xfa, 0x7f, 0xc0, 0x00, 0x00, // Single-precision NaN
3344
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003345 /* 12. Epoch date with double precision plus infinity */
Laurence Lundbladec7114722020-08-13 05:11:40 -07003346 0xc1,
3347 0xfb, 0x7f, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // +infinity
3348
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003349 /* 13. Epoch date with half-precision negative infinity */
Laurence Lundbladec7114722020-08-13 05:11:40 -07003350 0xc1, // tag for epoch date
3351 0xf9, 0xfc, 0x00, // -Infinity
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003352};
3353
3354
Laurence Lundbladec7114722020-08-13 05:11:40 -07003355
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003356// have to check float expected only to within an epsilon
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07003357#ifndef QCBOR_DISABLE_FLOAT_HW_USE
Laurence Lundblade02fcf312020-07-17 02:49:46 -07003358static int CHECK_EXPECTED_DOUBLE(double val, double expected) {
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003359
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003360 double diff = val - expected;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003361
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003362 diff = fabs(diff);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003363
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003364 return diff > 0.0000001;
3365}
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07003366#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003367
3368
Laurence Lundblade37286c02022-09-03 10:05:02 -07003369/* Test date decoding using GetNext() */
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03003370int32_t DateParseTest(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003371{
3372 QCBORDecodeContext DCtx;
Laurence Lundbladec7114722020-08-13 05:11:40 -07003373 QCBORItem Item;
3374 QCBORError uError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003375
Laurence Lundbladeee851742020-01-08 08:37:05 -08003376 QCBORDecode_Init(&DCtx,
3377 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spDateTestInput),
3378 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003379
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003380 /* 1. The valid date string "1985-04-12" */
Laurence Lundbladec7114722020-08-13 05:11:40 -07003381 if((uError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003382 return -1;
Laurence Lundbladec7114722020-08-13 05:11:40 -07003383 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003384 if(Item.uDataType != QCBOR_TYPE_DATE_STRING ||
Laurence Lundbladeba587682024-02-07 16:46:43 -08003385 UsefulBufCompareToSZ(Item.val.string, "1985-04-12")){
Laurence Lundblade67bd5512018-11-02 21:44:06 +07003386 return -2;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003387 }
3388
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003389 /* 2. An invalid date string due to wrong tag content type */
Laurence Lundbladec7114722020-08-13 05:11:40 -07003390 uError = QCBORDecode_GetNext(&DCtx, &Item);
3391 if(uError != QCBOR_ERR_BAD_OPT_TAG) {
Laurence Lundblade67bd5512018-11-02 21:44:06 +07003392 return -3;
Laurence Lundbladec7114722020-08-13 05:11:40 -07003393 }
3394
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003395 /* 3. A valid epoch date, 1400000000; Tue, 13 May 2014 16:53:20 GMT */
3396 uError = QCBORDecode_GetNext(&DCtx, &Item);
3397 if(uError != QCBOR_SUCCESS) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07003398 return -4;
3399 }
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003400 if(uError == QCBOR_SUCCESS) {
3401 if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH ||
3402 Item.val.epochDate.nSeconds != 1400000000
Máté Tóth-Pálef5f07a2021-09-17 19:31:37 +02003403#ifndef USEFULBUF_DISABLE_ALL_FLOAT
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003404 || Item.val.epochDate.fSecondsFraction != 0
Máté Tóth-Pálef5f07a2021-09-17 19:31:37 +02003405#endif /* USEFULBUF_DISABLE_ALL_FLOAT */
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003406 ) {
3407 return -5;
3408 }
Laurence Lundbladec7114722020-08-13 05:11:40 -07003409 }
3410
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003411 /* 4. An invalid epoch date due to wrong tag content type */
Laurence Lundbladec7114722020-08-13 05:11:40 -07003412 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_OPT_TAG) {
3413 return -6;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003414 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003415
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003416 /* 5. Valid epoch date tag as content for a two other nested tags */
Laurence Lundblade99615302020-11-29 11:19:47 -08003417 // Epoch date wrapped in an CBOR_TAG_ENC_AS_B64 and an unknown tag.
3418 // The date is decoded and the two tags are returned. This is to
3419 // make sure the wrapping of epoch date in another tag works OK.
Laurence Lundbladec7114722020-08-13 05:11:40 -07003420 if((uError = QCBORDecode_GetNext(&DCtx, &Item))) {
3421 return -7;
3422 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003423 if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH ||
3424 Item.val.epochDate.nSeconds != 1400000001 ||
Máté Tóth-Pálef5f07a2021-09-17 19:31:37 +02003425#ifndef USEFULBUF_DISABLE_ALL_FLOAT
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003426 Item.val.epochDate.fSecondsFraction != 0 ||
Máté Tóth-Pálef5f07a2021-09-17 19:31:37 +02003427#endif /* USEFULBUF_DISABLE_ALL_FLOAT */
Laurence Lundblade99615302020-11-29 11:19:47 -08003428 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_ENC_AS_B64)) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07003429 return -8;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003430 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003431
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003432 /* 6. Epoch date with value to large to fit into int64 */
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003433 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_DATE_OVERFLOW) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07003434 return -9;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003435 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003436
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003437 /* 7. Epoch date with single-precision value of 1.1. */
3438 uError = QCBORDecode_GetNext(&DCtx, &Item);
3439 if(uError != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07003440 return -10;
3441 }
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003442 if(uError == QCBOR_SUCCESS) {
3443 if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH ||
3444 Item.val.epochDate.nSeconds != 1
3445#ifndef QCBOR_DISABLE_FLOAT_HW_USE
3446 || CHECK_EXPECTED_DOUBLE(Item.val.epochDate.fSecondsFraction, 0.1)
Máté Tóth-Pálef5f07a2021-09-17 19:31:37 +02003447#endif /* USEFULBUF_DISABLE_ALL_FLOAT */
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003448 ) {
3449 return -11;
3450 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003451 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003452
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003453 /* 8. Epoch date with too-large single-precision float */
3454 uError = QCBORDecode_GetNext(&DCtx, &Item);
3455 if(uError != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_DATE_OVERFLOW)) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07003456 return -12;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003457 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003458
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003459 /* 9. Epoch date with slightly too-large double-precision value */
3460 uError = QCBORDecode_GetNext(&DCtx, &Item);
3461 if(uError != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_DATE_OVERFLOW)) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07003462 return -13;
Laurence Lundbladea1ad8782019-11-08 00:12:11 -08003463 }
3464
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003465 /* 10. Epoch date with largest supported double-precision value */
3466 uError = QCBORDecode_GetNext(&DCtx, &Item);
3467 if(uError != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)) {
3468 return -14;
3469 }
3470 if(uError == QCBOR_SUCCESS) {
3471 if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH ||
3472 Item.val.epochDate.nSeconds != 9223372036854773760
3473#ifndef QCBOR_DISABLE_FLOAT_HW_USE
3474 || Item.val.epochDate.fSecondsFraction != 0.0
3475#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
3476 ) {
3477 return -14;
3478 }
3479 }
Laurence Lundbladec7114722020-08-13 05:11:40 -07003480
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003481 /* 11. Epoch date with single-precision NaN */
3482 if(QCBORDecode_GetNext(&DCtx, &Item) != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_DATE_OVERFLOW)) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07003483 return -15;
3484 }
3485
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003486 /* 12. Epoch date with double-precision plus infinity */
3487 if(QCBORDecode_GetNext(&DCtx, &Item) != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_DATE_OVERFLOW)) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07003488 return -16;
3489 }
3490
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003491 /* 13. Epoch date with half-precision negative infinity */
3492 uError = QCBORDecode_GetNext(&DCtx, &Item);
3493 if(uError != FLOAT_ERR_CODE_NO_HALF_PREC_NO_FLOAT_HW(QCBOR_ERR_DATE_OVERFLOW)) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07003494 return -17;
3495 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003496
3497 return 0;
3498}
3499
Laurence Lundblade37286c02022-09-03 10:05:02 -07003500
Laurence Lundblade4b270642020-08-14 12:53:07 -07003501/*
3502 Test cases covered here. Some items cover more than one of these.
3503 positive integer (zero counts as a positive integer)
3504 negative integer
3505 half-precision float
3506 single-precision float
3507 double-precision float
Laurence Lundbladec7114722020-08-13 05:11:40 -07003508
Laurence Lundblade4b270642020-08-14 12:53:07 -07003509 float Overflow error
3510 Wrong type error for epoch
3511 Wrong type error for date string
3512 float disabled error
3513 half-precision disabled error
3514 -Infinity
3515 Slightly too large integer
3516 Slightly too far from zero
Laurence Lundbladec7114722020-08-13 05:11:40 -07003517
Laurence Lundblade4b270642020-08-14 12:53:07 -07003518 Get epoch by int
3519 Get string by int
3520 Get epoch by string
3521 Get string by string
3522 Fail to get epoch by wrong int label
3523 Fail to get string by wrong string label
3524 Fail to get epoch by string because it is invalid
3525 Fail to get epoch by int because it is invalid
3526
3527 Untagged values
3528 */
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08003529static const uint8_t spSpiffyDateTestInput[] = {
Laurence Lundblade37286c02022-09-03 10:05:02 -07003530 0x87, // array of 7 items
3531
3532 0xa6, // Open a map for tests involving untagged items with labels.
3533
3534 // Untagged integer 0
3535 0x08,
3536 0x00,
3537
3538 // Utagged date string with string label y
3539 0x61, 0x79,
3540 0x6a, '2','0','8','5','-','0','4','-','1','2', // Untagged date string
3541
3542 // Untagged single-precision float with value 3.14 with string label x
3543 0x61, 0x78,
3544 0xFA, 0x40, 0x48, 0xF5, 0xC3,
3545
3546 // Untagged half-precision float with value -2
3547 0x09,
3548 0xF9, 0xC0, 0x00,
3549
3550 /* Untagged date-only date string */
3551 0x18, 0x63,
3552 0x6A, 0x31, 0x39, 0x38, 0x35, 0x2D, 0x30, 0x34, 0x2D, 0x31, 0x32, /* "1985-04-12" */
3553
3554 /* Untagged days-count epoch date */
3555 0x11,
3556 0x19, 0x0F, 0x9A, /* 3994 */
3557
3558 // End of map, back to array
3559
3560 0xa7, // Open map of tagged items with labels
3561
3562 0x00,
3563 0xc0, // tag for string date
3564 0x6a, '1','9','8','5','-','0','4','-','1','2', // Tagged date string
3565
3566
3567 0x01,
3568 0xda, 0x03, 0x03, 0x03, 0x03, // An additional tag
3569 0xc1, // tag for epoch date
3570 0x1a, 0x53, 0x72, 0x4E, 0x00, // Epoch date 1400000000; Tue, 13 May 2014 16:53:20 GMT
3571
3572 0x05,
3573 0xc1,
3574 0xfb, 0xc3, 0xdf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, // -9223372036854773760 largest negative
3575
3576
3577 0x07,
3578 0xc1, // tag for epoch date
3579 0xfb, 0x43, 0xdf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, // 9223372036854773760 largest supported
3580
3581 /* Tagged days-count epoch date */
3582 0x63, 0x53, 0x44, 0x45,
3583 0xD8, 0x64, /* tag(100) */
3584 0x39, 0x29, 0xB3, /* -10676 */
3585
3586 // Untagged -1000 with label z
3587 0x61, 0x7a,
3588 0xda, 0x01, 0x01, 0x01, 0x01, // An additional tag
3589 0x39, 0x03, 0xe7,
3590
3591 /* Tagged date-only date string */
3592 0x63, 0x53, 0x44, 0x53,
3593 0xD9, 0x03, 0xEC,
3594 0x6A, 0x31, 0x39, 0x38, 0x35, 0x2D, 0x30, 0x34, 0x2D, 0x31, 0x32, /* "1985-04-12" */
3595
3596 // End of map of tagged items
Laurence Lundblade4b270642020-08-14 12:53:07 -07003597
3598 0xc1,
3599 0xfb, 0xc3, 0xdf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, // -9.2233720368547748E+18, too negative
3600
Laurence Lundbladec7114722020-08-13 05:11:40 -07003601 0xc1, // tag for epoch date
Laurence Lundblade4b270642020-08-14 12:53:07 -07003602 0x1b, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, // Too-large integer
3603
3604 0xc1, // tag for epoch date
3605 0xf9, 0xfc, 0x00, // Half-precision -Infinity
3606
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07003607 // These two at the end because they are unrecoverable errors
3608 0xc1, // tag for epoch date
3609 0x80, // Erroneous empty array as content for date
3610
3611 0xc0, // tag for string date
3612 0xa0 // Erroneous empty map as content for date
Laurence Lundbladec7114722020-08-13 05:11:40 -07003613};
3614
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03003615int32_t SpiffyDateDecodeTest(void)
Laurence Lundbladec7114722020-08-13 05:11:40 -07003616{
3617 QCBORDecodeContext DC;
Laurence Lundblade4b270642020-08-14 12:53:07 -07003618 QCBORError uError;
Laurence Lundblade37286c02022-09-03 10:05:02 -07003619 int64_t nEpochDate3, nEpochDate5,
3620 nEpochDate4, nEpochDate6,
3621 nEpochDays2;
3622 UsefulBufC StringDate1, StringDate2, StringDays2;
Laurence Lundbladec7114722020-08-13 05:11:40 -07003623
3624 QCBORDecode_Init(&DC,
Laurence Lundblade4b270642020-08-14 12:53:07 -07003625 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSpiffyDateTestInput),
Laurence Lundbladec7114722020-08-13 05:11:40 -07003626 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade37286c02022-09-03 10:05:02 -07003627
3628 /* Items are in an array or map to test look up by label and other
3629 * that might not occur in isolated items. But it does make the
3630 * test a bit messy. */
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07003631 QCBORDecode_EnterArray(&DC, NULL);
Laurence Lundbladec7114722020-08-13 05:11:40 -07003632
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07003633 QCBORDecode_EnterMap(&DC, NULL);
Laurence Lundblade4b270642020-08-14 12:53:07 -07003634
Laurence Lundblade4b270642020-08-14 12:53:07 -07003635 // A single-precision date
Laurence Lundblade9b334962020-08-27 10:55:53 -07003636 QCBORDecode_GetEpochDateInMapSZ(&DC, "x", QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
3637 &nEpochDate5);
Laurence Lundblade4b270642020-08-14 12:53:07 -07003638 uError = QCBORDecode_GetAndResetError(&DC);
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003639 if(uError != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07003640 return 104;
3641 }
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003642 if(uError == QCBOR_SUCCESS) {
3643 if(nEpochDate5 != 3) {
3644 return 103;
3645 }
3646 }
Laurence Lundbladec7114722020-08-13 05:11:40 -07003647
Laurence Lundblade9b334962020-08-27 10:55:53 -07003648 // A half-precision date with value -2 FFF
3649 QCBORDecode_GetEpochDateInMapN(&DC, 9, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
3650 &nEpochDate4);
Laurence Lundblade4b270642020-08-14 12:53:07 -07003651 uError = QCBORDecode_GetAndResetError(&DC);
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003652 if(uError != FLOAT_ERR_CODE_NO_HALF_PREC_NO_FLOAT_HW(QCBOR_SUCCESS)) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07003653 return 106;
3654 }
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003655 if(uError == QCBOR_SUCCESS) {
3656 if(nEpochDate4 != -2) {
3657 return 105;
3658 }
3659 }
Laurence Lundblade4b270642020-08-14 12:53:07 -07003660
3661 // Fail to get an epoch date by string label
Laurence Lundblade9b334962020-08-27 10:55:53 -07003662 QCBORDecode_GetEpochDateInMapSZ(&DC, "no-label",
3663 QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
3664 &nEpochDate6);
Laurence Lundblade4b270642020-08-14 12:53:07 -07003665 uError = QCBORDecode_GetAndResetError(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07003666 if(uError != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07003667 return 107;
3668 }
3669
3670 // Fail to get an epoch date by integer label
Laurence Lundblade9b334962020-08-27 10:55:53 -07003671 QCBORDecode_GetEpochDateInMapN(&DC, 99999, QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
3672 &nEpochDate6);
Laurence Lundblade4b270642020-08-14 12:53:07 -07003673 uError = QCBORDecode_GetAndResetError(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07003674 if(uError != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07003675 return 108;
3676 }
3677
3678 // Fail to get a string date by string label
Laurence Lundblade9b334962020-08-27 10:55:53 -07003679 QCBORDecode_GetDateStringInMapSZ(&DC, "no-label",
3680 QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
3681 &StringDate1);
Laurence Lundblade4b270642020-08-14 12:53:07 -07003682 uError = QCBORDecode_GetAndResetError(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07003683 if(uError != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07003684 return 109;
3685 }
3686
3687 // Fail to get a string date by integer label
Laurence Lundblade9b334962020-08-27 10:55:53 -07003688 QCBORDecode_GetDateStringInMapN(&DC, 99999, QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
3689 &StringDate1);
Laurence Lundblade4b270642020-08-14 12:53:07 -07003690 uError = QCBORDecode_GetAndResetError(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07003691 if(uError != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07003692 return 110;
3693 }
3694
3695 // The rest of these succeed even if float features are disabled
Laurence Lundbladea9489f82020-09-12 13:50:56 -07003696
Laurence Lundblade37286c02022-09-03 10:05:02 -07003697
3698 // Untagged integer 0
3699 QCBORDecode_GetEpochDateInMapN(&DC, 8, QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
3700 &nEpochDate3);
3701 // Untagged date string
3702 QCBORDecode_GetDateStringInMapSZ(&DC, "y", QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
3703 &StringDate2);
3704
3705 QCBORDecode_GetDaysStringInMapN(&DC, 99, QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
3706 &StringDays2);
3707
3708 QCBORDecode_GetEpochDaysInMapN(&DC, 17, QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
3709 &nEpochDays2);
3710
3711 QCBORDecode_ExitMap(&DC);
3712 if(QCBORDecode_GetError(&DC) != QCBOR_SUCCESS) {
3713 return 3001;
3714 }
3715
3716 // The map of tagged items
3717 QCBORDecode_EnterMap(&DC, NULL);
3718
3719#ifndef QCBOR_DISABLE_TAGS
3720 int64_t nEpochDate2,
3721 nEpochDateFail,
3722 nEpochDate1400000000, nEpochDays1;
3723 UsefulBufC StringDays1;
3724 uint64_t uTag1, uTag2;
3725
3726 // Tagged date string
3727 QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
3728 &StringDate1);
3729
Laurence Lundblade4b270642020-08-14 12:53:07 -07003730 // Epoch date 1400000000; Tue, 13 May 2014 16:53:20 GMT
Laurence Lundblade9b334962020-08-27 10:55:53 -07003731 QCBORDecode_GetEpochDateInMapN(&DC,
3732 1,
3733 QCBOR_TAG_REQUIREMENT_TAG |
3734 QCBOR_TAG_REQUIREMENT_ALLOW_ADDITIONAL_TAGS,
3735 &nEpochDate1400000000);
3736 uTag1 = QCBORDecode_GetNthTagOfLast(&DC, 0);
Laurence Lundblade37286c02022-09-03 10:05:02 -07003737
3738 // Get largest negative double precision epoch date allowed
3739 QCBORDecode_GetEpochDateInMapN(&DC,
3740 5,
3741 QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG |
3742 QCBOR_TAG_REQUIREMENT_ALLOW_ADDITIONAL_TAGS,
3743 &nEpochDate2);
3744 uError = QCBORDecode_GetAndResetError(&DC);
3745 if(uError != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)) {
3746 return 102;
3747 }
3748 if(uError == QCBOR_SUCCESS) {
3749 if(nEpochDate2 != -9223372036854773760LL) {
3750 return 101;
3751 }
3752 }
3753
Laurence Lundblade4b270642020-08-14 12:53:07 -07003754 // Untagged -1000 with label z
Laurence Lundblade9b334962020-08-27 10:55:53 -07003755 QCBORDecode_GetEpochDateInMapSZ(&DC,
3756 "z",
3757 QCBOR_TAG_REQUIREMENT_NOT_A_TAG |
3758 QCBOR_TAG_REQUIREMENT_ALLOW_ADDITIONAL_TAGS,
3759 &nEpochDate6);
3760 uTag2 = QCBORDecode_GetNthTagOfLast(&DC, 0);
Laurence Lundblade4b270642020-08-14 12:53:07 -07003761
Laurence Lundblade37286c02022-09-03 10:05:02 -07003762
3763 // Get largest double precision epoch date allowed
3764 QCBORDecode_GetEpochDateInMapN(&DC, 7, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
3765 &nEpochDate2);
3766 uError = QCBORDecode_GetAndResetError(&DC);
3767 if(uError != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)) {
3768 return 112;
3769 }
3770 if(uError == QCBOR_SUCCESS) {
3771 if(nEpochDate2 != 9223372036854773760ULL) {
3772 return 111;
3773 }
3774 }
3775
Laurence Lundblade46d63e92021-05-13 11:37:10 -07003776 /* The days format is much simpler than the date format
3777 * because it can't be a floating point value. The test
3778 * of the spiffy decode functions sufficiently covers
3779 * the test of the non-spiffy decode days date decoding.
3780 * There is no full fan out of the error conditions
3781 * and decode options as that is implemented by code
3782 * that is tested well by the date testing above.
3783 */
3784 QCBORDecode_GetDaysStringInMapSZ(&DC, "SDS", QCBOR_TAG_REQUIREMENT_TAG,
3785 &StringDays1);
3786
Laurence Lundblade46d63e92021-05-13 11:37:10 -07003787 QCBORDecode_GetEpochDaysInMapSZ(&DC, "SDE", QCBOR_TAG_REQUIREMENT_TAG,
3788 &nEpochDays1);
3789
Laurence Lundblade4b270642020-08-14 12:53:07 -07003790 QCBORDecode_ExitMap(&DC);
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07003791 if(QCBORDecode_GetError(&DC) != QCBOR_SUCCESS) {
3792 return 3001;
3793 }
3794
Laurence Lundblade37286c02022-09-03 10:05:02 -07003795 // Too-negative float, -9.2233720368547748E+18
3796 QCBORDecode_GetEpochDate(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nEpochDateFail);
3797 uError = QCBORDecode_GetAndResetError(&DC);
3798 if(uError != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_DATE_OVERFLOW)) {
3799 return 1111;
3800 }
3801
3802 // Too-large integer
3803 QCBORDecode_GetEpochDate(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nEpochDateFail);
3804 uError = QCBORDecode_GetAndResetError(&DC);
3805 if(uError != QCBOR_ERR_DATE_OVERFLOW) {
3806 return 1;
3807 }
3808
3809 // Half-precision minus infinity
3810 QCBORDecode_GetEpochDate(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nEpochDateFail);
3811 uError = QCBORDecode_GetAndResetError(&DC);
3812 if(uError != FLOAT_ERR_CODE_NO_HALF_PREC_NO_FLOAT_HW(QCBOR_ERR_DATE_OVERFLOW)) {
3813 return 2;
3814 }
3815
3816
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07003817 // Bad content for epoch date
3818 QCBORDecode_GetEpochDate(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nEpochDateFail);
3819 uError = QCBORDecode_GetAndResetError(&DC);
3820 if(uError != QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT) {
3821 return 3;
3822 }
3823
3824 // Bad content for string date
3825 QCBORDecode_GetDateString(&DC, QCBOR_TAG_REQUIREMENT_TAG, &StringDate1);
3826 uError = QCBORDecode_GetAndResetError(&DC);
3827 if(uError != QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT) {
3828 return 4;
3829 }
3830
Laurence Lundblade4b270642020-08-14 12:53:07 -07003831 QCBORDecode_ExitArray(&DC);
3832 uError = QCBORDecode_Finish(&DC);
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07003833 if(uError != QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT) {
Laurence Lundblade9b334962020-08-27 10:55:53 -07003834 return 1000 + (int32_t)uError;
Laurence Lundblade4b270642020-08-14 12:53:07 -07003835 }
Laurence Lundblade37286c02022-09-03 10:05:02 -07003836#else /* QCBOR_DISABLE_TAGS */
3837 QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
3838 &StringDate1);
3839 uError = QCBORDecode_GetAndResetError(&DC);
3840 if(uError != QCBOR_ERR_TAGS_DISABLED) {
3841 return 4;
3842 }
3843#endif /* QCBOR_DISABLE_TAGS */
3844
3845
3846#ifndef QCBOR_DISABLE_TAGS
Laurence Lundblade4b270642020-08-14 12:53:07 -07003847
Laurence Lundblade9b334962020-08-27 10:55:53 -07003848 if(nEpochDate1400000000 != 1400000000) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07003849 return 200;
3850 }
3851
Laurence Lundblade9b334962020-08-27 10:55:53 -07003852 if(uTag1 != 0x03030303) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07003853 return 201;
3854 }
3855
Laurence Lundblade37286c02022-09-03 10:05:02 -07003856 if(nEpochDays1 != -10676) {
3857 return 205;
Laurence Lundblade4b270642020-08-14 12:53:07 -07003858 }
3859
Laurence Lundblade37286c02022-09-03 10:05:02 -07003860 if(UsefulBuf_Compare(StringDays1, UsefulBuf_FromSZ("1985-04-12"))) {
3861 return 207;
Laurence Lundblade4b270642020-08-14 12:53:07 -07003862 }
3863
Laurence Lundblade9b334962020-08-27 10:55:53 -07003864 if(uTag2 != 0x01010101) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07003865 return 204;
3866 }
3867
Laurence Lundblade37286c02022-09-03 10:05:02 -07003868 if(nEpochDate6 != -1000) {
3869 return 203;
Laurence Lundblade46d63e92021-05-13 11:37:10 -07003870 }
3871
Laurence Lundblade9b334962020-08-27 10:55:53 -07003872 if(UsefulBuf_Compare(StringDate1, UsefulBuf_FromSZ("1985-04-12"))) {
3873 return 205;
3874 }
3875
Laurence Lundblade37286c02022-09-03 10:05:02 -07003876#endif /* QCBOR_DISABLE_TAGS */
3877
3878 if(nEpochDate3 != 0) {
3879 return 202;
3880 }
3881
3882 if(nEpochDays2 != 3994) {
Laurence Lundblade9b334962020-08-27 10:55:53 -07003883 return 206;
3884 }
3885
Laurence Lundblade37286c02022-09-03 10:05:02 -07003886 if(UsefulBuf_Compare(StringDate2, UsefulBuf_FromSZ("2085-04-12"))) {
3887 return 206;
Laurence Lundblade46d63e92021-05-13 11:37:10 -07003888 }
3889
3890 if(UsefulBuf_Compare(StringDays2, UsefulBuf_FromSZ("1985-04-12"))) {
3891 return 208;
3892 }
3893
Laurence Lundbladec7114722020-08-13 05:11:40 -07003894 return 0;
3895}
3896
3897
Laurence Lundblade9b334962020-08-27 10:55:53 -07003898// Input for one of the tagging tests
Laurence Lundbladecc7da412020-12-27 00:09:07 -08003899static const uint8_t spTagInput[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003900 0xd9, 0xd9, 0xf7, // CBOR magic number
Laurence Lundblade9b334962020-08-27 10:55:53 -07003901 0x81, // Array of one
3902 0xd8, 0x04, // non-preferred serialization of tag 4, decimal fraction
3903 0x82, // Array of two that is the faction 1/3
3904 0x01,
3905 0x03,
3906
3907 /*
3908 More than 4 tags on an item 225(226(227(228(229([])))))
3909 */
3910 0xd8, 0xe1,
3911 0xd8, 0xe2,
3912 0xd8, 0xe3,
3913 0xd8, 0xe4,
3914 0xd8, 0xe5,
3915 0x80,
3916
3917 /* tag 10489608748473423768(
3918 2442302356(
3919 21590(
3920 240(
3921 []))))
3922 */
3923 0xdb, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98,
3924 0xda, 0x91, 0x92, 0x93, 0x94,
3925 0xd9, 0x54, 0x56,
3926 0xd8, 0xf0,
3927 0x80,
3928
3929 /* tag 21590(
3930 10489608748473423768(
3931 2442302357(
3932 65534(
3933 []))))
3934 */
3935 0xdb, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x54, 0x56,
3936 0xdb, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98,
3937 0xda, 0x91, 0x92, 0x93, 0x95,
3938 0xd9, 0xff, 0xfe,
3939 0x80,
3940
3941 /* Make sure to blow past the limit of tags that must be mapped.
3942 works in conjuntion with entries above.
3943 269488144(269488145(269488146(269488147([]))))
3944 */
3945 0xda, 0x10, 0x10, 0x10, 0x10,
3946 0xda, 0x10, 0x10, 0x10, 0x11,
3947 0xda, 0x10, 0x10, 0x10, 0x12,
3948 0xda, 0x10, 0x10, 0x10, 0x13,
3949 0x80,
3950
3951 /* An invalid decimal fraction with an additional tag */
3952 0xd9, 0xff, 0xfa,
3953 0xd8, 0x02, // non-preferred serialization of tag 2, a big num
3954 0x00, // the integer 0; should be a byte string
3955};
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003956
Laurence Lundblade59289e52019-12-30 13:44:37 -08003957/*
3958 DB 9192939495969798 # tag(10489608748473423768)
Laurence Lundblade9b334962020-08-27 10:55:53 -07003959 80 # array(0)
Laurence Lundblade59289e52019-12-30 13:44:37 -08003960 */
Laurence Lundbladecc7da412020-12-27 00:09:07 -08003961static const uint8_t spEncodedLargeTag[] = {0xdb, 0x91, 0x92, 0x93, 0x94, 0x95,
Laurence Lundbladeee851742020-01-08 08:37:05 -08003962 0x96, 0x97, 0x98, 0x80};
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003963
Laurence Lundblade59289e52019-12-30 13:44:37 -08003964/*
3965DB 9192939495969798 # tag(10489608748473423768)
3966 D8 88 # tag(136)
3967 C6 # tag(6)
3968 C7 # tag(7)
3969 80 # array(0)
3970*/
Laurence Lundbladecc7da412020-12-27 00:09:07 -08003971static const uint8_t spLotsOfTags[] = {0xdb, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96,
Laurence Lundbladeee851742020-01-08 08:37:05 -08003972 0x97, 0x98, 0xd8, 0x88, 0xc6, 0xc7, 0x80};
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003973
3974/*
Laurence Lundblade9b334962020-08-27 10:55:53 -07003975 55799(55799(55799({
3976 6(7(-23)): 5859837686836516696(7({
3977 7(-20): 11({
3978 17(-18): 17(17(17("Organization"))),
3979 9(-17): 773("SSG"),
3980 -15: 16(17(6(7("Confusion")))),
3981 17(-16): 17("San Diego"),
3982 17(-14): 17("US")
3983 }),
3984 23(-19): 19({
3985 -11: 9({
3986 -9: -7
3987 }),
3988 90599561(90599561(90599561(-10))): 12(h'0102030405060708090A')
3989 })
3990 })),
3991 16(-22): 23({
3992 11(8(7(-5))): 8(-3)
3993 })
3994 })))
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003995 */
Laurence Lundbladecc7da412020-12-27 00:09:07 -08003996static const uint8_t spCSRWithTags[] = {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003997 0xd9, 0xd9, 0xf7, 0xd9, 0xd9, 0xf7, 0xd9, 0xd9, 0xf7, 0xa2,
3998 0xc6, 0xc7, 0x36,
3999 0xdb, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0xc7, 0xa2,
4000 0xda, 0x00, 0x00, 0x00, 0x07, 0x33,
4001 0xcb, 0xa5,
4002 0xd1, 0x31,
4003 0xd1, 0xd1, 0xd1, 0x6c,
4004 0x4f, 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74, 0x69, 0x6f, 0x6e,
4005 0xc9, 0x30,
4006 0xd9, 0x03, 0x05, 0x63,
4007 0x53, 0x53, 0x47,
4008 0x2e,
Laurence Lundblade9b334962020-08-27 10:55:53 -07004009 0xd0, 0xd1, 0xc6, 0xc7,
4010 0x69,
4011 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73, 0x69, 0x6f, 0x6e,
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004012 0xd1, 0x2f,
4013 0xd1, 0x69,
4014 0x53, 0x61, 0x6e, 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f,
4015 0xd1, 0x2d,
4016 0xd1, 0x62,
4017 0x55, 0x53,
4018 0xd7, 0x32,
4019 0xd3, 0xa2,
4020 0x2a,
4021 0xc9, 0xa1,
4022 0x28,
4023 0x26,
4024 0xda, 0x05, 0x66, 0x70, 0x89, 0xda, 0x05, 0x66, 0x70, 0x89, 0xda, 0x05, 0x66, 0x70, 0x89, 0x29,
4025 0xcc, 0x4a,
4026 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,0x07, 0x08, 0x09, 0x0a,
4027 0xd0, 0x35,
4028 0xd7, 0xa1,
4029 0xcb, 0xc8, 0xc7, 0x24,
4030 0xc8, 0x22};
4031
Laurence Lundblade9b334962020-08-27 10:55:53 -07004032
Laurence Lundbladecc7da412020-12-27 00:09:07 -08004033static const uint8_t spSpiffyTagInput[] = {
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08004034 0x85, // Open array
Laurence Lundblade9b334962020-08-27 10:55:53 -07004035
4036 0xc0, // tag for string date
4037 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string
4038
4039 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string
4040
4041 0x4a, '1','9','8','5','-','0','4','-','1','2', // Date string in byte string
4042
4043 0xd8, 0x23, // tag for regex
4044 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string
4045
4046 0xc0, // tag for string date
4047 0x4a, '1','9','8','5','-','0','4','-','1','2', // Date string in byte string
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07004048
4049 // This last case makes the array untraversable because it is
4050 // an uncrecoverable error. Make sure it stays last and is the only
4051 // instance so the other tests can work.
Laurence Lundblade9b334962020-08-27 10:55:53 -07004052};
4053
4054
Laurence Lundbladec5fef682020-01-25 11:38:45 -08004055static int32_t CheckCSRMaps(QCBORDecodeContext *pDC);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004056
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004057
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03004058int32_t OptTagParseTest(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004059{
4060 QCBORDecodeContext DCtx;
Laurence Lundblade9b334962020-08-27 10:55:53 -07004061 QCBORItem Item;
4062 QCBORError uError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004063
Laurence Lundbladeee851742020-01-08 08:37:05 -08004064 QCBORDecode_Init(&DCtx,
Laurence Lundblade9b334962020-08-27 10:55:53 -07004065 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTagInput),
Laurence Lundbladeee851742020-01-08 08:37:05 -08004066 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004067
Laurence Lundblade9b334962020-08-27 10:55:53 -07004068 /*
4069 This test matches the magic number tag and the fraction tag
4070 55799([...])
4071 */
4072 uError = QCBORDecode_GetNext(&DCtx, &Item);
4073 if(uError != QCBOR_SUCCESS) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004074 return -2;
4075 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004076 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004077 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC)) {
4078 return -3;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004079 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004080
Laurence Lundblade9b334962020-08-27 10:55:53 -07004081 /*
4082 4([1,3])
4083 */
4084 uError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundbladedd6e76e2021-03-10 01:54:01 -07004085#ifdef QCBOR_DISABLE_EXP_AND_MANTISSA
Laurence Lundblade9b334962020-08-27 10:55:53 -07004086 if(uError != QCBOR_SUCCESS ||
4087 Item.uDataType != QCBOR_TYPE_ARRAY ||
4088 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_DECIMAL_FRACTION) ||
4089 QCBORDecode_GetNthTag(&DCtx, &Item, 0) != CBOR_TAG_DECIMAL_FRACTION ||
4090 QCBORDecode_GetNthTag(&DCtx, &Item, 1) != CBOR_TAG_INVALID64 ||
4091 QCBORDecode_GetNthTag(&DCtx, &Item, 2) != CBOR_TAG_INVALID64 ||
4092 QCBORDecode_GetNthTag(&DCtx, &Item, 3) != CBOR_TAG_INVALID64 ||
4093 QCBORDecode_GetNthTag(&DCtx, &Item, 4) != CBOR_TAG_INVALID64 ||
4094 Item.val.uCount != 2) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004095 return -4;
4096 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07004097 // consume the items in the array
4098 uError = QCBORDecode_GetNext(&DCtx, &Item);
4099 uError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundblade59289e52019-12-30 13:44:37 -08004100
Laurence Lundbladedd6e76e2021-03-10 01:54:01 -07004101#else /* QCBOR_DISABLE_EXP_AND_MANTISSA */
Laurence Lundblade9b334962020-08-27 10:55:53 -07004102 if(uError != QCBOR_SUCCESS ||
4103 Item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION ||
4104 QCBORDecode_GetNthTag(&DCtx, &Item, 0) != CBOR_TAG_INVALID64 ||
4105 QCBORDecode_GetNthTag(&DCtx, &Item, 1) != CBOR_TAG_INVALID64 ||
4106 QCBORDecode_GetNthTag(&DCtx, &Item, 2) != CBOR_TAG_INVALID64 ||
4107 QCBORDecode_GetNthTag(&DCtx, &Item, 3) != CBOR_TAG_INVALID64 ||
4108 QCBORDecode_GetNthTag(&DCtx, &Item, 4) != CBOR_TAG_INVALID64 ) {
4109 return -5;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004110 }
Laurence Lundbladedd6e76e2021-03-10 01:54:01 -07004111#endif /* QCBOR_DISABLE_EXP_AND_MANTISSA */
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004112
Laurence Lundblade9b334962020-08-27 10:55:53 -07004113 /*
4114 More than 4 tags on an item 225(226(227(228(229([])))))
4115 */
4116 uError = QCBORDecode_GetNext(&DCtx, &Item);
4117 if(uError != QCBOR_ERR_TOO_MANY_TAGS) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004118 return -6;
4119 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07004120
Laurence Lundblade88e9db22020-11-02 03:56:33 -08004121 if(QCBORDecode_GetNthTag(&DCtx, &Item, 0) != CBOR_TAG_INVALID64) {
4122 return -106;
4123 }
4124
4125
Laurence Lundblade9b334962020-08-27 10:55:53 -07004126 /* tag 10489608748473423768(
4127 2442302356(
4128 21590(
4129 240(
4130 []))))
4131 */
4132 uError = QCBORDecode_GetNext(&DCtx, &Item);
4133 if(uError != QCBOR_SUCCESS ||
4134 Item.uDataType != QCBOR_TYPE_ARRAY ||
4135 QCBORDecode_GetNthTag(&DCtx, &Item, 3) != 10489608748473423768ULL ||
4136 QCBORDecode_GetNthTag(&DCtx, &Item, 2) != 2442302356ULL ||
4137 QCBORDecode_GetNthTag(&DCtx, &Item, 1) != 21590ULL ||
4138 QCBORDecode_GetNthTag(&DCtx, &Item, 0) != 240ULL) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004139 return -7;
Laurence Lundblade9b334962020-08-27 10:55:53 -07004140 }
4141
4142 /* tag 21590(
4143 10489608748473423768(
4144 2442302357(
4145 21591(
4146 []))))
4147 */
4148 uError = QCBORDecode_GetNext(&DCtx, &Item);
4149 if(uError != QCBOR_SUCCESS ||
4150 Item.uDataType != QCBOR_TYPE_ARRAY ||
4151 QCBORDecode_GetNthTag(&DCtx, &Item, 0) != 65534ULL ||
4152 QCBORDecode_GetNthTag(&DCtx, &Item, 1) != 2442302357ULL ||
4153 QCBORDecode_GetNthTag(&DCtx, &Item, 2) != 10489608748473423768ULL ||
4154 QCBORDecode_GetNthTag(&DCtx, &Item, 3) != 21590ULL) {
4155 return -8;
4156 }
4157
4158 /* Make sure to blow past the limit of tags that must be mapped.
4159 works in conjuntion with entries above.
4160 269488144(269488145(269488146(269488147([]))))
4161 */
4162 uError = QCBORDecode_GetNext(&DCtx, &Item);
4163 if(uError != QCBOR_ERR_TOO_MANY_TAGS) {
4164 return -9;
4165 }
4166
4167 uError = QCBORDecode_GetNext(&DCtx, &Item);
4168 if(uError == QCBOR_SUCCESS) {
4169 return -10;
4170 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004171
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004172 // ----------------------------------
Laurence Lundbladeee851742020-01-08 08:37:05 -08004173 // This test sets up a caller-config list that includes the very large
Laurence Lundblade9b334962020-08-27 10:55:53 -07004174 // tage and then matches it. Caller-config lists are no longer
4175 // used or needed. This tests backwards compatibility with them.
Laurence Lundbladeee851742020-01-08 08:37:05 -08004176 QCBORDecode_Init(&DCtx,
4177 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEncodedLargeTag),
4178 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004179 const uint64_t puList[] = {0x9192939495969798, 257};
4180 const QCBORTagListIn TL = {2, puList};
4181 QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004182
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004183 if(QCBORDecode_GetNext(&DCtx, &Item)) {
4184 return -8;
4185 }
4186 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
4187 !QCBORDecode_IsTagged(&DCtx, &Item, 0x9192939495969798) ||
4188 QCBORDecode_IsTagged(&DCtx, &Item, 257) ||
4189 QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_BIGFLOAT) ||
4190 Item.val.uCount != 0) {
4191 return -9;
4192 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004193
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004194 //------------------------
Laurence Lundbladeee851742020-01-08 08:37:05 -08004195 // Sets up a caller-configured list and look up something not in it
Laurence Lundblade9b334962020-08-27 10:55:53 -07004196 // Another backwards compatibility test.
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004197 const uint64_t puLongList[17] = {1,2,1};
4198 const QCBORTagListIn TLLong = {17, puLongList};
Laurence Lundbladeee851742020-01-08 08:37:05 -08004199 QCBORDecode_Init(&DCtx,
4200 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEncodedLargeTag),
4201 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004202 QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TLLong);
4203 if(QCBORDecode_GetNext(&DCtx, &Item)) {
4204 return -11;
4205 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004206
Laurence Lundbladef7a70bc2020-10-24 12:23:25 -07004207 uint64_t puTags[4];
Laurence Lundblade9b334962020-08-27 10:55:53 -07004208 QCBORTagListOut Out = {0, 4, puTags};
4209
4210
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004211 // This tests retrievel of the full tag list
Laurence Lundbladeee851742020-01-08 08:37:05 -08004212 QCBORDecode_Init(&DCtx,
4213 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spLotsOfTags),
4214 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004215 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
4216 return -12;
4217 }
4218 if(puTags[0] != 0x9192939495969798 ||
4219 puTags[1] != 0x88 ||
Laurence Lundblade59289e52019-12-30 13:44:37 -08004220 puTags[2] != 0x06 ||
4221 puTags[3] != 0x07) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004222 return -13;
4223 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004224
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004225 // ----------------------
Laurence Lundblade9b334962020-08-27 10:55:53 -07004226 // This tests too small of an out list
Laurence Lundbladeee851742020-01-08 08:37:05 -08004227 QCBORDecode_Init(&DCtx,
4228 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spLotsOfTags),
4229 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004230 QCBORTagListOut OutSmall = {0, 3, puTags};
4231 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &OutSmall) != QCBOR_ERR_TOO_MANY_TAGS) {
4232 return -14;
4233 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004234
Laurence Lundblade9b334962020-08-27 10:55:53 -07004235
4236
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004237 // ---------------
Laurence Lundblade9b334962020-08-27 10:55:53 -07004238 // Decode a version of the "CSR" that has had a ton of tags randomly inserted
4239 // It is a bit of a messy test and maybe could be improved, but
4240 // it is retained as a backwards compatibility check.
Laurence Lundbladeee851742020-01-08 08:37:05 -08004241 QCBORDecode_Init(&DCtx,
4242 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRWithTags),
4243 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004244 int n = CheckCSRMaps(&DCtx);
4245 if(n) {
4246 return n-2000;
4247 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004248
Laurence Lundblade59289e52019-12-30 13:44:37 -08004249 Out = (QCBORTagListOut){0, 16, puTags};
Laurence Lundbladeee851742020-01-08 08:37:05 -08004250 QCBORDecode_Init(&DCtx,
4251 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRWithTags),
4252 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004253
Laurence Lundblade9b334962020-08-27 10:55:53 -07004254 /* With the spiffy decode revision, this tag list is not used.
4255 It doesn't matter if a tag is in this list or not so some
4256 tests that couldn't process a tag because it isn't in this list
4257 now can process these unlisted tags. The tests have been
4258 adjusted for this. */
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004259 const uint64_t puTagList[] = {773, 1, 90599561};
4260 const QCBORTagListIn TagList = {3, puTagList};
4261 QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TagList);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004262
4263
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004264 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
4265 return -100;
4266 }
4267 if(Item.uDataType != QCBOR_TYPE_MAP ||
4268 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC) ||
4269 QCBORDecode_IsTagged(&DCtx, &Item, 90599561) ||
4270 QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_DATE_EPOCH) ||
4271 Item.val.uCount != 2 ||
4272 puTags[0] != CBOR_TAG_CBOR_MAGIC ||
4273 puTags[1] != CBOR_TAG_CBOR_MAGIC ||
4274 puTags[2] != CBOR_TAG_CBOR_MAGIC ||
4275 Out.uNumUsed != 3) {
4276 return -101;
4277 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004278
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004279 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
4280 return -102;
4281 }
4282 if(Item.uDataType != QCBOR_TYPE_MAP ||
4283 QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC) ||
4284 QCBORDecode_IsTagged(&DCtx, &Item, 6) ||
Laurence Lundbladef7a70bc2020-10-24 12:23:25 -07004285 !QCBORDecode_IsTagged(&DCtx, &Item, 7) ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004286 Item.val.uCount != 2 ||
4287 puTags[0] != 5859837686836516696 ||
4288 puTags[1] != 7 ||
4289 Out.uNumUsed != 2) {
4290 return -103;
4291 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004292
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004293 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
4294 return -104;
4295 }
4296 if(Item.uDataType != QCBOR_TYPE_MAP ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004297 Item.val.uCount != 5 ||
4298 puTags[0] != 0x0b ||
4299 Out.uNumUsed != 1) {
4300 return -105;
4301 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004302
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004303 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
4304 return -106;
4305 }
4306 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
4307 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_COSE_MAC0) ||
4308 Item.val.string.len != 12 ||
4309 puTags[0] != CBOR_TAG_COSE_MAC0 ||
4310 puTags[1] != CBOR_TAG_COSE_MAC0 ||
4311 puTags[2] != CBOR_TAG_COSE_MAC0 ||
4312 Out.uNumUsed != 3) {
4313 return -105;
4314 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004315
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004316 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
4317 return -107;
4318 }
4319 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
4320 !QCBORDecode_IsTagged(&DCtx, &Item, 773) ||
4321 Item.val.string.len != 3 ||
4322 puTags[0] != 773 ||
4323 Out.uNumUsed != 1) {
4324 return -108;
4325 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004326
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004327 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
4328 return -109;
4329 }
4330 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade59289e52019-12-30 13:44:37 -08004331 !QCBORDecode_IsTagged(&DCtx, &Item, 16) ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004332 Item.val.string.len != 9 ||
Laurence Lundblade59289e52019-12-30 13:44:37 -08004333 puTags[0] != 16 ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07004334 puTags[3] != 7 ||
4335 Out.uNumUsed != 4) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004336 return -110;
4337 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004338
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004339 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
4340 return -111;
4341 }
4342 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
4343 !QCBORDecode_IsTagged(&DCtx, &Item, 17) ||
4344 Item.val.string.len != 9 ||
4345 puTags[0] != 17 ||
4346 Out.uNumUsed != 1) {
4347 return -112;
4348 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004349
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004350 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
4351 return -111;
4352 }
4353 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
4354 !QCBORDecode_IsTagged(&DCtx, &Item, 17) ||
4355 Item.val.string.len != 2 ||
4356 puTags[0] != 17 ||
4357 Out.uNumUsed != 1) {
4358 return -112;
4359 }
4360
4361 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
4362 return -113;
4363 }
4364 if(Item.uDataType != QCBOR_TYPE_MAP ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07004365 !QCBORDecode_IsTagged(&DCtx, &Item, 19) ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004366 Item.val.uCount != 2 ||
4367 puTags[0] != 19 ||
4368 Out.uNumUsed != 1) {
4369 return -114;
4370 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004371
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004372 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
4373 return -115;
4374 }
4375 if(Item.uDataType != QCBOR_TYPE_MAP ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07004376 !QCBORDecode_IsTagged(&DCtx, &Item, 9) ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004377 Item.val.uCount != 1 ||
4378 puTags[0] != 9 ||
4379 Out.uNumUsed != 1) {
4380 return -116;
4381 }
4382
4383 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
4384 return -116;
4385 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004386 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004387 Item.val.int64 != -7 ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004388 Out.uNumUsed != 0) {
4389 return -117;
4390 }
4391
4392 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
4393 return -118;
4394 }
4395 if(Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
4396 Item.val.string.len != 10 ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004397 puTags[0] != 12 ||
4398 Out.uNumUsed != 1) {
4399 return -119;
4400 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004401
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004402 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
4403 return -120;
4404 }
4405 if(Item.uDataType != QCBOR_TYPE_MAP ||
4406 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_ENC_AS_B16) ||
4407 Item.val.uCount != 1 ||
4408 puTags[0] != 0x17 ||
4409 Out.uNumUsed != 1) {
4410 return -121;
4411 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004412
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004413 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
4414 return -122;
4415 }
4416 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07004417 !QCBORDecode_IsTagged(&DCtx, &Item, 8) ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004418 Item.val.int64 != -3 ||
4419 puTags[0] != 8 ||
4420 Out.uNumUsed != 1) {
4421 return -123;
4422 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004423
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004424 if(QCBORDecode_Finish(&DCtx)) {
4425 return -124;
4426 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07004427
4428 UsefulBufC DateString;
4429 QCBORDecode_Init(&DCtx,
4430 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSpiffyTagInput),
4431 QCBOR_DECODE_MODE_NORMAL);
4432
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07004433 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade9b334962020-08-27 10:55:53 -07004434 // tagged date string
4435 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString);
4436 // untagged date string
4437 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString);
4438 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_SUCCESS) {
4439 return 100;
4440 }
4441 // untagged byte string
4442 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString);
4443 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
4444 return 101;
4445 }
4446 // tagged regex
4447 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString);
4448 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
4449 return 102;
4450 }
4451 // tagged date string with a byte string
4452 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString);
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07004453 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT) {
Laurence Lundblade9b334962020-08-27 10:55:53 -07004454 return 103;
4455 }
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07004456 // The exit errors out because the last item, the date string with
4457 // bad content makes the array untraversable (the bad date string
4458 // could have tag content of an array or such that is not consumed
4459 // by the date decoding).
Laurence Lundblade9b334962020-08-27 10:55:53 -07004460 QCBORDecode_ExitArray(&DCtx);
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07004461 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT) {
Laurence Lundblade9b334962020-08-27 10:55:53 -07004462 return 104;
4463 }
4464
4465
4466 QCBORDecode_Init(&DCtx,
4467 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSpiffyTagInput),
4468 QCBOR_DECODE_MODE_NORMAL);
4469
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07004470 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade9b334962020-08-27 10:55:53 -07004471 // tagged date string
4472 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString);
4473 // untagged date string
4474 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString);
4475 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_SUCCESS) {
4476 return 200;
4477 }
4478 // untagged byte string
4479 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString);
4480 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
4481 return 201;
4482 }
4483 // tagged regex
4484 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString);
4485 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
4486 return 202;
4487 }
4488 // tagged date string with a byte string
4489 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString);
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07004490 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT) {
Laurence Lundblade9b334962020-08-27 10:55:53 -07004491 return 203;
4492 }
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07004493 // See comments above
Laurence Lundblade9b334962020-08-27 10:55:53 -07004494 QCBORDecode_ExitArray(&DCtx);
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07004495 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT) {
Laurence Lundblade9b334962020-08-27 10:55:53 -07004496 return 204;
4497 }
4498
4499 QCBORDecode_Init(&DCtx,
4500 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSpiffyTagInput),
4501 QCBOR_DECODE_MODE_NORMAL);
4502
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07004503 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade9b334962020-08-27 10:55:53 -07004504 // tagged date string
4505 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString);
4506 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
4507 return 300;
4508 }
4509 // untagged date string
4510 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString);
4511 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
4512 return 301;
4513 }
4514 // untagged byte string
4515 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString);
4516 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
4517 return 302;
4518 }
4519 // tagged regex
4520 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString);
4521 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
4522 return 303;
4523 }
4524 // tagged date string with a byte string
4525 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString);
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07004526 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT) {
Laurence Lundblade9b334962020-08-27 10:55:53 -07004527 return 304;
4528 }
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07004529 // See comments above
Laurence Lundblade9b334962020-08-27 10:55:53 -07004530 QCBORDecode_ExitArray(&DCtx);
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07004531 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT) {
Laurence Lundblade9b334962020-08-27 10:55:53 -07004532 return 305;
4533 }
4534
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004535 return 0;
4536}
4537
Laurence Lundblade37286c02022-09-03 10:05:02 -07004538/*
4539 * These are showing the big numbers converted to integers.
4540 * The tag numbers are not shown.
4541 *
Laurence Lundbladeec290b82024-06-10 11:10:54 -07004542 * [
4543 * 18446744073709551616,
4544 * -18446744073709551617,
4545 * {
4546 * -64: -18446744073709551617,
4547 * 64: 18446744073709551616,
4548 * "BN+": 18446744073709551616,
4549 * "BN-": -18446744073709551617
Laurence Lundblade37286c02022-09-03 10:05:02 -07004550 * }
4551 * ]
4552 */
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004553
Laurence Lundbladecc7da412020-12-27 00:09:07 -08004554static const uint8_t spBigNumInput[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004555 0x83,
4556 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
4557 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
4558 0xA4,
Laurence Lundbladeec290b82024-06-10 11:10:54 -07004559 0x38, 0x3F,
4560 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004561 0x18, 0x40,
4562 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
Laurence Lundbladeec290b82024-06-10 11:10:54 -07004563 0x63, 0x42, 0x4E, 0x2B,
4564 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
4565 0x63, 0x42, 0x4E, 0x2D,
4566 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
4567};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004568
Laurence Lundblade37286c02022-09-03 10:05:02 -07004569#ifndef QCBOR_DISABLE_TAGS
Laurence Lundbladecc7da412020-12-27 00:09:07 -08004570/* The expected big num */
4571static const uint8_t spBigNum[] = {
4572 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
4573 0x00};
Laurence Lundblade37286c02022-09-03 10:05:02 -07004574#endif /* QCBOR_DISABLE_TAGS */
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004575
4576
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03004577int32_t BignumParseTest(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004578{
4579 QCBORDecodeContext DCtx;
4580 QCBORItem Item;
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07004581 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004582
Laurence Lundbladeee851742020-01-08 08:37:05 -08004583 QCBORDecode_Init(&DCtx,
4584 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNumInput),
4585 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004586
4587
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004588 //
4589 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
4590 return -1;
4591 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade830fbf92020-05-31 17:22:33 -07004592 return -2;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004593 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004594
Laurence Lundblade37286c02022-09-03 10:05:02 -07004595#ifndef QCBOR_DISABLE_TAGS
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004596 //
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004597 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07004598 return -3;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004599 if(Item.uDataType != QCBOR_TYPE_POSBIGNUM ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07004600 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07004601 return -4;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004602 }
4603
4604 //
4605 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07004606 return -5;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004607 if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07004608 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07004609 return -6;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004610 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004611
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004612 //
4613 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07004614 return -7;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004615 if(Item.uDataType != QCBOR_TYPE_MAP) {
Laurence Lundblade830fbf92020-05-31 17:22:33 -07004616 return -8;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004617 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004618
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004619 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundbladeec290b82024-06-10 11:10:54 -07004620 return -15;
4621 if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM ||
4622 Item.uLabelType != QCBOR_TYPE_INT64 ||
4623 Item.label.int64 != -64 ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07004624 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundbladeec290b82024-06-10 11:10:54 -07004625 return -16;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004626 }
4627
4628 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07004629 return -11;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004630 if(Item.uDataType != QCBOR_TYPE_POSBIGNUM ||
4631 Item.uLabelType != QCBOR_TYPE_INT64 ||
4632 Item.label.int64 != 64 ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07004633 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07004634 return -12;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004635 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004636
Laurence Lundbladeec290b82024-06-10 11:10:54 -07004637#ifndef QCBOR_DISABLE_NON_INTEGER_LABELS
4638 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
4639 return -9;
4640 if(Item.uDataType != QCBOR_TYPE_POSBIGNUM ||
4641 Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
4642 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
4643 return -10;
4644 }
4645
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004646 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07004647 return -13;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004648 if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM ||
4649 Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07004650 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07004651 return -14;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004652 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004653
Laurence Lundbladeec290b82024-06-10 11:10:54 -07004654
4655#endif /* ! QCBOR_DISABLE_NON_INTEGER_LABELS */
4656
Laurence Lundblade37286c02022-09-03 10:05:02 -07004657#else
4658
4659 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_TAGS_DISABLED) {
4660 return -100;
4661 }
4662#endif /* QCBOR_DISABLE_TAGS */
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004663
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004664 return 0;
4665}
4666
4667
Laurence Lundbladec5fef682020-01-25 11:38:45 -08004668static int32_t CheckItemWithIntLabel(QCBORDecodeContext *pCtx,
Laurence Lundbladeee851742020-01-08 08:37:05 -08004669 uint8_t uDataType,
4670 uint8_t uNestingLevel,
4671 uint8_t uNextNest,
4672 int64_t nLabel,
4673 QCBORItem *pItem)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004674{
4675 QCBORItem Item;
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07004676 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004677
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004678 if((nCBORError = QCBORDecode_GetNext(pCtx, &Item))) return -1;
4679 if(Item.uDataType != uDataType) return -1;
4680 if(uNestingLevel > 0) {
Laurence Lundbladeec290b82024-06-10 11:10:54 -07004681 if(Item.uLabelType != QCBOR_TYPE_INT64) {
Laurence Lundbladeee851742020-01-08 08:37:05 -08004682 return -1;
4683 }
Laurence Lundbladeec290b82024-06-10 11:10:54 -07004684 if(Item.label.int64 != nLabel) {
4685 return -1;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004686 }
Laurence Lundbladeec290b82024-06-10 11:10:54 -07004687
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004688 }
4689 if(Item.uNestingLevel != uNestingLevel) return -1;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304690 if(Item.uNextNestLevel != uNextNest) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004691
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004692 if(pItem) {
4693 *pItem = Item;
4694 }
4695 return 0;
4696}
4697
Laurence Lundblade742df4a2018-10-13 20:07:17 +08004698// Same code checks definite and indefinite length versions of the map
Laurence Lundbladec5fef682020-01-25 11:38:45 -08004699static int32_t CheckCSRMaps(QCBORDecodeContext *pDC)
Laurence Lundblade742df4a2018-10-13 20:07:17 +08004700{
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304701 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 0, 1, 0, NULL)) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004702
Laurence Lundblade9b334962020-08-27 10:55:53 -07004703 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 1, 2, -23, NULL)) return -2;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004704
Laurence Lundblade9b334962020-08-27 10:55:53 -07004705 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 2, 3, -20, NULL)) return -3;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004706
Laurence Lundblade9b334962020-08-27 10:55:53 -07004707 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -18, NULL)) return -4;
4708 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -17, NULL)) return -5;
4709 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -15, NULL)) return -6;
4710 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -16, NULL)) return -7;
4711 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 2, -14, NULL)) return -8;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004712
Laurence Lundblade9b334962020-08-27 10:55:53 -07004713 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 2, 3, -19, NULL)) return -9;
4714 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 3, 4, -11, NULL)) return -10;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004715
Laurence Lundblade9b334962020-08-27 10:55:53 -07004716 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_INT64, 4, 3, -9, NULL)) return -11;
4717 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_BYTE_STRING, 3, 1, -10, NULL)) return -12;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004718
Laurence Lundblade9b334962020-08-27 10:55:53 -07004719 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 1, 2, -22, NULL)) return -13;
4720 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_INT64, 2, 0, -5, NULL)) return -14;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004721
Laurence Lundblade9b334962020-08-27 10:55:53 -07004722 if(QCBORDecode_Finish(pDC)) return -20;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004723
Laurence Lundblade742df4a2018-10-13 20:07:17 +08004724 return 0;
4725}
4726
4727
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004728/*
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004729{
4730 -23: {
4731 -20: {
4732 -18: "Organization",
4733 -17: "SSG",
4734 -15: "Confusion",
4735 -16: "San Diego",
4736 -14: "US"
4737 },
4738 -19: {
4739 -11: {
4740 -9: -7
4741 },
4742 -10: '\u0001\u0002\u0003\u0004\u0005\u0006\a\b\t\n'
4743 }
4744 },
4745 -22: {
4746 -5: -3
4747 }
4748}
Laurence Lundbladecc7da412020-12-27 00:09:07 -08004749*/
4750static const uint8_t spCSRInput[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004751 0xa2, 0x36, 0xa2, 0x33, 0xa5, 0x31, 0x6c, 0x4f,
4752 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74,
4753 0x69, 0x6f, 0x6e, 0x30, 0x63, 0x53, 0x53, 0x47,
4754 0x2e, 0x69, 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73,
4755 0x69, 0x6f, 0x6e, 0x2f, 0x69, 0x53, 0x61, 0x6e,
4756 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f, 0x2d, 0x62,
4757 0x55, 0x53, 0x32, 0xa2, 0x2a, 0xa1, 0x28, 0x26,
4758 0x29, 0x4a, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
4759 0x07, 0x08, 0x09, 0x0a, 0x35, 0xa1, 0x24, 0x22};
4760
Laurence Lundbladecc7da412020-12-27 00:09:07 -08004761// Same map as above, but using indefinite lengths
4762static const uint8_t spCSRInputIndefLen[] = {
4763 0xbf, 0x36, 0xbf, 0x33, 0xbf, 0x31, 0x6c, 0x4f,
4764 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74,
4765 0x69, 0x6f, 0x6e, 0x30, 0x63, 0x53, 0x53, 0x47,
4766 0x2e, 0x69, 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73,
4767 0x69, 0x6f, 0x6e, 0x2f, 0x69, 0x53, 0x61, 0x6e,
4768 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f, 0x2d, 0x62,
4769 0x55, 0x53, 0xff, 0x32, 0xbf, 0x2a, 0xbf, 0x28,
4770 0x26, 0xff, 0x29, 0x4a, 0x01, 0x02, 0x03, 0x04,
4771 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0xff, 0xff,
4772 0x35, 0xbf, 0x24, 0x22, 0xff, 0xff};
4773
4774
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03004775int32_t NestedMapTest(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004776{
4777 QCBORDecodeContext DCtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004778
Laurence Lundbladeee851742020-01-08 08:37:05 -08004779 QCBORDecode_Init(&DCtx,
4780 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput),
4781 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004782
Laurence Lundblade742df4a2018-10-13 20:07:17 +08004783 return CheckCSRMaps(&DCtx);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004784}
4785
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08004786
4787
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03004788int32_t StringDecoderModeFailTest(void)
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08004789{
4790 QCBORDecodeContext DCtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004791
Laurence Lundbladeee851742020-01-08 08:37:05 -08004792 QCBORDecode_Init(&DCtx,
4793 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput),
4794 QCBOR_DECODE_MODE_MAP_STRINGS_ONLY);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004795
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08004796 QCBORItem Item;
4797 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004798
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08004799 if(QCBORDecode_GetNext(&DCtx, &Item)) {
4800 return -1;
4801 }
4802 if(Item.uDataType != QCBOR_TYPE_MAP) {
4803 return -2;
4804 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004805
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08004806 nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
4807 if(nCBORError != QCBOR_ERR_MAP_LABEL_TYPE) {
4808 return -3;
4809 }
4810
4811 return 0;
4812}
4813
4814
Laurence Lundblade742df4a2018-10-13 20:07:17 +08004815
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03004816int32_t NestedMapTestIndefLen(void)
Laurence Lundblade742df4a2018-10-13 20:07:17 +08004817{
4818 QCBORDecodeContext DCtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004819
Laurence Lundbladeee851742020-01-08 08:37:05 -08004820 QCBORDecode_Init(&DCtx,
4821 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInputIndefLen),
4822 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004823
Laurence Lundblade742df4a2018-10-13 20:07:17 +08004824 return CheckCSRMaps(&DCtx);
4825}
4826
4827
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004828
Laurence Lundblade17ede402018-10-13 11:43:07 +08004829static UsefulBufC make_nested_indefinite_arrays(int n, UsefulBuf Storage)
4830{
4831 UsefulOutBuf UOB;
4832 UsefulOutBuf_Init(&UOB, Storage);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004833
Laurence Lundblade17ede402018-10-13 11:43:07 +08004834 int i;
4835 for(i = 0; i < n; i++) {
4836 UsefulOutBuf_AppendByte(&UOB, 0x9f);
4837 }
4838
4839 for(i = 0; i < n; i++) {
4840 UsefulOutBuf_AppendByte(&UOB, 0xff);
4841 }
4842 return UsefulOutBuf_OutUBuf(&UOB);
4843}
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08004844
4845
Laurence Lundbladec5fef682020-01-25 11:38:45 -08004846static int32_t parse_indeflen_nested(UsefulBufC Nested, int nNestLevel)
Laurence Lundblade17ede402018-10-13 11:43:07 +08004847{
4848 QCBORDecodeContext DC;
4849 QCBORDecode_Init(&DC, Nested, 0);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004850
Laurence Lundblade17ede402018-10-13 11:43:07 +08004851 int j;
4852 for(j = 0; j < nNestLevel; j++) {
4853 QCBORItem Item;
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07004854 QCBORError nReturn = QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade17ede402018-10-13 11:43:07 +08004855 if(j >= QCBOR_MAX_ARRAY_NESTING) {
4856 // Should be in error
Laurence Lundbladea9489f82020-09-12 13:50:56 -07004857 if(nReturn != QCBOR_ERR_ARRAY_DECODE_NESTING_TOO_DEEP) {
Laurence Lundblade17ede402018-10-13 11:43:07 +08004858 return -4;
4859 } else {
4860 return 0; // Decoding doesn't recover after an error
4861 }
4862 } else {
4863 // Should be no error
4864 if(nReturn) {
4865 return -9; // Should not have got an error
4866 }
4867 }
4868 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
4869 return -7;
4870 }
4871 }
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07004872 QCBORError nReturn = QCBORDecode_Finish(&DC);
Laurence Lundblade17ede402018-10-13 11:43:07 +08004873 if(nReturn) {
4874 return -3;
4875 }
4876 return 0;
4877}
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08004878
4879
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03004880int32_t IndefiniteLengthNestTest(void)
Laurence Lundblade17ede402018-10-13 11:43:07 +08004881{
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05304882 UsefulBuf_MAKE_STACK_UB(Storage, 50);
Laurence Lundblade17ede402018-10-13 11:43:07 +08004883 int i;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004884 for(i=1; i < QCBOR_MAX_ARRAY_NESTING+4; i++) {
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08004885 const UsefulBufC Nested = make_nested_indefinite_arrays(i, Storage);
Laurence Lundblade17ede402018-10-13 11:43:07 +08004886 int nReturn = parse_indeflen_nested(Nested, i);
4887 if(nReturn) {
4888 return nReturn;
4889 }
4890 }
4891 return 0;
4892}
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08004893
Laurence Lundbladeee851742020-01-08 08:37:05 -08004894// [1, [2, 3]]
4895static const uint8_t spIndefiniteArray[] = {0x9f, 0x01, 0x82, 0x02, 0x03, 0xff};
4896// No closing break
4897static const uint8_t spIndefiniteArrayBad1[] = {0x9f};
4898// Not enough closing breaks
4899static const uint8_t spIndefiniteArrayBad2[] = {0x9f, 0x9f, 0x02, 0xff};
4900// Too many closing breaks
4901static const uint8_t spIndefiniteArrayBad3[] = {0x9f, 0x02, 0xff, 0xff};
4902// Unclosed indeflen inside def len
4903static const uint8_t spIndefiniteArrayBad4[] = {0x81, 0x9f};
4904// confused tag
4905static const uint8_t spIndefiniteArrayBad5[] = {0x9f, 0xd1, 0xff};
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08004906
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03004907int32_t IndefiniteLengthArrayMapTest(void)
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08004908{
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07004909 QCBORError nResult;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08004910 // --- first test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07004911 UsefulBufC IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArray);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004912
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08004913 // Decode it and see if it is OK
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08004914 QCBORDecodeContext DC;
4915 QCBORItem Item;
4916 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004917
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08004918 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05304919
4920 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
4921 Item.uNestingLevel != 0 ||
4922 Item.uNextNestLevel != 1) {
4923 return -111;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08004924 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004925
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08004926 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05304927 if(Item.uDataType != QCBOR_TYPE_INT64 ||
4928 Item.uNestingLevel != 1 ||
4929 Item.uNextNestLevel != 1) {
4930 return -2;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08004931 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004932
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08004933 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05304934 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
4935 Item.uNestingLevel != 1 ||
4936 Item.uNextNestLevel != 2) {
4937 return -3;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08004938 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004939
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08004940 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade12b495d2018-12-17 11:15:54 -08004941 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade6de37062018-10-15 12:22:42 +05304942 Item.uNestingLevel != 2 ||
4943 Item.uNextNestLevel != 2) {
4944 return -4;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08004945 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004946
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08004947 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade12b495d2018-12-17 11:15:54 -08004948 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade6de37062018-10-15 12:22:42 +05304949 Item.uNestingLevel != 2 ||
4950 Item.uNextNestLevel != 0) {
4951 return -5;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08004952 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004953
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08004954 if(QCBORDecode_Finish(&DC)) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05304955 return -6;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08004956 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004957
Laurence Lundblade19e0c802018-10-13 12:19:55 +08004958 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07004959 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad1);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004960
Laurence Lundblade19e0c802018-10-13 12:19:55 +08004961 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004962
Laurence Lundblade19e0c802018-10-13 12:19:55 +08004963 nResult = QCBORDecode_GetNext(&DC, &Item);
4964 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05304965 return -7;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08004966 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004967
Laurence Lundblade570fab52018-10-13 18:28:27 +08004968 nResult = QCBORDecode_Finish(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07004969 if(nResult != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05304970 return -8;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08004971 }
4972
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004973
Laurence Lundblade19e0c802018-10-13 12:19:55 +08004974 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07004975 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad2);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004976
Laurence Lundblade19e0c802018-10-13 12:19:55 +08004977 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004978
Laurence Lundblade19e0c802018-10-13 12:19:55 +08004979 nResult = QCBORDecode_GetNext(&DC, &Item);
4980 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05304981 return -9;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08004982 }
4983
4984 nResult = QCBORDecode_GetNext(&DC, &Item);
4985 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05304986 return -10;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08004987 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004988
Laurence Lundblade19e0c802018-10-13 12:19:55 +08004989 nResult = QCBORDecode_GetNext(&DC, &Item);
4990 if(nResult || Item.uDataType != QCBOR_TYPE_INT64) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05304991 return -11;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08004992 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004993
Laurence Lundblade19e0c802018-10-13 12:19:55 +08004994 nResult = QCBORDecode_Finish(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07004995 if(nResult != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05304996 return -12;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08004997 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004998
4999
Laurence Lundblade19e0c802018-10-13 12:19:55 +08005000 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07005001 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad3);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005002
Laurence Lundblade19e0c802018-10-13 12:19:55 +08005003 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005004
Laurence Lundblade19e0c802018-10-13 12:19:55 +08005005 nResult = QCBORDecode_GetNext(&DC, &Item);
5006 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05305007 return -13;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08005008 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005009
Laurence Lundblade19e0c802018-10-13 12:19:55 +08005010 nResult = QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade642282a2020-06-23 12:00:33 -07005011 if(nResult != QCBOR_SUCCESS) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05305012 return -14;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08005013 }
Laurence Lundblade6de37062018-10-15 12:22:42 +05305014
Laurence Lundblade642282a2020-06-23 12:00:33 -07005015 nResult = QCBORDecode_GetNext(&DC, &Item);
5016 if(nResult != QCBOR_ERR_BAD_BREAK) {
5017 return -140;
5018 }
5019
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005020
Laurence Lundblade570fab52018-10-13 18:28:27 +08005021 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07005022 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad4);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005023
Laurence Lundblade570fab52018-10-13 18:28:27 +08005024 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005025
Laurence Lundblade570fab52018-10-13 18:28:27 +08005026 nResult = QCBORDecode_GetNext(&DC, &Item);
5027 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05305028 return -15;
Laurence Lundblade570fab52018-10-13 18:28:27 +08005029 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005030
Laurence Lundblade570fab52018-10-13 18:28:27 +08005031 nResult = QCBORDecode_GetNext(&DC, &Item);
5032 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05305033 return -16;
Laurence Lundblade570fab52018-10-13 18:28:27 +08005034 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005035
Laurence Lundblade570fab52018-10-13 18:28:27 +08005036 nResult = QCBORDecode_Finish(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07005037 if(nResult != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05305038 return -17;
Laurence Lundblade570fab52018-10-13 18:28:27 +08005039 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005040
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05305041 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07005042 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad5);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005043
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05305044 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005045
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05305046 nResult = QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade37286c02022-09-03 10:05:02 -07005047
5048#ifndef QCBOR_DISABLE_TAGS
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05305049 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05305050 return -18;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05305051 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005052
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05305053 nResult = QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05305054 if(nResult != QCBOR_ERR_BAD_BREAK) {
5055 return -19;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05305056 }
Laurence Lundblade37286c02022-09-03 10:05:02 -07005057#else /* QCBOR_DISABLE_TAGS */
5058 if(nResult != QCBOR_ERR_TAGS_DISABLED) {
5059 return -20;
5060 }
5061#endif /* QCBOR_DISABLE_TAGS */
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005062
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08005063 return 0;
5064}
5065
Laurence Lundblade17ede402018-10-13 11:43:07 +08005066
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08005067#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
5068
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07005069static const uint8_t spIndefiniteLenString[] = {
Laurence Lundblade17ede402018-10-13 11:43:07 +08005070 0x81, // Array of length one
5071 0x7f, // text string marked with indefinite length
5072 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment
5073 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment
5074 0xff // ending break
5075};
5076
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07005077static const uint8_t spIndefiniteLenStringBad2[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305078 0x81, // Array of length one
5079 0x7f, // text string marked with indefinite length
5080 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment
5081 0x44, 0x6d, 0x69, 0x6e, 0x67, // second segment of wrong type
5082 0xff // ending break
5083};
5084
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07005085static const uint8_t spIndefiniteLenStringBad3[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305086 0x81, // Array of length one
5087 0x7f, // text string marked with indefinite length
5088 0x01, 0x02, // Not a string
5089 0xff // ending break
5090};
5091
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07005092static const uint8_t spIndefiniteLenStringBad4[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305093 0x81, // Array of length one
5094 0x7f, // text string marked with indefinite length
5095 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment
5096 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment
5097 // missing end of string
5098};
5099
Laurence Lundbladeec290b82024-06-10 11:10:54 -07005100#ifndef QCBOR_DISABLE_NON_INTEGER_LABELS
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07005101static const uint8_t spIndefiniteLenStringLabel[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305102 0xa1, // Array of length one
5103 0x7f, // text string marked with indefinite length
5104 0x65, 0x73, 0x74, 0x72, 0x75, 0x75, // first segment
5105 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment
5106 0xff, // ending break
5107 0x01 // integer being labeled.
5108};
Laurence Lundbladeec290b82024-06-10 11:10:54 -07005109#endif /* ! QCBOR_DISABLE_NON_INTEGER_LABELS */
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305110
Laurence Lundbladec5fef682020-01-25 11:38:45 -08005111/**
5112 Make an indefinite length string
5113
5114 @param Storage Storage for string, must be 144 bytes in size
5115 @return The indefinite length string
5116
5117 This makes an array with one indefinite length string that has 7 chunks
5118 from size of 1 byte up to 64 bytes.
5119 */
5120static UsefulBufC MakeIndefiniteBigBstr(UsefulBuf Storage)
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305121{
5122 UsefulOutBuf UOB;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005123
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305124 UsefulOutBuf_Init(&UOB, Storage);
5125 UsefulOutBuf_AppendByte(&UOB, 0x81);
5126 UsefulOutBuf_AppendByte(&UOB, 0x5f);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005127
Laurence Lundbladec5fef682020-01-25 11:38:45 -08005128 uint8_t uStringByte = 0;
5129 // Use of type int is intentional
5130 for(int uChunkSize = 1; uChunkSize <= 128; uChunkSize *= 2) {
5131 // Not using preferred encoding here, but that is OK.
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305132 UsefulOutBuf_AppendByte(&UOB, 0x58);
Laurence Lundbladec5fef682020-01-25 11:38:45 -08005133 UsefulOutBuf_AppendByte(&UOB, (uint8_t)uChunkSize);
5134 for(int j = 0; j < uChunkSize; j++) {
5135 UsefulOutBuf_AppendByte(&UOB, uStringByte);
5136 uStringByte++;
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305137 }
5138 }
5139 UsefulOutBuf_AppendByte(&UOB, 0xff);
5140
5141 return UsefulOutBuf_OutUBuf(&UOB);
5142}
5143
5144static int CheckBigString(UsefulBufC BigString)
5145{
5146 if(BigString.len != 255) {
5147 return 1;
5148 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005149
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305150 for(uint8_t i = 0; i < 255; i++){
5151 if(((const uint8_t *)BigString.ptr)[i] != i) {
5152 return 1;
5153 }
5154 }
5155 return 0;
5156}
5157
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05305158
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03005159int32_t IndefiniteLengthStringTest(void)
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05305160{
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305161 QCBORDecodeContext DC;
5162 QCBORItem Item;
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05305163 // big enough for MakeIndefiniteBigBstr() + MemPool overhead
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08005164 UsefulBuf_MAKE_STACK_UB(MemPool, 350);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005165
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305166 // --- Simple normal indefinite length string ------
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07005167 UsefulBufC IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenString);
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305168 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005169
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05305170 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305171 return -1;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05305172 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005173
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305174 if(QCBORDecode_GetNext(&DC, &Item)) {
5175 return -2;
5176 }
5177 if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.uDataAlloc) {
5178 return -3;
5179 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005180
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305181 if(QCBORDecode_GetNext(&DC, &Item)) {
5182 return -4;
5183 }
5184 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || !Item.uDataAlloc) {
5185 return -5;
5186 }
5187 if(QCBORDecode_Finish(&DC)) {
5188 return -6;
5189 }
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05305190
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305191 // ----- types mismatch ---
Laurence Lundbladeee851742020-01-08 08:37:05 -08005192 QCBORDecode_Init(&DC,
5193 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad2),
5194 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005195
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305196 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
5197 return -7;
5198 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005199
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305200 if(QCBORDecode_GetNext(&DC, &Item)) {
5201 return -8;
5202 }
5203 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
5204 return -9;
5205 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005206
Laurence Lundblade30816f22018-11-10 13:40:22 +07005207 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_INDEFINITE_STRING_CHUNK) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305208 return -10;
5209 }
5210
5211 // ----- not a string ---
Laurence Lundbladeee851742020-01-08 08:37:05 -08005212 QCBORDecode_Init(&DC,
5213 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad3),
5214 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005215
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305216 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
5217 return -11;
5218 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005219
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305220 if(QCBORDecode_GetNext(&DC, &Item)) {
5221 return -12;
5222 }
5223 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
5224 return -13;
5225 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005226
Laurence Lundblade30816f22018-11-10 13:40:22 +07005227 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_INDEFINITE_STRING_CHUNK) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305228 return -14;
5229 }
5230
5231 // ----- no end -----
Laurence Lundbladeee851742020-01-08 08:37:05 -08005232 QCBORDecode_Init(&DC,
5233 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad4),
5234 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005235
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305236 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
5237 return -15;
5238 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005239
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305240 if(QCBORDecode_GetNext(&DC, &Item)) {
5241 return -16;
5242 }
5243 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
5244 return -17;
5245 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005246
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305247 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_HIT_END) {
5248 return -18;
5249 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005250
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305251 // ------ Don't set a string allocator and see an error -----
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05305252 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005253
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05305254 QCBORDecode_GetNext(&DC, &Item);
5255 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305256 return -19;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05305257 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005258
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05305259 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_NO_STRING_ALLOCATOR) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305260 return -20;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05305261 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005262
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05305263 // ----- Mempool is way too small -----
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08005264 UsefulBuf_MAKE_STACK_UB(MemPoolTooSmall, QCBOR_DECODE_MIN_MEM_POOL_SIZE-1);
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05305265
5266 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
5267 if(!QCBORDecode_SetMemPool(&DC, MemPoolTooSmall, false)) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305268 return -21;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05305269 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005270
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05305271 // ----- Mempool is way too small -----
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05305272 UsefulBuf_MAKE_STACK_UB(BigIndefBStrStorage, 290);
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08005273 const UsefulBufC BigIndefBStr = MakeIndefiniteBigBstr(BigIndefBStrStorage);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005274
Laurence Lundbladeee851742020-01-08 08:37:05 -08005275 // 80 is big enough for MemPool overhead, but not BigIndefBStr
5276 UsefulBuf_MAKE_STACK_UB(MemPoolSmall, 80);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005277
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305278 QCBORDecode_Init(&DC, BigIndefBStr, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05305279 if(QCBORDecode_SetMemPool(&DC, MemPoolSmall, false)) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305280 return -22;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05305281 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005282
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05305283 QCBORDecode_GetNext(&DC, &Item);
5284 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305285 return -23;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05305286 }
Laurence Lundblade30816f22018-11-10 13:40:22 +07005287 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_STRING_ALLOCATE) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305288 return -24;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05305289 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005290
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305291 // ---- big bstr -----
5292 QCBORDecode_Init(&DC, BigIndefBStr, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005293
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305294 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
5295 return -25;
5296 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005297
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05305298 if(QCBORDecode_GetNext(&DC, &Item)) {
5299 return -26;
5300 }
5301 if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.uDataAlloc) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305302 return -26;
5303 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005304
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305305 if(QCBORDecode_GetNext(&DC, &Item)) {
5306 return -27;
5307 }
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05305308 if(Item.uDataType != QCBOR_TYPE_BYTE_STRING || !Item.uDataAlloc || Item.uNestingLevel != 1) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305309 return -28;
5310 }
5311 if(CheckBigString(Item.val.string)) {
5312 return -3;
5313 }
5314 if(QCBORDecode_Finish(&DC)) {
5315 return -29;
5316 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005317
Laurence Lundbladeec290b82024-06-10 11:10:54 -07005318#ifndef QCBOR_DISABLE_NON_INTEGER_LABELS
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305319 // --- label is an indefinite length string ------
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07005320 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringLabel), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005321
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305322 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
5323 return -30;
5324 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005325
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305326 QCBORDecode_GetNext(&DC, &Item);
5327 if(Item.uDataType != QCBOR_TYPE_MAP) {
5328 return -31;
5329 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005330
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305331 if(QCBORDecode_GetNext(&DC, &Item)){
5332 return -32;
5333 }
Laurence Lundbladeee851742020-01-08 08:37:05 -08005334 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
5335 Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305336 Item.uDataAlloc || !Item.uLabelAlloc ||
5337 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("struuming"))) {
5338 return -33;
5339 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005340
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305341 if(QCBORDecode_Finish(&DC)) {
5342 return -34;
5343 }
Laurence Lundbladeec290b82024-06-10 11:10:54 -07005344#endif /* ! QCBOR_DISABLE_NON_INTEGER_LABELS */
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005345
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08005346 return 0;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08005347}
5348
5349
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03005350int32_t AllocAllStringsTest(void)
Laurence Lundbladea44d5062018-10-17 18:45:12 +05305351{
5352 QCBORDecodeContext DC;
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08005353 QCBORError nCBORError;
5354
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005355
Laurence Lundbladea44d5062018-10-17 18:45:12 +05305356 // First test, use the "CSRMap" as easy input and checking
Laurence Lundbladeee851742020-01-08 08:37:05 -08005357 QCBORDecode_Init(&DC,
5358 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput),
5359 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005360
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08005361 UsefulBuf_MAKE_STACK_UB(Pool, sizeof(spCSRInput) + QCBOR_DECODE_MIN_MEM_POOL_SIZE);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005362
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08005363 nCBORError = QCBORDecode_SetMemPool(&DC, Pool, 1); // Turn on copying.
5364 if(nCBORError) {
Laurence Lundbladea44d5062018-10-17 18:45:12 +05305365 return -1;
5366 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005367
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08005368 if(CheckCSRMaps(&DC)) {
5369 return -2;
5370 }
5371
Laurence Lundbladeec290b82024-06-10 11:10:54 -07005372#ifndef QCBOR_DISABLE_NON_INTEGER_LABELS
Laurence Lundblade2f467f92020-10-09 17:50:11 -07005373 // Next parse, save pointers to a few strings, destroy original and
5374 // see all is OK.
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08005375 UsefulBuf_MAKE_STACK_UB(CopyOfStorage, sizeof(pValidMapEncoded) + QCBOR_DECODE_MIN_MEM_POOL_SIZE);
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08005376 const UsefulBufC CopyOf = UsefulBuf_Copy(CopyOfStorage, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded));
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08005377
Laurence Lundbladea44d5062018-10-17 18:45:12 +05305378 QCBORDecode_Init(&DC, CopyOf, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08005379 UsefulBuf_Set(Pool, '/');
Laurence Lundbladea44d5062018-10-17 18:45:12 +05305380 QCBORDecode_SetMemPool(&DC, Pool, 1); // Turn on copying.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005381
Laurence Lundbladea44d5062018-10-17 18:45:12 +05305382 QCBORItem Item1, Item2, Item3, Item4;
5383 if((nCBORError = QCBORDecode_GetNext(&DC, &Item1)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07005384 return (int32_t)nCBORError;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05305385 if(Item1.uDataType != QCBOR_TYPE_MAP ||
5386 Item1.val.uCount != 3)
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08005387 return -3;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05305388 if((nCBORError = QCBORDecode_GetNext(&DC, &Item1)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07005389 return (int32_t)nCBORError;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05305390 if((nCBORError = QCBORDecode_GetNext(&DC, &Item2)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07005391 return (int32_t)nCBORError;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05305392 if((nCBORError = QCBORDecode_GetNext(&DC, &Item3)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07005393 return (int32_t)nCBORError;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05305394 if((nCBORError = QCBORDecode_GetNext(&DC, &Item4)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07005395 return (int32_t)nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005396
Laurence Lundblade05ec57b2018-10-21 01:50:03 +05305397 UsefulBuf_Set(CopyOfStorage, '_');
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005398
Laurence Lundbladea44d5062018-10-17 18:45:12 +05305399 if(Item1.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladea44d5062018-10-17 18:45:12 +05305400 Item1.uDataType != QCBOR_TYPE_INT64 ||
5401 Item1.val.int64 != 42 ||
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08005402 Item1.uDataAlloc != 0 ||
5403 Item1.uLabelAlloc == 0 ||
Laurence Lundbladeec290b82024-06-10 11:10:54 -07005404 UsefulBufCompareToSZ(Item1.label.string, "first integer") ||
5405 Item1.label.string.ptr < Pool.ptr ||
5406 Item1.label.string.ptr > (const void *)((const uint8_t *)Pool.ptr + Pool.len)) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08005407 return -4;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09005408 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005409
Laurence Lundbladea44d5062018-10-17 18:45:12 +05305410
5411 if(Item2.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07005412 UsefulBufCompareToSZ(Item2.label.string, "an array of two strings") ||
Laurence Lundbladea44d5062018-10-17 18:45:12 +05305413 Item2.uDataType != QCBOR_TYPE_ARRAY ||
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08005414 Item2.uDataAlloc != 0 ||
5415 Item2.uLabelAlloc == 0 ||
Laurence Lundbladea44d5062018-10-17 18:45:12 +05305416 Item2.val.uCount != 2)
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08005417 return -5;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005418
Laurence Lundbladea44d5062018-10-17 18:45:12 +05305419 if(Item3.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08005420 Item3.uDataAlloc == 0 ||
5421 Item3.uLabelAlloc != 0 ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07005422 UsefulBufCompareToSZ(Item3.val.string, "string1")) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08005423 return -6;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09005424 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005425
Laurence Lundbladea44d5062018-10-17 18:45:12 +05305426 if(Item4.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08005427 Item4.uDataAlloc == 0 ||
5428 Item4.uLabelAlloc != 0 ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07005429 UsefulBufCompareToSZ(Item4.val.string, "string2")) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08005430 return -7;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09005431 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005432
Laurence Lundbladea44d5062018-10-17 18:45:12 +05305433 // Next parse with a pool that is too small
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08005434 UsefulBuf_MAKE_STACK_UB(SmallPool, QCBOR_DECODE_MIN_MEM_POOL_SIZE + 1);
Laurence Lundbladeee851742020-01-08 08:37:05 -08005435 QCBORDecode_Init(&DC,
5436 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded),
5437 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladea44d5062018-10-17 18:45:12 +05305438 QCBORDecode_SetMemPool(&DC, SmallPool, 1); // Turn on copying.
5439 if((nCBORError = QCBORDecode_GetNext(&DC, &Item1)))
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08005440 return -8;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05305441 if(Item1.uDataType != QCBOR_TYPE_MAP ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09005442 Item1.val.uCount != 3) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08005443 return -9;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09005444 }
Laurence Lundbladea44d5062018-10-17 18:45:12 +05305445 if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item1))){
5446 if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item2))) {
5447 if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item3))) {
5448 nCBORError = QCBORDecode_GetNext(&DC, &Item4);
5449 }
5450 }
5451 }
Laurence Lundblade30816f22018-11-10 13:40:22 +07005452 if(nCBORError != QCBOR_ERR_STRING_ALLOCATE) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08005453 return -10;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05305454 }
Laurence Lundbladeec290b82024-06-10 11:10:54 -07005455#endif /* ! QCBOR_DISABLE_NON_INTEGER_LABELS */
Laurence Lundbladea44d5062018-10-17 18:45:12 +05305456
5457 return 0;
5458}
5459
Laurence Lundbladef6531662018-12-04 10:42:22 +09005460
Laurence Lundbladec5fef682020-01-25 11:38:45 -08005461int32_t MemPoolTest(void)
Laurence Lundblade0155b622018-10-12 20:04:37 +08005462{
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08005463 // Set up the decoder with a tiny bit of CBOR to parse because
5464 // nothing can be done with it unless that is set up.
Laurence Lundbladef6531662018-12-04 10:42:22 +09005465 QCBORDecodeContext DC;
5466 const uint8_t pMinimalCBOR[] = {0xa0}; // One empty map
5467 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pMinimalCBOR),0);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005468
Laurence Lundbladef6531662018-12-04 10:42:22 +09005469 // Set up an memory pool of 100 bytes
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08005470 // Then fish into the internals of the decode context
5471 // to get the allocator function so it can be called directly.
5472 // Also figure out how much pool is available for use
5473 // buy subtracting out the overhead.
Laurence Lundbladef6531662018-12-04 10:42:22 +09005474 UsefulBuf_MAKE_STACK_UB(Pool, 100);
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08005475 QCBORError nError = QCBORDecode_SetMemPool(&DC, Pool, 0);
5476 if(nError) {
5477 return -9;
5478 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08005479 QCBORStringAllocate pAlloc = DC.StringAllocator.pfAllocator;
5480 void *pAllocCtx = DC.StringAllocator.pAllocateCxt;
5481 size_t uAvailPool = Pool.len - QCBOR_DECODE_MIN_MEM_POOL_SIZE;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005482
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08005483 // First test -- ask for one more byte than available and see failure
5484 UsefulBuf Allocated = (*pAlloc)(pAllocCtx, NULL, uAvailPool+1);
Laurence Lundbladef6531662018-12-04 10:42:22 +09005485 if(!UsefulBuf_IsNULL(Allocated)) {
5486 return -1;
5487 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005488
Laurence Lundbladef6531662018-12-04 10:42:22 +09005489 // Re do the set up for the next test that will do a successful alloc,
5490 // a fail, a free and then success
Laurence Lundbladef6531662018-12-04 10:42:22 +09005491 QCBORDecode_SetMemPool(&DC, Pool, 0);
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08005492 pAlloc = DC.StringAllocator.pfAllocator;
5493 pAllocCtx = DC.StringAllocator.pAllocateCxt;
5494 uAvailPool = Pool.len - QCBOR_DECODE_MIN_MEM_POOL_SIZE;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005495
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08005496 // Allocate one byte less than available and see success
5497 Allocated = (pAlloc)(pAllocCtx, NULL, uAvailPool-1);
Laurence Lundbladef6531662018-12-04 10:42:22 +09005498 if(UsefulBuf_IsNULL(Allocated)) { // expected to succeed
5499 return -2;
5500 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08005501 // Ask for some more and see failure
5502 UsefulBuf Allocated2 = (*pAlloc)(pAllocCtx, NULL, uAvailPool/2);
Laurence Lundbladef6531662018-12-04 10:42:22 +09005503 if(!UsefulBuf_IsNULL(Allocated2)) { // expected to fail
5504 return -3;
5505 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08005506 // Free the first allocate, retry the second and see success
5507 (*pAlloc)(pAllocCtx, Allocated.ptr, 0); // Free
5508 Allocated = (*pAlloc)(pAllocCtx, NULL, uAvailPool/2);
Laurence Lundbladef6531662018-12-04 10:42:22 +09005509 if(UsefulBuf_IsNULL(Allocated)) { // succeed because of the free
5510 return -4;
5511 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005512
Laurence Lundbladef6531662018-12-04 10:42:22 +09005513 // Re do set up for next test that involves a successful alloc,
5514 // and a successful realloc and a failed realloc
5515 QCBORDecode_SetMemPool(&DC, Pool, 0);
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08005516 pAlloc = DC.StringAllocator.pfAllocator;
5517 pAllocCtx = DC.StringAllocator.pAllocateCxt;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005518
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08005519 // Allocate half the pool and see success
5520 Allocated = (*pAlloc)(pAllocCtx, NULL, uAvailPool/2);
Laurence Lundbladef6531662018-12-04 10:42:22 +09005521 if(UsefulBuf_IsNULL(Allocated)) { // expected to succeed
5522 return -5;
5523 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08005524 // Reallocate to take up the whole pool and see success
5525 Allocated2 = (*pAlloc)(pAllocCtx, Allocated.ptr, uAvailPool);
Laurence Lundbladef6531662018-12-04 10:42:22 +09005526 if(UsefulBuf_IsNULL(Allocated2)) {
5527 return -6;
5528 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08005529 // Make sure its the same pointer and the size is right
Laurence Lundbladef6531662018-12-04 10:42:22 +09005530 if(Allocated2.ptr != Allocated.ptr || Allocated2.len != uAvailPool) {
5531 return -7;
5532 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08005533 // Try to allocate more to be sure there is failure after a realloc
5534 UsefulBuf Allocated3 = (*pAlloc)(pAllocCtx, Allocated.ptr, uAvailPool+1);
5535 if(!UsefulBuf_IsNULL(Allocated3)) {
Laurence Lundbladef6531662018-12-04 10:42:22 +09005536 return -8;
5537 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005538
Laurence Lundbladef6531662018-12-04 10:42:22 +09005539 return 0;
5540}
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08005541
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08005542
5543/* Just enough of an allocator to test configuration of one */
5544static UsefulBuf AllocateTestFunction(void *pCtx, void *pOldMem, size_t uNewSize)
5545{
5546 (void)pOldMem; // unused variable
5547
5548 if(uNewSize) {
5549 // Assumes the context pointer is the buffer and
5550 // nothing too big will ever be asked for.
5551 // This is only good for this basic test!
5552 return (UsefulBuf) {pCtx, uNewSize};
5553 } else {
5554 return NULLUsefulBuf;
5555 }
5556}
5557
5558
Laurence Lundbladec5fef682020-01-25 11:38:45 -08005559int32_t SetUpAllocatorTest(void)
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08005560{
5561 // Set up the decoder with a tiny bit of CBOR to parse because
5562 // nothing can be done with it unless that is set up.
5563 QCBORDecodeContext DC;
5564 const uint8_t pMinimalCBOR[] = {0x62, 0x48, 0x69}; // "Hi"
5565 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pMinimalCBOR),0);
5566
5567 uint8_t pAllocatorBuffer[50];
5568
5569 // This is really just to test that this call works.
5570 // The full functionality of string allocators is tested
5571 // elsewhere with the MemPool internal allocator.
5572 QCBORDecode_SetUpAllocator(&DC, AllocateTestFunction, pAllocatorBuffer, 1);
5573
5574 QCBORItem Item;
5575 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_SUCCESS) {
5576 return -1;
5577 }
5578
5579 if(Item.uDataAlloc == 0 ||
5580 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
5581 Item.val.string.ptr != pAllocatorBuffer) {
5582 return -2;
5583 }
5584
5585 if(QCBORDecode_Finish(&DC) != QCBOR_SUCCESS) {
5586 return -3;
5587 }
5588
5589 return 0;
5590}
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08005591#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
5592
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08005593
Laurence Lundbladedd6e76e2021-03-10 01:54:01 -07005594#ifndef QCBOR_DISABLE_EXP_AND_MANTISSA
Laurence Lundblade59289e52019-12-30 13:44:37 -08005595
Laurence Lundblade37286c02022-09-03 10:05:02 -07005596struct EaMTest {
5597 const char *szName;
5598 UsefulBufC Input;
5599 uint8_t uTagRequirement;
5600 bool bHasTags;
5601
5602 /* Expected values for GetNext */
5603 QCBORError uExpectedErrorGN;
5604 uint8_t uQCBORTypeGN;
5605 int64_t nExponentGN;
5606 int64_t nMantissaGN;
5607 UsefulBufC MantissaGN;
5608
5609 /* Expected values for GetDecimalFraction */
5610 QCBORError uExpectedErrorGDF;
5611 int64_t nExponentGDF;
5612 int64_t nMantissaGDF;
5613
5614 /* Expected values for GetDecimalFractionBig */
5615 QCBORError uExpectedErrorGDFB;
5616 int64_t nExponentGDFB;
5617 UsefulBufC MantissaGDFB;
5618 bool IsNegativeGDFB;
5619
5620 /* Expected values for GetBigFloat */
5621 QCBORError uExpectedErrorGBF;
5622 int64_t nExponentGBF;
5623 int64_t nMantissaGBF;
5624
5625 /* Expected values for GetBigFloatBig */
5626 QCBORError uExpectedErrorGBFB;
5627 int64_t nExponentGBFB;
5628 UsefulBufC MantissaGBFB;
5629 bool IsNegativeGBFB;
Laurence Lundblade59289e52019-12-30 13:44:37 -08005630};
5631
Laurence Lundbladefaec39f2020-08-02 21:53:53 -07005632
Laurence Lundblade37286c02022-09-03 10:05:02 -07005633
5634static const struct EaMTest pEaMTests[] = {
5635 {
5636 "1. Untagged pair (big float or decimal fraction), no tag required",
5637 {(const uint8_t []){0x82, 0x20, 0x03}, 3},
5638 QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
5639 false,
5640
5641 QCBOR_SUCCESS, /* for GetNext */
5642 QCBOR_TYPE_ARRAY,
5643 0,
5644 0,
5645 {(const uint8_t []){0x00}, 1},
5646
5647 QCBOR_SUCCESS, /* GetDecimalFraction */
5648 -1,
5649 3,
5650
5651 QCBOR_SUCCESS, /* for GetDecimalFractionBig */
5652 -1,
5653 {(const uint8_t []){0x03}, 1},
5654 false,
5655
5656 QCBOR_SUCCESS, /* for GetBigFloat */
5657 -1,
5658 3,
5659
5660 QCBOR_SUCCESS, /* for GetBigFloatBig */
5661 -1,
5662 {(const uint8_t []){0x03}, 1},
5663 false
5664 },
5665
5666 {
5667 "2. Untagged pair (big float or decimal fraction), tag required",
5668 {(const uint8_t []){0x82, 0x20, 0x03}, 3},
5669 QCBOR_TAG_REQUIREMENT_TAG,
5670 false,
5671
5672 QCBOR_SUCCESS, /* for GetNext */
5673 QCBOR_TYPE_ARRAY,
5674 0,
5675 0,
5676 {(const uint8_t []){0x00}, 1},
5677
5678 QCBOR_ERR_UNEXPECTED_TYPE, /* for GetDecimalFraction */
5679 0,
5680 0,
5681
5682 QCBOR_ERR_UNEXPECTED_TYPE, /* for GetDecimalFractionBig */
5683 0,
5684 {(const uint8_t []){0x00}, 1},
5685 false,
5686
5687 QCBOR_ERR_UNEXPECTED_TYPE, /* for GetBigFloat */
5688 0,
5689 0,
5690
5691 QCBOR_ERR_UNEXPECTED_TYPE, /* for GetBigFloatBig */
5692 0,
5693 {(const uint8_t []){0x00}, 1},
5694 false
5695
5696 },
5697
5698 {
5699 "3. Tagged 1.5 decimal fraction, tag 4 optional",
5700 {(const uint8_t []){0xC4, 0x82, 0x20, 0x03}, 4},
5701 QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
5702 true,
5703
5704 QCBOR_SUCCESS, /* for GetNext */
5705 QCBOR_TYPE_DECIMAL_FRACTION,
5706 -1,
5707 3,
5708 {(const uint8_t []){0x00}, 1},
5709
5710
5711 QCBOR_SUCCESS, /* for GetDecimalFraction */
5712 -1,
5713 3,
5714
5715 QCBOR_SUCCESS, /* for GetDecimalFractionBig */
5716 -1,
5717 {(const uint8_t []){0x03}, 1},
5718 false,
5719
5720 QCBOR_ERR_UNEXPECTED_TYPE, /* for GetBigFloat */
5721 0,
5722 0,
5723
5724 QCBOR_ERR_UNEXPECTED_TYPE, /* for GetBigFloatBig */
5725 0,
5726 {(const uint8_t []){0x00}, 1},
5727 false
5728 },
5729 {
5730 "4. Tagged 100 * 2^300 big float, tag 5 optional",
5731 {(const uint8_t []){0xC5, 0x82, 0x19, 0x01, 0x2C, 0x18, 0x64}, 7},
5732 QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
5733 true,
5734
5735 QCBOR_SUCCESS, /* for GetNext */
5736 QCBOR_TYPE_BIGFLOAT,
5737 300,
5738 100,
5739 {(const uint8_t []){0x00}, 1},
5740
5741
5742 QCBOR_ERR_UNEXPECTED_TYPE, /* for GetDecimalFraction */
5743 0,
5744 0,
5745
5746 QCBOR_ERR_UNEXPECTED_TYPE, /* for GetDecimalFractionBig */
5747 0,
5748 {(const uint8_t []){0x03}, 1},
5749 false,
5750
5751 QCBOR_SUCCESS, /* for GetBigFloat */
5752 300,
5753 100,
5754
5755 QCBOR_SUCCESS, /* for GetBigFloatBig */
5756 300,
5757 {(const uint8_t []){0x64}, 1},
5758 false
5759 },
5760
5761 {
5762 "5. Tagged 4([-20, 4759477275222530853136]) decimal fraction, tag 4 required",
5763 {(const uint8_t []){0xC4, 0x82, 0x33,
5764 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,}, 15},
5765 QCBOR_TAG_REQUIREMENT_TAG,
5766 true,
5767
5768 QCBOR_SUCCESS, /* for GetNext */
5769 QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM,
5770 -20,
5771 0,
5772 {(const uint8_t []){0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10}, 10},
5773
5774 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW, /* for GetDecimalFraction */
5775 0,
5776 0,
5777
5778 QCBOR_SUCCESS, /* for GetDecimalFractionBig */
5779 -20,
5780 {(const uint8_t []){0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10}, 10},
5781 false,
5782
5783 QCBOR_ERR_UNEXPECTED_TYPE, /* for GetBigFloat */
5784 0,
5785 0,
5786
5787 QCBOR_ERR_UNEXPECTED_TYPE, /* for GetBigFloatBig */
5788 0,
5789 {(const uint8_t []){0x00}, 0},
5790 false
5791 },
5792
5793 {
5794 "6. Error: Mantissa and exponent inside a Mantissa and exponent",
5795 {(const uint8_t []){0xC4, 0x82, 0x33,
5796 0xC5, 0x82, 0x19, 0x01, 0x2C, 0x18, 0x64}, 10},
5797 QCBOR_TAG_REQUIREMENT_TAG,
5798 true,
5799
5800 QCBOR_ERR_BAD_EXP_AND_MANTISSA, /* for GetNext */
5801 QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM,
5802 0,
5803 0,
5804 {(const uint8_t []){0x00}, 0},
5805
5806 QCBOR_ERR_BAD_EXP_AND_MANTISSA, /* for GetDecimalFraction */
5807 0,
5808 0,
5809
5810 QCBOR_ERR_BAD_EXP_AND_MANTISSA, /* for GetDecimalFractionBig */
5811 0,
5812 {(const uint8_t []){0x00}, 0},
5813 false,
5814
5815 QCBOR_ERR_BAD_EXP_AND_MANTISSA, /* for GetBigFloat */
5816 0,
5817 0,
5818
5819 QCBOR_ERR_BAD_EXP_AND_MANTISSA, /* for GetBigFloatBig */
5820 0,
5821 {(const uint8_t []){0x00}, 0},
5822 false
5823 },
5824 {
5825 "7. Tagged 5([-20, 4294967295]) big float, big num mantissa, tag 5 required",
5826 {(const uint8_t []){0xC5, 0x82, 0x33,
5827 0xC2, 0x44, 0xff, 0xff, 0xff, 0xff}, 9},
5828 QCBOR_TAG_REQUIREMENT_TAG,
5829 true,
5830
5831 QCBOR_SUCCESS, /* for GetNext */
5832 QCBOR_TYPE_BIGFLOAT_POS_BIGNUM,
5833 -20,
5834 0,
5835 {(const uint8_t []){0xff, 0xff, 0xff, 0xff}, 4},
5836
5837 QCBOR_ERR_UNEXPECTED_TYPE, /* for GetDecimalFraction */
5838 0,
5839 0,
5840
5841 QCBOR_ERR_UNEXPECTED_TYPE, /* for GetDecimalFractionBig */
5842 -20,
5843 {(const uint8_t []){0x00}, 1},
5844 false,
5845
5846 QCBOR_SUCCESS, /* for GetBigFloat */
5847 -20,
5848 4294967295,
5849
5850 QCBOR_SUCCESS, /* for GetBigFloatBig */
5851 -20,
5852 {(const uint8_t []){0xff, 0xff, 0xff, 0xff}, 4},
5853 false
5854 },
5855
5856 {
5857 /* Special case for test 8. Don't renumber it. */
5858 "8. Untagged pair with big num (big float or decimal fraction), tag optional",
5859 {(const uint8_t []){0x82, 0x33, 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10}, 14},
5860 QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
5861 true,
5862
5863 QCBOR_SUCCESS, /* for GetNext */
5864 QCBOR_TYPE_ARRAY,
5865 0,
5866 0,
5867 {(const uint8_t []){0x00}, 1},
5868
5869 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW, /* GetDecimalFraction */
5870 0,
5871 0,
5872
5873 QCBOR_SUCCESS, /* for GetDecimalFractionBig */
5874 -20,
5875 {(const uint8_t []){0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10}, 10},
5876 false,
5877
5878 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW, /* for GetBigFloat */
5879 0,
5880 0,
5881
5882 QCBOR_SUCCESS, /* for GetBigFloatBig */
5883 -20,
5884 {(const uint8_t []){0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10}, 10},
5885 false
5886 },
5887
5888 {
5889 "9. decimal fraction with large exponent and negative big num mantissa",
5890 {(const uint8_t []){0xC4, 0x82, 0x1B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
5891 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10}, 23},
5892 QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
5893 true,
5894
5895 QCBOR_SUCCESS, /* for GetNext */
5896 QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM,
5897 9223372036854775807,
5898 0,
5899 {(const uint8_t []){0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10}, 10},
5900
5901 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW, /* GetDecimalFraction */
5902 0,
5903 0,
5904
5905 QCBOR_SUCCESS, /* for GetDecimalFractionBig */
5906 9223372036854775807,
5907 {(const uint8_t []){0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10}, 10},
5908 true,
5909
5910 QCBOR_ERR_UNEXPECTED_TYPE, /* for GetBigFloat */
5911 0,
5912 0,
5913
5914 QCBOR_ERR_UNEXPECTED_TYPE, /* for GetBigFloatBig */
5915 0,
5916 {(const uint8_t []){0x00}, 1},
5917 false
5918 },
5919};
5920
5921
5922
5923int32_t ProcessEaMTests(void)
Laurence Lundblade59289e52019-12-30 13:44:37 -08005924{
Laurence Lundblade37286c02022-09-03 10:05:02 -07005925 size_t uIndex;
5926 QCBORDecodeContext DCtx;
5927 QCBORItem Item;
5928 QCBORError uError;
5929 int64_t nMantissa, nExponent;
5930 MakeUsefulBufOnStack( MantissaBuf, 200);
5931 UsefulBufC Mantissa;
5932 bool bMantissaIsNegative;
5933
5934 for(uIndex = 0; uIndex < C_ARRAY_COUNT(pEaMTests, struct EaMTest); uIndex++) {
5935 const struct EaMTest *pT = &pEaMTests[uIndex];
5936 /* Decode with GetNext */
5937 QCBORDecode_Init(&DCtx, pT->Input, 0);
5938
5939 if(uIndex + 1 == 9) {
5940 nExponent = 99; // just to set a break point
5941 }
5942
5943 uError = QCBORDecode_GetNext(&DCtx, &Item);
5944#ifdef QCBOR_DISABLE_TAGS
5945 /* Test 8 is a special case when tags are disabled */
5946 if(pT->bHasTags && uIndex + 1 != 8) {
5947 if(uError != QCBOR_ERR_TAGS_DISABLED) {
5948 return (int32_t)(1+uIndex) * 1000 + 9;
5949 }
5950 } else {
5951#endif
5952 /* Now check return code, data type, mantissa and exponent */
5953 if(pT->uExpectedErrorGN != uError) {
5954 return (int32_t)(1+uIndex) * 1000 + 1;
5955 }
5956 if(uError == QCBOR_SUCCESS && pT->uQCBORTypeGN != QCBOR_TYPE_ARRAY) {
5957 if(pT->uQCBORTypeGN != Item.uDataType) {
5958 return (int32_t)(1+uIndex) * 1000 + 2;
5959 }
5960 if(pT->nExponentGN != Item.val.expAndMantissa.nExponent) {
5961 return (int32_t)(1+uIndex) * 1000 + 3;
5962 }
5963 if(Item.uDataType == QCBOR_TYPE_DECIMAL_FRACTION || Item.uDataType == QCBOR_TYPE_BIGFLOAT ) {
5964 if(pT->nMantissaGN != Item.val.expAndMantissa.Mantissa.nInt) {
5965 return (int32_t)(1+uIndex) * 1000 + 4;
5966 }
5967 } else {
5968 if(UsefulBuf_Compare(Item.val.expAndMantissa.Mantissa.bigNum, pT->MantissaGN)) {
5969 return (int32_t)(1+uIndex) * 1000 + 5;
5970 }
5971 }
5972 }
5973#ifdef QCBOR_DISABLE_TAGS
5974 }
5975#endif
5976
5977 /* Decode with GetDecimalFraction */
5978 QCBORDecode_Init(&DCtx, pT->Input, 0);
5979 QCBORDecode_GetDecimalFraction(&DCtx,
5980 pT->uTagRequirement,
5981 &nMantissa,
5982 &nExponent);
5983 uError = QCBORDecode_GetAndResetError(&DCtx);
5984#ifdef QCBOR_DISABLE_TAGS
5985 if(pT->bHasTags) {
5986 if(uError != QCBOR_ERR_TAGS_DISABLED) {
5987 return (int32_t)(1+uIndex) * 1000 + 39;
5988 }
5989 } else {
5990#endif
5991 /* Now check return code, mantissa and exponent */
5992 if(pT->uExpectedErrorGDF != uError) {
5993 return (int32_t)(1+uIndex) * 1000 + 31;
5994 }
5995 if(uError == QCBOR_SUCCESS) {
5996 if(pT->nExponentGDF != nExponent) {
5997 return (int32_t)(1+uIndex) * 1000 + 32;
5998 }
5999 if(pT->nMantissaGDF != nMantissa) {
6000 return (int32_t)(1+uIndex) * 1000 + 33;
6001 }
6002 }
6003#ifdef QCBOR_DISABLE_TAGS
6004 }
6005#endif
6006
6007 /* Decode with GetDecimalFractionBig */
6008 QCBORDecode_Init(&DCtx, pT->Input, 0);
6009 QCBORDecode_GetDecimalFractionBig(&DCtx,
6010 pT->uTagRequirement,
6011 MantissaBuf,
6012 &Mantissa,
6013 &bMantissaIsNegative,
6014 &nExponent);
6015 uError = QCBORDecode_GetAndResetError(&DCtx);
6016#ifdef QCBOR_DISABLE_TAGS
6017 if(pT->bHasTags) {
6018 if(uError != QCBOR_ERR_TAGS_DISABLED) {
6019 return (int32_t)(1+uIndex) * 1000 + 49;
6020 }
6021 } else {
6022#endif
6023 /* Now check return code, mantissa (bytes and sign) and exponent */
6024 if(pT->uExpectedErrorGDFB != uError) {
6025 return (int32_t)(1+uIndex) * 1000 + 41;
6026 }
6027 if(uError == QCBOR_SUCCESS) {
6028 if(pT->nExponentGDFB != nExponent) {
6029 return (int32_t)(1+uIndex) * 1000 + 42;
6030 }
6031 if(pT->IsNegativeGDFB != bMantissaIsNegative) {
6032 return (int32_t)(1+uIndex) * 1000 + 43;
6033 }
6034 if(UsefulBuf_Compare(Mantissa, pT->MantissaGDFB)) {
6035 return (int32_t)(1+uIndex) * 1000 + 44;
6036 }
6037 }
6038#ifdef QCBOR_DISABLE_TAGS
6039 }
6040#endif
6041
6042 /* Decode with GetBigFloat */
6043 QCBORDecode_Init(&DCtx, pT->Input, 0);
6044 QCBORDecode_GetBigFloat(&DCtx,
6045 pT->uTagRequirement,
6046 &nMantissa,
6047 &nExponent);
6048 uError = QCBORDecode_GetAndResetError(&DCtx);
6049#ifdef QCBOR_DISABLE_TAGS
6050 if(pT->bHasTags) {
6051 if(uError != QCBOR_ERR_TAGS_DISABLED) {
6052 return (int32_t)(1+uIndex) * 1000 + 19;
6053 }
6054 } else {
6055#endif
6056 /* Now check return code, mantissa and exponent */
6057 if(pT->uExpectedErrorGBF != uError) {
6058 return (int32_t)(1+uIndex) * 1000 + 11;
6059 }
6060 if(uError == QCBOR_SUCCESS) {
6061 if(pT->nExponentGBF != nExponent) {
6062 return (int32_t)(1+uIndex) * 1000 + 12;
6063 }
6064 if(pT->nMantissaGBF != nMantissa) {
6065 return (int32_t)(1+uIndex) * 1000 + 13;
6066 }
6067 }
6068#ifdef QCBOR_DISABLE_TAGS
6069 }
6070#endif
6071
6072 /* Decode with GetBigFloatBig */
6073 QCBORDecode_Init(&DCtx, pT->Input, 0);
6074 QCBORDecode_GetBigFloatBig(&DCtx,
6075 pT->uTagRequirement,
6076 MantissaBuf,
6077 &Mantissa,
6078 &bMantissaIsNegative,
6079 &nExponent);
6080 uError = QCBORDecode_GetAndResetError(&DCtx);
6081#ifdef QCBOR_DISABLE_TAGS
6082 if(pT->bHasTags) {
6083 if(uError != QCBOR_ERR_TAGS_DISABLED) {
6084 return (int32_t)(1+uIndex) * 1000 + 29;
6085 }
6086 } else {
6087#endif
6088 /* Now check return code, mantissa (bytes and sign) and exponent */
6089 if(pT->uExpectedErrorGBFB != uError) {
6090 return (int32_t)(1+uIndex) * 1000 + 21;
6091 }
6092 if(uError == QCBOR_SUCCESS) {
6093 if(pT->nExponentGBFB != nExponent) {
6094 return (int32_t)(1+uIndex) * 1000 + 22;
6095 }
6096 if(pT->IsNegativeGBFB != bMantissaIsNegative) {
6097 return (int32_t)(1+uIndex) * 1000 + 23;
6098 }
6099 if(UsefulBuf_Compare(Mantissa, pT->MantissaGBFB)) {
6100 return (int32_t)(1+uIndex) * 1000 + 24;
6101 }
6102 }
6103#ifdef QCBOR_DISABLE_TAGS
6104 }
6105#endif
6106 }
6107
6108 return 0;
6109}
6110
6111
6112int32_t ExponentAndMantissaDecodeTestsSecondary(void)
6113{
6114#ifndef QCBOR_DISABLE_TAGS
Laurence Lundblade59289e52019-12-30 13:44:37 -08006115 QCBORDecodeContext DC;
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07006116 QCBORError uErr;
Laurence Lundblade59289e52019-12-30 13:44:37 -08006117 QCBORItem item;
6118
Laurence Lundblade17af4902020-01-07 19:11:55 -08006119 static const uint8_t spBigNumMantissa[] = {0x01, 0x02, 0x03, 0x04, 0x05,
6120 0x06, 0x07, 0x08, 0x09, 0x010};
6121 UsefulBufC BN = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNumMantissa);
Laurence Lundblade59289e52019-12-30 13:44:37 -08006122
6123
Laurence Lundblade59289e52019-12-30 13:44:37 -08006124
6125 /* Now encode some stuff and then decode it */
6126 uint8_t pBuf[40];
6127 QCBOREncodeContext EC;
6128 UsefulBufC Encoded;
6129
6130 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(pBuf));
6131 QCBOREncode_OpenArray(&EC);
6132 QCBOREncode_AddDecimalFraction(&EC, 999, 1000); // 999 * (10 ^ 1000)
6133 QCBOREncode_AddBigFloat(&EC, 100, INT32_MIN);
6134 QCBOREncode_AddDecimalFractionBigNum(&EC, BN, false, INT32_MAX);
6135 QCBOREncode_CloseArray(&EC);
6136 QCBOREncode_Finish(&EC, &Encoded);
6137
6138
6139 QCBORDecode_Init(&DC, Encoded, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07006140 uErr = QCBORDecode_GetNext(&DC, &item);
6141 if(uErr != QCBOR_SUCCESS) {
6142 return 100;
Laurence Lundblade59289e52019-12-30 13:44:37 -08006143 }
6144
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07006145 uErr = QCBORDecode_GetNext(&DC, &item);
6146 if(uErr != QCBOR_SUCCESS) {
6147 return 101;
Laurence Lundblade59289e52019-12-30 13:44:37 -08006148 }
6149
6150 if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION ||
6151 item.val.expAndMantissa.nExponent != 1000 ||
6152 item.val.expAndMantissa.Mantissa.nInt != 999) {
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07006153 return 102;
Laurence Lundblade59289e52019-12-30 13:44:37 -08006154 }
6155
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07006156 uErr = QCBORDecode_GetNext(&DC, &item);
6157 if(uErr != QCBOR_SUCCESS) {
6158 return 103;
Laurence Lundblade59289e52019-12-30 13:44:37 -08006159 }
6160
6161 if(item.uDataType != QCBOR_TYPE_BIGFLOAT ||
6162 item.val.expAndMantissa.nExponent != INT32_MIN ||
6163 item.val.expAndMantissa.Mantissa.nInt != 100) {
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07006164 return 104;
Laurence Lundblade59289e52019-12-30 13:44:37 -08006165 }
6166
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07006167 uErr = QCBORDecode_GetNext(&DC, &item);
6168 if(uErr != QCBOR_SUCCESS) {
6169 return 105;
Laurence Lundblade59289e52019-12-30 13:44:37 -08006170 }
6171
6172 if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM ||
6173 item.val.expAndMantissa.nExponent != INT32_MAX ||
6174 UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) {
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07006175 return 106;
6176 }
6177
Laurence Lundblade37286c02022-09-03 10:05:02 -07006178#endif /* QCBOR_TAGS_DISABLED */
Laurence Lundblade59289e52019-12-30 13:44:37 -08006179
6180 return 0;
6181}
6182
6183
Laurence Lundblade37286c02022-09-03 10:05:02 -07006184int32_t ExponentAndMantissaDecodeTests(void)
6185{
6186 int32_t rv = ProcessEaMTests();
6187 if(rv) {
6188 return rv;
6189 }
6190
6191 return ExponentAndMantissaDecodeTestsSecondary();
6192}
6193
6194
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08006195static const struct DecodeFailTestInput ExponentAndMantissaFailures[] = {
6196 { "Exponent > INT64_MAX",
6197 QCBOR_DECODE_MODE_NORMAL,
6198 {"\xC4\x82\x1B\x7f\xFF\xFF\xFF\xFF\xFF\xFF\xFF\x1B\x80\xFF\xFF\xFF\xFF\xFF\xFF\xFF", 20},
6199 QCBOR_ERR_BAD_EXP_AND_MANTISSA
6200 },
6201 { "Mantissa > INT64_MAX",
6202 QCBOR_DECODE_MODE_NORMAL,
6203 {"\xC4\x82\x1B\x80\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xC3\x4A\x01\x02\x03\x04\x05\x06\x07\x08\x09\x10", 23},
6204 QCBOR_ERR_BAD_EXP_AND_MANTISSA
6205 },
6206 {
6207 "End of input",
6208 QCBOR_DECODE_MODE_NORMAL,
6209 {"\xC4\x82", 2},
6210 QCBOR_ERR_NO_MORE_ITEMS
6211 },
6212 {"bad content for big num",
6213 QCBOR_DECODE_MODE_NORMAL,
6214 {"\xC4\x82\x01\xc3\x01", 5},
6215 QCBOR_ERR_BAD_OPT_TAG
6216 },
6217 {"bad content for big num",
6218 QCBOR_DECODE_MODE_NORMAL,
6219 {"\xC4\x82\xc2\x01\x1f", 5},
6220 QCBOR_ERR_BAD_INT
6221 },
6222 {"Bad integer for exponent",
6223 QCBOR_DECODE_MODE_NORMAL,
6224 {"\xC4\x82\x01\x1f", 4},
6225 QCBOR_ERR_BAD_INT
6226 },
6227 {"Bad integer for mantissa",
6228 QCBOR_DECODE_MODE_NORMAL,
6229 {"\xC4\x82\x1f\x01", 4},
6230 QCBOR_ERR_BAD_INT
6231 },
6232 {"3 items in array",
6233 QCBOR_DECODE_MODE_NORMAL,
6234 {"\xC4\x83\x03\x01\x02", 5},
6235 QCBOR_ERR_BAD_EXP_AND_MANTISSA},
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08006236#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08006237 {"unterminated indefinite length array",
6238 QCBOR_DECODE_MODE_NORMAL,
6239 {"\xC4\x9f\x03\x01\x02", 5},
6240 QCBOR_ERR_BAD_EXP_AND_MANTISSA
6241 },
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08006242#else /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08006243 {"unterminated indefinite length array",
6244 QCBOR_DECODE_MODE_NORMAL,
6245 {"\xC4\x9f\x03\x01\x02", 5},
6246 QCBOR_ERR_INDEF_LEN_ARRAYS_DISABLED
6247 },
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08006248#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08006249 {"Empty array",
6250 QCBOR_DECODE_MODE_NORMAL,
6251 {"\xC4\x80", 2},
6252 QCBOR_ERR_NO_MORE_ITEMS
6253 },
6254 {"Second is not an integer",
6255 QCBOR_DECODE_MODE_NORMAL,
6256 {"\xC4\x82\x03\x40", 4},
6257 QCBOR_ERR_BAD_EXP_AND_MANTISSA
6258 },
6259 {"First is not an integer",
6260 QCBOR_DECODE_MODE_NORMAL,
6261 {"\xC4\x82\x40", 3},
6262 QCBOR_ERR_BAD_EXP_AND_MANTISSA
6263 },
6264 {"Not an array",
6265 QCBOR_DECODE_MODE_NORMAL,
6266 {"\xC4\xA2", 2},
6267 QCBOR_ERR_BAD_EXP_AND_MANTISSA
6268 }
Laurence Lundblade59289e52019-12-30 13:44:37 -08006269};
6270
6271
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08006272int32_t
6273ExponentAndMantissaDecodeFailTests(void)
Laurence Lundblade59289e52019-12-30 13:44:37 -08006274{
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08006275 return ProcessDecodeFailures(ExponentAndMantissaFailures,
6276 C_ARRAY_COUNT(ExponentAndMantissaFailures,
6277 struct DecodeFailTestInput));
Laurence Lundblade59289e52019-12-30 13:44:37 -08006278}
6279
Laurence Lundbladedd6e76e2021-03-10 01:54:01 -07006280#endif /* QCBOR_DISABLE_EXP_AND_MANTISSA */
Laurence Lundbladebb87be22020-04-09 19:15:32 -07006281
6282
6283
6284/*
6285 Some basic CBOR with map and array used in a lot of tests.
6286 The map labels are all strings
6287
Laurence Lundblade8ffdb742020-05-07 02:49:18 -07006288 {
6289 "first integer": 42,
Laurence Lundbladebb87be22020-04-09 19:15:32 -07006290 "an array of two strings": [
6291 "string1", "string2"
6292 ],
6293 "map in a map": {
6294 "bytes 1": h'78787878',
6295 "bytes 2": h'79797979',
6296 "another int": 98,
6297 "text 2": "lies, damn lies and statistics"
6298 }
Laurence Lundbladeddebabe2020-11-22 11:32:17 -08006299 }
Laurence Lundbladebb87be22020-04-09 19:15:32 -07006300 */
Laurence Lundblade9b334962020-08-27 10:55:53 -07006301
Laurence Lundbladef7a70bc2020-10-24 12:23:25 -07006302int32_t SpiffyDecodeBasicMap(UsefulBufC input)
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07006303{
6304 QCBORItem Item1, Item2, Item3;
6305 int64_t nDecodedInt1, nDecodedInt2;
6306 UsefulBufC B1, B2, S1, S2, S3;
6307
6308 QCBORDecodeContext DCtx;
6309 QCBORError nCBORError;
6310
6311 QCBORDecode_Init(&DCtx, input, 0);
6312
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006313 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07006314
6315 QCBORDecode_GetInt64InMapSZ(&DCtx, "first integer", &nDecodedInt1);
6316
6317 QCBORDecode_EnterMapFromMapSZ(&DCtx, "map in a map");
6318 QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2);
Laurence Lundblade323f8a92020-09-06 19:43:09 -07006319 QCBORDecode_GetByteStringInMapSZ(&DCtx, "bytes 1", &B1);
6320 QCBORDecode_GetByteStringInMapSZ(&DCtx, "bytes 2", &B2);
6321 QCBORDecode_GetTextStringInMapSZ(&DCtx, "text 2", &S1);
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07006322 QCBORDecode_ExitMap(&DCtx);
6323
6324 QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings");
6325 QCBORDecode_GetNext(&DCtx, &Item1);
6326 QCBORDecode_GetNext(&DCtx, &Item2);
6327 if(QCBORDecode_GetNext(&DCtx, &Item3) != QCBOR_ERR_NO_MORE_ITEMS) {
6328 return -400;
6329 }
6330 QCBORDecode_ExitArray(&DCtx);
6331
6332 // Parse the same array again using GetText() instead of GetItem()
6333 QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings");
Laurence Lundblade323f8a92020-09-06 19:43:09 -07006334 QCBORDecode_GetTextString(&DCtx, &S2);
6335 QCBORDecode_GetTextString(&DCtx, &S3);
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07006336 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) {
6337 return 5000;
6338 }
6339 /* QCBORDecode_GetText(&DCtx, &S3);
6340 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_NO_MORE_ITEMS) {
6341 return 5001;
6342 } */
6343
6344 QCBORDecode_ExitArray(&DCtx);
6345
6346 QCBORDecode_ExitMap(&DCtx);
6347
6348 nCBORError = QCBORDecode_Finish(&DCtx);
6349
6350 if(nCBORError) {
6351 return (int32_t)nCBORError;
6352 }
6353
6354 if(nDecodedInt1 != 42) {
6355 return 1001;
6356 }
6357
6358 if(nDecodedInt2 != 98) {
6359 return 1002;
6360 }
6361
6362 if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07006363 UsefulBufCompareToSZ(Item1.val.string, "string1")) {
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07006364 return 1003;
6365 }
6366
6367 if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07006368 UsefulBufCompareToSZ(Item2.val.string, "string2")) {
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07006369 return 1004;
6370 }
6371
Laurence Lundblade9b334962020-08-27 10:55:53 -07006372 if(UsefulBufCompareToSZ(S1, "lies, damn lies and statistics")) {
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07006373 return 1005;
6374 }
6375
6376 if(UsefulBuf_Compare(B1, UsefulBuf_FromSZ("xxxx"))){
6377 return 1006;
6378 }
6379
6380 if(UsefulBuf_Compare(B2, UsefulBuf_FromSZ("yyyy"))){
6381 return 1007;
6382 }
6383
6384 if(UsefulBuf_Compare(S2, UsefulBuf_FromSZ("string1"))){
6385 return 1008;
6386 }
6387
6388 if(UsefulBuf_Compare(S3, UsefulBuf_FromSZ("string2"))){
6389 return 1009;
6390 }
6391
6392 return 0;
6393}
6394
Laurence Lundbladeddebabe2020-11-22 11:32:17 -08006395/*
Laurence Lundbladecc7da412020-12-27 00:09:07 -08006396 {
6397 -75008: h'05083399',
6398 88: [],
6399 100100: {
6400 "sub1": {
6401 10: [
6402 0
6403 ],
6404 -75009: h'A46823990001',
6405 100100: {
6406 "json": "{ \"ueid\", \"xyz\"}",
6407 "subsub": {
6408 100002: h'141813191001'
6409 }
6410 }
Laurence Lundbladeddebabe2020-11-22 11:32:17 -08006411 }
Laurence Lundbladecc7da412020-12-27 00:09:07 -08006412 }
Laurence Lundbladeddebabe2020-11-22 11:32:17 -08006413 }
Laurence Lundbladeddebabe2020-11-22 11:32:17 -08006414 */
6415
6416static const uint8_t spNestedCBOR[] = {
Laurence Lundbladecc7da412020-12-27 00:09:07 -08006417 0xa3, 0x3a, 0x00, 0x01, 0x24, 0xff, 0x44, 0x05,
6418 0x08, 0x33, 0x99, 0x18, 0x58, 0x80, 0x1a, 0x00,
6419 0x01, 0x87, 0x04, 0xa1, 0x64, 0x73, 0x75, 0x62,
6420 0x31, 0xa3, 0x0a, 0x81, 0x00, 0x3a, 0x00, 0x01,
6421 0x25, 0x00, 0x46, 0xa4, 0x68, 0x23, 0x99, 0x00,
6422 0x01, 0x1a, 0x00, 0x01, 0x87, 0x04, 0xa2, 0x64,
6423 0x6a, 0x73, 0x6f, 0x6e, 0x70, 0x7b, 0x20, 0x22,
6424 0x75, 0x65, 0x69, 0x64, 0x22, 0x2c, 0x20, 0x22,
6425 0x78, 0x79, 0x7a, 0x22, 0x7d, 0x66, 0x73, 0x75,
6426 0x62, 0x73, 0x75, 0x62, 0xa1, 0x1a, 0x00, 0x01,
6427 0x86, 0xa2, 0x46, 0x14, 0x18, 0x13, 0x19, 0x10,
6428 0x01
Laurence Lundbladeddebabe2020-11-22 11:32:17 -08006429};
6430
6431/* Get item in multi-level nesting in spNestedCBOR */
6432static int32_t DecodeNestedGetSubSub(QCBORDecodeContext *pDCtx)
6433{
6434 UsefulBufC String;
6435
6436 uint8_t test_oemid_bytes[] = {0x14, 0x18, 0x13, 0x19, 0x10, 0x01};
6437 const struct q_useful_buf_c test_oemid = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(test_oemid_bytes);
6438
6439 QCBORDecode_EnterMapFromMapN(pDCtx, 100100);
6440 QCBORDecode_EnterMap(pDCtx, NULL);
6441 QCBORDecode_EnterMapFromMapN(pDCtx, 100100);
6442 QCBORDecode_EnterMapFromMapSZ(pDCtx, "subsub");
6443 QCBORDecode_GetByteStringInMapN(pDCtx, 100002, &String);
6444 if(QCBORDecode_GetError(pDCtx)) {
6445 return 4001;
6446 }
6447 if(UsefulBuf_Compare(String, test_oemid)) {
6448 return 4002;
6449 }
6450 QCBORDecode_ExitMap(pDCtx);
6451 QCBORDecode_ExitMap(pDCtx);
6452 QCBORDecode_ExitMap(pDCtx);
6453 QCBORDecode_ExitMap(pDCtx);
6454
6455 return 0;
6456}
6457
6458/* Iterations on the zero-length array in spNestedCBOR */
6459static int32_t DecodeNestedGetEmpty(QCBORDecodeContext *pDCtx)
6460{
6461 QCBORItem Item;
6462 QCBORError uErr;
6463
6464 QCBORDecode_EnterArrayFromMapN(pDCtx, 88);
6465 for(int x = 0; x < 20; x++) {
6466 uErr = QCBORDecode_GetNext(pDCtx, &Item);
6467 if(uErr != QCBOR_ERR_NO_MORE_ITEMS) {
6468 return 4100;
6469
6470 }
6471 }
6472 QCBORDecode_ExitArray(pDCtx);
6473 if(QCBORDecode_GetError(pDCtx)) {
6474 return 4101;
6475 }
6476
6477 return 0;
6478}
6479
6480/* Various iterations on the array that contains a zero in spNestedCBOR */
6481static int32_t DecodeNestedGetZero(QCBORDecodeContext *pDCtx)
6482{
6483 QCBORError uErr;
6484
6485 QCBORDecode_EnterMapFromMapN(pDCtx, 100100);
6486 QCBORDecode_EnterMapFromMapSZ(pDCtx, "sub1");
6487 QCBORDecode_EnterArrayFromMapN(pDCtx, 10);
6488 int64_t nInt = 99;
6489 QCBORDecode_GetInt64(pDCtx, &nInt);
6490 if(nInt != 0) {
6491 return 4200;
6492 }
6493 for(int x = 0; x < 20; x++) {
6494 QCBORItem Item;
6495 uErr = QCBORDecode_GetNext(pDCtx, &Item);
6496 if(uErr != QCBOR_ERR_NO_MORE_ITEMS) {
6497 return 4201;
6498
6499 }
6500 }
6501 QCBORDecode_ExitArray(pDCtx);
6502 if(QCBORDecode_GetAndResetError(pDCtx)) {
6503 return 4202;
6504 }
6505 QCBORDecode_EnterArrayFromMapN(pDCtx, 10);
6506 UsefulBufC dD;
6507 QCBORDecode_GetByteString(pDCtx, &dD);
6508 if(QCBORDecode_GetAndResetError(pDCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
6509 return 4203;
6510 }
6511 for(int x = 0; x < 20; x++) {
6512 QCBORDecode_GetByteString(pDCtx, &dD);
6513 uErr = QCBORDecode_GetAndResetError(pDCtx);
6514 if(uErr != QCBOR_ERR_NO_MORE_ITEMS) {
6515 return 4204;
6516 }
6517 }
6518 QCBORDecode_ExitArray(pDCtx);
6519 QCBORDecode_ExitMap(pDCtx);
6520 QCBORDecode_ExitMap(pDCtx);
6521
6522 return 0;
6523}
6524
6525/* Repeatedly enter and exit maps and arrays, go off the end of maps
6526 and arrays and such. */
Laurence Lundbladeb9702452021-03-08 21:02:57 -08006527static int32_t DecodeNestedIterate(void)
Laurence Lundbladeddebabe2020-11-22 11:32:17 -08006528{
6529 QCBORDecodeContext DCtx;
6530 int32_t nReturn;
6531 QCBORError uErr;
6532
6533 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spNestedCBOR), 0);
6534 QCBORDecode_EnterMap(&DCtx, NULL);
6535
6536 for(int j = 0; j < 5; j++) {
6537 for(int i = 0; i < 20; i++) {
6538 nReturn = DecodeNestedGetSubSub(&DCtx);
6539 if(nReturn) {
6540 return nReturn;
6541 }
6542 }
6543
6544 for(int i = 0; i < 20; i++) {
6545 nReturn = DecodeNestedGetEmpty(&DCtx);
6546 if(nReturn ) {
6547 return nReturn;
6548 }
6549 }
6550
6551 for(int i = 0; i < 20; i++) {
6552 nReturn = DecodeNestedGetZero(&DCtx);
6553 if(nReturn ) {
6554 return nReturn;
6555 }
6556 }
6557 }
6558
6559 QCBORDecode_ExitMap(&DCtx);
6560 uErr = QCBORDecode_Finish(&DCtx);
6561 if(uErr) {
6562 return (int32_t)uErr + 4100;
6563 }
6564
6565 return 0;
6566}
6567
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07006568
6569/*
Laurence Lundbladecc7da412020-12-27 00:09:07 -08006570 [
6571 23,
6572 6000,
6573 h'67616C6163746963',
6574 h'686176656E20746F6B656E'
6575 ]
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07006576 */
6577static const uint8_t spSimpleArray[] = {
Laurence Lundblade9b334962020-08-27 10:55:53 -07006578 0x84,
6579 0x17,
6580 0x19, 0x17, 0x70,
6581 0x48, 0x67, 0x61, 0x6C, 0x61, 0x63, 0x74, 0x69, 0x63,
6582 0x4B, 0x68, 0x61, 0x76, 0x65, 0x6E, 0x20, 0x74, 0x6F, 0x6B, 0x65, 0x6E};
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07006583
Laurence Lundbladecc7da412020-12-27 00:09:07 -08006584/* [h'', {}, [], 0] */
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08006585static const uint8_t spArrayOfEmpty[] = {0x84, 0x40, 0xa0, 0x80, 0x00};
6586
Laurence Lundbladecc7da412020-12-27 00:09:07 -08006587/* {} */
Laurence Lundbladee6f15112020-07-23 18:44:16 -07006588static const uint8_t spEmptyMap[] = {0xa0};
6589
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08006590#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundbladecc7da412020-12-27 00:09:07 -08006591/* {} */
Laurence Lundbladee6f15112020-07-23 18:44:16 -07006592static const uint8_t spEmptyInDefinteLengthMap[] = {0xbf, 0xff};
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07006593
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08006594
Laurence Lundbladef0499502020-08-01 11:55:57 -07006595/*
6596 {
6597 0: [],
6598 9: [
6599 [],
6600 []
6601 ],
6602 8: {
6603 1: [],
6604 2: {},
6605 3: []
6606 },
6607 4: {},
6608 5: [],
6609 6: [
6610 [],
6611 []
6612 ]
Laurence Lundbladecc7da412020-12-27 00:09:07 -08006613 }
Laurence Lundbladef0499502020-08-01 11:55:57 -07006614 */
6615static const uint8_t spMapOfEmpty[] = {
Laurence Lundbladecc7da412020-12-27 00:09:07 -08006616 0xa6, 0x00, 0x80, 0x09, 0x82, 0x80, 0x80, 0x08,
6617 0xa3, 0x01, 0x80, 0x02, 0xa0, 0x03, 0x80, 0x04,
6618 0xa0, 0x05, 0x9f, 0xff, 0x06, 0x9f, 0x80, 0x9f,
6619 0xff, 0xff};
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07006620
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08006621#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
6622
6623
Laurence Lundblade93d3f532020-09-28 21:09:12 -07006624/*
6625 Too many tags
Laurence Lundblade93d3f532020-09-28 21:09:12 -07006626 Duplicate label
6627 Integer overflow
6628 Date overflow
6629
Laurence Lundbladecc7da412020-12-27 00:09:07 -08006630 {
6631 1: 224(225(226(227(4(0))))),
Laurence Lundbladecc7da412020-12-27 00:09:07 -08006632 3: -18446744073709551616,
6633 4: 1(1.0e+300),
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07006634 5: 0,
6635 8: 8
Laurence Lundbladecc7da412020-12-27 00:09:07 -08006636 }
Laurence Lundblade93d3f532020-09-28 21:09:12 -07006637 */
6638static const uint8_t spRecoverableMapErrors[] = {
Laurence Lundblade37286c02022-09-03 10:05:02 -07006639#ifndef QCBOR_DISABLE_TAGS
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07006640 0xa6,
Laurence Lundblade93d3f532020-09-28 21:09:12 -07006641 0x04, 0xc1, 0xfb, 0x7e, 0x37, 0xe4, 0x3c, 0x88, 0x00, 0x75, 0x9c,
Laurence Lundblade37286c02022-09-03 10:05:02 -07006642 0x01, 0xd8, 0xe0, 0xd8, 0xe1, 0xd8, 0xe2, 0xd8, 0xe3, 0xd8, 0x04, 0x00,
6643#else
6644 0xa4,
6645#endif
6646 0x03, 0x3b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
Laurence Lundblade93d3f532020-09-28 21:09:12 -07006647 0x05, 0x00,
6648 0x05, 0x00,
6649 0x08, 0x08,
Laurence Lundblade93d3f532020-09-28 21:09:12 -07006650};
6651
Laurence Lundbladecc7da412020-12-27 00:09:07 -08006652/* Bad break */
Laurence Lundblade93d3f532020-09-28 21:09:12 -07006653static const uint8_t spUnRecoverableMapError1[] = {
6654 0xa2, 0xff, 0x01, 0x00, 0x02, 0x00
6655};
6656
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08006657#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundbladecc7da412020-12-27 00:09:07 -08006658/* No more items */
Laurence Lundblade93d3f532020-09-28 21:09:12 -07006659static const uint8_t spUnRecoverableMapError2[] = {
6660 0xbf, 0x02, 0xbf, 0xff, 0x01, 0x00, 0x02, 0x00
6661};
6662
Laurence Lundbladecc7da412020-12-27 00:09:07 -08006663/* Hit end because string is too long */
Laurence Lundblade93d3f532020-09-28 21:09:12 -07006664static const uint8_t spUnRecoverableMapError3[] = {
6665 0xbf, 0x02, 0x69, 0x64, 0x64, 0xff
6666};
6667
Laurence Lundbladecc7da412020-12-27 00:09:07 -08006668/* Hit end because string is too long */
Laurence Lundblade93d3f532020-09-28 21:09:12 -07006669static const uint8_t spUnRecoverableMapError4[] = {
6670 0xbf,
6671 0x02, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f,
6672 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f,
6673 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
6674 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
6675 0xff
6676};
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08006677#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
Laurence Lundblade93d3f532020-09-28 21:09:12 -07006678
Laurence Lundblade63926052021-03-29 16:05:51 -07006679const unsigned char not_well_formed_submod_section[] = {
6680 0xa1, 0x14, 0x1f,
6681};
Laurence Lundblade93d3f532020-09-28 21:09:12 -07006682
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07006683
6684/* Array of length 3, but only two items. */
6685const unsigned char spBadConsumeInput[] = {
6686 0x83, 0x00, 0x00
6687};
6688
6689/* Tag nesting too deep. */
6690const unsigned char spBadConsumeInput2[] = {
6691 0x81,
6692 0xD8, 0x37,
6693 0xD8, 0x2C,
6694 0xD8, 0x21,
6695 0xD6,
6696 0xCB,
6697 00
6698};
6699
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07006700
6701const unsigned char spBadConsumeInput4[] = {
6702 0x81, 0x9f, 0x00, 0xff
6703};
6704
6705const unsigned char spBadConsumeInput5[] = {
6706 0xa1, 0x80, 0x00
6707};
6708
Laurence Lundbladec5f45e42023-12-18 09:23:20 -07006709/*
6710 Lots of nesting for various nesting tests.
6711 { 1:1,
6712 2:{
6713 21:21,
6714 22:{
6715 221:[2111, 2112, 2113],
6716 222:222,
6717 223: {}
6718 },
6719 23: 23
6720 },
6721 3:3,
6722 4: [ {} ]
6723 }
6724 */
6725static const uint8_t spNested[] = {
67260xA4, /* Map of 4 */
6727 0x01, 0x01, /* Map entry 1 : 1 */
6728 0x02, 0xA3, /* Map entry 2 : {, an array of 3 */
6729 0x15, 0x15, /* Map entry 21 : 21 */
6730 0x16, 0xA3, /* Map entry 22 : {, a map of 3 */
6731 0x18, 0xDD, 0x83, /* Map entry 221 : [ an array of 3 */
6732 0x19, 0x08, 0x3F, /* Array item 2111 */
6733 0x19, 0x08, 0x40, /* Array item 2112 */
6734 0x19, 0x08, 0x41, /* Array item 2113 */
6735 0x18, 0xDE, 0x18, 0xDE, /* Map entry 222 : 222 */
6736 0x18, 0xDF, 0xA0, /* Map entry 223 : {} */
6737 0x17, 0x17, /* Map entry 23 : 23 */
6738 0x03, 0x03, /* Map entry 3 : 3 */
6739 0x04, 0x81, /* Map entry 4: [, an array of 1 */
6740 0xA0 /* Array entry {}, an empty map */
6741};
6742
6743
6744static int32_t EnterMapCursorTest(void)
6745{
6746 QCBORDecodeContext DCtx;
6747 QCBORItem Item1;
Laurence Lundblade11654912024-05-09 11:49:24 -07006748 int64_t nInt;
6749 QCBORError uErr;
6750
6751 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spNested), 0);
6752 QCBORDecode_EnterMap(&DCtx, NULL);
6753 QCBORDecode_GetInt64InMapN (&DCtx, 3, &nInt);
6754 uErr = QCBORDecode_GetNext(&DCtx, &Item1);
Laurence Lundbladea29f45a2024-05-14 15:55:19 -07006755 if(uErr != QCBOR_SUCCESS) {
6756 return 701;
6757 }
Laurence Lundblade11654912024-05-09 11:49:24 -07006758 if(Item1.uDataType != QCBOR_TYPE_INT64) {
6759 return 700;
6760 }
6761
Laurence Lundbladec5f45e42023-12-18 09:23:20 -07006762
6763 int i;
6764 for(i = 0; i < 13; i++) {
6765 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spNested), 0);
6766 QCBORDecode_EnterMap(&DCtx, NULL);
6767 int j;
6768 /* Move travesal cursor */
6769 for(j = 0; j < i; j++) {
6770 QCBORDecode_GetNext(&DCtx, &Item1);
6771 }
6772 QCBORDecode_EnterMapFromMapN(&DCtx, 2);
6773 QCBORDecode_ExitMap(&DCtx);
6774 QCBORDecode_GetNext(&DCtx, &Item1);
6775 if(Item1.label.int64 != 3) {
6776 return 8000;
6777 }
6778 }
6779
6780 for(i = 0; i < 13; i++) {
6781 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spNested), 0);
6782 QCBORDecode_EnterMap(&DCtx, NULL);
6783 int j;
6784 /* Move travesal cursor */
6785 for(j = 0; j < i; j++) {
6786 QCBORDecode_GetNext(&DCtx, &Item1);
6787 }
6788 QCBORDecode_EnterMapFromMapN(&DCtx, 2);
6789 QCBORDecode_EnterMapFromMapN(&DCtx, 22);
6790 QCBORDecode_ExitMap(&DCtx);
6791 QCBORDecode_GetNext(&DCtx, &Item1);
6792 if(Item1.label.int64 != 23) {
6793 return 8000;
6794 }
6795 }
6796
6797 for(i = 0; i < 13; i++) {
6798 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spNested), 0);
6799 QCBORDecode_EnterMap(&DCtx, NULL);
6800 int j;
6801 /* Move travesal cursor */
6802 for(j = 0; j < i; j++) {
6803 QCBORDecode_GetNext(&DCtx, &Item1);
6804 }
6805 QCBORDecode_EnterMapFromMapN(&DCtx, 2);
6806 QCBORDecode_EnterMapFromMapN(&DCtx, 22);
6807 for(j = 0; j < i; j++) {
6808 QCBORDecode_GetNext(&DCtx, &Item1);
6809 }
6810 QCBORDecode_EnterArrayFromMapN(&DCtx, 221);
6811 QCBORDecode_ExitArray(&DCtx);
6812 QCBORDecode_ExitMap(&DCtx);
6813 QCBORDecode_GetNext(&DCtx, &Item1);
6814 if(Item1.label.int64 != 23) {
6815 return 8000;
6816 }
6817 QCBORDecode_ExitMap(&DCtx);
6818 QCBORDecode_GetNext(&DCtx, &Item1);
6819 if(Item1.label.int64 != 3) {
6820 return 8000;
6821 }
6822 }
6823
6824 return 0;
6825}
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07006826
6827
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03006828int32_t EnterMapTest(void)
Laurence Lundbladebb87be22020-04-09 19:15:32 -07006829{
Laurence Lundbladef0499502020-08-01 11:55:57 -07006830 QCBORItem Item1;
Laurence Lundbladebb87be22020-04-09 19:15:32 -07006831 QCBORDecodeContext DCtx;
Laurence Lundbladef0499502020-08-01 11:55:57 -07006832 int32_t nReturn;
6833 QCBORError uErr;
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07006834
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08006835#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07006836 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spMapOfEmpty), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006837 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07006838
Laurence Lundbladef0499502020-08-01 11:55:57 -07006839
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006840 QCBORDecode_EnterArray(&DCtx, NULL); // Label 0
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07006841 QCBORDecode_ExitArray(&DCtx);
6842
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006843 QCBORDecode_EnterArray(&DCtx, NULL); // Label 9
6844 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07006845 QCBORDecode_ExitArray(&DCtx);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006846 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07006847 QCBORDecode_ExitArray(&DCtx);
6848 QCBORDecode_ExitArray(&DCtx);
6849
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006850 QCBORDecode_EnterMap(&DCtx, NULL); // Label 8
6851 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07006852 QCBORDecode_ExitArray(&DCtx);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006853 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07006854 QCBORDecode_ExitMap(&DCtx);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006855 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07006856 QCBORDecode_ExitArray(&DCtx);
6857 QCBORDecode_ExitMap(&DCtx);
6858
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006859 QCBORDecode_EnterMap(&DCtx, NULL); // Label4
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07006860 QCBORDecode_ExitMap(&DCtx);
6861
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006862 QCBORDecode_EnterArray(&DCtx, NULL); // Label 5
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07006863 QCBORDecode_ExitArray(&DCtx);
6864
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006865 QCBORDecode_EnterArray(&DCtx, NULL); // Label 6
6866 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07006867 QCBORDecode_ExitArray(&DCtx);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006868 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07006869 QCBORDecode_ExitArray(&DCtx);
6870 QCBORDecode_ExitArray(&DCtx);
6871
6872 QCBORDecode_ExitMap(&DCtx);
6873
6874 uErr = QCBORDecode_Finish(&DCtx);
6875 if(uErr != QCBOR_SUCCESS){
6876 return 3011;
6877 }
6878
Laurence Lundbladeec290b82024-06-10 11:10:54 -07006879#ifndef QCBOR_DISABLE_NON_INTEGER_LABELS
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07006880 (void)pValidMapIndefEncoded;
Laurence Lundbladef7a70bc2020-10-24 12:23:25 -07006881 nReturn = SpiffyDecodeBasicMap(UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapIndefEncoded));
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07006882 if(nReturn) {
6883 return nReturn + 20000;
Laurence Lundbladebb87be22020-04-09 19:15:32 -07006884 }
Laurence Lundbladeec290b82024-06-10 11:10:54 -07006885#endif /* ! QCBOR_DISABLE_NON_INTEGER_LABELS */
6886#endif /* ! QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08006887
Laurence Lundbladeec290b82024-06-10 11:10:54 -07006888#ifndef QCBOR_DISABLE_NON_INTEGER_LABELS
6889 QCBORItem ArrayItem;
Laurence Lundbladebb87be22020-04-09 19:15:32 -07006890
Laurence Lundbladef7a70bc2020-10-24 12:23:25 -07006891 nReturn = SpiffyDecodeBasicMap(UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded));
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006892 if(nReturn) {
6893 return nReturn;
6894 }
Laurence Lundbladebb87be22020-04-09 19:15:32 -07006895
Laurence Lundblade8ffdb742020-05-07 02:49:18 -07006896
Laurence Lundblade937ea812020-05-08 11:38:23 -07006897
Laurence Lundblade2f467f92020-10-09 17:50:11 -07006898 // These tests confirm the cursor is at the right place after entering
6899 // a map or array
Laurence Lundblade9b334962020-08-27 10:55:53 -07006900 const UsefulBufC ValidEncodedMap = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded);
Laurence Lundblade937ea812020-05-08 11:38:23 -07006901
6902 // Confirm cursor is at right place
Laurence Lundblade9b334962020-08-27 10:55:53 -07006903 QCBORDecode_Init(&DCtx, ValidEncodedMap, 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006904 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade937ea812020-05-08 11:38:23 -07006905 QCBORDecode_GetNext(&DCtx, &Item1);
6906 if(Item1.uDataType != QCBOR_TYPE_INT64) {
6907 return 2001;
6908 }
6909
6910
Laurence Lundblade9b334962020-08-27 10:55:53 -07006911 QCBORDecode_Init(&DCtx, ValidEncodedMap, 0);
Laurence Lundblade6f3f78e2020-08-31 13:09:14 -07006912 QCBORDecode_VGetNext(&DCtx, &Item1);
6913 QCBORDecode_VGetNext(&DCtx, &Item1);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006914 QCBORDecode_EnterArray(&DCtx, &ArrayItem);
6915 if(ArrayItem.uLabelType != QCBOR_TYPE_TEXT_STRING ||
6916 UsefulBuf_Compare(ArrayItem.label.string,
6917 UsefulBuf_FROM_SZ_LITERAL("an array of two strings"))) {
6918 return 2051;
6919 }
Laurence Lundblade937ea812020-05-08 11:38:23 -07006920 QCBORDecode_GetNext(&DCtx, &Item1);
6921 if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING) {
6922 return 2002;
6923 }
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006924 QCBORDecode_ExitArray(&DCtx);
6925 QCBORDecode_EnterMap(&DCtx, &ArrayItem);
6926 if(ArrayItem.uLabelType != QCBOR_TYPE_TEXT_STRING ||
6927 UsefulBuf_Compare(ArrayItem.label.string,
6928 UsefulBuf_FROM_SZ_LITERAL("map in a map"))) {
6929 return 2052;
6930 }
6931
Laurence Lundblade937ea812020-05-08 11:38:23 -07006932
Laurence Lundblade9b334962020-08-27 10:55:53 -07006933 QCBORDecode_Init(&DCtx, ValidEncodedMap, 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006934 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade64b607e2020-05-13 13:05:57 -07006935 QCBORDecode_GetNext(&DCtx, &Item1);
6936 QCBORDecode_GetNext(&DCtx, &Item1);
6937 QCBORDecode_GetNext(&DCtx, &Item1);
Laurence Lundblade937ea812020-05-08 11:38:23 -07006938 QCBORDecode_EnterMapFromMapSZ(&DCtx, "map in a map");
6939 QCBORDecode_GetNext(&DCtx, &Item1);
6940 if(Item1.uDataType != QCBOR_TYPE_BYTE_STRING) {
6941 return 2003;
6942 }
6943
Laurence Lundblade9b334962020-08-27 10:55:53 -07006944 QCBORDecode_Init(&DCtx, ValidEncodedMap, 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006945 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade937ea812020-05-08 11:38:23 -07006946 QCBORDecode_GetNext(&DCtx, &Item1);
6947 QCBORDecode_GetNext(&DCtx, &Item1);
6948 QCBORDecode_GetNext(&DCtx, &Item1);
6949 QCBORDecode_GetNext(&DCtx, &Item1);
6950 QCBORDecode_GetNext(&DCtx, &Item1);
6951 QCBORDecode_GetNext(&DCtx, &Item1);
6952 QCBORDecode_GetNext(&DCtx, &Item1);
6953 QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings");
6954 QCBORDecode_GetNext(&DCtx, &Item1);
6955 if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING) {
Laurence Lundblade64b607e2020-05-13 13:05:57 -07006956 return 2004;
Laurence Lundblade937ea812020-05-08 11:38:23 -07006957 }
6958
Laurence Lundblade9b334962020-08-27 10:55:53 -07006959 QCBORDecode_Init(&DCtx, ValidEncodedMap, 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006960 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade2b843b52020-06-16 20:51:03 -07006961 QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings");
6962 QCBORDecode_ExitArray(&DCtx);
6963 QCBORDecode_GetNext(&DCtx, &Item1);
6964 if(Item1.uDataType != QCBOR_TYPE_MAP && Item1.uLabelAlloc != QCBOR_TYPE_TEXT_STRING) {
6965 return 2006;
6966 }
6967 QCBORDecode_ExitMap(&DCtx);
6968 if(QCBORDecode_GetNext(&DCtx, &Item1) != QCBOR_ERR_NO_MORE_ITEMS) {
6969 return 2007;
6970 }
Laurence Lundbladeec290b82024-06-10 11:10:54 -07006971#endif /* !QCBOR_DISABLE_NON_INTEGER_LABELS */
Laurence Lundblade2b843b52020-06-16 20:51:03 -07006972
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07006973 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSimpleArray), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006974 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07006975 int64_t nDecodedInt2;
Laurence Lundbladeec290b82024-06-10 11:10:54 -07006976
6977 UsefulBufC String;
6978 QCBORDecode_GetTextStringInMapN(&DCtx, 88, &String);
6979 uErr = QCBORDecode_GetAndResetError(&DCtx);
6980 if(uErr != QCBOR_ERR_MAP_NOT_ENTERED){
6981 return 2009;
6982 }
6983#ifndef QCBOR_DISABLE_NON_INTEGER_LABELS
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07006984 QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2);
6985 uErr = QCBORDecode_GetAndResetError(&DCtx);
6986 if(uErr != QCBOR_ERR_MAP_NOT_ENTERED){
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07006987 return 2008;
6988 }
Laurence Lundbladeec290b82024-06-10 11:10:54 -07006989#endif /* ! QCBOR_DISABLE_NON_INTEGER_LABELS */
Laurence Lundblade937ea812020-05-08 11:38:23 -07006990
Laurence Lundbladee6f15112020-07-23 18:44:16 -07006991
6992 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEmptyMap), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006993 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundbladee6f15112020-07-23 18:44:16 -07006994 // This will fail because the map is empty.
6995 QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2);
6996 uErr = QCBORDecode_GetAndResetError(&DCtx);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006997 if(uErr != QCBOR_ERR_LABEL_NOT_FOUND){
Laurence Lundbladee6f15112020-07-23 18:44:16 -07006998 return 2010;
6999 }
7000 QCBORDecode_ExitMap(&DCtx);
7001 uErr = QCBORDecode_Finish(&DCtx);
7002 if(uErr != QCBOR_SUCCESS){
7003 return 2011;
7004 }
7005
7006
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08007007#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundbladee6f15112020-07-23 18:44:16 -07007008 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEmptyInDefinteLengthMap), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07007009 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundbladee6f15112020-07-23 18:44:16 -07007010 // This will fail because the map is empty.
7011 QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2);
7012 uErr = QCBORDecode_GetAndResetError(&DCtx);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07007013 if(uErr != QCBOR_ERR_LABEL_NOT_FOUND){
Laurence Lundblade085d7952020-07-24 10:26:30 -07007014 return 2012;
Laurence Lundbladee6f15112020-07-23 18:44:16 -07007015 }
7016 QCBORDecode_ExitMap(&DCtx);
7017 uErr = QCBORDecode_Finish(&DCtx);
7018 if(uErr != QCBOR_SUCCESS){
Laurence Lundblade085d7952020-07-24 10:26:30 -07007019 return 2013;
Laurence Lundbladee6f15112020-07-23 18:44:16 -07007020 }
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08007021#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
Laurence Lundbladee6f15112020-07-23 18:44:16 -07007022
Laurence Lundblade5f4e8712020-07-25 11:44:43 -07007023
7024 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spArrayOfEmpty), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07007025 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade323f8a92020-09-06 19:43:09 -07007026 QCBORDecode_GetByteString(&DCtx, &String);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07007027 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade5f4e8712020-07-25 11:44:43 -07007028 QCBORDecode_ExitMap(&DCtx);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07007029 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade5f4e8712020-07-25 11:44:43 -07007030 QCBORDecode_ExitArray(&DCtx);
7031 QCBORDecode_GetInt64(&DCtx, &nDecodedInt2);
7032 QCBORDecode_ExitArray(&DCtx);
7033 uErr = QCBORDecode_Finish(&DCtx);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07007034 if(uErr != QCBOR_SUCCESS) {
Laurence Lundblade5f4e8712020-07-25 11:44:43 -07007035 return 2014;
7036 }
7037
Laurence Lundblade93d3f532020-09-28 21:09:12 -07007038 int64_t nInt;
7039 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spRecoverableMapErrors), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07007040 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade37286c02022-09-03 10:05:02 -07007041#ifndef QCBOR_DISABLE_TAGS
Laurence Lundblade93d3f532020-09-28 21:09:12 -07007042 QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt);
Laurence Lundblade88e9db22020-11-02 03:56:33 -08007043 uErr = QCBORDecode_GetError(&DCtx);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07007044 if(uErr != QCBOR_ERR_TOO_MANY_TAGS) {
7045 return 2021;
7046 }
Laurence Lundblade88e9db22020-11-02 03:56:33 -08007047 if(QCBORDecode_GetNthTagOfLast(&DCtx, 0) != CBOR_TAG_INVALID64) {
7048 return 2121;
7049 }
7050 (void)QCBORDecode_GetAndResetError(&DCtx);
Laurence Lundblade37286c02022-09-03 10:05:02 -07007051#endif
Laurence Lundblade88e9db22020-11-02 03:56:33 -08007052
Laurence Lundblade93d3f532020-09-28 21:09:12 -07007053
Laurence Lundblade93d3f532020-09-28 21:09:12 -07007054 QCBORDecode_GetInt64InMapN(&DCtx, 0x03, &nInt);
7055 uErr = QCBORDecode_GetAndResetError(&DCtx);
7056 if(uErr != QCBOR_ERR_INT_OVERFLOW) {
7057 return 2023;
7058 }
7059
Laurence Lundblade37286c02022-09-03 10:05:02 -07007060#ifndef QCBOR_DISABLE_TAGS
Laurence Lundblade93d3f532020-09-28 21:09:12 -07007061 QCBORDecode_GetEpochDateInMapN(&DCtx, 0x04, QCBOR_TAG_REQUIREMENT_TAG, &nInt);
7062 uErr = QCBORDecode_GetAndResetError(&DCtx);
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007063 if(uErr != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_DATE_OVERFLOW)) {
Laurence Lundblade93d3f532020-09-28 21:09:12 -07007064 return 2024;
7065 }
Laurence Lundblade37286c02022-09-03 10:05:02 -07007066#endif
Laurence Lundblade93d3f532020-09-28 21:09:12 -07007067
7068 QCBORDecode_GetInt64InMapN(&DCtx, 0x05, &nInt);
7069 uErr = QCBORDecode_GetAndResetError(&DCtx);
7070 if(uErr != QCBOR_ERR_DUPLICATE_LABEL) {
7071 return 2025;
7072 }
7073
7074 QCBORDecode_GetInt64InMapN(&DCtx, 0x08, &nInt);
7075
7076 QCBORDecode_ExitMap(&DCtx);
7077 uErr = QCBORDecode_Finish(&DCtx);
7078 if(uErr != QCBOR_SUCCESS) {
7079 return 2026;
7080 }
7081
7082 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUnRecoverableMapError1), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07007083 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07007084 QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt);
7085 uErr = QCBORDecode_GetAndResetError(&DCtx);
7086 if(uErr != QCBOR_ERR_BAD_BREAK) {
7087 return 2030;
7088 }
7089
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08007090#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundblade93d3f532020-09-28 21:09:12 -07007091 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUnRecoverableMapError2), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07007092 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07007093 QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt);
7094 uErr = QCBORDecode_GetAndResetError(&DCtx);
7095 if(uErr != QCBOR_ERR_NO_MORE_ITEMS) {
7096 return 2031;
7097 }
7098
7099 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUnRecoverableMapError3), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07007100 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07007101 QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt);
7102 uErr = QCBORDecode_GetAndResetError(&DCtx);
7103 if(uErr != QCBOR_ERR_HIT_END) {
7104 return 2032;
7105 }
7106
7107 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUnRecoverableMapError4), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07007108 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07007109 QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt);
7110 uErr = QCBORDecode_GetAndResetError(&DCtx);
7111 if(uErr != QCBOR_ERR_ARRAY_DECODE_NESTING_TOO_DEEP) {
7112 return 2033;
7113 }
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08007114#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
7115
Laurence Lundbladeec290b82024-06-10 11:10:54 -07007116#ifndef QCBOR_DISABLE_NON_INTEGER_LABELS
Laurence Lundblade732e52d2021-02-22 20:11:01 -07007117 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0);
7118 QCBORDecode_VGetNextConsume(&DCtx, &Item1);
7119 if(Item1.uDataType != QCBOR_TYPE_MAP) {
7120 return 2401;
7121 }
7122 if(QCBORDecode_GetError(&DCtx)) {
7123 return 2402;
7124 }
7125
7126 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0);
7127 QCBORDecode_VGetNext(&DCtx, &Item1);
7128 if(Item1.uDataType != QCBOR_TYPE_MAP ||
7129 Item1.val.uCount != 3 ||
7130 Item1.uNextNestLevel != 1) {
7131 return 2403;
7132 }
7133 if(QCBORDecode_GetError(&DCtx)) {
7134 return 2404;
7135 }
7136 QCBORDecode_VGetNextConsume(&DCtx, &Item1);
7137 if(Item1.uDataType != QCBOR_TYPE_INT64 ||
7138 Item1.uNextNestLevel != 1 ||
7139 Item1.val.int64 != 42) {
7140 return 2405;
7141 }
7142 if(QCBORDecode_GetError(&DCtx)) {
7143 return 2406;
7144 }
7145 QCBORDecode_VGetNextConsume(&DCtx, &Item1);
7146 if(Item1.uDataType != QCBOR_TYPE_ARRAY ||
7147 Item1.uNestingLevel != 1 ||
7148 Item1.uNextNestLevel != 1 ||
7149 Item1.val.uCount != 2) {
7150 return 2407;
7151 }
7152 if(QCBORDecode_GetError(&DCtx)) {
7153 return 2408;
7154 }
7155 QCBORDecode_VGetNextConsume(&DCtx, &Item1);
7156 if(Item1.uDataType != QCBOR_TYPE_MAP ||
7157 Item1.uNestingLevel != 1 ||
7158 Item1.uNextNestLevel != 0 ||
7159 Item1.val.uCount != 4) {
7160 return 2409;
7161 }
7162 if(QCBORDecode_GetError(&DCtx)) {
7163 return 2410;
7164 }
Laurence Lundbladeec290b82024-06-10 11:10:54 -07007165#endif /* ! QCBOR_DISABLE_NON_INTEGER_LABELS */
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07007166
Laurence Lundbladeddebabe2020-11-22 11:32:17 -08007167 nReturn = DecodeNestedIterate();
7168
Laurence Lundblade63926052021-03-29 16:05:51 -07007169
7170 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(not_well_formed_submod_section), 0);
7171 QCBORDecode_EnterMap(&DCtx, NULL);
7172 QCBORDecode_EnterMapFromMapN(&DCtx, 20);
7173 if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_BAD_INT) {
7174 return 2500;
7175 }
7176
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07007177 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBadConsumeInput), 0);
7178 QCBORDecode_VGetNextConsume(&DCtx, &Item1);
7179 if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_NO_MORE_ITEMS) {
7180 return 2600;
7181 }
7182
Laurence Lundblade37286c02022-09-03 10:05:02 -07007183#ifndef QCBOR_DISABLE_TAGS
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07007184 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBadConsumeInput2), 0);
7185 QCBORDecode_VGetNextConsume(&DCtx, &Item1);
7186 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) {
7187 return 2700;
7188 }
Laurence Lundblade37286c02022-09-03 10:05:02 -07007189#endif
7190
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07007191
7192 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBadConsumeInput4), 0);
7193 QCBORDecode_VGetNextConsume(&DCtx, &Item1);
7194#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
7195 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) {
7196 return 2900;
7197 }
7198#else
7199 if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_INDEF_LEN_ARRAYS_DISABLED) {
7200 return 2901;
7201 }
7202#endif
7203
7204 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBadConsumeInput5), 0);
7205 QCBORDecode_VGetNextConsume(&DCtx, &Item1);
7206 if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_MAP_LABEL_TYPE) {
7207 return 3000;
7208 }
7209
Laurence Lundbladec5f45e42023-12-18 09:23:20 -07007210 nReturn = EnterMapCursorTest();
7211
Laurence Lundbladeddebabe2020-11-22 11:32:17 -08007212 return nReturn;
Laurence Lundblade9c905e82020-04-25 11:31:38 -07007213}
7214
7215
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007216struct NumberConversion {
7217 char *szDescription;
7218 UsefulBufC CBOR;
7219 int64_t nConvertedToInt64;
7220 QCBORError uErrorInt64;
7221 uint64_t uConvertToUInt64;
7222 QCBORError uErrorUint64;
7223 double dConvertToDouble;
7224 QCBORError uErrorDouble;
7225};
7226
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007227#ifndef QCBOR_DISABLE_EXP_AND_MANTISSA
7228#define EXP_AND_MANTISSA_ERROR(x) x
7229#else
7230#define EXP_AND_MANTISSA_ERROR(x) QCBOR_ERR_UNEXPECTED_TYPE
7231#endif
7232
7233
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07007234static const struct NumberConversion NumberConversions[] = {
Laurence Lundblade37286c02022-09-03 10:05:02 -07007235#ifndef QCBOR_DISABLE_TAGS
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007236 {
Laurence Lundblade4e808ba2022-12-29 12:45:20 -07007237 "Big float: INT64_MIN * 2e-1 to test handling of INT64_MIN",
7238 {(uint8_t[]){0xC5, 0x82, 0x20,
7239 0x3B, 0x7f, 0xff, 0xff, 0xff, 0xff, 0x0ff, 0xff, 0xff,
7240 }, 15},
7241 -4611686018427387904, /* INT64_MIN / 2 */
7242 EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS),
7243 0,
7244 EXP_AND_MANTISSA_ERROR(QCBOR_ERR_NUMBER_SIGN_CONVERSION),
7245 -4.6116860184273879E+18,
7246 FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS))
7247 },
7248 {
Laurence Lundblade784b54b2020-08-10 01:24:52 -07007249 "too large to fit into int64_t",
7250 {(uint8_t[]){0xc3, 0x48, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 10},
7251 0,
7252 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
7253 0,
7254 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
7255 ((double)INT64_MIN) + 1 ,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007256 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)
Laurence Lundblade784b54b2020-08-10 01:24:52 -07007257 },
7258 {
7259 "largest negative int that fits in int64_t",
7260 {(uint8_t[]){0xc3, 0x48, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, 10},
7261 INT64_MIN,
7262 QCBOR_SUCCESS,
7263 0,
7264 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
7265 (double)INT64_MIN,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007266 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)
Laurence Lundblade784b54b2020-08-10 01:24:52 -07007267 },
7268 {
Laurence Lundbladeda095972020-06-06 18:35:33 -07007269 "negative bignum -1",
7270 {(uint8_t[]){0xc3, 0x41, 0x00}, 3},
7271 -1,
7272 QCBOR_SUCCESS,
7273 0,
7274 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
7275 -1.0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007276 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)
Laurence Lundbladeda095972020-06-06 18:35:33 -07007277 },
7278 {
Laurence Lundblade9ab5abb2020-05-20 12:10:45 -07007279 "Decimal Fraction with positive bignum 257 * 10e3",
Laurence Lundblade887add82020-05-17 05:50:34 -07007280 {(uint8_t[]){0xC4, 0x82, 0x1B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03,
7281 0xC2, 0x42, 0x01, 0x01}, 15},
7282 257000,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007283 EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS),
Laurence Lundblade887add82020-05-17 05:50:34 -07007284 257000,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007285 EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS),
Laurence Lundblade887add82020-05-17 05:50:34 -07007286 257000.0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007287 FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS))
Laurence Lundblade887add82020-05-17 05:50:34 -07007288 },
7289 {
Laurence Lundbladeda095972020-06-06 18:35:33 -07007290 "bigfloat with negative bignum -258 * 2e3",
Laurence Lundblade887add82020-05-17 05:50:34 -07007291 {(uint8_t[]){0xC5, 0x82, 0x1B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03,
7292 0xC3, 0x42, 0x01, 0x01}, 15},
Laurence Lundbladeda095972020-06-06 18:35:33 -07007293 -2064,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007294 EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS),
Laurence Lundblade887add82020-05-17 05:50:34 -07007295 0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007296 EXP_AND_MANTISSA_ERROR(QCBOR_ERR_NUMBER_SIGN_CONVERSION),
Laurence Lundbladeda095972020-06-06 18:35:33 -07007297 -2064.0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007298 FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS))
Laurence Lundblade887add82020-05-17 05:50:34 -07007299 },
7300 {
Laurence Lundblade9ab5abb2020-05-20 12:10:45 -07007301 "bigfloat with positive bignum 257 * 2e3",
Laurence Lundblade887add82020-05-17 05:50:34 -07007302 {(uint8_t[]){0xC5, 0x82, 0x1B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03,
7303 0xC2, 0x42, 0x01, 0x01}, 15},
7304 2056,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007305 EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS),
Laurence Lundblade887add82020-05-17 05:50:34 -07007306 2056,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007307 EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS),
Laurence Lundblade887add82020-05-17 05:50:34 -07007308 2056.0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007309 FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS))
Laurence Lundblade887add82020-05-17 05:50:34 -07007310 },
7311 {
Laurence Lundbladeda095972020-06-06 18:35:33 -07007312 "negative bignum 0xc349010000000000000000 -18446744073709551617",
Laurence Lundblade887add82020-05-17 05:50:34 -07007313 {(uint8_t[]){0xc3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 11},
7314 0,
7315 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
7316 0,
7317 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
7318 -18446744073709551617.0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007319 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)
Laurence Lundblade887add82020-05-17 05:50:34 -07007320 },
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08007321#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
Laurence Lundblade887add82020-05-17 05:50:34 -07007322 {
Laurence Lundblade313b2862020-05-16 01:23:06 -07007323 "Positive bignum 0x01020304 indefinite length string",
7324 {(uint8_t[]){0xC2, 0x5f, 0x42, 0x01, 0x02, 0x41, 0x03, 0x41, 0x04, 0xff}, 10},
7325 0x01020304,
7326 QCBOR_SUCCESS,
7327 0x01020304,
7328 QCBOR_SUCCESS,
7329 16909060.0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007330 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)
Laurence Lundblade313b2862020-05-16 01:23:06 -07007331 },
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08007332#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
Laurence Lundblade313b2862020-05-16 01:23:06 -07007333 {
Laurence Lundblade887add82020-05-17 05:50:34 -07007334 "Decimal Fraction with neg bignum [9223372036854775807, -4759477275222530853137]",
Laurence Lundblade313b2862020-05-16 01:23:06 -07007335 {(uint8_t[]){0xC4, 0x82, 0x1B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
7336 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,}, 23},
7337 0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007338 EXP_AND_MANTISSA_ERROR(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW),
Laurence Lundblade313b2862020-05-16 01:23:06 -07007339 0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007340 EXP_AND_MANTISSA_ERROR(QCBOR_ERR_NUMBER_SIGN_CONVERSION),
Laurence Lundblade313b2862020-05-16 01:23:06 -07007341 -INFINITY,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007342 FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS))
Laurence Lundblade313b2862020-05-16 01:23:06 -07007343 },
7344 {
7345 "big float [9223372036854775806, 9223372036854775806]",
7346 {(uint8_t[]){0xC5, 0x82, 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE,
7347 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE}, 20},
7348 0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007349 EXP_AND_MANTISSA_ERROR(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW),
Laurence Lundblade313b2862020-05-16 01:23:06 -07007350 0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007351 EXP_AND_MANTISSA_ERROR(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW),
Laurence Lundblade313b2862020-05-16 01:23:06 -07007352 INFINITY,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007353 FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS))
Laurence Lundblade313b2862020-05-16 01:23:06 -07007354 },
7355 {
Laurence Lundblade983500d2020-05-14 11:49:34 -07007356 "Big float 3 * 2^^2",
7357 {(uint8_t[]){0xC5, 0x82, 0x02, 0x03}, 4},
7358 12,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007359 EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS),
Laurence Lundblade983500d2020-05-14 11:49:34 -07007360 12,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007361 EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS),
Laurence Lundblade983500d2020-05-14 11:49:34 -07007362 12.0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007363 FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS))
Laurence Lundblade983500d2020-05-14 11:49:34 -07007364 },
Laurence Lundblade983500d2020-05-14 11:49:34 -07007365 {
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007366 "Decimal fraction 3/10",
7367 {(uint8_t[]){0xC4, 0x82, 0x20, 0x03}, 4},
7368 0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007369 EXP_AND_MANTISSA_ERROR(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW),
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007370 0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007371 EXP_AND_MANTISSA_ERROR(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW),
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007372 0.30000000000000004,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007373 FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS))
Laurence Lundbladedfd49fc2020-09-01 14:17:16 -07007374 },
7375 {
Laurence Lundblade11fd78b2020-09-01 22:13:27 -07007376 "extreme pos bignum",
7377 {(uint8_t[]){0xc2, 0x59, 0x01, 0x90,
7378 // 50 rows of 8 is 400 digits.
7379 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7380 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7381 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7382 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7383 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7384 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7385 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7386 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7387 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7388 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7389 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7390 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7391 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7392 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7393 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7394 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7395 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7396 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7397 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7398 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7399 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7400 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7401 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7402 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7403 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7404 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7405 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7406 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7407 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7408 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7409 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7410 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7411 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7412 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7413 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7414 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7415 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7416 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7417 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7418 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7419 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7420 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7421 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7422 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7423 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7424 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7425 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7426 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7427 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
gtravisb787e82a2023-11-30 18:38:21 -08007428 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0},
Laurence Lundblade11fd78b2020-09-01 22:13:27 -07007429 404},
7430 0,
7431 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
7432 0,
7433 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
Laurence Lundblade11fd78b2020-09-01 22:13:27 -07007434 INFINITY,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007435 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS),
Laurence Lundblade11fd78b2020-09-01 22:13:27 -07007436 },
7437
7438 {
7439 "extreme neg bignum",
7440 {(uint8_t[]){0xc3, 0x59, 0x01, 0x90,
7441 // 50 rows of 8 is 400 digits.
7442 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7443 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7444 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7445 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7446 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7447 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7448 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7449 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7450 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7451 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7452 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7453 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7454 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7455 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7456 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7457 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7458 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7459 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7460 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7461 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7462 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7463 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7464 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7465 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7466 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7467 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7468 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7469 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7470 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7471 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7472 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7473 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7474 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7475 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7476 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7477 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7478 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7479 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7480 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7481 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7482 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7483 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7484 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7485 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7486 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7487 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7488 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7489 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7490 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
gtravisb787e82a2023-11-30 18:38:21 -08007491 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0},
Laurence Lundblade11fd78b2020-09-01 22:13:27 -07007492 404},
7493 0,
7494 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
7495 0,
7496 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
Laurence Lundblade11fd78b2020-09-01 22:13:27 -07007497 -INFINITY,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007498 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)
Laurence Lundblade11fd78b2020-09-01 22:13:27 -07007499 },
Laurence Lundblade51722fd2020-09-02 13:01:33 -07007500
7501 {
7502 "big float underflow [9223372036854775806, -9223372036854775806]",
7503 {(uint8_t[]){
7504 0xC5, 0x82,
7505 0x3B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE,
7506 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE}, 20},
Laurence Lundblade51722fd2020-09-02 13:01:33 -07007507 0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007508 EXP_AND_MANTISSA_ERROR(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW),
Laurence Lundblade51722fd2020-09-02 13:01:33 -07007509 0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007510 EXP_AND_MANTISSA_ERROR(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW),
Laurence Lundblade51722fd2020-09-02 13:01:33 -07007511 0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007512 FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS))
Laurence Lundblade51722fd2020-09-02 13:01:33 -07007513 },
7514
7515 {
7516 "bigfloat that evaluates to -INFINITY",
7517 {(uint8_t[]){
7518 0xC5, 0x82,
7519 0x1B, 0x70, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03,
7520 0xC3, 0x42, 0x01, 0x01}, 15},
Laurence Lundblade51722fd2020-09-02 13:01:33 -07007521 0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007522 EXP_AND_MANTISSA_ERROR(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW),
Laurence Lundblade51722fd2020-09-02 13:01:33 -07007523 0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007524 EXP_AND_MANTISSA_ERROR(QCBOR_ERR_NUMBER_SIGN_CONVERSION),
Laurence Lundblade51722fd2020-09-02 13:01:33 -07007525 -INFINITY,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007526 FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS))
Laurence Lundblade51722fd2020-09-02 13:01:33 -07007527 },
Laurence Lundblade37286c02022-09-03 10:05:02 -07007528 {
7529 "Positive bignum 0xffff",
7530 {(uint8_t[]){0xC2, 0x42, 0xff, 0xff}, 4},
7531 65536-1,
7532 QCBOR_SUCCESS,
7533 0xffff,
7534 QCBOR_SUCCESS,
7535 65535.0,
7536 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)
7537 },
7538#endif /* QCBOR_DISABLE_TAGS */
7539 {
7540 "Positive integer 18446744073709551615",
7541 {(uint8_t[]){0x1b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, 9},
7542 0,
7543 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
7544 18446744073709551615ULL,
7545 QCBOR_SUCCESS,
7546 18446744073709551615.0,
7547 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)
7548 },
7549
7550 {
7551 "Postive integer 0",
7552 {(uint8_t[]){0x0}, 1},
7553 0LL,
7554 QCBOR_SUCCESS,
7555 0ULL,
7556 QCBOR_SUCCESS,
7557 0.0,
7558 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)
7559 },
7560 {
7561 "Negative integer -18446744073709551616",
7562 {(uint8_t[]){0x3b, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }, 9},
7563 -9223372036854775807-1, // INT64_MIN
7564 QCBOR_SUCCESS,
7565 0ULL,
7566 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
7567 -9223372036854775808.0,
7568 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)
7569 },
7570 {
7571 "Double Floating point value 100.3",
7572 {(uint8_t[]){0xfb, 0x40, 0x59, 0x13, 0x33, 0x33, 0x33, 0x33, 0x33}, 9},
7573 100L,
7574 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS),
7575 100ULL,
7576 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS),
7577 100.3,
7578 FLOAT_ERR_CODE_NO_FLOAT(QCBOR_SUCCESS),
7579 },
7580 {
7581 "Floating point value NaN 0xfa7fc00000",
7582 {(uint8_t[]){0xfa, 0x7f, 0xc0, 0x00, 0x00}, 5},
7583 0,
7584 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_FLOAT_EXCEPTION),
7585 0,
7586 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_FLOAT_EXCEPTION),
7587 NAN,
7588 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS),
7589 },
7590 {
7591 "half-precision Floating point value -4",
7592 {(uint8_t[]){0xf9, 0xc4, 0x00}, 3},
7593 // Normal case with all enabled.
7594 -4,
7595 FLOAT_ERR_CODE_NO_HALF_PREC_NO_FLOAT_HW(QCBOR_SUCCESS),
7596 0,
7597 FLOAT_ERR_CODE_NO_HALF_PREC_NO_FLOAT_HW(QCBOR_ERR_NUMBER_SIGN_CONVERSION),
7598 -4.0,
7599 FLOAT_ERR_CODE_NO_HALF_PREC(QCBOR_SUCCESS)
7600 },
7601 {
7602 "+inifinity single precision",
7603 {(uint8_t[]){0xfa, 0x7f, 0x80, 0x00, 0x00}, 5},
7604 0,
7605 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_FLOAT_EXCEPTION),
7606 0,
7607 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW),
7608 INFINITY,
7609 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)
7610 },
7611
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007612};
Laurence Lundblade9c905e82020-04-25 11:31:38 -07007613
7614
7615
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08007616
7617static int32_t SetUpDecoder(QCBORDecodeContext *DCtx, UsefulBufC CBOR, UsefulBuf Pool)
7618{
7619 QCBORDecode_Init(DCtx, CBOR, QCBOR_DECODE_MODE_NORMAL);
7620#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
7621 if(QCBORDecode_SetMemPool(DCtx, Pool, 0)) {
7622 return 1;
7623 }
7624#else /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
7625 (void)Pool;
7626#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
7627 return 0;
7628}
7629
7630
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03007631int32_t IntegerConvertTest(void)
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007632{
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08007633 const int nNumTests = C_ARRAY_COUNT(NumberConversions,
7634 struct NumberConversion);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007635
Laurence Lundblade9ab5abb2020-05-20 12:10:45 -07007636 for(int nIndex = 0; nIndex < nNumTests; nIndex++) {
7637 const struct NumberConversion *pF = &NumberConversions[nIndex];
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07007638
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007639 // Set up the decoding context including a memory pool so that
7640 // indefinite length items can be checked
7641 QCBORDecodeContext DCtx;
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007642 UsefulBuf_MAKE_STACK_UB(Pool, 100);
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07007643
7644 /* ----- test conversion to int64_t ------ */
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08007645 if(SetUpDecoder(&DCtx, pF->CBOR, Pool)) {
7646 return (int32_t)(3333+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007647 }
7648
7649 int64_t nInt;
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007650 QCBORDecode_GetInt64ConvertAll(&DCtx, 0xffff, &nInt);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07007651 if(QCBORDecode_GetError(&DCtx) != pF->uErrorInt64) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07007652 return (int32_t)(2000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007653 }
7654 if(pF->uErrorInt64 == QCBOR_SUCCESS && pF->nConvertedToInt64 != nInt) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07007655 return (int32_t)(3000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007656 }
7657
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07007658 /* ----- test conversion to uint64_t ------ */
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08007659 if(SetUpDecoder(&DCtx, pF->CBOR, Pool)) {
7660 return (int32_t)(3333+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007661 }
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08007662
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007663 uint64_t uInt;
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007664 QCBORDecode_GetUInt64ConvertAll(&DCtx, 0xffff, &uInt);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07007665 if(QCBORDecode_GetError(&DCtx) != pF->uErrorUint64) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07007666 return (int32_t)(4000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007667 }
7668 if(pF->uErrorUint64 == QCBOR_SUCCESS && pF->uConvertToUInt64 != uInt) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07007669 return (int32_t)(5000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007670 }
7671
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07007672 /* ----- test conversion to double ------ */
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08007673 if(SetUpDecoder(&DCtx, pF->CBOR, Pool)) {
7674 return (int32_t)(3333+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007675 }
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007676
7677#ifndef USEFULBUF_DISABLE_ALL_FLOAT
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007678 double d;
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007679 QCBORDecode_GetDoubleConvertAll(&DCtx, 0xffff, &d);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07007680 if(QCBORDecode_GetError(&DCtx) != pF->uErrorDouble) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07007681 return (int32_t)(6000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007682 }
7683 if(pF->uErrorDouble == QCBOR_SUCCESS) {
7684 if(isnan(pF->dConvertToDouble)) {
Laurence Lundblade983500d2020-05-14 11:49:34 -07007685 // NaN's can't be compared for equality. A NaN is
7686 // never equal to anything including another NaN
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007687 if(!isnan(d)) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07007688 return (int32_t)(7000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007689 }
7690 } else {
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007691 if(pF->dConvertToDouble != d) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07007692 return (int32_t)(8000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007693 }
7694 }
7695 }
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007696#endif /* USEFULBUF_DISABLE_ALL_FLOAT */
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007697 }
7698
7699 return 0;
7700}
7701
Laurence Lundbladea8758502022-05-15 17:57:46 -07007702#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
7703
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03007704int32_t CBORTestIssue134(void)
David Navarro9123e5b2022-03-28 16:04:03 +02007705{
7706 QCBORDecodeContext DCtx;
7707 QCBORItem Item;
7708 QCBORError uCBORError;
7709 const uint8_t spTestIssue134[] = { 0x5F, 0x40, 0xFF };
Laurence Lundblade9c905e82020-04-25 11:31:38 -07007710
David Navarro9123e5b2022-03-28 16:04:03 +02007711 QCBORDecode_Init(&DCtx,
7712 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTestIssue134),
7713 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade97c61bf2020-05-02 11:24:06 -07007714
David Navarro9123e5b2022-03-28 16:04:03 +02007715 UsefulBuf_MAKE_STACK_UB(StringBuf, 200);
7716 QCBORDecode_SetMemPool(&DCtx, StringBuf, false);
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03007717
David Navarro9123e5b2022-03-28 16:04:03 +02007718 do {
7719 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
7720 } while (QCBOR_SUCCESS == uCBORError);
7721
7722 uCBORError = QCBORDecode_Finish(&DCtx);
7723
Laurence Lundblade11ea3612022-07-01 13:26:23 -07007724 return (int32_t)uCBORError;
David Navarro9123e5b2022-03-28 16:04:03 +02007725}
Laurence Lundblade97c61bf2020-05-02 11:24:06 -07007726
Laurence Lundbladea8758502022-05-15 17:57:46 -07007727#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
7728
Laurence Lundblade37286c02022-09-03 10:05:02 -07007729
7730
7731static const uint8_t spSequenceTestInput[] = {
7732 /* 1. The valid date string "1985-04-12" */
7733 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string
7734
7735 /* 2. */
7736 0x00,
7737
7738 /* 3. A valid epoch date, 1400000000; Tue, 13 May 2014 16:53:20 GMT */
7739 0x1a, 0x53, 0x72, 0x4E, 0x00,
7740
7741 /* 4. */
7742 0x62, 'h', 'i',
7743};
7744
7745
Laurence Lundbladee3553422020-05-02 11:11:17 -07007746int32_t CBORSequenceDecodeTests(void)
7747{
7748 QCBORDecodeContext DCtx;
Laurence Lundblade87495732021-02-26 10:05:55 -07007749 QCBORItem Item;
7750 QCBORError uCBORError;
7751 size_t uConsumed;
Laurence Lundbladee3553422020-05-02 11:11:17 -07007752
7753 // --- Test a sequence with extra bytes ---
Laurence Lundblade9b334962020-08-27 10:55:53 -07007754
Laurence Lundbladee3553422020-05-02 11:11:17 -07007755 QCBORDecode_Init(&DCtx,
Laurence Lundblade37286c02022-09-03 10:05:02 -07007756 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSequenceTestInput),
Laurence Lundbladee3553422020-05-02 11:11:17 -07007757 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade9b334962020-08-27 10:55:53 -07007758
Laurence Lundblade37286c02022-09-03 10:05:02 -07007759 // Get 1.
Laurence Lundbladee3553422020-05-02 11:11:17 -07007760 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
7761 if(uCBORError != QCBOR_SUCCESS) {
7762 return 1;
7763 }
Laurence Lundblade37286c02022-09-03 10:05:02 -07007764 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ) {
Laurence Lundbladee3553422020-05-02 11:11:17 -07007765 return 2;
7766 }
Laurence Lundbladec7114722020-08-13 05:11:40 -07007767
Laurence Lundblade87495732021-02-26 10:05:55 -07007768 uCBORError = QCBORDecode_PartialFinish(&DCtx, &uConsumed);
7769 if(uCBORError != QCBOR_ERR_EXTRA_BYTES ||
Laurence Lundblade37286c02022-09-03 10:05:02 -07007770 uConsumed != 11) {
7771 return 102;
7772 }
7773
7774 // Get 2.
7775 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
7776 if(uCBORError != QCBOR_SUCCESS) {
7777 return 66;
7778 }
7779
7780 uCBORError = QCBORDecode_PartialFinish(&DCtx, &uConsumed);
7781 if(uCBORError != QCBOR_ERR_EXTRA_BYTES ||
Laurence Lundblade87495732021-02-26 10:05:55 -07007782 uConsumed != 12) {
7783 return 102;
7784 }
7785
Laurence Lundblade37286c02022-09-03 10:05:02 -07007786 // Get 3.
Laurence Lundbladec7114722020-08-13 05:11:40 -07007787 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundbladee3553422020-05-02 11:11:17 -07007788 if(uCBORError != QCBOR_SUCCESS) {
7789 return 2;
7790 }
Laurence Lundblade37286c02022-09-03 10:05:02 -07007791 if(Item.uDataType != QCBOR_TYPE_INT64) {
Laurence Lundbladee3553422020-05-02 11:11:17 -07007792 return 3;
7793 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07007794
Laurence Lundbladee3553422020-05-02 11:11:17 -07007795 // A sequence can have stuff at the end that may
7796 // or may not be valid CBOR. The protocol decoder knows
7797 // when to stop by definition of the protocol, not
7798 // when the top-level map or array is ended.
7799 // Finish still has to be called to know that
7800 // maps and arrays (if there were any) were closed
7801 // off correctly. When called like this it
7802 // must return the error QCBOR_ERR_EXTRA_BYTES.
7803 uCBORError = QCBORDecode_Finish(&DCtx);
7804 if(uCBORError != QCBOR_ERR_EXTRA_BYTES) {
7805 return 4;
7806 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07007807
Laurence Lundbladee3553422020-05-02 11:11:17 -07007808 // --- Test an empty input ----
7809 uint8_t empty[1];
7810 UsefulBufC Empty = {empty, 0};
7811 QCBORDecode_Init(&DCtx,
7812 Empty,
7813 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade9b334962020-08-27 10:55:53 -07007814
Laurence Lundbladee3553422020-05-02 11:11:17 -07007815 uCBORError = QCBORDecode_Finish(&DCtx);
7816 if(uCBORError != QCBOR_SUCCESS) {
7817 return 5;
7818 }
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07007819
7820
Laurence Lundbladee3553422020-05-02 11:11:17 -07007821 // --- Sequence with unclosed indefinite length array ---
7822 static const uint8_t xx[] = {0x01, 0x9f, 0x02};
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07007823
Laurence Lundbladee3553422020-05-02 11:11:17 -07007824 QCBORDecode_Init(&DCtx,
7825 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(xx),
7826 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07007827
Laurence Lundbladee3553422020-05-02 11:11:17 -07007828 // Get the first item
7829 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
7830 if(uCBORError != QCBOR_SUCCESS) {
7831 return 7;
7832 }
7833 if(Item.uDataType != QCBOR_TYPE_INT64) {
7834 return 8;
7835 }
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07007836
Laurence Lundbladee3553422020-05-02 11:11:17 -07007837 // Get a second item
7838 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08007839#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundbladee3553422020-05-02 11:11:17 -07007840 if(uCBORError != QCBOR_SUCCESS) {
7841 return 9;
7842 }
7843 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
7844 return 10;
7845 }
7846
7847 // Try to finish before consuming all bytes to confirm
7848 // that the still-open error is returned.
7849 uCBORError = QCBORDecode_Finish(&DCtx);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07007850 if(uCBORError != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
Laurence Lundbladee3553422020-05-02 11:11:17 -07007851 return 11;
7852 }
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08007853#else /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
7854 if(uCBORError != QCBOR_ERR_INDEF_LEN_ARRAYS_DISABLED) {
7855 return 20;
7856 }
7857#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
Laurence Lundbladee3553422020-05-02 11:11:17 -07007858
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07007859
Laurence Lundbladee3553422020-05-02 11:11:17 -07007860 // --- Sequence with a closed indefinite length array ---
7861 static const uint8_t yy[] = {0x01, 0x9f, 0xff};
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07007862
Laurence Lundbladee3553422020-05-02 11:11:17 -07007863 QCBORDecode_Init(&DCtx,
7864 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(yy),
7865 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07007866
Laurence Lundbladee3553422020-05-02 11:11:17 -07007867 // Get the first item
7868 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
7869 if(uCBORError != QCBOR_SUCCESS) {
7870 return 12;
7871 }
7872 if(Item.uDataType != QCBOR_TYPE_INT64) {
7873 return 13;
7874 }
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07007875
Laurence Lundbladee3553422020-05-02 11:11:17 -07007876 // Get a second item
7877 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08007878#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
7879
Laurence Lundbladee3553422020-05-02 11:11:17 -07007880 if(uCBORError != QCBOR_SUCCESS) {
7881 return 14;
7882 }
7883 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
7884 return 15;
7885 }
7886
7887 // Try to finish before consuming all bytes to confirm
7888 // that the still-open error is returned.
7889 uCBORError = QCBORDecode_Finish(&DCtx);
7890 if(uCBORError != QCBOR_SUCCESS) {
7891 return 16;
7892 }
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08007893#else /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
7894 if(uCBORError != QCBOR_ERR_INDEF_LEN_ARRAYS_DISABLED) {
7895 return 20;
7896 }
7897#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
Laurence Lundbladee3553422020-05-02 11:11:17 -07007898
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07007899
Laurence Lundbladee3553422020-05-02 11:11:17 -07007900 return 0;
7901}
7902
Laurence Lundbladee15326f2020-06-15 15:50:23 -07007903
Laurence Lundblade70ecead2020-06-15 19:40:06 -07007904
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03007905int32_t IntToTests(void)
Laurence Lundbladee15326f2020-06-15 15:50:23 -07007906{
7907 int nErrCode;
7908 int32_t n32;
7909 int16_t n16;
7910 int8_t n8;
7911 uint32_t u32;
7912 uint16_t u16;
7913 uint8_t u8;
7914 uint64_t u64;
7915
7916 nErrCode = QCBOR_Int64ToInt32(1, &n32);
7917 if(nErrCode == -1 || n32 != 1) {
7918 return 1;
7919 }
7920
7921 nErrCode = QCBOR_Int64ToInt32((int64_t)INT32_MAX, &n32);
7922 if(nErrCode == -1 || n32 != INT32_MAX) {
7923 return 2;
7924 }
7925
7926 nErrCode = QCBOR_Int64ToInt32((int64_t)INT32_MIN, &n32);
7927 if(nErrCode == -1 || n32 != INT32_MIN) {
7928 return 3;
7929 }
7930
7931 nErrCode = QCBOR_Int64ToInt32(((int64_t)INT32_MAX)+1, &n32);
7932 if(nErrCode != -1) {
7933 return 4;
7934 }
7935
7936 nErrCode = QCBOR_Int64ToInt32(((int64_t)INT32_MIN)-1, &n32);
7937 if(nErrCode != -1) {
7938 return 5;
7939 }
7940
7941
7942 nErrCode = QCBOR_Int64ToInt16((int64_t)INT16_MAX, &n16);
7943 if(nErrCode == -1 || n16 != INT16_MAX) {
7944 return 6;
7945 }
7946
7947 nErrCode = QCBOR_Int64ToInt16((int64_t)INT16_MIN, &n16);
7948 if(nErrCode == -1 || n16 != INT16_MIN) {
7949 return 7;
7950 }
7951
7952 nErrCode = QCBOR_Int64ToInt16(1, &n16);
7953 if(nErrCode == -1 || n16 != 1) {
7954 return 8;
7955 }
7956
7957 nErrCode = QCBOR_Int64ToInt16(((int64_t)INT16_MAX)+1, &n16);
7958 if(nErrCode != -1) {
7959 return 9;
7960 }
7961
7962 nErrCode = QCBOR_Int64ToInt16(((int64_t)INT16_MIN)-1, &n16);
7963 if(nErrCode != -1) {
7964 return 10;
7965 }
7966
7967
7968 nErrCode = QCBOR_Int64ToInt8(1, &n8);
7969 if(nErrCode == -1 || n8 != 1) {
7970 return 11;
7971 }
7972
7973 nErrCode = QCBOR_Int64ToInt8((int64_t)INT8_MAX, &n8);
7974 if(nErrCode == -1 || n8 != INT8_MAX) {
7975 return 12;
7976 }
7977
7978 nErrCode = QCBOR_Int64ToInt8((int64_t)INT8_MIN, &n8);
7979 if(nErrCode == -1 || n8 != INT8_MIN) {
7980 return 13;
7981 }
7982
7983 nErrCode = QCBOR_Int64ToInt8(((int64_t)INT8_MAX)+1, &n8);
7984 if(nErrCode != -1) {
7985 return 14;
7986 }
7987
7988 nErrCode = QCBOR_Int64ToInt8(((int64_t)INT8_MIN)-1, &n8);
7989 if(nErrCode != -1) {
7990 return 15;
7991 }
7992
7993
7994 nErrCode = QCBOR_Int64ToUInt32(1, &u32);
7995 if(nErrCode == -1 || u32 != 1) {
7996 return 16;
7997 }
7998
7999 nErrCode = QCBOR_Int64ToUInt32((int64_t)UINT32_MAX, &u32);
8000 if(nErrCode == -1 || u32 != UINT32_MAX) {
8001 return 17;
8002 }
8003
8004 nErrCode = QCBOR_Int64ToUInt32((int64_t)0, &u32);
8005 if(nErrCode == -1 || u32 != 0) {
8006 return 18;
8007 }
8008
8009 nErrCode = QCBOR_Int64ToUInt32(((int64_t)UINT32_MAX)+1, &u32);
8010 if(nErrCode != -1) {
8011 return 19;
8012 }
8013
8014 nErrCode = QCBOR_Int64ToUInt32((int64_t)-1, &u32);
8015 if(nErrCode != -1) {
8016 return 20;
8017 }
8018
8019
8020 nErrCode = QCBOR_Int64UToInt16((int64_t)UINT16_MAX, &u16);
8021 if(nErrCode == -1 || u16 != UINT16_MAX) {
8022 return 21;
8023 }
8024
8025 nErrCode = QCBOR_Int64UToInt16((int64_t)0, &u16);
8026 if(nErrCode == -1 || u16 != 0) {
8027 return 22;
8028 }
8029
8030 nErrCode = QCBOR_Int64UToInt16(1, &u16);
8031 if(nErrCode == -1 || u16 != 1) {
8032 return 23;
8033 }
8034
8035 nErrCode = QCBOR_Int64UToInt16(((int64_t)UINT16_MAX)+1, &u16);
8036 if(nErrCode != -1) {
8037 return 24;
8038 }
8039
8040 nErrCode = QCBOR_Int64UToInt16((int64_t)-1, &u16);
8041 if(nErrCode != -1) {
8042 return 25;
8043 }
8044
8045
8046 nErrCode = QCBOR_Int64ToUInt8((int64_t)UINT8_MAX, &u8);
8047 if(nErrCode == -1 || u8 != UINT8_MAX) {
8048 return 26;
8049 }
8050
8051 nErrCode = QCBOR_Int64ToUInt8((int64_t)0, &u8);
8052 if(nErrCode == -1 || u8 != 0) {
8053 return 27;
8054 }
8055
8056 nErrCode = QCBOR_Int64ToUInt8(1, &u8);
8057 if(nErrCode == -1 || u8 != 1) {
8058 return 28;
8059 }
8060
8061 nErrCode = QCBOR_Int64ToUInt8(((int64_t)UINT16_MAX)+1, &u8);
8062 if(nErrCode != -1) {
8063 return 29;
8064 }
8065
8066 nErrCode = QCBOR_Int64ToUInt8((int64_t)-1, &u8);
8067 if(nErrCode != -1) {
8068 return 30;
8069 }
8070
8071
8072 nErrCode = QCBOR_Int64ToUInt64(1, &u64);
8073 if(nErrCode == -1 || u64 != 1) {
8074 return 31;
8075 }
8076
8077 nErrCode = QCBOR_Int64ToUInt64(INT64_MAX, &u64);
8078 if(nErrCode == -1 || u64 != INT64_MAX) {
8079 return 32;
8080 }
8081
8082 nErrCode = QCBOR_Int64ToUInt64((int64_t)0, &u64);
8083 if(nErrCode == -1 || u64 != 0) {
8084 return 33;
8085 }
8086
8087 nErrCode = QCBOR_Int64ToUInt64((int64_t)-1, &u64);
8088 if(nErrCode != -1) {
8089 return 34;
8090 }
8091
8092 return 0;
8093}
8094
Laurence Lundblade0750fc42020-06-20 21:02:34 -07008095
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07008096
8097
Laurence Lundblade0750fc42020-06-20 21:02:34 -07008098/*
8099A sequence with
8100 A wrapping bstr
8101 containing a map
8102 1
8103 2
8104 A wrapping bstr
8105 containing an array
8106 3
8107 wrapping bstr
8108 4
8109 5
8110 6
8111 array
8112 7
8113 8
Laurence Lundblade0750fc42020-06-20 21:02:34 -07008114 */
8115
Laurence Lundblade55013642020-09-23 05:39:22 -07008116static UsefulBufC EncodeBstrWrapTestData(UsefulBuf OutputBuffer)
Laurence Lundblade0750fc42020-06-20 21:02:34 -07008117{
Laurence Lundblade55013642020-09-23 05:39:22 -07008118 UsefulBufC Encoded;
Laurence Lundblade0750fc42020-06-20 21:02:34 -07008119 QCBOREncodeContext EC;
Laurence Lundblade55013642020-09-23 05:39:22 -07008120 QCBORError uErr;
Laurence Lundblade0750fc42020-06-20 21:02:34 -07008121
Laurence Lundblade55013642020-09-23 05:39:22 -07008122 QCBOREncode_Init(&EC, OutputBuffer);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07008123
8124 QCBOREncode_BstrWrap(&EC);
8125 QCBOREncode_OpenMap(&EC);
8126 QCBOREncode_AddInt64ToMapN(&EC, 100, 1);
8127 QCBOREncode_AddInt64ToMapN(&EC, 200, 2);
8128 QCBOREncode_CloseMap(&EC);
8129 QCBOREncode_BstrWrap(&EC);
8130 QCBOREncode_OpenArray(&EC);
8131 QCBOREncode_AddInt64(&EC, 3);
8132 QCBOREncode_BstrWrap(&EC);
8133 QCBOREncode_AddInt64(&EC, 4);
8134 QCBOREncode_CloseBstrWrap(&EC, NULL);
8135 QCBOREncode_AddInt64(&EC, 5);
8136 QCBOREncode_CloseArray(&EC);
8137 QCBOREncode_CloseBstrWrap(&EC, NULL);
8138 QCBOREncode_AddInt64(&EC, 6);
8139 QCBOREncode_CloseBstrWrap(&EC, NULL);
8140 QCBOREncode_OpenArray(&EC);
8141 QCBOREncode_AddInt64(&EC, 7);
8142 QCBOREncode_AddInt64(&EC, 8);
8143 QCBOREncode_CloseArray(&EC);
8144
8145 uErr = QCBOREncode_Finish(&EC, &Encoded);
Laurence Lundblade40a04322020-06-27 22:52:52 -07008146 if(uErr) {
8147 Encoded = NULLUsefulBufC;
8148 }
Laurence Lundblade0750fc42020-06-20 21:02:34 -07008149
8150 return Encoded;
8151}
8152
Laurence Lundbladecc7da412020-12-27 00:09:07 -08008153/* h'FF' */
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08008154static const uint8_t spBreakInByteString[] = {
8155 0x41, 0xff
8156};
8157
Laurence Lundblade0750fc42020-06-20 21:02:34 -07008158
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03008159int32_t EnterBstrTest(void)
Laurence Lundblade0750fc42020-06-20 21:02:34 -07008160{
Laurence Lundblade8510f8c2020-12-01 11:31:16 -08008161 UsefulBuf_MAKE_STACK_UB(OutputBuffer, 100);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07008162
8163 QCBORDecodeContext DC;
8164
Laurence Lundblade55013642020-09-23 05:39:22 -07008165 QCBORDecode_Init(&DC, EncodeBstrWrapTestData(OutputBuffer), 0);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07008166
Laurence Lundblade55013642020-09-23 05:39:22 -07008167 int64_t n1, n2, n3, n4, n5, n6, n7, n8;
Laurence Lundblade0750fc42020-06-20 21:02:34 -07008168
8169
Laurence Lundblade9b334962020-08-27 10:55:53 -07008170 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07008171 QCBORDecode_EnterMap(&DC, NULL);
Laurence Lundblade55013642020-09-23 05:39:22 -07008172 QCBORDecode_GetInt64InMapN(&DC, 100, &n1);
8173 QCBORDecode_GetInt64InMapN(&DC, 200, &n2);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07008174 QCBORDecode_ExitMap(&DC);
Laurence Lundblade9b334962020-08-27 10:55:53 -07008175 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07008176 QCBORDecode_EnterArray(&DC, NULL);
Laurence Lundblade55013642020-09-23 05:39:22 -07008177 QCBORDecode_GetInt64(&DC, &n3);
Laurence Lundblade9b334962020-08-27 10:55:53 -07008178 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
Laurence Lundblade55013642020-09-23 05:39:22 -07008179 QCBORDecode_GetInt64(&DC, &n4);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07008180 QCBORDecode_ExitBstrWrapped(&DC);
Laurence Lundblade55013642020-09-23 05:39:22 -07008181 QCBORDecode_GetInt64(&DC, &n5);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07008182 QCBORDecode_ExitArray(&DC);
8183 QCBORDecode_ExitBstrWrapped(&DC);
Laurence Lundblade55013642020-09-23 05:39:22 -07008184 QCBORDecode_GetInt64(&DC, &n6);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07008185 QCBORDecode_ExitBstrWrapped(&DC);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07008186 QCBORDecode_EnterArray(&DC, NULL);
Laurence Lundblade55013642020-09-23 05:39:22 -07008187 QCBORDecode_GetInt64(&DC, &n7);
8188 QCBORDecode_GetInt64(&DC, &n8);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07008189 QCBORDecode_ExitArray(&DC);
8190
8191 QCBORError uErr = QCBORDecode_Finish(&DC);
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08008192 if(uErr) {
8193 return (int32_t)uErr;
8194 }
Laurence Lundblade0750fc42020-06-20 21:02:34 -07008195
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08008196
8197 /* Enter and exit byte string wrapped CBOR that is bad. It has just a break.
8198 * Successful because no items are fetched from byte string.
8199 */
8200 QCBORDecode_Init(&DC,
8201 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBreakInByteString),
8202 0);
8203 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
8204 uErr = QCBORDecode_GetError(&DC);
8205 if(uErr) {
8206 return 100 + (int32_t)uErr;
8207 }
8208
8209 QCBORDecode_ExitBstrWrapped(&DC);
8210 uErr = QCBORDecode_GetError(&DC);
8211 if(uErr) {
8212 return 200 + (int32_t)uErr;
8213 }
8214
8215 /* Try to get item that is a break out of a byte string wrapped CBOR.
8216 * It fails because there should be no break.
8217 */
8218 QCBORDecode_Init(&DC,
8219 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBreakInByteString),
8220 0);
8221 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
8222 QCBORItem Item;
8223 uErr = QCBORDecode_GetNext(&DC, &Item);
8224 if(uErr != QCBOR_ERR_BAD_BREAK) {
8225 return 300 + (int32_t)uErr;
8226 }
8227
8228 return 0;
Laurence Lundblade0750fc42020-06-20 21:02:34 -07008229}
Laurence Lundblade37f46e52020-08-04 03:32:14 -07008230
8231
8232
8233
8234static const uint8_t spTaggedTypes[] = {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008235 0xb2,
Laurence Lundblade37f46e52020-08-04 03:32:14 -07008236
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07008237 // Date string
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008238 0x00,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07008239 0xc0, 0x74, 0x32, 0x30, 0x30, 0x33, 0x2D, 0x31, 0x32, 0x2D,
8240 0x31, 0x33, 0x54, 0x31, 0x38, 0x3A, 0x33, 0x30, 0x3A, 0x30,
8241 0x32, 0x5A,
Laurence Lundblade37f46e52020-08-04 03:32:14 -07008242
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008243 0x01,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07008244 0x74, 0x32, 0x30, 0x30, 0x33, 0x2D, 0x31, 0x32, 0x2D, 0x31,
8245 0x33, 0x54, 0x31, 0x38, 0x3A, 0x33, 0x30, 0x3A, 0x30, 0x32,
8246 0x5A,
Laurence Lundblade37f46e52020-08-04 03:32:14 -07008247
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07008248 // Bignum
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008249 10,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07008250 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
8251 0x09, 0x10,
Laurence Lundblade37f46e52020-08-04 03:32:14 -07008252
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008253 11,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07008254 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
8255 0x09, 0x10,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008256
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07008257 // URL
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008258 20,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07008259 0xd8, 0x20, 0x6f, 0x68, 0x74, 0x74, 0x70, 0x3A, 0x2F, 0x2F,
8260 0x63, 0x62, 0x6F, 0x72, 0x2E, 0x6D, 0x65, 0x2F,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008261
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07008262 21,
8263 0x6f, 0x68, 0x74, 0x74, 0x70, 0x3A, 0x2F, 0x2F, 0x63, 0x62,
8264 0x6F, 0x72, 0x2E, 0x6D, 0x65, 0x2F,
8265
8266 // B64
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008267 0x18, 0x1e,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07008268 0xd8, 0x22, 0x6c, 0x63, 0x47, 0x78, 0x6C, 0x59, 0x58, 0x4E,
8269 0x31, 0x63, 0x6D, 0x55, 0x75,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008270
8271 0x18, 0x1f,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07008272 0x6c, 0x63, 0x47, 0x78, 0x6C, 0x59, 0x58, 0x4E, 0x31, 0x63,
8273 0x6D, 0x55, 0x75,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008274
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07008275 // B64URL
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008276 0x18, 0x28,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07008277 0xd8, 0x21, 0x6c, 0x63, 0x47, 0x78, 0x6C, 0x59, 0x58, 0x4E,
8278 0x31, 0x63, 0x6D, 0x55, 0x75,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008279
8280 0x18, 0x29,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07008281 0x6c, 0x63, 0x47, 0x78, 0x6C, 0x59, 0x58, 0x4E, 0x31, 0x63,
8282 0x6D, 0x55, 0x75,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008283
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07008284 // Regex
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008285 0x18, 0x32,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07008286 0xd8, 0x23, 0x68, 0x31, 0x30, 0x30, 0x5C, 0x73, 0x2A, 0x6D,
8287 0x6B,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008288
8289 0x18, 0x33,
8290 0x68, 0x31, 0x30, 0x30, 0x5C, 0x73, 0x2A, 0x6D, 0x6B,
8291
8292 // MIME
8293 0x18, 0x3c,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07008294 0xd8, 0x24, 0x72, 0x4D, 0x49, 0x4D, 0x45, 0x2D, 0x56, 0x65,
8295 0x72, 0x73, 0x69, 0x6F, 0x6E, 0x3A, 0x20, 0x31, 0x2E, 0x30,
8296 0x0A,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008297
8298 0x18, 0x3d,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07008299 0x72, 0x4D, 0x49, 0x4D, 0x45, 0x2D, 0x56, 0x65, 0x72, 0x73,
8300 0x69, 0x6F, 0x6E, 0x3A, 0x20, 0x31, 0x2E, 0x30, 0x0A,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008301
8302 0x18, 0x3e,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07008303 0xd9, 0x01, 0x01, 0x52, 0x4D, 0x49, 0x4D, 0x45, 0x2D, 0x56,
8304 0x65, 0x72, 0x73, 0x69, 0x6F, 0x6E, 0x3A, 0x20, 0x31, 0x2E,
8305 0x30, 0x0A,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008306
8307 0x18, 0x3f,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07008308 0x52, 0x4D, 0x49, 0x4D, 0x45, 0x2D, 0x56, 0x65, 0x72, 0x73,
8309 0x69, 0x6F, 0x6E, 0x3A, 0x20, 0x31, 0x2E, 0x30, 0x0A,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008310
8311 // UUID
8312 0x18, 0x46,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07008313 0xd8, 0x25, 0x50, 0x53, 0x4D, 0x41, 0x52, 0x54, 0x43, 0x53,
8314 0x4C, 0x54, 0x54, 0x43, 0x46, 0x49, 0x43, 0x41, 0x32,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008315
8316 0x18, 0x47,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07008317 0x50, 0x53, 0x4D, 0x41, 0x52, 0x54, 0x43, 0x53, 0x4C, 0x54,
8318 0x54, 0x43, 0x46, 0x49, 0x43, 0x41, 0x32
Laurence Lundblade37f46e52020-08-04 03:32:14 -07008319};
8320
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03008321int32_t DecodeTaggedTypeTests(void)
Laurence Lundblade37f46e52020-08-04 03:32:14 -07008322{
8323 QCBORDecodeContext DC;
8324 QCBORError uErr;
8325
8326 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTaggedTypes), 0);
8327
8328 UsefulBufC String;
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008329 bool bNeg;
Laurence Lundblade37f46e52020-08-04 03:32:14 -07008330
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07008331 QCBORDecode_EnterMap(&DC, NULL);
Laurence Lundblade9b334962020-08-27 10:55:53 -07008332 QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008333 QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &String);
Laurence Lundblade37f46e52020-08-04 03:32:14 -07008334 if(QCBORDecode_GetError(&DC) != QCBOR_SUCCESS) {
8335 return 1;
8336 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07008337 QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade37f46e52020-08-04 03:32:14 -07008338 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_UNEXPECTED_TYPE) {
8339 return 2;
8340 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07008341 QCBORDecode_GetDateStringInMapN(&DC, 1, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade37f46e52020-08-04 03:32:14 -07008342 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_UNEXPECTED_TYPE) {
8343 return 3;
8344 }
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008345 QCBORDecode_GetDateStringInMapN(&DC, 1, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &String);
Laurence Lundblade9b334962020-08-27 10:55:53 -07008346 QCBORDecode_GetDateStringInMapN(&DC, 1, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade37f46e52020-08-04 03:32:14 -07008347 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
8348 return 4;
8349 }
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008350 QCBORDecode_GetDateStringInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07008351 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008352 return 5;
8353 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07008354
Laurence Lundblade9b334962020-08-27 10:55:53 -07008355 QCBORDecode_GetBignumInMapN(&DC, 10, QCBOR_TAG_REQUIREMENT_TAG, &String, &bNeg);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008356 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
8357 bNeg != false) {
8358 return 10;
8359 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07008360 QCBORDecode_GetBignumInMapN(&DC, 11, QCBOR_TAG_REQUIREMENT_TAG, &String, &bNeg);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008361 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
8362 bNeg != true) {
8363 return 11;
8364 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07008365 QCBORDecode_GetBignumInMapN(&DC, 11, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bNeg);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008366 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_UNEXPECTED_TYPE) {
8367 return 12;
8368 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07008369 QCBORDecode_GetBignumInMapN(&DC, 14, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bNeg);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07008370 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008371 return 13;
8372 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07008373 QCBORDecode_GetBignumInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bNeg);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07008374 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008375 return 14;
8376 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07008377
Laurence Lundblade9b334962020-08-27 10:55:53 -07008378 QCBORDecode_GetURIInMapN(&DC, 20, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008379 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
8380 return 20;
8381 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07008382 QCBORDecode_GetURIInMapN(&DC, 21, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008383 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
8384 return 21;
8385 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07008386 QCBORDecode_GetURIInMapN(&DC, 22, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07008387 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008388 return 22;
8389 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07008390 QCBORDecode_GetURIInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07008391 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008392 return 23;
8393 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07008394
Laurence Lundblade24bd7e12021-01-09 00:05:11 -08008395#ifndef QCBOR_DISABLE_UNCOMMON_TAGS
Laurence Lundblade9b334962020-08-27 10:55:53 -07008396 QCBORDecode_GetB64InMapN(&DC, 30, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008397 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
8398 return 30;
8399 }
Laurence Lundblade24bd7e12021-01-09 00:05:11 -08008400#endif
Laurence Lundblade9b334962020-08-27 10:55:53 -07008401 QCBORDecode_GetB64InMapN(&DC, 31, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008402 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
8403 return 31;
8404 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07008405 QCBORDecode_GetB64InMapN(&DC, 32, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07008406 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008407 return 32;
8408 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07008409 QCBORDecode_GetB64InMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07008410 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008411 return 33;
8412 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07008413
Laurence Lundblade24bd7e12021-01-09 00:05:11 -08008414#ifndef QCBOR_DISABLE_UNCOMMON_TAGS
Laurence Lundblade9b334962020-08-27 10:55:53 -07008415 QCBORDecode_GetB64URLInMapN(&DC, 40, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008416 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
8417 return 40;
8418 }
Laurence Lundblade24bd7e12021-01-09 00:05:11 -08008419#endif
Laurence Lundblade9b334962020-08-27 10:55:53 -07008420 QCBORDecode_GetB64URLInMapN(&DC, 41, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008421 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
8422 return 41;
8423 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07008424 QCBORDecode_GetB64URLInMapN(&DC, 42, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07008425 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008426 return 42;
8427 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07008428 QCBORDecode_GetB64URLInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07008429 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008430 return 43;
8431 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07008432
Laurence Lundblade24bd7e12021-01-09 00:05:11 -08008433#ifndef QCBOR_DISABLE_UNCOMMON_TAGS
Laurence Lundblade9b334962020-08-27 10:55:53 -07008434 QCBORDecode_GetRegexInMapN(&DC, 50, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008435 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
8436 return 50;
8437 }
Laurence Lundblade24bd7e12021-01-09 00:05:11 -08008438#endif
Laurence Lundblade9b334962020-08-27 10:55:53 -07008439 QCBORDecode_GetRegexInMapN(&DC, 51, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008440 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
8441 return 51;
8442 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07008443 QCBORDecode_GetRegexInMapN(&DC, 52, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07008444 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008445 return 52;
8446 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07008447 QCBORDecode_GetRegexInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07008448 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008449 return 53;
8450 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07008451
Laurence Lundblade24bd7e12021-01-09 00:05:11 -08008452#ifndef QCBOR_DISABLE_UNCOMMON_TAGS
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008453 // MIME
8454 bool bIsNot7Bit;
Laurence Lundblade9b334962020-08-27 10:55:53 -07008455 QCBORDecode_GetMIMEMessageInMapN(&DC, 60, QCBOR_TAG_REQUIREMENT_TAG, &String, &bIsNot7Bit);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008456 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
8457 bIsNot7Bit == true) {
8458 return 60;
8459 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07008460 QCBORDecode_GetMIMEMessageInMapN(&DC, 61, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bIsNot7Bit);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008461 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
8462 bIsNot7Bit == true) {
8463 return 61;
8464 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07008465 QCBORDecode_GetMIMEMessageInMapN(&DC, 62, QCBOR_TAG_REQUIREMENT_TAG, &String, &bIsNot7Bit);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008466 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
8467 bIsNot7Bit == false) {
8468 return 62;
8469 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07008470 QCBORDecode_GetMIMEMessageInMapN(&DC, 63, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bIsNot7Bit);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008471 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
8472 bIsNot7Bit == false) {
8473 return 63;
8474 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07008475 QCBORDecode_GetMIMEMessageInMapN(&DC, 64, QCBOR_TAG_REQUIREMENT_TAG, &String, &bNeg);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07008476 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008477 return 64;
8478 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07008479 QCBORDecode_GetMIMEMessageInMapSZ(&DC, "zzz", QCBOR_TAG_REQUIREMENT_TAG, &String, &bNeg);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07008480 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008481 return 65;
8482 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07008483
Laurence Lundblade24bd7e12021-01-09 00:05:11 -08008484
Laurence Lundblade9b334962020-08-27 10:55:53 -07008485 QCBORDecode_GetBinaryUUIDInMapN(&DC, 70, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008486 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
8487 return 70;
8488 }
Laurence Lundblade24bd7e12021-01-09 00:05:11 -08008489#endif /* #ifndef QCBOR_DISABLE_UNCOMMON_TAGS */
8490
Laurence Lundblade9b334962020-08-27 10:55:53 -07008491 QCBORDecode_GetBinaryUUIDInMapN(&DC, 71, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008492 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
8493 return 71;
8494 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07008495 QCBORDecode_GetBinaryUUIDInMapN(&DC, 72, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07008496 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008497 return 72;
8498 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07008499 QCBORDecode_GetBinaryUUIDInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07008500 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008501 return 73;
8502 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07008503
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07008504 // Improvement: add some more error test cases
8505
Laurence Lundblade37f46e52020-08-04 03:32:14 -07008506 QCBORDecode_ExitMap(&DC);
8507
8508 uErr = QCBORDecode_Finish(&DC);
8509 if(uErr != QCBOR_SUCCESS) {
8510 return 100;
8511 }
8512
8513 return 0;
8514}
Laurence Lundbladea4308a82020-10-03 18:08:57 -07008515
8516
8517
8518
8519/*
Laurence Lundbladecc7da412020-12-27 00:09:07 -08008520 [
8521 "aaaaaaaaaa",
8522 {}
8523 ]
Laurence Lundbladea4308a82020-10-03 18:08:57 -07008524 */
8525static const uint8_t spTooLarge1[] = {
8526 0x9f,
8527 0x6a, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61,
8528 0xa0,
8529 0xff
8530};
8531
8532/*
Laurence Lundbladecc7da412020-12-27 00:09:07 -08008533 [
8534 {
8535 0: "aaaaaaaaaa"
8536 }
8537 ]
Laurence Lundbladea4308a82020-10-03 18:08:57 -07008538 */
8539static const uint8_t spTooLarge2[] = {
8540 0x9f,
8541 0xa1,
8542 0x00,
8543 0x6a, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61,
8544 0xff
8545};
8546
8547/*
Laurence Lundbladecc7da412020-12-27 00:09:07 -08008548 h'A1006A61616161616161616161'
Laurence Lundbladea4308a82020-10-03 18:08:57 -07008549
Laurence Lundbladecc7da412020-12-27 00:09:07 -08008550 {
8551 0: "aaaaaaaaaa"
8552 }
Laurence Lundbladea4308a82020-10-03 18:08:57 -07008553 */
8554static const uint8_t spTooLarge3[] = {
8555 0x4d,
8556 0xa1,
8557 0x00,
8558 0x6a, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61,
8559};
8560
8561int32_t TooLargeInputTest(void)
8562{
8563 QCBORDecodeContext DC;
8564 QCBORError uErr;
8565 UsefulBufC String;
8566
8567 // These tests require a build with QCBOR_MAX_DECODE_INPUT_SIZE set
8568 // to 10 There's not really any way to test this error
8569 // condition. The error condition is not complex, so setting
8570 // QCBOR_MAX_DECODE_INPUT_SIZE gives an OK test.
8571
8572 // The input CBOR is only too large because the
8573 // QCBOR_MAX_DECODE_INPUT_SIZE is 10.
8574 //
8575 // This test is disabled for the normal test runs because of the
8576 // special build requirement.
8577
8578
8579 // Tests the start of a map being too large
8580 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTooLarge1), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07008581 QCBORDecode_EnterArray(&DC, NULL);
Laurence Lundbladea4308a82020-10-03 18:08:57 -07008582 QCBORDecode_GetTextString(&DC, &String);
8583 uErr = QCBORDecode_GetError(&DC);
8584 if(uErr != QCBOR_SUCCESS) {
8585 return 1;
8586 }
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07008587 QCBORDecode_EnterMap(&DC, NULL);
Laurence Lundbladea4308a82020-10-03 18:08:57 -07008588 uErr = QCBORDecode_GetError(&DC);
8589 if(uErr != QCBOR_ERR_INPUT_TOO_LARGE) {
8590 return 2;
8591 }
8592
8593 // Tests the end of a map being too large
8594 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTooLarge2), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07008595 QCBORDecode_EnterArray(&DC, NULL);
8596 QCBORDecode_EnterMap(&DC, NULL);
Laurence Lundbladea4308a82020-10-03 18:08:57 -07008597 uErr = QCBORDecode_GetError(&DC);
8598 if(uErr != QCBOR_SUCCESS) {
8599 return 3;
8600 }
8601 QCBORDecode_ExitMap(&DC);
8602 uErr = QCBORDecode_GetError(&DC);
8603 if(uErr != QCBOR_ERR_INPUT_TOO_LARGE) {
8604 return 4;
8605 }
8606
8607 // Tests the entire input CBOR being too large when processing bstr wrapping
8608 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTooLarge3), QCBOR_DECODE_MODE_NORMAL);
8609 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
8610 uErr = QCBORDecode_GetError(&DC);
8611 if(uErr != QCBOR_ERR_INPUT_TOO_LARGE) {
8612 return 5;
8613 }
8614
8615 return 0;
8616}
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07008617
8618
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08008619#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
8620
Laurence Lundblade37286c02022-09-03 10:05:02 -07008621/*
8622 An array of three map entries
8623 1) Indefinite length string label for indefinite lenght byte string
8624 2) Indefinite length string label for an integer
8625 3) Indefinite length string label for an indefinite-length negative big num
8626 */
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07008627static const uint8_t spMapWithIndefLenStrings[] = {
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08008628 0xa3,
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07008629 0x7f, 0x61, 'l', 0x64, 'a', 'b', 'e', 'l' , 0x61, '1', 0xff,
8630 0x5f, 0x42, 0x01, 0x02, 0x43, 0x03, 0x04, 0x05, 0xff,
8631 0x7f, 0x62, 'd', 'y', 0x61, 'm', 0x61, 'o', 0xff,
8632 0x03,
8633 0x7f, 0x62, 'l', 'a', 0x63, 'b', 'e', 'l', 0x61, '2', 0xff,
8634 0xc3,
8635 0x5f, 0x42, 0x00, 0x01, 0x42, 0x00, 0x01, 0x41, 0x01, 0xff,
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07008636};
8637
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03008638int32_t SpiffyIndefiniteLengthStringsTests(void)
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07008639{
8640 QCBORDecodeContext DCtx;
8641
8642 QCBORDecode_Init(&DCtx,
8643 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spMapWithIndefLenStrings),
8644 QCBOR_DECODE_MODE_NORMAL);
8645
Laurence Lundblade8510f8c2020-12-01 11:31:16 -08008646 UsefulBuf_MAKE_STACK_UB(StringBuf, 200);
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07008647 QCBORDecode_SetMemPool(&DCtx, StringBuf, false);
8648
8649 UsefulBufC ByteString;
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07008650 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07008651 QCBORDecode_GetByteStringInMapSZ(&DCtx, "label1", &ByteString);
Laurence Lundblade37286c02022-09-03 10:05:02 -07008652
8653#ifndef QCBOR_DISABLE_TAGS
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07008654 if(QCBORDecode_GetAndResetError(&DCtx)) {
8655 return 1;
8656 }
8657
8658 const uint8_t pExectedBytes[] = {0x01, 0x02, 0x03, 0x04, 0x05};
8659 if(UsefulBuf_Compare(ByteString, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pExectedBytes))) {
8660 return 2;
8661 }
8662
8663 uint64_t uInt;
8664 QCBORDecode_GetUInt64InMapSZ(&DCtx, "dymo", &uInt);
8665 if(QCBORDecode_GetAndResetError(&DCtx)) {
8666 return 3;
8667 }
8668 if(uInt != 3) {
8669 return 4;
8670 }
8671
Máté Tóth-Pálef5f07a2021-09-17 19:31:37 +02008672#ifndef USEFULBUF_DISABLE_ALL_FLOAT
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07008673 double uDouble;
8674 QCBORDecode_GetDoubleConvertAllInMapSZ(&DCtx,
8675 "label2",
8676 0xff,
8677 &uDouble);
Laurence Lundblade37286c02022-09-03 10:05:02 -07008678
Laurence Lundbladeb8e19aa2020-10-07 20:59:11 -07008679#ifndef QCBOR_DISABLE_FLOAT_HW_USE
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07008680 if(QCBORDecode_GetAndResetError(&DCtx)) {
8681 return 5;
8682 }
8683 if(uDouble != -16777474) {
8684 return 6;
8685 }
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08008686#else /* QCBOR_DISABLE_FLOAT_HW_USE */
Laurence Lundbladeb8e19aa2020-10-07 20:59:11 -07008687 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_HW_FLOAT_DISABLED) {
8688 return 7;
8689 }
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08008690#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
Máté Tóth-Pálef5f07a2021-09-17 19:31:37 +02008691#endif /* USEFULBUF_DISABLE_ALL_FLOAT */
Laurence Lundbladeb8e19aa2020-10-07 20:59:11 -07008692
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07008693 QCBORDecode_ExitMap(&DCtx);
8694
8695 if(QCBORDecode_Finish(&DCtx)) {
8696 return 99;
8697 }
8698
Laurence Lundblade37286c02022-09-03 10:05:02 -07008699#else /* QCBOR_DISABLE_TAGS */
8700 /* The big num in the input is a CBOR tag and you can't do
8701 * map lookups in a map with a tag so this test does very little
8702 * when tags are disabled. That is OK, the test coverage is still
8703 * good when they are not.
8704 */
8705 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_TAGS_DISABLED) {
8706 return 1002;
8707 }
8708#endif /*QCBOR_DISABLE_TAGS */
8709
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07008710 return 0;
8711}
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08008712#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
Laurence Lundblade2a26abb2020-11-05 19:06:54 -08008713
8714
Laurence Lundbladeec290b82024-06-10 11:10:54 -07008715#ifndef QCBOR_DISABLE_NON_INTEGER_LABELS
Laurence Lundbladecf41c522021-02-20 10:19:07 -07008716/*
8717 * An array of an integer and an array. The second array contains
8718 * a bstr-wrapped map.
8719 *
8720 * [7, [h'A36D6669... (see next lines) 73']]
8721 *
8722 * {"first integer": 42,
8723 * "an array of two strings": ["string1", "string2"],
8724 * "map in a map":
8725 * { "bytes 1": h'78787878',
8726 * "bytes 2": h'79797979',
8727 * "another int": 98,
8728 * "text 2": "lies, damn lies and statistics"
8729 * }
8730 * }
8731 */
Laurence Lundblade2a26abb2020-11-05 19:06:54 -08008732
Laurence Lundbladecf41c522021-02-20 10:19:07 -07008733static const uint8_t pValidWrappedMapEncoded[] = {
8734 0x82, 0x07, 0x81, 0x58, 0x97,
8735 0xa3, 0x6d, 0x66, 0x69, 0x72, 0x73, 0x74, 0x20, 0x69, 0x6e,
8736 0x74, 0x65, 0x67, 0x65, 0x72, 0x18, 0x2a, 0x77, 0x61, 0x6e,
8737 0x20, 0x61, 0x72, 0x72, 0x61, 0x79, 0x20, 0x6f, 0x66, 0x20,
8738 0x74, 0x77, 0x6f, 0x20, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67,
8739 0x73, 0x82, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x31,
8740 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x32, 0x6c, 0x6d,
8741 0x61, 0x70, 0x20, 0x69, 0x6e, 0x20, 0x61, 0x20, 0x6d, 0x61,
8742 0x70, 0xa4, 0x67, 0x62, 0x79, 0x74, 0x65, 0x73, 0x20, 0x31,
8743 0x44, 0x78, 0x78, 0x78, 0x78, 0x67, 0x62, 0x79, 0x74, 0x65,
8744 0x73, 0x20, 0x32, 0x44, 0x79, 0x79, 0x79, 0x79, 0x6b, 0x61,
8745 0x6e, 0x6f, 0x74, 0x68, 0x65, 0x72, 0x20, 0x69, 0x6e, 0x74,
8746 0x18, 0x62, 0x66, 0x74, 0x65, 0x78, 0x74, 0x20, 0x32, 0x78,
8747 0x1e, 0x6c, 0x69, 0x65, 0x73, 0x2c, 0x20, 0x64, 0x61, 0x6d,
8748 0x6e, 0x20, 0x6c, 0x69, 0x65, 0x73, 0x20, 0x61, 0x6e, 0x64,
8749 0x20, 0x73, 0x74, 0x61, 0x74, 0x69, 0x73, 0x74, 0x69, 0x63,
8750 0x73
8751};
8752
8753#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
8754
8755/* As above, but the arrays are indefinite length */
8756static const uint8_t pValidIndefWrappedMapEncoded[] = {
8757 0x9f, 0x07, 0x9f, 0x58, 0x97,
8758 0xa3, 0x6d, 0x66, 0x69, 0x72, 0x73, 0x74, 0x20, 0x69, 0x6e,
8759 0x74, 0x65, 0x67, 0x65, 0x72, 0x18, 0x2a, 0x77, 0x61, 0x6e,
8760 0x20, 0x61, 0x72, 0x72, 0x61, 0x79, 0x20, 0x6f, 0x66, 0x20,
8761 0x74, 0x77, 0x6f, 0x20, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67,
8762 0x73, 0x82, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x31,
8763 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x32, 0x6c, 0x6d,
8764 0x61, 0x70, 0x20, 0x69, 0x6e, 0x20, 0x61, 0x20, 0x6d, 0x61,
8765 0x70, 0xa4, 0x67, 0x62, 0x79, 0x74, 0x65, 0x73, 0x20, 0x31,
8766 0x44, 0x78, 0x78, 0x78, 0x78, 0x67, 0x62, 0x79, 0x74, 0x65,
8767 0x73, 0x20, 0x32, 0x44, 0x79, 0x79, 0x79, 0x79, 0x6b, 0x61,
8768 0x6e, 0x6f, 0x74, 0x68, 0x65, 0x72, 0x20, 0x69, 0x6e, 0x74,
8769 0x18, 0x62, 0x66, 0x74, 0x65, 0x78, 0x74, 0x20, 0x32, 0x78,
8770 0x1e, 0x6c, 0x69, 0x65, 0x73, 0x2c, 0x20, 0x64, 0x61, 0x6d,
8771 0x6e, 0x20, 0x6c, 0x69, 0x65, 0x73, 0x20, 0x61, 0x6e, 0x64,
8772 0x20, 0x73, 0x74, 0x61, 0x74, 0x69, 0x73, 0x74, 0x69, 0x63,
8773 0x73,
8774 0xff, 0xff
8775};
Laurence Lundbladeec290b82024-06-10 11:10:54 -07008776#endif /* ! QCBOR_DISABLE_NON_INTEGER_LABELS */
Laurence Lundbladecf41c522021-02-20 10:19:07 -07008777
8778static const uint8_t pWithEmptyMap[] = {0x82, 0x18, 0x64, 0xa0};
8779
Laurence Lundbladeec290b82024-06-10 11:10:54 -07008780
Laurence Lundbladecf41c522021-02-20 10:19:07 -07008781#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
8782static const uint8_t pWithEmptyMapInDef[] = {0x9f, 0x18, 0x64, 0xbf, 0xff, 0xff};
8783#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
8784
8785#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
Laurence Lundblade37286c02022-09-03 10:05:02 -07008786/*
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08008787 * An array of one that contains a byte string that is an indefinite
8788 * length string that CBOR wraps an array of three numbers [42, 43,
8789 * 44]. The byte string is an implicit tag 24.
8790 *
8791 * [
8792 * (_ h'83', h'18', h'2A182B', h'182C')
8793 * ]
Laurence Lundblade37286c02022-09-03 10:05:02 -07008794 */
Laurence Lundbladecf41c522021-02-20 10:19:07 -07008795static const uint8_t pWrappedByIndefiniteLength[] = {
8796 0x81,
Laurence Lundbladecf41c522021-02-20 10:19:07 -07008797 0x5f,
8798 0x41, 0x83,
8799 0x41, 0x18,
8800 0x43, 0x2A, 0x18, 0x2B,
8801 0x42, 0x18, 0x2C,
8802 0xff
8803};
8804#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
8805
Laurence Lundbladeec290b82024-06-10 11:10:54 -07008806#endif
Laurence Lundbladecf41c522021-02-20 10:19:07 -07008807
Laurence Lundbladeec290b82024-06-10 11:10:54 -07008808#ifndef QCBOR_DISABLE_NON_INTEGER_LABELS
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03008809int32_t PeekAndRewindTest(void)
Laurence Lundblade2a26abb2020-11-05 19:06:54 -08008810{
8811 QCBORItem Item;
8812 QCBORError nCBORError;
8813 QCBORDecodeContext DCtx;
8814
Laurence Lundbladeec290b82024-06-10 11:10:54 -07008815 // Improvement: rework this test to use only integer labels.
8816
Laurence Lundblade2a26abb2020-11-05 19:06:54 -08008817 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0);
8818
8819 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
8820 return 100+(int32_t)nCBORError;
8821 }
8822 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
8823 return 200;
8824 }
8825
Laurence Lundblade3427dee2021-06-20 11:11:24 -07008826 QCBORDecode_VPeekNext(&DCtx, &Item);
8827 if((nCBORError = QCBORDecode_GetError(&DCtx))) {
8828 return 150+(int32_t)nCBORError;
8829 }
8830 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
8831 return 250;
8832 }
8833
Laurence Lundblade2a26abb2020-11-05 19:06:54 -08008834 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
8835 return (int32_t)nCBORError;
8836 }
8837 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
8838 return 300;
8839 }
8840
8841 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
8842 return 400 + (int32_t)nCBORError;
8843 }
8844 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
8845 return 500;
8846 }
8847
8848 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8849 return (int32_t)nCBORError;
8850 }
8851 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
8852 return 600;
8853 }
8854
8855 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
8856 return 900 + (int32_t)nCBORError;
8857 }
8858 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
8859 Item.uDataType != QCBOR_TYPE_INT64 ||
8860 Item.val.int64 != 42 ||
8861 Item.uDataAlloc ||
8862 Item.uLabelAlloc ||
8863 UsefulBufCompareToSZ(Item.label.string, "first integer")) {
8864 return 1000;
8865 }
8866
8867 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8868 return 1100 + (int32_t)nCBORError;
8869 }
8870
8871 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
8872 Item.uDataType != QCBOR_TYPE_INT64 ||
8873 Item.val.int64 != 42 ||
8874 Item.uDataAlloc ||
8875 Item.uLabelAlloc ||
8876 UsefulBufCompareToSZ(Item.label.string, "first integer")) {
8877 return 1200;
8878 }
8879
8880
8881 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8882 return 1300 + (int32_t)nCBORError;
8883 }
8884 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
8885 Item.uDataAlloc ||
8886 Item.uLabelAlloc ||
8887 UsefulBufCompareToSZ(Item.label.string, "an array of two strings") ||
8888 Item.uDataType != QCBOR_TYPE_ARRAY ||
Laurence Lundbladecf41c522021-02-20 10:19:07 -07008889 Item.val.uCount != 2) {
Laurence Lundblade2a26abb2020-11-05 19:06:54 -08008890 return 1400;
Laurence Lundbladecf41c522021-02-20 10:19:07 -07008891 }
Laurence Lundblade2a26abb2020-11-05 19:06:54 -08008892
8893 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8894 return 1500 + (int32_t)nCBORError;
8895 }
8896 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
8897 Item.uDataAlloc ||
8898 Item.uLabelAlloc ||
8899 UsefulBufCompareToSZ(Item.val.string, "string1")) {
8900 return 1600;
8901 }
8902
8903 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
8904 return 1700 + (int32_t)nCBORError;
8905 }
8906 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
8907 Item.uDataAlloc ||
8908 Item.uLabelAlloc ||
8909 UsefulBufCompareToSZ(Item.val.string, "string2")) {
8910 return 1800;
8911 }
8912
8913 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
8914 return (int32_t)nCBORError;
8915 }
8916 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
8917 Item.uDataAlloc ||
8918 Item.uLabelAlloc ||
8919 UsefulBufCompareToSZ(Item.val.string, "string2")) {
8920 return 1900;
8921 }
8922
8923 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8924 return (int32_t)nCBORError;
8925 }
8926 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
8927 Item.uDataAlloc ||
8928 Item.uLabelAlloc ||
8929 UsefulBufCompareToSZ(Item.val.string, "string2")) {
8930 return 2000;
8931 }
8932
8933
8934 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8935 return 2100 + (int32_t)nCBORError;
8936 }
8937 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
8938 Item.uDataAlloc ||
8939 Item.uLabelAlloc ||
8940 UsefulBufCompareToSZ(Item.label.string, "map in a map") ||
8941 Item.uDataType != QCBOR_TYPE_MAP ||
8942 Item.val.uCount != 4) {
8943 return 2100;
8944 }
8945
8946 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8947 return 2200 + (int32_t)nCBORError;
8948 }
8949 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
8950 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("bytes 1"))||
8951 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
8952 Item.uDataAlloc ||
8953 Item.uLabelAlloc ||
8954 UsefulBufCompareToSZ(Item.val.string, "xxxx")) {
8955 return 2300;
8956 }
8957
8958 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
8959 return 2400 + (int32_t)nCBORError;
8960 }
8961 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
8962 UsefulBufCompareToSZ(Item.label.string, "bytes 2") ||
8963 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
8964 Item.uDataAlloc ||
8965 Item.uLabelAlloc ||
8966 UsefulBufCompareToSZ(Item.val.string, "yyyy")) {
8967 return 2500;
8968 }
8969
8970 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8971 return 2600 + (int32_t)nCBORError;
8972 }
8973 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
8974 UsefulBufCompareToSZ(Item.label.string, "bytes 2") ||
8975 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
8976 Item.uDataAlloc ||
8977 Item.uLabelAlloc ||
8978 UsefulBufCompareToSZ(Item.val.string, "yyyy")) {
8979 return 2700;
8980 }
8981
8982 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8983 return 2800 + (int32_t)nCBORError;
8984 }
8985 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
8986 Item.uDataAlloc ||
8987 Item.uLabelAlloc ||
8988 UsefulBufCompareToSZ(Item.label.string, "another int") ||
8989 Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundbladecf41c522021-02-20 10:19:07 -07008990 Item.val.int64 != 98) {
Laurence Lundblade2a26abb2020-11-05 19:06:54 -08008991 return 2900;
Laurence Lundbladecf41c522021-02-20 10:19:07 -07008992 }
Laurence Lundblade2a26abb2020-11-05 19:06:54 -08008993
8994 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
8995 return 3000 + (int32_t)nCBORError;
8996 }
8997 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
8998 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("text 2"))||
8999 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
9000 Item.uDataAlloc ||
9001 Item.uLabelAlloc ||
9002 UsefulBufCompareToSZ(Item.val.string, "lies, damn lies and statistics")) {
9003 return 3100;
9004 }
9005
9006 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
9007 return 3200 + (int32_t)nCBORError;
9008 }
9009 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
9010 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("text 2"))||
9011 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
9012 Item.uDataAlloc ||
9013 Item.uLabelAlloc ||
9014 UsefulBufCompareToSZ(Item.val.string, "lies, damn lies and statistics")) {
9015 return 3300;
9016 }
9017
Laurence Lundblade3427dee2021-06-20 11:11:24 -07009018 nCBORError = QCBORDecode_PeekNext(&DCtx, &Item);
9019 if(nCBORError != QCBOR_ERR_NO_MORE_ITEMS) {
9020 return 3300 + (int32_t)nCBORError;
9021 }
9022
9023 QCBORDecode_VPeekNext(&DCtx, &Item);
9024 nCBORError = QCBORDecode_GetError(&DCtx);
9025 if(nCBORError != QCBOR_ERR_NO_MORE_ITEMS) {
9026 return 3400 + (int32_t)nCBORError;
9027 }
9028
9029 QCBORDecode_VPeekNext(&DCtx, &Item);
9030 nCBORError = QCBORDecode_GetError(&DCtx);
9031 if(nCBORError != QCBOR_ERR_NO_MORE_ITEMS) {
9032 return 3500 + (int32_t)nCBORError;
9033 }
Laurence Lundbladecf41c522021-02-20 10:19:07 -07009034
9035
9036 // Rewind to top level after entering several maps
9037 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0);
9038
9039 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
9040 return (int32_t)nCBORError;
9041 }
9042 if(Item.uDataType != QCBOR_TYPE_MAP ||
9043 Item.val.uCount != 3) {
9044 return 400;
9045 }
9046
9047 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
9048 return 4000+(int32_t)nCBORError;
9049 }
9050
9051 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
9052 Item.uDataType != QCBOR_TYPE_INT64 ||
9053 Item.val.int64 != 42 ||
9054 Item.uDataAlloc ||
9055 Item.uLabelAlloc ||
9056 UsefulBufCompareToSZ(Item.label.string, "first integer")) {
9057 return 4100;
9058 }
9059
9060 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
9061 return 4100+(int32_t)nCBORError;
9062 }
9063 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
9064 Item.uDataAlloc ||
9065 Item.uLabelAlloc ||
9066 UsefulBufCompareToSZ(Item.label.string, "an array of two strings") ||
9067 Item.uDataType != QCBOR_TYPE_ARRAY ||
9068 Item.val.uCount != 2) {
9069 return 4200;
9070 }
9071
9072 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
9073 return 4200+(int32_t)nCBORError;
9074 }
9075 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
9076 Item.uDataAlloc ||
9077 Item.uLabelAlloc ||
9078 UsefulBufCompareToSZ(Item.val.string, "string1")) {
9079 return 4300;
9080 }
9081
9082 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
9083 return 4300+(int32_t)nCBORError;
9084 }
9085 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
9086 Item.uDataAlloc ||
9087 Item.uLabelAlloc ||
9088 UsefulBufCompareToSZ(Item.val.string, "string2")) {
9089 return 4400;
9090 }
9091
9092 QCBORDecode_Rewind(&DCtx);
9093
9094 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
9095 return 4400+(int32_t)nCBORError;
9096 }
9097 if(Item.uDataType != QCBOR_TYPE_MAP ||
9098 Item.val.uCount != 3) {
9099 return 4500;
9100 }
9101
9102 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
9103 return (int32_t)nCBORError;
9104 }
9105
9106 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
9107 Item.uDataType != QCBOR_TYPE_INT64 ||
9108 Item.val.int64 != 42 ||
9109 Item.uDataAlloc ||
9110 Item.uLabelAlloc ||
9111 UsefulBufCompareToSZ(Item.label.string, "first integer")) {
9112 return 4600;
9113 }
9114
9115 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
9116 return (int32_t)nCBORError;
9117 }
9118 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
9119 Item.uDataAlloc ||
9120 Item.uLabelAlloc ||
9121 UsefulBufCompareToSZ(Item.label.string, "an array of two strings") ||
9122 Item.uDataType != QCBOR_TYPE_ARRAY ||
9123 Item.val.uCount != 2) {
9124 return 4700;
9125 }
9126
9127 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
9128 return (int32_t)nCBORError;
9129 }
9130 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
9131 Item.uDataAlloc ||
9132 Item.uLabelAlloc ||
9133 UsefulBufCompareToSZ(Item.val.string, "string1")) {
9134 return 4800;
9135 }
9136
9137 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
9138 return 4900+(int32_t)nCBORError;
9139 }
9140 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
9141 Item.uDataAlloc ||
9142 Item.uLabelAlloc ||
9143 UsefulBufCompareToSZ(Item.val.string, "string2")) {
9144 return 5000;
9145 }
9146
9147
9148 // Rewind an entered map
9149 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0);
9150
9151 QCBORDecode_EnterMap(&DCtx, NULL);
9152
9153 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
9154 return 5100+(int32_t)nCBORError;
9155 }
9156
9157 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
9158 Item.uDataType != QCBOR_TYPE_INT64 ||
9159 Item.val.int64 != 42 ||
9160 Item.uDataAlloc ||
9161 Item.uLabelAlloc ||
9162 UsefulBufCompareToSZ(Item.label.string, "first integer")) {
9163 return 5200;
9164 }
9165
9166 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
9167 return 5200+(int32_t)nCBORError;
9168 }
9169 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
9170 Item.uDataAlloc ||
9171 Item.uLabelAlloc ||
9172 UsefulBufCompareToSZ(Item.label.string, "an array of two strings") ||
9173 Item.uDataType != QCBOR_TYPE_ARRAY ||
9174 Item.val.uCount != 2) {
9175 return -5300;
9176 }
9177
9178 QCBORDecode_Rewind(&DCtx);
9179
9180 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
9181 return 5300+(int32_t)nCBORError;
9182 }
9183
9184 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
9185 Item.uDataType != QCBOR_TYPE_INT64 ||
9186 Item.val.int64 != 42 ||
9187 Item.uDataAlloc ||
9188 Item.uLabelAlloc ||
9189 UsefulBufCompareToSZ(Item.label.string, "first integer")) {
9190 return 5400;
9191 }
9192
9193 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
9194 return 5400+(int32_t)nCBORError;
9195 }
9196 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
9197 Item.uDataAlloc ||
9198 Item.uLabelAlloc ||
9199 UsefulBufCompareToSZ(Item.label.string, "an array of two strings") ||
9200 Item.uDataType != QCBOR_TYPE_ARRAY ||
9201 Item.val.uCount != 2) {
9202 return 5500;
9203 }
9204
9205
9206 // Rewind and entered array inside an entered map
9207 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0);
9208
9209 QCBORDecode_EnterMap(&DCtx, NULL);
9210
9211 QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings");
9212
9213 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
9214 return 5600+(int32_t)nCBORError;
9215 }
9216 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
9217 Item.uDataAlloc ||
9218 Item.uLabelAlloc ||
9219 UsefulBufCompareToSZ(Item.val.string, "string1")) {
9220 return 5700;
9221 }
9222
9223 QCBORDecode_Rewind(&DCtx);
9224
9225 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
9226 return 5700+(int32_t)nCBORError;
9227 }
9228 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
9229 Item.uDataAlloc ||
9230 Item.uLabelAlloc ||
9231 UsefulBufCompareToSZ(Item.val.string, "string1")) {
9232 return 5800;
9233 }
9234
9235 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
9236 return (int32_t)nCBORError;
9237 }
9238 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
9239 Item.uDataAlloc ||
9240 Item.uLabelAlloc ||
9241 UsefulBufCompareToSZ(Item.val.string, "string2")) {
9242 return 5900;
9243 }
9244
9245 QCBORDecode_Rewind(&DCtx);
9246
9247 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
9248 return 5900+(int32_t)nCBORError;
9249 }
9250 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
9251 Item.uDataAlloc ||
9252 Item.uLabelAlloc ||
9253 UsefulBufCompareToSZ(Item.val.string, "string1")) {
9254 return 6000;
9255 }
9256
9257
9258 // Rewind a byte string inside an array inside an array
9259 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidWrappedMapEncoded), 0);
9260
9261 QCBORDecode_EnterArray(&DCtx, NULL);
9262
9263 uint64_t i;
9264 QCBORDecode_GetUInt64(&DCtx, &i);
9265
9266 QCBORDecode_EnterArray(&DCtx, NULL);
9267
9268 QCBORDecode_EnterBstrWrapped(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
9269 if(QCBORDecode_GetError(&DCtx)) {
9270 return 6100;
9271 }
9272
9273 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
9274 return (int32_t)nCBORError;
9275 }
9276 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
9277 return 6200;
9278 }
9279
9280 QCBORDecode_Rewind(&DCtx);
9281
9282 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
9283 return 6300+(int32_t)nCBORError;
9284 }
9285 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
9286 return 6400;
9287 }
9288
9289#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
9290 // Rewind a byte string inside an indefinite-length array inside
9291 // indefinite-length array
9292
9293 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidIndefWrappedMapEncoded), 0);
9294
9295 QCBORDecode_EnterArray(&DCtx, NULL);
9296
9297 QCBORDecode_GetUInt64(&DCtx, &i);
9298
9299 QCBORDecode_EnterArray(&DCtx, NULL);
9300
9301 QCBORDecode_EnterBstrWrapped(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
9302 if(QCBORDecode_GetError(&DCtx)) {
9303 return 6500;
9304 }
9305
9306 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
9307 return 6600+(int32_t)nCBORError;
9308 }
9309 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
9310 return 6700;
9311 }
9312
9313 QCBORDecode_Rewind(&DCtx);
9314
9315 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
9316 return 6800+(int32_t)nCBORError;
9317 }
9318 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
9319 return 6900;
9320 }
9321#endif
9322
9323 // Rewind an empty map
9324 // [100, {}]
9325 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pWithEmptyMap), 0);
9326 QCBORDecode_EnterArray(&DCtx, NULL);
9327 QCBORDecode_GetUInt64(&DCtx, &i);
9328 if(i != 100) {
9329 return 7010;
9330 }
9331 QCBORDecode_EnterMap(&DCtx, NULL);
9332
9333 /* Do it 5 times to be sure multiple rewinds work */
9334 for(int n = 0; n < 5; n++) {
9335 nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
9336 if(nCBORError != QCBOR_ERR_NO_MORE_ITEMS) {
9337 return 7000 + n;
9338 }
9339 QCBORDecode_Rewind(&DCtx);
9340 }
9341 QCBORDecode_ExitMap(&DCtx);
9342 QCBORDecode_Rewind(&DCtx);
9343 QCBORDecode_GetUInt64(&DCtx, &i);
9344 if(i != 100) {
9345 return 7010;
9346 }
9347 QCBORDecode_ExitArray(&DCtx);
9348 QCBORDecode_Rewind(&DCtx);
9349 QCBORDecode_EnterArray(&DCtx, NULL);
9350 i = 9;
9351 QCBORDecode_GetUInt64(&DCtx, &i);
9352 if(i != 100) {
9353 return 7020;
9354 }
9355 if(QCBORDecode_GetError(&DCtx)){
9356 return 7030;
9357 }
9358
9359 // Rewind an empty indefinite length map
9360#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
9361 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pWithEmptyMapInDef), 0);
9362 QCBORDecode_EnterArray(&DCtx, NULL);
9363 QCBORDecode_GetUInt64(&DCtx, &i);
9364 if(i != 100) {
9365 return 7810;
9366 }
9367 QCBORDecode_EnterMap(&DCtx, NULL);
9368
9369 /* Do it 5 times to be sure multiple rewinds work */
9370 for(int n = 0; n < 5; n++) {
9371 nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
9372 if(nCBORError != QCBOR_ERR_NO_MORE_ITEMS) {
9373 return 7800 + n;
9374 }
9375 QCBORDecode_Rewind(&DCtx);
9376 }
9377 QCBORDecode_ExitMap(&DCtx);
9378 QCBORDecode_Rewind(&DCtx);
9379 QCBORDecode_GetUInt64(&DCtx, &i);
9380 if(i != 100) {
9381 return 7810;
9382 }
9383 QCBORDecode_ExitArray(&DCtx);
9384 QCBORDecode_Rewind(&DCtx);
9385 QCBORDecode_EnterArray(&DCtx, NULL);
9386 i = 9;
9387 QCBORDecode_GetUInt64(&DCtx, &i);
9388 if(i != 100) {
9389 return 7820;
9390 }
9391 if(QCBORDecode_GetError(&DCtx)){
9392 return 7830;
9393 }
9394#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
9395
9396 // Rewind an indefnite length byte-string wrapped sequence
9397#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
9398 QCBORDecode_Init(&DCtx,
9399 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pWrappedByIndefiniteLength),
9400 0);
9401 UsefulBuf_MAKE_STACK_UB(Pool, 100);
9402 QCBORDecode_SetMemPool(&DCtx, Pool, 0);
9403
9404 QCBORDecode_EnterArray(&DCtx, NULL);
9405 QCBORDecode_EnterBstrWrapped(&DCtx, 2, NULL);
Laurence Lundblade31fddb72024-05-13 13:03:35 -07009406 if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_CANNOT_ENTER_ALLOCATED_STRING) {
Laurence Lundbladecf41c522021-02-20 10:19:07 -07009407 return 7300;
9408 }
9409
9410 /*
Laurence Lundblade31fddb72024-05-13 13:03:35 -07009411 Improvement: Fix QCBORDecode_EnterBstrWrapped() so it can work on
9412 allocated strings. This is a fairly big job because of all the
9413 UsefulBuf internal book keeping that needs tweaking.
Laurence Lundbladecf41c522021-02-20 10:19:07 -07009414 QCBORDecode_GetUInt64(&DCtx, &i);
9415 if(i != 42) {
9416 return 7110;
9417 }
9418 QCBORDecode_Rewind(&DCtx);
9419 QCBORDecode_GetUInt64(&DCtx, &i);
9420 if(i != 42) {
9421 return 7220;
Laurence Lundblade31fddb72024-05-13 13:03:35 -07009422 }
9423 */
Laurence Lundblade37286c02022-09-03 10:05:02 -07009424
Laurence Lundbladeec290b82024-06-10 11:10:54 -07009425#endif /* ! QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
Laurence Lundbladecf41c522021-02-20 10:19:07 -07009426
9427
9428 // Rewind an indefnite length byte-string wrapped sequence
9429
Laurence Lundblade2a26abb2020-11-05 19:06:54 -08009430 return 0;
9431}
Laurence Lundblade9f9c3732021-03-23 09:38:46 -07009432
Laurence Lundbladeec290b82024-06-10 11:10:54 -07009433#endif /* ! QCBOR_DISABLE_NON_INTEGER_LABELS */
Laurence Lundblade9f9c3732021-03-23 09:38:46 -07009434
9435
9436static const uint8_t spBooleansInMap[] =
9437{
9438 0xa1, 0x08, 0xf5
9439};
9440
9441static const uint8_t spBooleansInMapWrongType[] =
9442{
9443 0xa1, 0x08, 0xf6
9444};
9445
9446static const uint8_t spBooleansInMapNWF[] =
9447{
9448 0xa1, 0x08, 0x1a
9449};
9450
Laurence Lundblade8782dd32021-04-27 04:15:37 -07009451static const uint8_t spNullInMap[] =
9452{
9453 0xa1, 0x08, 0xf6
9454};
9455
9456static const uint8_t spUndefinedInMap[] =
9457{
9458 0xa1, 0x08, 0xf7
9459};
9460
Laurence Lundblade9f9c3732021-03-23 09:38:46 -07009461
9462int32_t BoolTest(void)
9463{
9464 QCBORDecodeContext DCtx;
9465 bool b;
9466
Laurence Lundblade8782dd32021-04-27 04:15:37 -07009467 QCBORDecode_Init(&DCtx,
9468 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMap),
9469 0);
Laurence Lundblade9f9c3732021-03-23 09:38:46 -07009470 QCBORDecode_EnterMap(&DCtx, NULL);
9471 QCBORDecode_GetBool(&DCtx, &b);
9472 if(QCBORDecode_GetAndResetError(&DCtx) || !b) {
9473 return 1;
9474 }
9475
9476 QCBORDecode_GetBoolInMapN(&DCtx, 7, &b);
9477 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_LABEL_NOT_FOUND) {
9478 return 2;
9479 }
9480
9481 QCBORDecode_GetBoolInMapN(&DCtx, 8, &b);
9482 if(QCBORDecode_GetAndResetError(&DCtx) || !b) {
9483 return 3;
9484 }
9485
9486
9487 QCBORDecode_GetBoolInMapSZ(&DCtx, "xx", &b);
9488 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_LABEL_NOT_FOUND) {
9489 return 4;
9490 }
9491
Laurence Lundblade8782dd32021-04-27 04:15:37 -07009492 QCBORDecode_Init(&DCtx,
9493 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMapWrongType),
9494 0);
Laurence Lundblade9f9c3732021-03-23 09:38:46 -07009495 QCBORDecode_EnterMap(&DCtx, NULL);
9496 QCBORDecode_GetBool(&DCtx, &b);
9497 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
9498 return 5;
9499 }
9500
Laurence Lundblade8782dd32021-04-27 04:15:37 -07009501 QCBORDecode_Init(&DCtx,
9502 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMapNWF),
9503 0);
Laurence Lundblade9f9c3732021-03-23 09:38:46 -07009504 QCBORDecode_EnterMap(&DCtx, NULL);
9505 QCBORDecode_GetBool(&DCtx, &b);
9506 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_HIT_END) {
9507 return 6;
9508 }
9509
Laurence Lundblade8782dd32021-04-27 04:15:37 -07009510
9511 QCBORDecode_Init(&DCtx,
9512 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spNullInMap),
9513 0);
9514 QCBORDecode_EnterMap(&DCtx, NULL);
9515 QCBORDecode_GetNull(&DCtx);
9516 if(QCBORDecode_GetAndResetError(&DCtx)) {
9517 return 7;
9518 }
9519
9520 QCBORDecode_Init(&DCtx,
9521 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMap),
9522 0);
9523 QCBORDecode_EnterMap(&DCtx, NULL);
9524 QCBORDecode_GetNull(&DCtx);
9525 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
9526 return 8;
9527 }
9528
9529 QCBORDecode_Init(&DCtx,
9530 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spNullInMap),
9531 0);
9532 QCBORDecode_EnterMap(&DCtx, NULL);
9533 QCBORDecode_GetNullInMapN(&DCtx, 8);
9534 if(QCBORDecode_GetAndResetError(&DCtx)) {
9535 return 9;
9536 }
9537
9538 QCBORDecode_Init(&DCtx,
9539 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMap),
9540 0);
9541 QCBORDecode_EnterMap(&DCtx, NULL);
9542 QCBORDecode_GetNullInMapN(&DCtx, 8);
9543 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
9544 return 10;
9545 }
9546
9547 QCBORDecode_Init(&DCtx,
9548 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMapNWF),
9549 0);
9550 QCBORDecode_EnterMap(&DCtx, NULL);
9551 QCBORDecode_GetUndefined(&DCtx);
9552 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_HIT_END) {
9553 return 11;
9554 }
9555
9556 QCBORDecode_Init(&DCtx,
9557 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUndefinedInMap),
9558 0);
9559 QCBORDecode_EnterMap(&DCtx, NULL);
9560 QCBORDecode_GetUndefined(&DCtx);
9561 if(QCBORDecode_GetAndResetError(&DCtx)) {
9562 return 12;
9563 }
9564
9565 QCBORDecode_Init(&DCtx,
9566 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMap),
9567 0);
9568 QCBORDecode_EnterMap(&DCtx, NULL);
9569 QCBORDecode_GetUndefined(&DCtx);
9570 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
9571 return 13;
9572 }
9573
9574 QCBORDecode_Init(&DCtx,
9575 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUndefinedInMap),
9576 0);
9577 QCBORDecode_EnterMap(&DCtx, NULL);
9578 QCBORDecode_GetUndefinedInMapN(&DCtx, 8);
9579 if(QCBORDecode_GetAndResetError(&DCtx)) {
9580 return 14;
9581 }
9582
9583 QCBORDecode_Init(&DCtx,
9584 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMap),
9585 0);
9586 QCBORDecode_EnterMap(&DCtx, NULL);
9587 QCBORDecode_GetUndefinedInMapN(&DCtx, 8);
9588 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
9589 return 15;
9590 }
9591
9592 QCBORDecode_Init(&DCtx,
9593 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMapNWF),
9594 0);
9595 QCBORDecode_EnterMap(&DCtx, NULL);
9596 QCBORDecode_GetUndefined(&DCtx);
9597 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_HIT_END) {
9598 return 15;
9599 }
9600
Laurence Lundblade9f9c3732021-03-23 09:38:46 -07009601 return 0;
9602}
Laurence Lundbladef00b8be2024-03-08 10:34:33 -08009603
9604
Laurence Lundbladeec290b82024-06-10 11:10:54 -07009605#ifndef QCBOR_DISABLE_NON_INTEGER_LABELS
Laurence Lundbladea29f45a2024-05-14 15:55:19 -07009606static const uint8_t spExpectedArray2s[] = {
9607 0x82, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67,
9608 0x31, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67,
9609 0x32};
9610
9611#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
9612static const uint8_t spExpectedArray2sIndef[] = {
9613 0x9f, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67,
9614 0x31, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67,
9615 0x32, 0xff};
9616#endif
9617
9618static const uint8_t spExpectedMap4[] = {
9619 0xa4, 0x67, 0x62, 0x79, 0x74, 0x65, 0x73, 0x20,
9620 0x31, 0x44, 0x78, 0x78, 0x78, 0x78, 0x67, 0x62,
9621 0x79, 0x74, 0x65, 0x73, 0x20, 0x32, 0x44, 0x79,
9622 0x79, 0x79, 0x79, 0x6b, 0x61, 0x6e, 0x6f, 0x74,
9623 0x68, 0x65, 0x72, 0x20, 0x69, 0x6e, 0x74, 0x18,
9624 0x62, 0x66, 0x74, 0x65, 0x78, 0x74, 0x20, 0x32,
9625 0x78, 0x1e, 0x6c, 0x69, 0x65, 0x73, 0x2c, 0x20,
9626 0x64, 0x61, 0x6d, 0x6e, 0x20, 0x6c, 0x69, 0x65,
9627 0x73, 0x20, 0x61, 0x6e, 0x64, 0x20, 0x73, 0x74,
9628 0x61, 0x74, 0x69, 0x73, 0x74, 0x69, 0x63, 0x73};
9629
9630
9631#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
9632
9633static const uint8_t spExpectedMap4Indef[] = {
9634 0xbf, 0x67, 0x62, 0x79, 0x74, 0x65, 0x73, 0x20,
9635 0x31, 0x44, 0x78, 0x78, 0x78, 0x78, 0x67, 0x62,
9636 0x79, 0x74, 0x65, 0x73, 0x20, 0x32, 0x44, 0x79,
9637 0x79, 0x79, 0x79, 0x6b, 0x61, 0x6e, 0x6f, 0x74,
9638 0x68, 0x65, 0x72, 0x20, 0x69, 0x6e, 0x74, 0x18,
9639 0x62, 0x66, 0x74, 0x65, 0x78, 0x74, 0x20, 0x32,
9640 0x78, 0x1e, 0x6c, 0x69, 0x65, 0x73, 0x2c, 0x20,
9641 0x64, 0x61, 0x6d, 0x6e, 0x20, 0x6c, 0x69, 0x65,
9642 0x73, 0x20, 0x61, 0x6e, 0x64, 0x20, 0x73, 0x74,
9643 0x61, 0x74, 0x69, 0x73, 0x74, 0x69, 0x63, 0x73,
9644 0xff};
9645
9646/*
9647 * [[[[[0, []]]]], 0]
9648 */
9649static const uint8_t spDefAndIndef[] = {
9650 0x82,
9651 0x9f, 0x9f, 0x9f, 0x82, 0x00, 0x9f, 0xff, 0xff, 0xff, 0xff, 0x00
9652};
9653#endif /* !QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
9654
Laurence Lundbladea29f45a2024-05-14 15:55:19 -07009655#ifndef QCBOR_DISABLE_TAGS
9656/* An exp / mant tag in two nested arrays */
9657static const uint8_t spExpMant[] = {0x81, 0x81, 0xC4, 0x82, 0x20, 0x03};
9658#endif /* !QCBOR_DISABLE_TAGS */
Laurence Lundbladeec290b82024-06-10 11:10:54 -07009659#endif
Laurence Lundbladea29f45a2024-05-14 15:55:19 -07009660
Laurence Lundbladeec290b82024-06-10 11:10:54 -07009661#ifndef QCBOR_DISABLE_NON_INTEGER_LABELS
Laurence Lundbladea29f45a2024-05-14 15:55:19 -07009662int32_t GetMapAndArrayTest(void)
9663{
9664 QCBORDecodeContext DCtx;
9665 size_t uPosition ;
9666 QCBORItem Item;
9667 UsefulBufC ReturnedEncodedCBOR;
9668
Laurence Lundbladeec290b82024-06-10 11:10:54 -07009669 // Improvement: rework so it can run with QCBOR_DISABLE_NON_INTEGER_LABELS
Laurence Lundbladea29f45a2024-05-14 15:55:19 -07009670 QCBORDecode_Init(&DCtx,
9671 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded),
9672 0);
9673
9674 QCBORDecode_EnterMap(&DCtx, NULL);
9675 QCBORDecode_VGetNextConsume(&DCtx, &Item);
9676 QCBORDecode_GetArray(&DCtx, &Item, &ReturnedEncodedCBOR);
9677 if(QCBORDecode_GetError(&DCtx)) {
9678 return 1;
9679 }
9680 if(Item.val.uCount != 2) {
9681 return 2;
9682 }
9683 if(UsefulBuf_Compare(ReturnedEncodedCBOR, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedArray2s))) {
9684 return 3;
9685 }
9686
9687 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
9688 UsefulBuf_Compare(Item.label.string, UsefulBuf_FROM_SZ_LITERAL("an array of two strings"))) {
9689 return 4;
9690 }
9691
9692 uPosition = QCBORDecode_Tell(&DCtx);
9693
9694
9695 QCBORDecode_GetMap(&DCtx, &Item, &ReturnedEncodedCBOR);
9696 if(QCBORDecode_GetError(&DCtx)) {
9697 return 10;
9698 }
9699 if(Item.val.uCount != 4) {
9700 return 11;
9701 }
9702 if(UsefulBuf_Compare(ReturnedEncodedCBOR, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedMap4))) {
9703 return 12;
9704 }
9705 uPosition = QCBORDecode_Tell(&DCtx);
9706 QCBORDecode_GetArrayFromMapSZ(&DCtx,
9707 "an array of two strings",
9708 &Item,
9709 &ReturnedEncodedCBOR);
9710 if(QCBORDecode_GetError(&DCtx)) {
9711 return 20;
9712 }
9713 if(Item.val.uCount != 2) {
9714 return 21;
9715 }
9716 if(UsefulBuf_Compare(ReturnedEncodedCBOR, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedArray2s))) {
9717 return 22;
9718 }
9719 if(uPosition != QCBORDecode_Tell(&DCtx)) {
9720 return 23;
9721 }
9722
9723 QCBORDecode_Rewind(&DCtx);
9724
9725 uPosition = QCBORDecode_Tell(&DCtx);
9726 QCBORDecode_GetMapFromMapSZ(&DCtx, "map in a map", &Item, &ReturnedEncodedCBOR);
9727 if(QCBORDecode_GetError(&DCtx)) {
9728 return 30;
9729 }
9730 if(Item.val.uCount != 4) {
9731 return 31;
9732 }
9733 if(UsefulBuf_Compare(ReturnedEncodedCBOR, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedMap4))) {
9734 return 32;
9735 }
9736 if(uPosition != QCBORDecode_Tell(&DCtx)) {
9737 return 33;
9738 }
9739
9740 uPosition = QCBORDecode_Tell(&DCtx);
9741 QCBORDecode_GetArrayFromMapSZ(&DCtx, "map in a map", &Item, &ReturnedEncodedCBOR);
9742 if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
9743 return 40;
9744 }
9745 if(UINT32_MAX != QCBORDecode_Tell(&DCtx)) {
9746 return 41;
9747 }
9748 QCBORDecode_GetAndResetError(&DCtx);
9749 if(uPosition != QCBORDecode_Tell(&DCtx)) {
9750 return 42;
9751 }
9752
9753
9754#ifndef QCBOR_DISABLE_TAGS
9755 UsefulBufC ExpMant = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpMant);
9756 QCBORDecode_Init(&DCtx, ExpMant, 0);
9757 QCBORDecode_EnterArray(&DCtx, NULL);
9758 QCBORDecode_EnterArray(&DCtx, NULL);
9759 QCBORDecode_GetArray(&DCtx, &Item, &ReturnedEncodedCBOR);
9760 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) {
9761 return 200;
9762 }
9763 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
9764 return 201;
9765 }
9766 if(!QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_DECIMAL_FRACTION)) {
9767 return 202;
9768 }
9769 if(Item.val.uCount != 2) {
9770 return 201;
9771 }
9772 if(UsefulBuf_Compare(ReturnedEncodedCBOR, UsefulBuf_Tail(ExpMant, 2))) {
9773 return 205;
9774 }
9775#endif /* !QCBOR_DISABLE_TAGS */
9776
9777
9778#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
9779
9780 UsefulBufC DefAndIndef = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spDefAndIndef);
9781 QCBORDecode_Init(&DCtx, DefAndIndef, 0);
9782 QCBORDecode_EnterArray(&DCtx, NULL);
9783 QCBORDecode_GetArray(&DCtx, &Item, &ReturnedEncodedCBOR);
9784 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) {
9785 return 50;
9786 }
9787 if(UsefulBuf_Compare(ReturnedEncodedCBOR, UsefulBuf_Tail(UsefulBuf_Head(DefAndIndef, 11), 1))) {
9788 return 51;
9789 }
9790
9791 QCBORDecode_Init(&DCtx, DefAndIndef, 0);
9792 QCBORDecode_EnterArray(&DCtx, NULL);
9793 QCBORDecode_EnterArray(&DCtx, NULL);
9794 QCBORDecode_GetArray(&DCtx, &Item, &ReturnedEncodedCBOR);
9795 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) {
9796 return 52;
9797 }
9798 if(UsefulBuf_Compare(ReturnedEncodedCBOR, UsefulBuf_Tail(UsefulBuf_Head(DefAndIndef, 10), 2))) {
9799 return 53;
9800 }
9801
9802 QCBORDecode_Init(&DCtx, DefAndIndef, 0);
9803 QCBORDecode_EnterArray(&DCtx, NULL);
9804 QCBORDecode_EnterArray(&DCtx, NULL);
9805 QCBORDecode_EnterArray(&DCtx, NULL);
9806 QCBORDecode_GetArray(&DCtx, &Item, &ReturnedEncodedCBOR);
9807 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) {
9808 return 54;
9809 }
9810 if(UsefulBuf_Compare(ReturnedEncodedCBOR, UsefulBuf_Tail(UsefulBuf_Head(DefAndIndef, 9), 3))) {
9811 return 55;
9812 }
9813 QCBORDecode_Init(&DCtx, DefAndIndef, 0);
9814 QCBORDecode_EnterArray(&DCtx, NULL);
9815 QCBORDecode_EnterArray(&DCtx, NULL);
9816 QCBORDecode_EnterArray(&DCtx, NULL);
9817 QCBORDecode_EnterArray(&DCtx, NULL);
9818 QCBORDecode_GetArray(&DCtx, &Item, &ReturnedEncodedCBOR);
9819 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) {
9820 return 56;
9821 }
9822 if(UsefulBuf_Compare(ReturnedEncodedCBOR, UsefulBuf_Tail(UsefulBuf_Head(DefAndIndef, 8), 4))) {
9823 return 57;
9824 }
9825
9826 QCBORDecode_Init(&DCtx, DefAndIndef, 0);
9827 QCBORDecode_EnterArray(&DCtx, NULL);
9828 QCBORDecode_EnterArray(&DCtx, NULL);
9829 QCBORDecode_EnterArray(&DCtx, NULL);
9830 QCBORDecode_EnterArray(&DCtx, NULL);
9831 QCBORDecode_EnterArray(&DCtx, NULL);
9832 QCBORDecode_VGetNextConsume(&DCtx, &Item);
9833 QCBORDecode_GetArray(&DCtx, &Item, &ReturnedEncodedCBOR);
9834 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) {
9835 return 58;
9836 }
9837 if(UsefulBuf_Compare(ReturnedEncodedCBOR, UsefulBuf_Tail(UsefulBuf_Head(DefAndIndef, 8), 6))) {
9838 return 59;
9839 }
9840
9841
9842 QCBORDecode_Init(&DCtx,
9843 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapIndefEncoded),
9844 0);
9845
9846 QCBORDecode_EnterMap(&DCtx, NULL);
9847 QCBORDecode_VGetNextConsume(&DCtx, &Item);
9848 QCBORDecode_GetArray(&DCtx, &Item, &ReturnedEncodedCBOR);
9849 if(QCBORDecode_GetError(&DCtx)) {
9850 return 60;
9851 }
9852 if(Item.val.uCount != UINT16_MAX) {
9853 return 61;
9854 }
9855 if(UsefulBuf_Compare(ReturnedEncodedCBOR, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedArray2sIndef))) {
9856 return 62;
9857 }
9858
9859 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
9860 UsefulBuf_Compare(Item.label.string, UsefulBuf_FROM_SZ_LITERAL("an array of two strings"))) {
9861 return 63;
9862 }
9863
9864 uPosition = QCBORDecode_Tell(&DCtx);
9865
9866
9867 QCBORDecode_GetMap(&DCtx, &Item, &ReturnedEncodedCBOR);
9868 if(QCBORDecode_GetError(&DCtx)) {
9869 return 70;
9870 }
9871 if(Item.val.uCount != UINT16_MAX) {
9872 return 71;
9873 }
9874 if(UsefulBuf_Compare(ReturnedEncodedCBOR, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedMap4Indef))) {
9875 return 72;
9876 }
9877
9878
9879 uPosition = QCBORDecode_Tell(&DCtx);
9880 QCBORDecode_GetArrayFromMapSZ(&DCtx,
9881 "an array of two strings",
9882 &Item,
9883 &ReturnedEncodedCBOR);
9884 if(QCBORDecode_GetError(&DCtx)) {
9885 return 80;
9886 }
9887 if(Item.val.uCount != UINT16_MAX) {
9888 return 81;
9889 }
9890 if(UsefulBuf_Compare(ReturnedEncodedCBOR, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedArray2sIndef))) {
9891 return 82;
9892 }
9893 if(uPosition != QCBORDecode_Tell(&DCtx)) {
9894 return 83;
9895 }
9896
9897 QCBORDecode_Rewind(&DCtx);
9898
9899 uPosition = QCBORDecode_Tell(&DCtx);
9900 QCBORDecode_GetMapFromMapSZ(&DCtx, "map in a map", &Item, &ReturnedEncodedCBOR);
9901 if(QCBORDecode_GetError(&DCtx)) {
9902 return 90;
9903 }
9904 if(Item.val.uCount != UINT16_MAX) {
9905 return 91;
9906 }
9907 if(UsefulBuf_Compare(ReturnedEncodedCBOR, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedMap4Indef))) {
9908 return 92;
9909 }
9910 if(uPosition != QCBORDecode_Tell(&DCtx)) {
9911 return 93;
9912 }
9913
9914 uPosition = QCBORDecode_Tell(&DCtx);
9915 QCBORDecode_GetArrayFromMapSZ(&DCtx, "map in a map", &Item, &ReturnedEncodedCBOR);
9916 if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
9917 return 100;
9918 }
9919 if(UINT32_MAX != QCBORDecode_Tell(&DCtx)) {
9920 return 101;
9921 }
9922 QCBORDecode_GetAndResetError(&DCtx);
9923 if(uPosition != QCBORDecode_Tell(&DCtx)) {
9924 return 102;
9925 }
Laurence Lundbladeec290b82024-06-10 11:10:54 -07009926#endif /* ! QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
Laurence Lundbladea29f45a2024-05-14 15:55:19 -07009927
9928 return 0;
9929}
Laurence Lundbladeec290b82024-06-10 11:10:54 -07009930#endif /* ! QCBOR_DISABLE_NON_INTEGER_LABELS */
Laurence Lundbladea29f45a2024-05-14 15:55:19 -07009931
9932
Laurence Lundbladef00b8be2024-03-08 10:34:33 -08009933int32_t
9934ErrorHandlingTests(void)
9935{
9936 QCBORDecodeContext DCtx;
9937 QCBORItem Item;
9938 QCBORError uError;
9939 int64_t integer;
9940
9941 /* Test QCBORDecode_SetError() */
9942 QCBORDecode_Init(&DCtx,
9943 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded),
9944 QCBOR_DECODE_MODE_NORMAL);
9945
9946 QCBORDecode_SetError(&DCtx, QCBOR_ERR_FIRST_USER_DEFINED);
9947
9948 QCBORDecode_VGetNext(&DCtx, &Item);
9949
9950 uError = QCBORDecode_GetError(&DCtx);
9951
9952 if(uError != QCBOR_ERR_FIRST_USER_DEFINED) {
9953 return -1;
9954 }
9955
9956 if(Item.uLabelType != QCBOR_TYPE_NONE ||
9957 Item.uDataType != QCBOR_TYPE_NONE) {
9958 return -2;
9959 }
9960
9961
9962 /* Test data type returned from previous error */
9963 QCBORDecode_Init(&DCtx,
9964 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded),
9965 QCBOR_DECODE_MODE_NORMAL);
9966 QCBORDecode_GetInt64(&DCtx, &integer);
9967 uError = QCBORDecode_GetError(&DCtx);
9968 if(uError != QCBOR_ERR_UNEXPECTED_TYPE) {
9969 return -3;
9970 }
9971
9972 QCBORDecode_VGetNext(&DCtx, &Item);
9973 if(Item.uLabelType != QCBOR_TYPE_NONE ||
9974 Item.uDataType != QCBOR_TYPE_NONE) {
9975 return -2;
9976 }
9977 uError = QCBORDecode_GetError(&DCtx);
9978 if(uError != QCBOR_ERR_UNEXPECTED_TYPE) {
9979 return -3;
9980 }
9981
9982
9983 /* Test error classification functions */
9984
9985 if(!QCBORDecode_IsUnrecoverableError(QCBOR_ERR_INDEFINITE_STRING_CHUNK)) {
9986 return -10;
9987 }
9988 if(QCBORDecode_IsUnrecoverableError(QCBOR_SUCCESS)) {
9989 return -11;
9990 }
9991 if(!QCBORDecode_IsUnrecoverableError(QCBOR_ERR_INDEFINITE_STRING_CHUNK)) {
9992 return -12;
9993 }
9994 if(QCBORDecode_IsUnrecoverableError(QCBOR_ERR_DUPLICATE_LABEL)) {
9995 return -13;
9996 }
9997
9998 if(!QCBORDecode_IsNotWellFormedError(QCBOR_ERR_BAD_TYPE_7)) {
9999 return -20;
10000 }
10001 if(!QCBORDecode_IsNotWellFormedError(QCBOR_ERR_BAD_BREAK)) {
10002 return -21;
10003 }
10004 if(QCBORDecode_IsNotWellFormedError(QCBOR_SUCCESS)) {
10005 return -22;
10006 }
10007 if(QCBORDecode_IsNotWellFormedError(QCBOR_ERR_ARRAY_DECODE_TOO_LONG)) {
10008 return -23;
10009 }
10010
Laurence Lundbladecd34e582024-06-06 10:55:14 -070010011 /* Test error strings */
10012 const char *szErrString;
10013
10014 szErrString = qcbor_err_to_str(QCBOR_ERR_ARRAY_DECODE_TOO_LONG);
10015 if(szErrString == NULL) {
10016 return -100;
10017 }
10018 if(strcmp(szErrString, "QCBOR_ERR_ARRAY_DECODE_TOO_LONG")) {
10019 return -101;
10020 }
10021
10022 szErrString = qcbor_err_to_str(QCBOR_SUCCESS);
10023 if(szErrString == NULL) {
10024 return -102;
10025 }
10026 if(strcmp(szErrString, "QCBOR_SUCCESS")) {
10027 return -103;
10028 }
10029
10030 szErrString = qcbor_err_to_str(100);
10031 if(szErrString == NULL) {
10032 return -104;
10033 }
10034 if(strcmp(szErrString, "Unidentified QCBOR error")) {
10035 return -105;
10036 }
10037
10038 szErrString = qcbor_err_to_str(200);
10039 if(szErrString == NULL) {
10040 return -106;
10041 }
10042 if(strcmp(szErrString, "USER_DEFINED_200")) {
10043 return -107;
10044 }
10045
Laurence Lundbladef00b8be2024-03-08 10:34:33 -080010046 return 0;
10047}
Laurence Lundblade11654912024-05-09 11:49:24 -070010048
10049
10050int32_t TellTests(void)
10051{
10052 QCBORDecodeContext DCtx;
10053 QCBORItem Item;
10054 uint32_t uPosition;
10055 int nIndex;
10056 int64_t nDecodedInt;
10057
Laurence Lundbladeec290b82024-06-10 11:10:54 -070010058 // Improvement: rewrite so this can run with only integer labels
Laurence Lundblade11654912024-05-09 11:49:24 -070010059 static const uint32_t aPos[] =
Laurence Lundblade535bb5e2024-06-16 12:06:32 -070010060 {0, 1, 17, 42, 50, 58, 72, 85, 98, 112, 151};
Laurence Lundblade11654912024-05-09 11:49:24 -070010061 QCBORDecode_Init(&DCtx,
10062 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded),
10063 0);
10064 for(nIndex = 0; ; nIndex++) {
10065 uPosition = QCBORDecode_Tell(&DCtx);
10066 if(uPosition != aPos[nIndex]) {
10067 return nIndex;
10068 }
10069
Laurence Lundblade535bb5e2024-06-16 12:06:32 -070010070 if(QCBORDecode_EndCheck(&DCtx)) {
Laurence Lundblade11654912024-05-09 11:49:24 -070010071 break;
10072 }
10073
10074 QCBORDecode_VGetNext(&DCtx, &Item);
10075 }
10076
10077#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
10078 static const uint32_t aPosIndef[] =
Laurence Lundblade535bb5e2024-06-16 12:06:32 -070010079 {0, 1, 17, 42, 50, 59, 73, 86, 99, 113, 154};
Laurence Lundblade11654912024-05-09 11:49:24 -070010080 QCBORDecode_Init(&DCtx,
10081 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapIndefEncoded),
10082 0);
10083
10084 for(nIndex = 0; ; nIndex++) {
10085 uPosition = QCBORDecode_Tell(&DCtx);
10086 if(uPosition != aPosIndef[nIndex]) {
10087 return nIndex + 100;
10088 }
10089
Laurence Lundblade535bb5e2024-06-16 12:06:32 -070010090 if(QCBORDecode_EndCheck(&DCtx)) {
Laurence Lundblade11654912024-05-09 11:49:24 -070010091 break;
10092 }
10093
10094 QCBORDecode_VGetNext(&DCtx, &Item);
10095 }
10096#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
10097
10098 /* Next, some tests with entered maps and arrays */
10099 QCBORDecode_Init(&DCtx,
10100 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded),
10101 0);
10102 QCBORDecode_EnterMap(&DCtx, &Item);
10103 if(QCBORDecode_Tell(&DCtx) != 1) {
10104 return 1001;
10105 }
10106 QCBORDecode_GetInt64InMapSZ(&DCtx, "first integer", &nDecodedInt);
10107 if(QCBORDecode_Tell(&DCtx) != 1) {
10108 return 1002;
10109 }
10110 QCBORDecode_EnterMapFromMapSZ(&DCtx, "map in a map");
10111 if(QCBORDecode_Tell(&DCtx) != 72) {
10112 return 1003;
10113 }
10114
10115 QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt);
10116 if(nDecodedInt != 98) {
10117 return 1004;
10118 }
10119 /* Getting non-aggregate types doesn't affect cursor position. */
10120 if(QCBORDecode_Tell(&DCtx) != 72) {
10121 return 1005;
10122 }
10123 QCBORDecode_VGetNext(&DCtx, &Item);
10124 if(QCBORDecode_Tell(&DCtx) != 85) {
10125 return 1006;
10126 }
10127 QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt);
10128 if(nDecodedInt != 98) {
10129 return 1007;
10130 }
10131 /* Getting non-aggregate types doesn't affect cursor position. */
10132 if(QCBORDecode_Tell(&DCtx) != 85) {
10133 return 1008;
10134 }
10135
10136 QCBORDecode_ExitMap(&DCtx);
Laurence Lundblade535bb5e2024-06-16 12:06:32 -070010137 if(QCBORDecode_Tell(&DCtx) != 151) {
Laurence Lundblade11654912024-05-09 11:49:24 -070010138 return 1009;
10139 }
10140 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_NO_MORE_ITEMS) {
10141 return 1010;
10142 }
10143
10144#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
10145 /* Next, some tests with entered maps and arrays */
10146 QCBORDecode_Init(&DCtx,
10147 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapIndefEncoded),
10148 0);
10149 QCBORDecode_EnterMap(&DCtx, &Item);
10150 if(QCBORDecode_Tell(&DCtx) != 1) {
10151 return 2000;
10152 }
10153 QCBORDecode_GetInt64InMapSZ(&DCtx, "first integer", &nDecodedInt);
10154 if(QCBORDecode_Tell(&DCtx) != 1) {
10155 return 2001;
10156 }
10157 QCBORDecode_EnterMapFromMapSZ(&DCtx, "map in a map");
10158 if(QCBORDecode_Tell(&DCtx) != 73) {
10159 return 2002;
10160 }
10161
10162 QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt);
10163 if(nDecodedInt != 98) {
10164 return 2003;
10165 }
10166 /* Getting non-aggregate types doesn't affect cursor position. */
10167 if(QCBORDecode_Tell(&DCtx) != 73) {
10168 return 2004;
10169 }
10170 QCBORDecode_VGetNext(&DCtx, &Item);
10171 if(QCBORDecode_Tell(&DCtx) != 86) {
10172 return 2005;
10173 }
10174 QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt);
10175 if(nDecodedInt != 98) {
10176 return 2006;
10177 }
10178 /* Getting non-aggregate types doesn't affect cursor position. */
10179 if(QCBORDecode_Tell(&DCtx) != 86) {
10180 return 2007;
10181 }
10182
10183 QCBORDecode_ExitMap(&DCtx);
Laurence Lundblade535bb5e2024-06-16 12:06:32 -070010184 if(QCBORDecode_Tell(&DCtx) != 154) {
Laurence Lundblade11654912024-05-09 11:49:24 -070010185 return 2008;
10186 }
10187 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_NO_MORE_ITEMS) {
10188 return 2010;
10189 }
10190#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
10191
10192
10193
10194 /* Error state test */
10195 QCBORDecode_Init(&DCtx,
10196 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded),
10197 0);
10198 /* Cause an error */
10199 QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt);
10200 if(QCBORDecode_Tell(&DCtx) != UINT32_MAX) {
10201 return 3000;
10202 }
Laurence Lundblade535bb5e2024-06-16 12:06:32 -070010203 if(QCBORDecode_EndCheck(&DCtx) != QCBOR_ERR_MAP_NOT_ENTERED) {
10204 return 3001;
10205 }
10206
Laurence Lundblade11654912024-05-09 11:49:24 -070010207
10208 /* Empties tests */
10209 const uint8_t pMinimalCBOR[] = {0xa0}; // One empty map
10210 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pMinimalCBOR),0);
10211 if(QCBORDecode_Tell(&DCtx) != 0) {
10212 return 4000;
10213 }
Laurence Lundblade535bb5e2024-06-16 12:06:32 -070010214 if(QCBORDecode_EndCheck(&DCtx) != QCBOR_SUCCESS) {
10215 return 4008;
10216 }
Laurence Lundblade11654912024-05-09 11:49:24 -070010217 QCBORDecode_EnterMap(&DCtx, &Item);
10218 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) {
10219 return 4001;
10220 }
Laurence Lundblade535bb5e2024-06-16 12:06:32 -070010221 if(QCBORDecode_Tell(&DCtx) != 1) {
Laurence Lundblade11654912024-05-09 11:49:24 -070010222 return 4002;
10223 }
10224 QCBORDecode_ExitMap(&DCtx);
10225 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) {
Laurence Lundblade535bb5e2024-06-16 12:06:32 -070010226 return 4003;
Laurence Lundblade11654912024-05-09 11:49:24 -070010227 }
Laurence Lundblade535bb5e2024-06-16 12:06:32 -070010228 if(QCBORDecode_Tell(&DCtx) != 1) {
10229 return 4004;
10230 }
10231 if(QCBORDecode_EndCheck(&DCtx) != QCBOR_ERR_NO_MORE_ITEMS) {
10232 return 4005;
Laurence Lundblade11654912024-05-09 11:49:24 -070010233 }
10234 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_NO_MORE_ITEMS) {
10235 return 4010;
10236 }
10237
10238#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
10239 const uint8_t pMinimalIndefCBOR[] = {0xbf, 0xff}; // One empty map
10240 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pMinimalIndefCBOR),0);
10241 if(QCBORDecode_Tell(&DCtx) != 0) {
10242 return 4100;
10243 }
10244 QCBORDecode_EnterMap(&DCtx, &Item);
10245 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) {
10246 return 4101;
10247 }
Laurence Lundblade535bb5e2024-06-16 12:06:32 -070010248 if(QCBORDecode_Tell(&DCtx) != 2) {
Laurence Lundblade11654912024-05-09 11:49:24 -070010249 return 4102;
10250 }
10251 QCBORDecode_ExitMap(&DCtx);
10252 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) {
Laurence Lundblade535bb5e2024-06-16 12:06:32 -070010253 return 4103;
Laurence Lundblade11654912024-05-09 11:49:24 -070010254 }
Laurence Lundblade535bb5e2024-06-16 12:06:32 -070010255 if(QCBORDecode_Tell(&DCtx) != 2) {
10256 return 4104;
10257 }
10258 if(QCBORDecode_EndCheck(&DCtx) != QCBOR_ERR_NO_MORE_ITEMS) {
10259 return 4005;
Laurence Lundblade11654912024-05-09 11:49:24 -070010260 }
10261 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_NO_MORE_ITEMS) {
10262 return 4110;
10263 }
10264#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
10265
10266 /* Test on a CBOR sequence */
10267 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSequenceTestInput),0);
10268 if(QCBORDecode_Tell(&DCtx) != 0) {
10269 return 5000;
10270 }
10271 QCBORDecode_VGetNext(&DCtx, &Item);
10272 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) {
10273 return 5001;
10274 }
10275 if(QCBORDecode_Tell(&DCtx) != 11) {
10276 return 5002;
10277 }
10278 QCBORDecode_VGetNext(&DCtx, &Item);
10279 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) {
10280 return 5003;
10281 }
10282 if(QCBORDecode_Tell(&DCtx) != 12) {
10283 return 5004;
10284 }
10285 QCBORDecode_VGetNext(&DCtx, &Item);
10286 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) {
10287 return 5005;
10288 }
10289 if(QCBORDecode_Tell(&DCtx) != 17) {
10290 return 5006;
10291 }
10292 QCBORDecode_VGetNext(&DCtx, &Item);
10293 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) {
10294 return 5007;
10295 }
Laurence Lundblade535bb5e2024-06-16 12:06:32 -070010296 if(QCBORDecode_Tell(&DCtx) != 20) {
Laurence Lundblade11654912024-05-09 11:49:24 -070010297 return 5008;
10298 }
10299 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_NO_MORE_ITEMS) {
10300 return 5010;
10301 }
10302
10303
10304 QCBORDecode_Init(&DCtx,
10305 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded),
10306 0);
10307 QCBORDecode_EnterMap(&DCtx, &Item);
10308 QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings");
10309 if(QCBORDecode_Tell(&DCtx) != 42) {
10310 return 6001;
10311 }
10312 QCBORDecode_VGetNext(&DCtx, &Item);
10313 if(QCBORDecode_Tell(&DCtx) != 50) {
10314 return 6002;
10315 }
10316 QCBORDecode_VGetNext(&DCtx, &Item);
10317 if(QCBORDecode_Tell(&DCtx) != 58) {
10318 return 6008;
10319 }
10320 QCBORDecode_VGetNext(&DCtx, &Item);
10321 (void)QCBORDecode_GetAndResetError(&DCtx);
10322 if(QCBORDecode_Tell(&DCtx) != 58) {
10323 return 6003;
10324 }
10325 QCBORDecode_ExitArray(&DCtx);
10326 if(QCBORDecode_Tell(&DCtx) != 58) {
10327 return 6004;
10328 }
10329
10330 static const uint32_t aEmptiesPos[] =
Laurence Lundblade535bb5e2024-06-16 12:06:32 -070010331 {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 13, 15};
Laurence Lundblade11654912024-05-09 11:49:24 -070010332 QCBORDecode_Init(&DCtx,
10333 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(sEmpties),
10334 0);
10335 for(nIndex = 0; ; nIndex++) {
10336 uPosition = QCBORDecode_Tell(&DCtx);
10337 if(uPosition != aEmptiesPos[nIndex]) {
10338 return nIndex + 200;
10339 }
10340
Laurence Lundblade535bb5e2024-06-16 12:06:32 -070010341 if(QCBORDecode_EndCheck(&DCtx)) {
Laurence Lundblade11654912024-05-09 11:49:24 -070010342 break;
10343 }
10344
10345 QCBORDecode_VGetNext(&DCtx, &Item);
10346 }
10347
10348#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
10349 static const uint32_t aIndefEmptiesPos[] =
Laurence Lundblade535bb5e2024-06-16 12:06:32 -070010350 {0, 1, 2, 4, 5, 7, 8, 10, 12, 13, 16, 19, 25};
Laurence Lundblade11654912024-05-09 11:49:24 -070010351 QCBORDecode_Init(&DCtx,
10352 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(sEmptiesIndef),
10353 0);
10354 for(nIndex = 0; ; nIndex++) {
10355 uPosition = QCBORDecode_Tell(&DCtx);
10356 if(uPosition != aIndefEmptiesPos[nIndex]) {
10357 return nIndex + 300;
10358 }
10359
Laurence Lundblade535bb5e2024-06-16 12:06:32 -070010360 if(QCBORDecode_EndCheck(&DCtx)) {
Laurence Lundblade11654912024-05-09 11:49:24 -070010361 break;
10362 }
10363
10364 QCBORDecode_VGetNext(&DCtx, &Item);
10365 }
10366#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
10367
10368
10369 return 0;
10370}