blob: 7434f0d51c86451521348558be6cb6437e62c2e1 [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 }
Laurence Lundbladecdbbc192024-06-28 15:13:04 -07002449#ifndef QCBOR_DISABLE_NON_INTEGER_LABELS
Laurence Lundblade3888f002024-06-12 21:20:56 -07002450 QCBORDecode_GetSimple(&DCtx, &uSimple);
2451 if(QCBORDecode_GetError(&DCtx) || uSimple != 255) {
2452 return 27;
2453 }
2454 QCBORDecode_VGetNext(&DCtx, &Item);
2455 QCBORDecode_GetSimple(&DCtx, &uSimple);
2456 if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_NO_MORE_ITEMS) {
2457 return 28;
2458 }
2459
2460 QCBORDecode_Rewind(&DCtx);
2461
2462 QCBORDecode_GetSimpleInMapN(&DCtx, 6, &uSimple);
2463 if(QCBORDecode_GetError(&DCtx) || uSimple != 19) {
2464 return 30;
2465 }
2466
2467 QCBORDecode_GetSimpleInMapSZ(&DCtx, "@", &uSimple);
2468 if(QCBORDecode_GetError(&DCtx) || uSimple != 255) {
2469 return 31;
2470 }
2471
2472 QCBORDecode_GetSimpleInMapN(&DCtx, 99, &uSimple);
2473 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_LABEL_NOT_FOUND) {
2474 return 32;
2475 }
2476
2477 QCBORDecode_GetSimpleInMapSZ(&DCtx, "xx", &uSimple);
2478 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_LABEL_NOT_FOUND) {
2479 return 33;
2480 }
2481
2482 QCBORDecode_GetSimpleInMapN(&DCtx, 15, &uSimple);
2483 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
2484 return 34;
2485 }
Laurence Lundbladecdbbc192024-06-28 15:13:04 -07002486#endif /* ! QCBOR_DISABLE_NON_INTEGER_LABELS */
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002487
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002488 return 0;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002489}
2490
2491
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03002492int32_t NotWellFormedTests(void)
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002493{
2494 // Loop over all the not-well-formed instance of CBOR
2495 // that are test vectors in not_well_formed_cbor.h
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08002496 const uint16_t nArraySize = C_ARRAY_COUNT(paNotWellFormedCBOR,
2497 struct someBinaryBytes);
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002498 for(uint16_t nIterate = 0; nIterate < nArraySize; nIterate++) {
2499 const struct someBinaryBytes *pBytes = &paNotWellFormedCBOR[nIterate];
2500 const UsefulBufC Input = (UsefulBufC){pBytes->p, pBytes->n};
2501
Laurence Lundbladeee851742020-01-08 08:37:05 -08002502 // Set up decoder context. String allocator needed for indefinite
2503 // string test cases
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002504 QCBORDecodeContext DCtx;
2505 QCBORDecode_Init(&DCtx, Input, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08002506#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002507 UsefulBuf_MAKE_STACK_UB(Pool, 100);
2508 QCBORDecode_SetMemPool(&DCtx, Pool, 0);
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08002509#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002510
2511 // Loop getting items until no more to get
Laurence Lundbladef71e1622020-08-06 18:52:13 -07002512 QCBORError uCBORError;
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002513 do {
2514 QCBORItem Item;
2515
Laurence Lundbladef71e1622020-08-06 18:52:13 -07002516 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
2517 } while(uCBORError == QCBOR_SUCCESS);
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002518
2519 // Every test vector must fail with
2520 // a not-well-formed error. If not
2521 // this test fails.
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002522 if(!QCBORDecode_IsNotWellFormedError(uCBORError) &&
Laurence Lundbladef71e1622020-08-06 18:52:13 -07002523 uCBORError != QCBOR_ERR_NO_MORE_ITEMS) {
Laurence Lundblade4bf6e712020-12-10 11:53:21 -08002524 /* Return index of failure and QCBOR error in the result */
2525 return (int32_t)(nIterate * 100 + uCBORError);
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002526 }
2527 }
2528 return 0;
2529}
2530
2531
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002532struct DecodeFailTestInput {
2533 const char *szDescription; /* Description of the test */
2534 QCBORDecodeMode DecoderMode; /* The QCBOR Decoder Mode for test */
2535 UsefulBufC Input; /* Chunk of CBOR that cases error */
2536 QCBORError nError; /* The expected error */
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002537};
2538
Laurence Lundblade59289e52019-12-30 13:44:37 -08002539
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002540static int32_t
2541ProcessDecodeFailures(const struct DecodeFailTestInput *pFailInputs, const int nNumFails)
Laurence Lundblade59289e52019-12-30 13:44:37 -08002542{
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002543 int nIndex;
2544 QCBORDecodeContext DCtx;
2545 QCBORError uCBORError;
2546 QCBORItem Item;
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08002547
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002548 for(nIndex = 0; nIndex < nNumFails; nIndex++) {
2549 const struct DecodeFailTestInput *pF = &pFailInputs[nIndex];
2550
2551 QCBORDecode_Init(&DCtx, pF->Input, pF->DecoderMode);
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08002552
2553#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002554 /* Set up the decoding context including a memory pool so that
2555 * indefinite length items can be checked.
2556 */
Laurence Lundblade59289e52019-12-30 13:44:37 -08002557 UsefulBuf_MAKE_STACK_UB(Pool, 100);
Laurence Lundblade830fbf92020-05-31 17:22:33 -07002558
Laurence Lundblade4bf6e712020-12-10 11:53:21 -08002559 uCBORError = QCBORDecode_SetMemPool(&DCtx, Pool, 0);
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002560 if(uCBORError != QCBOR_SUCCESS) {
2561 return -1;
Laurence Lundblade59289e52019-12-30 13:44:37 -08002562 }
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08002563#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
2564
Laurence Lundbladecdbbc192024-06-28 15:13:04 -07002565 if(nIndex == 4) {
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002566 uCBORError = 9; /* For setting break points */
Laurence Lundblade37286c02022-09-03 10:05:02 -07002567 }
2568
Laurence Lundbladecdbbc192024-06-28 15:13:04 -07002569 /* Iterate until there is an error of some sort */
Laurence Lundblade59289e52019-12-30 13:44:37 -08002570 do {
Laurence Lundbladecdbbc192024-06-28 15:13:04 -07002571 /* Set to something non-zero, something other than QCBOR_TYPE_NONE */
Laurence Lundblade59289e52019-12-30 13:44:37 -08002572 memset(&Item, 0x33, sizeof(Item));
2573
Laurence Lundblade4bf6e712020-12-10 11:53:21 -08002574 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
2575 } while(uCBORError == QCBOR_SUCCESS);
2576
Laurence Lundbladecdbbc192024-06-28 15:13:04 -07002577 /* Must get the expected error or the this test fails.
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002578 * The data and label type must also be QCBOR_TYPE_NONE.
2579 */
Laurence Lundblade4bf6e712020-12-10 11:53:21 -08002580 if(uCBORError != pF->nError ||
Laurence Lundblade59289e52019-12-30 13:44:37 -08002581 Item.uDataType != QCBOR_TYPE_NONE ||
2582 Item.uLabelType != QCBOR_TYPE_NONE) {
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002583 return (int32_t)(nIndex * 1000 + (int)uCBORError);
Laurence Lundblade59289e52019-12-30 13:44:37 -08002584 }
2585 }
2586
2587 return 0;
2588}
2589
2590
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002591static const struct DecodeFailTestInput Failures[] = {
2592 /* Most of this is copied from not_well_formed.h. Here the error
2593 * code returned is also checked.
2594 */
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002595
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08002596#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002597 /* Indefinite length strings must be closed off */
2598 { "An indefinite length byte string not closed off",
2599 QCBOR_DECODE_MODE_NORMAL,
2600 {"0x5f\x41\x00", 3},
2601 QCBOR_ERR_HIT_END
2602 },
2603 { "An indefinite length text string not closed off",
2604 QCBOR_DECODE_MODE_NORMAL,
2605 {"\x7f\x61\x00", 3},
2606 QCBOR_ERR_HIT_END
2607 },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002608
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002609 /* All the chunks in an indefinite length string must be of the
2610 * type of indefinite length string
2611 */
2612 { "Indefinite length byte string with text string chunk",
2613 QCBOR_DECODE_MODE_NORMAL,
2614 {"\x5f\x61\x00\xff", 4},
2615 QCBOR_ERR_INDEFINITE_STRING_CHUNK
2616 },
2617 { "Indefinite length text string with a byte string chunk",
2618 QCBOR_DECODE_MODE_NORMAL,
2619 {"\x7f\x41\x00\xff", 4},
2620 QCBOR_ERR_INDEFINITE_STRING_CHUNK
2621 },
2622 { "Indefinite length byte string with a positive integer chunk",
2623 QCBOR_DECODE_MODE_NORMAL,
2624 {"\x5f\x00\xff", 3},
2625 QCBOR_ERR_INDEFINITE_STRING_CHUNK
2626 },
2627 { "Indefinite length byte string with an negative integer chunk",
2628 QCBOR_DECODE_MODE_NORMAL,
2629 {"\x5f\x21\xff", 3},
2630 QCBOR_ERR_INDEFINITE_STRING_CHUNK
2631 },
2632 { "Indefinite length byte string with an array chunk",
2633 QCBOR_DECODE_MODE_NORMAL,
2634 {"\x5f\x80\xff", 3},
2635 QCBOR_ERR_INDEFINITE_STRING_CHUNK
2636 },
2637 { "Indefinite length byte string with an map chunk",
2638 QCBOR_DECODE_MODE_NORMAL,
2639 {"\x5f\xa0\xff", 3},
2640 QCBOR_ERR_INDEFINITE_STRING_CHUNK
2641 },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002642
Laurence Lundblade37286c02022-09-03 10:05:02 -07002643#ifndef QCBOR_DISABLE_TAGS
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002644 { "Indefinite length byte string with tagged integer chunk",
2645 QCBOR_DECODE_MODE_NORMAL,
2646 {"\x5f\xc0\x00\xff", 4},
2647 QCBOR_ERR_INDEFINITE_STRING_CHUNK
2648 },
Laurence Lundblade37286c02022-09-03 10:05:02 -07002649#else
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002650 { "Indefinite length byte string with tagged integer chunk",
2651 QCBOR_DECODE_MODE_NORMAL,
2652 {"\x5f\xc0\x00\xff", 4},
2653 QCBOR_ERR_TAGS_DISABLED
2654 },
Laurence Lundblade37286c02022-09-03 10:05:02 -07002655#endif /* QCBOR_DISABLE_TAGS */
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002656
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002657 { "Indefinite length byte string with an simple type chunk",
2658 QCBOR_DECODE_MODE_NORMAL,
2659 {"\x5f\xe0\xff", 3},
2660 QCBOR_ERR_INDEFINITE_STRING_CHUNK
2661 },
2662 { "???",
2663 QCBOR_DECODE_MODE_NORMAL,
2664 {"\x5f\x5f\x41\x00\xff\xff", 6},
2665 QCBOR_ERR_INDEFINITE_STRING_CHUNK
2666 },
2667 { "indefinite length text string with indefinite string inside",
2668 QCBOR_DECODE_MODE_NORMAL,
2669 {"\x7f\x7f\x61\x00\xff\xff", 6},
2670 QCBOR_ERR_INDEFINITE_STRING_CHUNK
2671 },
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08002672#else /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
2673
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08002674#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
2675
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002676 /* Definte length maps and arrays must be closed by having the right number of items */
2677 { "A definte length array that is supposed to have 1 item, but has none",
2678 QCBOR_DECODE_MODE_NORMAL,
2679 {"\x81", 1},
2680 QCBOR_ERR_NO_MORE_ITEMS
2681 },
2682 { "A definte length array that is supposed to have 2 items, but has only 1",
2683 QCBOR_DECODE_MODE_NORMAL,
2684 {"\x82\x00", 2},
2685 QCBOR_ERR_NO_MORE_ITEMS
2686 },
2687 { "A definte length array that is supposed to have 511 items, but has only 1",
2688 QCBOR_DECODE_MODE_NORMAL,
2689 {"\x9a\x01\xff\x00", 4},
2690 QCBOR_ERR_HIT_END
2691 },
2692 { "A definte length map that is supposed to have 1 item, but has none",
2693 QCBOR_DECODE_MODE_NORMAL,
2694 {"\xa1", 1},
2695 QCBOR_ERR_NO_MORE_ITEMS
2696 },
2697 { "A definte length map that is supposed to have s item, but has only 1",
2698 QCBOR_DECODE_MODE_NORMAL,
2699 {"\xa2\x01\x02", 3},
2700 QCBOR_ERR_NO_MORE_ITEMS
2701 },
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08002702#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002703 /* Indefinte length maps and arrays must be ended by a break */
2704 { "Indefinite length array with zero items and no break",
2705 QCBOR_DECODE_MODE_NORMAL,
2706 {"\x9f", 1},
2707 QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002708
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002709 { "Indefinite length array with two items and no break",
2710 QCBOR_DECODE_MODE_NORMAL,
2711 {"\x9\x01\x02", 3},
2712 QCBOR_ERR_NO_MORE_ITEMS
2713 },
2714 { "Indefinite length map with zero items and no break",
2715 QCBOR_DECODE_MODE_NORMAL,
2716 {"\xbf", 1},
2717 QCBOR_ERR_NO_MORE_ITEMS
2718 },
2719 { "Indefinite length map with two items and no break",
2720 QCBOR_DECODE_MODE_NORMAL,
2721 {"\xbf\x01\x02\x01\x02", 5},
2722 QCBOR_ERR_NO_MORE_ITEMS
2723 },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002724
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002725 /* Nested maps and arrays must be closed off (some extra nested test vectors) */
2726 { "Unclosed indefinite array containing a closed definite length array",
2727 QCBOR_DECODE_MODE_NORMAL,
2728 {"\x9f\x80\x00", 3},
2729 QCBOR_ERR_NO_MORE_ITEMS
2730 },
2731
2732 { "Definite length array containing an unclosed indefinite length array",
2733 QCBOR_DECODE_MODE_NORMAL,
2734 {"\x81\x9f", 2},
2735 QCBOR_ERR_NO_MORE_ITEMS
2736 },
2737 { "Unclosed indefinite map containing a closed definite length array",
2738 QCBOR_DECODE_MODE_NORMAL,
2739 {"\xbf\x01\x80\x00\xa0", 5},
2740 QCBOR_ERR_NO_MORE_ITEMS
2741 },
2742 { "Definite length map containing an unclosed indefinite length array",
2743 QCBOR_DECODE_MODE_NORMAL,
2744 {"\xa1\x02\x9f", 3},
2745 QCBOR_ERR_NO_MORE_ITEMS
2746 },
2747 { "Deeply nested definite length arrays with deepest one unclosed",
2748 QCBOR_DECODE_MODE_NORMAL,
2749 {"\x81\x81\x81\x81\x81\x81\x81\x81\x81", 9},
2750 QCBOR_ERR_NO_MORE_ITEMS
2751 },
2752 { "Deeply nested indefinite length arrays with deepest one unclosed",
2753 QCBOR_DECODE_MODE_NORMAL,
2754 {"\x9f\x9f\x9f\x9f\x9f\xff\xff\xff\xff", 9},
2755 QCBOR_ERR_NO_MORE_ITEMS
2756 },
2757 { "Mixed nesting with indefinite unclosed",
2758 QCBOR_DECODE_MODE_NORMAL,
2759 {"\x9f\x81\x9f\x81\x9f\x9f\xff\xff\xff", 9},
2760 QCBOR_ERR_NO_MORE_ITEMS },
2761 { "Mixed nesting with definite unclosed",
2762 QCBOR_DECODE_MODE_NORMAL,
2763 {"\x9f\x82\x9f\x81\x9f\x9f\xff\xff\xff\xff", 10},
2764 QCBOR_ERR_BAD_BREAK
2765 },
2766 { "Unclosed indefinite length map in definite length maps",
2767 QCBOR_DECODE_MODE_NORMAL,
2768 {"\xa1\x01\xa2\x02\xbf\xff\x02\xbf", 8},
2769 QCBOR_ERR_NO_MORE_ITEMS
2770 },
2771 { "Unclosed definite length map in indefinite length maps",
2772 QCBOR_DECODE_MODE_NORMAL,
2773 {"\xbf\x01\xbf\x02\xa1", 5},
2774 QCBOR_ERR_NO_MORE_ITEMS
2775 },
2776 { "Unclosed indefinite length array in definite length maps",
2777 QCBOR_DECODE_MODE_NORMAL,
2778 {"\xa1\x01\xa2\x02\x9f\xff\x02\x9f", 8},
2779 QCBOR_ERR_NO_MORE_ITEMS
2780 },
2781 { "Unclosed definite length array in indefinite length maps",
2782 QCBOR_DECODE_MODE_NORMAL,
2783 {"\xbf\x01\xbf\x02\x81", 5},
2784 QCBOR_ERR_NO_MORE_ITEMS
2785 },
2786 { "Unclosed indefinite length map in definite length arrays",
2787 QCBOR_DECODE_MODE_NORMAL,
2788 {"\x81\x82\xbf\xff\xbf", 5},
2789 QCBOR_ERR_NO_MORE_ITEMS
2790 },
2791 { "Unclosed definite length map in indefinite length arrays",
2792 QCBOR_DECODE_MODE_NORMAL,
2793 {"\x9f\x9f\xa1", 3},
2794 QCBOR_ERR_NO_MORE_ITEMS
2795 },
2796
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08002797#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002798
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002799 /* The "argument" for the data item is incomplete */
2800 { "Positive integer missing 1 byte argument",
2801 QCBOR_DECODE_MODE_NORMAL,
2802 {"\x18", 1},
2803 QCBOR_ERR_HIT_END
2804 },
2805 { "Positive integer missing 2 byte argument",
2806 QCBOR_DECODE_MODE_NORMAL,
2807 {"\x19", 1},
2808 QCBOR_ERR_HIT_END
2809 },
2810 { "Positive integer missing 4 byte argument",
2811 QCBOR_DECODE_MODE_NORMAL,
2812 {"\x1a", 1},
2813 QCBOR_ERR_HIT_END
2814 },
2815 { "Positive integer missing 8 byte argument",
2816 QCBOR_DECODE_MODE_NORMAL,
2817 {"\x1b", 1},
2818 QCBOR_ERR_HIT_END
2819 },
2820 { "Positive integer missing 1 byte of 2 byte argument",
2821 QCBOR_DECODE_MODE_NORMAL,
2822 {"\x19\x01", 2},
2823 QCBOR_ERR_HIT_END
2824 },
2825 { "Positive integer missing 2 bytes of 4 byte argument",
2826 QCBOR_DECODE_MODE_NORMAL,
2827 {"\x1a\x01\x02", 3},
2828 QCBOR_ERR_HIT_END
2829 },
2830 { "Positive integer missing 1 bytes of 7 byte argument",
2831 QCBOR_DECODE_MODE_NORMAL,
2832 {"\x1b\x01\x02\x03\x04\x05\x06\x07", 8},
2833 QCBOR_ERR_HIT_END
2834 },
2835 { "Negative integer missing 1 byte argument",
2836 QCBOR_DECODE_MODE_NORMAL,
2837 {"\x38", 1},
2838 QCBOR_ERR_HIT_END
2839 },
2840 { "Binary string missing 1 byte argument",
2841 QCBOR_DECODE_MODE_NORMAL,
2842 {"\x58", 1},
2843 QCBOR_ERR_HIT_END
2844 },
2845 { "Text string missing 1 byte argument",
2846 QCBOR_DECODE_MODE_NORMAL,
2847 {"\x78", 1},
2848 QCBOR_ERR_HIT_END
2849 },
2850 { "Array missing 1 byte argument",
2851 QCBOR_DECODE_MODE_NORMAL,
2852 {"\x98", 1},
2853 QCBOR_ERR_HIT_END
2854 },
2855 { "Map missing 1 byte argument",
2856 QCBOR_DECODE_MODE_NORMAL,
2857 {"\xb8", 1},
2858 QCBOR_ERR_HIT_END
2859 },
2860 { "Tag missing 1 byte argument",
2861 QCBOR_DECODE_MODE_NORMAL,
2862 {"\xd8", 1},
2863 QCBOR_ERR_HIT_END
2864 },
2865 { "Simple missing 1 byte argument",
2866 QCBOR_DECODE_MODE_NORMAL,
2867 {"\xf8", 1},
2868 QCBOR_ERR_HIT_END
2869 },
2870 { "half-precision with 1 byte argument",
2871 QCBOR_DECODE_MODE_NORMAL,
2872 {"\xf9\x00", 2},
2873 QCBOR_ERR_HIT_END
2874 },
2875 { "single-precision with 2 byte argument",
2876 QCBOR_DECODE_MODE_NORMAL,
2877 {"\0xfa\x00\x00", 3},
2878 QCBOR_ERR_HIT_END
2879 },
2880 { "double-precision with 3 byte argument",
2881 QCBOR_DECODE_MODE_NORMAL,
2882 {"\xfb\x00\x00\x00", 4},
2883 QCBOR_ERR_HIT_END
2884 },
Laurence Lundblade2f467f92020-10-09 17:50:11 -07002885
Laurence Lundblade37286c02022-09-03 10:05:02 -07002886#ifndef QCBOR_DISABLE_TAGS
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002887 { "Tag with no content",
2888 QCBOR_DECODE_MODE_NORMAL,
2889 {"\xc0", 1},
2890 QCBOR_ERR_HIT_END
2891 },
Laurence Lundblade37286c02022-09-03 10:05:02 -07002892#else /* QCBOR_DISABLE_TAGS */
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002893 { "Tag with no content",
2894 QCBOR_DECODE_MODE_NORMAL,
2895 {"\xc0", 1},
2896 QCBOR_ERR_TAGS_DISABLED
2897 },
Laurence Lundblade37286c02022-09-03 10:05:02 -07002898#endif /* QCBOR_DISABLE_TAGS */
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002899
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002900 /* Breaks must not occur in definite length arrays and maps */
2901 { "Array of length 1 with sole member replaced by a break",
2902 QCBOR_DECODE_MODE_NORMAL,
2903 {"\x81\xff", 2},
2904 QCBOR_ERR_BAD_BREAK
2905 },
2906 { "Array of length 2 with 2nd member replaced by a break",
2907 QCBOR_DECODE_MODE_NORMAL,
2908 {"\x82\x00\xff", 3},
2909 QCBOR_ERR_BAD_BREAK
2910 },
2911 { "Map of length 1 with sole member label replaced by a break",
2912 QCBOR_DECODE_MODE_NORMAL,
2913 {"\xa1\xff", 2},
2914 QCBOR_ERR_BAD_BREAK
2915 },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002916
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002917 /* Map of length 1 with sole member label replaced by break */
2918 { "Alternate representation that some decoders handle differently",
2919 QCBOR_DECODE_MODE_NORMAL,
2920 {"\xa1\xff\x00", 3},
2921 QCBOR_ERR_BAD_BREAK
2922 },
2923 { "Array of length 1 with 2nd member value replaced by a break",
2924 QCBOR_DECODE_MODE_NORMAL,
2925 {"\xa1\x00\xff", 3},
2926 QCBOR_ERR_BAD_BREAK
2927 },
Laurence Lundbladeec290b82024-06-10 11:10:54 -07002928 { "Map of length 2 with 2nd entry label replaced by a break",
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002929 QCBOR_DECODE_MODE_NORMAL,
Laurence Lundbladeec290b82024-06-10 11:10:54 -07002930 {"\xa2\x00\x00\xff\x00", 5},
2931 QCBOR_ERR_BAD_BREAK
2932 },
2933 { "Map of length 2 with 2nd entry value replaced by a break",
2934 QCBOR_DECODE_MODE_NORMAL,
2935 {"\xa2\x00\x00\x01\xff", 5},
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002936 QCBOR_ERR_BAD_BREAK
2937 },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002938
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002939 /* Breaks must not occur on their own out of an indefinite length data item */
2940 { "A bare break is not well formed",
2941 QCBOR_DECODE_MODE_NORMAL,
2942 {"\xff", 1},
2943 QCBOR_ERR_BAD_BREAK
2944 },
2945 { "A bare break after a zero length definite length array",
2946 QCBOR_DECODE_MODE_NORMAL,
2947 {"\x80\xff", 2},
2948 QCBOR_ERR_BAD_BREAK
2949 },
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08002950#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002951 { "A bare break after a zero length indefinite length map",
2952 QCBOR_DECODE_MODE_NORMAL,
2953 {"\x9f\xff\xff", 3},
2954 QCBOR_ERR_BAD_BREAK
2955 },
2956 { "A break inside a definite length array inside an indefenite length array",
2957 QCBOR_DECODE_MODE_NORMAL,
2958 {"\x9f\x81\xff", 3},
2959 QCBOR_ERR_BAD_BREAK
2960 },
2961 { "Complicated mixed nesting with break outside indefinite length array",
2962 QCBOR_DECODE_MODE_NORMAL,
2963 {"\x9f\x82\x9f\x81\x9f\x9f\xff\xff\xff\xff", 10},
2964 QCBOR_ERR_BAD_BREAK },
2965#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
2966
2967 /* Forbidden two byte encodings of simple types */
2968 { "Must use 0xe0 instead",
2969 QCBOR_DECODE_MODE_NORMAL,
2970 {"\xf8\x00", 2},
2971 QCBOR_ERR_BAD_TYPE_7
2972 },
2973 { "Should use 0xe1 instead",
2974 QCBOR_DECODE_MODE_NORMAL,
2975 {"\xf8\x01", 2},
2976 QCBOR_ERR_BAD_TYPE_7
2977 },
2978 { "Should use 0xe2 instead",
2979 QCBOR_DECODE_MODE_NORMAL,
2980 {"\xf8\x02", 2},
2981 QCBOR_ERR_BAD_TYPE_7
2982 }, { "Should use 0xe3 instead",
2983 QCBOR_DECODE_MODE_NORMAL,
2984 {"\xf8\x03", 2},
2985 QCBOR_ERR_BAD_TYPE_7
2986 },
2987 { "Should use 0xe4 instead",
2988 QCBOR_DECODE_MODE_NORMAL,
2989 {"\xf8\x04", 2},
2990 QCBOR_ERR_BAD_TYPE_7
2991 },
2992 { "Should use 0xe5 instead",
2993 QCBOR_DECODE_MODE_NORMAL,
2994 {"\xf8\x05", 2},
2995 QCBOR_ERR_BAD_TYPE_7
2996 },
2997 { "Should use 0xe6 instead",
2998 QCBOR_DECODE_MODE_NORMAL,
2999 {"\xf8\x06", 2},
3000 QCBOR_ERR_BAD_TYPE_7
3001 },
3002 { "Should use 0xe7 instead",
3003 QCBOR_DECODE_MODE_NORMAL,
3004 {"\xf8\x07", 2},
3005 QCBOR_ERR_BAD_TYPE_7
3006 },
3007 { "Should use 0xe8 instead",
3008 QCBOR_DECODE_MODE_NORMAL,
3009 {"\xf8\x08", 2},
3010 QCBOR_ERR_BAD_TYPE_7
3011 },
3012 { "Should use 0xe9 instead",
3013 QCBOR_DECODE_MODE_NORMAL,
3014 {"\xf8\x09", 2},
3015 QCBOR_ERR_BAD_TYPE_7
3016 },
3017 { "Should use 0xea instead",
3018 QCBOR_DECODE_MODE_NORMAL,
3019 {"\xf8\x0a", 2},
3020 QCBOR_ERR_BAD_TYPE_7
3021 },
3022 { "Should use 0xeb instead",
3023 QCBOR_DECODE_MODE_NORMAL,
3024 {"\xf8\x0b", 2},
3025 QCBOR_ERR_BAD_TYPE_7
3026 },
3027 { "Should use 0xec instead",
3028 QCBOR_DECODE_MODE_NORMAL,
3029 {"\xf8\x0c", 2},
3030 QCBOR_ERR_BAD_TYPE_7
3031 },
3032 { "Should use 0xed instead",
3033 QCBOR_DECODE_MODE_NORMAL,
3034 {"\xf8\x0d", 2},
3035 QCBOR_ERR_BAD_TYPE_7
3036 },
3037 { "Should use 0xee instead",
3038 QCBOR_DECODE_MODE_NORMAL,
3039 {"\xf8\x0e", 2},
3040 QCBOR_ERR_BAD_TYPE_7
3041 },
3042 { "Should use 0xef instead",
3043 QCBOR_DECODE_MODE_NORMAL,
3044 {"\xf8\x0f", 2},
3045 QCBOR_ERR_BAD_TYPE_7
3046 },
3047 { "Should use 0xf0 instead",
3048 QCBOR_DECODE_MODE_NORMAL,
3049 {"\xf8\x10", 2},
3050 QCBOR_ERR_BAD_TYPE_7
3051 },
3052 { "Should use 0xf1 instead",
3053 QCBOR_DECODE_MODE_NORMAL,
3054 {"\xf8\x11", 2},
3055 QCBOR_ERR_BAD_TYPE_7
3056 },
3057 { "Should use 0xf2 instead",
3058 QCBOR_DECODE_MODE_NORMAL,
3059 {"\xf8\x12", 2},
3060 QCBOR_ERR_BAD_TYPE_7
3061 },
3062 { "Should use 0xf3 instead",
3063 QCBOR_DECODE_MODE_NORMAL,
3064 {"\xf8\x13", 2},
3065 QCBOR_ERR_BAD_TYPE_7
3066 },
3067 { "Should use 0xf4 instead",
3068 QCBOR_DECODE_MODE_NORMAL,
3069 {"\xf8\x14", 2},
3070 QCBOR_ERR_BAD_TYPE_7
3071 },
3072 { "Should use 0xf5 instead",
3073 QCBOR_DECODE_MODE_NORMAL,
3074 {"\xf8\x15", 2},
3075 QCBOR_ERR_BAD_TYPE_7
3076 },
3077 { "Should use 0xf6 instead",
3078 QCBOR_DECODE_MODE_NORMAL,
3079 {"\xf8\x16", 2},
3080 QCBOR_ERR_BAD_TYPE_7
3081 },
3082 { "Should use 0xef7 instead",
3083 QCBOR_DECODE_MODE_NORMAL,
3084 {"\xf8\x17", 2},
3085 QCBOR_ERR_BAD_TYPE_7
3086 },
3087 { "Should use 0xef8 instead",
3088 QCBOR_DECODE_MODE_NORMAL,
3089 {"\xf8\x18", 2},
3090 QCBOR_ERR_BAD_TYPE_7
3091 },
3092 { "Reserved",
3093 QCBOR_DECODE_MODE_NORMAL,
3094 {"\xf8\x18", 2},
3095 QCBOR_ERR_BAD_TYPE_7
3096 },
3097
3098 /* Maps must have an even number of data items (key & value) */
3099 { "Map with 1 item when it should have 2",
3100 QCBOR_DECODE_MODE_NORMAL,
3101 {"\xa1\x00", 2},
3102 QCBOR_ERR_HIT_END
3103 },
3104 { "Map with 3 item when it should have 4",
3105 QCBOR_DECODE_MODE_NORMAL,
3106 {"\xa2\x00\x00\x00", 2},
3107 QCBOR_ERR_HIT_END
3108 },
3109#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
3110 { "Map with 1 item when it should have 2",
3111 QCBOR_DECODE_MODE_NORMAL,
3112 {"\xbf\x00\xff", 3},
3113 QCBOR_ERR_BAD_BREAK
3114 },
3115 { "Map with 3 item when it should have 4",
3116 QCBOR_DECODE_MODE_NORMAL,
3117 {"\xbf\x00\x00\x00\xff", 5},
3118 QCBOR_ERR_BAD_BREAK
3119 },
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08003120#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07003121
3122
Laurence Lundblade37286c02022-09-03 10:05:02 -07003123#ifndef QCBOR_DISABLE_TAGS
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08003124 /* In addition to not-well-formed, some invalid CBOR */
3125 { "Text-based date, with an integer",
3126 QCBOR_DECODE_MODE_NORMAL,
3127 {"\xc0\x00", 2},
3128 QCBOR_ERR_BAD_OPT_TAG
3129 },
3130 { "Epoch date, with an byte string",
3131 QCBOR_DECODE_MODE_NORMAL,
3132 {"\xc1\x41\x33", 3},
3133 QCBOR_ERR_BAD_OPT_TAG
3134 },
3135 { "tagged as both epoch and string dates",
3136 QCBOR_DECODE_MODE_NORMAL,
3137 {"\xc1\xc0\x00", 3},
3138 QCBOR_ERR_BAD_OPT_TAG
3139 },
3140 { "big num tagged an int, not a byte string",
3141 QCBOR_DECODE_MODE_NORMAL,
3142 {"\xc2\x00", 2},
3143 QCBOR_ERR_BAD_OPT_TAG
3144 },
Laurence Lundblade37286c02022-09-03 10:05:02 -07003145#else /* QCBOR_DISABLE_TAGS */
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08003146 /* In addition to not-well-formed, some invalid CBOR */
3147 { "Text-based date, with an integer",
3148 QCBOR_DECODE_MODE_NORMAL,
3149 {"\xc0\x00", 2},
3150 QCBOR_ERR_TAGS_DISABLED
3151 },
3152 { "Epoch date, with an byte string",
3153 QCBOR_DECODE_MODE_NORMAL,
3154 {"\xc1\x41\x33", 3},
3155 QCBOR_ERR_TAGS_DISABLED
3156 },
3157 { "tagged as both epoch and string dates",
3158 QCBOR_DECODE_MODE_NORMAL,
3159 {"\xc1\xc0\x00", 3},
3160 QCBOR_ERR_TAGS_DISABLED
3161 },
3162 { "big num tagged an int, not a byte string",
3163 QCBOR_DECODE_MODE_NORMAL,
3164 {"\xc2\x00", 2},
3165 QCBOR_ERR_TAGS_DISABLED
3166 },
Laurence Lundblade37286c02022-09-03 10:05:02 -07003167#endif /* QCBOR_DISABLE_TAGS */
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003168};
3169
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08003170
3171
3172int32_t
3173DecodeFailureTests(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003174{
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003175 int32_t nResult;
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07003176
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08003177 nResult = ProcessDecodeFailures(Failures ,C_ARRAY_COUNT(Failures, struct DecodeFailTestInput));
Laurence Lundblade59289e52019-12-30 13:44:37 -08003178 if(nResult) {
3179 return nResult;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003180 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003181
Laurence Lundblade3a6042e2019-06-28 19:58:04 -07003182 // Corrupt the UsefulInputBuf and see that
3183 // it reflected correctly for CBOR decoding
Laurence Lundbladee6f15112020-07-23 18:44:16 -07003184 QCBORDecodeContext DCtx;
3185 QCBORItem Item;
3186 QCBORError uQCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003187
Laurence Lundbladee6f15112020-07-23 18:44:16 -07003188 QCBORDecode_Init(&DCtx,
3189 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSimpleValues),
3190 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003191
Laurence Lundbladee6f15112020-07-23 18:44:16 -07003192 if((uQCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
3193 return (int32_t)uQCBORError;
3194 }
3195 if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.val.uCount != 10) {
3196 // This wasn't supposed to happen
3197 return -1;
3198 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003199
Laurence Lundbladee6f15112020-07-23 18:44:16 -07003200 DCtx.InBuf.magic = 0; // Reach in and corrupt the UsefulInputBuf
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003201
Laurence Lundbladee6f15112020-07-23 18:44:16 -07003202 uQCBORError = QCBORDecode_GetNext(&DCtx, &Item);
3203 if(uQCBORError != QCBOR_ERR_NO_MORE_ITEMS) {
3204 // Did not get back the error expected
3205 return -2;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003206 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003207
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003208
Laurence Lundblade98427e92020-09-28 21:33:23 -07003209 /*
3210 The max size of a string for QCBOR is SIZE_MAX - 4 so this
3211 tests here can be performed to see that the max length
3212 error check works correctly. See DecodeBytes(). If the max
3213 size was SIZE_MAX, it wouldn't be possible to test this.
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003214
Laurence Lundblade98427e92020-09-28 21:33:23 -07003215 This test will automatocally adapt the all CPU sizes
3216 through the use of SIZE_MAX.
3217 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003218
Laurence Lundblade8510f8c2020-12-01 11:31:16 -08003219 UsefulBuf_MAKE_STACK_UB( HeadBuf, QCBOR_HEAD_BUFFER_SIZE);
Laurence Lundblade98427e92020-09-28 21:33:23 -07003220 UsefulBufC EncodedHead;
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003221
Laurence Lundblade98427e92020-09-28 21:33:23 -07003222 // This makes a CBOR head with a text string that is very long
3223 // but doesn't fill in the bytes of the text string as that is
3224 // not needed to test this part of QCBOR.
3225 EncodedHead = QCBOREncode_EncodeHead(HeadBuf, CBOR_MAJOR_TYPE_TEXT_STRING, 0, SIZE_MAX);
3226
3227 QCBORDecode_Init(&DCtx, EncodedHead, QCBOR_DECODE_MODE_NORMAL);
3228
3229 if(QCBOR_ERR_STRING_TOO_LONG != QCBORDecode_GetNext(&DCtx, &Item)) {
3230 return -4;
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003231 }
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003232
Laurence Lundblade3a6042e2019-06-28 19:58:04 -07003233 return 0;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003234}
3235
3236
Laurence Lundbladea2e29072018-12-30 09:20:06 -08003237/* Try all 256 values of the byte at nLen including recursing for
3238 each of the values to try values at nLen+1 ... up to nLenMax
3239 */
Laurence Lundblade06350ea2020-01-27 19:32:40 -08003240static void ComprehensiveInputRecurser(uint8_t *pBuf, size_t nLen, size_t nLenMax)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003241{
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003242 if(nLen >= nLenMax) {
3243 return;
3244 }
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08003245
Laurence Lundbladea2e29072018-12-30 09:20:06 -08003246 for(int inputByte = 0; inputByte < 256; inputByte++) {
3247 // Set up the input
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003248 pBuf[nLen] = (uint8_t)inputByte;
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08003249 const UsefulBufC Input = {pBuf, nLen+1};
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08003250
Laurence Lundbladea2e29072018-12-30 09:20:06 -08003251 // Get ready to parse
3252 QCBORDecodeContext DCtx;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003253 QCBORDecode_Init(&DCtx, Input, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003254
Laurence Lundbladea2e29072018-12-30 09:20:06 -08003255 // Parse by getting the next item until an error occurs
3256 // Just about every possible decoder error can occur here
3257 // The goal of this test is not to check for the correct
3258 // error since that is not really possible. It is to
3259 // see that there is no crash on hostile input.
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003260 while(1) {
Laurence Lundbladea2e29072018-12-30 09:20:06 -08003261 QCBORItem Item;
3262 QCBORError nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003263 if(nCBORError != QCBOR_SUCCESS) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003264 break;
3265 }
3266 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003267
Laurence Lundbladea2e29072018-12-30 09:20:06 -08003268 ComprehensiveInputRecurser(pBuf, nLen+1, nLenMax);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003269 }
3270}
3271
3272
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03003273int32_t ComprehensiveInputTest(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003274{
Laurence Lundbladea2e29072018-12-30 09:20:06 -08003275 // Size 2 tests 64K inputs and runs quickly
3276 uint8_t pBuf[2];
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003277
Laurence Lundbladea2e29072018-12-30 09:20:06 -08003278 ComprehensiveInputRecurser(pBuf, 0, sizeof(pBuf));
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003279
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003280 return 0;
3281}
3282
Laurence Lundbladea2e29072018-12-30 09:20:06 -08003283
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03003284int32_t BigComprehensiveInputTest(void)
Laurence Lundbladea2e29072018-12-30 09:20:06 -08003285{
3286 // size 3 tests 16 million inputs and runs OK
3287 // in seconds on fast machines. Size 4 takes
3288 // 10+ minutes and 5 half a day on fast
3289 // machines. This test is kept separate from
3290 // the others so as to no slow down the use
3291 // of them as a very frequent regression.
3292 uint8_t pBuf[3]; //
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08003293
Laurence Lundbladea2e29072018-12-30 09:20:06 -08003294 ComprehensiveInputRecurser(pBuf, 0, sizeof(pBuf));
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08003295
Laurence Lundbladea2e29072018-12-30 09:20:06 -08003296 return 0;
3297}
3298
3299
Laurence Lundbladecc7da412020-12-27 00:09:07 -08003300static const uint8_t spDateTestInput[] = {
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003301 /* 1. The valid date string "1985-04-12" */
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003302 0xc0, // tag for string date
3303 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003304
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003305 /* 2. An invalid date string due to wrong tag content type */
Laurence Lundbladec7114722020-08-13 05:11:40 -07003306 0xc0, // tag for string date
3307 0x00, // Wrong type for a string date
3308
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003309 /* 3. A valid epoch date, 1400000000; Tue, 13 May 2014 16:53:20 GMT */
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003310 0xc1, // tag for epoch date
3311 0x1a, 0x53, 0x72, 0x4E, 0x00, // Epoch date 1400000000; Tue, 13 May 2014 16:53:20 GMT
3312
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003313 /* 4. An invalid epoch date due to wrong tag content type */
Laurence Lundbladec7114722020-08-13 05:11:40 -07003314 0xc1,
3315 0x62, 'h', 'i', // wrong type tagged
3316
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003317 /* 5. Valid epoch date tag as content for a two other nested tags */
Laurence Lundblade99615302020-11-29 11:19:47 -08003318 // CBOR_TAG_ENC_AS_B64
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003319 0xcf, 0xd8, 0x16, 0xc1, // Epoch date with extra tags
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003320 0x1a, 0x53, 0x72, 0x4E, 0x01,
3321
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003322 /* 6. Epoch date with value to large to fit into int64 */
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003323 0xc1, // tag for epoch date
3324 0x1b, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, // Too large integer
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003325
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003326 /* 7. Epoch date with single-precision value of 1.1. */
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003327 0xc1, // tag for epoch date
Laurence Lundblade3ed0bca2020-07-14 22:50:10 -07003328 0xfa, 0x3f, 0x8c, 0xcc, 0xcd, // single with value 1.1
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003329
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003330 /* 8. Epoch date with too-large single precision float */
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003331 0xc1, // tag for epoch date
Laurence Lundbladea1ad8782019-11-08 00:12:11 -08003332 0xfa, 0x7f, 0x7f, 0xff, 0xff, // 3.4028234663852886e+38 too large
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003333
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003334 /* 9. Epoch date with slightly too-large double precision value */
Laurence Lundbladea1ad8782019-11-08 00:12:11 -08003335 0xc1, // tag for epoch date
3336 0xfb, 0x43, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 9223372036854775808.000000 just barely too large
3337 //0xfa, 0x7f, 0x7f, 0xff, 0xff // 3.4028234663852886e+38 too large
3338
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003339 /* 10. Epoch date with largest supported double precision value */
Laurence Lundbladea1ad8782019-11-08 00:12:11 -08003340 0xc1, // tag for epoch date
Laurence Lundbladec7114722020-08-13 05:11:40 -07003341 0xfb, 0x43, 0xdf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, // 9223372036854773760 largest supported
3342
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003343 /* 11. Epoch date with single-precision NaN */
Laurence Lundbladec7114722020-08-13 05:11:40 -07003344 0xc1, // tag for epoch date
3345 0xfa, 0x7f, 0xc0, 0x00, 0x00, // Single-precision NaN
3346
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003347 /* 12. Epoch date with double precision plus infinity */
Laurence Lundbladec7114722020-08-13 05:11:40 -07003348 0xc1,
3349 0xfb, 0x7f, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // +infinity
3350
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003351 /* 13. Epoch date with half-precision negative infinity */
Laurence Lundbladec7114722020-08-13 05:11:40 -07003352 0xc1, // tag for epoch date
3353 0xf9, 0xfc, 0x00, // -Infinity
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003354};
3355
3356
Laurence Lundbladec7114722020-08-13 05:11:40 -07003357
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003358// have to check float expected only to within an epsilon
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07003359#ifndef QCBOR_DISABLE_FLOAT_HW_USE
Laurence Lundblade02fcf312020-07-17 02:49:46 -07003360static int CHECK_EXPECTED_DOUBLE(double val, double expected) {
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003361
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003362 double diff = val - expected;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003363
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003364 diff = fabs(diff);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003365
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003366 return diff > 0.0000001;
3367}
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07003368#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003369
3370
Laurence Lundblade37286c02022-09-03 10:05:02 -07003371/* Test date decoding using GetNext() */
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03003372int32_t DateParseTest(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003373{
3374 QCBORDecodeContext DCtx;
Laurence Lundbladec7114722020-08-13 05:11:40 -07003375 QCBORItem Item;
3376 QCBORError uError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003377
Laurence Lundbladeee851742020-01-08 08:37:05 -08003378 QCBORDecode_Init(&DCtx,
3379 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spDateTestInput),
3380 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003381
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003382 /* 1. The valid date string "1985-04-12" */
Laurence Lundbladec7114722020-08-13 05:11:40 -07003383 if((uError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003384 return -1;
Laurence Lundbladec7114722020-08-13 05:11:40 -07003385 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003386 if(Item.uDataType != QCBOR_TYPE_DATE_STRING ||
Laurence Lundbladeba587682024-02-07 16:46:43 -08003387 UsefulBufCompareToSZ(Item.val.string, "1985-04-12")){
Laurence Lundblade67bd5512018-11-02 21:44:06 +07003388 return -2;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003389 }
3390
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003391 /* 2. An invalid date string due to wrong tag content type */
Laurence Lundbladec7114722020-08-13 05:11:40 -07003392 uError = QCBORDecode_GetNext(&DCtx, &Item);
3393 if(uError != QCBOR_ERR_BAD_OPT_TAG) {
Laurence Lundblade67bd5512018-11-02 21:44:06 +07003394 return -3;
Laurence Lundbladec7114722020-08-13 05:11:40 -07003395 }
3396
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003397 /* 3. A valid epoch date, 1400000000; Tue, 13 May 2014 16:53:20 GMT */
3398 uError = QCBORDecode_GetNext(&DCtx, &Item);
3399 if(uError != QCBOR_SUCCESS) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07003400 return -4;
3401 }
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003402 if(uError == QCBOR_SUCCESS) {
3403 if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH ||
3404 Item.val.epochDate.nSeconds != 1400000000
Máté Tóth-Pálef5f07a2021-09-17 19:31:37 +02003405#ifndef USEFULBUF_DISABLE_ALL_FLOAT
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003406 || Item.val.epochDate.fSecondsFraction != 0
Máté Tóth-Pálef5f07a2021-09-17 19:31:37 +02003407#endif /* USEFULBUF_DISABLE_ALL_FLOAT */
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003408 ) {
3409 return -5;
3410 }
Laurence Lundbladec7114722020-08-13 05:11:40 -07003411 }
3412
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003413 /* 4. An invalid epoch date due to wrong tag content type */
Laurence Lundbladec7114722020-08-13 05:11:40 -07003414 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_OPT_TAG) {
3415 return -6;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003416 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003417
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003418 /* 5. Valid epoch date tag as content for a two other nested tags */
Laurence Lundblade99615302020-11-29 11:19:47 -08003419 // Epoch date wrapped in an CBOR_TAG_ENC_AS_B64 and an unknown tag.
3420 // The date is decoded and the two tags are returned. This is to
3421 // make sure the wrapping of epoch date in another tag works OK.
Laurence Lundbladec7114722020-08-13 05:11:40 -07003422 if((uError = QCBORDecode_GetNext(&DCtx, &Item))) {
3423 return -7;
3424 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003425 if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH ||
3426 Item.val.epochDate.nSeconds != 1400000001 ||
Máté Tóth-Pálef5f07a2021-09-17 19:31:37 +02003427#ifndef USEFULBUF_DISABLE_ALL_FLOAT
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003428 Item.val.epochDate.fSecondsFraction != 0 ||
Máté Tóth-Pálef5f07a2021-09-17 19:31:37 +02003429#endif /* USEFULBUF_DISABLE_ALL_FLOAT */
Laurence Lundblade99615302020-11-29 11:19:47 -08003430 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_ENC_AS_B64)) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07003431 return -8;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003432 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003433
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003434 /* 6. Epoch date with value to large to fit into int64 */
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003435 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_DATE_OVERFLOW) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07003436 return -9;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003437 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003438
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003439 /* 7. Epoch date with single-precision value of 1.1. */
3440 uError = QCBORDecode_GetNext(&DCtx, &Item);
3441 if(uError != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07003442 return -10;
3443 }
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003444 if(uError == QCBOR_SUCCESS) {
3445 if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH ||
3446 Item.val.epochDate.nSeconds != 1
3447#ifndef QCBOR_DISABLE_FLOAT_HW_USE
3448 || CHECK_EXPECTED_DOUBLE(Item.val.epochDate.fSecondsFraction, 0.1)
Máté Tóth-Pálef5f07a2021-09-17 19:31:37 +02003449#endif /* USEFULBUF_DISABLE_ALL_FLOAT */
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003450 ) {
3451 return -11;
3452 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003453 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003454
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003455 /* 8. Epoch date with too-large single-precision float */
3456 uError = QCBORDecode_GetNext(&DCtx, &Item);
3457 if(uError != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_DATE_OVERFLOW)) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07003458 return -12;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003459 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003460
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003461 /* 9. Epoch date with slightly too-large double-precision value */
3462 uError = QCBORDecode_GetNext(&DCtx, &Item);
3463 if(uError != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_DATE_OVERFLOW)) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07003464 return -13;
Laurence Lundbladea1ad8782019-11-08 00:12:11 -08003465 }
3466
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003467 /* 10. Epoch date with largest supported double-precision value */
3468 uError = QCBORDecode_GetNext(&DCtx, &Item);
3469 if(uError != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)) {
3470 return -14;
3471 }
3472 if(uError == QCBOR_SUCCESS) {
3473 if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH ||
3474 Item.val.epochDate.nSeconds != 9223372036854773760
3475#ifndef QCBOR_DISABLE_FLOAT_HW_USE
3476 || Item.val.epochDate.fSecondsFraction != 0.0
3477#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
3478 ) {
3479 return -14;
3480 }
3481 }
Laurence Lundbladec7114722020-08-13 05:11:40 -07003482
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003483 /* 11. Epoch date with single-precision NaN */
3484 if(QCBORDecode_GetNext(&DCtx, &Item) != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_DATE_OVERFLOW)) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07003485 return -15;
3486 }
3487
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003488 /* 12. Epoch date with double-precision plus infinity */
3489 if(QCBORDecode_GetNext(&DCtx, &Item) != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_DATE_OVERFLOW)) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07003490 return -16;
3491 }
3492
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003493 /* 13. Epoch date with half-precision negative infinity */
3494 uError = QCBORDecode_GetNext(&DCtx, &Item);
3495 if(uError != FLOAT_ERR_CODE_NO_HALF_PREC_NO_FLOAT_HW(QCBOR_ERR_DATE_OVERFLOW)) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07003496 return -17;
3497 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003498
3499 return 0;
3500}
3501
Laurence Lundblade37286c02022-09-03 10:05:02 -07003502
Laurence Lundblade4b270642020-08-14 12:53:07 -07003503/*
3504 Test cases covered here. Some items cover more than one of these.
3505 positive integer (zero counts as a positive integer)
3506 negative integer
3507 half-precision float
3508 single-precision float
3509 double-precision float
Laurence Lundbladec7114722020-08-13 05:11:40 -07003510
Laurence Lundblade4b270642020-08-14 12:53:07 -07003511 float Overflow error
3512 Wrong type error for epoch
3513 Wrong type error for date string
3514 float disabled error
3515 half-precision disabled error
3516 -Infinity
3517 Slightly too large integer
3518 Slightly too far from zero
Laurence Lundbladec7114722020-08-13 05:11:40 -07003519
Laurence Lundblade4b270642020-08-14 12:53:07 -07003520 Get epoch by int
3521 Get string by int
3522 Get epoch by string
3523 Get string by string
3524 Fail to get epoch by wrong int label
3525 Fail to get string by wrong string label
3526 Fail to get epoch by string because it is invalid
3527 Fail to get epoch by int because it is invalid
3528
3529 Untagged values
3530 */
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08003531static const uint8_t spSpiffyDateTestInput[] = {
Laurence Lundblade37286c02022-09-03 10:05:02 -07003532 0x87, // array of 7 items
3533
3534 0xa6, // Open a map for tests involving untagged items with labels.
3535
3536 // Untagged integer 0
3537 0x08,
3538 0x00,
3539
3540 // Utagged date string with string label y
3541 0x61, 0x79,
3542 0x6a, '2','0','8','5','-','0','4','-','1','2', // Untagged date string
3543
3544 // Untagged single-precision float with value 3.14 with string label x
3545 0x61, 0x78,
3546 0xFA, 0x40, 0x48, 0xF5, 0xC3,
3547
3548 // Untagged half-precision float with value -2
3549 0x09,
3550 0xF9, 0xC0, 0x00,
3551
3552 /* Untagged date-only date string */
3553 0x18, 0x63,
3554 0x6A, 0x31, 0x39, 0x38, 0x35, 0x2D, 0x30, 0x34, 0x2D, 0x31, 0x32, /* "1985-04-12" */
3555
3556 /* Untagged days-count epoch date */
3557 0x11,
3558 0x19, 0x0F, 0x9A, /* 3994 */
3559
3560 // End of map, back to array
3561
3562 0xa7, // Open map of tagged items with labels
3563
3564 0x00,
3565 0xc0, // tag for string date
3566 0x6a, '1','9','8','5','-','0','4','-','1','2', // Tagged date string
3567
3568
3569 0x01,
3570 0xda, 0x03, 0x03, 0x03, 0x03, // An additional tag
3571 0xc1, // tag for epoch date
3572 0x1a, 0x53, 0x72, 0x4E, 0x00, // Epoch date 1400000000; Tue, 13 May 2014 16:53:20 GMT
3573
3574 0x05,
3575 0xc1,
3576 0xfb, 0xc3, 0xdf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, // -9223372036854773760 largest negative
3577
3578
3579 0x07,
3580 0xc1, // tag for epoch date
3581 0xfb, 0x43, 0xdf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, // 9223372036854773760 largest supported
3582
3583 /* Tagged days-count epoch date */
3584 0x63, 0x53, 0x44, 0x45,
3585 0xD8, 0x64, /* tag(100) */
3586 0x39, 0x29, 0xB3, /* -10676 */
3587
3588 // Untagged -1000 with label z
3589 0x61, 0x7a,
3590 0xda, 0x01, 0x01, 0x01, 0x01, // An additional tag
3591 0x39, 0x03, 0xe7,
3592
3593 /* Tagged date-only date string */
3594 0x63, 0x53, 0x44, 0x53,
3595 0xD9, 0x03, 0xEC,
3596 0x6A, 0x31, 0x39, 0x38, 0x35, 0x2D, 0x30, 0x34, 0x2D, 0x31, 0x32, /* "1985-04-12" */
3597
3598 // End of map of tagged items
Laurence Lundblade4b270642020-08-14 12:53:07 -07003599
3600 0xc1,
3601 0xfb, 0xc3, 0xdf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, // -9.2233720368547748E+18, too negative
3602
Laurence Lundbladec7114722020-08-13 05:11:40 -07003603 0xc1, // tag for epoch date
Laurence Lundblade4b270642020-08-14 12:53:07 -07003604 0x1b, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, // Too-large integer
3605
3606 0xc1, // tag for epoch date
3607 0xf9, 0xfc, 0x00, // Half-precision -Infinity
3608
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07003609 // These two at the end because they are unrecoverable errors
3610 0xc1, // tag for epoch date
3611 0x80, // Erroneous empty array as content for date
3612
3613 0xc0, // tag for string date
3614 0xa0 // Erroneous empty map as content for date
Laurence Lundbladec7114722020-08-13 05:11:40 -07003615};
3616
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03003617int32_t SpiffyDateDecodeTest(void)
Laurence Lundbladec7114722020-08-13 05:11:40 -07003618{
3619 QCBORDecodeContext DC;
Laurence Lundblade4b270642020-08-14 12:53:07 -07003620 QCBORError uError;
Laurence Lundblade37286c02022-09-03 10:05:02 -07003621 int64_t nEpochDate3, nEpochDate5,
3622 nEpochDate4, nEpochDate6,
3623 nEpochDays2;
3624 UsefulBufC StringDate1, StringDate2, StringDays2;
Laurence Lundbladec7114722020-08-13 05:11:40 -07003625
3626 QCBORDecode_Init(&DC,
Laurence Lundblade4b270642020-08-14 12:53:07 -07003627 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSpiffyDateTestInput),
Laurence Lundbladec7114722020-08-13 05:11:40 -07003628 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade37286c02022-09-03 10:05:02 -07003629
3630 /* Items are in an array or map to test look up by label and other
3631 * that might not occur in isolated items. But it does make the
3632 * test a bit messy. */
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07003633 QCBORDecode_EnterArray(&DC, NULL);
Laurence Lundbladec7114722020-08-13 05:11:40 -07003634
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07003635 QCBORDecode_EnterMap(&DC, NULL);
Laurence Lundblade4b270642020-08-14 12:53:07 -07003636
Laurence Lundblade4b270642020-08-14 12:53:07 -07003637 // A single-precision date
Laurence Lundblade9b334962020-08-27 10:55:53 -07003638 QCBORDecode_GetEpochDateInMapSZ(&DC, "x", QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
3639 &nEpochDate5);
Laurence Lundblade4b270642020-08-14 12:53:07 -07003640 uError = QCBORDecode_GetAndResetError(&DC);
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003641 if(uError != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07003642 return 104;
3643 }
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003644 if(uError == QCBOR_SUCCESS) {
3645 if(nEpochDate5 != 3) {
3646 return 103;
3647 }
3648 }
Laurence Lundbladec7114722020-08-13 05:11:40 -07003649
Laurence Lundblade9b334962020-08-27 10:55:53 -07003650 // A half-precision date with value -2 FFF
3651 QCBORDecode_GetEpochDateInMapN(&DC, 9, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
3652 &nEpochDate4);
Laurence Lundblade4b270642020-08-14 12:53:07 -07003653 uError = QCBORDecode_GetAndResetError(&DC);
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003654 if(uError != FLOAT_ERR_CODE_NO_HALF_PREC_NO_FLOAT_HW(QCBOR_SUCCESS)) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07003655 return 106;
3656 }
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003657 if(uError == QCBOR_SUCCESS) {
3658 if(nEpochDate4 != -2) {
3659 return 105;
3660 }
3661 }
Laurence Lundblade4b270642020-08-14 12:53:07 -07003662
3663 // Fail to get an epoch date by string label
Laurence Lundblade9b334962020-08-27 10:55:53 -07003664 QCBORDecode_GetEpochDateInMapSZ(&DC, "no-label",
3665 QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
3666 &nEpochDate6);
Laurence Lundblade4b270642020-08-14 12:53:07 -07003667 uError = QCBORDecode_GetAndResetError(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07003668 if(uError != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07003669 return 107;
3670 }
3671
3672 // Fail to get an epoch date by integer label
Laurence Lundblade9b334962020-08-27 10:55:53 -07003673 QCBORDecode_GetEpochDateInMapN(&DC, 99999, QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
3674 &nEpochDate6);
Laurence Lundblade4b270642020-08-14 12:53:07 -07003675 uError = QCBORDecode_GetAndResetError(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07003676 if(uError != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07003677 return 108;
3678 }
3679
3680 // Fail to get a string date by string label
Laurence Lundblade9b334962020-08-27 10:55:53 -07003681 QCBORDecode_GetDateStringInMapSZ(&DC, "no-label",
3682 QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
3683 &StringDate1);
Laurence Lundblade4b270642020-08-14 12:53:07 -07003684 uError = QCBORDecode_GetAndResetError(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07003685 if(uError != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07003686 return 109;
3687 }
3688
3689 // Fail to get a string date by integer label
Laurence Lundblade9b334962020-08-27 10:55:53 -07003690 QCBORDecode_GetDateStringInMapN(&DC, 99999, QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
3691 &StringDate1);
Laurence Lundblade4b270642020-08-14 12:53:07 -07003692 uError = QCBORDecode_GetAndResetError(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07003693 if(uError != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07003694 return 110;
3695 }
3696
3697 // The rest of these succeed even if float features are disabled
Laurence Lundbladea9489f82020-09-12 13:50:56 -07003698
Laurence Lundblade37286c02022-09-03 10:05:02 -07003699
3700 // Untagged integer 0
3701 QCBORDecode_GetEpochDateInMapN(&DC, 8, QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
3702 &nEpochDate3);
3703 // Untagged date string
3704 QCBORDecode_GetDateStringInMapSZ(&DC, "y", QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
3705 &StringDate2);
3706
3707 QCBORDecode_GetDaysStringInMapN(&DC, 99, QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
3708 &StringDays2);
3709
3710 QCBORDecode_GetEpochDaysInMapN(&DC, 17, QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
3711 &nEpochDays2);
3712
3713 QCBORDecode_ExitMap(&DC);
3714 if(QCBORDecode_GetError(&DC) != QCBOR_SUCCESS) {
3715 return 3001;
3716 }
3717
3718 // The map of tagged items
3719 QCBORDecode_EnterMap(&DC, NULL);
3720
3721#ifndef QCBOR_DISABLE_TAGS
3722 int64_t nEpochDate2,
3723 nEpochDateFail,
3724 nEpochDate1400000000, nEpochDays1;
3725 UsefulBufC StringDays1;
3726 uint64_t uTag1, uTag2;
3727
3728 // Tagged date string
3729 QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
3730 &StringDate1);
3731
Laurence Lundblade4b270642020-08-14 12:53:07 -07003732 // Epoch date 1400000000; Tue, 13 May 2014 16:53:20 GMT
Laurence Lundblade9b334962020-08-27 10:55:53 -07003733 QCBORDecode_GetEpochDateInMapN(&DC,
3734 1,
3735 QCBOR_TAG_REQUIREMENT_TAG |
3736 QCBOR_TAG_REQUIREMENT_ALLOW_ADDITIONAL_TAGS,
3737 &nEpochDate1400000000);
3738 uTag1 = QCBORDecode_GetNthTagOfLast(&DC, 0);
Laurence Lundblade37286c02022-09-03 10:05:02 -07003739
3740 // Get largest negative double precision epoch date allowed
3741 QCBORDecode_GetEpochDateInMapN(&DC,
3742 5,
3743 QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG |
3744 QCBOR_TAG_REQUIREMENT_ALLOW_ADDITIONAL_TAGS,
3745 &nEpochDate2);
3746 uError = QCBORDecode_GetAndResetError(&DC);
3747 if(uError != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)) {
3748 return 102;
3749 }
3750 if(uError == QCBOR_SUCCESS) {
3751 if(nEpochDate2 != -9223372036854773760LL) {
3752 return 101;
3753 }
3754 }
3755
Laurence Lundblade4b270642020-08-14 12:53:07 -07003756 // Untagged -1000 with label z
Laurence Lundblade9b334962020-08-27 10:55:53 -07003757 QCBORDecode_GetEpochDateInMapSZ(&DC,
3758 "z",
3759 QCBOR_TAG_REQUIREMENT_NOT_A_TAG |
3760 QCBOR_TAG_REQUIREMENT_ALLOW_ADDITIONAL_TAGS,
3761 &nEpochDate6);
3762 uTag2 = QCBORDecode_GetNthTagOfLast(&DC, 0);
Laurence Lundblade4b270642020-08-14 12:53:07 -07003763
Laurence Lundblade37286c02022-09-03 10:05:02 -07003764
3765 // Get largest double precision epoch date allowed
3766 QCBORDecode_GetEpochDateInMapN(&DC, 7, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
3767 &nEpochDate2);
3768 uError = QCBORDecode_GetAndResetError(&DC);
3769 if(uError != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)) {
3770 return 112;
3771 }
3772 if(uError == QCBOR_SUCCESS) {
3773 if(nEpochDate2 != 9223372036854773760ULL) {
3774 return 111;
3775 }
3776 }
3777
Laurence Lundblade46d63e92021-05-13 11:37:10 -07003778 /* The days format is much simpler than the date format
3779 * because it can't be a floating point value. The test
3780 * of the spiffy decode functions sufficiently covers
3781 * the test of the non-spiffy decode days date decoding.
3782 * There is no full fan out of the error conditions
3783 * and decode options as that is implemented by code
3784 * that is tested well by the date testing above.
3785 */
3786 QCBORDecode_GetDaysStringInMapSZ(&DC, "SDS", QCBOR_TAG_REQUIREMENT_TAG,
3787 &StringDays1);
3788
Laurence Lundblade46d63e92021-05-13 11:37:10 -07003789 QCBORDecode_GetEpochDaysInMapSZ(&DC, "SDE", QCBOR_TAG_REQUIREMENT_TAG,
3790 &nEpochDays1);
3791
Laurence Lundblade4b270642020-08-14 12:53:07 -07003792 QCBORDecode_ExitMap(&DC);
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07003793 if(QCBORDecode_GetError(&DC) != QCBOR_SUCCESS) {
3794 return 3001;
3795 }
3796
Laurence Lundblade37286c02022-09-03 10:05:02 -07003797 // Too-negative float, -9.2233720368547748E+18
3798 QCBORDecode_GetEpochDate(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nEpochDateFail);
3799 uError = QCBORDecode_GetAndResetError(&DC);
3800 if(uError != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_DATE_OVERFLOW)) {
3801 return 1111;
3802 }
3803
3804 // Too-large integer
3805 QCBORDecode_GetEpochDate(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nEpochDateFail);
3806 uError = QCBORDecode_GetAndResetError(&DC);
3807 if(uError != QCBOR_ERR_DATE_OVERFLOW) {
3808 return 1;
3809 }
3810
3811 // Half-precision minus infinity
3812 QCBORDecode_GetEpochDate(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nEpochDateFail);
3813 uError = QCBORDecode_GetAndResetError(&DC);
3814 if(uError != FLOAT_ERR_CODE_NO_HALF_PREC_NO_FLOAT_HW(QCBOR_ERR_DATE_OVERFLOW)) {
3815 return 2;
3816 }
3817
3818
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07003819 // Bad content for epoch date
3820 QCBORDecode_GetEpochDate(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nEpochDateFail);
3821 uError = QCBORDecode_GetAndResetError(&DC);
3822 if(uError != QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT) {
3823 return 3;
3824 }
3825
3826 // Bad content for string date
3827 QCBORDecode_GetDateString(&DC, QCBOR_TAG_REQUIREMENT_TAG, &StringDate1);
3828 uError = QCBORDecode_GetAndResetError(&DC);
3829 if(uError != QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT) {
3830 return 4;
3831 }
3832
Laurence Lundblade4b270642020-08-14 12:53:07 -07003833 QCBORDecode_ExitArray(&DC);
3834 uError = QCBORDecode_Finish(&DC);
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07003835 if(uError != QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT) {
Laurence Lundblade9b334962020-08-27 10:55:53 -07003836 return 1000 + (int32_t)uError;
Laurence Lundblade4b270642020-08-14 12:53:07 -07003837 }
Laurence Lundblade37286c02022-09-03 10:05:02 -07003838#else /* QCBOR_DISABLE_TAGS */
3839 QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
3840 &StringDate1);
3841 uError = QCBORDecode_GetAndResetError(&DC);
3842 if(uError != QCBOR_ERR_TAGS_DISABLED) {
3843 return 4;
3844 }
3845#endif /* QCBOR_DISABLE_TAGS */
3846
3847
3848#ifndef QCBOR_DISABLE_TAGS
Laurence Lundblade4b270642020-08-14 12:53:07 -07003849
Laurence Lundblade9b334962020-08-27 10:55:53 -07003850 if(nEpochDate1400000000 != 1400000000) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07003851 return 200;
3852 }
3853
Laurence Lundblade9b334962020-08-27 10:55:53 -07003854 if(uTag1 != 0x03030303) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07003855 return 201;
3856 }
3857
Laurence Lundblade37286c02022-09-03 10:05:02 -07003858 if(nEpochDays1 != -10676) {
3859 return 205;
Laurence Lundblade4b270642020-08-14 12:53:07 -07003860 }
3861
Laurence Lundblade37286c02022-09-03 10:05:02 -07003862 if(UsefulBuf_Compare(StringDays1, UsefulBuf_FromSZ("1985-04-12"))) {
3863 return 207;
Laurence Lundblade4b270642020-08-14 12:53:07 -07003864 }
3865
Laurence Lundblade9b334962020-08-27 10:55:53 -07003866 if(uTag2 != 0x01010101) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07003867 return 204;
3868 }
3869
Laurence Lundblade37286c02022-09-03 10:05:02 -07003870 if(nEpochDate6 != -1000) {
3871 return 203;
Laurence Lundblade46d63e92021-05-13 11:37:10 -07003872 }
3873
Laurence Lundblade9b334962020-08-27 10:55:53 -07003874 if(UsefulBuf_Compare(StringDate1, UsefulBuf_FromSZ("1985-04-12"))) {
3875 return 205;
3876 }
3877
Laurence Lundblade37286c02022-09-03 10:05:02 -07003878#endif /* QCBOR_DISABLE_TAGS */
3879
3880 if(nEpochDate3 != 0) {
3881 return 202;
3882 }
3883
3884 if(nEpochDays2 != 3994) {
Laurence Lundblade9b334962020-08-27 10:55:53 -07003885 return 206;
3886 }
3887
Laurence Lundblade37286c02022-09-03 10:05:02 -07003888 if(UsefulBuf_Compare(StringDate2, UsefulBuf_FromSZ("2085-04-12"))) {
3889 return 206;
Laurence Lundblade46d63e92021-05-13 11:37:10 -07003890 }
3891
3892 if(UsefulBuf_Compare(StringDays2, UsefulBuf_FromSZ("1985-04-12"))) {
3893 return 208;
3894 }
3895
Laurence Lundbladec7114722020-08-13 05:11:40 -07003896 return 0;
3897}
3898
3899
Laurence Lundblade9b334962020-08-27 10:55:53 -07003900// Input for one of the tagging tests
Laurence Lundbladecc7da412020-12-27 00:09:07 -08003901static const uint8_t spTagInput[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003902 0xd9, 0xd9, 0xf7, // CBOR magic number
Laurence Lundblade9b334962020-08-27 10:55:53 -07003903 0x81, // Array of one
3904 0xd8, 0x04, // non-preferred serialization of tag 4, decimal fraction
3905 0x82, // Array of two that is the faction 1/3
3906 0x01,
3907 0x03,
3908
3909 /*
3910 More than 4 tags on an item 225(226(227(228(229([])))))
3911 */
3912 0xd8, 0xe1,
3913 0xd8, 0xe2,
3914 0xd8, 0xe3,
3915 0xd8, 0xe4,
3916 0xd8, 0xe5,
3917 0x80,
3918
3919 /* tag 10489608748473423768(
3920 2442302356(
3921 21590(
3922 240(
3923 []))))
3924 */
3925 0xdb, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98,
3926 0xda, 0x91, 0x92, 0x93, 0x94,
3927 0xd9, 0x54, 0x56,
3928 0xd8, 0xf0,
3929 0x80,
3930
3931 /* tag 21590(
3932 10489608748473423768(
3933 2442302357(
3934 65534(
3935 []))))
3936 */
3937 0xdb, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x54, 0x56,
3938 0xdb, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98,
3939 0xda, 0x91, 0x92, 0x93, 0x95,
3940 0xd9, 0xff, 0xfe,
3941 0x80,
3942
3943 /* Make sure to blow past the limit of tags that must be mapped.
3944 works in conjuntion with entries above.
3945 269488144(269488145(269488146(269488147([]))))
3946 */
3947 0xda, 0x10, 0x10, 0x10, 0x10,
3948 0xda, 0x10, 0x10, 0x10, 0x11,
3949 0xda, 0x10, 0x10, 0x10, 0x12,
3950 0xda, 0x10, 0x10, 0x10, 0x13,
3951 0x80,
3952
3953 /* An invalid decimal fraction with an additional tag */
3954 0xd9, 0xff, 0xfa,
3955 0xd8, 0x02, // non-preferred serialization of tag 2, a big num
3956 0x00, // the integer 0; should be a byte string
3957};
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003958
Laurence Lundblade59289e52019-12-30 13:44:37 -08003959/*
3960 DB 9192939495969798 # tag(10489608748473423768)
Laurence Lundblade9b334962020-08-27 10:55:53 -07003961 80 # array(0)
Laurence Lundblade59289e52019-12-30 13:44:37 -08003962 */
Laurence Lundbladecc7da412020-12-27 00:09:07 -08003963static const uint8_t spEncodedLargeTag[] = {0xdb, 0x91, 0x92, 0x93, 0x94, 0x95,
Laurence Lundbladeee851742020-01-08 08:37:05 -08003964 0x96, 0x97, 0x98, 0x80};
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003965
Laurence Lundblade59289e52019-12-30 13:44:37 -08003966/*
3967DB 9192939495969798 # tag(10489608748473423768)
3968 D8 88 # tag(136)
3969 C6 # tag(6)
3970 C7 # tag(7)
3971 80 # array(0)
3972*/
Laurence Lundbladecc7da412020-12-27 00:09:07 -08003973static const uint8_t spLotsOfTags[] = {0xdb, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96,
Laurence Lundbladeee851742020-01-08 08:37:05 -08003974 0x97, 0x98, 0xd8, 0x88, 0xc6, 0xc7, 0x80};
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003975
3976/*
Laurence Lundblade9b334962020-08-27 10:55:53 -07003977 55799(55799(55799({
3978 6(7(-23)): 5859837686836516696(7({
3979 7(-20): 11({
3980 17(-18): 17(17(17("Organization"))),
3981 9(-17): 773("SSG"),
3982 -15: 16(17(6(7("Confusion")))),
3983 17(-16): 17("San Diego"),
3984 17(-14): 17("US")
3985 }),
3986 23(-19): 19({
3987 -11: 9({
3988 -9: -7
3989 }),
3990 90599561(90599561(90599561(-10))): 12(h'0102030405060708090A')
3991 })
3992 })),
3993 16(-22): 23({
3994 11(8(7(-5))): 8(-3)
3995 })
3996 })))
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003997 */
Laurence Lundbladecc7da412020-12-27 00:09:07 -08003998static const uint8_t spCSRWithTags[] = {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003999 0xd9, 0xd9, 0xf7, 0xd9, 0xd9, 0xf7, 0xd9, 0xd9, 0xf7, 0xa2,
4000 0xc6, 0xc7, 0x36,
4001 0xdb, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0xc7, 0xa2,
4002 0xda, 0x00, 0x00, 0x00, 0x07, 0x33,
4003 0xcb, 0xa5,
4004 0xd1, 0x31,
4005 0xd1, 0xd1, 0xd1, 0x6c,
4006 0x4f, 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74, 0x69, 0x6f, 0x6e,
4007 0xc9, 0x30,
4008 0xd9, 0x03, 0x05, 0x63,
4009 0x53, 0x53, 0x47,
4010 0x2e,
Laurence Lundblade9b334962020-08-27 10:55:53 -07004011 0xd0, 0xd1, 0xc6, 0xc7,
4012 0x69,
4013 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73, 0x69, 0x6f, 0x6e,
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004014 0xd1, 0x2f,
4015 0xd1, 0x69,
4016 0x53, 0x61, 0x6e, 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f,
4017 0xd1, 0x2d,
4018 0xd1, 0x62,
4019 0x55, 0x53,
4020 0xd7, 0x32,
4021 0xd3, 0xa2,
4022 0x2a,
4023 0xc9, 0xa1,
4024 0x28,
4025 0x26,
4026 0xda, 0x05, 0x66, 0x70, 0x89, 0xda, 0x05, 0x66, 0x70, 0x89, 0xda, 0x05, 0x66, 0x70, 0x89, 0x29,
4027 0xcc, 0x4a,
4028 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,0x07, 0x08, 0x09, 0x0a,
4029 0xd0, 0x35,
4030 0xd7, 0xa1,
4031 0xcb, 0xc8, 0xc7, 0x24,
4032 0xc8, 0x22};
4033
Laurence Lundblade9b334962020-08-27 10:55:53 -07004034
Laurence Lundbladecc7da412020-12-27 00:09:07 -08004035static const uint8_t spSpiffyTagInput[] = {
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08004036 0x85, // Open array
Laurence Lundblade9b334962020-08-27 10:55:53 -07004037
4038 0xc0, // tag for string date
4039 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string
4040
4041 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string
4042
4043 0x4a, '1','9','8','5','-','0','4','-','1','2', // Date string in byte string
4044
4045 0xd8, 0x23, // tag for regex
4046 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string
4047
4048 0xc0, // tag for string date
4049 0x4a, '1','9','8','5','-','0','4','-','1','2', // Date string in byte string
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07004050
4051 // This last case makes the array untraversable because it is
4052 // an uncrecoverable error. Make sure it stays last and is the only
4053 // instance so the other tests can work.
Laurence Lundblade9b334962020-08-27 10:55:53 -07004054};
4055
4056
Laurence Lundbladec5fef682020-01-25 11:38:45 -08004057static int32_t CheckCSRMaps(QCBORDecodeContext *pDC);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004058
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004059
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03004060int32_t OptTagParseTest(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004061{
4062 QCBORDecodeContext DCtx;
Laurence Lundblade9b334962020-08-27 10:55:53 -07004063 QCBORItem Item;
4064 QCBORError uError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004065
Laurence Lundbladeee851742020-01-08 08:37:05 -08004066 QCBORDecode_Init(&DCtx,
Laurence Lundblade9b334962020-08-27 10:55:53 -07004067 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTagInput),
Laurence Lundbladeee851742020-01-08 08:37:05 -08004068 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004069
Laurence Lundblade9b334962020-08-27 10:55:53 -07004070 /*
4071 This test matches the magic number tag and the fraction tag
4072 55799([...])
4073 */
4074 uError = QCBORDecode_GetNext(&DCtx, &Item);
4075 if(uError != QCBOR_SUCCESS) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004076 return -2;
4077 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004078 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004079 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC)) {
4080 return -3;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004081 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004082
Laurence Lundblade9b334962020-08-27 10:55:53 -07004083 /*
4084 4([1,3])
4085 */
4086 uError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundbladedd6e76e2021-03-10 01:54:01 -07004087#ifdef QCBOR_DISABLE_EXP_AND_MANTISSA
Laurence Lundblade9b334962020-08-27 10:55:53 -07004088 if(uError != QCBOR_SUCCESS ||
4089 Item.uDataType != QCBOR_TYPE_ARRAY ||
4090 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_DECIMAL_FRACTION) ||
4091 QCBORDecode_GetNthTag(&DCtx, &Item, 0) != CBOR_TAG_DECIMAL_FRACTION ||
4092 QCBORDecode_GetNthTag(&DCtx, &Item, 1) != CBOR_TAG_INVALID64 ||
4093 QCBORDecode_GetNthTag(&DCtx, &Item, 2) != CBOR_TAG_INVALID64 ||
4094 QCBORDecode_GetNthTag(&DCtx, &Item, 3) != CBOR_TAG_INVALID64 ||
4095 QCBORDecode_GetNthTag(&DCtx, &Item, 4) != CBOR_TAG_INVALID64 ||
4096 Item.val.uCount != 2) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004097 return -4;
4098 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07004099 // consume the items in the array
4100 uError = QCBORDecode_GetNext(&DCtx, &Item);
4101 uError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundblade59289e52019-12-30 13:44:37 -08004102
Laurence Lundbladedd6e76e2021-03-10 01:54:01 -07004103#else /* QCBOR_DISABLE_EXP_AND_MANTISSA */
Laurence Lundblade9b334962020-08-27 10:55:53 -07004104 if(uError != QCBOR_SUCCESS ||
4105 Item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION ||
4106 QCBORDecode_GetNthTag(&DCtx, &Item, 0) != CBOR_TAG_INVALID64 ||
4107 QCBORDecode_GetNthTag(&DCtx, &Item, 1) != CBOR_TAG_INVALID64 ||
4108 QCBORDecode_GetNthTag(&DCtx, &Item, 2) != CBOR_TAG_INVALID64 ||
4109 QCBORDecode_GetNthTag(&DCtx, &Item, 3) != CBOR_TAG_INVALID64 ||
4110 QCBORDecode_GetNthTag(&DCtx, &Item, 4) != CBOR_TAG_INVALID64 ) {
4111 return -5;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004112 }
Laurence Lundbladedd6e76e2021-03-10 01:54:01 -07004113#endif /* QCBOR_DISABLE_EXP_AND_MANTISSA */
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004114
Laurence Lundblade9b334962020-08-27 10:55:53 -07004115 /*
4116 More than 4 tags on an item 225(226(227(228(229([])))))
4117 */
4118 uError = QCBORDecode_GetNext(&DCtx, &Item);
4119 if(uError != QCBOR_ERR_TOO_MANY_TAGS) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004120 return -6;
4121 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07004122
Laurence Lundblade88e9db22020-11-02 03:56:33 -08004123 if(QCBORDecode_GetNthTag(&DCtx, &Item, 0) != CBOR_TAG_INVALID64) {
4124 return -106;
4125 }
4126
4127
Laurence Lundblade9b334962020-08-27 10:55:53 -07004128 /* tag 10489608748473423768(
4129 2442302356(
4130 21590(
4131 240(
4132 []))))
4133 */
4134 uError = QCBORDecode_GetNext(&DCtx, &Item);
4135 if(uError != QCBOR_SUCCESS ||
4136 Item.uDataType != QCBOR_TYPE_ARRAY ||
4137 QCBORDecode_GetNthTag(&DCtx, &Item, 3) != 10489608748473423768ULL ||
4138 QCBORDecode_GetNthTag(&DCtx, &Item, 2) != 2442302356ULL ||
4139 QCBORDecode_GetNthTag(&DCtx, &Item, 1) != 21590ULL ||
4140 QCBORDecode_GetNthTag(&DCtx, &Item, 0) != 240ULL) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004141 return -7;
Laurence Lundblade9b334962020-08-27 10:55:53 -07004142 }
4143
4144 /* tag 21590(
4145 10489608748473423768(
4146 2442302357(
4147 21591(
4148 []))))
4149 */
4150 uError = QCBORDecode_GetNext(&DCtx, &Item);
4151 if(uError != QCBOR_SUCCESS ||
4152 Item.uDataType != QCBOR_TYPE_ARRAY ||
4153 QCBORDecode_GetNthTag(&DCtx, &Item, 0) != 65534ULL ||
4154 QCBORDecode_GetNthTag(&DCtx, &Item, 1) != 2442302357ULL ||
4155 QCBORDecode_GetNthTag(&DCtx, &Item, 2) != 10489608748473423768ULL ||
4156 QCBORDecode_GetNthTag(&DCtx, &Item, 3) != 21590ULL) {
4157 return -8;
4158 }
4159
4160 /* Make sure to blow past the limit of tags that must be mapped.
4161 works in conjuntion with entries above.
4162 269488144(269488145(269488146(269488147([]))))
4163 */
4164 uError = QCBORDecode_GetNext(&DCtx, &Item);
4165 if(uError != QCBOR_ERR_TOO_MANY_TAGS) {
4166 return -9;
4167 }
4168
4169 uError = QCBORDecode_GetNext(&DCtx, &Item);
4170 if(uError == QCBOR_SUCCESS) {
4171 return -10;
4172 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004173
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004174 // ----------------------------------
Laurence Lundbladeee851742020-01-08 08:37:05 -08004175 // This test sets up a caller-config list that includes the very large
Laurence Lundblade9b334962020-08-27 10:55:53 -07004176 // tage and then matches it. Caller-config lists are no longer
4177 // used or needed. This tests backwards compatibility with them.
Laurence Lundbladeee851742020-01-08 08:37:05 -08004178 QCBORDecode_Init(&DCtx,
4179 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEncodedLargeTag),
4180 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004181 const uint64_t puList[] = {0x9192939495969798, 257};
4182 const QCBORTagListIn TL = {2, puList};
4183 QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004184
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004185 if(QCBORDecode_GetNext(&DCtx, &Item)) {
4186 return -8;
4187 }
4188 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
4189 !QCBORDecode_IsTagged(&DCtx, &Item, 0x9192939495969798) ||
4190 QCBORDecode_IsTagged(&DCtx, &Item, 257) ||
4191 QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_BIGFLOAT) ||
4192 Item.val.uCount != 0) {
4193 return -9;
4194 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004195
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004196 //------------------------
Laurence Lundbladeee851742020-01-08 08:37:05 -08004197 // Sets up a caller-configured list and look up something not in it
Laurence Lundblade9b334962020-08-27 10:55:53 -07004198 // Another backwards compatibility test.
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004199 const uint64_t puLongList[17] = {1,2,1};
4200 const QCBORTagListIn TLLong = {17, puLongList};
Laurence Lundbladeee851742020-01-08 08:37:05 -08004201 QCBORDecode_Init(&DCtx,
4202 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEncodedLargeTag),
4203 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004204 QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TLLong);
4205 if(QCBORDecode_GetNext(&DCtx, &Item)) {
4206 return -11;
4207 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004208
Laurence Lundbladef7a70bc2020-10-24 12:23:25 -07004209 uint64_t puTags[4];
Laurence Lundblade9b334962020-08-27 10:55:53 -07004210 QCBORTagListOut Out = {0, 4, puTags};
4211
4212
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004213 // This tests retrievel of the full tag list
Laurence Lundbladeee851742020-01-08 08:37:05 -08004214 QCBORDecode_Init(&DCtx,
4215 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spLotsOfTags),
4216 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004217 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
4218 return -12;
4219 }
4220 if(puTags[0] != 0x9192939495969798 ||
4221 puTags[1] != 0x88 ||
Laurence Lundblade59289e52019-12-30 13:44:37 -08004222 puTags[2] != 0x06 ||
4223 puTags[3] != 0x07) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004224 return -13;
4225 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004226
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004227 // ----------------------
Laurence Lundblade9b334962020-08-27 10:55:53 -07004228 // This tests too small of an out list
Laurence Lundbladeee851742020-01-08 08:37:05 -08004229 QCBORDecode_Init(&DCtx,
4230 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spLotsOfTags),
4231 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004232 QCBORTagListOut OutSmall = {0, 3, puTags};
4233 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &OutSmall) != QCBOR_ERR_TOO_MANY_TAGS) {
4234 return -14;
4235 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004236
Laurence Lundblade9b334962020-08-27 10:55:53 -07004237
4238
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004239 // ---------------
Laurence Lundblade9b334962020-08-27 10:55:53 -07004240 // Decode a version of the "CSR" that has had a ton of tags randomly inserted
4241 // It is a bit of a messy test and maybe could be improved, but
4242 // it is retained as a backwards compatibility check.
Laurence Lundbladeee851742020-01-08 08:37:05 -08004243 QCBORDecode_Init(&DCtx,
4244 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRWithTags),
4245 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004246 int n = CheckCSRMaps(&DCtx);
4247 if(n) {
4248 return n-2000;
4249 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004250
Laurence Lundblade59289e52019-12-30 13:44:37 -08004251 Out = (QCBORTagListOut){0, 16, puTags};
Laurence Lundbladeee851742020-01-08 08:37:05 -08004252 QCBORDecode_Init(&DCtx,
4253 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRWithTags),
4254 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004255
Laurence Lundblade9b334962020-08-27 10:55:53 -07004256 /* With the spiffy decode revision, this tag list is not used.
4257 It doesn't matter if a tag is in this list or not so some
4258 tests that couldn't process a tag because it isn't in this list
4259 now can process these unlisted tags. The tests have been
4260 adjusted for this. */
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004261 const uint64_t puTagList[] = {773, 1, 90599561};
4262 const QCBORTagListIn TagList = {3, puTagList};
4263 QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TagList);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004264
4265
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004266 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
4267 return -100;
4268 }
4269 if(Item.uDataType != QCBOR_TYPE_MAP ||
4270 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC) ||
4271 QCBORDecode_IsTagged(&DCtx, &Item, 90599561) ||
4272 QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_DATE_EPOCH) ||
4273 Item.val.uCount != 2 ||
4274 puTags[0] != CBOR_TAG_CBOR_MAGIC ||
4275 puTags[1] != CBOR_TAG_CBOR_MAGIC ||
4276 puTags[2] != CBOR_TAG_CBOR_MAGIC ||
4277 Out.uNumUsed != 3) {
4278 return -101;
4279 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004280
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004281 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
4282 return -102;
4283 }
4284 if(Item.uDataType != QCBOR_TYPE_MAP ||
4285 QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC) ||
4286 QCBORDecode_IsTagged(&DCtx, &Item, 6) ||
Laurence Lundbladef7a70bc2020-10-24 12:23:25 -07004287 !QCBORDecode_IsTagged(&DCtx, &Item, 7) ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004288 Item.val.uCount != 2 ||
4289 puTags[0] != 5859837686836516696 ||
4290 puTags[1] != 7 ||
4291 Out.uNumUsed != 2) {
4292 return -103;
4293 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004294
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004295 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
4296 return -104;
4297 }
4298 if(Item.uDataType != QCBOR_TYPE_MAP ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004299 Item.val.uCount != 5 ||
4300 puTags[0] != 0x0b ||
4301 Out.uNumUsed != 1) {
4302 return -105;
4303 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004304
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004305 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
4306 return -106;
4307 }
4308 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
4309 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_COSE_MAC0) ||
4310 Item.val.string.len != 12 ||
4311 puTags[0] != CBOR_TAG_COSE_MAC0 ||
4312 puTags[1] != CBOR_TAG_COSE_MAC0 ||
4313 puTags[2] != CBOR_TAG_COSE_MAC0 ||
4314 Out.uNumUsed != 3) {
4315 return -105;
4316 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004317
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004318 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
4319 return -107;
4320 }
4321 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
4322 !QCBORDecode_IsTagged(&DCtx, &Item, 773) ||
4323 Item.val.string.len != 3 ||
4324 puTags[0] != 773 ||
4325 Out.uNumUsed != 1) {
4326 return -108;
4327 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004328
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004329 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
4330 return -109;
4331 }
4332 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade59289e52019-12-30 13:44:37 -08004333 !QCBORDecode_IsTagged(&DCtx, &Item, 16) ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004334 Item.val.string.len != 9 ||
Laurence Lundblade59289e52019-12-30 13:44:37 -08004335 puTags[0] != 16 ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07004336 puTags[3] != 7 ||
4337 Out.uNumUsed != 4) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004338 return -110;
4339 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004340
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004341 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
4342 return -111;
4343 }
4344 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
4345 !QCBORDecode_IsTagged(&DCtx, &Item, 17) ||
4346 Item.val.string.len != 9 ||
4347 puTags[0] != 17 ||
4348 Out.uNumUsed != 1) {
4349 return -112;
4350 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004351
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004352 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
4353 return -111;
4354 }
4355 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
4356 !QCBORDecode_IsTagged(&DCtx, &Item, 17) ||
4357 Item.val.string.len != 2 ||
4358 puTags[0] != 17 ||
4359 Out.uNumUsed != 1) {
4360 return -112;
4361 }
4362
4363 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
4364 return -113;
4365 }
4366 if(Item.uDataType != QCBOR_TYPE_MAP ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07004367 !QCBORDecode_IsTagged(&DCtx, &Item, 19) ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004368 Item.val.uCount != 2 ||
4369 puTags[0] != 19 ||
4370 Out.uNumUsed != 1) {
4371 return -114;
4372 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004373
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004374 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
4375 return -115;
4376 }
4377 if(Item.uDataType != QCBOR_TYPE_MAP ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07004378 !QCBORDecode_IsTagged(&DCtx, &Item, 9) ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004379 Item.val.uCount != 1 ||
4380 puTags[0] != 9 ||
4381 Out.uNumUsed != 1) {
4382 return -116;
4383 }
4384
4385 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
4386 return -116;
4387 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004388 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004389 Item.val.int64 != -7 ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004390 Out.uNumUsed != 0) {
4391 return -117;
4392 }
4393
4394 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
4395 return -118;
4396 }
4397 if(Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
4398 Item.val.string.len != 10 ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004399 puTags[0] != 12 ||
4400 Out.uNumUsed != 1) {
4401 return -119;
4402 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004403
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004404 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
4405 return -120;
4406 }
4407 if(Item.uDataType != QCBOR_TYPE_MAP ||
4408 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_ENC_AS_B16) ||
4409 Item.val.uCount != 1 ||
4410 puTags[0] != 0x17 ||
4411 Out.uNumUsed != 1) {
4412 return -121;
4413 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004414
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004415 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
4416 return -122;
4417 }
4418 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07004419 !QCBORDecode_IsTagged(&DCtx, &Item, 8) ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004420 Item.val.int64 != -3 ||
4421 puTags[0] != 8 ||
4422 Out.uNumUsed != 1) {
4423 return -123;
4424 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004425
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004426 if(QCBORDecode_Finish(&DCtx)) {
4427 return -124;
4428 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07004429
4430 UsefulBufC DateString;
4431 QCBORDecode_Init(&DCtx,
4432 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSpiffyTagInput),
4433 QCBOR_DECODE_MODE_NORMAL);
4434
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07004435 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade9b334962020-08-27 10:55:53 -07004436 // tagged date string
4437 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString);
4438 // untagged date string
4439 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString);
4440 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_SUCCESS) {
4441 return 100;
4442 }
4443 // untagged byte string
4444 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString);
4445 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
4446 return 101;
4447 }
4448 // tagged regex
4449 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString);
4450 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
4451 return 102;
4452 }
4453 // tagged date string with a byte string
4454 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString);
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07004455 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT) {
Laurence Lundblade9b334962020-08-27 10:55:53 -07004456 return 103;
4457 }
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07004458 // The exit errors out because the last item, the date string with
4459 // bad content makes the array untraversable (the bad date string
4460 // could have tag content of an array or such that is not consumed
4461 // by the date decoding).
Laurence Lundblade9b334962020-08-27 10:55:53 -07004462 QCBORDecode_ExitArray(&DCtx);
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07004463 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT) {
Laurence Lundblade9b334962020-08-27 10:55:53 -07004464 return 104;
4465 }
4466
4467
4468 QCBORDecode_Init(&DCtx,
Laurence Lundbladecdbbc192024-06-28 15:13:04 -07004469 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRWithTags),
4470 QCBOR_DECODE_MODE_NORMAL);
4471 QCBORDecode_EnterMap(&DCtx, NULL);
4472 if(QCBORDecode_GetNthTagOfLast(&DCtx, 0) != 55799) {
4473 return 200;
4474 }
4475 if(QCBORDecode_GetNthTagOfLast(&DCtx, 1) != 55799) {
4476 return 202;
4477 }
4478 if(QCBORDecode_GetNthTagOfLast(&DCtx, 2) != 55799) {
4479 return 203;
4480 }
4481 if(QCBORDecode_GetNthTagOfLast(&DCtx, 3) != CBOR_TAG_INVALID64) {
4482 return 204;
4483 }
4484
4485 QCBORDecode_EnterMap(&DCtx, NULL);
4486 if(QCBORDecode_GetNthTagOfLast(&DCtx, 0) != 7) {
4487 return 210;
4488 }
4489 if(QCBORDecode_GetNthTagOfLast(&DCtx, 1) != 5859837686836516696) {
4490 return 212;
4491 }
4492 if(QCBORDecode_GetNthTagOfLast(&DCtx, 2) != CBOR_TAG_INVALID64) {
4493 return 213;
4494 }
4495 if(QCBORDecode_GetNthTagOfLast(&DCtx, 3) != CBOR_TAG_INVALID64) {
4496 return 214;
4497 }
4498
4499
4500 QCBORDecode_Init(&DCtx,
4501 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRWithTags),
4502 QCBOR_DECODE_MODE_NORMAL);
4503 QCBORDecode_EnterMap(&DCtx, NULL);
4504 QCBORDecode_EnterMapFromMapN(&DCtx, -23);
4505 if(QCBORDecode_GetNthTagOfLast(&DCtx, 0) != 7) {
4506 return 220;
4507 }
4508 if(QCBORDecode_GetNthTagOfLast(&DCtx, 1) != 5859837686836516696) {
4509 return 221;
4510 }
4511 if(QCBORDecode_GetNthTagOfLast(&DCtx, 2) != CBOR_TAG_INVALID64) {
4512 return 222;
4513 }
4514
4515#ifndef QCBOR_DISABLE_NON_INTEGER_LABELS
4516 QCBORDecode_Init(&DCtx,
4517 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRWithTags),
4518 QCBOR_DECODE_MODE_MAP_AS_ARRAY);
4519 QCBORDecode_EnterArray(&DCtx, NULL);
4520 if(QCBORDecode_GetNthTagOfLast(&DCtx, 0) != 55799) {
4521 return 230;
4522 }
4523 if(QCBORDecode_GetNthTagOfLast(&DCtx, 1) != 55799) {
4524 return 231;
4525 }
4526 if(QCBORDecode_GetNthTagOfLast(&DCtx, 2) != 55799) {
4527 return 232;
4528 }
4529 if(QCBORDecode_GetNthTagOfLast(&DCtx, 3) != CBOR_TAG_INVALID64) {
4530 return 234;
4531 }
4532 int64_t nInt;
4533 QCBORDecode_GetInt64(&DCtx, &nInt);
4534 if(QCBORDecode_GetNthTagOfLast(&DCtx, 0) != 7) {
4535 return 240;
4536 }
4537 if(QCBORDecode_GetNthTagOfLast(&DCtx, 1) != 6) {
4538 return 241;
4539 }
4540 if(QCBORDecode_GetNthTagOfLast(&DCtx, 2) != CBOR_TAG_INVALID64) {
4541 return 242;
4542 }
4543 if(QCBORDecode_GetNthTagOfLast(&DCtx, 3) != CBOR_TAG_INVALID64) {
4544 return 243;
4545 }
4546#endif /* ! QCBOR_DISABLE_NON_INTEGER_LABELS */
4547
4548 QCBORDecode_Init(&DCtx,
Laurence Lundblade9b334962020-08-27 10:55:53 -07004549 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSpiffyTagInput),
4550 QCBOR_DECODE_MODE_NORMAL);
4551
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07004552 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade9b334962020-08-27 10:55:53 -07004553 // tagged date string
4554 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString);
4555 // untagged date string
4556 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString);
4557 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_SUCCESS) {
Laurence Lundbladecdbbc192024-06-28 15:13:04 -07004558 return 250;
Laurence Lundblade9b334962020-08-27 10:55:53 -07004559 }
4560 // untagged byte string
4561 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString);
4562 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
Laurence Lundbladecdbbc192024-06-28 15:13:04 -07004563 return 251;
Laurence Lundblade9b334962020-08-27 10:55:53 -07004564 }
4565 // tagged regex
4566 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString);
4567 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
Laurence Lundbladecdbbc192024-06-28 15:13:04 -07004568 return 252;
Laurence Lundblade9b334962020-08-27 10:55:53 -07004569 }
4570 // tagged date string with a byte string
4571 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString);
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07004572 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT) {
Laurence Lundbladecdbbc192024-06-28 15:13:04 -07004573 return 253;
Laurence Lundblade9b334962020-08-27 10:55:53 -07004574 }
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07004575 // See comments above
Laurence Lundblade9b334962020-08-27 10:55:53 -07004576 QCBORDecode_ExitArray(&DCtx);
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07004577 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT) {
Laurence Lundbladecdbbc192024-06-28 15:13:04 -07004578 return 254;
Laurence Lundblade9b334962020-08-27 10:55:53 -07004579 }
4580
4581 QCBORDecode_Init(&DCtx,
4582 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSpiffyTagInput),
4583 QCBOR_DECODE_MODE_NORMAL);
4584
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07004585 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade9b334962020-08-27 10:55:53 -07004586 // tagged date string
4587 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString);
4588 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
4589 return 300;
4590 }
4591 // untagged date string
4592 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString);
4593 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
4594 return 301;
4595 }
4596 // untagged byte string
4597 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString);
4598 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
4599 return 302;
4600 }
4601 // tagged regex
4602 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString);
4603 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
4604 return 303;
4605 }
4606 // tagged date string with a byte string
4607 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString);
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07004608 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT) {
Laurence Lundblade9b334962020-08-27 10:55:53 -07004609 return 304;
4610 }
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07004611 // See comments above
Laurence Lundblade9b334962020-08-27 10:55:53 -07004612 QCBORDecode_ExitArray(&DCtx);
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07004613 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT) {
Laurence Lundblade9b334962020-08-27 10:55:53 -07004614 return 305;
4615 }
4616
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004617 return 0;
4618}
4619
Laurence Lundblade37286c02022-09-03 10:05:02 -07004620/*
4621 * These are showing the big numbers converted to integers.
4622 * The tag numbers are not shown.
4623 *
Laurence Lundbladeec290b82024-06-10 11:10:54 -07004624 * [
4625 * 18446744073709551616,
4626 * -18446744073709551617,
4627 * {
4628 * -64: -18446744073709551617,
4629 * 64: 18446744073709551616,
4630 * "BN+": 18446744073709551616,
4631 * "BN-": -18446744073709551617
Laurence Lundblade37286c02022-09-03 10:05:02 -07004632 * }
4633 * ]
4634 */
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004635
Laurence Lundbladecc7da412020-12-27 00:09:07 -08004636static const uint8_t spBigNumInput[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004637 0x83,
4638 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
4639 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
4640 0xA4,
Laurence Lundbladeec290b82024-06-10 11:10:54 -07004641 0x38, 0x3F,
4642 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004643 0x18, 0x40,
4644 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
Laurence Lundbladeec290b82024-06-10 11:10:54 -07004645 0x63, 0x42, 0x4E, 0x2B,
4646 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
4647 0x63, 0x42, 0x4E, 0x2D,
4648 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
4649};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004650
Laurence Lundblade37286c02022-09-03 10:05:02 -07004651#ifndef QCBOR_DISABLE_TAGS
Laurence Lundbladecc7da412020-12-27 00:09:07 -08004652/* The expected big num */
4653static const uint8_t spBigNum[] = {
4654 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
4655 0x00};
Laurence Lundblade37286c02022-09-03 10:05:02 -07004656#endif /* QCBOR_DISABLE_TAGS */
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004657
4658
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03004659int32_t BignumParseTest(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004660{
4661 QCBORDecodeContext DCtx;
4662 QCBORItem Item;
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07004663 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004664
Laurence Lundbladeee851742020-01-08 08:37:05 -08004665 QCBORDecode_Init(&DCtx,
4666 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNumInput),
4667 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004668
4669
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004670 //
4671 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
4672 return -1;
4673 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade830fbf92020-05-31 17:22:33 -07004674 return -2;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004675 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004676
Laurence Lundblade37286c02022-09-03 10:05:02 -07004677#ifndef QCBOR_DISABLE_TAGS
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004678 //
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004679 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07004680 return -3;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004681 if(Item.uDataType != QCBOR_TYPE_POSBIGNUM ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07004682 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07004683 return -4;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004684 }
4685
4686 //
4687 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07004688 return -5;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004689 if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07004690 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07004691 return -6;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004692 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004693
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004694 //
4695 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07004696 return -7;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004697 if(Item.uDataType != QCBOR_TYPE_MAP) {
Laurence Lundblade830fbf92020-05-31 17:22:33 -07004698 return -8;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004699 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004700
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004701 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundbladeec290b82024-06-10 11:10:54 -07004702 return -15;
4703 if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM ||
4704 Item.uLabelType != QCBOR_TYPE_INT64 ||
4705 Item.label.int64 != -64 ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07004706 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundbladeec290b82024-06-10 11:10:54 -07004707 return -16;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004708 }
4709
4710 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07004711 return -11;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004712 if(Item.uDataType != QCBOR_TYPE_POSBIGNUM ||
4713 Item.uLabelType != QCBOR_TYPE_INT64 ||
4714 Item.label.int64 != 64 ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07004715 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07004716 return -12;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004717 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004718
Laurence Lundbladeec290b82024-06-10 11:10:54 -07004719#ifndef QCBOR_DISABLE_NON_INTEGER_LABELS
4720 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
4721 return -9;
4722 if(Item.uDataType != QCBOR_TYPE_POSBIGNUM ||
4723 Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
4724 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
4725 return -10;
4726 }
4727
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004728 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07004729 return -13;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004730 if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM ||
4731 Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07004732 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07004733 return -14;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004734 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004735
Laurence Lundbladeec290b82024-06-10 11:10:54 -07004736
4737#endif /* ! QCBOR_DISABLE_NON_INTEGER_LABELS */
4738
Laurence Lundblade37286c02022-09-03 10:05:02 -07004739#else
4740
4741 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_TAGS_DISABLED) {
4742 return -100;
4743 }
4744#endif /* QCBOR_DISABLE_TAGS */
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004745
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004746 return 0;
4747}
4748
4749
Laurence Lundbladec5fef682020-01-25 11:38:45 -08004750static int32_t CheckItemWithIntLabel(QCBORDecodeContext *pCtx,
Laurence Lundbladeee851742020-01-08 08:37:05 -08004751 uint8_t uDataType,
4752 uint8_t uNestingLevel,
4753 uint8_t uNextNest,
4754 int64_t nLabel,
4755 QCBORItem *pItem)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004756{
4757 QCBORItem Item;
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07004758 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004759
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004760 if((nCBORError = QCBORDecode_GetNext(pCtx, &Item))) return -1;
4761 if(Item.uDataType != uDataType) return -1;
4762 if(uNestingLevel > 0) {
Laurence Lundbladeec290b82024-06-10 11:10:54 -07004763 if(Item.uLabelType != QCBOR_TYPE_INT64) {
Laurence Lundbladeee851742020-01-08 08:37:05 -08004764 return -1;
4765 }
Laurence Lundbladeec290b82024-06-10 11:10:54 -07004766 if(Item.label.int64 != nLabel) {
4767 return -1;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004768 }
Laurence Lundbladeec290b82024-06-10 11:10:54 -07004769
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004770 }
4771 if(Item.uNestingLevel != uNestingLevel) return -1;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304772 if(Item.uNextNestLevel != uNextNest) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004773
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004774 if(pItem) {
4775 *pItem = Item;
4776 }
4777 return 0;
4778}
4779
Laurence Lundblade742df4a2018-10-13 20:07:17 +08004780// Same code checks definite and indefinite length versions of the map
Laurence Lundbladec5fef682020-01-25 11:38:45 -08004781static int32_t CheckCSRMaps(QCBORDecodeContext *pDC)
Laurence Lundblade742df4a2018-10-13 20:07:17 +08004782{
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304783 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 0, 1, 0, NULL)) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004784
Laurence Lundblade9b334962020-08-27 10:55:53 -07004785 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 1, 2, -23, NULL)) return -2;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004786
Laurence Lundblade9b334962020-08-27 10:55:53 -07004787 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 2, 3, -20, NULL)) return -3;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004788
Laurence Lundblade9b334962020-08-27 10:55:53 -07004789 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -18, NULL)) return -4;
4790 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -17, NULL)) return -5;
4791 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -15, NULL)) return -6;
4792 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -16, NULL)) return -7;
4793 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 2, -14, NULL)) return -8;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004794
Laurence Lundblade9b334962020-08-27 10:55:53 -07004795 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 2, 3, -19, NULL)) return -9;
4796 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 3, 4, -11, NULL)) return -10;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004797
Laurence Lundblade9b334962020-08-27 10:55:53 -07004798 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_INT64, 4, 3, -9, NULL)) return -11;
4799 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_BYTE_STRING, 3, 1, -10, NULL)) return -12;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004800
Laurence Lundblade9b334962020-08-27 10:55:53 -07004801 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 1, 2, -22, NULL)) return -13;
4802 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_INT64, 2, 0, -5, NULL)) return -14;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004803
Laurence Lundblade9b334962020-08-27 10:55:53 -07004804 if(QCBORDecode_Finish(pDC)) return -20;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004805
Laurence Lundblade742df4a2018-10-13 20:07:17 +08004806 return 0;
4807}
4808
4809
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004810/*
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004811{
4812 -23: {
4813 -20: {
4814 -18: "Organization",
4815 -17: "SSG",
4816 -15: "Confusion",
4817 -16: "San Diego",
4818 -14: "US"
4819 },
4820 -19: {
4821 -11: {
4822 -9: -7
4823 },
4824 -10: '\u0001\u0002\u0003\u0004\u0005\u0006\a\b\t\n'
4825 }
4826 },
4827 -22: {
4828 -5: -3
4829 }
4830}
Laurence Lundbladecc7da412020-12-27 00:09:07 -08004831*/
4832static const uint8_t spCSRInput[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004833 0xa2, 0x36, 0xa2, 0x33, 0xa5, 0x31, 0x6c, 0x4f,
4834 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74,
4835 0x69, 0x6f, 0x6e, 0x30, 0x63, 0x53, 0x53, 0x47,
4836 0x2e, 0x69, 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73,
4837 0x69, 0x6f, 0x6e, 0x2f, 0x69, 0x53, 0x61, 0x6e,
4838 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f, 0x2d, 0x62,
4839 0x55, 0x53, 0x32, 0xa2, 0x2a, 0xa1, 0x28, 0x26,
4840 0x29, 0x4a, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
4841 0x07, 0x08, 0x09, 0x0a, 0x35, 0xa1, 0x24, 0x22};
4842
Laurence Lundbladecc7da412020-12-27 00:09:07 -08004843// Same map as above, but using indefinite lengths
4844static const uint8_t spCSRInputIndefLen[] = {
4845 0xbf, 0x36, 0xbf, 0x33, 0xbf, 0x31, 0x6c, 0x4f,
4846 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74,
4847 0x69, 0x6f, 0x6e, 0x30, 0x63, 0x53, 0x53, 0x47,
4848 0x2e, 0x69, 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73,
4849 0x69, 0x6f, 0x6e, 0x2f, 0x69, 0x53, 0x61, 0x6e,
4850 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f, 0x2d, 0x62,
4851 0x55, 0x53, 0xff, 0x32, 0xbf, 0x2a, 0xbf, 0x28,
4852 0x26, 0xff, 0x29, 0x4a, 0x01, 0x02, 0x03, 0x04,
4853 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0xff, 0xff,
4854 0x35, 0xbf, 0x24, 0x22, 0xff, 0xff};
4855
4856
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03004857int32_t NestedMapTest(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004858{
4859 QCBORDecodeContext DCtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004860
Laurence Lundbladeee851742020-01-08 08:37:05 -08004861 QCBORDecode_Init(&DCtx,
4862 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput),
4863 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004864
Laurence Lundblade742df4a2018-10-13 20:07:17 +08004865 return CheckCSRMaps(&DCtx);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004866}
4867
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08004868
4869
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03004870int32_t StringDecoderModeFailTest(void)
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08004871{
4872 QCBORDecodeContext DCtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004873
Laurence Lundbladeee851742020-01-08 08:37:05 -08004874 QCBORDecode_Init(&DCtx,
4875 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput),
4876 QCBOR_DECODE_MODE_MAP_STRINGS_ONLY);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004877
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08004878 QCBORItem Item;
4879 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004880
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08004881 if(QCBORDecode_GetNext(&DCtx, &Item)) {
4882 return -1;
4883 }
4884 if(Item.uDataType != QCBOR_TYPE_MAP) {
4885 return -2;
4886 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004887
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08004888 nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
4889 if(nCBORError != QCBOR_ERR_MAP_LABEL_TYPE) {
4890 return -3;
4891 }
4892
4893 return 0;
4894}
4895
4896
Laurence Lundblade742df4a2018-10-13 20:07:17 +08004897
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03004898int32_t NestedMapTestIndefLen(void)
Laurence Lundblade742df4a2018-10-13 20:07:17 +08004899{
4900 QCBORDecodeContext DCtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004901
Laurence Lundbladeee851742020-01-08 08:37:05 -08004902 QCBORDecode_Init(&DCtx,
4903 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInputIndefLen),
4904 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004905
Laurence Lundblade742df4a2018-10-13 20:07:17 +08004906 return CheckCSRMaps(&DCtx);
4907}
4908
4909
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004910
Laurence Lundblade17ede402018-10-13 11:43:07 +08004911static UsefulBufC make_nested_indefinite_arrays(int n, UsefulBuf Storage)
4912{
4913 UsefulOutBuf UOB;
4914 UsefulOutBuf_Init(&UOB, Storage);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004915
Laurence Lundblade17ede402018-10-13 11:43:07 +08004916 int i;
4917 for(i = 0; i < n; i++) {
4918 UsefulOutBuf_AppendByte(&UOB, 0x9f);
4919 }
4920
4921 for(i = 0; i < n; i++) {
4922 UsefulOutBuf_AppendByte(&UOB, 0xff);
4923 }
4924 return UsefulOutBuf_OutUBuf(&UOB);
4925}
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08004926
4927
Laurence Lundbladec5fef682020-01-25 11:38:45 -08004928static int32_t parse_indeflen_nested(UsefulBufC Nested, int nNestLevel)
Laurence Lundblade17ede402018-10-13 11:43:07 +08004929{
4930 QCBORDecodeContext DC;
4931 QCBORDecode_Init(&DC, Nested, 0);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004932
Laurence Lundblade17ede402018-10-13 11:43:07 +08004933 int j;
4934 for(j = 0; j < nNestLevel; j++) {
4935 QCBORItem Item;
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07004936 QCBORError nReturn = QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade17ede402018-10-13 11:43:07 +08004937 if(j >= QCBOR_MAX_ARRAY_NESTING) {
4938 // Should be in error
Laurence Lundbladea9489f82020-09-12 13:50:56 -07004939 if(nReturn != QCBOR_ERR_ARRAY_DECODE_NESTING_TOO_DEEP) {
Laurence Lundblade17ede402018-10-13 11:43:07 +08004940 return -4;
4941 } else {
4942 return 0; // Decoding doesn't recover after an error
4943 }
4944 } else {
4945 // Should be no error
4946 if(nReturn) {
4947 return -9; // Should not have got an error
4948 }
4949 }
4950 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
4951 return -7;
4952 }
4953 }
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07004954 QCBORError nReturn = QCBORDecode_Finish(&DC);
Laurence Lundblade17ede402018-10-13 11:43:07 +08004955 if(nReturn) {
4956 return -3;
4957 }
4958 return 0;
4959}
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08004960
4961
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03004962int32_t IndefiniteLengthNestTest(void)
Laurence Lundblade17ede402018-10-13 11:43:07 +08004963{
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05304964 UsefulBuf_MAKE_STACK_UB(Storage, 50);
Laurence Lundblade17ede402018-10-13 11:43:07 +08004965 int i;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004966 for(i=1; i < QCBOR_MAX_ARRAY_NESTING+4; i++) {
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08004967 const UsefulBufC Nested = make_nested_indefinite_arrays(i, Storage);
Laurence Lundblade17ede402018-10-13 11:43:07 +08004968 int nReturn = parse_indeflen_nested(Nested, i);
4969 if(nReturn) {
4970 return nReturn;
4971 }
4972 }
4973 return 0;
4974}
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08004975
Laurence Lundbladeee851742020-01-08 08:37:05 -08004976// [1, [2, 3]]
4977static const uint8_t spIndefiniteArray[] = {0x9f, 0x01, 0x82, 0x02, 0x03, 0xff};
4978// No closing break
4979static const uint8_t spIndefiniteArrayBad1[] = {0x9f};
4980// Not enough closing breaks
4981static const uint8_t spIndefiniteArrayBad2[] = {0x9f, 0x9f, 0x02, 0xff};
4982// Too many closing breaks
4983static const uint8_t spIndefiniteArrayBad3[] = {0x9f, 0x02, 0xff, 0xff};
4984// Unclosed indeflen inside def len
4985static const uint8_t spIndefiniteArrayBad4[] = {0x81, 0x9f};
4986// confused tag
4987static const uint8_t spIndefiniteArrayBad5[] = {0x9f, 0xd1, 0xff};
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08004988
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03004989int32_t IndefiniteLengthArrayMapTest(void)
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08004990{
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07004991 QCBORError nResult;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08004992 // --- first test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07004993 UsefulBufC IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArray);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004994
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08004995 // Decode it and see if it is OK
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08004996 QCBORDecodeContext DC;
4997 QCBORItem Item;
4998 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004999
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08005000 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05305001
5002 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
5003 Item.uNestingLevel != 0 ||
5004 Item.uNextNestLevel != 1) {
5005 return -111;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08005006 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005007
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08005008 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05305009 if(Item.uDataType != QCBOR_TYPE_INT64 ||
5010 Item.uNestingLevel != 1 ||
5011 Item.uNextNestLevel != 1) {
5012 return -2;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08005013 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005014
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08005015 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05305016 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
5017 Item.uNestingLevel != 1 ||
5018 Item.uNextNestLevel != 2) {
5019 return -3;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08005020 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005021
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08005022 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade12b495d2018-12-17 11:15:54 -08005023 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade6de37062018-10-15 12:22:42 +05305024 Item.uNestingLevel != 2 ||
5025 Item.uNextNestLevel != 2) {
5026 return -4;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08005027 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005028
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08005029 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade12b495d2018-12-17 11:15:54 -08005030 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade6de37062018-10-15 12:22:42 +05305031 Item.uNestingLevel != 2 ||
5032 Item.uNextNestLevel != 0) {
5033 return -5;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08005034 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005035
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08005036 if(QCBORDecode_Finish(&DC)) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05305037 return -6;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08005038 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005039
Laurence Lundblade19e0c802018-10-13 12:19:55 +08005040 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07005041 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad1);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005042
Laurence Lundblade19e0c802018-10-13 12:19:55 +08005043 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005044
Laurence Lundblade19e0c802018-10-13 12:19:55 +08005045 nResult = QCBORDecode_GetNext(&DC, &Item);
5046 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05305047 return -7;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08005048 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005049
Laurence Lundblade570fab52018-10-13 18:28:27 +08005050 nResult = QCBORDecode_Finish(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07005051 if(nResult != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05305052 return -8;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08005053 }
5054
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005055
Laurence Lundblade19e0c802018-10-13 12:19:55 +08005056 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07005057 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad2);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005058
Laurence Lundblade19e0c802018-10-13 12:19:55 +08005059 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005060
Laurence Lundblade19e0c802018-10-13 12:19:55 +08005061 nResult = QCBORDecode_GetNext(&DC, &Item);
5062 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05305063 return -9;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08005064 }
5065
5066 nResult = QCBORDecode_GetNext(&DC, &Item);
5067 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05305068 return -10;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08005069 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005070
Laurence Lundblade19e0c802018-10-13 12:19:55 +08005071 nResult = QCBORDecode_GetNext(&DC, &Item);
5072 if(nResult || Item.uDataType != QCBOR_TYPE_INT64) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05305073 return -11;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08005074 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005075
Laurence Lundblade19e0c802018-10-13 12:19:55 +08005076 nResult = QCBORDecode_Finish(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07005077 if(nResult != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05305078 return -12;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08005079 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005080
5081
Laurence Lundblade19e0c802018-10-13 12:19:55 +08005082 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07005083 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad3);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005084
Laurence Lundblade19e0c802018-10-13 12:19:55 +08005085 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005086
Laurence Lundblade19e0c802018-10-13 12:19:55 +08005087 nResult = QCBORDecode_GetNext(&DC, &Item);
5088 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05305089 return -13;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08005090 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005091
Laurence Lundblade19e0c802018-10-13 12:19:55 +08005092 nResult = QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade642282a2020-06-23 12:00:33 -07005093 if(nResult != QCBOR_SUCCESS) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05305094 return -14;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08005095 }
Laurence Lundblade6de37062018-10-15 12:22:42 +05305096
Laurence Lundblade642282a2020-06-23 12:00:33 -07005097 nResult = QCBORDecode_GetNext(&DC, &Item);
5098 if(nResult != QCBOR_ERR_BAD_BREAK) {
5099 return -140;
5100 }
5101
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005102
Laurence Lundblade570fab52018-10-13 18:28:27 +08005103 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07005104 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad4);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005105
Laurence Lundblade570fab52018-10-13 18:28:27 +08005106 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005107
Laurence Lundblade570fab52018-10-13 18:28:27 +08005108 nResult = QCBORDecode_GetNext(&DC, &Item);
5109 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05305110 return -15;
Laurence Lundblade570fab52018-10-13 18:28:27 +08005111 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005112
Laurence Lundblade570fab52018-10-13 18:28:27 +08005113 nResult = QCBORDecode_GetNext(&DC, &Item);
5114 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05305115 return -16;
Laurence Lundblade570fab52018-10-13 18:28:27 +08005116 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005117
Laurence Lundblade570fab52018-10-13 18:28:27 +08005118 nResult = QCBORDecode_Finish(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07005119 if(nResult != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05305120 return -17;
Laurence Lundblade570fab52018-10-13 18:28:27 +08005121 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005122
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05305123 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07005124 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad5);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005125
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05305126 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005127
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05305128 nResult = QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade37286c02022-09-03 10:05:02 -07005129
5130#ifndef QCBOR_DISABLE_TAGS
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05305131 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05305132 return -18;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05305133 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005134
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05305135 nResult = QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05305136 if(nResult != QCBOR_ERR_BAD_BREAK) {
5137 return -19;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05305138 }
Laurence Lundblade37286c02022-09-03 10:05:02 -07005139#else /* QCBOR_DISABLE_TAGS */
5140 if(nResult != QCBOR_ERR_TAGS_DISABLED) {
5141 return -20;
5142 }
5143#endif /* QCBOR_DISABLE_TAGS */
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005144
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08005145 return 0;
5146}
5147
Laurence Lundblade17ede402018-10-13 11:43:07 +08005148
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08005149#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
5150
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07005151static const uint8_t spIndefiniteLenString[] = {
Laurence Lundblade17ede402018-10-13 11:43:07 +08005152 0x81, // Array of length one
5153 0x7f, // text string marked with indefinite length
5154 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment
5155 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment
5156 0xff // ending break
5157};
5158
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07005159static const uint8_t spIndefiniteLenStringBad2[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305160 0x81, // Array of length one
5161 0x7f, // text string marked with indefinite length
5162 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment
5163 0x44, 0x6d, 0x69, 0x6e, 0x67, // second segment of wrong type
5164 0xff // ending break
5165};
5166
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07005167static const uint8_t spIndefiniteLenStringBad3[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305168 0x81, // Array of length one
5169 0x7f, // text string marked with indefinite length
5170 0x01, 0x02, // Not a string
5171 0xff // ending break
5172};
5173
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07005174static const uint8_t spIndefiniteLenStringBad4[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305175 0x81, // Array of length one
5176 0x7f, // text string marked with indefinite length
5177 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment
5178 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment
5179 // missing end of string
5180};
5181
Laurence Lundbladeec290b82024-06-10 11:10:54 -07005182#ifndef QCBOR_DISABLE_NON_INTEGER_LABELS
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07005183static const uint8_t spIndefiniteLenStringLabel[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305184 0xa1, // Array of length one
5185 0x7f, // text string marked with indefinite length
5186 0x65, 0x73, 0x74, 0x72, 0x75, 0x75, // first segment
5187 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment
5188 0xff, // ending break
5189 0x01 // integer being labeled.
5190};
Laurence Lundbladeec290b82024-06-10 11:10:54 -07005191#endif /* ! QCBOR_DISABLE_NON_INTEGER_LABELS */
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305192
Laurence Lundbladec5fef682020-01-25 11:38:45 -08005193/**
5194 Make an indefinite length string
5195
5196 @param Storage Storage for string, must be 144 bytes in size
5197 @return The indefinite length string
5198
5199 This makes an array with one indefinite length string that has 7 chunks
5200 from size of 1 byte up to 64 bytes.
5201 */
5202static UsefulBufC MakeIndefiniteBigBstr(UsefulBuf Storage)
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305203{
5204 UsefulOutBuf UOB;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005205
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305206 UsefulOutBuf_Init(&UOB, Storage);
5207 UsefulOutBuf_AppendByte(&UOB, 0x81);
5208 UsefulOutBuf_AppendByte(&UOB, 0x5f);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005209
Laurence Lundbladec5fef682020-01-25 11:38:45 -08005210 uint8_t uStringByte = 0;
5211 // Use of type int is intentional
5212 for(int uChunkSize = 1; uChunkSize <= 128; uChunkSize *= 2) {
5213 // Not using preferred encoding here, but that is OK.
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305214 UsefulOutBuf_AppendByte(&UOB, 0x58);
Laurence Lundbladec5fef682020-01-25 11:38:45 -08005215 UsefulOutBuf_AppendByte(&UOB, (uint8_t)uChunkSize);
5216 for(int j = 0; j < uChunkSize; j++) {
5217 UsefulOutBuf_AppendByte(&UOB, uStringByte);
5218 uStringByte++;
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305219 }
5220 }
5221 UsefulOutBuf_AppendByte(&UOB, 0xff);
5222
5223 return UsefulOutBuf_OutUBuf(&UOB);
5224}
5225
5226static int CheckBigString(UsefulBufC BigString)
5227{
5228 if(BigString.len != 255) {
5229 return 1;
5230 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005231
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305232 for(uint8_t i = 0; i < 255; i++){
5233 if(((const uint8_t *)BigString.ptr)[i] != i) {
5234 return 1;
5235 }
5236 }
5237 return 0;
5238}
5239
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05305240
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03005241int32_t IndefiniteLengthStringTest(void)
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05305242{
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305243 QCBORDecodeContext DC;
5244 QCBORItem Item;
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05305245 // big enough for MakeIndefiniteBigBstr() + MemPool overhead
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08005246 UsefulBuf_MAKE_STACK_UB(MemPool, 350);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005247
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305248 // --- Simple normal indefinite length string ------
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07005249 UsefulBufC IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenString);
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305250 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005251
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05305252 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305253 return -1;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05305254 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005255
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305256 if(QCBORDecode_GetNext(&DC, &Item)) {
5257 return -2;
5258 }
5259 if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.uDataAlloc) {
5260 return -3;
5261 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005262
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305263 if(QCBORDecode_GetNext(&DC, &Item)) {
5264 return -4;
5265 }
5266 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || !Item.uDataAlloc) {
5267 return -5;
5268 }
5269 if(QCBORDecode_Finish(&DC)) {
5270 return -6;
5271 }
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05305272
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305273 // ----- types mismatch ---
Laurence Lundbladeee851742020-01-08 08:37:05 -08005274 QCBORDecode_Init(&DC,
5275 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad2),
5276 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005277
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305278 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
5279 return -7;
5280 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005281
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305282 if(QCBORDecode_GetNext(&DC, &Item)) {
5283 return -8;
5284 }
5285 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
5286 return -9;
5287 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005288
Laurence Lundblade30816f22018-11-10 13:40:22 +07005289 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_INDEFINITE_STRING_CHUNK) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305290 return -10;
5291 }
5292
5293 // ----- not a string ---
Laurence Lundbladeee851742020-01-08 08:37:05 -08005294 QCBORDecode_Init(&DC,
5295 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad3),
5296 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005297
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305298 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
5299 return -11;
5300 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005301
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305302 if(QCBORDecode_GetNext(&DC, &Item)) {
5303 return -12;
5304 }
5305 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
5306 return -13;
5307 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005308
Laurence Lundblade30816f22018-11-10 13:40:22 +07005309 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_INDEFINITE_STRING_CHUNK) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305310 return -14;
5311 }
5312
5313 // ----- no end -----
Laurence Lundbladeee851742020-01-08 08:37:05 -08005314 QCBORDecode_Init(&DC,
5315 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad4),
5316 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005317
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305318 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
5319 return -15;
5320 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005321
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305322 if(QCBORDecode_GetNext(&DC, &Item)) {
5323 return -16;
5324 }
5325 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
5326 return -17;
5327 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005328
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305329 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_HIT_END) {
5330 return -18;
5331 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005332
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305333 // ------ Don't set a string allocator and see an error -----
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05305334 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005335
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05305336 QCBORDecode_GetNext(&DC, &Item);
5337 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305338 return -19;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05305339 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005340
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05305341 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_NO_STRING_ALLOCATOR) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305342 return -20;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05305343 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005344
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05305345 // ----- Mempool is way too small -----
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08005346 UsefulBuf_MAKE_STACK_UB(MemPoolTooSmall, QCBOR_DECODE_MIN_MEM_POOL_SIZE-1);
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05305347
5348 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
5349 if(!QCBORDecode_SetMemPool(&DC, MemPoolTooSmall, false)) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305350 return -21;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05305351 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005352
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05305353 // ----- Mempool is way too small -----
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05305354 UsefulBuf_MAKE_STACK_UB(BigIndefBStrStorage, 290);
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08005355 const UsefulBufC BigIndefBStr = MakeIndefiniteBigBstr(BigIndefBStrStorage);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005356
Laurence Lundbladeee851742020-01-08 08:37:05 -08005357 // 80 is big enough for MemPool overhead, but not BigIndefBStr
5358 UsefulBuf_MAKE_STACK_UB(MemPoolSmall, 80);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005359
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305360 QCBORDecode_Init(&DC, BigIndefBStr, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05305361 if(QCBORDecode_SetMemPool(&DC, MemPoolSmall, false)) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305362 return -22;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05305363 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005364
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05305365 QCBORDecode_GetNext(&DC, &Item);
5366 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305367 return -23;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05305368 }
Laurence Lundblade30816f22018-11-10 13:40:22 +07005369 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_STRING_ALLOCATE) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305370 return -24;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05305371 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005372
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305373 // ---- big bstr -----
5374 QCBORDecode_Init(&DC, BigIndefBStr, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005375
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305376 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
5377 return -25;
5378 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005379
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05305380 if(QCBORDecode_GetNext(&DC, &Item)) {
5381 return -26;
5382 }
5383 if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.uDataAlloc) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305384 return -26;
5385 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005386
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305387 if(QCBORDecode_GetNext(&DC, &Item)) {
5388 return -27;
5389 }
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05305390 if(Item.uDataType != QCBOR_TYPE_BYTE_STRING || !Item.uDataAlloc || Item.uNestingLevel != 1) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305391 return -28;
5392 }
5393 if(CheckBigString(Item.val.string)) {
5394 return -3;
5395 }
5396 if(QCBORDecode_Finish(&DC)) {
5397 return -29;
5398 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005399
Laurence Lundbladeec290b82024-06-10 11:10:54 -07005400#ifndef QCBOR_DISABLE_NON_INTEGER_LABELS
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305401 // --- label is an indefinite length string ------
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07005402 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringLabel), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005403
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305404 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
5405 return -30;
5406 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005407
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305408 QCBORDecode_GetNext(&DC, &Item);
5409 if(Item.uDataType != QCBOR_TYPE_MAP) {
5410 return -31;
5411 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005412
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305413 if(QCBORDecode_GetNext(&DC, &Item)){
5414 return -32;
5415 }
Laurence Lundbladeee851742020-01-08 08:37:05 -08005416 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
5417 Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305418 Item.uDataAlloc || !Item.uLabelAlloc ||
5419 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("struuming"))) {
5420 return -33;
5421 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005422
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305423 if(QCBORDecode_Finish(&DC)) {
5424 return -34;
5425 }
Laurence Lundbladeec290b82024-06-10 11:10:54 -07005426#endif /* ! QCBOR_DISABLE_NON_INTEGER_LABELS */
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005427
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08005428 return 0;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08005429}
5430
5431
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03005432int32_t AllocAllStringsTest(void)
Laurence Lundbladea44d5062018-10-17 18:45:12 +05305433{
5434 QCBORDecodeContext DC;
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08005435 QCBORError nCBORError;
5436
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005437
Laurence Lundbladea44d5062018-10-17 18:45:12 +05305438 // First test, use the "CSRMap" as easy input and checking
Laurence Lundbladeee851742020-01-08 08:37:05 -08005439 QCBORDecode_Init(&DC,
5440 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput),
5441 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005442
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08005443 UsefulBuf_MAKE_STACK_UB(Pool, sizeof(spCSRInput) + QCBOR_DECODE_MIN_MEM_POOL_SIZE);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005444
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08005445 nCBORError = QCBORDecode_SetMemPool(&DC, Pool, 1); // Turn on copying.
5446 if(nCBORError) {
Laurence Lundbladea44d5062018-10-17 18:45:12 +05305447 return -1;
5448 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005449
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08005450 if(CheckCSRMaps(&DC)) {
5451 return -2;
5452 }
5453
Laurence Lundbladeec290b82024-06-10 11:10:54 -07005454#ifndef QCBOR_DISABLE_NON_INTEGER_LABELS
Laurence Lundblade2f467f92020-10-09 17:50:11 -07005455 // Next parse, save pointers to a few strings, destroy original and
5456 // see all is OK.
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08005457 UsefulBuf_MAKE_STACK_UB(CopyOfStorage, sizeof(pValidMapEncoded) + QCBOR_DECODE_MIN_MEM_POOL_SIZE);
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08005458 const UsefulBufC CopyOf = UsefulBuf_Copy(CopyOfStorage, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded));
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08005459
Laurence Lundbladea44d5062018-10-17 18:45:12 +05305460 QCBORDecode_Init(&DC, CopyOf, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08005461 UsefulBuf_Set(Pool, '/');
Laurence Lundbladea44d5062018-10-17 18:45:12 +05305462 QCBORDecode_SetMemPool(&DC, Pool, 1); // Turn on copying.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005463
Laurence Lundbladea44d5062018-10-17 18:45:12 +05305464 QCBORItem Item1, Item2, Item3, Item4;
5465 if((nCBORError = QCBORDecode_GetNext(&DC, &Item1)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07005466 return (int32_t)nCBORError;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05305467 if(Item1.uDataType != QCBOR_TYPE_MAP ||
5468 Item1.val.uCount != 3)
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08005469 return -3;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05305470 if((nCBORError = QCBORDecode_GetNext(&DC, &Item1)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07005471 return (int32_t)nCBORError;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05305472 if((nCBORError = QCBORDecode_GetNext(&DC, &Item2)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07005473 return (int32_t)nCBORError;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05305474 if((nCBORError = QCBORDecode_GetNext(&DC, &Item3)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07005475 return (int32_t)nCBORError;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05305476 if((nCBORError = QCBORDecode_GetNext(&DC, &Item4)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07005477 return (int32_t)nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005478
Laurence Lundblade05ec57b2018-10-21 01:50:03 +05305479 UsefulBuf_Set(CopyOfStorage, '_');
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005480
Laurence Lundbladea44d5062018-10-17 18:45:12 +05305481 if(Item1.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladea44d5062018-10-17 18:45:12 +05305482 Item1.uDataType != QCBOR_TYPE_INT64 ||
5483 Item1.val.int64 != 42 ||
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08005484 Item1.uDataAlloc != 0 ||
5485 Item1.uLabelAlloc == 0 ||
Laurence Lundbladeec290b82024-06-10 11:10:54 -07005486 UsefulBufCompareToSZ(Item1.label.string, "first integer") ||
5487 Item1.label.string.ptr < Pool.ptr ||
5488 Item1.label.string.ptr > (const void *)((const uint8_t *)Pool.ptr + Pool.len)) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08005489 return -4;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09005490 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005491
Laurence Lundbladea44d5062018-10-17 18:45:12 +05305492
5493 if(Item2.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07005494 UsefulBufCompareToSZ(Item2.label.string, "an array of two strings") ||
Laurence Lundbladea44d5062018-10-17 18:45:12 +05305495 Item2.uDataType != QCBOR_TYPE_ARRAY ||
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08005496 Item2.uDataAlloc != 0 ||
5497 Item2.uLabelAlloc == 0 ||
Laurence Lundbladea44d5062018-10-17 18:45:12 +05305498 Item2.val.uCount != 2)
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08005499 return -5;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005500
Laurence Lundbladea44d5062018-10-17 18:45:12 +05305501 if(Item3.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08005502 Item3.uDataAlloc == 0 ||
5503 Item3.uLabelAlloc != 0 ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07005504 UsefulBufCompareToSZ(Item3.val.string, "string1")) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08005505 return -6;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09005506 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005507
Laurence Lundbladea44d5062018-10-17 18:45:12 +05305508 if(Item4.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08005509 Item4.uDataAlloc == 0 ||
5510 Item4.uLabelAlloc != 0 ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07005511 UsefulBufCompareToSZ(Item4.val.string, "string2")) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08005512 return -7;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09005513 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005514
Laurence Lundbladea44d5062018-10-17 18:45:12 +05305515 // Next parse with a pool that is too small
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08005516 UsefulBuf_MAKE_STACK_UB(SmallPool, QCBOR_DECODE_MIN_MEM_POOL_SIZE + 1);
Laurence Lundbladeee851742020-01-08 08:37:05 -08005517 QCBORDecode_Init(&DC,
5518 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded),
5519 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladea44d5062018-10-17 18:45:12 +05305520 QCBORDecode_SetMemPool(&DC, SmallPool, 1); // Turn on copying.
5521 if((nCBORError = QCBORDecode_GetNext(&DC, &Item1)))
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08005522 return -8;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05305523 if(Item1.uDataType != QCBOR_TYPE_MAP ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09005524 Item1.val.uCount != 3) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08005525 return -9;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09005526 }
Laurence Lundbladea44d5062018-10-17 18:45:12 +05305527 if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item1))){
5528 if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item2))) {
5529 if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item3))) {
5530 nCBORError = QCBORDecode_GetNext(&DC, &Item4);
5531 }
5532 }
5533 }
Laurence Lundblade30816f22018-11-10 13:40:22 +07005534 if(nCBORError != QCBOR_ERR_STRING_ALLOCATE) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08005535 return -10;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05305536 }
Laurence Lundbladeec290b82024-06-10 11:10:54 -07005537#endif /* ! QCBOR_DISABLE_NON_INTEGER_LABELS */
Laurence Lundbladea44d5062018-10-17 18:45:12 +05305538
5539 return 0;
5540}
5541
Laurence Lundbladef6531662018-12-04 10:42:22 +09005542
Laurence Lundbladec5fef682020-01-25 11:38:45 -08005543int32_t MemPoolTest(void)
Laurence Lundblade0155b622018-10-12 20:04:37 +08005544{
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08005545 // Set up the decoder with a tiny bit of CBOR to parse because
5546 // nothing can be done with it unless that is set up.
Laurence Lundbladef6531662018-12-04 10:42:22 +09005547 QCBORDecodeContext DC;
5548 const uint8_t pMinimalCBOR[] = {0xa0}; // One empty map
5549 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pMinimalCBOR),0);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005550
Laurence Lundbladef6531662018-12-04 10:42:22 +09005551 // Set up an memory pool of 100 bytes
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08005552 // Then fish into the internals of the decode context
5553 // to get the allocator function so it can be called directly.
5554 // Also figure out how much pool is available for use
5555 // buy subtracting out the overhead.
Laurence Lundbladef6531662018-12-04 10:42:22 +09005556 UsefulBuf_MAKE_STACK_UB(Pool, 100);
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08005557 QCBORError nError = QCBORDecode_SetMemPool(&DC, Pool, 0);
5558 if(nError) {
5559 return -9;
5560 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08005561 QCBORStringAllocate pAlloc = DC.StringAllocator.pfAllocator;
5562 void *pAllocCtx = DC.StringAllocator.pAllocateCxt;
5563 size_t uAvailPool = Pool.len - QCBOR_DECODE_MIN_MEM_POOL_SIZE;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005564
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08005565 // First test -- ask for one more byte than available and see failure
5566 UsefulBuf Allocated = (*pAlloc)(pAllocCtx, NULL, uAvailPool+1);
Laurence Lundbladef6531662018-12-04 10:42:22 +09005567 if(!UsefulBuf_IsNULL(Allocated)) {
5568 return -1;
5569 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005570
Laurence Lundbladef6531662018-12-04 10:42:22 +09005571 // Re do the set up for the next test that will do a successful alloc,
5572 // a fail, a free and then success
Laurence Lundbladef6531662018-12-04 10:42:22 +09005573 QCBORDecode_SetMemPool(&DC, Pool, 0);
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08005574 pAlloc = DC.StringAllocator.pfAllocator;
5575 pAllocCtx = DC.StringAllocator.pAllocateCxt;
5576 uAvailPool = Pool.len - QCBOR_DECODE_MIN_MEM_POOL_SIZE;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005577
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08005578 // Allocate one byte less than available and see success
5579 Allocated = (pAlloc)(pAllocCtx, NULL, uAvailPool-1);
Laurence Lundbladef6531662018-12-04 10:42:22 +09005580 if(UsefulBuf_IsNULL(Allocated)) { // expected to succeed
5581 return -2;
5582 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08005583 // Ask for some more and see failure
5584 UsefulBuf Allocated2 = (*pAlloc)(pAllocCtx, NULL, uAvailPool/2);
Laurence Lundbladef6531662018-12-04 10:42:22 +09005585 if(!UsefulBuf_IsNULL(Allocated2)) { // expected to fail
5586 return -3;
5587 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08005588 // Free the first allocate, retry the second and see success
5589 (*pAlloc)(pAllocCtx, Allocated.ptr, 0); // Free
5590 Allocated = (*pAlloc)(pAllocCtx, NULL, uAvailPool/2);
Laurence Lundbladef6531662018-12-04 10:42:22 +09005591 if(UsefulBuf_IsNULL(Allocated)) { // succeed because of the free
5592 return -4;
5593 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005594
Laurence Lundbladef6531662018-12-04 10:42:22 +09005595 // Re do set up for next test that involves a successful alloc,
5596 // and a successful realloc and a failed realloc
5597 QCBORDecode_SetMemPool(&DC, Pool, 0);
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08005598 pAlloc = DC.StringAllocator.pfAllocator;
5599 pAllocCtx = DC.StringAllocator.pAllocateCxt;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005600
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08005601 // Allocate half the pool and see success
5602 Allocated = (*pAlloc)(pAllocCtx, NULL, uAvailPool/2);
Laurence Lundbladef6531662018-12-04 10:42:22 +09005603 if(UsefulBuf_IsNULL(Allocated)) { // expected to succeed
5604 return -5;
5605 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08005606 // Reallocate to take up the whole pool and see success
5607 Allocated2 = (*pAlloc)(pAllocCtx, Allocated.ptr, uAvailPool);
Laurence Lundbladef6531662018-12-04 10:42:22 +09005608 if(UsefulBuf_IsNULL(Allocated2)) {
5609 return -6;
5610 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08005611 // Make sure its the same pointer and the size is right
Laurence Lundbladef6531662018-12-04 10:42:22 +09005612 if(Allocated2.ptr != Allocated.ptr || Allocated2.len != uAvailPool) {
5613 return -7;
5614 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08005615 // Try to allocate more to be sure there is failure after a realloc
5616 UsefulBuf Allocated3 = (*pAlloc)(pAllocCtx, Allocated.ptr, uAvailPool+1);
5617 if(!UsefulBuf_IsNULL(Allocated3)) {
Laurence Lundbladef6531662018-12-04 10:42:22 +09005618 return -8;
5619 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005620
Laurence Lundbladef6531662018-12-04 10:42:22 +09005621 return 0;
5622}
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08005623
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08005624
5625/* Just enough of an allocator to test configuration of one */
5626static UsefulBuf AllocateTestFunction(void *pCtx, void *pOldMem, size_t uNewSize)
5627{
5628 (void)pOldMem; // unused variable
5629
5630 if(uNewSize) {
5631 // Assumes the context pointer is the buffer and
5632 // nothing too big will ever be asked for.
5633 // This is only good for this basic test!
5634 return (UsefulBuf) {pCtx, uNewSize};
5635 } else {
5636 return NULLUsefulBuf;
5637 }
5638}
5639
5640
Laurence Lundbladec5fef682020-01-25 11:38:45 -08005641int32_t SetUpAllocatorTest(void)
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08005642{
5643 // Set up the decoder with a tiny bit of CBOR to parse because
5644 // nothing can be done with it unless that is set up.
5645 QCBORDecodeContext DC;
5646 const uint8_t pMinimalCBOR[] = {0x62, 0x48, 0x69}; // "Hi"
5647 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pMinimalCBOR),0);
5648
5649 uint8_t pAllocatorBuffer[50];
5650
5651 // This is really just to test that this call works.
5652 // The full functionality of string allocators is tested
5653 // elsewhere with the MemPool internal allocator.
5654 QCBORDecode_SetUpAllocator(&DC, AllocateTestFunction, pAllocatorBuffer, 1);
5655
5656 QCBORItem Item;
5657 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_SUCCESS) {
5658 return -1;
5659 }
5660
5661 if(Item.uDataAlloc == 0 ||
5662 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
5663 Item.val.string.ptr != pAllocatorBuffer) {
5664 return -2;
5665 }
5666
5667 if(QCBORDecode_Finish(&DC) != QCBOR_SUCCESS) {
5668 return -3;
5669 }
5670
5671 return 0;
5672}
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08005673#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
5674
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08005675
Laurence Lundbladedd6e76e2021-03-10 01:54:01 -07005676#ifndef QCBOR_DISABLE_EXP_AND_MANTISSA
Laurence Lundblade59289e52019-12-30 13:44:37 -08005677
Laurence Lundblade37286c02022-09-03 10:05:02 -07005678struct EaMTest {
5679 const char *szName;
5680 UsefulBufC Input;
5681 uint8_t uTagRequirement;
5682 bool bHasTags;
5683
5684 /* Expected values for GetNext */
5685 QCBORError uExpectedErrorGN;
5686 uint8_t uQCBORTypeGN;
5687 int64_t nExponentGN;
5688 int64_t nMantissaGN;
5689 UsefulBufC MantissaGN;
5690
5691 /* Expected values for GetDecimalFraction */
5692 QCBORError uExpectedErrorGDF;
5693 int64_t nExponentGDF;
5694 int64_t nMantissaGDF;
5695
5696 /* Expected values for GetDecimalFractionBig */
5697 QCBORError uExpectedErrorGDFB;
5698 int64_t nExponentGDFB;
5699 UsefulBufC MantissaGDFB;
5700 bool IsNegativeGDFB;
5701
5702 /* Expected values for GetBigFloat */
5703 QCBORError uExpectedErrorGBF;
5704 int64_t nExponentGBF;
5705 int64_t nMantissaGBF;
5706
5707 /* Expected values for GetBigFloatBig */
5708 QCBORError uExpectedErrorGBFB;
5709 int64_t nExponentGBFB;
5710 UsefulBufC MantissaGBFB;
5711 bool IsNegativeGBFB;
Laurence Lundblade59289e52019-12-30 13:44:37 -08005712};
5713
Laurence Lundbladefaec39f2020-08-02 21:53:53 -07005714
Laurence Lundblade37286c02022-09-03 10:05:02 -07005715
5716static const struct EaMTest pEaMTests[] = {
5717 {
5718 "1. Untagged pair (big float or decimal fraction), no tag required",
5719 {(const uint8_t []){0x82, 0x20, 0x03}, 3},
5720 QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
5721 false,
5722
5723 QCBOR_SUCCESS, /* for GetNext */
5724 QCBOR_TYPE_ARRAY,
5725 0,
5726 0,
5727 {(const uint8_t []){0x00}, 1},
5728
5729 QCBOR_SUCCESS, /* GetDecimalFraction */
5730 -1,
5731 3,
5732
5733 QCBOR_SUCCESS, /* for GetDecimalFractionBig */
5734 -1,
5735 {(const uint8_t []){0x03}, 1},
5736 false,
5737
5738 QCBOR_SUCCESS, /* for GetBigFloat */
5739 -1,
5740 3,
5741
5742 QCBOR_SUCCESS, /* for GetBigFloatBig */
5743 -1,
5744 {(const uint8_t []){0x03}, 1},
5745 false
5746 },
5747
5748 {
5749 "2. Untagged pair (big float or decimal fraction), tag required",
5750 {(const uint8_t []){0x82, 0x20, 0x03}, 3},
5751 QCBOR_TAG_REQUIREMENT_TAG,
5752 false,
5753
5754 QCBOR_SUCCESS, /* for GetNext */
5755 QCBOR_TYPE_ARRAY,
5756 0,
5757 0,
5758 {(const uint8_t []){0x00}, 1},
5759
5760 QCBOR_ERR_UNEXPECTED_TYPE, /* for GetDecimalFraction */
5761 0,
5762 0,
5763
5764 QCBOR_ERR_UNEXPECTED_TYPE, /* for GetDecimalFractionBig */
5765 0,
5766 {(const uint8_t []){0x00}, 1},
5767 false,
5768
5769 QCBOR_ERR_UNEXPECTED_TYPE, /* for GetBigFloat */
5770 0,
5771 0,
5772
5773 QCBOR_ERR_UNEXPECTED_TYPE, /* for GetBigFloatBig */
5774 0,
5775 {(const uint8_t []){0x00}, 1},
5776 false
5777
5778 },
5779
5780 {
5781 "3. Tagged 1.5 decimal fraction, tag 4 optional",
5782 {(const uint8_t []){0xC4, 0x82, 0x20, 0x03}, 4},
5783 QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
5784 true,
5785
5786 QCBOR_SUCCESS, /* for GetNext */
5787 QCBOR_TYPE_DECIMAL_FRACTION,
5788 -1,
5789 3,
5790 {(const uint8_t []){0x00}, 1},
5791
5792
5793 QCBOR_SUCCESS, /* for GetDecimalFraction */
5794 -1,
5795 3,
5796
5797 QCBOR_SUCCESS, /* for GetDecimalFractionBig */
5798 -1,
5799 {(const uint8_t []){0x03}, 1},
5800 false,
5801
5802 QCBOR_ERR_UNEXPECTED_TYPE, /* for GetBigFloat */
5803 0,
5804 0,
5805
5806 QCBOR_ERR_UNEXPECTED_TYPE, /* for GetBigFloatBig */
5807 0,
5808 {(const uint8_t []){0x00}, 1},
5809 false
5810 },
5811 {
5812 "4. Tagged 100 * 2^300 big float, tag 5 optional",
5813 {(const uint8_t []){0xC5, 0x82, 0x19, 0x01, 0x2C, 0x18, 0x64}, 7},
5814 QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
5815 true,
5816
5817 QCBOR_SUCCESS, /* for GetNext */
5818 QCBOR_TYPE_BIGFLOAT,
5819 300,
5820 100,
5821 {(const uint8_t []){0x00}, 1},
5822
5823
5824 QCBOR_ERR_UNEXPECTED_TYPE, /* for GetDecimalFraction */
5825 0,
5826 0,
5827
5828 QCBOR_ERR_UNEXPECTED_TYPE, /* for GetDecimalFractionBig */
5829 0,
5830 {(const uint8_t []){0x03}, 1},
5831 false,
5832
5833 QCBOR_SUCCESS, /* for GetBigFloat */
5834 300,
5835 100,
5836
5837 QCBOR_SUCCESS, /* for GetBigFloatBig */
5838 300,
5839 {(const uint8_t []){0x64}, 1},
5840 false
5841 },
5842
5843 {
5844 "5. Tagged 4([-20, 4759477275222530853136]) decimal fraction, tag 4 required",
5845 {(const uint8_t []){0xC4, 0x82, 0x33,
5846 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,}, 15},
5847 QCBOR_TAG_REQUIREMENT_TAG,
5848 true,
5849
5850 QCBOR_SUCCESS, /* for GetNext */
5851 QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM,
5852 -20,
5853 0,
5854 {(const uint8_t []){0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10}, 10},
5855
5856 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW, /* for GetDecimalFraction */
5857 0,
5858 0,
5859
5860 QCBOR_SUCCESS, /* for GetDecimalFractionBig */
5861 -20,
5862 {(const uint8_t []){0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10}, 10},
5863 false,
5864
5865 QCBOR_ERR_UNEXPECTED_TYPE, /* for GetBigFloat */
5866 0,
5867 0,
5868
5869 QCBOR_ERR_UNEXPECTED_TYPE, /* for GetBigFloatBig */
5870 0,
5871 {(const uint8_t []){0x00}, 0},
5872 false
5873 },
5874
5875 {
5876 "6. Error: Mantissa and exponent inside a Mantissa and exponent",
5877 {(const uint8_t []){0xC4, 0x82, 0x33,
5878 0xC5, 0x82, 0x19, 0x01, 0x2C, 0x18, 0x64}, 10},
5879 QCBOR_TAG_REQUIREMENT_TAG,
5880 true,
5881
5882 QCBOR_ERR_BAD_EXP_AND_MANTISSA, /* for GetNext */
5883 QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM,
5884 0,
5885 0,
5886 {(const uint8_t []){0x00}, 0},
5887
5888 QCBOR_ERR_BAD_EXP_AND_MANTISSA, /* for GetDecimalFraction */
5889 0,
5890 0,
5891
5892 QCBOR_ERR_BAD_EXP_AND_MANTISSA, /* for GetDecimalFractionBig */
5893 0,
5894 {(const uint8_t []){0x00}, 0},
5895 false,
5896
5897 QCBOR_ERR_BAD_EXP_AND_MANTISSA, /* for GetBigFloat */
5898 0,
5899 0,
5900
5901 QCBOR_ERR_BAD_EXP_AND_MANTISSA, /* for GetBigFloatBig */
5902 0,
5903 {(const uint8_t []){0x00}, 0},
5904 false
5905 },
5906 {
5907 "7. Tagged 5([-20, 4294967295]) big float, big num mantissa, tag 5 required",
5908 {(const uint8_t []){0xC5, 0x82, 0x33,
5909 0xC2, 0x44, 0xff, 0xff, 0xff, 0xff}, 9},
5910 QCBOR_TAG_REQUIREMENT_TAG,
5911 true,
5912
5913 QCBOR_SUCCESS, /* for GetNext */
5914 QCBOR_TYPE_BIGFLOAT_POS_BIGNUM,
5915 -20,
5916 0,
5917 {(const uint8_t []){0xff, 0xff, 0xff, 0xff}, 4},
5918
5919 QCBOR_ERR_UNEXPECTED_TYPE, /* for GetDecimalFraction */
5920 0,
5921 0,
5922
5923 QCBOR_ERR_UNEXPECTED_TYPE, /* for GetDecimalFractionBig */
5924 -20,
5925 {(const uint8_t []){0x00}, 1},
5926 false,
5927
5928 QCBOR_SUCCESS, /* for GetBigFloat */
5929 -20,
5930 4294967295,
5931
5932 QCBOR_SUCCESS, /* for GetBigFloatBig */
5933 -20,
5934 {(const uint8_t []){0xff, 0xff, 0xff, 0xff}, 4},
5935 false
5936 },
5937
5938 {
5939 /* Special case for test 8. Don't renumber it. */
5940 "8. Untagged pair with big num (big float or decimal fraction), tag optional",
5941 {(const uint8_t []){0x82, 0x33, 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10}, 14},
5942 QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
5943 true,
5944
5945 QCBOR_SUCCESS, /* for GetNext */
5946 QCBOR_TYPE_ARRAY,
5947 0,
5948 0,
5949 {(const uint8_t []){0x00}, 1},
5950
5951 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW, /* GetDecimalFraction */
5952 0,
5953 0,
5954
5955 QCBOR_SUCCESS, /* for GetDecimalFractionBig */
5956 -20,
5957 {(const uint8_t []){0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10}, 10},
5958 false,
5959
5960 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW, /* for GetBigFloat */
5961 0,
5962 0,
5963
5964 QCBOR_SUCCESS, /* for GetBigFloatBig */
5965 -20,
5966 {(const uint8_t []){0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10}, 10},
5967 false
5968 },
5969
5970 {
5971 "9. decimal fraction with large exponent and negative big num mantissa",
5972 {(const uint8_t []){0xC4, 0x82, 0x1B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
5973 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10}, 23},
5974 QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
5975 true,
5976
5977 QCBOR_SUCCESS, /* for GetNext */
5978 QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM,
5979 9223372036854775807,
5980 0,
5981 {(const uint8_t []){0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10}, 10},
5982
5983 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW, /* GetDecimalFraction */
5984 0,
5985 0,
5986
5987 QCBOR_SUCCESS, /* for GetDecimalFractionBig */
5988 9223372036854775807,
5989 {(const uint8_t []){0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10}, 10},
5990 true,
5991
5992 QCBOR_ERR_UNEXPECTED_TYPE, /* for GetBigFloat */
5993 0,
5994 0,
5995
5996 QCBOR_ERR_UNEXPECTED_TYPE, /* for GetBigFloatBig */
5997 0,
5998 {(const uint8_t []){0x00}, 1},
5999 false
6000 },
6001};
6002
6003
6004
6005int32_t ProcessEaMTests(void)
Laurence Lundblade59289e52019-12-30 13:44:37 -08006006{
Laurence Lundblade37286c02022-09-03 10:05:02 -07006007 size_t uIndex;
6008 QCBORDecodeContext DCtx;
6009 QCBORItem Item;
6010 QCBORError uError;
6011 int64_t nMantissa, nExponent;
6012 MakeUsefulBufOnStack( MantissaBuf, 200);
6013 UsefulBufC Mantissa;
6014 bool bMantissaIsNegative;
6015
6016 for(uIndex = 0; uIndex < C_ARRAY_COUNT(pEaMTests, struct EaMTest); uIndex++) {
6017 const struct EaMTest *pT = &pEaMTests[uIndex];
6018 /* Decode with GetNext */
6019 QCBORDecode_Init(&DCtx, pT->Input, 0);
6020
6021 if(uIndex + 1 == 9) {
6022 nExponent = 99; // just to set a break point
6023 }
6024
6025 uError = QCBORDecode_GetNext(&DCtx, &Item);
6026#ifdef QCBOR_DISABLE_TAGS
6027 /* Test 8 is a special case when tags are disabled */
6028 if(pT->bHasTags && uIndex + 1 != 8) {
6029 if(uError != QCBOR_ERR_TAGS_DISABLED) {
6030 return (int32_t)(1+uIndex) * 1000 + 9;
6031 }
6032 } else {
6033#endif
6034 /* Now check return code, data type, mantissa and exponent */
6035 if(pT->uExpectedErrorGN != uError) {
6036 return (int32_t)(1+uIndex) * 1000 + 1;
6037 }
6038 if(uError == QCBOR_SUCCESS && pT->uQCBORTypeGN != QCBOR_TYPE_ARRAY) {
6039 if(pT->uQCBORTypeGN != Item.uDataType) {
6040 return (int32_t)(1+uIndex) * 1000 + 2;
6041 }
6042 if(pT->nExponentGN != Item.val.expAndMantissa.nExponent) {
6043 return (int32_t)(1+uIndex) * 1000 + 3;
6044 }
6045 if(Item.uDataType == QCBOR_TYPE_DECIMAL_FRACTION || Item.uDataType == QCBOR_TYPE_BIGFLOAT ) {
6046 if(pT->nMantissaGN != Item.val.expAndMantissa.Mantissa.nInt) {
6047 return (int32_t)(1+uIndex) * 1000 + 4;
6048 }
6049 } else {
6050 if(UsefulBuf_Compare(Item.val.expAndMantissa.Mantissa.bigNum, pT->MantissaGN)) {
6051 return (int32_t)(1+uIndex) * 1000 + 5;
6052 }
6053 }
6054 }
6055#ifdef QCBOR_DISABLE_TAGS
6056 }
6057#endif
6058
6059 /* Decode with GetDecimalFraction */
6060 QCBORDecode_Init(&DCtx, pT->Input, 0);
6061 QCBORDecode_GetDecimalFraction(&DCtx,
6062 pT->uTagRequirement,
6063 &nMantissa,
6064 &nExponent);
6065 uError = QCBORDecode_GetAndResetError(&DCtx);
6066#ifdef QCBOR_DISABLE_TAGS
6067 if(pT->bHasTags) {
6068 if(uError != QCBOR_ERR_TAGS_DISABLED) {
6069 return (int32_t)(1+uIndex) * 1000 + 39;
6070 }
6071 } else {
6072#endif
6073 /* Now check return code, mantissa and exponent */
6074 if(pT->uExpectedErrorGDF != uError) {
6075 return (int32_t)(1+uIndex) * 1000 + 31;
6076 }
6077 if(uError == QCBOR_SUCCESS) {
6078 if(pT->nExponentGDF != nExponent) {
6079 return (int32_t)(1+uIndex) * 1000 + 32;
6080 }
6081 if(pT->nMantissaGDF != nMantissa) {
6082 return (int32_t)(1+uIndex) * 1000 + 33;
6083 }
6084 }
6085#ifdef QCBOR_DISABLE_TAGS
6086 }
6087#endif
6088
6089 /* Decode with GetDecimalFractionBig */
6090 QCBORDecode_Init(&DCtx, pT->Input, 0);
6091 QCBORDecode_GetDecimalFractionBig(&DCtx,
6092 pT->uTagRequirement,
6093 MantissaBuf,
6094 &Mantissa,
6095 &bMantissaIsNegative,
6096 &nExponent);
6097 uError = QCBORDecode_GetAndResetError(&DCtx);
6098#ifdef QCBOR_DISABLE_TAGS
6099 if(pT->bHasTags) {
6100 if(uError != QCBOR_ERR_TAGS_DISABLED) {
6101 return (int32_t)(1+uIndex) * 1000 + 49;
6102 }
6103 } else {
6104#endif
6105 /* Now check return code, mantissa (bytes and sign) and exponent */
6106 if(pT->uExpectedErrorGDFB != uError) {
6107 return (int32_t)(1+uIndex) * 1000 + 41;
6108 }
6109 if(uError == QCBOR_SUCCESS) {
6110 if(pT->nExponentGDFB != nExponent) {
6111 return (int32_t)(1+uIndex) * 1000 + 42;
6112 }
6113 if(pT->IsNegativeGDFB != bMantissaIsNegative) {
6114 return (int32_t)(1+uIndex) * 1000 + 43;
6115 }
6116 if(UsefulBuf_Compare(Mantissa, pT->MantissaGDFB)) {
6117 return (int32_t)(1+uIndex) * 1000 + 44;
6118 }
6119 }
6120#ifdef QCBOR_DISABLE_TAGS
6121 }
6122#endif
6123
6124 /* Decode with GetBigFloat */
6125 QCBORDecode_Init(&DCtx, pT->Input, 0);
6126 QCBORDecode_GetBigFloat(&DCtx,
6127 pT->uTagRequirement,
6128 &nMantissa,
6129 &nExponent);
6130 uError = QCBORDecode_GetAndResetError(&DCtx);
6131#ifdef QCBOR_DISABLE_TAGS
6132 if(pT->bHasTags) {
6133 if(uError != QCBOR_ERR_TAGS_DISABLED) {
6134 return (int32_t)(1+uIndex) * 1000 + 19;
6135 }
6136 } else {
6137#endif
6138 /* Now check return code, mantissa and exponent */
6139 if(pT->uExpectedErrorGBF != uError) {
6140 return (int32_t)(1+uIndex) * 1000 + 11;
6141 }
6142 if(uError == QCBOR_SUCCESS) {
6143 if(pT->nExponentGBF != nExponent) {
6144 return (int32_t)(1+uIndex) * 1000 + 12;
6145 }
6146 if(pT->nMantissaGBF != nMantissa) {
6147 return (int32_t)(1+uIndex) * 1000 + 13;
6148 }
6149 }
6150#ifdef QCBOR_DISABLE_TAGS
6151 }
6152#endif
6153
6154 /* Decode with GetBigFloatBig */
6155 QCBORDecode_Init(&DCtx, pT->Input, 0);
6156 QCBORDecode_GetBigFloatBig(&DCtx,
6157 pT->uTagRequirement,
6158 MantissaBuf,
6159 &Mantissa,
6160 &bMantissaIsNegative,
6161 &nExponent);
6162 uError = QCBORDecode_GetAndResetError(&DCtx);
6163#ifdef QCBOR_DISABLE_TAGS
6164 if(pT->bHasTags) {
6165 if(uError != QCBOR_ERR_TAGS_DISABLED) {
6166 return (int32_t)(1+uIndex) * 1000 + 29;
6167 }
6168 } else {
6169#endif
6170 /* Now check return code, mantissa (bytes and sign) and exponent */
6171 if(pT->uExpectedErrorGBFB != uError) {
6172 return (int32_t)(1+uIndex) * 1000 + 21;
6173 }
6174 if(uError == QCBOR_SUCCESS) {
6175 if(pT->nExponentGBFB != nExponent) {
6176 return (int32_t)(1+uIndex) * 1000 + 22;
6177 }
6178 if(pT->IsNegativeGBFB != bMantissaIsNegative) {
6179 return (int32_t)(1+uIndex) * 1000 + 23;
6180 }
6181 if(UsefulBuf_Compare(Mantissa, pT->MantissaGBFB)) {
6182 return (int32_t)(1+uIndex) * 1000 + 24;
6183 }
6184 }
6185#ifdef QCBOR_DISABLE_TAGS
6186 }
6187#endif
6188 }
6189
6190 return 0;
6191}
6192
6193
6194int32_t ExponentAndMantissaDecodeTestsSecondary(void)
6195{
6196#ifndef QCBOR_DISABLE_TAGS
Laurence Lundblade59289e52019-12-30 13:44:37 -08006197 QCBORDecodeContext DC;
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07006198 QCBORError uErr;
Laurence Lundblade59289e52019-12-30 13:44:37 -08006199 QCBORItem item;
6200
Laurence Lundblade17af4902020-01-07 19:11:55 -08006201 static const uint8_t spBigNumMantissa[] = {0x01, 0x02, 0x03, 0x04, 0x05,
6202 0x06, 0x07, 0x08, 0x09, 0x010};
6203 UsefulBufC BN = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNumMantissa);
Laurence Lundblade59289e52019-12-30 13:44:37 -08006204
6205
Laurence Lundblade59289e52019-12-30 13:44:37 -08006206
6207 /* Now encode some stuff and then decode it */
6208 uint8_t pBuf[40];
6209 QCBOREncodeContext EC;
6210 UsefulBufC Encoded;
6211
6212 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(pBuf));
6213 QCBOREncode_OpenArray(&EC);
6214 QCBOREncode_AddDecimalFraction(&EC, 999, 1000); // 999 * (10 ^ 1000)
6215 QCBOREncode_AddBigFloat(&EC, 100, INT32_MIN);
6216 QCBOREncode_AddDecimalFractionBigNum(&EC, BN, false, INT32_MAX);
6217 QCBOREncode_CloseArray(&EC);
6218 QCBOREncode_Finish(&EC, &Encoded);
6219
6220
6221 QCBORDecode_Init(&DC, Encoded, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07006222 uErr = QCBORDecode_GetNext(&DC, &item);
6223 if(uErr != QCBOR_SUCCESS) {
6224 return 100;
Laurence Lundblade59289e52019-12-30 13:44:37 -08006225 }
6226
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07006227 uErr = QCBORDecode_GetNext(&DC, &item);
6228 if(uErr != QCBOR_SUCCESS) {
6229 return 101;
Laurence Lundblade59289e52019-12-30 13:44:37 -08006230 }
6231
6232 if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION ||
6233 item.val.expAndMantissa.nExponent != 1000 ||
6234 item.val.expAndMantissa.Mantissa.nInt != 999) {
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07006235 return 102;
Laurence Lundblade59289e52019-12-30 13:44:37 -08006236 }
6237
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07006238 uErr = QCBORDecode_GetNext(&DC, &item);
6239 if(uErr != QCBOR_SUCCESS) {
6240 return 103;
Laurence Lundblade59289e52019-12-30 13:44:37 -08006241 }
6242
6243 if(item.uDataType != QCBOR_TYPE_BIGFLOAT ||
6244 item.val.expAndMantissa.nExponent != INT32_MIN ||
6245 item.val.expAndMantissa.Mantissa.nInt != 100) {
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07006246 return 104;
Laurence Lundblade59289e52019-12-30 13:44:37 -08006247 }
6248
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07006249 uErr = QCBORDecode_GetNext(&DC, &item);
6250 if(uErr != QCBOR_SUCCESS) {
6251 return 105;
Laurence Lundblade59289e52019-12-30 13:44:37 -08006252 }
6253
6254 if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM ||
6255 item.val.expAndMantissa.nExponent != INT32_MAX ||
6256 UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) {
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07006257 return 106;
6258 }
6259
Laurence Lundblade37286c02022-09-03 10:05:02 -07006260#endif /* QCBOR_TAGS_DISABLED */
Laurence Lundblade59289e52019-12-30 13:44:37 -08006261
6262 return 0;
6263}
6264
6265
Laurence Lundblade37286c02022-09-03 10:05:02 -07006266int32_t ExponentAndMantissaDecodeTests(void)
6267{
6268 int32_t rv = ProcessEaMTests();
6269 if(rv) {
6270 return rv;
6271 }
6272
6273 return ExponentAndMantissaDecodeTestsSecondary();
6274}
6275
6276
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08006277static const struct DecodeFailTestInput ExponentAndMantissaFailures[] = {
6278 { "Exponent > INT64_MAX",
6279 QCBOR_DECODE_MODE_NORMAL,
6280 {"\xC4\x82\x1B\x7f\xFF\xFF\xFF\xFF\xFF\xFF\xFF\x1B\x80\xFF\xFF\xFF\xFF\xFF\xFF\xFF", 20},
6281 QCBOR_ERR_BAD_EXP_AND_MANTISSA
6282 },
6283 { "Mantissa > INT64_MAX",
6284 QCBOR_DECODE_MODE_NORMAL,
6285 {"\xC4\x82\x1B\x80\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xC3\x4A\x01\x02\x03\x04\x05\x06\x07\x08\x09\x10", 23},
6286 QCBOR_ERR_BAD_EXP_AND_MANTISSA
6287 },
6288 {
6289 "End of input",
6290 QCBOR_DECODE_MODE_NORMAL,
6291 {"\xC4\x82", 2},
6292 QCBOR_ERR_NO_MORE_ITEMS
6293 },
6294 {"bad content for big num",
6295 QCBOR_DECODE_MODE_NORMAL,
6296 {"\xC4\x82\x01\xc3\x01", 5},
6297 QCBOR_ERR_BAD_OPT_TAG
6298 },
6299 {"bad content for big num",
6300 QCBOR_DECODE_MODE_NORMAL,
Laurence Lundbladecdbbc192024-06-28 15:13:04 -07006301 {"\xC4\x82\xC2\x01\x1F", 5},
6302 QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08006303 },
6304 {"Bad integer for exponent",
6305 QCBOR_DECODE_MODE_NORMAL,
6306 {"\xC4\x82\x01\x1f", 4},
6307 QCBOR_ERR_BAD_INT
6308 },
6309 {"Bad integer for mantissa",
6310 QCBOR_DECODE_MODE_NORMAL,
6311 {"\xC4\x82\x1f\x01", 4},
6312 QCBOR_ERR_BAD_INT
6313 },
6314 {"3 items in array",
6315 QCBOR_DECODE_MODE_NORMAL,
6316 {"\xC4\x83\x03\x01\x02", 5},
6317 QCBOR_ERR_BAD_EXP_AND_MANTISSA},
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08006318#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08006319 {"unterminated indefinite length array",
6320 QCBOR_DECODE_MODE_NORMAL,
6321 {"\xC4\x9f\x03\x01\x02", 5},
6322 QCBOR_ERR_BAD_EXP_AND_MANTISSA
6323 },
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08006324#else /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08006325 {"unterminated indefinite length array",
6326 QCBOR_DECODE_MODE_NORMAL,
6327 {"\xC4\x9f\x03\x01\x02", 5},
6328 QCBOR_ERR_INDEF_LEN_ARRAYS_DISABLED
6329 },
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08006330#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08006331 {"Empty array",
6332 QCBOR_DECODE_MODE_NORMAL,
6333 {"\xC4\x80", 2},
6334 QCBOR_ERR_NO_MORE_ITEMS
6335 },
6336 {"Second is not an integer",
6337 QCBOR_DECODE_MODE_NORMAL,
6338 {"\xC4\x82\x03\x40", 4},
6339 QCBOR_ERR_BAD_EXP_AND_MANTISSA
6340 },
6341 {"First is not an integer",
6342 QCBOR_DECODE_MODE_NORMAL,
6343 {"\xC4\x82\x40", 3},
6344 QCBOR_ERR_BAD_EXP_AND_MANTISSA
6345 },
6346 {"Not an array",
6347 QCBOR_DECODE_MODE_NORMAL,
6348 {"\xC4\xA2", 2},
6349 QCBOR_ERR_BAD_EXP_AND_MANTISSA
6350 }
Laurence Lundblade59289e52019-12-30 13:44:37 -08006351};
6352
6353
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08006354int32_t
6355ExponentAndMantissaDecodeFailTests(void)
Laurence Lundblade59289e52019-12-30 13:44:37 -08006356{
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08006357 return ProcessDecodeFailures(ExponentAndMantissaFailures,
6358 C_ARRAY_COUNT(ExponentAndMantissaFailures,
6359 struct DecodeFailTestInput));
Laurence Lundblade59289e52019-12-30 13:44:37 -08006360}
6361
Laurence Lundbladedd6e76e2021-03-10 01:54:01 -07006362#endif /* QCBOR_DISABLE_EXP_AND_MANTISSA */
Laurence Lundbladebb87be22020-04-09 19:15:32 -07006363
6364
6365
6366/*
6367 Some basic CBOR with map and array used in a lot of tests.
6368 The map labels are all strings
6369
Laurence Lundblade8ffdb742020-05-07 02:49:18 -07006370 {
6371 "first integer": 42,
Laurence Lundbladebb87be22020-04-09 19:15:32 -07006372 "an array of two strings": [
6373 "string1", "string2"
6374 ],
6375 "map in a map": {
6376 "bytes 1": h'78787878',
6377 "bytes 2": h'79797979',
6378 "another int": 98,
6379 "text 2": "lies, damn lies and statistics"
6380 }
Laurence Lundbladeddebabe2020-11-22 11:32:17 -08006381 }
Laurence Lundbladebb87be22020-04-09 19:15:32 -07006382 */
Laurence Lundblade9b334962020-08-27 10:55:53 -07006383
Laurence Lundbladef7a70bc2020-10-24 12:23:25 -07006384int32_t SpiffyDecodeBasicMap(UsefulBufC input)
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07006385{
6386 QCBORItem Item1, Item2, Item3;
6387 int64_t nDecodedInt1, nDecodedInt2;
6388 UsefulBufC B1, B2, S1, S2, S3;
6389
6390 QCBORDecodeContext DCtx;
6391 QCBORError nCBORError;
6392
6393 QCBORDecode_Init(&DCtx, input, 0);
6394
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006395 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07006396
6397 QCBORDecode_GetInt64InMapSZ(&DCtx, "first integer", &nDecodedInt1);
6398
6399 QCBORDecode_EnterMapFromMapSZ(&DCtx, "map in a map");
6400 QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2);
Laurence Lundblade323f8a92020-09-06 19:43:09 -07006401 QCBORDecode_GetByteStringInMapSZ(&DCtx, "bytes 1", &B1);
6402 QCBORDecode_GetByteStringInMapSZ(&DCtx, "bytes 2", &B2);
6403 QCBORDecode_GetTextStringInMapSZ(&DCtx, "text 2", &S1);
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07006404 QCBORDecode_ExitMap(&DCtx);
6405
6406 QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings");
6407 QCBORDecode_GetNext(&DCtx, &Item1);
6408 QCBORDecode_GetNext(&DCtx, &Item2);
6409 if(QCBORDecode_GetNext(&DCtx, &Item3) != QCBOR_ERR_NO_MORE_ITEMS) {
6410 return -400;
6411 }
6412 QCBORDecode_ExitArray(&DCtx);
6413
6414 // Parse the same array again using GetText() instead of GetItem()
6415 QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings");
Laurence Lundblade323f8a92020-09-06 19:43:09 -07006416 QCBORDecode_GetTextString(&DCtx, &S2);
6417 QCBORDecode_GetTextString(&DCtx, &S3);
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07006418 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) {
6419 return 5000;
6420 }
6421 /* QCBORDecode_GetText(&DCtx, &S3);
6422 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_NO_MORE_ITEMS) {
6423 return 5001;
6424 } */
6425
6426 QCBORDecode_ExitArray(&DCtx);
6427
6428 QCBORDecode_ExitMap(&DCtx);
6429
6430 nCBORError = QCBORDecode_Finish(&DCtx);
6431
6432 if(nCBORError) {
6433 return (int32_t)nCBORError;
6434 }
6435
6436 if(nDecodedInt1 != 42) {
6437 return 1001;
6438 }
6439
6440 if(nDecodedInt2 != 98) {
6441 return 1002;
6442 }
6443
6444 if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07006445 UsefulBufCompareToSZ(Item1.val.string, "string1")) {
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07006446 return 1003;
6447 }
6448
6449 if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07006450 UsefulBufCompareToSZ(Item2.val.string, "string2")) {
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07006451 return 1004;
6452 }
6453
Laurence Lundblade9b334962020-08-27 10:55:53 -07006454 if(UsefulBufCompareToSZ(S1, "lies, damn lies and statistics")) {
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07006455 return 1005;
6456 }
6457
6458 if(UsefulBuf_Compare(B1, UsefulBuf_FromSZ("xxxx"))){
6459 return 1006;
6460 }
6461
6462 if(UsefulBuf_Compare(B2, UsefulBuf_FromSZ("yyyy"))){
6463 return 1007;
6464 }
6465
6466 if(UsefulBuf_Compare(S2, UsefulBuf_FromSZ("string1"))){
6467 return 1008;
6468 }
6469
6470 if(UsefulBuf_Compare(S3, UsefulBuf_FromSZ("string2"))){
6471 return 1009;
6472 }
6473
6474 return 0;
6475}
6476
Laurence Lundbladeddebabe2020-11-22 11:32:17 -08006477/*
Laurence Lundbladecc7da412020-12-27 00:09:07 -08006478 {
6479 -75008: h'05083399',
6480 88: [],
6481 100100: {
6482 "sub1": {
6483 10: [
6484 0
6485 ],
6486 -75009: h'A46823990001',
6487 100100: {
6488 "json": "{ \"ueid\", \"xyz\"}",
6489 "subsub": {
6490 100002: h'141813191001'
6491 }
6492 }
Laurence Lundbladeddebabe2020-11-22 11:32:17 -08006493 }
Laurence Lundbladecc7da412020-12-27 00:09:07 -08006494 }
Laurence Lundbladeddebabe2020-11-22 11:32:17 -08006495 }
Laurence Lundbladeddebabe2020-11-22 11:32:17 -08006496 */
6497
6498static const uint8_t spNestedCBOR[] = {
Laurence Lundbladecc7da412020-12-27 00:09:07 -08006499 0xa3, 0x3a, 0x00, 0x01, 0x24, 0xff, 0x44, 0x05,
6500 0x08, 0x33, 0x99, 0x18, 0x58, 0x80, 0x1a, 0x00,
6501 0x01, 0x87, 0x04, 0xa1, 0x64, 0x73, 0x75, 0x62,
6502 0x31, 0xa3, 0x0a, 0x81, 0x00, 0x3a, 0x00, 0x01,
6503 0x25, 0x00, 0x46, 0xa4, 0x68, 0x23, 0x99, 0x00,
6504 0x01, 0x1a, 0x00, 0x01, 0x87, 0x04, 0xa2, 0x64,
6505 0x6a, 0x73, 0x6f, 0x6e, 0x70, 0x7b, 0x20, 0x22,
6506 0x75, 0x65, 0x69, 0x64, 0x22, 0x2c, 0x20, 0x22,
6507 0x78, 0x79, 0x7a, 0x22, 0x7d, 0x66, 0x73, 0x75,
6508 0x62, 0x73, 0x75, 0x62, 0xa1, 0x1a, 0x00, 0x01,
6509 0x86, 0xa2, 0x46, 0x14, 0x18, 0x13, 0x19, 0x10,
6510 0x01
Laurence Lundbladeddebabe2020-11-22 11:32:17 -08006511};
6512
6513/* Get item in multi-level nesting in spNestedCBOR */
6514static int32_t DecodeNestedGetSubSub(QCBORDecodeContext *pDCtx)
6515{
6516 UsefulBufC String;
6517
6518 uint8_t test_oemid_bytes[] = {0x14, 0x18, 0x13, 0x19, 0x10, 0x01};
6519 const struct q_useful_buf_c test_oemid = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(test_oemid_bytes);
6520
6521 QCBORDecode_EnterMapFromMapN(pDCtx, 100100);
6522 QCBORDecode_EnterMap(pDCtx, NULL);
6523 QCBORDecode_EnterMapFromMapN(pDCtx, 100100);
6524 QCBORDecode_EnterMapFromMapSZ(pDCtx, "subsub");
6525 QCBORDecode_GetByteStringInMapN(pDCtx, 100002, &String);
6526 if(QCBORDecode_GetError(pDCtx)) {
6527 return 4001;
6528 }
6529 if(UsefulBuf_Compare(String, test_oemid)) {
6530 return 4002;
6531 }
6532 QCBORDecode_ExitMap(pDCtx);
6533 QCBORDecode_ExitMap(pDCtx);
6534 QCBORDecode_ExitMap(pDCtx);
6535 QCBORDecode_ExitMap(pDCtx);
6536
6537 return 0;
6538}
6539
6540/* Iterations on the zero-length array in spNestedCBOR */
6541static int32_t DecodeNestedGetEmpty(QCBORDecodeContext *pDCtx)
6542{
6543 QCBORItem Item;
6544 QCBORError uErr;
6545
6546 QCBORDecode_EnterArrayFromMapN(pDCtx, 88);
6547 for(int x = 0; x < 20; x++) {
6548 uErr = QCBORDecode_GetNext(pDCtx, &Item);
6549 if(uErr != QCBOR_ERR_NO_MORE_ITEMS) {
6550 return 4100;
6551
6552 }
6553 }
6554 QCBORDecode_ExitArray(pDCtx);
6555 if(QCBORDecode_GetError(pDCtx)) {
6556 return 4101;
6557 }
6558
6559 return 0;
6560}
6561
6562/* Various iterations on the array that contains a zero in spNestedCBOR */
6563static int32_t DecodeNestedGetZero(QCBORDecodeContext *pDCtx)
6564{
6565 QCBORError uErr;
6566
6567 QCBORDecode_EnterMapFromMapN(pDCtx, 100100);
6568 QCBORDecode_EnterMapFromMapSZ(pDCtx, "sub1");
6569 QCBORDecode_EnterArrayFromMapN(pDCtx, 10);
6570 int64_t nInt = 99;
6571 QCBORDecode_GetInt64(pDCtx, &nInt);
6572 if(nInt != 0) {
6573 return 4200;
6574 }
6575 for(int x = 0; x < 20; x++) {
6576 QCBORItem Item;
6577 uErr = QCBORDecode_GetNext(pDCtx, &Item);
6578 if(uErr != QCBOR_ERR_NO_MORE_ITEMS) {
6579 return 4201;
6580
6581 }
6582 }
6583 QCBORDecode_ExitArray(pDCtx);
6584 if(QCBORDecode_GetAndResetError(pDCtx)) {
6585 return 4202;
6586 }
6587 QCBORDecode_EnterArrayFromMapN(pDCtx, 10);
6588 UsefulBufC dD;
6589 QCBORDecode_GetByteString(pDCtx, &dD);
6590 if(QCBORDecode_GetAndResetError(pDCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
6591 return 4203;
6592 }
6593 for(int x = 0; x < 20; x++) {
6594 QCBORDecode_GetByteString(pDCtx, &dD);
6595 uErr = QCBORDecode_GetAndResetError(pDCtx);
6596 if(uErr != QCBOR_ERR_NO_MORE_ITEMS) {
6597 return 4204;
6598 }
6599 }
6600 QCBORDecode_ExitArray(pDCtx);
6601 QCBORDecode_ExitMap(pDCtx);
6602 QCBORDecode_ExitMap(pDCtx);
6603
6604 return 0;
6605}
6606
6607/* Repeatedly enter and exit maps and arrays, go off the end of maps
6608 and arrays and such. */
Laurence Lundbladeb9702452021-03-08 21:02:57 -08006609static int32_t DecodeNestedIterate(void)
Laurence Lundbladeddebabe2020-11-22 11:32:17 -08006610{
6611 QCBORDecodeContext DCtx;
6612 int32_t nReturn;
6613 QCBORError uErr;
6614
6615 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spNestedCBOR), 0);
6616 QCBORDecode_EnterMap(&DCtx, NULL);
6617
6618 for(int j = 0; j < 5; j++) {
6619 for(int i = 0; i < 20; i++) {
6620 nReturn = DecodeNestedGetSubSub(&DCtx);
6621 if(nReturn) {
6622 return nReturn;
6623 }
6624 }
6625
6626 for(int i = 0; i < 20; i++) {
6627 nReturn = DecodeNestedGetEmpty(&DCtx);
6628 if(nReturn ) {
6629 return nReturn;
6630 }
6631 }
6632
6633 for(int i = 0; i < 20; i++) {
6634 nReturn = DecodeNestedGetZero(&DCtx);
6635 if(nReturn ) {
6636 return nReturn;
6637 }
6638 }
6639 }
6640
6641 QCBORDecode_ExitMap(&DCtx);
6642 uErr = QCBORDecode_Finish(&DCtx);
6643 if(uErr) {
6644 return (int32_t)uErr + 4100;
6645 }
6646
6647 return 0;
6648}
6649
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07006650
6651/*
Laurence Lundbladecc7da412020-12-27 00:09:07 -08006652 [
6653 23,
6654 6000,
6655 h'67616C6163746963',
6656 h'686176656E20746F6B656E'
6657 ]
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07006658 */
6659static const uint8_t spSimpleArray[] = {
Laurence Lundblade9b334962020-08-27 10:55:53 -07006660 0x84,
6661 0x17,
6662 0x19, 0x17, 0x70,
6663 0x48, 0x67, 0x61, 0x6C, 0x61, 0x63, 0x74, 0x69, 0x63,
6664 0x4B, 0x68, 0x61, 0x76, 0x65, 0x6E, 0x20, 0x74, 0x6F, 0x6B, 0x65, 0x6E};
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07006665
Laurence Lundbladecc7da412020-12-27 00:09:07 -08006666/* [h'', {}, [], 0] */
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08006667static const uint8_t spArrayOfEmpty[] = {0x84, 0x40, 0xa0, 0x80, 0x00};
6668
Laurence Lundbladecc7da412020-12-27 00:09:07 -08006669/* {} */
Laurence Lundbladee6f15112020-07-23 18:44:16 -07006670static const uint8_t spEmptyMap[] = {0xa0};
6671
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08006672#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundbladecc7da412020-12-27 00:09:07 -08006673/* {} */
Laurence Lundbladee6f15112020-07-23 18:44:16 -07006674static const uint8_t spEmptyInDefinteLengthMap[] = {0xbf, 0xff};
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07006675
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08006676
Laurence Lundbladef0499502020-08-01 11:55:57 -07006677/*
6678 {
6679 0: [],
6680 9: [
6681 [],
6682 []
6683 ],
6684 8: {
6685 1: [],
6686 2: {},
6687 3: []
6688 },
6689 4: {},
6690 5: [],
6691 6: [
6692 [],
6693 []
6694 ]
Laurence Lundbladecc7da412020-12-27 00:09:07 -08006695 }
Laurence Lundbladef0499502020-08-01 11:55:57 -07006696 */
6697static const uint8_t spMapOfEmpty[] = {
Laurence Lundbladecc7da412020-12-27 00:09:07 -08006698 0xa6, 0x00, 0x80, 0x09, 0x82, 0x80, 0x80, 0x08,
6699 0xa3, 0x01, 0x80, 0x02, 0xa0, 0x03, 0x80, 0x04,
6700 0xa0, 0x05, 0x9f, 0xff, 0x06, 0x9f, 0x80, 0x9f,
6701 0xff, 0xff};
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07006702
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08006703#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
6704
6705
Laurence Lundblade93d3f532020-09-28 21:09:12 -07006706/*
6707 Too many tags
Laurence Lundblade93d3f532020-09-28 21:09:12 -07006708 Duplicate label
6709 Integer overflow
6710 Date overflow
6711
Laurence Lundbladecc7da412020-12-27 00:09:07 -08006712 {
6713 1: 224(225(226(227(4(0))))),
Laurence Lundbladecc7da412020-12-27 00:09:07 -08006714 3: -18446744073709551616,
6715 4: 1(1.0e+300),
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07006716 5: 0,
6717 8: 8
Laurence Lundbladecc7da412020-12-27 00:09:07 -08006718 }
Laurence Lundblade93d3f532020-09-28 21:09:12 -07006719 */
6720static const uint8_t spRecoverableMapErrors[] = {
Laurence Lundblade37286c02022-09-03 10:05:02 -07006721#ifndef QCBOR_DISABLE_TAGS
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07006722 0xa6,
Laurence Lundblade93d3f532020-09-28 21:09:12 -07006723 0x04, 0xc1, 0xfb, 0x7e, 0x37, 0xe4, 0x3c, 0x88, 0x00, 0x75, 0x9c,
Laurence Lundblade37286c02022-09-03 10:05:02 -07006724 0x01, 0xd8, 0xe0, 0xd8, 0xe1, 0xd8, 0xe2, 0xd8, 0xe3, 0xd8, 0x04, 0x00,
6725#else
6726 0xa4,
6727#endif
6728 0x03, 0x3b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
Laurence Lundblade93d3f532020-09-28 21:09:12 -07006729 0x05, 0x00,
6730 0x05, 0x00,
6731 0x08, 0x08,
Laurence Lundblade93d3f532020-09-28 21:09:12 -07006732};
6733
Laurence Lundbladecc7da412020-12-27 00:09:07 -08006734/* Bad break */
Laurence Lundblade93d3f532020-09-28 21:09:12 -07006735static const uint8_t spUnRecoverableMapError1[] = {
6736 0xa2, 0xff, 0x01, 0x00, 0x02, 0x00
6737};
6738
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08006739#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundbladecc7da412020-12-27 00:09:07 -08006740/* No more items */
Laurence Lundblade93d3f532020-09-28 21:09:12 -07006741static const uint8_t spUnRecoverableMapError2[] = {
6742 0xbf, 0x02, 0xbf, 0xff, 0x01, 0x00, 0x02, 0x00
6743};
6744
Laurence Lundbladecc7da412020-12-27 00:09:07 -08006745/* Hit end because string is too long */
Laurence Lundblade93d3f532020-09-28 21:09:12 -07006746static const uint8_t spUnRecoverableMapError3[] = {
6747 0xbf, 0x02, 0x69, 0x64, 0x64, 0xff
6748};
6749
Laurence Lundbladecc7da412020-12-27 00:09:07 -08006750/* Hit end because string is too long */
Laurence Lundblade93d3f532020-09-28 21:09:12 -07006751static const uint8_t spUnRecoverableMapError4[] = {
6752 0xbf,
6753 0x02, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f,
6754 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f,
6755 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
6756 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
6757 0xff
6758};
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08006759#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
Laurence Lundblade93d3f532020-09-28 21:09:12 -07006760
Laurence Lundblade63926052021-03-29 16:05:51 -07006761const unsigned char not_well_formed_submod_section[] = {
6762 0xa1, 0x14, 0x1f,
6763};
Laurence Lundblade93d3f532020-09-28 21:09:12 -07006764
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07006765
6766/* Array of length 3, but only two items. */
6767const unsigned char spBadConsumeInput[] = {
6768 0x83, 0x00, 0x00
6769};
6770
6771/* Tag nesting too deep. */
6772const unsigned char spBadConsumeInput2[] = {
6773 0x81,
6774 0xD8, 0x37,
6775 0xD8, 0x2C,
6776 0xD8, 0x21,
6777 0xD6,
6778 0xCB,
6779 00
6780};
6781
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07006782
6783const unsigned char spBadConsumeInput4[] = {
6784 0x81, 0x9f, 0x00, 0xff
6785};
6786
6787const unsigned char spBadConsumeInput5[] = {
6788 0xa1, 0x80, 0x00
6789};
6790
Laurence Lundbladec5f45e42023-12-18 09:23:20 -07006791/*
6792 Lots of nesting for various nesting tests.
6793 { 1:1,
6794 2:{
6795 21:21,
6796 22:{
6797 221:[2111, 2112, 2113],
6798 222:222,
6799 223: {}
6800 },
6801 23: 23
6802 },
6803 3:3,
6804 4: [ {} ]
6805 }
6806 */
6807static const uint8_t spNested[] = {
68080xA4, /* Map of 4 */
6809 0x01, 0x01, /* Map entry 1 : 1 */
6810 0x02, 0xA3, /* Map entry 2 : {, an array of 3 */
6811 0x15, 0x15, /* Map entry 21 : 21 */
6812 0x16, 0xA3, /* Map entry 22 : {, a map of 3 */
6813 0x18, 0xDD, 0x83, /* Map entry 221 : [ an array of 3 */
6814 0x19, 0x08, 0x3F, /* Array item 2111 */
6815 0x19, 0x08, 0x40, /* Array item 2112 */
6816 0x19, 0x08, 0x41, /* Array item 2113 */
6817 0x18, 0xDE, 0x18, 0xDE, /* Map entry 222 : 222 */
6818 0x18, 0xDF, 0xA0, /* Map entry 223 : {} */
6819 0x17, 0x17, /* Map entry 23 : 23 */
6820 0x03, 0x03, /* Map entry 3 : 3 */
6821 0x04, 0x81, /* Map entry 4: [, an array of 1 */
6822 0xA0 /* Array entry {}, an empty map */
6823};
6824
6825
6826static int32_t EnterMapCursorTest(void)
6827{
6828 QCBORDecodeContext DCtx;
6829 QCBORItem Item1;
Laurence Lundblade11654912024-05-09 11:49:24 -07006830 int64_t nInt;
6831 QCBORError uErr;
6832
6833 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spNested), 0);
6834 QCBORDecode_EnterMap(&DCtx, NULL);
6835 QCBORDecode_GetInt64InMapN (&DCtx, 3, &nInt);
6836 uErr = QCBORDecode_GetNext(&DCtx, &Item1);
Laurence Lundbladea29f45a2024-05-14 15:55:19 -07006837 if(uErr != QCBOR_SUCCESS) {
6838 return 701;
6839 }
Laurence Lundblade11654912024-05-09 11:49:24 -07006840 if(Item1.uDataType != QCBOR_TYPE_INT64) {
6841 return 700;
6842 }
6843
Laurence Lundbladec5f45e42023-12-18 09:23:20 -07006844
6845 int i;
6846 for(i = 0; i < 13; i++) {
6847 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spNested), 0);
6848 QCBORDecode_EnterMap(&DCtx, NULL);
6849 int j;
6850 /* Move travesal cursor */
6851 for(j = 0; j < i; j++) {
6852 QCBORDecode_GetNext(&DCtx, &Item1);
6853 }
6854 QCBORDecode_EnterMapFromMapN(&DCtx, 2);
6855 QCBORDecode_ExitMap(&DCtx);
6856 QCBORDecode_GetNext(&DCtx, &Item1);
6857 if(Item1.label.int64 != 3) {
6858 return 8000;
6859 }
6860 }
6861
6862 for(i = 0; i < 13; i++) {
6863 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spNested), 0);
6864 QCBORDecode_EnterMap(&DCtx, NULL);
6865 int j;
6866 /* Move travesal cursor */
6867 for(j = 0; j < i; j++) {
6868 QCBORDecode_GetNext(&DCtx, &Item1);
6869 }
6870 QCBORDecode_EnterMapFromMapN(&DCtx, 2);
6871 QCBORDecode_EnterMapFromMapN(&DCtx, 22);
6872 QCBORDecode_ExitMap(&DCtx);
6873 QCBORDecode_GetNext(&DCtx, &Item1);
6874 if(Item1.label.int64 != 23) {
6875 return 8000;
6876 }
6877 }
6878
6879 for(i = 0; i < 13; i++) {
6880 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spNested), 0);
6881 QCBORDecode_EnterMap(&DCtx, NULL);
6882 int j;
6883 /* Move travesal cursor */
6884 for(j = 0; j < i; j++) {
6885 QCBORDecode_GetNext(&DCtx, &Item1);
6886 }
6887 QCBORDecode_EnterMapFromMapN(&DCtx, 2);
6888 QCBORDecode_EnterMapFromMapN(&DCtx, 22);
6889 for(j = 0; j < i; j++) {
6890 QCBORDecode_GetNext(&DCtx, &Item1);
6891 }
6892 QCBORDecode_EnterArrayFromMapN(&DCtx, 221);
6893 QCBORDecode_ExitArray(&DCtx);
6894 QCBORDecode_ExitMap(&DCtx);
6895 QCBORDecode_GetNext(&DCtx, &Item1);
6896 if(Item1.label.int64 != 23) {
6897 return 8000;
6898 }
6899 QCBORDecode_ExitMap(&DCtx);
6900 QCBORDecode_GetNext(&DCtx, &Item1);
6901 if(Item1.label.int64 != 3) {
6902 return 8000;
6903 }
6904 }
6905
6906 return 0;
6907}
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07006908
6909
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03006910int32_t EnterMapTest(void)
Laurence Lundbladebb87be22020-04-09 19:15:32 -07006911{
Laurence Lundbladef0499502020-08-01 11:55:57 -07006912 QCBORItem Item1;
Laurence Lundbladebb87be22020-04-09 19:15:32 -07006913 QCBORDecodeContext DCtx;
Laurence Lundbladef0499502020-08-01 11:55:57 -07006914 int32_t nReturn;
6915 QCBORError uErr;
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07006916
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08006917#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07006918 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spMapOfEmpty), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006919 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07006920
Laurence Lundbladef0499502020-08-01 11:55:57 -07006921
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006922 QCBORDecode_EnterArray(&DCtx, NULL); // Label 0
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07006923 QCBORDecode_ExitArray(&DCtx);
6924
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006925 QCBORDecode_EnterArray(&DCtx, NULL); // Label 9
6926 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07006927 QCBORDecode_ExitArray(&DCtx);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006928 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07006929 QCBORDecode_ExitArray(&DCtx);
6930 QCBORDecode_ExitArray(&DCtx);
6931
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006932 QCBORDecode_EnterMap(&DCtx, NULL); // Label 8
6933 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07006934 QCBORDecode_ExitArray(&DCtx);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006935 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07006936 QCBORDecode_ExitMap(&DCtx);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006937 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07006938 QCBORDecode_ExitArray(&DCtx);
6939 QCBORDecode_ExitMap(&DCtx);
6940
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006941 QCBORDecode_EnterMap(&DCtx, NULL); // Label4
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07006942 QCBORDecode_ExitMap(&DCtx);
6943
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006944 QCBORDecode_EnterArray(&DCtx, NULL); // Label 5
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07006945 QCBORDecode_ExitArray(&DCtx);
6946
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006947 QCBORDecode_EnterArray(&DCtx, NULL); // Label 6
6948 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07006949 QCBORDecode_ExitArray(&DCtx);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006950 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07006951 QCBORDecode_ExitArray(&DCtx);
6952 QCBORDecode_ExitArray(&DCtx);
6953
6954 QCBORDecode_ExitMap(&DCtx);
6955
6956 uErr = QCBORDecode_Finish(&DCtx);
6957 if(uErr != QCBOR_SUCCESS){
6958 return 3011;
6959 }
6960
Laurence Lundbladeec290b82024-06-10 11:10:54 -07006961#ifndef QCBOR_DISABLE_NON_INTEGER_LABELS
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07006962 (void)pValidMapIndefEncoded;
Laurence Lundbladef7a70bc2020-10-24 12:23:25 -07006963 nReturn = SpiffyDecodeBasicMap(UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapIndefEncoded));
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07006964 if(nReturn) {
6965 return nReturn + 20000;
Laurence Lundbladebb87be22020-04-09 19:15:32 -07006966 }
Laurence Lundbladeec290b82024-06-10 11:10:54 -07006967#endif /* ! QCBOR_DISABLE_NON_INTEGER_LABELS */
6968#endif /* ! QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08006969
Laurence Lundbladeec290b82024-06-10 11:10:54 -07006970#ifndef QCBOR_DISABLE_NON_INTEGER_LABELS
6971 QCBORItem ArrayItem;
Laurence Lundbladebb87be22020-04-09 19:15:32 -07006972
Laurence Lundbladef7a70bc2020-10-24 12:23:25 -07006973 nReturn = SpiffyDecodeBasicMap(UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded));
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006974 if(nReturn) {
6975 return nReturn;
6976 }
Laurence Lundbladebb87be22020-04-09 19:15:32 -07006977
Laurence Lundblade8ffdb742020-05-07 02:49:18 -07006978
Laurence Lundblade937ea812020-05-08 11:38:23 -07006979
Laurence Lundblade2f467f92020-10-09 17:50:11 -07006980 // These tests confirm the cursor is at the right place after entering
6981 // a map or array
Laurence Lundblade9b334962020-08-27 10:55:53 -07006982 const UsefulBufC ValidEncodedMap = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded);
Laurence Lundblade937ea812020-05-08 11:38:23 -07006983
6984 // Confirm cursor is at right place
Laurence Lundblade9b334962020-08-27 10:55:53 -07006985 QCBORDecode_Init(&DCtx, ValidEncodedMap, 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006986 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade937ea812020-05-08 11:38:23 -07006987 QCBORDecode_GetNext(&DCtx, &Item1);
6988 if(Item1.uDataType != QCBOR_TYPE_INT64) {
6989 return 2001;
6990 }
6991
6992
Laurence Lundblade9b334962020-08-27 10:55:53 -07006993 QCBORDecode_Init(&DCtx, ValidEncodedMap, 0);
Laurence Lundblade6f3f78e2020-08-31 13:09:14 -07006994 QCBORDecode_VGetNext(&DCtx, &Item1);
6995 QCBORDecode_VGetNext(&DCtx, &Item1);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006996 QCBORDecode_EnterArray(&DCtx, &ArrayItem);
6997 if(ArrayItem.uLabelType != QCBOR_TYPE_TEXT_STRING ||
6998 UsefulBuf_Compare(ArrayItem.label.string,
6999 UsefulBuf_FROM_SZ_LITERAL("an array of two strings"))) {
7000 return 2051;
7001 }
Laurence Lundblade937ea812020-05-08 11:38:23 -07007002 QCBORDecode_GetNext(&DCtx, &Item1);
7003 if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING) {
7004 return 2002;
7005 }
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07007006 QCBORDecode_ExitArray(&DCtx);
7007 QCBORDecode_EnterMap(&DCtx, &ArrayItem);
7008 if(ArrayItem.uLabelType != QCBOR_TYPE_TEXT_STRING ||
7009 UsefulBuf_Compare(ArrayItem.label.string,
7010 UsefulBuf_FROM_SZ_LITERAL("map in a map"))) {
7011 return 2052;
7012 }
7013
Laurence Lundblade937ea812020-05-08 11:38:23 -07007014
Laurence Lundblade9b334962020-08-27 10:55:53 -07007015 QCBORDecode_Init(&DCtx, ValidEncodedMap, 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07007016 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade64b607e2020-05-13 13:05:57 -07007017 QCBORDecode_GetNext(&DCtx, &Item1);
7018 QCBORDecode_GetNext(&DCtx, &Item1);
7019 QCBORDecode_GetNext(&DCtx, &Item1);
Laurence Lundblade937ea812020-05-08 11:38:23 -07007020 QCBORDecode_EnterMapFromMapSZ(&DCtx, "map in a map");
7021 QCBORDecode_GetNext(&DCtx, &Item1);
7022 if(Item1.uDataType != QCBOR_TYPE_BYTE_STRING) {
7023 return 2003;
7024 }
7025
Laurence Lundblade9b334962020-08-27 10:55:53 -07007026 QCBORDecode_Init(&DCtx, ValidEncodedMap, 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07007027 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade937ea812020-05-08 11:38:23 -07007028 QCBORDecode_GetNext(&DCtx, &Item1);
7029 QCBORDecode_GetNext(&DCtx, &Item1);
7030 QCBORDecode_GetNext(&DCtx, &Item1);
7031 QCBORDecode_GetNext(&DCtx, &Item1);
7032 QCBORDecode_GetNext(&DCtx, &Item1);
7033 QCBORDecode_GetNext(&DCtx, &Item1);
7034 QCBORDecode_GetNext(&DCtx, &Item1);
7035 QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings");
7036 QCBORDecode_GetNext(&DCtx, &Item1);
7037 if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING) {
Laurence Lundblade64b607e2020-05-13 13:05:57 -07007038 return 2004;
Laurence Lundblade937ea812020-05-08 11:38:23 -07007039 }
7040
Laurence Lundblade9b334962020-08-27 10:55:53 -07007041 QCBORDecode_Init(&DCtx, ValidEncodedMap, 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07007042 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade2b843b52020-06-16 20:51:03 -07007043 QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings");
7044 QCBORDecode_ExitArray(&DCtx);
7045 QCBORDecode_GetNext(&DCtx, &Item1);
7046 if(Item1.uDataType != QCBOR_TYPE_MAP && Item1.uLabelAlloc != QCBOR_TYPE_TEXT_STRING) {
7047 return 2006;
7048 }
7049 QCBORDecode_ExitMap(&DCtx);
7050 if(QCBORDecode_GetNext(&DCtx, &Item1) != QCBOR_ERR_NO_MORE_ITEMS) {
7051 return 2007;
7052 }
Laurence Lundbladeec290b82024-06-10 11:10:54 -07007053#endif /* !QCBOR_DISABLE_NON_INTEGER_LABELS */
Laurence Lundblade2b843b52020-06-16 20:51:03 -07007054
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07007055 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSimpleArray), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07007056 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07007057 int64_t nDecodedInt2;
Laurence Lundbladeec290b82024-06-10 11:10:54 -07007058
7059 UsefulBufC String;
7060 QCBORDecode_GetTextStringInMapN(&DCtx, 88, &String);
7061 uErr = QCBORDecode_GetAndResetError(&DCtx);
7062 if(uErr != QCBOR_ERR_MAP_NOT_ENTERED){
7063 return 2009;
7064 }
7065#ifndef QCBOR_DISABLE_NON_INTEGER_LABELS
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07007066 QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2);
7067 uErr = QCBORDecode_GetAndResetError(&DCtx);
7068 if(uErr != QCBOR_ERR_MAP_NOT_ENTERED){
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07007069 return 2008;
7070 }
Laurence Lundbladeec290b82024-06-10 11:10:54 -07007071#endif /* ! QCBOR_DISABLE_NON_INTEGER_LABELS */
Laurence Lundblade937ea812020-05-08 11:38:23 -07007072
Laurence Lundbladee6f15112020-07-23 18:44:16 -07007073
7074 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEmptyMap), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07007075 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundbladee6f15112020-07-23 18:44:16 -07007076 // This will fail because the map is empty.
7077 QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2);
7078 uErr = QCBORDecode_GetAndResetError(&DCtx);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07007079 if(uErr != QCBOR_ERR_LABEL_NOT_FOUND){
Laurence Lundbladee6f15112020-07-23 18:44:16 -07007080 return 2010;
7081 }
7082 QCBORDecode_ExitMap(&DCtx);
7083 uErr = QCBORDecode_Finish(&DCtx);
7084 if(uErr != QCBOR_SUCCESS){
7085 return 2011;
7086 }
7087
7088
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08007089#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundbladee6f15112020-07-23 18:44:16 -07007090 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEmptyInDefinteLengthMap), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07007091 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundbladee6f15112020-07-23 18:44:16 -07007092 // This will fail because the map is empty.
7093 QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2);
7094 uErr = QCBORDecode_GetAndResetError(&DCtx);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07007095 if(uErr != QCBOR_ERR_LABEL_NOT_FOUND){
Laurence Lundblade085d7952020-07-24 10:26:30 -07007096 return 2012;
Laurence Lundbladee6f15112020-07-23 18:44:16 -07007097 }
7098 QCBORDecode_ExitMap(&DCtx);
7099 uErr = QCBORDecode_Finish(&DCtx);
7100 if(uErr != QCBOR_SUCCESS){
Laurence Lundblade085d7952020-07-24 10:26:30 -07007101 return 2013;
Laurence Lundbladee6f15112020-07-23 18:44:16 -07007102 }
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08007103#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
Laurence Lundbladee6f15112020-07-23 18:44:16 -07007104
Laurence Lundblade5f4e8712020-07-25 11:44:43 -07007105
7106 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spArrayOfEmpty), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07007107 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade323f8a92020-09-06 19:43:09 -07007108 QCBORDecode_GetByteString(&DCtx, &String);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07007109 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade5f4e8712020-07-25 11:44:43 -07007110 QCBORDecode_ExitMap(&DCtx);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07007111 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade5f4e8712020-07-25 11:44:43 -07007112 QCBORDecode_ExitArray(&DCtx);
7113 QCBORDecode_GetInt64(&DCtx, &nDecodedInt2);
7114 QCBORDecode_ExitArray(&DCtx);
7115 uErr = QCBORDecode_Finish(&DCtx);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07007116 if(uErr != QCBOR_SUCCESS) {
Laurence Lundblade5f4e8712020-07-25 11:44:43 -07007117 return 2014;
7118 }
7119
Laurence Lundblade93d3f532020-09-28 21:09:12 -07007120 int64_t nInt;
7121 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spRecoverableMapErrors), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07007122 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade37286c02022-09-03 10:05:02 -07007123#ifndef QCBOR_DISABLE_TAGS
Laurence Lundblade93d3f532020-09-28 21:09:12 -07007124 QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt);
Laurence Lundblade88e9db22020-11-02 03:56:33 -08007125 uErr = QCBORDecode_GetError(&DCtx);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07007126 if(uErr != QCBOR_ERR_TOO_MANY_TAGS) {
7127 return 2021;
7128 }
Laurence Lundblade88e9db22020-11-02 03:56:33 -08007129 if(QCBORDecode_GetNthTagOfLast(&DCtx, 0) != CBOR_TAG_INVALID64) {
7130 return 2121;
7131 }
7132 (void)QCBORDecode_GetAndResetError(&DCtx);
Laurence Lundblade37286c02022-09-03 10:05:02 -07007133#endif
Laurence Lundblade88e9db22020-11-02 03:56:33 -08007134
Laurence Lundblade93d3f532020-09-28 21:09:12 -07007135
Laurence Lundblade93d3f532020-09-28 21:09:12 -07007136 QCBORDecode_GetInt64InMapN(&DCtx, 0x03, &nInt);
7137 uErr = QCBORDecode_GetAndResetError(&DCtx);
7138 if(uErr != QCBOR_ERR_INT_OVERFLOW) {
7139 return 2023;
7140 }
7141
Laurence Lundblade37286c02022-09-03 10:05:02 -07007142#ifndef QCBOR_DISABLE_TAGS
Laurence Lundblade93d3f532020-09-28 21:09:12 -07007143 QCBORDecode_GetEpochDateInMapN(&DCtx, 0x04, QCBOR_TAG_REQUIREMENT_TAG, &nInt);
7144 uErr = QCBORDecode_GetAndResetError(&DCtx);
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007145 if(uErr != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_DATE_OVERFLOW)) {
Laurence Lundblade93d3f532020-09-28 21:09:12 -07007146 return 2024;
7147 }
Laurence Lundblade37286c02022-09-03 10:05:02 -07007148#endif
Laurence Lundblade93d3f532020-09-28 21:09:12 -07007149
7150 QCBORDecode_GetInt64InMapN(&DCtx, 0x05, &nInt);
7151 uErr = QCBORDecode_GetAndResetError(&DCtx);
7152 if(uErr != QCBOR_ERR_DUPLICATE_LABEL) {
7153 return 2025;
7154 }
7155
7156 QCBORDecode_GetInt64InMapN(&DCtx, 0x08, &nInt);
7157
7158 QCBORDecode_ExitMap(&DCtx);
7159 uErr = QCBORDecode_Finish(&DCtx);
7160 if(uErr != QCBOR_SUCCESS) {
7161 return 2026;
7162 }
7163
7164 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUnRecoverableMapError1), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07007165 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07007166 QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt);
7167 uErr = QCBORDecode_GetAndResetError(&DCtx);
7168 if(uErr != QCBOR_ERR_BAD_BREAK) {
7169 return 2030;
7170 }
7171
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08007172#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundblade93d3f532020-09-28 21:09:12 -07007173 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUnRecoverableMapError2), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07007174 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07007175 QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt);
7176 uErr = QCBORDecode_GetAndResetError(&DCtx);
7177 if(uErr != QCBOR_ERR_NO_MORE_ITEMS) {
7178 return 2031;
7179 }
7180
7181 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUnRecoverableMapError3), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07007182 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07007183 QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt);
7184 uErr = QCBORDecode_GetAndResetError(&DCtx);
7185 if(uErr != QCBOR_ERR_HIT_END) {
7186 return 2032;
7187 }
7188
7189 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUnRecoverableMapError4), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07007190 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07007191 QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt);
7192 uErr = QCBORDecode_GetAndResetError(&DCtx);
7193 if(uErr != QCBOR_ERR_ARRAY_DECODE_NESTING_TOO_DEEP) {
7194 return 2033;
7195 }
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08007196#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
7197
Laurence Lundbladeec290b82024-06-10 11:10:54 -07007198#ifndef QCBOR_DISABLE_NON_INTEGER_LABELS
Laurence Lundblade732e52d2021-02-22 20:11:01 -07007199 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0);
7200 QCBORDecode_VGetNextConsume(&DCtx, &Item1);
7201 if(Item1.uDataType != QCBOR_TYPE_MAP) {
7202 return 2401;
7203 }
7204 if(QCBORDecode_GetError(&DCtx)) {
7205 return 2402;
7206 }
7207
7208 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0);
7209 QCBORDecode_VGetNext(&DCtx, &Item1);
7210 if(Item1.uDataType != QCBOR_TYPE_MAP ||
7211 Item1.val.uCount != 3 ||
7212 Item1.uNextNestLevel != 1) {
7213 return 2403;
7214 }
7215 if(QCBORDecode_GetError(&DCtx)) {
7216 return 2404;
7217 }
7218 QCBORDecode_VGetNextConsume(&DCtx, &Item1);
7219 if(Item1.uDataType != QCBOR_TYPE_INT64 ||
7220 Item1.uNextNestLevel != 1 ||
7221 Item1.val.int64 != 42) {
7222 return 2405;
7223 }
7224 if(QCBORDecode_GetError(&DCtx)) {
7225 return 2406;
7226 }
7227 QCBORDecode_VGetNextConsume(&DCtx, &Item1);
7228 if(Item1.uDataType != QCBOR_TYPE_ARRAY ||
7229 Item1.uNestingLevel != 1 ||
7230 Item1.uNextNestLevel != 1 ||
7231 Item1.val.uCount != 2) {
7232 return 2407;
7233 }
7234 if(QCBORDecode_GetError(&DCtx)) {
7235 return 2408;
7236 }
7237 QCBORDecode_VGetNextConsume(&DCtx, &Item1);
7238 if(Item1.uDataType != QCBOR_TYPE_MAP ||
7239 Item1.uNestingLevel != 1 ||
7240 Item1.uNextNestLevel != 0 ||
7241 Item1.val.uCount != 4) {
7242 return 2409;
7243 }
7244 if(QCBORDecode_GetError(&DCtx)) {
7245 return 2410;
7246 }
Laurence Lundbladeec290b82024-06-10 11:10:54 -07007247#endif /* ! QCBOR_DISABLE_NON_INTEGER_LABELS */
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07007248
Laurence Lundbladeddebabe2020-11-22 11:32:17 -08007249 nReturn = DecodeNestedIterate();
7250
Laurence Lundblade63926052021-03-29 16:05:51 -07007251
7252 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(not_well_formed_submod_section), 0);
7253 QCBORDecode_EnterMap(&DCtx, NULL);
7254 QCBORDecode_EnterMapFromMapN(&DCtx, 20);
7255 if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_BAD_INT) {
7256 return 2500;
7257 }
7258
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07007259 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBadConsumeInput), 0);
7260 QCBORDecode_VGetNextConsume(&DCtx, &Item1);
7261 if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_NO_MORE_ITEMS) {
7262 return 2600;
7263 }
7264
Laurence Lundblade37286c02022-09-03 10:05:02 -07007265#ifndef QCBOR_DISABLE_TAGS
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07007266 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBadConsumeInput2), 0);
7267 QCBORDecode_VGetNextConsume(&DCtx, &Item1);
7268 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) {
7269 return 2700;
7270 }
Laurence Lundblade37286c02022-09-03 10:05:02 -07007271#endif
7272
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07007273
7274 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBadConsumeInput4), 0);
7275 QCBORDecode_VGetNextConsume(&DCtx, &Item1);
7276#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
7277 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) {
7278 return 2900;
7279 }
7280#else
7281 if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_INDEF_LEN_ARRAYS_DISABLED) {
7282 return 2901;
7283 }
7284#endif
7285
7286 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBadConsumeInput5), 0);
7287 QCBORDecode_VGetNextConsume(&DCtx, &Item1);
7288 if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_MAP_LABEL_TYPE) {
7289 return 3000;
7290 }
7291
Laurence Lundbladec5f45e42023-12-18 09:23:20 -07007292 nReturn = EnterMapCursorTest();
7293
Laurence Lundbladeddebabe2020-11-22 11:32:17 -08007294 return nReturn;
Laurence Lundblade9c905e82020-04-25 11:31:38 -07007295}
7296
7297
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007298struct NumberConversion {
7299 char *szDescription;
7300 UsefulBufC CBOR;
7301 int64_t nConvertedToInt64;
7302 QCBORError uErrorInt64;
7303 uint64_t uConvertToUInt64;
7304 QCBORError uErrorUint64;
7305 double dConvertToDouble;
7306 QCBORError uErrorDouble;
7307};
7308
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007309#ifndef QCBOR_DISABLE_EXP_AND_MANTISSA
7310#define EXP_AND_MANTISSA_ERROR(x) x
7311#else
7312#define EXP_AND_MANTISSA_ERROR(x) QCBOR_ERR_UNEXPECTED_TYPE
7313#endif
7314
7315
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07007316static const struct NumberConversion NumberConversions[] = {
Laurence Lundblade37286c02022-09-03 10:05:02 -07007317#ifndef QCBOR_DISABLE_TAGS
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007318 {
Laurence Lundblade4e808ba2022-12-29 12:45:20 -07007319 "Big float: INT64_MIN * 2e-1 to test handling of INT64_MIN",
7320 {(uint8_t[]){0xC5, 0x82, 0x20,
7321 0x3B, 0x7f, 0xff, 0xff, 0xff, 0xff, 0x0ff, 0xff, 0xff,
7322 }, 15},
7323 -4611686018427387904, /* INT64_MIN / 2 */
7324 EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS),
7325 0,
7326 EXP_AND_MANTISSA_ERROR(QCBOR_ERR_NUMBER_SIGN_CONVERSION),
7327 -4.6116860184273879E+18,
7328 FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS))
7329 },
7330 {
Laurence Lundblade784b54b2020-08-10 01:24:52 -07007331 "too large to fit into int64_t",
7332 {(uint8_t[]){0xc3, 0x48, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 10},
7333 0,
7334 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
7335 0,
7336 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
7337 ((double)INT64_MIN) + 1 ,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007338 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)
Laurence Lundblade784b54b2020-08-10 01:24:52 -07007339 },
7340 {
7341 "largest negative int that fits in int64_t",
7342 {(uint8_t[]){0xc3, 0x48, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, 10},
7343 INT64_MIN,
7344 QCBOR_SUCCESS,
7345 0,
7346 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
7347 (double)INT64_MIN,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007348 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)
Laurence Lundblade784b54b2020-08-10 01:24:52 -07007349 },
7350 {
Laurence Lundbladeda095972020-06-06 18:35:33 -07007351 "negative bignum -1",
7352 {(uint8_t[]){0xc3, 0x41, 0x00}, 3},
7353 -1,
7354 QCBOR_SUCCESS,
7355 0,
7356 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
7357 -1.0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007358 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)
Laurence Lundbladeda095972020-06-06 18:35:33 -07007359 },
7360 {
Laurence Lundblade9ab5abb2020-05-20 12:10:45 -07007361 "Decimal Fraction with positive bignum 257 * 10e3",
Laurence Lundblade887add82020-05-17 05:50:34 -07007362 {(uint8_t[]){0xC4, 0x82, 0x1B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03,
7363 0xC2, 0x42, 0x01, 0x01}, 15},
7364 257000,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007365 EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS),
Laurence Lundblade887add82020-05-17 05:50:34 -07007366 257000,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007367 EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS),
Laurence Lundblade887add82020-05-17 05:50:34 -07007368 257000.0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007369 FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS))
Laurence Lundblade887add82020-05-17 05:50:34 -07007370 },
7371 {
Laurence Lundbladeda095972020-06-06 18:35:33 -07007372 "bigfloat with negative bignum -258 * 2e3",
Laurence Lundblade887add82020-05-17 05:50:34 -07007373 {(uint8_t[]){0xC5, 0x82, 0x1B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03,
7374 0xC3, 0x42, 0x01, 0x01}, 15},
Laurence Lundbladeda095972020-06-06 18:35:33 -07007375 -2064,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007376 EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS),
Laurence Lundblade887add82020-05-17 05:50:34 -07007377 0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007378 EXP_AND_MANTISSA_ERROR(QCBOR_ERR_NUMBER_SIGN_CONVERSION),
Laurence Lundbladeda095972020-06-06 18:35:33 -07007379 -2064.0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007380 FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS))
Laurence Lundblade887add82020-05-17 05:50:34 -07007381 },
7382 {
Laurence Lundblade9ab5abb2020-05-20 12:10:45 -07007383 "bigfloat with positive bignum 257 * 2e3",
Laurence Lundblade887add82020-05-17 05:50:34 -07007384 {(uint8_t[]){0xC5, 0x82, 0x1B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03,
7385 0xC2, 0x42, 0x01, 0x01}, 15},
7386 2056,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007387 EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS),
Laurence Lundblade887add82020-05-17 05:50:34 -07007388 2056,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007389 EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS),
Laurence Lundblade887add82020-05-17 05:50:34 -07007390 2056.0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007391 FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS))
Laurence Lundblade887add82020-05-17 05:50:34 -07007392 },
7393 {
Laurence Lundbladeda095972020-06-06 18:35:33 -07007394 "negative bignum 0xc349010000000000000000 -18446744073709551617",
Laurence Lundblade887add82020-05-17 05:50:34 -07007395 {(uint8_t[]){0xc3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 11},
7396 0,
7397 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
7398 0,
7399 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
7400 -18446744073709551617.0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007401 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)
Laurence Lundblade887add82020-05-17 05:50:34 -07007402 },
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08007403#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
Laurence Lundblade887add82020-05-17 05:50:34 -07007404 {
Laurence Lundblade313b2862020-05-16 01:23:06 -07007405 "Positive bignum 0x01020304 indefinite length string",
7406 {(uint8_t[]){0xC2, 0x5f, 0x42, 0x01, 0x02, 0x41, 0x03, 0x41, 0x04, 0xff}, 10},
7407 0x01020304,
7408 QCBOR_SUCCESS,
7409 0x01020304,
7410 QCBOR_SUCCESS,
7411 16909060.0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007412 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)
Laurence Lundblade313b2862020-05-16 01:23:06 -07007413 },
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08007414#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
Laurence Lundblade313b2862020-05-16 01:23:06 -07007415 {
Laurence Lundblade887add82020-05-17 05:50:34 -07007416 "Decimal Fraction with neg bignum [9223372036854775807, -4759477275222530853137]",
Laurence Lundblade313b2862020-05-16 01:23:06 -07007417 {(uint8_t[]){0xC4, 0x82, 0x1B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
7418 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,}, 23},
7419 0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007420 EXP_AND_MANTISSA_ERROR(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW),
Laurence Lundblade313b2862020-05-16 01:23:06 -07007421 0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007422 EXP_AND_MANTISSA_ERROR(QCBOR_ERR_NUMBER_SIGN_CONVERSION),
Laurence Lundblade313b2862020-05-16 01:23:06 -07007423 -INFINITY,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007424 FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS))
Laurence Lundblade313b2862020-05-16 01:23:06 -07007425 },
7426 {
7427 "big float [9223372036854775806, 9223372036854775806]",
7428 {(uint8_t[]){0xC5, 0x82, 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE,
7429 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE}, 20},
7430 0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007431 EXP_AND_MANTISSA_ERROR(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW),
Laurence Lundblade313b2862020-05-16 01:23:06 -07007432 0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007433 EXP_AND_MANTISSA_ERROR(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW),
Laurence Lundblade313b2862020-05-16 01:23:06 -07007434 INFINITY,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007435 FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS))
Laurence Lundblade313b2862020-05-16 01:23:06 -07007436 },
7437 {
Laurence Lundblade983500d2020-05-14 11:49:34 -07007438 "Big float 3 * 2^^2",
7439 {(uint8_t[]){0xC5, 0x82, 0x02, 0x03}, 4},
7440 12,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007441 EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS),
Laurence Lundblade983500d2020-05-14 11:49:34 -07007442 12,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007443 EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS),
Laurence Lundblade983500d2020-05-14 11:49:34 -07007444 12.0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007445 FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS))
Laurence Lundblade983500d2020-05-14 11:49:34 -07007446 },
Laurence Lundblade983500d2020-05-14 11:49:34 -07007447 {
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007448 "Decimal fraction 3/10",
7449 {(uint8_t[]){0xC4, 0x82, 0x20, 0x03}, 4},
7450 0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007451 EXP_AND_MANTISSA_ERROR(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW),
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007452 0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007453 EXP_AND_MANTISSA_ERROR(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW),
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007454 0.30000000000000004,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007455 FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS))
Laurence Lundbladedfd49fc2020-09-01 14:17:16 -07007456 },
7457 {
Laurence Lundblade11fd78b2020-09-01 22:13:27 -07007458 "extreme pos bignum",
7459 {(uint8_t[]){0xc2, 0x59, 0x01, 0x90,
7460 // 50 rows of 8 is 400 digits.
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,
7491 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7492 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7493 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7494 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7495 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7496 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7497 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7498 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7499 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7500 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7501 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7502 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7503 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7504 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7505 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7506 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7507 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7508 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7509 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
gtravisb787e82a2023-11-30 18:38:21 -08007510 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0},
Laurence Lundblade11fd78b2020-09-01 22:13:27 -07007511 404},
7512 0,
7513 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
7514 0,
7515 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
Laurence Lundblade11fd78b2020-09-01 22:13:27 -07007516 INFINITY,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007517 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS),
Laurence Lundblade11fd78b2020-09-01 22:13:27 -07007518 },
7519
7520 {
7521 "extreme neg bignum",
7522 {(uint8_t[]){0xc3, 0x59, 0x01, 0x90,
7523 // 50 rows of 8 is 400 digits.
7524 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7525 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7526 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7527 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7528 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7529 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7530 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7531 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7532 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7533 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7534 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7535 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7536 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7537 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7538 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7539 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7540 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7541 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7542 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7543 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7544 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7545 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7546 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7547 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7548 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7549 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7550 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7551 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7552 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7553 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7554 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7555 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7556 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7557 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7558 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7559 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7560 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7561 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7562 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7563 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7564 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7565 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7566 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7567 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7568 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7569 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7570 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7571 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7572 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
gtravisb787e82a2023-11-30 18:38:21 -08007573 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0},
Laurence Lundblade11fd78b2020-09-01 22:13:27 -07007574 404},
7575 0,
7576 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
7577 0,
7578 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
Laurence Lundblade11fd78b2020-09-01 22:13:27 -07007579 -INFINITY,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007580 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)
Laurence Lundblade11fd78b2020-09-01 22:13:27 -07007581 },
Laurence Lundblade51722fd2020-09-02 13:01:33 -07007582
7583 {
7584 "big float underflow [9223372036854775806, -9223372036854775806]",
7585 {(uint8_t[]){
7586 0xC5, 0x82,
7587 0x3B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE,
7588 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE}, 20},
Laurence Lundblade51722fd2020-09-02 13:01:33 -07007589 0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007590 EXP_AND_MANTISSA_ERROR(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW),
Laurence Lundblade51722fd2020-09-02 13:01:33 -07007591 0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007592 EXP_AND_MANTISSA_ERROR(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW),
Laurence Lundblade51722fd2020-09-02 13:01:33 -07007593 0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007594 FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS))
Laurence Lundblade51722fd2020-09-02 13:01:33 -07007595 },
7596
7597 {
7598 "bigfloat that evaluates to -INFINITY",
7599 {(uint8_t[]){
7600 0xC5, 0x82,
7601 0x1B, 0x70, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03,
7602 0xC3, 0x42, 0x01, 0x01}, 15},
Laurence Lundblade51722fd2020-09-02 13:01:33 -07007603 0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007604 EXP_AND_MANTISSA_ERROR(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW),
Laurence Lundblade51722fd2020-09-02 13:01:33 -07007605 0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007606 EXP_AND_MANTISSA_ERROR(QCBOR_ERR_NUMBER_SIGN_CONVERSION),
Laurence Lundblade51722fd2020-09-02 13:01:33 -07007607 -INFINITY,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007608 FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS))
Laurence Lundblade51722fd2020-09-02 13:01:33 -07007609 },
Laurence Lundblade37286c02022-09-03 10:05:02 -07007610 {
7611 "Positive bignum 0xffff",
7612 {(uint8_t[]){0xC2, 0x42, 0xff, 0xff}, 4},
7613 65536-1,
7614 QCBOR_SUCCESS,
7615 0xffff,
7616 QCBOR_SUCCESS,
7617 65535.0,
7618 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)
7619 },
7620#endif /* QCBOR_DISABLE_TAGS */
7621 {
7622 "Positive integer 18446744073709551615",
7623 {(uint8_t[]){0x1b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, 9},
7624 0,
7625 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
7626 18446744073709551615ULL,
7627 QCBOR_SUCCESS,
7628 18446744073709551615.0,
7629 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)
7630 },
7631
7632 {
7633 "Postive integer 0",
7634 {(uint8_t[]){0x0}, 1},
7635 0LL,
7636 QCBOR_SUCCESS,
7637 0ULL,
7638 QCBOR_SUCCESS,
7639 0.0,
7640 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)
7641 },
7642 {
7643 "Negative integer -18446744073709551616",
7644 {(uint8_t[]){0x3b, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }, 9},
7645 -9223372036854775807-1, // INT64_MIN
7646 QCBOR_SUCCESS,
7647 0ULL,
7648 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
7649 -9223372036854775808.0,
7650 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)
7651 },
7652 {
7653 "Double Floating point value 100.3",
7654 {(uint8_t[]){0xfb, 0x40, 0x59, 0x13, 0x33, 0x33, 0x33, 0x33, 0x33}, 9},
7655 100L,
7656 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS),
7657 100ULL,
7658 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS),
7659 100.3,
7660 FLOAT_ERR_CODE_NO_FLOAT(QCBOR_SUCCESS),
7661 },
7662 {
7663 "Floating point value NaN 0xfa7fc00000",
7664 {(uint8_t[]){0xfa, 0x7f, 0xc0, 0x00, 0x00}, 5},
7665 0,
7666 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_FLOAT_EXCEPTION),
7667 0,
7668 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_FLOAT_EXCEPTION),
7669 NAN,
7670 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS),
7671 },
7672 {
7673 "half-precision Floating point value -4",
7674 {(uint8_t[]){0xf9, 0xc4, 0x00}, 3},
7675 // Normal case with all enabled.
7676 -4,
7677 FLOAT_ERR_CODE_NO_HALF_PREC_NO_FLOAT_HW(QCBOR_SUCCESS),
7678 0,
7679 FLOAT_ERR_CODE_NO_HALF_PREC_NO_FLOAT_HW(QCBOR_ERR_NUMBER_SIGN_CONVERSION),
7680 -4.0,
7681 FLOAT_ERR_CODE_NO_HALF_PREC(QCBOR_SUCCESS)
7682 },
7683 {
7684 "+inifinity single precision",
7685 {(uint8_t[]){0xfa, 0x7f, 0x80, 0x00, 0x00}, 5},
7686 0,
7687 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_FLOAT_EXCEPTION),
7688 0,
7689 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW),
7690 INFINITY,
7691 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)
7692 },
7693
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007694};
Laurence Lundblade9c905e82020-04-25 11:31:38 -07007695
7696
7697
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08007698
7699static int32_t SetUpDecoder(QCBORDecodeContext *DCtx, UsefulBufC CBOR, UsefulBuf Pool)
7700{
7701 QCBORDecode_Init(DCtx, CBOR, QCBOR_DECODE_MODE_NORMAL);
7702#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
7703 if(QCBORDecode_SetMemPool(DCtx, Pool, 0)) {
7704 return 1;
7705 }
7706#else /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
7707 (void)Pool;
7708#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
7709 return 0;
7710}
7711
7712
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03007713int32_t IntegerConvertTest(void)
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007714{
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08007715 const int nNumTests = C_ARRAY_COUNT(NumberConversions,
7716 struct NumberConversion);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007717
Laurence Lundblade9ab5abb2020-05-20 12:10:45 -07007718 for(int nIndex = 0; nIndex < nNumTests; nIndex++) {
7719 const struct NumberConversion *pF = &NumberConversions[nIndex];
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07007720
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007721 // Set up the decoding context including a memory pool so that
7722 // indefinite length items can be checked
7723 QCBORDecodeContext DCtx;
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007724 UsefulBuf_MAKE_STACK_UB(Pool, 100);
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07007725
7726 /* ----- test conversion to int64_t ------ */
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08007727 if(SetUpDecoder(&DCtx, pF->CBOR, Pool)) {
7728 return (int32_t)(3333+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007729 }
7730
7731 int64_t nInt;
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007732 QCBORDecode_GetInt64ConvertAll(&DCtx, 0xffff, &nInt);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07007733 if(QCBORDecode_GetError(&DCtx) != pF->uErrorInt64) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07007734 return (int32_t)(2000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007735 }
7736 if(pF->uErrorInt64 == QCBOR_SUCCESS && pF->nConvertedToInt64 != nInt) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07007737 return (int32_t)(3000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007738 }
7739
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07007740 /* ----- test conversion to uint64_t ------ */
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08007741 if(SetUpDecoder(&DCtx, pF->CBOR, Pool)) {
7742 return (int32_t)(3333+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007743 }
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08007744
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007745 uint64_t uInt;
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007746 QCBORDecode_GetUInt64ConvertAll(&DCtx, 0xffff, &uInt);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07007747 if(QCBORDecode_GetError(&DCtx) != pF->uErrorUint64) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07007748 return (int32_t)(4000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007749 }
7750 if(pF->uErrorUint64 == QCBOR_SUCCESS && pF->uConvertToUInt64 != uInt) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07007751 return (int32_t)(5000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007752 }
7753
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07007754 /* ----- test conversion to double ------ */
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08007755 if(SetUpDecoder(&DCtx, pF->CBOR, Pool)) {
7756 return (int32_t)(3333+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007757 }
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007758
7759#ifndef USEFULBUF_DISABLE_ALL_FLOAT
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007760 double d;
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007761 QCBORDecode_GetDoubleConvertAll(&DCtx, 0xffff, &d);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07007762 if(QCBORDecode_GetError(&DCtx) != pF->uErrorDouble) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07007763 return (int32_t)(6000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007764 }
7765 if(pF->uErrorDouble == QCBOR_SUCCESS) {
7766 if(isnan(pF->dConvertToDouble)) {
Laurence Lundblade983500d2020-05-14 11:49:34 -07007767 // NaN's can't be compared for equality. A NaN is
7768 // never equal to anything including another NaN
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007769 if(!isnan(d)) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07007770 return (int32_t)(7000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007771 }
7772 } else {
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007773 if(pF->dConvertToDouble != d) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07007774 return (int32_t)(8000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007775 }
7776 }
7777 }
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007778#endif /* USEFULBUF_DISABLE_ALL_FLOAT */
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007779 }
7780
7781 return 0;
7782}
7783
Laurence Lundbladea8758502022-05-15 17:57:46 -07007784#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
7785
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03007786int32_t CBORTestIssue134(void)
David Navarro9123e5b2022-03-28 16:04:03 +02007787{
7788 QCBORDecodeContext DCtx;
7789 QCBORItem Item;
7790 QCBORError uCBORError;
7791 const uint8_t spTestIssue134[] = { 0x5F, 0x40, 0xFF };
Laurence Lundblade9c905e82020-04-25 11:31:38 -07007792
David Navarro9123e5b2022-03-28 16:04:03 +02007793 QCBORDecode_Init(&DCtx,
7794 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTestIssue134),
7795 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade97c61bf2020-05-02 11:24:06 -07007796
David Navarro9123e5b2022-03-28 16:04:03 +02007797 UsefulBuf_MAKE_STACK_UB(StringBuf, 200);
7798 QCBORDecode_SetMemPool(&DCtx, StringBuf, false);
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03007799
David Navarro9123e5b2022-03-28 16:04:03 +02007800 do {
7801 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
7802 } while (QCBOR_SUCCESS == uCBORError);
7803
7804 uCBORError = QCBORDecode_Finish(&DCtx);
7805
Laurence Lundblade11ea3612022-07-01 13:26:23 -07007806 return (int32_t)uCBORError;
David Navarro9123e5b2022-03-28 16:04:03 +02007807}
Laurence Lundblade97c61bf2020-05-02 11:24:06 -07007808
Laurence Lundbladea8758502022-05-15 17:57:46 -07007809#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
7810
Laurence Lundblade37286c02022-09-03 10:05:02 -07007811
7812
7813static const uint8_t spSequenceTestInput[] = {
7814 /* 1. The valid date string "1985-04-12" */
7815 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string
7816
7817 /* 2. */
7818 0x00,
7819
7820 /* 3. A valid epoch date, 1400000000; Tue, 13 May 2014 16:53:20 GMT */
7821 0x1a, 0x53, 0x72, 0x4E, 0x00,
7822
7823 /* 4. */
7824 0x62, 'h', 'i',
7825};
7826
7827
Laurence Lundbladee3553422020-05-02 11:11:17 -07007828int32_t CBORSequenceDecodeTests(void)
7829{
7830 QCBORDecodeContext DCtx;
Laurence Lundblade87495732021-02-26 10:05:55 -07007831 QCBORItem Item;
7832 QCBORError uCBORError;
7833 size_t uConsumed;
Laurence Lundbladee3553422020-05-02 11:11:17 -07007834
7835 // --- Test a sequence with extra bytes ---
Laurence Lundblade9b334962020-08-27 10:55:53 -07007836
Laurence Lundbladee3553422020-05-02 11:11:17 -07007837 QCBORDecode_Init(&DCtx,
Laurence Lundblade37286c02022-09-03 10:05:02 -07007838 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSequenceTestInput),
Laurence Lundbladee3553422020-05-02 11:11:17 -07007839 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade9b334962020-08-27 10:55:53 -07007840
Laurence Lundblade37286c02022-09-03 10:05:02 -07007841 // Get 1.
Laurence Lundbladee3553422020-05-02 11:11:17 -07007842 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
7843 if(uCBORError != QCBOR_SUCCESS) {
7844 return 1;
7845 }
Laurence Lundblade37286c02022-09-03 10:05:02 -07007846 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ) {
Laurence Lundbladee3553422020-05-02 11:11:17 -07007847 return 2;
7848 }
Laurence Lundbladec7114722020-08-13 05:11:40 -07007849
Laurence Lundblade87495732021-02-26 10:05:55 -07007850 uCBORError = QCBORDecode_PartialFinish(&DCtx, &uConsumed);
7851 if(uCBORError != QCBOR_ERR_EXTRA_BYTES ||
Laurence Lundblade37286c02022-09-03 10:05:02 -07007852 uConsumed != 11) {
7853 return 102;
7854 }
7855
7856 // Get 2.
7857 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
7858 if(uCBORError != QCBOR_SUCCESS) {
7859 return 66;
7860 }
7861
7862 uCBORError = QCBORDecode_PartialFinish(&DCtx, &uConsumed);
7863 if(uCBORError != QCBOR_ERR_EXTRA_BYTES ||
Laurence Lundblade87495732021-02-26 10:05:55 -07007864 uConsumed != 12) {
7865 return 102;
7866 }
7867
Laurence Lundblade37286c02022-09-03 10:05:02 -07007868 // Get 3.
Laurence Lundbladec7114722020-08-13 05:11:40 -07007869 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundbladee3553422020-05-02 11:11:17 -07007870 if(uCBORError != QCBOR_SUCCESS) {
7871 return 2;
7872 }
Laurence Lundblade37286c02022-09-03 10:05:02 -07007873 if(Item.uDataType != QCBOR_TYPE_INT64) {
Laurence Lundbladee3553422020-05-02 11:11:17 -07007874 return 3;
7875 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07007876
Laurence Lundbladee3553422020-05-02 11:11:17 -07007877 // A sequence can have stuff at the end that may
7878 // or may not be valid CBOR. The protocol decoder knows
7879 // when to stop by definition of the protocol, not
7880 // when the top-level map or array is ended.
7881 // Finish still has to be called to know that
7882 // maps and arrays (if there were any) were closed
7883 // off correctly. When called like this it
7884 // must return the error QCBOR_ERR_EXTRA_BYTES.
7885 uCBORError = QCBORDecode_Finish(&DCtx);
7886 if(uCBORError != QCBOR_ERR_EXTRA_BYTES) {
7887 return 4;
7888 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07007889
Laurence Lundbladee3553422020-05-02 11:11:17 -07007890 // --- Test an empty input ----
7891 uint8_t empty[1];
7892 UsefulBufC Empty = {empty, 0};
7893 QCBORDecode_Init(&DCtx,
7894 Empty,
7895 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade9b334962020-08-27 10:55:53 -07007896
Laurence Lundbladee3553422020-05-02 11:11:17 -07007897 uCBORError = QCBORDecode_Finish(&DCtx);
7898 if(uCBORError != QCBOR_SUCCESS) {
7899 return 5;
7900 }
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07007901
7902
Laurence Lundbladee3553422020-05-02 11:11:17 -07007903 // --- Sequence with unclosed indefinite length array ---
7904 static const uint8_t xx[] = {0x01, 0x9f, 0x02};
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07007905
Laurence Lundbladee3553422020-05-02 11:11:17 -07007906 QCBORDecode_Init(&DCtx,
7907 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(xx),
7908 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07007909
Laurence Lundbladee3553422020-05-02 11:11:17 -07007910 // Get the first item
7911 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
7912 if(uCBORError != QCBOR_SUCCESS) {
7913 return 7;
7914 }
7915 if(Item.uDataType != QCBOR_TYPE_INT64) {
7916 return 8;
7917 }
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07007918
Laurence Lundbladee3553422020-05-02 11:11:17 -07007919 // Get a second item
7920 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08007921#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundbladee3553422020-05-02 11:11:17 -07007922 if(uCBORError != QCBOR_SUCCESS) {
7923 return 9;
7924 }
7925 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
7926 return 10;
7927 }
7928
7929 // Try to finish before consuming all bytes to confirm
7930 // that the still-open error is returned.
7931 uCBORError = QCBORDecode_Finish(&DCtx);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07007932 if(uCBORError != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
Laurence Lundbladee3553422020-05-02 11:11:17 -07007933 return 11;
7934 }
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08007935#else /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
7936 if(uCBORError != QCBOR_ERR_INDEF_LEN_ARRAYS_DISABLED) {
7937 return 20;
7938 }
7939#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
Laurence Lundbladee3553422020-05-02 11:11:17 -07007940
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07007941
Laurence Lundbladee3553422020-05-02 11:11:17 -07007942 // --- Sequence with a closed indefinite length array ---
7943 static const uint8_t yy[] = {0x01, 0x9f, 0xff};
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07007944
Laurence Lundbladee3553422020-05-02 11:11:17 -07007945 QCBORDecode_Init(&DCtx,
7946 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(yy),
7947 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07007948
Laurence Lundbladee3553422020-05-02 11:11:17 -07007949 // Get the first item
7950 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
7951 if(uCBORError != QCBOR_SUCCESS) {
7952 return 12;
7953 }
7954 if(Item.uDataType != QCBOR_TYPE_INT64) {
7955 return 13;
7956 }
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07007957
Laurence Lundbladee3553422020-05-02 11:11:17 -07007958 // Get a second item
7959 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08007960#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
7961
Laurence Lundbladee3553422020-05-02 11:11:17 -07007962 if(uCBORError != QCBOR_SUCCESS) {
7963 return 14;
7964 }
7965 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
7966 return 15;
7967 }
7968
7969 // Try to finish before consuming all bytes to confirm
7970 // that the still-open error is returned.
7971 uCBORError = QCBORDecode_Finish(&DCtx);
7972 if(uCBORError != QCBOR_SUCCESS) {
7973 return 16;
7974 }
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08007975#else /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
7976 if(uCBORError != QCBOR_ERR_INDEF_LEN_ARRAYS_DISABLED) {
7977 return 20;
7978 }
7979#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
Laurence Lundbladee3553422020-05-02 11:11:17 -07007980
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07007981
Laurence Lundbladee3553422020-05-02 11:11:17 -07007982 return 0;
7983}
7984
Laurence Lundbladee15326f2020-06-15 15:50:23 -07007985
Laurence Lundblade70ecead2020-06-15 19:40:06 -07007986
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03007987int32_t IntToTests(void)
Laurence Lundbladee15326f2020-06-15 15:50:23 -07007988{
7989 int nErrCode;
7990 int32_t n32;
7991 int16_t n16;
7992 int8_t n8;
7993 uint32_t u32;
7994 uint16_t u16;
7995 uint8_t u8;
7996 uint64_t u64;
7997
7998 nErrCode = QCBOR_Int64ToInt32(1, &n32);
7999 if(nErrCode == -1 || n32 != 1) {
8000 return 1;
8001 }
8002
8003 nErrCode = QCBOR_Int64ToInt32((int64_t)INT32_MAX, &n32);
8004 if(nErrCode == -1 || n32 != INT32_MAX) {
8005 return 2;
8006 }
8007
8008 nErrCode = QCBOR_Int64ToInt32((int64_t)INT32_MIN, &n32);
8009 if(nErrCode == -1 || n32 != INT32_MIN) {
8010 return 3;
8011 }
8012
8013 nErrCode = QCBOR_Int64ToInt32(((int64_t)INT32_MAX)+1, &n32);
8014 if(nErrCode != -1) {
8015 return 4;
8016 }
8017
8018 nErrCode = QCBOR_Int64ToInt32(((int64_t)INT32_MIN)-1, &n32);
8019 if(nErrCode != -1) {
8020 return 5;
8021 }
8022
8023
8024 nErrCode = QCBOR_Int64ToInt16((int64_t)INT16_MAX, &n16);
8025 if(nErrCode == -1 || n16 != INT16_MAX) {
8026 return 6;
8027 }
8028
8029 nErrCode = QCBOR_Int64ToInt16((int64_t)INT16_MIN, &n16);
8030 if(nErrCode == -1 || n16 != INT16_MIN) {
8031 return 7;
8032 }
8033
8034 nErrCode = QCBOR_Int64ToInt16(1, &n16);
8035 if(nErrCode == -1 || n16 != 1) {
8036 return 8;
8037 }
8038
8039 nErrCode = QCBOR_Int64ToInt16(((int64_t)INT16_MAX)+1, &n16);
8040 if(nErrCode != -1) {
8041 return 9;
8042 }
8043
8044 nErrCode = QCBOR_Int64ToInt16(((int64_t)INT16_MIN)-1, &n16);
8045 if(nErrCode != -1) {
8046 return 10;
8047 }
8048
8049
8050 nErrCode = QCBOR_Int64ToInt8(1, &n8);
8051 if(nErrCode == -1 || n8 != 1) {
8052 return 11;
8053 }
8054
8055 nErrCode = QCBOR_Int64ToInt8((int64_t)INT8_MAX, &n8);
8056 if(nErrCode == -1 || n8 != INT8_MAX) {
8057 return 12;
8058 }
8059
8060 nErrCode = QCBOR_Int64ToInt8((int64_t)INT8_MIN, &n8);
8061 if(nErrCode == -1 || n8 != INT8_MIN) {
8062 return 13;
8063 }
8064
8065 nErrCode = QCBOR_Int64ToInt8(((int64_t)INT8_MAX)+1, &n8);
8066 if(nErrCode != -1) {
8067 return 14;
8068 }
8069
8070 nErrCode = QCBOR_Int64ToInt8(((int64_t)INT8_MIN)-1, &n8);
8071 if(nErrCode != -1) {
8072 return 15;
8073 }
8074
8075
8076 nErrCode = QCBOR_Int64ToUInt32(1, &u32);
8077 if(nErrCode == -1 || u32 != 1) {
8078 return 16;
8079 }
8080
8081 nErrCode = QCBOR_Int64ToUInt32((int64_t)UINT32_MAX, &u32);
8082 if(nErrCode == -1 || u32 != UINT32_MAX) {
8083 return 17;
8084 }
8085
8086 nErrCode = QCBOR_Int64ToUInt32((int64_t)0, &u32);
8087 if(nErrCode == -1 || u32 != 0) {
8088 return 18;
8089 }
8090
8091 nErrCode = QCBOR_Int64ToUInt32(((int64_t)UINT32_MAX)+1, &u32);
8092 if(nErrCode != -1) {
8093 return 19;
8094 }
8095
8096 nErrCode = QCBOR_Int64ToUInt32((int64_t)-1, &u32);
8097 if(nErrCode != -1) {
8098 return 20;
8099 }
8100
8101
Sebastian Burck5a0ab732024-06-29 06:31:20 +02008102 nErrCode = QCBOR_Int64ToUInt16((int64_t)UINT16_MAX, &u16);
Laurence Lundbladee15326f2020-06-15 15:50:23 -07008103 if(nErrCode == -1 || u16 != UINT16_MAX) {
8104 return 21;
8105 }
8106
Sebastian Burck5a0ab732024-06-29 06:31:20 +02008107 nErrCode = QCBOR_Int64ToUInt16((int64_t)0, &u16);
Laurence Lundbladee15326f2020-06-15 15:50:23 -07008108 if(nErrCode == -1 || u16 != 0) {
8109 return 22;
8110 }
8111
Sebastian Burck5a0ab732024-06-29 06:31:20 +02008112 nErrCode = QCBOR_Int64ToUInt16(1, &u16);
Laurence Lundbladee15326f2020-06-15 15:50:23 -07008113 if(nErrCode == -1 || u16 != 1) {
8114 return 23;
8115 }
8116
Sebastian Burck5a0ab732024-06-29 06:31:20 +02008117 nErrCode = QCBOR_Int64ToUInt16(((int64_t)UINT16_MAX)+1, &u16);
Laurence Lundbladee15326f2020-06-15 15:50:23 -07008118 if(nErrCode != -1) {
8119 return 24;
8120 }
8121
Sebastian Burck5a0ab732024-06-29 06:31:20 +02008122 nErrCode = QCBOR_Int64ToUInt16((int64_t)-1, &u16);
Laurence Lundbladee15326f2020-06-15 15:50:23 -07008123 if(nErrCode != -1) {
8124 return 25;
8125 }
8126
8127
8128 nErrCode = QCBOR_Int64ToUInt8((int64_t)UINT8_MAX, &u8);
8129 if(nErrCode == -1 || u8 != UINT8_MAX) {
8130 return 26;
8131 }
8132
8133 nErrCode = QCBOR_Int64ToUInt8((int64_t)0, &u8);
8134 if(nErrCode == -1 || u8 != 0) {
8135 return 27;
8136 }
8137
8138 nErrCode = QCBOR_Int64ToUInt8(1, &u8);
8139 if(nErrCode == -1 || u8 != 1) {
8140 return 28;
8141 }
8142
8143 nErrCode = QCBOR_Int64ToUInt8(((int64_t)UINT16_MAX)+1, &u8);
8144 if(nErrCode != -1) {
8145 return 29;
8146 }
8147
8148 nErrCode = QCBOR_Int64ToUInt8((int64_t)-1, &u8);
8149 if(nErrCode != -1) {
8150 return 30;
8151 }
8152
8153
8154 nErrCode = QCBOR_Int64ToUInt64(1, &u64);
8155 if(nErrCode == -1 || u64 != 1) {
8156 return 31;
8157 }
8158
8159 nErrCode = QCBOR_Int64ToUInt64(INT64_MAX, &u64);
8160 if(nErrCode == -1 || u64 != INT64_MAX) {
8161 return 32;
8162 }
8163
8164 nErrCode = QCBOR_Int64ToUInt64((int64_t)0, &u64);
8165 if(nErrCode == -1 || u64 != 0) {
8166 return 33;
8167 }
8168
8169 nErrCode = QCBOR_Int64ToUInt64((int64_t)-1, &u64);
8170 if(nErrCode != -1) {
8171 return 34;
8172 }
8173
8174 return 0;
8175}
8176
Laurence Lundblade0750fc42020-06-20 21:02:34 -07008177
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07008178
8179
Laurence Lundblade0750fc42020-06-20 21:02:34 -07008180/*
8181A sequence with
8182 A wrapping bstr
8183 containing a map
8184 1
8185 2
8186 A wrapping bstr
8187 containing an array
8188 3
8189 wrapping bstr
8190 4
8191 5
8192 6
8193 array
8194 7
8195 8
Laurence Lundblade0750fc42020-06-20 21:02:34 -07008196 */
8197
Laurence Lundblade55013642020-09-23 05:39:22 -07008198static UsefulBufC EncodeBstrWrapTestData(UsefulBuf OutputBuffer)
Laurence Lundblade0750fc42020-06-20 21:02:34 -07008199{
Laurence Lundblade55013642020-09-23 05:39:22 -07008200 UsefulBufC Encoded;
Laurence Lundblade0750fc42020-06-20 21:02:34 -07008201 QCBOREncodeContext EC;
Laurence Lundblade55013642020-09-23 05:39:22 -07008202 QCBORError uErr;
Laurence Lundblade0750fc42020-06-20 21:02:34 -07008203
Laurence Lundblade55013642020-09-23 05:39:22 -07008204 QCBOREncode_Init(&EC, OutputBuffer);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07008205
8206 QCBOREncode_BstrWrap(&EC);
8207 QCBOREncode_OpenMap(&EC);
8208 QCBOREncode_AddInt64ToMapN(&EC, 100, 1);
8209 QCBOREncode_AddInt64ToMapN(&EC, 200, 2);
8210 QCBOREncode_CloseMap(&EC);
8211 QCBOREncode_BstrWrap(&EC);
8212 QCBOREncode_OpenArray(&EC);
8213 QCBOREncode_AddInt64(&EC, 3);
8214 QCBOREncode_BstrWrap(&EC);
8215 QCBOREncode_AddInt64(&EC, 4);
8216 QCBOREncode_CloseBstrWrap(&EC, NULL);
8217 QCBOREncode_AddInt64(&EC, 5);
8218 QCBOREncode_CloseArray(&EC);
8219 QCBOREncode_CloseBstrWrap(&EC, NULL);
8220 QCBOREncode_AddInt64(&EC, 6);
8221 QCBOREncode_CloseBstrWrap(&EC, NULL);
8222 QCBOREncode_OpenArray(&EC);
8223 QCBOREncode_AddInt64(&EC, 7);
8224 QCBOREncode_AddInt64(&EC, 8);
8225 QCBOREncode_CloseArray(&EC);
8226
8227 uErr = QCBOREncode_Finish(&EC, &Encoded);
Laurence Lundblade40a04322020-06-27 22:52:52 -07008228 if(uErr) {
8229 Encoded = NULLUsefulBufC;
8230 }
Laurence Lundblade0750fc42020-06-20 21:02:34 -07008231
8232 return Encoded;
8233}
8234
Laurence Lundbladecc7da412020-12-27 00:09:07 -08008235/* h'FF' */
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08008236static const uint8_t spBreakInByteString[] = {
8237 0x41, 0xff
8238};
8239
Laurence Lundblade0750fc42020-06-20 21:02:34 -07008240
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03008241int32_t EnterBstrTest(void)
Laurence Lundblade0750fc42020-06-20 21:02:34 -07008242{
Laurence Lundblade8510f8c2020-12-01 11:31:16 -08008243 UsefulBuf_MAKE_STACK_UB(OutputBuffer, 100);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07008244
8245 QCBORDecodeContext DC;
8246
Laurence Lundblade55013642020-09-23 05:39:22 -07008247 QCBORDecode_Init(&DC, EncodeBstrWrapTestData(OutputBuffer), 0);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07008248
Laurence Lundblade55013642020-09-23 05:39:22 -07008249 int64_t n1, n2, n3, n4, n5, n6, n7, n8;
Laurence Lundblade0750fc42020-06-20 21:02:34 -07008250
8251
Laurence Lundblade9b334962020-08-27 10:55:53 -07008252 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07008253 QCBORDecode_EnterMap(&DC, NULL);
Laurence Lundblade55013642020-09-23 05:39:22 -07008254 QCBORDecode_GetInt64InMapN(&DC, 100, &n1);
8255 QCBORDecode_GetInt64InMapN(&DC, 200, &n2);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07008256 QCBORDecode_ExitMap(&DC);
Laurence Lundblade9b334962020-08-27 10:55:53 -07008257 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07008258 QCBORDecode_EnterArray(&DC, NULL);
Laurence Lundblade55013642020-09-23 05:39:22 -07008259 QCBORDecode_GetInt64(&DC, &n3);
Laurence Lundblade9b334962020-08-27 10:55:53 -07008260 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
Laurence Lundblade55013642020-09-23 05:39:22 -07008261 QCBORDecode_GetInt64(&DC, &n4);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07008262 QCBORDecode_ExitBstrWrapped(&DC);
Laurence Lundblade55013642020-09-23 05:39:22 -07008263 QCBORDecode_GetInt64(&DC, &n5);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07008264 QCBORDecode_ExitArray(&DC);
8265 QCBORDecode_ExitBstrWrapped(&DC);
Laurence Lundblade55013642020-09-23 05:39:22 -07008266 QCBORDecode_GetInt64(&DC, &n6);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07008267 QCBORDecode_ExitBstrWrapped(&DC);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07008268 QCBORDecode_EnterArray(&DC, NULL);
Laurence Lundblade55013642020-09-23 05:39:22 -07008269 QCBORDecode_GetInt64(&DC, &n7);
8270 QCBORDecode_GetInt64(&DC, &n8);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07008271 QCBORDecode_ExitArray(&DC);
8272
8273 QCBORError uErr = QCBORDecode_Finish(&DC);
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08008274 if(uErr) {
8275 return (int32_t)uErr;
8276 }
Laurence Lundblade0750fc42020-06-20 21:02:34 -07008277
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08008278
8279 /* Enter and exit byte string wrapped CBOR that is bad. It has just a break.
8280 * Successful because no items are fetched from byte string.
8281 */
8282 QCBORDecode_Init(&DC,
8283 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBreakInByteString),
8284 0);
8285 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
8286 uErr = QCBORDecode_GetError(&DC);
8287 if(uErr) {
8288 return 100 + (int32_t)uErr;
8289 }
8290
8291 QCBORDecode_ExitBstrWrapped(&DC);
8292 uErr = QCBORDecode_GetError(&DC);
8293 if(uErr) {
8294 return 200 + (int32_t)uErr;
8295 }
8296
8297 /* Try to get item that is a break out of a byte string wrapped CBOR.
8298 * It fails because there should be no break.
8299 */
8300 QCBORDecode_Init(&DC,
8301 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBreakInByteString),
8302 0);
8303 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
8304 QCBORItem Item;
8305 uErr = QCBORDecode_GetNext(&DC, &Item);
8306 if(uErr != QCBOR_ERR_BAD_BREAK) {
8307 return 300 + (int32_t)uErr;
8308 }
8309
8310 return 0;
Laurence Lundblade0750fc42020-06-20 21:02:34 -07008311}
Laurence Lundblade37f46e52020-08-04 03:32:14 -07008312
8313
8314
8315
8316static const uint8_t spTaggedTypes[] = {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008317 0xb2,
Laurence Lundblade37f46e52020-08-04 03:32:14 -07008318
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07008319 // Date string
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008320 0x00,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07008321 0xc0, 0x74, 0x32, 0x30, 0x30, 0x33, 0x2D, 0x31, 0x32, 0x2D,
8322 0x31, 0x33, 0x54, 0x31, 0x38, 0x3A, 0x33, 0x30, 0x3A, 0x30,
8323 0x32, 0x5A,
Laurence Lundblade37f46e52020-08-04 03:32:14 -07008324
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008325 0x01,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07008326 0x74, 0x32, 0x30, 0x30, 0x33, 0x2D, 0x31, 0x32, 0x2D, 0x31,
8327 0x33, 0x54, 0x31, 0x38, 0x3A, 0x33, 0x30, 0x3A, 0x30, 0x32,
8328 0x5A,
Laurence Lundblade37f46e52020-08-04 03:32:14 -07008329
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07008330 // Bignum
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008331 10,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07008332 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
8333 0x09, 0x10,
Laurence Lundblade37f46e52020-08-04 03:32:14 -07008334
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008335 11,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07008336 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
8337 0x09, 0x10,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008338
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07008339 // URL
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008340 20,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07008341 0xd8, 0x20, 0x6f, 0x68, 0x74, 0x74, 0x70, 0x3A, 0x2F, 0x2F,
8342 0x63, 0x62, 0x6F, 0x72, 0x2E, 0x6D, 0x65, 0x2F,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008343
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07008344 21,
8345 0x6f, 0x68, 0x74, 0x74, 0x70, 0x3A, 0x2F, 0x2F, 0x63, 0x62,
8346 0x6F, 0x72, 0x2E, 0x6D, 0x65, 0x2F,
8347
8348 // B64
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008349 0x18, 0x1e,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07008350 0xd8, 0x22, 0x6c, 0x63, 0x47, 0x78, 0x6C, 0x59, 0x58, 0x4E,
8351 0x31, 0x63, 0x6D, 0x55, 0x75,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008352
8353 0x18, 0x1f,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07008354 0x6c, 0x63, 0x47, 0x78, 0x6C, 0x59, 0x58, 0x4E, 0x31, 0x63,
8355 0x6D, 0x55, 0x75,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008356
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07008357 // B64URL
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008358 0x18, 0x28,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07008359 0xd8, 0x21, 0x6c, 0x63, 0x47, 0x78, 0x6C, 0x59, 0x58, 0x4E,
8360 0x31, 0x63, 0x6D, 0x55, 0x75,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008361
8362 0x18, 0x29,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07008363 0x6c, 0x63, 0x47, 0x78, 0x6C, 0x59, 0x58, 0x4E, 0x31, 0x63,
8364 0x6D, 0x55, 0x75,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008365
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07008366 // Regex
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008367 0x18, 0x32,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07008368 0xd8, 0x23, 0x68, 0x31, 0x30, 0x30, 0x5C, 0x73, 0x2A, 0x6D,
8369 0x6B,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008370
8371 0x18, 0x33,
8372 0x68, 0x31, 0x30, 0x30, 0x5C, 0x73, 0x2A, 0x6D, 0x6B,
8373
8374 // MIME
8375 0x18, 0x3c,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07008376 0xd8, 0x24, 0x72, 0x4D, 0x49, 0x4D, 0x45, 0x2D, 0x56, 0x65,
8377 0x72, 0x73, 0x69, 0x6F, 0x6E, 0x3A, 0x20, 0x31, 0x2E, 0x30,
8378 0x0A,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008379
8380 0x18, 0x3d,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07008381 0x72, 0x4D, 0x49, 0x4D, 0x45, 0x2D, 0x56, 0x65, 0x72, 0x73,
8382 0x69, 0x6F, 0x6E, 0x3A, 0x20, 0x31, 0x2E, 0x30, 0x0A,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008383
8384 0x18, 0x3e,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07008385 0xd9, 0x01, 0x01, 0x52, 0x4D, 0x49, 0x4D, 0x45, 0x2D, 0x56,
8386 0x65, 0x72, 0x73, 0x69, 0x6F, 0x6E, 0x3A, 0x20, 0x31, 0x2E,
8387 0x30, 0x0A,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008388
8389 0x18, 0x3f,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07008390 0x52, 0x4D, 0x49, 0x4D, 0x45, 0x2D, 0x56, 0x65, 0x72, 0x73,
8391 0x69, 0x6F, 0x6E, 0x3A, 0x20, 0x31, 0x2E, 0x30, 0x0A,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008392
8393 // UUID
8394 0x18, 0x46,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07008395 0xd8, 0x25, 0x50, 0x53, 0x4D, 0x41, 0x52, 0x54, 0x43, 0x53,
8396 0x4C, 0x54, 0x54, 0x43, 0x46, 0x49, 0x43, 0x41, 0x32,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008397
8398 0x18, 0x47,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07008399 0x50, 0x53, 0x4D, 0x41, 0x52, 0x54, 0x43, 0x53, 0x4C, 0x54,
8400 0x54, 0x43, 0x46, 0x49, 0x43, 0x41, 0x32
Laurence Lundblade37f46e52020-08-04 03:32:14 -07008401};
8402
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03008403int32_t DecodeTaggedTypeTests(void)
Laurence Lundblade37f46e52020-08-04 03:32:14 -07008404{
8405 QCBORDecodeContext DC;
8406 QCBORError uErr;
8407
8408 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTaggedTypes), 0);
8409
8410 UsefulBufC String;
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008411 bool bNeg;
Laurence Lundblade37f46e52020-08-04 03:32:14 -07008412
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07008413 QCBORDecode_EnterMap(&DC, NULL);
Laurence Lundblade9b334962020-08-27 10:55:53 -07008414 QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008415 QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &String);
Laurence Lundblade37f46e52020-08-04 03:32:14 -07008416 if(QCBORDecode_GetError(&DC) != QCBOR_SUCCESS) {
8417 return 1;
8418 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07008419 QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade37f46e52020-08-04 03:32:14 -07008420 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_UNEXPECTED_TYPE) {
8421 return 2;
8422 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07008423 QCBORDecode_GetDateStringInMapN(&DC, 1, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade37f46e52020-08-04 03:32:14 -07008424 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_UNEXPECTED_TYPE) {
8425 return 3;
8426 }
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008427 QCBORDecode_GetDateStringInMapN(&DC, 1, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &String);
Laurence Lundblade9b334962020-08-27 10:55:53 -07008428 QCBORDecode_GetDateStringInMapN(&DC, 1, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade37f46e52020-08-04 03:32:14 -07008429 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
8430 return 4;
8431 }
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008432 QCBORDecode_GetDateStringInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07008433 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008434 return 5;
8435 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07008436
Laurence Lundblade9b334962020-08-27 10:55:53 -07008437 QCBORDecode_GetBignumInMapN(&DC, 10, QCBOR_TAG_REQUIREMENT_TAG, &String, &bNeg);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008438 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
8439 bNeg != false) {
8440 return 10;
8441 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07008442 QCBORDecode_GetBignumInMapN(&DC, 11, QCBOR_TAG_REQUIREMENT_TAG, &String, &bNeg);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008443 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
8444 bNeg != true) {
8445 return 11;
8446 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07008447 QCBORDecode_GetBignumInMapN(&DC, 11, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bNeg);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008448 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_UNEXPECTED_TYPE) {
8449 return 12;
8450 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07008451 QCBORDecode_GetBignumInMapN(&DC, 14, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bNeg);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07008452 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008453 return 13;
8454 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07008455 QCBORDecode_GetBignumInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bNeg);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07008456 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008457 return 14;
8458 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07008459
Laurence Lundblade9b334962020-08-27 10:55:53 -07008460 QCBORDecode_GetURIInMapN(&DC, 20, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008461 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
8462 return 20;
8463 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07008464 QCBORDecode_GetURIInMapN(&DC, 21, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008465 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
8466 return 21;
8467 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07008468 QCBORDecode_GetURIInMapN(&DC, 22, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07008469 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008470 return 22;
8471 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07008472 QCBORDecode_GetURIInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07008473 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008474 return 23;
8475 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07008476
Laurence Lundblade24bd7e12021-01-09 00:05:11 -08008477#ifndef QCBOR_DISABLE_UNCOMMON_TAGS
Laurence Lundblade9b334962020-08-27 10:55:53 -07008478 QCBORDecode_GetB64InMapN(&DC, 30, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008479 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
8480 return 30;
8481 }
Laurence Lundblade24bd7e12021-01-09 00:05:11 -08008482#endif
Laurence Lundblade9b334962020-08-27 10:55:53 -07008483 QCBORDecode_GetB64InMapN(&DC, 31, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008484 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
8485 return 31;
8486 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07008487 QCBORDecode_GetB64InMapN(&DC, 32, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07008488 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008489 return 32;
8490 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07008491 QCBORDecode_GetB64InMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07008492 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008493 return 33;
8494 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07008495
Laurence Lundblade24bd7e12021-01-09 00:05:11 -08008496#ifndef QCBOR_DISABLE_UNCOMMON_TAGS
Laurence Lundblade9b334962020-08-27 10:55:53 -07008497 QCBORDecode_GetB64URLInMapN(&DC, 40, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008498 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
8499 return 40;
8500 }
Laurence Lundblade24bd7e12021-01-09 00:05:11 -08008501#endif
Laurence Lundblade9b334962020-08-27 10:55:53 -07008502 QCBORDecode_GetB64URLInMapN(&DC, 41, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008503 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
8504 return 41;
8505 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07008506 QCBORDecode_GetB64URLInMapN(&DC, 42, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07008507 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008508 return 42;
8509 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07008510 QCBORDecode_GetB64URLInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07008511 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008512 return 43;
8513 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07008514
Laurence Lundblade24bd7e12021-01-09 00:05:11 -08008515#ifndef QCBOR_DISABLE_UNCOMMON_TAGS
Laurence Lundblade9b334962020-08-27 10:55:53 -07008516 QCBORDecode_GetRegexInMapN(&DC, 50, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008517 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
8518 return 50;
8519 }
Laurence Lundblade24bd7e12021-01-09 00:05:11 -08008520#endif
Laurence Lundblade9b334962020-08-27 10:55:53 -07008521 QCBORDecode_GetRegexInMapN(&DC, 51, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008522 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
8523 return 51;
8524 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07008525 QCBORDecode_GetRegexInMapN(&DC, 52, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07008526 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008527 return 52;
8528 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07008529 QCBORDecode_GetRegexInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07008530 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008531 return 53;
8532 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07008533
Laurence Lundblade24bd7e12021-01-09 00:05:11 -08008534#ifndef QCBOR_DISABLE_UNCOMMON_TAGS
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008535 // MIME
8536 bool bIsNot7Bit;
Laurence Lundblade9b334962020-08-27 10:55:53 -07008537 QCBORDecode_GetMIMEMessageInMapN(&DC, 60, QCBOR_TAG_REQUIREMENT_TAG, &String, &bIsNot7Bit);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008538 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
8539 bIsNot7Bit == true) {
8540 return 60;
8541 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07008542 QCBORDecode_GetMIMEMessageInMapN(&DC, 61, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bIsNot7Bit);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008543 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
8544 bIsNot7Bit == true) {
8545 return 61;
8546 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07008547 QCBORDecode_GetMIMEMessageInMapN(&DC, 62, QCBOR_TAG_REQUIREMENT_TAG, &String, &bIsNot7Bit);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008548 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
8549 bIsNot7Bit == false) {
8550 return 62;
8551 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07008552 QCBORDecode_GetMIMEMessageInMapN(&DC, 63, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bIsNot7Bit);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008553 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
8554 bIsNot7Bit == false) {
8555 return 63;
8556 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07008557 QCBORDecode_GetMIMEMessageInMapN(&DC, 64, QCBOR_TAG_REQUIREMENT_TAG, &String, &bNeg);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07008558 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008559 return 64;
8560 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07008561 QCBORDecode_GetMIMEMessageInMapSZ(&DC, "zzz", QCBOR_TAG_REQUIREMENT_TAG, &String, &bNeg);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07008562 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008563 return 65;
8564 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07008565
Laurence Lundblade24bd7e12021-01-09 00:05:11 -08008566
Laurence Lundblade9b334962020-08-27 10:55:53 -07008567 QCBORDecode_GetBinaryUUIDInMapN(&DC, 70, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008568 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
8569 return 70;
8570 }
Laurence Lundblade24bd7e12021-01-09 00:05:11 -08008571#endif /* #ifndef QCBOR_DISABLE_UNCOMMON_TAGS */
8572
Laurence Lundblade9b334962020-08-27 10:55:53 -07008573 QCBORDecode_GetBinaryUUIDInMapN(&DC, 71, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008574 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
8575 return 71;
8576 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07008577 QCBORDecode_GetBinaryUUIDInMapN(&DC, 72, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07008578 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008579 return 72;
8580 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07008581 QCBORDecode_GetBinaryUUIDInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07008582 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008583 return 73;
8584 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07008585
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07008586 // Improvement: add some more error test cases
8587
Laurence Lundblade37f46e52020-08-04 03:32:14 -07008588 QCBORDecode_ExitMap(&DC);
8589
8590 uErr = QCBORDecode_Finish(&DC);
8591 if(uErr != QCBOR_SUCCESS) {
8592 return 100;
8593 }
8594
8595 return 0;
8596}
Laurence Lundbladea4308a82020-10-03 18:08:57 -07008597
8598
8599
8600
8601/*
Laurence Lundbladecc7da412020-12-27 00:09:07 -08008602 [
8603 "aaaaaaaaaa",
8604 {}
8605 ]
Laurence Lundbladea4308a82020-10-03 18:08:57 -07008606 */
8607static const uint8_t spTooLarge1[] = {
8608 0x9f,
8609 0x6a, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61,
8610 0xa0,
8611 0xff
8612};
8613
8614/*
Laurence Lundbladecc7da412020-12-27 00:09:07 -08008615 [
8616 {
8617 0: "aaaaaaaaaa"
8618 }
8619 ]
Laurence Lundbladea4308a82020-10-03 18:08:57 -07008620 */
8621static const uint8_t spTooLarge2[] = {
8622 0x9f,
8623 0xa1,
8624 0x00,
8625 0x6a, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61,
8626 0xff
8627};
8628
8629/*
Laurence Lundbladecc7da412020-12-27 00:09:07 -08008630 h'A1006A61616161616161616161'
Laurence Lundbladea4308a82020-10-03 18:08:57 -07008631
Laurence Lundbladecc7da412020-12-27 00:09:07 -08008632 {
8633 0: "aaaaaaaaaa"
8634 }
Laurence Lundbladea4308a82020-10-03 18:08:57 -07008635 */
8636static const uint8_t spTooLarge3[] = {
8637 0x4d,
8638 0xa1,
8639 0x00,
8640 0x6a, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61,
8641};
8642
8643int32_t TooLargeInputTest(void)
8644{
8645 QCBORDecodeContext DC;
8646 QCBORError uErr;
8647 UsefulBufC String;
8648
8649 // These tests require a build with QCBOR_MAX_DECODE_INPUT_SIZE set
8650 // to 10 There's not really any way to test this error
8651 // condition. The error condition is not complex, so setting
8652 // QCBOR_MAX_DECODE_INPUT_SIZE gives an OK test.
8653
8654 // The input CBOR is only too large because the
8655 // QCBOR_MAX_DECODE_INPUT_SIZE is 10.
8656 //
8657 // This test is disabled for the normal test runs because of the
8658 // special build requirement.
8659
8660
8661 // Tests the start of a map being too large
8662 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTooLarge1), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07008663 QCBORDecode_EnterArray(&DC, NULL);
Laurence Lundbladea4308a82020-10-03 18:08:57 -07008664 QCBORDecode_GetTextString(&DC, &String);
8665 uErr = QCBORDecode_GetError(&DC);
8666 if(uErr != QCBOR_SUCCESS) {
8667 return 1;
8668 }
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07008669 QCBORDecode_EnterMap(&DC, NULL);
Laurence Lundbladea4308a82020-10-03 18:08:57 -07008670 uErr = QCBORDecode_GetError(&DC);
8671 if(uErr != QCBOR_ERR_INPUT_TOO_LARGE) {
8672 return 2;
8673 }
8674
8675 // Tests the end of a map being too large
8676 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTooLarge2), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07008677 QCBORDecode_EnterArray(&DC, NULL);
8678 QCBORDecode_EnterMap(&DC, NULL);
Laurence Lundbladea4308a82020-10-03 18:08:57 -07008679 uErr = QCBORDecode_GetError(&DC);
8680 if(uErr != QCBOR_SUCCESS) {
8681 return 3;
8682 }
8683 QCBORDecode_ExitMap(&DC);
8684 uErr = QCBORDecode_GetError(&DC);
8685 if(uErr != QCBOR_ERR_INPUT_TOO_LARGE) {
8686 return 4;
8687 }
8688
8689 // Tests the entire input CBOR being too large when processing bstr wrapping
8690 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTooLarge3), QCBOR_DECODE_MODE_NORMAL);
8691 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
8692 uErr = QCBORDecode_GetError(&DC);
8693 if(uErr != QCBOR_ERR_INPUT_TOO_LARGE) {
8694 return 5;
8695 }
8696
8697 return 0;
8698}
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07008699
8700
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08008701#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
8702
Laurence Lundblade37286c02022-09-03 10:05:02 -07008703/*
8704 An array of three map entries
8705 1) Indefinite length string label for indefinite lenght byte string
8706 2) Indefinite length string label for an integer
8707 3) Indefinite length string label for an indefinite-length negative big num
8708 */
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07008709static const uint8_t spMapWithIndefLenStrings[] = {
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08008710 0xa3,
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07008711 0x7f, 0x61, 'l', 0x64, 'a', 'b', 'e', 'l' , 0x61, '1', 0xff,
8712 0x5f, 0x42, 0x01, 0x02, 0x43, 0x03, 0x04, 0x05, 0xff,
8713 0x7f, 0x62, 'd', 'y', 0x61, 'm', 0x61, 'o', 0xff,
8714 0x03,
8715 0x7f, 0x62, 'l', 'a', 0x63, 'b', 'e', 'l', 0x61, '2', 0xff,
8716 0xc3,
8717 0x5f, 0x42, 0x00, 0x01, 0x42, 0x00, 0x01, 0x41, 0x01, 0xff,
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07008718};
8719
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03008720int32_t SpiffyIndefiniteLengthStringsTests(void)
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07008721{
8722 QCBORDecodeContext DCtx;
8723
8724 QCBORDecode_Init(&DCtx,
8725 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spMapWithIndefLenStrings),
8726 QCBOR_DECODE_MODE_NORMAL);
8727
Laurence Lundblade8510f8c2020-12-01 11:31:16 -08008728 UsefulBuf_MAKE_STACK_UB(StringBuf, 200);
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07008729 QCBORDecode_SetMemPool(&DCtx, StringBuf, false);
8730
8731 UsefulBufC ByteString;
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07008732 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07008733 QCBORDecode_GetByteStringInMapSZ(&DCtx, "label1", &ByteString);
Laurence Lundblade37286c02022-09-03 10:05:02 -07008734
8735#ifndef QCBOR_DISABLE_TAGS
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07008736 if(QCBORDecode_GetAndResetError(&DCtx)) {
8737 return 1;
8738 }
8739
8740 const uint8_t pExectedBytes[] = {0x01, 0x02, 0x03, 0x04, 0x05};
8741 if(UsefulBuf_Compare(ByteString, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pExectedBytes))) {
8742 return 2;
8743 }
8744
8745 uint64_t uInt;
8746 QCBORDecode_GetUInt64InMapSZ(&DCtx, "dymo", &uInt);
8747 if(QCBORDecode_GetAndResetError(&DCtx)) {
8748 return 3;
8749 }
8750 if(uInt != 3) {
8751 return 4;
8752 }
8753
Máté Tóth-Pálef5f07a2021-09-17 19:31:37 +02008754#ifndef USEFULBUF_DISABLE_ALL_FLOAT
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07008755 double uDouble;
8756 QCBORDecode_GetDoubleConvertAllInMapSZ(&DCtx,
8757 "label2",
8758 0xff,
8759 &uDouble);
Laurence Lundblade37286c02022-09-03 10:05:02 -07008760
Laurence Lundbladeb8e19aa2020-10-07 20:59:11 -07008761#ifndef QCBOR_DISABLE_FLOAT_HW_USE
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07008762 if(QCBORDecode_GetAndResetError(&DCtx)) {
8763 return 5;
8764 }
8765 if(uDouble != -16777474) {
8766 return 6;
8767 }
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08008768#else /* QCBOR_DISABLE_FLOAT_HW_USE */
Laurence Lundbladeb8e19aa2020-10-07 20:59:11 -07008769 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_HW_FLOAT_DISABLED) {
8770 return 7;
8771 }
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08008772#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
Máté Tóth-Pálef5f07a2021-09-17 19:31:37 +02008773#endif /* USEFULBUF_DISABLE_ALL_FLOAT */
Laurence Lundbladeb8e19aa2020-10-07 20:59:11 -07008774
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07008775 QCBORDecode_ExitMap(&DCtx);
8776
8777 if(QCBORDecode_Finish(&DCtx)) {
8778 return 99;
8779 }
8780
Laurence Lundblade37286c02022-09-03 10:05:02 -07008781#else /* QCBOR_DISABLE_TAGS */
8782 /* The big num in the input is a CBOR tag and you can't do
8783 * map lookups in a map with a tag so this test does very little
8784 * when tags are disabled. That is OK, the test coverage is still
8785 * good when they are not.
8786 */
8787 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_TAGS_DISABLED) {
8788 return 1002;
8789 }
8790#endif /*QCBOR_DISABLE_TAGS */
8791
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07008792 return 0;
8793}
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08008794#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
Laurence Lundblade2a26abb2020-11-05 19:06:54 -08008795
8796
Laurence Lundbladeec290b82024-06-10 11:10:54 -07008797#ifndef QCBOR_DISABLE_NON_INTEGER_LABELS
Laurence Lundbladecf41c522021-02-20 10:19:07 -07008798/*
8799 * An array of an integer and an array. The second array contains
8800 * a bstr-wrapped map.
8801 *
8802 * [7, [h'A36D6669... (see next lines) 73']]
8803 *
8804 * {"first integer": 42,
8805 * "an array of two strings": ["string1", "string2"],
8806 * "map in a map":
8807 * { "bytes 1": h'78787878',
8808 * "bytes 2": h'79797979',
8809 * "another int": 98,
8810 * "text 2": "lies, damn lies and statistics"
8811 * }
8812 * }
8813 */
Laurence Lundblade2a26abb2020-11-05 19:06:54 -08008814
Laurence Lundbladecf41c522021-02-20 10:19:07 -07008815static const uint8_t pValidWrappedMapEncoded[] = {
8816 0x82, 0x07, 0x81, 0x58, 0x97,
8817 0xa3, 0x6d, 0x66, 0x69, 0x72, 0x73, 0x74, 0x20, 0x69, 0x6e,
8818 0x74, 0x65, 0x67, 0x65, 0x72, 0x18, 0x2a, 0x77, 0x61, 0x6e,
8819 0x20, 0x61, 0x72, 0x72, 0x61, 0x79, 0x20, 0x6f, 0x66, 0x20,
8820 0x74, 0x77, 0x6f, 0x20, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67,
8821 0x73, 0x82, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x31,
8822 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x32, 0x6c, 0x6d,
8823 0x61, 0x70, 0x20, 0x69, 0x6e, 0x20, 0x61, 0x20, 0x6d, 0x61,
8824 0x70, 0xa4, 0x67, 0x62, 0x79, 0x74, 0x65, 0x73, 0x20, 0x31,
8825 0x44, 0x78, 0x78, 0x78, 0x78, 0x67, 0x62, 0x79, 0x74, 0x65,
8826 0x73, 0x20, 0x32, 0x44, 0x79, 0x79, 0x79, 0x79, 0x6b, 0x61,
8827 0x6e, 0x6f, 0x74, 0x68, 0x65, 0x72, 0x20, 0x69, 0x6e, 0x74,
8828 0x18, 0x62, 0x66, 0x74, 0x65, 0x78, 0x74, 0x20, 0x32, 0x78,
8829 0x1e, 0x6c, 0x69, 0x65, 0x73, 0x2c, 0x20, 0x64, 0x61, 0x6d,
8830 0x6e, 0x20, 0x6c, 0x69, 0x65, 0x73, 0x20, 0x61, 0x6e, 0x64,
8831 0x20, 0x73, 0x74, 0x61, 0x74, 0x69, 0x73, 0x74, 0x69, 0x63,
8832 0x73
8833};
8834
8835#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
8836
8837/* As above, but the arrays are indefinite length */
8838static const uint8_t pValidIndefWrappedMapEncoded[] = {
8839 0x9f, 0x07, 0x9f, 0x58, 0x97,
8840 0xa3, 0x6d, 0x66, 0x69, 0x72, 0x73, 0x74, 0x20, 0x69, 0x6e,
8841 0x74, 0x65, 0x67, 0x65, 0x72, 0x18, 0x2a, 0x77, 0x61, 0x6e,
8842 0x20, 0x61, 0x72, 0x72, 0x61, 0x79, 0x20, 0x6f, 0x66, 0x20,
8843 0x74, 0x77, 0x6f, 0x20, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67,
8844 0x73, 0x82, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x31,
8845 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x32, 0x6c, 0x6d,
8846 0x61, 0x70, 0x20, 0x69, 0x6e, 0x20, 0x61, 0x20, 0x6d, 0x61,
8847 0x70, 0xa4, 0x67, 0x62, 0x79, 0x74, 0x65, 0x73, 0x20, 0x31,
8848 0x44, 0x78, 0x78, 0x78, 0x78, 0x67, 0x62, 0x79, 0x74, 0x65,
8849 0x73, 0x20, 0x32, 0x44, 0x79, 0x79, 0x79, 0x79, 0x6b, 0x61,
8850 0x6e, 0x6f, 0x74, 0x68, 0x65, 0x72, 0x20, 0x69, 0x6e, 0x74,
8851 0x18, 0x62, 0x66, 0x74, 0x65, 0x78, 0x74, 0x20, 0x32, 0x78,
8852 0x1e, 0x6c, 0x69, 0x65, 0x73, 0x2c, 0x20, 0x64, 0x61, 0x6d,
8853 0x6e, 0x20, 0x6c, 0x69, 0x65, 0x73, 0x20, 0x61, 0x6e, 0x64,
8854 0x20, 0x73, 0x74, 0x61, 0x74, 0x69, 0x73, 0x74, 0x69, 0x63,
8855 0x73,
8856 0xff, 0xff
8857};
Laurence Lundbladeec290b82024-06-10 11:10:54 -07008858#endif /* ! QCBOR_DISABLE_NON_INTEGER_LABELS */
Laurence Lundbladecf41c522021-02-20 10:19:07 -07008859
8860static const uint8_t pWithEmptyMap[] = {0x82, 0x18, 0x64, 0xa0};
8861
Laurence Lundbladeec290b82024-06-10 11:10:54 -07008862
Laurence Lundbladecf41c522021-02-20 10:19:07 -07008863#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
8864static const uint8_t pWithEmptyMapInDef[] = {0x9f, 0x18, 0x64, 0xbf, 0xff, 0xff};
8865#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
8866
8867#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
Laurence Lundblade37286c02022-09-03 10:05:02 -07008868/*
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08008869 * An array of one that contains a byte string that is an indefinite
8870 * length string that CBOR wraps an array of three numbers [42, 43,
8871 * 44]. The byte string is an implicit tag 24.
8872 *
8873 * [
8874 * (_ h'83', h'18', h'2A182B', h'182C')
8875 * ]
Laurence Lundblade37286c02022-09-03 10:05:02 -07008876 */
Laurence Lundbladecf41c522021-02-20 10:19:07 -07008877static const uint8_t pWrappedByIndefiniteLength[] = {
8878 0x81,
Laurence Lundbladecf41c522021-02-20 10:19:07 -07008879 0x5f,
8880 0x41, 0x83,
8881 0x41, 0x18,
8882 0x43, 0x2A, 0x18, 0x2B,
8883 0x42, 0x18, 0x2C,
8884 0xff
8885};
8886#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
8887
Laurence Lundbladeec290b82024-06-10 11:10:54 -07008888#endif
Laurence Lundbladecf41c522021-02-20 10:19:07 -07008889
Laurence Lundbladeec290b82024-06-10 11:10:54 -07008890#ifndef QCBOR_DISABLE_NON_INTEGER_LABELS
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03008891int32_t PeekAndRewindTest(void)
Laurence Lundblade2a26abb2020-11-05 19:06:54 -08008892{
8893 QCBORItem Item;
8894 QCBORError nCBORError;
8895 QCBORDecodeContext DCtx;
8896
Laurence Lundbladeec290b82024-06-10 11:10:54 -07008897 // Improvement: rework this test to use only integer labels.
8898
Laurence Lundblade2a26abb2020-11-05 19:06:54 -08008899 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0);
8900
8901 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
8902 return 100+(int32_t)nCBORError;
8903 }
8904 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
8905 return 200;
8906 }
8907
Laurence Lundblade3427dee2021-06-20 11:11:24 -07008908 QCBORDecode_VPeekNext(&DCtx, &Item);
8909 if((nCBORError = QCBORDecode_GetError(&DCtx))) {
8910 return 150+(int32_t)nCBORError;
8911 }
8912 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
8913 return 250;
8914 }
8915
Laurence Lundblade2a26abb2020-11-05 19:06:54 -08008916 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
8917 return (int32_t)nCBORError;
8918 }
8919 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
8920 return 300;
8921 }
8922
8923 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
8924 return 400 + (int32_t)nCBORError;
8925 }
8926 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
8927 return 500;
8928 }
8929
8930 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8931 return (int32_t)nCBORError;
8932 }
8933 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
8934 return 600;
8935 }
8936
8937 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
8938 return 900 + (int32_t)nCBORError;
8939 }
8940 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
8941 Item.uDataType != QCBOR_TYPE_INT64 ||
8942 Item.val.int64 != 42 ||
8943 Item.uDataAlloc ||
8944 Item.uLabelAlloc ||
8945 UsefulBufCompareToSZ(Item.label.string, "first integer")) {
8946 return 1000;
8947 }
8948
8949 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8950 return 1100 + (int32_t)nCBORError;
8951 }
8952
8953 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
8954 Item.uDataType != QCBOR_TYPE_INT64 ||
8955 Item.val.int64 != 42 ||
8956 Item.uDataAlloc ||
8957 Item.uLabelAlloc ||
8958 UsefulBufCompareToSZ(Item.label.string, "first integer")) {
8959 return 1200;
8960 }
8961
8962
8963 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8964 return 1300 + (int32_t)nCBORError;
8965 }
8966 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
8967 Item.uDataAlloc ||
8968 Item.uLabelAlloc ||
8969 UsefulBufCompareToSZ(Item.label.string, "an array of two strings") ||
8970 Item.uDataType != QCBOR_TYPE_ARRAY ||
Laurence Lundbladecf41c522021-02-20 10:19:07 -07008971 Item.val.uCount != 2) {
Laurence Lundblade2a26abb2020-11-05 19:06:54 -08008972 return 1400;
Laurence Lundbladecf41c522021-02-20 10:19:07 -07008973 }
Laurence Lundblade2a26abb2020-11-05 19:06:54 -08008974
8975 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8976 return 1500 + (int32_t)nCBORError;
8977 }
8978 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
8979 Item.uDataAlloc ||
8980 Item.uLabelAlloc ||
8981 UsefulBufCompareToSZ(Item.val.string, "string1")) {
8982 return 1600;
8983 }
8984
8985 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
8986 return 1700 + (int32_t)nCBORError;
8987 }
8988 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
8989 Item.uDataAlloc ||
8990 Item.uLabelAlloc ||
8991 UsefulBufCompareToSZ(Item.val.string, "string2")) {
8992 return 1800;
8993 }
8994
8995 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
8996 return (int32_t)nCBORError;
8997 }
8998 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
8999 Item.uDataAlloc ||
9000 Item.uLabelAlloc ||
9001 UsefulBufCompareToSZ(Item.val.string, "string2")) {
9002 return 1900;
9003 }
9004
9005 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
9006 return (int32_t)nCBORError;
9007 }
9008 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
9009 Item.uDataAlloc ||
9010 Item.uLabelAlloc ||
9011 UsefulBufCompareToSZ(Item.val.string, "string2")) {
9012 return 2000;
9013 }
9014
9015
9016 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
9017 return 2100 + (int32_t)nCBORError;
9018 }
9019 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
9020 Item.uDataAlloc ||
9021 Item.uLabelAlloc ||
9022 UsefulBufCompareToSZ(Item.label.string, "map in a map") ||
9023 Item.uDataType != QCBOR_TYPE_MAP ||
9024 Item.val.uCount != 4) {
9025 return 2100;
9026 }
9027
9028 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
9029 return 2200 + (int32_t)nCBORError;
9030 }
9031 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
9032 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("bytes 1"))||
9033 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
9034 Item.uDataAlloc ||
9035 Item.uLabelAlloc ||
9036 UsefulBufCompareToSZ(Item.val.string, "xxxx")) {
9037 return 2300;
9038 }
9039
9040 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
9041 return 2400 + (int32_t)nCBORError;
9042 }
9043 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
9044 UsefulBufCompareToSZ(Item.label.string, "bytes 2") ||
9045 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
9046 Item.uDataAlloc ||
9047 Item.uLabelAlloc ||
9048 UsefulBufCompareToSZ(Item.val.string, "yyyy")) {
9049 return 2500;
9050 }
9051
9052 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
9053 return 2600 + (int32_t)nCBORError;
9054 }
9055 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
9056 UsefulBufCompareToSZ(Item.label.string, "bytes 2") ||
9057 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
9058 Item.uDataAlloc ||
9059 Item.uLabelAlloc ||
9060 UsefulBufCompareToSZ(Item.val.string, "yyyy")) {
9061 return 2700;
9062 }
9063
9064 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
9065 return 2800 + (int32_t)nCBORError;
9066 }
9067 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
9068 Item.uDataAlloc ||
9069 Item.uLabelAlloc ||
9070 UsefulBufCompareToSZ(Item.label.string, "another int") ||
9071 Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundbladecf41c522021-02-20 10:19:07 -07009072 Item.val.int64 != 98) {
Laurence Lundblade2a26abb2020-11-05 19:06:54 -08009073 return 2900;
Laurence Lundbladecf41c522021-02-20 10:19:07 -07009074 }
Laurence Lundblade2a26abb2020-11-05 19:06:54 -08009075
9076 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
9077 return 3000 + (int32_t)nCBORError;
9078 }
9079 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
9080 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("text 2"))||
9081 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
9082 Item.uDataAlloc ||
9083 Item.uLabelAlloc ||
9084 UsefulBufCompareToSZ(Item.val.string, "lies, damn lies and statistics")) {
9085 return 3100;
9086 }
9087
9088 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
9089 return 3200 + (int32_t)nCBORError;
9090 }
9091 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
9092 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("text 2"))||
9093 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
9094 Item.uDataAlloc ||
9095 Item.uLabelAlloc ||
9096 UsefulBufCompareToSZ(Item.val.string, "lies, damn lies and statistics")) {
9097 return 3300;
9098 }
9099
Laurence Lundblade3427dee2021-06-20 11:11:24 -07009100 nCBORError = QCBORDecode_PeekNext(&DCtx, &Item);
9101 if(nCBORError != QCBOR_ERR_NO_MORE_ITEMS) {
9102 return 3300 + (int32_t)nCBORError;
9103 }
9104
9105 QCBORDecode_VPeekNext(&DCtx, &Item);
9106 nCBORError = QCBORDecode_GetError(&DCtx);
9107 if(nCBORError != QCBOR_ERR_NO_MORE_ITEMS) {
9108 return 3400 + (int32_t)nCBORError;
9109 }
9110
9111 QCBORDecode_VPeekNext(&DCtx, &Item);
9112 nCBORError = QCBORDecode_GetError(&DCtx);
9113 if(nCBORError != QCBOR_ERR_NO_MORE_ITEMS) {
9114 return 3500 + (int32_t)nCBORError;
9115 }
Laurence Lundbladecf41c522021-02-20 10:19:07 -07009116
9117
9118 // Rewind to top level after entering several maps
9119 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0);
9120
9121 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
9122 return (int32_t)nCBORError;
9123 }
9124 if(Item.uDataType != QCBOR_TYPE_MAP ||
9125 Item.val.uCount != 3) {
9126 return 400;
9127 }
9128
9129 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
9130 return 4000+(int32_t)nCBORError;
9131 }
9132
9133 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
9134 Item.uDataType != QCBOR_TYPE_INT64 ||
9135 Item.val.int64 != 42 ||
9136 Item.uDataAlloc ||
9137 Item.uLabelAlloc ||
9138 UsefulBufCompareToSZ(Item.label.string, "first integer")) {
9139 return 4100;
9140 }
9141
9142 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
9143 return 4100+(int32_t)nCBORError;
9144 }
9145 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
9146 Item.uDataAlloc ||
9147 Item.uLabelAlloc ||
9148 UsefulBufCompareToSZ(Item.label.string, "an array of two strings") ||
9149 Item.uDataType != QCBOR_TYPE_ARRAY ||
9150 Item.val.uCount != 2) {
9151 return 4200;
9152 }
9153
9154 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
9155 return 4200+(int32_t)nCBORError;
9156 }
9157 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
9158 Item.uDataAlloc ||
9159 Item.uLabelAlloc ||
9160 UsefulBufCompareToSZ(Item.val.string, "string1")) {
9161 return 4300;
9162 }
9163
9164 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
9165 return 4300+(int32_t)nCBORError;
9166 }
9167 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
9168 Item.uDataAlloc ||
9169 Item.uLabelAlloc ||
9170 UsefulBufCompareToSZ(Item.val.string, "string2")) {
9171 return 4400;
9172 }
9173
9174 QCBORDecode_Rewind(&DCtx);
9175
9176 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
9177 return 4400+(int32_t)nCBORError;
9178 }
9179 if(Item.uDataType != QCBOR_TYPE_MAP ||
9180 Item.val.uCount != 3) {
9181 return 4500;
9182 }
9183
9184 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
9185 return (int32_t)nCBORError;
9186 }
9187
9188 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
9189 Item.uDataType != QCBOR_TYPE_INT64 ||
9190 Item.val.int64 != 42 ||
9191 Item.uDataAlloc ||
9192 Item.uLabelAlloc ||
9193 UsefulBufCompareToSZ(Item.label.string, "first integer")) {
9194 return 4600;
9195 }
9196
9197 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
9198 return (int32_t)nCBORError;
9199 }
9200 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
9201 Item.uDataAlloc ||
9202 Item.uLabelAlloc ||
9203 UsefulBufCompareToSZ(Item.label.string, "an array of two strings") ||
9204 Item.uDataType != QCBOR_TYPE_ARRAY ||
9205 Item.val.uCount != 2) {
9206 return 4700;
9207 }
9208
9209 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
9210 return (int32_t)nCBORError;
9211 }
9212 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
9213 Item.uDataAlloc ||
9214 Item.uLabelAlloc ||
9215 UsefulBufCompareToSZ(Item.val.string, "string1")) {
9216 return 4800;
9217 }
9218
9219 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
9220 return 4900+(int32_t)nCBORError;
9221 }
9222 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
9223 Item.uDataAlloc ||
9224 Item.uLabelAlloc ||
9225 UsefulBufCompareToSZ(Item.val.string, "string2")) {
9226 return 5000;
9227 }
9228
9229
9230 // Rewind an entered map
9231 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0);
9232
9233 QCBORDecode_EnterMap(&DCtx, NULL);
9234
9235 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
9236 return 5100+(int32_t)nCBORError;
9237 }
9238
9239 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
9240 Item.uDataType != QCBOR_TYPE_INT64 ||
9241 Item.val.int64 != 42 ||
9242 Item.uDataAlloc ||
9243 Item.uLabelAlloc ||
9244 UsefulBufCompareToSZ(Item.label.string, "first integer")) {
9245 return 5200;
9246 }
9247
9248 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
9249 return 5200+(int32_t)nCBORError;
9250 }
9251 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
9252 Item.uDataAlloc ||
9253 Item.uLabelAlloc ||
9254 UsefulBufCompareToSZ(Item.label.string, "an array of two strings") ||
9255 Item.uDataType != QCBOR_TYPE_ARRAY ||
9256 Item.val.uCount != 2) {
9257 return -5300;
9258 }
9259
9260 QCBORDecode_Rewind(&DCtx);
9261
9262 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
9263 return 5300+(int32_t)nCBORError;
9264 }
9265
9266 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
9267 Item.uDataType != QCBOR_TYPE_INT64 ||
9268 Item.val.int64 != 42 ||
9269 Item.uDataAlloc ||
9270 Item.uLabelAlloc ||
9271 UsefulBufCompareToSZ(Item.label.string, "first integer")) {
9272 return 5400;
9273 }
9274
9275 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
9276 return 5400+(int32_t)nCBORError;
9277 }
9278 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
9279 Item.uDataAlloc ||
9280 Item.uLabelAlloc ||
9281 UsefulBufCompareToSZ(Item.label.string, "an array of two strings") ||
9282 Item.uDataType != QCBOR_TYPE_ARRAY ||
9283 Item.val.uCount != 2) {
9284 return 5500;
9285 }
9286
9287
9288 // Rewind and entered array inside an entered map
9289 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0);
9290
9291 QCBORDecode_EnterMap(&DCtx, NULL);
9292
9293 QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings");
9294
9295 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
9296 return 5600+(int32_t)nCBORError;
9297 }
9298 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
9299 Item.uDataAlloc ||
9300 Item.uLabelAlloc ||
9301 UsefulBufCompareToSZ(Item.val.string, "string1")) {
9302 return 5700;
9303 }
9304
9305 QCBORDecode_Rewind(&DCtx);
9306
9307 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
9308 return 5700+(int32_t)nCBORError;
9309 }
9310 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
9311 Item.uDataAlloc ||
9312 Item.uLabelAlloc ||
9313 UsefulBufCompareToSZ(Item.val.string, "string1")) {
9314 return 5800;
9315 }
9316
9317 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
9318 return (int32_t)nCBORError;
9319 }
9320 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
9321 Item.uDataAlloc ||
9322 Item.uLabelAlloc ||
9323 UsefulBufCompareToSZ(Item.val.string, "string2")) {
9324 return 5900;
9325 }
9326
9327 QCBORDecode_Rewind(&DCtx);
9328
9329 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
9330 return 5900+(int32_t)nCBORError;
9331 }
9332 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
9333 Item.uDataAlloc ||
9334 Item.uLabelAlloc ||
9335 UsefulBufCompareToSZ(Item.val.string, "string1")) {
9336 return 6000;
9337 }
9338
9339
9340 // Rewind a byte string inside an array inside an array
9341 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidWrappedMapEncoded), 0);
9342
9343 QCBORDecode_EnterArray(&DCtx, NULL);
9344
9345 uint64_t i;
9346 QCBORDecode_GetUInt64(&DCtx, &i);
9347
9348 QCBORDecode_EnterArray(&DCtx, NULL);
9349
9350 QCBORDecode_EnterBstrWrapped(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
9351 if(QCBORDecode_GetError(&DCtx)) {
9352 return 6100;
9353 }
9354
9355 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
9356 return (int32_t)nCBORError;
9357 }
9358 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
9359 return 6200;
9360 }
9361
9362 QCBORDecode_Rewind(&DCtx);
9363
9364 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
9365 return 6300+(int32_t)nCBORError;
9366 }
9367 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
9368 return 6400;
9369 }
9370
9371#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
9372 // Rewind a byte string inside an indefinite-length array inside
9373 // indefinite-length array
9374
9375 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidIndefWrappedMapEncoded), 0);
9376
9377 QCBORDecode_EnterArray(&DCtx, NULL);
9378
9379 QCBORDecode_GetUInt64(&DCtx, &i);
9380
9381 QCBORDecode_EnterArray(&DCtx, NULL);
9382
9383 QCBORDecode_EnterBstrWrapped(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
9384 if(QCBORDecode_GetError(&DCtx)) {
9385 return 6500;
9386 }
9387
9388 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
9389 return 6600+(int32_t)nCBORError;
9390 }
9391 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
9392 return 6700;
9393 }
9394
9395 QCBORDecode_Rewind(&DCtx);
9396
9397 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
9398 return 6800+(int32_t)nCBORError;
9399 }
9400 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
9401 return 6900;
9402 }
9403#endif
9404
9405 // Rewind an empty map
9406 // [100, {}]
9407 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pWithEmptyMap), 0);
9408 QCBORDecode_EnterArray(&DCtx, NULL);
9409 QCBORDecode_GetUInt64(&DCtx, &i);
9410 if(i != 100) {
9411 return 7010;
9412 }
9413 QCBORDecode_EnterMap(&DCtx, NULL);
9414
9415 /* Do it 5 times to be sure multiple rewinds work */
9416 for(int n = 0; n < 5; n++) {
9417 nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
9418 if(nCBORError != QCBOR_ERR_NO_MORE_ITEMS) {
9419 return 7000 + n;
9420 }
9421 QCBORDecode_Rewind(&DCtx);
9422 }
9423 QCBORDecode_ExitMap(&DCtx);
9424 QCBORDecode_Rewind(&DCtx);
9425 QCBORDecode_GetUInt64(&DCtx, &i);
9426 if(i != 100) {
9427 return 7010;
9428 }
9429 QCBORDecode_ExitArray(&DCtx);
9430 QCBORDecode_Rewind(&DCtx);
9431 QCBORDecode_EnterArray(&DCtx, NULL);
9432 i = 9;
9433 QCBORDecode_GetUInt64(&DCtx, &i);
9434 if(i != 100) {
9435 return 7020;
9436 }
9437 if(QCBORDecode_GetError(&DCtx)){
9438 return 7030;
9439 }
9440
9441 // Rewind an empty indefinite length map
9442#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
9443 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pWithEmptyMapInDef), 0);
9444 QCBORDecode_EnterArray(&DCtx, NULL);
9445 QCBORDecode_GetUInt64(&DCtx, &i);
9446 if(i != 100) {
9447 return 7810;
9448 }
9449 QCBORDecode_EnterMap(&DCtx, NULL);
9450
9451 /* Do it 5 times to be sure multiple rewinds work */
9452 for(int n = 0; n < 5; n++) {
9453 nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
9454 if(nCBORError != QCBOR_ERR_NO_MORE_ITEMS) {
9455 return 7800 + n;
9456 }
9457 QCBORDecode_Rewind(&DCtx);
9458 }
9459 QCBORDecode_ExitMap(&DCtx);
9460 QCBORDecode_Rewind(&DCtx);
9461 QCBORDecode_GetUInt64(&DCtx, &i);
9462 if(i != 100) {
9463 return 7810;
9464 }
9465 QCBORDecode_ExitArray(&DCtx);
9466 QCBORDecode_Rewind(&DCtx);
9467 QCBORDecode_EnterArray(&DCtx, NULL);
9468 i = 9;
9469 QCBORDecode_GetUInt64(&DCtx, &i);
9470 if(i != 100) {
9471 return 7820;
9472 }
9473 if(QCBORDecode_GetError(&DCtx)){
9474 return 7830;
9475 }
9476#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
9477
9478 // Rewind an indefnite length byte-string wrapped sequence
9479#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
9480 QCBORDecode_Init(&DCtx,
9481 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pWrappedByIndefiniteLength),
9482 0);
9483 UsefulBuf_MAKE_STACK_UB(Pool, 100);
9484 QCBORDecode_SetMemPool(&DCtx, Pool, 0);
9485
9486 QCBORDecode_EnterArray(&DCtx, NULL);
9487 QCBORDecode_EnterBstrWrapped(&DCtx, 2, NULL);
Laurence Lundblade31fddb72024-05-13 13:03:35 -07009488 if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_CANNOT_ENTER_ALLOCATED_STRING) {
Laurence Lundbladecf41c522021-02-20 10:19:07 -07009489 return 7300;
9490 }
9491
9492 /*
Laurence Lundblade31fddb72024-05-13 13:03:35 -07009493 Improvement: Fix QCBORDecode_EnterBstrWrapped() so it can work on
9494 allocated strings. This is a fairly big job because of all the
9495 UsefulBuf internal book keeping that needs tweaking.
Laurence Lundbladecf41c522021-02-20 10:19:07 -07009496 QCBORDecode_GetUInt64(&DCtx, &i);
9497 if(i != 42) {
9498 return 7110;
9499 }
9500 QCBORDecode_Rewind(&DCtx);
9501 QCBORDecode_GetUInt64(&DCtx, &i);
9502 if(i != 42) {
9503 return 7220;
Laurence Lundblade31fddb72024-05-13 13:03:35 -07009504 }
9505 */
Laurence Lundblade37286c02022-09-03 10:05:02 -07009506
Laurence Lundbladeec290b82024-06-10 11:10:54 -07009507#endif /* ! QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
Laurence Lundbladecf41c522021-02-20 10:19:07 -07009508
9509
9510 // Rewind an indefnite length byte-string wrapped sequence
9511
Laurence Lundblade2a26abb2020-11-05 19:06:54 -08009512 return 0;
9513}
Laurence Lundblade9f9c3732021-03-23 09:38:46 -07009514
Laurence Lundbladeec290b82024-06-10 11:10:54 -07009515#endif /* ! QCBOR_DISABLE_NON_INTEGER_LABELS */
Laurence Lundblade9f9c3732021-03-23 09:38:46 -07009516
9517
9518static const uint8_t spBooleansInMap[] =
9519{
9520 0xa1, 0x08, 0xf5
9521};
9522
9523static const uint8_t spBooleansInMapWrongType[] =
9524{
9525 0xa1, 0x08, 0xf6
9526};
9527
9528static const uint8_t spBooleansInMapNWF[] =
9529{
9530 0xa1, 0x08, 0x1a
9531};
9532
Laurence Lundblade8782dd32021-04-27 04:15:37 -07009533static const uint8_t spNullInMap[] =
9534{
9535 0xa1, 0x08, 0xf6
9536};
9537
9538static const uint8_t spUndefinedInMap[] =
9539{
9540 0xa1, 0x08, 0xf7
9541};
9542
Laurence Lundblade9f9c3732021-03-23 09:38:46 -07009543
Laurence Lundbladecdbbc192024-06-28 15:13:04 -07009544#ifndef QCBOR_DISABLE_TAGS
9545static const uint8_t spTaggedSimples[] =
9546{
9547 0xd8, 0x58, 0xd8, 0x2c, 0xd6, 0xf5,
9548 0xd9, 0x0f, 0xA0, 0xf7
9549};
9550#endif /* ! QCBOR_DISABLE_TAGS */
9551
9552
9553
Laurence Lundblade9f9c3732021-03-23 09:38:46 -07009554int32_t BoolTest(void)
9555{
9556 QCBORDecodeContext DCtx;
9557 bool b;
9558
Laurence Lundblade8782dd32021-04-27 04:15:37 -07009559 QCBORDecode_Init(&DCtx,
9560 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMap),
9561 0);
Laurence Lundblade9f9c3732021-03-23 09:38:46 -07009562 QCBORDecode_EnterMap(&DCtx, NULL);
9563 QCBORDecode_GetBool(&DCtx, &b);
9564 if(QCBORDecode_GetAndResetError(&DCtx) || !b) {
9565 return 1;
9566 }
9567
9568 QCBORDecode_GetBoolInMapN(&DCtx, 7, &b);
9569 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_LABEL_NOT_FOUND) {
9570 return 2;
9571 }
9572
9573 QCBORDecode_GetBoolInMapN(&DCtx, 8, &b);
9574 if(QCBORDecode_GetAndResetError(&DCtx) || !b) {
9575 return 3;
9576 }
9577
9578
9579 QCBORDecode_GetBoolInMapSZ(&DCtx, "xx", &b);
9580 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_LABEL_NOT_FOUND) {
9581 return 4;
9582 }
9583
Laurence Lundblade8782dd32021-04-27 04:15:37 -07009584 QCBORDecode_Init(&DCtx,
9585 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMapWrongType),
9586 0);
Laurence Lundblade9f9c3732021-03-23 09:38:46 -07009587 QCBORDecode_EnterMap(&DCtx, NULL);
9588 QCBORDecode_GetBool(&DCtx, &b);
9589 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
9590 return 5;
9591 }
9592
Laurence Lundblade8782dd32021-04-27 04:15:37 -07009593 QCBORDecode_Init(&DCtx,
9594 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMapNWF),
9595 0);
Laurence Lundblade9f9c3732021-03-23 09:38:46 -07009596 QCBORDecode_EnterMap(&DCtx, NULL);
9597 QCBORDecode_GetBool(&DCtx, &b);
9598 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_HIT_END) {
9599 return 6;
9600 }
9601
Laurence Lundblade8782dd32021-04-27 04:15:37 -07009602
9603 QCBORDecode_Init(&DCtx,
9604 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spNullInMap),
9605 0);
9606 QCBORDecode_EnterMap(&DCtx, NULL);
9607 QCBORDecode_GetNull(&DCtx);
9608 if(QCBORDecode_GetAndResetError(&DCtx)) {
9609 return 7;
9610 }
9611
9612 QCBORDecode_Init(&DCtx,
9613 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMap),
9614 0);
9615 QCBORDecode_EnterMap(&DCtx, NULL);
9616 QCBORDecode_GetNull(&DCtx);
9617 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
9618 return 8;
9619 }
9620
9621 QCBORDecode_Init(&DCtx,
9622 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spNullInMap),
9623 0);
9624 QCBORDecode_EnterMap(&DCtx, NULL);
9625 QCBORDecode_GetNullInMapN(&DCtx, 8);
9626 if(QCBORDecode_GetAndResetError(&DCtx)) {
9627 return 9;
9628 }
9629
9630 QCBORDecode_Init(&DCtx,
9631 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMap),
9632 0);
9633 QCBORDecode_EnterMap(&DCtx, NULL);
9634 QCBORDecode_GetNullInMapN(&DCtx, 8);
9635 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
9636 return 10;
9637 }
9638
9639 QCBORDecode_Init(&DCtx,
9640 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMapNWF),
9641 0);
9642 QCBORDecode_EnterMap(&DCtx, NULL);
9643 QCBORDecode_GetUndefined(&DCtx);
9644 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_HIT_END) {
9645 return 11;
9646 }
9647
9648 QCBORDecode_Init(&DCtx,
9649 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUndefinedInMap),
9650 0);
9651 QCBORDecode_EnterMap(&DCtx, NULL);
9652 QCBORDecode_GetUndefined(&DCtx);
9653 if(QCBORDecode_GetAndResetError(&DCtx)) {
9654 return 12;
9655 }
9656
9657 QCBORDecode_Init(&DCtx,
9658 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMap),
9659 0);
9660 QCBORDecode_EnterMap(&DCtx, NULL);
9661 QCBORDecode_GetUndefined(&DCtx);
9662 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
9663 return 13;
9664 }
9665
9666 QCBORDecode_Init(&DCtx,
9667 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUndefinedInMap),
9668 0);
9669 QCBORDecode_EnterMap(&DCtx, NULL);
9670 QCBORDecode_GetUndefinedInMapN(&DCtx, 8);
9671 if(QCBORDecode_GetAndResetError(&DCtx)) {
9672 return 14;
9673 }
9674
9675 QCBORDecode_Init(&DCtx,
9676 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMap),
9677 0);
9678 QCBORDecode_EnterMap(&DCtx, NULL);
9679 QCBORDecode_GetUndefinedInMapN(&DCtx, 8);
9680 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
9681 return 15;
9682 }
9683
9684 QCBORDecode_Init(&DCtx,
9685 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMapNWF),
9686 0);
9687 QCBORDecode_EnterMap(&DCtx, NULL);
9688 QCBORDecode_GetUndefined(&DCtx);
9689 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_HIT_END) {
9690 return 15;
9691 }
9692
Laurence Lundbladecdbbc192024-06-28 15:13:04 -07009693#ifndef QCBOR_DISABLE_TAGS
9694 QCBORDecode_Init(&DCtx,
9695 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTaggedSimples),
9696 0);
9697 QCBORDecode_GetBool(&DCtx, &b);
9698 if(QCBORDecode_GetNthTagOfLast(&DCtx, 0) != 22) {
9699 return 401;
9700 }
9701 if(QCBORDecode_GetNthTagOfLast(&DCtx, 1) != 44) {
9702 return 402;
9703 }
9704 if(QCBORDecode_GetNthTagOfLast(&DCtx, 2) != 88) {
9705 return 403;
9706 }
9707 if(QCBORDecode_GetNthTagOfLast(&DCtx, 3) != CBOR_TAG_INVALID64) {
9708 return 404;
9709 }
9710 QCBORDecode_GetUndefined(&DCtx);
9711 if(QCBORDecode_GetNthTagOfLast(&DCtx, 0) != 4000) {
9712 return 405;
9713 }
9714 if(QCBORDecode_GetNthTagOfLast(&DCtx, 1) != CBOR_TAG_INVALID64) {
9715 return 406;
9716 }
9717 QCBORDecode_GetNull(&DCtx); /* Off the end */
9718 if(QCBORDecode_GetNthTagOfLast(&DCtx, 0) != CBOR_TAG_INVALID64) {
9719 return 407;
9720 }
9721#endif /* ! QCBOR_DISABLE_TAGS */
9722
Laurence Lundblade9f9c3732021-03-23 09:38:46 -07009723 return 0;
9724}
Laurence Lundbladef00b8be2024-03-08 10:34:33 -08009725
9726
Laurence Lundbladeec290b82024-06-10 11:10:54 -07009727#ifndef QCBOR_DISABLE_NON_INTEGER_LABELS
Laurence Lundbladea29f45a2024-05-14 15:55:19 -07009728static const uint8_t spExpectedArray2s[] = {
9729 0x82, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67,
9730 0x31, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67,
9731 0x32};
9732
9733#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
9734static const uint8_t spExpectedArray2sIndef[] = {
9735 0x9f, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67,
9736 0x31, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67,
9737 0x32, 0xff};
9738#endif
9739
9740static const uint8_t spExpectedMap4[] = {
9741 0xa4, 0x67, 0x62, 0x79, 0x74, 0x65, 0x73, 0x20,
9742 0x31, 0x44, 0x78, 0x78, 0x78, 0x78, 0x67, 0x62,
9743 0x79, 0x74, 0x65, 0x73, 0x20, 0x32, 0x44, 0x79,
9744 0x79, 0x79, 0x79, 0x6b, 0x61, 0x6e, 0x6f, 0x74,
9745 0x68, 0x65, 0x72, 0x20, 0x69, 0x6e, 0x74, 0x18,
9746 0x62, 0x66, 0x74, 0x65, 0x78, 0x74, 0x20, 0x32,
9747 0x78, 0x1e, 0x6c, 0x69, 0x65, 0x73, 0x2c, 0x20,
9748 0x64, 0x61, 0x6d, 0x6e, 0x20, 0x6c, 0x69, 0x65,
9749 0x73, 0x20, 0x61, 0x6e, 0x64, 0x20, 0x73, 0x74,
9750 0x61, 0x74, 0x69, 0x73, 0x74, 0x69, 0x63, 0x73};
9751
9752
9753#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
9754
9755static const uint8_t spExpectedMap4Indef[] = {
9756 0xbf, 0x67, 0x62, 0x79, 0x74, 0x65, 0x73, 0x20,
9757 0x31, 0x44, 0x78, 0x78, 0x78, 0x78, 0x67, 0x62,
9758 0x79, 0x74, 0x65, 0x73, 0x20, 0x32, 0x44, 0x79,
9759 0x79, 0x79, 0x79, 0x6b, 0x61, 0x6e, 0x6f, 0x74,
9760 0x68, 0x65, 0x72, 0x20, 0x69, 0x6e, 0x74, 0x18,
9761 0x62, 0x66, 0x74, 0x65, 0x78, 0x74, 0x20, 0x32,
9762 0x78, 0x1e, 0x6c, 0x69, 0x65, 0x73, 0x2c, 0x20,
9763 0x64, 0x61, 0x6d, 0x6e, 0x20, 0x6c, 0x69, 0x65,
9764 0x73, 0x20, 0x61, 0x6e, 0x64, 0x20, 0x73, 0x74,
9765 0x61, 0x74, 0x69, 0x73, 0x74, 0x69, 0x63, 0x73,
9766 0xff};
9767
9768/*
9769 * [[[[[0, []]]]], 0]
9770 */
9771static const uint8_t spDefAndIndef[] = {
9772 0x82,
9773 0x9f, 0x9f, 0x9f, 0x82, 0x00, 0x9f, 0xff, 0xff, 0xff, 0xff, 0x00
9774};
9775#endif /* !QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
9776
Laurence Lundbladea29f45a2024-05-14 15:55:19 -07009777#ifndef QCBOR_DISABLE_TAGS
9778/* An exp / mant tag in two nested arrays */
9779static const uint8_t spExpMant[] = {0x81, 0x81, 0xC4, 0x82, 0x20, 0x03};
9780#endif /* !QCBOR_DISABLE_TAGS */
Laurence Lundbladeec290b82024-06-10 11:10:54 -07009781#endif
Laurence Lundbladea29f45a2024-05-14 15:55:19 -07009782
Laurence Lundbladeec290b82024-06-10 11:10:54 -07009783#ifndef QCBOR_DISABLE_NON_INTEGER_LABELS
Laurence Lundbladea29f45a2024-05-14 15:55:19 -07009784int32_t GetMapAndArrayTest(void)
9785{
9786 QCBORDecodeContext DCtx;
9787 size_t uPosition ;
9788 QCBORItem Item;
9789 UsefulBufC ReturnedEncodedCBOR;
9790
Laurence Lundbladeec290b82024-06-10 11:10:54 -07009791 // Improvement: rework so it can run with QCBOR_DISABLE_NON_INTEGER_LABELS
Laurence Lundbladea29f45a2024-05-14 15:55:19 -07009792 QCBORDecode_Init(&DCtx,
9793 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded),
9794 0);
9795
9796 QCBORDecode_EnterMap(&DCtx, NULL);
9797 QCBORDecode_VGetNextConsume(&DCtx, &Item);
9798 QCBORDecode_GetArray(&DCtx, &Item, &ReturnedEncodedCBOR);
9799 if(QCBORDecode_GetError(&DCtx)) {
9800 return 1;
9801 }
9802 if(Item.val.uCount != 2) {
9803 return 2;
9804 }
9805 if(UsefulBuf_Compare(ReturnedEncodedCBOR, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedArray2s))) {
9806 return 3;
9807 }
9808
9809 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
9810 UsefulBuf_Compare(Item.label.string, UsefulBuf_FROM_SZ_LITERAL("an array of two strings"))) {
9811 return 4;
9812 }
9813
9814 uPosition = QCBORDecode_Tell(&DCtx);
9815
9816
9817 QCBORDecode_GetMap(&DCtx, &Item, &ReturnedEncodedCBOR);
9818 if(QCBORDecode_GetError(&DCtx)) {
9819 return 10;
9820 }
9821 if(Item.val.uCount != 4) {
9822 return 11;
9823 }
9824 if(UsefulBuf_Compare(ReturnedEncodedCBOR, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedMap4))) {
9825 return 12;
9826 }
9827 uPosition = QCBORDecode_Tell(&DCtx);
9828 QCBORDecode_GetArrayFromMapSZ(&DCtx,
9829 "an array of two strings",
9830 &Item,
9831 &ReturnedEncodedCBOR);
9832 if(QCBORDecode_GetError(&DCtx)) {
9833 return 20;
9834 }
9835 if(Item.val.uCount != 2) {
9836 return 21;
9837 }
9838 if(UsefulBuf_Compare(ReturnedEncodedCBOR, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedArray2s))) {
9839 return 22;
9840 }
9841 if(uPosition != QCBORDecode_Tell(&DCtx)) {
9842 return 23;
9843 }
9844
9845 QCBORDecode_Rewind(&DCtx);
9846
9847 uPosition = QCBORDecode_Tell(&DCtx);
9848 QCBORDecode_GetMapFromMapSZ(&DCtx, "map in a map", &Item, &ReturnedEncodedCBOR);
9849 if(QCBORDecode_GetError(&DCtx)) {
9850 return 30;
9851 }
9852 if(Item.val.uCount != 4) {
9853 return 31;
9854 }
9855 if(UsefulBuf_Compare(ReturnedEncodedCBOR, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedMap4))) {
9856 return 32;
9857 }
9858 if(uPosition != QCBORDecode_Tell(&DCtx)) {
9859 return 33;
9860 }
9861
9862 uPosition = QCBORDecode_Tell(&DCtx);
9863 QCBORDecode_GetArrayFromMapSZ(&DCtx, "map in a map", &Item, &ReturnedEncodedCBOR);
9864 if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
9865 return 40;
9866 }
9867 if(UINT32_MAX != QCBORDecode_Tell(&DCtx)) {
9868 return 41;
9869 }
9870 QCBORDecode_GetAndResetError(&DCtx);
9871 if(uPosition != QCBORDecode_Tell(&DCtx)) {
9872 return 42;
9873 }
9874
9875
9876#ifndef QCBOR_DISABLE_TAGS
9877 UsefulBufC ExpMant = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpMant);
9878 QCBORDecode_Init(&DCtx, ExpMant, 0);
9879 QCBORDecode_EnterArray(&DCtx, NULL);
9880 QCBORDecode_EnterArray(&DCtx, NULL);
9881 QCBORDecode_GetArray(&DCtx, &Item, &ReturnedEncodedCBOR);
9882 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) {
9883 return 200;
9884 }
9885 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
9886 return 201;
9887 }
9888 if(!QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_DECIMAL_FRACTION)) {
9889 return 202;
9890 }
9891 if(Item.val.uCount != 2) {
9892 return 201;
9893 }
9894 if(UsefulBuf_Compare(ReturnedEncodedCBOR, UsefulBuf_Tail(ExpMant, 2))) {
9895 return 205;
9896 }
9897#endif /* !QCBOR_DISABLE_TAGS */
9898
9899
9900#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
9901
9902 UsefulBufC DefAndIndef = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spDefAndIndef);
9903 QCBORDecode_Init(&DCtx, DefAndIndef, 0);
9904 QCBORDecode_EnterArray(&DCtx, NULL);
9905 QCBORDecode_GetArray(&DCtx, &Item, &ReturnedEncodedCBOR);
9906 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) {
9907 return 50;
9908 }
9909 if(UsefulBuf_Compare(ReturnedEncodedCBOR, UsefulBuf_Tail(UsefulBuf_Head(DefAndIndef, 11), 1))) {
9910 return 51;
9911 }
9912
9913 QCBORDecode_Init(&DCtx, DefAndIndef, 0);
9914 QCBORDecode_EnterArray(&DCtx, NULL);
9915 QCBORDecode_EnterArray(&DCtx, NULL);
9916 QCBORDecode_GetArray(&DCtx, &Item, &ReturnedEncodedCBOR);
9917 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) {
9918 return 52;
9919 }
9920 if(UsefulBuf_Compare(ReturnedEncodedCBOR, UsefulBuf_Tail(UsefulBuf_Head(DefAndIndef, 10), 2))) {
9921 return 53;
9922 }
9923
9924 QCBORDecode_Init(&DCtx, DefAndIndef, 0);
9925 QCBORDecode_EnterArray(&DCtx, NULL);
9926 QCBORDecode_EnterArray(&DCtx, NULL);
9927 QCBORDecode_EnterArray(&DCtx, NULL);
9928 QCBORDecode_GetArray(&DCtx, &Item, &ReturnedEncodedCBOR);
9929 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) {
9930 return 54;
9931 }
9932 if(UsefulBuf_Compare(ReturnedEncodedCBOR, UsefulBuf_Tail(UsefulBuf_Head(DefAndIndef, 9), 3))) {
9933 return 55;
9934 }
9935 QCBORDecode_Init(&DCtx, DefAndIndef, 0);
9936 QCBORDecode_EnterArray(&DCtx, NULL);
9937 QCBORDecode_EnterArray(&DCtx, NULL);
9938 QCBORDecode_EnterArray(&DCtx, NULL);
9939 QCBORDecode_EnterArray(&DCtx, NULL);
9940 QCBORDecode_GetArray(&DCtx, &Item, &ReturnedEncodedCBOR);
9941 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) {
9942 return 56;
9943 }
9944 if(UsefulBuf_Compare(ReturnedEncodedCBOR, UsefulBuf_Tail(UsefulBuf_Head(DefAndIndef, 8), 4))) {
9945 return 57;
9946 }
9947
9948 QCBORDecode_Init(&DCtx, DefAndIndef, 0);
9949 QCBORDecode_EnterArray(&DCtx, NULL);
9950 QCBORDecode_EnterArray(&DCtx, NULL);
9951 QCBORDecode_EnterArray(&DCtx, NULL);
9952 QCBORDecode_EnterArray(&DCtx, NULL);
9953 QCBORDecode_EnterArray(&DCtx, NULL);
9954 QCBORDecode_VGetNextConsume(&DCtx, &Item);
9955 QCBORDecode_GetArray(&DCtx, &Item, &ReturnedEncodedCBOR);
9956 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) {
9957 return 58;
9958 }
9959 if(UsefulBuf_Compare(ReturnedEncodedCBOR, UsefulBuf_Tail(UsefulBuf_Head(DefAndIndef, 8), 6))) {
9960 return 59;
9961 }
9962
9963
9964 QCBORDecode_Init(&DCtx,
9965 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapIndefEncoded),
9966 0);
9967
9968 QCBORDecode_EnterMap(&DCtx, NULL);
9969 QCBORDecode_VGetNextConsume(&DCtx, &Item);
9970 QCBORDecode_GetArray(&DCtx, &Item, &ReturnedEncodedCBOR);
9971 if(QCBORDecode_GetError(&DCtx)) {
9972 return 60;
9973 }
9974 if(Item.val.uCount != UINT16_MAX) {
9975 return 61;
9976 }
9977 if(UsefulBuf_Compare(ReturnedEncodedCBOR, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedArray2sIndef))) {
9978 return 62;
9979 }
9980
9981 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
9982 UsefulBuf_Compare(Item.label.string, UsefulBuf_FROM_SZ_LITERAL("an array of two strings"))) {
9983 return 63;
9984 }
9985
9986 uPosition = QCBORDecode_Tell(&DCtx);
9987
9988
9989 QCBORDecode_GetMap(&DCtx, &Item, &ReturnedEncodedCBOR);
9990 if(QCBORDecode_GetError(&DCtx)) {
9991 return 70;
9992 }
9993 if(Item.val.uCount != UINT16_MAX) {
9994 return 71;
9995 }
9996 if(UsefulBuf_Compare(ReturnedEncodedCBOR, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedMap4Indef))) {
9997 return 72;
9998 }
9999
10000
10001 uPosition = QCBORDecode_Tell(&DCtx);
10002 QCBORDecode_GetArrayFromMapSZ(&DCtx,
10003 "an array of two strings",
10004 &Item,
10005 &ReturnedEncodedCBOR);
10006 if(QCBORDecode_GetError(&DCtx)) {
10007 return 80;
10008 }
10009 if(Item.val.uCount != UINT16_MAX) {
10010 return 81;
10011 }
10012 if(UsefulBuf_Compare(ReturnedEncodedCBOR, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedArray2sIndef))) {
10013 return 82;
10014 }
10015 if(uPosition != QCBORDecode_Tell(&DCtx)) {
10016 return 83;
10017 }
10018
10019 QCBORDecode_Rewind(&DCtx);
10020
10021 uPosition = QCBORDecode_Tell(&DCtx);
10022 QCBORDecode_GetMapFromMapSZ(&DCtx, "map in a map", &Item, &ReturnedEncodedCBOR);
10023 if(QCBORDecode_GetError(&DCtx)) {
10024 return 90;
10025 }
10026 if(Item.val.uCount != UINT16_MAX) {
10027 return 91;
10028 }
10029 if(UsefulBuf_Compare(ReturnedEncodedCBOR, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedMap4Indef))) {
10030 return 92;
10031 }
10032 if(uPosition != QCBORDecode_Tell(&DCtx)) {
10033 return 93;
10034 }
10035
10036 uPosition = QCBORDecode_Tell(&DCtx);
10037 QCBORDecode_GetArrayFromMapSZ(&DCtx, "map in a map", &Item, &ReturnedEncodedCBOR);
10038 if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
10039 return 100;
10040 }
10041 if(UINT32_MAX != QCBORDecode_Tell(&DCtx)) {
10042 return 101;
10043 }
10044 QCBORDecode_GetAndResetError(&DCtx);
10045 if(uPosition != QCBORDecode_Tell(&DCtx)) {
10046 return 102;
10047 }
Laurence Lundbladeec290b82024-06-10 11:10:54 -070010048#endif /* ! QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
Laurence Lundbladea29f45a2024-05-14 15:55:19 -070010049
10050 return 0;
10051}
Laurence Lundbladeec290b82024-06-10 11:10:54 -070010052#endif /* ! QCBOR_DISABLE_NON_INTEGER_LABELS */
Laurence Lundbladea29f45a2024-05-14 15:55:19 -070010053
10054
Laurence Lundbladef00b8be2024-03-08 10:34:33 -080010055int32_t
10056ErrorHandlingTests(void)
10057{
10058 QCBORDecodeContext DCtx;
10059 QCBORItem Item;
10060 QCBORError uError;
10061 int64_t integer;
10062
10063 /* Test QCBORDecode_SetError() */
10064 QCBORDecode_Init(&DCtx,
10065 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded),
10066 QCBOR_DECODE_MODE_NORMAL);
10067
10068 QCBORDecode_SetError(&DCtx, QCBOR_ERR_FIRST_USER_DEFINED);
10069
10070 QCBORDecode_VGetNext(&DCtx, &Item);
10071
10072 uError = QCBORDecode_GetError(&DCtx);
10073
10074 if(uError != QCBOR_ERR_FIRST_USER_DEFINED) {
10075 return -1;
10076 }
10077
10078 if(Item.uLabelType != QCBOR_TYPE_NONE ||
10079 Item.uDataType != QCBOR_TYPE_NONE) {
10080 return -2;
10081 }
10082
10083
10084 /* Test data type returned from previous error */
10085 QCBORDecode_Init(&DCtx,
10086 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded),
10087 QCBOR_DECODE_MODE_NORMAL);
10088 QCBORDecode_GetInt64(&DCtx, &integer);
10089 uError = QCBORDecode_GetError(&DCtx);
10090 if(uError != QCBOR_ERR_UNEXPECTED_TYPE) {
10091 return -3;
10092 }
10093
10094 QCBORDecode_VGetNext(&DCtx, &Item);
10095 if(Item.uLabelType != QCBOR_TYPE_NONE ||
10096 Item.uDataType != QCBOR_TYPE_NONE) {
10097 return -2;
10098 }
10099 uError = QCBORDecode_GetError(&DCtx);
10100 if(uError != QCBOR_ERR_UNEXPECTED_TYPE) {
10101 return -3;
10102 }
10103
10104
10105 /* Test error classification functions */
10106
10107 if(!QCBORDecode_IsUnrecoverableError(QCBOR_ERR_INDEFINITE_STRING_CHUNK)) {
10108 return -10;
10109 }
10110 if(QCBORDecode_IsUnrecoverableError(QCBOR_SUCCESS)) {
10111 return -11;
10112 }
10113 if(!QCBORDecode_IsUnrecoverableError(QCBOR_ERR_INDEFINITE_STRING_CHUNK)) {
10114 return -12;
10115 }
10116 if(QCBORDecode_IsUnrecoverableError(QCBOR_ERR_DUPLICATE_LABEL)) {
10117 return -13;
10118 }
10119
10120 if(!QCBORDecode_IsNotWellFormedError(QCBOR_ERR_BAD_TYPE_7)) {
10121 return -20;
10122 }
10123 if(!QCBORDecode_IsNotWellFormedError(QCBOR_ERR_BAD_BREAK)) {
10124 return -21;
10125 }
10126 if(QCBORDecode_IsNotWellFormedError(QCBOR_SUCCESS)) {
10127 return -22;
10128 }
10129 if(QCBORDecode_IsNotWellFormedError(QCBOR_ERR_ARRAY_DECODE_TOO_LONG)) {
10130 return -23;
10131 }
10132
Laurence Lundbladecd34e582024-06-06 10:55:14 -070010133 /* Test error strings */
10134 const char *szErrString;
10135
10136 szErrString = qcbor_err_to_str(QCBOR_ERR_ARRAY_DECODE_TOO_LONG);
10137 if(szErrString == NULL) {
10138 return -100;
10139 }
10140 if(strcmp(szErrString, "QCBOR_ERR_ARRAY_DECODE_TOO_LONG")) {
10141 return -101;
10142 }
10143
10144 szErrString = qcbor_err_to_str(QCBOR_SUCCESS);
10145 if(szErrString == NULL) {
10146 return -102;
10147 }
10148 if(strcmp(szErrString, "QCBOR_SUCCESS")) {
10149 return -103;
10150 }
10151
10152 szErrString = qcbor_err_to_str(100);
10153 if(szErrString == NULL) {
10154 return -104;
10155 }
10156 if(strcmp(szErrString, "Unidentified QCBOR error")) {
10157 return -105;
10158 }
10159
10160 szErrString = qcbor_err_to_str(200);
10161 if(szErrString == NULL) {
10162 return -106;
10163 }
10164 if(strcmp(szErrString, "USER_DEFINED_200")) {
10165 return -107;
10166 }
10167
Laurence Lundbladee2226742024-08-16 10:50:23 -070010168 QCBORDecode_Init(&DCtx,
10169 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded),
10170 QCBOR_DECODE_MODE_NORMAL);
10171
10172 UsefulBufC Xx = QCBORDecode_RetrieveUndecodedInput(&DCtx);
10173 if(Xx.ptr != pValidMapEncoded) {
10174 return -200;
10175 }
10176 if(Xx.len != sizeof(pValidMapEncoded)) {
10177 return -201;
10178 }
10179
Laurence Lundbladef00b8be2024-03-08 10:34:33 -080010180 return 0;
10181}
Laurence Lundblade11654912024-05-09 11:49:24 -070010182
10183
10184int32_t TellTests(void)
10185{
10186 QCBORDecodeContext DCtx;
10187 QCBORItem Item;
10188 uint32_t uPosition;
10189 int nIndex;
10190 int64_t nDecodedInt;
10191
Laurence Lundbladeec290b82024-06-10 11:10:54 -070010192 // Improvement: rewrite so this can run with only integer labels
Laurence Lundblade11654912024-05-09 11:49:24 -070010193 static const uint32_t aPos[] =
Laurence Lundblade535bb5e2024-06-16 12:06:32 -070010194 {0, 1, 17, 42, 50, 58, 72, 85, 98, 112, 151};
Laurence Lundblade11654912024-05-09 11:49:24 -070010195 QCBORDecode_Init(&DCtx,
10196 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded),
10197 0);
10198 for(nIndex = 0; ; nIndex++) {
10199 uPosition = QCBORDecode_Tell(&DCtx);
10200 if(uPosition != aPos[nIndex]) {
10201 return nIndex;
10202 }
10203
Laurence Lundblade535bb5e2024-06-16 12:06:32 -070010204 if(QCBORDecode_EndCheck(&DCtx)) {
Laurence Lundblade11654912024-05-09 11:49:24 -070010205 break;
10206 }
10207
10208 QCBORDecode_VGetNext(&DCtx, &Item);
10209 }
10210
10211#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
10212 static const uint32_t aPosIndef[] =
Laurence Lundblade535bb5e2024-06-16 12:06:32 -070010213 {0, 1, 17, 42, 50, 59, 73, 86, 99, 113, 154};
Laurence Lundblade11654912024-05-09 11:49:24 -070010214 QCBORDecode_Init(&DCtx,
10215 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapIndefEncoded),
10216 0);
10217
10218 for(nIndex = 0; ; nIndex++) {
10219 uPosition = QCBORDecode_Tell(&DCtx);
10220 if(uPosition != aPosIndef[nIndex]) {
10221 return nIndex + 100;
10222 }
10223
Laurence Lundblade535bb5e2024-06-16 12:06:32 -070010224 if(QCBORDecode_EndCheck(&DCtx)) {
Laurence Lundblade11654912024-05-09 11:49:24 -070010225 break;
10226 }
10227
10228 QCBORDecode_VGetNext(&DCtx, &Item);
10229 }
10230#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
10231
10232 /* Next, some tests with entered maps and arrays */
10233 QCBORDecode_Init(&DCtx,
10234 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded),
10235 0);
10236 QCBORDecode_EnterMap(&DCtx, &Item);
10237 if(QCBORDecode_Tell(&DCtx) != 1) {
10238 return 1001;
10239 }
10240 QCBORDecode_GetInt64InMapSZ(&DCtx, "first integer", &nDecodedInt);
10241 if(QCBORDecode_Tell(&DCtx) != 1) {
10242 return 1002;
10243 }
10244 QCBORDecode_EnterMapFromMapSZ(&DCtx, "map in a map");
10245 if(QCBORDecode_Tell(&DCtx) != 72) {
10246 return 1003;
10247 }
10248
10249 QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt);
10250 if(nDecodedInt != 98) {
10251 return 1004;
10252 }
10253 /* Getting non-aggregate types doesn't affect cursor position. */
10254 if(QCBORDecode_Tell(&DCtx) != 72) {
10255 return 1005;
10256 }
10257 QCBORDecode_VGetNext(&DCtx, &Item);
10258 if(QCBORDecode_Tell(&DCtx) != 85) {
10259 return 1006;
10260 }
10261 QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt);
10262 if(nDecodedInt != 98) {
10263 return 1007;
10264 }
10265 /* Getting non-aggregate types doesn't affect cursor position. */
10266 if(QCBORDecode_Tell(&DCtx) != 85) {
10267 return 1008;
10268 }
10269
10270 QCBORDecode_ExitMap(&DCtx);
Laurence Lundblade535bb5e2024-06-16 12:06:32 -070010271 if(QCBORDecode_Tell(&DCtx) != 151) {
Laurence Lundblade11654912024-05-09 11:49:24 -070010272 return 1009;
10273 }
10274 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_NO_MORE_ITEMS) {
10275 return 1010;
10276 }
10277
10278#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
10279 /* Next, some tests with entered maps and arrays */
10280 QCBORDecode_Init(&DCtx,
10281 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapIndefEncoded),
10282 0);
10283 QCBORDecode_EnterMap(&DCtx, &Item);
10284 if(QCBORDecode_Tell(&DCtx) != 1) {
10285 return 2000;
10286 }
10287 QCBORDecode_GetInt64InMapSZ(&DCtx, "first integer", &nDecodedInt);
10288 if(QCBORDecode_Tell(&DCtx) != 1) {
10289 return 2001;
10290 }
10291 QCBORDecode_EnterMapFromMapSZ(&DCtx, "map in a map");
10292 if(QCBORDecode_Tell(&DCtx) != 73) {
10293 return 2002;
10294 }
10295
10296 QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt);
10297 if(nDecodedInt != 98) {
10298 return 2003;
10299 }
10300 /* Getting non-aggregate types doesn't affect cursor position. */
10301 if(QCBORDecode_Tell(&DCtx) != 73) {
10302 return 2004;
10303 }
10304 QCBORDecode_VGetNext(&DCtx, &Item);
10305 if(QCBORDecode_Tell(&DCtx) != 86) {
10306 return 2005;
10307 }
10308 QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt);
10309 if(nDecodedInt != 98) {
10310 return 2006;
10311 }
10312 /* Getting non-aggregate types doesn't affect cursor position. */
10313 if(QCBORDecode_Tell(&DCtx) != 86) {
10314 return 2007;
10315 }
10316
10317 QCBORDecode_ExitMap(&DCtx);
Laurence Lundblade535bb5e2024-06-16 12:06:32 -070010318 if(QCBORDecode_Tell(&DCtx) != 154) {
Laurence Lundblade11654912024-05-09 11:49:24 -070010319 return 2008;
10320 }
10321 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_NO_MORE_ITEMS) {
10322 return 2010;
10323 }
10324#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
10325
10326
10327
10328 /* Error state test */
10329 QCBORDecode_Init(&DCtx,
10330 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded),
10331 0);
10332 /* Cause an error */
10333 QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt);
10334 if(QCBORDecode_Tell(&DCtx) != UINT32_MAX) {
10335 return 3000;
10336 }
Laurence Lundblade535bb5e2024-06-16 12:06:32 -070010337 if(QCBORDecode_EndCheck(&DCtx) != QCBOR_ERR_MAP_NOT_ENTERED) {
10338 return 3001;
10339 }
10340
Laurence Lundblade11654912024-05-09 11:49:24 -070010341
10342 /* Empties tests */
10343 const uint8_t pMinimalCBOR[] = {0xa0}; // One empty map
10344 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pMinimalCBOR),0);
10345 if(QCBORDecode_Tell(&DCtx) != 0) {
10346 return 4000;
10347 }
Laurence Lundblade535bb5e2024-06-16 12:06:32 -070010348 if(QCBORDecode_EndCheck(&DCtx) != QCBOR_SUCCESS) {
10349 return 4008;
10350 }
Laurence Lundblade11654912024-05-09 11:49:24 -070010351 QCBORDecode_EnterMap(&DCtx, &Item);
10352 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) {
10353 return 4001;
10354 }
Laurence Lundblade535bb5e2024-06-16 12:06:32 -070010355 if(QCBORDecode_Tell(&DCtx) != 1) {
Laurence Lundblade11654912024-05-09 11:49:24 -070010356 return 4002;
10357 }
10358 QCBORDecode_ExitMap(&DCtx);
10359 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) {
Laurence Lundblade535bb5e2024-06-16 12:06:32 -070010360 return 4003;
Laurence Lundblade11654912024-05-09 11:49:24 -070010361 }
Laurence Lundblade535bb5e2024-06-16 12:06:32 -070010362 if(QCBORDecode_Tell(&DCtx) != 1) {
10363 return 4004;
10364 }
10365 if(QCBORDecode_EndCheck(&DCtx) != QCBOR_ERR_NO_MORE_ITEMS) {
10366 return 4005;
Laurence Lundblade11654912024-05-09 11:49:24 -070010367 }
10368 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_NO_MORE_ITEMS) {
10369 return 4010;
10370 }
10371
10372#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
10373 const uint8_t pMinimalIndefCBOR[] = {0xbf, 0xff}; // One empty map
10374 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pMinimalIndefCBOR),0);
10375 if(QCBORDecode_Tell(&DCtx) != 0) {
10376 return 4100;
10377 }
10378 QCBORDecode_EnterMap(&DCtx, &Item);
10379 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) {
10380 return 4101;
10381 }
Laurence Lundblade535bb5e2024-06-16 12:06:32 -070010382 if(QCBORDecode_Tell(&DCtx) != 2) {
Laurence Lundblade11654912024-05-09 11:49:24 -070010383 return 4102;
10384 }
10385 QCBORDecode_ExitMap(&DCtx);
10386 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) {
Laurence Lundblade535bb5e2024-06-16 12:06:32 -070010387 return 4103;
Laurence Lundblade11654912024-05-09 11:49:24 -070010388 }
Laurence Lundblade535bb5e2024-06-16 12:06:32 -070010389 if(QCBORDecode_Tell(&DCtx) != 2) {
10390 return 4104;
10391 }
10392 if(QCBORDecode_EndCheck(&DCtx) != QCBOR_ERR_NO_MORE_ITEMS) {
10393 return 4005;
Laurence Lundblade11654912024-05-09 11:49:24 -070010394 }
10395 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_NO_MORE_ITEMS) {
10396 return 4110;
10397 }
10398#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
10399
10400 /* Test on a CBOR sequence */
10401 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSequenceTestInput),0);
10402 if(QCBORDecode_Tell(&DCtx) != 0) {
10403 return 5000;
10404 }
10405 QCBORDecode_VGetNext(&DCtx, &Item);
10406 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) {
10407 return 5001;
10408 }
10409 if(QCBORDecode_Tell(&DCtx) != 11) {
10410 return 5002;
10411 }
10412 QCBORDecode_VGetNext(&DCtx, &Item);
10413 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) {
10414 return 5003;
10415 }
10416 if(QCBORDecode_Tell(&DCtx) != 12) {
10417 return 5004;
10418 }
10419 QCBORDecode_VGetNext(&DCtx, &Item);
10420 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) {
10421 return 5005;
10422 }
10423 if(QCBORDecode_Tell(&DCtx) != 17) {
10424 return 5006;
10425 }
10426 QCBORDecode_VGetNext(&DCtx, &Item);
10427 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) {
10428 return 5007;
10429 }
Laurence Lundblade535bb5e2024-06-16 12:06:32 -070010430 if(QCBORDecode_Tell(&DCtx) != 20) {
Laurence Lundblade11654912024-05-09 11:49:24 -070010431 return 5008;
10432 }
10433 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_NO_MORE_ITEMS) {
10434 return 5010;
10435 }
10436
10437
10438 QCBORDecode_Init(&DCtx,
10439 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded),
10440 0);
10441 QCBORDecode_EnterMap(&DCtx, &Item);
10442 QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings");
10443 if(QCBORDecode_Tell(&DCtx) != 42) {
10444 return 6001;
10445 }
10446 QCBORDecode_VGetNext(&DCtx, &Item);
10447 if(QCBORDecode_Tell(&DCtx) != 50) {
10448 return 6002;
10449 }
10450 QCBORDecode_VGetNext(&DCtx, &Item);
10451 if(QCBORDecode_Tell(&DCtx) != 58) {
10452 return 6008;
10453 }
10454 QCBORDecode_VGetNext(&DCtx, &Item);
10455 (void)QCBORDecode_GetAndResetError(&DCtx);
10456 if(QCBORDecode_Tell(&DCtx) != 58) {
10457 return 6003;
10458 }
10459 QCBORDecode_ExitArray(&DCtx);
10460 if(QCBORDecode_Tell(&DCtx) != 58) {
10461 return 6004;
10462 }
10463
10464 static const uint32_t aEmptiesPos[] =
Laurence Lundblade535bb5e2024-06-16 12:06:32 -070010465 {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 13, 15};
Laurence Lundblade11654912024-05-09 11:49:24 -070010466 QCBORDecode_Init(&DCtx,
10467 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(sEmpties),
10468 0);
10469 for(nIndex = 0; ; nIndex++) {
10470 uPosition = QCBORDecode_Tell(&DCtx);
10471 if(uPosition != aEmptiesPos[nIndex]) {
10472 return nIndex + 200;
10473 }
10474
Laurence Lundblade535bb5e2024-06-16 12:06:32 -070010475 if(QCBORDecode_EndCheck(&DCtx)) {
Laurence Lundblade11654912024-05-09 11:49:24 -070010476 break;
10477 }
10478
10479 QCBORDecode_VGetNext(&DCtx, &Item);
10480 }
10481
10482#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
10483 static const uint32_t aIndefEmptiesPos[] =
Laurence Lundblade535bb5e2024-06-16 12:06:32 -070010484 {0, 1, 2, 4, 5, 7, 8, 10, 12, 13, 16, 19, 25};
Laurence Lundblade11654912024-05-09 11:49:24 -070010485 QCBORDecode_Init(&DCtx,
10486 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(sEmptiesIndef),
10487 0);
10488 for(nIndex = 0; ; nIndex++) {
10489 uPosition = QCBORDecode_Tell(&DCtx);
10490 if(uPosition != aIndefEmptiesPos[nIndex]) {
10491 return nIndex + 300;
10492 }
10493
Laurence Lundblade535bb5e2024-06-16 12:06:32 -070010494 if(QCBORDecode_EndCheck(&DCtx)) {
Laurence Lundblade11654912024-05-09 11:49:24 -070010495 break;
10496 }
10497
10498 QCBORDecode_VGetNext(&DCtx, &Item);
10499 }
10500#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
10501
10502
10503 return 0;
10504}