blob: aae1f80212784a29d03cbfe190f231cbccc0da3f [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
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03002337int32_t ParseSimpleTest(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002338{
2339 QCBORDecodeContext DCtx;
2340 QCBORItem Item;
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07002341 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002342
2343
Laurence Lundbladeee851742020-01-08 08:37:05 -08002344 QCBORDecode_Init(&DCtx,
2345 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSimpleValues),
2346 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002347
2348
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002349 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07002350 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002351 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
2352 Item.val.uCount != 10)
2353 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002354
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002355 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07002356 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002357 if(Item.uDataType != QCBOR_TYPE_FALSE)
2358 return -1;
2359
2360 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07002361 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002362 if(Item.uDataType != QCBOR_TYPE_TRUE)
2363 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002364
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002365 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07002366 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002367 if(Item.uDataType != QCBOR_TYPE_NULL)
2368 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002369
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002370 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07002371 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002372 if(Item.uDataType != QCBOR_TYPE_UNDEF)
2373 return -1;
2374
2375 // A break
Laurence Lundblade9e3651c2018-10-10 11:49:55 +08002376 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_BREAK)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002377 return -1;
2378
2379 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07002380 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002381 if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 0)
2382 return -1;
2383
2384 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07002385 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002386 if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 19)
2387 return -1;
2388
Laurence Lundblade077475f2019-04-26 09:06:33 -07002389 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_TYPE_7)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002390 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002391
Laurence Lundblade077475f2019-04-26 09:06:33 -07002392 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_TYPE_7)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002393 return -1;
2394
Laurence Lundblade077475f2019-04-26 09:06:33 -07002395 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_TYPE_7)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002396 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002397
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002398 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07002399 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002400 if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 32)
2401 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002402
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002403 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07002404 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002405 if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 255)
2406 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002407
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002408 return 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002409
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002410}
2411
2412
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03002413int32_t NotWellFormedTests(void)
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002414{
2415 // Loop over all the not-well-formed instance of CBOR
2416 // that are test vectors in not_well_formed_cbor.h
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08002417 const uint16_t nArraySize = C_ARRAY_COUNT(paNotWellFormedCBOR,
2418 struct someBinaryBytes);
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002419 for(uint16_t nIterate = 0; nIterate < nArraySize; nIterate++) {
2420 const struct someBinaryBytes *pBytes = &paNotWellFormedCBOR[nIterate];
2421 const UsefulBufC Input = (UsefulBufC){pBytes->p, pBytes->n};
2422
Laurence Lundbladeee851742020-01-08 08:37:05 -08002423 // Set up decoder context. String allocator needed for indefinite
2424 // string test cases
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002425 QCBORDecodeContext DCtx;
2426 QCBORDecode_Init(&DCtx, Input, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08002427#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002428 UsefulBuf_MAKE_STACK_UB(Pool, 100);
2429 QCBORDecode_SetMemPool(&DCtx, Pool, 0);
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08002430#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002431
2432 // Loop getting items until no more to get
Laurence Lundbladef71e1622020-08-06 18:52:13 -07002433 QCBORError uCBORError;
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002434 do {
2435 QCBORItem Item;
2436
Laurence Lundbladef71e1622020-08-06 18:52:13 -07002437 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
2438 } while(uCBORError == QCBOR_SUCCESS);
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002439
2440 // Every test vector must fail with
2441 // a not-well-formed error. If not
2442 // this test fails.
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002443 if(!QCBORDecode_IsNotWellFormedError(uCBORError) &&
Laurence Lundbladef71e1622020-08-06 18:52:13 -07002444 uCBORError != QCBOR_ERR_NO_MORE_ITEMS) {
Laurence Lundblade4bf6e712020-12-10 11:53:21 -08002445 /* Return index of failure and QCBOR error in the result */
2446 return (int32_t)(nIterate * 100 + uCBORError);
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002447 }
2448 }
2449 return 0;
2450}
2451
2452
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002453struct DecodeFailTestInput {
2454 const char *szDescription; /* Description of the test */
2455 QCBORDecodeMode DecoderMode; /* The QCBOR Decoder Mode for test */
2456 UsefulBufC Input; /* Chunk of CBOR that cases error */
2457 QCBORError nError; /* The expected error */
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002458};
2459
Laurence Lundblade59289e52019-12-30 13:44:37 -08002460
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002461static int32_t
2462ProcessDecodeFailures(const struct DecodeFailTestInput *pFailInputs, const int nNumFails)
Laurence Lundblade59289e52019-12-30 13:44:37 -08002463{
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002464 int nIndex;
2465 QCBORDecodeContext DCtx;
2466 QCBORError uCBORError;
2467 QCBORItem Item;
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08002468
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002469 for(nIndex = 0; nIndex < nNumFails; nIndex++) {
2470 const struct DecodeFailTestInput *pF = &pFailInputs[nIndex];
2471
2472 QCBORDecode_Init(&DCtx, pF->Input, pF->DecoderMode);
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08002473
2474#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002475 /* Set up the decoding context including a memory pool so that
2476 * indefinite length items can be checked.
2477 */
Laurence Lundblade59289e52019-12-30 13:44:37 -08002478 UsefulBuf_MAKE_STACK_UB(Pool, 100);
Laurence Lundblade830fbf92020-05-31 17:22:33 -07002479
Laurence Lundblade4bf6e712020-12-10 11:53:21 -08002480 uCBORError = QCBORDecode_SetMemPool(&DCtx, Pool, 0);
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002481 if(uCBORError != QCBOR_SUCCESS) {
2482 return -1;
Laurence Lundblade59289e52019-12-30 13:44:37 -08002483 }
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08002484#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
2485
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002486 if(nIndex == 8) {
2487 uCBORError = 9; /* For setting break points */
Laurence Lundblade37286c02022-09-03 10:05:02 -07002488 }
2489
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002490 /* Iterate until there is an error of some sort of error */
Laurence Lundblade59289e52019-12-30 13:44:37 -08002491 do {
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002492 /* Set to something none-zero, something other than QCBOR_TYPE_NONE */
Laurence Lundblade59289e52019-12-30 13:44:37 -08002493 memset(&Item, 0x33, sizeof(Item));
2494
Laurence Lundblade4bf6e712020-12-10 11:53:21 -08002495 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
2496 } while(uCBORError == QCBOR_SUCCESS);
2497
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002498 /* Must get the expected error or the this test fails
2499 * The data and label type must also be QCBOR_TYPE_NONE.
2500 */
Laurence Lundblade4bf6e712020-12-10 11:53:21 -08002501 if(uCBORError != pF->nError ||
Laurence Lundblade59289e52019-12-30 13:44:37 -08002502 Item.uDataType != QCBOR_TYPE_NONE ||
2503 Item.uLabelType != QCBOR_TYPE_NONE) {
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002504 return (int32_t)(nIndex * 1000 + (int)uCBORError);
Laurence Lundblade59289e52019-12-30 13:44:37 -08002505 }
2506 }
2507
2508 return 0;
2509}
2510
2511
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002512static const struct DecodeFailTestInput Failures[] = {
2513 /* Most of this is copied from not_well_formed.h. Here the error
2514 * code returned is also checked.
2515 */
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002516
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08002517#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002518 /* Indefinite length strings must be closed off */
2519 { "An indefinite length byte string not closed off",
2520 QCBOR_DECODE_MODE_NORMAL,
2521 {"0x5f\x41\x00", 3},
2522 QCBOR_ERR_HIT_END
2523 },
2524 { "An indefinite length text string not closed off",
2525 QCBOR_DECODE_MODE_NORMAL,
2526 {"\x7f\x61\x00", 3},
2527 QCBOR_ERR_HIT_END
2528 },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002529
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002530 /* All the chunks in an indefinite length string must be of the
2531 * type of indefinite length string
2532 */
2533 { "Indefinite length byte string with text string chunk",
2534 QCBOR_DECODE_MODE_NORMAL,
2535 {"\x5f\x61\x00\xff", 4},
2536 QCBOR_ERR_INDEFINITE_STRING_CHUNK
2537 },
2538 { "Indefinite length text string with a byte string chunk",
2539 QCBOR_DECODE_MODE_NORMAL,
2540 {"\x7f\x41\x00\xff", 4},
2541 QCBOR_ERR_INDEFINITE_STRING_CHUNK
2542 },
2543 { "Indefinite length byte string with a positive integer chunk",
2544 QCBOR_DECODE_MODE_NORMAL,
2545 {"\x5f\x00\xff", 3},
2546 QCBOR_ERR_INDEFINITE_STRING_CHUNK
2547 },
2548 { "Indefinite length byte string with an negative integer chunk",
2549 QCBOR_DECODE_MODE_NORMAL,
2550 {"\x5f\x21\xff", 3},
2551 QCBOR_ERR_INDEFINITE_STRING_CHUNK
2552 },
2553 { "Indefinite length byte string with an array chunk",
2554 QCBOR_DECODE_MODE_NORMAL,
2555 {"\x5f\x80\xff", 3},
2556 QCBOR_ERR_INDEFINITE_STRING_CHUNK
2557 },
2558 { "Indefinite length byte string with an map chunk",
2559 QCBOR_DECODE_MODE_NORMAL,
2560 {"\x5f\xa0\xff", 3},
2561 QCBOR_ERR_INDEFINITE_STRING_CHUNK
2562 },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002563
Laurence Lundblade37286c02022-09-03 10:05:02 -07002564#ifndef QCBOR_DISABLE_TAGS
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002565 { "Indefinite length byte string with tagged integer chunk",
2566 QCBOR_DECODE_MODE_NORMAL,
2567 {"\x5f\xc0\x00\xff", 4},
2568 QCBOR_ERR_INDEFINITE_STRING_CHUNK
2569 },
Laurence Lundblade37286c02022-09-03 10:05:02 -07002570#else
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002571 { "Indefinite length byte string with tagged integer chunk",
2572 QCBOR_DECODE_MODE_NORMAL,
2573 {"\x5f\xc0\x00\xff", 4},
2574 QCBOR_ERR_TAGS_DISABLED
2575 },
Laurence Lundblade37286c02022-09-03 10:05:02 -07002576#endif /* QCBOR_DISABLE_TAGS */
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002577
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002578 { "Indefinite length byte string with an simple type chunk",
2579 QCBOR_DECODE_MODE_NORMAL,
2580 {"\x5f\xe0\xff", 3},
2581 QCBOR_ERR_INDEFINITE_STRING_CHUNK
2582 },
2583 { "???",
2584 QCBOR_DECODE_MODE_NORMAL,
2585 {"\x5f\x5f\x41\x00\xff\xff", 6},
2586 QCBOR_ERR_INDEFINITE_STRING_CHUNK
2587 },
2588 { "indefinite length text string with indefinite string inside",
2589 QCBOR_DECODE_MODE_NORMAL,
2590 {"\x7f\x7f\x61\x00\xff\xff", 6},
2591 QCBOR_ERR_INDEFINITE_STRING_CHUNK
2592 },
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08002593#else /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
2594
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08002595#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
2596
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002597 /* Definte length maps and arrays must be closed by having the right number of items */
2598 { "A definte length array that is supposed to have 1 item, but has none",
2599 QCBOR_DECODE_MODE_NORMAL,
2600 {"\x81", 1},
2601 QCBOR_ERR_NO_MORE_ITEMS
2602 },
2603 { "A definte length array that is supposed to have 2 items, but has only 1",
2604 QCBOR_DECODE_MODE_NORMAL,
2605 {"\x82\x00", 2},
2606 QCBOR_ERR_NO_MORE_ITEMS
2607 },
2608 { "A definte length array that is supposed to have 511 items, but has only 1",
2609 QCBOR_DECODE_MODE_NORMAL,
2610 {"\x9a\x01\xff\x00", 4},
2611 QCBOR_ERR_HIT_END
2612 },
2613 { "A definte length map that is supposed to have 1 item, but has none",
2614 QCBOR_DECODE_MODE_NORMAL,
2615 {"\xa1", 1},
2616 QCBOR_ERR_NO_MORE_ITEMS
2617 },
2618 { "A definte length map that is supposed to have s item, but has only 1",
2619 QCBOR_DECODE_MODE_NORMAL,
2620 {"\xa2\x01\x02", 3},
2621 QCBOR_ERR_NO_MORE_ITEMS
2622 },
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08002623#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002624 /* Indefinte length maps and arrays must be ended by a break */
2625 { "Indefinite length array with zero items and no break",
2626 QCBOR_DECODE_MODE_NORMAL,
2627 {"\x9f", 1},
2628 QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002629
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002630 { "Indefinite length array with two items and no break",
2631 QCBOR_DECODE_MODE_NORMAL,
2632 {"\x9\x01\x02", 3},
2633 QCBOR_ERR_NO_MORE_ITEMS
2634 },
2635 { "Indefinite length map with zero items and no break",
2636 QCBOR_DECODE_MODE_NORMAL,
2637 {"\xbf", 1},
2638 QCBOR_ERR_NO_MORE_ITEMS
2639 },
2640 { "Indefinite length map with two items and no break",
2641 QCBOR_DECODE_MODE_NORMAL,
2642 {"\xbf\x01\x02\x01\x02", 5},
2643 QCBOR_ERR_NO_MORE_ITEMS
2644 },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002645
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002646 /* Nested maps and arrays must be closed off (some extra nested test vectors) */
2647 { "Unclosed indefinite array containing a closed definite length array",
2648 QCBOR_DECODE_MODE_NORMAL,
2649 {"\x9f\x80\x00", 3},
2650 QCBOR_ERR_NO_MORE_ITEMS
2651 },
2652
2653 { "Definite length array containing an unclosed indefinite length array",
2654 QCBOR_DECODE_MODE_NORMAL,
2655 {"\x81\x9f", 2},
2656 QCBOR_ERR_NO_MORE_ITEMS
2657 },
2658 { "Unclosed indefinite map containing a closed definite length array",
2659 QCBOR_DECODE_MODE_NORMAL,
2660 {"\xbf\x01\x80\x00\xa0", 5},
2661 QCBOR_ERR_NO_MORE_ITEMS
2662 },
2663 { "Definite length map containing an unclosed indefinite length array",
2664 QCBOR_DECODE_MODE_NORMAL,
2665 {"\xa1\x02\x9f", 3},
2666 QCBOR_ERR_NO_MORE_ITEMS
2667 },
2668 { "Deeply nested definite length arrays with deepest one unclosed",
2669 QCBOR_DECODE_MODE_NORMAL,
2670 {"\x81\x81\x81\x81\x81\x81\x81\x81\x81", 9},
2671 QCBOR_ERR_NO_MORE_ITEMS
2672 },
2673 { "Deeply nested indefinite length arrays with deepest one unclosed",
2674 QCBOR_DECODE_MODE_NORMAL,
2675 {"\x9f\x9f\x9f\x9f\x9f\xff\xff\xff\xff", 9},
2676 QCBOR_ERR_NO_MORE_ITEMS
2677 },
2678 { "Mixed nesting with indefinite unclosed",
2679 QCBOR_DECODE_MODE_NORMAL,
2680 {"\x9f\x81\x9f\x81\x9f\x9f\xff\xff\xff", 9},
2681 QCBOR_ERR_NO_MORE_ITEMS },
2682 { "Mixed nesting with definite unclosed",
2683 QCBOR_DECODE_MODE_NORMAL,
2684 {"\x9f\x82\x9f\x81\x9f\x9f\xff\xff\xff\xff", 10},
2685 QCBOR_ERR_BAD_BREAK
2686 },
2687 { "Unclosed indefinite length map in definite length maps",
2688 QCBOR_DECODE_MODE_NORMAL,
2689 {"\xa1\x01\xa2\x02\xbf\xff\x02\xbf", 8},
2690 QCBOR_ERR_NO_MORE_ITEMS
2691 },
2692 { "Unclosed definite length map in indefinite length maps",
2693 QCBOR_DECODE_MODE_NORMAL,
2694 {"\xbf\x01\xbf\x02\xa1", 5},
2695 QCBOR_ERR_NO_MORE_ITEMS
2696 },
2697 { "Unclosed indefinite length array in definite length maps",
2698 QCBOR_DECODE_MODE_NORMAL,
2699 {"\xa1\x01\xa2\x02\x9f\xff\x02\x9f", 8},
2700 QCBOR_ERR_NO_MORE_ITEMS
2701 },
2702 { "Unclosed definite length array in indefinite length maps",
2703 QCBOR_DECODE_MODE_NORMAL,
2704 {"\xbf\x01\xbf\x02\x81", 5},
2705 QCBOR_ERR_NO_MORE_ITEMS
2706 },
2707 { "Unclosed indefinite length map in definite length arrays",
2708 QCBOR_DECODE_MODE_NORMAL,
2709 {"\x81\x82\xbf\xff\xbf", 5},
2710 QCBOR_ERR_NO_MORE_ITEMS
2711 },
2712 { "Unclosed definite length map in indefinite length arrays",
2713 QCBOR_DECODE_MODE_NORMAL,
2714 {"\x9f\x9f\xa1", 3},
2715 QCBOR_ERR_NO_MORE_ITEMS
2716 },
2717
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08002718#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002719
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002720 /* The "argument" for the data item is incomplete */
2721 { "Positive integer missing 1 byte argument",
2722 QCBOR_DECODE_MODE_NORMAL,
2723 {"\x18", 1},
2724 QCBOR_ERR_HIT_END
2725 },
2726 { "Positive integer missing 2 byte argument",
2727 QCBOR_DECODE_MODE_NORMAL,
2728 {"\x19", 1},
2729 QCBOR_ERR_HIT_END
2730 },
2731 { "Positive integer missing 4 byte argument",
2732 QCBOR_DECODE_MODE_NORMAL,
2733 {"\x1a", 1},
2734 QCBOR_ERR_HIT_END
2735 },
2736 { "Positive integer missing 8 byte argument",
2737 QCBOR_DECODE_MODE_NORMAL,
2738 {"\x1b", 1},
2739 QCBOR_ERR_HIT_END
2740 },
2741 { "Positive integer missing 1 byte of 2 byte argument",
2742 QCBOR_DECODE_MODE_NORMAL,
2743 {"\x19\x01", 2},
2744 QCBOR_ERR_HIT_END
2745 },
2746 { "Positive integer missing 2 bytes of 4 byte argument",
2747 QCBOR_DECODE_MODE_NORMAL,
2748 {"\x1a\x01\x02", 3},
2749 QCBOR_ERR_HIT_END
2750 },
2751 { "Positive integer missing 1 bytes of 7 byte argument",
2752 QCBOR_DECODE_MODE_NORMAL,
2753 {"\x1b\x01\x02\x03\x04\x05\x06\x07", 8},
2754 QCBOR_ERR_HIT_END
2755 },
2756 { "Negative integer missing 1 byte argument",
2757 QCBOR_DECODE_MODE_NORMAL,
2758 {"\x38", 1},
2759 QCBOR_ERR_HIT_END
2760 },
2761 { "Binary string missing 1 byte argument",
2762 QCBOR_DECODE_MODE_NORMAL,
2763 {"\x58", 1},
2764 QCBOR_ERR_HIT_END
2765 },
2766 { "Text string missing 1 byte argument",
2767 QCBOR_DECODE_MODE_NORMAL,
2768 {"\x78", 1},
2769 QCBOR_ERR_HIT_END
2770 },
2771 { "Array missing 1 byte argument",
2772 QCBOR_DECODE_MODE_NORMAL,
2773 {"\x98", 1},
2774 QCBOR_ERR_HIT_END
2775 },
2776 { "Map missing 1 byte argument",
2777 QCBOR_DECODE_MODE_NORMAL,
2778 {"\xb8", 1},
2779 QCBOR_ERR_HIT_END
2780 },
2781 { "Tag missing 1 byte argument",
2782 QCBOR_DECODE_MODE_NORMAL,
2783 {"\xd8", 1},
2784 QCBOR_ERR_HIT_END
2785 },
2786 { "Simple missing 1 byte argument",
2787 QCBOR_DECODE_MODE_NORMAL,
2788 {"\xf8", 1},
2789 QCBOR_ERR_HIT_END
2790 },
2791 { "half-precision with 1 byte argument",
2792 QCBOR_DECODE_MODE_NORMAL,
2793 {"\xf9\x00", 2},
2794 QCBOR_ERR_HIT_END
2795 },
2796 { "single-precision with 2 byte argument",
2797 QCBOR_DECODE_MODE_NORMAL,
2798 {"\0xfa\x00\x00", 3},
2799 QCBOR_ERR_HIT_END
2800 },
2801 { "double-precision with 3 byte argument",
2802 QCBOR_DECODE_MODE_NORMAL,
2803 {"\xfb\x00\x00\x00", 4},
2804 QCBOR_ERR_HIT_END
2805 },
Laurence Lundblade2f467f92020-10-09 17:50:11 -07002806
Laurence Lundblade37286c02022-09-03 10:05:02 -07002807#ifndef QCBOR_DISABLE_TAGS
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002808 { "Tag with no content",
2809 QCBOR_DECODE_MODE_NORMAL,
2810 {"\xc0", 1},
2811 QCBOR_ERR_HIT_END
2812 },
Laurence Lundblade37286c02022-09-03 10:05:02 -07002813#else /* QCBOR_DISABLE_TAGS */
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002814 { "Tag with no content",
2815 QCBOR_DECODE_MODE_NORMAL,
2816 {"\xc0", 1},
2817 QCBOR_ERR_TAGS_DISABLED
2818 },
Laurence Lundblade37286c02022-09-03 10:05:02 -07002819#endif /* QCBOR_DISABLE_TAGS */
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002820
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002821 /* Breaks must not occur in definite length arrays and maps */
2822 { "Array of length 1 with sole member replaced by a break",
2823 QCBOR_DECODE_MODE_NORMAL,
2824 {"\x81\xff", 2},
2825 QCBOR_ERR_BAD_BREAK
2826 },
2827 { "Array of length 2 with 2nd member replaced by a break",
2828 QCBOR_DECODE_MODE_NORMAL,
2829 {"\x82\x00\xff", 3},
2830 QCBOR_ERR_BAD_BREAK
2831 },
2832 { "Map of length 1 with sole member label replaced by a break",
2833 QCBOR_DECODE_MODE_NORMAL,
2834 {"\xa1\xff", 2},
2835 QCBOR_ERR_BAD_BREAK
2836 },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002837
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002838 /* Map of length 1 with sole member label replaced by break */
2839 { "Alternate representation that some decoders handle differently",
2840 QCBOR_DECODE_MODE_NORMAL,
2841 {"\xa1\xff\x00", 3},
2842 QCBOR_ERR_BAD_BREAK
2843 },
2844 { "Array of length 1 with 2nd member value replaced by a break",
2845 QCBOR_DECODE_MODE_NORMAL,
2846 {"\xa1\x00\xff", 3},
2847 QCBOR_ERR_BAD_BREAK
2848 },
Laurence Lundbladeec290b82024-06-10 11:10:54 -07002849 { "Map of length 2 with 2nd entry label replaced by a break",
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002850 QCBOR_DECODE_MODE_NORMAL,
Laurence Lundbladeec290b82024-06-10 11:10:54 -07002851 {"\xa2\x00\x00\xff\x00", 5},
2852 QCBOR_ERR_BAD_BREAK
2853 },
2854 { "Map of length 2 with 2nd entry value replaced by a break",
2855 QCBOR_DECODE_MODE_NORMAL,
2856 {"\xa2\x00\x00\x01\xff", 5},
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002857 QCBOR_ERR_BAD_BREAK
2858 },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002859
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002860 /* Breaks must not occur on their own out of an indefinite length data item */
2861 { "A bare break is not well formed",
2862 QCBOR_DECODE_MODE_NORMAL,
2863 {"\xff", 1},
2864 QCBOR_ERR_BAD_BREAK
2865 },
2866 { "A bare break after a zero length definite length array",
2867 QCBOR_DECODE_MODE_NORMAL,
2868 {"\x80\xff", 2},
2869 QCBOR_ERR_BAD_BREAK
2870 },
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08002871#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002872 { "A bare break after a zero length indefinite length map",
2873 QCBOR_DECODE_MODE_NORMAL,
2874 {"\x9f\xff\xff", 3},
2875 QCBOR_ERR_BAD_BREAK
2876 },
2877 { "A break inside a definite length array inside an indefenite length array",
2878 QCBOR_DECODE_MODE_NORMAL,
2879 {"\x9f\x81\xff", 3},
2880 QCBOR_ERR_BAD_BREAK
2881 },
2882 { "Complicated mixed nesting with break outside indefinite length array",
2883 QCBOR_DECODE_MODE_NORMAL,
2884 {"\x9f\x82\x9f\x81\x9f\x9f\xff\xff\xff\xff", 10},
2885 QCBOR_ERR_BAD_BREAK },
2886#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
2887
2888 /* Forbidden two byte encodings of simple types */
2889 { "Must use 0xe0 instead",
2890 QCBOR_DECODE_MODE_NORMAL,
2891 {"\xf8\x00", 2},
2892 QCBOR_ERR_BAD_TYPE_7
2893 },
2894 { "Should use 0xe1 instead",
2895 QCBOR_DECODE_MODE_NORMAL,
2896 {"\xf8\x01", 2},
2897 QCBOR_ERR_BAD_TYPE_7
2898 },
2899 { "Should use 0xe2 instead",
2900 QCBOR_DECODE_MODE_NORMAL,
2901 {"\xf8\x02", 2},
2902 QCBOR_ERR_BAD_TYPE_7
2903 }, { "Should use 0xe3 instead",
2904 QCBOR_DECODE_MODE_NORMAL,
2905 {"\xf8\x03", 2},
2906 QCBOR_ERR_BAD_TYPE_7
2907 },
2908 { "Should use 0xe4 instead",
2909 QCBOR_DECODE_MODE_NORMAL,
2910 {"\xf8\x04", 2},
2911 QCBOR_ERR_BAD_TYPE_7
2912 },
2913 { "Should use 0xe5 instead",
2914 QCBOR_DECODE_MODE_NORMAL,
2915 {"\xf8\x05", 2},
2916 QCBOR_ERR_BAD_TYPE_7
2917 },
2918 { "Should use 0xe6 instead",
2919 QCBOR_DECODE_MODE_NORMAL,
2920 {"\xf8\x06", 2},
2921 QCBOR_ERR_BAD_TYPE_7
2922 },
2923 { "Should use 0xe7 instead",
2924 QCBOR_DECODE_MODE_NORMAL,
2925 {"\xf8\x07", 2},
2926 QCBOR_ERR_BAD_TYPE_7
2927 },
2928 { "Should use 0xe8 instead",
2929 QCBOR_DECODE_MODE_NORMAL,
2930 {"\xf8\x08", 2},
2931 QCBOR_ERR_BAD_TYPE_7
2932 },
2933 { "Should use 0xe9 instead",
2934 QCBOR_DECODE_MODE_NORMAL,
2935 {"\xf8\x09", 2},
2936 QCBOR_ERR_BAD_TYPE_7
2937 },
2938 { "Should use 0xea instead",
2939 QCBOR_DECODE_MODE_NORMAL,
2940 {"\xf8\x0a", 2},
2941 QCBOR_ERR_BAD_TYPE_7
2942 },
2943 { "Should use 0xeb instead",
2944 QCBOR_DECODE_MODE_NORMAL,
2945 {"\xf8\x0b", 2},
2946 QCBOR_ERR_BAD_TYPE_7
2947 },
2948 { "Should use 0xec instead",
2949 QCBOR_DECODE_MODE_NORMAL,
2950 {"\xf8\x0c", 2},
2951 QCBOR_ERR_BAD_TYPE_7
2952 },
2953 { "Should use 0xed instead",
2954 QCBOR_DECODE_MODE_NORMAL,
2955 {"\xf8\x0d", 2},
2956 QCBOR_ERR_BAD_TYPE_7
2957 },
2958 { "Should use 0xee instead",
2959 QCBOR_DECODE_MODE_NORMAL,
2960 {"\xf8\x0e", 2},
2961 QCBOR_ERR_BAD_TYPE_7
2962 },
2963 { "Should use 0xef instead",
2964 QCBOR_DECODE_MODE_NORMAL,
2965 {"\xf8\x0f", 2},
2966 QCBOR_ERR_BAD_TYPE_7
2967 },
2968 { "Should use 0xf0 instead",
2969 QCBOR_DECODE_MODE_NORMAL,
2970 {"\xf8\x10", 2},
2971 QCBOR_ERR_BAD_TYPE_7
2972 },
2973 { "Should use 0xf1 instead",
2974 QCBOR_DECODE_MODE_NORMAL,
2975 {"\xf8\x11", 2},
2976 QCBOR_ERR_BAD_TYPE_7
2977 },
2978 { "Should use 0xf2 instead",
2979 QCBOR_DECODE_MODE_NORMAL,
2980 {"\xf8\x12", 2},
2981 QCBOR_ERR_BAD_TYPE_7
2982 },
2983 { "Should use 0xf3 instead",
2984 QCBOR_DECODE_MODE_NORMAL,
2985 {"\xf8\x13", 2},
2986 QCBOR_ERR_BAD_TYPE_7
2987 },
2988 { "Should use 0xf4 instead",
2989 QCBOR_DECODE_MODE_NORMAL,
2990 {"\xf8\x14", 2},
2991 QCBOR_ERR_BAD_TYPE_7
2992 },
2993 { "Should use 0xf5 instead",
2994 QCBOR_DECODE_MODE_NORMAL,
2995 {"\xf8\x15", 2},
2996 QCBOR_ERR_BAD_TYPE_7
2997 },
2998 { "Should use 0xf6 instead",
2999 QCBOR_DECODE_MODE_NORMAL,
3000 {"\xf8\x16", 2},
3001 QCBOR_ERR_BAD_TYPE_7
3002 },
3003 { "Should use 0xef7 instead",
3004 QCBOR_DECODE_MODE_NORMAL,
3005 {"\xf8\x17", 2},
3006 QCBOR_ERR_BAD_TYPE_7
3007 },
3008 { "Should use 0xef8 instead",
3009 QCBOR_DECODE_MODE_NORMAL,
3010 {"\xf8\x18", 2},
3011 QCBOR_ERR_BAD_TYPE_7
3012 },
3013 { "Reserved",
3014 QCBOR_DECODE_MODE_NORMAL,
3015 {"\xf8\x18", 2},
3016 QCBOR_ERR_BAD_TYPE_7
3017 },
3018
3019 /* Maps must have an even number of data items (key & value) */
3020 { "Map with 1 item when it should have 2",
3021 QCBOR_DECODE_MODE_NORMAL,
3022 {"\xa1\x00", 2},
3023 QCBOR_ERR_HIT_END
3024 },
3025 { "Map with 3 item when it should have 4",
3026 QCBOR_DECODE_MODE_NORMAL,
3027 {"\xa2\x00\x00\x00", 2},
3028 QCBOR_ERR_HIT_END
3029 },
3030#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
3031 { "Map with 1 item when it should have 2",
3032 QCBOR_DECODE_MODE_NORMAL,
3033 {"\xbf\x00\xff", 3},
3034 QCBOR_ERR_BAD_BREAK
3035 },
3036 { "Map with 3 item when it should have 4",
3037 QCBOR_DECODE_MODE_NORMAL,
3038 {"\xbf\x00\x00\x00\xff", 5},
3039 QCBOR_ERR_BAD_BREAK
3040 },
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08003041#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07003042
3043
Laurence Lundblade37286c02022-09-03 10:05:02 -07003044#ifndef QCBOR_DISABLE_TAGS
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08003045 /* In addition to not-well-formed, some invalid CBOR */
3046 { "Text-based date, with an integer",
3047 QCBOR_DECODE_MODE_NORMAL,
3048 {"\xc0\x00", 2},
3049 QCBOR_ERR_BAD_OPT_TAG
3050 },
3051 { "Epoch date, with an byte string",
3052 QCBOR_DECODE_MODE_NORMAL,
3053 {"\xc1\x41\x33", 3},
3054 QCBOR_ERR_BAD_OPT_TAG
3055 },
3056 { "tagged as both epoch and string dates",
3057 QCBOR_DECODE_MODE_NORMAL,
3058 {"\xc1\xc0\x00", 3},
3059 QCBOR_ERR_BAD_OPT_TAG
3060 },
3061 { "big num tagged an int, not a byte string",
3062 QCBOR_DECODE_MODE_NORMAL,
3063 {"\xc2\x00", 2},
3064 QCBOR_ERR_BAD_OPT_TAG
3065 },
Laurence Lundblade37286c02022-09-03 10:05:02 -07003066#else /* QCBOR_DISABLE_TAGS */
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08003067 /* In addition to not-well-formed, some invalid CBOR */
3068 { "Text-based date, with an integer",
3069 QCBOR_DECODE_MODE_NORMAL,
3070 {"\xc0\x00", 2},
3071 QCBOR_ERR_TAGS_DISABLED
3072 },
3073 { "Epoch date, with an byte string",
3074 QCBOR_DECODE_MODE_NORMAL,
3075 {"\xc1\x41\x33", 3},
3076 QCBOR_ERR_TAGS_DISABLED
3077 },
3078 { "tagged as both epoch and string dates",
3079 QCBOR_DECODE_MODE_NORMAL,
3080 {"\xc1\xc0\x00", 3},
3081 QCBOR_ERR_TAGS_DISABLED
3082 },
3083 { "big num tagged an int, not a byte string",
3084 QCBOR_DECODE_MODE_NORMAL,
3085 {"\xc2\x00", 2},
3086 QCBOR_ERR_TAGS_DISABLED
3087 },
Laurence Lundblade37286c02022-09-03 10:05:02 -07003088#endif /* QCBOR_DISABLE_TAGS */
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003089};
3090
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08003091
3092
3093int32_t
3094DecodeFailureTests(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003095{
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003096 int32_t nResult;
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07003097
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08003098 nResult = ProcessDecodeFailures(Failures ,C_ARRAY_COUNT(Failures, struct DecodeFailTestInput));
Laurence Lundblade59289e52019-12-30 13:44:37 -08003099 if(nResult) {
3100 return nResult;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003101 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003102
Laurence Lundblade3a6042e2019-06-28 19:58:04 -07003103 // Corrupt the UsefulInputBuf and see that
3104 // it reflected correctly for CBOR decoding
Laurence Lundbladee6f15112020-07-23 18:44:16 -07003105 QCBORDecodeContext DCtx;
3106 QCBORItem Item;
3107 QCBORError uQCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003108
Laurence Lundbladee6f15112020-07-23 18:44:16 -07003109 QCBORDecode_Init(&DCtx,
3110 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSimpleValues),
3111 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003112
Laurence Lundbladee6f15112020-07-23 18:44:16 -07003113 if((uQCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
3114 return (int32_t)uQCBORError;
3115 }
3116 if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.val.uCount != 10) {
3117 // This wasn't supposed to happen
3118 return -1;
3119 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003120
Laurence Lundbladee6f15112020-07-23 18:44:16 -07003121 DCtx.InBuf.magic = 0; // Reach in and corrupt the UsefulInputBuf
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003122
Laurence Lundbladee6f15112020-07-23 18:44:16 -07003123 uQCBORError = QCBORDecode_GetNext(&DCtx, &Item);
3124 if(uQCBORError != QCBOR_ERR_NO_MORE_ITEMS) {
3125 // Did not get back the error expected
3126 return -2;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003127 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003128
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003129
Laurence Lundblade98427e92020-09-28 21:33:23 -07003130 /*
3131 The max size of a string for QCBOR is SIZE_MAX - 4 so this
3132 tests here can be performed to see that the max length
3133 error check works correctly. See DecodeBytes(). If the max
3134 size was SIZE_MAX, it wouldn't be possible to test this.
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003135
Laurence Lundblade98427e92020-09-28 21:33:23 -07003136 This test will automatocally adapt the all CPU sizes
3137 through the use of SIZE_MAX.
3138 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003139
Laurence Lundblade8510f8c2020-12-01 11:31:16 -08003140 UsefulBuf_MAKE_STACK_UB( HeadBuf, QCBOR_HEAD_BUFFER_SIZE);
Laurence Lundblade98427e92020-09-28 21:33:23 -07003141 UsefulBufC EncodedHead;
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003142
Laurence Lundblade98427e92020-09-28 21:33:23 -07003143 // This makes a CBOR head with a text string that is very long
3144 // but doesn't fill in the bytes of the text string as that is
3145 // not needed to test this part of QCBOR.
3146 EncodedHead = QCBOREncode_EncodeHead(HeadBuf, CBOR_MAJOR_TYPE_TEXT_STRING, 0, SIZE_MAX);
3147
3148 QCBORDecode_Init(&DCtx, EncodedHead, QCBOR_DECODE_MODE_NORMAL);
3149
3150 if(QCBOR_ERR_STRING_TOO_LONG != QCBORDecode_GetNext(&DCtx, &Item)) {
3151 return -4;
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003152 }
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003153
Laurence Lundblade3a6042e2019-06-28 19:58:04 -07003154 return 0;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003155}
3156
3157
Laurence Lundbladea2e29072018-12-30 09:20:06 -08003158/* Try all 256 values of the byte at nLen including recursing for
3159 each of the values to try values at nLen+1 ... up to nLenMax
3160 */
Laurence Lundblade06350ea2020-01-27 19:32:40 -08003161static void ComprehensiveInputRecurser(uint8_t *pBuf, size_t nLen, size_t nLenMax)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003162{
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003163 if(nLen >= nLenMax) {
3164 return;
3165 }
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08003166
Laurence Lundbladea2e29072018-12-30 09:20:06 -08003167 for(int inputByte = 0; inputByte < 256; inputByte++) {
3168 // Set up the input
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003169 pBuf[nLen] = (uint8_t)inputByte;
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08003170 const UsefulBufC Input = {pBuf, nLen+1};
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08003171
Laurence Lundbladea2e29072018-12-30 09:20:06 -08003172 // Get ready to parse
3173 QCBORDecodeContext DCtx;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003174 QCBORDecode_Init(&DCtx, Input, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003175
Laurence Lundbladea2e29072018-12-30 09:20:06 -08003176 // Parse by getting the next item until an error occurs
3177 // Just about every possible decoder error can occur here
3178 // The goal of this test is not to check for the correct
3179 // error since that is not really possible. It is to
3180 // see that there is no crash on hostile input.
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003181 while(1) {
Laurence Lundbladea2e29072018-12-30 09:20:06 -08003182 QCBORItem Item;
3183 QCBORError nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003184 if(nCBORError != QCBOR_SUCCESS) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003185 break;
3186 }
3187 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003188
Laurence Lundbladea2e29072018-12-30 09:20:06 -08003189 ComprehensiveInputRecurser(pBuf, nLen+1, nLenMax);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003190 }
3191}
3192
3193
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03003194int32_t ComprehensiveInputTest(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003195{
Laurence Lundbladea2e29072018-12-30 09:20:06 -08003196 // Size 2 tests 64K inputs and runs quickly
3197 uint8_t pBuf[2];
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003198
Laurence Lundbladea2e29072018-12-30 09:20:06 -08003199 ComprehensiveInputRecurser(pBuf, 0, sizeof(pBuf));
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003200
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003201 return 0;
3202}
3203
Laurence Lundbladea2e29072018-12-30 09:20:06 -08003204
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03003205int32_t BigComprehensiveInputTest(void)
Laurence Lundbladea2e29072018-12-30 09:20:06 -08003206{
3207 // size 3 tests 16 million inputs and runs OK
3208 // in seconds on fast machines. Size 4 takes
3209 // 10+ minutes and 5 half a day on fast
3210 // machines. This test is kept separate from
3211 // the others so as to no slow down the use
3212 // of them as a very frequent regression.
3213 uint8_t pBuf[3]; //
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08003214
Laurence Lundbladea2e29072018-12-30 09:20:06 -08003215 ComprehensiveInputRecurser(pBuf, 0, sizeof(pBuf));
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08003216
Laurence Lundbladea2e29072018-12-30 09:20:06 -08003217 return 0;
3218}
3219
3220
Laurence Lundbladecc7da412020-12-27 00:09:07 -08003221static const uint8_t spDateTestInput[] = {
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003222 /* 1. The valid date string "1985-04-12" */
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003223 0xc0, // tag for string date
3224 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003225
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003226 /* 2. An invalid date string due to wrong tag content type */
Laurence Lundbladec7114722020-08-13 05:11:40 -07003227 0xc0, // tag for string date
3228 0x00, // Wrong type for a string date
3229
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003230 /* 3. A valid epoch date, 1400000000; Tue, 13 May 2014 16:53:20 GMT */
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003231 0xc1, // tag for epoch date
3232 0x1a, 0x53, 0x72, 0x4E, 0x00, // Epoch date 1400000000; Tue, 13 May 2014 16:53:20 GMT
3233
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003234 /* 4. An invalid epoch date due to wrong tag content type */
Laurence Lundbladec7114722020-08-13 05:11:40 -07003235 0xc1,
3236 0x62, 'h', 'i', // wrong type tagged
3237
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003238 /* 5. Valid epoch date tag as content for a two other nested tags */
Laurence Lundblade99615302020-11-29 11:19:47 -08003239 // CBOR_TAG_ENC_AS_B64
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003240 0xcf, 0xd8, 0x16, 0xc1, // Epoch date with extra tags
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003241 0x1a, 0x53, 0x72, 0x4E, 0x01,
3242
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003243 /* 6. Epoch date with value to large to fit into int64 */
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003244 0xc1, // tag for epoch date
3245 0x1b, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, // Too large integer
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003246
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003247 /* 7. Epoch date with single-precision value of 1.1. */
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003248 0xc1, // tag for epoch date
Laurence Lundblade3ed0bca2020-07-14 22:50:10 -07003249 0xfa, 0x3f, 0x8c, 0xcc, 0xcd, // single with value 1.1
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003250
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003251 /* 8. Epoch date with too-large single precision float */
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003252 0xc1, // tag for epoch date
Laurence Lundbladea1ad8782019-11-08 00:12:11 -08003253 0xfa, 0x7f, 0x7f, 0xff, 0xff, // 3.4028234663852886e+38 too large
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003254
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003255 /* 9. Epoch date with slightly too-large double precision value */
Laurence Lundbladea1ad8782019-11-08 00:12:11 -08003256 0xc1, // tag for epoch date
3257 0xfb, 0x43, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 9223372036854775808.000000 just barely too large
3258 //0xfa, 0x7f, 0x7f, 0xff, 0xff // 3.4028234663852886e+38 too large
3259
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003260 /* 10. Epoch date with largest supported double precision value */
Laurence Lundbladea1ad8782019-11-08 00:12:11 -08003261 0xc1, // tag for epoch date
Laurence Lundbladec7114722020-08-13 05:11:40 -07003262 0xfb, 0x43, 0xdf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, // 9223372036854773760 largest supported
3263
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003264 /* 11. Epoch date with single-precision NaN */
Laurence Lundbladec7114722020-08-13 05:11:40 -07003265 0xc1, // tag for epoch date
3266 0xfa, 0x7f, 0xc0, 0x00, 0x00, // Single-precision NaN
3267
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003268 /* 12. Epoch date with double precision plus infinity */
Laurence Lundbladec7114722020-08-13 05:11:40 -07003269 0xc1,
3270 0xfb, 0x7f, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // +infinity
3271
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003272 /* 13. Epoch date with half-precision negative infinity */
Laurence Lundbladec7114722020-08-13 05:11:40 -07003273 0xc1, // tag for epoch date
3274 0xf9, 0xfc, 0x00, // -Infinity
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003275};
3276
3277
Laurence Lundbladec7114722020-08-13 05:11:40 -07003278
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003279// have to check float expected only to within an epsilon
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07003280#ifndef QCBOR_DISABLE_FLOAT_HW_USE
Laurence Lundblade02fcf312020-07-17 02:49:46 -07003281static int CHECK_EXPECTED_DOUBLE(double val, double expected) {
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003282
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003283 double diff = val - expected;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003284
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003285 diff = fabs(diff);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003286
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003287 return diff > 0.0000001;
3288}
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07003289#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003290
3291
Laurence Lundblade37286c02022-09-03 10:05:02 -07003292/* Test date decoding using GetNext() */
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03003293int32_t DateParseTest(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003294{
3295 QCBORDecodeContext DCtx;
Laurence Lundbladec7114722020-08-13 05:11:40 -07003296 QCBORItem Item;
3297 QCBORError uError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003298
Laurence Lundbladeee851742020-01-08 08:37:05 -08003299 QCBORDecode_Init(&DCtx,
3300 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spDateTestInput),
3301 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003302
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003303 /* 1. The valid date string "1985-04-12" */
Laurence Lundbladec7114722020-08-13 05:11:40 -07003304 if((uError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003305 return -1;
Laurence Lundbladec7114722020-08-13 05:11:40 -07003306 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003307 if(Item.uDataType != QCBOR_TYPE_DATE_STRING ||
Laurence Lundbladeba587682024-02-07 16:46:43 -08003308 UsefulBufCompareToSZ(Item.val.string, "1985-04-12")){
Laurence Lundblade67bd5512018-11-02 21:44:06 +07003309 return -2;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003310 }
3311
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003312 /* 2. An invalid date string due to wrong tag content type */
Laurence Lundbladec7114722020-08-13 05:11:40 -07003313 uError = QCBORDecode_GetNext(&DCtx, &Item);
3314 if(uError != QCBOR_ERR_BAD_OPT_TAG) {
Laurence Lundblade67bd5512018-11-02 21:44:06 +07003315 return -3;
Laurence Lundbladec7114722020-08-13 05:11:40 -07003316 }
3317
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003318 /* 3. A valid epoch date, 1400000000; Tue, 13 May 2014 16:53:20 GMT */
3319 uError = QCBORDecode_GetNext(&DCtx, &Item);
3320 if(uError != QCBOR_SUCCESS) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07003321 return -4;
3322 }
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003323 if(uError == QCBOR_SUCCESS) {
3324 if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH ||
3325 Item.val.epochDate.nSeconds != 1400000000
Máté Tóth-Pálef5f07a2021-09-17 19:31:37 +02003326#ifndef USEFULBUF_DISABLE_ALL_FLOAT
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003327 || Item.val.epochDate.fSecondsFraction != 0
Máté Tóth-Pálef5f07a2021-09-17 19:31:37 +02003328#endif /* USEFULBUF_DISABLE_ALL_FLOAT */
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003329 ) {
3330 return -5;
3331 }
Laurence Lundbladec7114722020-08-13 05:11:40 -07003332 }
3333
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003334 /* 4. An invalid epoch date due to wrong tag content type */
Laurence Lundbladec7114722020-08-13 05:11:40 -07003335 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_OPT_TAG) {
3336 return -6;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003337 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003338
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003339 /* 5. Valid epoch date tag as content for a two other nested tags */
Laurence Lundblade99615302020-11-29 11:19:47 -08003340 // Epoch date wrapped in an CBOR_TAG_ENC_AS_B64 and an unknown tag.
3341 // The date is decoded and the two tags are returned. This is to
3342 // make sure the wrapping of epoch date in another tag works OK.
Laurence Lundbladec7114722020-08-13 05:11:40 -07003343 if((uError = QCBORDecode_GetNext(&DCtx, &Item))) {
3344 return -7;
3345 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003346 if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH ||
3347 Item.val.epochDate.nSeconds != 1400000001 ||
Máté Tóth-Pálef5f07a2021-09-17 19:31:37 +02003348#ifndef USEFULBUF_DISABLE_ALL_FLOAT
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003349 Item.val.epochDate.fSecondsFraction != 0 ||
Máté Tóth-Pálef5f07a2021-09-17 19:31:37 +02003350#endif /* USEFULBUF_DISABLE_ALL_FLOAT */
Laurence Lundblade99615302020-11-29 11:19:47 -08003351 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_ENC_AS_B64)) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07003352 return -8;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003353 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003354
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003355 /* 6. Epoch date with value to large to fit into int64 */
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003356 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_DATE_OVERFLOW) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07003357 return -9;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003358 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003359
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003360 /* 7. Epoch date with single-precision value of 1.1. */
3361 uError = QCBORDecode_GetNext(&DCtx, &Item);
3362 if(uError != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07003363 return -10;
3364 }
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003365 if(uError == QCBOR_SUCCESS) {
3366 if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH ||
3367 Item.val.epochDate.nSeconds != 1
3368#ifndef QCBOR_DISABLE_FLOAT_HW_USE
3369 || CHECK_EXPECTED_DOUBLE(Item.val.epochDate.fSecondsFraction, 0.1)
Máté Tóth-Pálef5f07a2021-09-17 19:31:37 +02003370#endif /* USEFULBUF_DISABLE_ALL_FLOAT */
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003371 ) {
3372 return -11;
3373 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003374 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003375
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003376 /* 8. Epoch date with too-large single-precision float */
3377 uError = QCBORDecode_GetNext(&DCtx, &Item);
3378 if(uError != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_DATE_OVERFLOW)) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07003379 return -12;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003380 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003381
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003382 /* 9. Epoch date with slightly too-large double-precision value */
3383 uError = QCBORDecode_GetNext(&DCtx, &Item);
3384 if(uError != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_DATE_OVERFLOW)) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07003385 return -13;
Laurence Lundbladea1ad8782019-11-08 00:12:11 -08003386 }
3387
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003388 /* 10. Epoch date with largest supported double-precision value */
3389 uError = QCBORDecode_GetNext(&DCtx, &Item);
3390 if(uError != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)) {
3391 return -14;
3392 }
3393 if(uError == QCBOR_SUCCESS) {
3394 if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH ||
3395 Item.val.epochDate.nSeconds != 9223372036854773760
3396#ifndef QCBOR_DISABLE_FLOAT_HW_USE
3397 || Item.val.epochDate.fSecondsFraction != 0.0
3398#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
3399 ) {
3400 return -14;
3401 }
3402 }
Laurence Lundbladec7114722020-08-13 05:11:40 -07003403
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003404 /* 11. Epoch date with single-precision NaN */
3405 if(QCBORDecode_GetNext(&DCtx, &Item) != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_DATE_OVERFLOW)) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07003406 return -15;
3407 }
3408
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003409 /* 12. Epoch date with double-precision plus infinity */
3410 if(QCBORDecode_GetNext(&DCtx, &Item) != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_DATE_OVERFLOW)) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07003411 return -16;
3412 }
3413
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003414 /* 13. Epoch date with half-precision negative infinity */
3415 uError = QCBORDecode_GetNext(&DCtx, &Item);
3416 if(uError != FLOAT_ERR_CODE_NO_HALF_PREC_NO_FLOAT_HW(QCBOR_ERR_DATE_OVERFLOW)) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07003417 return -17;
3418 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003419
3420 return 0;
3421}
3422
Laurence Lundblade37286c02022-09-03 10:05:02 -07003423
Laurence Lundblade4b270642020-08-14 12:53:07 -07003424/*
3425 Test cases covered here. Some items cover more than one of these.
3426 positive integer (zero counts as a positive integer)
3427 negative integer
3428 half-precision float
3429 single-precision float
3430 double-precision float
Laurence Lundbladec7114722020-08-13 05:11:40 -07003431
Laurence Lundblade4b270642020-08-14 12:53:07 -07003432 float Overflow error
3433 Wrong type error for epoch
3434 Wrong type error for date string
3435 float disabled error
3436 half-precision disabled error
3437 -Infinity
3438 Slightly too large integer
3439 Slightly too far from zero
Laurence Lundbladec7114722020-08-13 05:11:40 -07003440
Laurence Lundblade4b270642020-08-14 12:53:07 -07003441 Get epoch by int
3442 Get string by int
3443 Get epoch by string
3444 Get string by string
3445 Fail to get epoch by wrong int label
3446 Fail to get string by wrong string label
3447 Fail to get epoch by string because it is invalid
3448 Fail to get epoch by int because it is invalid
3449
3450 Untagged values
3451 */
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08003452static const uint8_t spSpiffyDateTestInput[] = {
Laurence Lundblade37286c02022-09-03 10:05:02 -07003453 0x87, // array of 7 items
3454
3455 0xa6, // Open a map for tests involving untagged items with labels.
3456
3457 // Untagged integer 0
3458 0x08,
3459 0x00,
3460
3461 // Utagged date string with string label y
3462 0x61, 0x79,
3463 0x6a, '2','0','8','5','-','0','4','-','1','2', // Untagged date string
3464
3465 // Untagged single-precision float with value 3.14 with string label x
3466 0x61, 0x78,
3467 0xFA, 0x40, 0x48, 0xF5, 0xC3,
3468
3469 // Untagged half-precision float with value -2
3470 0x09,
3471 0xF9, 0xC0, 0x00,
3472
3473 /* Untagged date-only date string */
3474 0x18, 0x63,
3475 0x6A, 0x31, 0x39, 0x38, 0x35, 0x2D, 0x30, 0x34, 0x2D, 0x31, 0x32, /* "1985-04-12" */
3476
3477 /* Untagged days-count epoch date */
3478 0x11,
3479 0x19, 0x0F, 0x9A, /* 3994 */
3480
3481 // End of map, back to array
3482
3483 0xa7, // Open map of tagged items with labels
3484
3485 0x00,
3486 0xc0, // tag for string date
3487 0x6a, '1','9','8','5','-','0','4','-','1','2', // Tagged date string
3488
3489
3490 0x01,
3491 0xda, 0x03, 0x03, 0x03, 0x03, // An additional tag
3492 0xc1, // tag for epoch date
3493 0x1a, 0x53, 0x72, 0x4E, 0x00, // Epoch date 1400000000; Tue, 13 May 2014 16:53:20 GMT
3494
3495 0x05,
3496 0xc1,
3497 0xfb, 0xc3, 0xdf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, // -9223372036854773760 largest negative
3498
3499
3500 0x07,
3501 0xc1, // tag for epoch date
3502 0xfb, 0x43, 0xdf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, // 9223372036854773760 largest supported
3503
3504 /* Tagged days-count epoch date */
3505 0x63, 0x53, 0x44, 0x45,
3506 0xD8, 0x64, /* tag(100) */
3507 0x39, 0x29, 0xB3, /* -10676 */
3508
3509 // Untagged -1000 with label z
3510 0x61, 0x7a,
3511 0xda, 0x01, 0x01, 0x01, 0x01, // An additional tag
3512 0x39, 0x03, 0xe7,
3513
3514 /* Tagged date-only date string */
3515 0x63, 0x53, 0x44, 0x53,
3516 0xD9, 0x03, 0xEC,
3517 0x6A, 0x31, 0x39, 0x38, 0x35, 0x2D, 0x30, 0x34, 0x2D, 0x31, 0x32, /* "1985-04-12" */
3518
3519 // End of map of tagged items
Laurence Lundblade4b270642020-08-14 12:53:07 -07003520
3521 0xc1,
3522 0xfb, 0xc3, 0xdf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, // -9.2233720368547748E+18, too negative
3523
Laurence Lundbladec7114722020-08-13 05:11:40 -07003524 0xc1, // tag for epoch date
Laurence Lundblade4b270642020-08-14 12:53:07 -07003525 0x1b, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, // Too-large integer
3526
3527 0xc1, // tag for epoch date
3528 0xf9, 0xfc, 0x00, // Half-precision -Infinity
3529
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07003530 // These two at the end because they are unrecoverable errors
3531 0xc1, // tag for epoch date
3532 0x80, // Erroneous empty array as content for date
3533
3534 0xc0, // tag for string date
3535 0xa0 // Erroneous empty map as content for date
Laurence Lundbladec7114722020-08-13 05:11:40 -07003536};
3537
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03003538int32_t SpiffyDateDecodeTest(void)
Laurence Lundbladec7114722020-08-13 05:11:40 -07003539{
3540 QCBORDecodeContext DC;
Laurence Lundblade4b270642020-08-14 12:53:07 -07003541 QCBORError uError;
Laurence Lundblade37286c02022-09-03 10:05:02 -07003542 int64_t nEpochDate3, nEpochDate5,
3543 nEpochDate4, nEpochDate6,
3544 nEpochDays2;
3545 UsefulBufC StringDate1, StringDate2, StringDays2;
Laurence Lundbladec7114722020-08-13 05:11:40 -07003546
3547 QCBORDecode_Init(&DC,
Laurence Lundblade4b270642020-08-14 12:53:07 -07003548 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSpiffyDateTestInput),
Laurence Lundbladec7114722020-08-13 05:11:40 -07003549 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade37286c02022-09-03 10:05:02 -07003550
3551 /* Items are in an array or map to test look up by label and other
3552 * that might not occur in isolated items. But it does make the
3553 * test a bit messy. */
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07003554 QCBORDecode_EnterArray(&DC, NULL);
Laurence Lundbladec7114722020-08-13 05:11:40 -07003555
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07003556 QCBORDecode_EnterMap(&DC, NULL);
Laurence Lundblade4b270642020-08-14 12:53:07 -07003557
Laurence Lundblade4b270642020-08-14 12:53:07 -07003558 // A single-precision date
Laurence Lundblade9b334962020-08-27 10:55:53 -07003559 QCBORDecode_GetEpochDateInMapSZ(&DC, "x", QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
3560 &nEpochDate5);
Laurence Lundblade4b270642020-08-14 12:53:07 -07003561 uError = QCBORDecode_GetAndResetError(&DC);
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003562 if(uError != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07003563 return 104;
3564 }
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003565 if(uError == QCBOR_SUCCESS) {
3566 if(nEpochDate5 != 3) {
3567 return 103;
3568 }
3569 }
Laurence Lundbladec7114722020-08-13 05:11:40 -07003570
Laurence Lundblade9b334962020-08-27 10:55:53 -07003571 // A half-precision date with value -2 FFF
3572 QCBORDecode_GetEpochDateInMapN(&DC, 9, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
3573 &nEpochDate4);
Laurence Lundblade4b270642020-08-14 12:53:07 -07003574 uError = QCBORDecode_GetAndResetError(&DC);
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003575 if(uError != FLOAT_ERR_CODE_NO_HALF_PREC_NO_FLOAT_HW(QCBOR_SUCCESS)) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07003576 return 106;
3577 }
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003578 if(uError == QCBOR_SUCCESS) {
3579 if(nEpochDate4 != -2) {
3580 return 105;
3581 }
3582 }
Laurence Lundblade4b270642020-08-14 12:53:07 -07003583
3584 // Fail to get an epoch date by string label
Laurence Lundblade9b334962020-08-27 10:55:53 -07003585 QCBORDecode_GetEpochDateInMapSZ(&DC, "no-label",
3586 QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
3587 &nEpochDate6);
Laurence Lundblade4b270642020-08-14 12:53:07 -07003588 uError = QCBORDecode_GetAndResetError(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07003589 if(uError != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07003590 return 107;
3591 }
3592
3593 // Fail to get an epoch date by integer label
Laurence Lundblade9b334962020-08-27 10:55:53 -07003594 QCBORDecode_GetEpochDateInMapN(&DC, 99999, QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
3595 &nEpochDate6);
Laurence Lundblade4b270642020-08-14 12:53:07 -07003596 uError = QCBORDecode_GetAndResetError(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07003597 if(uError != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07003598 return 108;
3599 }
3600
3601 // Fail to get a string date by string label
Laurence Lundblade9b334962020-08-27 10:55:53 -07003602 QCBORDecode_GetDateStringInMapSZ(&DC, "no-label",
3603 QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
3604 &StringDate1);
Laurence Lundblade4b270642020-08-14 12:53:07 -07003605 uError = QCBORDecode_GetAndResetError(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07003606 if(uError != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07003607 return 109;
3608 }
3609
3610 // Fail to get a string date by integer label
Laurence Lundblade9b334962020-08-27 10:55:53 -07003611 QCBORDecode_GetDateStringInMapN(&DC, 99999, QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
3612 &StringDate1);
Laurence Lundblade4b270642020-08-14 12:53:07 -07003613 uError = QCBORDecode_GetAndResetError(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07003614 if(uError != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07003615 return 110;
3616 }
3617
3618 // The rest of these succeed even if float features are disabled
Laurence Lundbladea9489f82020-09-12 13:50:56 -07003619
Laurence Lundblade37286c02022-09-03 10:05:02 -07003620
3621 // Untagged integer 0
3622 QCBORDecode_GetEpochDateInMapN(&DC, 8, QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
3623 &nEpochDate3);
3624 // Untagged date string
3625 QCBORDecode_GetDateStringInMapSZ(&DC, "y", QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
3626 &StringDate2);
3627
3628 QCBORDecode_GetDaysStringInMapN(&DC, 99, QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
3629 &StringDays2);
3630
3631 QCBORDecode_GetEpochDaysInMapN(&DC, 17, QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
3632 &nEpochDays2);
3633
3634 QCBORDecode_ExitMap(&DC);
3635 if(QCBORDecode_GetError(&DC) != QCBOR_SUCCESS) {
3636 return 3001;
3637 }
3638
3639 // The map of tagged items
3640 QCBORDecode_EnterMap(&DC, NULL);
3641
3642#ifndef QCBOR_DISABLE_TAGS
3643 int64_t nEpochDate2,
3644 nEpochDateFail,
3645 nEpochDate1400000000, nEpochDays1;
3646 UsefulBufC StringDays1;
3647 uint64_t uTag1, uTag2;
3648
3649 // Tagged date string
3650 QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
3651 &StringDate1);
3652
Laurence Lundblade4b270642020-08-14 12:53:07 -07003653 // Epoch date 1400000000; Tue, 13 May 2014 16:53:20 GMT
Laurence Lundblade9b334962020-08-27 10:55:53 -07003654 QCBORDecode_GetEpochDateInMapN(&DC,
3655 1,
3656 QCBOR_TAG_REQUIREMENT_TAG |
3657 QCBOR_TAG_REQUIREMENT_ALLOW_ADDITIONAL_TAGS,
3658 &nEpochDate1400000000);
3659 uTag1 = QCBORDecode_GetNthTagOfLast(&DC, 0);
Laurence Lundblade37286c02022-09-03 10:05:02 -07003660
3661 // Get largest negative double precision epoch date allowed
3662 QCBORDecode_GetEpochDateInMapN(&DC,
3663 5,
3664 QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG |
3665 QCBOR_TAG_REQUIREMENT_ALLOW_ADDITIONAL_TAGS,
3666 &nEpochDate2);
3667 uError = QCBORDecode_GetAndResetError(&DC);
3668 if(uError != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)) {
3669 return 102;
3670 }
3671 if(uError == QCBOR_SUCCESS) {
3672 if(nEpochDate2 != -9223372036854773760LL) {
3673 return 101;
3674 }
3675 }
3676
Laurence Lundblade4b270642020-08-14 12:53:07 -07003677 // Untagged -1000 with label z
Laurence Lundblade9b334962020-08-27 10:55:53 -07003678 QCBORDecode_GetEpochDateInMapSZ(&DC,
3679 "z",
3680 QCBOR_TAG_REQUIREMENT_NOT_A_TAG |
3681 QCBOR_TAG_REQUIREMENT_ALLOW_ADDITIONAL_TAGS,
3682 &nEpochDate6);
3683 uTag2 = QCBORDecode_GetNthTagOfLast(&DC, 0);
Laurence Lundblade4b270642020-08-14 12:53:07 -07003684
Laurence Lundblade37286c02022-09-03 10:05:02 -07003685
3686 // Get largest double precision epoch date allowed
3687 QCBORDecode_GetEpochDateInMapN(&DC, 7, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
3688 &nEpochDate2);
3689 uError = QCBORDecode_GetAndResetError(&DC);
3690 if(uError != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)) {
3691 return 112;
3692 }
3693 if(uError == QCBOR_SUCCESS) {
3694 if(nEpochDate2 != 9223372036854773760ULL) {
3695 return 111;
3696 }
3697 }
3698
Laurence Lundblade46d63e92021-05-13 11:37:10 -07003699 /* The days format is much simpler than the date format
3700 * because it can't be a floating point value. The test
3701 * of the spiffy decode functions sufficiently covers
3702 * the test of the non-spiffy decode days date decoding.
3703 * There is no full fan out of the error conditions
3704 * and decode options as that is implemented by code
3705 * that is tested well by the date testing above.
3706 */
3707 QCBORDecode_GetDaysStringInMapSZ(&DC, "SDS", QCBOR_TAG_REQUIREMENT_TAG,
3708 &StringDays1);
3709
Laurence Lundblade46d63e92021-05-13 11:37:10 -07003710 QCBORDecode_GetEpochDaysInMapSZ(&DC, "SDE", QCBOR_TAG_REQUIREMENT_TAG,
3711 &nEpochDays1);
3712
Laurence Lundblade4b270642020-08-14 12:53:07 -07003713 QCBORDecode_ExitMap(&DC);
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07003714 if(QCBORDecode_GetError(&DC) != QCBOR_SUCCESS) {
3715 return 3001;
3716 }
3717
Laurence Lundblade37286c02022-09-03 10:05:02 -07003718 // Too-negative float, -9.2233720368547748E+18
3719 QCBORDecode_GetEpochDate(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nEpochDateFail);
3720 uError = QCBORDecode_GetAndResetError(&DC);
3721 if(uError != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_DATE_OVERFLOW)) {
3722 return 1111;
3723 }
3724
3725 // Too-large integer
3726 QCBORDecode_GetEpochDate(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nEpochDateFail);
3727 uError = QCBORDecode_GetAndResetError(&DC);
3728 if(uError != QCBOR_ERR_DATE_OVERFLOW) {
3729 return 1;
3730 }
3731
3732 // Half-precision minus infinity
3733 QCBORDecode_GetEpochDate(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nEpochDateFail);
3734 uError = QCBORDecode_GetAndResetError(&DC);
3735 if(uError != FLOAT_ERR_CODE_NO_HALF_PREC_NO_FLOAT_HW(QCBOR_ERR_DATE_OVERFLOW)) {
3736 return 2;
3737 }
3738
3739
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07003740 // Bad content for epoch date
3741 QCBORDecode_GetEpochDate(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nEpochDateFail);
3742 uError = QCBORDecode_GetAndResetError(&DC);
3743 if(uError != QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT) {
3744 return 3;
3745 }
3746
3747 // Bad content for string date
3748 QCBORDecode_GetDateString(&DC, QCBOR_TAG_REQUIREMENT_TAG, &StringDate1);
3749 uError = QCBORDecode_GetAndResetError(&DC);
3750 if(uError != QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT) {
3751 return 4;
3752 }
3753
Laurence Lundblade4b270642020-08-14 12:53:07 -07003754 QCBORDecode_ExitArray(&DC);
3755 uError = QCBORDecode_Finish(&DC);
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07003756 if(uError != QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT) {
Laurence Lundblade9b334962020-08-27 10:55:53 -07003757 return 1000 + (int32_t)uError;
Laurence Lundblade4b270642020-08-14 12:53:07 -07003758 }
Laurence Lundblade37286c02022-09-03 10:05:02 -07003759#else /* QCBOR_DISABLE_TAGS */
3760 QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
3761 &StringDate1);
3762 uError = QCBORDecode_GetAndResetError(&DC);
3763 if(uError != QCBOR_ERR_TAGS_DISABLED) {
3764 return 4;
3765 }
3766#endif /* QCBOR_DISABLE_TAGS */
3767
3768
3769#ifndef QCBOR_DISABLE_TAGS
Laurence Lundblade4b270642020-08-14 12:53:07 -07003770
Laurence Lundblade9b334962020-08-27 10:55:53 -07003771 if(nEpochDate1400000000 != 1400000000) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07003772 return 200;
3773 }
3774
Laurence Lundblade9b334962020-08-27 10:55:53 -07003775 if(uTag1 != 0x03030303) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07003776 return 201;
3777 }
3778
Laurence Lundblade37286c02022-09-03 10:05:02 -07003779 if(nEpochDays1 != -10676) {
3780 return 205;
Laurence Lundblade4b270642020-08-14 12:53:07 -07003781 }
3782
Laurence Lundblade37286c02022-09-03 10:05:02 -07003783 if(UsefulBuf_Compare(StringDays1, UsefulBuf_FromSZ("1985-04-12"))) {
3784 return 207;
Laurence Lundblade4b270642020-08-14 12:53:07 -07003785 }
3786
Laurence Lundblade9b334962020-08-27 10:55:53 -07003787 if(uTag2 != 0x01010101) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07003788 return 204;
3789 }
3790
Laurence Lundblade37286c02022-09-03 10:05:02 -07003791 if(nEpochDate6 != -1000) {
3792 return 203;
Laurence Lundblade46d63e92021-05-13 11:37:10 -07003793 }
3794
Laurence Lundblade9b334962020-08-27 10:55:53 -07003795 if(UsefulBuf_Compare(StringDate1, UsefulBuf_FromSZ("1985-04-12"))) {
3796 return 205;
3797 }
3798
Laurence Lundblade37286c02022-09-03 10:05:02 -07003799#endif /* QCBOR_DISABLE_TAGS */
3800
3801 if(nEpochDate3 != 0) {
3802 return 202;
3803 }
3804
3805 if(nEpochDays2 != 3994) {
Laurence Lundblade9b334962020-08-27 10:55:53 -07003806 return 206;
3807 }
3808
Laurence Lundblade37286c02022-09-03 10:05:02 -07003809 if(UsefulBuf_Compare(StringDate2, UsefulBuf_FromSZ("2085-04-12"))) {
3810 return 206;
Laurence Lundblade46d63e92021-05-13 11:37:10 -07003811 }
3812
3813 if(UsefulBuf_Compare(StringDays2, UsefulBuf_FromSZ("1985-04-12"))) {
3814 return 208;
3815 }
3816
Laurence Lundbladec7114722020-08-13 05:11:40 -07003817 return 0;
3818}
3819
3820
Laurence Lundblade9b334962020-08-27 10:55:53 -07003821// Input for one of the tagging tests
Laurence Lundbladecc7da412020-12-27 00:09:07 -08003822static const uint8_t spTagInput[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003823 0xd9, 0xd9, 0xf7, // CBOR magic number
Laurence Lundblade9b334962020-08-27 10:55:53 -07003824 0x81, // Array of one
3825 0xd8, 0x04, // non-preferred serialization of tag 4, decimal fraction
3826 0x82, // Array of two that is the faction 1/3
3827 0x01,
3828 0x03,
3829
3830 /*
3831 More than 4 tags on an item 225(226(227(228(229([])))))
3832 */
3833 0xd8, 0xe1,
3834 0xd8, 0xe2,
3835 0xd8, 0xe3,
3836 0xd8, 0xe4,
3837 0xd8, 0xe5,
3838 0x80,
3839
3840 /* tag 10489608748473423768(
3841 2442302356(
3842 21590(
3843 240(
3844 []))))
3845 */
3846 0xdb, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98,
3847 0xda, 0x91, 0x92, 0x93, 0x94,
3848 0xd9, 0x54, 0x56,
3849 0xd8, 0xf0,
3850 0x80,
3851
3852 /* tag 21590(
3853 10489608748473423768(
3854 2442302357(
3855 65534(
3856 []))))
3857 */
3858 0xdb, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x54, 0x56,
3859 0xdb, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98,
3860 0xda, 0x91, 0x92, 0x93, 0x95,
3861 0xd9, 0xff, 0xfe,
3862 0x80,
3863
3864 /* Make sure to blow past the limit of tags that must be mapped.
3865 works in conjuntion with entries above.
3866 269488144(269488145(269488146(269488147([]))))
3867 */
3868 0xda, 0x10, 0x10, 0x10, 0x10,
3869 0xda, 0x10, 0x10, 0x10, 0x11,
3870 0xda, 0x10, 0x10, 0x10, 0x12,
3871 0xda, 0x10, 0x10, 0x10, 0x13,
3872 0x80,
3873
3874 /* An invalid decimal fraction with an additional tag */
3875 0xd9, 0xff, 0xfa,
3876 0xd8, 0x02, // non-preferred serialization of tag 2, a big num
3877 0x00, // the integer 0; should be a byte string
3878};
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003879
Laurence Lundblade59289e52019-12-30 13:44:37 -08003880/*
3881 DB 9192939495969798 # tag(10489608748473423768)
Laurence Lundblade9b334962020-08-27 10:55:53 -07003882 80 # array(0)
Laurence Lundblade59289e52019-12-30 13:44:37 -08003883 */
Laurence Lundbladecc7da412020-12-27 00:09:07 -08003884static const uint8_t spEncodedLargeTag[] = {0xdb, 0x91, 0x92, 0x93, 0x94, 0x95,
Laurence Lundbladeee851742020-01-08 08:37:05 -08003885 0x96, 0x97, 0x98, 0x80};
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003886
Laurence Lundblade59289e52019-12-30 13:44:37 -08003887/*
3888DB 9192939495969798 # tag(10489608748473423768)
3889 D8 88 # tag(136)
3890 C6 # tag(6)
3891 C7 # tag(7)
3892 80 # array(0)
3893*/
Laurence Lundbladecc7da412020-12-27 00:09:07 -08003894static const uint8_t spLotsOfTags[] = {0xdb, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96,
Laurence Lundbladeee851742020-01-08 08:37:05 -08003895 0x97, 0x98, 0xd8, 0x88, 0xc6, 0xc7, 0x80};
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003896
3897/*
Laurence Lundblade9b334962020-08-27 10:55:53 -07003898 55799(55799(55799({
3899 6(7(-23)): 5859837686836516696(7({
3900 7(-20): 11({
3901 17(-18): 17(17(17("Organization"))),
3902 9(-17): 773("SSG"),
3903 -15: 16(17(6(7("Confusion")))),
3904 17(-16): 17("San Diego"),
3905 17(-14): 17("US")
3906 }),
3907 23(-19): 19({
3908 -11: 9({
3909 -9: -7
3910 }),
3911 90599561(90599561(90599561(-10))): 12(h'0102030405060708090A')
3912 })
3913 })),
3914 16(-22): 23({
3915 11(8(7(-5))): 8(-3)
3916 })
3917 })))
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003918 */
Laurence Lundbladecc7da412020-12-27 00:09:07 -08003919static const uint8_t spCSRWithTags[] = {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003920 0xd9, 0xd9, 0xf7, 0xd9, 0xd9, 0xf7, 0xd9, 0xd9, 0xf7, 0xa2,
3921 0xc6, 0xc7, 0x36,
3922 0xdb, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0xc7, 0xa2,
3923 0xda, 0x00, 0x00, 0x00, 0x07, 0x33,
3924 0xcb, 0xa5,
3925 0xd1, 0x31,
3926 0xd1, 0xd1, 0xd1, 0x6c,
3927 0x4f, 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74, 0x69, 0x6f, 0x6e,
3928 0xc9, 0x30,
3929 0xd9, 0x03, 0x05, 0x63,
3930 0x53, 0x53, 0x47,
3931 0x2e,
Laurence Lundblade9b334962020-08-27 10:55:53 -07003932 0xd0, 0xd1, 0xc6, 0xc7,
3933 0x69,
3934 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73, 0x69, 0x6f, 0x6e,
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003935 0xd1, 0x2f,
3936 0xd1, 0x69,
3937 0x53, 0x61, 0x6e, 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f,
3938 0xd1, 0x2d,
3939 0xd1, 0x62,
3940 0x55, 0x53,
3941 0xd7, 0x32,
3942 0xd3, 0xa2,
3943 0x2a,
3944 0xc9, 0xa1,
3945 0x28,
3946 0x26,
3947 0xda, 0x05, 0x66, 0x70, 0x89, 0xda, 0x05, 0x66, 0x70, 0x89, 0xda, 0x05, 0x66, 0x70, 0x89, 0x29,
3948 0xcc, 0x4a,
3949 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,0x07, 0x08, 0x09, 0x0a,
3950 0xd0, 0x35,
3951 0xd7, 0xa1,
3952 0xcb, 0xc8, 0xc7, 0x24,
3953 0xc8, 0x22};
3954
Laurence Lundblade9b334962020-08-27 10:55:53 -07003955
Laurence Lundbladecc7da412020-12-27 00:09:07 -08003956static const uint8_t spSpiffyTagInput[] = {
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08003957 0x85, // Open array
Laurence Lundblade9b334962020-08-27 10:55:53 -07003958
3959 0xc0, // tag for string date
3960 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string
3961
3962 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string
3963
3964 0x4a, '1','9','8','5','-','0','4','-','1','2', // Date string in byte string
3965
3966 0xd8, 0x23, // tag for regex
3967 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string
3968
3969 0xc0, // tag for string date
3970 0x4a, '1','9','8','5','-','0','4','-','1','2', // Date string in byte string
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07003971
3972 // This last case makes the array untraversable because it is
3973 // an uncrecoverable error. Make sure it stays last and is the only
3974 // instance so the other tests can work.
Laurence Lundblade9b334962020-08-27 10:55:53 -07003975};
3976
3977
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003978static int32_t CheckCSRMaps(QCBORDecodeContext *pDC);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003979
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003980
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03003981int32_t OptTagParseTest(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003982{
3983 QCBORDecodeContext DCtx;
Laurence Lundblade9b334962020-08-27 10:55:53 -07003984 QCBORItem Item;
3985 QCBORError uError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003986
Laurence Lundbladeee851742020-01-08 08:37:05 -08003987 QCBORDecode_Init(&DCtx,
Laurence Lundblade9b334962020-08-27 10:55:53 -07003988 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTagInput),
Laurence Lundbladeee851742020-01-08 08:37:05 -08003989 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003990
Laurence Lundblade9b334962020-08-27 10:55:53 -07003991 /*
3992 This test matches the magic number tag and the fraction tag
3993 55799([...])
3994 */
3995 uError = QCBORDecode_GetNext(&DCtx, &Item);
3996 if(uError != QCBOR_SUCCESS) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003997 return -2;
3998 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003999 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004000 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC)) {
4001 return -3;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004002 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004003
Laurence Lundblade9b334962020-08-27 10:55:53 -07004004 /*
4005 4([1,3])
4006 */
4007 uError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundbladedd6e76e2021-03-10 01:54:01 -07004008#ifdef QCBOR_DISABLE_EXP_AND_MANTISSA
Laurence Lundblade9b334962020-08-27 10:55:53 -07004009 if(uError != QCBOR_SUCCESS ||
4010 Item.uDataType != QCBOR_TYPE_ARRAY ||
4011 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_DECIMAL_FRACTION) ||
4012 QCBORDecode_GetNthTag(&DCtx, &Item, 0) != CBOR_TAG_DECIMAL_FRACTION ||
4013 QCBORDecode_GetNthTag(&DCtx, &Item, 1) != CBOR_TAG_INVALID64 ||
4014 QCBORDecode_GetNthTag(&DCtx, &Item, 2) != CBOR_TAG_INVALID64 ||
4015 QCBORDecode_GetNthTag(&DCtx, &Item, 3) != CBOR_TAG_INVALID64 ||
4016 QCBORDecode_GetNthTag(&DCtx, &Item, 4) != CBOR_TAG_INVALID64 ||
4017 Item.val.uCount != 2) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004018 return -4;
4019 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07004020 // consume the items in the array
4021 uError = QCBORDecode_GetNext(&DCtx, &Item);
4022 uError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundblade59289e52019-12-30 13:44:37 -08004023
Laurence Lundbladedd6e76e2021-03-10 01:54:01 -07004024#else /* QCBOR_DISABLE_EXP_AND_MANTISSA */
Laurence Lundblade9b334962020-08-27 10:55:53 -07004025 if(uError != QCBOR_SUCCESS ||
4026 Item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION ||
4027 QCBORDecode_GetNthTag(&DCtx, &Item, 0) != CBOR_TAG_INVALID64 ||
4028 QCBORDecode_GetNthTag(&DCtx, &Item, 1) != CBOR_TAG_INVALID64 ||
4029 QCBORDecode_GetNthTag(&DCtx, &Item, 2) != CBOR_TAG_INVALID64 ||
4030 QCBORDecode_GetNthTag(&DCtx, &Item, 3) != CBOR_TAG_INVALID64 ||
4031 QCBORDecode_GetNthTag(&DCtx, &Item, 4) != CBOR_TAG_INVALID64 ) {
4032 return -5;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004033 }
Laurence Lundbladedd6e76e2021-03-10 01:54:01 -07004034#endif /* QCBOR_DISABLE_EXP_AND_MANTISSA */
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004035
Laurence Lundblade9b334962020-08-27 10:55:53 -07004036 /*
4037 More than 4 tags on an item 225(226(227(228(229([])))))
4038 */
4039 uError = QCBORDecode_GetNext(&DCtx, &Item);
4040 if(uError != QCBOR_ERR_TOO_MANY_TAGS) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004041 return -6;
4042 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07004043
Laurence Lundblade88e9db22020-11-02 03:56:33 -08004044 if(QCBORDecode_GetNthTag(&DCtx, &Item, 0) != CBOR_TAG_INVALID64) {
4045 return -106;
4046 }
4047
4048
Laurence Lundblade9b334962020-08-27 10:55:53 -07004049 /* tag 10489608748473423768(
4050 2442302356(
4051 21590(
4052 240(
4053 []))))
4054 */
4055 uError = QCBORDecode_GetNext(&DCtx, &Item);
4056 if(uError != QCBOR_SUCCESS ||
4057 Item.uDataType != QCBOR_TYPE_ARRAY ||
4058 QCBORDecode_GetNthTag(&DCtx, &Item, 3) != 10489608748473423768ULL ||
4059 QCBORDecode_GetNthTag(&DCtx, &Item, 2) != 2442302356ULL ||
4060 QCBORDecode_GetNthTag(&DCtx, &Item, 1) != 21590ULL ||
4061 QCBORDecode_GetNthTag(&DCtx, &Item, 0) != 240ULL) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004062 return -7;
Laurence Lundblade9b334962020-08-27 10:55:53 -07004063 }
4064
4065 /* tag 21590(
4066 10489608748473423768(
4067 2442302357(
4068 21591(
4069 []))))
4070 */
4071 uError = QCBORDecode_GetNext(&DCtx, &Item);
4072 if(uError != QCBOR_SUCCESS ||
4073 Item.uDataType != QCBOR_TYPE_ARRAY ||
4074 QCBORDecode_GetNthTag(&DCtx, &Item, 0) != 65534ULL ||
4075 QCBORDecode_GetNthTag(&DCtx, &Item, 1) != 2442302357ULL ||
4076 QCBORDecode_GetNthTag(&DCtx, &Item, 2) != 10489608748473423768ULL ||
4077 QCBORDecode_GetNthTag(&DCtx, &Item, 3) != 21590ULL) {
4078 return -8;
4079 }
4080
4081 /* Make sure to blow past the limit of tags that must be mapped.
4082 works in conjuntion with entries above.
4083 269488144(269488145(269488146(269488147([]))))
4084 */
4085 uError = QCBORDecode_GetNext(&DCtx, &Item);
4086 if(uError != QCBOR_ERR_TOO_MANY_TAGS) {
4087 return -9;
4088 }
4089
4090 uError = QCBORDecode_GetNext(&DCtx, &Item);
4091 if(uError == QCBOR_SUCCESS) {
4092 return -10;
4093 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004094
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004095 // ----------------------------------
Laurence Lundbladeee851742020-01-08 08:37:05 -08004096 // This test sets up a caller-config list that includes the very large
Laurence Lundblade9b334962020-08-27 10:55:53 -07004097 // tage and then matches it. Caller-config lists are no longer
4098 // used or needed. This tests backwards compatibility with them.
Laurence Lundbladeee851742020-01-08 08:37:05 -08004099 QCBORDecode_Init(&DCtx,
4100 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEncodedLargeTag),
4101 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004102 const uint64_t puList[] = {0x9192939495969798, 257};
4103 const QCBORTagListIn TL = {2, puList};
4104 QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004105
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004106 if(QCBORDecode_GetNext(&DCtx, &Item)) {
4107 return -8;
4108 }
4109 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
4110 !QCBORDecode_IsTagged(&DCtx, &Item, 0x9192939495969798) ||
4111 QCBORDecode_IsTagged(&DCtx, &Item, 257) ||
4112 QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_BIGFLOAT) ||
4113 Item.val.uCount != 0) {
4114 return -9;
4115 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004116
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004117 //------------------------
Laurence Lundbladeee851742020-01-08 08:37:05 -08004118 // Sets up a caller-configured list and look up something not in it
Laurence Lundblade9b334962020-08-27 10:55:53 -07004119 // Another backwards compatibility test.
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004120 const uint64_t puLongList[17] = {1,2,1};
4121 const QCBORTagListIn TLLong = {17, puLongList};
Laurence Lundbladeee851742020-01-08 08:37:05 -08004122 QCBORDecode_Init(&DCtx,
4123 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEncodedLargeTag),
4124 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004125 QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TLLong);
4126 if(QCBORDecode_GetNext(&DCtx, &Item)) {
4127 return -11;
4128 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004129
Laurence Lundbladef7a70bc2020-10-24 12:23:25 -07004130 uint64_t puTags[4];
Laurence Lundblade9b334962020-08-27 10:55:53 -07004131 QCBORTagListOut Out = {0, 4, puTags};
4132
4133
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004134 // This tests retrievel of the full tag list
Laurence Lundbladeee851742020-01-08 08:37:05 -08004135 QCBORDecode_Init(&DCtx,
4136 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spLotsOfTags),
4137 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004138 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
4139 return -12;
4140 }
4141 if(puTags[0] != 0x9192939495969798 ||
4142 puTags[1] != 0x88 ||
Laurence Lundblade59289e52019-12-30 13:44:37 -08004143 puTags[2] != 0x06 ||
4144 puTags[3] != 0x07) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004145 return -13;
4146 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004147
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004148 // ----------------------
Laurence Lundblade9b334962020-08-27 10:55:53 -07004149 // This tests too small of an out list
Laurence Lundbladeee851742020-01-08 08:37:05 -08004150 QCBORDecode_Init(&DCtx,
4151 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spLotsOfTags),
4152 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004153 QCBORTagListOut OutSmall = {0, 3, puTags};
4154 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &OutSmall) != QCBOR_ERR_TOO_MANY_TAGS) {
4155 return -14;
4156 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004157
Laurence Lundblade9b334962020-08-27 10:55:53 -07004158
4159
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004160 // ---------------
Laurence Lundblade9b334962020-08-27 10:55:53 -07004161 // Decode a version of the "CSR" that has had a ton of tags randomly inserted
4162 // It is a bit of a messy test and maybe could be improved, but
4163 // it is retained as a backwards compatibility check.
Laurence Lundbladeee851742020-01-08 08:37:05 -08004164 QCBORDecode_Init(&DCtx,
4165 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRWithTags),
4166 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004167 int n = CheckCSRMaps(&DCtx);
4168 if(n) {
4169 return n-2000;
4170 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004171
Laurence Lundblade59289e52019-12-30 13:44:37 -08004172 Out = (QCBORTagListOut){0, 16, puTags};
Laurence Lundbladeee851742020-01-08 08:37:05 -08004173 QCBORDecode_Init(&DCtx,
4174 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRWithTags),
4175 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004176
Laurence Lundblade9b334962020-08-27 10:55:53 -07004177 /* With the spiffy decode revision, this tag list is not used.
4178 It doesn't matter if a tag is in this list or not so some
4179 tests that couldn't process a tag because it isn't in this list
4180 now can process these unlisted tags. The tests have been
4181 adjusted for this. */
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004182 const uint64_t puTagList[] = {773, 1, 90599561};
4183 const QCBORTagListIn TagList = {3, puTagList};
4184 QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TagList);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004185
4186
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004187 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
4188 return -100;
4189 }
4190 if(Item.uDataType != QCBOR_TYPE_MAP ||
4191 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC) ||
4192 QCBORDecode_IsTagged(&DCtx, &Item, 90599561) ||
4193 QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_DATE_EPOCH) ||
4194 Item.val.uCount != 2 ||
4195 puTags[0] != CBOR_TAG_CBOR_MAGIC ||
4196 puTags[1] != CBOR_TAG_CBOR_MAGIC ||
4197 puTags[2] != CBOR_TAG_CBOR_MAGIC ||
4198 Out.uNumUsed != 3) {
4199 return -101;
4200 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004201
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004202 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
4203 return -102;
4204 }
4205 if(Item.uDataType != QCBOR_TYPE_MAP ||
4206 QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC) ||
4207 QCBORDecode_IsTagged(&DCtx, &Item, 6) ||
Laurence Lundbladef7a70bc2020-10-24 12:23:25 -07004208 !QCBORDecode_IsTagged(&DCtx, &Item, 7) ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004209 Item.val.uCount != 2 ||
4210 puTags[0] != 5859837686836516696 ||
4211 puTags[1] != 7 ||
4212 Out.uNumUsed != 2) {
4213 return -103;
4214 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004215
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004216 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
4217 return -104;
4218 }
4219 if(Item.uDataType != QCBOR_TYPE_MAP ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004220 Item.val.uCount != 5 ||
4221 puTags[0] != 0x0b ||
4222 Out.uNumUsed != 1) {
4223 return -105;
4224 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004225
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004226 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
4227 return -106;
4228 }
4229 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
4230 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_COSE_MAC0) ||
4231 Item.val.string.len != 12 ||
4232 puTags[0] != CBOR_TAG_COSE_MAC0 ||
4233 puTags[1] != CBOR_TAG_COSE_MAC0 ||
4234 puTags[2] != CBOR_TAG_COSE_MAC0 ||
4235 Out.uNumUsed != 3) {
4236 return -105;
4237 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004238
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004239 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
4240 return -107;
4241 }
4242 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
4243 !QCBORDecode_IsTagged(&DCtx, &Item, 773) ||
4244 Item.val.string.len != 3 ||
4245 puTags[0] != 773 ||
4246 Out.uNumUsed != 1) {
4247 return -108;
4248 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004249
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004250 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
4251 return -109;
4252 }
4253 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade59289e52019-12-30 13:44:37 -08004254 !QCBORDecode_IsTagged(&DCtx, &Item, 16) ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004255 Item.val.string.len != 9 ||
Laurence Lundblade59289e52019-12-30 13:44:37 -08004256 puTags[0] != 16 ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07004257 puTags[3] != 7 ||
4258 Out.uNumUsed != 4) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004259 return -110;
4260 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004261
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004262 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
4263 return -111;
4264 }
4265 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
4266 !QCBORDecode_IsTagged(&DCtx, &Item, 17) ||
4267 Item.val.string.len != 9 ||
4268 puTags[0] != 17 ||
4269 Out.uNumUsed != 1) {
4270 return -112;
4271 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004272
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004273 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
4274 return -111;
4275 }
4276 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
4277 !QCBORDecode_IsTagged(&DCtx, &Item, 17) ||
4278 Item.val.string.len != 2 ||
4279 puTags[0] != 17 ||
4280 Out.uNumUsed != 1) {
4281 return -112;
4282 }
4283
4284 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
4285 return -113;
4286 }
4287 if(Item.uDataType != QCBOR_TYPE_MAP ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07004288 !QCBORDecode_IsTagged(&DCtx, &Item, 19) ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004289 Item.val.uCount != 2 ||
4290 puTags[0] != 19 ||
4291 Out.uNumUsed != 1) {
4292 return -114;
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 -115;
4297 }
4298 if(Item.uDataType != QCBOR_TYPE_MAP ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07004299 !QCBORDecode_IsTagged(&DCtx, &Item, 9) ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004300 Item.val.uCount != 1 ||
4301 puTags[0] != 9 ||
4302 Out.uNumUsed != 1) {
4303 return -116;
4304 }
4305
4306 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
4307 return -116;
4308 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004309 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004310 Item.val.int64 != -7 ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004311 Out.uNumUsed != 0) {
4312 return -117;
4313 }
4314
4315 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
4316 return -118;
4317 }
4318 if(Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
4319 Item.val.string.len != 10 ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004320 puTags[0] != 12 ||
4321 Out.uNumUsed != 1) {
4322 return -119;
4323 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004324
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004325 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
4326 return -120;
4327 }
4328 if(Item.uDataType != QCBOR_TYPE_MAP ||
4329 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_ENC_AS_B16) ||
4330 Item.val.uCount != 1 ||
4331 puTags[0] != 0x17 ||
4332 Out.uNumUsed != 1) {
4333 return -121;
4334 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004335
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004336 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
4337 return -122;
4338 }
4339 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07004340 !QCBORDecode_IsTagged(&DCtx, &Item, 8) ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004341 Item.val.int64 != -3 ||
4342 puTags[0] != 8 ||
4343 Out.uNumUsed != 1) {
4344 return -123;
4345 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004346
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004347 if(QCBORDecode_Finish(&DCtx)) {
4348 return -124;
4349 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07004350
4351 UsefulBufC DateString;
4352 QCBORDecode_Init(&DCtx,
4353 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSpiffyTagInput),
4354 QCBOR_DECODE_MODE_NORMAL);
4355
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07004356 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade9b334962020-08-27 10:55:53 -07004357 // tagged date string
4358 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString);
4359 // untagged date string
4360 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString);
4361 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_SUCCESS) {
4362 return 100;
4363 }
4364 // untagged byte string
4365 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString);
4366 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
4367 return 101;
4368 }
4369 // tagged regex
4370 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString);
4371 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
4372 return 102;
4373 }
4374 // tagged date string with a byte string
4375 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString);
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07004376 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT) {
Laurence Lundblade9b334962020-08-27 10:55:53 -07004377 return 103;
4378 }
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07004379 // The exit errors out because the last item, the date string with
4380 // bad content makes the array untraversable (the bad date string
4381 // could have tag content of an array or such that is not consumed
4382 // by the date decoding).
Laurence Lundblade9b334962020-08-27 10:55:53 -07004383 QCBORDecode_ExitArray(&DCtx);
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07004384 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT) {
Laurence Lundblade9b334962020-08-27 10:55:53 -07004385 return 104;
4386 }
4387
4388
4389 QCBORDecode_Init(&DCtx,
4390 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSpiffyTagInput),
4391 QCBOR_DECODE_MODE_NORMAL);
4392
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07004393 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade9b334962020-08-27 10:55:53 -07004394 // tagged date string
4395 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString);
4396 // untagged date string
4397 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString);
4398 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_SUCCESS) {
4399 return 200;
4400 }
4401 // untagged byte string
4402 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString);
4403 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
4404 return 201;
4405 }
4406 // tagged regex
4407 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString);
4408 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
4409 return 202;
4410 }
4411 // tagged date string with a byte string
4412 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString);
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07004413 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT) {
Laurence Lundblade9b334962020-08-27 10:55:53 -07004414 return 203;
4415 }
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07004416 // See comments above
Laurence Lundblade9b334962020-08-27 10:55:53 -07004417 QCBORDecode_ExitArray(&DCtx);
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07004418 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT) {
Laurence Lundblade9b334962020-08-27 10:55:53 -07004419 return 204;
4420 }
4421
4422 QCBORDecode_Init(&DCtx,
4423 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSpiffyTagInput),
4424 QCBOR_DECODE_MODE_NORMAL);
4425
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07004426 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade9b334962020-08-27 10:55:53 -07004427 // tagged date string
4428 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString);
4429 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
4430 return 300;
4431 }
4432 // untagged date string
4433 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString);
4434 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
4435 return 301;
4436 }
4437 // untagged byte string
4438 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString);
4439 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
4440 return 302;
4441 }
4442 // tagged regex
4443 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString);
4444 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
4445 return 303;
4446 }
4447 // tagged date string with a byte string
4448 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString);
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07004449 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT) {
Laurence Lundblade9b334962020-08-27 10:55:53 -07004450 return 304;
4451 }
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07004452 // See comments above
Laurence Lundblade9b334962020-08-27 10:55:53 -07004453 QCBORDecode_ExitArray(&DCtx);
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07004454 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT) {
Laurence Lundblade9b334962020-08-27 10:55:53 -07004455 return 305;
4456 }
4457
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004458 return 0;
4459}
4460
Laurence Lundblade37286c02022-09-03 10:05:02 -07004461/*
4462 * These are showing the big numbers converted to integers.
4463 * The tag numbers are not shown.
4464 *
Laurence Lundbladeec290b82024-06-10 11:10:54 -07004465 * [
4466 * 18446744073709551616,
4467 * -18446744073709551617,
4468 * {
4469 * -64: -18446744073709551617,
4470 * 64: 18446744073709551616,
4471 * "BN+": 18446744073709551616,
4472 * "BN-": -18446744073709551617
Laurence Lundblade37286c02022-09-03 10:05:02 -07004473 * }
4474 * ]
4475 */
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004476
Laurence Lundbladecc7da412020-12-27 00:09:07 -08004477static const uint8_t spBigNumInput[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004478 0x83,
4479 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
4480 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
4481 0xA4,
Laurence Lundbladeec290b82024-06-10 11:10:54 -07004482 0x38, 0x3F,
4483 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004484 0x18, 0x40,
4485 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
Laurence Lundbladeec290b82024-06-10 11:10:54 -07004486 0x63, 0x42, 0x4E, 0x2B,
4487 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
4488 0x63, 0x42, 0x4E, 0x2D,
4489 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
4490};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004491
Laurence Lundblade37286c02022-09-03 10:05:02 -07004492#ifndef QCBOR_DISABLE_TAGS
Laurence Lundbladecc7da412020-12-27 00:09:07 -08004493/* The expected big num */
4494static const uint8_t spBigNum[] = {
4495 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
4496 0x00};
Laurence Lundblade37286c02022-09-03 10:05:02 -07004497#endif /* QCBOR_DISABLE_TAGS */
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004498
4499
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03004500int32_t BignumParseTest(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004501{
4502 QCBORDecodeContext DCtx;
4503 QCBORItem Item;
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07004504 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004505
Laurence Lundbladeee851742020-01-08 08:37:05 -08004506 QCBORDecode_Init(&DCtx,
4507 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNumInput),
4508 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004509
4510
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004511 //
4512 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
4513 return -1;
4514 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade830fbf92020-05-31 17:22:33 -07004515 return -2;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004516 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004517
Laurence Lundblade37286c02022-09-03 10:05:02 -07004518#ifndef QCBOR_DISABLE_TAGS
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004519 //
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004520 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07004521 return -3;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004522 if(Item.uDataType != QCBOR_TYPE_POSBIGNUM ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07004523 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07004524 return -4;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004525 }
4526
4527 //
4528 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07004529 return -5;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004530 if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07004531 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07004532 return -6;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004533 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004534
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004535 //
4536 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07004537 return -7;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004538 if(Item.uDataType != QCBOR_TYPE_MAP) {
Laurence Lundblade830fbf92020-05-31 17:22:33 -07004539 return -8;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004540 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004541
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004542 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundbladeec290b82024-06-10 11:10:54 -07004543 return -15;
4544 if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM ||
4545 Item.uLabelType != QCBOR_TYPE_INT64 ||
4546 Item.label.int64 != -64 ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07004547 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundbladeec290b82024-06-10 11:10:54 -07004548 return -16;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004549 }
4550
4551 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07004552 return -11;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004553 if(Item.uDataType != QCBOR_TYPE_POSBIGNUM ||
4554 Item.uLabelType != QCBOR_TYPE_INT64 ||
4555 Item.label.int64 != 64 ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07004556 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07004557 return -12;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004558 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004559
Laurence Lundbladeec290b82024-06-10 11:10:54 -07004560#ifndef QCBOR_DISABLE_NON_INTEGER_LABELS
4561 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
4562 return -9;
4563 if(Item.uDataType != QCBOR_TYPE_POSBIGNUM ||
4564 Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
4565 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
4566 return -10;
4567 }
4568
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004569 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07004570 return -13;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004571 if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM ||
4572 Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07004573 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07004574 return -14;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004575 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004576
Laurence Lundbladeec290b82024-06-10 11:10:54 -07004577
4578#endif /* ! QCBOR_DISABLE_NON_INTEGER_LABELS */
4579
Laurence Lundblade37286c02022-09-03 10:05:02 -07004580#else
4581
4582 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_TAGS_DISABLED) {
4583 return -100;
4584 }
4585#endif /* QCBOR_DISABLE_TAGS */
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004586
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004587 return 0;
4588}
4589
4590
Laurence Lundbladec5fef682020-01-25 11:38:45 -08004591static int32_t CheckItemWithIntLabel(QCBORDecodeContext *pCtx,
Laurence Lundbladeee851742020-01-08 08:37:05 -08004592 uint8_t uDataType,
4593 uint8_t uNestingLevel,
4594 uint8_t uNextNest,
4595 int64_t nLabel,
4596 QCBORItem *pItem)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004597{
4598 QCBORItem Item;
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07004599 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004600
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004601 if((nCBORError = QCBORDecode_GetNext(pCtx, &Item))) return -1;
4602 if(Item.uDataType != uDataType) return -1;
4603 if(uNestingLevel > 0) {
Laurence Lundbladeec290b82024-06-10 11:10:54 -07004604 if(Item.uLabelType != QCBOR_TYPE_INT64) {
Laurence Lundbladeee851742020-01-08 08:37:05 -08004605 return -1;
4606 }
Laurence Lundbladeec290b82024-06-10 11:10:54 -07004607 if(Item.label.int64 != nLabel) {
4608 return -1;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004609 }
Laurence Lundbladeec290b82024-06-10 11:10:54 -07004610
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004611 }
4612 if(Item.uNestingLevel != uNestingLevel) return -1;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304613 if(Item.uNextNestLevel != uNextNest) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004614
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004615 if(pItem) {
4616 *pItem = Item;
4617 }
4618 return 0;
4619}
4620
Laurence Lundblade742df4a2018-10-13 20:07:17 +08004621// Same code checks definite and indefinite length versions of the map
Laurence Lundbladec5fef682020-01-25 11:38:45 -08004622static int32_t CheckCSRMaps(QCBORDecodeContext *pDC)
Laurence Lundblade742df4a2018-10-13 20:07:17 +08004623{
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304624 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 0, 1, 0, NULL)) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004625
Laurence Lundblade9b334962020-08-27 10:55:53 -07004626 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 1, 2, -23, NULL)) return -2;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004627
Laurence Lundblade9b334962020-08-27 10:55:53 -07004628 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 2, 3, -20, NULL)) return -3;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004629
Laurence Lundblade9b334962020-08-27 10:55:53 -07004630 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -18, NULL)) return -4;
4631 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -17, NULL)) return -5;
4632 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -15, NULL)) return -6;
4633 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -16, NULL)) return -7;
4634 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 2, -14, NULL)) return -8;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004635
Laurence Lundblade9b334962020-08-27 10:55:53 -07004636 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 2, 3, -19, NULL)) return -9;
4637 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 3, 4, -11, NULL)) return -10;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004638
Laurence Lundblade9b334962020-08-27 10:55:53 -07004639 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_INT64, 4, 3, -9, NULL)) return -11;
4640 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_BYTE_STRING, 3, 1, -10, NULL)) return -12;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004641
Laurence Lundblade9b334962020-08-27 10:55:53 -07004642 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 1, 2, -22, NULL)) return -13;
4643 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_INT64, 2, 0, -5, NULL)) return -14;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004644
Laurence Lundblade9b334962020-08-27 10:55:53 -07004645 if(QCBORDecode_Finish(pDC)) return -20;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004646
Laurence Lundblade742df4a2018-10-13 20:07:17 +08004647 return 0;
4648}
4649
4650
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004651/*
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004652{
4653 -23: {
4654 -20: {
4655 -18: "Organization",
4656 -17: "SSG",
4657 -15: "Confusion",
4658 -16: "San Diego",
4659 -14: "US"
4660 },
4661 -19: {
4662 -11: {
4663 -9: -7
4664 },
4665 -10: '\u0001\u0002\u0003\u0004\u0005\u0006\a\b\t\n'
4666 }
4667 },
4668 -22: {
4669 -5: -3
4670 }
4671}
Laurence Lundbladecc7da412020-12-27 00:09:07 -08004672*/
4673static const uint8_t spCSRInput[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004674 0xa2, 0x36, 0xa2, 0x33, 0xa5, 0x31, 0x6c, 0x4f,
4675 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74,
4676 0x69, 0x6f, 0x6e, 0x30, 0x63, 0x53, 0x53, 0x47,
4677 0x2e, 0x69, 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73,
4678 0x69, 0x6f, 0x6e, 0x2f, 0x69, 0x53, 0x61, 0x6e,
4679 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f, 0x2d, 0x62,
4680 0x55, 0x53, 0x32, 0xa2, 0x2a, 0xa1, 0x28, 0x26,
4681 0x29, 0x4a, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
4682 0x07, 0x08, 0x09, 0x0a, 0x35, 0xa1, 0x24, 0x22};
4683
Laurence Lundbladecc7da412020-12-27 00:09:07 -08004684// Same map as above, but using indefinite lengths
4685static const uint8_t spCSRInputIndefLen[] = {
4686 0xbf, 0x36, 0xbf, 0x33, 0xbf, 0x31, 0x6c, 0x4f,
4687 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74,
4688 0x69, 0x6f, 0x6e, 0x30, 0x63, 0x53, 0x53, 0x47,
4689 0x2e, 0x69, 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73,
4690 0x69, 0x6f, 0x6e, 0x2f, 0x69, 0x53, 0x61, 0x6e,
4691 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f, 0x2d, 0x62,
4692 0x55, 0x53, 0xff, 0x32, 0xbf, 0x2a, 0xbf, 0x28,
4693 0x26, 0xff, 0x29, 0x4a, 0x01, 0x02, 0x03, 0x04,
4694 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0xff, 0xff,
4695 0x35, 0xbf, 0x24, 0x22, 0xff, 0xff};
4696
4697
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03004698int32_t NestedMapTest(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004699{
4700 QCBORDecodeContext DCtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004701
Laurence Lundbladeee851742020-01-08 08:37:05 -08004702 QCBORDecode_Init(&DCtx,
4703 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput),
4704 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004705
Laurence Lundblade742df4a2018-10-13 20:07:17 +08004706 return CheckCSRMaps(&DCtx);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004707}
4708
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08004709
4710
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03004711int32_t StringDecoderModeFailTest(void)
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08004712{
4713 QCBORDecodeContext DCtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004714
Laurence Lundbladeee851742020-01-08 08:37:05 -08004715 QCBORDecode_Init(&DCtx,
4716 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput),
4717 QCBOR_DECODE_MODE_MAP_STRINGS_ONLY);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004718
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08004719 QCBORItem Item;
4720 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004721
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08004722 if(QCBORDecode_GetNext(&DCtx, &Item)) {
4723 return -1;
4724 }
4725 if(Item.uDataType != QCBOR_TYPE_MAP) {
4726 return -2;
4727 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004728
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08004729 nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
4730 if(nCBORError != QCBOR_ERR_MAP_LABEL_TYPE) {
4731 return -3;
4732 }
4733
4734 return 0;
4735}
4736
4737
Laurence Lundblade742df4a2018-10-13 20:07:17 +08004738
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03004739int32_t NestedMapTestIndefLen(void)
Laurence Lundblade742df4a2018-10-13 20:07:17 +08004740{
4741 QCBORDecodeContext DCtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004742
Laurence Lundbladeee851742020-01-08 08:37:05 -08004743 QCBORDecode_Init(&DCtx,
4744 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInputIndefLen),
4745 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004746
Laurence Lundblade742df4a2018-10-13 20:07:17 +08004747 return CheckCSRMaps(&DCtx);
4748}
4749
4750
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004751
Laurence Lundblade17ede402018-10-13 11:43:07 +08004752static UsefulBufC make_nested_indefinite_arrays(int n, UsefulBuf Storage)
4753{
4754 UsefulOutBuf UOB;
4755 UsefulOutBuf_Init(&UOB, Storage);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004756
Laurence Lundblade17ede402018-10-13 11:43:07 +08004757 int i;
4758 for(i = 0; i < n; i++) {
4759 UsefulOutBuf_AppendByte(&UOB, 0x9f);
4760 }
4761
4762 for(i = 0; i < n; i++) {
4763 UsefulOutBuf_AppendByte(&UOB, 0xff);
4764 }
4765 return UsefulOutBuf_OutUBuf(&UOB);
4766}
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08004767
4768
Laurence Lundbladec5fef682020-01-25 11:38:45 -08004769static int32_t parse_indeflen_nested(UsefulBufC Nested, int nNestLevel)
Laurence Lundblade17ede402018-10-13 11:43:07 +08004770{
4771 QCBORDecodeContext DC;
4772 QCBORDecode_Init(&DC, Nested, 0);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004773
Laurence Lundblade17ede402018-10-13 11:43:07 +08004774 int j;
4775 for(j = 0; j < nNestLevel; j++) {
4776 QCBORItem Item;
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07004777 QCBORError nReturn = QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade17ede402018-10-13 11:43:07 +08004778 if(j >= QCBOR_MAX_ARRAY_NESTING) {
4779 // Should be in error
Laurence Lundbladea9489f82020-09-12 13:50:56 -07004780 if(nReturn != QCBOR_ERR_ARRAY_DECODE_NESTING_TOO_DEEP) {
Laurence Lundblade17ede402018-10-13 11:43:07 +08004781 return -4;
4782 } else {
4783 return 0; // Decoding doesn't recover after an error
4784 }
4785 } else {
4786 // Should be no error
4787 if(nReturn) {
4788 return -9; // Should not have got an error
4789 }
4790 }
4791 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
4792 return -7;
4793 }
4794 }
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07004795 QCBORError nReturn = QCBORDecode_Finish(&DC);
Laurence Lundblade17ede402018-10-13 11:43:07 +08004796 if(nReturn) {
4797 return -3;
4798 }
4799 return 0;
4800}
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08004801
4802
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03004803int32_t IndefiniteLengthNestTest(void)
Laurence Lundblade17ede402018-10-13 11:43:07 +08004804{
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05304805 UsefulBuf_MAKE_STACK_UB(Storage, 50);
Laurence Lundblade17ede402018-10-13 11:43:07 +08004806 int i;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004807 for(i=1; i < QCBOR_MAX_ARRAY_NESTING+4; i++) {
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08004808 const UsefulBufC Nested = make_nested_indefinite_arrays(i, Storage);
Laurence Lundblade17ede402018-10-13 11:43:07 +08004809 int nReturn = parse_indeflen_nested(Nested, i);
4810 if(nReturn) {
4811 return nReturn;
4812 }
4813 }
4814 return 0;
4815}
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08004816
Laurence Lundbladeee851742020-01-08 08:37:05 -08004817// [1, [2, 3]]
4818static const uint8_t spIndefiniteArray[] = {0x9f, 0x01, 0x82, 0x02, 0x03, 0xff};
4819// No closing break
4820static const uint8_t spIndefiniteArrayBad1[] = {0x9f};
4821// Not enough closing breaks
4822static const uint8_t spIndefiniteArrayBad2[] = {0x9f, 0x9f, 0x02, 0xff};
4823// Too many closing breaks
4824static const uint8_t spIndefiniteArrayBad3[] = {0x9f, 0x02, 0xff, 0xff};
4825// Unclosed indeflen inside def len
4826static const uint8_t spIndefiniteArrayBad4[] = {0x81, 0x9f};
4827// confused tag
4828static const uint8_t spIndefiniteArrayBad5[] = {0x9f, 0xd1, 0xff};
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08004829
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03004830int32_t IndefiniteLengthArrayMapTest(void)
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08004831{
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07004832 QCBORError nResult;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08004833 // --- first test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07004834 UsefulBufC IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArray);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004835
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08004836 // Decode it and see if it is OK
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08004837 QCBORDecodeContext DC;
4838 QCBORItem Item;
4839 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004840
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08004841 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05304842
4843 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
4844 Item.uNestingLevel != 0 ||
4845 Item.uNextNestLevel != 1) {
4846 return -111;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08004847 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004848
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08004849 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05304850 if(Item.uDataType != QCBOR_TYPE_INT64 ||
4851 Item.uNestingLevel != 1 ||
4852 Item.uNextNestLevel != 1) {
4853 return -2;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08004854 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004855
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08004856 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05304857 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
4858 Item.uNestingLevel != 1 ||
4859 Item.uNextNestLevel != 2) {
4860 return -3;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08004861 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004862
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08004863 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade12b495d2018-12-17 11:15:54 -08004864 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade6de37062018-10-15 12:22:42 +05304865 Item.uNestingLevel != 2 ||
4866 Item.uNextNestLevel != 2) {
4867 return -4;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08004868 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004869
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08004870 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade12b495d2018-12-17 11:15:54 -08004871 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade6de37062018-10-15 12:22:42 +05304872 Item.uNestingLevel != 2 ||
4873 Item.uNextNestLevel != 0) {
4874 return -5;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08004875 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004876
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08004877 if(QCBORDecode_Finish(&DC)) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05304878 return -6;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08004879 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004880
Laurence Lundblade19e0c802018-10-13 12:19:55 +08004881 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07004882 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad1);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004883
Laurence Lundblade19e0c802018-10-13 12:19:55 +08004884 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004885
Laurence Lundblade19e0c802018-10-13 12:19:55 +08004886 nResult = QCBORDecode_GetNext(&DC, &Item);
4887 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05304888 return -7;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08004889 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004890
Laurence Lundblade570fab52018-10-13 18:28:27 +08004891 nResult = QCBORDecode_Finish(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07004892 if(nResult != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05304893 return -8;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08004894 }
4895
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004896
Laurence Lundblade19e0c802018-10-13 12:19:55 +08004897 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07004898 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad2);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004899
Laurence Lundblade19e0c802018-10-13 12:19:55 +08004900 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004901
Laurence Lundblade19e0c802018-10-13 12:19:55 +08004902 nResult = QCBORDecode_GetNext(&DC, &Item);
4903 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05304904 return -9;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08004905 }
4906
4907 nResult = QCBORDecode_GetNext(&DC, &Item);
4908 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05304909 return -10;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08004910 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004911
Laurence Lundblade19e0c802018-10-13 12:19:55 +08004912 nResult = QCBORDecode_GetNext(&DC, &Item);
4913 if(nResult || Item.uDataType != QCBOR_TYPE_INT64) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05304914 return -11;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08004915 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004916
Laurence Lundblade19e0c802018-10-13 12:19:55 +08004917 nResult = QCBORDecode_Finish(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07004918 if(nResult != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05304919 return -12;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08004920 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004921
4922
Laurence Lundblade19e0c802018-10-13 12:19:55 +08004923 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07004924 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad3);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004925
Laurence Lundblade19e0c802018-10-13 12:19:55 +08004926 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004927
Laurence Lundblade19e0c802018-10-13 12:19:55 +08004928 nResult = QCBORDecode_GetNext(&DC, &Item);
4929 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05304930 return -13;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08004931 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004932
Laurence Lundblade19e0c802018-10-13 12:19:55 +08004933 nResult = QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade642282a2020-06-23 12:00:33 -07004934 if(nResult != QCBOR_SUCCESS) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05304935 return -14;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08004936 }
Laurence Lundblade6de37062018-10-15 12:22:42 +05304937
Laurence Lundblade642282a2020-06-23 12:00:33 -07004938 nResult = QCBORDecode_GetNext(&DC, &Item);
4939 if(nResult != QCBOR_ERR_BAD_BREAK) {
4940 return -140;
4941 }
4942
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004943
Laurence Lundblade570fab52018-10-13 18:28:27 +08004944 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07004945 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad4);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004946
Laurence Lundblade570fab52018-10-13 18:28:27 +08004947 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004948
Laurence Lundblade570fab52018-10-13 18:28:27 +08004949 nResult = QCBORDecode_GetNext(&DC, &Item);
4950 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05304951 return -15;
Laurence Lundblade570fab52018-10-13 18:28:27 +08004952 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004953
Laurence Lundblade570fab52018-10-13 18:28:27 +08004954 nResult = QCBORDecode_GetNext(&DC, &Item);
4955 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05304956 return -16;
Laurence Lundblade570fab52018-10-13 18:28:27 +08004957 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004958
Laurence Lundblade570fab52018-10-13 18:28:27 +08004959 nResult = QCBORDecode_Finish(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07004960 if(nResult != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05304961 return -17;
Laurence Lundblade570fab52018-10-13 18:28:27 +08004962 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004963
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304964 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07004965 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad5);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004966
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304967 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004968
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304969 nResult = QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade37286c02022-09-03 10:05:02 -07004970
4971#ifndef QCBOR_DISABLE_TAGS
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304972 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05304973 return -18;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304974 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004975
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304976 nResult = QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05304977 if(nResult != QCBOR_ERR_BAD_BREAK) {
4978 return -19;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304979 }
Laurence Lundblade37286c02022-09-03 10:05:02 -07004980#else /* QCBOR_DISABLE_TAGS */
4981 if(nResult != QCBOR_ERR_TAGS_DISABLED) {
4982 return -20;
4983 }
4984#endif /* QCBOR_DISABLE_TAGS */
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004985
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08004986 return 0;
4987}
4988
Laurence Lundblade17ede402018-10-13 11:43:07 +08004989
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08004990#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
4991
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07004992static const uint8_t spIndefiniteLenString[] = {
Laurence Lundblade17ede402018-10-13 11:43:07 +08004993 0x81, // Array of length one
4994 0x7f, // text string marked with indefinite length
4995 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment
4996 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment
4997 0xff // ending break
4998};
4999
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07005000static const uint8_t spIndefiniteLenStringBad2[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305001 0x81, // Array of length one
5002 0x7f, // text string marked with indefinite length
5003 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment
5004 0x44, 0x6d, 0x69, 0x6e, 0x67, // second segment of wrong type
5005 0xff // ending break
5006};
5007
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07005008static const uint8_t spIndefiniteLenStringBad3[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305009 0x81, // Array of length one
5010 0x7f, // text string marked with indefinite length
5011 0x01, 0x02, // Not a string
5012 0xff // ending break
5013};
5014
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07005015static const uint8_t spIndefiniteLenStringBad4[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305016 0x81, // Array of length one
5017 0x7f, // text string marked with indefinite length
5018 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment
5019 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment
5020 // missing end of string
5021};
5022
Laurence Lundbladeec290b82024-06-10 11:10:54 -07005023#ifndef QCBOR_DISABLE_NON_INTEGER_LABELS
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07005024static const uint8_t spIndefiniteLenStringLabel[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305025 0xa1, // Array of length one
5026 0x7f, // text string marked with indefinite length
5027 0x65, 0x73, 0x74, 0x72, 0x75, 0x75, // first segment
5028 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment
5029 0xff, // ending break
5030 0x01 // integer being labeled.
5031};
Laurence Lundbladeec290b82024-06-10 11:10:54 -07005032#endif /* ! QCBOR_DISABLE_NON_INTEGER_LABELS */
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305033
Laurence Lundbladec5fef682020-01-25 11:38:45 -08005034/**
5035 Make an indefinite length string
5036
5037 @param Storage Storage for string, must be 144 bytes in size
5038 @return The indefinite length string
5039
5040 This makes an array with one indefinite length string that has 7 chunks
5041 from size of 1 byte up to 64 bytes.
5042 */
5043static UsefulBufC MakeIndefiniteBigBstr(UsefulBuf Storage)
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305044{
5045 UsefulOutBuf UOB;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005046
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305047 UsefulOutBuf_Init(&UOB, Storage);
5048 UsefulOutBuf_AppendByte(&UOB, 0x81);
5049 UsefulOutBuf_AppendByte(&UOB, 0x5f);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005050
Laurence Lundbladec5fef682020-01-25 11:38:45 -08005051 uint8_t uStringByte = 0;
5052 // Use of type int is intentional
5053 for(int uChunkSize = 1; uChunkSize <= 128; uChunkSize *= 2) {
5054 // Not using preferred encoding here, but that is OK.
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305055 UsefulOutBuf_AppendByte(&UOB, 0x58);
Laurence Lundbladec5fef682020-01-25 11:38:45 -08005056 UsefulOutBuf_AppendByte(&UOB, (uint8_t)uChunkSize);
5057 for(int j = 0; j < uChunkSize; j++) {
5058 UsefulOutBuf_AppendByte(&UOB, uStringByte);
5059 uStringByte++;
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305060 }
5061 }
5062 UsefulOutBuf_AppendByte(&UOB, 0xff);
5063
5064 return UsefulOutBuf_OutUBuf(&UOB);
5065}
5066
5067static int CheckBigString(UsefulBufC BigString)
5068{
5069 if(BigString.len != 255) {
5070 return 1;
5071 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005072
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305073 for(uint8_t i = 0; i < 255; i++){
5074 if(((const uint8_t *)BigString.ptr)[i] != i) {
5075 return 1;
5076 }
5077 }
5078 return 0;
5079}
5080
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05305081
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03005082int32_t IndefiniteLengthStringTest(void)
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05305083{
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305084 QCBORDecodeContext DC;
5085 QCBORItem Item;
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05305086 // big enough for MakeIndefiniteBigBstr() + MemPool overhead
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08005087 UsefulBuf_MAKE_STACK_UB(MemPool, 350);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005088
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305089 // --- Simple normal indefinite length string ------
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07005090 UsefulBufC IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenString);
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305091 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005092
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05305093 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305094 return -1;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05305095 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005096
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305097 if(QCBORDecode_GetNext(&DC, &Item)) {
5098 return -2;
5099 }
5100 if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.uDataAlloc) {
5101 return -3;
5102 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005103
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305104 if(QCBORDecode_GetNext(&DC, &Item)) {
5105 return -4;
5106 }
5107 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || !Item.uDataAlloc) {
5108 return -5;
5109 }
5110 if(QCBORDecode_Finish(&DC)) {
5111 return -6;
5112 }
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05305113
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305114 // ----- types mismatch ---
Laurence Lundbladeee851742020-01-08 08:37:05 -08005115 QCBORDecode_Init(&DC,
5116 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad2),
5117 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005118
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305119 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
5120 return -7;
5121 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005122
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305123 if(QCBORDecode_GetNext(&DC, &Item)) {
5124 return -8;
5125 }
5126 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
5127 return -9;
5128 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005129
Laurence Lundblade30816f22018-11-10 13:40:22 +07005130 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_INDEFINITE_STRING_CHUNK) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305131 return -10;
5132 }
5133
5134 // ----- not a string ---
Laurence Lundbladeee851742020-01-08 08:37:05 -08005135 QCBORDecode_Init(&DC,
5136 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad3),
5137 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005138
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305139 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
5140 return -11;
5141 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005142
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305143 if(QCBORDecode_GetNext(&DC, &Item)) {
5144 return -12;
5145 }
5146 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
5147 return -13;
5148 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005149
Laurence Lundblade30816f22018-11-10 13:40:22 +07005150 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_INDEFINITE_STRING_CHUNK) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305151 return -14;
5152 }
5153
5154 // ----- no end -----
Laurence Lundbladeee851742020-01-08 08:37:05 -08005155 QCBORDecode_Init(&DC,
5156 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad4),
5157 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005158
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305159 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
5160 return -15;
5161 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005162
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305163 if(QCBORDecode_GetNext(&DC, &Item)) {
5164 return -16;
5165 }
5166 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
5167 return -17;
5168 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005169
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305170 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_HIT_END) {
5171 return -18;
5172 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005173
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305174 // ------ Don't set a string allocator and see an error -----
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05305175 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005176
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05305177 QCBORDecode_GetNext(&DC, &Item);
5178 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305179 return -19;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05305180 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005181
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05305182 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_NO_STRING_ALLOCATOR) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305183 return -20;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05305184 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005185
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05305186 // ----- Mempool is way too small -----
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08005187 UsefulBuf_MAKE_STACK_UB(MemPoolTooSmall, QCBOR_DECODE_MIN_MEM_POOL_SIZE-1);
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05305188
5189 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
5190 if(!QCBORDecode_SetMemPool(&DC, MemPoolTooSmall, false)) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305191 return -21;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05305192 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005193
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05305194 // ----- Mempool is way too small -----
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05305195 UsefulBuf_MAKE_STACK_UB(BigIndefBStrStorage, 290);
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08005196 const UsefulBufC BigIndefBStr = MakeIndefiniteBigBstr(BigIndefBStrStorage);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005197
Laurence Lundbladeee851742020-01-08 08:37:05 -08005198 // 80 is big enough for MemPool overhead, but not BigIndefBStr
5199 UsefulBuf_MAKE_STACK_UB(MemPoolSmall, 80);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005200
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305201 QCBORDecode_Init(&DC, BigIndefBStr, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05305202 if(QCBORDecode_SetMemPool(&DC, MemPoolSmall, false)) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305203 return -22;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05305204 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005205
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05305206 QCBORDecode_GetNext(&DC, &Item);
5207 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305208 return -23;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05305209 }
Laurence Lundblade30816f22018-11-10 13:40:22 +07005210 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_STRING_ALLOCATE) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305211 return -24;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05305212 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005213
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305214 // ---- big bstr -----
5215 QCBORDecode_Init(&DC, BigIndefBStr, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005216
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305217 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
5218 return -25;
5219 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005220
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05305221 if(QCBORDecode_GetNext(&DC, &Item)) {
5222 return -26;
5223 }
5224 if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.uDataAlloc) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305225 return -26;
5226 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005227
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305228 if(QCBORDecode_GetNext(&DC, &Item)) {
5229 return -27;
5230 }
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05305231 if(Item.uDataType != QCBOR_TYPE_BYTE_STRING || !Item.uDataAlloc || Item.uNestingLevel != 1) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305232 return -28;
5233 }
5234 if(CheckBigString(Item.val.string)) {
5235 return -3;
5236 }
5237 if(QCBORDecode_Finish(&DC)) {
5238 return -29;
5239 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005240
Laurence Lundbladeec290b82024-06-10 11:10:54 -07005241#ifndef QCBOR_DISABLE_NON_INTEGER_LABELS
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305242 // --- label is an indefinite length string ------
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07005243 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringLabel), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005244
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305245 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
5246 return -30;
5247 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005248
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305249 QCBORDecode_GetNext(&DC, &Item);
5250 if(Item.uDataType != QCBOR_TYPE_MAP) {
5251 return -31;
5252 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005253
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305254 if(QCBORDecode_GetNext(&DC, &Item)){
5255 return -32;
5256 }
Laurence Lundbladeee851742020-01-08 08:37:05 -08005257 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
5258 Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305259 Item.uDataAlloc || !Item.uLabelAlloc ||
5260 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("struuming"))) {
5261 return -33;
5262 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005263
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305264 if(QCBORDecode_Finish(&DC)) {
5265 return -34;
5266 }
Laurence Lundbladeec290b82024-06-10 11:10:54 -07005267#endif /* ! QCBOR_DISABLE_NON_INTEGER_LABELS */
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005268
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08005269 return 0;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08005270}
5271
5272
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03005273int32_t AllocAllStringsTest(void)
Laurence Lundbladea44d5062018-10-17 18:45:12 +05305274{
5275 QCBORDecodeContext DC;
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08005276 QCBORError nCBORError;
5277
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005278
Laurence Lundbladea44d5062018-10-17 18:45:12 +05305279 // First test, use the "CSRMap" as easy input and checking
Laurence Lundbladeee851742020-01-08 08:37:05 -08005280 QCBORDecode_Init(&DC,
5281 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput),
5282 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005283
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08005284 UsefulBuf_MAKE_STACK_UB(Pool, sizeof(spCSRInput) + QCBOR_DECODE_MIN_MEM_POOL_SIZE);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005285
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08005286 nCBORError = QCBORDecode_SetMemPool(&DC, Pool, 1); // Turn on copying.
5287 if(nCBORError) {
Laurence Lundbladea44d5062018-10-17 18:45:12 +05305288 return -1;
5289 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005290
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08005291 if(CheckCSRMaps(&DC)) {
5292 return -2;
5293 }
5294
Laurence Lundbladeec290b82024-06-10 11:10:54 -07005295#ifndef QCBOR_DISABLE_NON_INTEGER_LABELS
Laurence Lundblade2f467f92020-10-09 17:50:11 -07005296 // Next parse, save pointers to a few strings, destroy original and
5297 // see all is OK.
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08005298 UsefulBuf_MAKE_STACK_UB(CopyOfStorage, sizeof(pValidMapEncoded) + QCBOR_DECODE_MIN_MEM_POOL_SIZE);
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08005299 const UsefulBufC CopyOf = UsefulBuf_Copy(CopyOfStorage, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded));
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08005300
Laurence Lundbladea44d5062018-10-17 18:45:12 +05305301 QCBORDecode_Init(&DC, CopyOf, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08005302 UsefulBuf_Set(Pool, '/');
Laurence Lundbladea44d5062018-10-17 18:45:12 +05305303 QCBORDecode_SetMemPool(&DC, Pool, 1); // Turn on copying.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005304
Laurence Lundbladea44d5062018-10-17 18:45:12 +05305305 QCBORItem Item1, Item2, Item3, Item4;
5306 if((nCBORError = QCBORDecode_GetNext(&DC, &Item1)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07005307 return (int32_t)nCBORError;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05305308 if(Item1.uDataType != QCBOR_TYPE_MAP ||
5309 Item1.val.uCount != 3)
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08005310 return -3;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05305311 if((nCBORError = QCBORDecode_GetNext(&DC, &Item1)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07005312 return (int32_t)nCBORError;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05305313 if((nCBORError = QCBORDecode_GetNext(&DC, &Item2)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07005314 return (int32_t)nCBORError;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05305315 if((nCBORError = QCBORDecode_GetNext(&DC, &Item3)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07005316 return (int32_t)nCBORError;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05305317 if((nCBORError = QCBORDecode_GetNext(&DC, &Item4)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07005318 return (int32_t)nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005319
Laurence Lundblade05ec57b2018-10-21 01:50:03 +05305320 UsefulBuf_Set(CopyOfStorage, '_');
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005321
Laurence Lundbladea44d5062018-10-17 18:45:12 +05305322 if(Item1.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladea44d5062018-10-17 18:45:12 +05305323 Item1.uDataType != QCBOR_TYPE_INT64 ||
5324 Item1.val.int64 != 42 ||
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08005325 Item1.uDataAlloc != 0 ||
5326 Item1.uLabelAlloc == 0 ||
Laurence Lundbladeec290b82024-06-10 11:10:54 -07005327 UsefulBufCompareToSZ(Item1.label.string, "first integer") ||
5328 Item1.label.string.ptr < Pool.ptr ||
5329 Item1.label.string.ptr > (const void *)((const uint8_t *)Pool.ptr + Pool.len)) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08005330 return -4;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09005331 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005332
Laurence Lundbladea44d5062018-10-17 18:45:12 +05305333
5334 if(Item2.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07005335 UsefulBufCompareToSZ(Item2.label.string, "an array of two strings") ||
Laurence Lundbladea44d5062018-10-17 18:45:12 +05305336 Item2.uDataType != QCBOR_TYPE_ARRAY ||
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08005337 Item2.uDataAlloc != 0 ||
5338 Item2.uLabelAlloc == 0 ||
Laurence Lundbladea44d5062018-10-17 18:45:12 +05305339 Item2.val.uCount != 2)
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08005340 return -5;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005341
Laurence Lundbladea44d5062018-10-17 18:45:12 +05305342 if(Item3.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08005343 Item3.uDataAlloc == 0 ||
5344 Item3.uLabelAlloc != 0 ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07005345 UsefulBufCompareToSZ(Item3.val.string, "string1")) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08005346 return -6;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09005347 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005348
Laurence Lundbladea44d5062018-10-17 18:45:12 +05305349 if(Item4.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08005350 Item4.uDataAlloc == 0 ||
5351 Item4.uLabelAlloc != 0 ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07005352 UsefulBufCompareToSZ(Item4.val.string, "string2")) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08005353 return -7;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09005354 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005355
Laurence Lundbladea44d5062018-10-17 18:45:12 +05305356 // Next parse with a pool that is too small
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08005357 UsefulBuf_MAKE_STACK_UB(SmallPool, QCBOR_DECODE_MIN_MEM_POOL_SIZE + 1);
Laurence Lundbladeee851742020-01-08 08:37:05 -08005358 QCBORDecode_Init(&DC,
5359 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded),
5360 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladea44d5062018-10-17 18:45:12 +05305361 QCBORDecode_SetMemPool(&DC, SmallPool, 1); // Turn on copying.
5362 if((nCBORError = QCBORDecode_GetNext(&DC, &Item1)))
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08005363 return -8;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05305364 if(Item1.uDataType != QCBOR_TYPE_MAP ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09005365 Item1.val.uCount != 3) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08005366 return -9;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09005367 }
Laurence Lundbladea44d5062018-10-17 18:45:12 +05305368 if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item1))){
5369 if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item2))) {
5370 if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item3))) {
5371 nCBORError = QCBORDecode_GetNext(&DC, &Item4);
5372 }
5373 }
5374 }
Laurence Lundblade30816f22018-11-10 13:40:22 +07005375 if(nCBORError != QCBOR_ERR_STRING_ALLOCATE) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08005376 return -10;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05305377 }
Laurence Lundbladeec290b82024-06-10 11:10:54 -07005378#endif /* ! QCBOR_DISABLE_NON_INTEGER_LABELS */
Laurence Lundbladea44d5062018-10-17 18:45:12 +05305379
5380 return 0;
5381}
5382
Laurence Lundbladef6531662018-12-04 10:42:22 +09005383
Laurence Lundbladec5fef682020-01-25 11:38:45 -08005384int32_t MemPoolTest(void)
Laurence Lundblade0155b622018-10-12 20:04:37 +08005385{
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08005386 // Set up the decoder with a tiny bit of CBOR to parse because
5387 // nothing can be done with it unless that is set up.
Laurence Lundbladef6531662018-12-04 10:42:22 +09005388 QCBORDecodeContext DC;
5389 const uint8_t pMinimalCBOR[] = {0xa0}; // One empty map
5390 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pMinimalCBOR),0);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005391
Laurence Lundbladef6531662018-12-04 10:42:22 +09005392 // Set up an memory pool of 100 bytes
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08005393 // Then fish into the internals of the decode context
5394 // to get the allocator function so it can be called directly.
5395 // Also figure out how much pool is available for use
5396 // buy subtracting out the overhead.
Laurence Lundbladef6531662018-12-04 10:42:22 +09005397 UsefulBuf_MAKE_STACK_UB(Pool, 100);
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08005398 QCBORError nError = QCBORDecode_SetMemPool(&DC, Pool, 0);
5399 if(nError) {
5400 return -9;
5401 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08005402 QCBORStringAllocate pAlloc = DC.StringAllocator.pfAllocator;
5403 void *pAllocCtx = DC.StringAllocator.pAllocateCxt;
5404 size_t uAvailPool = Pool.len - QCBOR_DECODE_MIN_MEM_POOL_SIZE;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005405
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08005406 // First test -- ask for one more byte than available and see failure
5407 UsefulBuf Allocated = (*pAlloc)(pAllocCtx, NULL, uAvailPool+1);
Laurence Lundbladef6531662018-12-04 10:42:22 +09005408 if(!UsefulBuf_IsNULL(Allocated)) {
5409 return -1;
5410 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005411
Laurence Lundbladef6531662018-12-04 10:42:22 +09005412 // Re do the set up for the next test that will do a successful alloc,
5413 // a fail, a free and then success
Laurence Lundbladef6531662018-12-04 10:42:22 +09005414 QCBORDecode_SetMemPool(&DC, Pool, 0);
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08005415 pAlloc = DC.StringAllocator.pfAllocator;
5416 pAllocCtx = DC.StringAllocator.pAllocateCxt;
5417 uAvailPool = Pool.len - QCBOR_DECODE_MIN_MEM_POOL_SIZE;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005418
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08005419 // Allocate one byte less than available and see success
5420 Allocated = (pAlloc)(pAllocCtx, NULL, uAvailPool-1);
Laurence Lundbladef6531662018-12-04 10:42:22 +09005421 if(UsefulBuf_IsNULL(Allocated)) { // expected to succeed
5422 return -2;
5423 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08005424 // Ask for some more and see failure
5425 UsefulBuf Allocated2 = (*pAlloc)(pAllocCtx, NULL, uAvailPool/2);
Laurence Lundbladef6531662018-12-04 10:42:22 +09005426 if(!UsefulBuf_IsNULL(Allocated2)) { // expected to fail
5427 return -3;
5428 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08005429 // Free the first allocate, retry the second and see success
5430 (*pAlloc)(pAllocCtx, Allocated.ptr, 0); // Free
5431 Allocated = (*pAlloc)(pAllocCtx, NULL, uAvailPool/2);
Laurence Lundbladef6531662018-12-04 10:42:22 +09005432 if(UsefulBuf_IsNULL(Allocated)) { // succeed because of the free
5433 return -4;
5434 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005435
Laurence Lundbladef6531662018-12-04 10:42:22 +09005436 // Re do set up for next test that involves a successful alloc,
5437 // and a successful realloc and a failed realloc
5438 QCBORDecode_SetMemPool(&DC, Pool, 0);
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08005439 pAlloc = DC.StringAllocator.pfAllocator;
5440 pAllocCtx = DC.StringAllocator.pAllocateCxt;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005441
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08005442 // Allocate half the pool and see success
5443 Allocated = (*pAlloc)(pAllocCtx, NULL, uAvailPool/2);
Laurence Lundbladef6531662018-12-04 10:42:22 +09005444 if(UsefulBuf_IsNULL(Allocated)) { // expected to succeed
5445 return -5;
5446 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08005447 // Reallocate to take up the whole pool and see success
5448 Allocated2 = (*pAlloc)(pAllocCtx, Allocated.ptr, uAvailPool);
Laurence Lundbladef6531662018-12-04 10:42:22 +09005449 if(UsefulBuf_IsNULL(Allocated2)) {
5450 return -6;
5451 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08005452 // Make sure its the same pointer and the size is right
Laurence Lundbladef6531662018-12-04 10:42:22 +09005453 if(Allocated2.ptr != Allocated.ptr || Allocated2.len != uAvailPool) {
5454 return -7;
5455 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08005456 // Try to allocate more to be sure there is failure after a realloc
5457 UsefulBuf Allocated3 = (*pAlloc)(pAllocCtx, Allocated.ptr, uAvailPool+1);
5458 if(!UsefulBuf_IsNULL(Allocated3)) {
Laurence Lundbladef6531662018-12-04 10:42:22 +09005459 return -8;
5460 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005461
Laurence Lundbladef6531662018-12-04 10:42:22 +09005462 return 0;
5463}
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08005464
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08005465
5466/* Just enough of an allocator to test configuration of one */
5467static UsefulBuf AllocateTestFunction(void *pCtx, void *pOldMem, size_t uNewSize)
5468{
5469 (void)pOldMem; // unused variable
5470
5471 if(uNewSize) {
5472 // Assumes the context pointer is the buffer and
5473 // nothing too big will ever be asked for.
5474 // This is only good for this basic test!
5475 return (UsefulBuf) {pCtx, uNewSize};
5476 } else {
5477 return NULLUsefulBuf;
5478 }
5479}
5480
5481
Laurence Lundbladec5fef682020-01-25 11:38:45 -08005482int32_t SetUpAllocatorTest(void)
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08005483{
5484 // Set up the decoder with a tiny bit of CBOR to parse because
5485 // nothing can be done with it unless that is set up.
5486 QCBORDecodeContext DC;
5487 const uint8_t pMinimalCBOR[] = {0x62, 0x48, 0x69}; // "Hi"
5488 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pMinimalCBOR),0);
5489
5490 uint8_t pAllocatorBuffer[50];
5491
5492 // This is really just to test that this call works.
5493 // The full functionality of string allocators is tested
5494 // elsewhere with the MemPool internal allocator.
5495 QCBORDecode_SetUpAllocator(&DC, AllocateTestFunction, pAllocatorBuffer, 1);
5496
5497 QCBORItem Item;
5498 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_SUCCESS) {
5499 return -1;
5500 }
5501
5502 if(Item.uDataAlloc == 0 ||
5503 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
5504 Item.val.string.ptr != pAllocatorBuffer) {
5505 return -2;
5506 }
5507
5508 if(QCBORDecode_Finish(&DC) != QCBOR_SUCCESS) {
5509 return -3;
5510 }
5511
5512 return 0;
5513}
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08005514#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
5515
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08005516
Laurence Lundbladedd6e76e2021-03-10 01:54:01 -07005517#ifndef QCBOR_DISABLE_EXP_AND_MANTISSA
Laurence Lundblade59289e52019-12-30 13:44:37 -08005518
Laurence Lundblade37286c02022-09-03 10:05:02 -07005519struct EaMTest {
5520 const char *szName;
5521 UsefulBufC Input;
5522 uint8_t uTagRequirement;
5523 bool bHasTags;
5524
5525 /* Expected values for GetNext */
5526 QCBORError uExpectedErrorGN;
5527 uint8_t uQCBORTypeGN;
5528 int64_t nExponentGN;
5529 int64_t nMantissaGN;
5530 UsefulBufC MantissaGN;
5531
5532 /* Expected values for GetDecimalFraction */
5533 QCBORError uExpectedErrorGDF;
5534 int64_t nExponentGDF;
5535 int64_t nMantissaGDF;
5536
5537 /* Expected values for GetDecimalFractionBig */
5538 QCBORError uExpectedErrorGDFB;
5539 int64_t nExponentGDFB;
5540 UsefulBufC MantissaGDFB;
5541 bool IsNegativeGDFB;
5542
5543 /* Expected values for GetBigFloat */
5544 QCBORError uExpectedErrorGBF;
5545 int64_t nExponentGBF;
5546 int64_t nMantissaGBF;
5547
5548 /* Expected values for GetBigFloatBig */
5549 QCBORError uExpectedErrorGBFB;
5550 int64_t nExponentGBFB;
5551 UsefulBufC MantissaGBFB;
5552 bool IsNegativeGBFB;
Laurence Lundblade59289e52019-12-30 13:44:37 -08005553};
5554
Laurence Lundbladefaec39f2020-08-02 21:53:53 -07005555
Laurence Lundblade37286c02022-09-03 10:05:02 -07005556
5557static const struct EaMTest pEaMTests[] = {
5558 {
5559 "1. Untagged pair (big float or decimal fraction), no tag required",
5560 {(const uint8_t []){0x82, 0x20, 0x03}, 3},
5561 QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
5562 false,
5563
5564 QCBOR_SUCCESS, /* for GetNext */
5565 QCBOR_TYPE_ARRAY,
5566 0,
5567 0,
5568 {(const uint8_t []){0x00}, 1},
5569
5570 QCBOR_SUCCESS, /* GetDecimalFraction */
5571 -1,
5572 3,
5573
5574 QCBOR_SUCCESS, /* for GetDecimalFractionBig */
5575 -1,
5576 {(const uint8_t []){0x03}, 1},
5577 false,
5578
5579 QCBOR_SUCCESS, /* for GetBigFloat */
5580 -1,
5581 3,
5582
5583 QCBOR_SUCCESS, /* for GetBigFloatBig */
5584 -1,
5585 {(const uint8_t []){0x03}, 1},
5586 false
5587 },
5588
5589 {
5590 "2. Untagged pair (big float or decimal fraction), tag required",
5591 {(const uint8_t []){0x82, 0x20, 0x03}, 3},
5592 QCBOR_TAG_REQUIREMENT_TAG,
5593 false,
5594
5595 QCBOR_SUCCESS, /* for GetNext */
5596 QCBOR_TYPE_ARRAY,
5597 0,
5598 0,
5599 {(const uint8_t []){0x00}, 1},
5600
5601 QCBOR_ERR_UNEXPECTED_TYPE, /* for GetDecimalFraction */
5602 0,
5603 0,
5604
5605 QCBOR_ERR_UNEXPECTED_TYPE, /* for GetDecimalFractionBig */
5606 0,
5607 {(const uint8_t []){0x00}, 1},
5608 false,
5609
5610 QCBOR_ERR_UNEXPECTED_TYPE, /* for GetBigFloat */
5611 0,
5612 0,
5613
5614 QCBOR_ERR_UNEXPECTED_TYPE, /* for GetBigFloatBig */
5615 0,
5616 {(const uint8_t []){0x00}, 1},
5617 false
5618
5619 },
5620
5621 {
5622 "3. Tagged 1.5 decimal fraction, tag 4 optional",
5623 {(const uint8_t []){0xC4, 0x82, 0x20, 0x03}, 4},
5624 QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
5625 true,
5626
5627 QCBOR_SUCCESS, /* for GetNext */
5628 QCBOR_TYPE_DECIMAL_FRACTION,
5629 -1,
5630 3,
5631 {(const uint8_t []){0x00}, 1},
5632
5633
5634 QCBOR_SUCCESS, /* for GetDecimalFraction */
5635 -1,
5636 3,
5637
5638 QCBOR_SUCCESS, /* for GetDecimalFractionBig */
5639 -1,
5640 {(const uint8_t []){0x03}, 1},
5641 false,
5642
5643 QCBOR_ERR_UNEXPECTED_TYPE, /* for GetBigFloat */
5644 0,
5645 0,
5646
5647 QCBOR_ERR_UNEXPECTED_TYPE, /* for GetBigFloatBig */
5648 0,
5649 {(const uint8_t []){0x00}, 1},
5650 false
5651 },
5652 {
5653 "4. Tagged 100 * 2^300 big float, tag 5 optional",
5654 {(const uint8_t []){0xC5, 0x82, 0x19, 0x01, 0x2C, 0x18, 0x64}, 7},
5655 QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
5656 true,
5657
5658 QCBOR_SUCCESS, /* for GetNext */
5659 QCBOR_TYPE_BIGFLOAT,
5660 300,
5661 100,
5662 {(const uint8_t []){0x00}, 1},
5663
5664
5665 QCBOR_ERR_UNEXPECTED_TYPE, /* for GetDecimalFraction */
5666 0,
5667 0,
5668
5669 QCBOR_ERR_UNEXPECTED_TYPE, /* for GetDecimalFractionBig */
5670 0,
5671 {(const uint8_t []){0x03}, 1},
5672 false,
5673
5674 QCBOR_SUCCESS, /* for GetBigFloat */
5675 300,
5676 100,
5677
5678 QCBOR_SUCCESS, /* for GetBigFloatBig */
5679 300,
5680 {(const uint8_t []){0x64}, 1},
5681 false
5682 },
5683
5684 {
5685 "5. Tagged 4([-20, 4759477275222530853136]) decimal fraction, tag 4 required",
5686 {(const uint8_t []){0xC4, 0x82, 0x33,
5687 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,}, 15},
5688 QCBOR_TAG_REQUIREMENT_TAG,
5689 true,
5690
5691 QCBOR_SUCCESS, /* for GetNext */
5692 QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM,
5693 -20,
5694 0,
5695 {(const uint8_t []){0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10}, 10},
5696
5697 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW, /* for GetDecimalFraction */
5698 0,
5699 0,
5700
5701 QCBOR_SUCCESS, /* for GetDecimalFractionBig */
5702 -20,
5703 {(const uint8_t []){0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10}, 10},
5704 false,
5705
5706 QCBOR_ERR_UNEXPECTED_TYPE, /* for GetBigFloat */
5707 0,
5708 0,
5709
5710 QCBOR_ERR_UNEXPECTED_TYPE, /* for GetBigFloatBig */
5711 0,
5712 {(const uint8_t []){0x00}, 0},
5713 false
5714 },
5715
5716 {
5717 "6. Error: Mantissa and exponent inside a Mantissa and exponent",
5718 {(const uint8_t []){0xC4, 0x82, 0x33,
5719 0xC5, 0x82, 0x19, 0x01, 0x2C, 0x18, 0x64}, 10},
5720 QCBOR_TAG_REQUIREMENT_TAG,
5721 true,
5722
5723 QCBOR_ERR_BAD_EXP_AND_MANTISSA, /* for GetNext */
5724 QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM,
5725 0,
5726 0,
5727 {(const uint8_t []){0x00}, 0},
5728
5729 QCBOR_ERR_BAD_EXP_AND_MANTISSA, /* for GetDecimalFraction */
5730 0,
5731 0,
5732
5733 QCBOR_ERR_BAD_EXP_AND_MANTISSA, /* for GetDecimalFractionBig */
5734 0,
5735 {(const uint8_t []){0x00}, 0},
5736 false,
5737
5738 QCBOR_ERR_BAD_EXP_AND_MANTISSA, /* for GetBigFloat */
5739 0,
5740 0,
5741
5742 QCBOR_ERR_BAD_EXP_AND_MANTISSA, /* for GetBigFloatBig */
5743 0,
5744 {(const uint8_t []){0x00}, 0},
5745 false
5746 },
5747 {
5748 "7. Tagged 5([-20, 4294967295]) big float, big num mantissa, tag 5 required",
5749 {(const uint8_t []){0xC5, 0x82, 0x33,
5750 0xC2, 0x44, 0xff, 0xff, 0xff, 0xff}, 9},
5751 QCBOR_TAG_REQUIREMENT_TAG,
5752 true,
5753
5754 QCBOR_SUCCESS, /* for GetNext */
5755 QCBOR_TYPE_BIGFLOAT_POS_BIGNUM,
5756 -20,
5757 0,
5758 {(const uint8_t []){0xff, 0xff, 0xff, 0xff}, 4},
5759
5760 QCBOR_ERR_UNEXPECTED_TYPE, /* for GetDecimalFraction */
5761 0,
5762 0,
5763
5764 QCBOR_ERR_UNEXPECTED_TYPE, /* for GetDecimalFractionBig */
5765 -20,
5766 {(const uint8_t []){0x00}, 1},
5767 false,
5768
5769 QCBOR_SUCCESS, /* for GetBigFloat */
5770 -20,
5771 4294967295,
5772
5773 QCBOR_SUCCESS, /* for GetBigFloatBig */
5774 -20,
5775 {(const uint8_t []){0xff, 0xff, 0xff, 0xff}, 4},
5776 false
5777 },
5778
5779 {
5780 /* Special case for test 8. Don't renumber it. */
5781 "8. Untagged pair with big num (big float or decimal fraction), tag optional",
5782 {(const uint8_t []){0x82, 0x33, 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10}, 14},
5783 QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
5784 true,
5785
5786 QCBOR_SUCCESS, /* for GetNext */
5787 QCBOR_TYPE_ARRAY,
5788 0,
5789 0,
5790 {(const uint8_t []){0x00}, 1},
5791
5792 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW, /* GetDecimalFraction */
5793 0,
5794 0,
5795
5796 QCBOR_SUCCESS, /* for GetDecimalFractionBig */
5797 -20,
5798 {(const uint8_t []){0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10}, 10},
5799 false,
5800
5801 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW, /* for GetBigFloat */
5802 0,
5803 0,
5804
5805 QCBOR_SUCCESS, /* for GetBigFloatBig */
5806 -20,
5807 {(const uint8_t []){0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10}, 10},
5808 false
5809 },
5810
5811 {
5812 "9. decimal fraction with large exponent and negative big num mantissa",
5813 {(const uint8_t []){0xC4, 0x82, 0x1B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
5814 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10}, 23},
5815 QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
5816 true,
5817
5818 QCBOR_SUCCESS, /* for GetNext */
5819 QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM,
5820 9223372036854775807,
5821 0,
5822 {(const uint8_t []){0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10}, 10},
5823
5824 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW, /* GetDecimalFraction */
5825 0,
5826 0,
5827
5828 QCBOR_SUCCESS, /* for GetDecimalFractionBig */
5829 9223372036854775807,
5830 {(const uint8_t []){0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10}, 10},
5831 true,
5832
5833 QCBOR_ERR_UNEXPECTED_TYPE, /* for GetBigFloat */
5834 0,
5835 0,
5836
5837 QCBOR_ERR_UNEXPECTED_TYPE, /* for GetBigFloatBig */
5838 0,
5839 {(const uint8_t []){0x00}, 1},
5840 false
5841 },
5842};
5843
5844
5845
5846int32_t ProcessEaMTests(void)
Laurence Lundblade59289e52019-12-30 13:44:37 -08005847{
Laurence Lundblade37286c02022-09-03 10:05:02 -07005848 size_t uIndex;
5849 QCBORDecodeContext DCtx;
5850 QCBORItem Item;
5851 QCBORError uError;
5852 int64_t nMantissa, nExponent;
5853 MakeUsefulBufOnStack( MantissaBuf, 200);
5854 UsefulBufC Mantissa;
5855 bool bMantissaIsNegative;
5856
5857 for(uIndex = 0; uIndex < C_ARRAY_COUNT(pEaMTests, struct EaMTest); uIndex++) {
5858 const struct EaMTest *pT = &pEaMTests[uIndex];
5859 /* Decode with GetNext */
5860 QCBORDecode_Init(&DCtx, pT->Input, 0);
5861
5862 if(uIndex + 1 == 9) {
5863 nExponent = 99; // just to set a break point
5864 }
5865
5866 uError = QCBORDecode_GetNext(&DCtx, &Item);
5867#ifdef QCBOR_DISABLE_TAGS
5868 /* Test 8 is a special case when tags are disabled */
5869 if(pT->bHasTags && uIndex + 1 != 8) {
5870 if(uError != QCBOR_ERR_TAGS_DISABLED) {
5871 return (int32_t)(1+uIndex) * 1000 + 9;
5872 }
5873 } else {
5874#endif
5875 /* Now check return code, data type, mantissa and exponent */
5876 if(pT->uExpectedErrorGN != uError) {
5877 return (int32_t)(1+uIndex) * 1000 + 1;
5878 }
5879 if(uError == QCBOR_SUCCESS && pT->uQCBORTypeGN != QCBOR_TYPE_ARRAY) {
5880 if(pT->uQCBORTypeGN != Item.uDataType) {
5881 return (int32_t)(1+uIndex) * 1000 + 2;
5882 }
5883 if(pT->nExponentGN != Item.val.expAndMantissa.nExponent) {
5884 return (int32_t)(1+uIndex) * 1000 + 3;
5885 }
5886 if(Item.uDataType == QCBOR_TYPE_DECIMAL_FRACTION || Item.uDataType == QCBOR_TYPE_BIGFLOAT ) {
5887 if(pT->nMantissaGN != Item.val.expAndMantissa.Mantissa.nInt) {
5888 return (int32_t)(1+uIndex) * 1000 + 4;
5889 }
5890 } else {
5891 if(UsefulBuf_Compare(Item.val.expAndMantissa.Mantissa.bigNum, pT->MantissaGN)) {
5892 return (int32_t)(1+uIndex) * 1000 + 5;
5893 }
5894 }
5895 }
5896#ifdef QCBOR_DISABLE_TAGS
5897 }
5898#endif
5899
5900 /* Decode with GetDecimalFraction */
5901 QCBORDecode_Init(&DCtx, pT->Input, 0);
5902 QCBORDecode_GetDecimalFraction(&DCtx,
5903 pT->uTagRequirement,
5904 &nMantissa,
5905 &nExponent);
5906 uError = QCBORDecode_GetAndResetError(&DCtx);
5907#ifdef QCBOR_DISABLE_TAGS
5908 if(pT->bHasTags) {
5909 if(uError != QCBOR_ERR_TAGS_DISABLED) {
5910 return (int32_t)(1+uIndex) * 1000 + 39;
5911 }
5912 } else {
5913#endif
5914 /* Now check return code, mantissa and exponent */
5915 if(pT->uExpectedErrorGDF != uError) {
5916 return (int32_t)(1+uIndex) * 1000 + 31;
5917 }
5918 if(uError == QCBOR_SUCCESS) {
5919 if(pT->nExponentGDF != nExponent) {
5920 return (int32_t)(1+uIndex) * 1000 + 32;
5921 }
5922 if(pT->nMantissaGDF != nMantissa) {
5923 return (int32_t)(1+uIndex) * 1000 + 33;
5924 }
5925 }
5926#ifdef QCBOR_DISABLE_TAGS
5927 }
5928#endif
5929
5930 /* Decode with GetDecimalFractionBig */
5931 QCBORDecode_Init(&DCtx, pT->Input, 0);
5932 QCBORDecode_GetDecimalFractionBig(&DCtx,
5933 pT->uTagRequirement,
5934 MantissaBuf,
5935 &Mantissa,
5936 &bMantissaIsNegative,
5937 &nExponent);
5938 uError = QCBORDecode_GetAndResetError(&DCtx);
5939#ifdef QCBOR_DISABLE_TAGS
5940 if(pT->bHasTags) {
5941 if(uError != QCBOR_ERR_TAGS_DISABLED) {
5942 return (int32_t)(1+uIndex) * 1000 + 49;
5943 }
5944 } else {
5945#endif
5946 /* Now check return code, mantissa (bytes and sign) and exponent */
5947 if(pT->uExpectedErrorGDFB != uError) {
5948 return (int32_t)(1+uIndex) * 1000 + 41;
5949 }
5950 if(uError == QCBOR_SUCCESS) {
5951 if(pT->nExponentGDFB != nExponent) {
5952 return (int32_t)(1+uIndex) * 1000 + 42;
5953 }
5954 if(pT->IsNegativeGDFB != bMantissaIsNegative) {
5955 return (int32_t)(1+uIndex) * 1000 + 43;
5956 }
5957 if(UsefulBuf_Compare(Mantissa, pT->MantissaGDFB)) {
5958 return (int32_t)(1+uIndex) * 1000 + 44;
5959 }
5960 }
5961#ifdef QCBOR_DISABLE_TAGS
5962 }
5963#endif
5964
5965 /* Decode with GetBigFloat */
5966 QCBORDecode_Init(&DCtx, pT->Input, 0);
5967 QCBORDecode_GetBigFloat(&DCtx,
5968 pT->uTagRequirement,
5969 &nMantissa,
5970 &nExponent);
5971 uError = QCBORDecode_GetAndResetError(&DCtx);
5972#ifdef QCBOR_DISABLE_TAGS
5973 if(pT->bHasTags) {
5974 if(uError != QCBOR_ERR_TAGS_DISABLED) {
5975 return (int32_t)(1+uIndex) * 1000 + 19;
5976 }
5977 } else {
5978#endif
5979 /* Now check return code, mantissa and exponent */
5980 if(pT->uExpectedErrorGBF != uError) {
5981 return (int32_t)(1+uIndex) * 1000 + 11;
5982 }
5983 if(uError == QCBOR_SUCCESS) {
5984 if(pT->nExponentGBF != nExponent) {
5985 return (int32_t)(1+uIndex) * 1000 + 12;
5986 }
5987 if(pT->nMantissaGBF != nMantissa) {
5988 return (int32_t)(1+uIndex) * 1000 + 13;
5989 }
5990 }
5991#ifdef QCBOR_DISABLE_TAGS
5992 }
5993#endif
5994
5995 /* Decode with GetBigFloatBig */
5996 QCBORDecode_Init(&DCtx, pT->Input, 0);
5997 QCBORDecode_GetBigFloatBig(&DCtx,
5998 pT->uTagRequirement,
5999 MantissaBuf,
6000 &Mantissa,
6001 &bMantissaIsNegative,
6002 &nExponent);
6003 uError = QCBORDecode_GetAndResetError(&DCtx);
6004#ifdef QCBOR_DISABLE_TAGS
6005 if(pT->bHasTags) {
6006 if(uError != QCBOR_ERR_TAGS_DISABLED) {
6007 return (int32_t)(1+uIndex) * 1000 + 29;
6008 }
6009 } else {
6010#endif
6011 /* Now check return code, mantissa (bytes and sign) and exponent */
6012 if(pT->uExpectedErrorGBFB != uError) {
6013 return (int32_t)(1+uIndex) * 1000 + 21;
6014 }
6015 if(uError == QCBOR_SUCCESS) {
6016 if(pT->nExponentGBFB != nExponent) {
6017 return (int32_t)(1+uIndex) * 1000 + 22;
6018 }
6019 if(pT->IsNegativeGBFB != bMantissaIsNegative) {
6020 return (int32_t)(1+uIndex) * 1000 + 23;
6021 }
6022 if(UsefulBuf_Compare(Mantissa, pT->MantissaGBFB)) {
6023 return (int32_t)(1+uIndex) * 1000 + 24;
6024 }
6025 }
6026#ifdef QCBOR_DISABLE_TAGS
6027 }
6028#endif
6029 }
6030
6031 return 0;
6032}
6033
6034
6035int32_t ExponentAndMantissaDecodeTestsSecondary(void)
6036{
6037#ifndef QCBOR_DISABLE_TAGS
Laurence Lundblade59289e52019-12-30 13:44:37 -08006038 QCBORDecodeContext DC;
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07006039 QCBORError uErr;
Laurence Lundblade59289e52019-12-30 13:44:37 -08006040 QCBORItem item;
6041
Laurence Lundblade17af4902020-01-07 19:11:55 -08006042 static const uint8_t spBigNumMantissa[] = {0x01, 0x02, 0x03, 0x04, 0x05,
6043 0x06, 0x07, 0x08, 0x09, 0x010};
6044 UsefulBufC BN = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNumMantissa);
Laurence Lundblade59289e52019-12-30 13:44:37 -08006045
6046
Laurence Lundblade59289e52019-12-30 13:44:37 -08006047
6048 /* Now encode some stuff and then decode it */
6049 uint8_t pBuf[40];
6050 QCBOREncodeContext EC;
6051 UsefulBufC Encoded;
6052
6053 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(pBuf));
6054 QCBOREncode_OpenArray(&EC);
6055 QCBOREncode_AddDecimalFraction(&EC, 999, 1000); // 999 * (10 ^ 1000)
6056 QCBOREncode_AddBigFloat(&EC, 100, INT32_MIN);
6057 QCBOREncode_AddDecimalFractionBigNum(&EC, BN, false, INT32_MAX);
6058 QCBOREncode_CloseArray(&EC);
6059 QCBOREncode_Finish(&EC, &Encoded);
6060
6061
6062 QCBORDecode_Init(&DC, Encoded, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07006063 uErr = QCBORDecode_GetNext(&DC, &item);
6064 if(uErr != QCBOR_SUCCESS) {
6065 return 100;
Laurence Lundblade59289e52019-12-30 13:44:37 -08006066 }
6067
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07006068 uErr = QCBORDecode_GetNext(&DC, &item);
6069 if(uErr != QCBOR_SUCCESS) {
6070 return 101;
Laurence Lundblade59289e52019-12-30 13:44:37 -08006071 }
6072
6073 if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION ||
6074 item.val.expAndMantissa.nExponent != 1000 ||
6075 item.val.expAndMantissa.Mantissa.nInt != 999) {
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07006076 return 102;
Laurence Lundblade59289e52019-12-30 13:44:37 -08006077 }
6078
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07006079 uErr = QCBORDecode_GetNext(&DC, &item);
6080 if(uErr != QCBOR_SUCCESS) {
6081 return 103;
Laurence Lundblade59289e52019-12-30 13:44:37 -08006082 }
6083
6084 if(item.uDataType != QCBOR_TYPE_BIGFLOAT ||
6085 item.val.expAndMantissa.nExponent != INT32_MIN ||
6086 item.val.expAndMantissa.Mantissa.nInt != 100) {
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07006087 return 104;
Laurence Lundblade59289e52019-12-30 13:44:37 -08006088 }
6089
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07006090 uErr = QCBORDecode_GetNext(&DC, &item);
6091 if(uErr != QCBOR_SUCCESS) {
6092 return 105;
Laurence Lundblade59289e52019-12-30 13:44:37 -08006093 }
6094
6095 if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM ||
6096 item.val.expAndMantissa.nExponent != INT32_MAX ||
6097 UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) {
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07006098 return 106;
6099 }
6100
Laurence Lundblade37286c02022-09-03 10:05:02 -07006101#endif /* QCBOR_TAGS_DISABLED */
Laurence Lundblade59289e52019-12-30 13:44:37 -08006102
6103 return 0;
6104}
6105
6106
Laurence Lundblade37286c02022-09-03 10:05:02 -07006107int32_t ExponentAndMantissaDecodeTests(void)
6108{
6109 int32_t rv = ProcessEaMTests();
6110 if(rv) {
6111 return rv;
6112 }
6113
6114 return ExponentAndMantissaDecodeTestsSecondary();
6115}
6116
6117
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08006118static const struct DecodeFailTestInput ExponentAndMantissaFailures[] = {
6119 { "Exponent > INT64_MAX",
6120 QCBOR_DECODE_MODE_NORMAL,
6121 {"\xC4\x82\x1B\x7f\xFF\xFF\xFF\xFF\xFF\xFF\xFF\x1B\x80\xFF\xFF\xFF\xFF\xFF\xFF\xFF", 20},
6122 QCBOR_ERR_BAD_EXP_AND_MANTISSA
6123 },
6124 { "Mantissa > INT64_MAX",
6125 QCBOR_DECODE_MODE_NORMAL,
6126 {"\xC4\x82\x1B\x80\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xC3\x4A\x01\x02\x03\x04\x05\x06\x07\x08\x09\x10", 23},
6127 QCBOR_ERR_BAD_EXP_AND_MANTISSA
6128 },
6129 {
6130 "End of input",
6131 QCBOR_DECODE_MODE_NORMAL,
6132 {"\xC4\x82", 2},
6133 QCBOR_ERR_NO_MORE_ITEMS
6134 },
6135 {"bad content for big num",
6136 QCBOR_DECODE_MODE_NORMAL,
6137 {"\xC4\x82\x01\xc3\x01", 5},
6138 QCBOR_ERR_BAD_OPT_TAG
6139 },
6140 {"bad content for big num",
6141 QCBOR_DECODE_MODE_NORMAL,
6142 {"\xC4\x82\xc2\x01\x1f", 5},
6143 QCBOR_ERR_BAD_INT
6144 },
6145 {"Bad integer for exponent",
6146 QCBOR_DECODE_MODE_NORMAL,
6147 {"\xC4\x82\x01\x1f", 4},
6148 QCBOR_ERR_BAD_INT
6149 },
6150 {"Bad integer for mantissa",
6151 QCBOR_DECODE_MODE_NORMAL,
6152 {"\xC4\x82\x1f\x01", 4},
6153 QCBOR_ERR_BAD_INT
6154 },
6155 {"3 items in array",
6156 QCBOR_DECODE_MODE_NORMAL,
6157 {"\xC4\x83\x03\x01\x02", 5},
6158 QCBOR_ERR_BAD_EXP_AND_MANTISSA},
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08006159#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08006160 {"unterminated indefinite length array",
6161 QCBOR_DECODE_MODE_NORMAL,
6162 {"\xC4\x9f\x03\x01\x02", 5},
6163 QCBOR_ERR_BAD_EXP_AND_MANTISSA
6164 },
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08006165#else /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08006166 {"unterminated indefinite length array",
6167 QCBOR_DECODE_MODE_NORMAL,
6168 {"\xC4\x9f\x03\x01\x02", 5},
6169 QCBOR_ERR_INDEF_LEN_ARRAYS_DISABLED
6170 },
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08006171#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08006172 {"Empty array",
6173 QCBOR_DECODE_MODE_NORMAL,
6174 {"\xC4\x80", 2},
6175 QCBOR_ERR_NO_MORE_ITEMS
6176 },
6177 {"Second is not an integer",
6178 QCBOR_DECODE_MODE_NORMAL,
6179 {"\xC4\x82\x03\x40", 4},
6180 QCBOR_ERR_BAD_EXP_AND_MANTISSA
6181 },
6182 {"First is not an integer",
6183 QCBOR_DECODE_MODE_NORMAL,
6184 {"\xC4\x82\x40", 3},
6185 QCBOR_ERR_BAD_EXP_AND_MANTISSA
6186 },
6187 {"Not an array",
6188 QCBOR_DECODE_MODE_NORMAL,
6189 {"\xC4\xA2", 2},
6190 QCBOR_ERR_BAD_EXP_AND_MANTISSA
6191 }
Laurence Lundblade59289e52019-12-30 13:44:37 -08006192};
6193
6194
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08006195int32_t
6196ExponentAndMantissaDecodeFailTests(void)
Laurence Lundblade59289e52019-12-30 13:44:37 -08006197{
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08006198 return ProcessDecodeFailures(ExponentAndMantissaFailures,
6199 C_ARRAY_COUNT(ExponentAndMantissaFailures,
6200 struct DecodeFailTestInput));
Laurence Lundblade59289e52019-12-30 13:44:37 -08006201}
6202
Laurence Lundbladedd6e76e2021-03-10 01:54:01 -07006203#endif /* QCBOR_DISABLE_EXP_AND_MANTISSA */
Laurence Lundbladebb87be22020-04-09 19:15:32 -07006204
6205
6206
6207/*
6208 Some basic CBOR with map and array used in a lot of tests.
6209 The map labels are all strings
6210
Laurence Lundblade8ffdb742020-05-07 02:49:18 -07006211 {
6212 "first integer": 42,
Laurence Lundbladebb87be22020-04-09 19:15:32 -07006213 "an array of two strings": [
6214 "string1", "string2"
6215 ],
6216 "map in a map": {
6217 "bytes 1": h'78787878',
6218 "bytes 2": h'79797979',
6219 "another int": 98,
6220 "text 2": "lies, damn lies and statistics"
6221 }
Laurence Lundbladeddebabe2020-11-22 11:32:17 -08006222 }
Laurence Lundbladebb87be22020-04-09 19:15:32 -07006223 */
Laurence Lundblade9b334962020-08-27 10:55:53 -07006224
Laurence Lundbladef7a70bc2020-10-24 12:23:25 -07006225int32_t SpiffyDecodeBasicMap(UsefulBufC input)
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07006226{
6227 QCBORItem Item1, Item2, Item3;
6228 int64_t nDecodedInt1, nDecodedInt2;
6229 UsefulBufC B1, B2, S1, S2, S3;
6230
6231 QCBORDecodeContext DCtx;
6232 QCBORError nCBORError;
6233
6234 QCBORDecode_Init(&DCtx, input, 0);
6235
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006236 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07006237
6238 QCBORDecode_GetInt64InMapSZ(&DCtx, "first integer", &nDecodedInt1);
6239
6240 QCBORDecode_EnterMapFromMapSZ(&DCtx, "map in a map");
6241 QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2);
Laurence Lundblade323f8a92020-09-06 19:43:09 -07006242 QCBORDecode_GetByteStringInMapSZ(&DCtx, "bytes 1", &B1);
6243 QCBORDecode_GetByteStringInMapSZ(&DCtx, "bytes 2", &B2);
6244 QCBORDecode_GetTextStringInMapSZ(&DCtx, "text 2", &S1);
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07006245 QCBORDecode_ExitMap(&DCtx);
6246
6247 QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings");
6248 QCBORDecode_GetNext(&DCtx, &Item1);
6249 QCBORDecode_GetNext(&DCtx, &Item2);
6250 if(QCBORDecode_GetNext(&DCtx, &Item3) != QCBOR_ERR_NO_MORE_ITEMS) {
6251 return -400;
6252 }
6253 QCBORDecode_ExitArray(&DCtx);
6254
6255 // Parse the same array again using GetText() instead of GetItem()
6256 QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings");
Laurence Lundblade323f8a92020-09-06 19:43:09 -07006257 QCBORDecode_GetTextString(&DCtx, &S2);
6258 QCBORDecode_GetTextString(&DCtx, &S3);
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07006259 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) {
6260 return 5000;
6261 }
6262 /* QCBORDecode_GetText(&DCtx, &S3);
6263 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_NO_MORE_ITEMS) {
6264 return 5001;
6265 } */
6266
6267 QCBORDecode_ExitArray(&DCtx);
6268
6269 QCBORDecode_ExitMap(&DCtx);
6270
6271 nCBORError = QCBORDecode_Finish(&DCtx);
6272
6273 if(nCBORError) {
6274 return (int32_t)nCBORError;
6275 }
6276
6277 if(nDecodedInt1 != 42) {
6278 return 1001;
6279 }
6280
6281 if(nDecodedInt2 != 98) {
6282 return 1002;
6283 }
6284
6285 if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07006286 UsefulBufCompareToSZ(Item1.val.string, "string1")) {
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07006287 return 1003;
6288 }
6289
6290 if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07006291 UsefulBufCompareToSZ(Item2.val.string, "string2")) {
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07006292 return 1004;
6293 }
6294
Laurence Lundblade9b334962020-08-27 10:55:53 -07006295 if(UsefulBufCompareToSZ(S1, "lies, damn lies and statistics")) {
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07006296 return 1005;
6297 }
6298
6299 if(UsefulBuf_Compare(B1, UsefulBuf_FromSZ("xxxx"))){
6300 return 1006;
6301 }
6302
6303 if(UsefulBuf_Compare(B2, UsefulBuf_FromSZ("yyyy"))){
6304 return 1007;
6305 }
6306
6307 if(UsefulBuf_Compare(S2, UsefulBuf_FromSZ("string1"))){
6308 return 1008;
6309 }
6310
6311 if(UsefulBuf_Compare(S3, UsefulBuf_FromSZ("string2"))){
6312 return 1009;
6313 }
6314
6315 return 0;
6316}
6317
Laurence Lundbladeddebabe2020-11-22 11:32:17 -08006318/*
Laurence Lundbladecc7da412020-12-27 00:09:07 -08006319 {
6320 -75008: h'05083399',
6321 88: [],
6322 100100: {
6323 "sub1": {
6324 10: [
6325 0
6326 ],
6327 -75009: h'A46823990001',
6328 100100: {
6329 "json": "{ \"ueid\", \"xyz\"}",
6330 "subsub": {
6331 100002: h'141813191001'
6332 }
6333 }
Laurence Lundbladeddebabe2020-11-22 11:32:17 -08006334 }
Laurence Lundbladecc7da412020-12-27 00:09:07 -08006335 }
Laurence Lundbladeddebabe2020-11-22 11:32:17 -08006336 }
Laurence Lundbladeddebabe2020-11-22 11:32:17 -08006337 */
6338
6339static const uint8_t spNestedCBOR[] = {
Laurence Lundbladecc7da412020-12-27 00:09:07 -08006340 0xa3, 0x3a, 0x00, 0x01, 0x24, 0xff, 0x44, 0x05,
6341 0x08, 0x33, 0x99, 0x18, 0x58, 0x80, 0x1a, 0x00,
6342 0x01, 0x87, 0x04, 0xa1, 0x64, 0x73, 0x75, 0x62,
6343 0x31, 0xa3, 0x0a, 0x81, 0x00, 0x3a, 0x00, 0x01,
6344 0x25, 0x00, 0x46, 0xa4, 0x68, 0x23, 0x99, 0x00,
6345 0x01, 0x1a, 0x00, 0x01, 0x87, 0x04, 0xa2, 0x64,
6346 0x6a, 0x73, 0x6f, 0x6e, 0x70, 0x7b, 0x20, 0x22,
6347 0x75, 0x65, 0x69, 0x64, 0x22, 0x2c, 0x20, 0x22,
6348 0x78, 0x79, 0x7a, 0x22, 0x7d, 0x66, 0x73, 0x75,
6349 0x62, 0x73, 0x75, 0x62, 0xa1, 0x1a, 0x00, 0x01,
6350 0x86, 0xa2, 0x46, 0x14, 0x18, 0x13, 0x19, 0x10,
6351 0x01
Laurence Lundbladeddebabe2020-11-22 11:32:17 -08006352};
6353
6354/* Get item in multi-level nesting in spNestedCBOR */
6355static int32_t DecodeNestedGetSubSub(QCBORDecodeContext *pDCtx)
6356{
6357 UsefulBufC String;
6358
6359 uint8_t test_oemid_bytes[] = {0x14, 0x18, 0x13, 0x19, 0x10, 0x01};
6360 const struct q_useful_buf_c test_oemid = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(test_oemid_bytes);
6361
6362 QCBORDecode_EnterMapFromMapN(pDCtx, 100100);
6363 QCBORDecode_EnterMap(pDCtx, NULL);
6364 QCBORDecode_EnterMapFromMapN(pDCtx, 100100);
6365 QCBORDecode_EnterMapFromMapSZ(pDCtx, "subsub");
6366 QCBORDecode_GetByteStringInMapN(pDCtx, 100002, &String);
6367 if(QCBORDecode_GetError(pDCtx)) {
6368 return 4001;
6369 }
6370 if(UsefulBuf_Compare(String, test_oemid)) {
6371 return 4002;
6372 }
6373 QCBORDecode_ExitMap(pDCtx);
6374 QCBORDecode_ExitMap(pDCtx);
6375 QCBORDecode_ExitMap(pDCtx);
6376 QCBORDecode_ExitMap(pDCtx);
6377
6378 return 0;
6379}
6380
6381/* Iterations on the zero-length array in spNestedCBOR */
6382static int32_t DecodeNestedGetEmpty(QCBORDecodeContext *pDCtx)
6383{
6384 QCBORItem Item;
6385 QCBORError uErr;
6386
6387 QCBORDecode_EnterArrayFromMapN(pDCtx, 88);
6388 for(int x = 0; x < 20; x++) {
6389 uErr = QCBORDecode_GetNext(pDCtx, &Item);
6390 if(uErr != QCBOR_ERR_NO_MORE_ITEMS) {
6391 return 4100;
6392
6393 }
6394 }
6395 QCBORDecode_ExitArray(pDCtx);
6396 if(QCBORDecode_GetError(pDCtx)) {
6397 return 4101;
6398 }
6399
6400 return 0;
6401}
6402
6403/* Various iterations on the array that contains a zero in spNestedCBOR */
6404static int32_t DecodeNestedGetZero(QCBORDecodeContext *pDCtx)
6405{
6406 QCBORError uErr;
6407
6408 QCBORDecode_EnterMapFromMapN(pDCtx, 100100);
6409 QCBORDecode_EnterMapFromMapSZ(pDCtx, "sub1");
6410 QCBORDecode_EnterArrayFromMapN(pDCtx, 10);
6411 int64_t nInt = 99;
6412 QCBORDecode_GetInt64(pDCtx, &nInt);
6413 if(nInt != 0) {
6414 return 4200;
6415 }
6416 for(int x = 0; x < 20; x++) {
6417 QCBORItem Item;
6418 uErr = QCBORDecode_GetNext(pDCtx, &Item);
6419 if(uErr != QCBOR_ERR_NO_MORE_ITEMS) {
6420 return 4201;
6421
6422 }
6423 }
6424 QCBORDecode_ExitArray(pDCtx);
6425 if(QCBORDecode_GetAndResetError(pDCtx)) {
6426 return 4202;
6427 }
6428 QCBORDecode_EnterArrayFromMapN(pDCtx, 10);
6429 UsefulBufC dD;
6430 QCBORDecode_GetByteString(pDCtx, &dD);
6431 if(QCBORDecode_GetAndResetError(pDCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
6432 return 4203;
6433 }
6434 for(int x = 0; x < 20; x++) {
6435 QCBORDecode_GetByteString(pDCtx, &dD);
6436 uErr = QCBORDecode_GetAndResetError(pDCtx);
6437 if(uErr != QCBOR_ERR_NO_MORE_ITEMS) {
6438 return 4204;
6439 }
6440 }
6441 QCBORDecode_ExitArray(pDCtx);
6442 QCBORDecode_ExitMap(pDCtx);
6443 QCBORDecode_ExitMap(pDCtx);
6444
6445 return 0;
6446}
6447
6448/* Repeatedly enter and exit maps and arrays, go off the end of maps
6449 and arrays and such. */
Laurence Lundbladeb9702452021-03-08 21:02:57 -08006450static int32_t DecodeNestedIterate(void)
Laurence Lundbladeddebabe2020-11-22 11:32:17 -08006451{
6452 QCBORDecodeContext DCtx;
6453 int32_t nReturn;
6454 QCBORError uErr;
6455
6456 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spNestedCBOR), 0);
6457 QCBORDecode_EnterMap(&DCtx, NULL);
6458
6459 for(int j = 0; j < 5; j++) {
6460 for(int i = 0; i < 20; i++) {
6461 nReturn = DecodeNestedGetSubSub(&DCtx);
6462 if(nReturn) {
6463 return nReturn;
6464 }
6465 }
6466
6467 for(int i = 0; i < 20; i++) {
6468 nReturn = DecodeNestedGetEmpty(&DCtx);
6469 if(nReturn ) {
6470 return nReturn;
6471 }
6472 }
6473
6474 for(int i = 0; i < 20; i++) {
6475 nReturn = DecodeNestedGetZero(&DCtx);
6476 if(nReturn ) {
6477 return nReturn;
6478 }
6479 }
6480 }
6481
6482 QCBORDecode_ExitMap(&DCtx);
6483 uErr = QCBORDecode_Finish(&DCtx);
6484 if(uErr) {
6485 return (int32_t)uErr + 4100;
6486 }
6487
6488 return 0;
6489}
6490
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07006491
6492/*
Laurence Lundbladecc7da412020-12-27 00:09:07 -08006493 [
6494 23,
6495 6000,
6496 h'67616C6163746963',
6497 h'686176656E20746F6B656E'
6498 ]
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07006499 */
6500static const uint8_t spSimpleArray[] = {
Laurence Lundblade9b334962020-08-27 10:55:53 -07006501 0x84,
6502 0x17,
6503 0x19, 0x17, 0x70,
6504 0x48, 0x67, 0x61, 0x6C, 0x61, 0x63, 0x74, 0x69, 0x63,
6505 0x4B, 0x68, 0x61, 0x76, 0x65, 0x6E, 0x20, 0x74, 0x6F, 0x6B, 0x65, 0x6E};
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07006506
Laurence Lundbladecc7da412020-12-27 00:09:07 -08006507/* [h'', {}, [], 0] */
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08006508static const uint8_t spArrayOfEmpty[] = {0x84, 0x40, 0xa0, 0x80, 0x00};
6509
Laurence Lundbladecc7da412020-12-27 00:09:07 -08006510/* {} */
Laurence Lundbladee6f15112020-07-23 18:44:16 -07006511static const uint8_t spEmptyMap[] = {0xa0};
6512
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08006513#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundbladecc7da412020-12-27 00:09:07 -08006514/* {} */
Laurence Lundbladee6f15112020-07-23 18:44:16 -07006515static const uint8_t spEmptyInDefinteLengthMap[] = {0xbf, 0xff};
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07006516
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08006517
Laurence Lundbladef0499502020-08-01 11:55:57 -07006518/*
6519 {
6520 0: [],
6521 9: [
6522 [],
6523 []
6524 ],
6525 8: {
6526 1: [],
6527 2: {},
6528 3: []
6529 },
6530 4: {},
6531 5: [],
6532 6: [
6533 [],
6534 []
6535 ]
Laurence Lundbladecc7da412020-12-27 00:09:07 -08006536 }
Laurence Lundbladef0499502020-08-01 11:55:57 -07006537 */
6538static const uint8_t spMapOfEmpty[] = {
Laurence Lundbladecc7da412020-12-27 00:09:07 -08006539 0xa6, 0x00, 0x80, 0x09, 0x82, 0x80, 0x80, 0x08,
6540 0xa3, 0x01, 0x80, 0x02, 0xa0, 0x03, 0x80, 0x04,
6541 0xa0, 0x05, 0x9f, 0xff, 0x06, 0x9f, 0x80, 0x9f,
6542 0xff, 0xff};
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07006543
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08006544#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
6545
6546
Laurence Lundblade93d3f532020-09-28 21:09:12 -07006547/*
6548 Too many tags
Laurence Lundblade93d3f532020-09-28 21:09:12 -07006549 Duplicate label
6550 Integer overflow
6551 Date overflow
6552
Laurence Lundbladecc7da412020-12-27 00:09:07 -08006553 {
6554 1: 224(225(226(227(4(0))))),
Laurence Lundbladecc7da412020-12-27 00:09:07 -08006555 3: -18446744073709551616,
6556 4: 1(1.0e+300),
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07006557 5: 0,
6558 8: 8
Laurence Lundbladecc7da412020-12-27 00:09:07 -08006559 }
Laurence Lundblade93d3f532020-09-28 21:09:12 -07006560 */
6561static const uint8_t spRecoverableMapErrors[] = {
Laurence Lundblade37286c02022-09-03 10:05:02 -07006562#ifndef QCBOR_DISABLE_TAGS
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07006563 0xa6,
Laurence Lundblade93d3f532020-09-28 21:09:12 -07006564 0x04, 0xc1, 0xfb, 0x7e, 0x37, 0xe4, 0x3c, 0x88, 0x00, 0x75, 0x9c,
Laurence Lundblade37286c02022-09-03 10:05:02 -07006565 0x01, 0xd8, 0xe0, 0xd8, 0xe1, 0xd8, 0xe2, 0xd8, 0xe3, 0xd8, 0x04, 0x00,
6566#else
6567 0xa4,
6568#endif
6569 0x03, 0x3b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
Laurence Lundblade93d3f532020-09-28 21:09:12 -07006570 0x05, 0x00,
6571 0x05, 0x00,
6572 0x08, 0x08,
Laurence Lundblade93d3f532020-09-28 21:09:12 -07006573};
6574
Laurence Lundbladecc7da412020-12-27 00:09:07 -08006575/* Bad break */
Laurence Lundblade93d3f532020-09-28 21:09:12 -07006576static const uint8_t spUnRecoverableMapError1[] = {
6577 0xa2, 0xff, 0x01, 0x00, 0x02, 0x00
6578};
6579
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08006580#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundbladecc7da412020-12-27 00:09:07 -08006581/* No more items */
Laurence Lundblade93d3f532020-09-28 21:09:12 -07006582static const uint8_t spUnRecoverableMapError2[] = {
6583 0xbf, 0x02, 0xbf, 0xff, 0x01, 0x00, 0x02, 0x00
6584};
6585
Laurence Lundbladecc7da412020-12-27 00:09:07 -08006586/* Hit end because string is too long */
Laurence Lundblade93d3f532020-09-28 21:09:12 -07006587static const uint8_t spUnRecoverableMapError3[] = {
6588 0xbf, 0x02, 0x69, 0x64, 0x64, 0xff
6589};
6590
Laurence Lundbladecc7da412020-12-27 00:09:07 -08006591/* Hit end because string is too long */
Laurence Lundblade93d3f532020-09-28 21:09:12 -07006592static const uint8_t spUnRecoverableMapError4[] = {
6593 0xbf,
6594 0x02, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f,
6595 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f,
6596 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
6597 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
6598 0xff
6599};
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08006600#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
Laurence Lundblade93d3f532020-09-28 21:09:12 -07006601
Laurence Lundblade63926052021-03-29 16:05:51 -07006602const unsigned char not_well_formed_submod_section[] = {
6603 0xa1, 0x14, 0x1f,
6604};
Laurence Lundblade93d3f532020-09-28 21:09:12 -07006605
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07006606
6607/* Array of length 3, but only two items. */
6608const unsigned char spBadConsumeInput[] = {
6609 0x83, 0x00, 0x00
6610};
6611
6612/* Tag nesting too deep. */
6613const unsigned char spBadConsumeInput2[] = {
6614 0x81,
6615 0xD8, 0x37,
6616 0xD8, 0x2C,
6617 0xD8, 0x21,
6618 0xD6,
6619 0xCB,
6620 00
6621};
6622
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07006623
6624const unsigned char spBadConsumeInput4[] = {
6625 0x81, 0x9f, 0x00, 0xff
6626};
6627
6628const unsigned char spBadConsumeInput5[] = {
6629 0xa1, 0x80, 0x00
6630};
6631
Laurence Lundbladec5f45e42023-12-18 09:23:20 -07006632/*
6633 Lots of nesting for various nesting tests.
6634 { 1:1,
6635 2:{
6636 21:21,
6637 22:{
6638 221:[2111, 2112, 2113],
6639 222:222,
6640 223: {}
6641 },
6642 23: 23
6643 },
6644 3:3,
6645 4: [ {} ]
6646 }
6647 */
6648static const uint8_t spNested[] = {
66490xA4, /* Map of 4 */
6650 0x01, 0x01, /* Map entry 1 : 1 */
6651 0x02, 0xA3, /* Map entry 2 : {, an array of 3 */
6652 0x15, 0x15, /* Map entry 21 : 21 */
6653 0x16, 0xA3, /* Map entry 22 : {, a map of 3 */
6654 0x18, 0xDD, 0x83, /* Map entry 221 : [ an array of 3 */
6655 0x19, 0x08, 0x3F, /* Array item 2111 */
6656 0x19, 0x08, 0x40, /* Array item 2112 */
6657 0x19, 0x08, 0x41, /* Array item 2113 */
6658 0x18, 0xDE, 0x18, 0xDE, /* Map entry 222 : 222 */
6659 0x18, 0xDF, 0xA0, /* Map entry 223 : {} */
6660 0x17, 0x17, /* Map entry 23 : 23 */
6661 0x03, 0x03, /* Map entry 3 : 3 */
6662 0x04, 0x81, /* Map entry 4: [, an array of 1 */
6663 0xA0 /* Array entry {}, an empty map */
6664};
6665
6666
6667static int32_t EnterMapCursorTest(void)
6668{
6669 QCBORDecodeContext DCtx;
6670 QCBORItem Item1;
Laurence Lundblade11654912024-05-09 11:49:24 -07006671 int64_t nInt;
6672 QCBORError uErr;
6673
6674 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spNested), 0);
6675 QCBORDecode_EnterMap(&DCtx, NULL);
6676 QCBORDecode_GetInt64InMapN (&DCtx, 3, &nInt);
6677 uErr = QCBORDecode_GetNext(&DCtx, &Item1);
Laurence Lundbladea29f45a2024-05-14 15:55:19 -07006678 if(uErr != QCBOR_SUCCESS) {
6679 return 701;
6680 }
Laurence Lundblade11654912024-05-09 11:49:24 -07006681 if(Item1.uDataType != QCBOR_TYPE_INT64) {
6682 return 700;
6683 }
6684
Laurence Lundbladec5f45e42023-12-18 09:23:20 -07006685
6686 int i;
6687 for(i = 0; i < 13; i++) {
6688 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spNested), 0);
6689 QCBORDecode_EnterMap(&DCtx, NULL);
6690 int j;
6691 /* Move travesal cursor */
6692 for(j = 0; j < i; j++) {
6693 QCBORDecode_GetNext(&DCtx, &Item1);
6694 }
6695 QCBORDecode_EnterMapFromMapN(&DCtx, 2);
6696 QCBORDecode_ExitMap(&DCtx);
6697 QCBORDecode_GetNext(&DCtx, &Item1);
6698 if(Item1.label.int64 != 3) {
6699 return 8000;
6700 }
6701 }
6702
6703 for(i = 0; i < 13; i++) {
6704 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spNested), 0);
6705 QCBORDecode_EnterMap(&DCtx, NULL);
6706 int j;
6707 /* Move travesal cursor */
6708 for(j = 0; j < i; j++) {
6709 QCBORDecode_GetNext(&DCtx, &Item1);
6710 }
6711 QCBORDecode_EnterMapFromMapN(&DCtx, 2);
6712 QCBORDecode_EnterMapFromMapN(&DCtx, 22);
6713 QCBORDecode_ExitMap(&DCtx);
6714 QCBORDecode_GetNext(&DCtx, &Item1);
6715 if(Item1.label.int64 != 23) {
6716 return 8000;
6717 }
6718 }
6719
6720 for(i = 0; i < 13; i++) {
6721 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spNested), 0);
6722 QCBORDecode_EnterMap(&DCtx, NULL);
6723 int j;
6724 /* Move travesal cursor */
6725 for(j = 0; j < i; j++) {
6726 QCBORDecode_GetNext(&DCtx, &Item1);
6727 }
6728 QCBORDecode_EnterMapFromMapN(&DCtx, 2);
6729 QCBORDecode_EnterMapFromMapN(&DCtx, 22);
6730 for(j = 0; j < i; j++) {
6731 QCBORDecode_GetNext(&DCtx, &Item1);
6732 }
6733 QCBORDecode_EnterArrayFromMapN(&DCtx, 221);
6734 QCBORDecode_ExitArray(&DCtx);
6735 QCBORDecode_ExitMap(&DCtx);
6736 QCBORDecode_GetNext(&DCtx, &Item1);
6737 if(Item1.label.int64 != 23) {
6738 return 8000;
6739 }
6740 QCBORDecode_ExitMap(&DCtx);
6741 QCBORDecode_GetNext(&DCtx, &Item1);
6742 if(Item1.label.int64 != 3) {
6743 return 8000;
6744 }
6745 }
6746
6747 return 0;
6748}
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07006749
6750
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03006751int32_t EnterMapTest(void)
Laurence Lundbladebb87be22020-04-09 19:15:32 -07006752{
Laurence Lundbladef0499502020-08-01 11:55:57 -07006753 QCBORItem Item1;
Laurence Lundbladebb87be22020-04-09 19:15:32 -07006754 QCBORDecodeContext DCtx;
Laurence Lundbladef0499502020-08-01 11:55:57 -07006755 int32_t nReturn;
6756 QCBORError uErr;
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07006757
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08006758#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07006759 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spMapOfEmpty), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006760 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07006761
Laurence Lundbladef0499502020-08-01 11:55:57 -07006762
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006763 QCBORDecode_EnterArray(&DCtx, NULL); // Label 0
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07006764 QCBORDecode_ExitArray(&DCtx);
6765
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006766 QCBORDecode_EnterArray(&DCtx, NULL); // Label 9
6767 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07006768 QCBORDecode_ExitArray(&DCtx);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006769 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07006770 QCBORDecode_ExitArray(&DCtx);
6771 QCBORDecode_ExitArray(&DCtx);
6772
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006773 QCBORDecode_EnterMap(&DCtx, NULL); // Label 8
6774 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07006775 QCBORDecode_ExitArray(&DCtx);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006776 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07006777 QCBORDecode_ExitMap(&DCtx);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006778 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07006779 QCBORDecode_ExitArray(&DCtx);
6780 QCBORDecode_ExitMap(&DCtx);
6781
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006782 QCBORDecode_EnterMap(&DCtx, NULL); // Label4
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07006783 QCBORDecode_ExitMap(&DCtx);
6784
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006785 QCBORDecode_EnterArray(&DCtx, NULL); // Label 5
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07006786 QCBORDecode_ExitArray(&DCtx);
6787
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006788 QCBORDecode_EnterArray(&DCtx, NULL); // Label 6
6789 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07006790 QCBORDecode_ExitArray(&DCtx);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006791 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07006792 QCBORDecode_ExitArray(&DCtx);
6793 QCBORDecode_ExitArray(&DCtx);
6794
6795 QCBORDecode_ExitMap(&DCtx);
6796
6797 uErr = QCBORDecode_Finish(&DCtx);
6798 if(uErr != QCBOR_SUCCESS){
6799 return 3011;
6800 }
6801
Laurence Lundbladeec290b82024-06-10 11:10:54 -07006802#ifndef QCBOR_DISABLE_NON_INTEGER_LABELS
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07006803 (void)pValidMapIndefEncoded;
Laurence Lundbladef7a70bc2020-10-24 12:23:25 -07006804 nReturn = SpiffyDecodeBasicMap(UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapIndefEncoded));
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07006805 if(nReturn) {
6806 return nReturn + 20000;
Laurence Lundbladebb87be22020-04-09 19:15:32 -07006807 }
Laurence Lundbladeec290b82024-06-10 11:10:54 -07006808#endif /* ! QCBOR_DISABLE_NON_INTEGER_LABELS */
6809#endif /* ! QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08006810
Laurence Lundbladeec290b82024-06-10 11:10:54 -07006811#ifndef QCBOR_DISABLE_NON_INTEGER_LABELS
6812 QCBORItem ArrayItem;
Laurence Lundbladebb87be22020-04-09 19:15:32 -07006813
Laurence Lundbladef7a70bc2020-10-24 12:23:25 -07006814 nReturn = SpiffyDecodeBasicMap(UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded));
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006815 if(nReturn) {
6816 return nReturn;
6817 }
Laurence Lundbladebb87be22020-04-09 19:15:32 -07006818
Laurence Lundblade8ffdb742020-05-07 02:49:18 -07006819
Laurence Lundblade937ea812020-05-08 11:38:23 -07006820
Laurence Lundblade2f467f92020-10-09 17:50:11 -07006821 // These tests confirm the cursor is at the right place after entering
6822 // a map or array
Laurence Lundblade9b334962020-08-27 10:55:53 -07006823 const UsefulBufC ValidEncodedMap = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded);
Laurence Lundblade937ea812020-05-08 11:38:23 -07006824
6825 // Confirm cursor is at right place
Laurence Lundblade9b334962020-08-27 10:55:53 -07006826 QCBORDecode_Init(&DCtx, ValidEncodedMap, 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006827 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade937ea812020-05-08 11:38:23 -07006828 QCBORDecode_GetNext(&DCtx, &Item1);
6829 if(Item1.uDataType != QCBOR_TYPE_INT64) {
6830 return 2001;
6831 }
6832
6833
Laurence Lundblade9b334962020-08-27 10:55:53 -07006834 QCBORDecode_Init(&DCtx, ValidEncodedMap, 0);
Laurence Lundblade6f3f78e2020-08-31 13:09:14 -07006835 QCBORDecode_VGetNext(&DCtx, &Item1);
6836 QCBORDecode_VGetNext(&DCtx, &Item1);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006837 QCBORDecode_EnterArray(&DCtx, &ArrayItem);
6838 if(ArrayItem.uLabelType != QCBOR_TYPE_TEXT_STRING ||
6839 UsefulBuf_Compare(ArrayItem.label.string,
6840 UsefulBuf_FROM_SZ_LITERAL("an array of two strings"))) {
6841 return 2051;
6842 }
Laurence Lundblade937ea812020-05-08 11:38:23 -07006843 QCBORDecode_GetNext(&DCtx, &Item1);
6844 if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING) {
6845 return 2002;
6846 }
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006847 QCBORDecode_ExitArray(&DCtx);
6848 QCBORDecode_EnterMap(&DCtx, &ArrayItem);
6849 if(ArrayItem.uLabelType != QCBOR_TYPE_TEXT_STRING ||
6850 UsefulBuf_Compare(ArrayItem.label.string,
6851 UsefulBuf_FROM_SZ_LITERAL("map in a map"))) {
6852 return 2052;
6853 }
6854
Laurence Lundblade937ea812020-05-08 11:38:23 -07006855
Laurence Lundblade9b334962020-08-27 10:55:53 -07006856 QCBORDecode_Init(&DCtx, ValidEncodedMap, 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006857 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade64b607e2020-05-13 13:05:57 -07006858 QCBORDecode_GetNext(&DCtx, &Item1);
6859 QCBORDecode_GetNext(&DCtx, &Item1);
6860 QCBORDecode_GetNext(&DCtx, &Item1);
Laurence Lundblade937ea812020-05-08 11:38:23 -07006861 QCBORDecode_EnterMapFromMapSZ(&DCtx, "map in a map");
6862 QCBORDecode_GetNext(&DCtx, &Item1);
6863 if(Item1.uDataType != QCBOR_TYPE_BYTE_STRING) {
6864 return 2003;
6865 }
6866
Laurence Lundblade9b334962020-08-27 10:55:53 -07006867 QCBORDecode_Init(&DCtx, ValidEncodedMap, 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006868 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade937ea812020-05-08 11:38:23 -07006869 QCBORDecode_GetNext(&DCtx, &Item1);
6870 QCBORDecode_GetNext(&DCtx, &Item1);
6871 QCBORDecode_GetNext(&DCtx, &Item1);
6872 QCBORDecode_GetNext(&DCtx, &Item1);
6873 QCBORDecode_GetNext(&DCtx, &Item1);
6874 QCBORDecode_GetNext(&DCtx, &Item1);
6875 QCBORDecode_GetNext(&DCtx, &Item1);
6876 QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings");
6877 QCBORDecode_GetNext(&DCtx, &Item1);
6878 if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING) {
Laurence Lundblade64b607e2020-05-13 13:05:57 -07006879 return 2004;
Laurence Lundblade937ea812020-05-08 11:38:23 -07006880 }
6881
Laurence Lundblade9b334962020-08-27 10:55:53 -07006882 QCBORDecode_Init(&DCtx, ValidEncodedMap, 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006883 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade2b843b52020-06-16 20:51:03 -07006884 QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings");
6885 QCBORDecode_ExitArray(&DCtx);
6886 QCBORDecode_GetNext(&DCtx, &Item1);
6887 if(Item1.uDataType != QCBOR_TYPE_MAP && Item1.uLabelAlloc != QCBOR_TYPE_TEXT_STRING) {
6888 return 2006;
6889 }
6890 QCBORDecode_ExitMap(&DCtx);
6891 if(QCBORDecode_GetNext(&DCtx, &Item1) != QCBOR_ERR_NO_MORE_ITEMS) {
6892 return 2007;
6893 }
Laurence Lundbladeec290b82024-06-10 11:10:54 -07006894#endif /* !QCBOR_DISABLE_NON_INTEGER_LABELS */
Laurence Lundblade2b843b52020-06-16 20:51:03 -07006895
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07006896 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSimpleArray), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006897 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07006898 int64_t nDecodedInt2;
Laurence Lundbladeec290b82024-06-10 11:10:54 -07006899
6900 UsefulBufC String;
6901 QCBORDecode_GetTextStringInMapN(&DCtx, 88, &String);
6902 uErr = QCBORDecode_GetAndResetError(&DCtx);
6903 if(uErr != QCBOR_ERR_MAP_NOT_ENTERED){
6904 return 2009;
6905 }
6906#ifndef QCBOR_DISABLE_NON_INTEGER_LABELS
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07006907 QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2);
6908 uErr = QCBORDecode_GetAndResetError(&DCtx);
6909 if(uErr != QCBOR_ERR_MAP_NOT_ENTERED){
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07006910 return 2008;
6911 }
Laurence Lundbladeec290b82024-06-10 11:10:54 -07006912#endif /* ! QCBOR_DISABLE_NON_INTEGER_LABELS */
Laurence Lundblade937ea812020-05-08 11:38:23 -07006913
Laurence Lundbladee6f15112020-07-23 18:44:16 -07006914
6915 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEmptyMap), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006916 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundbladee6f15112020-07-23 18:44:16 -07006917 // This will fail because the map is empty.
6918 QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2);
6919 uErr = QCBORDecode_GetAndResetError(&DCtx);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006920 if(uErr != QCBOR_ERR_LABEL_NOT_FOUND){
Laurence Lundbladee6f15112020-07-23 18:44:16 -07006921 return 2010;
6922 }
6923 QCBORDecode_ExitMap(&DCtx);
6924 uErr = QCBORDecode_Finish(&DCtx);
6925 if(uErr != QCBOR_SUCCESS){
6926 return 2011;
6927 }
6928
6929
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08006930#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundbladee6f15112020-07-23 18:44:16 -07006931 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEmptyInDefinteLengthMap), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006932 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundbladee6f15112020-07-23 18:44:16 -07006933 // This will fail because the map is empty.
6934 QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2);
6935 uErr = QCBORDecode_GetAndResetError(&DCtx);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006936 if(uErr != QCBOR_ERR_LABEL_NOT_FOUND){
Laurence Lundblade085d7952020-07-24 10:26:30 -07006937 return 2012;
Laurence Lundbladee6f15112020-07-23 18:44:16 -07006938 }
6939 QCBORDecode_ExitMap(&DCtx);
6940 uErr = QCBORDecode_Finish(&DCtx);
6941 if(uErr != QCBOR_SUCCESS){
Laurence Lundblade085d7952020-07-24 10:26:30 -07006942 return 2013;
Laurence Lundbladee6f15112020-07-23 18:44:16 -07006943 }
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08006944#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
Laurence Lundbladee6f15112020-07-23 18:44:16 -07006945
Laurence Lundblade5f4e8712020-07-25 11:44:43 -07006946
6947 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spArrayOfEmpty), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006948 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade323f8a92020-09-06 19:43:09 -07006949 QCBORDecode_GetByteString(&DCtx, &String);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006950 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade5f4e8712020-07-25 11:44:43 -07006951 QCBORDecode_ExitMap(&DCtx);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006952 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade5f4e8712020-07-25 11:44:43 -07006953 QCBORDecode_ExitArray(&DCtx);
6954 QCBORDecode_GetInt64(&DCtx, &nDecodedInt2);
6955 QCBORDecode_ExitArray(&DCtx);
6956 uErr = QCBORDecode_Finish(&DCtx);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07006957 if(uErr != QCBOR_SUCCESS) {
Laurence Lundblade5f4e8712020-07-25 11:44:43 -07006958 return 2014;
6959 }
6960
Laurence Lundblade93d3f532020-09-28 21:09:12 -07006961 int64_t nInt;
6962 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spRecoverableMapErrors), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006963 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade37286c02022-09-03 10:05:02 -07006964#ifndef QCBOR_DISABLE_TAGS
Laurence Lundblade93d3f532020-09-28 21:09:12 -07006965 QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt);
Laurence Lundblade88e9db22020-11-02 03:56:33 -08006966 uErr = QCBORDecode_GetError(&DCtx);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07006967 if(uErr != QCBOR_ERR_TOO_MANY_TAGS) {
6968 return 2021;
6969 }
Laurence Lundblade88e9db22020-11-02 03:56:33 -08006970 if(QCBORDecode_GetNthTagOfLast(&DCtx, 0) != CBOR_TAG_INVALID64) {
6971 return 2121;
6972 }
6973 (void)QCBORDecode_GetAndResetError(&DCtx);
Laurence Lundblade37286c02022-09-03 10:05:02 -07006974#endif
Laurence Lundblade88e9db22020-11-02 03:56:33 -08006975
Laurence Lundblade93d3f532020-09-28 21:09:12 -07006976
Laurence Lundblade93d3f532020-09-28 21:09:12 -07006977 QCBORDecode_GetInt64InMapN(&DCtx, 0x03, &nInt);
6978 uErr = QCBORDecode_GetAndResetError(&DCtx);
6979 if(uErr != QCBOR_ERR_INT_OVERFLOW) {
6980 return 2023;
6981 }
6982
Laurence Lundblade37286c02022-09-03 10:05:02 -07006983#ifndef QCBOR_DISABLE_TAGS
Laurence Lundblade93d3f532020-09-28 21:09:12 -07006984 QCBORDecode_GetEpochDateInMapN(&DCtx, 0x04, QCBOR_TAG_REQUIREMENT_TAG, &nInt);
6985 uErr = QCBORDecode_GetAndResetError(&DCtx);
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006986 if(uErr != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_DATE_OVERFLOW)) {
Laurence Lundblade93d3f532020-09-28 21:09:12 -07006987 return 2024;
6988 }
Laurence Lundblade37286c02022-09-03 10:05:02 -07006989#endif
Laurence Lundblade93d3f532020-09-28 21:09:12 -07006990
6991 QCBORDecode_GetInt64InMapN(&DCtx, 0x05, &nInt);
6992 uErr = QCBORDecode_GetAndResetError(&DCtx);
6993 if(uErr != QCBOR_ERR_DUPLICATE_LABEL) {
6994 return 2025;
6995 }
6996
6997 QCBORDecode_GetInt64InMapN(&DCtx, 0x08, &nInt);
6998
6999 QCBORDecode_ExitMap(&DCtx);
7000 uErr = QCBORDecode_Finish(&DCtx);
7001 if(uErr != QCBOR_SUCCESS) {
7002 return 2026;
7003 }
7004
7005 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUnRecoverableMapError1), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07007006 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07007007 QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt);
7008 uErr = QCBORDecode_GetAndResetError(&DCtx);
7009 if(uErr != QCBOR_ERR_BAD_BREAK) {
7010 return 2030;
7011 }
7012
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08007013#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundblade93d3f532020-09-28 21:09:12 -07007014 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUnRecoverableMapError2), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07007015 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07007016 QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt);
7017 uErr = QCBORDecode_GetAndResetError(&DCtx);
7018 if(uErr != QCBOR_ERR_NO_MORE_ITEMS) {
7019 return 2031;
7020 }
7021
7022 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUnRecoverableMapError3), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07007023 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07007024 QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt);
7025 uErr = QCBORDecode_GetAndResetError(&DCtx);
7026 if(uErr != QCBOR_ERR_HIT_END) {
7027 return 2032;
7028 }
7029
7030 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUnRecoverableMapError4), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07007031 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07007032 QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt);
7033 uErr = QCBORDecode_GetAndResetError(&DCtx);
7034 if(uErr != QCBOR_ERR_ARRAY_DECODE_NESTING_TOO_DEEP) {
7035 return 2033;
7036 }
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08007037#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
7038
Laurence Lundbladeec290b82024-06-10 11:10:54 -07007039#ifndef QCBOR_DISABLE_NON_INTEGER_LABELS
Laurence Lundblade732e52d2021-02-22 20:11:01 -07007040 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0);
7041 QCBORDecode_VGetNextConsume(&DCtx, &Item1);
7042 if(Item1.uDataType != QCBOR_TYPE_MAP) {
7043 return 2401;
7044 }
7045 if(QCBORDecode_GetError(&DCtx)) {
7046 return 2402;
7047 }
7048
7049 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0);
7050 QCBORDecode_VGetNext(&DCtx, &Item1);
7051 if(Item1.uDataType != QCBOR_TYPE_MAP ||
7052 Item1.val.uCount != 3 ||
7053 Item1.uNextNestLevel != 1) {
7054 return 2403;
7055 }
7056 if(QCBORDecode_GetError(&DCtx)) {
7057 return 2404;
7058 }
7059 QCBORDecode_VGetNextConsume(&DCtx, &Item1);
7060 if(Item1.uDataType != QCBOR_TYPE_INT64 ||
7061 Item1.uNextNestLevel != 1 ||
7062 Item1.val.int64 != 42) {
7063 return 2405;
7064 }
7065 if(QCBORDecode_GetError(&DCtx)) {
7066 return 2406;
7067 }
7068 QCBORDecode_VGetNextConsume(&DCtx, &Item1);
7069 if(Item1.uDataType != QCBOR_TYPE_ARRAY ||
7070 Item1.uNestingLevel != 1 ||
7071 Item1.uNextNestLevel != 1 ||
7072 Item1.val.uCount != 2) {
7073 return 2407;
7074 }
7075 if(QCBORDecode_GetError(&DCtx)) {
7076 return 2408;
7077 }
7078 QCBORDecode_VGetNextConsume(&DCtx, &Item1);
7079 if(Item1.uDataType != QCBOR_TYPE_MAP ||
7080 Item1.uNestingLevel != 1 ||
7081 Item1.uNextNestLevel != 0 ||
7082 Item1.val.uCount != 4) {
7083 return 2409;
7084 }
7085 if(QCBORDecode_GetError(&DCtx)) {
7086 return 2410;
7087 }
Laurence Lundbladeec290b82024-06-10 11:10:54 -07007088#endif /* ! QCBOR_DISABLE_NON_INTEGER_LABELS */
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07007089
Laurence Lundbladeddebabe2020-11-22 11:32:17 -08007090 nReturn = DecodeNestedIterate();
7091
Laurence Lundblade63926052021-03-29 16:05:51 -07007092
7093 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(not_well_formed_submod_section), 0);
7094 QCBORDecode_EnterMap(&DCtx, NULL);
7095 QCBORDecode_EnterMapFromMapN(&DCtx, 20);
7096 if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_BAD_INT) {
7097 return 2500;
7098 }
7099
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07007100 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBadConsumeInput), 0);
7101 QCBORDecode_VGetNextConsume(&DCtx, &Item1);
7102 if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_NO_MORE_ITEMS) {
7103 return 2600;
7104 }
7105
Laurence Lundblade37286c02022-09-03 10:05:02 -07007106#ifndef QCBOR_DISABLE_TAGS
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07007107 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBadConsumeInput2), 0);
7108 QCBORDecode_VGetNextConsume(&DCtx, &Item1);
7109 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) {
7110 return 2700;
7111 }
Laurence Lundblade37286c02022-09-03 10:05:02 -07007112#endif
7113
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07007114
7115 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBadConsumeInput4), 0);
7116 QCBORDecode_VGetNextConsume(&DCtx, &Item1);
7117#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
7118 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) {
7119 return 2900;
7120 }
7121#else
7122 if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_INDEF_LEN_ARRAYS_DISABLED) {
7123 return 2901;
7124 }
7125#endif
7126
7127 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBadConsumeInput5), 0);
7128 QCBORDecode_VGetNextConsume(&DCtx, &Item1);
7129 if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_MAP_LABEL_TYPE) {
7130 return 3000;
7131 }
7132
Laurence Lundbladec5f45e42023-12-18 09:23:20 -07007133 nReturn = EnterMapCursorTest();
7134
Laurence Lundbladeddebabe2020-11-22 11:32:17 -08007135 return nReturn;
Laurence Lundblade9c905e82020-04-25 11:31:38 -07007136}
7137
7138
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007139struct NumberConversion {
7140 char *szDescription;
7141 UsefulBufC CBOR;
7142 int64_t nConvertedToInt64;
7143 QCBORError uErrorInt64;
7144 uint64_t uConvertToUInt64;
7145 QCBORError uErrorUint64;
7146 double dConvertToDouble;
7147 QCBORError uErrorDouble;
7148};
7149
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007150#ifndef QCBOR_DISABLE_EXP_AND_MANTISSA
7151#define EXP_AND_MANTISSA_ERROR(x) x
7152#else
7153#define EXP_AND_MANTISSA_ERROR(x) QCBOR_ERR_UNEXPECTED_TYPE
7154#endif
7155
7156
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07007157static const struct NumberConversion NumberConversions[] = {
Laurence Lundblade37286c02022-09-03 10:05:02 -07007158#ifndef QCBOR_DISABLE_TAGS
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007159 {
Laurence Lundblade4e808ba2022-12-29 12:45:20 -07007160 "Big float: INT64_MIN * 2e-1 to test handling of INT64_MIN",
7161 {(uint8_t[]){0xC5, 0x82, 0x20,
7162 0x3B, 0x7f, 0xff, 0xff, 0xff, 0xff, 0x0ff, 0xff, 0xff,
7163 }, 15},
7164 -4611686018427387904, /* INT64_MIN / 2 */
7165 EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS),
7166 0,
7167 EXP_AND_MANTISSA_ERROR(QCBOR_ERR_NUMBER_SIGN_CONVERSION),
7168 -4.6116860184273879E+18,
7169 FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS))
7170 },
7171 {
Laurence Lundblade784b54b2020-08-10 01:24:52 -07007172 "too large to fit into int64_t",
7173 {(uint8_t[]){0xc3, 0x48, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 10},
7174 0,
7175 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
7176 0,
7177 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
7178 ((double)INT64_MIN) + 1 ,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007179 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)
Laurence Lundblade784b54b2020-08-10 01:24:52 -07007180 },
7181 {
7182 "largest negative int that fits in int64_t",
7183 {(uint8_t[]){0xc3, 0x48, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, 10},
7184 INT64_MIN,
7185 QCBOR_SUCCESS,
7186 0,
7187 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
7188 (double)INT64_MIN,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007189 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)
Laurence Lundblade784b54b2020-08-10 01:24:52 -07007190 },
7191 {
Laurence Lundbladeda095972020-06-06 18:35:33 -07007192 "negative bignum -1",
7193 {(uint8_t[]){0xc3, 0x41, 0x00}, 3},
7194 -1,
7195 QCBOR_SUCCESS,
7196 0,
7197 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
7198 -1.0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007199 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)
Laurence Lundbladeda095972020-06-06 18:35:33 -07007200 },
7201 {
Laurence Lundblade9ab5abb2020-05-20 12:10:45 -07007202 "Decimal Fraction with positive bignum 257 * 10e3",
Laurence Lundblade887add82020-05-17 05:50:34 -07007203 {(uint8_t[]){0xC4, 0x82, 0x1B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03,
7204 0xC2, 0x42, 0x01, 0x01}, 15},
7205 257000,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007206 EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS),
Laurence Lundblade887add82020-05-17 05:50:34 -07007207 257000,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007208 EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS),
Laurence Lundblade887add82020-05-17 05:50:34 -07007209 257000.0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007210 FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS))
Laurence Lundblade887add82020-05-17 05:50:34 -07007211 },
7212 {
Laurence Lundbladeda095972020-06-06 18:35:33 -07007213 "bigfloat with negative bignum -258 * 2e3",
Laurence Lundblade887add82020-05-17 05:50:34 -07007214 {(uint8_t[]){0xC5, 0x82, 0x1B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03,
7215 0xC3, 0x42, 0x01, 0x01}, 15},
Laurence Lundbladeda095972020-06-06 18:35:33 -07007216 -2064,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007217 EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS),
Laurence Lundblade887add82020-05-17 05:50:34 -07007218 0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007219 EXP_AND_MANTISSA_ERROR(QCBOR_ERR_NUMBER_SIGN_CONVERSION),
Laurence Lundbladeda095972020-06-06 18:35:33 -07007220 -2064.0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007221 FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS))
Laurence Lundblade887add82020-05-17 05:50:34 -07007222 },
7223 {
Laurence Lundblade9ab5abb2020-05-20 12:10:45 -07007224 "bigfloat with positive bignum 257 * 2e3",
Laurence Lundblade887add82020-05-17 05:50:34 -07007225 {(uint8_t[]){0xC5, 0x82, 0x1B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03,
7226 0xC2, 0x42, 0x01, 0x01}, 15},
7227 2056,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007228 EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS),
Laurence Lundblade887add82020-05-17 05:50:34 -07007229 2056,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007230 EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS),
Laurence Lundblade887add82020-05-17 05:50:34 -07007231 2056.0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007232 FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS))
Laurence Lundblade887add82020-05-17 05:50:34 -07007233 },
7234 {
Laurence Lundbladeda095972020-06-06 18:35:33 -07007235 "negative bignum 0xc349010000000000000000 -18446744073709551617",
Laurence Lundblade887add82020-05-17 05:50:34 -07007236 {(uint8_t[]){0xc3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 11},
7237 0,
7238 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
7239 0,
7240 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
7241 -18446744073709551617.0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007242 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)
Laurence Lundblade887add82020-05-17 05:50:34 -07007243 },
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08007244#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
Laurence Lundblade887add82020-05-17 05:50:34 -07007245 {
Laurence Lundblade313b2862020-05-16 01:23:06 -07007246 "Positive bignum 0x01020304 indefinite length string",
7247 {(uint8_t[]){0xC2, 0x5f, 0x42, 0x01, 0x02, 0x41, 0x03, 0x41, 0x04, 0xff}, 10},
7248 0x01020304,
7249 QCBOR_SUCCESS,
7250 0x01020304,
7251 QCBOR_SUCCESS,
7252 16909060.0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007253 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)
Laurence Lundblade313b2862020-05-16 01:23:06 -07007254 },
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08007255#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
Laurence Lundblade313b2862020-05-16 01:23:06 -07007256 {
Laurence Lundblade887add82020-05-17 05:50:34 -07007257 "Decimal Fraction with neg bignum [9223372036854775807, -4759477275222530853137]",
Laurence Lundblade313b2862020-05-16 01:23:06 -07007258 {(uint8_t[]){0xC4, 0x82, 0x1B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
7259 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,}, 23},
7260 0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007261 EXP_AND_MANTISSA_ERROR(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW),
Laurence Lundblade313b2862020-05-16 01:23:06 -07007262 0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007263 EXP_AND_MANTISSA_ERROR(QCBOR_ERR_NUMBER_SIGN_CONVERSION),
Laurence Lundblade313b2862020-05-16 01:23:06 -07007264 -INFINITY,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007265 FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS))
Laurence Lundblade313b2862020-05-16 01:23:06 -07007266 },
7267 {
7268 "big float [9223372036854775806, 9223372036854775806]",
7269 {(uint8_t[]){0xC5, 0x82, 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE,
7270 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE}, 20},
7271 0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007272 EXP_AND_MANTISSA_ERROR(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW),
Laurence Lundblade313b2862020-05-16 01:23:06 -07007273 0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007274 EXP_AND_MANTISSA_ERROR(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW),
Laurence Lundblade313b2862020-05-16 01:23:06 -07007275 INFINITY,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007276 FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS))
Laurence Lundblade313b2862020-05-16 01:23:06 -07007277 },
7278 {
Laurence Lundblade983500d2020-05-14 11:49:34 -07007279 "Big float 3 * 2^^2",
7280 {(uint8_t[]){0xC5, 0x82, 0x02, 0x03}, 4},
7281 12,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007282 EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS),
Laurence Lundblade983500d2020-05-14 11:49:34 -07007283 12,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007284 EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS),
Laurence Lundblade983500d2020-05-14 11:49:34 -07007285 12.0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007286 FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS))
Laurence Lundblade983500d2020-05-14 11:49:34 -07007287 },
Laurence Lundblade983500d2020-05-14 11:49:34 -07007288 {
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007289 "Decimal fraction 3/10",
7290 {(uint8_t[]){0xC4, 0x82, 0x20, 0x03}, 4},
7291 0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007292 EXP_AND_MANTISSA_ERROR(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW),
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007293 0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007294 EXP_AND_MANTISSA_ERROR(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW),
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007295 0.30000000000000004,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007296 FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS))
Laurence Lundbladedfd49fc2020-09-01 14:17:16 -07007297 },
7298 {
Laurence Lundblade11fd78b2020-09-01 22:13:27 -07007299 "extreme pos bignum",
7300 {(uint8_t[]){0xc2, 0x59, 0x01, 0x90,
7301 // 50 rows of 8 is 400 digits.
7302 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7303 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7304 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7305 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7306 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7307 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7308 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7309 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7310 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7311 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7312 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7313 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7314 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7315 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7316 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7317 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7318 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7319 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7320 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7321 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7322 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7323 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7324 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7325 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7326 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7327 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7328 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7329 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7330 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7331 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7332 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7333 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7334 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7335 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7336 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7337 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7338 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7339 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7340 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7341 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7342 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7343 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7344 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7345 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7346 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7347 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7348 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7349 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7350 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
gtravisb787e82a2023-11-30 18:38:21 -08007351 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0},
Laurence Lundblade11fd78b2020-09-01 22:13:27 -07007352 404},
7353 0,
7354 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
7355 0,
7356 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
Laurence Lundblade11fd78b2020-09-01 22:13:27 -07007357 INFINITY,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007358 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS),
Laurence Lundblade11fd78b2020-09-01 22:13:27 -07007359 },
7360
7361 {
7362 "extreme neg bignum",
7363 {(uint8_t[]){0xc3, 0x59, 0x01, 0x90,
7364 // 50 rows of 8 is 400 digits.
7365 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7366 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7367 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7368 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7369 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7370 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7371 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7372 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7373 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7374 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7375 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7376 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7377 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7378 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7379 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7380 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7381 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7382 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7383 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7384 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7385 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7386 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7387 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7388 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7389 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7390 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7391 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7392 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7393 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7394 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7395 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7396 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7397 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7398 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7399 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7400 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7401 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7402 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7403 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7404 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7405 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7406 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7407 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7408 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7409 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7410 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7411 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7412 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7413 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
gtravisb787e82a2023-11-30 18:38:21 -08007414 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0},
Laurence Lundblade11fd78b2020-09-01 22:13:27 -07007415 404},
7416 0,
7417 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
7418 0,
7419 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
Laurence Lundblade11fd78b2020-09-01 22:13:27 -07007420 -INFINITY,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007421 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)
Laurence Lundblade11fd78b2020-09-01 22:13:27 -07007422 },
Laurence Lundblade51722fd2020-09-02 13:01:33 -07007423
7424 {
7425 "big float underflow [9223372036854775806, -9223372036854775806]",
7426 {(uint8_t[]){
7427 0xC5, 0x82,
7428 0x3B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE,
7429 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE}, 20},
Laurence Lundblade51722fd2020-09-02 13:01:33 -07007430 0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007431 EXP_AND_MANTISSA_ERROR(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW),
Laurence Lundblade51722fd2020-09-02 13:01:33 -07007432 0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007433 EXP_AND_MANTISSA_ERROR(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW),
Laurence Lundblade51722fd2020-09-02 13:01:33 -07007434 0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007435 FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS))
Laurence Lundblade51722fd2020-09-02 13:01:33 -07007436 },
7437
7438 {
7439 "bigfloat that evaluates to -INFINITY",
7440 {(uint8_t[]){
7441 0xC5, 0x82,
7442 0x1B, 0x70, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03,
7443 0xC3, 0x42, 0x01, 0x01}, 15},
Laurence Lundblade51722fd2020-09-02 13:01:33 -07007444 0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007445 EXP_AND_MANTISSA_ERROR(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW),
Laurence Lundblade51722fd2020-09-02 13:01:33 -07007446 0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007447 EXP_AND_MANTISSA_ERROR(QCBOR_ERR_NUMBER_SIGN_CONVERSION),
Laurence Lundblade51722fd2020-09-02 13:01:33 -07007448 -INFINITY,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007449 FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS))
Laurence Lundblade51722fd2020-09-02 13:01:33 -07007450 },
Laurence Lundblade37286c02022-09-03 10:05:02 -07007451 {
7452 "Positive bignum 0xffff",
7453 {(uint8_t[]){0xC2, 0x42, 0xff, 0xff}, 4},
7454 65536-1,
7455 QCBOR_SUCCESS,
7456 0xffff,
7457 QCBOR_SUCCESS,
7458 65535.0,
7459 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)
7460 },
7461#endif /* QCBOR_DISABLE_TAGS */
7462 {
7463 "Positive integer 18446744073709551615",
7464 {(uint8_t[]){0x1b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, 9},
7465 0,
7466 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
7467 18446744073709551615ULL,
7468 QCBOR_SUCCESS,
7469 18446744073709551615.0,
7470 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)
7471 },
7472
7473 {
7474 "Postive integer 0",
7475 {(uint8_t[]){0x0}, 1},
7476 0LL,
7477 QCBOR_SUCCESS,
7478 0ULL,
7479 QCBOR_SUCCESS,
7480 0.0,
7481 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)
7482 },
7483 {
7484 "Negative integer -18446744073709551616",
7485 {(uint8_t[]){0x3b, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }, 9},
7486 -9223372036854775807-1, // INT64_MIN
7487 QCBOR_SUCCESS,
7488 0ULL,
7489 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
7490 -9223372036854775808.0,
7491 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)
7492 },
7493 {
7494 "Double Floating point value 100.3",
7495 {(uint8_t[]){0xfb, 0x40, 0x59, 0x13, 0x33, 0x33, 0x33, 0x33, 0x33}, 9},
7496 100L,
7497 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS),
7498 100ULL,
7499 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS),
7500 100.3,
7501 FLOAT_ERR_CODE_NO_FLOAT(QCBOR_SUCCESS),
7502 },
7503 {
7504 "Floating point value NaN 0xfa7fc00000",
7505 {(uint8_t[]){0xfa, 0x7f, 0xc0, 0x00, 0x00}, 5},
7506 0,
7507 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_FLOAT_EXCEPTION),
7508 0,
7509 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_FLOAT_EXCEPTION),
7510 NAN,
7511 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS),
7512 },
7513 {
7514 "half-precision Floating point value -4",
7515 {(uint8_t[]){0xf9, 0xc4, 0x00}, 3},
7516 // Normal case with all enabled.
7517 -4,
7518 FLOAT_ERR_CODE_NO_HALF_PREC_NO_FLOAT_HW(QCBOR_SUCCESS),
7519 0,
7520 FLOAT_ERR_CODE_NO_HALF_PREC_NO_FLOAT_HW(QCBOR_ERR_NUMBER_SIGN_CONVERSION),
7521 -4.0,
7522 FLOAT_ERR_CODE_NO_HALF_PREC(QCBOR_SUCCESS)
7523 },
7524 {
7525 "+inifinity single precision",
7526 {(uint8_t[]){0xfa, 0x7f, 0x80, 0x00, 0x00}, 5},
7527 0,
7528 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_FLOAT_EXCEPTION),
7529 0,
7530 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW),
7531 INFINITY,
7532 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)
7533 },
7534
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007535};
Laurence Lundblade9c905e82020-04-25 11:31:38 -07007536
7537
7538
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08007539
7540static int32_t SetUpDecoder(QCBORDecodeContext *DCtx, UsefulBufC CBOR, UsefulBuf Pool)
7541{
7542 QCBORDecode_Init(DCtx, CBOR, QCBOR_DECODE_MODE_NORMAL);
7543#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
7544 if(QCBORDecode_SetMemPool(DCtx, Pool, 0)) {
7545 return 1;
7546 }
7547#else /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
7548 (void)Pool;
7549#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
7550 return 0;
7551}
7552
7553
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03007554int32_t IntegerConvertTest(void)
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007555{
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08007556 const int nNumTests = C_ARRAY_COUNT(NumberConversions,
7557 struct NumberConversion);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007558
Laurence Lundblade9ab5abb2020-05-20 12:10:45 -07007559 for(int nIndex = 0; nIndex < nNumTests; nIndex++) {
7560 const struct NumberConversion *pF = &NumberConversions[nIndex];
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07007561
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007562 // Set up the decoding context including a memory pool so that
7563 // indefinite length items can be checked
7564 QCBORDecodeContext DCtx;
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007565 UsefulBuf_MAKE_STACK_UB(Pool, 100);
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07007566
7567 /* ----- test conversion to int64_t ------ */
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08007568 if(SetUpDecoder(&DCtx, pF->CBOR, Pool)) {
7569 return (int32_t)(3333+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007570 }
7571
7572 int64_t nInt;
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007573 QCBORDecode_GetInt64ConvertAll(&DCtx, 0xffff, &nInt);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07007574 if(QCBORDecode_GetError(&DCtx) != pF->uErrorInt64) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07007575 return (int32_t)(2000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007576 }
7577 if(pF->uErrorInt64 == QCBOR_SUCCESS && pF->nConvertedToInt64 != nInt) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07007578 return (int32_t)(3000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007579 }
7580
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07007581 /* ----- test conversion to uint64_t ------ */
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08007582 if(SetUpDecoder(&DCtx, pF->CBOR, Pool)) {
7583 return (int32_t)(3333+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007584 }
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08007585
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007586 uint64_t uInt;
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007587 QCBORDecode_GetUInt64ConvertAll(&DCtx, 0xffff, &uInt);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07007588 if(QCBORDecode_GetError(&DCtx) != pF->uErrorUint64) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07007589 return (int32_t)(4000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007590 }
7591 if(pF->uErrorUint64 == QCBOR_SUCCESS && pF->uConvertToUInt64 != uInt) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07007592 return (int32_t)(5000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007593 }
7594
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07007595 /* ----- test conversion to double ------ */
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08007596 if(SetUpDecoder(&DCtx, pF->CBOR, Pool)) {
7597 return (int32_t)(3333+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007598 }
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007599
7600#ifndef USEFULBUF_DISABLE_ALL_FLOAT
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007601 double d;
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007602 QCBORDecode_GetDoubleConvertAll(&DCtx, 0xffff, &d);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07007603 if(QCBORDecode_GetError(&DCtx) != pF->uErrorDouble) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07007604 return (int32_t)(6000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007605 }
7606 if(pF->uErrorDouble == QCBOR_SUCCESS) {
7607 if(isnan(pF->dConvertToDouble)) {
Laurence Lundblade983500d2020-05-14 11:49:34 -07007608 // NaN's can't be compared for equality. A NaN is
7609 // never equal to anything including another NaN
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007610 if(!isnan(d)) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07007611 return (int32_t)(7000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007612 }
7613 } else {
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007614 if(pF->dConvertToDouble != d) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07007615 return (int32_t)(8000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007616 }
7617 }
7618 }
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007619#endif /* USEFULBUF_DISABLE_ALL_FLOAT */
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007620 }
7621
7622 return 0;
7623}
7624
Laurence Lundbladea8758502022-05-15 17:57:46 -07007625#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
7626
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03007627int32_t CBORTestIssue134(void)
David Navarro9123e5b2022-03-28 16:04:03 +02007628{
7629 QCBORDecodeContext DCtx;
7630 QCBORItem Item;
7631 QCBORError uCBORError;
7632 const uint8_t spTestIssue134[] = { 0x5F, 0x40, 0xFF };
Laurence Lundblade9c905e82020-04-25 11:31:38 -07007633
David Navarro9123e5b2022-03-28 16:04:03 +02007634 QCBORDecode_Init(&DCtx,
7635 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTestIssue134),
7636 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade97c61bf2020-05-02 11:24:06 -07007637
David Navarro9123e5b2022-03-28 16:04:03 +02007638 UsefulBuf_MAKE_STACK_UB(StringBuf, 200);
7639 QCBORDecode_SetMemPool(&DCtx, StringBuf, false);
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03007640
David Navarro9123e5b2022-03-28 16:04:03 +02007641 do {
7642 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
7643 } while (QCBOR_SUCCESS == uCBORError);
7644
7645 uCBORError = QCBORDecode_Finish(&DCtx);
7646
Laurence Lundblade11ea3612022-07-01 13:26:23 -07007647 return (int32_t)uCBORError;
David Navarro9123e5b2022-03-28 16:04:03 +02007648}
Laurence Lundblade97c61bf2020-05-02 11:24:06 -07007649
Laurence Lundbladea8758502022-05-15 17:57:46 -07007650#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
7651
Laurence Lundblade37286c02022-09-03 10:05:02 -07007652
7653
7654static const uint8_t spSequenceTestInput[] = {
7655 /* 1. The valid date string "1985-04-12" */
7656 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string
7657
7658 /* 2. */
7659 0x00,
7660
7661 /* 3. A valid epoch date, 1400000000; Tue, 13 May 2014 16:53:20 GMT */
7662 0x1a, 0x53, 0x72, 0x4E, 0x00,
7663
7664 /* 4. */
7665 0x62, 'h', 'i',
7666};
7667
7668
Laurence Lundbladee3553422020-05-02 11:11:17 -07007669int32_t CBORSequenceDecodeTests(void)
7670{
7671 QCBORDecodeContext DCtx;
Laurence Lundblade87495732021-02-26 10:05:55 -07007672 QCBORItem Item;
7673 QCBORError uCBORError;
7674 size_t uConsumed;
Laurence Lundbladee3553422020-05-02 11:11:17 -07007675
7676 // --- Test a sequence with extra bytes ---
Laurence Lundblade9b334962020-08-27 10:55:53 -07007677
Laurence Lundbladee3553422020-05-02 11:11:17 -07007678 QCBORDecode_Init(&DCtx,
Laurence Lundblade37286c02022-09-03 10:05:02 -07007679 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSequenceTestInput),
Laurence Lundbladee3553422020-05-02 11:11:17 -07007680 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade9b334962020-08-27 10:55:53 -07007681
Laurence Lundblade37286c02022-09-03 10:05:02 -07007682 // Get 1.
Laurence Lundbladee3553422020-05-02 11:11:17 -07007683 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
7684 if(uCBORError != QCBOR_SUCCESS) {
7685 return 1;
7686 }
Laurence Lundblade37286c02022-09-03 10:05:02 -07007687 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ) {
Laurence Lundbladee3553422020-05-02 11:11:17 -07007688 return 2;
7689 }
Laurence Lundbladec7114722020-08-13 05:11:40 -07007690
Laurence Lundblade87495732021-02-26 10:05:55 -07007691 uCBORError = QCBORDecode_PartialFinish(&DCtx, &uConsumed);
7692 if(uCBORError != QCBOR_ERR_EXTRA_BYTES ||
Laurence Lundblade37286c02022-09-03 10:05:02 -07007693 uConsumed != 11) {
7694 return 102;
7695 }
7696
7697 // Get 2.
7698 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
7699 if(uCBORError != QCBOR_SUCCESS) {
7700 return 66;
7701 }
7702
7703 uCBORError = QCBORDecode_PartialFinish(&DCtx, &uConsumed);
7704 if(uCBORError != QCBOR_ERR_EXTRA_BYTES ||
Laurence Lundblade87495732021-02-26 10:05:55 -07007705 uConsumed != 12) {
7706 return 102;
7707 }
7708
Laurence Lundblade37286c02022-09-03 10:05:02 -07007709 // Get 3.
Laurence Lundbladec7114722020-08-13 05:11:40 -07007710 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundbladee3553422020-05-02 11:11:17 -07007711 if(uCBORError != QCBOR_SUCCESS) {
7712 return 2;
7713 }
Laurence Lundblade37286c02022-09-03 10:05:02 -07007714 if(Item.uDataType != QCBOR_TYPE_INT64) {
Laurence Lundbladee3553422020-05-02 11:11:17 -07007715 return 3;
7716 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07007717
Laurence Lundbladee3553422020-05-02 11:11:17 -07007718 // A sequence can have stuff at the end that may
7719 // or may not be valid CBOR. The protocol decoder knows
7720 // when to stop by definition of the protocol, not
7721 // when the top-level map or array is ended.
7722 // Finish still has to be called to know that
7723 // maps and arrays (if there were any) were closed
7724 // off correctly. When called like this it
7725 // must return the error QCBOR_ERR_EXTRA_BYTES.
7726 uCBORError = QCBORDecode_Finish(&DCtx);
7727 if(uCBORError != QCBOR_ERR_EXTRA_BYTES) {
7728 return 4;
7729 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07007730
Laurence Lundbladee3553422020-05-02 11:11:17 -07007731 // --- Test an empty input ----
7732 uint8_t empty[1];
7733 UsefulBufC Empty = {empty, 0};
7734 QCBORDecode_Init(&DCtx,
7735 Empty,
7736 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade9b334962020-08-27 10:55:53 -07007737
Laurence Lundbladee3553422020-05-02 11:11:17 -07007738 uCBORError = QCBORDecode_Finish(&DCtx);
7739 if(uCBORError != QCBOR_SUCCESS) {
7740 return 5;
7741 }
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07007742
7743
Laurence Lundbladee3553422020-05-02 11:11:17 -07007744 // --- Sequence with unclosed indefinite length array ---
7745 static const uint8_t xx[] = {0x01, 0x9f, 0x02};
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07007746
Laurence Lundbladee3553422020-05-02 11:11:17 -07007747 QCBORDecode_Init(&DCtx,
7748 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(xx),
7749 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07007750
Laurence Lundbladee3553422020-05-02 11:11:17 -07007751 // Get the first item
7752 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
7753 if(uCBORError != QCBOR_SUCCESS) {
7754 return 7;
7755 }
7756 if(Item.uDataType != QCBOR_TYPE_INT64) {
7757 return 8;
7758 }
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07007759
Laurence Lundbladee3553422020-05-02 11:11:17 -07007760 // Get a second item
7761 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08007762#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundbladee3553422020-05-02 11:11:17 -07007763 if(uCBORError != QCBOR_SUCCESS) {
7764 return 9;
7765 }
7766 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
7767 return 10;
7768 }
7769
7770 // Try to finish before consuming all bytes to confirm
7771 // that the still-open error is returned.
7772 uCBORError = QCBORDecode_Finish(&DCtx);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07007773 if(uCBORError != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
Laurence Lundbladee3553422020-05-02 11:11:17 -07007774 return 11;
7775 }
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08007776#else /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
7777 if(uCBORError != QCBOR_ERR_INDEF_LEN_ARRAYS_DISABLED) {
7778 return 20;
7779 }
7780#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
Laurence Lundbladee3553422020-05-02 11:11:17 -07007781
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07007782
Laurence Lundbladee3553422020-05-02 11:11:17 -07007783 // --- Sequence with a closed indefinite length array ---
7784 static const uint8_t yy[] = {0x01, 0x9f, 0xff};
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07007785
Laurence Lundbladee3553422020-05-02 11:11:17 -07007786 QCBORDecode_Init(&DCtx,
7787 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(yy),
7788 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07007789
Laurence Lundbladee3553422020-05-02 11:11:17 -07007790 // Get the first item
7791 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
7792 if(uCBORError != QCBOR_SUCCESS) {
7793 return 12;
7794 }
7795 if(Item.uDataType != QCBOR_TYPE_INT64) {
7796 return 13;
7797 }
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07007798
Laurence Lundbladee3553422020-05-02 11:11:17 -07007799 // Get a second item
7800 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08007801#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
7802
Laurence Lundbladee3553422020-05-02 11:11:17 -07007803 if(uCBORError != QCBOR_SUCCESS) {
7804 return 14;
7805 }
7806 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
7807 return 15;
7808 }
7809
7810 // Try to finish before consuming all bytes to confirm
7811 // that the still-open error is returned.
7812 uCBORError = QCBORDecode_Finish(&DCtx);
7813 if(uCBORError != QCBOR_SUCCESS) {
7814 return 16;
7815 }
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08007816#else /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
7817 if(uCBORError != QCBOR_ERR_INDEF_LEN_ARRAYS_DISABLED) {
7818 return 20;
7819 }
7820#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
Laurence Lundbladee3553422020-05-02 11:11:17 -07007821
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07007822
Laurence Lundbladee3553422020-05-02 11:11:17 -07007823 return 0;
7824}
7825
Laurence Lundbladee15326f2020-06-15 15:50:23 -07007826
Laurence Lundblade70ecead2020-06-15 19:40:06 -07007827
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03007828int32_t IntToTests(void)
Laurence Lundbladee15326f2020-06-15 15:50:23 -07007829{
7830 int nErrCode;
7831 int32_t n32;
7832 int16_t n16;
7833 int8_t n8;
7834 uint32_t u32;
7835 uint16_t u16;
7836 uint8_t u8;
7837 uint64_t u64;
7838
7839 nErrCode = QCBOR_Int64ToInt32(1, &n32);
7840 if(nErrCode == -1 || n32 != 1) {
7841 return 1;
7842 }
7843
7844 nErrCode = QCBOR_Int64ToInt32((int64_t)INT32_MAX, &n32);
7845 if(nErrCode == -1 || n32 != INT32_MAX) {
7846 return 2;
7847 }
7848
7849 nErrCode = QCBOR_Int64ToInt32((int64_t)INT32_MIN, &n32);
7850 if(nErrCode == -1 || n32 != INT32_MIN) {
7851 return 3;
7852 }
7853
7854 nErrCode = QCBOR_Int64ToInt32(((int64_t)INT32_MAX)+1, &n32);
7855 if(nErrCode != -1) {
7856 return 4;
7857 }
7858
7859 nErrCode = QCBOR_Int64ToInt32(((int64_t)INT32_MIN)-1, &n32);
7860 if(nErrCode != -1) {
7861 return 5;
7862 }
7863
7864
7865 nErrCode = QCBOR_Int64ToInt16((int64_t)INT16_MAX, &n16);
7866 if(nErrCode == -1 || n16 != INT16_MAX) {
7867 return 6;
7868 }
7869
7870 nErrCode = QCBOR_Int64ToInt16((int64_t)INT16_MIN, &n16);
7871 if(nErrCode == -1 || n16 != INT16_MIN) {
7872 return 7;
7873 }
7874
7875 nErrCode = QCBOR_Int64ToInt16(1, &n16);
7876 if(nErrCode == -1 || n16 != 1) {
7877 return 8;
7878 }
7879
7880 nErrCode = QCBOR_Int64ToInt16(((int64_t)INT16_MAX)+1, &n16);
7881 if(nErrCode != -1) {
7882 return 9;
7883 }
7884
7885 nErrCode = QCBOR_Int64ToInt16(((int64_t)INT16_MIN)-1, &n16);
7886 if(nErrCode != -1) {
7887 return 10;
7888 }
7889
7890
7891 nErrCode = QCBOR_Int64ToInt8(1, &n8);
7892 if(nErrCode == -1 || n8 != 1) {
7893 return 11;
7894 }
7895
7896 nErrCode = QCBOR_Int64ToInt8((int64_t)INT8_MAX, &n8);
7897 if(nErrCode == -1 || n8 != INT8_MAX) {
7898 return 12;
7899 }
7900
7901 nErrCode = QCBOR_Int64ToInt8((int64_t)INT8_MIN, &n8);
7902 if(nErrCode == -1 || n8 != INT8_MIN) {
7903 return 13;
7904 }
7905
7906 nErrCode = QCBOR_Int64ToInt8(((int64_t)INT8_MAX)+1, &n8);
7907 if(nErrCode != -1) {
7908 return 14;
7909 }
7910
7911 nErrCode = QCBOR_Int64ToInt8(((int64_t)INT8_MIN)-1, &n8);
7912 if(nErrCode != -1) {
7913 return 15;
7914 }
7915
7916
7917 nErrCode = QCBOR_Int64ToUInt32(1, &u32);
7918 if(nErrCode == -1 || u32 != 1) {
7919 return 16;
7920 }
7921
7922 nErrCode = QCBOR_Int64ToUInt32((int64_t)UINT32_MAX, &u32);
7923 if(nErrCode == -1 || u32 != UINT32_MAX) {
7924 return 17;
7925 }
7926
7927 nErrCode = QCBOR_Int64ToUInt32((int64_t)0, &u32);
7928 if(nErrCode == -1 || u32 != 0) {
7929 return 18;
7930 }
7931
7932 nErrCode = QCBOR_Int64ToUInt32(((int64_t)UINT32_MAX)+1, &u32);
7933 if(nErrCode != -1) {
7934 return 19;
7935 }
7936
7937 nErrCode = QCBOR_Int64ToUInt32((int64_t)-1, &u32);
7938 if(nErrCode != -1) {
7939 return 20;
7940 }
7941
7942
7943 nErrCode = QCBOR_Int64UToInt16((int64_t)UINT16_MAX, &u16);
7944 if(nErrCode == -1 || u16 != UINT16_MAX) {
7945 return 21;
7946 }
7947
7948 nErrCode = QCBOR_Int64UToInt16((int64_t)0, &u16);
7949 if(nErrCode == -1 || u16 != 0) {
7950 return 22;
7951 }
7952
7953 nErrCode = QCBOR_Int64UToInt16(1, &u16);
7954 if(nErrCode == -1 || u16 != 1) {
7955 return 23;
7956 }
7957
7958 nErrCode = QCBOR_Int64UToInt16(((int64_t)UINT16_MAX)+1, &u16);
7959 if(nErrCode != -1) {
7960 return 24;
7961 }
7962
7963 nErrCode = QCBOR_Int64UToInt16((int64_t)-1, &u16);
7964 if(nErrCode != -1) {
7965 return 25;
7966 }
7967
7968
7969 nErrCode = QCBOR_Int64ToUInt8((int64_t)UINT8_MAX, &u8);
7970 if(nErrCode == -1 || u8 != UINT8_MAX) {
7971 return 26;
7972 }
7973
7974 nErrCode = QCBOR_Int64ToUInt8((int64_t)0, &u8);
7975 if(nErrCode == -1 || u8 != 0) {
7976 return 27;
7977 }
7978
7979 nErrCode = QCBOR_Int64ToUInt8(1, &u8);
7980 if(nErrCode == -1 || u8 != 1) {
7981 return 28;
7982 }
7983
7984 nErrCode = QCBOR_Int64ToUInt8(((int64_t)UINT16_MAX)+1, &u8);
7985 if(nErrCode != -1) {
7986 return 29;
7987 }
7988
7989 nErrCode = QCBOR_Int64ToUInt8((int64_t)-1, &u8);
7990 if(nErrCode != -1) {
7991 return 30;
7992 }
7993
7994
7995 nErrCode = QCBOR_Int64ToUInt64(1, &u64);
7996 if(nErrCode == -1 || u64 != 1) {
7997 return 31;
7998 }
7999
8000 nErrCode = QCBOR_Int64ToUInt64(INT64_MAX, &u64);
8001 if(nErrCode == -1 || u64 != INT64_MAX) {
8002 return 32;
8003 }
8004
8005 nErrCode = QCBOR_Int64ToUInt64((int64_t)0, &u64);
8006 if(nErrCode == -1 || u64 != 0) {
8007 return 33;
8008 }
8009
8010 nErrCode = QCBOR_Int64ToUInt64((int64_t)-1, &u64);
8011 if(nErrCode != -1) {
8012 return 34;
8013 }
8014
8015 return 0;
8016}
8017
Laurence Lundblade0750fc42020-06-20 21:02:34 -07008018
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07008019
8020
Laurence Lundblade0750fc42020-06-20 21:02:34 -07008021/*
8022A sequence with
8023 A wrapping bstr
8024 containing a map
8025 1
8026 2
8027 A wrapping bstr
8028 containing an array
8029 3
8030 wrapping bstr
8031 4
8032 5
8033 6
8034 array
8035 7
8036 8
Laurence Lundblade0750fc42020-06-20 21:02:34 -07008037 */
8038
Laurence Lundblade55013642020-09-23 05:39:22 -07008039static UsefulBufC EncodeBstrWrapTestData(UsefulBuf OutputBuffer)
Laurence Lundblade0750fc42020-06-20 21:02:34 -07008040{
Laurence Lundblade55013642020-09-23 05:39:22 -07008041 UsefulBufC Encoded;
Laurence Lundblade0750fc42020-06-20 21:02:34 -07008042 QCBOREncodeContext EC;
Laurence Lundblade55013642020-09-23 05:39:22 -07008043 QCBORError uErr;
Laurence Lundblade0750fc42020-06-20 21:02:34 -07008044
Laurence Lundblade55013642020-09-23 05:39:22 -07008045 QCBOREncode_Init(&EC, OutputBuffer);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07008046
8047 QCBOREncode_BstrWrap(&EC);
8048 QCBOREncode_OpenMap(&EC);
8049 QCBOREncode_AddInt64ToMapN(&EC, 100, 1);
8050 QCBOREncode_AddInt64ToMapN(&EC, 200, 2);
8051 QCBOREncode_CloseMap(&EC);
8052 QCBOREncode_BstrWrap(&EC);
8053 QCBOREncode_OpenArray(&EC);
8054 QCBOREncode_AddInt64(&EC, 3);
8055 QCBOREncode_BstrWrap(&EC);
8056 QCBOREncode_AddInt64(&EC, 4);
8057 QCBOREncode_CloseBstrWrap(&EC, NULL);
8058 QCBOREncode_AddInt64(&EC, 5);
8059 QCBOREncode_CloseArray(&EC);
8060 QCBOREncode_CloseBstrWrap(&EC, NULL);
8061 QCBOREncode_AddInt64(&EC, 6);
8062 QCBOREncode_CloseBstrWrap(&EC, NULL);
8063 QCBOREncode_OpenArray(&EC);
8064 QCBOREncode_AddInt64(&EC, 7);
8065 QCBOREncode_AddInt64(&EC, 8);
8066 QCBOREncode_CloseArray(&EC);
8067
8068 uErr = QCBOREncode_Finish(&EC, &Encoded);
Laurence Lundblade40a04322020-06-27 22:52:52 -07008069 if(uErr) {
8070 Encoded = NULLUsefulBufC;
8071 }
Laurence Lundblade0750fc42020-06-20 21:02:34 -07008072
8073 return Encoded;
8074}
8075
Laurence Lundbladecc7da412020-12-27 00:09:07 -08008076/* h'FF' */
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08008077static const uint8_t spBreakInByteString[] = {
8078 0x41, 0xff
8079};
8080
Laurence Lundblade0750fc42020-06-20 21:02:34 -07008081
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03008082int32_t EnterBstrTest(void)
Laurence Lundblade0750fc42020-06-20 21:02:34 -07008083{
Laurence Lundblade8510f8c2020-12-01 11:31:16 -08008084 UsefulBuf_MAKE_STACK_UB(OutputBuffer, 100);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07008085
8086 QCBORDecodeContext DC;
8087
Laurence Lundblade55013642020-09-23 05:39:22 -07008088 QCBORDecode_Init(&DC, EncodeBstrWrapTestData(OutputBuffer), 0);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07008089
Laurence Lundblade55013642020-09-23 05:39:22 -07008090 int64_t n1, n2, n3, n4, n5, n6, n7, n8;
Laurence Lundblade0750fc42020-06-20 21:02:34 -07008091
8092
Laurence Lundblade9b334962020-08-27 10:55:53 -07008093 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07008094 QCBORDecode_EnterMap(&DC, NULL);
Laurence Lundblade55013642020-09-23 05:39:22 -07008095 QCBORDecode_GetInt64InMapN(&DC, 100, &n1);
8096 QCBORDecode_GetInt64InMapN(&DC, 200, &n2);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07008097 QCBORDecode_ExitMap(&DC);
Laurence Lundblade9b334962020-08-27 10:55:53 -07008098 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07008099 QCBORDecode_EnterArray(&DC, NULL);
Laurence Lundblade55013642020-09-23 05:39:22 -07008100 QCBORDecode_GetInt64(&DC, &n3);
Laurence Lundblade9b334962020-08-27 10:55:53 -07008101 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
Laurence Lundblade55013642020-09-23 05:39:22 -07008102 QCBORDecode_GetInt64(&DC, &n4);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07008103 QCBORDecode_ExitBstrWrapped(&DC);
Laurence Lundblade55013642020-09-23 05:39:22 -07008104 QCBORDecode_GetInt64(&DC, &n5);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07008105 QCBORDecode_ExitArray(&DC);
8106 QCBORDecode_ExitBstrWrapped(&DC);
Laurence Lundblade55013642020-09-23 05:39:22 -07008107 QCBORDecode_GetInt64(&DC, &n6);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07008108 QCBORDecode_ExitBstrWrapped(&DC);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07008109 QCBORDecode_EnterArray(&DC, NULL);
Laurence Lundblade55013642020-09-23 05:39:22 -07008110 QCBORDecode_GetInt64(&DC, &n7);
8111 QCBORDecode_GetInt64(&DC, &n8);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07008112 QCBORDecode_ExitArray(&DC);
8113
8114 QCBORError uErr = QCBORDecode_Finish(&DC);
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08008115 if(uErr) {
8116 return (int32_t)uErr;
8117 }
Laurence Lundblade0750fc42020-06-20 21:02:34 -07008118
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08008119
8120 /* Enter and exit byte string wrapped CBOR that is bad. It has just a break.
8121 * Successful because no items are fetched from byte string.
8122 */
8123 QCBORDecode_Init(&DC,
8124 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBreakInByteString),
8125 0);
8126 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
8127 uErr = QCBORDecode_GetError(&DC);
8128 if(uErr) {
8129 return 100 + (int32_t)uErr;
8130 }
8131
8132 QCBORDecode_ExitBstrWrapped(&DC);
8133 uErr = QCBORDecode_GetError(&DC);
8134 if(uErr) {
8135 return 200 + (int32_t)uErr;
8136 }
8137
8138 /* Try to get item that is a break out of a byte string wrapped CBOR.
8139 * It fails because there should be no break.
8140 */
8141 QCBORDecode_Init(&DC,
8142 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBreakInByteString),
8143 0);
8144 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
8145 QCBORItem Item;
8146 uErr = QCBORDecode_GetNext(&DC, &Item);
8147 if(uErr != QCBOR_ERR_BAD_BREAK) {
8148 return 300 + (int32_t)uErr;
8149 }
8150
8151 return 0;
Laurence Lundblade0750fc42020-06-20 21:02:34 -07008152}
Laurence Lundblade37f46e52020-08-04 03:32:14 -07008153
8154
8155
8156
8157static const uint8_t spTaggedTypes[] = {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008158 0xb2,
Laurence Lundblade37f46e52020-08-04 03:32:14 -07008159
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07008160 // Date string
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008161 0x00,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07008162 0xc0, 0x74, 0x32, 0x30, 0x30, 0x33, 0x2D, 0x31, 0x32, 0x2D,
8163 0x31, 0x33, 0x54, 0x31, 0x38, 0x3A, 0x33, 0x30, 0x3A, 0x30,
8164 0x32, 0x5A,
Laurence Lundblade37f46e52020-08-04 03:32:14 -07008165
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008166 0x01,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07008167 0x74, 0x32, 0x30, 0x30, 0x33, 0x2D, 0x31, 0x32, 0x2D, 0x31,
8168 0x33, 0x54, 0x31, 0x38, 0x3A, 0x33, 0x30, 0x3A, 0x30, 0x32,
8169 0x5A,
Laurence Lundblade37f46e52020-08-04 03:32:14 -07008170
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07008171 // Bignum
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008172 10,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07008173 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
8174 0x09, 0x10,
Laurence Lundblade37f46e52020-08-04 03:32:14 -07008175
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008176 11,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07008177 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
8178 0x09, 0x10,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008179
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07008180 // URL
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008181 20,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07008182 0xd8, 0x20, 0x6f, 0x68, 0x74, 0x74, 0x70, 0x3A, 0x2F, 0x2F,
8183 0x63, 0x62, 0x6F, 0x72, 0x2E, 0x6D, 0x65, 0x2F,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008184
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07008185 21,
8186 0x6f, 0x68, 0x74, 0x74, 0x70, 0x3A, 0x2F, 0x2F, 0x63, 0x62,
8187 0x6F, 0x72, 0x2E, 0x6D, 0x65, 0x2F,
8188
8189 // B64
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008190 0x18, 0x1e,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07008191 0xd8, 0x22, 0x6c, 0x63, 0x47, 0x78, 0x6C, 0x59, 0x58, 0x4E,
8192 0x31, 0x63, 0x6D, 0x55, 0x75,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008193
8194 0x18, 0x1f,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07008195 0x6c, 0x63, 0x47, 0x78, 0x6C, 0x59, 0x58, 0x4E, 0x31, 0x63,
8196 0x6D, 0x55, 0x75,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008197
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07008198 // B64URL
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008199 0x18, 0x28,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07008200 0xd8, 0x21, 0x6c, 0x63, 0x47, 0x78, 0x6C, 0x59, 0x58, 0x4E,
8201 0x31, 0x63, 0x6D, 0x55, 0x75,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008202
8203 0x18, 0x29,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07008204 0x6c, 0x63, 0x47, 0x78, 0x6C, 0x59, 0x58, 0x4E, 0x31, 0x63,
8205 0x6D, 0x55, 0x75,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008206
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07008207 // Regex
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008208 0x18, 0x32,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07008209 0xd8, 0x23, 0x68, 0x31, 0x30, 0x30, 0x5C, 0x73, 0x2A, 0x6D,
8210 0x6B,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008211
8212 0x18, 0x33,
8213 0x68, 0x31, 0x30, 0x30, 0x5C, 0x73, 0x2A, 0x6D, 0x6B,
8214
8215 // MIME
8216 0x18, 0x3c,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07008217 0xd8, 0x24, 0x72, 0x4D, 0x49, 0x4D, 0x45, 0x2D, 0x56, 0x65,
8218 0x72, 0x73, 0x69, 0x6F, 0x6E, 0x3A, 0x20, 0x31, 0x2E, 0x30,
8219 0x0A,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008220
8221 0x18, 0x3d,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07008222 0x72, 0x4D, 0x49, 0x4D, 0x45, 0x2D, 0x56, 0x65, 0x72, 0x73,
8223 0x69, 0x6F, 0x6E, 0x3A, 0x20, 0x31, 0x2E, 0x30, 0x0A,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008224
8225 0x18, 0x3e,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07008226 0xd9, 0x01, 0x01, 0x52, 0x4D, 0x49, 0x4D, 0x45, 0x2D, 0x56,
8227 0x65, 0x72, 0x73, 0x69, 0x6F, 0x6E, 0x3A, 0x20, 0x31, 0x2E,
8228 0x30, 0x0A,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008229
8230 0x18, 0x3f,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07008231 0x52, 0x4D, 0x49, 0x4D, 0x45, 0x2D, 0x56, 0x65, 0x72, 0x73,
8232 0x69, 0x6F, 0x6E, 0x3A, 0x20, 0x31, 0x2E, 0x30, 0x0A,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008233
8234 // UUID
8235 0x18, 0x46,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07008236 0xd8, 0x25, 0x50, 0x53, 0x4D, 0x41, 0x52, 0x54, 0x43, 0x53,
8237 0x4C, 0x54, 0x54, 0x43, 0x46, 0x49, 0x43, 0x41, 0x32,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008238
8239 0x18, 0x47,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07008240 0x50, 0x53, 0x4D, 0x41, 0x52, 0x54, 0x43, 0x53, 0x4C, 0x54,
8241 0x54, 0x43, 0x46, 0x49, 0x43, 0x41, 0x32
Laurence Lundblade37f46e52020-08-04 03:32:14 -07008242};
8243
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03008244int32_t DecodeTaggedTypeTests(void)
Laurence Lundblade37f46e52020-08-04 03:32:14 -07008245{
8246 QCBORDecodeContext DC;
8247 QCBORError uErr;
8248
8249 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTaggedTypes), 0);
8250
8251 UsefulBufC String;
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008252 bool bNeg;
Laurence Lundblade37f46e52020-08-04 03:32:14 -07008253
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07008254 QCBORDecode_EnterMap(&DC, NULL);
Laurence Lundblade9b334962020-08-27 10:55:53 -07008255 QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008256 QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &String);
Laurence Lundblade37f46e52020-08-04 03:32:14 -07008257 if(QCBORDecode_GetError(&DC) != QCBOR_SUCCESS) {
8258 return 1;
8259 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07008260 QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade37f46e52020-08-04 03:32:14 -07008261 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_UNEXPECTED_TYPE) {
8262 return 2;
8263 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07008264 QCBORDecode_GetDateStringInMapN(&DC, 1, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade37f46e52020-08-04 03:32:14 -07008265 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_UNEXPECTED_TYPE) {
8266 return 3;
8267 }
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008268 QCBORDecode_GetDateStringInMapN(&DC, 1, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &String);
Laurence Lundblade9b334962020-08-27 10:55:53 -07008269 QCBORDecode_GetDateStringInMapN(&DC, 1, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade37f46e52020-08-04 03:32:14 -07008270 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
8271 return 4;
8272 }
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008273 QCBORDecode_GetDateStringInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07008274 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008275 return 5;
8276 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07008277
Laurence Lundblade9b334962020-08-27 10:55:53 -07008278 QCBORDecode_GetBignumInMapN(&DC, 10, QCBOR_TAG_REQUIREMENT_TAG, &String, &bNeg);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008279 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
8280 bNeg != false) {
8281 return 10;
8282 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07008283 QCBORDecode_GetBignumInMapN(&DC, 11, QCBOR_TAG_REQUIREMENT_TAG, &String, &bNeg);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008284 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
8285 bNeg != true) {
8286 return 11;
8287 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07008288 QCBORDecode_GetBignumInMapN(&DC, 11, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bNeg);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008289 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_UNEXPECTED_TYPE) {
8290 return 12;
8291 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07008292 QCBORDecode_GetBignumInMapN(&DC, 14, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bNeg);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07008293 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008294 return 13;
8295 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07008296 QCBORDecode_GetBignumInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bNeg);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07008297 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008298 return 14;
8299 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07008300
Laurence Lundblade9b334962020-08-27 10:55:53 -07008301 QCBORDecode_GetURIInMapN(&DC, 20, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008302 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
8303 return 20;
8304 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07008305 QCBORDecode_GetURIInMapN(&DC, 21, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008306 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
8307 return 21;
8308 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07008309 QCBORDecode_GetURIInMapN(&DC, 22, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07008310 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008311 return 22;
8312 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07008313 QCBORDecode_GetURIInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07008314 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008315 return 23;
8316 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07008317
Laurence Lundblade24bd7e12021-01-09 00:05:11 -08008318#ifndef QCBOR_DISABLE_UNCOMMON_TAGS
Laurence Lundblade9b334962020-08-27 10:55:53 -07008319 QCBORDecode_GetB64InMapN(&DC, 30, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008320 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
8321 return 30;
8322 }
Laurence Lundblade24bd7e12021-01-09 00:05:11 -08008323#endif
Laurence Lundblade9b334962020-08-27 10:55:53 -07008324 QCBORDecode_GetB64InMapN(&DC, 31, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008325 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
8326 return 31;
8327 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07008328 QCBORDecode_GetB64InMapN(&DC, 32, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07008329 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008330 return 32;
8331 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07008332 QCBORDecode_GetB64InMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07008333 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008334 return 33;
8335 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07008336
Laurence Lundblade24bd7e12021-01-09 00:05:11 -08008337#ifndef QCBOR_DISABLE_UNCOMMON_TAGS
Laurence Lundblade9b334962020-08-27 10:55:53 -07008338 QCBORDecode_GetB64URLInMapN(&DC, 40, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008339 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
8340 return 40;
8341 }
Laurence Lundblade24bd7e12021-01-09 00:05:11 -08008342#endif
Laurence Lundblade9b334962020-08-27 10:55:53 -07008343 QCBORDecode_GetB64URLInMapN(&DC, 41, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008344 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
8345 return 41;
8346 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07008347 QCBORDecode_GetB64URLInMapN(&DC, 42, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07008348 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008349 return 42;
8350 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07008351 QCBORDecode_GetB64URLInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07008352 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008353 return 43;
8354 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07008355
Laurence Lundblade24bd7e12021-01-09 00:05:11 -08008356#ifndef QCBOR_DISABLE_UNCOMMON_TAGS
Laurence Lundblade9b334962020-08-27 10:55:53 -07008357 QCBORDecode_GetRegexInMapN(&DC, 50, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008358 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
8359 return 50;
8360 }
Laurence Lundblade24bd7e12021-01-09 00:05:11 -08008361#endif
Laurence Lundblade9b334962020-08-27 10:55:53 -07008362 QCBORDecode_GetRegexInMapN(&DC, 51, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008363 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
8364 return 51;
8365 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07008366 QCBORDecode_GetRegexInMapN(&DC, 52, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07008367 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008368 return 52;
8369 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07008370 QCBORDecode_GetRegexInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07008371 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008372 return 53;
8373 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07008374
Laurence Lundblade24bd7e12021-01-09 00:05:11 -08008375#ifndef QCBOR_DISABLE_UNCOMMON_TAGS
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008376 // MIME
8377 bool bIsNot7Bit;
Laurence Lundblade9b334962020-08-27 10:55:53 -07008378 QCBORDecode_GetMIMEMessageInMapN(&DC, 60, QCBOR_TAG_REQUIREMENT_TAG, &String, &bIsNot7Bit);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008379 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
8380 bIsNot7Bit == true) {
8381 return 60;
8382 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07008383 QCBORDecode_GetMIMEMessageInMapN(&DC, 61, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bIsNot7Bit);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008384 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
8385 bIsNot7Bit == true) {
8386 return 61;
8387 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07008388 QCBORDecode_GetMIMEMessageInMapN(&DC, 62, QCBOR_TAG_REQUIREMENT_TAG, &String, &bIsNot7Bit);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008389 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
8390 bIsNot7Bit == false) {
8391 return 62;
8392 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07008393 QCBORDecode_GetMIMEMessageInMapN(&DC, 63, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bIsNot7Bit);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008394 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
8395 bIsNot7Bit == false) {
8396 return 63;
8397 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07008398 QCBORDecode_GetMIMEMessageInMapN(&DC, 64, QCBOR_TAG_REQUIREMENT_TAG, &String, &bNeg);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07008399 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008400 return 64;
8401 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07008402 QCBORDecode_GetMIMEMessageInMapSZ(&DC, "zzz", QCBOR_TAG_REQUIREMENT_TAG, &String, &bNeg);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07008403 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008404 return 65;
8405 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07008406
Laurence Lundblade24bd7e12021-01-09 00:05:11 -08008407
Laurence Lundblade9b334962020-08-27 10:55:53 -07008408 QCBORDecode_GetBinaryUUIDInMapN(&DC, 70, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008409 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
8410 return 70;
8411 }
Laurence Lundblade24bd7e12021-01-09 00:05:11 -08008412#endif /* #ifndef QCBOR_DISABLE_UNCOMMON_TAGS */
8413
Laurence Lundblade9b334962020-08-27 10:55:53 -07008414 QCBORDecode_GetBinaryUUIDInMapN(&DC, 71, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008415 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
8416 return 71;
8417 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07008418 QCBORDecode_GetBinaryUUIDInMapN(&DC, 72, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07008419 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008420 return 72;
8421 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07008422 QCBORDecode_GetBinaryUUIDInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07008423 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008424 return 73;
8425 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07008426
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07008427 // Improvement: add some more error test cases
8428
Laurence Lundblade37f46e52020-08-04 03:32:14 -07008429 QCBORDecode_ExitMap(&DC);
8430
8431 uErr = QCBORDecode_Finish(&DC);
8432 if(uErr != QCBOR_SUCCESS) {
8433 return 100;
8434 }
8435
8436 return 0;
8437}
Laurence Lundbladea4308a82020-10-03 18:08:57 -07008438
8439
8440
8441
8442/*
Laurence Lundbladecc7da412020-12-27 00:09:07 -08008443 [
8444 "aaaaaaaaaa",
8445 {}
8446 ]
Laurence Lundbladea4308a82020-10-03 18:08:57 -07008447 */
8448static const uint8_t spTooLarge1[] = {
8449 0x9f,
8450 0x6a, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61,
8451 0xa0,
8452 0xff
8453};
8454
8455/*
Laurence Lundbladecc7da412020-12-27 00:09:07 -08008456 [
8457 {
8458 0: "aaaaaaaaaa"
8459 }
8460 ]
Laurence Lundbladea4308a82020-10-03 18:08:57 -07008461 */
8462static const uint8_t spTooLarge2[] = {
8463 0x9f,
8464 0xa1,
8465 0x00,
8466 0x6a, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61,
8467 0xff
8468};
8469
8470/*
Laurence Lundbladecc7da412020-12-27 00:09:07 -08008471 h'A1006A61616161616161616161'
Laurence Lundbladea4308a82020-10-03 18:08:57 -07008472
Laurence Lundbladecc7da412020-12-27 00:09:07 -08008473 {
8474 0: "aaaaaaaaaa"
8475 }
Laurence Lundbladea4308a82020-10-03 18:08:57 -07008476 */
8477static const uint8_t spTooLarge3[] = {
8478 0x4d,
8479 0xa1,
8480 0x00,
8481 0x6a, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61,
8482};
8483
8484int32_t TooLargeInputTest(void)
8485{
8486 QCBORDecodeContext DC;
8487 QCBORError uErr;
8488 UsefulBufC String;
8489
8490 // These tests require a build with QCBOR_MAX_DECODE_INPUT_SIZE set
8491 // to 10 There's not really any way to test this error
8492 // condition. The error condition is not complex, so setting
8493 // QCBOR_MAX_DECODE_INPUT_SIZE gives an OK test.
8494
8495 // The input CBOR is only too large because the
8496 // QCBOR_MAX_DECODE_INPUT_SIZE is 10.
8497 //
8498 // This test is disabled for the normal test runs because of the
8499 // special build requirement.
8500
8501
8502 // Tests the start of a map being too large
8503 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTooLarge1), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07008504 QCBORDecode_EnterArray(&DC, NULL);
Laurence Lundbladea4308a82020-10-03 18:08:57 -07008505 QCBORDecode_GetTextString(&DC, &String);
8506 uErr = QCBORDecode_GetError(&DC);
8507 if(uErr != QCBOR_SUCCESS) {
8508 return 1;
8509 }
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07008510 QCBORDecode_EnterMap(&DC, NULL);
Laurence Lundbladea4308a82020-10-03 18:08:57 -07008511 uErr = QCBORDecode_GetError(&DC);
8512 if(uErr != QCBOR_ERR_INPUT_TOO_LARGE) {
8513 return 2;
8514 }
8515
8516 // Tests the end of a map being too large
8517 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTooLarge2), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07008518 QCBORDecode_EnterArray(&DC, NULL);
8519 QCBORDecode_EnterMap(&DC, NULL);
Laurence Lundbladea4308a82020-10-03 18:08:57 -07008520 uErr = QCBORDecode_GetError(&DC);
8521 if(uErr != QCBOR_SUCCESS) {
8522 return 3;
8523 }
8524 QCBORDecode_ExitMap(&DC);
8525 uErr = QCBORDecode_GetError(&DC);
8526 if(uErr != QCBOR_ERR_INPUT_TOO_LARGE) {
8527 return 4;
8528 }
8529
8530 // Tests the entire input CBOR being too large when processing bstr wrapping
8531 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTooLarge3), QCBOR_DECODE_MODE_NORMAL);
8532 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
8533 uErr = QCBORDecode_GetError(&DC);
8534 if(uErr != QCBOR_ERR_INPUT_TOO_LARGE) {
8535 return 5;
8536 }
8537
8538 return 0;
8539}
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07008540
8541
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08008542#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
8543
Laurence Lundblade37286c02022-09-03 10:05:02 -07008544/*
8545 An array of three map entries
8546 1) Indefinite length string label for indefinite lenght byte string
8547 2) Indefinite length string label for an integer
8548 3) Indefinite length string label for an indefinite-length negative big num
8549 */
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07008550static const uint8_t spMapWithIndefLenStrings[] = {
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08008551 0xa3,
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07008552 0x7f, 0x61, 'l', 0x64, 'a', 'b', 'e', 'l' , 0x61, '1', 0xff,
8553 0x5f, 0x42, 0x01, 0x02, 0x43, 0x03, 0x04, 0x05, 0xff,
8554 0x7f, 0x62, 'd', 'y', 0x61, 'm', 0x61, 'o', 0xff,
8555 0x03,
8556 0x7f, 0x62, 'l', 'a', 0x63, 'b', 'e', 'l', 0x61, '2', 0xff,
8557 0xc3,
8558 0x5f, 0x42, 0x00, 0x01, 0x42, 0x00, 0x01, 0x41, 0x01, 0xff,
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07008559};
8560
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03008561int32_t SpiffyIndefiniteLengthStringsTests(void)
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07008562{
8563 QCBORDecodeContext DCtx;
8564
8565 QCBORDecode_Init(&DCtx,
8566 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spMapWithIndefLenStrings),
8567 QCBOR_DECODE_MODE_NORMAL);
8568
Laurence Lundblade8510f8c2020-12-01 11:31:16 -08008569 UsefulBuf_MAKE_STACK_UB(StringBuf, 200);
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07008570 QCBORDecode_SetMemPool(&DCtx, StringBuf, false);
8571
8572 UsefulBufC ByteString;
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07008573 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07008574 QCBORDecode_GetByteStringInMapSZ(&DCtx, "label1", &ByteString);
Laurence Lundblade37286c02022-09-03 10:05:02 -07008575
8576#ifndef QCBOR_DISABLE_TAGS
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07008577 if(QCBORDecode_GetAndResetError(&DCtx)) {
8578 return 1;
8579 }
8580
8581 const uint8_t pExectedBytes[] = {0x01, 0x02, 0x03, 0x04, 0x05};
8582 if(UsefulBuf_Compare(ByteString, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pExectedBytes))) {
8583 return 2;
8584 }
8585
8586 uint64_t uInt;
8587 QCBORDecode_GetUInt64InMapSZ(&DCtx, "dymo", &uInt);
8588 if(QCBORDecode_GetAndResetError(&DCtx)) {
8589 return 3;
8590 }
8591 if(uInt != 3) {
8592 return 4;
8593 }
8594
Máté Tóth-Pálef5f07a2021-09-17 19:31:37 +02008595#ifndef USEFULBUF_DISABLE_ALL_FLOAT
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07008596 double uDouble;
8597 QCBORDecode_GetDoubleConvertAllInMapSZ(&DCtx,
8598 "label2",
8599 0xff,
8600 &uDouble);
Laurence Lundblade37286c02022-09-03 10:05:02 -07008601
Laurence Lundbladeb8e19aa2020-10-07 20:59:11 -07008602#ifndef QCBOR_DISABLE_FLOAT_HW_USE
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07008603 if(QCBORDecode_GetAndResetError(&DCtx)) {
8604 return 5;
8605 }
8606 if(uDouble != -16777474) {
8607 return 6;
8608 }
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08008609#else /* QCBOR_DISABLE_FLOAT_HW_USE */
Laurence Lundbladeb8e19aa2020-10-07 20:59:11 -07008610 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_HW_FLOAT_DISABLED) {
8611 return 7;
8612 }
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08008613#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
Máté Tóth-Pálef5f07a2021-09-17 19:31:37 +02008614#endif /* USEFULBUF_DISABLE_ALL_FLOAT */
Laurence Lundbladeb8e19aa2020-10-07 20:59:11 -07008615
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07008616 QCBORDecode_ExitMap(&DCtx);
8617
8618 if(QCBORDecode_Finish(&DCtx)) {
8619 return 99;
8620 }
8621
Laurence Lundblade37286c02022-09-03 10:05:02 -07008622#else /* QCBOR_DISABLE_TAGS */
8623 /* The big num in the input is a CBOR tag and you can't do
8624 * map lookups in a map with a tag so this test does very little
8625 * when tags are disabled. That is OK, the test coverage is still
8626 * good when they are not.
8627 */
8628 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_TAGS_DISABLED) {
8629 return 1002;
8630 }
8631#endif /*QCBOR_DISABLE_TAGS */
8632
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07008633 return 0;
8634}
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08008635#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
Laurence Lundblade2a26abb2020-11-05 19:06:54 -08008636
8637
Laurence Lundbladeec290b82024-06-10 11:10:54 -07008638#ifndef QCBOR_DISABLE_NON_INTEGER_LABELS
Laurence Lundbladecf41c522021-02-20 10:19:07 -07008639/*
8640 * An array of an integer and an array. The second array contains
8641 * a bstr-wrapped map.
8642 *
8643 * [7, [h'A36D6669... (see next lines) 73']]
8644 *
8645 * {"first integer": 42,
8646 * "an array of two strings": ["string1", "string2"],
8647 * "map in a map":
8648 * { "bytes 1": h'78787878',
8649 * "bytes 2": h'79797979',
8650 * "another int": 98,
8651 * "text 2": "lies, damn lies and statistics"
8652 * }
8653 * }
8654 */
Laurence Lundblade2a26abb2020-11-05 19:06:54 -08008655
Laurence Lundbladecf41c522021-02-20 10:19:07 -07008656static const uint8_t pValidWrappedMapEncoded[] = {
8657 0x82, 0x07, 0x81, 0x58, 0x97,
8658 0xa3, 0x6d, 0x66, 0x69, 0x72, 0x73, 0x74, 0x20, 0x69, 0x6e,
8659 0x74, 0x65, 0x67, 0x65, 0x72, 0x18, 0x2a, 0x77, 0x61, 0x6e,
8660 0x20, 0x61, 0x72, 0x72, 0x61, 0x79, 0x20, 0x6f, 0x66, 0x20,
8661 0x74, 0x77, 0x6f, 0x20, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67,
8662 0x73, 0x82, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x31,
8663 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x32, 0x6c, 0x6d,
8664 0x61, 0x70, 0x20, 0x69, 0x6e, 0x20, 0x61, 0x20, 0x6d, 0x61,
8665 0x70, 0xa4, 0x67, 0x62, 0x79, 0x74, 0x65, 0x73, 0x20, 0x31,
8666 0x44, 0x78, 0x78, 0x78, 0x78, 0x67, 0x62, 0x79, 0x74, 0x65,
8667 0x73, 0x20, 0x32, 0x44, 0x79, 0x79, 0x79, 0x79, 0x6b, 0x61,
8668 0x6e, 0x6f, 0x74, 0x68, 0x65, 0x72, 0x20, 0x69, 0x6e, 0x74,
8669 0x18, 0x62, 0x66, 0x74, 0x65, 0x78, 0x74, 0x20, 0x32, 0x78,
8670 0x1e, 0x6c, 0x69, 0x65, 0x73, 0x2c, 0x20, 0x64, 0x61, 0x6d,
8671 0x6e, 0x20, 0x6c, 0x69, 0x65, 0x73, 0x20, 0x61, 0x6e, 0x64,
8672 0x20, 0x73, 0x74, 0x61, 0x74, 0x69, 0x73, 0x74, 0x69, 0x63,
8673 0x73
8674};
8675
8676#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
8677
8678/* As above, but the arrays are indefinite length */
8679static const uint8_t pValidIndefWrappedMapEncoded[] = {
8680 0x9f, 0x07, 0x9f, 0x58, 0x97,
8681 0xa3, 0x6d, 0x66, 0x69, 0x72, 0x73, 0x74, 0x20, 0x69, 0x6e,
8682 0x74, 0x65, 0x67, 0x65, 0x72, 0x18, 0x2a, 0x77, 0x61, 0x6e,
8683 0x20, 0x61, 0x72, 0x72, 0x61, 0x79, 0x20, 0x6f, 0x66, 0x20,
8684 0x74, 0x77, 0x6f, 0x20, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67,
8685 0x73, 0x82, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x31,
8686 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x32, 0x6c, 0x6d,
8687 0x61, 0x70, 0x20, 0x69, 0x6e, 0x20, 0x61, 0x20, 0x6d, 0x61,
8688 0x70, 0xa4, 0x67, 0x62, 0x79, 0x74, 0x65, 0x73, 0x20, 0x31,
8689 0x44, 0x78, 0x78, 0x78, 0x78, 0x67, 0x62, 0x79, 0x74, 0x65,
8690 0x73, 0x20, 0x32, 0x44, 0x79, 0x79, 0x79, 0x79, 0x6b, 0x61,
8691 0x6e, 0x6f, 0x74, 0x68, 0x65, 0x72, 0x20, 0x69, 0x6e, 0x74,
8692 0x18, 0x62, 0x66, 0x74, 0x65, 0x78, 0x74, 0x20, 0x32, 0x78,
8693 0x1e, 0x6c, 0x69, 0x65, 0x73, 0x2c, 0x20, 0x64, 0x61, 0x6d,
8694 0x6e, 0x20, 0x6c, 0x69, 0x65, 0x73, 0x20, 0x61, 0x6e, 0x64,
8695 0x20, 0x73, 0x74, 0x61, 0x74, 0x69, 0x73, 0x74, 0x69, 0x63,
8696 0x73,
8697 0xff, 0xff
8698};
Laurence Lundbladeec290b82024-06-10 11:10:54 -07008699#endif /* ! QCBOR_DISABLE_NON_INTEGER_LABELS */
Laurence Lundbladecf41c522021-02-20 10:19:07 -07008700
8701static const uint8_t pWithEmptyMap[] = {0x82, 0x18, 0x64, 0xa0};
8702
Laurence Lundbladeec290b82024-06-10 11:10:54 -07008703
Laurence Lundbladecf41c522021-02-20 10:19:07 -07008704#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
8705static const uint8_t pWithEmptyMapInDef[] = {0x9f, 0x18, 0x64, 0xbf, 0xff, 0xff};
8706#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
8707
8708#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
Laurence Lundblade37286c02022-09-03 10:05:02 -07008709/*
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08008710 * An array of one that contains a byte string that is an indefinite
8711 * length string that CBOR wraps an array of three numbers [42, 43,
8712 * 44]. The byte string is an implicit tag 24.
8713 *
8714 * [
8715 * (_ h'83', h'18', h'2A182B', h'182C')
8716 * ]
Laurence Lundblade37286c02022-09-03 10:05:02 -07008717 */
Laurence Lundbladecf41c522021-02-20 10:19:07 -07008718static const uint8_t pWrappedByIndefiniteLength[] = {
8719 0x81,
Laurence Lundbladecf41c522021-02-20 10:19:07 -07008720 0x5f,
8721 0x41, 0x83,
8722 0x41, 0x18,
8723 0x43, 0x2A, 0x18, 0x2B,
8724 0x42, 0x18, 0x2C,
8725 0xff
8726};
8727#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
8728
Laurence Lundbladeec290b82024-06-10 11:10:54 -07008729#endif
Laurence Lundbladecf41c522021-02-20 10:19:07 -07008730
Laurence Lundbladeec290b82024-06-10 11:10:54 -07008731#ifndef QCBOR_DISABLE_NON_INTEGER_LABELS
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03008732int32_t PeekAndRewindTest(void)
Laurence Lundblade2a26abb2020-11-05 19:06:54 -08008733{
8734 QCBORItem Item;
8735 QCBORError nCBORError;
8736 QCBORDecodeContext DCtx;
8737
Laurence Lundbladeec290b82024-06-10 11:10:54 -07008738 // Improvement: rework this test to use only integer labels.
8739
Laurence Lundblade2a26abb2020-11-05 19:06:54 -08008740 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0);
8741
8742 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
8743 return 100+(int32_t)nCBORError;
8744 }
8745 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
8746 return 200;
8747 }
8748
Laurence Lundblade3427dee2021-06-20 11:11:24 -07008749 QCBORDecode_VPeekNext(&DCtx, &Item);
8750 if((nCBORError = QCBORDecode_GetError(&DCtx))) {
8751 return 150+(int32_t)nCBORError;
8752 }
8753 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
8754 return 250;
8755 }
8756
Laurence Lundblade2a26abb2020-11-05 19:06:54 -08008757 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
8758 return (int32_t)nCBORError;
8759 }
8760 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
8761 return 300;
8762 }
8763
8764 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
8765 return 400 + (int32_t)nCBORError;
8766 }
8767 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
8768 return 500;
8769 }
8770
8771 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8772 return (int32_t)nCBORError;
8773 }
8774 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
8775 return 600;
8776 }
8777
8778 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
8779 return 900 + (int32_t)nCBORError;
8780 }
8781 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
8782 Item.uDataType != QCBOR_TYPE_INT64 ||
8783 Item.val.int64 != 42 ||
8784 Item.uDataAlloc ||
8785 Item.uLabelAlloc ||
8786 UsefulBufCompareToSZ(Item.label.string, "first integer")) {
8787 return 1000;
8788 }
8789
8790 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8791 return 1100 + (int32_t)nCBORError;
8792 }
8793
8794 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
8795 Item.uDataType != QCBOR_TYPE_INT64 ||
8796 Item.val.int64 != 42 ||
8797 Item.uDataAlloc ||
8798 Item.uLabelAlloc ||
8799 UsefulBufCompareToSZ(Item.label.string, "first integer")) {
8800 return 1200;
8801 }
8802
8803
8804 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8805 return 1300 + (int32_t)nCBORError;
8806 }
8807 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
8808 Item.uDataAlloc ||
8809 Item.uLabelAlloc ||
8810 UsefulBufCompareToSZ(Item.label.string, "an array of two strings") ||
8811 Item.uDataType != QCBOR_TYPE_ARRAY ||
Laurence Lundbladecf41c522021-02-20 10:19:07 -07008812 Item.val.uCount != 2) {
Laurence Lundblade2a26abb2020-11-05 19:06:54 -08008813 return 1400;
Laurence Lundbladecf41c522021-02-20 10:19:07 -07008814 }
Laurence Lundblade2a26abb2020-11-05 19:06:54 -08008815
8816 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8817 return 1500 + (int32_t)nCBORError;
8818 }
8819 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
8820 Item.uDataAlloc ||
8821 Item.uLabelAlloc ||
8822 UsefulBufCompareToSZ(Item.val.string, "string1")) {
8823 return 1600;
8824 }
8825
8826 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
8827 return 1700 + (int32_t)nCBORError;
8828 }
8829 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
8830 Item.uDataAlloc ||
8831 Item.uLabelAlloc ||
8832 UsefulBufCompareToSZ(Item.val.string, "string2")) {
8833 return 1800;
8834 }
8835
8836 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
8837 return (int32_t)nCBORError;
8838 }
8839 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
8840 Item.uDataAlloc ||
8841 Item.uLabelAlloc ||
8842 UsefulBufCompareToSZ(Item.val.string, "string2")) {
8843 return 1900;
8844 }
8845
8846 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8847 return (int32_t)nCBORError;
8848 }
8849 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
8850 Item.uDataAlloc ||
8851 Item.uLabelAlloc ||
8852 UsefulBufCompareToSZ(Item.val.string, "string2")) {
8853 return 2000;
8854 }
8855
8856
8857 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8858 return 2100 + (int32_t)nCBORError;
8859 }
8860 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
8861 Item.uDataAlloc ||
8862 Item.uLabelAlloc ||
8863 UsefulBufCompareToSZ(Item.label.string, "map in a map") ||
8864 Item.uDataType != QCBOR_TYPE_MAP ||
8865 Item.val.uCount != 4) {
8866 return 2100;
8867 }
8868
8869 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8870 return 2200 + (int32_t)nCBORError;
8871 }
8872 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
8873 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("bytes 1"))||
8874 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
8875 Item.uDataAlloc ||
8876 Item.uLabelAlloc ||
8877 UsefulBufCompareToSZ(Item.val.string, "xxxx")) {
8878 return 2300;
8879 }
8880
8881 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
8882 return 2400 + (int32_t)nCBORError;
8883 }
8884 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
8885 UsefulBufCompareToSZ(Item.label.string, "bytes 2") ||
8886 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
8887 Item.uDataAlloc ||
8888 Item.uLabelAlloc ||
8889 UsefulBufCompareToSZ(Item.val.string, "yyyy")) {
8890 return 2500;
8891 }
8892
8893 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8894 return 2600 + (int32_t)nCBORError;
8895 }
8896 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
8897 UsefulBufCompareToSZ(Item.label.string, "bytes 2") ||
8898 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
8899 Item.uDataAlloc ||
8900 Item.uLabelAlloc ||
8901 UsefulBufCompareToSZ(Item.val.string, "yyyy")) {
8902 return 2700;
8903 }
8904
8905 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8906 return 2800 + (int32_t)nCBORError;
8907 }
8908 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
8909 Item.uDataAlloc ||
8910 Item.uLabelAlloc ||
8911 UsefulBufCompareToSZ(Item.label.string, "another int") ||
8912 Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundbladecf41c522021-02-20 10:19:07 -07008913 Item.val.int64 != 98) {
Laurence Lundblade2a26abb2020-11-05 19:06:54 -08008914 return 2900;
Laurence Lundbladecf41c522021-02-20 10:19:07 -07008915 }
Laurence Lundblade2a26abb2020-11-05 19:06:54 -08008916
8917 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
8918 return 3000 + (int32_t)nCBORError;
8919 }
8920 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
8921 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("text 2"))||
8922 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
8923 Item.uDataAlloc ||
8924 Item.uLabelAlloc ||
8925 UsefulBufCompareToSZ(Item.val.string, "lies, damn lies and statistics")) {
8926 return 3100;
8927 }
8928
8929 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8930 return 3200 + (int32_t)nCBORError;
8931 }
8932 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
8933 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("text 2"))||
8934 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
8935 Item.uDataAlloc ||
8936 Item.uLabelAlloc ||
8937 UsefulBufCompareToSZ(Item.val.string, "lies, damn lies and statistics")) {
8938 return 3300;
8939 }
8940
Laurence Lundblade3427dee2021-06-20 11:11:24 -07008941 nCBORError = QCBORDecode_PeekNext(&DCtx, &Item);
8942 if(nCBORError != QCBOR_ERR_NO_MORE_ITEMS) {
8943 return 3300 + (int32_t)nCBORError;
8944 }
8945
8946 QCBORDecode_VPeekNext(&DCtx, &Item);
8947 nCBORError = QCBORDecode_GetError(&DCtx);
8948 if(nCBORError != QCBOR_ERR_NO_MORE_ITEMS) {
8949 return 3400 + (int32_t)nCBORError;
8950 }
8951
8952 QCBORDecode_VPeekNext(&DCtx, &Item);
8953 nCBORError = QCBORDecode_GetError(&DCtx);
8954 if(nCBORError != QCBOR_ERR_NO_MORE_ITEMS) {
8955 return 3500 + (int32_t)nCBORError;
8956 }
Laurence Lundbladecf41c522021-02-20 10:19:07 -07008957
8958
8959 // Rewind to top level after entering several maps
8960 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0);
8961
8962 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8963 return (int32_t)nCBORError;
8964 }
8965 if(Item.uDataType != QCBOR_TYPE_MAP ||
8966 Item.val.uCount != 3) {
8967 return 400;
8968 }
8969
8970 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8971 return 4000+(int32_t)nCBORError;
8972 }
8973
8974 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
8975 Item.uDataType != QCBOR_TYPE_INT64 ||
8976 Item.val.int64 != 42 ||
8977 Item.uDataAlloc ||
8978 Item.uLabelAlloc ||
8979 UsefulBufCompareToSZ(Item.label.string, "first integer")) {
8980 return 4100;
8981 }
8982
8983 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8984 return 4100+(int32_t)nCBORError;
8985 }
8986 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
8987 Item.uDataAlloc ||
8988 Item.uLabelAlloc ||
8989 UsefulBufCompareToSZ(Item.label.string, "an array of two strings") ||
8990 Item.uDataType != QCBOR_TYPE_ARRAY ||
8991 Item.val.uCount != 2) {
8992 return 4200;
8993 }
8994
8995 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8996 return 4200+(int32_t)nCBORError;
8997 }
8998 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
8999 Item.uDataAlloc ||
9000 Item.uLabelAlloc ||
9001 UsefulBufCompareToSZ(Item.val.string, "string1")) {
9002 return 4300;
9003 }
9004
9005 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
9006 return 4300+(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 4400;
9013 }
9014
9015 QCBORDecode_Rewind(&DCtx);
9016
9017 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
9018 return 4400+(int32_t)nCBORError;
9019 }
9020 if(Item.uDataType != QCBOR_TYPE_MAP ||
9021 Item.val.uCount != 3) {
9022 return 4500;
9023 }
9024
9025 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
9026 return (int32_t)nCBORError;
9027 }
9028
9029 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
9030 Item.uDataType != QCBOR_TYPE_INT64 ||
9031 Item.val.int64 != 42 ||
9032 Item.uDataAlloc ||
9033 Item.uLabelAlloc ||
9034 UsefulBufCompareToSZ(Item.label.string, "first integer")) {
9035 return 4600;
9036 }
9037
9038 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
9039 return (int32_t)nCBORError;
9040 }
9041 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
9042 Item.uDataAlloc ||
9043 Item.uLabelAlloc ||
9044 UsefulBufCompareToSZ(Item.label.string, "an array of two strings") ||
9045 Item.uDataType != QCBOR_TYPE_ARRAY ||
9046 Item.val.uCount != 2) {
9047 return 4700;
9048 }
9049
9050 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
9051 return (int32_t)nCBORError;
9052 }
9053 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
9054 Item.uDataAlloc ||
9055 Item.uLabelAlloc ||
9056 UsefulBufCompareToSZ(Item.val.string, "string1")) {
9057 return 4800;
9058 }
9059
9060 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
9061 return 4900+(int32_t)nCBORError;
9062 }
9063 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
9064 Item.uDataAlloc ||
9065 Item.uLabelAlloc ||
9066 UsefulBufCompareToSZ(Item.val.string, "string2")) {
9067 return 5000;
9068 }
9069
9070
9071 // Rewind an entered map
9072 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0);
9073
9074 QCBORDecode_EnterMap(&DCtx, NULL);
9075
9076 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
9077 return 5100+(int32_t)nCBORError;
9078 }
9079
9080 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
9081 Item.uDataType != QCBOR_TYPE_INT64 ||
9082 Item.val.int64 != 42 ||
9083 Item.uDataAlloc ||
9084 Item.uLabelAlloc ||
9085 UsefulBufCompareToSZ(Item.label.string, "first integer")) {
9086 return 5200;
9087 }
9088
9089 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
9090 return 5200+(int32_t)nCBORError;
9091 }
9092 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
9093 Item.uDataAlloc ||
9094 Item.uLabelAlloc ||
9095 UsefulBufCompareToSZ(Item.label.string, "an array of two strings") ||
9096 Item.uDataType != QCBOR_TYPE_ARRAY ||
9097 Item.val.uCount != 2) {
9098 return -5300;
9099 }
9100
9101 QCBORDecode_Rewind(&DCtx);
9102
9103 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
9104 return 5300+(int32_t)nCBORError;
9105 }
9106
9107 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
9108 Item.uDataType != QCBOR_TYPE_INT64 ||
9109 Item.val.int64 != 42 ||
9110 Item.uDataAlloc ||
9111 Item.uLabelAlloc ||
9112 UsefulBufCompareToSZ(Item.label.string, "first integer")) {
9113 return 5400;
9114 }
9115
9116 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
9117 return 5400+(int32_t)nCBORError;
9118 }
9119 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
9120 Item.uDataAlloc ||
9121 Item.uLabelAlloc ||
9122 UsefulBufCompareToSZ(Item.label.string, "an array of two strings") ||
9123 Item.uDataType != QCBOR_TYPE_ARRAY ||
9124 Item.val.uCount != 2) {
9125 return 5500;
9126 }
9127
9128
9129 // Rewind and entered array inside an entered map
9130 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0);
9131
9132 QCBORDecode_EnterMap(&DCtx, NULL);
9133
9134 QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings");
9135
9136 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
9137 return 5600+(int32_t)nCBORError;
9138 }
9139 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
9140 Item.uDataAlloc ||
9141 Item.uLabelAlloc ||
9142 UsefulBufCompareToSZ(Item.val.string, "string1")) {
9143 return 5700;
9144 }
9145
9146 QCBORDecode_Rewind(&DCtx);
9147
9148 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
9149 return 5700+(int32_t)nCBORError;
9150 }
9151 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
9152 Item.uDataAlloc ||
9153 Item.uLabelAlloc ||
9154 UsefulBufCompareToSZ(Item.val.string, "string1")) {
9155 return 5800;
9156 }
9157
9158 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
9159 return (int32_t)nCBORError;
9160 }
9161 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
9162 Item.uDataAlloc ||
9163 Item.uLabelAlloc ||
9164 UsefulBufCompareToSZ(Item.val.string, "string2")) {
9165 return 5900;
9166 }
9167
9168 QCBORDecode_Rewind(&DCtx);
9169
9170 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
9171 return 5900+(int32_t)nCBORError;
9172 }
9173 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
9174 Item.uDataAlloc ||
9175 Item.uLabelAlloc ||
9176 UsefulBufCompareToSZ(Item.val.string, "string1")) {
9177 return 6000;
9178 }
9179
9180
9181 // Rewind a byte string inside an array inside an array
9182 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidWrappedMapEncoded), 0);
9183
9184 QCBORDecode_EnterArray(&DCtx, NULL);
9185
9186 uint64_t i;
9187 QCBORDecode_GetUInt64(&DCtx, &i);
9188
9189 QCBORDecode_EnterArray(&DCtx, NULL);
9190
9191 QCBORDecode_EnterBstrWrapped(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
9192 if(QCBORDecode_GetError(&DCtx)) {
9193 return 6100;
9194 }
9195
9196 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
9197 return (int32_t)nCBORError;
9198 }
9199 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
9200 return 6200;
9201 }
9202
9203 QCBORDecode_Rewind(&DCtx);
9204
9205 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
9206 return 6300+(int32_t)nCBORError;
9207 }
9208 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
9209 return 6400;
9210 }
9211
9212#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
9213 // Rewind a byte string inside an indefinite-length array inside
9214 // indefinite-length array
9215
9216 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidIndefWrappedMapEncoded), 0);
9217
9218 QCBORDecode_EnterArray(&DCtx, NULL);
9219
9220 QCBORDecode_GetUInt64(&DCtx, &i);
9221
9222 QCBORDecode_EnterArray(&DCtx, NULL);
9223
9224 QCBORDecode_EnterBstrWrapped(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
9225 if(QCBORDecode_GetError(&DCtx)) {
9226 return 6500;
9227 }
9228
9229 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
9230 return 6600+(int32_t)nCBORError;
9231 }
9232 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
9233 return 6700;
9234 }
9235
9236 QCBORDecode_Rewind(&DCtx);
9237
9238 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
9239 return 6800+(int32_t)nCBORError;
9240 }
9241 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
9242 return 6900;
9243 }
9244#endif
9245
9246 // Rewind an empty map
9247 // [100, {}]
9248 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pWithEmptyMap), 0);
9249 QCBORDecode_EnterArray(&DCtx, NULL);
9250 QCBORDecode_GetUInt64(&DCtx, &i);
9251 if(i != 100) {
9252 return 7010;
9253 }
9254 QCBORDecode_EnterMap(&DCtx, NULL);
9255
9256 /* Do it 5 times to be sure multiple rewinds work */
9257 for(int n = 0; n < 5; n++) {
9258 nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
9259 if(nCBORError != QCBOR_ERR_NO_MORE_ITEMS) {
9260 return 7000 + n;
9261 }
9262 QCBORDecode_Rewind(&DCtx);
9263 }
9264 QCBORDecode_ExitMap(&DCtx);
9265 QCBORDecode_Rewind(&DCtx);
9266 QCBORDecode_GetUInt64(&DCtx, &i);
9267 if(i != 100) {
9268 return 7010;
9269 }
9270 QCBORDecode_ExitArray(&DCtx);
9271 QCBORDecode_Rewind(&DCtx);
9272 QCBORDecode_EnterArray(&DCtx, NULL);
9273 i = 9;
9274 QCBORDecode_GetUInt64(&DCtx, &i);
9275 if(i != 100) {
9276 return 7020;
9277 }
9278 if(QCBORDecode_GetError(&DCtx)){
9279 return 7030;
9280 }
9281
9282 // Rewind an empty indefinite length map
9283#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
9284 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pWithEmptyMapInDef), 0);
9285 QCBORDecode_EnterArray(&DCtx, NULL);
9286 QCBORDecode_GetUInt64(&DCtx, &i);
9287 if(i != 100) {
9288 return 7810;
9289 }
9290 QCBORDecode_EnterMap(&DCtx, NULL);
9291
9292 /* Do it 5 times to be sure multiple rewinds work */
9293 for(int n = 0; n < 5; n++) {
9294 nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
9295 if(nCBORError != QCBOR_ERR_NO_MORE_ITEMS) {
9296 return 7800 + n;
9297 }
9298 QCBORDecode_Rewind(&DCtx);
9299 }
9300 QCBORDecode_ExitMap(&DCtx);
9301 QCBORDecode_Rewind(&DCtx);
9302 QCBORDecode_GetUInt64(&DCtx, &i);
9303 if(i != 100) {
9304 return 7810;
9305 }
9306 QCBORDecode_ExitArray(&DCtx);
9307 QCBORDecode_Rewind(&DCtx);
9308 QCBORDecode_EnterArray(&DCtx, NULL);
9309 i = 9;
9310 QCBORDecode_GetUInt64(&DCtx, &i);
9311 if(i != 100) {
9312 return 7820;
9313 }
9314 if(QCBORDecode_GetError(&DCtx)){
9315 return 7830;
9316 }
9317#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
9318
9319 // Rewind an indefnite length byte-string wrapped sequence
9320#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
9321 QCBORDecode_Init(&DCtx,
9322 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pWrappedByIndefiniteLength),
9323 0);
9324 UsefulBuf_MAKE_STACK_UB(Pool, 100);
9325 QCBORDecode_SetMemPool(&DCtx, Pool, 0);
9326
9327 QCBORDecode_EnterArray(&DCtx, NULL);
9328 QCBORDecode_EnterBstrWrapped(&DCtx, 2, NULL);
Laurence Lundblade31fddb72024-05-13 13:03:35 -07009329 if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_CANNOT_ENTER_ALLOCATED_STRING) {
Laurence Lundbladecf41c522021-02-20 10:19:07 -07009330 return 7300;
9331 }
9332
9333 /*
Laurence Lundblade31fddb72024-05-13 13:03:35 -07009334 Improvement: Fix QCBORDecode_EnterBstrWrapped() so it can work on
9335 allocated strings. This is a fairly big job because of all the
9336 UsefulBuf internal book keeping that needs tweaking.
Laurence Lundbladecf41c522021-02-20 10:19:07 -07009337 QCBORDecode_GetUInt64(&DCtx, &i);
9338 if(i != 42) {
9339 return 7110;
9340 }
9341 QCBORDecode_Rewind(&DCtx);
9342 QCBORDecode_GetUInt64(&DCtx, &i);
9343 if(i != 42) {
9344 return 7220;
Laurence Lundblade31fddb72024-05-13 13:03:35 -07009345 }
9346 */
Laurence Lundblade37286c02022-09-03 10:05:02 -07009347
Laurence Lundbladeec290b82024-06-10 11:10:54 -07009348#endif /* ! QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
Laurence Lundbladecf41c522021-02-20 10:19:07 -07009349
9350
9351 // Rewind an indefnite length byte-string wrapped sequence
9352
Laurence Lundblade2a26abb2020-11-05 19:06:54 -08009353 return 0;
9354}
Laurence Lundblade9f9c3732021-03-23 09:38:46 -07009355
Laurence Lundbladeec290b82024-06-10 11:10:54 -07009356#endif /* ! QCBOR_DISABLE_NON_INTEGER_LABELS */
Laurence Lundblade9f9c3732021-03-23 09:38:46 -07009357
9358
9359static const uint8_t spBooleansInMap[] =
9360{
9361 0xa1, 0x08, 0xf5
9362};
9363
9364static const uint8_t spBooleansInMapWrongType[] =
9365{
9366 0xa1, 0x08, 0xf6
9367};
9368
9369static const uint8_t spBooleansInMapNWF[] =
9370{
9371 0xa1, 0x08, 0x1a
9372};
9373
Laurence Lundblade8782dd32021-04-27 04:15:37 -07009374static const uint8_t spNullInMap[] =
9375{
9376 0xa1, 0x08, 0xf6
9377};
9378
9379static const uint8_t spUndefinedInMap[] =
9380{
9381 0xa1, 0x08, 0xf7
9382};
9383
Laurence Lundblade9f9c3732021-03-23 09:38:46 -07009384
9385int32_t BoolTest(void)
9386{
9387 QCBORDecodeContext DCtx;
9388 bool b;
9389
Laurence Lundblade8782dd32021-04-27 04:15:37 -07009390 QCBORDecode_Init(&DCtx,
9391 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMap),
9392 0);
Laurence Lundblade9f9c3732021-03-23 09:38:46 -07009393 QCBORDecode_EnterMap(&DCtx, NULL);
9394 QCBORDecode_GetBool(&DCtx, &b);
9395 if(QCBORDecode_GetAndResetError(&DCtx) || !b) {
9396 return 1;
9397 }
9398
9399 QCBORDecode_GetBoolInMapN(&DCtx, 7, &b);
9400 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_LABEL_NOT_FOUND) {
9401 return 2;
9402 }
9403
9404 QCBORDecode_GetBoolInMapN(&DCtx, 8, &b);
9405 if(QCBORDecode_GetAndResetError(&DCtx) || !b) {
9406 return 3;
9407 }
9408
9409
9410 QCBORDecode_GetBoolInMapSZ(&DCtx, "xx", &b);
9411 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_LABEL_NOT_FOUND) {
9412 return 4;
9413 }
9414
Laurence Lundblade8782dd32021-04-27 04:15:37 -07009415 QCBORDecode_Init(&DCtx,
9416 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMapWrongType),
9417 0);
Laurence Lundblade9f9c3732021-03-23 09:38:46 -07009418 QCBORDecode_EnterMap(&DCtx, NULL);
9419 QCBORDecode_GetBool(&DCtx, &b);
9420 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
9421 return 5;
9422 }
9423
Laurence Lundblade8782dd32021-04-27 04:15:37 -07009424 QCBORDecode_Init(&DCtx,
9425 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMapNWF),
9426 0);
Laurence Lundblade9f9c3732021-03-23 09:38:46 -07009427 QCBORDecode_EnterMap(&DCtx, NULL);
9428 QCBORDecode_GetBool(&DCtx, &b);
9429 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_HIT_END) {
9430 return 6;
9431 }
9432
Laurence Lundblade8782dd32021-04-27 04:15:37 -07009433
9434 QCBORDecode_Init(&DCtx,
9435 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spNullInMap),
9436 0);
9437 QCBORDecode_EnterMap(&DCtx, NULL);
9438 QCBORDecode_GetNull(&DCtx);
9439 if(QCBORDecode_GetAndResetError(&DCtx)) {
9440 return 7;
9441 }
9442
9443 QCBORDecode_Init(&DCtx,
9444 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMap),
9445 0);
9446 QCBORDecode_EnterMap(&DCtx, NULL);
9447 QCBORDecode_GetNull(&DCtx);
9448 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
9449 return 8;
9450 }
9451
9452 QCBORDecode_Init(&DCtx,
9453 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spNullInMap),
9454 0);
9455 QCBORDecode_EnterMap(&DCtx, NULL);
9456 QCBORDecode_GetNullInMapN(&DCtx, 8);
9457 if(QCBORDecode_GetAndResetError(&DCtx)) {
9458 return 9;
9459 }
9460
9461 QCBORDecode_Init(&DCtx,
9462 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMap),
9463 0);
9464 QCBORDecode_EnterMap(&DCtx, NULL);
9465 QCBORDecode_GetNullInMapN(&DCtx, 8);
9466 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
9467 return 10;
9468 }
9469
9470 QCBORDecode_Init(&DCtx,
9471 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMapNWF),
9472 0);
9473 QCBORDecode_EnterMap(&DCtx, NULL);
9474 QCBORDecode_GetUndefined(&DCtx);
9475 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_HIT_END) {
9476 return 11;
9477 }
9478
9479 QCBORDecode_Init(&DCtx,
9480 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUndefinedInMap),
9481 0);
9482 QCBORDecode_EnterMap(&DCtx, NULL);
9483 QCBORDecode_GetUndefined(&DCtx);
9484 if(QCBORDecode_GetAndResetError(&DCtx)) {
9485 return 12;
9486 }
9487
9488 QCBORDecode_Init(&DCtx,
9489 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMap),
9490 0);
9491 QCBORDecode_EnterMap(&DCtx, NULL);
9492 QCBORDecode_GetUndefined(&DCtx);
9493 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
9494 return 13;
9495 }
9496
9497 QCBORDecode_Init(&DCtx,
9498 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUndefinedInMap),
9499 0);
9500 QCBORDecode_EnterMap(&DCtx, NULL);
9501 QCBORDecode_GetUndefinedInMapN(&DCtx, 8);
9502 if(QCBORDecode_GetAndResetError(&DCtx)) {
9503 return 14;
9504 }
9505
9506 QCBORDecode_Init(&DCtx,
9507 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMap),
9508 0);
9509 QCBORDecode_EnterMap(&DCtx, NULL);
9510 QCBORDecode_GetUndefinedInMapN(&DCtx, 8);
9511 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
9512 return 15;
9513 }
9514
9515 QCBORDecode_Init(&DCtx,
9516 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMapNWF),
9517 0);
9518 QCBORDecode_EnterMap(&DCtx, NULL);
9519 QCBORDecode_GetUndefined(&DCtx);
9520 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_HIT_END) {
9521 return 15;
9522 }
9523
Laurence Lundblade9f9c3732021-03-23 09:38:46 -07009524 return 0;
9525}
Laurence Lundbladef00b8be2024-03-08 10:34:33 -08009526
9527
Laurence Lundbladeec290b82024-06-10 11:10:54 -07009528#ifndef QCBOR_DISABLE_NON_INTEGER_LABELS
Laurence Lundbladea29f45a2024-05-14 15:55:19 -07009529static const uint8_t spExpectedArray2s[] = {
9530 0x82, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67,
9531 0x31, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67,
9532 0x32};
9533
9534#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
9535static const uint8_t spExpectedArray2sIndef[] = {
9536 0x9f, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67,
9537 0x31, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67,
9538 0x32, 0xff};
9539#endif
9540
9541static const uint8_t spExpectedMap4[] = {
9542 0xa4, 0x67, 0x62, 0x79, 0x74, 0x65, 0x73, 0x20,
9543 0x31, 0x44, 0x78, 0x78, 0x78, 0x78, 0x67, 0x62,
9544 0x79, 0x74, 0x65, 0x73, 0x20, 0x32, 0x44, 0x79,
9545 0x79, 0x79, 0x79, 0x6b, 0x61, 0x6e, 0x6f, 0x74,
9546 0x68, 0x65, 0x72, 0x20, 0x69, 0x6e, 0x74, 0x18,
9547 0x62, 0x66, 0x74, 0x65, 0x78, 0x74, 0x20, 0x32,
9548 0x78, 0x1e, 0x6c, 0x69, 0x65, 0x73, 0x2c, 0x20,
9549 0x64, 0x61, 0x6d, 0x6e, 0x20, 0x6c, 0x69, 0x65,
9550 0x73, 0x20, 0x61, 0x6e, 0x64, 0x20, 0x73, 0x74,
9551 0x61, 0x74, 0x69, 0x73, 0x74, 0x69, 0x63, 0x73};
9552
9553
9554#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
9555
9556static const uint8_t spExpectedMap4Indef[] = {
9557 0xbf, 0x67, 0x62, 0x79, 0x74, 0x65, 0x73, 0x20,
9558 0x31, 0x44, 0x78, 0x78, 0x78, 0x78, 0x67, 0x62,
9559 0x79, 0x74, 0x65, 0x73, 0x20, 0x32, 0x44, 0x79,
9560 0x79, 0x79, 0x79, 0x6b, 0x61, 0x6e, 0x6f, 0x74,
9561 0x68, 0x65, 0x72, 0x20, 0x69, 0x6e, 0x74, 0x18,
9562 0x62, 0x66, 0x74, 0x65, 0x78, 0x74, 0x20, 0x32,
9563 0x78, 0x1e, 0x6c, 0x69, 0x65, 0x73, 0x2c, 0x20,
9564 0x64, 0x61, 0x6d, 0x6e, 0x20, 0x6c, 0x69, 0x65,
9565 0x73, 0x20, 0x61, 0x6e, 0x64, 0x20, 0x73, 0x74,
9566 0x61, 0x74, 0x69, 0x73, 0x74, 0x69, 0x63, 0x73,
9567 0xff};
9568
9569/*
9570 * [[[[[0, []]]]], 0]
9571 */
9572static const uint8_t spDefAndIndef[] = {
9573 0x82,
9574 0x9f, 0x9f, 0x9f, 0x82, 0x00, 0x9f, 0xff, 0xff, 0xff, 0xff, 0x00
9575};
9576#endif /* !QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
9577
Laurence Lundbladea29f45a2024-05-14 15:55:19 -07009578#ifndef QCBOR_DISABLE_TAGS
9579/* An exp / mant tag in two nested arrays */
9580static const uint8_t spExpMant[] = {0x81, 0x81, 0xC4, 0x82, 0x20, 0x03};
9581#endif /* !QCBOR_DISABLE_TAGS */
Laurence Lundbladeec290b82024-06-10 11:10:54 -07009582#endif
Laurence Lundbladea29f45a2024-05-14 15:55:19 -07009583
Laurence Lundbladeec290b82024-06-10 11:10:54 -07009584#ifndef QCBOR_DISABLE_NON_INTEGER_LABELS
Laurence Lundbladea29f45a2024-05-14 15:55:19 -07009585int32_t GetMapAndArrayTest(void)
9586{
9587 QCBORDecodeContext DCtx;
9588 size_t uPosition ;
9589 QCBORItem Item;
9590 UsefulBufC ReturnedEncodedCBOR;
9591
Laurence Lundbladeec290b82024-06-10 11:10:54 -07009592 // Improvement: rework so it can run with QCBOR_DISABLE_NON_INTEGER_LABELS
Laurence Lundbladea29f45a2024-05-14 15:55:19 -07009593 QCBORDecode_Init(&DCtx,
9594 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded),
9595 0);
9596
9597 QCBORDecode_EnterMap(&DCtx, NULL);
9598 QCBORDecode_VGetNextConsume(&DCtx, &Item);
9599 QCBORDecode_GetArray(&DCtx, &Item, &ReturnedEncodedCBOR);
9600 if(QCBORDecode_GetError(&DCtx)) {
9601 return 1;
9602 }
9603 if(Item.val.uCount != 2) {
9604 return 2;
9605 }
9606 if(UsefulBuf_Compare(ReturnedEncodedCBOR, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedArray2s))) {
9607 return 3;
9608 }
9609
9610 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
9611 UsefulBuf_Compare(Item.label.string, UsefulBuf_FROM_SZ_LITERAL("an array of two strings"))) {
9612 return 4;
9613 }
9614
9615 uPosition = QCBORDecode_Tell(&DCtx);
9616
9617
9618 QCBORDecode_GetMap(&DCtx, &Item, &ReturnedEncodedCBOR);
9619 if(QCBORDecode_GetError(&DCtx)) {
9620 return 10;
9621 }
9622 if(Item.val.uCount != 4) {
9623 return 11;
9624 }
9625 if(UsefulBuf_Compare(ReturnedEncodedCBOR, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedMap4))) {
9626 return 12;
9627 }
9628 uPosition = QCBORDecode_Tell(&DCtx);
9629 QCBORDecode_GetArrayFromMapSZ(&DCtx,
9630 "an array of two strings",
9631 &Item,
9632 &ReturnedEncodedCBOR);
9633 if(QCBORDecode_GetError(&DCtx)) {
9634 return 20;
9635 }
9636 if(Item.val.uCount != 2) {
9637 return 21;
9638 }
9639 if(UsefulBuf_Compare(ReturnedEncodedCBOR, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedArray2s))) {
9640 return 22;
9641 }
9642 if(uPosition != QCBORDecode_Tell(&DCtx)) {
9643 return 23;
9644 }
9645
9646 QCBORDecode_Rewind(&DCtx);
9647
9648 uPosition = QCBORDecode_Tell(&DCtx);
9649 QCBORDecode_GetMapFromMapSZ(&DCtx, "map in a map", &Item, &ReturnedEncodedCBOR);
9650 if(QCBORDecode_GetError(&DCtx)) {
9651 return 30;
9652 }
9653 if(Item.val.uCount != 4) {
9654 return 31;
9655 }
9656 if(UsefulBuf_Compare(ReturnedEncodedCBOR, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedMap4))) {
9657 return 32;
9658 }
9659 if(uPosition != QCBORDecode_Tell(&DCtx)) {
9660 return 33;
9661 }
9662
9663 uPosition = QCBORDecode_Tell(&DCtx);
9664 QCBORDecode_GetArrayFromMapSZ(&DCtx, "map in a map", &Item, &ReturnedEncodedCBOR);
9665 if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
9666 return 40;
9667 }
9668 if(UINT32_MAX != QCBORDecode_Tell(&DCtx)) {
9669 return 41;
9670 }
9671 QCBORDecode_GetAndResetError(&DCtx);
9672 if(uPosition != QCBORDecode_Tell(&DCtx)) {
9673 return 42;
9674 }
9675
9676
9677#ifndef QCBOR_DISABLE_TAGS
9678 UsefulBufC ExpMant = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpMant);
9679 QCBORDecode_Init(&DCtx, ExpMant, 0);
9680 QCBORDecode_EnterArray(&DCtx, NULL);
9681 QCBORDecode_EnterArray(&DCtx, NULL);
9682 QCBORDecode_GetArray(&DCtx, &Item, &ReturnedEncodedCBOR);
9683 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) {
9684 return 200;
9685 }
9686 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
9687 return 201;
9688 }
9689 if(!QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_DECIMAL_FRACTION)) {
9690 return 202;
9691 }
9692 if(Item.val.uCount != 2) {
9693 return 201;
9694 }
9695 if(UsefulBuf_Compare(ReturnedEncodedCBOR, UsefulBuf_Tail(ExpMant, 2))) {
9696 return 205;
9697 }
9698#endif /* !QCBOR_DISABLE_TAGS */
9699
9700
9701#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
9702
9703 UsefulBufC DefAndIndef = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spDefAndIndef);
9704 QCBORDecode_Init(&DCtx, DefAndIndef, 0);
9705 QCBORDecode_EnterArray(&DCtx, NULL);
9706 QCBORDecode_GetArray(&DCtx, &Item, &ReturnedEncodedCBOR);
9707 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) {
9708 return 50;
9709 }
9710 if(UsefulBuf_Compare(ReturnedEncodedCBOR, UsefulBuf_Tail(UsefulBuf_Head(DefAndIndef, 11), 1))) {
9711 return 51;
9712 }
9713
9714 QCBORDecode_Init(&DCtx, DefAndIndef, 0);
9715 QCBORDecode_EnterArray(&DCtx, NULL);
9716 QCBORDecode_EnterArray(&DCtx, NULL);
9717 QCBORDecode_GetArray(&DCtx, &Item, &ReturnedEncodedCBOR);
9718 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) {
9719 return 52;
9720 }
9721 if(UsefulBuf_Compare(ReturnedEncodedCBOR, UsefulBuf_Tail(UsefulBuf_Head(DefAndIndef, 10), 2))) {
9722 return 53;
9723 }
9724
9725 QCBORDecode_Init(&DCtx, DefAndIndef, 0);
9726 QCBORDecode_EnterArray(&DCtx, NULL);
9727 QCBORDecode_EnterArray(&DCtx, NULL);
9728 QCBORDecode_EnterArray(&DCtx, NULL);
9729 QCBORDecode_GetArray(&DCtx, &Item, &ReturnedEncodedCBOR);
9730 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) {
9731 return 54;
9732 }
9733 if(UsefulBuf_Compare(ReturnedEncodedCBOR, UsefulBuf_Tail(UsefulBuf_Head(DefAndIndef, 9), 3))) {
9734 return 55;
9735 }
9736 QCBORDecode_Init(&DCtx, DefAndIndef, 0);
9737 QCBORDecode_EnterArray(&DCtx, NULL);
9738 QCBORDecode_EnterArray(&DCtx, NULL);
9739 QCBORDecode_EnterArray(&DCtx, NULL);
9740 QCBORDecode_EnterArray(&DCtx, NULL);
9741 QCBORDecode_GetArray(&DCtx, &Item, &ReturnedEncodedCBOR);
9742 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) {
9743 return 56;
9744 }
9745 if(UsefulBuf_Compare(ReturnedEncodedCBOR, UsefulBuf_Tail(UsefulBuf_Head(DefAndIndef, 8), 4))) {
9746 return 57;
9747 }
9748
9749 QCBORDecode_Init(&DCtx, DefAndIndef, 0);
9750 QCBORDecode_EnterArray(&DCtx, NULL);
9751 QCBORDecode_EnterArray(&DCtx, NULL);
9752 QCBORDecode_EnterArray(&DCtx, NULL);
9753 QCBORDecode_EnterArray(&DCtx, NULL);
9754 QCBORDecode_EnterArray(&DCtx, NULL);
9755 QCBORDecode_VGetNextConsume(&DCtx, &Item);
9756 QCBORDecode_GetArray(&DCtx, &Item, &ReturnedEncodedCBOR);
9757 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) {
9758 return 58;
9759 }
9760 if(UsefulBuf_Compare(ReturnedEncodedCBOR, UsefulBuf_Tail(UsefulBuf_Head(DefAndIndef, 8), 6))) {
9761 return 59;
9762 }
9763
9764
9765 QCBORDecode_Init(&DCtx,
9766 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapIndefEncoded),
9767 0);
9768
9769 QCBORDecode_EnterMap(&DCtx, NULL);
9770 QCBORDecode_VGetNextConsume(&DCtx, &Item);
9771 QCBORDecode_GetArray(&DCtx, &Item, &ReturnedEncodedCBOR);
9772 if(QCBORDecode_GetError(&DCtx)) {
9773 return 60;
9774 }
9775 if(Item.val.uCount != UINT16_MAX) {
9776 return 61;
9777 }
9778 if(UsefulBuf_Compare(ReturnedEncodedCBOR, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedArray2sIndef))) {
9779 return 62;
9780 }
9781
9782 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
9783 UsefulBuf_Compare(Item.label.string, UsefulBuf_FROM_SZ_LITERAL("an array of two strings"))) {
9784 return 63;
9785 }
9786
9787 uPosition = QCBORDecode_Tell(&DCtx);
9788
9789
9790 QCBORDecode_GetMap(&DCtx, &Item, &ReturnedEncodedCBOR);
9791 if(QCBORDecode_GetError(&DCtx)) {
9792 return 70;
9793 }
9794 if(Item.val.uCount != UINT16_MAX) {
9795 return 71;
9796 }
9797 if(UsefulBuf_Compare(ReturnedEncodedCBOR, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedMap4Indef))) {
9798 return 72;
9799 }
9800
9801
9802 uPosition = QCBORDecode_Tell(&DCtx);
9803 QCBORDecode_GetArrayFromMapSZ(&DCtx,
9804 "an array of two strings",
9805 &Item,
9806 &ReturnedEncodedCBOR);
9807 if(QCBORDecode_GetError(&DCtx)) {
9808 return 80;
9809 }
9810 if(Item.val.uCount != UINT16_MAX) {
9811 return 81;
9812 }
9813 if(UsefulBuf_Compare(ReturnedEncodedCBOR, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedArray2sIndef))) {
9814 return 82;
9815 }
9816 if(uPosition != QCBORDecode_Tell(&DCtx)) {
9817 return 83;
9818 }
9819
9820 QCBORDecode_Rewind(&DCtx);
9821
9822 uPosition = QCBORDecode_Tell(&DCtx);
9823 QCBORDecode_GetMapFromMapSZ(&DCtx, "map in a map", &Item, &ReturnedEncodedCBOR);
9824 if(QCBORDecode_GetError(&DCtx)) {
9825 return 90;
9826 }
9827 if(Item.val.uCount != UINT16_MAX) {
9828 return 91;
9829 }
9830 if(UsefulBuf_Compare(ReturnedEncodedCBOR, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedMap4Indef))) {
9831 return 92;
9832 }
9833 if(uPosition != QCBORDecode_Tell(&DCtx)) {
9834 return 93;
9835 }
9836
9837 uPosition = QCBORDecode_Tell(&DCtx);
9838 QCBORDecode_GetArrayFromMapSZ(&DCtx, "map in a map", &Item, &ReturnedEncodedCBOR);
9839 if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
9840 return 100;
9841 }
9842 if(UINT32_MAX != QCBORDecode_Tell(&DCtx)) {
9843 return 101;
9844 }
9845 QCBORDecode_GetAndResetError(&DCtx);
9846 if(uPosition != QCBORDecode_Tell(&DCtx)) {
9847 return 102;
9848 }
Laurence Lundbladeec290b82024-06-10 11:10:54 -07009849#endif /* ! QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
Laurence Lundbladea29f45a2024-05-14 15:55:19 -07009850
9851 return 0;
9852}
Laurence Lundbladeec290b82024-06-10 11:10:54 -07009853#endif /* ! QCBOR_DISABLE_NON_INTEGER_LABELS */
Laurence Lundbladea29f45a2024-05-14 15:55:19 -07009854
9855
Laurence Lundbladef00b8be2024-03-08 10:34:33 -08009856int32_t
9857ErrorHandlingTests(void)
9858{
9859 QCBORDecodeContext DCtx;
9860 QCBORItem Item;
9861 QCBORError uError;
9862 int64_t integer;
9863
9864 /* Test QCBORDecode_SetError() */
9865 QCBORDecode_Init(&DCtx,
9866 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded),
9867 QCBOR_DECODE_MODE_NORMAL);
9868
9869 QCBORDecode_SetError(&DCtx, QCBOR_ERR_FIRST_USER_DEFINED);
9870
9871 QCBORDecode_VGetNext(&DCtx, &Item);
9872
9873 uError = QCBORDecode_GetError(&DCtx);
9874
9875 if(uError != QCBOR_ERR_FIRST_USER_DEFINED) {
9876 return -1;
9877 }
9878
9879 if(Item.uLabelType != QCBOR_TYPE_NONE ||
9880 Item.uDataType != QCBOR_TYPE_NONE) {
9881 return -2;
9882 }
9883
9884
9885 /* Test data type returned from previous error */
9886 QCBORDecode_Init(&DCtx,
9887 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded),
9888 QCBOR_DECODE_MODE_NORMAL);
9889 QCBORDecode_GetInt64(&DCtx, &integer);
9890 uError = QCBORDecode_GetError(&DCtx);
9891 if(uError != QCBOR_ERR_UNEXPECTED_TYPE) {
9892 return -3;
9893 }
9894
9895 QCBORDecode_VGetNext(&DCtx, &Item);
9896 if(Item.uLabelType != QCBOR_TYPE_NONE ||
9897 Item.uDataType != QCBOR_TYPE_NONE) {
9898 return -2;
9899 }
9900 uError = QCBORDecode_GetError(&DCtx);
9901 if(uError != QCBOR_ERR_UNEXPECTED_TYPE) {
9902 return -3;
9903 }
9904
9905
9906 /* Test error classification functions */
9907
9908 if(!QCBORDecode_IsUnrecoverableError(QCBOR_ERR_INDEFINITE_STRING_CHUNK)) {
9909 return -10;
9910 }
9911 if(QCBORDecode_IsUnrecoverableError(QCBOR_SUCCESS)) {
9912 return -11;
9913 }
9914 if(!QCBORDecode_IsUnrecoverableError(QCBOR_ERR_INDEFINITE_STRING_CHUNK)) {
9915 return -12;
9916 }
9917 if(QCBORDecode_IsUnrecoverableError(QCBOR_ERR_DUPLICATE_LABEL)) {
9918 return -13;
9919 }
9920
9921 if(!QCBORDecode_IsNotWellFormedError(QCBOR_ERR_BAD_TYPE_7)) {
9922 return -20;
9923 }
9924 if(!QCBORDecode_IsNotWellFormedError(QCBOR_ERR_BAD_BREAK)) {
9925 return -21;
9926 }
9927 if(QCBORDecode_IsNotWellFormedError(QCBOR_SUCCESS)) {
9928 return -22;
9929 }
9930 if(QCBORDecode_IsNotWellFormedError(QCBOR_ERR_ARRAY_DECODE_TOO_LONG)) {
9931 return -23;
9932 }
9933
Laurence Lundbladecd34e582024-06-06 10:55:14 -07009934 /* Test error strings */
9935 const char *szErrString;
9936
9937 szErrString = qcbor_err_to_str(QCBOR_ERR_ARRAY_DECODE_TOO_LONG);
9938 if(szErrString == NULL) {
9939 return -100;
9940 }
9941 if(strcmp(szErrString, "QCBOR_ERR_ARRAY_DECODE_TOO_LONG")) {
9942 return -101;
9943 }
9944
9945 szErrString = qcbor_err_to_str(QCBOR_SUCCESS);
9946 if(szErrString == NULL) {
9947 return -102;
9948 }
9949 if(strcmp(szErrString, "QCBOR_SUCCESS")) {
9950 return -103;
9951 }
9952
9953 szErrString = qcbor_err_to_str(100);
9954 if(szErrString == NULL) {
9955 return -104;
9956 }
9957 if(strcmp(szErrString, "Unidentified QCBOR error")) {
9958 return -105;
9959 }
9960
9961 szErrString = qcbor_err_to_str(200);
9962 if(szErrString == NULL) {
9963 return -106;
9964 }
9965 if(strcmp(szErrString, "USER_DEFINED_200")) {
9966 return -107;
9967 }
9968
Laurence Lundbladef00b8be2024-03-08 10:34:33 -08009969 return 0;
9970}
Laurence Lundblade11654912024-05-09 11:49:24 -07009971
9972
9973int32_t TellTests(void)
9974{
9975 QCBORDecodeContext DCtx;
9976 QCBORItem Item;
9977 uint32_t uPosition;
9978 int nIndex;
9979 int64_t nDecodedInt;
9980
Laurence Lundbladeec290b82024-06-10 11:10:54 -07009981 // Improvement: rewrite so this can run with only integer labels
Laurence Lundblade11654912024-05-09 11:49:24 -07009982 static const uint32_t aPos[] =
9983 {0, 1, 17, 42, 50, 58, 72, 85, 98, 112, UINT32_MAX};
9984 QCBORDecode_Init(&DCtx,
9985 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded),
9986 0);
9987 for(nIndex = 0; ; nIndex++) {
9988 uPosition = QCBORDecode_Tell(&DCtx);
9989 if(uPosition != aPos[nIndex]) {
9990 return nIndex;
9991 }
9992
9993 if(uPosition == UINT32_MAX) {
9994 break;
9995 }
9996
9997 QCBORDecode_VGetNext(&DCtx, &Item);
9998 }
9999
10000#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
10001 static const uint32_t aPosIndef[] =
10002 {0, 1, 17, 42, 50, 59, 73, 86, 99, 113, UINT32_MAX};
10003 QCBORDecode_Init(&DCtx,
10004 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapIndefEncoded),
10005 0);
10006
10007 for(nIndex = 0; ; nIndex++) {
10008 uPosition = QCBORDecode_Tell(&DCtx);
10009 if(uPosition != aPosIndef[nIndex]) {
10010 return nIndex + 100;
10011 }
10012
10013 if(uPosition == UINT32_MAX) {
10014 break;
10015 }
10016
10017 QCBORDecode_VGetNext(&DCtx, &Item);
10018 }
10019#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
10020
10021 /* Next, some tests with entered maps and arrays */
10022 QCBORDecode_Init(&DCtx,
10023 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded),
10024 0);
10025 QCBORDecode_EnterMap(&DCtx, &Item);
10026 if(QCBORDecode_Tell(&DCtx) != 1) {
10027 return 1001;
10028 }
10029 QCBORDecode_GetInt64InMapSZ(&DCtx, "first integer", &nDecodedInt);
10030 if(QCBORDecode_Tell(&DCtx) != 1) {
10031 return 1002;
10032 }
10033 QCBORDecode_EnterMapFromMapSZ(&DCtx, "map in a map");
10034 if(QCBORDecode_Tell(&DCtx) != 72) {
10035 return 1003;
10036 }
10037
10038 QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt);
10039 if(nDecodedInt != 98) {
10040 return 1004;
10041 }
10042 /* Getting non-aggregate types doesn't affect cursor position. */
10043 if(QCBORDecode_Tell(&DCtx) != 72) {
10044 return 1005;
10045 }
10046 QCBORDecode_VGetNext(&DCtx, &Item);
10047 if(QCBORDecode_Tell(&DCtx) != 85) {
10048 return 1006;
10049 }
10050 QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt);
10051 if(nDecodedInt != 98) {
10052 return 1007;
10053 }
10054 /* Getting non-aggregate types doesn't affect cursor position. */
10055 if(QCBORDecode_Tell(&DCtx) != 85) {
10056 return 1008;
10057 }
10058
10059 QCBORDecode_ExitMap(&DCtx);
10060 if(QCBORDecode_Tell(&DCtx) != UINT32_MAX) {
10061 return 1009;
10062 }
10063 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_NO_MORE_ITEMS) {
10064 return 1010;
10065 }
10066
10067#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
10068 /* Next, some tests with entered maps and arrays */
10069 QCBORDecode_Init(&DCtx,
10070 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapIndefEncoded),
10071 0);
10072 QCBORDecode_EnterMap(&DCtx, &Item);
10073 if(QCBORDecode_Tell(&DCtx) != 1) {
10074 return 2000;
10075 }
10076 QCBORDecode_GetInt64InMapSZ(&DCtx, "first integer", &nDecodedInt);
10077 if(QCBORDecode_Tell(&DCtx) != 1) {
10078 return 2001;
10079 }
10080 QCBORDecode_EnterMapFromMapSZ(&DCtx, "map in a map");
10081 if(QCBORDecode_Tell(&DCtx) != 73) {
10082 return 2002;
10083 }
10084
10085 QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt);
10086 if(nDecodedInt != 98) {
10087 return 2003;
10088 }
10089 /* Getting non-aggregate types doesn't affect cursor position. */
10090 if(QCBORDecode_Tell(&DCtx) != 73) {
10091 return 2004;
10092 }
10093 QCBORDecode_VGetNext(&DCtx, &Item);
10094 if(QCBORDecode_Tell(&DCtx) != 86) {
10095 return 2005;
10096 }
10097 QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt);
10098 if(nDecodedInt != 98) {
10099 return 2006;
10100 }
10101 /* Getting non-aggregate types doesn't affect cursor position. */
10102 if(QCBORDecode_Tell(&DCtx) != 86) {
10103 return 2007;
10104 }
10105
10106 QCBORDecode_ExitMap(&DCtx);
10107 if(QCBORDecode_Tell(&DCtx) != UINT32_MAX) {
10108 return 2008;
10109 }
10110 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_NO_MORE_ITEMS) {
10111 return 2010;
10112 }
10113#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
10114
10115
10116
10117 /* Error state test */
10118 QCBORDecode_Init(&DCtx,
10119 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded),
10120 0);
10121 /* Cause an error */
10122 QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt);
10123 if(QCBORDecode_Tell(&DCtx) != UINT32_MAX) {
10124 return 3000;
10125 }
10126
10127 /* Empties tests */
10128 const uint8_t pMinimalCBOR[] = {0xa0}; // One empty map
10129 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pMinimalCBOR),0);
10130 if(QCBORDecode_Tell(&DCtx) != 0) {
10131 return 4000;
10132 }
10133 QCBORDecode_EnterMap(&DCtx, &Item);
10134 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) {
10135 return 4001;
10136 }
10137 if(QCBORDecode_Tell(&DCtx) != UINT32_MAX) {
10138 return 4002;
10139 }
10140 QCBORDecode_ExitMap(&DCtx);
10141 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) {
10142 return 4001;
10143 }
10144 if(QCBORDecode_Tell(&DCtx) != UINT32_MAX) {
10145 return 4002;
10146 }
10147 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_NO_MORE_ITEMS) {
10148 return 4010;
10149 }
10150
10151#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
10152 const uint8_t pMinimalIndefCBOR[] = {0xbf, 0xff}; // One empty map
10153 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pMinimalIndefCBOR),0);
10154 if(QCBORDecode_Tell(&DCtx) != 0) {
10155 return 4100;
10156 }
10157 QCBORDecode_EnterMap(&DCtx, &Item);
10158 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) {
10159 return 4101;
10160 }
10161 if(QCBORDecode_Tell(&DCtx) != UINT32_MAX) {
10162 return 4102;
10163 }
10164 QCBORDecode_ExitMap(&DCtx);
10165 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) {
10166 return 4101;
10167 }
10168 if(QCBORDecode_Tell(&DCtx) != UINT32_MAX) {
10169 return 4102;
10170 }
10171 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_NO_MORE_ITEMS) {
10172 return 4110;
10173 }
10174#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
10175
10176 /* Test on a CBOR sequence */
10177 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSequenceTestInput),0);
10178 if(QCBORDecode_Tell(&DCtx) != 0) {
10179 return 5000;
10180 }
10181 QCBORDecode_VGetNext(&DCtx, &Item);
10182 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) {
10183 return 5001;
10184 }
10185 if(QCBORDecode_Tell(&DCtx) != 11) {
10186 return 5002;
10187 }
10188 QCBORDecode_VGetNext(&DCtx, &Item);
10189 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) {
10190 return 5003;
10191 }
10192 if(QCBORDecode_Tell(&DCtx) != 12) {
10193 return 5004;
10194 }
10195 QCBORDecode_VGetNext(&DCtx, &Item);
10196 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) {
10197 return 5005;
10198 }
10199 if(QCBORDecode_Tell(&DCtx) != 17) {
10200 return 5006;
10201 }
10202 QCBORDecode_VGetNext(&DCtx, &Item);
10203 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) {
10204 return 5007;
10205 }
10206 if(QCBORDecode_Tell(&DCtx) != UINT32_MAX) {
10207 return 5008;
10208 }
10209 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_NO_MORE_ITEMS) {
10210 return 5010;
10211 }
10212
10213
10214 QCBORDecode_Init(&DCtx,
10215 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded),
10216 0);
10217 QCBORDecode_EnterMap(&DCtx, &Item);
10218 QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings");
10219 if(QCBORDecode_Tell(&DCtx) != 42) {
10220 return 6001;
10221 }
10222 QCBORDecode_VGetNext(&DCtx, &Item);
10223 if(QCBORDecode_Tell(&DCtx) != 50) {
10224 return 6002;
10225 }
10226 QCBORDecode_VGetNext(&DCtx, &Item);
10227 if(QCBORDecode_Tell(&DCtx) != 58) {
10228 return 6008;
10229 }
10230 QCBORDecode_VGetNext(&DCtx, &Item);
10231 (void)QCBORDecode_GetAndResetError(&DCtx);
10232 if(QCBORDecode_Tell(&DCtx) != 58) {
10233 return 6003;
10234 }
10235 QCBORDecode_ExitArray(&DCtx);
10236 if(QCBORDecode_Tell(&DCtx) != 58) {
10237 return 6004;
10238 }
10239
10240 static const uint32_t aEmptiesPos[] =
10241 {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 13, UINT32_MAX};
10242 QCBORDecode_Init(&DCtx,
10243 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(sEmpties),
10244 0);
10245 for(nIndex = 0; ; nIndex++) {
10246 uPosition = QCBORDecode_Tell(&DCtx);
10247 if(uPosition != aEmptiesPos[nIndex]) {
10248 return nIndex + 200;
10249 }
10250
10251 if(uPosition == UINT32_MAX) {
10252 break;
10253 }
10254
10255 QCBORDecode_VGetNext(&DCtx, &Item);
10256 }
10257
10258#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
10259 static const uint32_t aIndefEmptiesPos[] =
10260 {0, 1, 2, 4, 5, 7, 8, 10, 12, 13, 16, 19, UINT32_MAX};
10261 QCBORDecode_Init(&DCtx,
10262 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(sEmptiesIndef),
10263 0);
10264 for(nIndex = 0; ; nIndex++) {
10265 uPosition = QCBORDecode_Tell(&DCtx);
10266 if(uPosition != aIndefEmptiesPos[nIndex]) {
10267 return nIndex + 300;
10268 }
10269
10270 if(uPosition == UINT32_MAX) {
10271 break;
10272 }
10273
10274 QCBORDecode_VGetNext(&DCtx, &Item);
10275 }
10276#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
10277
10278
10279 return 0;
10280}