blob: 6d3cc9a0e48408e17d79e274ee5cb7655bd24927 [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 Lundbladec4bded32024-10-09 10:34:39 -07004109static const uint8_t spTaggedString[] = {
4110 0xd8, 0xf0, 0x61, 0x40,
4111};
4112
4113static const uint8_t spTaggedInt[] = {
4114 0xd8, 0xf4, 0x01,
4115};
4116
Laurence Lundbladec5fef682020-01-25 11:38:45 -08004117static int32_t CheckCSRMaps(QCBORDecodeContext *pDC);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004118
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004119
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03004120int32_t OptTagParseTest(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004121{
4122 QCBORDecodeContext DCtx;
Laurence Lundblade9b334962020-08-27 10:55:53 -07004123 QCBORItem Item;
4124 QCBORError uError;
Laurence Lundbladec4bded32024-10-09 10:34:39 -07004125 UsefulBufC UBC;
4126 int64_t nInt;
4127
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004128
Laurence Lundbladeee851742020-01-08 08:37:05 -08004129 QCBORDecode_Init(&DCtx,
Laurence Lundblade9b334962020-08-27 10:55:53 -07004130 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTagInput),
Laurence Lundbladeee851742020-01-08 08:37:05 -08004131 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004132
Laurence Lundblade9b334962020-08-27 10:55:53 -07004133 /*
4134 This test matches the magic number tag and the fraction tag
4135 55799([...])
4136 */
4137 uError = QCBORDecode_GetNext(&DCtx, &Item);
4138 if(uError != QCBOR_SUCCESS) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004139 return -2;
4140 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004141 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004142 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC)) {
4143 return -3;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004144 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004145
Laurence Lundblade9b334962020-08-27 10:55:53 -07004146 /*
4147 4([1,3])
4148 */
4149 uError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundbladedd6e76e2021-03-10 01:54:01 -07004150#ifdef QCBOR_DISABLE_EXP_AND_MANTISSA
Laurence Lundblade9b334962020-08-27 10:55:53 -07004151 if(uError != QCBOR_SUCCESS ||
4152 Item.uDataType != QCBOR_TYPE_ARRAY ||
4153 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_DECIMAL_FRACTION) ||
4154 QCBORDecode_GetNthTag(&DCtx, &Item, 0) != CBOR_TAG_DECIMAL_FRACTION ||
4155 QCBORDecode_GetNthTag(&DCtx, &Item, 1) != CBOR_TAG_INVALID64 ||
4156 QCBORDecode_GetNthTag(&DCtx, &Item, 2) != CBOR_TAG_INVALID64 ||
4157 QCBORDecode_GetNthTag(&DCtx, &Item, 3) != CBOR_TAG_INVALID64 ||
4158 QCBORDecode_GetNthTag(&DCtx, &Item, 4) != CBOR_TAG_INVALID64 ||
4159 Item.val.uCount != 2) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004160 return -4;
4161 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07004162 // consume the items in the array
4163 uError = QCBORDecode_GetNext(&DCtx, &Item);
4164 uError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundblade59289e52019-12-30 13:44:37 -08004165
Laurence Lundbladedd6e76e2021-03-10 01:54:01 -07004166#else /* QCBOR_DISABLE_EXP_AND_MANTISSA */
Laurence Lundblade9b334962020-08-27 10:55:53 -07004167 if(uError != QCBOR_SUCCESS ||
4168 Item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION ||
4169 QCBORDecode_GetNthTag(&DCtx, &Item, 0) != CBOR_TAG_INVALID64 ||
4170 QCBORDecode_GetNthTag(&DCtx, &Item, 1) != CBOR_TAG_INVALID64 ||
4171 QCBORDecode_GetNthTag(&DCtx, &Item, 2) != CBOR_TAG_INVALID64 ||
4172 QCBORDecode_GetNthTag(&DCtx, &Item, 3) != CBOR_TAG_INVALID64 ||
4173 QCBORDecode_GetNthTag(&DCtx, &Item, 4) != CBOR_TAG_INVALID64 ) {
4174 return -5;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004175 }
Laurence Lundbladedd6e76e2021-03-10 01:54:01 -07004176#endif /* QCBOR_DISABLE_EXP_AND_MANTISSA */
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004177
Laurence Lundblade9b334962020-08-27 10:55:53 -07004178 /*
4179 More than 4 tags on an item 225(226(227(228(229([])))))
4180 */
4181 uError = QCBORDecode_GetNext(&DCtx, &Item);
4182 if(uError != QCBOR_ERR_TOO_MANY_TAGS) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004183 return -6;
4184 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07004185
Laurence Lundblade88e9db22020-11-02 03:56:33 -08004186 if(QCBORDecode_GetNthTag(&DCtx, &Item, 0) != CBOR_TAG_INVALID64) {
4187 return -106;
4188 }
4189
4190
Laurence Lundblade9b334962020-08-27 10:55:53 -07004191 /* tag 10489608748473423768(
4192 2442302356(
4193 21590(
4194 240(
4195 []))))
4196 */
4197 uError = QCBORDecode_GetNext(&DCtx, &Item);
4198 if(uError != QCBOR_SUCCESS ||
4199 Item.uDataType != QCBOR_TYPE_ARRAY ||
4200 QCBORDecode_GetNthTag(&DCtx, &Item, 3) != 10489608748473423768ULL ||
4201 QCBORDecode_GetNthTag(&DCtx, &Item, 2) != 2442302356ULL ||
4202 QCBORDecode_GetNthTag(&DCtx, &Item, 1) != 21590ULL ||
4203 QCBORDecode_GetNthTag(&DCtx, &Item, 0) != 240ULL) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004204 return -7;
Laurence Lundblade9b334962020-08-27 10:55:53 -07004205 }
4206
4207 /* tag 21590(
4208 10489608748473423768(
4209 2442302357(
4210 21591(
4211 []))))
4212 */
4213 uError = QCBORDecode_GetNext(&DCtx, &Item);
4214 if(uError != QCBOR_SUCCESS ||
4215 Item.uDataType != QCBOR_TYPE_ARRAY ||
4216 QCBORDecode_GetNthTag(&DCtx, &Item, 0) != 65534ULL ||
4217 QCBORDecode_GetNthTag(&DCtx, &Item, 1) != 2442302357ULL ||
4218 QCBORDecode_GetNthTag(&DCtx, &Item, 2) != 10489608748473423768ULL ||
4219 QCBORDecode_GetNthTag(&DCtx, &Item, 3) != 21590ULL) {
4220 return -8;
4221 }
4222
4223 /* Make sure to blow past the limit of tags that must be mapped.
4224 works in conjuntion with entries above.
4225 269488144(269488145(269488146(269488147([]))))
4226 */
4227 uError = QCBORDecode_GetNext(&DCtx, &Item);
4228 if(uError != QCBOR_ERR_TOO_MANY_TAGS) {
4229 return -9;
4230 }
4231
4232 uError = QCBORDecode_GetNext(&DCtx, &Item);
4233 if(uError == QCBOR_SUCCESS) {
4234 return -10;
4235 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004236
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004237 // ----------------------------------
Laurence Lundbladeee851742020-01-08 08:37:05 -08004238 // This test sets up a caller-config list that includes the very large
Laurence Lundblade9b334962020-08-27 10:55:53 -07004239 // tage and then matches it. Caller-config lists are no longer
4240 // used or needed. This tests backwards compatibility with them.
Laurence Lundbladeee851742020-01-08 08:37:05 -08004241 QCBORDecode_Init(&DCtx,
4242 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEncodedLargeTag),
4243 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004244 const uint64_t puList[] = {0x9192939495969798, 257};
4245 const QCBORTagListIn TL = {2, puList};
4246 QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004247
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004248 if(QCBORDecode_GetNext(&DCtx, &Item)) {
4249 return -8;
4250 }
4251 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
4252 !QCBORDecode_IsTagged(&DCtx, &Item, 0x9192939495969798) ||
4253 QCBORDecode_IsTagged(&DCtx, &Item, 257) ||
4254 QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_BIGFLOAT) ||
4255 Item.val.uCount != 0) {
4256 return -9;
4257 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004258
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004259 //------------------------
Laurence Lundbladeee851742020-01-08 08:37:05 -08004260 // Sets up a caller-configured list and look up something not in it
Laurence Lundblade9b334962020-08-27 10:55:53 -07004261 // Another backwards compatibility test.
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004262 const uint64_t puLongList[17] = {1,2,1};
4263 const QCBORTagListIn TLLong = {17, puLongList};
Laurence Lundbladeee851742020-01-08 08:37:05 -08004264 QCBORDecode_Init(&DCtx,
4265 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEncodedLargeTag),
4266 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004267 QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TLLong);
4268 if(QCBORDecode_GetNext(&DCtx, &Item)) {
4269 return -11;
4270 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004271
Laurence Lundbladef7a70bc2020-10-24 12:23:25 -07004272 uint64_t puTags[4];
Laurence Lundblade9b334962020-08-27 10:55:53 -07004273 QCBORTagListOut Out = {0, 4, puTags};
4274
4275
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004276 // This tests retrievel of the full tag list
Laurence Lundbladeee851742020-01-08 08:37:05 -08004277 QCBORDecode_Init(&DCtx,
4278 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spLotsOfTags),
4279 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004280 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
4281 return -12;
4282 }
4283 if(puTags[0] != 0x9192939495969798 ||
4284 puTags[1] != 0x88 ||
Laurence Lundblade59289e52019-12-30 13:44:37 -08004285 puTags[2] != 0x06 ||
4286 puTags[3] != 0x07) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004287 return -13;
4288 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004289
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004290 // ----------------------
Laurence Lundblade9b334962020-08-27 10:55:53 -07004291 // This tests too small of an out list
Laurence Lundbladeee851742020-01-08 08:37:05 -08004292 QCBORDecode_Init(&DCtx,
4293 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spLotsOfTags),
4294 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004295 QCBORTagListOut OutSmall = {0, 3, puTags};
4296 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &OutSmall) != QCBOR_ERR_TOO_MANY_TAGS) {
4297 return -14;
4298 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004299
Laurence Lundblade9b334962020-08-27 10:55:53 -07004300
4301
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004302 // ---------------
Laurence Lundblade9b334962020-08-27 10:55:53 -07004303 // Decode a version of the "CSR" that has had a ton of tags randomly inserted
4304 // It is a bit of a messy test and maybe could be improved, but
4305 // it is retained as a backwards compatibility check.
Laurence Lundbladeee851742020-01-08 08:37:05 -08004306 QCBORDecode_Init(&DCtx,
4307 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRWithTags),
4308 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004309 int n = CheckCSRMaps(&DCtx);
4310 if(n) {
4311 return n-2000;
4312 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004313
Laurence Lundblade59289e52019-12-30 13:44:37 -08004314 Out = (QCBORTagListOut){0, 16, puTags};
Laurence Lundbladeee851742020-01-08 08:37:05 -08004315 QCBORDecode_Init(&DCtx,
4316 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRWithTags),
4317 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004318
Laurence Lundblade9b334962020-08-27 10:55:53 -07004319 /* With the spiffy decode revision, this tag list is not used.
4320 It doesn't matter if a tag is in this list or not so some
4321 tests that couldn't process a tag because it isn't in this list
4322 now can process these unlisted tags. The tests have been
4323 adjusted for this. */
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004324 const uint64_t puTagList[] = {773, 1, 90599561};
4325 const QCBORTagListIn TagList = {3, puTagList};
4326 QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TagList);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004327
4328
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004329 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
4330 return -100;
4331 }
4332 if(Item.uDataType != QCBOR_TYPE_MAP ||
4333 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC) ||
4334 QCBORDecode_IsTagged(&DCtx, &Item, 90599561) ||
4335 QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_DATE_EPOCH) ||
4336 Item.val.uCount != 2 ||
4337 puTags[0] != CBOR_TAG_CBOR_MAGIC ||
4338 puTags[1] != CBOR_TAG_CBOR_MAGIC ||
4339 puTags[2] != CBOR_TAG_CBOR_MAGIC ||
4340 Out.uNumUsed != 3) {
4341 return -101;
4342 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004343
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004344 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
4345 return -102;
4346 }
4347 if(Item.uDataType != QCBOR_TYPE_MAP ||
4348 QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC) ||
4349 QCBORDecode_IsTagged(&DCtx, &Item, 6) ||
Laurence Lundbladef7a70bc2020-10-24 12:23:25 -07004350 !QCBORDecode_IsTagged(&DCtx, &Item, 7) ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004351 Item.val.uCount != 2 ||
4352 puTags[0] != 5859837686836516696 ||
4353 puTags[1] != 7 ||
4354 Out.uNumUsed != 2) {
4355 return -103;
4356 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004357
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004358 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
4359 return -104;
4360 }
4361 if(Item.uDataType != QCBOR_TYPE_MAP ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004362 Item.val.uCount != 5 ||
4363 puTags[0] != 0x0b ||
4364 Out.uNumUsed != 1) {
4365 return -105;
4366 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004367
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004368 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
4369 return -106;
4370 }
4371 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
4372 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_COSE_MAC0) ||
4373 Item.val.string.len != 12 ||
4374 puTags[0] != CBOR_TAG_COSE_MAC0 ||
4375 puTags[1] != CBOR_TAG_COSE_MAC0 ||
4376 puTags[2] != CBOR_TAG_COSE_MAC0 ||
4377 Out.uNumUsed != 3) {
4378 return -105;
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 -107;
4383 }
4384 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
4385 !QCBORDecode_IsTagged(&DCtx, &Item, 773) ||
4386 Item.val.string.len != 3 ||
4387 puTags[0] != 773 ||
4388 Out.uNumUsed != 1) {
4389 return -108;
4390 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004391
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004392 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
4393 return -109;
4394 }
4395 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade59289e52019-12-30 13:44:37 -08004396 !QCBORDecode_IsTagged(&DCtx, &Item, 16) ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004397 Item.val.string.len != 9 ||
Laurence Lundblade59289e52019-12-30 13:44:37 -08004398 puTags[0] != 16 ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07004399 puTags[3] != 7 ||
4400 Out.uNumUsed != 4) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004401 return -110;
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 != 9 ||
4410 puTags[0] != 17 ||
4411 Out.uNumUsed != 1) {
4412 return -112;
4413 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004414
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004415 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
4416 return -111;
4417 }
4418 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
4419 !QCBORDecode_IsTagged(&DCtx, &Item, 17) ||
4420 Item.val.string.len != 2 ||
4421 puTags[0] != 17 ||
4422 Out.uNumUsed != 1) {
4423 return -112;
4424 }
4425
4426 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
4427 return -113;
4428 }
4429 if(Item.uDataType != QCBOR_TYPE_MAP ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07004430 !QCBORDecode_IsTagged(&DCtx, &Item, 19) ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004431 Item.val.uCount != 2 ||
4432 puTags[0] != 19 ||
4433 Out.uNumUsed != 1) {
4434 return -114;
4435 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004436
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004437 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
4438 return -115;
4439 }
4440 if(Item.uDataType != QCBOR_TYPE_MAP ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07004441 !QCBORDecode_IsTagged(&DCtx, &Item, 9) ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004442 Item.val.uCount != 1 ||
4443 puTags[0] != 9 ||
4444 Out.uNumUsed != 1) {
4445 return -116;
4446 }
4447
4448 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
4449 return -116;
4450 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004451 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004452 Item.val.int64 != -7 ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004453 Out.uNumUsed != 0) {
4454 return -117;
4455 }
4456
4457 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
4458 return -118;
4459 }
4460 if(Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
4461 Item.val.string.len != 10 ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004462 puTags[0] != 12 ||
4463 Out.uNumUsed != 1) {
4464 return -119;
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 -120;
4469 }
4470 if(Item.uDataType != QCBOR_TYPE_MAP ||
4471 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_ENC_AS_B16) ||
4472 Item.val.uCount != 1 ||
4473 puTags[0] != 0x17 ||
4474 Out.uNumUsed != 1) {
4475 return -121;
4476 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004477
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004478 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
4479 return -122;
4480 }
4481 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07004482 !QCBORDecode_IsTagged(&DCtx, &Item, 8) ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004483 Item.val.int64 != -3 ||
4484 puTags[0] != 8 ||
4485 Out.uNumUsed != 1) {
4486 return -123;
4487 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004488
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004489 if(QCBORDecode_Finish(&DCtx)) {
4490 return -124;
4491 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07004492
4493 UsefulBufC DateString;
4494 QCBORDecode_Init(&DCtx,
4495 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSpiffyTagInput),
4496 QCBOR_DECODE_MODE_NORMAL);
4497
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07004498 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade9b334962020-08-27 10:55:53 -07004499 // tagged date string
4500 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString);
4501 // untagged date string
4502 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString);
4503 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_SUCCESS) {
4504 return 100;
4505 }
4506 // untagged byte string
4507 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString);
4508 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
4509 return 101;
4510 }
4511 // tagged regex
4512 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString);
4513 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
4514 return 102;
4515 }
4516 // tagged date string with a byte string
4517 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString);
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07004518 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT) {
Laurence Lundblade9b334962020-08-27 10:55:53 -07004519 return 103;
4520 }
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07004521 // The exit errors out because the last item, the date string with
4522 // bad content makes the array untraversable (the bad date string
4523 // could have tag content of an array or such that is not consumed
4524 // by the date decoding).
Laurence Lundblade9b334962020-08-27 10:55:53 -07004525 QCBORDecode_ExitArray(&DCtx);
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07004526 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT) {
Laurence Lundblade9b334962020-08-27 10:55:53 -07004527 return 104;
4528 }
4529
4530
4531 QCBORDecode_Init(&DCtx,
Laurence Lundbladecdbbc192024-06-28 15:13:04 -07004532 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRWithTags),
4533 QCBOR_DECODE_MODE_NORMAL);
4534 QCBORDecode_EnterMap(&DCtx, NULL);
4535 if(QCBORDecode_GetNthTagOfLast(&DCtx, 0) != 55799) {
4536 return 200;
4537 }
4538 if(QCBORDecode_GetNthTagOfLast(&DCtx, 1) != 55799) {
4539 return 202;
4540 }
4541 if(QCBORDecode_GetNthTagOfLast(&DCtx, 2) != 55799) {
4542 return 203;
4543 }
4544 if(QCBORDecode_GetNthTagOfLast(&DCtx, 3) != CBOR_TAG_INVALID64) {
4545 return 204;
4546 }
4547
4548 QCBORDecode_EnterMap(&DCtx, NULL);
4549 if(QCBORDecode_GetNthTagOfLast(&DCtx, 0) != 7) {
4550 return 210;
4551 }
4552 if(QCBORDecode_GetNthTagOfLast(&DCtx, 1) != 5859837686836516696) {
4553 return 212;
4554 }
4555 if(QCBORDecode_GetNthTagOfLast(&DCtx, 2) != CBOR_TAG_INVALID64) {
4556 return 213;
4557 }
4558 if(QCBORDecode_GetNthTagOfLast(&DCtx, 3) != CBOR_TAG_INVALID64) {
4559 return 214;
4560 }
4561
4562
4563 QCBORDecode_Init(&DCtx,
4564 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRWithTags),
4565 QCBOR_DECODE_MODE_NORMAL);
4566 QCBORDecode_EnterMap(&DCtx, NULL);
4567 QCBORDecode_EnterMapFromMapN(&DCtx, -23);
4568 if(QCBORDecode_GetNthTagOfLast(&DCtx, 0) != 7) {
4569 return 220;
4570 }
4571 if(QCBORDecode_GetNthTagOfLast(&DCtx, 1) != 5859837686836516696) {
4572 return 221;
4573 }
4574 if(QCBORDecode_GetNthTagOfLast(&DCtx, 2) != CBOR_TAG_INVALID64) {
4575 return 222;
4576 }
4577
4578#ifndef QCBOR_DISABLE_NON_INTEGER_LABELS
4579 QCBORDecode_Init(&DCtx,
4580 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRWithTags),
4581 QCBOR_DECODE_MODE_MAP_AS_ARRAY);
4582 QCBORDecode_EnterArray(&DCtx, NULL);
4583 if(QCBORDecode_GetNthTagOfLast(&DCtx, 0) != 55799) {
4584 return 230;
4585 }
4586 if(QCBORDecode_GetNthTagOfLast(&DCtx, 1) != 55799) {
4587 return 231;
4588 }
4589 if(QCBORDecode_GetNthTagOfLast(&DCtx, 2) != 55799) {
4590 return 232;
4591 }
4592 if(QCBORDecode_GetNthTagOfLast(&DCtx, 3) != CBOR_TAG_INVALID64) {
4593 return 234;
4594 }
Laurence Lundbladecdbbc192024-06-28 15:13:04 -07004595 QCBORDecode_GetInt64(&DCtx, &nInt);
4596 if(QCBORDecode_GetNthTagOfLast(&DCtx, 0) != 7) {
4597 return 240;
4598 }
4599 if(QCBORDecode_GetNthTagOfLast(&DCtx, 1) != 6) {
4600 return 241;
4601 }
4602 if(QCBORDecode_GetNthTagOfLast(&DCtx, 2) != CBOR_TAG_INVALID64) {
4603 return 242;
4604 }
4605 if(QCBORDecode_GetNthTagOfLast(&DCtx, 3) != CBOR_TAG_INVALID64) {
4606 return 243;
4607 }
4608#endif /* ! QCBOR_DISABLE_NON_INTEGER_LABELS */
4609
Laurence Lundbladec4bded32024-10-09 10:34:39 -07004610
4611
Laurence Lundbladecdbbc192024-06-28 15:13:04 -07004612 QCBORDecode_Init(&DCtx,
Laurence Lundblade9b334962020-08-27 10:55:53 -07004613 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSpiffyTagInput),
4614 QCBOR_DECODE_MODE_NORMAL);
4615
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07004616 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade9b334962020-08-27 10:55:53 -07004617 // tagged date string
4618 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString);
4619 // untagged date string
4620 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString);
4621 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_SUCCESS) {
Laurence Lundbladecdbbc192024-06-28 15:13:04 -07004622 return 250;
Laurence Lundblade9b334962020-08-27 10:55:53 -07004623 }
4624 // untagged byte string
4625 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString);
4626 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
Laurence Lundbladecdbbc192024-06-28 15:13:04 -07004627 return 251;
Laurence Lundblade9b334962020-08-27 10:55:53 -07004628 }
4629 // tagged regex
4630 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString);
4631 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
Laurence Lundbladecdbbc192024-06-28 15:13:04 -07004632 return 252;
Laurence Lundblade9b334962020-08-27 10:55:53 -07004633 }
4634 // tagged date string with a byte string
4635 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString);
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07004636 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT) {
Laurence Lundbladecdbbc192024-06-28 15:13:04 -07004637 return 253;
Laurence Lundblade9b334962020-08-27 10:55:53 -07004638 }
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07004639 // See comments above
Laurence Lundblade9b334962020-08-27 10:55:53 -07004640 QCBORDecode_ExitArray(&DCtx);
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07004641 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT) {
Laurence Lundbladecdbbc192024-06-28 15:13:04 -07004642 return 254;
Laurence Lundblade9b334962020-08-27 10:55:53 -07004643 }
4644
4645 QCBORDecode_Init(&DCtx,
4646 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSpiffyTagInput),
4647 QCBOR_DECODE_MODE_NORMAL);
4648
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07004649 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade9b334962020-08-27 10:55:53 -07004650 // tagged date string
4651 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString);
4652 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
4653 return 300;
4654 }
4655 // untagged date string
4656 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString);
4657 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
4658 return 301;
4659 }
4660 // untagged byte string
4661 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString);
4662 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
4663 return 302;
4664 }
4665 // tagged regex
4666 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString);
4667 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
4668 return 303;
4669 }
4670 // tagged date string with a byte string
4671 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString);
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07004672 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT) {
Laurence Lundblade9b334962020-08-27 10:55:53 -07004673 return 304;
4674 }
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07004675 // See comments above
Laurence Lundblade9b334962020-08-27 10:55:53 -07004676 QCBORDecode_ExitArray(&DCtx);
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07004677 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT) {
Laurence Lundblade9b334962020-08-27 10:55:53 -07004678 return 305;
4679 }
4680
Laurence Lundbladec4bded32024-10-09 10:34:39 -07004681 QCBORDecode_Init(&DCtx,
4682 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTaggedString),
4683 QCBOR_DECODE_MODE_NORMAL);
4684
4685 /* See that QCBORDecode_GetTextString() ignores tags */
4686 QCBORDecode_GetTextString(&DCtx, &UBC);
4687 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) {
4688 return 400;
4689 }
4690 if(UBC.len != 1) {
4691 return 401;
4692 }
4693
4694 uint64_t uTagNumber = QCBORDecode_GetNthTagOfLast(&DCtx, 0);
4695 if(uTagNumber != 240) {
4696 return 404;
4697 }
4698
4699
4700 QCBORDecode_Init(&DCtx,
4701 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTaggedInt),
4702 QCBOR_DECODE_MODE_NORMAL);
4703 /* See that QCBORDecode_GetInt64() ignores tags */
4704 QCBORDecode_GetInt64(&DCtx, &nInt);
4705 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) {
4706 return 410;
4707 }
4708 if(nInt != 1) {
4709 return 411;
4710 }
4711
4712 uTagNumber = QCBORDecode_GetNthTagOfLast(&DCtx, 0);
4713 if(uTagNumber != 244) {
4714 return 414;
4715 }
4716
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004717 return 0;
4718}
4719
Laurence Lundblade37286c02022-09-03 10:05:02 -07004720/*
4721 * These are showing the big numbers converted to integers.
4722 * The tag numbers are not shown.
4723 *
Laurence Lundbladeec290b82024-06-10 11:10:54 -07004724 * [
4725 * 18446744073709551616,
4726 * -18446744073709551617,
4727 * {
4728 * -64: -18446744073709551617,
4729 * 64: 18446744073709551616,
4730 * "BN+": 18446744073709551616,
4731 * "BN-": -18446744073709551617
Laurence Lundblade37286c02022-09-03 10:05:02 -07004732 * }
4733 * ]
4734 */
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004735
Laurence Lundbladecc7da412020-12-27 00:09:07 -08004736static const uint8_t spBigNumInput[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004737 0x83,
4738 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
4739 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
4740 0xA4,
Laurence Lundbladeec290b82024-06-10 11:10:54 -07004741 0x38, 0x3F,
4742 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004743 0x18, 0x40,
4744 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
Laurence Lundbladeec290b82024-06-10 11:10:54 -07004745 0x63, 0x42, 0x4E, 0x2B,
4746 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
4747 0x63, 0x42, 0x4E, 0x2D,
4748 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
4749};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004750
Laurence Lundblade37286c02022-09-03 10:05:02 -07004751#ifndef QCBOR_DISABLE_TAGS
Laurence Lundbladecc7da412020-12-27 00:09:07 -08004752/* The expected big num */
4753static const uint8_t spBigNum[] = {
4754 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
4755 0x00};
Laurence Lundblade37286c02022-09-03 10:05:02 -07004756#endif /* QCBOR_DISABLE_TAGS */
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004757
4758
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03004759int32_t BignumParseTest(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004760{
4761 QCBORDecodeContext DCtx;
4762 QCBORItem Item;
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07004763 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004764
Laurence Lundbladeee851742020-01-08 08:37:05 -08004765 QCBORDecode_Init(&DCtx,
4766 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNumInput),
4767 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004768
4769
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004770 //
4771 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
4772 return -1;
4773 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade830fbf92020-05-31 17:22:33 -07004774 return -2;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004775 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004776
Laurence Lundblade37286c02022-09-03 10:05:02 -07004777#ifndef QCBOR_DISABLE_TAGS
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004778 //
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004779 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07004780 return -3;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004781 if(Item.uDataType != QCBOR_TYPE_POSBIGNUM ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07004782 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07004783 return -4;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004784 }
4785
4786 //
4787 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07004788 return -5;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004789 if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07004790 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07004791 return -6;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004792 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004793
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004794 //
4795 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07004796 return -7;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004797 if(Item.uDataType != QCBOR_TYPE_MAP) {
Laurence Lundblade830fbf92020-05-31 17:22:33 -07004798 return -8;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004799 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004800
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004801 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundbladeec290b82024-06-10 11:10:54 -07004802 return -15;
4803 if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM ||
4804 Item.uLabelType != QCBOR_TYPE_INT64 ||
4805 Item.label.int64 != -64 ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07004806 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundbladeec290b82024-06-10 11:10:54 -07004807 return -16;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004808 }
4809
4810 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07004811 return -11;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004812 if(Item.uDataType != QCBOR_TYPE_POSBIGNUM ||
4813 Item.uLabelType != QCBOR_TYPE_INT64 ||
4814 Item.label.int64 != 64 ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07004815 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07004816 return -12;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004817 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004818
Laurence Lundbladeec290b82024-06-10 11:10:54 -07004819#ifndef QCBOR_DISABLE_NON_INTEGER_LABELS
4820 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
4821 return -9;
4822 if(Item.uDataType != QCBOR_TYPE_POSBIGNUM ||
4823 Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
4824 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
4825 return -10;
4826 }
4827
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004828 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07004829 return -13;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004830 if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM ||
4831 Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07004832 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07004833 return -14;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004834 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004835
Laurence Lundbladeec290b82024-06-10 11:10:54 -07004836
4837#endif /* ! QCBOR_DISABLE_NON_INTEGER_LABELS */
4838
Laurence Lundblade37286c02022-09-03 10:05:02 -07004839#else
4840
4841 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_TAGS_DISABLED) {
4842 return -100;
4843 }
4844#endif /* QCBOR_DISABLE_TAGS */
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004845
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004846 return 0;
4847}
4848
4849
Laurence Lundbladec5fef682020-01-25 11:38:45 -08004850static int32_t CheckItemWithIntLabel(QCBORDecodeContext *pCtx,
Laurence Lundbladeee851742020-01-08 08:37:05 -08004851 uint8_t uDataType,
4852 uint8_t uNestingLevel,
4853 uint8_t uNextNest,
4854 int64_t nLabel,
4855 QCBORItem *pItem)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004856{
4857 QCBORItem Item;
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07004858 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004859
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004860 if((nCBORError = QCBORDecode_GetNext(pCtx, &Item))) return -1;
4861 if(Item.uDataType != uDataType) return -1;
4862 if(uNestingLevel > 0) {
Laurence Lundbladeec290b82024-06-10 11:10:54 -07004863 if(Item.uLabelType != QCBOR_TYPE_INT64) {
Laurence Lundbladeee851742020-01-08 08:37:05 -08004864 return -1;
4865 }
Laurence Lundbladeec290b82024-06-10 11:10:54 -07004866 if(Item.label.int64 != nLabel) {
4867 return -1;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004868 }
Laurence Lundbladeec290b82024-06-10 11:10:54 -07004869
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004870 }
4871 if(Item.uNestingLevel != uNestingLevel) return -1;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304872 if(Item.uNextNestLevel != uNextNest) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004873
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004874 if(pItem) {
4875 *pItem = Item;
4876 }
4877 return 0;
4878}
4879
Laurence Lundblade742df4a2018-10-13 20:07:17 +08004880// Same code checks definite and indefinite length versions of the map
Laurence Lundbladec5fef682020-01-25 11:38:45 -08004881static int32_t CheckCSRMaps(QCBORDecodeContext *pDC)
Laurence Lundblade742df4a2018-10-13 20:07:17 +08004882{
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304883 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 0, 1, 0, NULL)) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004884
Laurence Lundblade9b334962020-08-27 10:55:53 -07004885 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 1, 2, -23, NULL)) return -2;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004886
Laurence Lundblade9b334962020-08-27 10:55:53 -07004887 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 2, 3, -20, NULL)) return -3;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004888
Laurence Lundblade9b334962020-08-27 10:55:53 -07004889 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -18, NULL)) return -4;
4890 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -17, NULL)) return -5;
4891 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -15, NULL)) return -6;
4892 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -16, NULL)) return -7;
4893 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 2, -14, NULL)) return -8;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004894
Laurence Lundblade9b334962020-08-27 10:55:53 -07004895 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 2, 3, -19, NULL)) return -9;
4896 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 3, 4, -11, NULL)) return -10;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004897
Laurence Lundblade9b334962020-08-27 10:55:53 -07004898 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_INT64, 4, 3, -9, NULL)) return -11;
4899 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_BYTE_STRING, 3, 1, -10, NULL)) return -12;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004900
Laurence Lundblade9b334962020-08-27 10:55:53 -07004901 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 1, 2, -22, NULL)) return -13;
4902 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_INT64, 2, 0, -5, NULL)) return -14;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004903
Laurence Lundblade9b334962020-08-27 10:55:53 -07004904 if(QCBORDecode_Finish(pDC)) return -20;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004905
Laurence Lundblade742df4a2018-10-13 20:07:17 +08004906 return 0;
4907}
4908
4909
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004910/*
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004911{
4912 -23: {
4913 -20: {
4914 -18: "Organization",
4915 -17: "SSG",
4916 -15: "Confusion",
4917 -16: "San Diego",
4918 -14: "US"
4919 },
4920 -19: {
4921 -11: {
4922 -9: -7
4923 },
4924 -10: '\u0001\u0002\u0003\u0004\u0005\u0006\a\b\t\n'
4925 }
4926 },
4927 -22: {
4928 -5: -3
4929 }
4930}
Laurence Lundbladecc7da412020-12-27 00:09:07 -08004931*/
4932static const uint8_t spCSRInput[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004933 0xa2, 0x36, 0xa2, 0x33, 0xa5, 0x31, 0x6c, 0x4f,
4934 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74,
4935 0x69, 0x6f, 0x6e, 0x30, 0x63, 0x53, 0x53, 0x47,
4936 0x2e, 0x69, 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73,
4937 0x69, 0x6f, 0x6e, 0x2f, 0x69, 0x53, 0x61, 0x6e,
4938 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f, 0x2d, 0x62,
4939 0x55, 0x53, 0x32, 0xa2, 0x2a, 0xa1, 0x28, 0x26,
4940 0x29, 0x4a, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
4941 0x07, 0x08, 0x09, 0x0a, 0x35, 0xa1, 0x24, 0x22};
4942
Laurence Lundbladecc7da412020-12-27 00:09:07 -08004943// Same map as above, but using indefinite lengths
4944static const uint8_t spCSRInputIndefLen[] = {
4945 0xbf, 0x36, 0xbf, 0x33, 0xbf, 0x31, 0x6c, 0x4f,
4946 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74,
4947 0x69, 0x6f, 0x6e, 0x30, 0x63, 0x53, 0x53, 0x47,
4948 0x2e, 0x69, 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73,
4949 0x69, 0x6f, 0x6e, 0x2f, 0x69, 0x53, 0x61, 0x6e,
4950 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f, 0x2d, 0x62,
4951 0x55, 0x53, 0xff, 0x32, 0xbf, 0x2a, 0xbf, 0x28,
4952 0x26, 0xff, 0x29, 0x4a, 0x01, 0x02, 0x03, 0x04,
4953 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0xff, 0xff,
4954 0x35, 0xbf, 0x24, 0x22, 0xff, 0xff};
4955
4956
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03004957int32_t NestedMapTest(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004958{
4959 QCBORDecodeContext DCtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004960
Laurence Lundbladeee851742020-01-08 08:37:05 -08004961 QCBORDecode_Init(&DCtx,
4962 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput),
4963 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004964
Laurence Lundblade742df4a2018-10-13 20:07:17 +08004965 return CheckCSRMaps(&DCtx);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004966}
4967
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08004968
4969
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03004970int32_t StringDecoderModeFailTest(void)
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08004971{
4972 QCBORDecodeContext DCtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004973
Laurence Lundbladeee851742020-01-08 08:37:05 -08004974 QCBORDecode_Init(&DCtx,
4975 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput),
4976 QCBOR_DECODE_MODE_MAP_STRINGS_ONLY);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004977
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08004978 QCBORItem Item;
4979 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004980
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08004981 if(QCBORDecode_GetNext(&DCtx, &Item)) {
4982 return -1;
4983 }
4984 if(Item.uDataType != QCBOR_TYPE_MAP) {
4985 return -2;
4986 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004987
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08004988 nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
4989 if(nCBORError != QCBOR_ERR_MAP_LABEL_TYPE) {
4990 return -3;
4991 }
4992
4993 return 0;
4994}
4995
4996
Laurence Lundblade742df4a2018-10-13 20:07:17 +08004997
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03004998int32_t NestedMapTestIndefLen(void)
Laurence Lundblade742df4a2018-10-13 20:07:17 +08004999{
5000 QCBORDecodeContext DCtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005001
Laurence Lundbladeee851742020-01-08 08:37:05 -08005002 QCBORDecode_Init(&DCtx,
5003 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInputIndefLen),
5004 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005005
Laurence Lundblade742df4a2018-10-13 20:07:17 +08005006 return CheckCSRMaps(&DCtx);
5007}
5008
5009
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08005010
Laurence Lundblade17ede402018-10-13 11:43:07 +08005011static UsefulBufC make_nested_indefinite_arrays(int n, UsefulBuf Storage)
5012{
5013 UsefulOutBuf UOB;
5014 UsefulOutBuf_Init(&UOB, Storage);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005015
Laurence Lundblade17ede402018-10-13 11:43:07 +08005016 int i;
5017 for(i = 0; i < n; i++) {
5018 UsefulOutBuf_AppendByte(&UOB, 0x9f);
5019 }
5020
5021 for(i = 0; i < n; i++) {
5022 UsefulOutBuf_AppendByte(&UOB, 0xff);
5023 }
5024 return UsefulOutBuf_OutUBuf(&UOB);
5025}
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08005026
5027
Laurence Lundbladec5fef682020-01-25 11:38:45 -08005028static int32_t parse_indeflen_nested(UsefulBufC Nested, int nNestLevel)
Laurence Lundblade17ede402018-10-13 11:43:07 +08005029{
5030 QCBORDecodeContext DC;
5031 QCBORDecode_Init(&DC, Nested, 0);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005032
Laurence Lundblade17ede402018-10-13 11:43:07 +08005033 int j;
5034 for(j = 0; j < nNestLevel; j++) {
5035 QCBORItem Item;
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07005036 QCBORError nReturn = QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade17ede402018-10-13 11:43:07 +08005037 if(j >= QCBOR_MAX_ARRAY_NESTING) {
5038 // Should be in error
Laurence Lundbladea9489f82020-09-12 13:50:56 -07005039 if(nReturn != QCBOR_ERR_ARRAY_DECODE_NESTING_TOO_DEEP) {
Laurence Lundblade17ede402018-10-13 11:43:07 +08005040 return -4;
5041 } else {
5042 return 0; // Decoding doesn't recover after an error
5043 }
5044 } else {
5045 // Should be no error
5046 if(nReturn) {
5047 return -9; // Should not have got an error
5048 }
5049 }
5050 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
5051 return -7;
5052 }
5053 }
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07005054 QCBORError nReturn = QCBORDecode_Finish(&DC);
Laurence Lundblade17ede402018-10-13 11:43:07 +08005055 if(nReturn) {
5056 return -3;
5057 }
5058 return 0;
5059}
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08005060
5061
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03005062int32_t IndefiniteLengthNestTest(void)
Laurence Lundblade17ede402018-10-13 11:43:07 +08005063{
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05305064 UsefulBuf_MAKE_STACK_UB(Storage, 50);
Laurence Lundblade17ede402018-10-13 11:43:07 +08005065 int i;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005066 for(i=1; i < QCBOR_MAX_ARRAY_NESTING+4; i++) {
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08005067 const UsefulBufC Nested = make_nested_indefinite_arrays(i, Storage);
Laurence Lundblade17ede402018-10-13 11:43:07 +08005068 int nReturn = parse_indeflen_nested(Nested, i);
5069 if(nReturn) {
5070 return nReturn;
5071 }
5072 }
5073 return 0;
5074}
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08005075
Laurence Lundbladeee851742020-01-08 08:37:05 -08005076// [1, [2, 3]]
5077static const uint8_t spIndefiniteArray[] = {0x9f, 0x01, 0x82, 0x02, 0x03, 0xff};
5078// No closing break
5079static const uint8_t spIndefiniteArrayBad1[] = {0x9f};
5080// Not enough closing breaks
5081static const uint8_t spIndefiniteArrayBad2[] = {0x9f, 0x9f, 0x02, 0xff};
5082// Too many closing breaks
5083static const uint8_t spIndefiniteArrayBad3[] = {0x9f, 0x02, 0xff, 0xff};
5084// Unclosed indeflen inside def len
5085static const uint8_t spIndefiniteArrayBad4[] = {0x81, 0x9f};
5086// confused tag
5087static const uint8_t spIndefiniteArrayBad5[] = {0x9f, 0xd1, 0xff};
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08005088
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03005089int32_t IndefiniteLengthArrayMapTest(void)
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08005090{
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07005091 QCBORError nResult;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08005092 // --- first test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07005093 UsefulBufC IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArray);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005094
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08005095 // Decode it and see if it is OK
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08005096 QCBORDecodeContext DC;
5097 QCBORItem Item;
5098 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005099
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08005100 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05305101
5102 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
5103 Item.uNestingLevel != 0 ||
5104 Item.uNextNestLevel != 1) {
5105 return -111;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08005106 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005107
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08005108 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05305109 if(Item.uDataType != QCBOR_TYPE_INT64 ||
5110 Item.uNestingLevel != 1 ||
5111 Item.uNextNestLevel != 1) {
5112 return -2;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08005113 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005114
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08005115 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05305116 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
5117 Item.uNestingLevel != 1 ||
5118 Item.uNextNestLevel != 2) {
5119 return -3;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08005120 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005121
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08005122 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade12b495d2018-12-17 11:15:54 -08005123 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade6de37062018-10-15 12:22:42 +05305124 Item.uNestingLevel != 2 ||
5125 Item.uNextNestLevel != 2) {
5126 return -4;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08005127 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005128
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08005129 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade12b495d2018-12-17 11:15:54 -08005130 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade6de37062018-10-15 12:22:42 +05305131 Item.uNestingLevel != 2 ||
5132 Item.uNextNestLevel != 0) {
5133 return -5;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08005134 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005135
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08005136 if(QCBORDecode_Finish(&DC)) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05305137 return -6;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08005138 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005139
Laurence Lundblade19e0c802018-10-13 12:19:55 +08005140 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07005141 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad1);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005142
Laurence Lundblade19e0c802018-10-13 12:19:55 +08005143 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005144
Laurence Lundblade19e0c802018-10-13 12:19:55 +08005145 nResult = QCBORDecode_GetNext(&DC, &Item);
5146 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05305147 return -7;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08005148 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005149
Laurence Lundblade570fab52018-10-13 18:28:27 +08005150 nResult = QCBORDecode_Finish(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07005151 if(nResult != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05305152 return -8;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08005153 }
5154
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005155
Laurence Lundblade19e0c802018-10-13 12:19:55 +08005156 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07005157 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad2);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005158
Laurence Lundblade19e0c802018-10-13 12:19:55 +08005159 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005160
Laurence Lundblade19e0c802018-10-13 12:19:55 +08005161 nResult = QCBORDecode_GetNext(&DC, &Item);
5162 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05305163 return -9;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08005164 }
5165
5166 nResult = QCBORDecode_GetNext(&DC, &Item);
5167 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05305168 return -10;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08005169 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005170
Laurence Lundblade19e0c802018-10-13 12:19:55 +08005171 nResult = QCBORDecode_GetNext(&DC, &Item);
5172 if(nResult || Item.uDataType != QCBOR_TYPE_INT64) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05305173 return -11;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08005174 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005175
Laurence Lundblade19e0c802018-10-13 12:19:55 +08005176 nResult = QCBORDecode_Finish(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07005177 if(nResult != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05305178 return -12;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08005179 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005180
5181
Laurence Lundblade19e0c802018-10-13 12:19:55 +08005182 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07005183 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad3);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005184
Laurence Lundblade19e0c802018-10-13 12:19:55 +08005185 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005186
Laurence Lundblade19e0c802018-10-13 12:19:55 +08005187 nResult = QCBORDecode_GetNext(&DC, &Item);
5188 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05305189 return -13;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08005190 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005191
Laurence Lundblade19e0c802018-10-13 12:19:55 +08005192 nResult = QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade642282a2020-06-23 12:00:33 -07005193 if(nResult != QCBOR_SUCCESS) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05305194 return -14;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08005195 }
Laurence Lundblade6de37062018-10-15 12:22:42 +05305196
Laurence Lundblade642282a2020-06-23 12:00:33 -07005197 nResult = QCBORDecode_GetNext(&DC, &Item);
5198 if(nResult != QCBOR_ERR_BAD_BREAK) {
5199 return -140;
5200 }
5201
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005202
Laurence Lundblade570fab52018-10-13 18:28:27 +08005203 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07005204 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad4);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005205
Laurence Lundblade570fab52018-10-13 18:28:27 +08005206 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005207
Laurence Lundblade570fab52018-10-13 18:28:27 +08005208 nResult = QCBORDecode_GetNext(&DC, &Item);
5209 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05305210 return -15;
Laurence Lundblade570fab52018-10-13 18:28:27 +08005211 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005212
Laurence Lundblade570fab52018-10-13 18:28:27 +08005213 nResult = QCBORDecode_GetNext(&DC, &Item);
5214 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05305215 return -16;
Laurence Lundblade570fab52018-10-13 18:28:27 +08005216 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005217
Laurence Lundblade570fab52018-10-13 18:28:27 +08005218 nResult = QCBORDecode_Finish(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07005219 if(nResult != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05305220 return -17;
Laurence Lundblade570fab52018-10-13 18:28:27 +08005221 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005222
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05305223 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07005224 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad5);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005225
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05305226 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005227
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05305228 nResult = QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade37286c02022-09-03 10:05:02 -07005229
5230#ifndef QCBOR_DISABLE_TAGS
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05305231 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05305232 return -18;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05305233 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005234
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05305235 nResult = QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05305236 if(nResult != QCBOR_ERR_BAD_BREAK) {
5237 return -19;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05305238 }
Laurence Lundblade37286c02022-09-03 10:05:02 -07005239#else /* QCBOR_DISABLE_TAGS */
5240 if(nResult != QCBOR_ERR_TAGS_DISABLED) {
5241 return -20;
5242 }
5243#endif /* QCBOR_DISABLE_TAGS */
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005244
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08005245 return 0;
5246}
5247
Laurence Lundblade17ede402018-10-13 11:43:07 +08005248
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08005249#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
5250
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07005251static const uint8_t spIndefiniteLenString[] = {
Laurence Lundblade17ede402018-10-13 11:43:07 +08005252 0x81, // Array of length one
5253 0x7f, // text string marked with indefinite length
5254 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment
5255 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment
5256 0xff // ending break
5257};
5258
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07005259static const uint8_t spIndefiniteLenStringBad2[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305260 0x81, // Array of length one
5261 0x7f, // text string marked with indefinite length
5262 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment
5263 0x44, 0x6d, 0x69, 0x6e, 0x67, // second segment of wrong type
5264 0xff // ending break
5265};
5266
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07005267static const uint8_t spIndefiniteLenStringBad3[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305268 0x81, // Array of length one
5269 0x7f, // text string marked with indefinite length
5270 0x01, 0x02, // Not a string
5271 0xff // ending break
5272};
5273
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07005274static const uint8_t spIndefiniteLenStringBad4[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305275 0x81, // Array of length one
5276 0x7f, // text string marked with indefinite length
5277 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment
5278 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment
5279 // missing end of string
5280};
5281
Laurence Lundbladeec290b82024-06-10 11:10:54 -07005282#ifndef QCBOR_DISABLE_NON_INTEGER_LABELS
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07005283static const uint8_t spIndefiniteLenStringLabel[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305284 0xa1, // Array of length one
5285 0x7f, // text string marked with indefinite length
5286 0x65, 0x73, 0x74, 0x72, 0x75, 0x75, // first segment
5287 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment
5288 0xff, // ending break
5289 0x01 // integer being labeled.
5290};
Laurence Lundbladeec290b82024-06-10 11:10:54 -07005291#endif /* ! QCBOR_DISABLE_NON_INTEGER_LABELS */
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305292
Laurence Lundbladec5fef682020-01-25 11:38:45 -08005293/**
5294 Make an indefinite length string
5295
5296 @param Storage Storage for string, must be 144 bytes in size
5297 @return The indefinite length string
5298
5299 This makes an array with one indefinite length string that has 7 chunks
5300 from size of 1 byte up to 64 bytes.
5301 */
5302static UsefulBufC MakeIndefiniteBigBstr(UsefulBuf Storage)
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305303{
5304 UsefulOutBuf UOB;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005305
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305306 UsefulOutBuf_Init(&UOB, Storage);
5307 UsefulOutBuf_AppendByte(&UOB, 0x81);
5308 UsefulOutBuf_AppendByte(&UOB, 0x5f);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005309
Laurence Lundbladec5fef682020-01-25 11:38:45 -08005310 uint8_t uStringByte = 0;
5311 // Use of type int is intentional
5312 for(int uChunkSize = 1; uChunkSize <= 128; uChunkSize *= 2) {
5313 // Not using preferred encoding here, but that is OK.
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305314 UsefulOutBuf_AppendByte(&UOB, 0x58);
Laurence Lundbladec5fef682020-01-25 11:38:45 -08005315 UsefulOutBuf_AppendByte(&UOB, (uint8_t)uChunkSize);
5316 for(int j = 0; j < uChunkSize; j++) {
5317 UsefulOutBuf_AppendByte(&UOB, uStringByte);
5318 uStringByte++;
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305319 }
5320 }
5321 UsefulOutBuf_AppendByte(&UOB, 0xff);
5322
5323 return UsefulOutBuf_OutUBuf(&UOB);
5324}
5325
5326static int CheckBigString(UsefulBufC BigString)
5327{
5328 if(BigString.len != 255) {
5329 return 1;
5330 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005331
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305332 for(uint8_t i = 0; i < 255; i++){
5333 if(((const uint8_t *)BigString.ptr)[i] != i) {
5334 return 1;
5335 }
5336 }
5337 return 0;
5338}
5339
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05305340
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03005341int32_t IndefiniteLengthStringTest(void)
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05305342{
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305343 QCBORDecodeContext DC;
5344 QCBORItem Item;
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05305345 // big enough for MakeIndefiniteBigBstr() + MemPool overhead
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08005346 UsefulBuf_MAKE_STACK_UB(MemPool, 350);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005347
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305348 // --- Simple normal indefinite length string ------
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07005349 UsefulBufC IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenString);
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305350 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005351
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05305352 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305353 return -1;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05305354 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005355
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305356 if(QCBORDecode_GetNext(&DC, &Item)) {
5357 return -2;
5358 }
5359 if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.uDataAlloc) {
5360 return -3;
5361 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005362
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305363 if(QCBORDecode_GetNext(&DC, &Item)) {
5364 return -4;
5365 }
5366 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || !Item.uDataAlloc) {
5367 return -5;
5368 }
5369 if(QCBORDecode_Finish(&DC)) {
5370 return -6;
5371 }
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05305372
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305373 // ----- types mismatch ---
Laurence Lundbladeee851742020-01-08 08:37:05 -08005374 QCBORDecode_Init(&DC,
5375 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad2),
5376 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005377
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305378 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
5379 return -7;
5380 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005381
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305382 if(QCBORDecode_GetNext(&DC, &Item)) {
5383 return -8;
5384 }
5385 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
5386 return -9;
5387 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005388
Laurence Lundblade30816f22018-11-10 13:40:22 +07005389 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_INDEFINITE_STRING_CHUNK) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305390 return -10;
5391 }
5392
5393 // ----- not a string ---
Laurence Lundbladeee851742020-01-08 08:37:05 -08005394 QCBORDecode_Init(&DC,
5395 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad3),
5396 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005397
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305398 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
5399 return -11;
5400 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005401
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305402 if(QCBORDecode_GetNext(&DC, &Item)) {
5403 return -12;
5404 }
5405 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
5406 return -13;
5407 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005408
Laurence Lundblade30816f22018-11-10 13:40:22 +07005409 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_INDEFINITE_STRING_CHUNK) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305410 return -14;
5411 }
5412
5413 // ----- no end -----
Laurence Lundbladeee851742020-01-08 08:37:05 -08005414 QCBORDecode_Init(&DC,
5415 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad4),
5416 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005417
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305418 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
5419 return -15;
5420 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005421
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305422 if(QCBORDecode_GetNext(&DC, &Item)) {
5423 return -16;
5424 }
5425 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
5426 return -17;
5427 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005428
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305429 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_HIT_END) {
5430 return -18;
5431 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005432
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305433 // ------ Don't set a string allocator and see an error -----
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05305434 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005435
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05305436 QCBORDecode_GetNext(&DC, &Item);
5437 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305438 return -19;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05305439 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005440
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05305441 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_NO_STRING_ALLOCATOR) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305442 return -20;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05305443 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005444
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05305445 // ----- Mempool is way too small -----
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08005446 UsefulBuf_MAKE_STACK_UB(MemPoolTooSmall, QCBOR_DECODE_MIN_MEM_POOL_SIZE-1);
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05305447
5448 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
5449 if(!QCBORDecode_SetMemPool(&DC, MemPoolTooSmall, false)) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305450 return -21;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05305451 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005452
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05305453 // ----- Mempool is way too small -----
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05305454 UsefulBuf_MAKE_STACK_UB(BigIndefBStrStorage, 290);
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08005455 const UsefulBufC BigIndefBStr = MakeIndefiniteBigBstr(BigIndefBStrStorage);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005456
Laurence Lundbladeee851742020-01-08 08:37:05 -08005457 // 80 is big enough for MemPool overhead, but not BigIndefBStr
5458 UsefulBuf_MAKE_STACK_UB(MemPoolSmall, 80);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005459
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305460 QCBORDecode_Init(&DC, BigIndefBStr, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05305461 if(QCBORDecode_SetMemPool(&DC, MemPoolSmall, false)) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305462 return -22;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05305463 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005464
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05305465 QCBORDecode_GetNext(&DC, &Item);
5466 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305467 return -23;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05305468 }
Laurence Lundblade30816f22018-11-10 13:40:22 +07005469 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_STRING_ALLOCATE) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305470 return -24;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05305471 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005472
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305473 // ---- big bstr -----
5474 QCBORDecode_Init(&DC, BigIndefBStr, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005475
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305476 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
5477 return -25;
5478 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005479
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05305480 if(QCBORDecode_GetNext(&DC, &Item)) {
5481 return -26;
5482 }
5483 if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.uDataAlloc) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305484 return -26;
5485 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005486
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305487 if(QCBORDecode_GetNext(&DC, &Item)) {
5488 return -27;
5489 }
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05305490 if(Item.uDataType != QCBOR_TYPE_BYTE_STRING || !Item.uDataAlloc || Item.uNestingLevel != 1) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305491 return -28;
5492 }
5493 if(CheckBigString(Item.val.string)) {
5494 return -3;
5495 }
5496 if(QCBORDecode_Finish(&DC)) {
5497 return -29;
5498 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005499
Laurence Lundbladeec290b82024-06-10 11:10:54 -07005500#ifndef QCBOR_DISABLE_NON_INTEGER_LABELS
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305501 // --- label is an indefinite length string ------
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07005502 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringLabel), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005503
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305504 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
5505 return -30;
5506 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005507
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305508 QCBORDecode_GetNext(&DC, &Item);
5509 if(Item.uDataType != QCBOR_TYPE_MAP) {
5510 return -31;
5511 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005512
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305513 if(QCBORDecode_GetNext(&DC, &Item)){
5514 return -32;
5515 }
Laurence Lundbladeee851742020-01-08 08:37:05 -08005516 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
5517 Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305518 Item.uDataAlloc || !Item.uLabelAlloc ||
5519 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("struuming"))) {
5520 return -33;
5521 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005522
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305523 if(QCBORDecode_Finish(&DC)) {
5524 return -34;
5525 }
Laurence Lundbladeec290b82024-06-10 11:10:54 -07005526#endif /* ! QCBOR_DISABLE_NON_INTEGER_LABELS */
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005527
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08005528 return 0;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08005529}
5530
5531
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03005532int32_t AllocAllStringsTest(void)
Laurence Lundbladea44d5062018-10-17 18:45:12 +05305533{
5534 QCBORDecodeContext DC;
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08005535 QCBORError nCBORError;
5536
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005537
Laurence Lundbladea44d5062018-10-17 18:45:12 +05305538 // First test, use the "CSRMap" as easy input and checking
Laurence Lundbladeee851742020-01-08 08:37:05 -08005539 QCBORDecode_Init(&DC,
5540 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput),
5541 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005542
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08005543 UsefulBuf_MAKE_STACK_UB(Pool, sizeof(spCSRInput) + QCBOR_DECODE_MIN_MEM_POOL_SIZE);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005544
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08005545 nCBORError = QCBORDecode_SetMemPool(&DC, Pool, 1); // Turn on copying.
5546 if(nCBORError) {
Laurence Lundbladea44d5062018-10-17 18:45:12 +05305547 return -1;
5548 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005549
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08005550 if(CheckCSRMaps(&DC)) {
5551 return -2;
5552 }
5553
Laurence Lundbladeec290b82024-06-10 11:10:54 -07005554#ifndef QCBOR_DISABLE_NON_INTEGER_LABELS
Laurence Lundblade2f467f92020-10-09 17:50:11 -07005555 // Next parse, save pointers to a few strings, destroy original and
5556 // see all is OK.
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08005557 UsefulBuf_MAKE_STACK_UB(CopyOfStorage, sizeof(pValidMapEncoded) + QCBOR_DECODE_MIN_MEM_POOL_SIZE);
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08005558 const UsefulBufC CopyOf = UsefulBuf_Copy(CopyOfStorage, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded));
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08005559
Laurence Lundbladea44d5062018-10-17 18:45:12 +05305560 QCBORDecode_Init(&DC, CopyOf, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08005561 UsefulBuf_Set(Pool, '/');
Laurence Lundbladea44d5062018-10-17 18:45:12 +05305562 QCBORDecode_SetMemPool(&DC, Pool, 1); // Turn on copying.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005563
Laurence Lundbladea44d5062018-10-17 18:45:12 +05305564 QCBORItem Item1, Item2, Item3, Item4;
5565 if((nCBORError = QCBORDecode_GetNext(&DC, &Item1)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07005566 return (int32_t)nCBORError;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05305567 if(Item1.uDataType != QCBOR_TYPE_MAP ||
5568 Item1.val.uCount != 3)
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08005569 return -3;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05305570 if((nCBORError = QCBORDecode_GetNext(&DC, &Item1)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07005571 return (int32_t)nCBORError;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05305572 if((nCBORError = QCBORDecode_GetNext(&DC, &Item2)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07005573 return (int32_t)nCBORError;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05305574 if((nCBORError = QCBORDecode_GetNext(&DC, &Item3)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07005575 return (int32_t)nCBORError;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05305576 if((nCBORError = QCBORDecode_GetNext(&DC, &Item4)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07005577 return (int32_t)nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005578
Laurence Lundblade05ec57b2018-10-21 01:50:03 +05305579 UsefulBuf_Set(CopyOfStorage, '_');
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005580
Laurence Lundbladea44d5062018-10-17 18:45:12 +05305581 if(Item1.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladea44d5062018-10-17 18:45:12 +05305582 Item1.uDataType != QCBOR_TYPE_INT64 ||
5583 Item1.val.int64 != 42 ||
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08005584 Item1.uDataAlloc != 0 ||
5585 Item1.uLabelAlloc == 0 ||
Laurence Lundbladeec290b82024-06-10 11:10:54 -07005586 UsefulBufCompareToSZ(Item1.label.string, "first integer") ||
5587 Item1.label.string.ptr < Pool.ptr ||
5588 Item1.label.string.ptr > (const void *)((const uint8_t *)Pool.ptr + Pool.len)) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08005589 return -4;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09005590 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005591
Laurence Lundbladea44d5062018-10-17 18:45:12 +05305592
5593 if(Item2.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07005594 UsefulBufCompareToSZ(Item2.label.string, "an array of two strings") ||
Laurence Lundbladea44d5062018-10-17 18:45:12 +05305595 Item2.uDataType != QCBOR_TYPE_ARRAY ||
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08005596 Item2.uDataAlloc != 0 ||
5597 Item2.uLabelAlloc == 0 ||
Laurence Lundbladea44d5062018-10-17 18:45:12 +05305598 Item2.val.uCount != 2)
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08005599 return -5;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005600
Laurence Lundbladea44d5062018-10-17 18:45:12 +05305601 if(Item3.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08005602 Item3.uDataAlloc == 0 ||
5603 Item3.uLabelAlloc != 0 ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07005604 UsefulBufCompareToSZ(Item3.val.string, "string1")) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08005605 return -6;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09005606 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005607
Laurence Lundbladea44d5062018-10-17 18:45:12 +05305608 if(Item4.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08005609 Item4.uDataAlloc == 0 ||
5610 Item4.uLabelAlloc != 0 ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07005611 UsefulBufCompareToSZ(Item4.val.string, "string2")) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08005612 return -7;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09005613 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005614
Laurence Lundbladea44d5062018-10-17 18:45:12 +05305615 // Next parse with a pool that is too small
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08005616 UsefulBuf_MAKE_STACK_UB(SmallPool, QCBOR_DECODE_MIN_MEM_POOL_SIZE + 1);
Laurence Lundbladeee851742020-01-08 08:37:05 -08005617 QCBORDecode_Init(&DC,
5618 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded),
5619 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladea44d5062018-10-17 18:45:12 +05305620 QCBORDecode_SetMemPool(&DC, SmallPool, 1); // Turn on copying.
5621 if((nCBORError = QCBORDecode_GetNext(&DC, &Item1)))
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08005622 return -8;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05305623 if(Item1.uDataType != QCBOR_TYPE_MAP ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09005624 Item1.val.uCount != 3) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08005625 return -9;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09005626 }
Laurence Lundbladea44d5062018-10-17 18:45:12 +05305627 if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item1))){
5628 if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item2))) {
5629 if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item3))) {
5630 nCBORError = QCBORDecode_GetNext(&DC, &Item4);
5631 }
5632 }
5633 }
Laurence Lundblade30816f22018-11-10 13:40:22 +07005634 if(nCBORError != QCBOR_ERR_STRING_ALLOCATE) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08005635 return -10;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05305636 }
Laurence Lundbladeec290b82024-06-10 11:10:54 -07005637#endif /* ! QCBOR_DISABLE_NON_INTEGER_LABELS */
Laurence Lundbladea44d5062018-10-17 18:45:12 +05305638
5639 return 0;
5640}
5641
Laurence Lundbladef6531662018-12-04 10:42:22 +09005642
Laurence Lundbladec5fef682020-01-25 11:38:45 -08005643int32_t MemPoolTest(void)
Laurence Lundblade0155b622018-10-12 20:04:37 +08005644{
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08005645 // Set up the decoder with a tiny bit of CBOR to parse because
5646 // nothing can be done with it unless that is set up.
Laurence Lundbladef6531662018-12-04 10:42:22 +09005647 QCBORDecodeContext DC;
5648 const uint8_t pMinimalCBOR[] = {0xa0}; // One empty map
5649 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pMinimalCBOR),0);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005650
Laurence Lundbladef6531662018-12-04 10:42:22 +09005651 // Set up an memory pool of 100 bytes
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08005652 // Then fish into the internals of the decode context
5653 // to get the allocator function so it can be called directly.
5654 // Also figure out how much pool is available for use
5655 // buy subtracting out the overhead.
Laurence Lundbladef6531662018-12-04 10:42:22 +09005656 UsefulBuf_MAKE_STACK_UB(Pool, 100);
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08005657 QCBORError nError = QCBORDecode_SetMemPool(&DC, Pool, 0);
5658 if(nError) {
5659 return -9;
5660 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08005661 QCBORStringAllocate pAlloc = DC.StringAllocator.pfAllocator;
5662 void *pAllocCtx = DC.StringAllocator.pAllocateCxt;
5663 size_t uAvailPool = Pool.len - QCBOR_DECODE_MIN_MEM_POOL_SIZE;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005664
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08005665 // First test -- ask for one more byte than available and see failure
5666 UsefulBuf Allocated = (*pAlloc)(pAllocCtx, NULL, uAvailPool+1);
Laurence Lundbladef6531662018-12-04 10:42:22 +09005667 if(!UsefulBuf_IsNULL(Allocated)) {
5668 return -1;
5669 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005670
Laurence Lundbladef6531662018-12-04 10:42:22 +09005671 // Re do the set up for the next test that will do a successful alloc,
5672 // a fail, a free and then success
Laurence Lundbladef6531662018-12-04 10:42:22 +09005673 QCBORDecode_SetMemPool(&DC, Pool, 0);
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08005674 pAlloc = DC.StringAllocator.pfAllocator;
5675 pAllocCtx = DC.StringAllocator.pAllocateCxt;
5676 uAvailPool = Pool.len - QCBOR_DECODE_MIN_MEM_POOL_SIZE;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005677
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08005678 // Allocate one byte less than available and see success
5679 Allocated = (pAlloc)(pAllocCtx, NULL, uAvailPool-1);
Laurence Lundbladef6531662018-12-04 10:42:22 +09005680 if(UsefulBuf_IsNULL(Allocated)) { // expected to succeed
5681 return -2;
5682 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08005683 // Ask for some more and see failure
5684 UsefulBuf Allocated2 = (*pAlloc)(pAllocCtx, NULL, uAvailPool/2);
Laurence Lundbladef6531662018-12-04 10:42:22 +09005685 if(!UsefulBuf_IsNULL(Allocated2)) { // expected to fail
5686 return -3;
5687 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08005688 // Free the first allocate, retry the second and see success
5689 (*pAlloc)(pAllocCtx, Allocated.ptr, 0); // Free
5690 Allocated = (*pAlloc)(pAllocCtx, NULL, uAvailPool/2);
Laurence Lundbladef6531662018-12-04 10:42:22 +09005691 if(UsefulBuf_IsNULL(Allocated)) { // succeed because of the free
5692 return -4;
5693 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005694
Laurence Lundbladef6531662018-12-04 10:42:22 +09005695 // Re do set up for next test that involves a successful alloc,
5696 // and a successful realloc and a failed realloc
5697 QCBORDecode_SetMemPool(&DC, Pool, 0);
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08005698 pAlloc = DC.StringAllocator.pfAllocator;
5699 pAllocCtx = DC.StringAllocator.pAllocateCxt;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005700
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08005701 // Allocate half the pool and see success
5702 Allocated = (*pAlloc)(pAllocCtx, NULL, uAvailPool/2);
Laurence Lundbladef6531662018-12-04 10:42:22 +09005703 if(UsefulBuf_IsNULL(Allocated)) { // expected to succeed
5704 return -5;
5705 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08005706 // Reallocate to take up the whole pool and see success
5707 Allocated2 = (*pAlloc)(pAllocCtx, Allocated.ptr, uAvailPool);
Laurence Lundbladef6531662018-12-04 10:42:22 +09005708 if(UsefulBuf_IsNULL(Allocated2)) {
5709 return -6;
5710 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08005711 // Make sure its the same pointer and the size is right
Laurence Lundbladef6531662018-12-04 10:42:22 +09005712 if(Allocated2.ptr != Allocated.ptr || Allocated2.len != uAvailPool) {
5713 return -7;
5714 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08005715 // Try to allocate more to be sure there is failure after a realloc
5716 UsefulBuf Allocated3 = (*pAlloc)(pAllocCtx, Allocated.ptr, uAvailPool+1);
5717 if(!UsefulBuf_IsNULL(Allocated3)) {
Laurence Lundbladef6531662018-12-04 10:42:22 +09005718 return -8;
5719 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005720
Laurence Lundbladef6531662018-12-04 10:42:22 +09005721 return 0;
5722}
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08005723
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08005724
5725/* Just enough of an allocator to test configuration of one */
5726static UsefulBuf AllocateTestFunction(void *pCtx, void *pOldMem, size_t uNewSize)
5727{
5728 (void)pOldMem; // unused variable
5729
5730 if(uNewSize) {
5731 // Assumes the context pointer is the buffer and
5732 // nothing too big will ever be asked for.
5733 // This is only good for this basic test!
5734 return (UsefulBuf) {pCtx, uNewSize};
5735 } else {
5736 return NULLUsefulBuf;
5737 }
5738}
5739
5740
Laurence Lundbladec5fef682020-01-25 11:38:45 -08005741int32_t SetUpAllocatorTest(void)
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08005742{
5743 // Set up the decoder with a tiny bit of CBOR to parse because
5744 // nothing can be done with it unless that is set up.
5745 QCBORDecodeContext DC;
5746 const uint8_t pMinimalCBOR[] = {0x62, 0x48, 0x69}; // "Hi"
5747 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pMinimalCBOR),0);
5748
5749 uint8_t pAllocatorBuffer[50];
5750
5751 // This is really just to test that this call works.
5752 // The full functionality of string allocators is tested
5753 // elsewhere with the MemPool internal allocator.
5754 QCBORDecode_SetUpAllocator(&DC, AllocateTestFunction, pAllocatorBuffer, 1);
5755
5756 QCBORItem Item;
5757 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_SUCCESS) {
5758 return -1;
5759 }
5760
5761 if(Item.uDataAlloc == 0 ||
5762 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
5763 Item.val.string.ptr != pAllocatorBuffer) {
5764 return -2;
5765 }
5766
5767 if(QCBORDecode_Finish(&DC) != QCBOR_SUCCESS) {
5768 return -3;
5769 }
5770
5771 return 0;
5772}
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08005773#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
5774
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08005775
Laurence Lundbladedd6e76e2021-03-10 01:54:01 -07005776#ifndef QCBOR_DISABLE_EXP_AND_MANTISSA
Laurence Lundblade59289e52019-12-30 13:44:37 -08005777
Laurence Lundblade37286c02022-09-03 10:05:02 -07005778struct EaMTest {
5779 const char *szName;
5780 UsefulBufC Input;
5781 uint8_t uTagRequirement;
5782 bool bHasTags;
5783
5784 /* Expected values for GetNext */
5785 QCBORError uExpectedErrorGN;
5786 uint8_t uQCBORTypeGN;
5787 int64_t nExponentGN;
5788 int64_t nMantissaGN;
5789 UsefulBufC MantissaGN;
5790
5791 /* Expected values for GetDecimalFraction */
5792 QCBORError uExpectedErrorGDF;
5793 int64_t nExponentGDF;
5794 int64_t nMantissaGDF;
5795
5796 /* Expected values for GetDecimalFractionBig */
5797 QCBORError uExpectedErrorGDFB;
5798 int64_t nExponentGDFB;
5799 UsefulBufC MantissaGDFB;
5800 bool IsNegativeGDFB;
5801
5802 /* Expected values for GetBigFloat */
5803 QCBORError uExpectedErrorGBF;
5804 int64_t nExponentGBF;
5805 int64_t nMantissaGBF;
5806
5807 /* Expected values for GetBigFloatBig */
5808 QCBORError uExpectedErrorGBFB;
5809 int64_t nExponentGBFB;
5810 UsefulBufC MantissaGBFB;
5811 bool IsNegativeGBFB;
Laurence Lundblade59289e52019-12-30 13:44:37 -08005812};
5813
Laurence Lundbladefaec39f2020-08-02 21:53:53 -07005814
Laurence Lundblade37286c02022-09-03 10:05:02 -07005815
5816static const struct EaMTest pEaMTests[] = {
5817 {
5818 "1. Untagged pair (big float or decimal fraction), no tag required",
5819 {(const uint8_t []){0x82, 0x20, 0x03}, 3},
5820 QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
5821 false,
5822
5823 QCBOR_SUCCESS, /* for GetNext */
5824 QCBOR_TYPE_ARRAY,
5825 0,
5826 0,
5827 {(const uint8_t []){0x00}, 1},
5828
5829 QCBOR_SUCCESS, /* GetDecimalFraction */
5830 -1,
5831 3,
5832
5833 QCBOR_SUCCESS, /* for GetDecimalFractionBig */
5834 -1,
5835 {(const uint8_t []){0x03}, 1},
5836 false,
5837
5838 QCBOR_SUCCESS, /* for GetBigFloat */
5839 -1,
5840 3,
5841
5842 QCBOR_SUCCESS, /* for GetBigFloatBig */
5843 -1,
5844 {(const uint8_t []){0x03}, 1},
5845 false
5846 },
5847
5848 {
5849 "2. Untagged pair (big float or decimal fraction), tag required",
5850 {(const uint8_t []){0x82, 0x20, 0x03}, 3},
5851 QCBOR_TAG_REQUIREMENT_TAG,
5852 false,
5853
5854 QCBOR_SUCCESS, /* for GetNext */
5855 QCBOR_TYPE_ARRAY,
5856 0,
5857 0,
5858 {(const uint8_t []){0x00}, 1},
5859
5860 QCBOR_ERR_UNEXPECTED_TYPE, /* for GetDecimalFraction */
5861 0,
5862 0,
5863
5864 QCBOR_ERR_UNEXPECTED_TYPE, /* for GetDecimalFractionBig */
5865 0,
5866 {(const uint8_t []){0x00}, 1},
5867 false,
5868
5869 QCBOR_ERR_UNEXPECTED_TYPE, /* for GetBigFloat */
5870 0,
5871 0,
5872
5873 QCBOR_ERR_UNEXPECTED_TYPE, /* for GetBigFloatBig */
5874 0,
5875 {(const uint8_t []){0x00}, 1},
5876 false
5877
5878 },
5879
5880 {
5881 "3. Tagged 1.5 decimal fraction, tag 4 optional",
5882 {(const uint8_t []){0xC4, 0x82, 0x20, 0x03}, 4},
5883 QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
5884 true,
5885
5886 QCBOR_SUCCESS, /* for GetNext */
5887 QCBOR_TYPE_DECIMAL_FRACTION,
5888 -1,
5889 3,
5890 {(const uint8_t []){0x00}, 1},
5891
5892
5893 QCBOR_SUCCESS, /* for GetDecimalFraction */
5894 -1,
5895 3,
5896
5897 QCBOR_SUCCESS, /* for GetDecimalFractionBig */
5898 -1,
5899 {(const uint8_t []){0x03}, 1},
5900 false,
5901
5902 QCBOR_ERR_UNEXPECTED_TYPE, /* for GetBigFloat */
5903 0,
5904 0,
5905
5906 QCBOR_ERR_UNEXPECTED_TYPE, /* for GetBigFloatBig */
5907 0,
5908 {(const uint8_t []){0x00}, 1},
5909 false
5910 },
5911 {
5912 "4. Tagged 100 * 2^300 big float, tag 5 optional",
5913 {(const uint8_t []){0xC5, 0x82, 0x19, 0x01, 0x2C, 0x18, 0x64}, 7},
5914 QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
5915 true,
5916
5917 QCBOR_SUCCESS, /* for GetNext */
5918 QCBOR_TYPE_BIGFLOAT,
5919 300,
5920 100,
5921 {(const uint8_t []){0x00}, 1},
5922
5923
5924 QCBOR_ERR_UNEXPECTED_TYPE, /* for GetDecimalFraction */
5925 0,
5926 0,
5927
5928 QCBOR_ERR_UNEXPECTED_TYPE, /* for GetDecimalFractionBig */
5929 0,
5930 {(const uint8_t []){0x03}, 1},
5931 false,
5932
5933 QCBOR_SUCCESS, /* for GetBigFloat */
5934 300,
5935 100,
5936
5937 QCBOR_SUCCESS, /* for GetBigFloatBig */
5938 300,
5939 {(const uint8_t []){0x64}, 1},
5940 false
5941 },
5942
5943 {
5944 "5. Tagged 4([-20, 4759477275222530853136]) decimal fraction, tag 4 required",
5945 {(const uint8_t []){0xC4, 0x82, 0x33,
5946 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,}, 15},
5947 QCBOR_TAG_REQUIREMENT_TAG,
5948 true,
5949
5950 QCBOR_SUCCESS, /* for GetNext */
5951 QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM,
5952 -20,
5953 0,
5954 {(const uint8_t []){0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10}, 10},
5955
5956 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW, /* for GetDecimalFraction */
5957 0,
5958 0,
5959
5960 QCBOR_SUCCESS, /* for GetDecimalFractionBig */
5961 -20,
5962 {(const uint8_t []){0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10}, 10},
5963 false,
5964
5965 QCBOR_ERR_UNEXPECTED_TYPE, /* for GetBigFloat */
5966 0,
5967 0,
5968
5969 QCBOR_ERR_UNEXPECTED_TYPE, /* for GetBigFloatBig */
5970 0,
5971 {(const uint8_t []){0x00}, 0},
5972 false
5973 },
5974
5975 {
5976 "6. Error: Mantissa and exponent inside a Mantissa and exponent",
5977 {(const uint8_t []){0xC4, 0x82, 0x33,
5978 0xC5, 0x82, 0x19, 0x01, 0x2C, 0x18, 0x64}, 10},
5979 QCBOR_TAG_REQUIREMENT_TAG,
5980 true,
5981
5982 QCBOR_ERR_BAD_EXP_AND_MANTISSA, /* for GetNext */
5983 QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM,
5984 0,
5985 0,
5986 {(const uint8_t []){0x00}, 0},
5987
5988 QCBOR_ERR_BAD_EXP_AND_MANTISSA, /* for GetDecimalFraction */
5989 0,
5990 0,
5991
5992 QCBOR_ERR_BAD_EXP_AND_MANTISSA, /* for GetDecimalFractionBig */
5993 0,
5994 {(const uint8_t []){0x00}, 0},
5995 false,
5996
5997 QCBOR_ERR_BAD_EXP_AND_MANTISSA, /* for GetBigFloat */
5998 0,
5999 0,
6000
6001 QCBOR_ERR_BAD_EXP_AND_MANTISSA, /* for GetBigFloatBig */
6002 0,
6003 {(const uint8_t []){0x00}, 0},
6004 false
6005 },
6006 {
6007 "7. Tagged 5([-20, 4294967295]) big float, big num mantissa, tag 5 required",
6008 {(const uint8_t []){0xC5, 0x82, 0x33,
6009 0xC2, 0x44, 0xff, 0xff, 0xff, 0xff}, 9},
6010 QCBOR_TAG_REQUIREMENT_TAG,
6011 true,
6012
6013 QCBOR_SUCCESS, /* for GetNext */
6014 QCBOR_TYPE_BIGFLOAT_POS_BIGNUM,
6015 -20,
6016 0,
6017 {(const uint8_t []){0xff, 0xff, 0xff, 0xff}, 4},
6018
6019 QCBOR_ERR_UNEXPECTED_TYPE, /* for GetDecimalFraction */
6020 0,
6021 0,
6022
6023 QCBOR_ERR_UNEXPECTED_TYPE, /* for GetDecimalFractionBig */
6024 -20,
6025 {(const uint8_t []){0x00}, 1},
6026 false,
6027
6028 QCBOR_SUCCESS, /* for GetBigFloat */
6029 -20,
6030 4294967295,
6031
6032 QCBOR_SUCCESS, /* for GetBigFloatBig */
6033 -20,
6034 {(const uint8_t []){0xff, 0xff, 0xff, 0xff}, 4},
6035 false
6036 },
6037
6038 {
6039 /* Special case for test 8. Don't renumber it. */
6040 "8. Untagged pair with big num (big float or decimal fraction), tag optional",
6041 {(const uint8_t []){0x82, 0x33, 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10}, 14},
6042 QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
6043 true,
6044
6045 QCBOR_SUCCESS, /* for GetNext */
6046 QCBOR_TYPE_ARRAY,
6047 0,
6048 0,
6049 {(const uint8_t []){0x00}, 1},
6050
6051 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW, /* GetDecimalFraction */
6052 0,
6053 0,
6054
6055 QCBOR_SUCCESS, /* for GetDecimalFractionBig */
6056 -20,
6057 {(const uint8_t []){0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10}, 10},
6058 false,
6059
6060 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW, /* for GetBigFloat */
6061 0,
6062 0,
6063
6064 QCBOR_SUCCESS, /* for GetBigFloatBig */
6065 -20,
6066 {(const uint8_t []){0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10}, 10},
6067 false
6068 },
6069
6070 {
6071 "9. decimal fraction with large exponent and negative big num mantissa",
6072 {(const uint8_t []){0xC4, 0x82, 0x1B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
6073 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10}, 23},
6074 QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
6075 true,
6076
6077 QCBOR_SUCCESS, /* for GetNext */
6078 QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM,
6079 9223372036854775807,
6080 0,
6081 {(const uint8_t []){0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10}, 10},
6082
6083 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW, /* GetDecimalFraction */
6084 0,
6085 0,
6086
6087 QCBOR_SUCCESS, /* for GetDecimalFractionBig */
6088 9223372036854775807,
6089 {(const uint8_t []){0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10}, 10},
6090 true,
6091
6092 QCBOR_ERR_UNEXPECTED_TYPE, /* for GetBigFloat */
6093 0,
6094 0,
6095
6096 QCBOR_ERR_UNEXPECTED_TYPE, /* for GetBigFloatBig */
6097 0,
6098 {(const uint8_t []){0x00}, 1},
6099 false
6100 },
6101};
6102
6103
6104
6105int32_t ProcessEaMTests(void)
Laurence Lundblade59289e52019-12-30 13:44:37 -08006106{
Laurence Lundblade37286c02022-09-03 10:05:02 -07006107 size_t uIndex;
6108 QCBORDecodeContext DCtx;
6109 QCBORItem Item;
6110 QCBORError uError;
6111 int64_t nMantissa, nExponent;
6112 MakeUsefulBufOnStack( MantissaBuf, 200);
6113 UsefulBufC Mantissa;
6114 bool bMantissaIsNegative;
6115
6116 for(uIndex = 0; uIndex < C_ARRAY_COUNT(pEaMTests, struct EaMTest); uIndex++) {
6117 const struct EaMTest *pT = &pEaMTests[uIndex];
6118 /* Decode with GetNext */
6119 QCBORDecode_Init(&DCtx, pT->Input, 0);
6120
6121 if(uIndex + 1 == 9) {
6122 nExponent = 99; // just to set a break point
6123 }
6124
6125 uError = QCBORDecode_GetNext(&DCtx, &Item);
6126#ifdef QCBOR_DISABLE_TAGS
6127 /* Test 8 is a special case when tags are disabled */
6128 if(pT->bHasTags && uIndex + 1 != 8) {
6129 if(uError != QCBOR_ERR_TAGS_DISABLED) {
6130 return (int32_t)(1+uIndex) * 1000 + 9;
6131 }
6132 } else {
6133#endif
6134 /* Now check return code, data type, mantissa and exponent */
6135 if(pT->uExpectedErrorGN != uError) {
6136 return (int32_t)(1+uIndex) * 1000 + 1;
6137 }
6138 if(uError == QCBOR_SUCCESS && pT->uQCBORTypeGN != QCBOR_TYPE_ARRAY) {
6139 if(pT->uQCBORTypeGN != Item.uDataType) {
6140 return (int32_t)(1+uIndex) * 1000 + 2;
6141 }
6142 if(pT->nExponentGN != Item.val.expAndMantissa.nExponent) {
6143 return (int32_t)(1+uIndex) * 1000 + 3;
6144 }
6145 if(Item.uDataType == QCBOR_TYPE_DECIMAL_FRACTION || Item.uDataType == QCBOR_TYPE_BIGFLOAT ) {
6146 if(pT->nMantissaGN != Item.val.expAndMantissa.Mantissa.nInt) {
6147 return (int32_t)(1+uIndex) * 1000 + 4;
6148 }
6149 } else {
6150 if(UsefulBuf_Compare(Item.val.expAndMantissa.Mantissa.bigNum, pT->MantissaGN)) {
6151 return (int32_t)(1+uIndex) * 1000 + 5;
6152 }
6153 }
6154 }
6155#ifdef QCBOR_DISABLE_TAGS
6156 }
6157#endif
6158
6159 /* Decode with GetDecimalFraction */
6160 QCBORDecode_Init(&DCtx, pT->Input, 0);
6161 QCBORDecode_GetDecimalFraction(&DCtx,
6162 pT->uTagRequirement,
6163 &nMantissa,
6164 &nExponent);
6165 uError = QCBORDecode_GetAndResetError(&DCtx);
6166#ifdef QCBOR_DISABLE_TAGS
6167 if(pT->bHasTags) {
6168 if(uError != QCBOR_ERR_TAGS_DISABLED) {
6169 return (int32_t)(1+uIndex) * 1000 + 39;
6170 }
6171 } else {
6172#endif
6173 /* Now check return code, mantissa and exponent */
6174 if(pT->uExpectedErrorGDF != uError) {
6175 return (int32_t)(1+uIndex) * 1000 + 31;
6176 }
6177 if(uError == QCBOR_SUCCESS) {
6178 if(pT->nExponentGDF != nExponent) {
6179 return (int32_t)(1+uIndex) * 1000 + 32;
6180 }
6181 if(pT->nMantissaGDF != nMantissa) {
6182 return (int32_t)(1+uIndex) * 1000 + 33;
6183 }
6184 }
6185#ifdef QCBOR_DISABLE_TAGS
6186 }
6187#endif
6188
6189 /* Decode with GetDecimalFractionBig */
6190 QCBORDecode_Init(&DCtx, pT->Input, 0);
6191 QCBORDecode_GetDecimalFractionBig(&DCtx,
6192 pT->uTagRequirement,
6193 MantissaBuf,
6194 &Mantissa,
6195 &bMantissaIsNegative,
6196 &nExponent);
6197 uError = QCBORDecode_GetAndResetError(&DCtx);
6198#ifdef QCBOR_DISABLE_TAGS
6199 if(pT->bHasTags) {
6200 if(uError != QCBOR_ERR_TAGS_DISABLED) {
6201 return (int32_t)(1+uIndex) * 1000 + 49;
6202 }
6203 } else {
6204#endif
6205 /* Now check return code, mantissa (bytes and sign) and exponent */
6206 if(pT->uExpectedErrorGDFB != uError) {
6207 return (int32_t)(1+uIndex) * 1000 + 41;
6208 }
6209 if(uError == QCBOR_SUCCESS) {
6210 if(pT->nExponentGDFB != nExponent) {
6211 return (int32_t)(1+uIndex) * 1000 + 42;
6212 }
6213 if(pT->IsNegativeGDFB != bMantissaIsNegative) {
6214 return (int32_t)(1+uIndex) * 1000 + 43;
6215 }
6216 if(UsefulBuf_Compare(Mantissa, pT->MantissaGDFB)) {
6217 return (int32_t)(1+uIndex) * 1000 + 44;
6218 }
6219 }
6220#ifdef QCBOR_DISABLE_TAGS
6221 }
6222#endif
6223
6224 /* Decode with GetBigFloat */
6225 QCBORDecode_Init(&DCtx, pT->Input, 0);
6226 QCBORDecode_GetBigFloat(&DCtx,
6227 pT->uTagRequirement,
6228 &nMantissa,
6229 &nExponent);
6230 uError = QCBORDecode_GetAndResetError(&DCtx);
6231#ifdef QCBOR_DISABLE_TAGS
6232 if(pT->bHasTags) {
6233 if(uError != QCBOR_ERR_TAGS_DISABLED) {
6234 return (int32_t)(1+uIndex) * 1000 + 19;
6235 }
6236 } else {
6237#endif
6238 /* Now check return code, mantissa and exponent */
6239 if(pT->uExpectedErrorGBF != uError) {
6240 return (int32_t)(1+uIndex) * 1000 + 11;
6241 }
6242 if(uError == QCBOR_SUCCESS) {
6243 if(pT->nExponentGBF != nExponent) {
6244 return (int32_t)(1+uIndex) * 1000 + 12;
6245 }
6246 if(pT->nMantissaGBF != nMantissa) {
6247 return (int32_t)(1+uIndex) * 1000 + 13;
6248 }
6249 }
6250#ifdef QCBOR_DISABLE_TAGS
6251 }
6252#endif
6253
6254 /* Decode with GetBigFloatBig */
6255 QCBORDecode_Init(&DCtx, pT->Input, 0);
6256 QCBORDecode_GetBigFloatBig(&DCtx,
6257 pT->uTagRequirement,
6258 MantissaBuf,
6259 &Mantissa,
6260 &bMantissaIsNegative,
6261 &nExponent);
6262 uError = QCBORDecode_GetAndResetError(&DCtx);
6263#ifdef QCBOR_DISABLE_TAGS
6264 if(pT->bHasTags) {
6265 if(uError != QCBOR_ERR_TAGS_DISABLED) {
6266 return (int32_t)(1+uIndex) * 1000 + 29;
6267 }
6268 } else {
6269#endif
6270 /* Now check return code, mantissa (bytes and sign) and exponent */
6271 if(pT->uExpectedErrorGBFB != uError) {
6272 return (int32_t)(1+uIndex) * 1000 + 21;
6273 }
6274 if(uError == QCBOR_SUCCESS) {
6275 if(pT->nExponentGBFB != nExponent) {
6276 return (int32_t)(1+uIndex) * 1000 + 22;
6277 }
6278 if(pT->IsNegativeGBFB != bMantissaIsNegative) {
6279 return (int32_t)(1+uIndex) * 1000 + 23;
6280 }
6281 if(UsefulBuf_Compare(Mantissa, pT->MantissaGBFB)) {
6282 return (int32_t)(1+uIndex) * 1000 + 24;
6283 }
6284 }
6285#ifdef QCBOR_DISABLE_TAGS
6286 }
6287#endif
6288 }
6289
6290 return 0;
6291}
6292
6293
6294int32_t ExponentAndMantissaDecodeTestsSecondary(void)
6295{
6296#ifndef QCBOR_DISABLE_TAGS
Laurence Lundblade59289e52019-12-30 13:44:37 -08006297 QCBORDecodeContext DC;
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07006298 QCBORError uErr;
Laurence Lundblade59289e52019-12-30 13:44:37 -08006299 QCBORItem item;
6300
Laurence Lundblade17af4902020-01-07 19:11:55 -08006301 static const uint8_t spBigNumMantissa[] = {0x01, 0x02, 0x03, 0x04, 0x05,
6302 0x06, 0x07, 0x08, 0x09, 0x010};
6303 UsefulBufC BN = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNumMantissa);
Laurence Lundblade59289e52019-12-30 13:44:37 -08006304
6305
Laurence Lundblade59289e52019-12-30 13:44:37 -08006306
6307 /* Now encode some stuff and then decode it */
6308 uint8_t pBuf[40];
6309 QCBOREncodeContext EC;
6310 UsefulBufC Encoded;
6311
6312 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(pBuf));
6313 QCBOREncode_OpenArray(&EC);
6314 QCBOREncode_AddDecimalFraction(&EC, 999, 1000); // 999 * (10 ^ 1000)
Laurence Lundblade0f143652024-10-06 11:06:19 -07006315 QCBOREncode_AddTBigFloat(&EC, QCBOR_ENCODE_AS_TAG, 100, INT32_MIN);
6316 QCBOREncode_AddTDecimalFractionBigNum(&EC, QCBOR_ENCODE_AS_TAG, BN, false, INT32_MAX);
Laurence Lundblade59289e52019-12-30 13:44:37 -08006317 QCBOREncode_CloseArray(&EC);
6318 QCBOREncode_Finish(&EC, &Encoded);
6319
6320
6321 QCBORDecode_Init(&DC, Encoded, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07006322 uErr = QCBORDecode_GetNext(&DC, &item);
6323 if(uErr != QCBOR_SUCCESS) {
6324 return 100;
Laurence Lundblade59289e52019-12-30 13:44:37 -08006325 }
6326
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07006327 uErr = QCBORDecode_GetNext(&DC, &item);
6328 if(uErr != QCBOR_SUCCESS) {
6329 return 101;
Laurence Lundblade59289e52019-12-30 13:44:37 -08006330 }
6331
6332 if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION ||
6333 item.val.expAndMantissa.nExponent != 1000 ||
6334 item.val.expAndMantissa.Mantissa.nInt != 999) {
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07006335 return 102;
Laurence Lundblade59289e52019-12-30 13:44:37 -08006336 }
6337
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07006338 uErr = QCBORDecode_GetNext(&DC, &item);
6339 if(uErr != QCBOR_SUCCESS) {
6340 return 103;
Laurence Lundblade59289e52019-12-30 13:44:37 -08006341 }
6342
6343 if(item.uDataType != QCBOR_TYPE_BIGFLOAT ||
6344 item.val.expAndMantissa.nExponent != INT32_MIN ||
6345 item.val.expAndMantissa.Mantissa.nInt != 100) {
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07006346 return 104;
Laurence Lundblade59289e52019-12-30 13:44:37 -08006347 }
6348
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07006349 uErr = QCBORDecode_GetNext(&DC, &item);
6350 if(uErr != QCBOR_SUCCESS) {
6351 return 105;
Laurence Lundblade59289e52019-12-30 13:44:37 -08006352 }
6353
6354 if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM ||
6355 item.val.expAndMantissa.nExponent != INT32_MAX ||
6356 UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) {
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07006357 return 106;
6358 }
6359
Laurence Lundblade37286c02022-09-03 10:05:02 -07006360#endif /* QCBOR_TAGS_DISABLED */
Laurence Lundblade59289e52019-12-30 13:44:37 -08006361
6362 return 0;
6363}
6364
6365
Laurence Lundblade37286c02022-09-03 10:05:02 -07006366int32_t ExponentAndMantissaDecodeTests(void)
6367{
6368 int32_t rv = ProcessEaMTests();
6369 if(rv) {
6370 return rv;
6371 }
6372
6373 return ExponentAndMantissaDecodeTestsSecondary();
6374}
6375
6376
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08006377static const struct DecodeFailTestInput ExponentAndMantissaFailures[] = {
6378 { "Exponent > INT64_MAX",
6379 QCBOR_DECODE_MODE_NORMAL,
6380 {"\xC4\x82\x1B\x7f\xFF\xFF\xFF\xFF\xFF\xFF\xFF\x1B\x80\xFF\xFF\xFF\xFF\xFF\xFF\xFF", 20},
6381 QCBOR_ERR_BAD_EXP_AND_MANTISSA
6382 },
6383 { "Mantissa > INT64_MAX",
6384 QCBOR_DECODE_MODE_NORMAL,
6385 {"\xC4\x82\x1B\x80\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xC3\x4A\x01\x02\x03\x04\x05\x06\x07\x08\x09\x10", 23},
6386 QCBOR_ERR_BAD_EXP_AND_MANTISSA
6387 },
6388 {
6389 "End of input",
6390 QCBOR_DECODE_MODE_NORMAL,
6391 {"\xC4\x82", 2},
6392 QCBOR_ERR_NO_MORE_ITEMS
6393 },
6394 {"bad content for big num",
6395 QCBOR_DECODE_MODE_NORMAL,
6396 {"\xC4\x82\x01\xc3\x01", 5},
6397 QCBOR_ERR_BAD_OPT_TAG
6398 },
6399 {"bad content for big num",
6400 QCBOR_DECODE_MODE_NORMAL,
Laurence Lundbladecdbbc192024-06-28 15:13:04 -07006401 {"\xC4\x82\xC2\x01\x1F", 5},
6402 QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08006403 },
6404 {"Bad integer for exponent",
6405 QCBOR_DECODE_MODE_NORMAL,
6406 {"\xC4\x82\x01\x1f", 4},
6407 QCBOR_ERR_BAD_INT
6408 },
6409 {"Bad integer for mantissa",
6410 QCBOR_DECODE_MODE_NORMAL,
6411 {"\xC4\x82\x1f\x01", 4},
6412 QCBOR_ERR_BAD_INT
6413 },
6414 {"3 items in array",
6415 QCBOR_DECODE_MODE_NORMAL,
6416 {"\xC4\x83\x03\x01\x02", 5},
6417 QCBOR_ERR_BAD_EXP_AND_MANTISSA},
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08006418#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08006419 {"unterminated indefinite length array",
6420 QCBOR_DECODE_MODE_NORMAL,
6421 {"\xC4\x9f\x03\x01\x02", 5},
6422 QCBOR_ERR_BAD_EXP_AND_MANTISSA
6423 },
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08006424#else /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08006425 {"unterminated indefinite length array",
6426 QCBOR_DECODE_MODE_NORMAL,
6427 {"\xC4\x9f\x03\x01\x02", 5},
6428 QCBOR_ERR_INDEF_LEN_ARRAYS_DISABLED
6429 },
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08006430#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08006431 {"Empty array",
6432 QCBOR_DECODE_MODE_NORMAL,
6433 {"\xC4\x80", 2},
6434 QCBOR_ERR_NO_MORE_ITEMS
6435 },
6436 {"Second is not an integer",
6437 QCBOR_DECODE_MODE_NORMAL,
6438 {"\xC4\x82\x03\x40", 4},
6439 QCBOR_ERR_BAD_EXP_AND_MANTISSA
6440 },
6441 {"First is not an integer",
6442 QCBOR_DECODE_MODE_NORMAL,
6443 {"\xC4\x82\x40", 3},
6444 QCBOR_ERR_BAD_EXP_AND_MANTISSA
6445 },
6446 {"Not an array",
6447 QCBOR_DECODE_MODE_NORMAL,
6448 {"\xC4\xA2", 2},
6449 QCBOR_ERR_BAD_EXP_AND_MANTISSA
6450 }
Laurence Lundblade59289e52019-12-30 13:44:37 -08006451};
6452
6453
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08006454int32_t
6455ExponentAndMantissaDecodeFailTests(void)
Laurence Lundblade59289e52019-12-30 13:44:37 -08006456{
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08006457 return ProcessDecodeFailures(ExponentAndMantissaFailures,
6458 C_ARRAY_COUNT(ExponentAndMantissaFailures,
6459 struct DecodeFailTestInput));
Laurence Lundblade59289e52019-12-30 13:44:37 -08006460}
6461
Laurence Lundbladedd6e76e2021-03-10 01:54:01 -07006462#endif /* QCBOR_DISABLE_EXP_AND_MANTISSA */
Laurence Lundbladebb87be22020-04-09 19:15:32 -07006463
6464
6465
6466/*
6467 Some basic CBOR with map and array used in a lot of tests.
6468 The map labels are all strings
6469
Laurence Lundblade8ffdb742020-05-07 02:49:18 -07006470 {
6471 "first integer": 42,
Laurence Lundbladebb87be22020-04-09 19:15:32 -07006472 "an array of two strings": [
6473 "string1", "string2"
6474 ],
6475 "map in a map": {
6476 "bytes 1": h'78787878',
6477 "bytes 2": h'79797979',
6478 "another int": 98,
6479 "text 2": "lies, damn lies and statistics"
6480 }
Laurence Lundbladeddebabe2020-11-22 11:32:17 -08006481 }
Laurence Lundbladebb87be22020-04-09 19:15:32 -07006482 */
Laurence Lundblade9b334962020-08-27 10:55:53 -07006483
Laurence Lundbladef7a70bc2020-10-24 12:23:25 -07006484int32_t SpiffyDecodeBasicMap(UsefulBufC input)
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07006485{
6486 QCBORItem Item1, Item2, Item3;
6487 int64_t nDecodedInt1, nDecodedInt2;
6488 UsefulBufC B1, B2, S1, S2, S3;
6489
6490 QCBORDecodeContext DCtx;
6491 QCBORError nCBORError;
6492
6493 QCBORDecode_Init(&DCtx, input, 0);
6494
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006495 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07006496
6497 QCBORDecode_GetInt64InMapSZ(&DCtx, "first integer", &nDecodedInt1);
6498
6499 QCBORDecode_EnterMapFromMapSZ(&DCtx, "map in a map");
6500 QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2);
Laurence Lundblade323f8a92020-09-06 19:43:09 -07006501 QCBORDecode_GetByteStringInMapSZ(&DCtx, "bytes 1", &B1);
6502 QCBORDecode_GetByteStringInMapSZ(&DCtx, "bytes 2", &B2);
6503 QCBORDecode_GetTextStringInMapSZ(&DCtx, "text 2", &S1);
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07006504 QCBORDecode_ExitMap(&DCtx);
6505
6506 QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings");
6507 QCBORDecode_GetNext(&DCtx, &Item1);
6508 QCBORDecode_GetNext(&DCtx, &Item2);
6509 if(QCBORDecode_GetNext(&DCtx, &Item3) != QCBOR_ERR_NO_MORE_ITEMS) {
6510 return -400;
6511 }
6512 QCBORDecode_ExitArray(&DCtx);
6513
6514 // Parse the same array again using GetText() instead of GetItem()
6515 QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings");
Laurence Lundblade323f8a92020-09-06 19:43:09 -07006516 QCBORDecode_GetTextString(&DCtx, &S2);
6517 QCBORDecode_GetTextString(&DCtx, &S3);
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07006518 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) {
6519 return 5000;
6520 }
6521 /* QCBORDecode_GetText(&DCtx, &S3);
6522 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_NO_MORE_ITEMS) {
6523 return 5001;
6524 } */
6525
6526 QCBORDecode_ExitArray(&DCtx);
6527
6528 QCBORDecode_ExitMap(&DCtx);
6529
6530 nCBORError = QCBORDecode_Finish(&DCtx);
6531
6532 if(nCBORError) {
6533 return (int32_t)nCBORError;
6534 }
6535
6536 if(nDecodedInt1 != 42) {
6537 return 1001;
6538 }
6539
6540 if(nDecodedInt2 != 98) {
6541 return 1002;
6542 }
6543
6544 if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07006545 UsefulBufCompareToSZ(Item1.val.string, "string1")) {
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07006546 return 1003;
6547 }
6548
6549 if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07006550 UsefulBufCompareToSZ(Item2.val.string, "string2")) {
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07006551 return 1004;
6552 }
6553
Laurence Lundblade9b334962020-08-27 10:55:53 -07006554 if(UsefulBufCompareToSZ(S1, "lies, damn lies and statistics")) {
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07006555 return 1005;
6556 }
6557
6558 if(UsefulBuf_Compare(B1, UsefulBuf_FromSZ("xxxx"))){
6559 return 1006;
6560 }
6561
6562 if(UsefulBuf_Compare(B2, UsefulBuf_FromSZ("yyyy"))){
6563 return 1007;
6564 }
6565
6566 if(UsefulBuf_Compare(S2, UsefulBuf_FromSZ("string1"))){
6567 return 1008;
6568 }
6569
6570 if(UsefulBuf_Compare(S3, UsefulBuf_FromSZ("string2"))){
6571 return 1009;
6572 }
6573
6574 return 0;
6575}
6576
Laurence Lundbladeddebabe2020-11-22 11:32:17 -08006577/*
Laurence Lundbladecc7da412020-12-27 00:09:07 -08006578 {
6579 -75008: h'05083399',
6580 88: [],
6581 100100: {
6582 "sub1": {
6583 10: [
6584 0
6585 ],
6586 -75009: h'A46823990001',
6587 100100: {
6588 "json": "{ \"ueid\", \"xyz\"}",
6589 "subsub": {
6590 100002: h'141813191001'
6591 }
6592 }
Laurence Lundbladeddebabe2020-11-22 11:32:17 -08006593 }
Laurence Lundbladecc7da412020-12-27 00:09:07 -08006594 }
Laurence Lundbladeddebabe2020-11-22 11:32:17 -08006595 }
Laurence Lundbladeddebabe2020-11-22 11:32:17 -08006596 */
6597
6598static const uint8_t spNestedCBOR[] = {
Laurence Lundbladecc7da412020-12-27 00:09:07 -08006599 0xa3, 0x3a, 0x00, 0x01, 0x24, 0xff, 0x44, 0x05,
6600 0x08, 0x33, 0x99, 0x18, 0x58, 0x80, 0x1a, 0x00,
6601 0x01, 0x87, 0x04, 0xa1, 0x64, 0x73, 0x75, 0x62,
6602 0x31, 0xa3, 0x0a, 0x81, 0x00, 0x3a, 0x00, 0x01,
6603 0x25, 0x00, 0x46, 0xa4, 0x68, 0x23, 0x99, 0x00,
6604 0x01, 0x1a, 0x00, 0x01, 0x87, 0x04, 0xa2, 0x64,
6605 0x6a, 0x73, 0x6f, 0x6e, 0x70, 0x7b, 0x20, 0x22,
6606 0x75, 0x65, 0x69, 0x64, 0x22, 0x2c, 0x20, 0x22,
6607 0x78, 0x79, 0x7a, 0x22, 0x7d, 0x66, 0x73, 0x75,
6608 0x62, 0x73, 0x75, 0x62, 0xa1, 0x1a, 0x00, 0x01,
6609 0x86, 0xa2, 0x46, 0x14, 0x18, 0x13, 0x19, 0x10,
6610 0x01
Laurence Lundbladeddebabe2020-11-22 11:32:17 -08006611};
6612
6613/* Get item in multi-level nesting in spNestedCBOR */
6614static int32_t DecodeNestedGetSubSub(QCBORDecodeContext *pDCtx)
6615{
6616 UsefulBufC String;
6617
6618 uint8_t test_oemid_bytes[] = {0x14, 0x18, 0x13, 0x19, 0x10, 0x01};
6619 const struct q_useful_buf_c test_oemid = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(test_oemid_bytes);
6620
6621 QCBORDecode_EnterMapFromMapN(pDCtx, 100100);
6622 QCBORDecode_EnterMap(pDCtx, NULL);
6623 QCBORDecode_EnterMapFromMapN(pDCtx, 100100);
6624 QCBORDecode_EnterMapFromMapSZ(pDCtx, "subsub");
6625 QCBORDecode_GetByteStringInMapN(pDCtx, 100002, &String);
6626 if(QCBORDecode_GetError(pDCtx)) {
6627 return 4001;
6628 }
6629 if(UsefulBuf_Compare(String, test_oemid)) {
6630 return 4002;
6631 }
6632 QCBORDecode_ExitMap(pDCtx);
6633 QCBORDecode_ExitMap(pDCtx);
6634 QCBORDecode_ExitMap(pDCtx);
6635 QCBORDecode_ExitMap(pDCtx);
6636
6637 return 0;
6638}
6639
6640/* Iterations on the zero-length array in spNestedCBOR */
6641static int32_t DecodeNestedGetEmpty(QCBORDecodeContext *pDCtx)
6642{
6643 QCBORItem Item;
6644 QCBORError uErr;
6645
6646 QCBORDecode_EnterArrayFromMapN(pDCtx, 88);
6647 for(int x = 0; x < 20; x++) {
6648 uErr = QCBORDecode_GetNext(pDCtx, &Item);
6649 if(uErr != QCBOR_ERR_NO_MORE_ITEMS) {
6650 return 4100;
6651
6652 }
6653 }
6654 QCBORDecode_ExitArray(pDCtx);
6655 if(QCBORDecode_GetError(pDCtx)) {
6656 return 4101;
6657 }
6658
6659 return 0;
6660}
6661
6662/* Various iterations on the array that contains a zero in spNestedCBOR */
6663static int32_t DecodeNestedGetZero(QCBORDecodeContext *pDCtx)
6664{
6665 QCBORError uErr;
6666
6667 QCBORDecode_EnterMapFromMapN(pDCtx, 100100);
6668 QCBORDecode_EnterMapFromMapSZ(pDCtx, "sub1");
6669 QCBORDecode_EnterArrayFromMapN(pDCtx, 10);
6670 int64_t nInt = 99;
6671 QCBORDecode_GetInt64(pDCtx, &nInt);
6672 if(nInt != 0) {
6673 return 4200;
6674 }
6675 for(int x = 0; x < 20; x++) {
6676 QCBORItem Item;
6677 uErr = QCBORDecode_GetNext(pDCtx, &Item);
6678 if(uErr != QCBOR_ERR_NO_MORE_ITEMS) {
6679 return 4201;
6680
6681 }
6682 }
6683 QCBORDecode_ExitArray(pDCtx);
6684 if(QCBORDecode_GetAndResetError(pDCtx)) {
6685 return 4202;
6686 }
6687 QCBORDecode_EnterArrayFromMapN(pDCtx, 10);
6688 UsefulBufC dD;
6689 QCBORDecode_GetByteString(pDCtx, &dD);
6690 if(QCBORDecode_GetAndResetError(pDCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
6691 return 4203;
6692 }
6693 for(int x = 0; x < 20; x++) {
6694 QCBORDecode_GetByteString(pDCtx, &dD);
6695 uErr = QCBORDecode_GetAndResetError(pDCtx);
6696 if(uErr != QCBOR_ERR_NO_MORE_ITEMS) {
6697 return 4204;
6698 }
6699 }
6700 QCBORDecode_ExitArray(pDCtx);
6701 QCBORDecode_ExitMap(pDCtx);
6702 QCBORDecode_ExitMap(pDCtx);
6703
6704 return 0;
6705}
6706
6707/* Repeatedly enter and exit maps and arrays, go off the end of maps
6708 and arrays and such. */
Laurence Lundbladeb9702452021-03-08 21:02:57 -08006709static int32_t DecodeNestedIterate(void)
Laurence Lundbladeddebabe2020-11-22 11:32:17 -08006710{
6711 QCBORDecodeContext DCtx;
6712 int32_t nReturn;
6713 QCBORError uErr;
6714
6715 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spNestedCBOR), 0);
6716 QCBORDecode_EnterMap(&DCtx, NULL);
6717
6718 for(int j = 0; j < 5; j++) {
6719 for(int i = 0; i < 20; i++) {
6720 nReturn = DecodeNestedGetSubSub(&DCtx);
6721 if(nReturn) {
6722 return nReturn;
6723 }
6724 }
6725
6726 for(int i = 0; i < 20; i++) {
6727 nReturn = DecodeNestedGetEmpty(&DCtx);
6728 if(nReturn ) {
6729 return nReturn;
6730 }
6731 }
6732
6733 for(int i = 0; i < 20; i++) {
6734 nReturn = DecodeNestedGetZero(&DCtx);
6735 if(nReturn ) {
6736 return nReturn;
6737 }
6738 }
6739 }
6740
6741 QCBORDecode_ExitMap(&DCtx);
6742 uErr = QCBORDecode_Finish(&DCtx);
6743 if(uErr) {
6744 return (int32_t)uErr + 4100;
6745 }
6746
6747 return 0;
6748}
6749
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07006750
6751/*
Laurence Lundbladecc7da412020-12-27 00:09:07 -08006752 [
6753 23,
6754 6000,
6755 h'67616C6163746963',
6756 h'686176656E20746F6B656E'
6757 ]
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07006758 */
6759static const uint8_t spSimpleArray[] = {
Laurence Lundblade9b334962020-08-27 10:55:53 -07006760 0x84,
6761 0x17,
6762 0x19, 0x17, 0x70,
6763 0x48, 0x67, 0x61, 0x6C, 0x61, 0x63, 0x74, 0x69, 0x63,
6764 0x4B, 0x68, 0x61, 0x76, 0x65, 0x6E, 0x20, 0x74, 0x6F, 0x6B, 0x65, 0x6E};
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07006765
Laurence Lundbladecc7da412020-12-27 00:09:07 -08006766/* [h'', {}, [], 0] */
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08006767static const uint8_t spArrayOfEmpty[] = {0x84, 0x40, 0xa0, 0x80, 0x00};
6768
Laurence Lundbladecc7da412020-12-27 00:09:07 -08006769/* {} */
Laurence Lundbladee6f15112020-07-23 18:44:16 -07006770static const uint8_t spEmptyMap[] = {0xa0};
6771
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08006772#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundbladecc7da412020-12-27 00:09:07 -08006773/* {} */
Laurence Lundbladee6f15112020-07-23 18:44:16 -07006774static const uint8_t spEmptyInDefinteLengthMap[] = {0xbf, 0xff};
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07006775
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08006776
Laurence Lundbladef0499502020-08-01 11:55:57 -07006777/*
6778 {
6779 0: [],
6780 9: [
6781 [],
6782 []
6783 ],
6784 8: {
6785 1: [],
6786 2: {},
6787 3: []
6788 },
6789 4: {},
6790 5: [],
6791 6: [
6792 [],
6793 []
6794 ]
Laurence Lundbladecc7da412020-12-27 00:09:07 -08006795 }
Laurence Lundbladef0499502020-08-01 11:55:57 -07006796 */
6797static const uint8_t spMapOfEmpty[] = {
Laurence Lundbladecc7da412020-12-27 00:09:07 -08006798 0xa6, 0x00, 0x80, 0x09, 0x82, 0x80, 0x80, 0x08,
6799 0xa3, 0x01, 0x80, 0x02, 0xa0, 0x03, 0x80, 0x04,
6800 0xa0, 0x05, 0x9f, 0xff, 0x06, 0x9f, 0x80, 0x9f,
6801 0xff, 0xff};
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07006802
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08006803#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
6804
6805
Laurence Lundblade93d3f532020-09-28 21:09:12 -07006806/*
6807 Too many tags
Laurence Lundblade93d3f532020-09-28 21:09:12 -07006808 Duplicate label
6809 Integer overflow
6810 Date overflow
6811
Laurence Lundbladecc7da412020-12-27 00:09:07 -08006812 {
6813 1: 224(225(226(227(4(0))))),
Laurence Lundbladecc7da412020-12-27 00:09:07 -08006814 3: -18446744073709551616,
6815 4: 1(1.0e+300),
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07006816 5: 0,
6817 8: 8
Laurence Lundbladecc7da412020-12-27 00:09:07 -08006818 }
Laurence Lundblade93d3f532020-09-28 21:09:12 -07006819 */
6820static const uint8_t spRecoverableMapErrors[] = {
Laurence Lundblade37286c02022-09-03 10:05:02 -07006821#ifndef QCBOR_DISABLE_TAGS
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07006822 0xa6,
Laurence Lundblade93d3f532020-09-28 21:09:12 -07006823 0x04, 0xc1, 0xfb, 0x7e, 0x37, 0xe4, 0x3c, 0x88, 0x00, 0x75, 0x9c,
Laurence Lundblade37286c02022-09-03 10:05:02 -07006824 0x01, 0xd8, 0xe0, 0xd8, 0xe1, 0xd8, 0xe2, 0xd8, 0xe3, 0xd8, 0x04, 0x00,
6825#else
6826 0xa4,
6827#endif
6828 0x03, 0x3b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
Laurence Lundblade93d3f532020-09-28 21:09:12 -07006829 0x05, 0x00,
6830 0x05, 0x00,
6831 0x08, 0x08,
Laurence Lundblade93d3f532020-09-28 21:09:12 -07006832};
6833
Laurence Lundbladecc7da412020-12-27 00:09:07 -08006834/* Bad break */
Laurence Lundblade93d3f532020-09-28 21:09:12 -07006835static const uint8_t spUnRecoverableMapError1[] = {
6836 0xa2, 0xff, 0x01, 0x00, 0x02, 0x00
6837};
6838
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08006839#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundbladecc7da412020-12-27 00:09:07 -08006840/* No more items */
Laurence Lundblade93d3f532020-09-28 21:09:12 -07006841static const uint8_t spUnRecoverableMapError2[] = {
6842 0xbf, 0x02, 0xbf, 0xff, 0x01, 0x00, 0x02, 0x00
6843};
6844
Laurence Lundbladecc7da412020-12-27 00:09:07 -08006845/* Hit end because string is too long */
Laurence Lundblade93d3f532020-09-28 21:09:12 -07006846static const uint8_t spUnRecoverableMapError3[] = {
6847 0xbf, 0x02, 0x69, 0x64, 0x64, 0xff
6848};
6849
Laurence Lundbladecc7da412020-12-27 00:09:07 -08006850/* Hit end because string is too long */
Laurence Lundblade93d3f532020-09-28 21:09:12 -07006851static const uint8_t spUnRecoverableMapError4[] = {
6852 0xbf,
6853 0x02, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f,
6854 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f,
6855 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
6856 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
6857 0xff
6858};
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08006859#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
Laurence Lundblade93d3f532020-09-28 21:09:12 -07006860
Laurence Lundblade63926052021-03-29 16:05:51 -07006861const unsigned char not_well_formed_submod_section[] = {
6862 0xa1, 0x14, 0x1f,
6863};
Laurence Lundblade93d3f532020-09-28 21:09:12 -07006864
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07006865
6866/* Array of length 3, but only two items. */
6867const unsigned char spBadConsumeInput[] = {
6868 0x83, 0x00, 0x00
6869};
6870
6871/* Tag nesting too deep. */
6872const unsigned char spBadConsumeInput2[] = {
6873 0x81,
6874 0xD8, 0x37,
6875 0xD8, 0x2C,
6876 0xD8, 0x21,
6877 0xD6,
6878 0xCB,
6879 00
6880};
6881
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07006882
6883const unsigned char spBadConsumeInput4[] = {
6884 0x81, 0x9f, 0x00, 0xff
6885};
6886
6887const unsigned char spBadConsumeInput5[] = {
6888 0xa1, 0x80, 0x00
6889};
6890
Laurence Lundbladec5f45e42023-12-18 09:23:20 -07006891/*
6892 Lots of nesting for various nesting tests.
6893 { 1:1,
6894 2:{
6895 21:21,
6896 22:{
6897 221:[2111, 2112, 2113],
6898 222:222,
6899 223: {}
6900 },
6901 23: 23
6902 },
6903 3:3,
6904 4: [ {} ]
6905 }
6906 */
6907static const uint8_t spNested[] = {
69080xA4, /* Map of 4 */
6909 0x01, 0x01, /* Map entry 1 : 1 */
6910 0x02, 0xA3, /* Map entry 2 : {, an array of 3 */
6911 0x15, 0x15, /* Map entry 21 : 21 */
6912 0x16, 0xA3, /* Map entry 22 : {, a map of 3 */
6913 0x18, 0xDD, 0x83, /* Map entry 221 : [ an array of 3 */
6914 0x19, 0x08, 0x3F, /* Array item 2111 */
6915 0x19, 0x08, 0x40, /* Array item 2112 */
6916 0x19, 0x08, 0x41, /* Array item 2113 */
6917 0x18, 0xDE, 0x18, 0xDE, /* Map entry 222 : 222 */
6918 0x18, 0xDF, 0xA0, /* Map entry 223 : {} */
6919 0x17, 0x17, /* Map entry 23 : 23 */
6920 0x03, 0x03, /* Map entry 3 : 3 */
6921 0x04, 0x81, /* Map entry 4: [, an array of 1 */
6922 0xA0 /* Array entry {}, an empty map */
6923};
6924
6925
6926static int32_t EnterMapCursorTest(void)
6927{
6928 QCBORDecodeContext DCtx;
6929 QCBORItem Item1;
Laurence Lundblade11654912024-05-09 11:49:24 -07006930 int64_t nInt;
6931 QCBORError uErr;
6932
6933 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spNested), 0);
6934 QCBORDecode_EnterMap(&DCtx, NULL);
6935 QCBORDecode_GetInt64InMapN (&DCtx, 3, &nInt);
6936 uErr = QCBORDecode_GetNext(&DCtx, &Item1);
Laurence Lundbladea29f45a2024-05-14 15:55:19 -07006937 if(uErr != QCBOR_SUCCESS) {
6938 return 701;
6939 }
Laurence Lundblade11654912024-05-09 11:49:24 -07006940 if(Item1.uDataType != QCBOR_TYPE_INT64) {
6941 return 700;
6942 }
6943
Laurence Lundbladec5f45e42023-12-18 09:23:20 -07006944
6945 int i;
6946 for(i = 0; i < 13; i++) {
6947 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spNested), 0);
6948 QCBORDecode_EnterMap(&DCtx, NULL);
6949 int j;
6950 /* Move travesal cursor */
6951 for(j = 0; j < i; j++) {
6952 QCBORDecode_GetNext(&DCtx, &Item1);
6953 }
6954 QCBORDecode_EnterMapFromMapN(&DCtx, 2);
6955 QCBORDecode_ExitMap(&DCtx);
6956 QCBORDecode_GetNext(&DCtx, &Item1);
6957 if(Item1.label.int64 != 3) {
6958 return 8000;
6959 }
6960 }
6961
6962 for(i = 0; i < 13; i++) {
6963 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spNested), 0);
6964 QCBORDecode_EnterMap(&DCtx, NULL);
6965 int j;
6966 /* Move travesal cursor */
6967 for(j = 0; j < i; j++) {
6968 QCBORDecode_GetNext(&DCtx, &Item1);
6969 }
6970 QCBORDecode_EnterMapFromMapN(&DCtx, 2);
6971 QCBORDecode_EnterMapFromMapN(&DCtx, 22);
6972 QCBORDecode_ExitMap(&DCtx);
6973 QCBORDecode_GetNext(&DCtx, &Item1);
6974 if(Item1.label.int64 != 23) {
6975 return 8000;
6976 }
6977 }
6978
6979 for(i = 0; i < 13; i++) {
6980 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spNested), 0);
6981 QCBORDecode_EnterMap(&DCtx, NULL);
6982 int j;
6983 /* Move travesal cursor */
6984 for(j = 0; j < i; j++) {
6985 QCBORDecode_GetNext(&DCtx, &Item1);
6986 }
6987 QCBORDecode_EnterMapFromMapN(&DCtx, 2);
6988 QCBORDecode_EnterMapFromMapN(&DCtx, 22);
6989 for(j = 0; j < i; j++) {
6990 QCBORDecode_GetNext(&DCtx, &Item1);
6991 }
6992 QCBORDecode_EnterArrayFromMapN(&DCtx, 221);
6993 QCBORDecode_ExitArray(&DCtx);
6994 QCBORDecode_ExitMap(&DCtx);
6995 QCBORDecode_GetNext(&DCtx, &Item1);
6996 if(Item1.label.int64 != 23) {
6997 return 8000;
6998 }
6999 QCBORDecode_ExitMap(&DCtx);
7000 QCBORDecode_GetNext(&DCtx, &Item1);
7001 if(Item1.label.int64 != 3) {
7002 return 8000;
7003 }
7004 }
7005
7006 return 0;
7007}
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07007008
7009
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03007010int32_t EnterMapTest(void)
Laurence Lundbladebb87be22020-04-09 19:15:32 -07007011{
Laurence Lundbladef0499502020-08-01 11:55:57 -07007012 QCBORItem Item1;
Laurence Lundbladebb87be22020-04-09 19:15:32 -07007013 QCBORDecodeContext DCtx;
Laurence Lundbladef0499502020-08-01 11:55:57 -07007014 int32_t nReturn;
7015 QCBORError uErr;
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07007016
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08007017#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07007018 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spMapOfEmpty), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07007019 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07007020
Laurence Lundbladef0499502020-08-01 11:55:57 -07007021
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07007022 QCBORDecode_EnterArray(&DCtx, NULL); // Label 0
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07007023 QCBORDecode_ExitArray(&DCtx);
7024
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07007025 QCBORDecode_EnterArray(&DCtx, NULL); // Label 9
7026 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07007027 QCBORDecode_ExitArray(&DCtx);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07007028 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07007029 QCBORDecode_ExitArray(&DCtx);
7030 QCBORDecode_ExitArray(&DCtx);
7031
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07007032 QCBORDecode_EnterMap(&DCtx, NULL); // Label 8
7033 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07007034 QCBORDecode_ExitArray(&DCtx);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07007035 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07007036 QCBORDecode_ExitMap(&DCtx);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07007037 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07007038 QCBORDecode_ExitArray(&DCtx);
7039 QCBORDecode_ExitMap(&DCtx);
7040
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07007041 QCBORDecode_EnterMap(&DCtx, NULL); // Label4
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07007042 QCBORDecode_ExitMap(&DCtx);
7043
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07007044 QCBORDecode_EnterArray(&DCtx, NULL); // Label 5
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07007045 QCBORDecode_ExitArray(&DCtx);
7046
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07007047 QCBORDecode_EnterArray(&DCtx, NULL); // Label 6
7048 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07007049 QCBORDecode_ExitArray(&DCtx);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07007050 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07007051 QCBORDecode_ExitArray(&DCtx);
7052 QCBORDecode_ExitArray(&DCtx);
7053
7054 QCBORDecode_ExitMap(&DCtx);
7055
7056 uErr = QCBORDecode_Finish(&DCtx);
7057 if(uErr != QCBOR_SUCCESS){
7058 return 3011;
7059 }
7060
Laurence Lundbladeec290b82024-06-10 11:10:54 -07007061#ifndef QCBOR_DISABLE_NON_INTEGER_LABELS
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07007062 (void)pValidMapIndefEncoded;
Laurence Lundbladef7a70bc2020-10-24 12:23:25 -07007063 nReturn = SpiffyDecodeBasicMap(UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapIndefEncoded));
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07007064 if(nReturn) {
7065 return nReturn + 20000;
Laurence Lundbladebb87be22020-04-09 19:15:32 -07007066 }
Laurence Lundbladeec290b82024-06-10 11:10:54 -07007067#endif /* ! QCBOR_DISABLE_NON_INTEGER_LABELS */
7068#endif /* ! QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08007069
Laurence Lundbladeec290b82024-06-10 11:10:54 -07007070#ifndef QCBOR_DISABLE_NON_INTEGER_LABELS
7071 QCBORItem ArrayItem;
Laurence Lundbladebb87be22020-04-09 19:15:32 -07007072
Laurence Lundbladef7a70bc2020-10-24 12:23:25 -07007073 nReturn = SpiffyDecodeBasicMap(UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded));
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07007074 if(nReturn) {
7075 return nReturn;
7076 }
Laurence Lundbladebb87be22020-04-09 19:15:32 -07007077
Laurence Lundblade8ffdb742020-05-07 02:49:18 -07007078
Laurence Lundblade937ea812020-05-08 11:38:23 -07007079
Laurence Lundblade2f467f92020-10-09 17:50:11 -07007080 // These tests confirm the cursor is at the right place after entering
7081 // a map or array
Laurence Lundblade9b334962020-08-27 10:55:53 -07007082 const UsefulBufC ValidEncodedMap = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded);
Laurence Lundblade937ea812020-05-08 11:38:23 -07007083
7084 // Confirm cursor is at right place
Laurence Lundblade9b334962020-08-27 10:55:53 -07007085 QCBORDecode_Init(&DCtx, ValidEncodedMap, 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07007086 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade937ea812020-05-08 11:38:23 -07007087 QCBORDecode_GetNext(&DCtx, &Item1);
7088 if(Item1.uDataType != QCBOR_TYPE_INT64) {
7089 return 2001;
7090 }
7091
7092
Laurence Lundblade9b334962020-08-27 10:55:53 -07007093 QCBORDecode_Init(&DCtx, ValidEncodedMap, 0);
Laurence Lundblade6f3f78e2020-08-31 13:09:14 -07007094 QCBORDecode_VGetNext(&DCtx, &Item1);
7095 QCBORDecode_VGetNext(&DCtx, &Item1);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07007096 QCBORDecode_EnterArray(&DCtx, &ArrayItem);
7097 if(ArrayItem.uLabelType != QCBOR_TYPE_TEXT_STRING ||
7098 UsefulBuf_Compare(ArrayItem.label.string,
7099 UsefulBuf_FROM_SZ_LITERAL("an array of two strings"))) {
7100 return 2051;
7101 }
Laurence Lundblade937ea812020-05-08 11:38:23 -07007102 QCBORDecode_GetNext(&DCtx, &Item1);
7103 if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING) {
7104 return 2002;
7105 }
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07007106 QCBORDecode_ExitArray(&DCtx);
7107 QCBORDecode_EnterMap(&DCtx, &ArrayItem);
7108 if(ArrayItem.uLabelType != QCBOR_TYPE_TEXT_STRING ||
7109 UsefulBuf_Compare(ArrayItem.label.string,
7110 UsefulBuf_FROM_SZ_LITERAL("map in a map"))) {
7111 return 2052;
7112 }
7113
Laurence Lundblade937ea812020-05-08 11:38:23 -07007114
Laurence Lundblade9b334962020-08-27 10:55:53 -07007115 QCBORDecode_Init(&DCtx, ValidEncodedMap, 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07007116 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade64b607e2020-05-13 13:05:57 -07007117 QCBORDecode_GetNext(&DCtx, &Item1);
7118 QCBORDecode_GetNext(&DCtx, &Item1);
7119 QCBORDecode_GetNext(&DCtx, &Item1);
Laurence Lundblade937ea812020-05-08 11:38:23 -07007120 QCBORDecode_EnterMapFromMapSZ(&DCtx, "map in a map");
7121 QCBORDecode_GetNext(&DCtx, &Item1);
7122 if(Item1.uDataType != QCBOR_TYPE_BYTE_STRING) {
7123 return 2003;
7124 }
7125
Laurence Lundblade9b334962020-08-27 10:55:53 -07007126 QCBORDecode_Init(&DCtx, ValidEncodedMap, 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07007127 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade937ea812020-05-08 11:38:23 -07007128 QCBORDecode_GetNext(&DCtx, &Item1);
7129 QCBORDecode_GetNext(&DCtx, &Item1);
7130 QCBORDecode_GetNext(&DCtx, &Item1);
7131 QCBORDecode_GetNext(&DCtx, &Item1);
7132 QCBORDecode_GetNext(&DCtx, &Item1);
7133 QCBORDecode_GetNext(&DCtx, &Item1);
7134 QCBORDecode_GetNext(&DCtx, &Item1);
7135 QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings");
7136 QCBORDecode_GetNext(&DCtx, &Item1);
7137 if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING) {
Laurence Lundblade64b607e2020-05-13 13:05:57 -07007138 return 2004;
Laurence Lundblade937ea812020-05-08 11:38:23 -07007139 }
7140
Laurence Lundblade9b334962020-08-27 10:55:53 -07007141 QCBORDecode_Init(&DCtx, ValidEncodedMap, 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07007142 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade2b843b52020-06-16 20:51:03 -07007143 QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings");
7144 QCBORDecode_ExitArray(&DCtx);
7145 QCBORDecode_GetNext(&DCtx, &Item1);
7146 if(Item1.uDataType != QCBOR_TYPE_MAP && Item1.uLabelAlloc != QCBOR_TYPE_TEXT_STRING) {
7147 return 2006;
7148 }
7149 QCBORDecode_ExitMap(&DCtx);
7150 if(QCBORDecode_GetNext(&DCtx, &Item1) != QCBOR_ERR_NO_MORE_ITEMS) {
7151 return 2007;
7152 }
Laurence Lundbladeec290b82024-06-10 11:10:54 -07007153#endif /* !QCBOR_DISABLE_NON_INTEGER_LABELS */
Laurence Lundblade2b843b52020-06-16 20:51:03 -07007154
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07007155 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSimpleArray), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07007156 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07007157 int64_t nDecodedInt2;
Laurence Lundbladeec290b82024-06-10 11:10:54 -07007158
7159 UsefulBufC String;
7160 QCBORDecode_GetTextStringInMapN(&DCtx, 88, &String);
7161 uErr = QCBORDecode_GetAndResetError(&DCtx);
7162 if(uErr != QCBOR_ERR_MAP_NOT_ENTERED){
7163 return 2009;
7164 }
7165#ifndef QCBOR_DISABLE_NON_INTEGER_LABELS
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07007166 QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2);
7167 uErr = QCBORDecode_GetAndResetError(&DCtx);
7168 if(uErr != QCBOR_ERR_MAP_NOT_ENTERED){
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07007169 return 2008;
7170 }
Laurence Lundbladeec290b82024-06-10 11:10:54 -07007171#endif /* ! QCBOR_DISABLE_NON_INTEGER_LABELS */
Laurence Lundblade937ea812020-05-08 11:38:23 -07007172
Laurence Lundbladee6f15112020-07-23 18:44:16 -07007173
7174 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEmptyMap), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07007175 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundbladee6f15112020-07-23 18:44:16 -07007176 // This will fail because the map is empty.
7177 QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2);
7178 uErr = QCBORDecode_GetAndResetError(&DCtx);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07007179 if(uErr != QCBOR_ERR_LABEL_NOT_FOUND){
Laurence Lundbladee6f15112020-07-23 18:44:16 -07007180 return 2010;
7181 }
7182 QCBORDecode_ExitMap(&DCtx);
7183 uErr = QCBORDecode_Finish(&DCtx);
7184 if(uErr != QCBOR_SUCCESS){
7185 return 2011;
7186 }
7187
7188
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08007189#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundbladee6f15112020-07-23 18:44:16 -07007190 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEmptyInDefinteLengthMap), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07007191 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundbladee6f15112020-07-23 18:44:16 -07007192 // This will fail because the map is empty.
7193 QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2);
7194 uErr = QCBORDecode_GetAndResetError(&DCtx);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07007195 if(uErr != QCBOR_ERR_LABEL_NOT_FOUND){
Laurence Lundblade085d7952020-07-24 10:26:30 -07007196 return 2012;
Laurence Lundbladee6f15112020-07-23 18:44:16 -07007197 }
7198 QCBORDecode_ExitMap(&DCtx);
7199 uErr = QCBORDecode_Finish(&DCtx);
7200 if(uErr != QCBOR_SUCCESS){
Laurence Lundblade085d7952020-07-24 10:26:30 -07007201 return 2013;
Laurence Lundbladee6f15112020-07-23 18:44:16 -07007202 }
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08007203#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
Laurence Lundbladee6f15112020-07-23 18:44:16 -07007204
Laurence Lundblade5f4e8712020-07-25 11:44:43 -07007205
7206 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spArrayOfEmpty), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07007207 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade323f8a92020-09-06 19:43:09 -07007208 QCBORDecode_GetByteString(&DCtx, &String);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07007209 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade5f4e8712020-07-25 11:44:43 -07007210 QCBORDecode_ExitMap(&DCtx);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07007211 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade5f4e8712020-07-25 11:44:43 -07007212 QCBORDecode_ExitArray(&DCtx);
7213 QCBORDecode_GetInt64(&DCtx, &nDecodedInt2);
7214 QCBORDecode_ExitArray(&DCtx);
7215 uErr = QCBORDecode_Finish(&DCtx);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07007216 if(uErr != QCBOR_SUCCESS) {
Laurence Lundblade5f4e8712020-07-25 11:44:43 -07007217 return 2014;
7218 }
7219
Laurence Lundblade93d3f532020-09-28 21:09:12 -07007220 int64_t nInt;
7221 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spRecoverableMapErrors), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07007222 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade37286c02022-09-03 10:05:02 -07007223#ifndef QCBOR_DISABLE_TAGS
Laurence Lundblade93d3f532020-09-28 21:09:12 -07007224 QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt);
Laurence Lundblade88e9db22020-11-02 03:56:33 -08007225 uErr = QCBORDecode_GetError(&DCtx);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07007226 if(uErr != QCBOR_ERR_TOO_MANY_TAGS) {
7227 return 2021;
7228 }
Laurence Lundblade88e9db22020-11-02 03:56:33 -08007229 if(QCBORDecode_GetNthTagOfLast(&DCtx, 0) != CBOR_TAG_INVALID64) {
7230 return 2121;
7231 }
7232 (void)QCBORDecode_GetAndResetError(&DCtx);
Laurence Lundblade37286c02022-09-03 10:05:02 -07007233#endif
Laurence Lundblade88e9db22020-11-02 03:56:33 -08007234
Laurence Lundblade93d3f532020-09-28 21:09:12 -07007235
Laurence Lundblade93d3f532020-09-28 21:09:12 -07007236 QCBORDecode_GetInt64InMapN(&DCtx, 0x03, &nInt);
7237 uErr = QCBORDecode_GetAndResetError(&DCtx);
7238 if(uErr != QCBOR_ERR_INT_OVERFLOW) {
7239 return 2023;
7240 }
7241
Laurence Lundblade37286c02022-09-03 10:05:02 -07007242#ifndef QCBOR_DISABLE_TAGS
Laurence Lundblade93d3f532020-09-28 21:09:12 -07007243 QCBORDecode_GetEpochDateInMapN(&DCtx, 0x04, QCBOR_TAG_REQUIREMENT_TAG, &nInt);
7244 uErr = QCBORDecode_GetAndResetError(&DCtx);
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007245 if(uErr != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_DATE_OVERFLOW)) {
Laurence Lundblade93d3f532020-09-28 21:09:12 -07007246 return 2024;
7247 }
Laurence Lundblade37286c02022-09-03 10:05:02 -07007248#endif
Laurence Lundblade93d3f532020-09-28 21:09:12 -07007249
7250 QCBORDecode_GetInt64InMapN(&DCtx, 0x05, &nInt);
7251 uErr = QCBORDecode_GetAndResetError(&DCtx);
7252 if(uErr != QCBOR_ERR_DUPLICATE_LABEL) {
7253 return 2025;
7254 }
7255
7256 QCBORDecode_GetInt64InMapN(&DCtx, 0x08, &nInt);
7257
7258 QCBORDecode_ExitMap(&DCtx);
7259 uErr = QCBORDecode_Finish(&DCtx);
7260 if(uErr != QCBOR_SUCCESS) {
7261 return 2026;
7262 }
7263
7264 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUnRecoverableMapError1), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07007265 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07007266 QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt);
7267 uErr = QCBORDecode_GetAndResetError(&DCtx);
7268 if(uErr != QCBOR_ERR_BAD_BREAK) {
7269 return 2030;
7270 }
7271
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08007272#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundblade93d3f532020-09-28 21:09:12 -07007273 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUnRecoverableMapError2), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07007274 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07007275 QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt);
7276 uErr = QCBORDecode_GetAndResetError(&DCtx);
7277 if(uErr != QCBOR_ERR_NO_MORE_ITEMS) {
7278 return 2031;
7279 }
7280
7281 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUnRecoverableMapError3), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07007282 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07007283 QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt);
7284 uErr = QCBORDecode_GetAndResetError(&DCtx);
7285 if(uErr != QCBOR_ERR_HIT_END) {
7286 return 2032;
7287 }
7288
7289 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUnRecoverableMapError4), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07007290 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07007291 QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt);
7292 uErr = QCBORDecode_GetAndResetError(&DCtx);
7293 if(uErr != QCBOR_ERR_ARRAY_DECODE_NESTING_TOO_DEEP) {
7294 return 2033;
7295 }
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08007296#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
7297
Laurence Lundbladeec290b82024-06-10 11:10:54 -07007298#ifndef QCBOR_DISABLE_NON_INTEGER_LABELS
Laurence Lundblade732e52d2021-02-22 20:11:01 -07007299 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0);
7300 QCBORDecode_VGetNextConsume(&DCtx, &Item1);
7301 if(Item1.uDataType != QCBOR_TYPE_MAP) {
7302 return 2401;
7303 }
7304 if(QCBORDecode_GetError(&DCtx)) {
7305 return 2402;
7306 }
7307
7308 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0);
7309 QCBORDecode_VGetNext(&DCtx, &Item1);
7310 if(Item1.uDataType != QCBOR_TYPE_MAP ||
7311 Item1.val.uCount != 3 ||
7312 Item1.uNextNestLevel != 1) {
7313 return 2403;
7314 }
7315 if(QCBORDecode_GetError(&DCtx)) {
7316 return 2404;
7317 }
7318 QCBORDecode_VGetNextConsume(&DCtx, &Item1);
7319 if(Item1.uDataType != QCBOR_TYPE_INT64 ||
7320 Item1.uNextNestLevel != 1 ||
7321 Item1.val.int64 != 42) {
7322 return 2405;
7323 }
7324 if(QCBORDecode_GetError(&DCtx)) {
7325 return 2406;
7326 }
7327 QCBORDecode_VGetNextConsume(&DCtx, &Item1);
7328 if(Item1.uDataType != QCBOR_TYPE_ARRAY ||
7329 Item1.uNestingLevel != 1 ||
7330 Item1.uNextNestLevel != 1 ||
7331 Item1.val.uCount != 2) {
7332 return 2407;
7333 }
7334 if(QCBORDecode_GetError(&DCtx)) {
7335 return 2408;
7336 }
7337 QCBORDecode_VGetNextConsume(&DCtx, &Item1);
7338 if(Item1.uDataType != QCBOR_TYPE_MAP ||
7339 Item1.uNestingLevel != 1 ||
7340 Item1.uNextNestLevel != 0 ||
7341 Item1.val.uCount != 4) {
7342 return 2409;
7343 }
7344 if(QCBORDecode_GetError(&DCtx)) {
7345 return 2410;
7346 }
Laurence Lundbladeec290b82024-06-10 11:10:54 -07007347#endif /* ! QCBOR_DISABLE_NON_INTEGER_LABELS */
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07007348
Laurence Lundbladeddebabe2020-11-22 11:32:17 -08007349 nReturn = DecodeNestedIterate();
7350
Laurence Lundblade63926052021-03-29 16:05:51 -07007351
7352 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(not_well_formed_submod_section), 0);
7353 QCBORDecode_EnterMap(&DCtx, NULL);
7354 QCBORDecode_EnterMapFromMapN(&DCtx, 20);
7355 if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_BAD_INT) {
7356 return 2500;
7357 }
7358
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07007359 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBadConsumeInput), 0);
7360 QCBORDecode_VGetNextConsume(&DCtx, &Item1);
7361 if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_NO_MORE_ITEMS) {
7362 return 2600;
7363 }
7364
Laurence Lundblade37286c02022-09-03 10:05:02 -07007365#ifndef QCBOR_DISABLE_TAGS
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07007366 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBadConsumeInput2), 0);
7367 QCBORDecode_VGetNextConsume(&DCtx, &Item1);
7368 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) {
7369 return 2700;
7370 }
Laurence Lundblade37286c02022-09-03 10:05:02 -07007371#endif
7372
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07007373
7374 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBadConsumeInput4), 0);
7375 QCBORDecode_VGetNextConsume(&DCtx, &Item1);
7376#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
7377 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) {
7378 return 2900;
7379 }
7380#else
7381 if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_INDEF_LEN_ARRAYS_DISABLED) {
7382 return 2901;
7383 }
7384#endif
7385
7386 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBadConsumeInput5), 0);
7387 QCBORDecode_VGetNextConsume(&DCtx, &Item1);
7388 if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_MAP_LABEL_TYPE) {
7389 return 3000;
7390 }
7391
Laurence Lundbladec5f45e42023-12-18 09:23:20 -07007392 nReturn = EnterMapCursorTest();
7393
Laurence Lundbladeddebabe2020-11-22 11:32:17 -08007394 return nReturn;
Laurence Lundblade9c905e82020-04-25 11:31:38 -07007395}
7396
7397
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007398struct NumberConversion {
7399 char *szDescription;
7400 UsefulBufC CBOR;
7401 int64_t nConvertedToInt64;
7402 QCBORError uErrorInt64;
7403 uint64_t uConvertToUInt64;
7404 QCBORError uErrorUint64;
7405 double dConvertToDouble;
7406 QCBORError uErrorDouble;
7407};
7408
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007409#ifndef QCBOR_DISABLE_EXP_AND_MANTISSA
7410#define EXP_AND_MANTISSA_ERROR(x) x
7411#else
7412#define EXP_AND_MANTISSA_ERROR(x) QCBOR_ERR_UNEXPECTED_TYPE
7413#endif
7414
7415
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07007416static const struct NumberConversion NumberConversions[] = {
Laurence Lundblade37286c02022-09-03 10:05:02 -07007417#ifndef QCBOR_DISABLE_TAGS
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007418 {
Laurence Lundblade4e808ba2022-12-29 12:45:20 -07007419 "Big float: INT64_MIN * 2e-1 to test handling of INT64_MIN",
7420 {(uint8_t[]){0xC5, 0x82, 0x20,
7421 0x3B, 0x7f, 0xff, 0xff, 0xff, 0xff, 0x0ff, 0xff, 0xff,
7422 }, 15},
7423 -4611686018427387904, /* INT64_MIN / 2 */
7424 EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS),
7425 0,
7426 EXP_AND_MANTISSA_ERROR(QCBOR_ERR_NUMBER_SIGN_CONVERSION),
7427 -4.6116860184273879E+18,
7428 FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS))
7429 },
7430 {
Laurence Lundblade784b54b2020-08-10 01:24:52 -07007431 "too large to fit into int64_t",
7432 {(uint8_t[]){0xc3, 0x48, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 10},
7433 0,
7434 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
7435 0,
7436 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
7437 ((double)INT64_MIN) + 1 ,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007438 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)
Laurence Lundblade784b54b2020-08-10 01:24:52 -07007439 },
7440 {
7441 "largest negative int that fits in int64_t",
7442 {(uint8_t[]){0xc3, 0x48, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, 10},
7443 INT64_MIN,
7444 QCBOR_SUCCESS,
7445 0,
7446 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
7447 (double)INT64_MIN,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007448 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)
Laurence Lundblade784b54b2020-08-10 01:24:52 -07007449 },
7450 {
Laurence Lundbladeda095972020-06-06 18:35:33 -07007451 "negative bignum -1",
7452 {(uint8_t[]){0xc3, 0x41, 0x00}, 3},
7453 -1,
7454 QCBOR_SUCCESS,
7455 0,
7456 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
7457 -1.0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007458 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)
Laurence Lundbladeda095972020-06-06 18:35:33 -07007459 },
7460 {
Laurence Lundblade9ab5abb2020-05-20 12:10:45 -07007461 "Decimal Fraction with positive bignum 257 * 10e3",
Laurence Lundblade887add82020-05-17 05:50:34 -07007462 {(uint8_t[]){0xC4, 0x82, 0x1B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03,
7463 0xC2, 0x42, 0x01, 0x01}, 15},
7464 257000,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007465 EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS),
Laurence Lundblade887add82020-05-17 05:50:34 -07007466 257000,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007467 EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS),
Laurence Lundblade887add82020-05-17 05:50:34 -07007468 257000.0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007469 FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS))
Laurence Lundblade887add82020-05-17 05:50:34 -07007470 },
7471 {
Laurence Lundbladeda095972020-06-06 18:35:33 -07007472 "bigfloat with negative bignum -258 * 2e3",
Laurence Lundblade887add82020-05-17 05:50:34 -07007473 {(uint8_t[]){0xC5, 0x82, 0x1B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03,
7474 0xC3, 0x42, 0x01, 0x01}, 15},
Laurence Lundbladeda095972020-06-06 18:35:33 -07007475 -2064,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007476 EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS),
Laurence Lundblade887add82020-05-17 05:50:34 -07007477 0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007478 EXP_AND_MANTISSA_ERROR(QCBOR_ERR_NUMBER_SIGN_CONVERSION),
Laurence Lundbladeda095972020-06-06 18:35:33 -07007479 -2064.0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007480 FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS))
Laurence Lundblade887add82020-05-17 05:50:34 -07007481 },
7482 {
Laurence Lundblade9ab5abb2020-05-20 12:10:45 -07007483 "bigfloat with positive bignum 257 * 2e3",
Laurence Lundblade887add82020-05-17 05:50:34 -07007484 {(uint8_t[]){0xC5, 0x82, 0x1B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03,
7485 0xC2, 0x42, 0x01, 0x01}, 15},
7486 2056,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007487 EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS),
Laurence Lundblade887add82020-05-17 05:50:34 -07007488 2056,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007489 EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS),
Laurence Lundblade887add82020-05-17 05:50:34 -07007490 2056.0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007491 FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS))
Laurence Lundblade887add82020-05-17 05:50:34 -07007492 },
7493 {
Laurence Lundbladeda095972020-06-06 18:35:33 -07007494 "negative bignum 0xc349010000000000000000 -18446744073709551617",
Laurence Lundblade887add82020-05-17 05:50:34 -07007495 {(uint8_t[]){0xc3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 11},
7496 0,
7497 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
7498 0,
7499 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
7500 -18446744073709551617.0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007501 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)
Laurence Lundblade887add82020-05-17 05:50:34 -07007502 },
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08007503#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
Laurence Lundblade887add82020-05-17 05:50:34 -07007504 {
Laurence Lundblade313b2862020-05-16 01:23:06 -07007505 "Positive bignum 0x01020304 indefinite length string",
7506 {(uint8_t[]){0xC2, 0x5f, 0x42, 0x01, 0x02, 0x41, 0x03, 0x41, 0x04, 0xff}, 10},
7507 0x01020304,
7508 QCBOR_SUCCESS,
7509 0x01020304,
7510 QCBOR_SUCCESS,
7511 16909060.0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007512 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)
Laurence Lundblade313b2862020-05-16 01:23:06 -07007513 },
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08007514#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
Laurence Lundblade313b2862020-05-16 01:23:06 -07007515 {
Laurence Lundblade887add82020-05-17 05:50:34 -07007516 "Decimal Fraction with neg bignum [9223372036854775807, -4759477275222530853137]",
Laurence Lundblade313b2862020-05-16 01:23:06 -07007517 {(uint8_t[]){0xC4, 0x82, 0x1B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
7518 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,}, 23},
7519 0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007520 EXP_AND_MANTISSA_ERROR(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW),
Laurence Lundblade313b2862020-05-16 01:23:06 -07007521 0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007522 EXP_AND_MANTISSA_ERROR(QCBOR_ERR_NUMBER_SIGN_CONVERSION),
Laurence Lundblade313b2862020-05-16 01:23:06 -07007523 -INFINITY,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007524 FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS))
Laurence Lundblade313b2862020-05-16 01:23:06 -07007525 },
7526 {
7527 "big float [9223372036854775806, 9223372036854775806]",
7528 {(uint8_t[]){0xC5, 0x82, 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE,
7529 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE}, 20},
7530 0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007531 EXP_AND_MANTISSA_ERROR(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW),
Laurence Lundblade313b2862020-05-16 01:23:06 -07007532 0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007533 EXP_AND_MANTISSA_ERROR(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW),
Laurence Lundblade313b2862020-05-16 01:23:06 -07007534 INFINITY,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007535 FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS))
Laurence Lundblade313b2862020-05-16 01:23:06 -07007536 },
7537 {
Laurence Lundblade983500d2020-05-14 11:49:34 -07007538 "Big float 3 * 2^^2",
7539 {(uint8_t[]){0xC5, 0x82, 0x02, 0x03}, 4},
7540 12,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007541 EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS),
Laurence Lundblade983500d2020-05-14 11:49:34 -07007542 12,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007543 EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS),
Laurence Lundblade983500d2020-05-14 11:49:34 -07007544 12.0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007545 FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS))
Laurence Lundblade983500d2020-05-14 11:49:34 -07007546 },
Laurence Lundblade983500d2020-05-14 11:49:34 -07007547 {
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007548 "Decimal fraction 3/10",
7549 {(uint8_t[]){0xC4, 0x82, 0x20, 0x03}, 4},
7550 0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007551 EXP_AND_MANTISSA_ERROR(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW),
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007552 0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007553 EXP_AND_MANTISSA_ERROR(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW),
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007554 0.30000000000000004,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007555 FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS))
Laurence Lundbladedfd49fc2020-09-01 14:17:16 -07007556 },
7557 {
Laurence Lundblade11fd78b2020-09-01 22:13:27 -07007558 "extreme pos bignum",
7559 {(uint8_t[]){0xc2, 0x59, 0x01, 0x90,
7560 // 50 rows of 8 is 400 digits.
7561 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7562 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7563 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7564 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7565 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7566 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7567 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7568 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7569 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7570 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7571 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7572 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7573 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7574 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7575 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
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,
gtravisb787e82a2023-11-30 18:38:21 -08007610 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0},
Laurence Lundblade11fd78b2020-09-01 22:13:27 -07007611 404},
7612 0,
7613 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
7614 0,
7615 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
Laurence Lundblade11fd78b2020-09-01 22:13:27 -07007616 INFINITY,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007617 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS),
Laurence Lundblade11fd78b2020-09-01 22:13:27 -07007618 },
7619
7620 {
7621 "extreme neg bignum",
7622 {(uint8_t[]){0xc3, 0x59, 0x01, 0x90,
7623 // 50 rows of 8 is 400 digits.
7624 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7625 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7626 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7627 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7628 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7629 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7630 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7631 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7632 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7633 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7634 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7635 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7636 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7637 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7638 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7639 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7640 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7641 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7642 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7643 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7644 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7645 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7646 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7647 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7648 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7649 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7650 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7651 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7652 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7653 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7654 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7655 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7656 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7657 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7658 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7659 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7660 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7661 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7662 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7663 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7664 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7665 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7666 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7667 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7668 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7669 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7670 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7671 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7672 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
gtravisb787e82a2023-11-30 18:38:21 -08007673 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0},
Laurence Lundblade11fd78b2020-09-01 22:13:27 -07007674 404},
7675 0,
7676 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
7677 0,
7678 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
Laurence Lundblade11fd78b2020-09-01 22:13:27 -07007679 -INFINITY,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007680 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)
Laurence Lundblade11fd78b2020-09-01 22:13:27 -07007681 },
Laurence Lundblade51722fd2020-09-02 13:01:33 -07007682
7683 {
7684 "big float underflow [9223372036854775806, -9223372036854775806]",
7685 {(uint8_t[]){
7686 0xC5, 0x82,
7687 0x3B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE,
7688 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE}, 20},
Laurence Lundblade51722fd2020-09-02 13:01:33 -07007689 0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007690 EXP_AND_MANTISSA_ERROR(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW),
Laurence Lundblade51722fd2020-09-02 13:01:33 -07007691 0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007692 EXP_AND_MANTISSA_ERROR(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW),
Laurence Lundblade51722fd2020-09-02 13:01:33 -07007693 0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007694 FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS))
Laurence Lundblade51722fd2020-09-02 13:01:33 -07007695 },
7696
7697 {
7698 "bigfloat that evaluates to -INFINITY",
7699 {(uint8_t[]){
7700 0xC5, 0x82,
7701 0x1B, 0x70, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03,
7702 0xC3, 0x42, 0x01, 0x01}, 15},
Laurence Lundblade51722fd2020-09-02 13:01:33 -07007703 0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007704 EXP_AND_MANTISSA_ERROR(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW),
Laurence Lundblade51722fd2020-09-02 13:01:33 -07007705 0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007706 EXP_AND_MANTISSA_ERROR(QCBOR_ERR_NUMBER_SIGN_CONVERSION),
Laurence Lundblade51722fd2020-09-02 13:01:33 -07007707 -INFINITY,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007708 FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS))
Laurence Lundblade51722fd2020-09-02 13:01:33 -07007709 },
Laurence Lundblade37286c02022-09-03 10:05:02 -07007710 {
7711 "Positive bignum 0xffff",
7712 {(uint8_t[]){0xC2, 0x42, 0xff, 0xff}, 4},
7713 65536-1,
7714 QCBOR_SUCCESS,
7715 0xffff,
7716 QCBOR_SUCCESS,
7717 65535.0,
7718 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)
7719 },
7720#endif /* QCBOR_DISABLE_TAGS */
7721 {
7722 "Positive integer 18446744073709551615",
7723 {(uint8_t[]){0x1b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, 9},
7724 0,
7725 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
7726 18446744073709551615ULL,
7727 QCBOR_SUCCESS,
7728 18446744073709551615.0,
7729 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)
7730 },
7731
7732 {
7733 "Postive integer 0",
7734 {(uint8_t[]){0x0}, 1},
7735 0LL,
7736 QCBOR_SUCCESS,
7737 0ULL,
7738 QCBOR_SUCCESS,
7739 0.0,
7740 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)
7741 },
7742 {
7743 "Negative integer -18446744073709551616",
7744 {(uint8_t[]){0x3b, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }, 9},
7745 -9223372036854775807-1, // INT64_MIN
7746 QCBOR_SUCCESS,
7747 0ULL,
7748 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
7749 -9223372036854775808.0,
7750 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)
7751 },
7752 {
7753 "Double Floating point value 100.3",
7754 {(uint8_t[]){0xfb, 0x40, 0x59, 0x13, 0x33, 0x33, 0x33, 0x33, 0x33}, 9},
7755 100L,
7756 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS),
7757 100ULL,
7758 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS),
7759 100.3,
7760 FLOAT_ERR_CODE_NO_FLOAT(QCBOR_SUCCESS),
7761 },
7762 {
7763 "Floating point value NaN 0xfa7fc00000",
7764 {(uint8_t[]){0xfa, 0x7f, 0xc0, 0x00, 0x00}, 5},
7765 0,
7766 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_FLOAT_EXCEPTION),
7767 0,
7768 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_FLOAT_EXCEPTION),
7769 NAN,
7770 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS),
7771 },
7772 {
7773 "half-precision Floating point value -4",
7774 {(uint8_t[]){0xf9, 0xc4, 0x00}, 3},
7775 // Normal case with all enabled.
7776 -4,
7777 FLOAT_ERR_CODE_NO_HALF_PREC_NO_FLOAT_HW(QCBOR_SUCCESS),
7778 0,
7779 FLOAT_ERR_CODE_NO_HALF_PREC_NO_FLOAT_HW(QCBOR_ERR_NUMBER_SIGN_CONVERSION),
7780 -4.0,
7781 FLOAT_ERR_CODE_NO_HALF_PREC(QCBOR_SUCCESS)
7782 },
7783 {
7784 "+inifinity single precision",
7785 {(uint8_t[]){0xfa, 0x7f, 0x80, 0x00, 0x00}, 5},
7786 0,
7787 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_FLOAT_EXCEPTION),
7788 0,
7789 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW),
7790 INFINITY,
7791 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)
7792 },
7793
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007794};
Laurence Lundblade9c905e82020-04-25 11:31:38 -07007795
7796
7797
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08007798
7799static int32_t SetUpDecoder(QCBORDecodeContext *DCtx, UsefulBufC CBOR, UsefulBuf Pool)
7800{
7801 QCBORDecode_Init(DCtx, CBOR, QCBOR_DECODE_MODE_NORMAL);
7802#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
7803 if(QCBORDecode_SetMemPool(DCtx, Pool, 0)) {
7804 return 1;
7805 }
7806#else /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
7807 (void)Pool;
7808#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
7809 return 0;
7810}
7811
7812
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03007813int32_t IntegerConvertTest(void)
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007814{
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08007815 const int nNumTests = C_ARRAY_COUNT(NumberConversions,
7816 struct NumberConversion);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007817
Laurence Lundblade9ab5abb2020-05-20 12:10:45 -07007818 for(int nIndex = 0; nIndex < nNumTests; nIndex++) {
7819 const struct NumberConversion *pF = &NumberConversions[nIndex];
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07007820
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007821 // Set up the decoding context including a memory pool so that
7822 // indefinite length items can be checked
7823 QCBORDecodeContext DCtx;
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007824 UsefulBuf_MAKE_STACK_UB(Pool, 100);
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07007825
7826 /* ----- test conversion to int64_t ------ */
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08007827 if(SetUpDecoder(&DCtx, pF->CBOR, Pool)) {
7828 return (int32_t)(3333+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007829 }
7830
7831 int64_t nInt;
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007832 QCBORDecode_GetInt64ConvertAll(&DCtx, 0xffff, &nInt);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07007833 if(QCBORDecode_GetError(&DCtx) != pF->uErrorInt64) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07007834 return (int32_t)(2000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007835 }
7836 if(pF->uErrorInt64 == QCBOR_SUCCESS && pF->nConvertedToInt64 != nInt) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07007837 return (int32_t)(3000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007838 }
7839
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07007840 /* ----- test conversion to uint64_t ------ */
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08007841 if(SetUpDecoder(&DCtx, pF->CBOR, Pool)) {
7842 return (int32_t)(3333+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007843 }
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08007844
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007845 uint64_t uInt;
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007846 QCBORDecode_GetUInt64ConvertAll(&DCtx, 0xffff, &uInt);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07007847 if(QCBORDecode_GetError(&DCtx) != pF->uErrorUint64) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07007848 return (int32_t)(4000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007849 }
7850 if(pF->uErrorUint64 == QCBOR_SUCCESS && pF->uConvertToUInt64 != uInt) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07007851 return (int32_t)(5000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007852 }
7853
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07007854 /* ----- test conversion to double ------ */
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08007855 if(SetUpDecoder(&DCtx, pF->CBOR, Pool)) {
7856 return (int32_t)(3333+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007857 }
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007858
7859#ifndef USEFULBUF_DISABLE_ALL_FLOAT
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007860 double d;
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007861 QCBORDecode_GetDoubleConvertAll(&DCtx, 0xffff, &d);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07007862 if(QCBORDecode_GetError(&DCtx) != pF->uErrorDouble) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07007863 return (int32_t)(6000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007864 }
7865 if(pF->uErrorDouble == QCBOR_SUCCESS) {
7866 if(isnan(pF->dConvertToDouble)) {
Laurence Lundblade983500d2020-05-14 11:49:34 -07007867 // NaN's can't be compared for equality. A NaN is
7868 // never equal to anything including another NaN
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007869 if(!isnan(d)) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07007870 return (int32_t)(7000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007871 }
7872 } else {
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007873 if(pF->dConvertToDouble != d) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07007874 return (int32_t)(8000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007875 }
7876 }
7877 }
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007878#endif /* USEFULBUF_DISABLE_ALL_FLOAT */
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007879 }
7880
7881 return 0;
7882}
7883
Laurence Lundbladea8758502022-05-15 17:57:46 -07007884#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
7885
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03007886int32_t CBORTestIssue134(void)
David Navarro9123e5b2022-03-28 16:04:03 +02007887{
7888 QCBORDecodeContext DCtx;
7889 QCBORItem Item;
7890 QCBORError uCBORError;
7891 const uint8_t spTestIssue134[] = { 0x5F, 0x40, 0xFF };
Laurence Lundblade9c905e82020-04-25 11:31:38 -07007892
David Navarro9123e5b2022-03-28 16:04:03 +02007893 QCBORDecode_Init(&DCtx,
7894 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTestIssue134),
7895 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade97c61bf2020-05-02 11:24:06 -07007896
David Navarro9123e5b2022-03-28 16:04:03 +02007897 UsefulBuf_MAKE_STACK_UB(StringBuf, 200);
7898 QCBORDecode_SetMemPool(&DCtx, StringBuf, false);
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03007899
David Navarro9123e5b2022-03-28 16:04:03 +02007900 do {
7901 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
7902 } while (QCBOR_SUCCESS == uCBORError);
7903
7904 uCBORError = QCBORDecode_Finish(&DCtx);
7905
Laurence Lundblade11ea3612022-07-01 13:26:23 -07007906 return (int32_t)uCBORError;
David Navarro9123e5b2022-03-28 16:04:03 +02007907}
Laurence Lundblade97c61bf2020-05-02 11:24:06 -07007908
Laurence Lundbladea8758502022-05-15 17:57:46 -07007909#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
7910
Laurence Lundblade37286c02022-09-03 10:05:02 -07007911
7912
7913static const uint8_t spSequenceTestInput[] = {
7914 /* 1. The valid date string "1985-04-12" */
7915 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string
7916
7917 /* 2. */
7918 0x00,
7919
7920 /* 3. A valid epoch date, 1400000000; Tue, 13 May 2014 16:53:20 GMT */
7921 0x1a, 0x53, 0x72, 0x4E, 0x00,
7922
7923 /* 4. */
7924 0x62, 'h', 'i',
7925};
7926
7927
Laurence Lundbladee3553422020-05-02 11:11:17 -07007928int32_t CBORSequenceDecodeTests(void)
7929{
7930 QCBORDecodeContext DCtx;
Laurence Lundblade87495732021-02-26 10:05:55 -07007931 QCBORItem Item;
7932 QCBORError uCBORError;
7933 size_t uConsumed;
Laurence Lundbladee3553422020-05-02 11:11:17 -07007934
7935 // --- Test a sequence with extra bytes ---
Laurence Lundblade9b334962020-08-27 10:55:53 -07007936
Laurence Lundbladee3553422020-05-02 11:11:17 -07007937 QCBORDecode_Init(&DCtx,
Laurence Lundblade37286c02022-09-03 10:05:02 -07007938 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSequenceTestInput),
Laurence Lundbladee3553422020-05-02 11:11:17 -07007939 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade9b334962020-08-27 10:55:53 -07007940
Laurence Lundblade37286c02022-09-03 10:05:02 -07007941 // Get 1.
Laurence Lundbladee3553422020-05-02 11:11:17 -07007942 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
7943 if(uCBORError != QCBOR_SUCCESS) {
7944 return 1;
7945 }
Laurence Lundblade37286c02022-09-03 10:05:02 -07007946 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ) {
Laurence Lundbladee3553422020-05-02 11:11:17 -07007947 return 2;
7948 }
Laurence Lundbladec7114722020-08-13 05:11:40 -07007949
Laurence Lundblade87495732021-02-26 10:05:55 -07007950 uCBORError = QCBORDecode_PartialFinish(&DCtx, &uConsumed);
7951 if(uCBORError != QCBOR_ERR_EXTRA_BYTES ||
Laurence Lundblade37286c02022-09-03 10:05:02 -07007952 uConsumed != 11) {
7953 return 102;
7954 }
7955
7956 // Get 2.
7957 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
7958 if(uCBORError != QCBOR_SUCCESS) {
7959 return 66;
7960 }
7961
7962 uCBORError = QCBORDecode_PartialFinish(&DCtx, &uConsumed);
7963 if(uCBORError != QCBOR_ERR_EXTRA_BYTES ||
Laurence Lundblade87495732021-02-26 10:05:55 -07007964 uConsumed != 12) {
7965 return 102;
7966 }
7967
Laurence Lundblade37286c02022-09-03 10:05:02 -07007968 // Get 3.
Laurence Lundbladec7114722020-08-13 05:11:40 -07007969 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundbladee3553422020-05-02 11:11:17 -07007970 if(uCBORError != QCBOR_SUCCESS) {
7971 return 2;
7972 }
Laurence Lundblade37286c02022-09-03 10:05:02 -07007973 if(Item.uDataType != QCBOR_TYPE_INT64) {
Laurence Lundbladee3553422020-05-02 11:11:17 -07007974 return 3;
7975 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07007976
Laurence Lundbladee3553422020-05-02 11:11:17 -07007977 // A sequence can have stuff at the end that may
7978 // or may not be valid CBOR. The protocol decoder knows
7979 // when to stop by definition of the protocol, not
7980 // when the top-level map or array is ended.
7981 // Finish still has to be called to know that
7982 // maps and arrays (if there were any) were closed
7983 // off correctly. When called like this it
7984 // must return the error QCBOR_ERR_EXTRA_BYTES.
7985 uCBORError = QCBORDecode_Finish(&DCtx);
7986 if(uCBORError != QCBOR_ERR_EXTRA_BYTES) {
7987 return 4;
7988 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07007989
Laurence Lundbladee3553422020-05-02 11:11:17 -07007990 // --- Test an empty input ----
7991 uint8_t empty[1];
7992 UsefulBufC Empty = {empty, 0};
7993 QCBORDecode_Init(&DCtx,
7994 Empty,
7995 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade9b334962020-08-27 10:55:53 -07007996
Laurence Lundbladee3553422020-05-02 11:11:17 -07007997 uCBORError = QCBORDecode_Finish(&DCtx);
7998 if(uCBORError != QCBOR_SUCCESS) {
7999 return 5;
8000 }
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07008001
8002
Laurence Lundbladee3553422020-05-02 11:11:17 -07008003 // --- Sequence with unclosed indefinite length array ---
8004 static const uint8_t xx[] = {0x01, 0x9f, 0x02};
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07008005
Laurence Lundbladee3553422020-05-02 11:11:17 -07008006 QCBORDecode_Init(&DCtx,
8007 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(xx),
8008 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07008009
Laurence Lundbladee3553422020-05-02 11:11:17 -07008010 // Get the first item
8011 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
8012 if(uCBORError != QCBOR_SUCCESS) {
8013 return 7;
8014 }
8015 if(Item.uDataType != QCBOR_TYPE_INT64) {
8016 return 8;
8017 }
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07008018
Laurence Lundbladee3553422020-05-02 11:11:17 -07008019 // Get a second item
8020 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08008021#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundbladee3553422020-05-02 11:11:17 -07008022 if(uCBORError != QCBOR_SUCCESS) {
8023 return 9;
8024 }
8025 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
8026 return 10;
8027 }
8028
8029 // Try to finish before consuming all bytes to confirm
8030 // that the still-open error is returned.
8031 uCBORError = QCBORDecode_Finish(&DCtx);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07008032 if(uCBORError != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
Laurence Lundbladee3553422020-05-02 11:11:17 -07008033 return 11;
8034 }
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08008035#else /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
8036 if(uCBORError != QCBOR_ERR_INDEF_LEN_ARRAYS_DISABLED) {
8037 return 20;
8038 }
8039#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
Laurence Lundbladee3553422020-05-02 11:11:17 -07008040
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07008041
Laurence Lundbladee3553422020-05-02 11:11:17 -07008042 // --- Sequence with a closed indefinite length array ---
8043 static const uint8_t yy[] = {0x01, 0x9f, 0xff};
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07008044
Laurence Lundbladee3553422020-05-02 11:11:17 -07008045 QCBORDecode_Init(&DCtx,
8046 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(yy),
8047 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07008048
Laurence Lundbladee3553422020-05-02 11:11:17 -07008049 // Get the first item
8050 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
8051 if(uCBORError != QCBOR_SUCCESS) {
8052 return 12;
8053 }
8054 if(Item.uDataType != QCBOR_TYPE_INT64) {
8055 return 13;
8056 }
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07008057
Laurence Lundbladee3553422020-05-02 11:11:17 -07008058 // Get a second item
8059 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08008060#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
8061
Laurence Lundbladee3553422020-05-02 11:11:17 -07008062 if(uCBORError != QCBOR_SUCCESS) {
8063 return 14;
8064 }
8065 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
8066 return 15;
8067 }
8068
8069 // Try to finish before consuming all bytes to confirm
8070 // that the still-open error is returned.
8071 uCBORError = QCBORDecode_Finish(&DCtx);
8072 if(uCBORError != QCBOR_SUCCESS) {
8073 return 16;
8074 }
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08008075#else /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
8076 if(uCBORError != QCBOR_ERR_INDEF_LEN_ARRAYS_DISABLED) {
8077 return 20;
8078 }
8079#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
Laurence Lundbladee3553422020-05-02 11:11:17 -07008080
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07008081
Laurence Lundbladee3553422020-05-02 11:11:17 -07008082 return 0;
8083}
8084
Laurence Lundbladee15326f2020-06-15 15:50:23 -07008085
Laurence Lundblade70ecead2020-06-15 19:40:06 -07008086
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03008087int32_t IntToTests(void)
Laurence Lundbladee15326f2020-06-15 15:50:23 -07008088{
8089 int nErrCode;
8090 int32_t n32;
8091 int16_t n16;
8092 int8_t n8;
8093 uint32_t u32;
8094 uint16_t u16;
8095 uint8_t u8;
8096 uint64_t u64;
8097
8098 nErrCode = QCBOR_Int64ToInt32(1, &n32);
8099 if(nErrCode == -1 || n32 != 1) {
8100 return 1;
8101 }
8102
8103 nErrCode = QCBOR_Int64ToInt32((int64_t)INT32_MAX, &n32);
8104 if(nErrCode == -1 || n32 != INT32_MAX) {
8105 return 2;
8106 }
8107
8108 nErrCode = QCBOR_Int64ToInt32((int64_t)INT32_MIN, &n32);
8109 if(nErrCode == -1 || n32 != INT32_MIN) {
8110 return 3;
8111 }
8112
8113 nErrCode = QCBOR_Int64ToInt32(((int64_t)INT32_MAX)+1, &n32);
8114 if(nErrCode != -1) {
8115 return 4;
8116 }
8117
8118 nErrCode = QCBOR_Int64ToInt32(((int64_t)INT32_MIN)-1, &n32);
8119 if(nErrCode != -1) {
8120 return 5;
8121 }
8122
8123
8124 nErrCode = QCBOR_Int64ToInt16((int64_t)INT16_MAX, &n16);
8125 if(nErrCode == -1 || n16 != INT16_MAX) {
8126 return 6;
8127 }
8128
8129 nErrCode = QCBOR_Int64ToInt16((int64_t)INT16_MIN, &n16);
8130 if(nErrCode == -1 || n16 != INT16_MIN) {
8131 return 7;
8132 }
8133
8134 nErrCode = QCBOR_Int64ToInt16(1, &n16);
8135 if(nErrCode == -1 || n16 != 1) {
8136 return 8;
8137 }
8138
8139 nErrCode = QCBOR_Int64ToInt16(((int64_t)INT16_MAX)+1, &n16);
8140 if(nErrCode != -1) {
8141 return 9;
8142 }
8143
8144 nErrCode = QCBOR_Int64ToInt16(((int64_t)INT16_MIN)-1, &n16);
8145 if(nErrCode != -1) {
8146 return 10;
8147 }
8148
8149
8150 nErrCode = QCBOR_Int64ToInt8(1, &n8);
8151 if(nErrCode == -1 || n8 != 1) {
8152 return 11;
8153 }
8154
8155 nErrCode = QCBOR_Int64ToInt8((int64_t)INT8_MAX, &n8);
8156 if(nErrCode == -1 || n8 != INT8_MAX) {
8157 return 12;
8158 }
8159
8160 nErrCode = QCBOR_Int64ToInt8((int64_t)INT8_MIN, &n8);
8161 if(nErrCode == -1 || n8 != INT8_MIN) {
8162 return 13;
8163 }
8164
8165 nErrCode = QCBOR_Int64ToInt8(((int64_t)INT8_MAX)+1, &n8);
8166 if(nErrCode != -1) {
8167 return 14;
8168 }
8169
8170 nErrCode = QCBOR_Int64ToInt8(((int64_t)INT8_MIN)-1, &n8);
8171 if(nErrCode != -1) {
8172 return 15;
8173 }
8174
8175
8176 nErrCode = QCBOR_Int64ToUInt32(1, &u32);
8177 if(nErrCode == -1 || u32 != 1) {
8178 return 16;
8179 }
8180
8181 nErrCode = QCBOR_Int64ToUInt32((int64_t)UINT32_MAX, &u32);
8182 if(nErrCode == -1 || u32 != UINT32_MAX) {
8183 return 17;
8184 }
8185
8186 nErrCode = QCBOR_Int64ToUInt32((int64_t)0, &u32);
8187 if(nErrCode == -1 || u32 != 0) {
8188 return 18;
8189 }
8190
8191 nErrCode = QCBOR_Int64ToUInt32(((int64_t)UINT32_MAX)+1, &u32);
8192 if(nErrCode != -1) {
8193 return 19;
8194 }
8195
8196 nErrCode = QCBOR_Int64ToUInt32((int64_t)-1, &u32);
8197 if(nErrCode != -1) {
8198 return 20;
8199 }
8200
8201
Sebastian Burck5a0ab732024-06-29 06:31:20 +02008202 nErrCode = QCBOR_Int64ToUInt16((int64_t)UINT16_MAX, &u16);
Laurence Lundbladee15326f2020-06-15 15:50:23 -07008203 if(nErrCode == -1 || u16 != UINT16_MAX) {
8204 return 21;
8205 }
8206
Sebastian Burck5a0ab732024-06-29 06:31:20 +02008207 nErrCode = QCBOR_Int64ToUInt16((int64_t)0, &u16);
Laurence Lundbladee15326f2020-06-15 15:50:23 -07008208 if(nErrCode == -1 || u16 != 0) {
8209 return 22;
8210 }
8211
Sebastian Burck5a0ab732024-06-29 06:31:20 +02008212 nErrCode = QCBOR_Int64ToUInt16(1, &u16);
Laurence Lundbladee15326f2020-06-15 15:50:23 -07008213 if(nErrCode == -1 || u16 != 1) {
8214 return 23;
8215 }
8216
Sebastian Burck5a0ab732024-06-29 06:31:20 +02008217 nErrCode = QCBOR_Int64ToUInt16(((int64_t)UINT16_MAX)+1, &u16);
Laurence Lundbladee15326f2020-06-15 15:50:23 -07008218 if(nErrCode != -1) {
8219 return 24;
8220 }
8221
Sebastian Burck5a0ab732024-06-29 06:31:20 +02008222 nErrCode = QCBOR_Int64ToUInt16((int64_t)-1, &u16);
Laurence Lundbladee15326f2020-06-15 15:50:23 -07008223 if(nErrCode != -1) {
8224 return 25;
8225 }
8226
8227
8228 nErrCode = QCBOR_Int64ToUInt8((int64_t)UINT8_MAX, &u8);
8229 if(nErrCode == -1 || u8 != UINT8_MAX) {
8230 return 26;
8231 }
8232
8233 nErrCode = QCBOR_Int64ToUInt8((int64_t)0, &u8);
8234 if(nErrCode == -1 || u8 != 0) {
8235 return 27;
8236 }
8237
8238 nErrCode = QCBOR_Int64ToUInt8(1, &u8);
8239 if(nErrCode == -1 || u8 != 1) {
8240 return 28;
8241 }
8242
8243 nErrCode = QCBOR_Int64ToUInt8(((int64_t)UINT16_MAX)+1, &u8);
8244 if(nErrCode != -1) {
8245 return 29;
8246 }
8247
8248 nErrCode = QCBOR_Int64ToUInt8((int64_t)-1, &u8);
8249 if(nErrCode != -1) {
8250 return 30;
8251 }
8252
8253
8254 nErrCode = QCBOR_Int64ToUInt64(1, &u64);
8255 if(nErrCode == -1 || u64 != 1) {
8256 return 31;
8257 }
8258
8259 nErrCode = QCBOR_Int64ToUInt64(INT64_MAX, &u64);
8260 if(nErrCode == -1 || u64 != INT64_MAX) {
8261 return 32;
8262 }
8263
8264 nErrCode = QCBOR_Int64ToUInt64((int64_t)0, &u64);
8265 if(nErrCode == -1 || u64 != 0) {
8266 return 33;
8267 }
8268
8269 nErrCode = QCBOR_Int64ToUInt64((int64_t)-1, &u64);
8270 if(nErrCode != -1) {
8271 return 34;
8272 }
8273
8274 return 0;
8275}
8276
Laurence Lundblade0750fc42020-06-20 21:02:34 -07008277
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07008278
8279
Laurence Lundblade0750fc42020-06-20 21:02:34 -07008280/*
8281A sequence with
8282 A wrapping bstr
8283 containing a map
8284 1
8285 2
8286 A wrapping bstr
8287 containing an array
8288 3
8289 wrapping bstr
8290 4
8291 5
8292 6
8293 array
8294 7
8295 8
Laurence Lundblade0750fc42020-06-20 21:02:34 -07008296 */
8297
Laurence Lundblade55013642020-09-23 05:39:22 -07008298static UsefulBufC EncodeBstrWrapTestData(UsefulBuf OutputBuffer)
Laurence Lundblade0750fc42020-06-20 21:02:34 -07008299{
Laurence Lundblade55013642020-09-23 05:39:22 -07008300 UsefulBufC Encoded;
Laurence Lundblade0750fc42020-06-20 21:02:34 -07008301 QCBOREncodeContext EC;
Laurence Lundblade55013642020-09-23 05:39:22 -07008302 QCBORError uErr;
Laurence Lundblade0750fc42020-06-20 21:02:34 -07008303
Laurence Lundblade55013642020-09-23 05:39:22 -07008304 QCBOREncode_Init(&EC, OutputBuffer);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07008305
8306 QCBOREncode_BstrWrap(&EC);
8307 QCBOREncode_OpenMap(&EC);
8308 QCBOREncode_AddInt64ToMapN(&EC, 100, 1);
8309 QCBOREncode_AddInt64ToMapN(&EC, 200, 2);
8310 QCBOREncode_CloseMap(&EC);
8311 QCBOREncode_BstrWrap(&EC);
8312 QCBOREncode_OpenArray(&EC);
8313 QCBOREncode_AddInt64(&EC, 3);
8314 QCBOREncode_BstrWrap(&EC);
8315 QCBOREncode_AddInt64(&EC, 4);
8316 QCBOREncode_CloseBstrWrap(&EC, NULL);
8317 QCBOREncode_AddInt64(&EC, 5);
8318 QCBOREncode_CloseArray(&EC);
8319 QCBOREncode_CloseBstrWrap(&EC, NULL);
8320 QCBOREncode_AddInt64(&EC, 6);
8321 QCBOREncode_CloseBstrWrap(&EC, NULL);
8322 QCBOREncode_OpenArray(&EC);
8323 QCBOREncode_AddInt64(&EC, 7);
8324 QCBOREncode_AddInt64(&EC, 8);
8325 QCBOREncode_CloseArray(&EC);
8326
8327 uErr = QCBOREncode_Finish(&EC, &Encoded);
Laurence Lundblade40a04322020-06-27 22:52:52 -07008328 if(uErr) {
8329 Encoded = NULLUsefulBufC;
8330 }
Laurence Lundblade0750fc42020-06-20 21:02:34 -07008331
8332 return Encoded;
8333}
8334
Laurence Lundbladecc7da412020-12-27 00:09:07 -08008335/* h'FF' */
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08008336static const uint8_t spBreakInByteString[] = {
8337 0x41, 0xff
8338};
8339
Laurence Lundblade0750fc42020-06-20 21:02:34 -07008340
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03008341int32_t EnterBstrTest(void)
Laurence Lundblade0750fc42020-06-20 21:02:34 -07008342{
Laurence Lundblade8510f8c2020-12-01 11:31:16 -08008343 UsefulBuf_MAKE_STACK_UB(OutputBuffer, 100);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07008344
8345 QCBORDecodeContext DC;
8346
Laurence Lundblade55013642020-09-23 05:39:22 -07008347 QCBORDecode_Init(&DC, EncodeBstrWrapTestData(OutputBuffer), 0);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07008348
Laurence Lundblade55013642020-09-23 05:39:22 -07008349 int64_t n1, n2, n3, n4, n5, n6, n7, n8;
Laurence Lundblade0750fc42020-06-20 21:02:34 -07008350
8351
Laurence Lundblade9b334962020-08-27 10:55:53 -07008352 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07008353 QCBORDecode_EnterMap(&DC, NULL);
Laurence Lundblade55013642020-09-23 05:39:22 -07008354 QCBORDecode_GetInt64InMapN(&DC, 100, &n1);
8355 QCBORDecode_GetInt64InMapN(&DC, 200, &n2);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07008356 QCBORDecode_ExitMap(&DC);
Laurence Lundblade9b334962020-08-27 10:55:53 -07008357 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07008358 QCBORDecode_EnterArray(&DC, NULL);
Laurence Lundblade55013642020-09-23 05:39:22 -07008359 QCBORDecode_GetInt64(&DC, &n3);
Laurence Lundblade9b334962020-08-27 10:55:53 -07008360 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
Laurence Lundblade55013642020-09-23 05:39:22 -07008361 QCBORDecode_GetInt64(&DC, &n4);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07008362 QCBORDecode_ExitBstrWrapped(&DC);
Laurence Lundblade55013642020-09-23 05:39:22 -07008363 QCBORDecode_GetInt64(&DC, &n5);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07008364 QCBORDecode_ExitArray(&DC);
8365 QCBORDecode_ExitBstrWrapped(&DC);
Laurence Lundblade55013642020-09-23 05:39:22 -07008366 QCBORDecode_GetInt64(&DC, &n6);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07008367 QCBORDecode_ExitBstrWrapped(&DC);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07008368 QCBORDecode_EnterArray(&DC, NULL);
Laurence Lundblade55013642020-09-23 05:39:22 -07008369 QCBORDecode_GetInt64(&DC, &n7);
8370 QCBORDecode_GetInt64(&DC, &n8);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07008371 QCBORDecode_ExitArray(&DC);
8372
8373 QCBORError uErr = QCBORDecode_Finish(&DC);
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08008374 if(uErr) {
8375 return (int32_t)uErr;
8376 }
Laurence Lundblade0750fc42020-06-20 21:02:34 -07008377
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08008378
8379 /* Enter and exit byte string wrapped CBOR that is bad. It has just a break.
8380 * Successful because no items are fetched from byte string.
8381 */
8382 QCBORDecode_Init(&DC,
8383 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBreakInByteString),
8384 0);
8385 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
8386 uErr = QCBORDecode_GetError(&DC);
8387 if(uErr) {
8388 return 100 + (int32_t)uErr;
8389 }
8390
8391 QCBORDecode_ExitBstrWrapped(&DC);
8392 uErr = QCBORDecode_GetError(&DC);
8393 if(uErr) {
8394 return 200 + (int32_t)uErr;
8395 }
8396
8397 /* Try to get item that is a break out of a byte string wrapped CBOR.
8398 * It fails because there should be no break.
8399 */
8400 QCBORDecode_Init(&DC,
8401 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBreakInByteString),
8402 0);
8403 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
8404 QCBORItem Item;
8405 uErr = QCBORDecode_GetNext(&DC, &Item);
8406 if(uErr != QCBOR_ERR_BAD_BREAK) {
8407 return 300 + (int32_t)uErr;
8408 }
8409
8410 return 0;
Laurence Lundblade0750fc42020-06-20 21:02:34 -07008411}
Laurence Lundblade37f46e52020-08-04 03:32:14 -07008412
8413
8414
8415
8416static const uint8_t spTaggedTypes[] = {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008417 0xb2,
Laurence Lundblade37f46e52020-08-04 03:32:14 -07008418
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07008419 // Date string
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008420 0x00,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07008421 0xc0, 0x74, 0x32, 0x30, 0x30, 0x33, 0x2D, 0x31, 0x32, 0x2D,
8422 0x31, 0x33, 0x54, 0x31, 0x38, 0x3A, 0x33, 0x30, 0x3A, 0x30,
8423 0x32, 0x5A,
Laurence Lundblade37f46e52020-08-04 03:32:14 -07008424
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008425 0x01,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07008426 0x74, 0x32, 0x30, 0x30, 0x33, 0x2D, 0x31, 0x32, 0x2D, 0x31,
8427 0x33, 0x54, 0x31, 0x38, 0x3A, 0x33, 0x30, 0x3A, 0x30, 0x32,
8428 0x5A,
Laurence Lundblade37f46e52020-08-04 03:32:14 -07008429
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07008430 // Bignum
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008431 10,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07008432 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
8433 0x09, 0x10,
Laurence Lundblade37f46e52020-08-04 03:32:14 -07008434
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008435 11,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07008436 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
8437 0x09, 0x10,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008438
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07008439 // URL
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008440 20,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07008441 0xd8, 0x20, 0x6f, 0x68, 0x74, 0x74, 0x70, 0x3A, 0x2F, 0x2F,
8442 0x63, 0x62, 0x6F, 0x72, 0x2E, 0x6D, 0x65, 0x2F,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008443
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07008444 21,
8445 0x6f, 0x68, 0x74, 0x74, 0x70, 0x3A, 0x2F, 0x2F, 0x63, 0x62,
8446 0x6F, 0x72, 0x2E, 0x6D, 0x65, 0x2F,
8447
8448 // B64
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008449 0x18, 0x1e,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07008450 0xd8, 0x22, 0x6c, 0x63, 0x47, 0x78, 0x6C, 0x59, 0x58, 0x4E,
8451 0x31, 0x63, 0x6D, 0x55, 0x75,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008452
8453 0x18, 0x1f,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07008454 0x6c, 0x63, 0x47, 0x78, 0x6C, 0x59, 0x58, 0x4E, 0x31, 0x63,
8455 0x6D, 0x55, 0x75,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008456
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07008457 // B64URL
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008458 0x18, 0x28,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07008459 0xd8, 0x21, 0x6c, 0x63, 0x47, 0x78, 0x6C, 0x59, 0x58, 0x4E,
8460 0x31, 0x63, 0x6D, 0x55, 0x75,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008461
8462 0x18, 0x29,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07008463 0x6c, 0x63, 0x47, 0x78, 0x6C, 0x59, 0x58, 0x4E, 0x31, 0x63,
8464 0x6D, 0x55, 0x75,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008465
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07008466 // Regex
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008467 0x18, 0x32,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07008468 0xd8, 0x23, 0x68, 0x31, 0x30, 0x30, 0x5C, 0x73, 0x2A, 0x6D,
8469 0x6B,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008470
8471 0x18, 0x33,
8472 0x68, 0x31, 0x30, 0x30, 0x5C, 0x73, 0x2A, 0x6D, 0x6B,
8473
8474 // MIME
8475 0x18, 0x3c,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07008476 0xd8, 0x24, 0x72, 0x4D, 0x49, 0x4D, 0x45, 0x2D, 0x56, 0x65,
8477 0x72, 0x73, 0x69, 0x6F, 0x6E, 0x3A, 0x20, 0x31, 0x2E, 0x30,
8478 0x0A,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008479
8480 0x18, 0x3d,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07008481 0x72, 0x4D, 0x49, 0x4D, 0x45, 0x2D, 0x56, 0x65, 0x72, 0x73,
8482 0x69, 0x6F, 0x6E, 0x3A, 0x20, 0x31, 0x2E, 0x30, 0x0A,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008483
8484 0x18, 0x3e,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07008485 0xd9, 0x01, 0x01, 0x52, 0x4D, 0x49, 0x4D, 0x45, 0x2D, 0x56,
8486 0x65, 0x72, 0x73, 0x69, 0x6F, 0x6E, 0x3A, 0x20, 0x31, 0x2E,
8487 0x30, 0x0A,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008488
8489 0x18, 0x3f,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07008490 0x52, 0x4D, 0x49, 0x4D, 0x45, 0x2D, 0x56, 0x65, 0x72, 0x73,
8491 0x69, 0x6F, 0x6E, 0x3A, 0x20, 0x31, 0x2E, 0x30, 0x0A,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008492
8493 // UUID
8494 0x18, 0x46,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07008495 0xd8, 0x25, 0x50, 0x53, 0x4D, 0x41, 0x52, 0x54, 0x43, 0x53,
8496 0x4C, 0x54, 0x54, 0x43, 0x46, 0x49, 0x43, 0x41, 0x32,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008497
8498 0x18, 0x47,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07008499 0x50, 0x53, 0x4D, 0x41, 0x52, 0x54, 0x43, 0x53, 0x4C, 0x54,
8500 0x54, 0x43, 0x46, 0x49, 0x43, 0x41, 0x32
Laurence Lundblade37f46e52020-08-04 03:32:14 -07008501};
8502
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03008503int32_t DecodeTaggedTypeTests(void)
Laurence Lundblade37f46e52020-08-04 03:32:14 -07008504{
8505 QCBORDecodeContext DC;
8506 QCBORError uErr;
8507
8508 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTaggedTypes), 0);
8509
8510 UsefulBufC String;
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008511 bool bNeg;
Laurence Lundblade37f46e52020-08-04 03:32:14 -07008512
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07008513 QCBORDecode_EnterMap(&DC, NULL);
Laurence Lundblade9b334962020-08-27 10:55:53 -07008514 QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008515 QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &String);
Laurence Lundblade37f46e52020-08-04 03:32:14 -07008516 if(QCBORDecode_GetError(&DC) != QCBOR_SUCCESS) {
8517 return 1;
8518 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07008519 QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade37f46e52020-08-04 03:32:14 -07008520 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_UNEXPECTED_TYPE) {
8521 return 2;
8522 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07008523 QCBORDecode_GetDateStringInMapN(&DC, 1, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade37f46e52020-08-04 03:32:14 -07008524 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_UNEXPECTED_TYPE) {
8525 return 3;
8526 }
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008527 QCBORDecode_GetDateStringInMapN(&DC, 1, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &String);
Laurence Lundblade9b334962020-08-27 10:55:53 -07008528 QCBORDecode_GetDateStringInMapN(&DC, 1, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade37f46e52020-08-04 03:32:14 -07008529 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
8530 return 4;
8531 }
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008532 QCBORDecode_GetDateStringInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07008533 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008534 return 5;
8535 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07008536
Laurence Lundblade9b334962020-08-27 10:55:53 -07008537 QCBORDecode_GetBignumInMapN(&DC, 10, QCBOR_TAG_REQUIREMENT_TAG, &String, &bNeg);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008538 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
8539 bNeg != false) {
8540 return 10;
8541 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07008542 QCBORDecode_GetBignumInMapN(&DC, 11, QCBOR_TAG_REQUIREMENT_TAG, &String, &bNeg);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008543 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
8544 bNeg != true) {
8545 return 11;
8546 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07008547 QCBORDecode_GetBignumInMapN(&DC, 11, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bNeg);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008548 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_UNEXPECTED_TYPE) {
8549 return 12;
8550 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07008551 QCBORDecode_GetBignumInMapN(&DC, 14, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bNeg);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07008552 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008553 return 13;
8554 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07008555 QCBORDecode_GetBignumInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bNeg);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07008556 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008557 return 14;
8558 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07008559
Laurence Lundblade9b334962020-08-27 10:55:53 -07008560 QCBORDecode_GetURIInMapN(&DC, 20, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008561 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
8562 return 20;
8563 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07008564 QCBORDecode_GetURIInMapN(&DC, 21, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008565 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
8566 return 21;
8567 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07008568 QCBORDecode_GetURIInMapN(&DC, 22, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07008569 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008570 return 22;
8571 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07008572 QCBORDecode_GetURIInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07008573 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008574 return 23;
8575 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07008576
Laurence Lundblade24bd7e12021-01-09 00:05:11 -08008577#ifndef QCBOR_DISABLE_UNCOMMON_TAGS
Laurence Lundblade9b334962020-08-27 10:55:53 -07008578 QCBORDecode_GetB64InMapN(&DC, 30, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008579 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
8580 return 30;
8581 }
Laurence Lundblade24bd7e12021-01-09 00:05:11 -08008582#endif
Laurence Lundblade9b334962020-08-27 10:55:53 -07008583 QCBORDecode_GetB64InMapN(&DC, 31, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008584 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
8585 return 31;
8586 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07008587 QCBORDecode_GetB64InMapN(&DC, 32, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07008588 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008589 return 32;
8590 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07008591 QCBORDecode_GetB64InMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07008592 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008593 return 33;
8594 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07008595
Laurence Lundblade24bd7e12021-01-09 00:05:11 -08008596#ifndef QCBOR_DISABLE_UNCOMMON_TAGS
Laurence Lundblade9b334962020-08-27 10:55:53 -07008597 QCBORDecode_GetB64URLInMapN(&DC, 40, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008598 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
8599 return 40;
8600 }
Laurence Lundblade24bd7e12021-01-09 00:05:11 -08008601#endif
Laurence Lundblade9b334962020-08-27 10:55:53 -07008602 QCBORDecode_GetB64URLInMapN(&DC, 41, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008603 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
8604 return 41;
8605 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07008606 QCBORDecode_GetB64URLInMapN(&DC, 42, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07008607 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008608 return 42;
8609 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07008610 QCBORDecode_GetB64URLInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07008611 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008612 return 43;
8613 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07008614
Laurence Lundblade24bd7e12021-01-09 00:05:11 -08008615#ifndef QCBOR_DISABLE_UNCOMMON_TAGS
Laurence Lundblade9b334962020-08-27 10:55:53 -07008616 QCBORDecode_GetRegexInMapN(&DC, 50, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008617 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
8618 return 50;
8619 }
Laurence Lundblade24bd7e12021-01-09 00:05:11 -08008620#endif
Laurence Lundblade9b334962020-08-27 10:55:53 -07008621 QCBORDecode_GetRegexInMapN(&DC, 51, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008622 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
8623 return 51;
8624 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07008625 QCBORDecode_GetRegexInMapN(&DC, 52, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07008626 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008627 return 52;
8628 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07008629 QCBORDecode_GetRegexInMapSZ(&DC, "xxx", 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 53;
8632 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07008633
Laurence Lundblade24bd7e12021-01-09 00:05:11 -08008634#ifndef QCBOR_DISABLE_UNCOMMON_TAGS
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008635 // MIME
8636 bool bIsNot7Bit;
Laurence Lundblade9b334962020-08-27 10:55:53 -07008637 QCBORDecode_GetMIMEMessageInMapN(&DC, 60, QCBOR_TAG_REQUIREMENT_TAG, &String, &bIsNot7Bit);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008638 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
8639 bIsNot7Bit == true) {
8640 return 60;
8641 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07008642 QCBORDecode_GetMIMEMessageInMapN(&DC, 61, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bIsNot7Bit);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008643 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
8644 bIsNot7Bit == true) {
8645 return 61;
8646 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07008647 QCBORDecode_GetMIMEMessageInMapN(&DC, 62, QCBOR_TAG_REQUIREMENT_TAG, &String, &bIsNot7Bit);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008648 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
8649 bIsNot7Bit == false) {
8650 return 62;
8651 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07008652 QCBORDecode_GetMIMEMessageInMapN(&DC, 63, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bIsNot7Bit);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008653 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
8654 bIsNot7Bit == false) {
8655 return 63;
8656 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07008657 QCBORDecode_GetMIMEMessageInMapN(&DC, 64, QCBOR_TAG_REQUIREMENT_TAG, &String, &bNeg);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07008658 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008659 return 64;
8660 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07008661 QCBORDecode_GetMIMEMessageInMapSZ(&DC, "zzz", QCBOR_TAG_REQUIREMENT_TAG, &String, &bNeg);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07008662 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008663 return 65;
8664 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07008665
Laurence Lundblade24bd7e12021-01-09 00:05:11 -08008666
Laurence Lundblade9b334962020-08-27 10:55:53 -07008667 QCBORDecode_GetBinaryUUIDInMapN(&DC, 70, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008668 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
8669 return 70;
8670 }
Laurence Lundblade24bd7e12021-01-09 00:05:11 -08008671#endif /* #ifndef QCBOR_DISABLE_UNCOMMON_TAGS */
8672
Laurence Lundblade9b334962020-08-27 10:55:53 -07008673 QCBORDecode_GetBinaryUUIDInMapN(&DC, 71, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008674 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
8675 return 71;
8676 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07008677 QCBORDecode_GetBinaryUUIDInMapN(&DC, 72, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07008678 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008679 return 72;
8680 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07008681 QCBORDecode_GetBinaryUUIDInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07008682 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008683 return 73;
8684 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07008685
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07008686 // Improvement: add some more error test cases
8687
Laurence Lundblade37f46e52020-08-04 03:32:14 -07008688 QCBORDecode_ExitMap(&DC);
8689
8690 uErr = QCBORDecode_Finish(&DC);
8691 if(uErr != QCBOR_SUCCESS) {
8692 return 100;
8693 }
8694
8695 return 0;
8696}
Laurence Lundbladea4308a82020-10-03 18:08:57 -07008697
8698
8699
8700
8701/*
Laurence Lundbladecc7da412020-12-27 00:09:07 -08008702 [
8703 "aaaaaaaaaa",
8704 {}
8705 ]
Laurence Lundbladea4308a82020-10-03 18:08:57 -07008706 */
8707static const uint8_t spTooLarge1[] = {
8708 0x9f,
8709 0x6a, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61,
8710 0xa0,
8711 0xff
8712};
8713
8714/*
Laurence Lundbladecc7da412020-12-27 00:09:07 -08008715 [
8716 {
8717 0: "aaaaaaaaaa"
8718 }
8719 ]
Laurence Lundbladea4308a82020-10-03 18:08:57 -07008720 */
8721static const uint8_t spTooLarge2[] = {
8722 0x9f,
8723 0xa1,
8724 0x00,
8725 0x6a, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61,
8726 0xff
8727};
8728
8729/*
Laurence Lundbladecc7da412020-12-27 00:09:07 -08008730 h'A1006A61616161616161616161'
Laurence Lundbladea4308a82020-10-03 18:08:57 -07008731
Laurence Lundbladecc7da412020-12-27 00:09:07 -08008732 {
8733 0: "aaaaaaaaaa"
8734 }
Laurence Lundbladea4308a82020-10-03 18:08:57 -07008735 */
8736static const uint8_t spTooLarge3[] = {
8737 0x4d,
8738 0xa1,
8739 0x00,
8740 0x6a, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61,
8741};
8742
8743int32_t TooLargeInputTest(void)
8744{
8745 QCBORDecodeContext DC;
8746 QCBORError uErr;
8747 UsefulBufC String;
8748
8749 // These tests require a build with QCBOR_MAX_DECODE_INPUT_SIZE set
8750 // to 10 There's not really any way to test this error
8751 // condition. The error condition is not complex, so setting
8752 // QCBOR_MAX_DECODE_INPUT_SIZE gives an OK test.
8753
8754 // The input CBOR is only too large because the
8755 // QCBOR_MAX_DECODE_INPUT_SIZE is 10.
8756 //
8757 // This test is disabled for the normal test runs because of the
8758 // special build requirement.
8759
8760
8761 // Tests the start of a map being too large
8762 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTooLarge1), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07008763 QCBORDecode_EnterArray(&DC, NULL);
Laurence Lundbladea4308a82020-10-03 18:08:57 -07008764 QCBORDecode_GetTextString(&DC, &String);
8765 uErr = QCBORDecode_GetError(&DC);
8766 if(uErr != QCBOR_SUCCESS) {
8767 return 1;
8768 }
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07008769 QCBORDecode_EnterMap(&DC, NULL);
Laurence Lundbladea4308a82020-10-03 18:08:57 -07008770 uErr = QCBORDecode_GetError(&DC);
8771 if(uErr != QCBOR_ERR_INPUT_TOO_LARGE) {
8772 return 2;
8773 }
8774
8775 // Tests the end of a map being too large
8776 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTooLarge2), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07008777 QCBORDecode_EnterArray(&DC, NULL);
8778 QCBORDecode_EnterMap(&DC, NULL);
Laurence Lundbladea4308a82020-10-03 18:08:57 -07008779 uErr = QCBORDecode_GetError(&DC);
8780 if(uErr != QCBOR_SUCCESS) {
8781 return 3;
8782 }
8783 QCBORDecode_ExitMap(&DC);
8784 uErr = QCBORDecode_GetError(&DC);
8785 if(uErr != QCBOR_ERR_INPUT_TOO_LARGE) {
8786 return 4;
8787 }
8788
8789 // Tests the entire input CBOR being too large when processing bstr wrapping
8790 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTooLarge3), QCBOR_DECODE_MODE_NORMAL);
8791 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
8792 uErr = QCBORDecode_GetError(&DC);
8793 if(uErr != QCBOR_ERR_INPUT_TOO_LARGE) {
8794 return 5;
8795 }
8796
8797 return 0;
8798}
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07008799
8800
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08008801#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
8802
Laurence Lundblade37286c02022-09-03 10:05:02 -07008803/*
8804 An array of three map entries
8805 1) Indefinite length string label for indefinite lenght byte string
8806 2) Indefinite length string label for an integer
8807 3) Indefinite length string label for an indefinite-length negative big num
8808 */
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07008809static const uint8_t spMapWithIndefLenStrings[] = {
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08008810 0xa3,
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07008811 0x7f, 0x61, 'l', 0x64, 'a', 'b', 'e', 'l' , 0x61, '1', 0xff,
8812 0x5f, 0x42, 0x01, 0x02, 0x43, 0x03, 0x04, 0x05, 0xff,
8813 0x7f, 0x62, 'd', 'y', 0x61, 'm', 0x61, 'o', 0xff,
8814 0x03,
8815 0x7f, 0x62, 'l', 'a', 0x63, 'b', 'e', 'l', 0x61, '2', 0xff,
8816 0xc3,
8817 0x5f, 0x42, 0x00, 0x01, 0x42, 0x00, 0x01, 0x41, 0x01, 0xff,
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07008818};
8819
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03008820int32_t SpiffyIndefiniteLengthStringsTests(void)
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07008821{
8822 QCBORDecodeContext DCtx;
8823
8824 QCBORDecode_Init(&DCtx,
8825 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spMapWithIndefLenStrings),
8826 QCBOR_DECODE_MODE_NORMAL);
8827
Laurence Lundblade8510f8c2020-12-01 11:31:16 -08008828 UsefulBuf_MAKE_STACK_UB(StringBuf, 200);
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07008829 QCBORDecode_SetMemPool(&DCtx, StringBuf, false);
8830
8831 UsefulBufC ByteString;
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07008832 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07008833 QCBORDecode_GetByteStringInMapSZ(&DCtx, "label1", &ByteString);
Laurence Lundblade37286c02022-09-03 10:05:02 -07008834
8835#ifndef QCBOR_DISABLE_TAGS
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07008836 if(QCBORDecode_GetAndResetError(&DCtx)) {
8837 return 1;
8838 }
8839
8840 const uint8_t pExectedBytes[] = {0x01, 0x02, 0x03, 0x04, 0x05};
8841 if(UsefulBuf_Compare(ByteString, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pExectedBytes))) {
8842 return 2;
8843 }
8844
8845 uint64_t uInt;
8846 QCBORDecode_GetUInt64InMapSZ(&DCtx, "dymo", &uInt);
8847 if(QCBORDecode_GetAndResetError(&DCtx)) {
8848 return 3;
8849 }
8850 if(uInt != 3) {
8851 return 4;
8852 }
8853
Máté Tóth-Pálef5f07a2021-09-17 19:31:37 +02008854#ifndef USEFULBUF_DISABLE_ALL_FLOAT
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07008855 double uDouble;
8856 QCBORDecode_GetDoubleConvertAllInMapSZ(&DCtx,
8857 "label2",
8858 0xff,
8859 &uDouble);
Laurence Lundblade37286c02022-09-03 10:05:02 -07008860
Laurence Lundbladeb8e19aa2020-10-07 20:59:11 -07008861#ifndef QCBOR_DISABLE_FLOAT_HW_USE
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07008862 if(QCBORDecode_GetAndResetError(&DCtx)) {
8863 return 5;
8864 }
8865 if(uDouble != -16777474) {
8866 return 6;
8867 }
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08008868#else /* QCBOR_DISABLE_FLOAT_HW_USE */
Laurence Lundbladeb8e19aa2020-10-07 20:59:11 -07008869 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_HW_FLOAT_DISABLED) {
8870 return 7;
8871 }
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08008872#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
Máté Tóth-Pálef5f07a2021-09-17 19:31:37 +02008873#endif /* USEFULBUF_DISABLE_ALL_FLOAT */
Laurence Lundbladeb8e19aa2020-10-07 20:59:11 -07008874
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07008875 QCBORDecode_ExitMap(&DCtx);
8876
8877 if(QCBORDecode_Finish(&DCtx)) {
8878 return 99;
8879 }
8880
Laurence Lundblade37286c02022-09-03 10:05:02 -07008881#else /* QCBOR_DISABLE_TAGS */
8882 /* The big num in the input is a CBOR tag and you can't do
8883 * map lookups in a map with a tag so this test does very little
8884 * when tags are disabled. That is OK, the test coverage is still
8885 * good when they are not.
8886 */
8887 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_TAGS_DISABLED) {
8888 return 1002;
8889 }
8890#endif /*QCBOR_DISABLE_TAGS */
8891
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07008892 return 0;
8893}
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08008894#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
Laurence Lundblade2a26abb2020-11-05 19:06:54 -08008895
8896
Laurence Lundbladeec290b82024-06-10 11:10:54 -07008897#ifndef QCBOR_DISABLE_NON_INTEGER_LABELS
Laurence Lundbladecf41c522021-02-20 10:19:07 -07008898/*
8899 * An array of an integer and an array. The second array contains
8900 * a bstr-wrapped map.
8901 *
8902 * [7, [h'A36D6669... (see next lines) 73']]
8903 *
8904 * {"first integer": 42,
8905 * "an array of two strings": ["string1", "string2"],
8906 * "map in a map":
8907 * { "bytes 1": h'78787878',
8908 * "bytes 2": h'79797979',
8909 * "another int": 98,
8910 * "text 2": "lies, damn lies and statistics"
8911 * }
8912 * }
8913 */
Laurence Lundblade2a26abb2020-11-05 19:06:54 -08008914
Laurence Lundbladecf41c522021-02-20 10:19:07 -07008915static const uint8_t pValidWrappedMapEncoded[] = {
8916 0x82, 0x07, 0x81, 0x58, 0x97,
8917 0xa3, 0x6d, 0x66, 0x69, 0x72, 0x73, 0x74, 0x20, 0x69, 0x6e,
8918 0x74, 0x65, 0x67, 0x65, 0x72, 0x18, 0x2a, 0x77, 0x61, 0x6e,
8919 0x20, 0x61, 0x72, 0x72, 0x61, 0x79, 0x20, 0x6f, 0x66, 0x20,
8920 0x74, 0x77, 0x6f, 0x20, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67,
8921 0x73, 0x82, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x31,
8922 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x32, 0x6c, 0x6d,
8923 0x61, 0x70, 0x20, 0x69, 0x6e, 0x20, 0x61, 0x20, 0x6d, 0x61,
8924 0x70, 0xa4, 0x67, 0x62, 0x79, 0x74, 0x65, 0x73, 0x20, 0x31,
8925 0x44, 0x78, 0x78, 0x78, 0x78, 0x67, 0x62, 0x79, 0x74, 0x65,
8926 0x73, 0x20, 0x32, 0x44, 0x79, 0x79, 0x79, 0x79, 0x6b, 0x61,
8927 0x6e, 0x6f, 0x74, 0x68, 0x65, 0x72, 0x20, 0x69, 0x6e, 0x74,
8928 0x18, 0x62, 0x66, 0x74, 0x65, 0x78, 0x74, 0x20, 0x32, 0x78,
8929 0x1e, 0x6c, 0x69, 0x65, 0x73, 0x2c, 0x20, 0x64, 0x61, 0x6d,
8930 0x6e, 0x20, 0x6c, 0x69, 0x65, 0x73, 0x20, 0x61, 0x6e, 0x64,
8931 0x20, 0x73, 0x74, 0x61, 0x74, 0x69, 0x73, 0x74, 0x69, 0x63,
8932 0x73
8933};
8934
8935#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
8936
8937/* As above, but the arrays are indefinite length */
8938static const uint8_t pValidIndefWrappedMapEncoded[] = {
8939 0x9f, 0x07, 0x9f, 0x58, 0x97,
8940 0xa3, 0x6d, 0x66, 0x69, 0x72, 0x73, 0x74, 0x20, 0x69, 0x6e,
8941 0x74, 0x65, 0x67, 0x65, 0x72, 0x18, 0x2a, 0x77, 0x61, 0x6e,
8942 0x20, 0x61, 0x72, 0x72, 0x61, 0x79, 0x20, 0x6f, 0x66, 0x20,
8943 0x74, 0x77, 0x6f, 0x20, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67,
8944 0x73, 0x82, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x31,
8945 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x32, 0x6c, 0x6d,
8946 0x61, 0x70, 0x20, 0x69, 0x6e, 0x20, 0x61, 0x20, 0x6d, 0x61,
8947 0x70, 0xa4, 0x67, 0x62, 0x79, 0x74, 0x65, 0x73, 0x20, 0x31,
8948 0x44, 0x78, 0x78, 0x78, 0x78, 0x67, 0x62, 0x79, 0x74, 0x65,
8949 0x73, 0x20, 0x32, 0x44, 0x79, 0x79, 0x79, 0x79, 0x6b, 0x61,
8950 0x6e, 0x6f, 0x74, 0x68, 0x65, 0x72, 0x20, 0x69, 0x6e, 0x74,
8951 0x18, 0x62, 0x66, 0x74, 0x65, 0x78, 0x74, 0x20, 0x32, 0x78,
8952 0x1e, 0x6c, 0x69, 0x65, 0x73, 0x2c, 0x20, 0x64, 0x61, 0x6d,
8953 0x6e, 0x20, 0x6c, 0x69, 0x65, 0x73, 0x20, 0x61, 0x6e, 0x64,
8954 0x20, 0x73, 0x74, 0x61, 0x74, 0x69, 0x73, 0x74, 0x69, 0x63,
8955 0x73,
8956 0xff, 0xff
8957};
Laurence Lundbladeec290b82024-06-10 11:10:54 -07008958#endif /* ! QCBOR_DISABLE_NON_INTEGER_LABELS */
Laurence Lundbladecf41c522021-02-20 10:19:07 -07008959
8960static const uint8_t pWithEmptyMap[] = {0x82, 0x18, 0x64, 0xa0};
8961
Laurence Lundbladeec290b82024-06-10 11:10:54 -07008962
Laurence Lundbladecf41c522021-02-20 10:19:07 -07008963#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
8964static const uint8_t pWithEmptyMapInDef[] = {0x9f, 0x18, 0x64, 0xbf, 0xff, 0xff};
8965#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
8966
8967#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
Laurence Lundblade37286c02022-09-03 10:05:02 -07008968/*
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08008969 * An array of one that contains a byte string that is an indefinite
8970 * length string that CBOR wraps an array of three numbers [42, 43,
8971 * 44]. The byte string is an implicit tag 24.
8972 *
8973 * [
8974 * (_ h'83', h'18', h'2A182B', h'182C')
8975 * ]
Laurence Lundblade37286c02022-09-03 10:05:02 -07008976 */
Laurence Lundbladecf41c522021-02-20 10:19:07 -07008977static const uint8_t pWrappedByIndefiniteLength[] = {
8978 0x81,
Laurence Lundbladecf41c522021-02-20 10:19:07 -07008979 0x5f,
8980 0x41, 0x83,
8981 0x41, 0x18,
8982 0x43, 0x2A, 0x18, 0x2B,
8983 0x42, 0x18, 0x2C,
8984 0xff
8985};
8986#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
8987
Laurence Lundbladeec290b82024-06-10 11:10:54 -07008988#endif
Laurence Lundbladecf41c522021-02-20 10:19:07 -07008989
Laurence Lundbladeec290b82024-06-10 11:10:54 -07008990#ifndef QCBOR_DISABLE_NON_INTEGER_LABELS
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03008991int32_t PeekAndRewindTest(void)
Laurence Lundblade2a26abb2020-11-05 19:06:54 -08008992{
8993 QCBORItem Item;
8994 QCBORError nCBORError;
8995 QCBORDecodeContext DCtx;
8996
Laurence Lundbladeec290b82024-06-10 11:10:54 -07008997 // Improvement: rework this test to use only integer labels.
8998
Laurence Lundblade2a26abb2020-11-05 19:06:54 -08008999 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0);
9000
9001 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
9002 return 100+(int32_t)nCBORError;
9003 }
9004 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
9005 return 200;
9006 }
9007
Laurence Lundblade3427dee2021-06-20 11:11:24 -07009008 QCBORDecode_VPeekNext(&DCtx, &Item);
9009 if((nCBORError = QCBORDecode_GetError(&DCtx))) {
9010 return 150+(int32_t)nCBORError;
9011 }
9012 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
9013 return 250;
9014 }
9015
Laurence Lundblade2a26abb2020-11-05 19:06:54 -08009016 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
9017 return (int32_t)nCBORError;
9018 }
9019 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
9020 return 300;
9021 }
9022
9023 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
9024 return 400 + (int32_t)nCBORError;
9025 }
9026 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
9027 return 500;
9028 }
9029
9030 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
9031 return (int32_t)nCBORError;
9032 }
9033 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
9034 return 600;
9035 }
9036
9037 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
9038 return 900 + (int32_t)nCBORError;
9039 }
9040 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
9041 Item.uDataType != QCBOR_TYPE_INT64 ||
9042 Item.val.int64 != 42 ||
9043 Item.uDataAlloc ||
9044 Item.uLabelAlloc ||
9045 UsefulBufCompareToSZ(Item.label.string, "first integer")) {
9046 return 1000;
9047 }
9048
9049 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
9050 return 1100 + (int32_t)nCBORError;
9051 }
9052
9053 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
9054 Item.uDataType != QCBOR_TYPE_INT64 ||
9055 Item.val.int64 != 42 ||
9056 Item.uDataAlloc ||
9057 Item.uLabelAlloc ||
9058 UsefulBufCompareToSZ(Item.label.string, "first integer")) {
9059 return 1200;
9060 }
9061
9062
9063 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
9064 return 1300 + (int32_t)nCBORError;
9065 }
9066 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
9067 Item.uDataAlloc ||
9068 Item.uLabelAlloc ||
9069 UsefulBufCompareToSZ(Item.label.string, "an array of two strings") ||
9070 Item.uDataType != QCBOR_TYPE_ARRAY ||
Laurence Lundbladecf41c522021-02-20 10:19:07 -07009071 Item.val.uCount != 2) {
Laurence Lundblade2a26abb2020-11-05 19:06:54 -08009072 return 1400;
Laurence Lundbladecf41c522021-02-20 10:19:07 -07009073 }
Laurence Lundblade2a26abb2020-11-05 19:06:54 -08009074
9075 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
9076 return 1500 + (int32_t)nCBORError;
9077 }
9078 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
9079 Item.uDataAlloc ||
9080 Item.uLabelAlloc ||
9081 UsefulBufCompareToSZ(Item.val.string, "string1")) {
9082 return 1600;
9083 }
9084
9085 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
9086 return 1700 + (int32_t)nCBORError;
9087 }
9088 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
9089 Item.uDataAlloc ||
9090 Item.uLabelAlloc ||
9091 UsefulBufCompareToSZ(Item.val.string, "string2")) {
9092 return 1800;
9093 }
9094
9095 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
9096 return (int32_t)nCBORError;
9097 }
9098 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
9099 Item.uDataAlloc ||
9100 Item.uLabelAlloc ||
9101 UsefulBufCompareToSZ(Item.val.string, "string2")) {
9102 return 1900;
9103 }
9104
9105 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
9106 return (int32_t)nCBORError;
9107 }
9108 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
9109 Item.uDataAlloc ||
9110 Item.uLabelAlloc ||
9111 UsefulBufCompareToSZ(Item.val.string, "string2")) {
9112 return 2000;
9113 }
9114
9115
9116 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
9117 return 2100 + (int32_t)nCBORError;
9118 }
9119 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
9120 Item.uDataAlloc ||
9121 Item.uLabelAlloc ||
9122 UsefulBufCompareToSZ(Item.label.string, "map in a map") ||
9123 Item.uDataType != QCBOR_TYPE_MAP ||
9124 Item.val.uCount != 4) {
9125 return 2100;
9126 }
9127
9128 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
9129 return 2200 + (int32_t)nCBORError;
9130 }
9131 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
9132 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("bytes 1"))||
9133 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
9134 Item.uDataAlloc ||
9135 Item.uLabelAlloc ||
9136 UsefulBufCompareToSZ(Item.val.string, "xxxx")) {
9137 return 2300;
9138 }
9139
9140 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
9141 return 2400 + (int32_t)nCBORError;
9142 }
9143 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
9144 UsefulBufCompareToSZ(Item.label.string, "bytes 2") ||
9145 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
9146 Item.uDataAlloc ||
9147 Item.uLabelAlloc ||
9148 UsefulBufCompareToSZ(Item.val.string, "yyyy")) {
9149 return 2500;
9150 }
9151
9152 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
9153 return 2600 + (int32_t)nCBORError;
9154 }
9155 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
9156 UsefulBufCompareToSZ(Item.label.string, "bytes 2") ||
9157 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
9158 Item.uDataAlloc ||
9159 Item.uLabelAlloc ||
9160 UsefulBufCompareToSZ(Item.val.string, "yyyy")) {
9161 return 2700;
9162 }
9163
9164 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
9165 return 2800 + (int32_t)nCBORError;
9166 }
9167 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
9168 Item.uDataAlloc ||
9169 Item.uLabelAlloc ||
9170 UsefulBufCompareToSZ(Item.label.string, "another int") ||
9171 Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundbladecf41c522021-02-20 10:19:07 -07009172 Item.val.int64 != 98) {
Laurence Lundblade2a26abb2020-11-05 19:06:54 -08009173 return 2900;
Laurence Lundbladecf41c522021-02-20 10:19:07 -07009174 }
Laurence Lundblade2a26abb2020-11-05 19:06:54 -08009175
9176 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
9177 return 3000 + (int32_t)nCBORError;
9178 }
9179 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
9180 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("text 2"))||
9181 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
9182 Item.uDataAlloc ||
9183 Item.uLabelAlloc ||
9184 UsefulBufCompareToSZ(Item.val.string, "lies, damn lies and statistics")) {
9185 return 3100;
9186 }
9187
9188 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
9189 return 3200 + (int32_t)nCBORError;
9190 }
9191 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
9192 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("text 2"))||
9193 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
9194 Item.uDataAlloc ||
9195 Item.uLabelAlloc ||
9196 UsefulBufCompareToSZ(Item.val.string, "lies, damn lies and statistics")) {
9197 return 3300;
9198 }
9199
Laurence Lundblade3427dee2021-06-20 11:11:24 -07009200 nCBORError = QCBORDecode_PeekNext(&DCtx, &Item);
9201 if(nCBORError != QCBOR_ERR_NO_MORE_ITEMS) {
9202 return 3300 + (int32_t)nCBORError;
9203 }
9204
9205 QCBORDecode_VPeekNext(&DCtx, &Item);
9206 nCBORError = QCBORDecode_GetError(&DCtx);
9207 if(nCBORError != QCBOR_ERR_NO_MORE_ITEMS) {
9208 return 3400 + (int32_t)nCBORError;
9209 }
9210
9211 QCBORDecode_VPeekNext(&DCtx, &Item);
9212 nCBORError = QCBORDecode_GetError(&DCtx);
9213 if(nCBORError != QCBOR_ERR_NO_MORE_ITEMS) {
9214 return 3500 + (int32_t)nCBORError;
9215 }
Laurence Lundbladecf41c522021-02-20 10:19:07 -07009216
9217
9218 // Rewind to top level after entering several maps
9219 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0);
9220
9221 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
9222 return (int32_t)nCBORError;
9223 }
9224 if(Item.uDataType != QCBOR_TYPE_MAP ||
9225 Item.val.uCount != 3) {
9226 return 400;
9227 }
9228
9229 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
9230 return 4000+(int32_t)nCBORError;
9231 }
9232
9233 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
9234 Item.uDataType != QCBOR_TYPE_INT64 ||
9235 Item.val.int64 != 42 ||
9236 Item.uDataAlloc ||
9237 Item.uLabelAlloc ||
9238 UsefulBufCompareToSZ(Item.label.string, "first integer")) {
9239 return 4100;
9240 }
9241
9242 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
9243 return 4100+(int32_t)nCBORError;
9244 }
9245 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
9246 Item.uDataAlloc ||
9247 Item.uLabelAlloc ||
9248 UsefulBufCompareToSZ(Item.label.string, "an array of two strings") ||
9249 Item.uDataType != QCBOR_TYPE_ARRAY ||
9250 Item.val.uCount != 2) {
9251 return 4200;
9252 }
9253
9254 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
9255 return 4200+(int32_t)nCBORError;
9256 }
9257 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
9258 Item.uDataAlloc ||
9259 Item.uLabelAlloc ||
9260 UsefulBufCompareToSZ(Item.val.string, "string1")) {
9261 return 4300;
9262 }
9263
9264 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
9265 return 4300+(int32_t)nCBORError;
9266 }
9267 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
9268 Item.uDataAlloc ||
9269 Item.uLabelAlloc ||
9270 UsefulBufCompareToSZ(Item.val.string, "string2")) {
9271 return 4400;
9272 }
9273
9274 QCBORDecode_Rewind(&DCtx);
9275
9276 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
9277 return 4400+(int32_t)nCBORError;
9278 }
9279 if(Item.uDataType != QCBOR_TYPE_MAP ||
9280 Item.val.uCount != 3) {
9281 return 4500;
9282 }
9283
9284 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
9285 return (int32_t)nCBORError;
9286 }
9287
9288 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
9289 Item.uDataType != QCBOR_TYPE_INT64 ||
9290 Item.val.int64 != 42 ||
9291 Item.uDataAlloc ||
9292 Item.uLabelAlloc ||
9293 UsefulBufCompareToSZ(Item.label.string, "first integer")) {
9294 return 4600;
9295 }
9296
9297 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
9298 return (int32_t)nCBORError;
9299 }
9300 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
9301 Item.uDataAlloc ||
9302 Item.uLabelAlloc ||
9303 UsefulBufCompareToSZ(Item.label.string, "an array of two strings") ||
9304 Item.uDataType != QCBOR_TYPE_ARRAY ||
9305 Item.val.uCount != 2) {
9306 return 4700;
9307 }
9308
9309 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
9310 return (int32_t)nCBORError;
9311 }
9312 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
9313 Item.uDataAlloc ||
9314 Item.uLabelAlloc ||
9315 UsefulBufCompareToSZ(Item.val.string, "string1")) {
9316 return 4800;
9317 }
9318
9319 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
9320 return 4900+(int32_t)nCBORError;
9321 }
9322 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
9323 Item.uDataAlloc ||
9324 Item.uLabelAlloc ||
9325 UsefulBufCompareToSZ(Item.val.string, "string2")) {
9326 return 5000;
9327 }
9328
9329
9330 // Rewind an entered map
9331 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0);
9332
9333 QCBORDecode_EnterMap(&DCtx, NULL);
9334
9335 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
9336 return 5100+(int32_t)nCBORError;
9337 }
9338
9339 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
9340 Item.uDataType != QCBOR_TYPE_INT64 ||
9341 Item.val.int64 != 42 ||
9342 Item.uDataAlloc ||
9343 Item.uLabelAlloc ||
9344 UsefulBufCompareToSZ(Item.label.string, "first integer")) {
9345 return 5200;
9346 }
9347
9348 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
9349 return 5200+(int32_t)nCBORError;
9350 }
9351 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
9352 Item.uDataAlloc ||
9353 Item.uLabelAlloc ||
9354 UsefulBufCompareToSZ(Item.label.string, "an array of two strings") ||
9355 Item.uDataType != QCBOR_TYPE_ARRAY ||
9356 Item.val.uCount != 2) {
9357 return -5300;
9358 }
9359
9360 QCBORDecode_Rewind(&DCtx);
9361
9362 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
9363 return 5300+(int32_t)nCBORError;
9364 }
9365
9366 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
9367 Item.uDataType != QCBOR_TYPE_INT64 ||
9368 Item.val.int64 != 42 ||
9369 Item.uDataAlloc ||
9370 Item.uLabelAlloc ||
9371 UsefulBufCompareToSZ(Item.label.string, "first integer")) {
9372 return 5400;
9373 }
9374
9375 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
9376 return 5400+(int32_t)nCBORError;
9377 }
9378 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
9379 Item.uDataAlloc ||
9380 Item.uLabelAlloc ||
9381 UsefulBufCompareToSZ(Item.label.string, "an array of two strings") ||
9382 Item.uDataType != QCBOR_TYPE_ARRAY ||
9383 Item.val.uCount != 2) {
9384 return 5500;
9385 }
9386
9387
9388 // Rewind and entered array inside an entered map
9389 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0);
9390
9391 QCBORDecode_EnterMap(&DCtx, NULL);
9392
9393 QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings");
9394
9395 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
9396 return 5600+(int32_t)nCBORError;
9397 }
9398 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
9399 Item.uDataAlloc ||
9400 Item.uLabelAlloc ||
9401 UsefulBufCompareToSZ(Item.val.string, "string1")) {
9402 return 5700;
9403 }
9404
9405 QCBORDecode_Rewind(&DCtx);
9406
9407 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
9408 return 5700+(int32_t)nCBORError;
9409 }
9410 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
9411 Item.uDataAlloc ||
9412 Item.uLabelAlloc ||
9413 UsefulBufCompareToSZ(Item.val.string, "string1")) {
9414 return 5800;
9415 }
9416
9417 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
9418 return (int32_t)nCBORError;
9419 }
9420 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
9421 Item.uDataAlloc ||
9422 Item.uLabelAlloc ||
9423 UsefulBufCompareToSZ(Item.val.string, "string2")) {
9424 return 5900;
9425 }
9426
9427 QCBORDecode_Rewind(&DCtx);
9428
9429 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
9430 return 5900+(int32_t)nCBORError;
9431 }
9432 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
9433 Item.uDataAlloc ||
9434 Item.uLabelAlloc ||
9435 UsefulBufCompareToSZ(Item.val.string, "string1")) {
9436 return 6000;
9437 }
9438
9439
9440 // Rewind a byte string inside an array inside an array
9441 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidWrappedMapEncoded), 0);
9442
9443 QCBORDecode_EnterArray(&DCtx, NULL);
9444
9445 uint64_t i;
9446 QCBORDecode_GetUInt64(&DCtx, &i);
9447
9448 QCBORDecode_EnterArray(&DCtx, NULL);
9449
9450 QCBORDecode_EnterBstrWrapped(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
9451 if(QCBORDecode_GetError(&DCtx)) {
9452 return 6100;
9453 }
9454
9455 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
9456 return (int32_t)nCBORError;
9457 }
9458 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
9459 return 6200;
9460 }
9461
9462 QCBORDecode_Rewind(&DCtx);
9463
9464 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
9465 return 6300+(int32_t)nCBORError;
9466 }
9467 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
9468 return 6400;
9469 }
9470
9471#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
9472 // Rewind a byte string inside an indefinite-length array inside
9473 // indefinite-length array
9474
9475 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidIndefWrappedMapEncoded), 0);
9476
9477 QCBORDecode_EnterArray(&DCtx, NULL);
9478
9479 QCBORDecode_GetUInt64(&DCtx, &i);
9480
9481 QCBORDecode_EnterArray(&DCtx, NULL);
9482
9483 QCBORDecode_EnterBstrWrapped(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
9484 if(QCBORDecode_GetError(&DCtx)) {
9485 return 6500;
9486 }
9487
9488 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
9489 return 6600+(int32_t)nCBORError;
9490 }
9491 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
9492 return 6700;
9493 }
9494
9495 QCBORDecode_Rewind(&DCtx);
9496
9497 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
9498 return 6800+(int32_t)nCBORError;
9499 }
9500 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
9501 return 6900;
9502 }
9503#endif
9504
9505 // Rewind an empty map
9506 // [100, {}]
9507 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pWithEmptyMap), 0);
9508 QCBORDecode_EnterArray(&DCtx, NULL);
9509 QCBORDecode_GetUInt64(&DCtx, &i);
9510 if(i != 100) {
9511 return 7010;
9512 }
9513 QCBORDecode_EnterMap(&DCtx, NULL);
9514
9515 /* Do it 5 times to be sure multiple rewinds work */
9516 for(int n = 0; n < 5; n++) {
9517 nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
9518 if(nCBORError != QCBOR_ERR_NO_MORE_ITEMS) {
9519 return 7000 + n;
9520 }
9521 QCBORDecode_Rewind(&DCtx);
9522 }
9523 QCBORDecode_ExitMap(&DCtx);
9524 QCBORDecode_Rewind(&DCtx);
9525 QCBORDecode_GetUInt64(&DCtx, &i);
9526 if(i != 100) {
9527 return 7010;
9528 }
9529 QCBORDecode_ExitArray(&DCtx);
9530 QCBORDecode_Rewind(&DCtx);
9531 QCBORDecode_EnterArray(&DCtx, NULL);
9532 i = 9;
9533 QCBORDecode_GetUInt64(&DCtx, &i);
9534 if(i != 100) {
9535 return 7020;
9536 }
9537 if(QCBORDecode_GetError(&DCtx)){
9538 return 7030;
9539 }
9540
9541 // Rewind an empty indefinite length map
9542#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
9543 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pWithEmptyMapInDef), 0);
9544 QCBORDecode_EnterArray(&DCtx, NULL);
9545 QCBORDecode_GetUInt64(&DCtx, &i);
9546 if(i != 100) {
9547 return 7810;
9548 }
9549 QCBORDecode_EnterMap(&DCtx, NULL);
9550
9551 /* Do it 5 times to be sure multiple rewinds work */
9552 for(int n = 0; n < 5; n++) {
9553 nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
9554 if(nCBORError != QCBOR_ERR_NO_MORE_ITEMS) {
9555 return 7800 + n;
9556 }
9557 QCBORDecode_Rewind(&DCtx);
9558 }
9559 QCBORDecode_ExitMap(&DCtx);
9560 QCBORDecode_Rewind(&DCtx);
9561 QCBORDecode_GetUInt64(&DCtx, &i);
9562 if(i != 100) {
9563 return 7810;
9564 }
9565 QCBORDecode_ExitArray(&DCtx);
9566 QCBORDecode_Rewind(&DCtx);
9567 QCBORDecode_EnterArray(&DCtx, NULL);
9568 i = 9;
9569 QCBORDecode_GetUInt64(&DCtx, &i);
9570 if(i != 100) {
9571 return 7820;
9572 }
9573 if(QCBORDecode_GetError(&DCtx)){
9574 return 7830;
9575 }
9576#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
9577
9578 // Rewind an indefnite length byte-string wrapped sequence
9579#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
9580 QCBORDecode_Init(&DCtx,
9581 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pWrappedByIndefiniteLength),
9582 0);
9583 UsefulBuf_MAKE_STACK_UB(Pool, 100);
9584 QCBORDecode_SetMemPool(&DCtx, Pool, 0);
9585
9586 QCBORDecode_EnterArray(&DCtx, NULL);
9587 QCBORDecode_EnterBstrWrapped(&DCtx, 2, NULL);
Laurence Lundblade31fddb72024-05-13 13:03:35 -07009588 if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_CANNOT_ENTER_ALLOCATED_STRING) {
Laurence Lundbladecf41c522021-02-20 10:19:07 -07009589 return 7300;
9590 }
9591
9592 /*
Laurence Lundblade31fddb72024-05-13 13:03:35 -07009593 Improvement: Fix QCBORDecode_EnterBstrWrapped() so it can work on
9594 allocated strings. This is a fairly big job because of all the
9595 UsefulBuf internal book keeping that needs tweaking.
Laurence Lundbladecf41c522021-02-20 10:19:07 -07009596 QCBORDecode_GetUInt64(&DCtx, &i);
9597 if(i != 42) {
9598 return 7110;
9599 }
9600 QCBORDecode_Rewind(&DCtx);
9601 QCBORDecode_GetUInt64(&DCtx, &i);
9602 if(i != 42) {
9603 return 7220;
Laurence Lundblade31fddb72024-05-13 13:03:35 -07009604 }
9605 */
Laurence Lundblade37286c02022-09-03 10:05:02 -07009606
Laurence Lundbladeec290b82024-06-10 11:10:54 -07009607#endif /* ! QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
Laurence Lundbladecf41c522021-02-20 10:19:07 -07009608
9609
9610 // Rewind an indefnite length byte-string wrapped sequence
9611
Laurence Lundblade2a26abb2020-11-05 19:06:54 -08009612 return 0;
9613}
Laurence Lundblade9f9c3732021-03-23 09:38:46 -07009614
Laurence Lundbladeec290b82024-06-10 11:10:54 -07009615#endif /* ! QCBOR_DISABLE_NON_INTEGER_LABELS */
Laurence Lundblade9f9c3732021-03-23 09:38:46 -07009616
9617
9618static const uint8_t spBooleansInMap[] =
9619{
9620 0xa1, 0x08, 0xf5
9621};
9622
9623static const uint8_t spBooleansInMapWrongType[] =
9624{
9625 0xa1, 0x08, 0xf6
9626};
9627
9628static const uint8_t spBooleansInMapNWF[] =
9629{
9630 0xa1, 0x08, 0x1a
9631};
9632
Laurence Lundblade8782dd32021-04-27 04:15:37 -07009633static const uint8_t spNullInMap[] =
9634{
9635 0xa1, 0x08, 0xf6
9636};
9637
9638static const uint8_t spUndefinedInMap[] =
9639{
9640 0xa1, 0x08, 0xf7
9641};
9642
Laurence Lundblade9f9c3732021-03-23 09:38:46 -07009643
Laurence Lundbladecdbbc192024-06-28 15:13:04 -07009644#ifndef QCBOR_DISABLE_TAGS
9645static const uint8_t spTaggedSimples[] =
9646{
9647 0xd8, 0x58, 0xd8, 0x2c, 0xd6, 0xf5,
9648 0xd9, 0x0f, 0xA0, 0xf7
9649};
9650#endif /* ! QCBOR_DISABLE_TAGS */
9651
9652
9653
Laurence Lundblade9f9c3732021-03-23 09:38:46 -07009654int32_t BoolTest(void)
9655{
9656 QCBORDecodeContext DCtx;
9657 bool b;
9658
Laurence Lundblade8782dd32021-04-27 04:15:37 -07009659 QCBORDecode_Init(&DCtx,
9660 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMap),
9661 0);
Laurence Lundblade9f9c3732021-03-23 09:38:46 -07009662 QCBORDecode_EnterMap(&DCtx, NULL);
9663 QCBORDecode_GetBool(&DCtx, &b);
9664 if(QCBORDecode_GetAndResetError(&DCtx) || !b) {
9665 return 1;
9666 }
9667
9668 QCBORDecode_GetBoolInMapN(&DCtx, 7, &b);
9669 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_LABEL_NOT_FOUND) {
9670 return 2;
9671 }
9672
9673 QCBORDecode_GetBoolInMapN(&DCtx, 8, &b);
9674 if(QCBORDecode_GetAndResetError(&DCtx) || !b) {
9675 return 3;
9676 }
9677
9678
9679 QCBORDecode_GetBoolInMapSZ(&DCtx, "xx", &b);
9680 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_LABEL_NOT_FOUND) {
9681 return 4;
9682 }
9683
Laurence Lundblade8782dd32021-04-27 04:15:37 -07009684 QCBORDecode_Init(&DCtx,
9685 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMapWrongType),
9686 0);
Laurence Lundblade9f9c3732021-03-23 09:38:46 -07009687 QCBORDecode_EnterMap(&DCtx, NULL);
9688 QCBORDecode_GetBool(&DCtx, &b);
9689 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
9690 return 5;
9691 }
9692
Laurence Lundblade8782dd32021-04-27 04:15:37 -07009693 QCBORDecode_Init(&DCtx,
9694 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMapNWF),
9695 0);
Laurence Lundblade9f9c3732021-03-23 09:38:46 -07009696 QCBORDecode_EnterMap(&DCtx, NULL);
9697 QCBORDecode_GetBool(&DCtx, &b);
9698 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_HIT_END) {
9699 return 6;
9700 }
9701
Laurence Lundblade8782dd32021-04-27 04:15:37 -07009702
9703 QCBORDecode_Init(&DCtx,
9704 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spNullInMap),
9705 0);
9706 QCBORDecode_EnterMap(&DCtx, NULL);
9707 QCBORDecode_GetNull(&DCtx);
9708 if(QCBORDecode_GetAndResetError(&DCtx)) {
9709 return 7;
9710 }
9711
9712 QCBORDecode_Init(&DCtx,
9713 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMap),
9714 0);
9715 QCBORDecode_EnterMap(&DCtx, NULL);
9716 QCBORDecode_GetNull(&DCtx);
9717 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
9718 return 8;
9719 }
9720
9721 QCBORDecode_Init(&DCtx,
9722 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spNullInMap),
9723 0);
9724 QCBORDecode_EnterMap(&DCtx, NULL);
9725 QCBORDecode_GetNullInMapN(&DCtx, 8);
9726 if(QCBORDecode_GetAndResetError(&DCtx)) {
9727 return 9;
9728 }
9729
9730 QCBORDecode_Init(&DCtx,
9731 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMap),
9732 0);
9733 QCBORDecode_EnterMap(&DCtx, NULL);
9734 QCBORDecode_GetNullInMapN(&DCtx, 8);
9735 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
9736 return 10;
9737 }
9738
9739 QCBORDecode_Init(&DCtx,
9740 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMapNWF),
9741 0);
9742 QCBORDecode_EnterMap(&DCtx, NULL);
9743 QCBORDecode_GetUndefined(&DCtx);
9744 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_HIT_END) {
9745 return 11;
9746 }
9747
9748 QCBORDecode_Init(&DCtx,
9749 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUndefinedInMap),
9750 0);
9751 QCBORDecode_EnterMap(&DCtx, NULL);
9752 QCBORDecode_GetUndefined(&DCtx);
9753 if(QCBORDecode_GetAndResetError(&DCtx)) {
9754 return 12;
9755 }
9756
9757 QCBORDecode_Init(&DCtx,
9758 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMap),
9759 0);
9760 QCBORDecode_EnterMap(&DCtx, NULL);
9761 QCBORDecode_GetUndefined(&DCtx);
9762 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
9763 return 13;
9764 }
9765
9766 QCBORDecode_Init(&DCtx,
9767 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUndefinedInMap),
9768 0);
9769 QCBORDecode_EnterMap(&DCtx, NULL);
9770 QCBORDecode_GetUndefinedInMapN(&DCtx, 8);
9771 if(QCBORDecode_GetAndResetError(&DCtx)) {
9772 return 14;
9773 }
9774
9775 QCBORDecode_Init(&DCtx,
9776 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMap),
9777 0);
9778 QCBORDecode_EnterMap(&DCtx, NULL);
9779 QCBORDecode_GetUndefinedInMapN(&DCtx, 8);
9780 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
9781 return 15;
9782 }
9783
9784 QCBORDecode_Init(&DCtx,
9785 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMapNWF),
9786 0);
9787 QCBORDecode_EnterMap(&DCtx, NULL);
9788 QCBORDecode_GetUndefined(&DCtx);
9789 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_HIT_END) {
9790 return 15;
9791 }
9792
Laurence Lundbladecdbbc192024-06-28 15:13:04 -07009793#ifndef QCBOR_DISABLE_TAGS
9794 QCBORDecode_Init(&DCtx,
9795 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTaggedSimples),
9796 0);
9797 QCBORDecode_GetBool(&DCtx, &b);
9798 if(QCBORDecode_GetNthTagOfLast(&DCtx, 0) != 22) {
9799 return 401;
9800 }
9801 if(QCBORDecode_GetNthTagOfLast(&DCtx, 1) != 44) {
9802 return 402;
9803 }
9804 if(QCBORDecode_GetNthTagOfLast(&DCtx, 2) != 88) {
9805 return 403;
9806 }
9807 if(QCBORDecode_GetNthTagOfLast(&DCtx, 3) != CBOR_TAG_INVALID64) {
9808 return 404;
9809 }
9810 QCBORDecode_GetUndefined(&DCtx);
9811 if(QCBORDecode_GetNthTagOfLast(&DCtx, 0) != 4000) {
9812 return 405;
9813 }
9814 if(QCBORDecode_GetNthTagOfLast(&DCtx, 1) != CBOR_TAG_INVALID64) {
9815 return 406;
9816 }
9817 QCBORDecode_GetNull(&DCtx); /* Off the end */
9818 if(QCBORDecode_GetNthTagOfLast(&DCtx, 0) != CBOR_TAG_INVALID64) {
9819 return 407;
9820 }
9821#endif /* ! QCBOR_DISABLE_TAGS */
9822
Laurence Lundblade9f9c3732021-03-23 09:38:46 -07009823 return 0;
9824}
Laurence Lundbladef00b8be2024-03-08 10:34:33 -08009825
9826
Laurence Lundbladeec290b82024-06-10 11:10:54 -07009827#ifndef QCBOR_DISABLE_NON_INTEGER_LABELS
Laurence Lundbladea29f45a2024-05-14 15:55:19 -07009828static const uint8_t spExpectedArray2s[] = {
9829 0x82, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67,
9830 0x31, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67,
9831 0x32};
9832
9833#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
9834static const uint8_t spExpectedArray2sIndef[] = {
9835 0x9f, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67,
9836 0x31, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67,
9837 0x32, 0xff};
9838#endif
9839
9840static const uint8_t spExpectedMap4[] = {
9841 0xa4, 0x67, 0x62, 0x79, 0x74, 0x65, 0x73, 0x20,
9842 0x31, 0x44, 0x78, 0x78, 0x78, 0x78, 0x67, 0x62,
9843 0x79, 0x74, 0x65, 0x73, 0x20, 0x32, 0x44, 0x79,
9844 0x79, 0x79, 0x79, 0x6b, 0x61, 0x6e, 0x6f, 0x74,
9845 0x68, 0x65, 0x72, 0x20, 0x69, 0x6e, 0x74, 0x18,
9846 0x62, 0x66, 0x74, 0x65, 0x78, 0x74, 0x20, 0x32,
9847 0x78, 0x1e, 0x6c, 0x69, 0x65, 0x73, 0x2c, 0x20,
9848 0x64, 0x61, 0x6d, 0x6e, 0x20, 0x6c, 0x69, 0x65,
9849 0x73, 0x20, 0x61, 0x6e, 0x64, 0x20, 0x73, 0x74,
9850 0x61, 0x74, 0x69, 0x73, 0x74, 0x69, 0x63, 0x73};
9851
9852
9853#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
9854
9855static const uint8_t spExpectedMap4Indef[] = {
9856 0xbf, 0x67, 0x62, 0x79, 0x74, 0x65, 0x73, 0x20,
9857 0x31, 0x44, 0x78, 0x78, 0x78, 0x78, 0x67, 0x62,
9858 0x79, 0x74, 0x65, 0x73, 0x20, 0x32, 0x44, 0x79,
9859 0x79, 0x79, 0x79, 0x6b, 0x61, 0x6e, 0x6f, 0x74,
9860 0x68, 0x65, 0x72, 0x20, 0x69, 0x6e, 0x74, 0x18,
9861 0x62, 0x66, 0x74, 0x65, 0x78, 0x74, 0x20, 0x32,
9862 0x78, 0x1e, 0x6c, 0x69, 0x65, 0x73, 0x2c, 0x20,
9863 0x64, 0x61, 0x6d, 0x6e, 0x20, 0x6c, 0x69, 0x65,
9864 0x73, 0x20, 0x61, 0x6e, 0x64, 0x20, 0x73, 0x74,
9865 0x61, 0x74, 0x69, 0x73, 0x74, 0x69, 0x63, 0x73,
9866 0xff};
9867
9868/*
9869 * [[[[[0, []]]]], 0]
9870 */
9871static const uint8_t spDefAndIndef[] = {
9872 0x82,
9873 0x9f, 0x9f, 0x9f, 0x82, 0x00, 0x9f, 0xff, 0xff, 0xff, 0xff, 0x00
9874};
9875#endif /* !QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
9876
Laurence Lundbladea29f45a2024-05-14 15:55:19 -07009877#ifndef QCBOR_DISABLE_TAGS
9878/* An exp / mant tag in two nested arrays */
9879static const uint8_t spExpMant[] = {0x81, 0x81, 0xC4, 0x82, 0x20, 0x03};
9880#endif /* !QCBOR_DISABLE_TAGS */
Laurence Lundbladeec290b82024-06-10 11:10:54 -07009881#endif
Laurence Lundbladea29f45a2024-05-14 15:55:19 -07009882
Laurence Lundbladeec290b82024-06-10 11:10:54 -07009883#ifndef QCBOR_DISABLE_NON_INTEGER_LABELS
Laurence Lundbladea29f45a2024-05-14 15:55:19 -07009884int32_t GetMapAndArrayTest(void)
9885{
9886 QCBORDecodeContext DCtx;
9887 size_t uPosition ;
9888 QCBORItem Item;
9889 UsefulBufC ReturnedEncodedCBOR;
9890
Laurence Lundbladeec290b82024-06-10 11:10:54 -07009891 // Improvement: rework so it can run with QCBOR_DISABLE_NON_INTEGER_LABELS
Laurence Lundbladea29f45a2024-05-14 15:55:19 -07009892 QCBORDecode_Init(&DCtx,
9893 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded),
9894 0);
9895
9896 QCBORDecode_EnterMap(&DCtx, NULL);
9897 QCBORDecode_VGetNextConsume(&DCtx, &Item);
9898 QCBORDecode_GetArray(&DCtx, &Item, &ReturnedEncodedCBOR);
9899 if(QCBORDecode_GetError(&DCtx)) {
9900 return 1;
9901 }
9902 if(Item.val.uCount != 2) {
9903 return 2;
9904 }
9905 if(UsefulBuf_Compare(ReturnedEncodedCBOR, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedArray2s))) {
9906 return 3;
9907 }
9908
9909 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
9910 UsefulBuf_Compare(Item.label.string, UsefulBuf_FROM_SZ_LITERAL("an array of two strings"))) {
9911 return 4;
9912 }
9913
9914 uPosition = QCBORDecode_Tell(&DCtx);
9915
9916
9917 QCBORDecode_GetMap(&DCtx, &Item, &ReturnedEncodedCBOR);
9918 if(QCBORDecode_GetError(&DCtx)) {
9919 return 10;
9920 }
9921 if(Item.val.uCount != 4) {
9922 return 11;
9923 }
9924 if(UsefulBuf_Compare(ReturnedEncodedCBOR, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedMap4))) {
9925 return 12;
9926 }
9927 uPosition = QCBORDecode_Tell(&DCtx);
9928 QCBORDecode_GetArrayFromMapSZ(&DCtx,
9929 "an array of two strings",
9930 &Item,
9931 &ReturnedEncodedCBOR);
9932 if(QCBORDecode_GetError(&DCtx)) {
9933 return 20;
9934 }
9935 if(Item.val.uCount != 2) {
9936 return 21;
9937 }
9938 if(UsefulBuf_Compare(ReturnedEncodedCBOR, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedArray2s))) {
9939 return 22;
9940 }
9941 if(uPosition != QCBORDecode_Tell(&DCtx)) {
9942 return 23;
9943 }
9944
9945 QCBORDecode_Rewind(&DCtx);
9946
9947 uPosition = QCBORDecode_Tell(&DCtx);
9948 QCBORDecode_GetMapFromMapSZ(&DCtx, "map in a map", &Item, &ReturnedEncodedCBOR);
9949 if(QCBORDecode_GetError(&DCtx)) {
9950 return 30;
9951 }
9952 if(Item.val.uCount != 4) {
9953 return 31;
9954 }
9955 if(UsefulBuf_Compare(ReturnedEncodedCBOR, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedMap4))) {
9956 return 32;
9957 }
9958 if(uPosition != QCBORDecode_Tell(&DCtx)) {
9959 return 33;
9960 }
9961
9962 uPosition = QCBORDecode_Tell(&DCtx);
9963 QCBORDecode_GetArrayFromMapSZ(&DCtx, "map in a map", &Item, &ReturnedEncodedCBOR);
9964 if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
9965 return 40;
9966 }
9967 if(UINT32_MAX != QCBORDecode_Tell(&DCtx)) {
9968 return 41;
9969 }
9970 QCBORDecode_GetAndResetError(&DCtx);
9971 if(uPosition != QCBORDecode_Tell(&DCtx)) {
9972 return 42;
9973 }
9974
9975
9976#ifndef QCBOR_DISABLE_TAGS
9977 UsefulBufC ExpMant = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpMant);
9978 QCBORDecode_Init(&DCtx, ExpMant, 0);
9979 QCBORDecode_EnterArray(&DCtx, NULL);
9980 QCBORDecode_EnterArray(&DCtx, NULL);
9981 QCBORDecode_GetArray(&DCtx, &Item, &ReturnedEncodedCBOR);
9982 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) {
9983 return 200;
9984 }
9985 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
9986 return 201;
9987 }
9988 if(!QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_DECIMAL_FRACTION)) {
9989 return 202;
9990 }
9991 if(Item.val.uCount != 2) {
9992 return 201;
9993 }
9994 if(UsefulBuf_Compare(ReturnedEncodedCBOR, UsefulBuf_Tail(ExpMant, 2))) {
9995 return 205;
9996 }
9997#endif /* !QCBOR_DISABLE_TAGS */
9998
9999
10000#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
10001
10002 UsefulBufC DefAndIndef = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spDefAndIndef);
10003 QCBORDecode_Init(&DCtx, DefAndIndef, 0);
10004 QCBORDecode_EnterArray(&DCtx, NULL);
10005 QCBORDecode_GetArray(&DCtx, &Item, &ReturnedEncodedCBOR);
10006 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) {
10007 return 50;
10008 }
10009 if(UsefulBuf_Compare(ReturnedEncodedCBOR, UsefulBuf_Tail(UsefulBuf_Head(DefAndIndef, 11), 1))) {
10010 return 51;
10011 }
10012
10013 QCBORDecode_Init(&DCtx, DefAndIndef, 0);
10014 QCBORDecode_EnterArray(&DCtx, NULL);
10015 QCBORDecode_EnterArray(&DCtx, NULL);
10016 QCBORDecode_GetArray(&DCtx, &Item, &ReturnedEncodedCBOR);
10017 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) {
10018 return 52;
10019 }
10020 if(UsefulBuf_Compare(ReturnedEncodedCBOR, UsefulBuf_Tail(UsefulBuf_Head(DefAndIndef, 10), 2))) {
10021 return 53;
10022 }
10023
10024 QCBORDecode_Init(&DCtx, DefAndIndef, 0);
10025 QCBORDecode_EnterArray(&DCtx, NULL);
10026 QCBORDecode_EnterArray(&DCtx, NULL);
10027 QCBORDecode_EnterArray(&DCtx, NULL);
10028 QCBORDecode_GetArray(&DCtx, &Item, &ReturnedEncodedCBOR);
10029 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) {
10030 return 54;
10031 }
10032 if(UsefulBuf_Compare(ReturnedEncodedCBOR, UsefulBuf_Tail(UsefulBuf_Head(DefAndIndef, 9), 3))) {
10033 return 55;
10034 }
10035 QCBORDecode_Init(&DCtx, DefAndIndef, 0);
10036 QCBORDecode_EnterArray(&DCtx, NULL);
10037 QCBORDecode_EnterArray(&DCtx, NULL);
10038 QCBORDecode_EnterArray(&DCtx, NULL);
10039 QCBORDecode_EnterArray(&DCtx, NULL);
10040 QCBORDecode_GetArray(&DCtx, &Item, &ReturnedEncodedCBOR);
10041 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) {
10042 return 56;
10043 }
10044 if(UsefulBuf_Compare(ReturnedEncodedCBOR, UsefulBuf_Tail(UsefulBuf_Head(DefAndIndef, 8), 4))) {
10045 return 57;
10046 }
10047
10048 QCBORDecode_Init(&DCtx, DefAndIndef, 0);
10049 QCBORDecode_EnterArray(&DCtx, NULL);
10050 QCBORDecode_EnterArray(&DCtx, NULL);
10051 QCBORDecode_EnterArray(&DCtx, NULL);
10052 QCBORDecode_EnterArray(&DCtx, NULL);
10053 QCBORDecode_EnterArray(&DCtx, NULL);
10054 QCBORDecode_VGetNextConsume(&DCtx, &Item);
10055 QCBORDecode_GetArray(&DCtx, &Item, &ReturnedEncodedCBOR);
10056 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) {
10057 return 58;
10058 }
10059 if(UsefulBuf_Compare(ReturnedEncodedCBOR, UsefulBuf_Tail(UsefulBuf_Head(DefAndIndef, 8), 6))) {
10060 return 59;
10061 }
10062
10063
10064 QCBORDecode_Init(&DCtx,
10065 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapIndefEncoded),
10066 0);
10067
10068 QCBORDecode_EnterMap(&DCtx, NULL);
10069 QCBORDecode_VGetNextConsume(&DCtx, &Item);
10070 QCBORDecode_GetArray(&DCtx, &Item, &ReturnedEncodedCBOR);
10071 if(QCBORDecode_GetError(&DCtx)) {
10072 return 60;
10073 }
10074 if(Item.val.uCount != UINT16_MAX) {
10075 return 61;
10076 }
10077 if(UsefulBuf_Compare(ReturnedEncodedCBOR, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedArray2sIndef))) {
10078 return 62;
10079 }
10080
10081 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
10082 UsefulBuf_Compare(Item.label.string, UsefulBuf_FROM_SZ_LITERAL("an array of two strings"))) {
10083 return 63;
10084 }
10085
10086 uPosition = QCBORDecode_Tell(&DCtx);
10087
10088
10089 QCBORDecode_GetMap(&DCtx, &Item, &ReturnedEncodedCBOR);
10090 if(QCBORDecode_GetError(&DCtx)) {
10091 return 70;
10092 }
10093 if(Item.val.uCount != UINT16_MAX) {
10094 return 71;
10095 }
10096 if(UsefulBuf_Compare(ReturnedEncodedCBOR, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedMap4Indef))) {
10097 return 72;
10098 }
10099
10100
10101 uPosition = QCBORDecode_Tell(&DCtx);
10102 QCBORDecode_GetArrayFromMapSZ(&DCtx,
10103 "an array of two strings",
10104 &Item,
10105 &ReturnedEncodedCBOR);
10106 if(QCBORDecode_GetError(&DCtx)) {
10107 return 80;
10108 }
10109 if(Item.val.uCount != UINT16_MAX) {
10110 return 81;
10111 }
10112 if(UsefulBuf_Compare(ReturnedEncodedCBOR, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedArray2sIndef))) {
10113 return 82;
10114 }
10115 if(uPosition != QCBORDecode_Tell(&DCtx)) {
10116 return 83;
10117 }
10118
10119 QCBORDecode_Rewind(&DCtx);
10120
10121 uPosition = QCBORDecode_Tell(&DCtx);
10122 QCBORDecode_GetMapFromMapSZ(&DCtx, "map in a map", &Item, &ReturnedEncodedCBOR);
10123 if(QCBORDecode_GetError(&DCtx)) {
10124 return 90;
10125 }
10126 if(Item.val.uCount != UINT16_MAX) {
10127 return 91;
10128 }
10129 if(UsefulBuf_Compare(ReturnedEncodedCBOR, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedMap4Indef))) {
10130 return 92;
10131 }
10132 if(uPosition != QCBORDecode_Tell(&DCtx)) {
10133 return 93;
10134 }
10135
10136 uPosition = QCBORDecode_Tell(&DCtx);
10137 QCBORDecode_GetArrayFromMapSZ(&DCtx, "map in a map", &Item, &ReturnedEncodedCBOR);
10138 if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
10139 return 100;
10140 }
10141 if(UINT32_MAX != QCBORDecode_Tell(&DCtx)) {
10142 return 101;
10143 }
10144 QCBORDecode_GetAndResetError(&DCtx);
10145 if(uPosition != QCBORDecode_Tell(&DCtx)) {
10146 return 102;
10147 }
Laurence Lundbladeec290b82024-06-10 11:10:54 -070010148#endif /* ! QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
Laurence Lundbladea29f45a2024-05-14 15:55:19 -070010149
10150 return 0;
10151}
Laurence Lundbladeec290b82024-06-10 11:10:54 -070010152#endif /* ! QCBOR_DISABLE_NON_INTEGER_LABELS */
Laurence Lundbladea29f45a2024-05-14 15:55:19 -070010153
10154
Laurence Lundbladef00b8be2024-03-08 10:34:33 -080010155int32_t
10156ErrorHandlingTests(void)
10157{
10158 QCBORDecodeContext DCtx;
10159 QCBORItem Item;
10160 QCBORError uError;
10161 int64_t integer;
10162
10163 /* Test QCBORDecode_SetError() */
10164 QCBORDecode_Init(&DCtx,
10165 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded),
10166 QCBOR_DECODE_MODE_NORMAL);
10167
10168 QCBORDecode_SetError(&DCtx, QCBOR_ERR_FIRST_USER_DEFINED);
10169
10170 QCBORDecode_VGetNext(&DCtx, &Item);
10171
10172 uError = QCBORDecode_GetError(&DCtx);
10173
10174 if(uError != QCBOR_ERR_FIRST_USER_DEFINED) {
10175 return -1;
10176 }
10177
10178 if(Item.uLabelType != QCBOR_TYPE_NONE ||
10179 Item.uDataType != QCBOR_TYPE_NONE) {
10180 return -2;
10181 }
10182
10183
10184 /* Test data type returned from previous error */
10185 QCBORDecode_Init(&DCtx,
10186 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded),
10187 QCBOR_DECODE_MODE_NORMAL);
10188 QCBORDecode_GetInt64(&DCtx, &integer);
10189 uError = QCBORDecode_GetError(&DCtx);
10190 if(uError != QCBOR_ERR_UNEXPECTED_TYPE) {
10191 return -3;
10192 }
10193
10194 QCBORDecode_VGetNext(&DCtx, &Item);
10195 if(Item.uLabelType != QCBOR_TYPE_NONE ||
10196 Item.uDataType != QCBOR_TYPE_NONE) {
10197 return -2;
10198 }
10199 uError = QCBORDecode_GetError(&DCtx);
10200 if(uError != QCBOR_ERR_UNEXPECTED_TYPE) {
10201 return -3;
10202 }
10203
10204
10205 /* Test error classification functions */
10206
10207 if(!QCBORDecode_IsUnrecoverableError(QCBOR_ERR_INDEFINITE_STRING_CHUNK)) {
10208 return -10;
10209 }
10210 if(QCBORDecode_IsUnrecoverableError(QCBOR_SUCCESS)) {
10211 return -11;
10212 }
10213 if(!QCBORDecode_IsUnrecoverableError(QCBOR_ERR_INDEFINITE_STRING_CHUNK)) {
10214 return -12;
10215 }
10216 if(QCBORDecode_IsUnrecoverableError(QCBOR_ERR_DUPLICATE_LABEL)) {
10217 return -13;
10218 }
10219
10220 if(!QCBORDecode_IsNotWellFormedError(QCBOR_ERR_BAD_TYPE_7)) {
10221 return -20;
10222 }
10223 if(!QCBORDecode_IsNotWellFormedError(QCBOR_ERR_BAD_BREAK)) {
10224 return -21;
10225 }
10226 if(QCBORDecode_IsNotWellFormedError(QCBOR_SUCCESS)) {
10227 return -22;
10228 }
10229 if(QCBORDecode_IsNotWellFormedError(QCBOR_ERR_ARRAY_DECODE_TOO_LONG)) {
10230 return -23;
10231 }
10232
Laurence Lundbladecd34e582024-06-06 10:55:14 -070010233 /* Test error strings */
10234 const char *szErrString;
10235
10236 szErrString = qcbor_err_to_str(QCBOR_ERR_ARRAY_DECODE_TOO_LONG);
10237 if(szErrString == NULL) {
10238 return -100;
10239 }
10240 if(strcmp(szErrString, "QCBOR_ERR_ARRAY_DECODE_TOO_LONG")) {
10241 return -101;
10242 }
10243
10244 szErrString = qcbor_err_to_str(QCBOR_SUCCESS);
10245 if(szErrString == NULL) {
10246 return -102;
10247 }
10248 if(strcmp(szErrString, "QCBOR_SUCCESS")) {
10249 return -103;
10250 }
10251
10252 szErrString = qcbor_err_to_str(100);
10253 if(szErrString == NULL) {
10254 return -104;
10255 }
10256 if(strcmp(szErrString, "Unidentified QCBOR error")) {
10257 return -105;
10258 }
10259
10260 szErrString = qcbor_err_to_str(200);
10261 if(szErrString == NULL) {
10262 return -106;
10263 }
10264 if(strcmp(szErrString, "USER_DEFINED_200")) {
10265 return -107;
10266 }
10267
Laurence Lundbladee2226742024-08-16 10:50:23 -070010268 QCBORDecode_Init(&DCtx,
10269 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded),
10270 QCBOR_DECODE_MODE_NORMAL);
10271
10272 UsefulBufC Xx = QCBORDecode_RetrieveUndecodedInput(&DCtx);
10273 if(Xx.ptr != pValidMapEncoded) {
10274 return -200;
10275 }
10276 if(Xx.len != sizeof(pValidMapEncoded)) {
10277 return -201;
10278 }
10279
Laurence Lundbladef00b8be2024-03-08 10:34:33 -080010280 return 0;
10281}
Laurence Lundblade11654912024-05-09 11:49:24 -070010282
10283
10284int32_t TellTests(void)
10285{
10286 QCBORDecodeContext DCtx;
10287 QCBORItem Item;
10288 uint32_t uPosition;
10289 int nIndex;
10290 int64_t nDecodedInt;
10291
Laurence Lundbladeec290b82024-06-10 11:10:54 -070010292 // Improvement: rewrite so this can run with only integer labels
Laurence Lundblade11654912024-05-09 11:49:24 -070010293 static const uint32_t aPos[] =
Laurence Lundblade535bb5e2024-06-16 12:06:32 -070010294 {0, 1, 17, 42, 50, 58, 72, 85, 98, 112, 151};
Laurence Lundblade11654912024-05-09 11:49:24 -070010295 QCBORDecode_Init(&DCtx,
10296 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded),
10297 0);
10298 for(nIndex = 0; ; nIndex++) {
10299 uPosition = QCBORDecode_Tell(&DCtx);
10300 if(uPosition != aPos[nIndex]) {
10301 return nIndex;
10302 }
10303
Laurence Lundblade535bb5e2024-06-16 12:06:32 -070010304 if(QCBORDecode_EndCheck(&DCtx)) {
Laurence Lundblade11654912024-05-09 11:49:24 -070010305 break;
10306 }
10307
10308 QCBORDecode_VGetNext(&DCtx, &Item);
10309 }
10310
10311#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
10312 static const uint32_t aPosIndef[] =
Laurence Lundblade535bb5e2024-06-16 12:06:32 -070010313 {0, 1, 17, 42, 50, 59, 73, 86, 99, 113, 154};
Laurence Lundblade11654912024-05-09 11:49:24 -070010314 QCBORDecode_Init(&DCtx,
10315 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapIndefEncoded),
10316 0);
10317
10318 for(nIndex = 0; ; nIndex++) {
10319 uPosition = QCBORDecode_Tell(&DCtx);
10320 if(uPosition != aPosIndef[nIndex]) {
10321 return nIndex + 100;
10322 }
10323
Laurence Lundblade535bb5e2024-06-16 12:06:32 -070010324 if(QCBORDecode_EndCheck(&DCtx)) {
Laurence Lundblade11654912024-05-09 11:49:24 -070010325 break;
10326 }
10327
10328 QCBORDecode_VGetNext(&DCtx, &Item);
10329 }
10330#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
10331
10332 /* Next, some tests with entered maps and arrays */
10333 QCBORDecode_Init(&DCtx,
10334 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded),
10335 0);
10336 QCBORDecode_EnterMap(&DCtx, &Item);
10337 if(QCBORDecode_Tell(&DCtx) != 1) {
10338 return 1001;
10339 }
10340 QCBORDecode_GetInt64InMapSZ(&DCtx, "first integer", &nDecodedInt);
10341 if(QCBORDecode_Tell(&DCtx) != 1) {
10342 return 1002;
10343 }
10344 QCBORDecode_EnterMapFromMapSZ(&DCtx, "map in a map");
10345 if(QCBORDecode_Tell(&DCtx) != 72) {
10346 return 1003;
10347 }
10348
10349 QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt);
10350 if(nDecodedInt != 98) {
10351 return 1004;
10352 }
10353 /* Getting non-aggregate types doesn't affect cursor position. */
10354 if(QCBORDecode_Tell(&DCtx) != 72) {
10355 return 1005;
10356 }
10357 QCBORDecode_VGetNext(&DCtx, &Item);
10358 if(QCBORDecode_Tell(&DCtx) != 85) {
10359 return 1006;
10360 }
10361 QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt);
10362 if(nDecodedInt != 98) {
10363 return 1007;
10364 }
10365 /* Getting non-aggregate types doesn't affect cursor position. */
10366 if(QCBORDecode_Tell(&DCtx) != 85) {
10367 return 1008;
10368 }
10369
10370 QCBORDecode_ExitMap(&DCtx);
Laurence Lundblade535bb5e2024-06-16 12:06:32 -070010371 if(QCBORDecode_Tell(&DCtx) != 151) {
Laurence Lundblade11654912024-05-09 11:49:24 -070010372 return 1009;
10373 }
10374 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_NO_MORE_ITEMS) {
10375 return 1010;
10376 }
10377
10378#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
10379 /* Next, some tests with entered maps and arrays */
10380 QCBORDecode_Init(&DCtx,
10381 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapIndefEncoded),
10382 0);
10383 QCBORDecode_EnterMap(&DCtx, &Item);
10384 if(QCBORDecode_Tell(&DCtx) != 1) {
10385 return 2000;
10386 }
10387 QCBORDecode_GetInt64InMapSZ(&DCtx, "first integer", &nDecodedInt);
10388 if(QCBORDecode_Tell(&DCtx) != 1) {
10389 return 2001;
10390 }
10391 QCBORDecode_EnterMapFromMapSZ(&DCtx, "map in a map");
10392 if(QCBORDecode_Tell(&DCtx) != 73) {
10393 return 2002;
10394 }
10395
10396 QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt);
10397 if(nDecodedInt != 98) {
10398 return 2003;
10399 }
10400 /* Getting non-aggregate types doesn't affect cursor position. */
10401 if(QCBORDecode_Tell(&DCtx) != 73) {
10402 return 2004;
10403 }
10404 QCBORDecode_VGetNext(&DCtx, &Item);
10405 if(QCBORDecode_Tell(&DCtx) != 86) {
10406 return 2005;
10407 }
10408 QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt);
10409 if(nDecodedInt != 98) {
10410 return 2006;
10411 }
10412 /* Getting non-aggregate types doesn't affect cursor position. */
10413 if(QCBORDecode_Tell(&DCtx) != 86) {
10414 return 2007;
10415 }
10416
10417 QCBORDecode_ExitMap(&DCtx);
Laurence Lundblade535bb5e2024-06-16 12:06:32 -070010418 if(QCBORDecode_Tell(&DCtx) != 154) {
Laurence Lundblade11654912024-05-09 11:49:24 -070010419 return 2008;
10420 }
10421 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_NO_MORE_ITEMS) {
10422 return 2010;
10423 }
10424#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
10425
10426
10427
10428 /* Error state test */
10429 QCBORDecode_Init(&DCtx,
10430 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded),
10431 0);
10432 /* Cause an error */
10433 QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt);
10434 if(QCBORDecode_Tell(&DCtx) != UINT32_MAX) {
10435 return 3000;
10436 }
Laurence Lundblade535bb5e2024-06-16 12:06:32 -070010437 if(QCBORDecode_EndCheck(&DCtx) != QCBOR_ERR_MAP_NOT_ENTERED) {
10438 return 3001;
10439 }
10440
Laurence Lundblade11654912024-05-09 11:49:24 -070010441
10442 /* Empties tests */
10443 const uint8_t pMinimalCBOR[] = {0xa0}; // One empty map
10444 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pMinimalCBOR),0);
10445 if(QCBORDecode_Tell(&DCtx) != 0) {
10446 return 4000;
10447 }
Laurence Lundblade535bb5e2024-06-16 12:06:32 -070010448 if(QCBORDecode_EndCheck(&DCtx) != QCBOR_SUCCESS) {
10449 return 4008;
10450 }
Laurence Lundblade11654912024-05-09 11:49:24 -070010451 QCBORDecode_EnterMap(&DCtx, &Item);
10452 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) {
10453 return 4001;
10454 }
Laurence Lundblade535bb5e2024-06-16 12:06:32 -070010455 if(QCBORDecode_Tell(&DCtx) != 1) {
Laurence Lundblade11654912024-05-09 11:49:24 -070010456 return 4002;
10457 }
10458 QCBORDecode_ExitMap(&DCtx);
10459 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) {
Laurence Lundblade535bb5e2024-06-16 12:06:32 -070010460 return 4003;
Laurence Lundblade11654912024-05-09 11:49:24 -070010461 }
Laurence Lundblade535bb5e2024-06-16 12:06:32 -070010462 if(QCBORDecode_Tell(&DCtx) != 1) {
10463 return 4004;
10464 }
10465 if(QCBORDecode_EndCheck(&DCtx) != QCBOR_ERR_NO_MORE_ITEMS) {
10466 return 4005;
Laurence Lundblade11654912024-05-09 11:49:24 -070010467 }
10468 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_NO_MORE_ITEMS) {
10469 return 4010;
10470 }
10471
10472#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
10473 const uint8_t pMinimalIndefCBOR[] = {0xbf, 0xff}; // One empty map
10474 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pMinimalIndefCBOR),0);
10475 if(QCBORDecode_Tell(&DCtx) != 0) {
10476 return 4100;
10477 }
10478 QCBORDecode_EnterMap(&DCtx, &Item);
10479 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) {
10480 return 4101;
10481 }
Laurence Lundblade535bb5e2024-06-16 12:06:32 -070010482 if(QCBORDecode_Tell(&DCtx) != 2) {
Laurence Lundblade11654912024-05-09 11:49:24 -070010483 return 4102;
10484 }
10485 QCBORDecode_ExitMap(&DCtx);
10486 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) {
Laurence Lundblade535bb5e2024-06-16 12:06:32 -070010487 return 4103;
Laurence Lundblade11654912024-05-09 11:49:24 -070010488 }
Laurence Lundblade535bb5e2024-06-16 12:06:32 -070010489 if(QCBORDecode_Tell(&DCtx) != 2) {
10490 return 4104;
10491 }
10492 if(QCBORDecode_EndCheck(&DCtx) != QCBOR_ERR_NO_MORE_ITEMS) {
10493 return 4005;
Laurence Lundblade11654912024-05-09 11:49:24 -070010494 }
10495 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_NO_MORE_ITEMS) {
10496 return 4110;
10497 }
10498#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
10499
10500 /* Test on a CBOR sequence */
10501 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSequenceTestInput),0);
10502 if(QCBORDecode_Tell(&DCtx) != 0) {
10503 return 5000;
10504 }
10505 QCBORDecode_VGetNext(&DCtx, &Item);
10506 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) {
10507 return 5001;
10508 }
10509 if(QCBORDecode_Tell(&DCtx) != 11) {
10510 return 5002;
10511 }
10512 QCBORDecode_VGetNext(&DCtx, &Item);
10513 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) {
10514 return 5003;
10515 }
10516 if(QCBORDecode_Tell(&DCtx) != 12) {
10517 return 5004;
10518 }
10519 QCBORDecode_VGetNext(&DCtx, &Item);
10520 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) {
10521 return 5005;
10522 }
10523 if(QCBORDecode_Tell(&DCtx) != 17) {
10524 return 5006;
10525 }
10526 QCBORDecode_VGetNext(&DCtx, &Item);
10527 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) {
10528 return 5007;
10529 }
Laurence Lundblade535bb5e2024-06-16 12:06:32 -070010530 if(QCBORDecode_Tell(&DCtx) != 20) {
Laurence Lundblade11654912024-05-09 11:49:24 -070010531 return 5008;
10532 }
10533 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_NO_MORE_ITEMS) {
10534 return 5010;
10535 }
10536
10537
10538 QCBORDecode_Init(&DCtx,
10539 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded),
10540 0);
10541 QCBORDecode_EnterMap(&DCtx, &Item);
10542 QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings");
10543 if(QCBORDecode_Tell(&DCtx) != 42) {
10544 return 6001;
10545 }
10546 QCBORDecode_VGetNext(&DCtx, &Item);
10547 if(QCBORDecode_Tell(&DCtx) != 50) {
10548 return 6002;
10549 }
10550 QCBORDecode_VGetNext(&DCtx, &Item);
10551 if(QCBORDecode_Tell(&DCtx) != 58) {
10552 return 6008;
10553 }
10554 QCBORDecode_VGetNext(&DCtx, &Item);
10555 (void)QCBORDecode_GetAndResetError(&DCtx);
10556 if(QCBORDecode_Tell(&DCtx) != 58) {
10557 return 6003;
10558 }
10559 QCBORDecode_ExitArray(&DCtx);
10560 if(QCBORDecode_Tell(&DCtx) != 58) {
10561 return 6004;
10562 }
10563
10564 static const uint32_t aEmptiesPos[] =
Laurence Lundblade535bb5e2024-06-16 12:06:32 -070010565 {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 13, 15};
Laurence Lundblade11654912024-05-09 11:49:24 -070010566 QCBORDecode_Init(&DCtx,
10567 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(sEmpties),
10568 0);
10569 for(nIndex = 0; ; nIndex++) {
10570 uPosition = QCBORDecode_Tell(&DCtx);
10571 if(uPosition != aEmptiesPos[nIndex]) {
10572 return nIndex + 200;
10573 }
10574
Laurence Lundblade535bb5e2024-06-16 12:06:32 -070010575 if(QCBORDecode_EndCheck(&DCtx)) {
Laurence Lundblade11654912024-05-09 11:49:24 -070010576 break;
10577 }
10578
10579 QCBORDecode_VGetNext(&DCtx, &Item);
10580 }
10581
10582#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
10583 static const uint32_t aIndefEmptiesPos[] =
Laurence Lundblade535bb5e2024-06-16 12:06:32 -070010584 {0, 1, 2, 4, 5, 7, 8, 10, 12, 13, 16, 19, 25};
Laurence Lundblade11654912024-05-09 11:49:24 -070010585 QCBORDecode_Init(&DCtx,
10586 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(sEmptiesIndef),
10587 0);
10588 for(nIndex = 0; ; nIndex++) {
10589 uPosition = QCBORDecode_Tell(&DCtx);
10590 if(uPosition != aIndefEmptiesPos[nIndex]) {
10591 return nIndex + 300;
10592 }
10593
Laurence Lundblade535bb5e2024-06-16 12:06:32 -070010594 if(QCBORDecode_EndCheck(&DCtx)) {
Laurence Lundblade11654912024-05-09 11:49:24 -070010595 break;
10596 }
10597
10598 QCBORDecode_VGetNext(&DCtx, &Item);
10599 }
10600#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
10601
10602
10603 return 0;
10604}