blob: 9f16668e32996371740d0aec9560c80511df0612 [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
Laurence Lundbladeb95afec2024-08-26 10:51:28 -07002300/* These are just the item that open large maps and arrays, not
2301 * the items in the array. This is sufficient to test the
2302 * boundary condition. */
2303static const uint8_t spLargeArrayFake[] = {
2304 0x99, 0xff, 0xfe};
2305
2306static const uint8_t spTooLargeArrayFake[] = {
2307 0x99, 0xff, 0xff};
2308
2309static const uint8_t spLargeMapFake[] = {
2310 0xb9, 0x7f, 0xff};
2311
2312static const uint8_t spTooLargeMapFake[] = {
2313 0xba, 0x00, 0x00, 0x80, 0x00};
2314
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002315
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03002316int32_t ParseMapTest(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002317{
Laurence Lundbladeb95afec2024-08-26 10:51:28 -07002318 QCBORDecodeContext DCtx;
2319 QCBORItem Item;
2320 QCBORError uErr;
2321
2322 QCBORDecode_Init(&DCtx,
2323 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spLargeArrayFake),
2324 QCBOR_DECODE_MODE_NORMAL);
2325 uErr = QCBORDecode_GetNext(&DCtx, &Item);
2326 if(uErr != QCBOR_SUCCESS || Item.val.uCount != QCBOR_MAX_ITEMS_IN_ARRAY) {
2327 return -100;
2328 }
2329
2330 QCBORDecode_Init(&DCtx,
2331 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTooLargeArrayFake),
2332 QCBOR_DECODE_MODE_NORMAL);
2333 uErr = QCBORDecode_GetNext(&DCtx, &Item);
2334 if(uErr != QCBOR_ERR_ARRAY_DECODE_TOO_LONG) {
2335 return -101;
2336 }
2337
2338 QCBORDecode_Init(&DCtx,
2339 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spLargeMapFake),
2340 QCBOR_DECODE_MODE_NORMAL);
2341 uErr = QCBORDecode_GetNext(&DCtx, &Item);
2342 if(uErr != QCBOR_SUCCESS || Item.val.uCount != QCBOR_MAX_ITEMS_IN_MAP) {
2343 return -110;
2344 }
2345
2346 QCBORDecode_Init(&DCtx,
2347 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTooLargeMapFake),
2348 QCBOR_DECODE_MODE_NORMAL);
2349 uErr = QCBORDecode_GetNext(&DCtx, &Item);
2350 if(uErr != QCBOR_ERR_ARRAY_DECODE_TOO_LONG) {
2351 return -111;
2352 }
2353
2354
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002355 // Parse a moderatly complex map structure very thoroughly
2356 int32_t nResult = ParseMapTest1(QCBOR_DECODE_MODE_NORMAL);
2357 if(nResult) {
2358 return nResult;
2359 }
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08002360
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002361 // Again, but in strings-only mode. It should succeed since the input
2362 // map has only string labels.
2363 nResult = ParseMapTest1(QCBOR_DECODE_MODE_MAP_STRINGS_ONLY);
2364 if(nResult) {
2365 return nResult;
2366 }
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08002367
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002368 // Again, but try to finish the decoding before the end of the
2369 // input at 10 different place and see that the right error code
2370 // is returned.
2371 for(int i = 0; i < 10; i++) {
2372 nResult = ExtraBytesTest(i);
2373 if(nResult) {
2374 break;
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08002375 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002376 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002377
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002378 return nResult;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002379}
Laurence Lundbladeec290b82024-06-10 11:10:54 -07002380#endif /* ! QCBOR_DISABLE_NON_INTEGER_LABELS */
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002381
2382
Laurence Lundbladecc7da412020-12-27 00:09:07 -08002383/* The simple-values including some not well formed */
2384static const uint8_t spSimpleValues[] = {
2385 0x8a, 0xf4, 0xf5, 0xf6, 0xf7, 0xff, 0xe0, 0xf3,
2386 0xf8, 0x00, 0xf8, 0x13, 0xf8, 0x1f, 0xf8, 0x20,
2387 0xf8, 0xff};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002388
Laurence Lundblade3888f002024-06-12 21:20:56 -07002389/* A map of good simple values, plus one well-formed integer */
2390static const uint8_t spGoodSimpleValues[] = {
2391 0xa9, 0x01, 0xf4, 0x02, 0xf5, 0x03, 0xf6, 0x04, 0xf7,
2392 0x05, 0xe0, 0x06, 0xf3, 0x07, 0xf8, 0x20, 0x61, 0x40,
2393 0xf8, 0xff, 0x0f, 0x0f};
2394
2395int32_t SimpleValueDecodeTests(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002396{
2397 QCBORDecodeContext DCtx;
Laurence Lundblade3888f002024-06-12 21:20:56 -07002398 QCBORItem Item;
2399 QCBORError uErr;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002400
Laurence Lundbladeee851742020-01-08 08:37:05 -08002401 QCBORDecode_Init(&DCtx,
2402 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSimpleValues),
2403 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002404
2405
Laurence Lundblade3888f002024-06-12 21:20:56 -07002406 if((uErr = QCBORDecode_GetNext(&DCtx, &Item)))
2407 return (int32_t)uErr;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002408 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
2409 Item.val.uCount != 10)
Laurence Lundblade3888f002024-06-12 21:20:56 -07002410 return 1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002411
Laurence Lundblade3888f002024-06-12 21:20:56 -07002412 if((uErr = QCBORDecode_GetNext(&DCtx, &Item)))
2413 return (int32_t)uErr;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002414 if(Item.uDataType != QCBOR_TYPE_FALSE)
Laurence Lundblade3888f002024-06-12 21:20:56 -07002415 return 2;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002416
Laurence Lundblade3888f002024-06-12 21:20:56 -07002417 if((uErr = QCBORDecode_GetNext(&DCtx, &Item)))
2418 return (int32_t)uErr;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002419 if(Item.uDataType != QCBOR_TYPE_TRUE)
Laurence Lundblade3888f002024-06-12 21:20:56 -07002420 return 3;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002421
Laurence Lundblade3888f002024-06-12 21:20:56 -07002422 if((uErr = QCBORDecode_GetNext(&DCtx, &Item)))
2423 return (int32_t)uErr;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002424 if(Item.uDataType != QCBOR_TYPE_NULL)
Laurence Lundblade3888f002024-06-12 21:20:56 -07002425 return 4;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002426
Laurence Lundblade3888f002024-06-12 21:20:56 -07002427 if((uErr = QCBORDecode_GetNext(&DCtx, &Item)))
2428 return (int32_t)uErr;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002429 if(Item.uDataType != QCBOR_TYPE_UNDEF)
Laurence Lundblade3888f002024-06-12 21:20:56 -07002430 return 5;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002431
2432 // A break
Laurence Lundblade9e3651c2018-10-10 11:49:55 +08002433 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_BREAK)
Laurence Lundblade3888f002024-06-12 21:20:56 -07002434 return 6;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002435
Laurence Lundblade3888f002024-06-12 21:20:56 -07002436 if((uErr = QCBORDecode_GetNext(&DCtx, &Item)))
2437 return (int32_t)uErr;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002438 if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 0)
Laurence Lundblade3888f002024-06-12 21:20:56 -07002439 return 7;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002440
Laurence Lundblade3888f002024-06-12 21:20:56 -07002441 if((uErr = QCBORDecode_GetNext(&DCtx, &Item)))
2442 return (int32_t)uErr;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002443 if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 19)
Laurence Lundblade3888f002024-06-12 21:20:56 -07002444 return 8;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002445
Laurence Lundblade077475f2019-04-26 09:06:33 -07002446 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_TYPE_7)
Laurence Lundblade3888f002024-06-12 21:20:56 -07002447 return 9;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002448
Laurence Lundblade077475f2019-04-26 09:06:33 -07002449 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_TYPE_7)
Laurence Lundblade3888f002024-06-12 21:20:56 -07002450 return 10;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002451
Laurence Lundblade077475f2019-04-26 09:06:33 -07002452 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_TYPE_7)
Laurence Lundblade3888f002024-06-12 21:20:56 -07002453 return 11;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002454
Laurence Lundblade3888f002024-06-12 21:20:56 -07002455 if((uErr = QCBORDecode_GetNext(&DCtx, &Item)))
2456 return (int32_t)uErr;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002457 if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 32)
Laurence Lundblade3888f002024-06-12 21:20:56 -07002458 return 12;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002459
Laurence Lundblade3888f002024-06-12 21:20:56 -07002460 if((uErr = QCBORDecode_GetNext(&DCtx, &Item)))
2461 return (int32_t)uErr;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002462 if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 255)
Laurence Lundblade3888f002024-06-12 21:20:56 -07002463 return 13;
2464
2465
2466 QCBORDecode_Init(&DCtx,
2467 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spGoodSimpleValues),
2468 QCBOR_DECODE_MODE_NORMAL);
2469
2470 uint8_t uSimple;
2471
2472 QCBORDecode_EnterMap(&DCtx, &Item);
2473 QCBORDecode_GetSimple(&DCtx, &uSimple);
2474 if(QCBORDecode_GetError(&DCtx) || uSimple != CBOR_SIMPLEV_FALSE) {
2475 return 20;
2476 }
2477 QCBORDecode_GetSimple(&DCtx, &uSimple);
2478 if(QCBORDecode_GetError(&DCtx) || uSimple != CBOR_SIMPLEV_TRUE) {
2479 return 21;
2480 }
2481 QCBORDecode_GetSimple(&DCtx, &uSimple);
2482 if(QCBORDecode_GetError(&DCtx) || uSimple != CBOR_SIMPLEV_NULL) {
2483 return 22;
2484 }
2485 QCBORDecode_GetSimple(&DCtx, &uSimple);
2486 if(QCBORDecode_GetError(&DCtx) || uSimple != CBOR_SIMPLEV_UNDEF) {
2487 return 23;
2488 }
2489 QCBORDecode_GetSimple(&DCtx, &uSimple);
2490 if(QCBORDecode_GetError(&DCtx) || uSimple != 0) {
2491 return 24;
2492 }
2493 QCBORDecode_GetSimple(&DCtx, &uSimple);
2494 if(QCBORDecode_GetError(&DCtx) || uSimple != 19) {
2495 return 25;
2496 }
2497 QCBORDecode_GetSimple(&DCtx, &uSimple);
2498 if(QCBORDecode_GetError(&DCtx) || uSimple != 32) {
2499 return 26;
2500 }
Laurence Lundbladecdbbc192024-06-28 15:13:04 -07002501#ifndef QCBOR_DISABLE_NON_INTEGER_LABELS
Laurence Lundblade3888f002024-06-12 21:20:56 -07002502 QCBORDecode_GetSimple(&DCtx, &uSimple);
2503 if(QCBORDecode_GetError(&DCtx) || uSimple != 255) {
2504 return 27;
2505 }
2506 QCBORDecode_VGetNext(&DCtx, &Item);
2507 QCBORDecode_GetSimple(&DCtx, &uSimple);
2508 if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_NO_MORE_ITEMS) {
2509 return 28;
2510 }
2511
2512 QCBORDecode_Rewind(&DCtx);
2513
2514 QCBORDecode_GetSimpleInMapN(&DCtx, 6, &uSimple);
2515 if(QCBORDecode_GetError(&DCtx) || uSimple != 19) {
2516 return 30;
2517 }
2518
2519 QCBORDecode_GetSimpleInMapSZ(&DCtx, "@", &uSimple);
2520 if(QCBORDecode_GetError(&DCtx) || uSimple != 255) {
2521 return 31;
2522 }
2523
2524 QCBORDecode_GetSimpleInMapN(&DCtx, 99, &uSimple);
2525 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_LABEL_NOT_FOUND) {
2526 return 32;
2527 }
2528
2529 QCBORDecode_GetSimpleInMapSZ(&DCtx, "xx", &uSimple);
2530 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_LABEL_NOT_FOUND) {
2531 return 33;
2532 }
2533
2534 QCBORDecode_GetSimpleInMapN(&DCtx, 15, &uSimple);
2535 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
2536 return 34;
2537 }
Laurence Lundbladecdbbc192024-06-28 15:13:04 -07002538#endif /* ! QCBOR_DISABLE_NON_INTEGER_LABELS */
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002539
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002540 return 0;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002541}
2542
2543
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03002544int32_t NotWellFormedTests(void)
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002545{
2546 // Loop over all the not-well-formed instance of CBOR
2547 // that are test vectors in not_well_formed_cbor.h
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08002548 const uint16_t nArraySize = C_ARRAY_COUNT(paNotWellFormedCBOR,
2549 struct someBinaryBytes);
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002550 for(uint16_t nIterate = 0; nIterate < nArraySize; nIterate++) {
2551 const struct someBinaryBytes *pBytes = &paNotWellFormedCBOR[nIterate];
2552 const UsefulBufC Input = (UsefulBufC){pBytes->p, pBytes->n};
2553
Laurence Lundbladeee851742020-01-08 08:37:05 -08002554 // Set up decoder context. String allocator needed for indefinite
2555 // string test cases
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002556 QCBORDecodeContext DCtx;
2557 QCBORDecode_Init(&DCtx, Input, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08002558#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002559 UsefulBuf_MAKE_STACK_UB(Pool, 100);
2560 QCBORDecode_SetMemPool(&DCtx, Pool, 0);
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08002561#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002562
2563 // Loop getting items until no more to get
Laurence Lundbladef71e1622020-08-06 18:52:13 -07002564 QCBORError uCBORError;
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002565 do {
2566 QCBORItem Item;
2567
Laurence Lundbladef71e1622020-08-06 18:52:13 -07002568 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
2569 } while(uCBORError == QCBOR_SUCCESS);
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002570
2571 // Every test vector must fail with
2572 // a not-well-formed error. If not
2573 // this test fails.
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002574 if(!QCBORDecode_IsNotWellFormedError(uCBORError) &&
Laurence Lundbladef71e1622020-08-06 18:52:13 -07002575 uCBORError != QCBOR_ERR_NO_MORE_ITEMS) {
Laurence Lundblade4bf6e712020-12-10 11:53:21 -08002576 /* Return index of failure and QCBOR error in the result */
2577 return (int32_t)(nIterate * 100 + uCBORError);
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002578 }
2579 }
2580 return 0;
2581}
2582
2583
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002584struct DecodeFailTestInput {
2585 const char *szDescription; /* Description of the test */
2586 QCBORDecodeMode DecoderMode; /* The QCBOR Decoder Mode for test */
2587 UsefulBufC Input; /* Chunk of CBOR that cases error */
2588 QCBORError nError; /* The expected error */
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002589};
2590
Laurence Lundblade59289e52019-12-30 13:44:37 -08002591
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002592static int32_t
2593ProcessDecodeFailures(const struct DecodeFailTestInput *pFailInputs, const int nNumFails)
Laurence Lundblade59289e52019-12-30 13:44:37 -08002594{
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002595 int nIndex;
2596 QCBORDecodeContext DCtx;
2597 QCBORError uCBORError;
2598 QCBORItem Item;
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08002599
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002600 for(nIndex = 0; nIndex < nNumFails; nIndex++) {
2601 const struct DecodeFailTestInput *pF = &pFailInputs[nIndex];
2602
2603 QCBORDecode_Init(&DCtx, pF->Input, pF->DecoderMode);
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08002604
2605#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002606 /* Set up the decoding context including a memory pool so that
2607 * indefinite length items can be checked.
2608 */
Laurence Lundblade59289e52019-12-30 13:44:37 -08002609 UsefulBuf_MAKE_STACK_UB(Pool, 100);
Laurence Lundblade830fbf92020-05-31 17:22:33 -07002610
Laurence Lundblade4bf6e712020-12-10 11:53:21 -08002611 uCBORError = QCBORDecode_SetMemPool(&DCtx, Pool, 0);
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002612 if(uCBORError != QCBOR_SUCCESS) {
2613 return -1;
Laurence Lundblade59289e52019-12-30 13:44:37 -08002614 }
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08002615#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
2616
Laurence Lundbladecdbbc192024-06-28 15:13:04 -07002617 if(nIndex == 4) {
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002618 uCBORError = 9; /* For setting break points */
Laurence Lundblade37286c02022-09-03 10:05:02 -07002619 }
2620
Laurence Lundbladecdbbc192024-06-28 15:13:04 -07002621 /* Iterate until there is an error of some sort */
Laurence Lundblade59289e52019-12-30 13:44:37 -08002622 do {
Laurence Lundbladecdbbc192024-06-28 15:13:04 -07002623 /* Set to something non-zero, something other than QCBOR_TYPE_NONE */
Laurence Lundblade59289e52019-12-30 13:44:37 -08002624 memset(&Item, 0x33, sizeof(Item));
2625
Laurence Lundblade4bf6e712020-12-10 11:53:21 -08002626 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
2627 } while(uCBORError == QCBOR_SUCCESS);
2628
Laurence Lundbladecdbbc192024-06-28 15:13:04 -07002629 /* Must get the expected error or the this test fails.
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002630 * The data and label type must also be QCBOR_TYPE_NONE.
2631 */
Laurence Lundblade4bf6e712020-12-10 11:53:21 -08002632 if(uCBORError != pF->nError ||
Laurence Lundblade59289e52019-12-30 13:44:37 -08002633 Item.uDataType != QCBOR_TYPE_NONE ||
2634 Item.uLabelType != QCBOR_TYPE_NONE) {
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002635 return (int32_t)(nIndex * 1000 + (int)uCBORError);
Laurence Lundblade59289e52019-12-30 13:44:37 -08002636 }
2637 }
2638
2639 return 0;
2640}
2641
2642
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002643static const struct DecodeFailTestInput Failures[] = {
2644 /* Most of this is copied from not_well_formed.h. Here the error
2645 * code returned is also checked.
2646 */
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002647
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08002648#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002649 /* Indefinite length strings must be closed off */
2650 { "An indefinite length byte string not closed off",
2651 QCBOR_DECODE_MODE_NORMAL,
2652 {"0x5f\x41\x00", 3},
2653 QCBOR_ERR_HIT_END
2654 },
2655 { "An indefinite length text string not closed off",
2656 QCBOR_DECODE_MODE_NORMAL,
2657 {"\x7f\x61\x00", 3},
2658 QCBOR_ERR_HIT_END
2659 },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002660
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002661 /* All the chunks in an indefinite length string must be of the
2662 * type of indefinite length string
2663 */
2664 { "Indefinite length byte string with text string chunk",
2665 QCBOR_DECODE_MODE_NORMAL,
2666 {"\x5f\x61\x00\xff", 4},
2667 QCBOR_ERR_INDEFINITE_STRING_CHUNK
2668 },
2669 { "Indefinite length text string with a byte string chunk",
2670 QCBOR_DECODE_MODE_NORMAL,
2671 {"\x7f\x41\x00\xff", 4},
2672 QCBOR_ERR_INDEFINITE_STRING_CHUNK
2673 },
2674 { "Indefinite length byte string with a positive integer chunk",
2675 QCBOR_DECODE_MODE_NORMAL,
2676 {"\x5f\x00\xff", 3},
2677 QCBOR_ERR_INDEFINITE_STRING_CHUNK
2678 },
2679 { "Indefinite length byte string with an negative integer chunk",
2680 QCBOR_DECODE_MODE_NORMAL,
2681 {"\x5f\x21\xff", 3},
2682 QCBOR_ERR_INDEFINITE_STRING_CHUNK
2683 },
2684 { "Indefinite length byte string with an array chunk",
2685 QCBOR_DECODE_MODE_NORMAL,
2686 {"\x5f\x80\xff", 3},
2687 QCBOR_ERR_INDEFINITE_STRING_CHUNK
2688 },
2689 { "Indefinite length byte string with an map chunk",
2690 QCBOR_DECODE_MODE_NORMAL,
2691 {"\x5f\xa0\xff", 3},
2692 QCBOR_ERR_INDEFINITE_STRING_CHUNK
2693 },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002694
Laurence Lundblade37286c02022-09-03 10:05:02 -07002695#ifndef QCBOR_DISABLE_TAGS
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002696 { "Indefinite length byte string with tagged integer chunk",
2697 QCBOR_DECODE_MODE_NORMAL,
2698 {"\x5f\xc0\x00\xff", 4},
2699 QCBOR_ERR_INDEFINITE_STRING_CHUNK
2700 },
Laurence Lundblade37286c02022-09-03 10:05:02 -07002701#else
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002702 { "Indefinite length byte string with tagged integer chunk",
2703 QCBOR_DECODE_MODE_NORMAL,
2704 {"\x5f\xc0\x00\xff", 4},
2705 QCBOR_ERR_TAGS_DISABLED
2706 },
Laurence Lundblade37286c02022-09-03 10:05:02 -07002707#endif /* QCBOR_DISABLE_TAGS */
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002708
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002709 { "Indefinite length byte string with an simple type chunk",
2710 QCBOR_DECODE_MODE_NORMAL,
2711 {"\x5f\xe0\xff", 3},
2712 QCBOR_ERR_INDEFINITE_STRING_CHUNK
2713 },
2714 { "???",
2715 QCBOR_DECODE_MODE_NORMAL,
2716 {"\x5f\x5f\x41\x00\xff\xff", 6},
2717 QCBOR_ERR_INDEFINITE_STRING_CHUNK
2718 },
2719 { "indefinite length text string with indefinite string inside",
2720 QCBOR_DECODE_MODE_NORMAL,
2721 {"\x7f\x7f\x61\x00\xff\xff", 6},
2722 QCBOR_ERR_INDEFINITE_STRING_CHUNK
2723 },
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08002724#else /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
2725
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08002726#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
2727
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002728 /* Definte length maps and arrays must be closed by having the right number of items */
2729 { "A definte length array that is supposed to have 1 item, but has none",
2730 QCBOR_DECODE_MODE_NORMAL,
2731 {"\x81", 1},
2732 QCBOR_ERR_NO_MORE_ITEMS
2733 },
2734 { "A definte length array that is supposed to have 2 items, but has only 1",
2735 QCBOR_DECODE_MODE_NORMAL,
2736 {"\x82\x00", 2},
2737 QCBOR_ERR_NO_MORE_ITEMS
2738 },
2739 { "A definte length array that is supposed to have 511 items, but has only 1",
2740 QCBOR_DECODE_MODE_NORMAL,
2741 {"\x9a\x01\xff\x00", 4},
2742 QCBOR_ERR_HIT_END
2743 },
2744 { "A definte length map that is supposed to have 1 item, but has none",
2745 QCBOR_DECODE_MODE_NORMAL,
2746 {"\xa1", 1},
2747 QCBOR_ERR_NO_MORE_ITEMS
2748 },
2749 { "A definte length map that is supposed to have s item, but has only 1",
2750 QCBOR_DECODE_MODE_NORMAL,
2751 {"\xa2\x01\x02", 3},
2752 QCBOR_ERR_NO_MORE_ITEMS
2753 },
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08002754#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002755 /* Indefinte length maps and arrays must be ended by a break */
2756 { "Indefinite length array with zero items and no break",
2757 QCBOR_DECODE_MODE_NORMAL,
2758 {"\x9f", 1},
2759 QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002760
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002761 { "Indefinite length array with two items and no break",
2762 QCBOR_DECODE_MODE_NORMAL,
2763 {"\x9\x01\x02", 3},
2764 QCBOR_ERR_NO_MORE_ITEMS
2765 },
2766 { "Indefinite length map with zero items and no break",
2767 QCBOR_DECODE_MODE_NORMAL,
2768 {"\xbf", 1},
2769 QCBOR_ERR_NO_MORE_ITEMS
2770 },
2771 { "Indefinite length map with two items and no break",
2772 QCBOR_DECODE_MODE_NORMAL,
2773 {"\xbf\x01\x02\x01\x02", 5},
2774 QCBOR_ERR_NO_MORE_ITEMS
2775 },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002776
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002777 /* Nested maps and arrays must be closed off (some extra nested test vectors) */
2778 { "Unclosed indefinite array containing a closed definite length array",
2779 QCBOR_DECODE_MODE_NORMAL,
2780 {"\x9f\x80\x00", 3},
2781 QCBOR_ERR_NO_MORE_ITEMS
2782 },
2783
2784 { "Definite length array containing an unclosed indefinite length array",
2785 QCBOR_DECODE_MODE_NORMAL,
2786 {"\x81\x9f", 2},
2787 QCBOR_ERR_NO_MORE_ITEMS
2788 },
2789 { "Unclosed indefinite map containing a closed definite length array",
2790 QCBOR_DECODE_MODE_NORMAL,
2791 {"\xbf\x01\x80\x00\xa0", 5},
2792 QCBOR_ERR_NO_MORE_ITEMS
2793 },
2794 { "Definite length map containing an unclosed indefinite length array",
2795 QCBOR_DECODE_MODE_NORMAL,
2796 {"\xa1\x02\x9f", 3},
2797 QCBOR_ERR_NO_MORE_ITEMS
2798 },
2799 { "Deeply nested definite length arrays with deepest one unclosed",
2800 QCBOR_DECODE_MODE_NORMAL,
2801 {"\x81\x81\x81\x81\x81\x81\x81\x81\x81", 9},
2802 QCBOR_ERR_NO_MORE_ITEMS
2803 },
2804 { "Deeply nested indefinite length arrays with deepest one unclosed",
2805 QCBOR_DECODE_MODE_NORMAL,
2806 {"\x9f\x9f\x9f\x9f\x9f\xff\xff\xff\xff", 9},
2807 QCBOR_ERR_NO_MORE_ITEMS
2808 },
2809 { "Mixed nesting with indefinite unclosed",
2810 QCBOR_DECODE_MODE_NORMAL,
2811 {"\x9f\x81\x9f\x81\x9f\x9f\xff\xff\xff", 9},
2812 QCBOR_ERR_NO_MORE_ITEMS },
2813 { "Mixed nesting with definite unclosed",
2814 QCBOR_DECODE_MODE_NORMAL,
2815 {"\x9f\x82\x9f\x81\x9f\x9f\xff\xff\xff\xff", 10},
2816 QCBOR_ERR_BAD_BREAK
2817 },
2818 { "Unclosed indefinite length map in definite length maps",
2819 QCBOR_DECODE_MODE_NORMAL,
2820 {"\xa1\x01\xa2\x02\xbf\xff\x02\xbf", 8},
2821 QCBOR_ERR_NO_MORE_ITEMS
2822 },
2823 { "Unclosed definite length map in indefinite length maps",
2824 QCBOR_DECODE_MODE_NORMAL,
2825 {"\xbf\x01\xbf\x02\xa1", 5},
2826 QCBOR_ERR_NO_MORE_ITEMS
2827 },
2828 { "Unclosed indefinite length array in definite length maps",
2829 QCBOR_DECODE_MODE_NORMAL,
2830 {"\xa1\x01\xa2\x02\x9f\xff\x02\x9f", 8},
2831 QCBOR_ERR_NO_MORE_ITEMS
2832 },
2833 { "Unclosed definite length array in indefinite length maps",
2834 QCBOR_DECODE_MODE_NORMAL,
2835 {"\xbf\x01\xbf\x02\x81", 5},
2836 QCBOR_ERR_NO_MORE_ITEMS
2837 },
2838 { "Unclosed indefinite length map in definite length arrays",
2839 QCBOR_DECODE_MODE_NORMAL,
2840 {"\x81\x82\xbf\xff\xbf", 5},
2841 QCBOR_ERR_NO_MORE_ITEMS
2842 },
2843 { "Unclosed definite length map in indefinite length arrays",
2844 QCBOR_DECODE_MODE_NORMAL,
2845 {"\x9f\x9f\xa1", 3},
2846 QCBOR_ERR_NO_MORE_ITEMS
2847 },
2848
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08002849#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002850
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002851 /* The "argument" for the data item is incomplete */
2852 { "Positive integer missing 1 byte argument",
2853 QCBOR_DECODE_MODE_NORMAL,
2854 {"\x18", 1},
2855 QCBOR_ERR_HIT_END
2856 },
2857 { "Positive integer missing 2 byte argument",
2858 QCBOR_DECODE_MODE_NORMAL,
2859 {"\x19", 1},
2860 QCBOR_ERR_HIT_END
2861 },
2862 { "Positive integer missing 4 byte argument",
2863 QCBOR_DECODE_MODE_NORMAL,
2864 {"\x1a", 1},
2865 QCBOR_ERR_HIT_END
2866 },
2867 { "Positive integer missing 8 byte argument",
2868 QCBOR_DECODE_MODE_NORMAL,
2869 {"\x1b", 1},
2870 QCBOR_ERR_HIT_END
2871 },
2872 { "Positive integer missing 1 byte of 2 byte argument",
2873 QCBOR_DECODE_MODE_NORMAL,
2874 {"\x19\x01", 2},
2875 QCBOR_ERR_HIT_END
2876 },
2877 { "Positive integer missing 2 bytes of 4 byte argument",
2878 QCBOR_DECODE_MODE_NORMAL,
2879 {"\x1a\x01\x02", 3},
2880 QCBOR_ERR_HIT_END
2881 },
2882 { "Positive integer missing 1 bytes of 7 byte argument",
2883 QCBOR_DECODE_MODE_NORMAL,
2884 {"\x1b\x01\x02\x03\x04\x05\x06\x07", 8},
2885 QCBOR_ERR_HIT_END
2886 },
2887 { "Negative integer missing 1 byte argument",
2888 QCBOR_DECODE_MODE_NORMAL,
2889 {"\x38", 1},
2890 QCBOR_ERR_HIT_END
2891 },
2892 { "Binary string missing 1 byte argument",
2893 QCBOR_DECODE_MODE_NORMAL,
2894 {"\x58", 1},
2895 QCBOR_ERR_HIT_END
2896 },
2897 { "Text string missing 1 byte argument",
2898 QCBOR_DECODE_MODE_NORMAL,
2899 {"\x78", 1},
2900 QCBOR_ERR_HIT_END
2901 },
2902 { "Array missing 1 byte argument",
2903 QCBOR_DECODE_MODE_NORMAL,
2904 {"\x98", 1},
2905 QCBOR_ERR_HIT_END
2906 },
2907 { "Map missing 1 byte argument",
2908 QCBOR_DECODE_MODE_NORMAL,
2909 {"\xb8", 1},
2910 QCBOR_ERR_HIT_END
2911 },
2912 { "Tag missing 1 byte argument",
2913 QCBOR_DECODE_MODE_NORMAL,
2914 {"\xd8", 1},
2915 QCBOR_ERR_HIT_END
2916 },
2917 { "Simple missing 1 byte argument",
2918 QCBOR_DECODE_MODE_NORMAL,
2919 {"\xf8", 1},
2920 QCBOR_ERR_HIT_END
2921 },
2922 { "half-precision with 1 byte argument",
2923 QCBOR_DECODE_MODE_NORMAL,
2924 {"\xf9\x00", 2},
2925 QCBOR_ERR_HIT_END
2926 },
2927 { "single-precision with 2 byte argument",
2928 QCBOR_DECODE_MODE_NORMAL,
2929 {"\0xfa\x00\x00", 3},
2930 QCBOR_ERR_HIT_END
2931 },
2932 { "double-precision with 3 byte argument",
2933 QCBOR_DECODE_MODE_NORMAL,
2934 {"\xfb\x00\x00\x00", 4},
2935 QCBOR_ERR_HIT_END
2936 },
Laurence Lundblade2f467f92020-10-09 17:50:11 -07002937
Laurence Lundblade37286c02022-09-03 10:05:02 -07002938#ifndef QCBOR_DISABLE_TAGS
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002939 { "Tag with no content",
2940 QCBOR_DECODE_MODE_NORMAL,
2941 {"\xc0", 1},
2942 QCBOR_ERR_HIT_END
2943 },
Laurence Lundblade37286c02022-09-03 10:05:02 -07002944#else /* QCBOR_DISABLE_TAGS */
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002945 { "Tag with no content",
2946 QCBOR_DECODE_MODE_NORMAL,
2947 {"\xc0", 1},
2948 QCBOR_ERR_TAGS_DISABLED
2949 },
Laurence Lundblade37286c02022-09-03 10:05:02 -07002950#endif /* QCBOR_DISABLE_TAGS */
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002951
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002952 /* Breaks must not occur in definite length arrays and maps */
2953 { "Array of length 1 with sole member replaced by a break",
2954 QCBOR_DECODE_MODE_NORMAL,
2955 {"\x81\xff", 2},
2956 QCBOR_ERR_BAD_BREAK
2957 },
2958 { "Array of length 2 with 2nd member replaced by a break",
2959 QCBOR_DECODE_MODE_NORMAL,
2960 {"\x82\x00\xff", 3},
2961 QCBOR_ERR_BAD_BREAK
2962 },
2963 { "Map of length 1 with sole member label replaced by a break",
2964 QCBOR_DECODE_MODE_NORMAL,
2965 {"\xa1\xff", 2},
2966 QCBOR_ERR_BAD_BREAK
2967 },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002968
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002969 /* Map of length 1 with sole member label replaced by break */
2970 { "Alternate representation that some decoders handle differently",
2971 QCBOR_DECODE_MODE_NORMAL,
2972 {"\xa1\xff\x00", 3},
2973 QCBOR_ERR_BAD_BREAK
2974 },
2975 { "Array of length 1 with 2nd member value replaced by a break",
2976 QCBOR_DECODE_MODE_NORMAL,
2977 {"\xa1\x00\xff", 3},
2978 QCBOR_ERR_BAD_BREAK
2979 },
Laurence Lundbladeec290b82024-06-10 11:10:54 -07002980 { "Map of length 2 with 2nd entry label replaced by a break",
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002981 QCBOR_DECODE_MODE_NORMAL,
Laurence Lundbladeec290b82024-06-10 11:10:54 -07002982 {"\xa2\x00\x00\xff\x00", 5},
2983 QCBOR_ERR_BAD_BREAK
2984 },
2985 { "Map of length 2 with 2nd entry value replaced by a break",
2986 QCBOR_DECODE_MODE_NORMAL,
2987 {"\xa2\x00\x00\x01\xff", 5},
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002988 QCBOR_ERR_BAD_BREAK
2989 },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002990
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002991 /* Breaks must not occur on their own out of an indefinite length data item */
2992 { "A bare break is not well formed",
2993 QCBOR_DECODE_MODE_NORMAL,
2994 {"\xff", 1},
2995 QCBOR_ERR_BAD_BREAK
2996 },
2997 { "A bare break after a zero length definite length array",
2998 QCBOR_DECODE_MODE_NORMAL,
2999 {"\x80\xff", 2},
3000 QCBOR_ERR_BAD_BREAK
3001 },
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08003002#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08003003 { "A bare break after a zero length indefinite length map",
3004 QCBOR_DECODE_MODE_NORMAL,
3005 {"\x9f\xff\xff", 3},
3006 QCBOR_ERR_BAD_BREAK
3007 },
3008 { "A break inside a definite length array inside an indefenite length array",
3009 QCBOR_DECODE_MODE_NORMAL,
3010 {"\x9f\x81\xff", 3},
3011 QCBOR_ERR_BAD_BREAK
3012 },
3013 { "Complicated mixed nesting with break outside indefinite length array",
3014 QCBOR_DECODE_MODE_NORMAL,
3015 {"\x9f\x82\x9f\x81\x9f\x9f\xff\xff\xff\xff", 10},
3016 QCBOR_ERR_BAD_BREAK },
3017#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
3018
3019 /* Forbidden two byte encodings of simple types */
3020 { "Must use 0xe0 instead",
3021 QCBOR_DECODE_MODE_NORMAL,
3022 {"\xf8\x00", 2},
3023 QCBOR_ERR_BAD_TYPE_7
3024 },
3025 { "Should use 0xe1 instead",
3026 QCBOR_DECODE_MODE_NORMAL,
3027 {"\xf8\x01", 2},
3028 QCBOR_ERR_BAD_TYPE_7
3029 },
3030 { "Should use 0xe2 instead",
3031 QCBOR_DECODE_MODE_NORMAL,
3032 {"\xf8\x02", 2},
3033 QCBOR_ERR_BAD_TYPE_7
3034 }, { "Should use 0xe3 instead",
3035 QCBOR_DECODE_MODE_NORMAL,
3036 {"\xf8\x03", 2},
3037 QCBOR_ERR_BAD_TYPE_7
3038 },
3039 { "Should use 0xe4 instead",
3040 QCBOR_DECODE_MODE_NORMAL,
3041 {"\xf8\x04", 2},
3042 QCBOR_ERR_BAD_TYPE_7
3043 },
3044 { "Should use 0xe5 instead",
3045 QCBOR_DECODE_MODE_NORMAL,
3046 {"\xf8\x05", 2},
3047 QCBOR_ERR_BAD_TYPE_7
3048 },
3049 { "Should use 0xe6 instead",
3050 QCBOR_DECODE_MODE_NORMAL,
3051 {"\xf8\x06", 2},
3052 QCBOR_ERR_BAD_TYPE_7
3053 },
3054 { "Should use 0xe7 instead",
3055 QCBOR_DECODE_MODE_NORMAL,
3056 {"\xf8\x07", 2},
3057 QCBOR_ERR_BAD_TYPE_7
3058 },
3059 { "Should use 0xe8 instead",
3060 QCBOR_DECODE_MODE_NORMAL,
3061 {"\xf8\x08", 2},
3062 QCBOR_ERR_BAD_TYPE_7
3063 },
3064 { "Should use 0xe9 instead",
3065 QCBOR_DECODE_MODE_NORMAL,
3066 {"\xf8\x09", 2},
3067 QCBOR_ERR_BAD_TYPE_7
3068 },
3069 { "Should use 0xea instead",
3070 QCBOR_DECODE_MODE_NORMAL,
3071 {"\xf8\x0a", 2},
3072 QCBOR_ERR_BAD_TYPE_7
3073 },
3074 { "Should use 0xeb instead",
3075 QCBOR_DECODE_MODE_NORMAL,
3076 {"\xf8\x0b", 2},
3077 QCBOR_ERR_BAD_TYPE_7
3078 },
3079 { "Should use 0xec instead",
3080 QCBOR_DECODE_MODE_NORMAL,
3081 {"\xf8\x0c", 2},
3082 QCBOR_ERR_BAD_TYPE_7
3083 },
3084 { "Should use 0xed instead",
3085 QCBOR_DECODE_MODE_NORMAL,
3086 {"\xf8\x0d", 2},
3087 QCBOR_ERR_BAD_TYPE_7
3088 },
3089 { "Should use 0xee instead",
3090 QCBOR_DECODE_MODE_NORMAL,
3091 {"\xf8\x0e", 2},
3092 QCBOR_ERR_BAD_TYPE_7
3093 },
3094 { "Should use 0xef instead",
3095 QCBOR_DECODE_MODE_NORMAL,
3096 {"\xf8\x0f", 2},
3097 QCBOR_ERR_BAD_TYPE_7
3098 },
3099 { "Should use 0xf0 instead",
3100 QCBOR_DECODE_MODE_NORMAL,
3101 {"\xf8\x10", 2},
3102 QCBOR_ERR_BAD_TYPE_7
3103 },
3104 { "Should use 0xf1 instead",
3105 QCBOR_DECODE_MODE_NORMAL,
3106 {"\xf8\x11", 2},
3107 QCBOR_ERR_BAD_TYPE_7
3108 },
3109 { "Should use 0xf2 instead",
3110 QCBOR_DECODE_MODE_NORMAL,
3111 {"\xf8\x12", 2},
3112 QCBOR_ERR_BAD_TYPE_7
3113 },
3114 { "Should use 0xf3 instead",
3115 QCBOR_DECODE_MODE_NORMAL,
3116 {"\xf8\x13", 2},
3117 QCBOR_ERR_BAD_TYPE_7
3118 },
3119 { "Should use 0xf4 instead",
3120 QCBOR_DECODE_MODE_NORMAL,
3121 {"\xf8\x14", 2},
3122 QCBOR_ERR_BAD_TYPE_7
3123 },
3124 { "Should use 0xf5 instead",
3125 QCBOR_DECODE_MODE_NORMAL,
3126 {"\xf8\x15", 2},
3127 QCBOR_ERR_BAD_TYPE_7
3128 },
3129 { "Should use 0xf6 instead",
3130 QCBOR_DECODE_MODE_NORMAL,
3131 {"\xf8\x16", 2},
3132 QCBOR_ERR_BAD_TYPE_7
3133 },
3134 { "Should use 0xef7 instead",
3135 QCBOR_DECODE_MODE_NORMAL,
3136 {"\xf8\x17", 2},
3137 QCBOR_ERR_BAD_TYPE_7
3138 },
3139 { "Should use 0xef8 instead",
3140 QCBOR_DECODE_MODE_NORMAL,
3141 {"\xf8\x18", 2},
3142 QCBOR_ERR_BAD_TYPE_7
3143 },
3144 { "Reserved",
3145 QCBOR_DECODE_MODE_NORMAL,
3146 {"\xf8\x18", 2},
3147 QCBOR_ERR_BAD_TYPE_7
3148 },
3149
3150 /* Maps must have an even number of data items (key & value) */
3151 { "Map with 1 item when it should have 2",
3152 QCBOR_DECODE_MODE_NORMAL,
3153 {"\xa1\x00", 2},
3154 QCBOR_ERR_HIT_END
3155 },
3156 { "Map with 3 item when it should have 4",
3157 QCBOR_DECODE_MODE_NORMAL,
3158 {"\xa2\x00\x00\x00", 2},
3159 QCBOR_ERR_HIT_END
3160 },
3161#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
3162 { "Map with 1 item when it should have 2",
3163 QCBOR_DECODE_MODE_NORMAL,
3164 {"\xbf\x00\xff", 3},
3165 QCBOR_ERR_BAD_BREAK
3166 },
3167 { "Map with 3 item when it should have 4",
3168 QCBOR_DECODE_MODE_NORMAL,
3169 {"\xbf\x00\x00\x00\xff", 5},
3170 QCBOR_ERR_BAD_BREAK
3171 },
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08003172#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07003173
3174
Laurence Lundblade37286c02022-09-03 10:05:02 -07003175#ifndef QCBOR_DISABLE_TAGS
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08003176 /* In addition to not-well-formed, some invalid CBOR */
3177 { "Text-based date, with an integer",
3178 QCBOR_DECODE_MODE_NORMAL,
3179 {"\xc0\x00", 2},
3180 QCBOR_ERR_BAD_OPT_TAG
3181 },
3182 { "Epoch date, with an byte string",
3183 QCBOR_DECODE_MODE_NORMAL,
3184 {"\xc1\x41\x33", 3},
3185 QCBOR_ERR_BAD_OPT_TAG
3186 },
3187 { "tagged as both epoch and string dates",
3188 QCBOR_DECODE_MODE_NORMAL,
3189 {"\xc1\xc0\x00", 3},
3190 QCBOR_ERR_BAD_OPT_TAG
3191 },
3192 { "big num tagged an int, not a byte string",
3193 QCBOR_DECODE_MODE_NORMAL,
3194 {"\xc2\x00", 2},
3195 QCBOR_ERR_BAD_OPT_TAG
3196 },
Laurence Lundblade37286c02022-09-03 10:05:02 -07003197#else /* QCBOR_DISABLE_TAGS */
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08003198 /* In addition to not-well-formed, some invalid CBOR */
3199 { "Text-based date, with an integer",
3200 QCBOR_DECODE_MODE_NORMAL,
3201 {"\xc0\x00", 2},
3202 QCBOR_ERR_TAGS_DISABLED
3203 },
3204 { "Epoch date, with an byte string",
3205 QCBOR_DECODE_MODE_NORMAL,
3206 {"\xc1\x41\x33", 3},
3207 QCBOR_ERR_TAGS_DISABLED
3208 },
3209 { "tagged as both epoch and string dates",
3210 QCBOR_DECODE_MODE_NORMAL,
3211 {"\xc1\xc0\x00", 3},
3212 QCBOR_ERR_TAGS_DISABLED
3213 },
3214 { "big num tagged an int, not a byte string",
3215 QCBOR_DECODE_MODE_NORMAL,
3216 {"\xc2\x00", 2},
3217 QCBOR_ERR_TAGS_DISABLED
3218 },
Laurence Lundblade37286c02022-09-03 10:05:02 -07003219#endif /* QCBOR_DISABLE_TAGS */
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003220};
3221
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08003222
3223
3224int32_t
3225DecodeFailureTests(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003226{
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003227 int32_t nResult;
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07003228
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08003229 nResult = ProcessDecodeFailures(Failures ,C_ARRAY_COUNT(Failures, struct DecodeFailTestInput));
Laurence Lundblade59289e52019-12-30 13:44:37 -08003230 if(nResult) {
3231 return nResult;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003232 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003233
Laurence Lundblade3a6042e2019-06-28 19:58:04 -07003234 // Corrupt the UsefulInputBuf and see that
3235 // it reflected correctly for CBOR decoding
Laurence Lundbladee6f15112020-07-23 18:44:16 -07003236 QCBORDecodeContext DCtx;
3237 QCBORItem Item;
3238 QCBORError uQCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003239
Laurence Lundbladee6f15112020-07-23 18:44:16 -07003240 QCBORDecode_Init(&DCtx,
3241 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSimpleValues),
3242 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003243
Laurence Lundbladee6f15112020-07-23 18:44:16 -07003244 if((uQCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
3245 return (int32_t)uQCBORError;
3246 }
3247 if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.val.uCount != 10) {
3248 // This wasn't supposed to happen
3249 return -1;
3250 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003251
Laurence Lundbladee6f15112020-07-23 18:44:16 -07003252 DCtx.InBuf.magic = 0; // Reach in and corrupt the UsefulInputBuf
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003253
Laurence Lundbladee6f15112020-07-23 18:44:16 -07003254 uQCBORError = QCBORDecode_GetNext(&DCtx, &Item);
3255 if(uQCBORError != QCBOR_ERR_NO_MORE_ITEMS) {
3256 // Did not get back the error expected
3257 return -2;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003258 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003259
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003260
Laurence Lundblade98427e92020-09-28 21:33:23 -07003261 /*
3262 The max size of a string for QCBOR is SIZE_MAX - 4 so this
3263 tests here can be performed to see that the max length
3264 error check works correctly. See DecodeBytes(). If the max
3265 size was SIZE_MAX, it wouldn't be possible to test this.
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003266
Laurence Lundblade98427e92020-09-28 21:33:23 -07003267 This test will automatocally adapt the all CPU sizes
3268 through the use of SIZE_MAX.
3269 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003270
Laurence Lundblade8510f8c2020-12-01 11:31:16 -08003271 UsefulBuf_MAKE_STACK_UB( HeadBuf, QCBOR_HEAD_BUFFER_SIZE);
Laurence Lundblade98427e92020-09-28 21:33:23 -07003272 UsefulBufC EncodedHead;
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003273
Laurence Lundblade98427e92020-09-28 21:33:23 -07003274 // This makes a CBOR head with a text string that is very long
3275 // but doesn't fill in the bytes of the text string as that is
3276 // not needed to test this part of QCBOR.
3277 EncodedHead = QCBOREncode_EncodeHead(HeadBuf, CBOR_MAJOR_TYPE_TEXT_STRING, 0, SIZE_MAX);
3278
3279 QCBORDecode_Init(&DCtx, EncodedHead, QCBOR_DECODE_MODE_NORMAL);
3280
3281 if(QCBOR_ERR_STRING_TOO_LONG != QCBORDecode_GetNext(&DCtx, &Item)) {
3282 return -4;
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003283 }
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003284
Laurence Lundblade3a6042e2019-06-28 19:58:04 -07003285 return 0;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003286}
3287
3288
Laurence Lundbladea2e29072018-12-30 09:20:06 -08003289/* Try all 256 values of the byte at nLen including recursing for
3290 each of the values to try values at nLen+1 ... up to nLenMax
3291 */
Laurence Lundblade06350ea2020-01-27 19:32:40 -08003292static void ComprehensiveInputRecurser(uint8_t *pBuf, size_t nLen, size_t nLenMax)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003293{
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003294 if(nLen >= nLenMax) {
3295 return;
3296 }
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08003297
Laurence Lundbladea2e29072018-12-30 09:20:06 -08003298 for(int inputByte = 0; inputByte < 256; inputByte++) {
3299 // Set up the input
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003300 pBuf[nLen] = (uint8_t)inputByte;
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08003301 const UsefulBufC Input = {pBuf, nLen+1};
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08003302
Laurence Lundbladea2e29072018-12-30 09:20:06 -08003303 // Get ready to parse
3304 QCBORDecodeContext DCtx;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003305 QCBORDecode_Init(&DCtx, Input, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003306
Laurence Lundbladea2e29072018-12-30 09:20:06 -08003307 // Parse by getting the next item until an error occurs
3308 // Just about every possible decoder error can occur here
3309 // The goal of this test is not to check for the correct
3310 // error since that is not really possible. It is to
3311 // see that there is no crash on hostile input.
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003312 while(1) {
Laurence Lundbladea2e29072018-12-30 09:20:06 -08003313 QCBORItem Item;
3314 QCBORError nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003315 if(nCBORError != QCBOR_SUCCESS) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003316 break;
3317 }
3318 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003319
Laurence Lundbladea2e29072018-12-30 09:20:06 -08003320 ComprehensiveInputRecurser(pBuf, nLen+1, nLenMax);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003321 }
3322}
3323
3324
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03003325int32_t ComprehensiveInputTest(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003326{
Laurence Lundbladea2e29072018-12-30 09:20:06 -08003327 // Size 2 tests 64K inputs and runs quickly
3328 uint8_t pBuf[2];
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003329
Laurence Lundbladea2e29072018-12-30 09:20:06 -08003330 ComprehensiveInputRecurser(pBuf, 0, sizeof(pBuf));
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003331
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003332 return 0;
3333}
3334
Laurence Lundbladea2e29072018-12-30 09:20:06 -08003335
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03003336int32_t BigComprehensiveInputTest(void)
Laurence Lundbladea2e29072018-12-30 09:20:06 -08003337{
3338 // size 3 tests 16 million inputs and runs OK
3339 // in seconds on fast machines. Size 4 takes
3340 // 10+ minutes and 5 half a day on fast
3341 // machines. This test is kept separate from
3342 // the others so as to no slow down the use
3343 // of them as a very frequent regression.
3344 uint8_t pBuf[3]; //
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08003345
Laurence Lundbladea2e29072018-12-30 09:20:06 -08003346 ComprehensiveInputRecurser(pBuf, 0, sizeof(pBuf));
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08003347
Laurence Lundbladea2e29072018-12-30 09:20:06 -08003348 return 0;
3349}
3350
3351
Laurence Lundbladecc7da412020-12-27 00:09:07 -08003352static const uint8_t spDateTestInput[] = {
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003353 /* 1. The valid date string "1985-04-12" */
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003354 0xc0, // tag for string date
3355 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003356
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003357 /* 2. An invalid date string due to wrong tag content type */
Laurence Lundbladec7114722020-08-13 05:11:40 -07003358 0xc0, // tag for string date
3359 0x00, // Wrong type for a string date
3360
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003361 /* 3. A valid epoch date, 1400000000; Tue, 13 May 2014 16:53:20 GMT */
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003362 0xc1, // tag for epoch date
3363 0x1a, 0x53, 0x72, 0x4E, 0x00, // Epoch date 1400000000; Tue, 13 May 2014 16:53:20 GMT
3364
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003365 /* 4. An invalid epoch date due to wrong tag content type */
Laurence Lundbladec7114722020-08-13 05:11:40 -07003366 0xc1,
3367 0x62, 'h', 'i', // wrong type tagged
3368
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003369 /* 5. Valid epoch date tag as content for a two other nested tags */
Laurence Lundblade99615302020-11-29 11:19:47 -08003370 // CBOR_TAG_ENC_AS_B64
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003371 0xcf, 0xd8, 0x16, 0xc1, // Epoch date with extra tags
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003372 0x1a, 0x53, 0x72, 0x4E, 0x01,
3373
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003374 /* 6. Epoch date with value to large to fit into int64 */
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003375 0xc1, // tag for epoch date
3376 0x1b, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, // Too large integer
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003377
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003378 /* 7. Epoch date with single-precision value of 1.1. */
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003379 0xc1, // tag for epoch date
Laurence Lundblade3ed0bca2020-07-14 22:50:10 -07003380 0xfa, 0x3f, 0x8c, 0xcc, 0xcd, // single with value 1.1
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003381
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003382 /* 8. Epoch date with too-large single precision float */
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003383 0xc1, // tag for epoch date
Laurence Lundbladea1ad8782019-11-08 00:12:11 -08003384 0xfa, 0x7f, 0x7f, 0xff, 0xff, // 3.4028234663852886e+38 too large
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003385
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003386 /* 9. Epoch date with slightly too-large double precision value */
Laurence Lundbladea1ad8782019-11-08 00:12:11 -08003387 0xc1, // tag for epoch date
3388 0xfb, 0x43, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 9223372036854775808.000000 just barely too large
3389 //0xfa, 0x7f, 0x7f, 0xff, 0xff // 3.4028234663852886e+38 too large
3390
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003391 /* 10. Epoch date with largest supported double precision value */
Laurence Lundbladea1ad8782019-11-08 00:12:11 -08003392 0xc1, // tag for epoch date
Laurence Lundbladec7114722020-08-13 05:11:40 -07003393 0xfb, 0x43, 0xdf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, // 9223372036854773760 largest supported
3394
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003395 /* 11. Epoch date with single-precision NaN */
Laurence Lundbladec7114722020-08-13 05:11:40 -07003396 0xc1, // tag for epoch date
3397 0xfa, 0x7f, 0xc0, 0x00, 0x00, // Single-precision NaN
3398
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003399 /* 12. Epoch date with double precision plus infinity */
Laurence Lundbladec7114722020-08-13 05:11:40 -07003400 0xc1,
3401 0xfb, 0x7f, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // +infinity
3402
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003403 /* 13. Epoch date with half-precision negative infinity */
Laurence Lundbladec7114722020-08-13 05:11:40 -07003404 0xc1, // tag for epoch date
3405 0xf9, 0xfc, 0x00, // -Infinity
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003406};
3407
3408
Laurence Lundbladec7114722020-08-13 05:11:40 -07003409
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003410// have to check float expected only to within an epsilon
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07003411#ifndef QCBOR_DISABLE_FLOAT_HW_USE
Laurence Lundblade02fcf312020-07-17 02:49:46 -07003412static int CHECK_EXPECTED_DOUBLE(double val, double expected) {
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003413
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003414 double diff = val - expected;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003415
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003416 diff = fabs(diff);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003417
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003418 return diff > 0.0000001;
3419}
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07003420#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003421
3422
Laurence Lundblade37286c02022-09-03 10:05:02 -07003423/* Test date decoding using GetNext() */
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03003424int32_t DateParseTest(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003425{
3426 QCBORDecodeContext DCtx;
Laurence Lundbladec7114722020-08-13 05:11:40 -07003427 QCBORItem Item;
3428 QCBORError uError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003429
Laurence Lundbladeee851742020-01-08 08:37:05 -08003430 QCBORDecode_Init(&DCtx,
3431 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spDateTestInput),
3432 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003433
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003434 /* 1. The valid date string "1985-04-12" */
Laurence Lundbladec7114722020-08-13 05:11:40 -07003435 if((uError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003436 return -1;
Laurence Lundbladec7114722020-08-13 05:11:40 -07003437 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003438 if(Item.uDataType != QCBOR_TYPE_DATE_STRING ||
Laurence Lundbladeba587682024-02-07 16:46:43 -08003439 UsefulBufCompareToSZ(Item.val.string, "1985-04-12")){
Laurence Lundblade67bd5512018-11-02 21:44:06 +07003440 return -2;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003441 }
3442
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003443 /* 2. An invalid date string due to wrong tag content type */
Laurence Lundbladec7114722020-08-13 05:11:40 -07003444 uError = QCBORDecode_GetNext(&DCtx, &Item);
3445 if(uError != QCBOR_ERR_BAD_OPT_TAG) {
Laurence Lundblade67bd5512018-11-02 21:44:06 +07003446 return -3;
Laurence Lundbladec7114722020-08-13 05:11:40 -07003447 }
3448
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003449 /* 3. A valid epoch date, 1400000000; Tue, 13 May 2014 16:53:20 GMT */
3450 uError = QCBORDecode_GetNext(&DCtx, &Item);
3451 if(uError != QCBOR_SUCCESS) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07003452 return -4;
3453 }
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003454 if(uError == QCBOR_SUCCESS) {
3455 if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH ||
3456 Item.val.epochDate.nSeconds != 1400000000
Máté Tóth-Pálef5f07a2021-09-17 19:31:37 +02003457#ifndef USEFULBUF_DISABLE_ALL_FLOAT
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003458 || Item.val.epochDate.fSecondsFraction != 0
Máté Tóth-Pálef5f07a2021-09-17 19:31:37 +02003459#endif /* USEFULBUF_DISABLE_ALL_FLOAT */
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003460 ) {
3461 return -5;
3462 }
Laurence Lundbladec7114722020-08-13 05:11:40 -07003463 }
3464
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003465 /* 4. An invalid epoch date due to wrong tag content type */
Laurence Lundbladec7114722020-08-13 05:11:40 -07003466 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_OPT_TAG) {
3467 return -6;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003468 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003469
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003470 /* 5. Valid epoch date tag as content for a two other nested tags */
Laurence Lundblade99615302020-11-29 11:19:47 -08003471 // Epoch date wrapped in an CBOR_TAG_ENC_AS_B64 and an unknown tag.
3472 // The date is decoded and the two tags are returned. This is to
3473 // make sure the wrapping of epoch date in another tag works OK.
Laurence Lundbladec7114722020-08-13 05:11:40 -07003474 if((uError = QCBORDecode_GetNext(&DCtx, &Item))) {
3475 return -7;
3476 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003477 if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH ||
3478 Item.val.epochDate.nSeconds != 1400000001 ||
Máté Tóth-Pálef5f07a2021-09-17 19:31:37 +02003479#ifndef USEFULBUF_DISABLE_ALL_FLOAT
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003480 Item.val.epochDate.fSecondsFraction != 0 ||
Máté Tóth-Pálef5f07a2021-09-17 19:31:37 +02003481#endif /* USEFULBUF_DISABLE_ALL_FLOAT */
Laurence Lundblade99615302020-11-29 11:19:47 -08003482 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_ENC_AS_B64)) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07003483 return -8;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003484 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003485
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003486 /* 6. Epoch date with value to large to fit into int64 */
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003487 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_DATE_OVERFLOW) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07003488 return -9;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003489 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003490
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003491 /* 7. Epoch date with single-precision value of 1.1. */
3492 uError = QCBORDecode_GetNext(&DCtx, &Item);
3493 if(uError != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07003494 return -10;
3495 }
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003496 if(uError == QCBOR_SUCCESS) {
3497 if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH ||
3498 Item.val.epochDate.nSeconds != 1
3499#ifndef QCBOR_DISABLE_FLOAT_HW_USE
3500 || CHECK_EXPECTED_DOUBLE(Item.val.epochDate.fSecondsFraction, 0.1)
Máté Tóth-Pálef5f07a2021-09-17 19:31:37 +02003501#endif /* USEFULBUF_DISABLE_ALL_FLOAT */
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003502 ) {
3503 return -11;
3504 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003505 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003506
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003507 /* 8. Epoch date with too-large single-precision float */
3508 uError = QCBORDecode_GetNext(&DCtx, &Item);
3509 if(uError != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_DATE_OVERFLOW)) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07003510 return -12;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003511 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003512
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003513 /* 9. Epoch date with slightly too-large double-precision value */
3514 uError = QCBORDecode_GetNext(&DCtx, &Item);
3515 if(uError != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_DATE_OVERFLOW)) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07003516 return -13;
Laurence Lundbladea1ad8782019-11-08 00:12:11 -08003517 }
3518
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003519 /* 10. Epoch date with largest supported double-precision value */
3520 uError = QCBORDecode_GetNext(&DCtx, &Item);
3521 if(uError != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)) {
3522 return -14;
3523 }
3524 if(uError == QCBOR_SUCCESS) {
3525 if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH ||
3526 Item.val.epochDate.nSeconds != 9223372036854773760
3527#ifndef QCBOR_DISABLE_FLOAT_HW_USE
3528 || Item.val.epochDate.fSecondsFraction != 0.0
3529#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
3530 ) {
3531 return -14;
3532 }
3533 }
Laurence Lundbladec7114722020-08-13 05:11:40 -07003534
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003535 /* 11. Epoch date with single-precision NaN */
3536 if(QCBORDecode_GetNext(&DCtx, &Item) != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_DATE_OVERFLOW)) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07003537 return -15;
3538 }
3539
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003540 /* 12. Epoch date with double-precision plus infinity */
3541 if(QCBORDecode_GetNext(&DCtx, &Item) != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_DATE_OVERFLOW)) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07003542 return -16;
3543 }
3544
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003545 /* 13. Epoch date with half-precision negative infinity */
3546 uError = QCBORDecode_GetNext(&DCtx, &Item);
3547 if(uError != FLOAT_ERR_CODE_NO_HALF_PREC_NO_FLOAT_HW(QCBOR_ERR_DATE_OVERFLOW)) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07003548 return -17;
3549 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003550
3551 return 0;
3552}
3553
Laurence Lundblade37286c02022-09-03 10:05:02 -07003554
Laurence Lundblade4b270642020-08-14 12:53:07 -07003555/*
3556 Test cases covered here. Some items cover more than one of these.
3557 positive integer (zero counts as a positive integer)
3558 negative integer
3559 half-precision float
3560 single-precision float
3561 double-precision float
Laurence Lundbladec7114722020-08-13 05:11:40 -07003562
Laurence Lundblade4b270642020-08-14 12:53:07 -07003563 float Overflow error
3564 Wrong type error for epoch
3565 Wrong type error for date string
3566 float disabled error
3567 half-precision disabled error
3568 -Infinity
3569 Slightly too large integer
3570 Slightly too far from zero
Laurence Lundbladec7114722020-08-13 05:11:40 -07003571
Laurence Lundblade4b270642020-08-14 12:53:07 -07003572 Get epoch by int
3573 Get string by int
3574 Get epoch by string
3575 Get string by string
3576 Fail to get epoch by wrong int label
3577 Fail to get string by wrong string label
3578 Fail to get epoch by string because it is invalid
3579 Fail to get epoch by int because it is invalid
3580
3581 Untagged values
3582 */
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08003583static const uint8_t spSpiffyDateTestInput[] = {
Laurence Lundblade37286c02022-09-03 10:05:02 -07003584 0x87, // array of 7 items
3585
3586 0xa6, // Open a map for tests involving untagged items with labels.
3587
3588 // Untagged integer 0
3589 0x08,
3590 0x00,
3591
3592 // Utagged date string with string label y
3593 0x61, 0x79,
3594 0x6a, '2','0','8','5','-','0','4','-','1','2', // Untagged date string
3595
3596 // Untagged single-precision float with value 3.14 with string label x
3597 0x61, 0x78,
3598 0xFA, 0x40, 0x48, 0xF5, 0xC3,
3599
3600 // Untagged half-precision float with value -2
3601 0x09,
3602 0xF9, 0xC0, 0x00,
3603
3604 /* Untagged date-only date string */
3605 0x18, 0x63,
3606 0x6A, 0x31, 0x39, 0x38, 0x35, 0x2D, 0x30, 0x34, 0x2D, 0x31, 0x32, /* "1985-04-12" */
3607
3608 /* Untagged days-count epoch date */
3609 0x11,
3610 0x19, 0x0F, 0x9A, /* 3994 */
3611
3612 // End of map, back to array
3613
3614 0xa7, // Open map of tagged items with labels
3615
3616 0x00,
3617 0xc0, // tag for string date
3618 0x6a, '1','9','8','5','-','0','4','-','1','2', // Tagged date string
3619
3620
3621 0x01,
3622 0xda, 0x03, 0x03, 0x03, 0x03, // An additional tag
3623 0xc1, // tag for epoch date
3624 0x1a, 0x53, 0x72, 0x4E, 0x00, // Epoch date 1400000000; Tue, 13 May 2014 16:53:20 GMT
3625
3626 0x05,
3627 0xc1,
3628 0xfb, 0xc3, 0xdf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, // -9223372036854773760 largest negative
3629
3630
3631 0x07,
3632 0xc1, // tag for epoch date
3633 0xfb, 0x43, 0xdf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, // 9223372036854773760 largest supported
3634
3635 /* Tagged days-count epoch date */
3636 0x63, 0x53, 0x44, 0x45,
3637 0xD8, 0x64, /* tag(100) */
3638 0x39, 0x29, 0xB3, /* -10676 */
3639
3640 // Untagged -1000 with label z
3641 0x61, 0x7a,
3642 0xda, 0x01, 0x01, 0x01, 0x01, // An additional tag
3643 0x39, 0x03, 0xe7,
3644
3645 /* Tagged date-only date string */
3646 0x63, 0x53, 0x44, 0x53,
3647 0xD9, 0x03, 0xEC,
3648 0x6A, 0x31, 0x39, 0x38, 0x35, 0x2D, 0x30, 0x34, 0x2D, 0x31, 0x32, /* "1985-04-12" */
3649
3650 // End of map of tagged items
Laurence Lundblade4b270642020-08-14 12:53:07 -07003651
3652 0xc1,
3653 0xfb, 0xc3, 0xdf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, // -9.2233720368547748E+18, too negative
3654
Laurence Lundbladec7114722020-08-13 05:11:40 -07003655 0xc1, // tag for epoch date
Laurence Lundblade4b270642020-08-14 12:53:07 -07003656 0x1b, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, // Too-large integer
3657
3658 0xc1, // tag for epoch date
3659 0xf9, 0xfc, 0x00, // Half-precision -Infinity
3660
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07003661 // These two at the end because they are unrecoverable errors
3662 0xc1, // tag for epoch date
3663 0x80, // Erroneous empty array as content for date
3664
3665 0xc0, // tag for string date
3666 0xa0 // Erroneous empty map as content for date
Laurence Lundbladec7114722020-08-13 05:11:40 -07003667};
3668
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03003669int32_t SpiffyDateDecodeTest(void)
Laurence Lundbladec7114722020-08-13 05:11:40 -07003670{
3671 QCBORDecodeContext DC;
Laurence Lundblade4b270642020-08-14 12:53:07 -07003672 QCBORError uError;
Laurence Lundblade37286c02022-09-03 10:05:02 -07003673 int64_t nEpochDate3, nEpochDate5,
3674 nEpochDate4, nEpochDate6,
3675 nEpochDays2;
3676 UsefulBufC StringDate1, StringDate2, StringDays2;
Laurence Lundbladec7114722020-08-13 05:11:40 -07003677
3678 QCBORDecode_Init(&DC,
Laurence Lundblade4b270642020-08-14 12:53:07 -07003679 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSpiffyDateTestInput),
Laurence Lundbladec7114722020-08-13 05:11:40 -07003680 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade37286c02022-09-03 10:05:02 -07003681
3682 /* Items are in an array or map to test look up by label and other
3683 * that might not occur in isolated items. But it does make the
3684 * test a bit messy. */
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07003685 QCBORDecode_EnterArray(&DC, NULL);
Laurence Lundbladec7114722020-08-13 05:11:40 -07003686
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07003687 QCBORDecode_EnterMap(&DC, NULL);
Laurence Lundblade4b270642020-08-14 12:53:07 -07003688
Laurence Lundblade4b270642020-08-14 12:53:07 -07003689 // A single-precision date
Laurence Lundblade9b334962020-08-27 10:55:53 -07003690 QCBORDecode_GetEpochDateInMapSZ(&DC, "x", QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
3691 &nEpochDate5);
Laurence Lundblade4b270642020-08-14 12:53:07 -07003692 uError = QCBORDecode_GetAndResetError(&DC);
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003693 if(uError != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07003694 return 104;
3695 }
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003696 if(uError == QCBOR_SUCCESS) {
3697 if(nEpochDate5 != 3) {
3698 return 103;
3699 }
3700 }
Laurence Lundbladec7114722020-08-13 05:11:40 -07003701
Laurence Lundblade9b334962020-08-27 10:55:53 -07003702 // A half-precision date with value -2 FFF
3703 QCBORDecode_GetEpochDateInMapN(&DC, 9, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
3704 &nEpochDate4);
Laurence Lundblade4b270642020-08-14 12:53:07 -07003705 uError = QCBORDecode_GetAndResetError(&DC);
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003706 if(uError != FLOAT_ERR_CODE_NO_HALF_PREC_NO_FLOAT_HW(QCBOR_SUCCESS)) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07003707 return 106;
3708 }
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003709 if(uError == QCBOR_SUCCESS) {
3710 if(nEpochDate4 != -2) {
3711 return 105;
3712 }
3713 }
Laurence Lundblade4b270642020-08-14 12:53:07 -07003714
3715 // Fail to get an epoch date by string label
Laurence Lundblade9b334962020-08-27 10:55:53 -07003716 QCBORDecode_GetEpochDateInMapSZ(&DC, "no-label",
3717 QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
3718 &nEpochDate6);
Laurence Lundblade4b270642020-08-14 12:53:07 -07003719 uError = QCBORDecode_GetAndResetError(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07003720 if(uError != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07003721 return 107;
3722 }
3723
3724 // Fail to get an epoch date by integer label
Laurence Lundblade9b334962020-08-27 10:55:53 -07003725 QCBORDecode_GetEpochDateInMapN(&DC, 99999, QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
3726 &nEpochDate6);
Laurence Lundblade4b270642020-08-14 12:53:07 -07003727 uError = QCBORDecode_GetAndResetError(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07003728 if(uError != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07003729 return 108;
3730 }
3731
3732 // Fail to get a string date by string label
Laurence Lundblade9b334962020-08-27 10:55:53 -07003733 QCBORDecode_GetDateStringInMapSZ(&DC, "no-label",
3734 QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
3735 &StringDate1);
Laurence Lundblade4b270642020-08-14 12:53:07 -07003736 uError = QCBORDecode_GetAndResetError(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07003737 if(uError != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07003738 return 109;
3739 }
3740
3741 // Fail to get a string date by integer label
Laurence Lundblade9b334962020-08-27 10:55:53 -07003742 QCBORDecode_GetDateStringInMapN(&DC, 99999, QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
3743 &StringDate1);
Laurence Lundblade4b270642020-08-14 12:53:07 -07003744 uError = QCBORDecode_GetAndResetError(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07003745 if(uError != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07003746 return 110;
3747 }
3748
3749 // The rest of these succeed even if float features are disabled
Laurence Lundbladea9489f82020-09-12 13:50:56 -07003750
Laurence Lundblade37286c02022-09-03 10:05:02 -07003751
3752 // Untagged integer 0
3753 QCBORDecode_GetEpochDateInMapN(&DC, 8, QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
3754 &nEpochDate3);
3755 // Untagged date string
3756 QCBORDecode_GetDateStringInMapSZ(&DC, "y", QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
3757 &StringDate2);
3758
3759 QCBORDecode_GetDaysStringInMapN(&DC, 99, QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
3760 &StringDays2);
3761
3762 QCBORDecode_GetEpochDaysInMapN(&DC, 17, QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
3763 &nEpochDays2);
3764
3765 QCBORDecode_ExitMap(&DC);
3766 if(QCBORDecode_GetError(&DC) != QCBOR_SUCCESS) {
3767 return 3001;
3768 }
3769
3770 // The map of tagged items
3771 QCBORDecode_EnterMap(&DC, NULL);
3772
3773#ifndef QCBOR_DISABLE_TAGS
3774 int64_t nEpochDate2,
3775 nEpochDateFail,
3776 nEpochDate1400000000, nEpochDays1;
3777 UsefulBufC StringDays1;
3778 uint64_t uTag1, uTag2;
3779
3780 // Tagged date string
3781 QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
3782 &StringDate1);
3783
Laurence Lundblade4b270642020-08-14 12:53:07 -07003784 // Epoch date 1400000000; Tue, 13 May 2014 16:53:20 GMT
Laurence Lundblade9b334962020-08-27 10:55:53 -07003785 QCBORDecode_GetEpochDateInMapN(&DC,
3786 1,
3787 QCBOR_TAG_REQUIREMENT_TAG |
3788 QCBOR_TAG_REQUIREMENT_ALLOW_ADDITIONAL_TAGS,
3789 &nEpochDate1400000000);
3790 uTag1 = QCBORDecode_GetNthTagOfLast(&DC, 0);
Laurence Lundblade37286c02022-09-03 10:05:02 -07003791
3792 // Get largest negative double precision epoch date allowed
3793 QCBORDecode_GetEpochDateInMapN(&DC,
3794 5,
3795 QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG |
3796 QCBOR_TAG_REQUIREMENT_ALLOW_ADDITIONAL_TAGS,
3797 &nEpochDate2);
3798 uError = QCBORDecode_GetAndResetError(&DC);
3799 if(uError != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)) {
3800 return 102;
3801 }
3802 if(uError == QCBOR_SUCCESS) {
3803 if(nEpochDate2 != -9223372036854773760LL) {
3804 return 101;
3805 }
3806 }
3807
Laurence Lundblade4b270642020-08-14 12:53:07 -07003808 // Untagged -1000 with label z
Laurence Lundblade9b334962020-08-27 10:55:53 -07003809 QCBORDecode_GetEpochDateInMapSZ(&DC,
3810 "z",
3811 QCBOR_TAG_REQUIREMENT_NOT_A_TAG |
3812 QCBOR_TAG_REQUIREMENT_ALLOW_ADDITIONAL_TAGS,
3813 &nEpochDate6);
3814 uTag2 = QCBORDecode_GetNthTagOfLast(&DC, 0);
Laurence Lundblade4b270642020-08-14 12:53:07 -07003815
Laurence Lundblade37286c02022-09-03 10:05:02 -07003816
3817 // Get largest double precision epoch date allowed
3818 QCBORDecode_GetEpochDateInMapN(&DC, 7, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
3819 &nEpochDate2);
3820 uError = QCBORDecode_GetAndResetError(&DC);
3821 if(uError != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)) {
3822 return 112;
3823 }
3824 if(uError == QCBOR_SUCCESS) {
3825 if(nEpochDate2 != 9223372036854773760ULL) {
3826 return 111;
3827 }
3828 }
3829
Laurence Lundblade46d63e92021-05-13 11:37:10 -07003830 /* The days format is much simpler than the date format
3831 * because it can't be a floating point value. The test
3832 * of the spiffy decode functions sufficiently covers
3833 * the test of the non-spiffy decode days date decoding.
3834 * There is no full fan out of the error conditions
3835 * and decode options as that is implemented by code
3836 * that is tested well by the date testing above.
3837 */
3838 QCBORDecode_GetDaysStringInMapSZ(&DC, "SDS", QCBOR_TAG_REQUIREMENT_TAG,
3839 &StringDays1);
3840
Laurence Lundblade46d63e92021-05-13 11:37:10 -07003841 QCBORDecode_GetEpochDaysInMapSZ(&DC, "SDE", QCBOR_TAG_REQUIREMENT_TAG,
3842 &nEpochDays1);
3843
Laurence Lundblade4b270642020-08-14 12:53:07 -07003844 QCBORDecode_ExitMap(&DC);
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07003845 if(QCBORDecode_GetError(&DC) != QCBOR_SUCCESS) {
3846 return 3001;
3847 }
3848
Laurence Lundblade37286c02022-09-03 10:05:02 -07003849 // Too-negative float, -9.2233720368547748E+18
3850 QCBORDecode_GetEpochDate(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nEpochDateFail);
3851 uError = QCBORDecode_GetAndResetError(&DC);
3852 if(uError != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_DATE_OVERFLOW)) {
3853 return 1111;
3854 }
3855
3856 // Too-large integer
3857 QCBORDecode_GetEpochDate(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nEpochDateFail);
3858 uError = QCBORDecode_GetAndResetError(&DC);
3859 if(uError != QCBOR_ERR_DATE_OVERFLOW) {
3860 return 1;
3861 }
3862
3863 // Half-precision minus infinity
3864 QCBORDecode_GetEpochDate(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nEpochDateFail);
3865 uError = QCBORDecode_GetAndResetError(&DC);
3866 if(uError != FLOAT_ERR_CODE_NO_HALF_PREC_NO_FLOAT_HW(QCBOR_ERR_DATE_OVERFLOW)) {
3867 return 2;
3868 }
3869
3870
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07003871 // Bad content for epoch date
3872 QCBORDecode_GetEpochDate(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nEpochDateFail);
3873 uError = QCBORDecode_GetAndResetError(&DC);
3874 if(uError != QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT) {
3875 return 3;
3876 }
3877
3878 // Bad content for string date
3879 QCBORDecode_GetDateString(&DC, QCBOR_TAG_REQUIREMENT_TAG, &StringDate1);
3880 uError = QCBORDecode_GetAndResetError(&DC);
3881 if(uError != QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT) {
3882 return 4;
3883 }
3884
Laurence Lundblade4b270642020-08-14 12:53:07 -07003885 QCBORDecode_ExitArray(&DC);
3886 uError = QCBORDecode_Finish(&DC);
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07003887 if(uError != QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT) {
Laurence Lundblade9b334962020-08-27 10:55:53 -07003888 return 1000 + (int32_t)uError;
Laurence Lundblade4b270642020-08-14 12:53:07 -07003889 }
Laurence Lundblade37286c02022-09-03 10:05:02 -07003890#else /* QCBOR_DISABLE_TAGS */
3891 QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
3892 &StringDate1);
3893 uError = QCBORDecode_GetAndResetError(&DC);
3894 if(uError != QCBOR_ERR_TAGS_DISABLED) {
3895 return 4;
3896 }
3897#endif /* QCBOR_DISABLE_TAGS */
3898
3899
3900#ifndef QCBOR_DISABLE_TAGS
Laurence Lundblade4b270642020-08-14 12:53:07 -07003901
Laurence Lundblade9b334962020-08-27 10:55:53 -07003902 if(nEpochDate1400000000 != 1400000000) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07003903 return 200;
3904 }
3905
Laurence Lundblade9b334962020-08-27 10:55:53 -07003906 if(uTag1 != 0x03030303) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07003907 return 201;
3908 }
3909
Laurence Lundblade37286c02022-09-03 10:05:02 -07003910 if(nEpochDays1 != -10676) {
3911 return 205;
Laurence Lundblade4b270642020-08-14 12:53:07 -07003912 }
3913
Laurence Lundblade37286c02022-09-03 10:05:02 -07003914 if(UsefulBuf_Compare(StringDays1, UsefulBuf_FromSZ("1985-04-12"))) {
3915 return 207;
Laurence Lundblade4b270642020-08-14 12:53:07 -07003916 }
3917
Laurence Lundblade9b334962020-08-27 10:55:53 -07003918 if(uTag2 != 0x01010101) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07003919 return 204;
3920 }
3921
Laurence Lundblade37286c02022-09-03 10:05:02 -07003922 if(nEpochDate6 != -1000) {
3923 return 203;
Laurence Lundblade46d63e92021-05-13 11:37:10 -07003924 }
3925
Laurence Lundblade9b334962020-08-27 10:55:53 -07003926 if(UsefulBuf_Compare(StringDate1, UsefulBuf_FromSZ("1985-04-12"))) {
3927 return 205;
3928 }
3929
Laurence Lundblade37286c02022-09-03 10:05:02 -07003930#endif /* QCBOR_DISABLE_TAGS */
3931
3932 if(nEpochDate3 != 0) {
3933 return 202;
3934 }
3935
3936 if(nEpochDays2 != 3994) {
Laurence Lundblade9b334962020-08-27 10:55:53 -07003937 return 206;
3938 }
3939
Laurence Lundblade37286c02022-09-03 10:05:02 -07003940 if(UsefulBuf_Compare(StringDate2, UsefulBuf_FromSZ("2085-04-12"))) {
3941 return 206;
Laurence Lundblade46d63e92021-05-13 11:37:10 -07003942 }
3943
3944 if(UsefulBuf_Compare(StringDays2, UsefulBuf_FromSZ("1985-04-12"))) {
3945 return 208;
3946 }
3947
Laurence Lundbladec7114722020-08-13 05:11:40 -07003948 return 0;
3949}
3950
3951
Laurence Lundblade9b334962020-08-27 10:55:53 -07003952// Input for one of the tagging tests
Laurence Lundbladecc7da412020-12-27 00:09:07 -08003953static const uint8_t spTagInput[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003954 0xd9, 0xd9, 0xf7, // CBOR magic number
Laurence Lundblade9b334962020-08-27 10:55:53 -07003955 0x81, // Array of one
3956 0xd8, 0x04, // non-preferred serialization of tag 4, decimal fraction
3957 0x82, // Array of two that is the faction 1/3
3958 0x01,
3959 0x03,
3960
3961 /*
3962 More than 4 tags on an item 225(226(227(228(229([])))))
3963 */
3964 0xd8, 0xe1,
3965 0xd8, 0xe2,
3966 0xd8, 0xe3,
3967 0xd8, 0xe4,
3968 0xd8, 0xe5,
3969 0x80,
3970
3971 /* tag 10489608748473423768(
3972 2442302356(
3973 21590(
3974 240(
3975 []))))
3976 */
3977 0xdb, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98,
3978 0xda, 0x91, 0x92, 0x93, 0x94,
3979 0xd9, 0x54, 0x56,
3980 0xd8, 0xf0,
3981 0x80,
3982
3983 /* tag 21590(
3984 10489608748473423768(
3985 2442302357(
3986 65534(
3987 []))))
3988 */
3989 0xdb, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x54, 0x56,
3990 0xdb, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98,
3991 0xda, 0x91, 0x92, 0x93, 0x95,
3992 0xd9, 0xff, 0xfe,
3993 0x80,
3994
3995 /* Make sure to blow past the limit of tags that must be mapped.
3996 works in conjuntion with entries above.
3997 269488144(269488145(269488146(269488147([]))))
3998 */
3999 0xda, 0x10, 0x10, 0x10, 0x10,
4000 0xda, 0x10, 0x10, 0x10, 0x11,
4001 0xda, 0x10, 0x10, 0x10, 0x12,
4002 0xda, 0x10, 0x10, 0x10, 0x13,
4003 0x80,
4004
4005 /* An invalid decimal fraction with an additional tag */
4006 0xd9, 0xff, 0xfa,
4007 0xd8, 0x02, // non-preferred serialization of tag 2, a big num
4008 0x00, // the integer 0; should be a byte string
4009};
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004010
Laurence Lundblade59289e52019-12-30 13:44:37 -08004011/*
4012 DB 9192939495969798 # tag(10489608748473423768)
Laurence Lundblade9b334962020-08-27 10:55:53 -07004013 80 # array(0)
Laurence Lundblade59289e52019-12-30 13:44:37 -08004014 */
Laurence Lundbladecc7da412020-12-27 00:09:07 -08004015static const uint8_t spEncodedLargeTag[] = {0xdb, 0x91, 0x92, 0x93, 0x94, 0x95,
Laurence Lundbladeee851742020-01-08 08:37:05 -08004016 0x96, 0x97, 0x98, 0x80};
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004017
Laurence Lundblade59289e52019-12-30 13:44:37 -08004018/*
4019DB 9192939495969798 # tag(10489608748473423768)
4020 D8 88 # tag(136)
4021 C6 # tag(6)
4022 C7 # tag(7)
4023 80 # array(0)
4024*/
Laurence Lundbladecc7da412020-12-27 00:09:07 -08004025static const uint8_t spLotsOfTags[] = {0xdb, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96,
Laurence Lundbladeee851742020-01-08 08:37:05 -08004026 0x97, 0x98, 0xd8, 0x88, 0xc6, 0xc7, 0x80};
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004027
4028/*
Laurence Lundblade9b334962020-08-27 10:55:53 -07004029 55799(55799(55799({
4030 6(7(-23)): 5859837686836516696(7({
4031 7(-20): 11({
4032 17(-18): 17(17(17("Organization"))),
4033 9(-17): 773("SSG"),
4034 -15: 16(17(6(7("Confusion")))),
4035 17(-16): 17("San Diego"),
4036 17(-14): 17("US")
4037 }),
4038 23(-19): 19({
4039 -11: 9({
4040 -9: -7
4041 }),
4042 90599561(90599561(90599561(-10))): 12(h'0102030405060708090A')
4043 })
4044 })),
4045 16(-22): 23({
4046 11(8(7(-5))): 8(-3)
4047 })
4048 })))
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004049 */
Laurence Lundbladecc7da412020-12-27 00:09:07 -08004050static const uint8_t spCSRWithTags[] = {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004051 0xd9, 0xd9, 0xf7, 0xd9, 0xd9, 0xf7, 0xd9, 0xd9, 0xf7, 0xa2,
4052 0xc6, 0xc7, 0x36,
4053 0xdb, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0xc7, 0xa2,
4054 0xda, 0x00, 0x00, 0x00, 0x07, 0x33,
4055 0xcb, 0xa5,
4056 0xd1, 0x31,
4057 0xd1, 0xd1, 0xd1, 0x6c,
4058 0x4f, 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74, 0x69, 0x6f, 0x6e,
4059 0xc9, 0x30,
4060 0xd9, 0x03, 0x05, 0x63,
4061 0x53, 0x53, 0x47,
4062 0x2e,
Laurence Lundblade9b334962020-08-27 10:55:53 -07004063 0xd0, 0xd1, 0xc6, 0xc7,
4064 0x69,
4065 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73, 0x69, 0x6f, 0x6e,
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004066 0xd1, 0x2f,
4067 0xd1, 0x69,
4068 0x53, 0x61, 0x6e, 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f,
4069 0xd1, 0x2d,
4070 0xd1, 0x62,
4071 0x55, 0x53,
4072 0xd7, 0x32,
4073 0xd3, 0xa2,
4074 0x2a,
4075 0xc9, 0xa1,
4076 0x28,
4077 0x26,
4078 0xda, 0x05, 0x66, 0x70, 0x89, 0xda, 0x05, 0x66, 0x70, 0x89, 0xda, 0x05, 0x66, 0x70, 0x89, 0x29,
4079 0xcc, 0x4a,
4080 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,0x07, 0x08, 0x09, 0x0a,
4081 0xd0, 0x35,
4082 0xd7, 0xa1,
4083 0xcb, 0xc8, 0xc7, 0x24,
4084 0xc8, 0x22};
4085
Laurence Lundblade9b334962020-08-27 10:55:53 -07004086
Laurence Lundbladecc7da412020-12-27 00:09:07 -08004087static const uint8_t spSpiffyTagInput[] = {
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08004088 0x85, // Open array
Laurence Lundblade9b334962020-08-27 10:55:53 -07004089
4090 0xc0, // tag for string date
4091 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string
4092
4093 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string
4094
4095 0x4a, '1','9','8','5','-','0','4','-','1','2', // Date string in byte string
4096
4097 0xd8, 0x23, // tag for regex
4098 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string
4099
4100 0xc0, // tag for string date
4101 0x4a, '1','9','8','5','-','0','4','-','1','2', // Date string in byte string
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07004102
4103 // This last case makes the array untraversable because it is
4104 // an uncrecoverable error. Make sure it stays last and is the only
4105 // instance so the other tests can work.
Laurence Lundblade9b334962020-08-27 10:55:53 -07004106};
4107
4108
Laurence Lundbladec5fef682020-01-25 11:38:45 -08004109static int32_t CheckCSRMaps(QCBORDecodeContext *pDC);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004110
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004111
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03004112int32_t OptTagParseTest(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004113{
4114 QCBORDecodeContext DCtx;
Laurence Lundblade9b334962020-08-27 10:55:53 -07004115 QCBORItem Item;
4116 QCBORError uError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004117
Laurence Lundbladeee851742020-01-08 08:37:05 -08004118 QCBORDecode_Init(&DCtx,
Laurence Lundblade9b334962020-08-27 10:55:53 -07004119 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTagInput),
Laurence Lundbladeee851742020-01-08 08:37:05 -08004120 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004121
Laurence Lundblade9b334962020-08-27 10:55:53 -07004122 /*
4123 This test matches the magic number tag and the fraction tag
4124 55799([...])
4125 */
4126 uError = QCBORDecode_GetNext(&DCtx, &Item);
4127 if(uError != QCBOR_SUCCESS) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004128 return -2;
4129 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004130 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004131 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC)) {
4132 return -3;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004133 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004134
Laurence Lundblade9b334962020-08-27 10:55:53 -07004135 /*
4136 4([1,3])
4137 */
4138 uError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundbladedd6e76e2021-03-10 01:54:01 -07004139#ifdef QCBOR_DISABLE_EXP_AND_MANTISSA
Laurence Lundblade9b334962020-08-27 10:55:53 -07004140 if(uError != QCBOR_SUCCESS ||
4141 Item.uDataType != QCBOR_TYPE_ARRAY ||
4142 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_DECIMAL_FRACTION) ||
4143 QCBORDecode_GetNthTag(&DCtx, &Item, 0) != CBOR_TAG_DECIMAL_FRACTION ||
4144 QCBORDecode_GetNthTag(&DCtx, &Item, 1) != CBOR_TAG_INVALID64 ||
4145 QCBORDecode_GetNthTag(&DCtx, &Item, 2) != CBOR_TAG_INVALID64 ||
4146 QCBORDecode_GetNthTag(&DCtx, &Item, 3) != CBOR_TAG_INVALID64 ||
4147 QCBORDecode_GetNthTag(&DCtx, &Item, 4) != CBOR_TAG_INVALID64 ||
4148 Item.val.uCount != 2) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004149 return -4;
4150 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07004151 // consume the items in the array
4152 uError = QCBORDecode_GetNext(&DCtx, &Item);
4153 uError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundblade59289e52019-12-30 13:44:37 -08004154
Laurence Lundbladedd6e76e2021-03-10 01:54:01 -07004155#else /* QCBOR_DISABLE_EXP_AND_MANTISSA */
Laurence Lundblade9b334962020-08-27 10:55:53 -07004156 if(uError != QCBOR_SUCCESS ||
4157 Item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION ||
4158 QCBORDecode_GetNthTag(&DCtx, &Item, 0) != CBOR_TAG_INVALID64 ||
4159 QCBORDecode_GetNthTag(&DCtx, &Item, 1) != CBOR_TAG_INVALID64 ||
4160 QCBORDecode_GetNthTag(&DCtx, &Item, 2) != CBOR_TAG_INVALID64 ||
4161 QCBORDecode_GetNthTag(&DCtx, &Item, 3) != CBOR_TAG_INVALID64 ||
4162 QCBORDecode_GetNthTag(&DCtx, &Item, 4) != CBOR_TAG_INVALID64 ) {
4163 return -5;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004164 }
Laurence Lundbladedd6e76e2021-03-10 01:54:01 -07004165#endif /* QCBOR_DISABLE_EXP_AND_MANTISSA */
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004166
Laurence Lundblade9b334962020-08-27 10:55:53 -07004167 /*
4168 More than 4 tags on an item 225(226(227(228(229([])))))
4169 */
4170 uError = QCBORDecode_GetNext(&DCtx, &Item);
4171 if(uError != QCBOR_ERR_TOO_MANY_TAGS) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004172 return -6;
4173 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07004174
Laurence Lundblade88e9db22020-11-02 03:56:33 -08004175 if(QCBORDecode_GetNthTag(&DCtx, &Item, 0) != CBOR_TAG_INVALID64) {
4176 return -106;
4177 }
4178
4179
Laurence Lundblade9b334962020-08-27 10:55:53 -07004180 /* tag 10489608748473423768(
4181 2442302356(
4182 21590(
4183 240(
4184 []))))
4185 */
4186 uError = QCBORDecode_GetNext(&DCtx, &Item);
4187 if(uError != QCBOR_SUCCESS ||
4188 Item.uDataType != QCBOR_TYPE_ARRAY ||
4189 QCBORDecode_GetNthTag(&DCtx, &Item, 3) != 10489608748473423768ULL ||
4190 QCBORDecode_GetNthTag(&DCtx, &Item, 2) != 2442302356ULL ||
4191 QCBORDecode_GetNthTag(&DCtx, &Item, 1) != 21590ULL ||
4192 QCBORDecode_GetNthTag(&DCtx, &Item, 0) != 240ULL) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004193 return -7;
Laurence Lundblade9b334962020-08-27 10:55:53 -07004194 }
4195
4196 /* tag 21590(
4197 10489608748473423768(
4198 2442302357(
4199 21591(
4200 []))))
4201 */
4202 uError = QCBORDecode_GetNext(&DCtx, &Item);
4203 if(uError != QCBOR_SUCCESS ||
4204 Item.uDataType != QCBOR_TYPE_ARRAY ||
4205 QCBORDecode_GetNthTag(&DCtx, &Item, 0) != 65534ULL ||
4206 QCBORDecode_GetNthTag(&DCtx, &Item, 1) != 2442302357ULL ||
4207 QCBORDecode_GetNthTag(&DCtx, &Item, 2) != 10489608748473423768ULL ||
4208 QCBORDecode_GetNthTag(&DCtx, &Item, 3) != 21590ULL) {
4209 return -8;
4210 }
4211
4212 /* Make sure to blow past the limit of tags that must be mapped.
4213 works in conjuntion with entries above.
4214 269488144(269488145(269488146(269488147([]))))
4215 */
4216 uError = QCBORDecode_GetNext(&DCtx, &Item);
4217 if(uError != QCBOR_ERR_TOO_MANY_TAGS) {
4218 return -9;
4219 }
4220
4221 uError = QCBORDecode_GetNext(&DCtx, &Item);
4222 if(uError == QCBOR_SUCCESS) {
4223 return -10;
4224 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004225
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004226 // ----------------------------------
Laurence Lundbladeee851742020-01-08 08:37:05 -08004227 // This test sets up a caller-config list that includes the very large
Laurence Lundblade9b334962020-08-27 10:55:53 -07004228 // tage and then matches it. Caller-config lists are no longer
4229 // used or needed. This tests backwards compatibility with them.
Laurence Lundbladeee851742020-01-08 08:37:05 -08004230 QCBORDecode_Init(&DCtx,
4231 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEncodedLargeTag),
4232 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004233 const uint64_t puList[] = {0x9192939495969798, 257};
4234 const QCBORTagListIn TL = {2, puList};
4235 QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004236
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004237 if(QCBORDecode_GetNext(&DCtx, &Item)) {
4238 return -8;
4239 }
4240 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
4241 !QCBORDecode_IsTagged(&DCtx, &Item, 0x9192939495969798) ||
4242 QCBORDecode_IsTagged(&DCtx, &Item, 257) ||
4243 QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_BIGFLOAT) ||
4244 Item.val.uCount != 0) {
4245 return -9;
4246 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004247
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004248 //------------------------
Laurence Lundbladeee851742020-01-08 08:37:05 -08004249 // Sets up a caller-configured list and look up something not in it
Laurence Lundblade9b334962020-08-27 10:55:53 -07004250 // Another backwards compatibility test.
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004251 const uint64_t puLongList[17] = {1,2,1};
4252 const QCBORTagListIn TLLong = {17, puLongList};
Laurence Lundbladeee851742020-01-08 08:37:05 -08004253 QCBORDecode_Init(&DCtx,
4254 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEncodedLargeTag),
4255 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004256 QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TLLong);
4257 if(QCBORDecode_GetNext(&DCtx, &Item)) {
4258 return -11;
4259 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004260
Laurence Lundbladef7a70bc2020-10-24 12:23:25 -07004261 uint64_t puTags[4];
Laurence Lundblade9b334962020-08-27 10:55:53 -07004262 QCBORTagListOut Out = {0, 4, puTags};
4263
4264
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004265 // This tests retrievel of the full tag list
Laurence Lundbladeee851742020-01-08 08:37:05 -08004266 QCBORDecode_Init(&DCtx,
4267 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spLotsOfTags),
4268 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004269 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
4270 return -12;
4271 }
4272 if(puTags[0] != 0x9192939495969798 ||
4273 puTags[1] != 0x88 ||
Laurence Lundblade59289e52019-12-30 13:44:37 -08004274 puTags[2] != 0x06 ||
4275 puTags[3] != 0x07) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004276 return -13;
4277 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004278
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004279 // ----------------------
Laurence Lundblade9b334962020-08-27 10:55:53 -07004280 // This tests too small of an out list
Laurence Lundbladeee851742020-01-08 08:37:05 -08004281 QCBORDecode_Init(&DCtx,
4282 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spLotsOfTags),
4283 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004284 QCBORTagListOut OutSmall = {0, 3, puTags};
4285 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &OutSmall) != QCBOR_ERR_TOO_MANY_TAGS) {
4286 return -14;
4287 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004288
Laurence Lundblade9b334962020-08-27 10:55:53 -07004289
4290
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004291 // ---------------
Laurence Lundblade9b334962020-08-27 10:55:53 -07004292 // Decode a version of the "CSR" that has had a ton of tags randomly inserted
4293 // It is a bit of a messy test and maybe could be improved, but
4294 // it is retained as a backwards compatibility check.
Laurence Lundbladeee851742020-01-08 08:37:05 -08004295 QCBORDecode_Init(&DCtx,
4296 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRWithTags),
4297 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004298 int n = CheckCSRMaps(&DCtx);
4299 if(n) {
4300 return n-2000;
4301 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004302
Laurence Lundblade59289e52019-12-30 13:44:37 -08004303 Out = (QCBORTagListOut){0, 16, puTags};
Laurence Lundbladeee851742020-01-08 08:37:05 -08004304 QCBORDecode_Init(&DCtx,
4305 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRWithTags),
4306 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004307
Laurence Lundblade9b334962020-08-27 10:55:53 -07004308 /* With the spiffy decode revision, this tag list is not used.
4309 It doesn't matter if a tag is in this list or not so some
4310 tests that couldn't process a tag because it isn't in this list
4311 now can process these unlisted tags. The tests have been
4312 adjusted for this. */
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004313 const uint64_t puTagList[] = {773, 1, 90599561};
4314 const QCBORTagListIn TagList = {3, puTagList};
4315 QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TagList);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004316
4317
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004318 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
4319 return -100;
4320 }
4321 if(Item.uDataType != QCBOR_TYPE_MAP ||
4322 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC) ||
4323 QCBORDecode_IsTagged(&DCtx, &Item, 90599561) ||
4324 QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_DATE_EPOCH) ||
4325 Item.val.uCount != 2 ||
4326 puTags[0] != CBOR_TAG_CBOR_MAGIC ||
4327 puTags[1] != CBOR_TAG_CBOR_MAGIC ||
4328 puTags[2] != CBOR_TAG_CBOR_MAGIC ||
4329 Out.uNumUsed != 3) {
4330 return -101;
4331 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004332
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004333 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
4334 return -102;
4335 }
4336 if(Item.uDataType != QCBOR_TYPE_MAP ||
4337 QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC) ||
4338 QCBORDecode_IsTagged(&DCtx, &Item, 6) ||
Laurence Lundbladef7a70bc2020-10-24 12:23:25 -07004339 !QCBORDecode_IsTagged(&DCtx, &Item, 7) ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004340 Item.val.uCount != 2 ||
4341 puTags[0] != 5859837686836516696 ||
4342 puTags[1] != 7 ||
4343 Out.uNumUsed != 2) {
4344 return -103;
4345 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004346
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004347 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
4348 return -104;
4349 }
4350 if(Item.uDataType != QCBOR_TYPE_MAP ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004351 Item.val.uCount != 5 ||
4352 puTags[0] != 0x0b ||
4353 Out.uNumUsed != 1) {
4354 return -105;
4355 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004356
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004357 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
4358 return -106;
4359 }
4360 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
4361 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_COSE_MAC0) ||
4362 Item.val.string.len != 12 ||
4363 puTags[0] != CBOR_TAG_COSE_MAC0 ||
4364 puTags[1] != CBOR_TAG_COSE_MAC0 ||
4365 puTags[2] != CBOR_TAG_COSE_MAC0 ||
4366 Out.uNumUsed != 3) {
4367 return -105;
4368 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004369
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004370 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
4371 return -107;
4372 }
4373 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
4374 !QCBORDecode_IsTagged(&DCtx, &Item, 773) ||
4375 Item.val.string.len != 3 ||
4376 puTags[0] != 773 ||
4377 Out.uNumUsed != 1) {
4378 return -108;
4379 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004380
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004381 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
4382 return -109;
4383 }
4384 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade59289e52019-12-30 13:44:37 -08004385 !QCBORDecode_IsTagged(&DCtx, &Item, 16) ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004386 Item.val.string.len != 9 ||
Laurence Lundblade59289e52019-12-30 13:44:37 -08004387 puTags[0] != 16 ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07004388 puTags[3] != 7 ||
4389 Out.uNumUsed != 4) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004390 return -110;
4391 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004392
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004393 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
4394 return -111;
4395 }
4396 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
4397 !QCBORDecode_IsTagged(&DCtx, &Item, 17) ||
4398 Item.val.string.len != 9 ||
4399 puTags[0] != 17 ||
4400 Out.uNumUsed != 1) {
4401 return -112;
4402 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004403
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004404 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
4405 return -111;
4406 }
4407 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
4408 !QCBORDecode_IsTagged(&DCtx, &Item, 17) ||
4409 Item.val.string.len != 2 ||
4410 puTags[0] != 17 ||
4411 Out.uNumUsed != 1) {
4412 return -112;
4413 }
4414
4415 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
4416 return -113;
4417 }
4418 if(Item.uDataType != QCBOR_TYPE_MAP ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07004419 !QCBORDecode_IsTagged(&DCtx, &Item, 19) ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004420 Item.val.uCount != 2 ||
4421 puTags[0] != 19 ||
4422 Out.uNumUsed != 1) {
4423 return -114;
4424 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004425
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004426 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
4427 return -115;
4428 }
4429 if(Item.uDataType != QCBOR_TYPE_MAP ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07004430 !QCBORDecode_IsTagged(&DCtx, &Item, 9) ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004431 Item.val.uCount != 1 ||
4432 puTags[0] != 9 ||
4433 Out.uNumUsed != 1) {
4434 return -116;
4435 }
4436
4437 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
4438 return -116;
4439 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004440 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004441 Item.val.int64 != -7 ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004442 Out.uNumUsed != 0) {
4443 return -117;
4444 }
4445
4446 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
4447 return -118;
4448 }
4449 if(Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
4450 Item.val.string.len != 10 ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004451 puTags[0] != 12 ||
4452 Out.uNumUsed != 1) {
4453 return -119;
4454 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004455
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004456 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
4457 return -120;
4458 }
4459 if(Item.uDataType != QCBOR_TYPE_MAP ||
4460 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_ENC_AS_B16) ||
4461 Item.val.uCount != 1 ||
4462 puTags[0] != 0x17 ||
4463 Out.uNumUsed != 1) {
4464 return -121;
4465 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004466
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004467 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
4468 return -122;
4469 }
4470 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07004471 !QCBORDecode_IsTagged(&DCtx, &Item, 8) ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004472 Item.val.int64 != -3 ||
4473 puTags[0] != 8 ||
4474 Out.uNumUsed != 1) {
4475 return -123;
4476 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004477
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004478 if(QCBORDecode_Finish(&DCtx)) {
4479 return -124;
4480 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07004481
4482 UsefulBufC DateString;
4483 QCBORDecode_Init(&DCtx,
4484 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSpiffyTagInput),
4485 QCBOR_DECODE_MODE_NORMAL);
4486
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07004487 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade9b334962020-08-27 10:55:53 -07004488 // tagged date string
4489 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString);
4490 // untagged date string
4491 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString);
4492 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_SUCCESS) {
4493 return 100;
4494 }
4495 // untagged byte string
4496 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString);
4497 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
4498 return 101;
4499 }
4500 // tagged regex
4501 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString);
4502 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
4503 return 102;
4504 }
4505 // tagged date string with a byte string
4506 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString);
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07004507 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT) {
Laurence Lundblade9b334962020-08-27 10:55:53 -07004508 return 103;
4509 }
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07004510 // The exit errors out because the last item, the date string with
4511 // bad content makes the array untraversable (the bad date string
4512 // could have tag content of an array or such that is not consumed
4513 // by the date decoding).
Laurence Lundblade9b334962020-08-27 10:55:53 -07004514 QCBORDecode_ExitArray(&DCtx);
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07004515 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT) {
Laurence Lundblade9b334962020-08-27 10:55:53 -07004516 return 104;
4517 }
4518
4519
4520 QCBORDecode_Init(&DCtx,
Laurence Lundbladecdbbc192024-06-28 15:13:04 -07004521 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRWithTags),
4522 QCBOR_DECODE_MODE_NORMAL);
4523 QCBORDecode_EnterMap(&DCtx, NULL);
4524 if(QCBORDecode_GetNthTagOfLast(&DCtx, 0) != 55799) {
4525 return 200;
4526 }
4527 if(QCBORDecode_GetNthTagOfLast(&DCtx, 1) != 55799) {
4528 return 202;
4529 }
4530 if(QCBORDecode_GetNthTagOfLast(&DCtx, 2) != 55799) {
4531 return 203;
4532 }
4533 if(QCBORDecode_GetNthTagOfLast(&DCtx, 3) != CBOR_TAG_INVALID64) {
4534 return 204;
4535 }
4536
4537 QCBORDecode_EnterMap(&DCtx, NULL);
4538 if(QCBORDecode_GetNthTagOfLast(&DCtx, 0) != 7) {
4539 return 210;
4540 }
4541 if(QCBORDecode_GetNthTagOfLast(&DCtx, 1) != 5859837686836516696) {
4542 return 212;
4543 }
4544 if(QCBORDecode_GetNthTagOfLast(&DCtx, 2) != CBOR_TAG_INVALID64) {
4545 return 213;
4546 }
4547 if(QCBORDecode_GetNthTagOfLast(&DCtx, 3) != CBOR_TAG_INVALID64) {
4548 return 214;
4549 }
4550
4551
4552 QCBORDecode_Init(&DCtx,
4553 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRWithTags),
4554 QCBOR_DECODE_MODE_NORMAL);
4555 QCBORDecode_EnterMap(&DCtx, NULL);
4556 QCBORDecode_EnterMapFromMapN(&DCtx, -23);
4557 if(QCBORDecode_GetNthTagOfLast(&DCtx, 0) != 7) {
4558 return 220;
4559 }
4560 if(QCBORDecode_GetNthTagOfLast(&DCtx, 1) != 5859837686836516696) {
4561 return 221;
4562 }
4563 if(QCBORDecode_GetNthTagOfLast(&DCtx, 2) != CBOR_TAG_INVALID64) {
4564 return 222;
4565 }
4566
4567#ifndef QCBOR_DISABLE_NON_INTEGER_LABELS
4568 QCBORDecode_Init(&DCtx,
4569 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRWithTags),
4570 QCBOR_DECODE_MODE_MAP_AS_ARRAY);
4571 QCBORDecode_EnterArray(&DCtx, NULL);
4572 if(QCBORDecode_GetNthTagOfLast(&DCtx, 0) != 55799) {
4573 return 230;
4574 }
4575 if(QCBORDecode_GetNthTagOfLast(&DCtx, 1) != 55799) {
4576 return 231;
4577 }
4578 if(QCBORDecode_GetNthTagOfLast(&DCtx, 2) != 55799) {
4579 return 232;
4580 }
4581 if(QCBORDecode_GetNthTagOfLast(&DCtx, 3) != CBOR_TAG_INVALID64) {
4582 return 234;
4583 }
4584 int64_t nInt;
4585 QCBORDecode_GetInt64(&DCtx, &nInt);
4586 if(QCBORDecode_GetNthTagOfLast(&DCtx, 0) != 7) {
4587 return 240;
4588 }
4589 if(QCBORDecode_GetNthTagOfLast(&DCtx, 1) != 6) {
4590 return 241;
4591 }
4592 if(QCBORDecode_GetNthTagOfLast(&DCtx, 2) != CBOR_TAG_INVALID64) {
4593 return 242;
4594 }
4595 if(QCBORDecode_GetNthTagOfLast(&DCtx, 3) != CBOR_TAG_INVALID64) {
4596 return 243;
4597 }
4598#endif /* ! QCBOR_DISABLE_NON_INTEGER_LABELS */
4599
4600 QCBORDecode_Init(&DCtx,
Laurence Lundblade9b334962020-08-27 10:55:53 -07004601 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSpiffyTagInput),
4602 QCBOR_DECODE_MODE_NORMAL);
4603
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07004604 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade9b334962020-08-27 10:55:53 -07004605 // tagged date string
4606 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString);
4607 // untagged date string
4608 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString);
4609 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_SUCCESS) {
Laurence Lundbladecdbbc192024-06-28 15:13:04 -07004610 return 250;
Laurence Lundblade9b334962020-08-27 10:55:53 -07004611 }
4612 // untagged byte string
4613 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString);
4614 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
Laurence Lundbladecdbbc192024-06-28 15:13:04 -07004615 return 251;
Laurence Lundblade9b334962020-08-27 10:55:53 -07004616 }
4617 // tagged regex
4618 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString);
4619 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
Laurence Lundbladecdbbc192024-06-28 15:13:04 -07004620 return 252;
Laurence Lundblade9b334962020-08-27 10:55:53 -07004621 }
4622 // tagged date string with a byte string
4623 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString);
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07004624 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT) {
Laurence Lundbladecdbbc192024-06-28 15:13:04 -07004625 return 253;
Laurence Lundblade9b334962020-08-27 10:55:53 -07004626 }
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07004627 // See comments above
Laurence Lundblade9b334962020-08-27 10:55:53 -07004628 QCBORDecode_ExitArray(&DCtx);
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07004629 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT) {
Laurence Lundbladecdbbc192024-06-28 15:13:04 -07004630 return 254;
Laurence Lundblade9b334962020-08-27 10:55:53 -07004631 }
4632
4633 QCBORDecode_Init(&DCtx,
4634 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSpiffyTagInput),
4635 QCBOR_DECODE_MODE_NORMAL);
4636
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07004637 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade9b334962020-08-27 10:55:53 -07004638 // tagged date string
4639 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString);
4640 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
4641 return 300;
4642 }
4643 // untagged date string
4644 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString);
4645 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
4646 return 301;
4647 }
4648 // untagged byte string
4649 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString);
4650 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
4651 return 302;
4652 }
4653 // tagged regex
4654 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString);
4655 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
4656 return 303;
4657 }
4658 // tagged date string with a byte string
4659 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString);
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07004660 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT) {
Laurence Lundblade9b334962020-08-27 10:55:53 -07004661 return 304;
4662 }
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07004663 // See comments above
Laurence Lundblade9b334962020-08-27 10:55:53 -07004664 QCBORDecode_ExitArray(&DCtx);
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07004665 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT) {
Laurence Lundblade9b334962020-08-27 10:55:53 -07004666 return 305;
4667 }
4668
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004669 return 0;
4670}
4671
Laurence Lundblade37286c02022-09-03 10:05:02 -07004672/*
4673 * These are showing the big numbers converted to integers.
4674 * The tag numbers are not shown.
4675 *
Laurence Lundbladeec290b82024-06-10 11:10:54 -07004676 * [
4677 * 18446744073709551616,
4678 * -18446744073709551617,
4679 * {
4680 * -64: -18446744073709551617,
4681 * 64: 18446744073709551616,
4682 * "BN+": 18446744073709551616,
4683 * "BN-": -18446744073709551617
Laurence Lundblade37286c02022-09-03 10:05:02 -07004684 * }
4685 * ]
4686 */
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004687
Laurence Lundbladecc7da412020-12-27 00:09:07 -08004688static const uint8_t spBigNumInput[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004689 0x83,
4690 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
4691 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
4692 0xA4,
Laurence Lundbladeec290b82024-06-10 11:10:54 -07004693 0x38, 0x3F,
4694 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004695 0x18, 0x40,
4696 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
Laurence Lundbladeec290b82024-06-10 11:10:54 -07004697 0x63, 0x42, 0x4E, 0x2B,
4698 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
4699 0x63, 0x42, 0x4E, 0x2D,
4700 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
4701};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004702
Laurence Lundblade37286c02022-09-03 10:05:02 -07004703#ifndef QCBOR_DISABLE_TAGS
Laurence Lundbladecc7da412020-12-27 00:09:07 -08004704/* The expected big num */
4705static const uint8_t spBigNum[] = {
4706 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
4707 0x00};
Laurence Lundblade37286c02022-09-03 10:05:02 -07004708#endif /* QCBOR_DISABLE_TAGS */
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004709
4710
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03004711int32_t BignumParseTest(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004712{
4713 QCBORDecodeContext DCtx;
4714 QCBORItem Item;
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07004715 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004716
Laurence Lundbladeee851742020-01-08 08:37:05 -08004717 QCBORDecode_Init(&DCtx,
4718 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNumInput),
4719 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004720
4721
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004722 //
4723 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
4724 return -1;
4725 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade830fbf92020-05-31 17:22:33 -07004726 return -2;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004727 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004728
Laurence Lundblade37286c02022-09-03 10:05:02 -07004729#ifndef QCBOR_DISABLE_TAGS
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004730 //
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004731 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07004732 return -3;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004733 if(Item.uDataType != QCBOR_TYPE_POSBIGNUM ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07004734 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07004735 return -4;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004736 }
4737
4738 //
4739 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07004740 return -5;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004741 if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07004742 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07004743 return -6;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004744 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004745
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004746 //
4747 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07004748 return -7;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004749 if(Item.uDataType != QCBOR_TYPE_MAP) {
Laurence Lundblade830fbf92020-05-31 17:22:33 -07004750 return -8;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004751 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004752
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004753 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundbladeec290b82024-06-10 11:10:54 -07004754 return -15;
4755 if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM ||
4756 Item.uLabelType != QCBOR_TYPE_INT64 ||
4757 Item.label.int64 != -64 ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07004758 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundbladeec290b82024-06-10 11:10:54 -07004759 return -16;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004760 }
4761
4762 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07004763 return -11;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004764 if(Item.uDataType != QCBOR_TYPE_POSBIGNUM ||
4765 Item.uLabelType != QCBOR_TYPE_INT64 ||
4766 Item.label.int64 != 64 ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07004767 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07004768 return -12;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004769 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004770
Laurence Lundbladeec290b82024-06-10 11:10:54 -07004771#ifndef QCBOR_DISABLE_NON_INTEGER_LABELS
4772 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
4773 return -9;
4774 if(Item.uDataType != QCBOR_TYPE_POSBIGNUM ||
4775 Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
4776 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
4777 return -10;
4778 }
4779
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004780 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07004781 return -13;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004782 if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM ||
4783 Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07004784 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07004785 return -14;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004786 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004787
Laurence Lundbladeec290b82024-06-10 11:10:54 -07004788
4789#endif /* ! QCBOR_DISABLE_NON_INTEGER_LABELS */
4790
Laurence Lundblade37286c02022-09-03 10:05:02 -07004791#else
4792
4793 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_TAGS_DISABLED) {
4794 return -100;
4795 }
4796#endif /* QCBOR_DISABLE_TAGS */
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004797
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004798 return 0;
4799}
4800
4801
Laurence Lundbladec5fef682020-01-25 11:38:45 -08004802static int32_t CheckItemWithIntLabel(QCBORDecodeContext *pCtx,
Laurence Lundbladeee851742020-01-08 08:37:05 -08004803 uint8_t uDataType,
4804 uint8_t uNestingLevel,
4805 uint8_t uNextNest,
4806 int64_t nLabel,
4807 QCBORItem *pItem)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004808{
4809 QCBORItem Item;
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07004810 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004811
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004812 if((nCBORError = QCBORDecode_GetNext(pCtx, &Item))) return -1;
4813 if(Item.uDataType != uDataType) return -1;
4814 if(uNestingLevel > 0) {
Laurence Lundbladeec290b82024-06-10 11:10:54 -07004815 if(Item.uLabelType != QCBOR_TYPE_INT64) {
Laurence Lundbladeee851742020-01-08 08:37:05 -08004816 return -1;
4817 }
Laurence Lundbladeec290b82024-06-10 11:10:54 -07004818 if(Item.label.int64 != nLabel) {
4819 return -1;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004820 }
Laurence Lundbladeec290b82024-06-10 11:10:54 -07004821
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004822 }
4823 if(Item.uNestingLevel != uNestingLevel) return -1;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304824 if(Item.uNextNestLevel != uNextNest) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004825
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004826 if(pItem) {
4827 *pItem = Item;
4828 }
4829 return 0;
4830}
4831
Laurence Lundblade742df4a2018-10-13 20:07:17 +08004832// Same code checks definite and indefinite length versions of the map
Laurence Lundbladec5fef682020-01-25 11:38:45 -08004833static int32_t CheckCSRMaps(QCBORDecodeContext *pDC)
Laurence Lundblade742df4a2018-10-13 20:07:17 +08004834{
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304835 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 0, 1, 0, NULL)) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004836
Laurence Lundblade9b334962020-08-27 10:55:53 -07004837 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 1, 2, -23, NULL)) return -2;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004838
Laurence Lundblade9b334962020-08-27 10:55:53 -07004839 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 2, 3, -20, NULL)) return -3;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004840
Laurence Lundblade9b334962020-08-27 10:55:53 -07004841 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -18, NULL)) return -4;
4842 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -17, NULL)) return -5;
4843 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -15, NULL)) return -6;
4844 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -16, NULL)) return -7;
4845 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 2, -14, NULL)) return -8;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004846
Laurence Lundblade9b334962020-08-27 10:55:53 -07004847 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 2, 3, -19, NULL)) return -9;
4848 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 3, 4, -11, NULL)) return -10;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004849
Laurence Lundblade9b334962020-08-27 10:55:53 -07004850 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_INT64, 4, 3, -9, NULL)) return -11;
4851 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_BYTE_STRING, 3, 1, -10, NULL)) return -12;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004852
Laurence Lundblade9b334962020-08-27 10:55:53 -07004853 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 1, 2, -22, NULL)) return -13;
4854 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_INT64, 2, 0, -5, NULL)) return -14;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004855
Laurence Lundblade9b334962020-08-27 10:55:53 -07004856 if(QCBORDecode_Finish(pDC)) return -20;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004857
Laurence Lundblade742df4a2018-10-13 20:07:17 +08004858 return 0;
4859}
4860
4861
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004862/*
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004863{
4864 -23: {
4865 -20: {
4866 -18: "Organization",
4867 -17: "SSG",
4868 -15: "Confusion",
4869 -16: "San Diego",
4870 -14: "US"
4871 },
4872 -19: {
4873 -11: {
4874 -9: -7
4875 },
4876 -10: '\u0001\u0002\u0003\u0004\u0005\u0006\a\b\t\n'
4877 }
4878 },
4879 -22: {
4880 -5: -3
4881 }
4882}
Laurence Lundbladecc7da412020-12-27 00:09:07 -08004883*/
4884static const uint8_t spCSRInput[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004885 0xa2, 0x36, 0xa2, 0x33, 0xa5, 0x31, 0x6c, 0x4f,
4886 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74,
4887 0x69, 0x6f, 0x6e, 0x30, 0x63, 0x53, 0x53, 0x47,
4888 0x2e, 0x69, 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73,
4889 0x69, 0x6f, 0x6e, 0x2f, 0x69, 0x53, 0x61, 0x6e,
4890 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f, 0x2d, 0x62,
4891 0x55, 0x53, 0x32, 0xa2, 0x2a, 0xa1, 0x28, 0x26,
4892 0x29, 0x4a, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
4893 0x07, 0x08, 0x09, 0x0a, 0x35, 0xa1, 0x24, 0x22};
4894
Laurence Lundbladecc7da412020-12-27 00:09:07 -08004895// Same map as above, but using indefinite lengths
4896static const uint8_t spCSRInputIndefLen[] = {
4897 0xbf, 0x36, 0xbf, 0x33, 0xbf, 0x31, 0x6c, 0x4f,
4898 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74,
4899 0x69, 0x6f, 0x6e, 0x30, 0x63, 0x53, 0x53, 0x47,
4900 0x2e, 0x69, 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73,
4901 0x69, 0x6f, 0x6e, 0x2f, 0x69, 0x53, 0x61, 0x6e,
4902 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f, 0x2d, 0x62,
4903 0x55, 0x53, 0xff, 0x32, 0xbf, 0x2a, 0xbf, 0x28,
4904 0x26, 0xff, 0x29, 0x4a, 0x01, 0x02, 0x03, 0x04,
4905 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0xff, 0xff,
4906 0x35, 0xbf, 0x24, 0x22, 0xff, 0xff};
4907
4908
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03004909int32_t NestedMapTest(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004910{
4911 QCBORDecodeContext DCtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004912
Laurence Lundbladeee851742020-01-08 08:37:05 -08004913 QCBORDecode_Init(&DCtx,
4914 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput),
4915 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004916
Laurence Lundblade742df4a2018-10-13 20:07:17 +08004917 return CheckCSRMaps(&DCtx);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004918}
4919
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08004920
4921
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03004922int32_t StringDecoderModeFailTest(void)
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08004923{
4924 QCBORDecodeContext DCtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004925
Laurence Lundbladeee851742020-01-08 08:37:05 -08004926 QCBORDecode_Init(&DCtx,
4927 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput),
4928 QCBOR_DECODE_MODE_MAP_STRINGS_ONLY);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004929
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08004930 QCBORItem Item;
4931 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004932
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08004933 if(QCBORDecode_GetNext(&DCtx, &Item)) {
4934 return -1;
4935 }
4936 if(Item.uDataType != QCBOR_TYPE_MAP) {
4937 return -2;
4938 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004939
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08004940 nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
4941 if(nCBORError != QCBOR_ERR_MAP_LABEL_TYPE) {
4942 return -3;
4943 }
4944
4945 return 0;
4946}
4947
4948
Laurence Lundblade742df4a2018-10-13 20:07:17 +08004949
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03004950int32_t NestedMapTestIndefLen(void)
Laurence Lundblade742df4a2018-10-13 20:07:17 +08004951{
4952 QCBORDecodeContext DCtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004953
Laurence Lundbladeee851742020-01-08 08:37:05 -08004954 QCBORDecode_Init(&DCtx,
4955 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInputIndefLen),
4956 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004957
Laurence Lundblade742df4a2018-10-13 20:07:17 +08004958 return CheckCSRMaps(&DCtx);
4959}
4960
4961
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004962
Laurence Lundblade17ede402018-10-13 11:43:07 +08004963static UsefulBufC make_nested_indefinite_arrays(int n, UsefulBuf Storage)
4964{
4965 UsefulOutBuf UOB;
4966 UsefulOutBuf_Init(&UOB, Storage);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004967
Laurence Lundblade17ede402018-10-13 11:43:07 +08004968 int i;
4969 for(i = 0; i < n; i++) {
4970 UsefulOutBuf_AppendByte(&UOB, 0x9f);
4971 }
4972
4973 for(i = 0; i < n; i++) {
4974 UsefulOutBuf_AppendByte(&UOB, 0xff);
4975 }
4976 return UsefulOutBuf_OutUBuf(&UOB);
4977}
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08004978
4979
Laurence Lundbladec5fef682020-01-25 11:38:45 -08004980static int32_t parse_indeflen_nested(UsefulBufC Nested, int nNestLevel)
Laurence Lundblade17ede402018-10-13 11:43:07 +08004981{
4982 QCBORDecodeContext DC;
4983 QCBORDecode_Init(&DC, Nested, 0);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004984
Laurence Lundblade17ede402018-10-13 11:43:07 +08004985 int j;
4986 for(j = 0; j < nNestLevel; j++) {
4987 QCBORItem Item;
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07004988 QCBORError nReturn = QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade17ede402018-10-13 11:43:07 +08004989 if(j >= QCBOR_MAX_ARRAY_NESTING) {
4990 // Should be in error
Laurence Lundbladea9489f82020-09-12 13:50:56 -07004991 if(nReturn != QCBOR_ERR_ARRAY_DECODE_NESTING_TOO_DEEP) {
Laurence Lundblade17ede402018-10-13 11:43:07 +08004992 return -4;
4993 } else {
4994 return 0; // Decoding doesn't recover after an error
4995 }
4996 } else {
4997 // Should be no error
4998 if(nReturn) {
4999 return -9; // Should not have got an error
5000 }
5001 }
5002 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
5003 return -7;
5004 }
5005 }
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07005006 QCBORError nReturn = QCBORDecode_Finish(&DC);
Laurence Lundblade17ede402018-10-13 11:43:07 +08005007 if(nReturn) {
5008 return -3;
5009 }
5010 return 0;
5011}
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08005012
5013
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03005014int32_t IndefiniteLengthNestTest(void)
Laurence Lundblade17ede402018-10-13 11:43:07 +08005015{
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05305016 UsefulBuf_MAKE_STACK_UB(Storage, 50);
Laurence Lundblade17ede402018-10-13 11:43:07 +08005017 int i;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005018 for(i=1; i < QCBOR_MAX_ARRAY_NESTING+4; i++) {
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08005019 const UsefulBufC Nested = make_nested_indefinite_arrays(i, Storage);
Laurence Lundblade17ede402018-10-13 11:43:07 +08005020 int nReturn = parse_indeflen_nested(Nested, i);
5021 if(nReturn) {
5022 return nReturn;
5023 }
5024 }
5025 return 0;
5026}
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08005027
Laurence Lundbladeee851742020-01-08 08:37:05 -08005028// [1, [2, 3]]
5029static const uint8_t spIndefiniteArray[] = {0x9f, 0x01, 0x82, 0x02, 0x03, 0xff};
5030// No closing break
5031static const uint8_t spIndefiniteArrayBad1[] = {0x9f};
5032// Not enough closing breaks
5033static const uint8_t spIndefiniteArrayBad2[] = {0x9f, 0x9f, 0x02, 0xff};
5034// Too many closing breaks
5035static const uint8_t spIndefiniteArrayBad3[] = {0x9f, 0x02, 0xff, 0xff};
5036// Unclosed indeflen inside def len
5037static const uint8_t spIndefiniteArrayBad4[] = {0x81, 0x9f};
5038// confused tag
5039static const uint8_t spIndefiniteArrayBad5[] = {0x9f, 0xd1, 0xff};
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08005040
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03005041int32_t IndefiniteLengthArrayMapTest(void)
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08005042{
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07005043 QCBORError nResult;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08005044 // --- first test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07005045 UsefulBufC IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArray);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005046
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08005047 // Decode it and see if it is OK
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08005048 QCBORDecodeContext DC;
5049 QCBORItem Item;
5050 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005051
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08005052 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05305053
5054 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
5055 Item.uNestingLevel != 0 ||
5056 Item.uNextNestLevel != 1) {
5057 return -111;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08005058 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005059
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08005060 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05305061 if(Item.uDataType != QCBOR_TYPE_INT64 ||
5062 Item.uNestingLevel != 1 ||
5063 Item.uNextNestLevel != 1) {
5064 return -2;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08005065 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005066
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08005067 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05305068 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
5069 Item.uNestingLevel != 1 ||
5070 Item.uNextNestLevel != 2) {
5071 return -3;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08005072 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005073
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08005074 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade12b495d2018-12-17 11:15:54 -08005075 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade6de37062018-10-15 12:22:42 +05305076 Item.uNestingLevel != 2 ||
5077 Item.uNextNestLevel != 2) {
5078 return -4;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08005079 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005080
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08005081 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade12b495d2018-12-17 11:15:54 -08005082 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade6de37062018-10-15 12:22:42 +05305083 Item.uNestingLevel != 2 ||
5084 Item.uNextNestLevel != 0) {
5085 return -5;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08005086 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005087
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08005088 if(QCBORDecode_Finish(&DC)) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05305089 return -6;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08005090 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005091
Laurence Lundblade19e0c802018-10-13 12:19:55 +08005092 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07005093 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad1);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005094
Laurence Lundblade19e0c802018-10-13 12:19:55 +08005095 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005096
Laurence Lundblade19e0c802018-10-13 12:19:55 +08005097 nResult = QCBORDecode_GetNext(&DC, &Item);
5098 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05305099 return -7;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08005100 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005101
Laurence Lundblade570fab52018-10-13 18:28:27 +08005102 nResult = QCBORDecode_Finish(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07005103 if(nResult != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05305104 return -8;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08005105 }
5106
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005107
Laurence Lundblade19e0c802018-10-13 12:19:55 +08005108 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07005109 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad2);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005110
Laurence Lundblade19e0c802018-10-13 12:19:55 +08005111 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005112
Laurence Lundblade19e0c802018-10-13 12:19:55 +08005113 nResult = QCBORDecode_GetNext(&DC, &Item);
5114 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05305115 return -9;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08005116 }
5117
5118 nResult = QCBORDecode_GetNext(&DC, &Item);
5119 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05305120 return -10;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08005121 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005122
Laurence Lundblade19e0c802018-10-13 12:19:55 +08005123 nResult = QCBORDecode_GetNext(&DC, &Item);
5124 if(nResult || Item.uDataType != QCBOR_TYPE_INT64) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05305125 return -11;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08005126 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005127
Laurence Lundblade19e0c802018-10-13 12:19:55 +08005128 nResult = QCBORDecode_Finish(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07005129 if(nResult != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05305130 return -12;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08005131 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005132
5133
Laurence Lundblade19e0c802018-10-13 12:19:55 +08005134 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07005135 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad3);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005136
Laurence Lundblade19e0c802018-10-13 12:19:55 +08005137 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005138
Laurence Lundblade19e0c802018-10-13 12:19:55 +08005139 nResult = QCBORDecode_GetNext(&DC, &Item);
5140 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05305141 return -13;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08005142 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005143
Laurence Lundblade19e0c802018-10-13 12:19:55 +08005144 nResult = QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade642282a2020-06-23 12:00:33 -07005145 if(nResult != QCBOR_SUCCESS) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05305146 return -14;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08005147 }
Laurence Lundblade6de37062018-10-15 12:22:42 +05305148
Laurence Lundblade642282a2020-06-23 12:00:33 -07005149 nResult = QCBORDecode_GetNext(&DC, &Item);
5150 if(nResult != QCBOR_ERR_BAD_BREAK) {
5151 return -140;
5152 }
5153
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005154
Laurence Lundblade570fab52018-10-13 18:28:27 +08005155 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07005156 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad4);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005157
Laurence Lundblade570fab52018-10-13 18:28:27 +08005158 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005159
Laurence Lundblade570fab52018-10-13 18:28:27 +08005160 nResult = QCBORDecode_GetNext(&DC, &Item);
5161 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05305162 return -15;
Laurence Lundblade570fab52018-10-13 18:28:27 +08005163 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005164
Laurence Lundblade570fab52018-10-13 18:28:27 +08005165 nResult = QCBORDecode_GetNext(&DC, &Item);
5166 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05305167 return -16;
Laurence Lundblade570fab52018-10-13 18:28:27 +08005168 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005169
Laurence Lundblade570fab52018-10-13 18:28:27 +08005170 nResult = QCBORDecode_Finish(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07005171 if(nResult != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05305172 return -17;
Laurence Lundblade570fab52018-10-13 18:28:27 +08005173 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005174
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05305175 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07005176 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad5);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005177
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05305178 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005179
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05305180 nResult = QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade37286c02022-09-03 10:05:02 -07005181
5182#ifndef QCBOR_DISABLE_TAGS
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05305183 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05305184 return -18;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05305185 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005186
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05305187 nResult = QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05305188 if(nResult != QCBOR_ERR_BAD_BREAK) {
5189 return -19;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05305190 }
Laurence Lundblade37286c02022-09-03 10:05:02 -07005191#else /* QCBOR_DISABLE_TAGS */
5192 if(nResult != QCBOR_ERR_TAGS_DISABLED) {
5193 return -20;
5194 }
5195#endif /* QCBOR_DISABLE_TAGS */
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005196
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08005197 return 0;
5198}
5199
Laurence Lundblade17ede402018-10-13 11:43:07 +08005200
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08005201#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
5202
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07005203static const uint8_t spIndefiniteLenString[] = {
Laurence Lundblade17ede402018-10-13 11:43:07 +08005204 0x81, // Array of length one
5205 0x7f, // text string marked with indefinite length
5206 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment
5207 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment
5208 0xff // ending break
5209};
5210
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07005211static const uint8_t spIndefiniteLenStringBad2[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305212 0x81, // Array of length one
5213 0x7f, // text string marked with indefinite length
5214 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment
5215 0x44, 0x6d, 0x69, 0x6e, 0x67, // second segment of wrong type
5216 0xff // ending break
5217};
5218
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07005219static const uint8_t spIndefiniteLenStringBad3[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305220 0x81, // Array of length one
5221 0x7f, // text string marked with indefinite length
5222 0x01, 0x02, // Not a string
5223 0xff // ending break
5224};
5225
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07005226static const uint8_t spIndefiniteLenStringBad4[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305227 0x81, // Array of length one
5228 0x7f, // text string marked with indefinite length
5229 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment
5230 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment
5231 // missing end of string
5232};
5233
Laurence Lundbladeec290b82024-06-10 11:10:54 -07005234#ifndef QCBOR_DISABLE_NON_INTEGER_LABELS
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07005235static const uint8_t spIndefiniteLenStringLabel[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305236 0xa1, // Array of length one
5237 0x7f, // text string marked with indefinite length
5238 0x65, 0x73, 0x74, 0x72, 0x75, 0x75, // first segment
5239 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment
5240 0xff, // ending break
5241 0x01 // integer being labeled.
5242};
Laurence Lundbladeec290b82024-06-10 11:10:54 -07005243#endif /* ! QCBOR_DISABLE_NON_INTEGER_LABELS */
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305244
Laurence Lundbladec5fef682020-01-25 11:38:45 -08005245/**
5246 Make an indefinite length string
5247
5248 @param Storage Storage for string, must be 144 bytes in size
5249 @return The indefinite length string
5250
5251 This makes an array with one indefinite length string that has 7 chunks
5252 from size of 1 byte up to 64 bytes.
5253 */
5254static UsefulBufC MakeIndefiniteBigBstr(UsefulBuf Storage)
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305255{
5256 UsefulOutBuf UOB;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005257
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305258 UsefulOutBuf_Init(&UOB, Storage);
5259 UsefulOutBuf_AppendByte(&UOB, 0x81);
5260 UsefulOutBuf_AppendByte(&UOB, 0x5f);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005261
Laurence Lundbladec5fef682020-01-25 11:38:45 -08005262 uint8_t uStringByte = 0;
5263 // Use of type int is intentional
5264 for(int uChunkSize = 1; uChunkSize <= 128; uChunkSize *= 2) {
5265 // Not using preferred encoding here, but that is OK.
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305266 UsefulOutBuf_AppendByte(&UOB, 0x58);
Laurence Lundbladec5fef682020-01-25 11:38:45 -08005267 UsefulOutBuf_AppendByte(&UOB, (uint8_t)uChunkSize);
5268 for(int j = 0; j < uChunkSize; j++) {
5269 UsefulOutBuf_AppendByte(&UOB, uStringByte);
5270 uStringByte++;
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305271 }
5272 }
5273 UsefulOutBuf_AppendByte(&UOB, 0xff);
5274
5275 return UsefulOutBuf_OutUBuf(&UOB);
5276}
5277
5278static int CheckBigString(UsefulBufC BigString)
5279{
5280 if(BigString.len != 255) {
5281 return 1;
5282 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005283
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305284 for(uint8_t i = 0; i < 255; i++){
5285 if(((const uint8_t *)BigString.ptr)[i] != i) {
5286 return 1;
5287 }
5288 }
5289 return 0;
5290}
5291
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05305292
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03005293int32_t IndefiniteLengthStringTest(void)
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05305294{
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305295 QCBORDecodeContext DC;
5296 QCBORItem Item;
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05305297 // big enough for MakeIndefiniteBigBstr() + MemPool overhead
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08005298 UsefulBuf_MAKE_STACK_UB(MemPool, 350);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005299
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305300 // --- Simple normal indefinite length string ------
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07005301 UsefulBufC IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenString);
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305302 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005303
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05305304 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305305 return -1;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05305306 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005307
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305308 if(QCBORDecode_GetNext(&DC, &Item)) {
5309 return -2;
5310 }
5311 if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.uDataAlloc) {
5312 return -3;
5313 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005314
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305315 if(QCBORDecode_GetNext(&DC, &Item)) {
5316 return -4;
5317 }
5318 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || !Item.uDataAlloc) {
5319 return -5;
5320 }
5321 if(QCBORDecode_Finish(&DC)) {
5322 return -6;
5323 }
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05305324
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305325 // ----- types mismatch ---
Laurence Lundbladeee851742020-01-08 08:37:05 -08005326 QCBORDecode_Init(&DC,
5327 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad2),
5328 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005329
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305330 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
5331 return -7;
5332 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005333
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305334 if(QCBORDecode_GetNext(&DC, &Item)) {
5335 return -8;
5336 }
5337 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
5338 return -9;
5339 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005340
Laurence Lundblade30816f22018-11-10 13:40:22 +07005341 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_INDEFINITE_STRING_CHUNK) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305342 return -10;
5343 }
5344
5345 // ----- not a string ---
Laurence Lundbladeee851742020-01-08 08:37:05 -08005346 QCBORDecode_Init(&DC,
5347 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad3),
5348 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005349
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305350 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
5351 return -11;
5352 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005353
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305354 if(QCBORDecode_GetNext(&DC, &Item)) {
5355 return -12;
5356 }
5357 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
5358 return -13;
5359 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005360
Laurence Lundblade30816f22018-11-10 13:40:22 +07005361 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_INDEFINITE_STRING_CHUNK) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305362 return -14;
5363 }
5364
5365 // ----- no end -----
Laurence Lundbladeee851742020-01-08 08:37:05 -08005366 QCBORDecode_Init(&DC,
5367 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad4),
5368 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005369
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305370 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
5371 return -15;
5372 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005373
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305374 if(QCBORDecode_GetNext(&DC, &Item)) {
5375 return -16;
5376 }
5377 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
5378 return -17;
5379 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005380
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305381 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_HIT_END) {
5382 return -18;
5383 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005384
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305385 // ------ Don't set a string allocator and see an error -----
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05305386 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005387
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05305388 QCBORDecode_GetNext(&DC, &Item);
5389 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305390 return -19;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05305391 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005392
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05305393 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_NO_STRING_ALLOCATOR) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305394 return -20;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05305395 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005396
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05305397 // ----- Mempool is way too small -----
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08005398 UsefulBuf_MAKE_STACK_UB(MemPoolTooSmall, QCBOR_DECODE_MIN_MEM_POOL_SIZE-1);
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05305399
5400 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
5401 if(!QCBORDecode_SetMemPool(&DC, MemPoolTooSmall, false)) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305402 return -21;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05305403 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005404
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05305405 // ----- Mempool is way too small -----
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05305406 UsefulBuf_MAKE_STACK_UB(BigIndefBStrStorage, 290);
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08005407 const UsefulBufC BigIndefBStr = MakeIndefiniteBigBstr(BigIndefBStrStorage);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005408
Laurence Lundbladeee851742020-01-08 08:37:05 -08005409 // 80 is big enough for MemPool overhead, but not BigIndefBStr
5410 UsefulBuf_MAKE_STACK_UB(MemPoolSmall, 80);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005411
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305412 QCBORDecode_Init(&DC, BigIndefBStr, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05305413 if(QCBORDecode_SetMemPool(&DC, MemPoolSmall, false)) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305414 return -22;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05305415 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005416
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05305417 QCBORDecode_GetNext(&DC, &Item);
5418 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305419 return -23;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05305420 }
Laurence Lundblade30816f22018-11-10 13:40:22 +07005421 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_STRING_ALLOCATE) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305422 return -24;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05305423 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005424
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305425 // ---- big bstr -----
5426 QCBORDecode_Init(&DC, BigIndefBStr, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005427
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305428 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
5429 return -25;
5430 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005431
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05305432 if(QCBORDecode_GetNext(&DC, &Item)) {
5433 return -26;
5434 }
5435 if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.uDataAlloc) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305436 return -26;
5437 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005438
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305439 if(QCBORDecode_GetNext(&DC, &Item)) {
5440 return -27;
5441 }
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05305442 if(Item.uDataType != QCBOR_TYPE_BYTE_STRING || !Item.uDataAlloc || Item.uNestingLevel != 1) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305443 return -28;
5444 }
5445 if(CheckBigString(Item.val.string)) {
5446 return -3;
5447 }
5448 if(QCBORDecode_Finish(&DC)) {
5449 return -29;
5450 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005451
Laurence Lundbladeec290b82024-06-10 11:10:54 -07005452#ifndef QCBOR_DISABLE_NON_INTEGER_LABELS
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305453 // --- label is an indefinite length string ------
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07005454 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringLabel), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005455
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305456 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
5457 return -30;
5458 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005459
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305460 QCBORDecode_GetNext(&DC, &Item);
5461 if(Item.uDataType != QCBOR_TYPE_MAP) {
5462 return -31;
5463 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005464
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305465 if(QCBORDecode_GetNext(&DC, &Item)){
5466 return -32;
5467 }
Laurence Lundbladeee851742020-01-08 08:37:05 -08005468 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
5469 Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305470 Item.uDataAlloc || !Item.uLabelAlloc ||
5471 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("struuming"))) {
5472 return -33;
5473 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005474
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305475 if(QCBORDecode_Finish(&DC)) {
5476 return -34;
5477 }
Laurence Lundbladeec290b82024-06-10 11:10:54 -07005478#endif /* ! QCBOR_DISABLE_NON_INTEGER_LABELS */
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005479
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08005480 return 0;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08005481}
5482
5483
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03005484int32_t AllocAllStringsTest(void)
Laurence Lundbladea44d5062018-10-17 18:45:12 +05305485{
5486 QCBORDecodeContext DC;
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08005487 QCBORError nCBORError;
5488
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005489
Laurence Lundbladea44d5062018-10-17 18:45:12 +05305490 // First test, use the "CSRMap" as easy input and checking
Laurence Lundbladeee851742020-01-08 08:37:05 -08005491 QCBORDecode_Init(&DC,
5492 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput),
5493 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005494
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08005495 UsefulBuf_MAKE_STACK_UB(Pool, sizeof(spCSRInput) + QCBOR_DECODE_MIN_MEM_POOL_SIZE);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005496
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08005497 nCBORError = QCBORDecode_SetMemPool(&DC, Pool, 1); // Turn on copying.
5498 if(nCBORError) {
Laurence Lundbladea44d5062018-10-17 18:45:12 +05305499 return -1;
5500 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005501
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08005502 if(CheckCSRMaps(&DC)) {
5503 return -2;
5504 }
5505
Laurence Lundbladeec290b82024-06-10 11:10:54 -07005506#ifndef QCBOR_DISABLE_NON_INTEGER_LABELS
Laurence Lundblade2f467f92020-10-09 17:50:11 -07005507 // Next parse, save pointers to a few strings, destroy original and
5508 // see all is OK.
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08005509 UsefulBuf_MAKE_STACK_UB(CopyOfStorage, sizeof(pValidMapEncoded) + QCBOR_DECODE_MIN_MEM_POOL_SIZE);
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08005510 const UsefulBufC CopyOf = UsefulBuf_Copy(CopyOfStorage, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded));
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08005511
Laurence Lundbladea44d5062018-10-17 18:45:12 +05305512 QCBORDecode_Init(&DC, CopyOf, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08005513 UsefulBuf_Set(Pool, '/');
Laurence Lundbladea44d5062018-10-17 18:45:12 +05305514 QCBORDecode_SetMemPool(&DC, Pool, 1); // Turn on copying.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005515
Laurence Lundbladea44d5062018-10-17 18:45:12 +05305516 QCBORItem Item1, Item2, Item3, Item4;
5517 if((nCBORError = QCBORDecode_GetNext(&DC, &Item1)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07005518 return (int32_t)nCBORError;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05305519 if(Item1.uDataType != QCBOR_TYPE_MAP ||
5520 Item1.val.uCount != 3)
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08005521 return -3;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05305522 if((nCBORError = QCBORDecode_GetNext(&DC, &Item1)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07005523 return (int32_t)nCBORError;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05305524 if((nCBORError = QCBORDecode_GetNext(&DC, &Item2)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07005525 return (int32_t)nCBORError;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05305526 if((nCBORError = QCBORDecode_GetNext(&DC, &Item3)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07005527 return (int32_t)nCBORError;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05305528 if((nCBORError = QCBORDecode_GetNext(&DC, &Item4)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07005529 return (int32_t)nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005530
Laurence Lundblade05ec57b2018-10-21 01:50:03 +05305531 UsefulBuf_Set(CopyOfStorage, '_');
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005532
Laurence Lundbladea44d5062018-10-17 18:45:12 +05305533 if(Item1.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladea44d5062018-10-17 18:45:12 +05305534 Item1.uDataType != QCBOR_TYPE_INT64 ||
5535 Item1.val.int64 != 42 ||
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08005536 Item1.uDataAlloc != 0 ||
5537 Item1.uLabelAlloc == 0 ||
Laurence Lundbladeec290b82024-06-10 11:10:54 -07005538 UsefulBufCompareToSZ(Item1.label.string, "first integer") ||
5539 Item1.label.string.ptr < Pool.ptr ||
5540 Item1.label.string.ptr > (const void *)((const uint8_t *)Pool.ptr + Pool.len)) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08005541 return -4;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09005542 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005543
Laurence Lundbladea44d5062018-10-17 18:45:12 +05305544
5545 if(Item2.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07005546 UsefulBufCompareToSZ(Item2.label.string, "an array of two strings") ||
Laurence Lundbladea44d5062018-10-17 18:45:12 +05305547 Item2.uDataType != QCBOR_TYPE_ARRAY ||
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08005548 Item2.uDataAlloc != 0 ||
5549 Item2.uLabelAlloc == 0 ||
Laurence Lundbladea44d5062018-10-17 18:45:12 +05305550 Item2.val.uCount != 2)
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08005551 return -5;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005552
Laurence Lundbladea44d5062018-10-17 18:45:12 +05305553 if(Item3.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08005554 Item3.uDataAlloc == 0 ||
5555 Item3.uLabelAlloc != 0 ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07005556 UsefulBufCompareToSZ(Item3.val.string, "string1")) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08005557 return -6;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09005558 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005559
Laurence Lundbladea44d5062018-10-17 18:45:12 +05305560 if(Item4.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08005561 Item4.uDataAlloc == 0 ||
5562 Item4.uLabelAlloc != 0 ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07005563 UsefulBufCompareToSZ(Item4.val.string, "string2")) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08005564 return -7;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09005565 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005566
Laurence Lundbladea44d5062018-10-17 18:45:12 +05305567 // Next parse with a pool that is too small
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08005568 UsefulBuf_MAKE_STACK_UB(SmallPool, QCBOR_DECODE_MIN_MEM_POOL_SIZE + 1);
Laurence Lundbladeee851742020-01-08 08:37:05 -08005569 QCBORDecode_Init(&DC,
5570 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded),
5571 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladea44d5062018-10-17 18:45:12 +05305572 QCBORDecode_SetMemPool(&DC, SmallPool, 1); // Turn on copying.
5573 if((nCBORError = QCBORDecode_GetNext(&DC, &Item1)))
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08005574 return -8;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05305575 if(Item1.uDataType != QCBOR_TYPE_MAP ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09005576 Item1.val.uCount != 3) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08005577 return -9;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09005578 }
Laurence Lundbladea44d5062018-10-17 18:45:12 +05305579 if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item1))){
5580 if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item2))) {
5581 if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item3))) {
5582 nCBORError = QCBORDecode_GetNext(&DC, &Item4);
5583 }
5584 }
5585 }
Laurence Lundblade30816f22018-11-10 13:40:22 +07005586 if(nCBORError != QCBOR_ERR_STRING_ALLOCATE) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08005587 return -10;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05305588 }
Laurence Lundbladeec290b82024-06-10 11:10:54 -07005589#endif /* ! QCBOR_DISABLE_NON_INTEGER_LABELS */
Laurence Lundbladea44d5062018-10-17 18:45:12 +05305590
5591 return 0;
5592}
5593
Laurence Lundbladef6531662018-12-04 10:42:22 +09005594
Laurence Lundbladec5fef682020-01-25 11:38:45 -08005595int32_t MemPoolTest(void)
Laurence Lundblade0155b622018-10-12 20:04:37 +08005596{
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08005597 // Set up the decoder with a tiny bit of CBOR to parse because
5598 // nothing can be done with it unless that is set up.
Laurence Lundbladef6531662018-12-04 10:42:22 +09005599 QCBORDecodeContext DC;
5600 const uint8_t pMinimalCBOR[] = {0xa0}; // One empty map
5601 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pMinimalCBOR),0);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005602
Laurence Lundbladef6531662018-12-04 10:42:22 +09005603 // Set up an memory pool of 100 bytes
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08005604 // Then fish into the internals of the decode context
5605 // to get the allocator function so it can be called directly.
5606 // Also figure out how much pool is available for use
5607 // buy subtracting out the overhead.
Laurence Lundbladef6531662018-12-04 10:42:22 +09005608 UsefulBuf_MAKE_STACK_UB(Pool, 100);
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08005609 QCBORError nError = QCBORDecode_SetMemPool(&DC, Pool, 0);
5610 if(nError) {
5611 return -9;
5612 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08005613 QCBORStringAllocate pAlloc = DC.StringAllocator.pfAllocator;
5614 void *pAllocCtx = DC.StringAllocator.pAllocateCxt;
5615 size_t uAvailPool = Pool.len - QCBOR_DECODE_MIN_MEM_POOL_SIZE;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005616
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08005617 // First test -- ask for one more byte than available and see failure
5618 UsefulBuf Allocated = (*pAlloc)(pAllocCtx, NULL, uAvailPool+1);
Laurence Lundbladef6531662018-12-04 10:42:22 +09005619 if(!UsefulBuf_IsNULL(Allocated)) {
5620 return -1;
5621 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005622
Laurence Lundbladef6531662018-12-04 10:42:22 +09005623 // Re do the set up for the next test that will do a successful alloc,
5624 // a fail, a free and then success
Laurence Lundbladef6531662018-12-04 10:42:22 +09005625 QCBORDecode_SetMemPool(&DC, Pool, 0);
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08005626 pAlloc = DC.StringAllocator.pfAllocator;
5627 pAllocCtx = DC.StringAllocator.pAllocateCxt;
5628 uAvailPool = Pool.len - QCBOR_DECODE_MIN_MEM_POOL_SIZE;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005629
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08005630 // Allocate one byte less than available and see success
5631 Allocated = (pAlloc)(pAllocCtx, NULL, uAvailPool-1);
Laurence Lundbladef6531662018-12-04 10:42:22 +09005632 if(UsefulBuf_IsNULL(Allocated)) { // expected to succeed
5633 return -2;
5634 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08005635 // Ask for some more and see failure
5636 UsefulBuf Allocated2 = (*pAlloc)(pAllocCtx, NULL, uAvailPool/2);
Laurence Lundbladef6531662018-12-04 10:42:22 +09005637 if(!UsefulBuf_IsNULL(Allocated2)) { // expected to fail
5638 return -3;
5639 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08005640 // Free the first allocate, retry the second and see success
5641 (*pAlloc)(pAllocCtx, Allocated.ptr, 0); // Free
5642 Allocated = (*pAlloc)(pAllocCtx, NULL, uAvailPool/2);
Laurence Lundbladef6531662018-12-04 10:42:22 +09005643 if(UsefulBuf_IsNULL(Allocated)) { // succeed because of the free
5644 return -4;
5645 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005646
Laurence Lundbladef6531662018-12-04 10:42:22 +09005647 // Re do set up for next test that involves a successful alloc,
5648 // and a successful realloc and a failed realloc
5649 QCBORDecode_SetMemPool(&DC, Pool, 0);
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08005650 pAlloc = DC.StringAllocator.pfAllocator;
5651 pAllocCtx = DC.StringAllocator.pAllocateCxt;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005652
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08005653 // Allocate half the pool and see success
5654 Allocated = (*pAlloc)(pAllocCtx, NULL, uAvailPool/2);
Laurence Lundbladef6531662018-12-04 10:42:22 +09005655 if(UsefulBuf_IsNULL(Allocated)) { // expected to succeed
5656 return -5;
5657 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08005658 // Reallocate to take up the whole pool and see success
5659 Allocated2 = (*pAlloc)(pAllocCtx, Allocated.ptr, uAvailPool);
Laurence Lundbladef6531662018-12-04 10:42:22 +09005660 if(UsefulBuf_IsNULL(Allocated2)) {
5661 return -6;
5662 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08005663 // Make sure its the same pointer and the size is right
Laurence Lundbladef6531662018-12-04 10:42:22 +09005664 if(Allocated2.ptr != Allocated.ptr || Allocated2.len != uAvailPool) {
5665 return -7;
5666 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08005667 // Try to allocate more to be sure there is failure after a realloc
5668 UsefulBuf Allocated3 = (*pAlloc)(pAllocCtx, Allocated.ptr, uAvailPool+1);
5669 if(!UsefulBuf_IsNULL(Allocated3)) {
Laurence Lundbladef6531662018-12-04 10:42:22 +09005670 return -8;
5671 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005672
Laurence Lundbladef6531662018-12-04 10:42:22 +09005673 return 0;
5674}
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08005675
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08005676
5677/* Just enough of an allocator to test configuration of one */
5678static UsefulBuf AllocateTestFunction(void *pCtx, void *pOldMem, size_t uNewSize)
5679{
5680 (void)pOldMem; // unused variable
5681
5682 if(uNewSize) {
5683 // Assumes the context pointer is the buffer and
5684 // nothing too big will ever be asked for.
5685 // This is only good for this basic test!
5686 return (UsefulBuf) {pCtx, uNewSize};
5687 } else {
5688 return NULLUsefulBuf;
5689 }
5690}
5691
5692
Laurence Lundbladec5fef682020-01-25 11:38:45 -08005693int32_t SetUpAllocatorTest(void)
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08005694{
5695 // Set up the decoder with a tiny bit of CBOR to parse because
5696 // nothing can be done with it unless that is set up.
5697 QCBORDecodeContext DC;
5698 const uint8_t pMinimalCBOR[] = {0x62, 0x48, 0x69}; // "Hi"
5699 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pMinimalCBOR),0);
5700
5701 uint8_t pAllocatorBuffer[50];
5702
5703 // This is really just to test that this call works.
5704 // The full functionality of string allocators is tested
5705 // elsewhere with the MemPool internal allocator.
5706 QCBORDecode_SetUpAllocator(&DC, AllocateTestFunction, pAllocatorBuffer, 1);
5707
5708 QCBORItem Item;
5709 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_SUCCESS) {
5710 return -1;
5711 }
5712
5713 if(Item.uDataAlloc == 0 ||
5714 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
5715 Item.val.string.ptr != pAllocatorBuffer) {
5716 return -2;
5717 }
5718
5719 if(QCBORDecode_Finish(&DC) != QCBOR_SUCCESS) {
5720 return -3;
5721 }
5722
5723 return 0;
5724}
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08005725#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
5726
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08005727
Laurence Lundbladedd6e76e2021-03-10 01:54:01 -07005728#ifndef QCBOR_DISABLE_EXP_AND_MANTISSA
Laurence Lundblade59289e52019-12-30 13:44:37 -08005729
Laurence Lundblade37286c02022-09-03 10:05:02 -07005730struct EaMTest {
5731 const char *szName;
5732 UsefulBufC Input;
5733 uint8_t uTagRequirement;
5734 bool bHasTags;
5735
5736 /* Expected values for GetNext */
5737 QCBORError uExpectedErrorGN;
5738 uint8_t uQCBORTypeGN;
5739 int64_t nExponentGN;
5740 int64_t nMantissaGN;
5741 UsefulBufC MantissaGN;
5742
5743 /* Expected values for GetDecimalFraction */
5744 QCBORError uExpectedErrorGDF;
5745 int64_t nExponentGDF;
5746 int64_t nMantissaGDF;
5747
5748 /* Expected values for GetDecimalFractionBig */
5749 QCBORError uExpectedErrorGDFB;
5750 int64_t nExponentGDFB;
5751 UsefulBufC MantissaGDFB;
5752 bool IsNegativeGDFB;
5753
5754 /* Expected values for GetBigFloat */
5755 QCBORError uExpectedErrorGBF;
5756 int64_t nExponentGBF;
5757 int64_t nMantissaGBF;
5758
5759 /* Expected values for GetBigFloatBig */
5760 QCBORError uExpectedErrorGBFB;
5761 int64_t nExponentGBFB;
5762 UsefulBufC MantissaGBFB;
5763 bool IsNegativeGBFB;
Laurence Lundblade59289e52019-12-30 13:44:37 -08005764};
5765
Laurence Lundbladefaec39f2020-08-02 21:53:53 -07005766
Laurence Lundblade37286c02022-09-03 10:05:02 -07005767
5768static const struct EaMTest pEaMTests[] = {
5769 {
5770 "1. Untagged pair (big float or decimal fraction), no tag required",
5771 {(const uint8_t []){0x82, 0x20, 0x03}, 3},
5772 QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
5773 false,
5774
5775 QCBOR_SUCCESS, /* for GetNext */
5776 QCBOR_TYPE_ARRAY,
5777 0,
5778 0,
5779 {(const uint8_t []){0x00}, 1},
5780
5781 QCBOR_SUCCESS, /* GetDecimalFraction */
5782 -1,
5783 3,
5784
5785 QCBOR_SUCCESS, /* for GetDecimalFractionBig */
5786 -1,
5787 {(const uint8_t []){0x03}, 1},
5788 false,
5789
5790 QCBOR_SUCCESS, /* for GetBigFloat */
5791 -1,
5792 3,
5793
5794 QCBOR_SUCCESS, /* for GetBigFloatBig */
5795 -1,
5796 {(const uint8_t []){0x03}, 1},
5797 false
5798 },
5799
5800 {
5801 "2. Untagged pair (big float or decimal fraction), tag required",
5802 {(const uint8_t []){0x82, 0x20, 0x03}, 3},
5803 QCBOR_TAG_REQUIREMENT_TAG,
5804 false,
5805
5806 QCBOR_SUCCESS, /* for GetNext */
5807 QCBOR_TYPE_ARRAY,
5808 0,
5809 0,
5810 {(const uint8_t []){0x00}, 1},
5811
5812 QCBOR_ERR_UNEXPECTED_TYPE, /* for GetDecimalFraction */
5813 0,
5814 0,
5815
5816 QCBOR_ERR_UNEXPECTED_TYPE, /* for GetDecimalFractionBig */
5817 0,
5818 {(const uint8_t []){0x00}, 1},
5819 false,
5820
5821 QCBOR_ERR_UNEXPECTED_TYPE, /* for GetBigFloat */
5822 0,
5823 0,
5824
5825 QCBOR_ERR_UNEXPECTED_TYPE, /* for GetBigFloatBig */
5826 0,
5827 {(const uint8_t []){0x00}, 1},
5828 false
5829
5830 },
5831
5832 {
5833 "3. Tagged 1.5 decimal fraction, tag 4 optional",
5834 {(const uint8_t []){0xC4, 0x82, 0x20, 0x03}, 4},
5835 QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
5836 true,
5837
5838 QCBOR_SUCCESS, /* for GetNext */
5839 QCBOR_TYPE_DECIMAL_FRACTION,
5840 -1,
5841 3,
5842 {(const uint8_t []){0x00}, 1},
5843
5844
5845 QCBOR_SUCCESS, /* for GetDecimalFraction */
5846 -1,
5847 3,
5848
5849 QCBOR_SUCCESS, /* for GetDecimalFractionBig */
5850 -1,
5851 {(const uint8_t []){0x03}, 1},
5852 false,
5853
5854 QCBOR_ERR_UNEXPECTED_TYPE, /* for GetBigFloat */
5855 0,
5856 0,
5857
5858 QCBOR_ERR_UNEXPECTED_TYPE, /* for GetBigFloatBig */
5859 0,
5860 {(const uint8_t []){0x00}, 1},
5861 false
5862 },
5863 {
5864 "4. Tagged 100 * 2^300 big float, tag 5 optional",
5865 {(const uint8_t []){0xC5, 0x82, 0x19, 0x01, 0x2C, 0x18, 0x64}, 7},
5866 QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
5867 true,
5868
5869 QCBOR_SUCCESS, /* for GetNext */
5870 QCBOR_TYPE_BIGFLOAT,
5871 300,
5872 100,
5873 {(const uint8_t []){0x00}, 1},
5874
5875
5876 QCBOR_ERR_UNEXPECTED_TYPE, /* for GetDecimalFraction */
5877 0,
5878 0,
5879
5880 QCBOR_ERR_UNEXPECTED_TYPE, /* for GetDecimalFractionBig */
5881 0,
5882 {(const uint8_t []){0x03}, 1},
5883 false,
5884
5885 QCBOR_SUCCESS, /* for GetBigFloat */
5886 300,
5887 100,
5888
5889 QCBOR_SUCCESS, /* for GetBigFloatBig */
5890 300,
5891 {(const uint8_t []){0x64}, 1},
5892 false
5893 },
5894
5895 {
5896 "5. Tagged 4([-20, 4759477275222530853136]) decimal fraction, tag 4 required",
5897 {(const uint8_t []){0xC4, 0x82, 0x33,
5898 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,}, 15},
5899 QCBOR_TAG_REQUIREMENT_TAG,
5900 true,
5901
5902 QCBOR_SUCCESS, /* for GetNext */
5903 QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM,
5904 -20,
5905 0,
5906 {(const uint8_t []){0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10}, 10},
5907
5908 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW, /* for GetDecimalFraction */
5909 0,
5910 0,
5911
5912 QCBOR_SUCCESS, /* for GetDecimalFractionBig */
5913 -20,
5914 {(const uint8_t []){0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10}, 10},
5915 false,
5916
5917 QCBOR_ERR_UNEXPECTED_TYPE, /* for GetBigFloat */
5918 0,
5919 0,
5920
5921 QCBOR_ERR_UNEXPECTED_TYPE, /* for GetBigFloatBig */
5922 0,
5923 {(const uint8_t []){0x00}, 0},
5924 false
5925 },
5926
5927 {
5928 "6. Error: Mantissa and exponent inside a Mantissa and exponent",
5929 {(const uint8_t []){0xC4, 0x82, 0x33,
5930 0xC5, 0x82, 0x19, 0x01, 0x2C, 0x18, 0x64}, 10},
5931 QCBOR_TAG_REQUIREMENT_TAG,
5932 true,
5933
5934 QCBOR_ERR_BAD_EXP_AND_MANTISSA, /* for GetNext */
5935 QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM,
5936 0,
5937 0,
5938 {(const uint8_t []){0x00}, 0},
5939
5940 QCBOR_ERR_BAD_EXP_AND_MANTISSA, /* for GetDecimalFraction */
5941 0,
5942 0,
5943
5944 QCBOR_ERR_BAD_EXP_AND_MANTISSA, /* for GetDecimalFractionBig */
5945 0,
5946 {(const uint8_t []){0x00}, 0},
5947 false,
5948
5949 QCBOR_ERR_BAD_EXP_AND_MANTISSA, /* for GetBigFloat */
5950 0,
5951 0,
5952
5953 QCBOR_ERR_BAD_EXP_AND_MANTISSA, /* for GetBigFloatBig */
5954 0,
5955 {(const uint8_t []){0x00}, 0},
5956 false
5957 },
5958 {
5959 "7. Tagged 5([-20, 4294967295]) big float, big num mantissa, tag 5 required",
5960 {(const uint8_t []){0xC5, 0x82, 0x33,
5961 0xC2, 0x44, 0xff, 0xff, 0xff, 0xff}, 9},
5962 QCBOR_TAG_REQUIREMENT_TAG,
5963 true,
5964
5965 QCBOR_SUCCESS, /* for GetNext */
5966 QCBOR_TYPE_BIGFLOAT_POS_BIGNUM,
5967 -20,
5968 0,
5969 {(const uint8_t []){0xff, 0xff, 0xff, 0xff}, 4},
5970
5971 QCBOR_ERR_UNEXPECTED_TYPE, /* for GetDecimalFraction */
5972 0,
5973 0,
5974
5975 QCBOR_ERR_UNEXPECTED_TYPE, /* for GetDecimalFractionBig */
5976 -20,
5977 {(const uint8_t []){0x00}, 1},
5978 false,
5979
5980 QCBOR_SUCCESS, /* for GetBigFloat */
5981 -20,
5982 4294967295,
5983
5984 QCBOR_SUCCESS, /* for GetBigFloatBig */
5985 -20,
5986 {(const uint8_t []){0xff, 0xff, 0xff, 0xff}, 4},
5987 false
5988 },
5989
5990 {
5991 /* Special case for test 8. Don't renumber it. */
5992 "8. Untagged pair with big num (big float or decimal fraction), tag optional",
5993 {(const uint8_t []){0x82, 0x33, 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10}, 14},
5994 QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
5995 true,
5996
5997 QCBOR_SUCCESS, /* for GetNext */
5998 QCBOR_TYPE_ARRAY,
5999 0,
6000 0,
6001 {(const uint8_t []){0x00}, 1},
6002
6003 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW, /* GetDecimalFraction */
6004 0,
6005 0,
6006
6007 QCBOR_SUCCESS, /* for GetDecimalFractionBig */
6008 -20,
6009 {(const uint8_t []){0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10}, 10},
6010 false,
6011
6012 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW, /* for GetBigFloat */
6013 0,
6014 0,
6015
6016 QCBOR_SUCCESS, /* for GetBigFloatBig */
6017 -20,
6018 {(const uint8_t []){0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10}, 10},
6019 false
6020 },
6021
6022 {
6023 "9. decimal fraction with large exponent and negative big num mantissa",
6024 {(const uint8_t []){0xC4, 0x82, 0x1B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
6025 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10}, 23},
6026 QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
6027 true,
6028
6029 QCBOR_SUCCESS, /* for GetNext */
6030 QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM,
6031 9223372036854775807,
6032 0,
6033 {(const uint8_t []){0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10}, 10},
6034
6035 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW, /* GetDecimalFraction */
6036 0,
6037 0,
6038
6039 QCBOR_SUCCESS, /* for GetDecimalFractionBig */
6040 9223372036854775807,
6041 {(const uint8_t []){0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10}, 10},
6042 true,
6043
6044 QCBOR_ERR_UNEXPECTED_TYPE, /* for GetBigFloat */
6045 0,
6046 0,
6047
6048 QCBOR_ERR_UNEXPECTED_TYPE, /* for GetBigFloatBig */
6049 0,
6050 {(const uint8_t []){0x00}, 1},
6051 false
6052 },
6053};
6054
6055
6056
6057int32_t ProcessEaMTests(void)
Laurence Lundblade59289e52019-12-30 13:44:37 -08006058{
Laurence Lundblade37286c02022-09-03 10:05:02 -07006059 size_t uIndex;
6060 QCBORDecodeContext DCtx;
6061 QCBORItem Item;
6062 QCBORError uError;
6063 int64_t nMantissa, nExponent;
6064 MakeUsefulBufOnStack( MantissaBuf, 200);
6065 UsefulBufC Mantissa;
6066 bool bMantissaIsNegative;
6067
6068 for(uIndex = 0; uIndex < C_ARRAY_COUNT(pEaMTests, struct EaMTest); uIndex++) {
6069 const struct EaMTest *pT = &pEaMTests[uIndex];
6070 /* Decode with GetNext */
6071 QCBORDecode_Init(&DCtx, pT->Input, 0);
6072
6073 if(uIndex + 1 == 9) {
6074 nExponent = 99; // just to set a break point
6075 }
6076
6077 uError = QCBORDecode_GetNext(&DCtx, &Item);
6078#ifdef QCBOR_DISABLE_TAGS
6079 /* Test 8 is a special case when tags are disabled */
6080 if(pT->bHasTags && uIndex + 1 != 8) {
6081 if(uError != QCBOR_ERR_TAGS_DISABLED) {
6082 return (int32_t)(1+uIndex) * 1000 + 9;
6083 }
6084 } else {
6085#endif
6086 /* Now check return code, data type, mantissa and exponent */
6087 if(pT->uExpectedErrorGN != uError) {
6088 return (int32_t)(1+uIndex) * 1000 + 1;
6089 }
6090 if(uError == QCBOR_SUCCESS && pT->uQCBORTypeGN != QCBOR_TYPE_ARRAY) {
6091 if(pT->uQCBORTypeGN != Item.uDataType) {
6092 return (int32_t)(1+uIndex) * 1000 + 2;
6093 }
6094 if(pT->nExponentGN != Item.val.expAndMantissa.nExponent) {
6095 return (int32_t)(1+uIndex) * 1000 + 3;
6096 }
6097 if(Item.uDataType == QCBOR_TYPE_DECIMAL_FRACTION || Item.uDataType == QCBOR_TYPE_BIGFLOAT ) {
6098 if(pT->nMantissaGN != Item.val.expAndMantissa.Mantissa.nInt) {
6099 return (int32_t)(1+uIndex) * 1000 + 4;
6100 }
6101 } else {
6102 if(UsefulBuf_Compare(Item.val.expAndMantissa.Mantissa.bigNum, pT->MantissaGN)) {
6103 return (int32_t)(1+uIndex) * 1000 + 5;
6104 }
6105 }
6106 }
6107#ifdef QCBOR_DISABLE_TAGS
6108 }
6109#endif
6110
6111 /* Decode with GetDecimalFraction */
6112 QCBORDecode_Init(&DCtx, pT->Input, 0);
6113 QCBORDecode_GetDecimalFraction(&DCtx,
6114 pT->uTagRequirement,
6115 &nMantissa,
6116 &nExponent);
6117 uError = QCBORDecode_GetAndResetError(&DCtx);
6118#ifdef QCBOR_DISABLE_TAGS
6119 if(pT->bHasTags) {
6120 if(uError != QCBOR_ERR_TAGS_DISABLED) {
6121 return (int32_t)(1+uIndex) * 1000 + 39;
6122 }
6123 } else {
6124#endif
6125 /* Now check return code, mantissa and exponent */
6126 if(pT->uExpectedErrorGDF != uError) {
6127 return (int32_t)(1+uIndex) * 1000 + 31;
6128 }
6129 if(uError == QCBOR_SUCCESS) {
6130 if(pT->nExponentGDF != nExponent) {
6131 return (int32_t)(1+uIndex) * 1000 + 32;
6132 }
6133 if(pT->nMantissaGDF != nMantissa) {
6134 return (int32_t)(1+uIndex) * 1000 + 33;
6135 }
6136 }
6137#ifdef QCBOR_DISABLE_TAGS
6138 }
6139#endif
6140
6141 /* Decode with GetDecimalFractionBig */
6142 QCBORDecode_Init(&DCtx, pT->Input, 0);
6143 QCBORDecode_GetDecimalFractionBig(&DCtx,
6144 pT->uTagRequirement,
6145 MantissaBuf,
6146 &Mantissa,
6147 &bMantissaIsNegative,
6148 &nExponent);
6149 uError = QCBORDecode_GetAndResetError(&DCtx);
6150#ifdef QCBOR_DISABLE_TAGS
6151 if(pT->bHasTags) {
6152 if(uError != QCBOR_ERR_TAGS_DISABLED) {
6153 return (int32_t)(1+uIndex) * 1000 + 49;
6154 }
6155 } else {
6156#endif
6157 /* Now check return code, mantissa (bytes and sign) and exponent */
6158 if(pT->uExpectedErrorGDFB != uError) {
6159 return (int32_t)(1+uIndex) * 1000 + 41;
6160 }
6161 if(uError == QCBOR_SUCCESS) {
6162 if(pT->nExponentGDFB != nExponent) {
6163 return (int32_t)(1+uIndex) * 1000 + 42;
6164 }
6165 if(pT->IsNegativeGDFB != bMantissaIsNegative) {
6166 return (int32_t)(1+uIndex) * 1000 + 43;
6167 }
6168 if(UsefulBuf_Compare(Mantissa, pT->MantissaGDFB)) {
6169 return (int32_t)(1+uIndex) * 1000 + 44;
6170 }
6171 }
6172#ifdef QCBOR_DISABLE_TAGS
6173 }
6174#endif
6175
6176 /* Decode with GetBigFloat */
6177 QCBORDecode_Init(&DCtx, pT->Input, 0);
6178 QCBORDecode_GetBigFloat(&DCtx,
6179 pT->uTagRequirement,
6180 &nMantissa,
6181 &nExponent);
6182 uError = QCBORDecode_GetAndResetError(&DCtx);
6183#ifdef QCBOR_DISABLE_TAGS
6184 if(pT->bHasTags) {
6185 if(uError != QCBOR_ERR_TAGS_DISABLED) {
6186 return (int32_t)(1+uIndex) * 1000 + 19;
6187 }
6188 } else {
6189#endif
6190 /* Now check return code, mantissa and exponent */
6191 if(pT->uExpectedErrorGBF != uError) {
6192 return (int32_t)(1+uIndex) * 1000 + 11;
6193 }
6194 if(uError == QCBOR_SUCCESS) {
6195 if(pT->nExponentGBF != nExponent) {
6196 return (int32_t)(1+uIndex) * 1000 + 12;
6197 }
6198 if(pT->nMantissaGBF != nMantissa) {
6199 return (int32_t)(1+uIndex) * 1000 + 13;
6200 }
6201 }
6202#ifdef QCBOR_DISABLE_TAGS
6203 }
6204#endif
6205
6206 /* Decode with GetBigFloatBig */
6207 QCBORDecode_Init(&DCtx, pT->Input, 0);
6208 QCBORDecode_GetBigFloatBig(&DCtx,
6209 pT->uTagRequirement,
6210 MantissaBuf,
6211 &Mantissa,
6212 &bMantissaIsNegative,
6213 &nExponent);
6214 uError = QCBORDecode_GetAndResetError(&DCtx);
6215#ifdef QCBOR_DISABLE_TAGS
6216 if(pT->bHasTags) {
6217 if(uError != QCBOR_ERR_TAGS_DISABLED) {
6218 return (int32_t)(1+uIndex) * 1000 + 29;
6219 }
6220 } else {
6221#endif
6222 /* Now check return code, mantissa (bytes and sign) and exponent */
6223 if(pT->uExpectedErrorGBFB != uError) {
6224 return (int32_t)(1+uIndex) * 1000 + 21;
6225 }
6226 if(uError == QCBOR_SUCCESS) {
6227 if(pT->nExponentGBFB != nExponent) {
6228 return (int32_t)(1+uIndex) * 1000 + 22;
6229 }
6230 if(pT->IsNegativeGBFB != bMantissaIsNegative) {
6231 return (int32_t)(1+uIndex) * 1000 + 23;
6232 }
6233 if(UsefulBuf_Compare(Mantissa, pT->MantissaGBFB)) {
6234 return (int32_t)(1+uIndex) * 1000 + 24;
6235 }
6236 }
6237#ifdef QCBOR_DISABLE_TAGS
6238 }
6239#endif
6240 }
6241
6242 return 0;
6243}
6244
6245
6246int32_t ExponentAndMantissaDecodeTestsSecondary(void)
6247{
6248#ifndef QCBOR_DISABLE_TAGS
Laurence Lundblade59289e52019-12-30 13:44:37 -08006249 QCBORDecodeContext DC;
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07006250 QCBORError uErr;
Laurence Lundblade59289e52019-12-30 13:44:37 -08006251 QCBORItem item;
6252
Laurence Lundblade17af4902020-01-07 19:11:55 -08006253 static const uint8_t spBigNumMantissa[] = {0x01, 0x02, 0x03, 0x04, 0x05,
6254 0x06, 0x07, 0x08, 0x09, 0x010};
6255 UsefulBufC BN = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNumMantissa);
Laurence Lundblade59289e52019-12-30 13:44:37 -08006256
6257
Laurence Lundblade59289e52019-12-30 13:44:37 -08006258
6259 /* Now encode some stuff and then decode it */
6260 uint8_t pBuf[40];
6261 QCBOREncodeContext EC;
6262 UsefulBufC Encoded;
6263
6264 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(pBuf));
6265 QCBOREncode_OpenArray(&EC);
6266 QCBOREncode_AddDecimalFraction(&EC, 999, 1000); // 999 * (10 ^ 1000)
6267 QCBOREncode_AddBigFloat(&EC, 100, INT32_MIN);
6268 QCBOREncode_AddDecimalFractionBigNum(&EC, BN, false, INT32_MAX);
6269 QCBOREncode_CloseArray(&EC);
6270 QCBOREncode_Finish(&EC, &Encoded);
6271
6272
6273 QCBORDecode_Init(&DC, Encoded, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07006274 uErr = QCBORDecode_GetNext(&DC, &item);
6275 if(uErr != QCBOR_SUCCESS) {
6276 return 100;
Laurence Lundblade59289e52019-12-30 13:44:37 -08006277 }
6278
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07006279 uErr = QCBORDecode_GetNext(&DC, &item);
6280 if(uErr != QCBOR_SUCCESS) {
6281 return 101;
Laurence Lundblade59289e52019-12-30 13:44:37 -08006282 }
6283
6284 if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION ||
6285 item.val.expAndMantissa.nExponent != 1000 ||
6286 item.val.expAndMantissa.Mantissa.nInt != 999) {
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07006287 return 102;
Laurence Lundblade59289e52019-12-30 13:44:37 -08006288 }
6289
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07006290 uErr = QCBORDecode_GetNext(&DC, &item);
6291 if(uErr != QCBOR_SUCCESS) {
6292 return 103;
Laurence Lundblade59289e52019-12-30 13:44:37 -08006293 }
6294
6295 if(item.uDataType != QCBOR_TYPE_BIGFLOAT ||
6296 item.val.expAndMantissa.nExponent != INT32_MIN ||
6297 item.val.expAndMantissa.Mantissa.nInt != 100) {
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07006298 return 104;
Laurence Lundblade59289e52019-12-30 13:44:37 -08006299 }
6300
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07006301 uErr = QCBORDecode_GetNext(&DC, &item);
6302 if(uErr != QCBOR_SUCCESS) {
6303 return 105;
Laurence Lundblade59289e52019-12-30 13:44:37 -08006304 }
6305
6306 if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM ||
6307 item.val.expAndMantissa.nExponent != INT32_MAX ||
6308 UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) {
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07006309 return 106;
6310 }
6311
Laurence Lundblade37286c02022-09-03 10:05:02 -07006312#endif /* QCBOR_TAGS_DISABLED */
Laurence Lundblade59289e52019-12-30 13:44:37 -08006313
6314 return 0;
6315}
6316
6317
Laurence Lundblade37286c02022-09-03 10:05:02 -07006318int32_t ExponentAndMantissaDecodeTests(void)
6319{
6320 int32_t rv = ProcessEaMTests();
6321 if(rv) {
6322 return rv;
6323 }
6324
6325 return ExponentAndMantissaDecodeTestsSecondary();
6326}
6327
6328
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08006329static const struct DecodeFailTestInput ExponentAndMantissaFailures[] = {
6330 { "Exponent > INT64_MAX",
6331 QCBOR_DECODE_MODE_NORMAL,
6332 {"\xC4\x82\x1B\x7f\xFF\xFF\xFF\xFF\xFF\xFF\xFF\x1B\x80\xFF\xFF\xFF\xFF\xFF\xFF\xFF", 20},
6333 QCBOR_ERR_BAD_EXP_AND_MANTISSA
6334 },
6335 { "Mantissa > INT64_MAX",
6336 QCBOR_DECODE_MODE_NORMAL,
6337 {"\xC4\x82\x1B\x80\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xC3\x4A\x01\x02\x03\x04\x05\x06\x07\x08\x09\x10", 23},
6338 QCBOR_ERR_BAD_EXP_AND_MANTISSA
6339 },
6340 {
6341 "End of input",
6342 QCBOR_DECODE_MODE_NORMAL,
6343 {"\xC4\x82", 2},
6344 QCBOR_ERR_NO_MORE_ITEMS
6345 },
6346 {"bad content for big num",
6347 QCBOR_DECODE_MODE_NORMAL,
6348 {"\xC4\x82\x01\xc3\x01", 5},
6349 QCBOR_ERR_BAD_OPT_TAG
6350 },
6351 {"bad content for big num",
6352 QCBOR_DECODE_MODE_NORMAL,
Laurence Lundbladecdbbc192024-06-28 15:13:04 -07006353 {"\xC4\x82\xC2\x01\x1F", 5},
6354 QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08006355 },
6356 {"Bad integer for exponent",
6357 QCBOR_DECODE_MODE_NORMAL,
6358 {"\xC4\x82\x01\x1f", 4},
6359 QCBOR_ERR_BAD_INT
6360 },
6361 {"Bad integer for mantissa",
6362 QCBOR_DECODE_MODE_NORMAL,
6363 {"\xC4\x82\x1f\x01", 4},
6364 QCBOR_ERR_BAD_INT
6365 },
6366 {"3 items in array",
6367 QCBOR_DECODE_MODE_NORMAL,
6368 {"\xC4\x83\x03\x01\x02", 5},
6369 QCBOR_ERR_BAD_EXP_AND_MANTISSA},
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08006370#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08006371 {"unterminated indefinite length array",
6372 QCBOR_DECODE_MODE_NORMAL,
6373 {"\xC4\x9f\x03\x01\x02", 5},
6374 QCBOR_ERR_BAD_EXP_AND_MANTISSA
6375 },
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08006376#else /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08006377 {"unterminated indefinite length array",
6378 QCBOR_DECODE_MODE_NORMAL,
6379 {"\xC4\x9f\x03\x01\x02", 5},
6380 QCBOR_ERR_INDEF_LEN_ARRAYS_DISABLED
6381 },
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08006382#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08006383 {"Empty array",
6384 QCBOR_DECODE_MODE_NORMAL,
6385 {"\xC4\x80", 2},
6386 QCBOR_ERR_NO_MORE_ITEMS
6387 },
6388 {"Second is not an integer",
6389 QCBOR_DECODE_MODE_NORMAL,
6390 {"\xC4\x82\x03\x40", 4},
6391 QCBOR_ERR_BAD_EXP_AND_MANTISSA
6392 },
6393 {"First is not an integer",
6394 QCBOR_DECODE_MODE_NORMAL,
6395 {"\xC4\x82\x40", 3},
6396 QCBOR_ERR_BAD_EXP_AND_MANTISSA
6397 },
6398 {"Not an array",
6399 QCBOR_DECODE_MODE_NORMAL,
6400 {"\xC4\xA2", 2},
6401 QCBOR_ERR_BAD_EXP_AND_MANTISSA
6402 }
Laurence Lundblade59289e52019-12-30 13:44:37 -08006403};
6404
6405
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08006406int32_t
6407ExponentAndMantissaDecodeFailTests(void)
Laurence Lundblade59289e52019-12-30 13:44:37 -08006408{
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08006409 return ProcessDecodeFailures(ExponentAndMantissaFailures,
6410 C_ARRAY_COUNT(ExponentAndMantissaFailures,
6411 struct DecodeFailTestInput));
Laurence Lundblade59289e52019-12-30 13:44:37 -08006412}
6413
Laurence Lundbladedd6e76e2021-03-10 01:54:01 -07006414#endif /* QCBOR_DISABLE_EXP_AND_MANTISSA */
Laurence Lundbladebb87be22020-04-09 19:15:32 -07006415
6416
6417
6418/*
6419 Some basic CBOR with map and array used in a lot of tests.
6420 The map labels are all strings
6421
Laurence Lundblade8ffdb742020-05-07 02:49:18 -07006422 {
6423 "first integer": 42,
Laurence Lundbladebb87be22020-04-09 19:15:32 -07006424 "an array of two strings": [
6425 "string1", "string2"
6426 ],
6427 "map in a map": {
6428 "bytes 1": h'78787878',
6429 "bytes 2": h'79797979',
6430 "another int": 98,
6431 "text 2": "lies, damn lies and statistics"
6432 }
Laurence Lundbladeddebabe2020-11-22 11:32:17 -08006433 }
Laurence Lundbladebb87be22020-04-09 19:15:32 -07006434 */
Laurence Lundblade9b334962020-08-27 10:55:53 -07006435
Laurence Lundbladef7a70bc2020-10-24 12:23:25 -07006436int32_t SpiffyDecodeBasicMap(UsefulBufC input)
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07006437{
6438 QCBORItem Item1, Item2, Item3;
6439 int64_t nDecodedInt1, nDecodedInt2;
6440 UsefulBufC B1, B2, S1, S2, S3;
6441
6442 QCBORDecodeContext DCtx;
6443 QCBORError nCBORError;
6444
6445 QCBORDecode_Init(&DCtx, input, 0);
6446
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006447 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07006448
6449 QCBORDecode_GetInt64InMapSZ(&DCtx, "first integer", &nDecodedInt1);
6450
6451 QCBORDecode_EnterMapFromMapSZ(&DCtx, "map in a map");
6452 QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2);
Laurence Lundblade323f8a92020-09-06 19:43:09 -07006453 QCBORDecode_GetByteStringInMapSZ(&DCtx, "bytes 1", &B1);
6454 QCBORDecode_GetByteStringInMapSZ(&DCtx, "bytes 2", &B2);
6455 QCBORDecode_GetTextStringInMapSZ(&DCtx, "text 2", &S1);
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07006456 QCBORDecode_ExitMap(&DCtx);
6457
6458 QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings");
6459 QCBORDecode_GetNext(&DCtx, &Item1);
6460 QCBORDecode_GetNext(&DCtx, &Item2);
6461 if(QCBORDecode_GetNext(&DCtx, &Item3) != QCBOR_ERR_NO_MORE_ITEMS) {
6462 return -400;
6463 }
6464 QCBORDecode_ExitArray(&DCtx);
6465
6466 // Parse the same array again using GetText() instead of GetItem()
6467 QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings");
Laurence Lundblade323f8a92020-09-06 19:43:09 -07006468 QCBORDecode_GetTextString(&DCtx, &S2);
6469 QCBORDecode_GetTextString(&DCtx, &S3);
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07006470 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) {
6471 return 5000;
6472 }
6473 /* QCBORDecode_GetText(&DCtx, &S3);
6474 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_NO_MORE_ITEMS) {
6475 return 5001;
6476 } */
6477
6478 QCBORDecode_ExitArray(&DCtx);
6479
6480 QCBORDecode_ExitMap(&DCtx);
6481
6482 nCBORError = QCBORDecode_Finish(&DCtx);
6483
6484 if(nCBORError) {
6485 return (int32_t)nCBORError;
6486 }
6487
6488 if(nDecodedInt1 != 42) {
6489 return 1001;
6490 }
6491
6492 if(nDecodedInt2 != 98) {
6493 return 1002;
6494 }
6495
6496 if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07006497 UsefulBufCompareToSZ(Item1.val.string, "string1")) {
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07006498 return 1003;
6499 }
6500
6501 if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07006502 UsefulBufCompareToSZ(Item2.val.string, "string2")) {
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07006503 return 1004;
6504 }
6505
Laurence Lundblade9b334962020-08-27 10:55:53 -07006506 if(UsefulBufCompareToSZ(S1, "lies, damn lies and statistics")) {
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07006507 return 1005;
6508 }
6509
6510 if(UsefulBuf_Compare(B1, UsefulBuf_FromSZ("xxxx"))){
6511 return 1006;
6512 }
6513
6514 if(UsefulBuf_Compare(B2, UsefulBuf_FromSZ("yyyy"))){
6515 return 1007;
6516 }
6517
6518 if(UsefulBuf_Compare(S2, UsefulBuf_FromSZ("string1"))){
6519 return 1008;
6520 }
6521
6522 if(UsefulBuf_Compare(S3, UsefulBuf_FromSZ("string2"))){
6523 return 1009;
6524 }
6525
6526 return 0;
6527}
6528
Laurence Lundbladeddebabe2020-11-22 11:32:17 -08006529/*
Laurence Lundbladecc7da412020-12-27 00:09:07 -08006530 {
6531 -75008: h'05083399',
6532 88: [],
6533 100100: {
6534 "sub1": {
6535 10: [
6536 0
6537 ],
6538 -75009: h'A46823990001',
6539 100100: {
6540 "json": "{ \"ueid\", \"xyz\"}",
6541 "subsub": {
6542 100002: h'141813191001'
6543 }
6544 }
Laurence Lundbladeddebabe2020-11-22 11:32:17 -08006545 }
Laurence Lundbladecc7da412020-12-27 00:09:07 -08006546 }
Laurence Lundbladeddebabe2020-11-22 11:32:17 -08006547 }
Laurence Lundbladeddebabe2020-11-22 11:32:17 -08006548 */
6549
6550static const uint8_t spNestedCBOR[] = {
Laurence Lundbladecc7da412020-12-27 00:09:07 -08006551 0xa3, 0x3a, 0x00, 0x01, 0x24, 0xff, 0x44, 0x05,
6552 0x08, 0x33, 0x99, 0x18, 0x58, 0x80, 0x1a, 0x00,
6553 0x01, 0x87, 0x04, 0xa1, 0x64, 0x73, 0x75, 0x62,
6554 0x31, 0xa3, 0x0a, 0x81, 0x00, 0x3a, 0x00, 0x01,
6555 0x25, 0x00, 0x46, 0xa4, 0x68, 0x23, 0x99, 0x00,
6556 0x01, 0x1a, 0x00, 0x01, 0x87, 0x04, 0xa2, 0x64,
6557 0x6a, 0x73, 0x6f, 0x6e, 0x70, 0x7b, 0x20, 0x22,
6558 0x75, 0x65, 0x69, 0x64, 0x22, 0x2c, 0x20, 0x22,
6559 0x78, 0x79, 0x7a, 0x22, 0x7d, 0x66, 0x73, 0x75,
6560 0x62, 0x73, 0x75, 0x62, 0xa1, 0x1a, 0x00, 0x01,
6561 0x86, 0xa2, 0x46, 0x14, 0x18, 0x13, 0x19, 0x10,
6562 0x01
Laurence Lundbladeddebabe2020-11-22 11:32:17 -08006563};
6564
6565/* Get item in multi-level nesting in spNestedCBOR */
6566static int32_t DecodeNestedGetSubSub(QCBORDecodeContext *pDCtx)
6567{
6568 UsefulBufC String;
6569
6570 uint8_t test_oemid_bytes[] = {0x14, 0x18, 0x13, 0x19, 0x10, 0x01};
6571 const struct q_useful_buf_c test_oemid = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(test_oemid_bytes);
6572
6573 QCBORDecode_EnterMapFromMapN(pDCtx, 100100);
6574 QCBORDecode_EnterMap(pDCtx, NULL);
6575 QCBORDecode_EnterMapFromMapN(pDCtx, 100100);
6576 QCBORDecode_EnterMapFromMapSZ(pDCtx, "subsub");
6577 QCBORDecode_GetByteStringInMapN(pDCtx, 100002, &String);
6578 if(QCBORDecode_GetError(pDCtx)) {
6579 return 4001;
6580 }
6581 if(UsefulBuf_Compare(String, test_oemid)) {
6582 return 4002;
6583 }
6584 QCBORDecode_ExitMap(pDCtx);
6585 QCBORDecode_ExitMap(pDCtx);
6586 QCBORDecode_ExitMap(pDCtx);
6587 QCBORDecode_ExitMap(pDCtx);
6588
6589 return 0;
6590}
6591
6592/* Iterations on the zero-length array in spNestedCBOR */
6593static int32_t DecodeNestedGetEmpty(QCBORDecodeContext *pDCtx)
6594{
6595 QCBORItem Item;
6596 QCBORError uErr;
6597
6598 QCBORDecode_EnterArrayFromMapN(pDCtx, 88);
6599 for(int x = 0; x < 20; x++) {
6600 uErr = QCBORDecode_GetNext(pDCtx, &Item);
6601 if(uErr != QCBOR_ERR_NO_MORE_ITEMS) {
6602 return 4100;
6603
6604 }
6605 }
6606 QCBORDecode_ExitArray(pDCtx);
6607 if(QCBORDecode_GetError(pDCtx)) {
6608 return 4101;
6609 }
6610
6611 return 0;
6612}
6613
6614/* Various iterations on the array that contains a zero in spNestedCBOR */
6615static int32_t DecodeNestedGetZero(QCBORDecodeContext *pDCtx)
6616{
6617 QCBORError uErr;
6618
6619 QCBORDecode_EnterMapFromMapN(pDCtx, 100100);
6620 QCBORDecode_EnterMapFromMapSZ(pDCtx, "sub1");
6621 QCBORDecode_EnterArrayFromMapN(pDCtx, 10);
6622 int64_t nInt = 99;
6623 QCBORDecode_GetInt64(pDCtx, &nInt);
6624 if(nInt != 0) {
6625 return 4200;
6626 }
6627 for(int x = 0; x < 20; x++) {
6628 QCBORItem Item;
6629 uErr = QCBORDecode_GetNext(pDCtx, &Item);
6630 if(uErr != QCBOR_ERR_NO_MORE_ITEMS) {
6631 return 4201;
6632
6633 }
6634 }
6635 QCBORDecode_ExitArray(pDCtx);
6636 if(QCBORDecode_GetAndResetError(pDCtx)) {
6637 return 4202;
6638 }
6639 QCBORDecode_EnterArrayFromMapN(pDCtx, 10);
6640 UsefulBufC dD;
6641 QCBORDecode_GetByteString(pDCtx, &dD);
6642 if(QCBORDecode_GetAndResetError(pDCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
6643 return 4203;
6644 }
6645 for(int x = 0; x < 20; x++) {
6646 QCBORDecode_GetByteString(pDCtx, &dD);
6647 uErr = QCBORDecode_GetAndResetError(pDCtx);
6648 if(uErr != QCBOR_ERR_NO_MORE_ITEMS) {
6649 return 4204;
6650 }
6651 }
6652 QCBORDecode_ExitArray(pDCtx);
6653 QCBORDecode_ExitMap(pDCtx);
6654 QCBORDecode_ExitMap(pDCtx);
6655
6656 return 0;
6657}
6658
6659/* Repeatedly enter and exit maps and arrays, go off the end of maps
6660 and arrays and such. */
Laurence Lundbladeb9702452021-03-08 21:02:57 -08006661static int32_t DecodeNestedIterate(void)
Laurence Lundbladeddebabe2020-11-22 11:32:17 -08006662{
6663 QCBORDecodeContext DCtx;
6664 int32_t nReturn;
6665 QCBORError uErr;
6666
6667 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spNestedCBOR), 0);
6668 QCBORDecode_EnterMap(&DCtx, NULL);
6669
6670 for(int j = 0; j < 5; j++) {
6671 for(int i = 0; i < 20; i++) {
6672 nReturn = DecodeNestedGetSubSub(&DCtx);
6673 if(nReturn) {
6674 return nReturn;
6675 }
6676 }
6677
6678 for(int i = 0; i < 20; i++) {
6679 nReturn = DecodeNestedGetEmpty(&DCtx);
6680 if(nReturn ) {
6681 return nReturn;
6682 }
6683 }
6684
6685 for(int i = 0; i < 20; i++) {
6686 nReturn = DecodeNestedGetZero(&DCtx);
6687 if(nReturn ) {
6688 return nReturn;
6689 }
6690 }
6691 }
6692
6693 QCBORDecode_ExitMap(&DCtx);
6694 uErr = QCBORDecode_Finish(&DCtx);
6695 if(uErr) {
6696 return (int32_t)uErr + 4100;
6697 }
6698
6699 return 0;
6700}
6701
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07006702
6703/*
Laurence Lundbladecc7da412020-12-27 00:09:07 -08006704 [
6705 23,
6706 6000,
6707 h'67616C6163746963',
6708 h'686176656E20746F6B656E'
6709 ]
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07006710 */
6711static const uint8_t spSimpleArray[] = {
Laurence Lundblade9b334962020-08-27 10:55:53 -07006712 0x84,
6713 0x17,
6714 0x19, 0x17, 0x70,
6715 0x48, 0x67, 0x61, 0x6C, 0x61, 0x63, 0x74, 0x69, 0x63,
6716 0x4B, 0x68, 0x61, 0x76, 0x65, 0x6E, 0x20, 0x74, 0x6F, 0x6B, 0x65, 0x6E};
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07006717
Laurence Lundbladecc7da412020-12-27 00:09:07 -08006718/* [h'', {}, [], 0] */
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08006719static const uint8_t spArrayOfEmpty[] = {0x84, 0x40, 0xa0, 0x80, 0x00};
6720
Laurence Lundbladecc7da412020-12-27 00:09:07 -08006721/* {} */
Laurence Lundbladee6f15112020-07-23 18:44:16 -07006722static const uint8_t spEmptyMap[] = {0xa0};
6723
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08006724#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundbladecc7da412020-12-27 00:09:07 -08006725/* {} */
Laurence Lundbladee6f15112020-07-23 18:44:16 -07006726static const uint8_t spEmptyInDefinteLengthMap[] = {0xbf, 0xff};
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07006727
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08006728
Laurence Lundbladef0499502020-08-01 11:55:57 -07006729/*
6730 {
6731 0: [],
6732 9: [
6733 [],
6734 []
6735 ],
6736 8: {
6737 1: [],
6738 2: {},
6739 3: []
6740 },
6741 4: {},
6742 5: [],
6743 6: [
6744 [],
6745 []
6746 ]
Laurence Lundbladecc7da412020-12-27 00:09:07 -08006747 }
Laurence Lundbladef0499502020-08-01 11:55:57 -07006748 */
6749static const uint8_t spMapOfEmpty[] = {
Laurence Lundbladecc7da412020-12-27 00:09:07 -08006750 0xa6, 0x00, 0x80, 0x09, 0x82, 0x80, 0x80, 0x08,
6751 0xa3, 0x01, 0x80, 0x02, 0xa0, 0x03, 0x80, 0x04,
6752 0xa0, 0x05, 0x9f, 0xff, 0x06, 0x9f, 0x80, 0x9f,
6753 0xff, 0xff};
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07006754
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08006755#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
6756
6757
Laurence Lundblade93d3f532020-09-28 21:09:12 -07006758/*
6759 Too many tags
Laurence Lundblade93d3f532020-09-28 21:09:12 -07006760 Duplicate label
6761 Integer overflow
6762 Date overflow
6763
Laurence Lundbladecc7da412020-12-27 00:09:07 -08006764 {
6765 1: 224(225(226(227(4(0))))),
Laurence Lundbladecc7da412020-12-27 00:09:07 -08006766 3: -18446744073709551616,
6767 4: 1(1.0e+300),
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07006768 5: 0,
6769 8: 8
Laurence Lundbladecc7da412020-12-27 00:09:07 -08006770 }
Laurence Lundblade93d3f532020-09-28 21:09:12 -07006771 */
6772static const uint8_t spRecoverableMapErrors[] = {
Laurence Lundblade37286c02022-09-03 10:05:02 -07006773#ifndef QCBOR_DISABLE_TAGS
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07006774 0xa6,
Laurence Lundblade93d3f532020-09-28 21:09:12 -07006775 0x04, 0xc1, 0xfb, 0x7e, 0x37, 0xe4, 0x3c, 0x88, 0x00, 0x75, 0x9c,
Laurence Lundblade37286c02022-09-03 10:05:02 -07006776 0x01, 0xd8, 0xe0, 0xd8, 0xe1, 0xd8, 0xe2, 0xd8, 0xe3, 0xd8, 0x04, 0x00,
6777#else
6778 0xa4,
6779#endif
6780 0x03, 0x3b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
Laurence Lundblade93d3f532020-09-28 21:09:12 -07006781 0x05, 0x00,
6782 0x05, 0x00,
6783 0x08, 0x08,
Laurence Lundblade93d3f532020-09-28 21:09:12 -07006784};
6785
Laurence Lundbladecc7da412020-12-27 00:09:07 -08006786/* Bad break */
Laurence Lundblade93d3f532020-09-28 21:09:12 -07006787static const uint8_t spUnRecoverableMapError1[] = {
6788 0xa2, 0xff, 0x01, 0x00, 0x02, 0x00
6789};
6790
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08006791#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundbladecc7da412020-12-27 00:09:07 -08006792/* No more items */
Laurence Lundblade93d3f532020-09-28 21:09:12 -07006793static const uint8_t spUnRecoverableMapError2[] = {
6794 0xbf, 0x02, 0xbf, 0xff, 0x01, 0x00, 0x02, 0x00
6795};
6796
Laurence Lundbladecc7da412020-12-27 00:09:07 -08006797/* Hit end because string is too long */
Laurence Lundblade93d3f532020-09-28 21:09:12 -07006798static const uint8_t spUnRecoverableMapError3[] = {
6799 0xbf, 0x02, 0x69, 0x64, 0x64, 0xff
6800};
6801
Laurence Lundbladecc7da412020-12-27 00:09:07 -08006802/* Hit end because string is too long */
Laurence Lundblade93d3f532020-09-28 21:09:12 -07006803static const uint8_t spUnRecoverableMapError4[] = {
6804 0xbf,
6805 0x02, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f,
6806 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f,
6807 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
6808 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
6809 0xff
6810};
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08006811#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
Laurence Lundblade93d3f532020-09-28 21:09:12 -07006812
Laurence Lundblade63926052021-03-29 16:05:51 -07006813const unsigned char not_well_formed_submod_section[] = {
6814 0xa1, 0x14, 0x1f,
6815};
Laurence Lundblade93d3f532020-09-28 21:09:12 -07006816
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07006817
6818/* Array of length 3, but only two items. */
6819const unsigned char spBadConsumeInput[] = {
6820 0x83, 0x00, 0x00
6821};
6822
6823/* Tag nesting too deep. */
6824const unsigned char spBadConsumeInput2[] = {
6825 0x81,
6826 0xD8, 0x37,
6827 0xD8, 0x2C,
6828 0xD8, 0x21,
6829 0xD6,
6830 0xCB,
6831 00
6832};
6833
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07006834
6835const unsigned char spBadConsumeInput4[] = {
6836 0x81, 0x9f, 0x00, 0xff
6837};
6838
6839const unsigned char spBadConsumeInput5[] = {
6840 0xa1, 0x80, 0x00
6841};
6842
Laurence Lundbladec5f45e42023-12-18 09:23:20 -07006843/*
6844 Lots of nesting for various nesting tests.
6845 { 1:1,
6846 2:{
6847 21:21,
6848 22:{
6849 221:[2111, 2112, 2113],
6850 222:222,
6851 223: {}
6852 },
6853 23: 23
6854 },
6855 3:3,
6856 4: [ {} ]
6857 }
6858 */
6859static const uint8_t spNested[] = {
68600xA4, /* Map of 4 */
6861 0x01, 0x01, /* Map entry 1 : 1 */
6862 0x02, 0xA3, /* Map entry 2 : {, an array of 3 */
6863 0x15, 0x15, /* Map entry 21 : 21 */
6864 0x16, 0xA3, /* Map entry 22 : {, a map of 3 */
6865 0x18, 0xDD, 0x83, /* Map entry 221 : [ an array of 3 */
6866 0x19, 0x08, 0x3F, /* Array item 2111 */
6867 0x19, 0x08, 0x40, /* Array item 2112 */
6868 0x19, 0x08, 0x41, /* Array item 2113 */
6869 0x18, 0xDE, 0x18, 0xDE, /* Map entry 222 : 222 */
6870 0x18, 0xDF, 0xA0, /* Map entry 223 : {} */
6871 0x17, 0x17, /* Map entry 23 : 23 */
6872 0x03, 0x03, /* Map entry 3 : 3 */
6873 0x04, 0x81, /* Map entry 4: [, an array of 1 */
6874 0xA0 /* Array entry {}, an empty map */
6875};
6876
6877
6878static int32_t EnterMapCursorTest(void)
6879{
6880 QCBORDecodeContext DCtx;
6881 QCBORItem Item1;
Laurence Lundblade11654912024-05-09 11:49:24 -07006882 int64_t nInt;
6883 QCBORError uErr;
6884
6885 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spNested), 0);
6886 QCBORDecode_EnterMap(&DCtx, NULL);
6887 QCBORDecode_GetInt64InMapN (&DCtx, 3, &nInt);
6888 uErr = QCBORDecode_GetNext(&DCtx, &Item1);
Laurence Lundbladea29f45a2024-05-14 15:55:19 -07006889 if(uErr != QCBOR_SUCCESS) {
6890 return 701;
6891 }
Laurence Lundblade11654912024-05-09 11:49:24 -07006892 if(Item1.uDataType != QCBOR_TYPE_INT64) {
6893 return 700;
6894 }
6895
Laurence Lundbladec5f45e42023-12-18 09:23:20 -07006896
6897 int i;
6898 for(i = 0; i < 13; i++) {
6899 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spNested), 0);
6900 QCBORDecode_EnterMap(&DCtx, NULL);
6901 int j;
6902 /* Move travesal cursor */
6903 for(j = 0; j < i; j++) {
6904 QCBORDecode_GetNext(&DCtx, &Item1);
6905 }
6906 QCBORDecode_EnterMapFromMapN(&DCtx, 2);
6907 QCBORDecode_ExitMap(&DCtx);
6908 QCBORDecode_GetNext(&DCtx, &Item1);
6909 if(Item1.label.int64 != 3) {
6910 return 8000;
6911 }
6912 }
6913
6914 for(i = 0; i < 13; i++) {
6915 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spNested), 0);
6916 QCBORDecode_EnterMap(&DCtx, NULL);
6917 int j;
6918 /* Move travesal cursor */
6919 for(j = 0; j < i; j++) {
6920 QCBORDecode_GetNext(&DCtx, &Item1);
6921 }
6922 QCBORDecode_EnterMapFromMapN(&DCtx, 2);
6923 QCBORDecode_EnterMapFromMapN(&DCtx, 22);
6924 QCBORDecode_ExitMap(&DCtx);
6925 QCBORDecode_GetNext(&DCtx, &Item1);
6926 if(Item1.label.int64 != 23) {
6927 return 8000;
6928 }
6929 }
6930
6931 for(i = 0; i < 13; i++) {
6932 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spNested), 0);
6933 QCBORDecode_EnterMap(&DCtx, NULL);
6934 int j;
6935 /* Move travesal cursor */
6936 for(j = 0; j < i; j++) {
6937 QCBORDecode_GetNext(&DCtx, &Item1);
6938 }
6939 QCBORDecode_EnterMapFromMapN(&DCtx, 2);
6940 QCBORDecode_EnterMapFromMapN(&DCtx, 22);
6941 for(j = 0; j < i; j++) {
6942 QCBORDecode_GetNext(&DCtx, &Item1);
6943 }
6944 QCBORDecode_EnterArrayFromMapN(&DCtx, 221);
6945 QCBORDecode_ExitArray(&DCtx);
6946 QCBORDecode_ExitMap(&DCtx);
6947 QCBORDecode_GetNext(&DCtx, &Item1);
6948 if(Item1.label.int64 != 23) {
6949 return 8000;
6950 }
6951 QCBORDecode_ExitMap(&DCtx);
6952 QCBORDecode_GetNext(&DCtx, &Item1);
6953 if(Item1.label.int64 != 3) {
6954 return 8000;
6955 }
6956 }
6957
6958 return 0;
6959}
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07006960
6961
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03006962int32_t EnterMapTest(void)
Laurence Lundbladebb87be22020-04-09 19:15:32 -07006963{
Laurence Lundbladef0499502020-08-01 11:55:57 -07006964 QCBORItem Item1;
Laurence Lundbladebb87be22020-04-09 19:15:32 -07006965 QCBORDecodeContext DCtx;
Laurence Lundbladef0499502020-08-01 11:55:57 -07006966 int32_t nReturn;
6967 QCBORError uErr;
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07006968
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08006969#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07006970 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spMapOfEmpty), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006971 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07006972
Laurence Lundbladef0499502020-08-01 11:55:57 -07006973
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006974 QCBORDecode_EnterArray(&DCtx, NULL); // Label 0
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07006975 QCBORDecode_ExitArray(&DCtx);
6976
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006977 QCBORDecode_EnterArray(&DCtx, NULL); // Label 9
6978 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07006979 QCBORDecode_ExitArray(&DCtx);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006980 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07006981 QCBORDecode_ExitArray(&DCtx);
6982 QCBORDecode_ExitArray(&DCtx);
6983
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006984 QCBORDecode_EnterMap(&DCtx, NULL); // Label 8
6985 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07006986 QCBORDecode_ExitArray(&DCtx);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006987 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07006988 QCBORDecode_ExitMap(&DCtx);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006989 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07006990 QCBORDecode_ExitArray(&DCtx);
6991 QCBORDecode_ExitMap(&DCtx);
6992
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006993 QCBORDecode_EnterMap(&DCtx, NULL); // Label4
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07006994 QCBORDecode_ExitMap(&DCtx);
6995
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006996 QCBORDecode_EnterArray(&DCtx, NULL); // Label 5
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07006997 QCBORDecode_ExitArray(&DCtx);
6998
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006999 QCBORDecode_EnterArray(&DCtx, NULL); // Label 6
7000 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07007001 QCBORDecode_ExitArray(&DCtx);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07007002 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07007003 QCBORDecode_ExitArray(&DCtx);
7004 QCBORDecode_ExitArray(&DCtx);
7005
7006 QCBORDecode_ExitMap(&DCtx);
7007
7008 uErr = QCBORDecode_Finish(&DCtx);
7009 if(uErr != QCBOR_SUCCESS){
7010 return 3011;
7011 }
7012
Laurence Lundbladeec290b82024-06-10 11:10:54 -07007013#ifndef QCBOR_DISABLE_NON_INTEGER_LABELS
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07007014 (void)pValidMapIndefEncoded;
Laurence Lundbladef7a70bc2020-10-24 12:23:25 -07007015 nReturn = SpiffyDecodeBasicMap(UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapIndefEncoded));
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07007016 if(nReturn) {
7017 return nReturn + 20000;
Laurence Lundbladebb87be22020-04-09 19:15:32 -07007018 }
Laurence Lundbladeec290b82024-06-10 11:10:54 -07007019#endif /* ! QCBOR_DISABLE_NON_INTEGER_LABELS */
7020#endif /* ! QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08007021
Laurence Lundbladeec290b82024-06-10 11:10:54 -07007022#ifndef QCBOR_DISABLE_NON_INTEGER_LABELS
7023 QCBORItem ArrayItem;
Laurence Lundbladebb87be22020-04-09 19:15:32 -07007024
Laurence Lundbladef7a70bc2020-10-24 12:23:25 -07007025 nReturn = SpiffyDecodeBasicMap(UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded));
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07007026 if(nReturn) {
7027 return nReturn;
7028 }
Laurence Lundbladebb87be22020-04-09 19:15:32 -07007029
Laurence Lundblade8ffdb742020-05-07 02:49:18 -07007030
Laurence Lundblade937ea812020-05-08 11:38:23 -07007031
Laurence Lundblade2f467f92020-10-09 17:50:11 -07007032 // These tests confirm the cursor is at the right place after entering
7033 // a map or array
Laurence Lundblade9b334962020-08-27 10:55:53 -07007034 const UsefulBufC ValidEncodedMap = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded);
Laurence Lundblade937ea812020-05-08 11:38:23 -07007035
7036 // Confirm cursor is at right place
Laurence Lundblade9b334962020-08-27 10:55:53 -07007037 QCBORDecode_Init(&DCtx, ValidEncodedMap, 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07007038 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade937ea812020-05-08 11:38:23 -07007039 QCBORDecode_GetNext(&DCtx, &Item1);
7040 if(Item1.uDataType != QCBOR_TYPE_INT64) {
7041 return 2001;
7042 }
7043
7044
Laurence Lundblade9b334962020-08-27 10:55:53 -07007045 QCBORDecode_Init(&DCtx, ValidEncodedMap, 0);
Laurence Lundblade6f3f78e2020-08-31 13:09:14 -07007046 QCBORDecode_VGetNext(&DCtx, &Item1);
7047 QCBORDecode_VGetNext(&DCtx, &Item1);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07007048 QCBORDecode_EnterArray(&DCtx, &ArrayItem);
7049 if(ArrayItem.uLabelType != QCBOR_TYPE_TEXT_STRING ||
7050 UsefulBuf_Compare(ArrayItem.label.string,
7051 UsefulBuf_FROM_SZ_LITERAL("an array of two strings"))) {
7052 return 2051;
7053 }
Laurence Lundblade937ea812020-05-08 11:38:23 -07007054 QCBORDecode_GetNext(&DCtx, &Item1);
7055 if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING) {
7056 return 2002;
7057 }
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07007058 QCBORDecode_ExitArray(&DCtx);
7059 QCBORDecode_EnterMap(&DCtx, &ArrayItem);
7060 if(ArrayItem.uLabelType != QCBOR_TYPE_TEXT_STRING ||
7061 UsefulBuf_Compare(ArrayItem.label.string,
7062 UsefulBuf_FROM_SZ_LITERAL("map in a map"))) {
7063 return 2052;
7064 }
7065
Laurence Lundblade937ea812020-05-08 11:38:23 -07007066
Laurence Lundblade9b334962020-08-27 10:55:53 -07007067 QCBORDecode_Init(&DCtx, ValidEncodedMap, 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07007068 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade64b607e2020-05-13 13:05:57 -07007069 QCBORDecode_GetNext(&DCtx, &Item1);
7070 QCBORDecode_GetNext(&DCtx, &Item1);
7071 QCBORDecode_GetNext(&DCtx, &Item1);
Laurence Lundblade937ea812020-05-08 11:38:23 -07007072 QCBORDecode_EnterMapFromMapSZ(&DCtx, "map in a map");
7073 QCBORDecode_GetNext(&DCtx, &Item1);
7074 if(Item1.uDataType != QCBOR_TYPE_BYTE_STRING) {
7075 return 2003;
7076 }
7077
Laurence Lundblade9b334962020-08-27 10:55:53 -07007078 QCBORDecode_Init(&DCtx, ValidEncodedMap, 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07007079 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade937ea812020-05-08 11:38:23 -07007080 QCBORDecode_GetNext(&DCtx, &Item1);
7081 QCBORDecode_GetNext(&DCtx, &Item1);
7082 QCBORDecode_GetNext(&DCtx, &Item1);
7083 QCBORDecode_GetNext(&DCtx, &Item1);
7084 QCBORDecode_GetNext(&DCtx, &Item1);
7085 QCBORDecode_GetNext(&DCtx, &Item1);
7086 QCBORDecode_GetNext(&DCtx, &Item1);
7087 QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings");
7088 QCBORDecode_GetNext(&DCtx, &Item1);
7089 if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING) {
Laurence Lundblade64b607e2020-05-13 13:05:57 -07007090 return 2004;
Laurence Lundblade937ea812020-05-08 11:38:23 -07007091 }
7092
Laurence Lundblade9b334962020-08-27 10:55:53 -07007093 QCBORDecode_Init(&DCtx, ValidEncodedMap, 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07007094 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade2b843b52020-06-16 20:51:03 -07007095 QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings");
7096 QCBORDecode_ExitArray(&DCtx);
7097 QCBORDecode_GetNext(&DCtx, &Item1);
7098 if(Item1.uDataType != QCBOR_TYPE_MAP && Item1.uLabelAlloc != QCBOR_TYPE_TEXT_STRING) {
7099 return 2006;
7100 }
7101 QCBORDecode_ExitMap(&DCtx);
7102 if(QCBORDecode_GetNext(&DCtx, &Item1) != QCBOR_ERR_NO_MORE_ITEMS) {
7103 return 2007;
7104 }
Laurence Lundbladeec290b82024-06-10 11:10:54 -07007105#endif /* !QCBOR_DISABLE_NON_INTEGER_LABELS */
Laurence Lundblade2b843b52020-06-16 20:51:03 -07007106
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07007107 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSimpleArray), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07007108 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07007109 int64_t nDecodedInt2;
Laurence Lundbladeec290b82024-06-10 11:10:54 -07007110
7111 UsefulBufC String;
7112 QCBORDecode_GetTextStringInMapN(&DCtx, 88, &String);
7113 uErr = QCBORDecode_GetAndResetError(&DCtx);
7114 if(uErr != QCBOR_ERR_MAP_NOT_ENTERED){
7115 return 2009;
7116 }
7117#ifndef QCBOR_DISABLE_NON_INTEGER_LABELS
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07007118 QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2);
7119 uErr = QCBORDecode_GetAndResetError(&DCtx);
7120 if(uErr != QCBOR_ERR_MAP_NOT_ENTERED){
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07007121 return 2008;
7122 }
Laurence Lundbladeec290b82024-06-10 11:10:54 -07007123#endif /* ! QCBOR_DISABLE_NON_INTEGER_LABELS */
Laurence Lundblade937ea812020-05-08 11:38:23 -07007124
Laurence Lundbladee6f15112020-07-23 18:44:16 -07007125
7126 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEmptyMap), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07007127 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundbladee6f15112020-07-23 18:44:16 -07007128 // This will fail because the map is empty.
7129 QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2);
7130 uErr = QCBORDecode_GetAndResetError(&DCtx);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07007131 if(uErr != QCBOR_ERR_LABEL_NOT_FOUND){
Laurence Lundbladee6f15112020-07-23 18:44:16 -07007132 return 2010;
7133 }
7134 QCBORDecode_ExitMap(&DCtx);
7135 uErr = QCBORDecode_Finish(&DCtx);
7136 if(uErr != QCBOR_SUCCESS){
7137 return 2011;
7138 }
7139
7140
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08007141#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundbladee6f15112020-07-23 18:44:16 -07007142 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEmptyInDefinteLengthMap), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07007143 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundbladee6f15112020-07-23 18:44:16 -07007144 // This will fail because the map is empty.
7145 QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2);
7146 uErr = QCBORDecode_GetAndResetError(&DCtx);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07007147 if(uErr != QCBOR_ERR_LABEL_NOT_FOUND){
Laurence Lundblade085d7952020-07-24 10:26:30 -07007148 return 2012;
Laurence Lundbladee6f15112020-07-23 18:44:16 -07007149 }
7150 QCBORDecode_ExitMap(&DCtx);
7151 uErr = QCBORDecode_Finish(&DCtx);
7152 if(uErr != QCBOR_SUCCESS){
Laurence Lundblade085d7952020-07-24 10:26:30 -07007153 return 2013;
Laurence Lundbladee6f15112020-07-23 18:44:16 -07007154 }
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08007155#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
Laurence Lundbladee6f15112020-07-23 18:44:16 -07007156
Laurence Lundblade5f4e8712020-07-25 11:44:43 -07007157
7158 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spArrayOfEmpty), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07007159 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade323f8a92020-09-06 19:43:09 -07007160 QCBORDecode_GetByteString(&DCtx, &String);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07007161 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade5f4e8712020-07-25 11:44:43 -07007162 QCBORDecode_ExitMap(&DCtx);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07007163 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade5f4e8712020-07-25 11:44:43 -07007164 QCBORDecode_ExitArray(&DCtx);
7165 QCBORDecode_GetInt64(&DCtx, &nDecodedInt2);
7166 QCBORDecode_ExitArray(&DCtx);
7167 uErr = QCBORDecode_Finish(&DCtx);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07007168 if(uErr != QCBOR_SUCCESS) {
Laurence Lundblade5f4e8712020-07-25 11:44:43 -07007169 return 2014;
7170 }
7171
Laurence Lundblade93d3f532020-09-28 21:09:12 -07007172 int64_t nInt;
7173 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spRecoverableMapErrors), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07007174 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade37286c02022-09-03 10:05:02 -07007175#ifndef QCBOR_DISABLE_TAGS
Laurence Lundblade93d3f532020-09-28 21:09:12 -07007176 QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt);
Laurence Lundblade88e9db22020-11-02 03:56:33 -08007177 uErr = QCBORDecode_GetError(&DCtx);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07007178 if(uErr != QCBOR_ERR_TOO_MANY_TAGS) {
7179 return 2021;
7180 }
Laurence Lundblade88e9db22020-11-02 03:56:33 -08007181 if(QCBORDecode_GetNthTagOfLast(&DCtx, 0) != CBOR_TAG_INVALID64) {
7182 return 2121;
7183 }
7184 (void)QCBORDecode_GetAndResetError(&DCtx);
Laurence Lundblade37286c02022-09-03 10:05:02 -07007185#endif
Laurence Lundblade88e9db22020-11-02 03:56:33 -08007186
Laurence Lundblade93d3f532020-09-28 21:09:12 -07007187
Laurence Lundblade93d3f532020-09-28 21:09:12 -07007188 QCBORDecode_GetInt64InMapN(&DCtx, 0x03, &nInt);
7189 uErr = QCBORDecode_GetAndResetError(&DCtx);
7190 if(uErr != QCBOR_ERR_INT_OVERFLOW) {
7191 return 2023;
7192 }
7193
Laurence Lundblade37286c02022-09-03 10:05:02 -07007194#ifndef QCBOR_DISABLE_TAGS
Laurence Lundblade93d3f532020-09-28 21:09:12 -07007195 QCBORDecode_GetEpochDateInMapN(&DCtx, 0x04, QCBOR_TAG_REQUIREMENT_TAG, &nInt);
7196 uErr = QCBORDecode_GetAndResetError(&DCtx);
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007197 if(uErr != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_DATE_OVERFLOW)) {
Laurence Lundblade93d3f532020-09-28 21:09:12 -07007198 return 2024;
7199 }
Laurence Lundblade37286c02022-09-03 10:05:02 -07007200#endif
Laurence Lundblade93d3f532020-09-28 21:09:12 -07007201
7202 QCBORDecode_GetInt64InMapN(&DCtx, 0x05, &nInt);
7203 uErr = QCBORDecode_GetAndResetError(&DCtx);
7204 if(uErr != QCBOR_ERR_DUPLICATE_LABEL) {
7205 return 2025;
7206 }
7207
7208 QCBORDecode_GetInt64InMapN(&DCtx, 0x08, &nInt);
7209
7210 QCBORDecode_ExitMap(&DCtx);
7211 uErr = QCBORDecode_Finish(&DCtx);
7212 if(uErr != QCBOR_SUCCESS) {
7213 return 2026;
7214 }
7215
7216 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUnRecoverableMapError1), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07007217 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07007218 QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt);
7219 uErr = QCBORDecode_GetAndResetError(&DCtx);
7220 if(uErr != QCBOR_ERR_BAD_BREAK) {
7221 return 2030;
7222 }
7223
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08007224#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundblade93d3f532020-09-28 21:09:12 -07007225 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUnRecoverableMapError2), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07007226 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07007227 QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt);
7228 uErr = QCBORDecode_GetAndResetError(&DCtx);
7229 if(uErr != QCBOR_ERR_NO_MORE_ITEMS) {
7230 return 2031;
7231 }
7232
7233 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUnRecoverableMapError3), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07007234 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07007235 QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt);
7236 uErr = QCBORDecode_GetAndResetError(&DCtx);
7237 if(uErr != QCBOR_ERR_HIT_END) {
7238 return 2032;
7239 }
7240
7241 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUnRecoverableMapError4), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07007242 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07007243 QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt);
7244 uErr = QCBORDecode_GetAndResetError(&DCtx);
7245 if(uErr != QCBOR_ERR_ARRAY_DECODE_NESTING_TOO_DEEP) {
7246 return 2033;
7247 }
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08007248#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
7249
Laurence Lundbladeec290b82024-06-10 11:10:54 -07007250#ifndef QCBOR_DISABLE_NON_INTEGER_LABELS
Laurence Lundblade732e52d2021-02-22 20:11:01 -07007251 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0);
7252 QCBORDecode_VGetNextConsume(&DCtx, &Item1);
7253 if(Item1.uDataType != QCBOR_TYPE_MAP) {
7254 return 2401;
7255 }
7256 if(QCBORDecode_GetError(&DCtx)) {
7257 return 2402;
7258 }
7259
7260 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0);
7261 QCBORDecode_VGetNext(&DCtx, &Item1);
7262 if(Item1.uDataType != QCBOR_TYPE_MAP ||
7263 Item1.val.uCount != 3 ||
7264 Item1.uNextNestLevel != 1) {
7265 return 2403;
7266 }
7267 if(QCBORDecode_GetError(&DCtx)) {
7268 return 2404;
7269 }
7270 QCBORDecode_VGetNextConsume(&DCtx, &Item1);
7271 if(Item1.uDataType != QCBOR_TYPE_INT64 ||
7272 Item1.uNextNestLevel != 1 ||
7273 Item1.val.int64 != 42) {
7274 return 2405;
7275 }
7276 if(QCBORDecode_GetError(&DCtx)) {
7277 return 2406;
7278 }
7279 QCBORDecode_VGetNextConsume(&DCtx, &Item1);
7280 if(Item1.uDataType != QCBOR_TYPE_ARRAY ||
7281 Item1.uNestingLevel != 1 ||
7282 Item1.uNextNestLevel != 1 ||
7283 Item1.val.uCount != 2) {
7284 return 2407;
7285 }
7286 if(QCBORDecode_GetError(&DCtx)) {
7287 return 2408;
7288 }
7289 QCBORDecode_VGetNextConsume(&DCtx, &Item1);
7290 if(Item1.uDataType != QCBOR_TYPE_MAP ||
7291 Item1.uNestingLevel != 1 ||
7292 Item1.uNextNestLevel != 0 ||
7293 Item1.val.uCount != 4) {
7294 return 2409;
7295 }
7296 if(QCBORDecode_GetError(&DCtx)) {
7297 return 2410;
7298 }
Laurence Lundbladeec290b82024-06-10 11:10:54 -07007299#endif /* ! QCBOR_DISABLE_NON_INTEGER_LABELS */
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07007300
Laurence Lundbladeddebabe2020-11-22 11:32:17 -08007301 nReturn = DecodeNestedIterate();
7302
Laurence Lundblade63926052021-03-29 16:05:51 -07007303
7304 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(not_well_formed_submod_section), 0);
7305 QCBORDecode_EnterMap(&DCtx, NULL);
7306 QCBORDecode_EnterMapFromMapN(&DCtx, 20);
7307 if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_BAD_INT) {
7308 return 2500;
7309 }
7310
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07007311 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBadConsumeInput), 0);
7312 QCBORDecode_VGetNextConsume(&DCtx, &Item1);
7313 if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_NO_MORE_ITEMS) {
7314 return 2600;
7315 }
7316
Laurence Lundblade37286c02022-09-03 10:05:02 -07007317#ifndef QCBOR_DISABLE_TAGS
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07007318 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBadConsumeInput2), 0);
7319 QCBORDecode_VGetNextConsume(&DCtx, &Item1);
7320 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) {
7321 return 2700;
7322 }
Laurence Lundblade37286c02022-09-03 10:05:02 -07007323#endif
7324
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07007325
7326 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBadConsumeInput4), 0);
7327 QCBORDecode_VGetNextConsume(&DCtx, &Item1);
7328#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
7329 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) {
7330 return 2900;
7331 }
7332#else
7333 if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_INDEF_LEN_ARRAYS_DISABLED) {
7334 return 2901;
7335 }
7336#endif
7337
7338 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBadConsumeInput5), 0);
7339 QCBORDecode_VGetNextConsume(&DCtx, &Item1);
7340 if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_MAP_LABEL_TYPE) {
7341 return 3000;
7342 }
7343
Laurence Lundbladec5f45e42023-12-18 09:23:20 -07007344 nReturn = EnterMapCursorTest();
7345
Laurence Lundbladeddebabe2020-11-22 11:32:17 -08007346 return nReturn;
Laurence Lundblade9c905e82020-04-25 11:31:38 -07007347}
7348
7349
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007350struct NumberConversion {
7351 char *szDescription;
7352 UsefulBufC CBOR;
7353 int64_t nConvertedToInt64;
7354 QCBORError uErrorInt64;
7355 uint64_t uConvertToUInt64;
7356 QCBORError uErrorUint64;
7357 double dConvertToDouble;
7358 QCBORError uErrorDouble;
7359};
7360
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007361#ifndef QCBOR_DISABLE_EXP_AND_MANTISSA
7362#define EXP_AND_MANTISSA_ERROR(x) x
7363#else
7364#define EXP_AND_MANTISSA_ERROR(x) QCBOR_ERR_UNEXPECTED_TYPE
7365#endif
7366
7367
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07007368static const struct NumberConversion NumberConversions[] = {
Laurence Lundblade37286c02022-09-03 10:05:02 -07007369#ifndef QCBOR_DISABLE_TAGS
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007370 {
Laurence Lundblade4e808ba2022-12-29 12:45:20 -07007371 "Big float: INT64_MIN * 2e-1 to test handling of INT64_MIN",
7372 {(uint8_t[]){0xC5, 0x82, 0x20,
7373 0x3B, 0x7f, 0xff, 0xff, 0xff, 0xff, 0x0ff, 0xff, 0xff,
7374 }, 15},
7375 -4611686018427387904, /* INT64_MIN / 2 */
7376 EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS),
7377 0,
7378 EXP_AND_MANTISSA_ERROR(QCBOR_ERR_NUMBER_SIGN_CONVERSION),
7379 -4.6116860184273879E+18,
7380 FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS))
7381 },
7382 {
Laurence Lundblade784b54b2020-08-10 01:24:52 -07007383 "too large to fit into int64_t",
7384 {(uint8_t[]){0xc3, 0x48, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 10},
7385 0,
7386 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
7387 0,
7388 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
7389 ((double)INT64_MIN) + 1 ,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007390 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)
Laurence Lundblade784b54b2020-08-10 01:24:52 -07007391 },
7392 {
7393 "largest negative int that fits in int64_t",
7394 {(uint8_t[]){0xc3, 0x48, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, 10},
7395 INT64_MIN,
7396 QCBOR_SUCCESS,
7397 0,
7398 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
7399 (double)INT64_MIN,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007400 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)
Laurence Lundblade784b54b2020-08-10 01:24:52 -07007401 },
7402 {
Laurence Lundbladeda095972020-06-06 18:35:33 -07007403 "negative bignum -1",
7404 {(uint8_t[]){0xc3, 0x41, 0x00}, 3},
7405 -1,
7406 QCBOR_SUCCESS,
7407 0,
7408 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
7409 -1.0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007410 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)
Laurence Lundbladeda095972020-06-06 18:35:33 -07007411 },
7412 {
Laurence Lundblade9ab5abb2020-05-20 12:10:45 -07007413 "Decimal Fraction with positive bignum 257 * 10e3",
Laurence Lundblade887add82020-05-17 05:50:34 -07007414 {(uint8_t[]){0xC4, 0x82, 0x1B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03,
7415 0xC2, 0x42, 0x01, 0x01}, 15},
7416 257000,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007417 EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS),
Laurence Lundblade887add82020-05-17 05:50:34 -07007418 257000,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007419 EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS),
Laurence Lundblade887add82020-05-17 05:50:34 -07007420 257000.0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007421 FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS))
Laurence Lundblade887add82020-05-17 05:50:34 -07007422 },
7423 {
Laurence Lundbladeda095972020-06-06 18:35:33 -07007424 "bigfloat with negative bignum -258 * 2e3",
Laurence Lundblade887add82020-05-17 05:50:34 -07007425 {(uint8_t[]){0xC5, 0x82, 0x1B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03,
7426 0xC3, 0x42, 0x01, 0x01}, 15},
Laurence Lundbladeda095972020-06-06 18:35:33 -07007427 -2064,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007428 EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS),
Laurence Lundblade887add82020-05-17 05:50:34 -07007429 0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007430 EXP_AND_MANTISSA_ERROR(QCBOR_ERR_NUMBER_SIGN_CONVERSION),
Laurence Lundbladeda095972020-06-06 18:35:33 -07007431 -2064.0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007432 FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS))
Laurence Lundblade887add82020-05-17 05:50:34 -07007433 },
7434 {
Laurence Lundblade9ab5abb2020-05-20 12:10:45 -07007435 "bigfloat with positive bignum 257 * 2e3",
Laurence Lundblade887add82020-05-17 05:50:34 -07007436 {(uint8_t[]){0xC5, 0x82, 0x1B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03,
7437 0xC2, 0x42, 0x01, 0x01}, 15},
7438 2056,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007439 EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS),
Laurence Lundblade887add82020-05-17 05:50:34 -07007440 2056,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007441 EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS),
Laurence Lundblade887add82020-05-17 05:50:34 -07007442 2056.0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007443 FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS))
Laurence Lundblade887add82020-05-17 05:50:34 -07007444 },
7445 {
Laurence Lundbladeda095972020-06-06 18:35:33 -07007446 "negative bignum 0xc349010000000000000000 -18446744073709551617",
Laurence Lundblade887add82020-05-17 05:50:34 -07007447 {(uint8_t[]){0xc3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 11},
7448 0,
7449 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
7450 0,
7451 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
7452 -18446744073709551617.0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007453 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)
Laurence Lundblade887add82020-05-17 05:50:34 -07007454 },
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08007455#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
Laurence Lundblade887add82020-05-17 05:50:34 -07007456 {
Laurence Lundblade313b2862020-05-16 01:23:06 -07007457 "Positive bignum 0x01020304 indefinite length string",
7458 {(uint8_t[]){0xC2, 0x5f, 0x42, 0x01, 0x02, 0x41, 0x03, 0x41, 0x04, 0xff}, 10},
7459 0x01020304,
7460 QCBOR_SUCCESS,
7461 0x01020304,
7462 QCBOR_SUCCESS,
7463 16909060.0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007464 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)
Laurence Lundblade313b2862020-05-16 01:23:06 -07007465 },
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08007466#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
Laurence Lundblade313b2862020-05-16 01:23:06 -07007467 {
Laurence Lundblade887add82020-05-17 05:50:34 -07007468 "Decimal Fraction with neg bignum [9223372036854775807, -4759477275222530853137]",
Laurence Lundblade313b2862020-05-16 01:23:06 -07007469 {(uint8_t[]){0xC4, 0x82, 0x1B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
7470 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,}, 23},
7471 0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007472 EXP_AND_MANTISSA_ERROR(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW),
Laurence Lundblade313b2862020-05-16 01:23:06 -07007473 0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007474 EXP_AND_MANTISSA_ERROR(QCBOR_ERR_NUMBER_SIGN_CONVERSION),
Laurence Lundblade313b2862020-05-16 01:23:06 -07007475 -INFINITY,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007476 FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS))
Laurence Lundblade313b2862020-05-16 01:23:06 -07007477 },
7478 {
7479 "big float [9223372036854775806, 9223372036854775806]",
7480 {(uint8_t[]){0xC5, 0x82, 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE,
7481 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE}, 20},
7482 0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007483 EXP_AND_MANTISSA_ERROR(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW),
Laurence Lundblade313b2862020-05-16 01:23:06 -07007484 0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007485 EXP_AND_MANTISSA_ERROR(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW),
Laurence Lundblade313b2862020-05-16 01:23:06 -07007486 INFINITY,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007487 FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS))
Laurence Lundblade313b2862020-05-16 01:23:06 -07007488 },
7489 {
Laurence Lundblade983500d2020-05-14 11:49:34 -07007490 "Big float 3 * 2^^2",
7491 {(uint8_t[]){0xC5, 0x82, 0x02, 0x03}, 4},
7492 12,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007493 EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS),
Laurence Lundblade983500d2020-05-14 11:49:34 -07007494 12,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007495 EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS),
Laurence Lundblade983500d2020-05-14 11:49:34 -07007496 12.0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007497 FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS))
Laurence Lundblade983500d2020-05-14 11:49:34 -07007498 },
Laurence Lundblade983500d2020-05-14 11:49:34 -07007499 {
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007500 "Decimal fraction 3/10",
7501 {(uint8_t[]){0xC4, 0x82, 0x20, 0x03}, 4},
7502 0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007503 EXP_AND_MANTISSA_ERROR(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW),
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007504 0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007505 EXP_AND_MANTISSA_ERROR(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW),
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007506 0.30000000000000004,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007507 FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS))
Laurence Lundbladedfd49fc2020-09-01 14:17:16 -07007508 },
7509 {
Laurence Lundblade11fd78b2020-09-01 22:13:27 -07007510 "extreme pos bignum",
7511 {(uint8_t[]){0xc2, 0x59, 0x01, 0x90,
7512 // 50 rows of 8 is 400 digits.
7513 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7514 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7515 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7516 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7517 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7518 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7519 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7520 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7521 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7522 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7523 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7524 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7525 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7526 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7527 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7528 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7529 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7530 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7531 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7532 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7533 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7534 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7535 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7536 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7537 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7538 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7539 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7540 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7541 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7542 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7543 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7544 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7545 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7546 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7547 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7548 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7549 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7550 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7551 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7552 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7553 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7554 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7555 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7556 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7557 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7558 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7559 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7560 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7561 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
gtravisb787e82a2023-11-30 18:38:21 -08007562 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0},
Laurence Lundblade11fd78b2020-09-01 22:13:27 -07007563 404},
7564 0,
7565 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
7566 0,
7567 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
Laurence Lundblade11fd78b2020-09-01 22:13:27 -07007568 INFINITY,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007569 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS),
Laurence Lundblade11fd78b2020-09-01 22:13:27 -07007570 },
7571
7572 {
7573 "extreme neg bignum",
7574 {(uint8_t[]){0xc3, 0x59, 0x01, 0x90,
7575 // 50 rows of 8 is 400 digits.
7576 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7577 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7578 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7579 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7580 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7581 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7582 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7583 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7584 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7585 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7586 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7587 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7588 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7589 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7590 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7591 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7592 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7593 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7594 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7595 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7596 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7597 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7598 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7599 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7600 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7601 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7602 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7603 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7604 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7605 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7606 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7607 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7608 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7609 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7610 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7611 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7612 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7613 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7614 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7615 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7616 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7617 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7618 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7619 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7620 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7621 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7622 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7623 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7624 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
gtravisb787e82a2023-11-30 18:38:21 -08007625 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0},
Laurence Lundblade11fd78b2020-09-01 22:13:27 -07007626 404},
7627 0,
7628 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
7629 0,
7630 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
Laurence Lundblade11fd78b2020-09-01 22:13:27 -07007631 -INFINITY,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007632 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)
Laurence Lundblade11fd78b2020-09-01 22:13:27 -07007633 },
Laurence Lundblade51722fd2020-09-02 13:01:33 -07007634
7635 {
7636 "big float underflow [9223372036854775806, -9223372036854775806]",
7637 {(uint8_t[]){
7638 0xC5, 0x82,
7639 0x3B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE,
7640 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE}, 20},
Laurence Lundblade51722fd2020-09-02 13:01:33 -07007641 0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007642 EXP_AND_MANTISSA_ERROR(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW),
Laurence Lundblade51722fd2020-09-02 13:01:33 -07007643 0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007644 EXP_AND_MANTISSA_ERROR(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW),
Laurence Lundblade51722fd2020-09-02 13:01:33 -07007645 0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007646 FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS))
Laurence Lundblade51722fd2020-09-02 13:01:33 -07007647 },
7648
7649 {
7650 "bigfloat that evaluates to -INFINITY",
7651 {(uint8_t[]){
7652 0xC5, 0x82,
7653 0x1B, 0x70, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03,
7654 0xC3, 0x42, 0x01, 0x01}, 15},
Laurence Lundblade51722fd2020-09-02 13:01:33 -07007655 0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007656 EXP_AND_MANTISSA_ERROR(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW),
Laurence Lundblade51722fd2020-09-02 13:01:33 -07007657 0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007658 EXP_AND_MANTISSA_ERROR(QCBOR_ERR_NUMBER_SIGN_CONVERSION),
Laurence Lundblade51722fd2020-09-02 13:01:33 -07007659 -INFINITY,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007660 FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS))
Laurence Lundblade51722fd2020-09-02 13:01:33 -07007661 },
Laurence Lundblade37286c02022-09-03 10:05:02 -07007662 {
7663 "Positive bignum 0xffff",
7664 {(uint8_t[]){0xC2, 0x42, 0xff, 0xff}, 4},
7665 65536-1,
7666 QCBOR_SUCCESS,
7667 0xffff,
7668 QCBOR_SUCCESS,
7669 65535.0,
7670 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)
7671 },
7672#endif /* QCBOR_DISABLE_TAGS */
7673 {
7674 "Positive integer 18446744073709551615",
7675 {(uint8_t[]){0x1b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, 9},
7676 0,
7677 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
7678 18446744073709551615ULL,
7679 QCBOR_SUCCESS,
7680 18446744073709551615.0,
7681 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)
7682 },
7683
7684 {
7685 "Postive integer 0",
7686 {(uint8_t[]){0x0}, 1},
7687 0LL,
7688 QCBOR_SUCCESS,
7689 0ULL,
7690 QCBOR_SUCCESS,
7691 0.0,
7692 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)
7693 },
7694 {
7695 "Negative integer -18446744073709551616",
7696 {(uint8_t[]){0x3b, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }, 9},
7697 -9223372036854775807-1, // INT64_MIN
7698 QCBOR_SUCCESS,
7699 0ULL,
7700 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
7701 -9223372036854775808.0,
7702 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)
7703 },
7704 {
7705 "Double Floating point value 100.3",
7706 {(uint8_t[]){0xfb, 0x40, 0x59, 0x13, 0x33, 0x33, 0x33, 0x33, 0x33}, 9},
7707 100L,
7708 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS),
7709 100ULL,
7710 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS),
7711 100.3,
7712 FLOAT_ERR_CODE_NO_FLOAT(QCBOR_SUCCESS),
7713 },
7714 {
7715 "Floating point value NaN 0xfa7fc00000",
7716 {(uint8_t[]){0xfa, 0x7f, 0xc0, 0x00, 0x00}, 5},
7717 0,
7718 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_FLOAT_EXCEPTION),
7719 0,
7720 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_FLOAT_EXCEPTION),
7721 NAN,
7722 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS),
7723 },
7724 {
7725 "half-precision Floating point value -4",
7726 {(uint8_t[]){0xf9, 0xc4, 0x00}, 3},
7727 // Normal case with all enabled.
7728 -4,
7729 FLOAT_ERR_CODE_NO_HALF_PREC_NO_FLOAT_HW(QCBOR_SUCCESS),
7730 0,
7731 FLOAT_ERR_CODE_NO_HALF_PREC_NO_FLOAT_HW(QCBOR_ERR_NUMBER_SIGN_CONVERSION),
7732 -4.0,
7733 FLOAT_ERR_CODE_NO_HALF_PREC(QCBOR_SUCCESS)
7734 },
7735 {
7736 "+inifinity single precision",
7737 {(uint8_t[]){0xfa, 0x7f, 0x80, 0x00, 0x00}, 5},
7738 0,
7739 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_FLOAT_EXCEPTION),
7740 0,
7741 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW),
7742 INFINITY,
7743 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)
7744 },
7745
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007746};
Laurence Lundblade9c905e82020-04-25 11:31:38 -07007747
7748
7749
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08007750
7751static int32_t SetUpDecoder(QCBORDecodeContext *DCtx, UsefulBufC CBOR, UsefulBuf Pool)
7752{
7753 QCBORDecode_Init(DCtx, CBOR, QCBOR_DECODE_MODE_NORMAL);
7754#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
7755 if(QCBORDecode_SetMemPool(DCtx, Pool, 0)) {
7756 return 1;
7757 }
7758#else /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
7759 (void)Pool;
7760#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
7761 return 0;
7762}
7763
7764
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03007765int32_t IntegerConvertTest(void)
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007766{
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08007767 const int nNumTests = C_ARRAY_COUNT(NumberConversions,
7768 struct NumberConversion);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007769
Laurence Lundblade9ab5abb2020-05-20 12:10:45 -07007770 for(int nIndex = 0; nIndex < nNumTests; nIndex++) {
7771 const struct NumberConversion *pF = &NumberConversions[nIndex];
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07007772
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007773 // Set up the decoding context including a memory pool so that
7774 // indefinite length items can be checked
7775 QCBORDecodeContext DCtx;
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007776 UsefulBuf_MAKE_STACK_UB(Pool, 100);
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07007777
7778 /* ----- test conversion to int64_t ------ */
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08007779 if(SetUpDecoder(&DCtx, pF->CBOR, Pool)) {
7780 return (int32_t)(3333+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007781 }
7782
7783 int64_t nInt;
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007784 QCBORDecode_GetInt64ConvertAll(&DCtx, 0xffff, &nInt);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07007785 if(QCBORDecode_GetError(&DCtx) != pF->uErrorInt64) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07007786 return (int32_t)(2000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007787 }
7788 if(pF->uErrorInt64 == QCBOR_SUCCESS && pF->nConvertedToInt64 != nInt) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07007789 return (int32_t)(3000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007790 }
7791
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07007792 /* ----- test conversion to uint64_t ------ */
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08007793 if(SetUpDecoder(&DCtx, pF->CBOR, Pool)) {
7794 return (int32_t)(3333+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007795 }
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08007796
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007797 uint64_t uInt;
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007798 QCBORDecode_GetUInt64ConvertAll(&DCtx, 0xffff, &uInt);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07007799 if(QCBORDecode_GetError(&DCtx) != pF->uErrorUint64) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07007800 return (int32_t)(4000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007801 }
7802 if(pF->uErrorUint64 == QCBOR_SUCCESS && pF->uConvertToUInt64 != uInt) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07007803 return (int32_t)(5000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007804 }
7805
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07007806 /* ----- test conversion to double ------ */
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08007807 if(SetUpDecoder(&DCtx, pF->CBOR, Pool)) {
7808 return (int32_t)(3333+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007809 }
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007810
7811#ifndef USEFULBUF_DISABLE_ALL_FLOAT
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007812 double d;
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007813 QCBORDecode_GetDoubleConvertAll(&DCtx, 0xffff, &d);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07007814 if(QCBORDecode_GetError(&DCtx) != pF->uErrorDouble) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07007815 return (int32_t)(6000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007816 }
7817 if(pF->uErrorDouble == QCBOR_SUCCESS) {
7818 if(isnan(pF->dConvertToDouble)) {
Laurence Lundblade983500d2020-05-14 11:49:34 -07007819 // NaN's can't be compared for equality. A NaN is
7820 // never equal to anything including another NaN
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007821 if(!isnan(d)) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07007822 return (int32_t)(7000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007823 }
7824 } else {
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007825 if(pF->dConvertToDouble != d) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07007826 return (int32_t)(8000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007827 }
7828 }
7829 }
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007830#endif /* USEFULBUF_DISABLE_ALL_FLOAT */
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007831 }
7832
7833 return 0;
7834}
7835
Laurence Lundbladea8758502022-05-15 17:57:46 -07007836#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
7837
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03007838int32_t CBORTestIssue134(void)
David Navarro9123e5b2022-03-28 16:04:03 +02007839{
7840 QCBORDecodeContext DCtx;
7841 QCBORItem Item;
7842 QCBORError uCBORError;
7843 const uint8_t spTestIssue134[] = { 0x5F, 0x40, 0xFF };
Laurence Lundblade9c905e82020-04-25 11:31:38 -07007844
David Navarro9123e5b2022-03-28 16:04:03 +02007845 QCBORDecode_Init(&DCtx,
7846 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTestIssue134),
7847 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade97c61bf2020-05-02 11:24:06 -07007848
David Navarro9123e5b2022-03-28 16:04:03 +02007849 UsefulBuf_MAKE_STACK_UB(StringBuf, 200);
7850 QCBORDecode_SetMemPool(&DCtx, StringBuf, false);
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03007851
David Navarro9123e5b2022-03-28 16:04:03 +02007852 do {
7853 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
7854 } while (QCBOR_SUCCESS == uCBORError);
7855
7856 uCBORError = QCBORDecode_Finish(&DCtx);
7857
Laurence Lundblade11ea3612022-07-01 13:26:23 -07007858 return (int32_t)uCBORError;
David Navarro9123e5b2022-03-28 16:04:03 +02007859}
Laurence Lundblade97c61bf2020-05-02 11:24:06 -07007860
Laurence Lundbladea8758502022-05-15 17:57:46 -07007861#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
7862
Laurence Lundblade37286c02022-09-03 10:05:02 -07007863
7864
7865static const uint8_t spSequenceTestInput[] = {
7866 /* 1. The valid date string "1985-04-12" */
7867 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string
7868
7869 /* 2. */
7870 0x00,
7871
7872 /* 3. A valid epoch date, 1400000000; Tue, 13 May 2014 16:53:20 GMT */
7873 0x1a, 0x53, 0x72, 0x4E, 0x00,
7874
7875 /* 4. */
7876 0x62, 'h', 'i',
7877};
7878
7879
Laurence Lundbladee3553422020-05-02 11:11:17 -07007880int32_t CBORSequenceDecodeTests(void)
7881{
7882 QCBORDecodeContext DCtx;
Laurence Lundblade87495732021-02-26 10:05:55 -07007883 QCBORItem Item;
7884 QCBORError uCBORError;
7885 size_t uConsumed;
Laurence Lundbladee3553422020-05-02 11:11:17 -07007886
7887 // --- Test a sequence with extra bytes ---
Laurence Lundblade9b334962020-08-27 10:55:53 -07007888
Laurence Lundbladee3553422020-05-02 11:11:17 -07007889 QCBORDecode_Init(&DCtx,
Laurence Lundblade37286c02022-09-03 10:05:02 -07007890 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSequenceTestInput),
Laurence Lundbladee3553422020-05-02 11:11:17 -07007891 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade9b334962020-08-27 10:55:53 -07007892
Laurence Lundblade37286c02022-09-03 10:05:02 -07007893 // Get 1.
Laurence Lundbladee3553422020-05-02 11:11:17 -07007894 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
7895 if(uCBORError != QCBOR_SUCCESS) {
7896 return 1;
7897 }
Laurence Lundblade37286c02022-09-03 10:05:02 -07007898 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ) {
Laurence Lundbladee3553422020-05-02 11:11:17 -07007899 return 2;
7900 }
Laurence Lundbladec7114722020-08-13 05:11:40 -07007901
Laurence Lundblade87495732021-02-26 10:05:55 -07007902 uCBORError = QCBORDecode_PartialFinish(&DCtx, &uConsumed);
7903 if(uCBORError != QCBOR_ERR_EXTRA_BYTES ||
Laurence Lundblade37286c02022-09-03 10:05:02 -07007904 uConsumed != 11) {
7905 return 102;
7906 }
7907
7908 // Get 2.
7909 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
7910 if(uCBORError != QCBOR_SUCCESS) {
7911 return 66;
7912 }
7913
7914 uCBORError = QCBORDecode_PartialFinish(&DCtx, &uConsumed);
7915 if(uCBORError != QCBOR_ERR_EXTRA_BYTES ||
Laurence Lundblade87495732021-02-26 10:05:55 -07007916 uConsumed != 12) {
7917 return 102;
7918 }
7919
Laurence Lundblade37286c02022-09-03 10:05:02 -07007920 // Get 3.
Laurence Lundbladec7114722020-08-13 05:11:40 -07007921 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundbladee3553422020-05-02 11:11:17 -07007922 if(uCBORError != QCBOR_SUCCESS) {
7923 return 2;
7924 }
Laurence Lundblade37286c02022-09-03 10:05:02 -07007925 if(Item.uDataType != QCBOR_TYPE_INT64) {
Laurence Lundbladee3553422020-05-02 11:11:17 -07007926 return 3;
7927 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07007928
Laurence Lundbladee3553422020-05-02 11:11:17 -07007929 // A sequence can have stuff at the end that may
7930 // or may not be valid CBOR. The protocol decoder knows
7931 // when to stop by definition of the protocol, not
7932 // when the top-level map or array is ended.
7933 // Finish still has to be called to know that
7934 // maps and arrays (if there were any) were closed
7935 // off correctly. When called like this it
7936 // must return the error QCBOR_ERR_EXTRA_BYTES.
7937 uCBORError = QCBORDecode_Finish(&DCtx);
7938 if(uCBORError != QCBOR_ERR_EXTRA_BYTES) {
7939 return 4;
7940 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07007941
Laurence Lundbladee3553422020-05-02 11:11:17 -07007942 // --- Test an empty input ----
7943 uint8_t empty[1];
7944 UsefulBufC Empty = {empty, 0};
7945 QCBORDecode_Init(&DCtx,
7946 Empty,
7947 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade9b334962020-08-27 10:55:53 -07007948
Laurence Lundbladee3553422020-05-02 11:11:17 -07007949 uCBORError = QCBORDecode_Finish(&DCtx);
7950 if(uCBORError != QCBOR_SUCCESS) {
7951 return 5;
7952 }
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07007953
7954
Laurence Lundbladee3553422020-05-02 11:11:17 -07007955 // --- Sequence with unclosed indefinite length array ---
7956 static const uint8_t xx[] = {0x01, 0x9f, 0x02};
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07007957
Laurence Lundbladee3553422020-05-02 11:11:17 -07007958 QCBORDecode_Init(&DCtx,
7959 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(xx),
7960 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07007961
Laurence Lundbladee3553422020-05-02 11:11:17 -07007962 // Get the first item
7963 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
7964 if(uCBORError != QCBOR_SUCCESS) {
7965 return 7;
7966 }
7967 if(Item.uDataType != QCBOR_TYPE_INT64) {
7968 return 8;
7969 }
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07007970
Laurence Lundbladee3553422020-05-02 11:11:17 -07007971 // Get a second item
7972 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08007973#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundbladee3553422020-05-02 11:11:17 -07007974 if(uCBORError != QCBOR_SUCCESS) {
7975 return 9;
7976 }
7977 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
7978 return 10;
7979 }
7980
7981 // Try to finish before consuming all bytes to confirm
7982 // that the still-open error is returned.
7983 uCBORError = QCBORDecode_Finish(&DCtx);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07007984 if(uCBORError != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
Laurence Lundbladee3553422020-05-02 11:11:17 -07007985 return 11;
7986 }
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08007987#else /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
7988 if(uCBORError != QCBOR_ERR_INDEF_LEN_ARRAYS_DISABLED) {
7989 return 20;
7990 }
7991#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
Laurence Lundbladee3553422020-05-02 11:11:17 -07007992
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07007993
Laurence Lundbladee3553422020-05-02 11:11:17 -07007994 // --- Sequence with a closed indefinite length array ---
7995 static const uint8_t yy[] = {0x01, 0x9f, 0xff};
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07007996
Laurence Lundbladee3553422020-05-02 11:11:17 -07007997 QCBORDecode_Init(&DCtx,
7998 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(yy),
7999 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07008000
Laurence Lundbladee3553422020-05-02 11:11:17 -07008001 // Get the first item
8002 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
8003 if(uCBORError != QCBOR_SUCCESS) {
8004 return 12;
8005 }
8006 if(Item.uDataType != QCBOR_TYPE_INT64) {
8007 return 13;
8008 }
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07008009
Laurence Lundbladee3553422020-05-02 11:11:17 -07008010 // Get a second item
8011 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08008012#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
8013
Laurence Lundbladee3553422020-05-02 11:11:17 -07008014 if(uCBORError != QCBOR_SUCCESS) {
8015 return 14;
8016 }
8017 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
8018 return 15;
8019 }
8020
8021 // Try to finish before consuming all bytes to confirm
8022 // that the still-open error is returned.
8023 uCBORError = QCBORDecode_Finish(&DCtx);
8024 if(uCBORError != QCBOR_SUCCESS) {
8025 return 16;
8026 }
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08008027#else /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
8028 if(uCBORError != QCBOR_ERR_INDEF_LEN_ARRAYS_DISABLED) {
8029 return 20;
8030 }
8031#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
Laurence Lundbladee3553422020-05-02 11:11:17 -07008032
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07008033
Laurence Lundbladee3553422020-05-02 11:11:17 -07008034 return 0;
8035}
8036
Laurence Lundbladee15326f2020-06-15 15:50:23 -07008037
Laurence Lundblade70ecead2020-06-15 19:40:06 -07008038
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03008039int32_t IntToTests(void)
Laurence Lundbladee15326f2020-06-15 15:50:23 -07008040{
8041 int nErrCode;
8042 int32_t n32;
8043 int16_t n16;
8044 int8_t n8;
8045 uint32_t u32;
8046 uint16_t u16;
8047 uint8_t u8;
8048 uint64_t u64;
8049
8050 nErrCode = QCBOR_Int64ToInt32(1, &n32);
8051 if(nErrCode == -1 || n32 != 1) {
8052 return 1;
8053 }
8054
8055 nErrCode = QCBOR_Int64ToInt32((int64_t)INT32_MAX, &n32);
8056 if(nErrCode == -1 || n32 != INT32_MAX) {
8057 return 2;
8058 }
8059
8060 nErrCode = QCBOR_Int64ToInt32((int64_t)INT32_MIN, &n32);
8061 if(nErrCode == -1 || n32 != INT32_MIN) {
8062 return 3;
8063 }
8064
8065 nErrCode = QCBOR_Int64ToInt32(((int64_t)INT32_MAX)+1, &n32);
8066 if(nErrCode != -1) {
8067 return 4;
8068 }
8069
8070 nErrCode = QCBOR_Int64ToInt32(((int64_t)INT32_MIN)-1, &n32);
8071 if(nErrCode != -1) {
8072 return 5;
8073 }
8074
8075
8076 nErrCode = QCBOR_Int64ToInt16((int64_t)INT16_MAX, &n16);
8077 if(nErrCode == -1 || n16 != INT16_MAX) {
8078 return 6;
8079 }
8080
8081 nErrCode = QCBOR_Int64ToInt16((int64_t)INT16_MIN, &n16);
8082 if(nErrCode == -1 || n16 != INT16_MIN) {
8083 return 7;
8084 }
8085
8086 nErrCode = QCBOR_Int64ToInt16(1, &n16);
8087 if(nErrCode == -1 || n16 != 1) {
8088 return 8;
8089 }
8090
8091 nErrCode = QCBOR_Int64ToInt16(((int64_t)INT16_MAX)+1, &n16);
8092 if(nErrCode != -1) {
8093 return 9;
8094 }
8095
8096 nErrCode = QCBOR_Int64ToInt16(((int64_t)INT16_MIN)-1, &n16);
8097 if(nErrCode != -1) {
8098 return 10;
8099 }
8100
8101
8102 nErrCode = QCBOR_Int64ToInt8(1, &n8);
8103 if(nErrCode == -1 || n8 != 1) {
8104 return 11;
8105 }
8106
8107 nErrCode = QCBOR_Int64ToInt8((int64_t)INT8_MAX, &n8);
8108 if(nErrCode == -1 || n8 != INT8_MAX) {
8109 return 12;
8110 }
8111
8112 nErrCode = QCBOR_Int64ToInt8((int64_t)INT8_MIN, &n8);
8113 if(nErrCode == -1 || n8 != INT8_MIN) {
8114 return 13;
8115 }
8116
8117 nErrCode = QCBOR_Int64ToInt8(((int64_t)INT8_MAX)+1, &n8);
8118 if(nErrCode != -1) {
8119 return 14;
8120 }
8121
8122 nErrCode = QCBOR_Int64ToInt8(((int64_t)INT8_MIN)-1, &n8);
8123 if(nErrCode != -1) {
8124 return 15;
8125 }
8126
8127
8128 nErrCode = QCBOR_Int64ToUInt32(1, &u32);
8129 if(nErrCode == -1 || u32 != 1) {
8130 return 16;
8131 }
8132
8133 nErrCode = QCBOR_Int64ToUInt32((int64_t)UINT32_MAX, &u32);
8134 if(nErrCode == -1 || u32 != UINT32_MAX) {
8135 return 17;
8136 }
8137
8138 nErrCode = QCBOR_Int64ToUInt32((int64_t)0, &u32);
8139 if(nErrCode == -1 || u32 != 0) {
8140 return 18;
8141 }
8142
8143 nErrCode = QCBOR_Int64ToUInt32(((int64_t)UINT32_MAX)+1, &u32);
8144 if(nErrCode != -1) {
8145 return 19;
8146 }
8147
8148 nErrCode = QCBOR_Int64ToUInt32((int64_t)-1, &u32);
8149 if(nErrCode != -1) {
8150 return 20;
8151 }
8152
8153
Sebastian Burck5a0ab732024-06-29 06:31:20 +02008154 nErrCode = QCBOR_Int64ToUInt16((int64_t)UINT16_MAX, &u16);
Laurence Lundbladee15326f2020-06-15 15:50:23 -07008155 if(nErrCode == -1 || u16 != UINT16_MAX) {
8156 return 21;
8157 }
8158
Sebastian Burck5a0ab732024-06-29 06:31:20 +02008159 nErrCode = QCBOR_Int64ToUInt16((int64_t)0, &u16);
Laurence Lundbladee15326f2020-06-15 15:50:23 -07008160 if(nErrCode == -1 || u16 != 0) {
8161 return 22;
8162 }
8163
Sebastian Burck5a0ab732024-06-29 06:31:20 +02008164 nErrCode = QCBOR_Int64ToUInt16(1, &u16);
Laurence Lundbladee15326f2020-06-15 15:50:23 -07008165 if(nErrCode == -1 || u16 != 1) {
8166 return 23;
8167 }
8168
Sebastian Burck5a0ab732024-06-29 06:31:20 +02008169 nErrCode = QCBOR_Int64ToUInt16(((int64_t)UINT16_MAX)+1, &u16);
Laurence Lundbladee15326f2020-06-15 15:50:23 -07008170 if(nErrCode != -1) {
8171 return 24;
8172 }
8173
Sebastian Burck5a0ab732024-06-29 06:31:20 +02008174 nErrCode = QCBOR_Int64ToUInt16((int64_t)-1, &u16);
Laurence Lundbladee15326f2020-06-15 15:50:23 -07008175 if(nErrCode != -1) {
8176 return 25;
8177 }
8178
8179
8180 nErrCode = QCBOR_Int64ToUInt8((int64_t)UINT8_MAX, &u8);
8181 if(nErrCode == -1 || u8 != UINT8_MAX) {
8182 return 26;
8183 }
8184
8185 nErrCode = QCBOR_Int64ToUInt8((int64_t)0, &u8);
8186 if(nErrCode == -1 || u8 != 0) {
8187 return 27;
8188 }
8189
8190 nErrCode = QCBOR_Int64ToUInt8(1, &u8);
8191 if(nErrCode == -1 || u8 != 1) {
8192 return 28;
8193 }
8194
8195 nErrCode = QCBOR_Int64ToUInt8(((int64_t)UINT16_MAX)+1, &u8);
8196 if(nErrCode != -1) {
8197 return 29;
8198 }
8199
8200 nErrCode = QCBOR_Int64ToUInt8((int64_t)-1, &u8);
8201 if(nErrCode != -1) {
8202 return 30;
8203 }
8204
8205
8206 nErrCode = QCBOR_Int64ToUInt64(1, &u64);
8207 if(nErrCode == -1 || u64 != 1) {
8208 return 31;
8209 }
8210
8211 nErrCode = QCBOR_Int64ToUInt64(INT64_MAX, &u64);
8212 if(nErrCode == -1 || u64 != INT64_MAX) {
8213 return 32;
8214 }
8215
8216 nErrCode = QCBOR_Int64ToUInt64((int64_t)0, &u64);
8217 if(nErrCode == -1 || u64 != 0) {
8218 return 33;
8219 }
8220
8221 nErrCode = QCBOR_Int64ToUInt64((int64_t)-1, &u64);
8222 if(nErrCode != -1) {
8223 return 34;
8224 }
8225
8226 return 0;
8227}
8228
Laurence Lundblade0750fc42020-06-20 21:02:34 -07008229
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07008230
8231
Laurence Lundblade0750fc42020-06-20 21:02:34 -07008232/*
8233A sequence with
8234 A wrapping bstr
8235 containing a map
8236 1
8237 2
8238 A wrapping bstr
8239 containing an array
8240 3
8241 wrapping bstr
8242 4
8243 5
8244 6
8245 array
8246 7
8247 8
Laurence Lundblade0750fc42020-06-20 21:02:34 -07008248 */
8249
Laurence Lundblade55013642020-09-23 05:39:22 -07008250static UsefulBufC EncodeBstrWrapTestData(UsefulBuf OutputBuffer)
Laurence Lundblade0750fc42020-06-20 21:02:34 -07008251{
Laurence Lundblade55013642020-09-23 05:39:22 -07008252 UsefulBufC Encoded;
Laurence Lundblade0750fc42020-06-20 21:02:34 -07008253 QCBOREncodeContext EC;
Laurence Lundblade55013642020-09-23 05:39:22 -07008254 QCBORError uErr;
Laurence Lundblade0750fc42020-06-20 21:02:34 -07008255
Laurence Lundblade55013642020-09-23 05:39:22 -07008256 QCBOREncode_Init(&EC, OutputBuffer);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07008257
8258 QCBOREncode_BstrWrap(&EC);
8259 QCBOREncode_OpenMap(&EC);
8260 QCBOREncode_AddInt64ToMapN(&EC, 100, 1);
8261 QCBOREncode_AddInt64ToMapN(&EC, 200, 2);
8262 QCBOREncode_CloseMap(&EC);
8263 QCBOREncode_BstrWrap(&EC);
8264 QCBOREncode_OpenArray(&EC);
8265 QCBOREncode_AddInt64(&EC, 3);
8266 QCBOREncode_BstrWrap(&EC);
8267 QCBOREncode_AddInt64(&EC, 4);
8268 QCBOREncode_CloseBstrWrap(&EC, NULL);
8269 QCBOREncode_AddInt64(&EC, 5);
8270 QCBOREncode_CloseArray(&EC);
8271 QCBOREncode_CloseBstrWrap(&EC, NULL);
8272 QCBOREncode_AddInt64(&EC, 6);
8273 QCBOREncode_CloseBstrWrap(&EC, NULL);
8274 QCBOREncode_OpenArray(&EC);
8275 QCBOREncode_AddInt64(&EC, 7);
8276 QCBOREncode_AddInt64(&EC, 8);
8277 QCBOREncode_CloseArray(&EC);
8278
8279 uErr = QCBOREncode_Finish(&EC, &Encoded);
Laurence Lundblade40a04322020-06-27 22:52:52 -07008280 if(uErr) {
8281 Encoded = NULLUsefulBufC;
8282 }
Laurence Lundblade0750fc42020-06-20 21:02:34 -07008283
8284 return Encoded;
8285}
8286
Laurence Lundbladecc7da412020-12-27 00:09:07 -08008287/* h'FF' */
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08008288static const uint8_t spBreakInByteString[] = {
8289 0x41, 0xff
8290};
8291
Laurence Lundblade0750fc42020-06-20 21:02:34 -07008292
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03008293int32_t EnterBstrTest(void)
Laurence Lundblade0750fc42020-06-20 21:02:34 -07008294{
Laurence Lundblade8510f8c2020-12-01 11:31:16 -08008295 UsefulBuf_MAKE_STACK_UB(OutputBuffer, 100);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07008296
8297 QCBORDecodeContext DC;
8298
Laurence Lundblade55013642020-09-23 05:39:22 -07008299 QCBORDecode_Init(&DC, EncodeBstrWrapTestData(OutputBuffer), 0);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07008300
Laurence Lundblade55013642020-09-23 05:39:22 -07008301 int64_t n1, n2, n3, n4, n5, n6, n7, n8;
Laurence Lundblade0750fc42020-06-20 21:02:34 -07008302
8303
Laurence Lundblade9b334962020-08-27 10:55:53 -07008304 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07008305 QCBORDecode_EnterMap(&DC, NULL);
Laurence Lundblade55013642020-09-23 05:39:22 -07008306 QCBORDecode_GetInt64InMapN(&DC, 100, &n1);
8307 QCBORDecode_GetInt64InMapN(&DC, 200, &n2);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07008308 QCBORDecode_ExitMap(&DC);
Laurence Lundblade9b334962020-08-27 10:55:53 -07008309 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07008310 QCBORDecode_EnterArray(&DC, NULL);
Laurence Lundblade55013642020-09-23 05:39:22 -07008311 QCBORDecode_GetInt64(&DC, &n3);
Laurence Lundblade9b334962020-08-27 10:55:53 -07008312 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
Laurence Lundblade55013642020-09-23 05:39:22 -07008313 QCBORDecode_GetInt64(&DC, &n4);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07008314 QCBORDecode_ExitBstrWrapped(&DC);
Laurence Lundblade55013642020-09-23 05:39:22 -07008315 QCBORDecode_GetInt64(&DC, &n5);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07008316 QCBORDecode_ExitArray(&DC);
8317 QCBORDecode_ExitBstrWrapped(&DC);
Laurence Lundblade55013642020-09-23 05:39:22 -07008318 QCBORDecode_GetInt64(&DC, &n6);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07008319 QCBORDecode_ExitBstrWrapped(&DC);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07008320 QCBORDecode_EnterArray(&DC, NULL);
Laurence Lundblade55013642020-09-23 05:39:22 -07008321 QCBORDecode_GetInt64(&DC, &n7);
8322 QCBORDecode_GetInt64(&DC, &n8);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07008323 QCBORDecode_ExitArray(&DC);
8324
8325 QCBORError uErr = QCBORDecode_Finish(&DC);
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08008326 if(uErr) {
8327 return (int32_t)uErr;
8328 }
Laurence Lundblade0750fc42020-06-20 21:02:34 -07008329
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08008330
8331 /* Enter and exit byte string wrapped CBOR that is bad. It has just a break.
8332 * Successful because no items are fetched from byte string.
8333 */
8334 QCBORDecode_Init(&DC,
8335 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBreakInByteString),
8336 0);
8337 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
8338 uErr = QCBORDecode_GetError(&DC);
8339 if(uErr) {
8340 return 100 + (int32_t)uErr;
8341 }
8342
8343 QCBORDecode_ExitBstrWrapped(&DC);
8344 uErr = QCBORDecode_GetError(&DC);
8345 if(uErr) {
8346 return 200 + (int32_t)uErr;
8347 }
8348
8349 /* Try to get item that is a break out of a byte string wrapped CBOR.
8350 * It fails because there should be no break.
8351 */
8352 QCBORDecode_Init(&DC,
8353 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBreakInByteString),
8354 0);
8355 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
8356 QCBORItem Item;
8357 uErr = QCBORDecode_GetNext(&DC, &Item);
8358 if(uErr != QCBOR_ERR_BAD_BREAK) {
8359 return 300 + (int32_t)uErr;
8360 }
8361
8362 return 0;
Laurence Lundblade0750fc42020-06-20 21:02:34 -07008363}
Laurence Lundblade37f46e52020-08-04 03:32:14 -07008364
8365
8366
8367
8368static const uint8_t spTaggedTypes[] = {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008369 0xb2,
Laurence Lundblade37f46e52020-08-04 03:32:14 -07008370
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07008371 // Date string
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008372 0x00,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07008373 0xc0, 0x74, 0x32, 0x30, 0x30, 0x33, 0x2D, 0x31, 0x32, 0x2D,
8374 0x31, 0x33, 0x54, 0x31, 0x38, 0x3A, 0x33, 0x30, 0x3A, 0x30,
8375 0x32, 0x5A,
Laurence Lundblade37f46e52020-08-04 03:32:14 -07008376
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008377 0x01,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07008378 0x74, 0x32, 0x30, 0x30, 0x33, 0x2D, 0x31, 0x32, 0x2D, 0x31,
8379 0x33, 0x54, 0x31, 0x38, 0x3A, 0x33, 0x30, 0x3A, 0x30, 0x32,
8380 0x5A,
Laurence Lundblade37f46e52020-08-04 03:32:14 -07008381
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07008382 // Bignum
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008383 10,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07008384 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
8385 0x09, 0x10,
Laurence Lundblade37f46e52020-08-04 03:32:14 -07008386
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008387 11,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07008388 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
8389 0x09, 0x10,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008390
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07008391 // URL
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008392 20,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07008393 0xd8, 0x20, 0x6f, 0x68, 0x74, 0x74, 0x70, 0x3A, 0x2F, 0x2F,
8394 0x63, 0x62, 0x6F, 0x72, 0x2E, 0x6D, 0x65, 0x2F,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008395
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07008396 21,
8397 0x6f, 0x68, 0x74, 0x74, 0x70, 0x3A, 0x2F, 0x2F, 0x63, 0x62,
8398 0x6F, 0x72, 0x2E, 0x6D, 0x65, 0x2F,
8399
8400 // B64
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008401 0x18, 0x1e,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07008402 0xd8, 0x22, 0x6c, 0x63, 0x47, 0x78, 0x6C, 0x59, 0x58, 0x4E,
8403 0x31, 0x63, 0x6D, 0x55, 0x75,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008404
8405 0x18, 0x1f,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07008406 0x6c, 0x63, 0x47, 0x78, 0x6C, 0x59, 0x58, 0x4E, 0x31, 0x63,
8407 0x6D, 0x55, 0x75,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008408
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07008409 // B64URL
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008410 0x18, 0x28,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07008411 0xd8, 0x21, 0x6c, 0x63, 0x47, 0x78, 0x6C, 0x59, 0x58, 0x4E,
8412 0x31, 0x63, 0x6D, 0x55, 0x75,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008413
8414 0x18, 0x29,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07008415 0x6c, 0x63, 0x47, 0x78, 0x6C, 0x59, 0x58, 0x4E, 0x31, 0x63,
8416 0x6D, 0x55, 0x75,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008417
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07008418 // Regex
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008419 0x18, 0x32,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07008420 0xd8, 0x23, 0x68, 0x31, 0x30, 0x30, 0x5C, 0x73, 0x2A, 0x6D,
8421 0x6B,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008422
8423 0x18, 0x33,
8424 0x68, 0x31, 0x30, 0x30, 0x5C, 0x73, 0x2A, 0x6D, 0x6B,
8425
8426 // MIME
8427 0x18, 0x3c,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07008428 0xd8, 0x24, 0x72, 0x4D, 0x49, 0x4D, 0x45, 0x2D, 0x56, 0x65,
8429 0x72, 0x73, 0x69, 0x6F, 0x6E, 0x3A, 0x20, 0x31, 0x2E, 0x30,
8430 0x0A,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008431
8432 0x18, 0x3d,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07008433 0x72, 0x4D, 0x49, 0x4D, 0x45, 0x2D, 0x56, 0x65, 0x72, 0x73,
8434 0x69, 0x6F, 0x6E, 0x3A, 0x20, 0x31, 0x2E, 0x30, 0x0A,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008435
8436 0x18, 0x3e,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07008437 0xd9, 0x01, 0x01, 0x52, 0x4D, 0x49, 0x4D, 0x45, 0x2D, 0x56,
8438 0x65, 0x72, 0x73, 0x69, 0x6F, 0x6E, 0x3A, 0x20, 0x31, 0x2E,
8439 0x30, 0x0A,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008440
8441 0x18, 0x3f,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07008442 0x52, 0x4D, 0x49, 0x4D, 0x45, 0x2D, 0x56, 0x65, 0x72, 0x73,
8443 0x69, 0x6F, 0x6E, 0x3A, 0x20, 0x31, 0x2E, 0x30, 0x0A,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008444
8445 // UUID
8446 0x18, 0x46,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07008447 0xd8, 0x25, 0x50, 0x53, 0x4D, 0x41, 0x52, 0x54, 0x43, 0x53,
8448 0x4C, 0x54, 0x54, 0x43, 0x46, 0x49, 0x43, 0x41, 0x32,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008449
8450 0x18, 0x47,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07008451 0x50, 0x53, 0x4D, 0x41, 0x52, 0x54, 0x43, 0x53, 0x4C, 0x54,
8452 0x54, 0x43, 0x46, 0x49, 0x43, 0x41, 0x32
Laurence Lundblade37f46e52020-08-04 03:32:14 -07008453};
8454
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03008455int32_t DecodeTaggedTypeTests(void)
Laurence Lundblade37f46e52020-08-04 03:32:14 -07008456{
8457 QCBORDecodeContext DC;
8458 QCBORError uErr;
8459
8460 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTaggedTypes), 0);
8461
8462 UsefulBufC String;
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008463 bool bNeg;
Laurence Lundblade37f46e52020-08-04 03:32:14 -07008464
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07008465 QCBORDecode_EnterMap(&DC, NULL);
Laurence Lundblade9b334962020-08-27 10:55:53 -07008466 QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008467 QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &String);
Laurence Lundblade37f46e52020-08-04 03:32:14 -07008468 if(QCBORDecode_GetError(&DC) != QCBOR_SUCCESS) {
8469 return 1;
8470 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07008471 QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade37f46e52020-08-04 03:32:14 -07008472 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_UNEXPECTED_TYPE) {
8473 return 2;
8474 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07008475 QCBORDecode_GetDateStringInMapN(&DC, 1, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade37f46e52020-08-04 03:32:14 -07008476 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_UNEXPECTED_TYPE) {
8477 return 3;
8478 }
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008479 QCBORDecode_GetDateStringInMapN(&DC, 1, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &String);
Laurence Lundblade9b334962020-08-27 10:55:53 -07008480 QCBORDecode_GetDateStringInMapN(&DC, 1, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade37f46e52020-08-04 03:32:14 -07008481 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
8482 return 4;
8483 }
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008484 QCBORDecode_GetDateStringInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07008485 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008486 return 5;
8487 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07008488
Laurence Lundblade9b334962020-08-27 10:55:53 -07008489 QCBORDecode_GetBignumInMapN(&DC, 10, QCBOR_TAG_REQUIREMENT_TAG, &String, &bNeg);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008490 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
8491 bNeg != false) {
8492 return 10;
8493 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07008494 QCBORDecode_GetBignumInMapN(&DC, 11, QCBOR_TAG_REQUIREMENT_TAG, &String, &bNeg);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008495 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
8496 bNeg != true) {
8497 return 11;
8498 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07008499 QCBORDecode_GetBignumInMapN(&DC, 11, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bNeg);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008500 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_UNEXPECTED_TYPE) {
8501 return 12;
8502 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07008503 QCBORDecode_GetBignumInMapN(&DC, 14, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bNeg);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07008504 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008505 return 13;
8506 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07008507 QCBORDecode_GetBignumInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bNeg);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07008508 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008509 return 14;
8510 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07008511
Laurence Lundblade9b334962020-08-27 10:55:53 -07008512 QCBORDecode_GetURIInMapN(&DC, 20, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008513 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
8514 return 20;
8515 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07008516 QCBORDecode_GetURIInMapN(&DC, 21, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008517 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
8518 return 21;
8519 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07008520 QCBORDecode_GetURIInMapN(&DC, 22, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07008521 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008522 return 22;
8523 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07008524 QCBORDecode_GetURIInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07008525 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008526 return 23;
8527 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07008528
Laurence Lundblade24bd7e12021-01-09 00:05:11 -08008529#ifndef QCBOR_DISABLE_UNCOMMON_TAGS
Laurence Lundblade9b334962020-08-27 10:55:53 -07008530 QCBORDecode_GetB64InMapN(&DC, 30, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008531 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
8532 return 30;
8533 }
Laurence Lundblade24bd7e12021-01-09 00:05:11 -08008534#endif
Laurence Lundblade9b334962020-08-27 10:55:53 -07008535 QCBORDecode_GetB64InMapN(&DC, 31, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008536 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
8537 return 31;
8538 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07008539 QCBORDecode_GetB64InMapN(&DC, 32, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07008540 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008541 return 32;
8542 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07008543 QCBORDecode_GetB64InMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07008544 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008545 return 33;
8546 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07008547
Laurence Lundblade24bd7e12021-01-09 00:05:11 -08008548#ifndef QCBOR_DISABLE_UNCOMMON_TAGS
Laurence Lundblade9b334962020-08-27 10:55:53 -07008549 QCBORDecode_GetB64URLInMapN(&DC, 40, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008550 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
8551 return 40;
8552 }
Laurence Lundblade24bd7e12021-01-09 00:05:11 -08008553#endif
Laurence Lundblade9b334962020-08-27 10:55:53 -07008554 QCBORDecode_GetB64URLInMapN(&DC, 41, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008555 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
8556 return 41;
8557 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07008558 QCBORDecode_GetB64URLInMapN(&DC, 42, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07008559 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008560 return 42;
8561 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07008562 QCBORDecode_GetB64URLInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07008563 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008564 return 43;
8565 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07008566
Laurence Lundblade24bd7e12021-01-09 00:05:11 -08008567#ifndef QCBOR_DISABLE_UNCOMMON_TAGS
Laurence Lundblade9b334962020-08-27 10:55:53 -07008568 QCBORDecode_GetRegexInMapN(&DC, 50, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008569 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
8570 return 50;
8571 }
Laurence Lundblade24bd7e12021-01-09 00:05:11 -08008572#endif
Laurence Lundblade9b334962020-08-27 10:55:53 -07008573 QCBORDecode_GetRegexInMapN(&DC, 51, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008574 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
8575 return 51;
8576 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07008577 QCBORDecode_GetRegexInMapN(&DC, 52, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07008578 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008579 return 52;
8580 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07008581 QCBORDecode_GetRegexInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07008582 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008583 return 53;
8584 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07008585
Laurence Lundblade24bd7e12021-01-09 00:05:11 -08008586#ifndef QCBOR_DISABLE_UNCOMMON_TAGS
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008587 // MIME
8588 bool bIsNot7Bit;
Laurence Lundblade9b334962020-08-27 10:55:53 -07008589 QCBORDecode_GetMIMEMessageInMapN(&DC, 60, QCBOR_TAG_REQUIREMENT_TAG, &String, &bIsNot7Bit);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008590 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
8591 bIsNot7Bit == true) {
8592 return 60;
8593 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07008594 QCBORDecode_GetMIMEMessageInMapN(&DC, 61, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bIsNot7Bit);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008595 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
8596 bIsNot7Bit == true) {
8597 return 61;
8598 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07008599 QCBORDecode_GetMIMEMessageInMapN(&DC, 62, QCBOR_TAG_REQUIREMENT_TAG, &String, &bIsNot7Bit);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008600 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
8601 bIsNot7Bit == false) {
8602 return 62;
8603 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07008604 QCBORDecode_GetMIMEMessageInMapN(&DC, 63, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bIsNot7Bit);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008605 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
8606 bIsNot7Bit == false) {
8607 return 63;
8608 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07008609 QCBORDecode_GetMIMEMessageInMapN(&DC, 64, QCBOR_TAG_REQUIREMENT_TAG, &String, &bNeg);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07008610 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008611 return 64;
8612 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07008613 QCBORDecode_GetMIMEMessageInMapSZ(&DC, "zzz", QCBOR_TAG_REQUIREMENT_TAG, &String, &bNeg);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07008614 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008615 return 65;
8616 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07008617
Laurence Lundblade24bd7e12021-01-09 00:05:11 -08008618
Laurence Lundblade9b334962020-08-27 10:55:53 -07008619 QCBORDecode_GetBinaryUUIDInMapN(&DC, 70, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008620 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
8621 return 70;
8622 }
Laurence Lundblade24bd7e12021-01-09 00:05:11 -08008623#endif /* #ifndef QCBOR_DISABLE_UNCOMMON_TAGS */
8624
Laurence Lundblade9b334962020-08-27 10:55:53 -07008625 QCBORDecode_GetBinaryUUIDInMapN(&DC, 71, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008626 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
8627 return 71;
8628 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07008629 QCBORDecode_GetBinaryUUIDInMapN(&DC, 72, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07008630 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008631 return 72;
8632 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07008633 QCBORDecode_GetBinaryUUIDInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07008634 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008635 return 73;
8636 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07008637
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07008638 // Improvement: add some more error test cases
8639
Laurence Lundblade37f46e52020-08-04 03:32:14 -07008640 QCBORDecode_ExitMap(&DC);
8641
8642 uErr = QCBORDecode_Finish(&DC);
8643 if(uErr != QCBOR_SUCCESS) {
8644 return 100;
8645 }
8646
8647 return 0;
8648}
Laurence Lundbladea4308a82020-10-03 18:08:57 -07008649
8650
8651
8652
8653/*
Laurence Lundbladecc7da412020-12-27 00:09:07 -08008654 [
8655 "aaaaaaaaaa",
8656 {}
8657 ]
Laurence Lundbladea4308a82020-10-03 18:08:57 -07008658 */
8659static const uint8_t spTooLarge1[] = {
8660 0x9f,
8661 0x6a, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61,
8662 0xa0,
8663 0xff
8664};
8665
8666/*
Laurence Lundbladecc7da412020-12-27 00:09:07 -08008667 [
8668 {
8669 0: "aaaaaaaaaa"
8670 }
8671 ]
Laurence Lundbladea4308a82020-10-03 18:08:57 -07008672 */
8673static const uint8_t spTooLarge2[] = {
8674 0x9f,
8675 0xa1,
8676 0x00,
8677 0x6a, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61,
8678 0xff
8679};
8680
8681/*
Laurence Lundbladecc7da412020-12-27 00:09:07 -08008682 h'A1006A61616161616161616161'
Laurence Lundbladea4308a82020-10-03 18:08:57 -07008683
Laurence Lundbladecc7da412020-12-27 00:09:07 -08008684 {
8685 0: "aaaaaaaaaa"
8686 }
Laurence Lundbladea4308a82020-10-03 18:08:57 -07008687 */
8688static const uint8_t spTooLarge3[] = {
8689 0x4d,
8690 0xa1,
8691 0x00,
8692 0x6a, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61,
8693};
8694
8695int32_t TooLargeInputTest(void)
8696{
8697 QCBORDecodeContext DC;
8698 QCBORError uErr;
8699 UsefulBufC String;
8700
8701 // These tests require a build with QCBOR_MAX_DECODE_INPUT_SIZE set
8702 // to 10 There's not really any way to test this error
8703 // condition. The error condition is not complex, so setting
8704 // QCBOR_MAX_DECODE_INPUT_SIZE gives an OK test.
8705
8706 // The input CBOR is only too large because the
8707 // QCBOR_MAX_DECODE_INPUT_SIZE is 10.
8708 //
8709 // This test is disabled for the normal test runs because of the
8710 // special build requirement.
8711
8712
8713 // Tests the start of a map being too large
8714 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTooLarge1), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07008715 QCBORDecode_EnterArray(&DC, NULL);
Laurence Lundbladea4308a82020-10-03 18:08:57 -07008716 QCBORDecode_GetTextString(&DC, &String);
8717 uErr = QCBORDecode_GetError(&DC);
8718 if(uErr != QCBOR_SUCCESS) {
8719 return 1;
8720 }
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07008721 QCBORDecode_EnterMap(&DC, NULL);
Laurence Lundbladea4308a82020-10-03 18:08:57 -07008722 uErr = QCBORDecode_GetError(&DC);
8723 if(uErr != QCBOR_ERR_INPUT_TOO_LARGE) {
8724 return 2;
8725 }
8726
8727 // Tests the end of a map being too large
8728 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTooLarge2), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07008729 QCBORDecode_EnterArray(&DC, NULL);
8730 QCBORDecode_EnterMap(&DC, NULL);
Laurence Lundbladea4308a82020-10-03 18:08:57 -07008731 uErr = QCBORDecode_GetError(&DC);
8732 if(uErr != QCBOR_SUCCESS) {
8733 return 3;
8734 }
8735 QCBORDecode_ExitMap(&DC);
8736 uErr = QCBORDecode_GetError(&DC);
8737 if(uErr != QCBOR_ERR_INPUT_TOO_LARGE) {
8738 return 4;
8739 }
8740
8741 // Tests the entire input CBOR being too large when processing bstr wrapping
8742 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTooLarge3), QCBOR_DECODE_MODE_NORMAL);
8743 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
8744 uErr = QCBORDecode_GetError(&DC);
8745 if(uErr != QCBOR_ERR_INPUT_TOO_LARGE) {
8746 return 5;
8747 }
8748
8749 return 0;
8750}
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07008751
8752
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08008753#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
8754
Laurence Lundblade37286c02022-09-03 10:05:02 -07008755/*
8756 An array of three map entries
8757 1) Indefinite length string label for indefinite lenght byte string
8758 2) Indefinite length string label for an integer
8759 3) Indefinite length string label for an indefinite-length negative big num
8760 */
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07008761static const uint8_t spMapWithIndefLenStrings[] = {
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08008762 0xa3,
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07008763 0x7f, 0x61, 'l', 0x64, 'a', 'b', 'e', 'l' , 0x61, '1', 0xff,
8764 0x5f, 0x42, 0x01, 0x02, 0x43, 0x03, 0x04, 0x05, 0xff,
8765 0x7f, 0x62, 'd', 'y', 0x61, 'm', 0x61, 'o', 0xff,
8766 0x03,
8767 0x7f, 0x62, 'l', 'a', 0x63, 'b', 'e', 'l', 0x61, '2', 0xff,
8768 0xc3,
8769 0x5f, 0x42, 0x00, 0x01, 0x42, 0x00, 0x01, 0x41, 0x01, 0xff,
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07008770};
8771
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03008772int32_t SpiffyIndefiniteLengthStringsTests(void)
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07008773{
8774 QCBORDecodeContext DCtx;
8775
8776 QCBORDecode_Init(&DCtx,
8777 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spMapWithIndefLenStrings),
8778 QCBOR_DECODE_MODE_NORMAL);
8779
Laurence Lundblade8510f8c2020-12-01 11:31:16 -08008780 UsefulBuf_MAKE_STACK_UB(StringBuf, 200);
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07008781 QCBORDecode_SetMemPool(&DCtx, StringBuf, false);
8782
8783 UsefulBufC ByteString;
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07008784 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07008785 QCBORDecode_GetByteStringInMapSZ(&DCtx, "label1", &ByteString);
Laurence Lundblade37286c02022-09-03 10:05:02 -07008786
8787#ifndef QCBOR_DISABLE_TAGS
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07008788 if(QCBORDecode_GetAndResetError(&DCtx)) {
8789 return 1;
8790 }
8791
8792 const uint8_t pExectedBytes[] = {0x01, 0x02, 0x03, 0x04, 0x05};
8793 if(UsefulBuf_Compare(ByteString, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pExectedBytes))) {
8794 return 2;
8795 }
8796
8797 uint64_t uInt;
8798 QCBORDecode_GetUInt64InMapSZ(&DCtx, "dymo", &uInt);
8799 if(QCBORDecode_GetAndResetError(&DCtx)) {
8800 return 3;
8801 }
8802 if(uInt != 3) {
8803 return 4;
8804 }
8805
Máté Tóth-Pálef5f07a2021-09-17 19:31:37 +02008806#ifndef USEFULBUF_DISABLE_ALL_FLOAT
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07008807 double uDouble;
8808 QCBORDecode_GetDoubleConvertAllInMapSZ(&DCtx,
8809 "label2",
8810 0xff,
8811 &uDouble);
Laurence Lundblade37286c02022-09-03 10:05:02 -07008812
Laurence Lundbladeb8e19aa2020-10-07 20:59:11 -07008813#ifndef QCBOR_DISABLE_FLOAT_HW_USE
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07008814 if(QCBORDecode_GetAndResetError(&DCtx)) {
8815 return 5;
8816 }
8817 if(uDouble != -16777474) {
8818 return 6;
8819 }
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08008820#else /* QCBOR_DISABLE_FLOAT_HW_USE */
Laurence Lundbladeb8e19aa2020-10-07 20:59:11 -07008821 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_HW_FLOAT_DISABLED) {
8822 return 7;
8823 }
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08008824#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
Máté Tóth-Pálef5f07a2021-09-17 19:31:37 +02008825#endif /* USEFULBUF_DISABLE_ALL_FLOAT */
Laurence Lundbladeb8e19aa2020-10-07 20:59:11 -07008826
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07008827 QCBORDecode_ExitMap(&DCtx);
8828
8829 if(QCBORDecode_Finish(&DCtx)) {
8830 return 99;
8831 }
8832
Laurence Lundblade37286c02022-09-03 10:05:02 -07008833#else /* QCBOR_DISABLE_TAGS */
8834 /* The big num in the input is a CBOR tag and you can't do
8835 * map lookups in a map with a tag so this test does very little
8836 * when tags are disabled. That is OK, the test coverage is still
8837 * good when they are not.
8838 */
8839 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_TAGS_DISABLED) {
8840 return 1002;
8841 }
8842#endif /*QCBOR_DISABLE_TAGS */
8843
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07008844 return 0;
8845}
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08008846#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
Laurence Lundblade2a26abb2020-11-05 19:06:54 -08008847
8848
Laurence Lundbladeec290b82024-06-10 11:10:54 -07008849#ifndef QCBOR_DISABLE_NON_INTEGER_LABELS
Laurence Lundbladecf41c522021-02-20 10:19:07 -07008850/*
8851 * An array of an integer and an array. The second array contains
8852 * a bstr-wrapped map.
8853 *
8854 * [7, [h'A36D6669... (see next lines) 73']]
8855 *
8856 * {"first integer": 42,
8857 * "an array of two strings": ["string1", "string2"],
8858 * "map in a map":
8859 * { "bytes 1": h'78787878',
8860 * "bytes 2": h'79797979',
8861 * "another int": 98,
8862 * "text 2": "lies, damn lies and statistics"
8863 * }
8864 * }
8865 */
Laurence Lundblade2a26abb2020-11-05 19:06:54 -08008866
Laurence Lundbladecf41c522021-02-20 10:19:07 -07008867static const uint8_t pValidWrappedMapEncoded[] = {
8868 0x82, 0x07, 0x81, 0x58, 0x97,
8869 0xa3, 0x6d, 0x66, 0x69, 0x72, 0x73, 0x74, 0x20, 0x69, 0x6e,
8870 0x74, 0x65, 0x67, 0x65, 0x72, 0x18, 0x2a, 0x77, 0x61, 0x6e,
8871 0x20, 0x61, 0x72, 0x72, 0x61, 0x79, 0x20, 0x6f, 0x66, 0x20,
8872 0x74, 0x77, 0x6f, 0x20, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67,
8873 0x73, 0x82, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x31,
8874 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x32, 0x6c, 0x6d,
8875 0x61, 0x70, 0x20, 0x69, 0x6e, 0x20, 0x61, 0x20, 0x6d, 0x61,
8876 0x70, 0xa4, 0x67, 0x62, 0x79, 0x74, 0x65, 0x73, 0x20, 0x31,
8877 0x44, 0x78, 0x78, 0x78, 0x78, 0x67, 0x62, 0x79, 0x74, 0x65,
8878 0x73, 0x20, 0x32, 0x44, 0x79, 0x79, 0x79, 0x79, 0x6b, 0x61,
8879 0x6e, 0x6f, 0x74, 0x68, 0x65, 0x72, 0x20, 0x69, 0x6e, 0x74,
8880 0x18, 0x62, 0x66, 0x74, 0x65, 0x78, 0x74, 0x20, 0x32, 0x78,
8881 0x1e, 0x6c, 0x69, 0x65, 0x73, 0x2c, 0x20, 0x64, 0x61, 0x6d,
8882 0x6e, 0x20, 0x6c, 0x69, 0x65, 0x73, 0x20, 0x61, 0x6e, 0x64,
8883 0x20, 0x73, 0x74, 0x61, 0x74, 0x69, 0x73, 0x74, 0x69, 0x63,
8884 0x73
8885};
8886
8887#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
8888
8889/* As above, but the arrays are indefinite length */
8890static const uint8_t pValidIndefWrappedMapEncoded[] = {
8891 0x9f, 0x07, 0x9f, 0x58, 0x97,
8892 0xa3, 0x6d, 0x66, 0x69, 0x72, 0x73, 0x74, 0x20, 0x69, 0x6e,
8893 0x74, 0x65, 0x67, 0x65, 0x72, 0x18, 0x2a, 0x77, 0x61, 0x6e,
8894 0x20, 0x61, 0x72, 0x72, 0x61, 0x79, 0x20, 0x6f, 0x66, 0x20,
8895 0x74, 0x77, 0x6f, 0x20, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67,
8896 0x73, 0x82, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x31,
8897 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x32, 0x6c, 0x6d,
8898 0x61, 0x70, 0x20, 0x69, 0x6e, 0x20, 0x61, 0x20, 0x6d, 0x61,
8899 0x70, 0xa4, 0x67, 0x62, 0x79, 0x74, 0x65, 0x73, 0x20, 0x31,
8900 0x44, 0x78, 0x78, 0x78, 0x78, 0x67, 0x62, 0x79, 0x74, 0x65,
8901 0x73, 0x20, 0x32, 0x44, 0x79, 0x79, 0x79, 0x79, 0x6b, 0x61,
8902 0x6e, 0x6f, 0x74, 0x68, 0x65, 0x72, 0x20, 0x69, 0x6e, 0x74,
8903 0x18, 0x62, 0x66, 0x74, 0x65, 0x78, 0x74, 0x20, 0x32, 0x78,
8904 0x1e, 0x6c, 0x69, 0x65, 0x73, 0x2c, 0x20, 0x64, 0x61, 0x6d,
8905 0x6e, 0x20, 0x6c, 0x69, 0x65, 0x73, 0x20, 0x61, 0x6e, 0x64,
8906 0x20, 0x73, 0x74, 0x61, 0x74, 0x69, 0x73, 0x74, 0x69, 0x63,
8907 0x73,
8908 0xff, 0xff
8909};
Laurence Lundbladeec290b82024-06-10 11:10:54 -07008910#endif /* ! QCBOR_DISABLE_NON_INTEGER_LABELS */
Laurence Lundbladecf41c522021-02-20 10:19:07 -07008911
8912static const uint8_t pWithEmptyMap[] = {0x82, 0x18, 0x64, 0xa0};
8913
Laurence Lundbladeec290b82024-06-10 11:10:54 -07008914
Laurence Lundbladecf41c522021-02-20 10:19:07 -07008915#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
8916static const uint8_t pWithEmptyMapInDef[] = {0x9f, 0x18, 0x64, 0xbf, 0xff, 0xff};
8917#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
8918
8919#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
Laurence Lundblade37286c02022-09-03 10:05:02 -07008920/*
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08008921 * An array of one that contains a byte string that is an indefinite
8922 * length string that CBOR wraps an array of three numbers [42, 43,
8923 * 44]. The byte string is an implicit tag 24.
8924 *
8925 * [
8926 * (_ h'83', h'18', h'2A182B', h'182C')
8927 * ]
Laurence Lundblade37286c02022-09-03 10:05:02 -07008928 */
Laurence Lundbladecf41c522021-02-20 10:19:07 -07008929static const uint8_t pWrappedByIndefiniteLength[] = {
8930 0x81,
Laurence Lundbladecf41c522021-02-20 10:19:07 -07008931 0x5f,
8932 0x41, 0x83,
8933 0x41, 0x18,
8934 0x43, 0x2A, 0x18, 0x2B,
8935 0x42, 0x18, 0x2C,
8936 0xff
8937};
8938#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
8939
Laurence Lundbladeec290b82024-06-10 11:10:54 -07008940#endif
Laurence Lundbladecf41c522021-02-20 10:19:07 -07008941
Laurence Lundbladeec290b82024-06-10 11:10:54 -07008942#ifndef QCBOR_DISABLE_NON_INTEGER_LABELS
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03008943int32_t PeekAndRewindTest(void)
Laurence Lundblade2a26abb2020-11-05 19:06:54 -08008944{
8945 QCBORItem Item;
8946 QCBORError nCBORError;
8947 QCBORDecodeContext DCtx;
8948
Laurence Lundbladeec290b82024-06-10 11:10:54 -07008949 // Improvement: rework this test to use only integer labels.
8950
Laurence Lundblade2a26abb2020-11-05 19:06:54 -08008951 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0);
8952
8953 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
8954 return 100+(int32_t)nCBORError;
8955 }
8956 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
8957 return 200;
8958 }
8959
Laurence Lundblade3427dee2021-06-20 11:11:24 -07008960 QCBORDecode_VPeekNext(&DCtx, &Item);
8961 if((nCBORError = QCBORDecode_GetError(&DCtx))) {
8962 return 150+(int32_t)nCBORError;
8963 }
8964 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
8965 return 250;
8966 }
8967
Laurence Lundblade2a26abb2020-11-05 19:06:54 -08008968 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
8969 return (int32_t)nCBORError;
8970 }
8971 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
8972 return 300;
8973 }
8974
8975 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
8976 return 400 + (int32_t)nCBORError;
8977 }
8978 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
8979 return 500;
8980 }
8981
8982 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8983 return (int32_t)nCBORError;
8984 }
8985 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
8986 return 600;
8987 }
8988
8989 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
8990 return 900 + (int32_t)nCBORError;
8991 }
8992 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
8993 Item.uDataType != QCBOR_TYPE_INT64 ||
8994 Item.val.int64 != 42 ||
8995 Item.uDataAlloc ||
8996 Item.uLabelAlloc ||
8997 UsefulBufCompareToSZ(Item.label.string, "first integer")) {
8998 return 1000;
8999 }
9000
9001 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
9002 return 1100 + (int32_t)nCBORError;
9003 }
9004
9005 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
9006 Item.uDataType != QCBOR_TYPE_INT64 ||
9007 Item.val.int64 != 42 ||
9008 Item.uDataAlloc ||
9009 Item.uLabelAlloc ||
9010 UsefulBufCompareToSZ(Item.label.string, "first integer")) {
9011 return 1200;
9012 }
9013
9014
9015 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
9016 return 1300 + (int32_t)nCBORError;
9017 }
9018 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
9019 Item.uDataAlloc ||
9020 Item.uLabelAlloc ||
9021 UsefulBufCompareToSZ(Item.label.string, "an array of two strings") ||
9022 Item.uDataType != QCBOR_TYPE_ARRAY ||
Laurence Lundbladecf41c522021-02-20 10:19:07 -07009023 Item.val.uCount != 2) {
Laurence Lundblade2a26abb2020-11-05 19:06:54 -08009024 return 1400;
Laurence Lundbladecf41c522021-02-20 10:19:07 -07009025 }
Laurence Lundblade2a26abb2020-11-05 19:06:54 -08009026
9027 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
9028 return 1500 + (int32_t)nCBORError;
9029 }
9030 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
9031 Item.uDataAlloc ||
9032 Item.uLabelAlloc ||
9033 UsefulBufCompareToSZ(Item.val.string, "string1")) {
9034 return 1600;
9035 }
9036
9037 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
9038 return 1700 + (int32_t)nCBORError;
9039 }
9040 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
9041 Item.uDataAlloc ||
9042 Item.uLabelAlloc ||
9043 UsefulBufCompareToSZ(Item.val.string, "string2")) {
9044 return 1800;
9045 }
9046
9047 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
9048 return (int32_t)nCBORError;
9049 }
9050 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
9051 Item.uDataAlloc ||
9052 Item.uLabelAlloc ||
9053 UsefulBufCompareToSZ(Item.val.string, "string2")) {
9054 return 1900;
9055 }
9056
9057 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
9058 return (int32_t)nCBORError;
9059 }
9060 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
9061 Item.uDataAlloc ||
9062 Item.uLabelAlloc ||
9063 UsefulBufCompareToSZ(Item.val.string, "string2")) {
9064 return 2000;
9065 }
9066
9067
9068 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
9069 return 2100 + (int32_t)nCBORError;
9070 }
9071 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
9072 Item.uDataAlloc ||
9073 Item.uLabelAlloc ||
9074 UsefulBufCompareToSZ(Item.label.string, "map in a map") ||
9075 Item.uDataType != QCBOR_TYPE_MAP ||
9076 Item.val.uCount != 4) {
9077 return 2100;
9078 }
9079
9080 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
9081 return 2200 + (int32_t)nCBORError;
9082 }
9083 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
9084 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("bytes 1"))||
9085 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
9086 Item.uDataAlloc ||
9087 Item.uLabelAlloc ||
9088 UsefulBufCompareToSZ(Item.val.string, "xxxx")) {
9089 return 2300;
9090 }
9091
9092 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
9093 return 2400 + (int32_t)nCBORError;
9094 }
9095 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
9096 UsefulBufCompareToSZ(Item.label.string, "bytes 2") ||
9097 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
9098 Item.uDataAlloc ||
9099 Item.uLabelAlloc ||
9100 UsefulBufCompareToSZ(Item.val.string, "yyyy")) {
9101 return 2500;
9102 }
9103
9104 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
9105 return 2600 + (int32_t)nCBORError;
9106 }
9107 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
9108 UsefulBufCompareToSZ(Item.label.string, "bytes 2") ||
9109 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
9110 Item.uDataAlloc ||
9111 Item.uLabelAlloc ||
9112 UsefulBufCompareToSZ(Item.val.string, "yyyy")) {
9113 return 2700;
9114 }
9115
9116 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
9117 return 2800 + (int32_t)nCBORError;
9118 }
9119 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
9120 Item.uDataAlloc ||
9121 Item.uLabelAlloc ||
9122 UsefulBufCompareToSZ(Item.label.string, "another int") ||
9123 Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundbladecf41c522021-02-20 10:19:07 -07009124 Item.val.int64 != 98) {
Laurence Lundblade2a26abb2020-11-05 19:06:54 -08009125 return 2900;
Laurence Lundbladecf41c522021-02-20 10:19:07 -07009126 }
Laurence Lundblade2a26abb2020-11-05 19:06:54 -08009127
9128 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
9129 return 3000 + (int32_t)nCBORError;
9130 }
9131 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
9132 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("text 2"))||
9133 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
9134 Item.uDataAlloc ||
9135 Item.uLabelAlloc ||
9136 UsefulBufCompareToSZ(Item.val.string, "lies, damn lies and statistics")) {
9137 return 3100;
9138 }
9139
9140 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
9141 return 3200 + (int32_t)nCBORError;
9142 }
9143 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
9144 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("text 2"))||
9145 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
9146 Item.uDataAlloc ||
9147 Item.uLabelAlloc ||
9148 UsefulBufCompareToSZ(Item.val.string, "lies, damn lies and statistics")) {
9149 return 3300;
9150 }
9151
Laurence Lundblade3427dee2021-06-20 11:11:24 -07009152 nCBORError = QCBORDecode_PeekNext(&DCtx, &Item);
9153 if(nCBORError != QCBOR_ERR_NO_MORE_ITEMS) {
9154 return 3300 + (int32_t)nCBORError;
9155 }
9156
9157 QCBORDecode_VPeekNext(&DCtx, &Item);
9158 nCBORError = QCBORDecode_GetError(&DCtx);
9159 if(nCBORError != QCBOR_ERR_NO_MORE_ITEMS) {
9160 return 3400 + (int32_t)nCBORError;
9161 }
9162
9163 QCBORDecode_VPeekNext(&DCtx, &Item);
9164 nCBORError = QCBORDecode_GetError(&DCtx);
9165 if(nCBORError != QCBOR_ERR_NO_MORE_ITEMS) {
9166 return 3500 + (int32_t)nCBORError;
9167 }
Laurence Lundbladecf41c522021-02-20 10:19:07 -07009168
9169
9170 // Rewind to top level after entering several maps
9171 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0);
9172
9173 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
9174 return (int32_t)nCBORError;
9175 }
9176 if(Item.uDataType != QCBOR_TYPE_MAP ||
9177 Item.val.uCount != 3) {
9178 return 400;
9179 }
9180
9181 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
9182 return 4000+(int32_t)nCBORError;
9183 }
9184
9185 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
9186 Item.uDataType != QCBOR_TYPE_INT64 ||
9187 Item.val.int64 != 42 ||
9188 Item.uDataAlloc ||
9189 Item.uLabelAlloc ||
9190 UsefulBufCompareToSZ(Item.label.string, "first integer")) {
9191 return 4100;
9192 }
9193
9194 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
9195 return 4100+(int32_t)nCBORError;
9196 }
9197 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
9198 Item.uDataAlloc ||
9199 Item.uLabelAlloc ||
9200 UsefulBufCompareToSZ(Item.label.string, "an array of two strings") ||
9201 Item.uDataType != QCBOR_TYPE_ARRAY ||
9202 Item.val.uCount != 2) {
9203 return 4200;
9204 }
9205
9206 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
9207 return 4200+(int32_t)nCBORError;
9208 }
9209 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
9210 Item.uDataAlloc ||
9211 Item.uLabelAlloc ||
9212 UsefulBufCompareToSZ(Item.val.string, "string1")) {
9213 return 4300;
9214 }
9215
9216 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
9217 return 4300+(int32_t)nCBORError;
9218 }
9219 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
9220 Item.uDataAlloc ||
9221 Item.uLabelAlloc ||
9222 UsefulBufCompareToSZ(Item.val.string, "string2")) {
9223 return 4400;
9224 }
9225
9226 QCBORDecode_Rewind(&DCtx);
9227
9228 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
9229 return 4400+(int32_t)nCBORError;
9230 }
9231 if(Item.uDataType != QCBOR_TYPE_MAP ||
9232 Item.val.uCount != 3) {
9233 return 4500;
9234 }
9235
9236 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
9237 return (int32_t)nCBORError;
9238 }
9239
9240 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
9241 Item.uDataType != QCBOR_TYPE_INT64 ||
9242 Item.val.int64 != 42 ||
9243 Item.uDataAlloc ||
9244 Item.uLabelAlloc ||
9245 UsefulBufCompareToSZ(Item.label.string, "first integer")) {
9246 return 4600;
9247 }
9248
9249 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
9250 return (int32_t)nCBORError;
9251 }
9252 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
9253 Item.uDataAlloc ||
9254 Item.uLabelAlloc ||
9255 UsefulBufCompareToSZ(Item.label.string, "an array of two strings") ||
9256 Item.uDataType != QCBOR_TYPE_ARRAY ||
9257 Item.val.uCount != 2) {
9258 return 4700;
9259 }
9260
9261 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
9262 return (int32_t)nCBORError;
9263 }
9264 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
9265 Item.uDataAlloc ||
9266 Item.uLabelAlloc ||
9267 UsefulBufCompareToSZ(Item.val.string, "string1")) {
9268 return 4800;
9269 }
9270
9271 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
9272 return 4900+(int32_t)nCBORError;
9273 }
9274 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
9275 Item.uDataAlloc ||
9276 Item.uLabelAlloc ||
9277 UsefulBufCompareToSZ(Item.val.string, "string2")) {
9278 return 5000;
9279 }
9280
9281
9282 // Rewind an entered map
9283 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0);
9284
9285 QCBORDecode_EnterMap(&DCtx, NULL);
9286
9287 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
9288 return 5100+(int32_t)nCBORError;
9289 }
9290
9291 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
9292 Item.uDataType != QCBOR_TYPE_INT64 ||
9293 Item.val.int64 != 42 ||
9294 Item.uDataAlloc ||
9295 Item.uLabelAlloc ||
9296 UsefulBufCompareToSZ(Item.label.string, "first integer")) {
9297 return 5200;
9298 }
9299
9300 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
9301 return 5200+(int32_t)nCBORError;
9302 }
9303 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
9304 Item.uDataAlloc ||
9305 Item.uLabelAlloc ||
9306 UsefulBufCompareToSZ(Item.label.string, "an array of two strings") ||
9307 Item.uDataType != QCBOR_TYPE_ARRAY ||
9308 Item.val.uCount != 2) {
9309 return -5300;
9310 }
9311
9312 QCBORDecode_Rewind(&DCtx);
9313
9314 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
9315 return 5300+(int32_t)nCBORError;
9316 }
9317
9318 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
9319 Item.uDataType != QCBOR_TYPE_INT64 ||
9320 Item.val.int64 != 42 ||
9321 Item.uDataAlloc ||
9322 Item.uLabelAlloc ||
9323 UsefulBufCompareToSZ(Item.label.string, "first integer")) {
9324 return 5400;
9325 }
9326
9327 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
9328 return 5400+(int32_t)nCBORError;
9329 }
9330 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
9331 Item.uDataAlloc ||
9332 Item.uLabelAlloc ||
9333 UsefulBufCompareToSZ(Item.label.string, "an array of two strings") ||
9334 Item.uDataType != QCBOR_TYPE_ARRAY ||
9335 Item.val.uCount != 2) {
9336 return 5500;
9337 }
9338
9339
9340 // Rewind and entered array inside an entered map
9341 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0);
9342
9343 QCBORDecode_EnterMap(&DCtx, NULL);
9344
9345 QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings");
9346
9347 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
9348 return 5600+(int32_t)nCBORError;
9349 }
9350 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
9351 Item.uDataAlloc ||
9352 Item.uLabelAlloc ||
9353 UsefulBufCompareToSZ(Item.val.string, "string1")) {
9354 return 5700;
9355 }
9356
9357 QCBORDecode_Rewind(&DCtx);
9358
9359 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
9360 return 5700+(int32_t)nCBORError;
9361 }
9362 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
9363 Item.uDataAlloc ||
9364 Item.uLabelAlloc ||
9365 UsefulBufCompareToSZ(Item.val.string, "string1")) {
9366 return 5800;
9367 }
9368
9369 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
9370 return (int32_t)nCBORError;
9371 }
9372 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
9373 Item.uDataAlloc ||
9374 Item.uLabelAlloc ||
9375 UsefulBufCompareToSZ(Item.val.string, "string2")) {
9376 return 5900;
9377 }
9378
9379 QCBORDecode_Rewind(&DCtx);
9380
9381 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
9382 return 5900+(int32_t)nCBORError;
9383 }
9384 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
9385 Item.uDataAlloc ||
9386 Item.uLabelAlloc ||
9387 UsefulBufCompareToSZ(Item.val.string, "string1")) {
9388 return 6000;
9389 }
9390
9391
9392 // Rewind a byte string inside an array inside an array
9393 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidWrappedMapEncoded), 0);
9394
9395 QCBORDecode_EnterArray(&DCtx, NULL);
9396
9397 uint64_t i;
9398 QCBORDecode_GetUInt64(&DCtx, &i);
9399
9400 QCBORDecode_EnterArray(&DCtx, NULL);
9401
9402 QCBORDecode_EnterBstrWrapped(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
9403 if(QCBORDecode_GetError(&DCtx)) {
9404 return 6100;
9405 }
9406
9407 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
9408 return (int32_t)nCBORError;
9409 }
9410 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
9411 return 6200;
9412 }
9413
9414 QCBORDecode_Rewind(&DCtx);
9415
9416 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
9417 return 6300+(int32_t)nCBORError;
9418 }
9419 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
9420 return 6400;
9421 }
9422
9423#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
9424 // Rewind a byte string inside an indefinite-length array inside
9425 // indefinite-length array
9426
9427 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidIndefWrappedMapEncoded), 0);
9428
9429 QCBORDecode_EnterArray(&DCtx, NULL);
9430
9431 QCBORDecode_GetUInt64(&DCtx, &i);
9432
9433 QCBORDecode_EnterArray(&DCtx, NULL);
9434
9435 QCBORDecode_EnterBstrWrapped(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
9436 if(QCBORDecode_GetError(&DCtx)) {
9437 return 6500;
9438 }
9439
9440 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
9441 return 6600+(int32_t)nCBORError;
9442 }
9443 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
9444 return 6700;
9445 }
9446
9447 QCBORDecode_Rewind(&DCtx);
9448
9449 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
9450 return 6800+(int32_t)nCBORError;
9451 }
9452 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
9453 return 6900;
9454 }
9455#endif
9456
9457 // Rewind an empty map
9458 // [100, {}]
9459 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pWithEmptyMap), 0);
9460 QCBORDecode_EnterArray(&DCtx, NULL);
9461 QCBORDecode_GetUInt64(&DCtx, &i);
9462 if(i != 100) {
9463 return 7010;
9464 }
9465 QCBORDecode_EnterMap(&DCtx, NULL);
9466
9467 /* Do it 5 times to be sure multiple rewinds work */
9468 for(int n = 0; n < 5; n++) {
9469 nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
9470 if(nCBORError != QCBOR_ERR_NO_MORE_ITEMS) {
9471 return 7000 + n;
9472 }
9473 QCBORDecode_Rewind(&DCtx);
9474 }
9475 QCBORDecode_ExitMap(&DCtx);
9476 QCBORDecode_Rewind(&DCtx);
9477 QCBORDecode_GetUInt64(&DCtx, &i);
9478 if(i != 100) {
9479 return 7010;
9480 }
9481 QCBORDecode_ExitArray(&DCtx);
9482 QCBORDecode_Rewind(&DCtx);
9483 QCBORDecode_EnterArray(&DCtx, NULL);
9484 i = 9;
9485 QCBORDecode_GetUInt64(&DCtx, &i);
9486 if(i != 100) {
9487 return 7020;
9488 }
9489 if(QCBORDecode_GetError(&DCtx)){
9490 return 7030;
9491 }
9492
9493 // Rewind an empty indefinite length map
9494#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
9495 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pWithEmptyMapInDef), 0);
9496 QCBORDecode_EnterArray(&DCtx, NULL);
9497 QCBORDecode_GetUInt64(&DCtx, &i);
9498 if(i != 100) {
9499 return 7810;
9500 }
9501 QCBORDecode_EnterMap(&DCtx, NULL);
9502
9503 /* Do it 5 times to be sure multiple rewinds work */
9504 for(int n = 0; n < 5; n++) {
9505 nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
9506 if(nCBORError != QCBOR_ERR_NO_MORE_ITEMS) {
9507 return 7800 + n;
9508 }
9509 QCBORDecode_Rewind(&DCtx);
9510 }
9511 QCBORDecode_ExitMap(&DCtx);
9512 QCBORDecode_Rewind(&DCtx);
9513 QCBORDecode_GetUInt64(&DCtx, &i);
9514 if(i != 100) {
9515 return 7810;
9516 }
9517 QCBORDecode_ExitArray(&DCtx);
9518 QCBORDecode_Rewind(&DCtx);
9519 QCBORDecode_EnterArray(&DCtx, NULL);
9520 i = 9;
9521 QCBORDecode_GetUInt64(&DCtx, &i);
9522 if(i != 100) {
9523 return 7820;
9524 }
9525 if(QCBORDecode_GetError(&DCtx)){
9526 return 7830;
9527 }
9528#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
9529
9530 // Rewind an indefnite length byte-string wrapped sequence
9531#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
9532 QCBORDecode_Init(&DCtx,
9533 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pWrappedByIndefiniteLength),
9534 0);
9535 UsefulBuf_MAKE_STACK_UB(Pool, 100);
9536 QCBORDecode_SetMemPool(&DCtx, Pool, 0);
9537
9538 QCBORDecode_EnterArray(&DCtx, NULL);
9539 QCBORDecode_EnterBstrWrapped(&DCtx, 2, NULL);
Laurence Lundblade31fddb72024-05-13 13:03:35 -07009540 if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_CANNOT_ENTER_ALLOCATED_STRING) {
Laurence Lundbladecf41c522021-02-20 10:19:07 -07009541 return 7300;
9542 }
9543
9544 /*
Laurence Lundblade31fddb72024-05-13 13:03:35 -07009545 Improvement: Fix QCBORDecode_EnterBstrWrapped() so it can work on
9546 allocated strings. This is a fairly big job because of all the
9547 UsefulBuf internal book keeping that needs tweaking.
Laurence Lundbladecf41c522021-02-20 10:19:07 -07009548 QCBORDecode_GetUInt64(&DCtx, &i);
9549 if(i != 42) {
9550 return 7110;
9551 }
9552 QCBORDecode_Rewind(&DCtx);
9553 QCBORDecode_GetUInt64(&DCtx, &i);
9554 if(i != 42) {
9555 return 7220;
Laurence Lundblade31fddb72024-05-13 13:03:35 -07009556 }
9557 */
Laurence Lundblade37286c02022-09-03 10:05:02 -07009558
Laurence Lundbladeec290b82024-06-10 11:10:54 -07009559#endif /* ! QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
Laurence Lundbladecf41c522021-02-20 10:19:07 -07009560
9561
9562 // Rewind an indefnite length byte-string wrapped sequence
9563
Laurence Lundblade2a26abb2020-11-05 19:06:54 -08009564 return 0;
9565}
Laurence Lundblade9f9c3732021-03-23 09:38:46 -07009566
Laurence Lundbladeec290b82024-06-10 11:10:54 -07009567#endif /* ! QCBOR_DISABLE_NON_INTEGER_LABELS */
Laurence Lundblade9f9c3732021-03-23 09:38:46 -07009568
9569
9570static const uint8_t spBooleansInMap[] =
9571{
9572 0xa1, 0x08, 0xf5
9573};
9574
9575static const uint8_t spBooleansInMapWrongType[] =
9576{
9577 0xa1, 0x08, 0xf6
9578};
9579
9580static const uint8_t spBooleansInMapNWF[] =
9581{
9582 0xa1, 0x08, 0x1a
9583};
9584
Laurence Lundblade8782dd32021-04-27 04:15:37 -07009585static const uint8_t spNullInMap[] =
9586{
9587 0xa1, 0x08, 0xf6
9588};
9589
9590static const uint8_t spUndefinedInMap[] =
9591{
9592 0xa1, 0x08, 0xf7
9593};
9594
Laurence Lundblade9f9c3732021-03-23 09:38:46 -07009595
Laurence Lundbladecdbbc192024-06-28 15:13:04 -07009596#ifndef QCBOR_DISABLE_TAGS
9597static const uint8_t spTaggedSimples[] =
9598{
9599 0xd8, 0x58, 0xd8, 0x2c, 0xd6, 0xf5,
9600 0xd9, 0x0f, 0xA0, 0xf7
9601};
9602#endif /* ! QCBOR_DISABLE_TAGS */
9603
9604
9605
Laurence Lundblade9f9c3732021-03-23 09:38:46 -07009606int32_t BoolTest(void)
9607{
9608 QCBORDecodeContext DCtx;
9609 bool b;
9610
Laurence Lundblade8782dd32021-04-27 04:15:37 -07009611 QCBORDecode_Init(&DCtx,
9612 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMap),
9613 0);
Laurence Lundblade9f9c3732021-03-23 09:38:46 -07009614 QCBORDecode_EnterMap(&DCtx, NULL);
9615 QCBORDecode_GetBool(&DCtx, &b);
9616 if(QCBORDecode_GetAndResetError(&DCtx) || !b) {
9617 return 1;
9618 }
9619
9620 QCBORDecode_GetBoolInMapN(&DCtx, 7, &b);
9621 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_LABEL_NOT_FOUND) {
9622 return 2;
9623 }
9624
9625 QCBORDecode_GetBoolInMapN(&DCtx, 8, &b);
9626 if(QCBORDecode_GetAndResetError(&DCtx) || !b) {
9627 return 3;
9628 }
9629
9630
9631 QCBORDecode_GetBoolInMapSZ(&DCtx, "xx", &b);
9632 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_LABEL_NOT_FOUND) {
9633 return 4;
9634 }
9635
Laurence Lundblade8782dd32021-04-27 04:15:37 -07009636 QCBORDecode_Init(&DCtx,
9637 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMapWrongType),
9638 0);
Laurence Lundblade9f9c3732021-03-23 09:38:46 -07009639 QCBORDecode_EnterMap(&DCtx, NULL);
9640 QCBORDecode_GetBool(&DCtx, &b);
9641 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
9642 return 5;
9643 }
9644
Laurence Lundblade8782dd32021-04-27 04:15:37 -07009645 QCBORDecode_Init(&DCtx,
9646 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMapNWF),
9647 0);
Laurence Lundblade9f9c3732021-03-23 09:38:46 -07009648 QCBORDecode_EnterMap(&DCtx, NULL);
9649 QCBORDecode_GetBool(&DCtx, &b);
9650 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_HIT_END) {
9651 return 6;
9652 }
9653
Laurence Lundblade8782dd32021-04-27 04:15:37 -07009654
9655 QCBORDecode_Init(&DCtx,
9656 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spNullInMap),
9657 0);
9658 QCBORDecode_EnterMap(&DCtx, NULL);
9659 QCBORDecode_GetNull(&DCtx);
9660 if(QCBORDecode_GetAndResetError(&DCtx)) {
9661 return 7;
9662 }
9663
9664 QCBORDecode_Init(&DCtx,
9665 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMap),
9666 0);
9667 QCBORDecode_EnterMap(&DCtx, NULL);
9668 QCBORDecode_GetNull(&DCtx);
9669 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
9670 return 8;
9671 }
9672
9673 QCBORDecode_Init(&DCtx,
9674 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spNullInMap),
9675 0);
9676 QCBORDecode_EnterMap(&DCtx, NULL);
9677 QCBORDecode_GetNullInMapN(&DCtx, 8);
9678 if(QCBORDecode_GetAndResetError(&DCtx)) {
9679 return 9;
9680 }
9681
9682 QCBORDecode_Init(&DCtx,
9683 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMap),
9684 0);
9685 QCBORDecode_EnterMap(&DCtx, NULL);
9686 QCBORDecode_GetNullInMapN(&DCtx, 8);
9687 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
9688 return 10;
9689 }
9690
9691 QCBORDecode_Init(&DCtx,
9692 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMapNWF),
9693 0);
9694 QCBORDecode_EnterMap(&DCtx, NULL);
9695 QCBORDecode_GetUndefined(&DCtx);
9696 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_HIT_END) {
9697 return 11;
9698 }
9699
9700 QCBORDecode_Init(&DCtx,
9701 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUndefinedInMap),
9702 0);
9703 QCBORDecode_EnterMap(&DCtx, NULL);
9704 QCBORDecode_GetUndefined(&DCtx);
9705 if(QCBORDecode_GetAndResetError(&DCtx)) {
9706 return 12;
9707 }
9708
9709 QCBORDecode_Init(&DCtx,
9710 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMap),
9711 0);
9712 QCBORDecode_EnterMap(&DCtx, NULL);
9713 QCBORDecode_GetUndefined(&DCtx);
9714 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
9715 return 13;
9716 }
9717
9718 QCBORDecode_Init(&DCtx,
9719 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUndefinedInMap),
9720 0);
9721 QCBORDecode_EnterMap(&DCtx, NULL);
9722 QCBORDecode_GetUndefinedInMapN(&DCtx, 8);
9723 if(QCBORDecode_GetAndResetError(&DCtx)) {
9724 return 14;
9725 }
9726
9727 QCBORDecode_Init(&DCtx,
9728 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMap),
9729 0);
9730 QCBORDecode_EnterMap(&DCtx, NULL);
9731 QCBORDecode_GetUndefinedInMapN(&DCtx, 8);
9732 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
9733 return 15;
9734 }
9735
9736 QCBORDecode_Init(&DCtx,
9737 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMapNWF),
9738 0);
9739 QCBORDecode_EnterMap(&DCtx, NULL);
9740 QCBORDecode_GetUndefined(&DCtx);
9741 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_HIT_END) {
9742 return 15;
9743 }
9744
Laurence Lundbladecdbbc192024-06-28 15:13:04 -07009745#ifndef QCBOR_DISABLE_TAGS
9746 QCBORDecode_Init(&DCtx,
9747 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTaggedSimples),
9748 0);
9749 QCBORDecode_GetBool(&DCtx, &b);
9750 if(QCBORDecode_GetNthTagOfLast(&DCtx, 0) != 22) {
9751 return 401;
9752 }
9753 if(QCBORDecode_GetNthTagOfLast(&DCtx, 1) != 44) {
9754 return 402;
9755 }
9756 if(QCBORDecode_GetNthTagOfLast(&DCtx, 2) != 88) {
9757 return 403;
9758 }
9759 if(QCBORDecode_GetNthTagOfLast(&DCtx, 3) != CBOR_TAG_INVALID64) {
9760 return 404;
9761 }
9762 QCBORDecode_GetUndefined(&DCtx);
9763 if(QCBORDecode_GetNthTagOfLast(&DCtx, 0) != 4000) {
9764 return 405;
9765 }
9766 if(QCBORDecode_GetNthTagOfLast(&DCtx, 1) != CBOR_TAG_INVALID64) {
9767 return 406;
9768 }
9769 QCBORDecode_GetNull(&DCtx); /* Off the end */
9770 if(QCBORDecode_GetNthTagOfLast(&DCtx, 0) != CBOR_TAG_INVALID64) {
9771 return 407;
9772 }
9773#endif /* ! QCBOR_DISABLE_TAGS */
9774
Laurence Lundblade9f9c3732021-03-23 09:38:46 -07009775 return 0;
9776}
Laurence Lundbladef00b8be2024-03-08 10:34:33 -08009777
9778
Laurence Lundbladeec290b82024-06-10 11:10:54 -07009779#ifndef QCBOR_DISABLE_NON_INTEGER_LABELS
Laurence Lundbladea29f45a2024-05-14 15:55:19 -07009780static const uint8_t spExpectedArray2s[] = {
9781 0x82, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67,
9782 0x31, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67,
9783 0x32};
9784
9785#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
9786static const uint8_t spExpectedArray2sIndef[] = {
9787 0x9f, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67,
9788 0x31, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67,
9789 0x32, 0xff};
9790#endif
9791
9792static const uint8_t spExpectedMap4[] = {
9793 0xa4, 0x67, 0x62, 0x79, 0x74, 0x65, 0x73, 0x20,
9794 0x31, 0x44, 0x78, 0x78, 0x78, 0x78, 0x67, 0x62,
9795 0x79, 0x74, 0x65, 0x73, 0x20, 0x32, 0x44, 0x79,
9796 0x79, 0x79, 0x79, 0x6b, 0x61, 0x6e, 0x6f, 0x74,
9797 0x68, 0x65, 0x72, 0x20, 0x69, 0x6e, 0x74, 0x18,
9798 0x62, 0x66, 0x74, 0x65, 0x78, 0x74, 0x20, 0x32,
9799 0x78, 0x1e, 0x6c, 0x69, 0x65, 0x73, 0x2c, 0x20,
9800 0x64, 0x61, 0x6d, 0x6e, 0x20, 0x6c, 0x69, 0x65,
9801 0x73, 0x20, 0x61, 0x6e, 0x64, 0x20, 0x73, 0x74,
9802 0x61, 0x74, 0x69, 0x73, 0x74, 0x69, 0x63, 0x73};
9803
9804
9805#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
9806
9807static const uint8_t spExpectedMap4Indef[] = {
9808 0xbf, 0x67, 0x62, 0x79, 0x74, 0x65, 0x73, 0x20,
9809 0x31, 0x44, 0x78, 0x78, 0x78, 0x78, 0x67, 0x62,
9810 0x79, 0x74, 0x65, 0x73, 0x20, 0x32, 0x44, 0x79,
9811 0x79, 0x79, 0x79, 0x6b, 0x61, 0x6e, 0x6f, 0x74,
9812 0x68, 0x65, 0x72, 0x20, 0x69, 0x6e, 0x74, 0x18,
9813 0x62, 0x66, 0x74, 0x65, 0x78, 0x74, 0x20, 0x32,
9814 0x78, 0x1e, 0x6c, 0x69, 0x65, 0x73, 0x2c, 0x20,
9815 0x64, 0x61, 0x6d, 0x6e, 0x20, 0x6c, 0x69, 0x65,
9816 0x73, 0x20, 0x61, 0x6e, 0x64, 0x20, 0x73, 0x74,
9817 0x61, 0x74, 0x69, 0x73, 0x74, 0x69, 0x63, 0x73,
9818 0xff};
9819
9820/*
9821 * [[[[[0, []]]]], 0]
9822 */
9823static const uint8_t spDefAndIndef[] = {
9824 0x82,
9825 0x9f, 0x9f, 0x9f, 0x82, 0x00, 0x9f, 0xff, 0xff, 0xff, 0xff, 0x00
9826};
9827#endif /* !QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
9828
Laurence Lundbladea29f45a2024-05-14 15:55:19 -07009829#ifndef QCBOR_DISABLE_TAGS
9830/* An exp / mant tag in two nested arrays */
9831static const uint8_t spExpMant[] = {0x81, 0x81, 0xC4, 0x82, 0x20, 0x03};
9832#endif /* !QCBOR_DISABLE_TAGS */
Laurence Lundbladeec290b82024-06-10 11:10:54 -07009833#endif
Laurence Lundbladea29f45a2024-05-14 15:55:19 -07009834
Laurence Lundbladeec290b82024-06-10 11:10:54 -07009835#ifndef QCBOR_DISABLE_NON_INTEGER_LABELS
Laurence Lundbladea29f45a2024-05-14 15:55:19 -07009836int32_t GetMapAndArrayTest(void)
9837{
9838 QCBORDecodeContext DCtx;
9839 size_t uPosition ;
9840 QCBORItem Item;
9841 UsefulBufC ReturnedEncodedCBOR;
9842
Laurence Lundbladeec290b82024-06-10 11:10:54 -07009843 // Improvement: rework so it can run with QCBOR_DISABLE_NON_INTEGER_LABELS
Laurence Lundbladea29f45a2024-05-14 15:55:19 -07009844 QCBORDecode_Init(&DCtx,
9845 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded),
9846 0);
9847
9848 QCBORDecode_EnterMap(&DCtx, NULL);
9849 QCBORDecode_VGetNextConsume(&DCtx, &Item);
9850 QCBORDecode_GetArray(&DCtx, &Item, &ReturnedEncodedCBOR);
9851 if(QCBORDecode_GetError(&DCtx)) {
9852 return 1;
9853 }
9854 if(Item.val.uCount != 2) {
9855 return 2;
9856 }
9857 if(UsefulBuf_Compare(ReturnedEncodedCBOR, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedArray2s))) {
9858 return 3;
9859 }
9860
9861 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
9862 UsefulBuf_Compare(Item.label.string, UsefulBuf_FROM_SZ_LITERAL("an array of two strings"))) {
9863 return 4;
9864 }
9865
9866 uPosition = QCBORDecode_Tell(&DCtx);
9867
9868
9869 QCBORDecode_GetMap(&DCtx, &Item, &ReturnedEncodedCBOR);
9870 if(QCBORDecode_GetError(&DCtx)) {
9871 return 10;
9872 }
9873 if(Item.val.uCount != 4) {
9874 return 11;
9875 }
9876 if(UsefulBuf_Compare(ReturnedEncodedCBOR, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedMap4))) {
9877 return 12;
9878 }
9879 uPosition = QCBORDecode_Tell(&DCtx);
9880 QCBORDecode_GetArrayFromMapSZ(&DCtx,
9881 "an array of two strings",
9882 &Item,
9883 &ReturnedEncodedCBOR);
9884 if(QCBORDecode_GetError(&DCtx)) {
9885 return 20;
9886 }
9887 if(Item.val.uCount != 2) {
9888 return 21;
9889 }
9890 if(UsefulBuf_Compare(ReturnedEncodedCBOR, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedArray2s))) {
9891 return 22;
9892 }
9893 if(uPosition != QCBORDecode_Tell(&DCtx)) {
9894 return 23;
9895 }
9896
9897 QCBORDecode_Rewind(&DCtx);
9898
9899 uPosition = QCBORDecode_Tell(&DCtx);
9900 QCBORDecode_GetMapFromMapSZ(&DCtx, "map in a map", &Item, &ReturnedEncodedCBOR);
9901 if(QCBORDecode_GetError(&DCtx)) {
9902 return 30;
9903 }
9904 if(Item.val.uCount != 4) {
9905 return 31;
9906 }
9907 if(UsefulBuf_Compare(ReturnedEncodedCBOR, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedMap4))) {
9908 return 32;
9909 }
9910 if(uPosition != QCBORDecode_Tell(&DCtx)) {
9911 return 33;
9912 }
9913
9914 uPosition = QCBORDecode_Tell(&DCtx);
9915 QCBORDecode_GetArrayFromMapSZ(&DCtx, "map in a map", &Item, &ReturnedEncodedCBOR);
9916 if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
9917 return 40;
9918 }
9919 if(UINT32_MAX != QCBORDecode_Tell(&DCtx)) {
9920 return 41;
9921 }
9922 QCBORDecode_GetAndResetError(&DCtx);
9923 if(uPosition != QCBORDecode_Tell(&DCtx)) {
9924 return 42;
9925 }
9926
9927
9928#ifndef QCBOR_DISABLE_TAGS
9929 UsefulBufC ExpMant = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpMant);
9930 QCBORDecode_Init(&DCtx, ExpMant, 0);
9931 QCBORDecode_EnterArray(&DCtx, NULL);
9932 QCBORDecode_EnterArray(&DCtx, NULL);
9933 QCBORDecode_GetArray(&DCtx, &Item, &ReturnedEncodedCBOR);
9934 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) {
9935 return 200;
9936 }
9937 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
9938 return 201;
9939 }
9940 if(!QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_DECIMAL_FRACTION)) {
9941 return 202;
9942 }
9943 if(Item.val.uCount != 2) {
9944 return 201;
9945 }
9946 if(UsefulBuf_Compare(ReturnedEncodedCBOR, UsefulBuf_Tail(ExpMant, 2))) {
9947 return 205;
9948 }
9949#endif /* !QCBOR_DISABLE_TAGS */
9950
9951
9952#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
9953
9954 UsefulBufC DefAndIndef = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spDefAndIndef);
9955 QCBORDecode_Init(&DCtx, DefAndIndef, 0);
9956 QCBORDecode_EnterArray(&DCtx, NULL);
9957 QCBORDecode_GetArray(&DCtx, &Item, &ReturnedEncodedCBOR);
9958 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) {
9959 return 50;
9960 }
9961 if(UsefulBuf_Compare(ReturnedEncodedCBOR, UsefulBuf_Tail(UsefulBuf_Head(DefAndIndef, 11), 1))) {
9962 return 51;
9963 }
9964
9965 QCBORDecode_Init(&DCtx, DefAndIndef, 0);
9966 QCBORDecode_EnterArray(&DCtx, NULL);
9967 QCBORDecode_EnterArray(&DCtx, NULL);
9968 QCBORDecode_GetArray(&DCtx, &Item, &ReturnedEncodedCBOR);
9969 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) {
9970 return 52;
9971 }
9972 if(UsefulBuf_Compare(ReturnedEncodedCBOR, UsefulBuf_Tail(UsefulBuf_Head(DefAndIndef, 10), 2))) {
9973 return 53;
9974 }
9975
9976 QCBORDecode_Init(&DCtx, DefAndIndef, 0);
9977 QCBORDecode_EnterArray(&DCtx, NULL);
9978 QCBORDecode_EnterArray(&DCtx, NULL);
9979 QCBORDecode_EnterArray(&DCtx, NULL);
9980 QCBORDecode_GetArray(&DCtx, &Item, &ReturnedEncodedCBOR);
9981 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) {
9982 return 54;
9983 }
9984 if(UsefulBuf_Compare(ReturnedEncodedCBOR, UsefulBuf_Tail(UsefulBuf_Head(DefAndIndef, 9), 3))) {
9985 return 55;
9986 }
9987 QCBORDecode_Init(&DCtx, DefAndIndef, 0);
9988 QCBORDecode_EnterArray(&DCtx, NULL);
9989 QCBORDecode_EnterArray(&DCtx, NULL);
9990 QCBORDecode_EnterArray(&DCtx, NULL);
9991 QCBORDecode_EnterArray(&DCtx, NULL);
9992 QCBORDecode_GetArray(&DCtx, &Item, &ReturnedEncodedCBOR);
9993 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) {
9994 return 56;
9995 }
9996 if(UsefulBuf_Compare(ReturnedEncodedCBOR, UsefulBuf_Tail(UsefulBuf_Head(DefAndIndef, 8), 4))) {
9997 return 57;
9998 }
9999
10000 QCBORDecode_Init(&DCtx, DefAndIndef, 0);
10001 QCBORDecode_EnterArray(&DCtx, NULL);
10002 QCBORDecode_EnterArray(&DCtx, NULL);
10003 QCBORDecode_EnterArray(&DCtx, NULL);
10004 QCBORDecode_EnterArray(&DCtx, NULL);
10005 QCBORDecode_EnterArray(&DCtx, NULL);
10006 QCBORDecode_VGetNextConsume(&DCtx, &Item);
10007 QCBORDecode_GetArray(&DCtx, &Item, &ReturnedEncodedCBOR);
10008 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) {
10009 return 58;
10010 }
10011 if(UsefulBuf_Compare(ReturnedEncodedCBOR, UsefulBuf_Tail(UsefulBuf_Head(DefAndIndef, 8), 6))) {
10012 return 59;
10013 }
10014
10015
10016 QCBORDecode_Init(&DCtx,
10017 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapIndefEncoded),
10018 0);
10019
10020 QCBORDecode_EnterMap(&DCtx, NULL);
10021 QCBORDecode_VGetNextConsume(&DCtx, &Item);
10022 QCBORDecode_GetArray(&DCtx, &Item, &ReturnedEncodedCBOR);
10023 if(QCBORDecode_GetError(&DCtx)) {
10024 return 60;
10025 }
10026 if(Item.val.uCount != UINT16_MAX) {
10027 return 61;
10028 }
10029 if(UsefulBuf_Compare(ReturnedEncodedCBOR, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedArray2sIndef))) {
10030 return 62;
10031 }
10032
10033 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
10034 UsefulBuf_Compare(Item.label.string, UsefulBuf_FROM_SZ_LITERAL("an array of two strings"))) {
10035 return 63;
10036 }
10037
10038 uPosition = QCBORDecode_Tell(&DCtx);
10039
10040
10041 QCBORDecode_GetMap(&DCtx, &Item, &ReturnedEncodedCBOR);
10042 if(QCBORDecode_GetError(&DCtx)) {
10043 return 70;
10044 }
10045 if(Item.val.uCount != UINT16_MAX) {
10046 return 71;
10047 }
10048 if(UsefulBuf_Compare(ReturnedEncodedCBOR, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedMap4Indef))) {
10049 return 72;
10050 }
10051
10052
10053 uPosition = QCBORDecode_Tell(&DCtx);
10054 QCBORDecode_GetArrayFromMapSZ(&DCtx,
10055 "an array of two strings",
10056 &Item,
10057 &ReturnedEncodedCBOR);
10058 if(QCBORDecode_GetError(&DCtx)) {
10059 return 80;
10060 }
10061 if(Item.val.uCount != UINT16_MAX) {
10062 return 81;
10063 }
10064 if(UsefulBuf_Compare(ReturnedEncodedCBOR, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedArray2sIndef))) {
10065 return 82;
10066 }
10067 if(uPosition != QCBORDecode_Tell(&DCtx)) {
10068 return 83;
10069 }
10070
10071 QCBORDecode_Rewind(&DCtx);
10072
10073 uPosition = QCBORDecode_Tell(&DCtx);
10074 QCBORDecode_GetMapFromMapSZ(&DCtx, "map in a map", &Item, &ReturnedEncodedCBOR);
10075 if(QCBORDecode_GetError(&DCtx)) {
10076 return 90;
10077 }
10078 if(Item.val.uCount != UINT16_MAX) {
10079 return 91;
10080 }
10081 if(UsefulBuf_Compare(ReturnedEncodedCBOR, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedMap4Indef))) {
10082 return 92;
10083 }
10084 if(uPosition != QCBORDecode_Tell(&DCtx)) {
10085 return 93;
10086 }
10087
10088 uPosition = QCBORDecode_Tell(&DCtx);
10089 QCBORDecode_GetArrayFromMapSZ(&DCtx, "map in a map", &Item, &ReturnedEncodedCBOR);
10090 if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
10091 return 100;
10092 }
10093 if(UINT32_MAX != QCBORDecode_Tell(&DCtx)) {
10094 return 101;
10095 }
10096 QCBORDecode_GetAndResetError(&DCtx);
10097 if(uPosition != QCBORDecode_Tell(&DCtx)) {
10098 return 102;
10099 }
Laurence Lundbladeec290b82024-06-10 11:10:54 -070010100#endif /* ! QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
Laurence Lundbladea29f45a2024-05-14 15:55:19 -070010101
10102 return 0;
10103}
Laurence Lundbladeec290b82024-06-10 11:10:54 -070010104#endif /* ! QCBOR_DISABLE_NON_INTEGER_LABELS */
Laurence Lundbladea29f45a2024-05-14 15:55:19 -070010105
10106
Laurence Lundbladef00b8be2024-03-08 10:34:33 -080010107int32_t
10108ErrorHandlingTests(void)
10109{
10110 QCBORDecodeContext DCtx;
10111 QCBORItem Item;
10112 QCBORError uError;
10113 int64_t integer;
10114
10115 /* Test QCBORDecode_SetError() */
10116 QCBORDecode_Init(&DCtx,
10117 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded),
10118 QCBOR_DECODE_MODE_NORMAL);
10119
10120 QCBORDecode_SetError(&DCtx, QCBOR_ERR_FIRST_USER_DEFINED);
10121
10122 QCBORDecode_VGetNext(&DCtx, &Item);
10123
10124 uError = QCBORDecode_GetError(&DCtx);
10125
10126 if(uError != QCBOR_ERR_FIRST_USER_DEFINED) {
10127 return -1;
10128 }
10129
10130 if(Item.uLabelType != QCBOR_TYPE_NONE ||
10131 Item.uDataType != QCBOR_TYPE_NONE) {
10132 return -2;
10133 }
10134
10135
10136 /* Test data type returned from previous error */
10137 QCBORDecode_Init(&DCtx,
10138 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded),
10139 QCBOR_DECODE_MODE_NORMAL);
10140 QCBORDecode_GetInt64(&DCtx, &integer);
10141 uError = QCBORDecode_GetError(&DCtx);
10142 if(uError != QCBOR_ERR_UNEXPECTED_TYPE) {
10143 return -3;
10144 }
10145
10146 QCBORDecode_VGetNext(&DCtx, &Item);
10147 if(Item.uLabelType != QCBOR_TYPE_NONE ||
10148 Item.uDataType != QCBOR_TYPE_NONE) {
10149 return -2;
10150 }
10151 uError = QCBORDecode_GetError(&DCtx);
10152 if(uError != QCBOR_ERR_UNEXPECTED_TYPE) {
10153 return -3;
10154 }
10155
10156
10157 /* Test error classification functions */
10158
10159 if(!QCBORDecode_IsUnrecoverableError(QCBOR_ERR_INDEFINITE_STRING_CHUNK)) {
10160 return -10;
10161 }
10162 if(QCBORDecode_IsUnrecoverableError(QCBOR_SUCCESS)) {
10163 return -11;
10164 }
10165 if(!QCBORDecode_IsUnrecoverableError(QCBOR_ERR_INDEFINITE_STRING_CHUNK)) {
10166 return -12;
10167 }
10168 if(QCBORDecode_IsUnrecoverableError(QCBOR_ERR_DUPLICATE_LABEL)) {
10169 return -13;
10170 }
10171
10172 if(!QCBORDecode_IsNotWellFormedError(QCBOR_ERR_BAD_TYPE_7)) {
10173 return -20;
10174 }
10175 if(!QCBORDecode_IsNotWellFormedError(QCBOR_ERR_BAD_BREAK)) {
10176 return -21;
10177 }
10178 if(QCBORDecode_IsNotWellFormedError(QCBOR_SUCCESS)) {
10179 return -22;
10180 }
10181 if(QCBORDecode_IsNotWellFormedError(QCBOR_ERR_ARRAY_DECODE_TOO_LONG)) {
10182 return -23;
10183 }
10184
Laurence Lundbladecd34e582024-06-06 10:55:14 -070010185 /* Test error strings */
10186 const char *szErrString;
10187
10188 szErrString = qcbor_err_to_str(QCBOR_ERR_ARRAY_DECODE_TOO_LONG);
10189 if(szErrString == NULL) {
10190 return -100;
10191 }
10192 if(strcmp(szErrString, "QCBOR_ERR_ARRAY_DECODE_TOO_LONG")) {
10193 return -101;
10194 }
10195
10196 szErrString = qcbor_err_to_str(QCBOR_SUCCESS);
10197 if(szErrString == NULL) {
10198 return -102;
10199 }
10200 if(strcmp(szErrString, "QCBOR_SUCCESS")) {
10201 return -103;
10202 }
10203
10204 szErrString = qcbor_err_to_str(100);
10205 if(szErrString == NULL) {
10206 return -104;
10207 }
10208 if(strcmp(szErrString, "Unidentified QCBOR error")) {
10209 return -105;
10210 }
10211
10212 szErrString = qcbor_err_to_str(200);
10213 if(szErrString == NULL) {
10214 return -106;
10215 }
10216 if(strcmp(szErrString, "USER_DEFINED_200")) {
10217 return -107;
10218 }
10219
Laurence Lundbladee2226742024-08-16 10:50:23 -070010220 QCBORDecode_Init(&DCtx,
10221 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded),
10222 QCBOR_DECODE_MODE_NORMAL);
10223
10224 UsefulBufC Xx = QCBORDecode_RetrieveUndecodedInput(&DCtx);
10225 if(Xx.ptr != pValidMapEncoded) {
10226 return -200;
10227 }
10228 if(Xx.len != sizeof(pValidMapEncoded)) {
10229 return -201;
10230 }
10231
Laurence Lundbladef00b8be2024-03-08 10:34:33 -080010232 return 0;
10233}
Laurence Lundblade11654912024-05-09 11:49:24 -070010234
10235
10236int32_t TellTests(void)
10237{
10238 QCBORDecodeContext DCtx;
10239 QCBORItem Item;
10240 uint32_t uPosition;
10241 int nIndex;
10242 int64_t nDecodedInt;
10243
Laurence Lundbladeec290b82024-06-10 11:10:54 -070010244 // Improvement: rewrite so this can run with only integer labels
Laurence Lundblade11654912024-05-09 11:49:24 -070010245 static const uint32_t aPos[] =
Laurence Lundblade535bb5e2024-06-16 12:06:32 -070010246 {0, 1, 17, 42, 50, 58, 72, 85, 98, 112, 151};
Laurence Lundblade11654912024-05-09 11:49:24 -070010247 QCBORDecode_Init(&DCtx,
10248 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded),
10249 0);
10250 for(nIndex = 0; ; nIndex++) {
10251 uPosition = QCBORDecode_Tell(&DCtx);
10252 if(uPosition != aPos[nIndex]) {
10253 return nIndex;
10254 }
10255
Laurence Lundblade535bb5e2024-06-16 12:06:32 -070010256 if(QCBORDecode_EndCheck(&DCtx)) {
Laurence Lundblade11654912024-05-09 11:49:24 -070010257 break;
10258 }
10259
10260 QCBORDecode_VGetNext(&DCtx, &Item);
10261 }
10262
10263#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
10264 static const uint32_t aPosIndef[] =
Laurence Lundblade535bb5e2024-06-16 12:06:32 -070010265 {0, 1, 17, 42, 50, 59, 73, 86, 99, 113, 154};
Laurence Lundblade11654912024-05-09 11:49:24 -070010266 QCBORDecode_Init(&DCtx,
10267 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapIndefEncoded),
10268 0);
10269
10270 for(nIndex = 0; ; nIndex++) {
10271 uPosition = QCBORDecode_Tell(&DCtx);
10272 if(uPosition != aPosIndef[nIndex]) {
10273 return nIndex + 100;
10274 }
10275
Laurence Lundblade535bb5e2024-06-16 12:06:32 -070010276 if(QCBORDecode_EndCheck(&DCtx)) {
Laurence Lundblade11654912024-05-09 11:49:24 -070010277 break;
10278 }
10279
10280 QCBORDecode_VGetNext(&DCtx, &Item);
10281 }
10282#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
10283
10284 /* Next, some tests with entered maps and arrays */
10285 QCBORDecode_Init(&DCtx,
10286 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded),
10287 0);
10288 QCBORDecode_EnterMap(&DCtx, &Item);
10289 if(QCBORDecode_Tell(&DCtx) != 1) {
10290 return 1001;
10291 }
10292 QCBORDecode_GetInt64InMapSZ(&DCtx, "first integer", &nDecodedInt);
10293 if(QCBORDecode_Tell(&DCtx) != 1) {
10294 return 1002;
10295 }
10296 QCBORDecode_EnterMapFromMapSZ(&DCtx, "map in a map");
10297 if(QCBORDecode_Tell(&DCtx) != 72) {
10298 return 1003;
10299 }
10300
10301 QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt);
10302 if(nDecodedInt != 98) {
10303 return 1004;
10304 }
10305 /* Getting non-aggregate types doesn't affect cursor position. */
10306 if(QCBORDecode_Tell(&DCtx) != 72) {
10307 return 1005;
10308 }
10309 QCBORDecode_VGetNext(&DCtx, &Item);
10310 if(QCBORDecode_Tell(&DCtx) != 85) {
10311 return 1006;
10312 }
10313 QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt);
10314 if(nDecodedInt != 98) {
10315 return 1007;
10316 }
10317 /* Getting non-aggregate types doesn't affect cursor position. */
10318 if(QCBORDecode_Tell(&DCtx) != 85) {
10319 return 1008;
10320 }
10321
10322 QCBORDecode_ExitMap(&DCtx);
Laurence Lundblade535bb5e2024-06-16 12:06:32 -070010323 if(QCBORDecode_Tell(&DCtx) != 151) {
Laurence Lundblade11654912024-05-09 11:49:24 -070010324 return 1009;
10325 }
10326 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_NO_MORE_ITEMS) {
10327 return 1010;
10328 }
10329
10330#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
10331 /* Next, some tests with entered maps and arrays */
10332 QCBORDecode_Init(&DCtx,
10333 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapIndefEncoded),
10334 0);
10335 QCBORDecode_EnterMap(&DCtx, &Item);
10336 if(QCBORDecode_Tell(&DCtx) != 1) {
10337 return 2000;
10338 }
10339 QCBORDecode_GetInt64InMapSZ(&DCtx, "first integer", &nDecodedInt);
10340 if(QCBORDecode_Tell(&DCtx) != 1) {
10341 return 2001;
10342 }
10343 QCBORDecode_EnterMapFromMapSZ(&DCtx, "map in a map");
10344 if(QCBORDecode_Tell(&DCtx) != 73) {
10345 return 2002;
10346 }
10347
10348 QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt);
10349 if(nDecodedInt != 98) {
10350 return 2003;
10351 }
10352 /* Getting non-aggregate types doesn't affect cursor position. */
10353 if(QCBORDecode_Tell(&DCtx) != 73) {
10354 return 2004;
10355 }
10356 QCBORDecode_VGetNext(&DCtx, &Item);
10357 if(QCBORDecode_Tell(&DCtx) != 86) {
10358 return 2005;
10359 }
10360 QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt);
10361 if(nDecodedInt != 98) {
10362 return 2006;
10363 }
10364 /* Getting non-aggregate types doesn't affect cursor position. */
10365 if(QCBORDecode_Tell(&DCtx) != 86) {
10366 return 2007;
10367 }
10368
10369 QCBORDecode_ExitMap(&DCtx);
Laurence Lundblade535bb5e2024-06-16 12:06:32 -070010370 if(QCBORDecode_Tell(&DCtx) != 154) {
Laurence Lundblade11654912024-05-09 11:49:24 -070010371 return 2008;
10372 }
10373 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_NO_MORE_ITEMS) {
10374 return 2010;
10375 }
10376#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
10377
10378
10379
10380 /* Error state test */
10381 QCBORDecode_Init(&DCtx,
10382 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded),
10383 0);
10384 /* Cause an error */
10385 QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt);
10386 if(QCBORDecode_Tell(&DCtx) != UINT32_MAX) {
10387 return 3000;
10388 }
Laurence Lundblade535bb5e2024-06-16 12:06:32 -070010389 if(QCBORDecode_EndCheck(&DCtx) != QCBOR_ERR_MAP_NOT_ENTERED) {
10390 return 3001;
10391 }
10392
Laurence Lundblade11654912024-05-09 11:49:24 -070010393
10394 /* Empties tests */
10395 const uint8_t pMinimalCBOR[] = {0xa0}; // One empty map
10396 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pMinimalCBOR),0);
10397 if(QCBORDecode_Tell(&DCtx) != 0) {
10398 return 4000;
10399 }
Laurence Lundblade535bb5e2024-06-16 12:06:32 -070010400 if(QCBORDecode_EndCheck(&DCtx) != QCBOR_SUCCESS) {
10401 return 4008;
10402 }
Laurence Lundblade11654912024-05-09 11:49:24 -070010403 QCBORDecode_EnterMap(&DCtx, &Item);
10404 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) {
10405 return 4001;
10406 }
Laurence Lundblade535bb5e2024-06-16 12:06:32 -070010407 if(QCBORDecode_Tell(&DCtx) != 1) {
Laurence Lundblade11654912024-05-09 11:49:24 -070010408 return 4002;
10409 }
10410 QCBORDecode_ExitMap(&DCtx);
10411 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) {
Laurence Lundblade535bb5e2024-06-16 12:06:32 -070010412 return 4003;
Laurence Lundblade11654912024-05-09 11:49:24 -070010413 }
Laurence Lundblade535bb5e2024-06-16 12:06:32 -070010414 if(QCBORDecode_Tell(&DCtx) != 1) {
10415 return 4004;
10416 }
10417 if(QCBORDecode_EndCheck(&DCtx) != QCBOR_ERR_NO_MORE_ITEMS) {
10418 return 4005;
Laurence Lundblade11654912024-05-09 11:49:24 -070010419 }
10420 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_NO_MORE_ITEMS) {
10421 return 4010;
10422 }
10423
10424#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
10425 const uint8_t pMinimalIndefCBOR[] = {0xbf, 0xff}; // One empty map
10426 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pMinimalIndefCBOR),0);
10427 if(QCBORDecode_Tell(&DCtx) != 0) {
10428 return 4100;
10429 }
10430 QCBORDecode_EnterMap(&DCtx, &Item);
10431 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) {
10432 return 4101;
10433 }
Laurence Lundblade535bb5e2024-06-16 12:06:32 -070010434 if(QCBORDecode_Tell(&DCtx) != 2) {
Laurence Lundblade11654912024-05-09 11:49:24 -070010435 return 4102;
10436 }
10437 QCBORDecode_ExitMap(&DCtx);
10438 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) {
Laurence Lundblade535bb5e2024-06-16 12:06:32 -070010439 return 4103;
Laurence Lundblade11654912024-05-09 11:49:24 -070010440 }
Laurence Lundblade535bb5e2024-06-16 12:06:32 -070010441 if(QCBORDecode_Tell(&DCtx) != 2) {
10442 return 4104;
10443 }
10444 if(QCBORDecode_EndCheck(&DCtx) != QCBOR_ERR_NO_MORE_ITEMS) {
10445 return 4005;
Laurence Lundblade11654912024-05-09 11:49:24 -070010446 }
10447 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_NO_MORE_ITEMS) {
10448 return 4110;
10449 }
10450#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
10451
10452 /* Test on a CBOR sequence */
10453 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSequenceTestInput),0);
10454 if(QCBORDecode_Tell(&DCtx) != 0) {
10455 return 5000;
10456 }
10457 QCBORDecode_VGetNext(&DCtx, &Item);
10458 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) {
10459 return 5001;
10460 }
10461 if(QCBORDecode_Tell(&DCtx) != 11) {
10462 return 5002;
10463 }
10464 QCBORDecode_VGetNext(&DCtx, &Item);
10465 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) {
10466 return 5003;
10467 }
10468 if(QCBORDecode_Tell(&DCtx) != 12) {
10469 return 5004;
10470 }
10471 QCBORDecode_VGetNext(&DCtx, &Item);
10472 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) {
10473 return 5005;
10474 }
10475 if(QCBORDecode_Tell(&DCtx) != 17) {
10476 return 5006;
10477 }
10478 QCBORDecode_VGetNext(&DCtx, &Item);
10479 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) {
10480 return 5007;
10481 }
Laurence Lundblade535bb5e2024-06-16 12:06:32 -070010482 if(QCBORDecode_Tell(&DCtx) != 20) {
Laurence Lundblade11654912024-05-09 11:49:24 -070010483 return 5008;
10484 }
10485 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_NO_MORE_ITEMS) {
10486 return 5010;
10487 }
10488
10489
10490 QCBORDecode_Init(&DCtx,
10491 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded),
10492 0);
10493 QCBORDecode_EnterMap(&DCtx, &Item);
10494 QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings");
10495 if(QCBORDecode_Tell(&DCtx) != 42) {
10496 return 6001;
10497 }
10498 QCBORDecode_VGetNext(&DCtx, &Item);
10499 if(QCBORDecode_Tell(&DCtx) != 50) {
10500 return 6002;
10501 }
10502 QCBORDecode_VGetNext(&DCtx, &Item);
10503 if(QCBORDecode_Tell(&DCtx) != 58) {
10504 return 6008;
10505 }
10506 QCBORDecode_VGetNext(&DCtx, &Item);
10507 (void)QCBORDecode_GetAndResetError(&DCtx);
10508 if(QCBORDecode_Tell(&DCtx) != 58) {
10509 return 6003;
10510 }
10511 QCBORDecode_ExitArray(&DCtx);
10512 if(QCBORDecode_Tell(&DCtx) != 58) {
10513 return 6004;
10514 }
10515
10516 static const uint32_t aEmptiesPos[] =
Laurence Lundblade535bb5e2024-06-16 12:06:32 -070010517 {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 13, 15};
Laurence Lundblade11654912024-05-09 11:49:24 -070010518 QCBORDecode_Init(&DCtx,
10519 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(sEmpties),
10520 0);
10521 for(nIndex = 0; ; nIndex++) {
10522 uPosition = QCBORDecode_Tell(&DCtx);
10523 if(uPosition != aEmptiesPos[nIndex]) {
10524 return nIndex + 200;
10525 }
10526
Laurence Lundblade535bb5e2024-06-16 12:06:32 -070010527 if(QCBORDecode_EndCheck(&DCtx)) {
Laurence Lundblade11654912024-05-09 11:49:24 -070010528 break;
10529 }
10530
10531 QCBORDecode_VGetNext(&DCtx, &Item);
10532 }
10533
10534#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
10535 static const uint32_t aIndefEmptiesPos[] =
Laurence Lundblade535bb5e2024-06-16 12:06:32 -070010536 {0, 1, 2, 4, 5, 7, 8, 10, 12, 13, 16, 19, 25};
Laurence Lundblade11654912024-05-09 11:49:24 -070010537 QCBORDecode_Init(&DCtx,
10538 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(sEmptiesIndef),
10539 0);
10540 for(nIndex = 0; ; nIndex++) {
10541 uPosition = QCBORDecode_Tell(&DCtx);
10542 if(uPosition != aIndefEmptiesPos[nIndex]) {
10543 return nIndex + 300;
10544 }
10545
Laurence Lundblade535bb5e2024-06-16 12:06:32 -070010546 if(QCBORDecode_EndCheck(&DCtx)) {
Laurence Lundblade11654912024-05-09 11:49:24 -070010547 break;
10548 }
10549
10550 QCBORDecode_VGetNext(&DCtx, &Item);
10551 }
10552#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
10553
10554
10555 return 0;
10556}