blob: 6b977685a7137acf31e7b3a84f09a7c6b2f50774 [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 Lundblade2d493002024-02-01 11:09:17 -0700135 0x98, 0x31, 0x3b, 0xff, 0xff, 0xff, 0xff, 0xff,
136 0xff, 0xff, 0xff, 0x3b, 0xFf, 0xff, 0xff, 0xff, 0xff,
137 0xff, 0xff, 0xfe, 0x3b, 0x7f, 0xff, 0xff, 0xff, 0xff,
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800138 0xff, 0xff, 0xff, 0x3b, 0x00, 0x00, 0x00, 0x01,
139 0x00, 0x00, 0x00, 0x00, 0x3a, 0xff, 0xff, 0xff,
140 0xff, 0x3a, 0xff, 0xff, 0xff, 0xfe, 0x3a, 0xff,
141 0xff, 0xff, 0xfd, 0x3a, 0x7f, 0xff, 0xff, 0xff,
142 0x3a, 0x7f, 0xff, 0xff, 0xfe, 0x3a, 0x00, 0x01,
143 0x00, 0x01, 0x3a, 0x00, 0x01, 0x00, 0x00, 0x39,
144 0xff, 0xff, 0x39, 0xff, 0xfe, 0x39, 0xff, 0xfd,
145 0x39, 0x01, 0x00, 0x38, 0xff, 0x38, 0xfe, 0x38,
146 0xfd, 0x38, 0x18, 0x37, 0x36, 0x20, 0x00, 0x00,
147 0x01, 0x16, 0x17, 0x18, 0x18, 0x18, 0x19, 0x18,
148 0x1a, 0x18, 0xfe, 0x18, 0xff, 0x19, 0x01, 0x00,
149 0x19, 0x01, 0x01, 0x19, 0xff, 0xfe, 0x19, 0xff,
150 0xff, 0x1a, 0x00, 0x01, 0x00, 0x00, 0x1a, 0x00,
151 0x01, 0x00, 0x01, 0x1a, 0x00, 0x01, 0x00, 0x02,
152 0x1a, 0x7f, 0xff, 0xff, 0xff, 0x1a, 0x7f, 0xff,
153 0xff, 0xff, 0x1a, 0x80, 0x00, 0x00, 0x00, 0x1a,
154 0x80, 0x00, 0x00, 0x01, 0x1a, 0xff, 0xff, 0xff,
155 0xfe, 0x1a, 0xff, 0xff, 0xff, 0xff, 0x1b, 0x00,
156 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x1b,
157 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01,
158 0x1b, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
159 0xff, 0x1b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
160 0xff, 0xff};
161
162
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800163// return CBOR error or -1 if type of value doesn't match
164
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800165static int32_t IntegerValuesParseTestInternal(QCBORDecodeContext *pDCtx)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800166{
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700167 QCBORItem Item;
168 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800169
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800170 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700171 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800172 if(Item.uDataType != QCBOR_TYPE_ARRAY)
173 return -1;
174
175 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700176 return (int32_t)nCBORError;
Laurence Lundblade2d493002024-02-01 11:09:17 -0700177 if(Item.uDataType != QCBOR_TYPE_65BIT_NEG_INT ||
178 Item.val.uint64 != 18446744073709551615ULL)
179 return -1;
180
181 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
182 return (int32_t)nCBORError;
183 if(Item.uDataType != QCBOR_TYPE_65BIT_NEG_INT ||
184 Item.val.uint64 != 18446744073709551614ULL)
185 return -1;
186
187 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
188 return (int32_t)nCBORError;
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800189 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade570fab52018-10-13 18:28:27 +0800190 Item.val.int64 != -9223372036854775807LL - 1)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800191 return -1;
192
193 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700194 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800195 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade570fab52018-10-13 18:28:27 +0800196 Item.val.int64 != -4294967297)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800197 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800198
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800199 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700200 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800201 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade570fab52018-10-13 18:28:27 +0800202 Item.val.int64 != -4294967296)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800203 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800204
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800205 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700206 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800207 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade570fab52018-10-13 18:28:27 +0800208 Item.val.int64 != -4294967295)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800209 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800210
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800211 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700212 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800213 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade570fab52018-10-13 18:28:27 +0800214 Item.val.int64 != -4294967294)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800215 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800216
217
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800218 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700219 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800220 if(Item.uDataType != QCBOR_TYPE_INT64 ||
221 Item.val.int64 != -2147483648)
222 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800223
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800224 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700225 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800226 if(Item.uDataType != QCBOR_TYPE_INT64 ||
227 Item.val.int64 != -2147483647)
228 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800229
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800230 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700231 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800232 if(Item.uDataType != QCBOR_TYPE_INT64 ||
233 Item.val.int64 != -65538)
234 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800235
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800236 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700237 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800238 if(Item.uDataType != QCBOR_TYPE_INT64 ||
239 Item.val.int64 != -65537)
240 return -1;
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 != -65536)
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 != -65535)
253 return -1;
254
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800255
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800256 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700257 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800258 if(Item.uDataType != QCBOR_TYPE_INT64 ||
259 Item.val.int64 != -65534)
260 return -1;
261
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800262
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800263 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700264 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800265 if(Item.uDataType != QCBOR_TYPE_INT64 ||
266 Item.val.int64 != -257)
267 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800268
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800269 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700270 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800271 if(Item.uDataType != QCBOR_TYPE_INT64 ||
272 Item.val.int64 != -256)
273 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800274
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800275 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700276 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800277 if(Item.uDataType != QCBOR_TYPE_INT64 ||
278 Item.val.int64 != -255)
279 return -1;
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 != -254)
285 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800286
287
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 != -25)
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 != -24)
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 != -23)
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 != -1)
313 return -1;
314
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800315
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800316 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700317 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800318 if(Item.uDataType != QCBOR_TYPE_INT64 ||
319 Item.val.int64 != 0)
320 return -1;
321
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800322
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800323 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700324 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800325 if(Item.uDataType != QCBOR_TYPE_INT64 ||
326 Item.val.int64 != 0)
327 return -1;
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 != 1)
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 != 22)
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 != 23)
347 return -1;
348
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800349
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800350 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700351 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800352 if(Item.uDataType != QCBOR_TYPE_INT64 ||
353 Item.val.int64 != 24)
354 return -1;
355
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800356
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800357 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700358 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800359 if(Item.uDataType != QCBOR_TYPE_INT64 ||
360 Item.val.int64 != 25)
361 return -1;
362
363 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 != 26)
367 return -1;
368
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800369
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 != 254)
374 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800375
376
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 != 255)
381 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800382
383
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800384 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700385 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800386 if(Item.uDataType != QCBOR_TYPE_INT64 ||
387 Item.val.int64 != 256)
388 return -1;
389
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800390
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800391 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700392 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800393 if(Item.uDataType != QCBOR_TYPE_INT64 ||
394 Item.val.int64 != 257)
395 return -1;
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 != 65534)
401 return -1;
402
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800403
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800404 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700405 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800406 if(Item.uDataType != QCBOR_TYPE_INT64 ||
407 Item.val.int64 != 65535)
408 return -1;
409
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800410
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800411 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700412 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800413 if(Item.uDataType != QCBOR_TYPE_INT64 ||
414 Item.val.int64 != 65536)
415 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800416
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800417 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700418 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800419 if(Item.uDataType != QCBOR_TYPE_INT64 ||
420 Item.val.int64 != 65537)
421 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800422
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800423 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700424 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800425 if(Item.uDataType != QCBOR_TYPE_INT64 ||
426 Item.val.int64 != 65538)
427 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800428
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800429 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700430 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800431 if(Item.uDataType != QCBOR_TYPE_INT64 ||
432 Item.val.int64 != 2147483647)
433 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800434
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800435 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700436 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800437 if(Item.uDataType != QCBOR_TYPE_INT64 ||
438 Item.val.int64 != 2147483647)
439 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800440
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800441 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700442 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800443 if(Item.uDataType != QCBOR_TYPE_INT64 ||
444 Item.val.int64 != 2147483648)
445 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800446
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800447 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700448 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800449 if(Item.uDataType != QCBOR_TYPE_INT64 ||
450 Item.val.int64 != 2147483649)
451 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800452
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 != 4294967294)
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 != 4294967295)
464 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800465
466
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800467 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700468 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800469 if(Item.uDataType != QCBOR_TYPE_INT64 ||
470 Item.val.int64 != 4294967296)
471 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800472
473
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800474 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700475 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800476 if(Item.uDataType != QCBOR_TYPE_INT64 ||
477 Item.val.int64 != 4294967297)
478 return -1;
479
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800480
481
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800482 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700483 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800484 if(Item.uDataType != QCBOR_TYPE_INT64 ||
485 Item.val.int64 != 9223372036854775807LL)
486 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800487
488
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800489 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700490 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800491 if(Item.uDataType != QCBOR_TYPE_UINT64 ||
492 Item.val.uint64 != 18446744073709551615ULL)
493 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800494
495
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800496 if(QCBORDecode_Finish(pDCtx) != QCBOR_SUCCESS) {
497 return -1;
498 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800499
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800500 return 0;
501}
502
503
Laurence Lundblade21d1d812019-09-28 22:47:49 -1000504
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800505/*
506 Tests the decoding of lots of different integers sizes
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +0800507 and values.
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800508 */
Maxim Zhukovd538f0a2022-12-20 20:40:38 +0300509int32_t IntegerValuesParseTest(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800510{
Laurence Lundblade21d1d812019-09-28 22:47:49 -1000511 int nReturn;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800512 QCBORDecodeContext DCtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800513
Laurence Lundblade21d1d812019-09-28 22:47:49 -1000514 QCBORDecode_Init(&DCtx,
515 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedEncodedInts),
516 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800517
Laurence Lundblade21d1d812019-09-28 22:47:49 -1000518 // The really big test of all successes
519 nReturn = IntegerValuesParseTestInternal(&DCtx);
520 if(nReturn) {
521 return nReturn;
522 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800523
Laurence Lundblade21d1d812019-09-28 22:47:49 -1000524 return(nReturn);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800525}
526
527
528/*
Laurence Lundbladeee851742020-01-08 08:37:05 -0800529 Creates a simple CBOR array and returns it in *pEncoded. The array is
530 malloced and needs to be freed. This is used by several tests.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800531
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800532 Two of the inputs can be set. Two other items in the array are fixed.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800533
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800534 */
535
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -0800536static uint8_t spSimpleArrayBuffer[50];
537
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800538static int32_t CreateSimpleArray(int nInt1, int nInt2, uint8_t **pEncoded, size_t *pEncodedLen)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800539{
540 QCBOREncodeContext ECtx;
541 int nReturn = -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800542
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800543 *pEncoded = NULL;
544 *pEncodedLen = INT32_MAX;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800545
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800546 // loop runs CBOR encoding twice. First with no buffer to
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -0800547 // calculate the length so buffer can be allocated correctly,
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800548 // and last with the buffer to do the actual encoding
549 do {
Laurence Lundblade0595e932018-11-02 22:22:47 +0700550 QCBOREncode_Init(&ECtx, (UsefulBuf){*pEncoded, *pEncodedLen});
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800551 QCBOREncode_OpenArray(&ECtx);
552 QCBOREncode_AddInt64(&ECtx, nInt1);
553 QCBOREncode_AddInt64(&ECtx, nInt2);
554 QCBOREncode_AddBytes(&ECtx, ((UsefulBufC) {"galactic", 8}));
555 QCBOREncode_AddBytes(&ECtx, ((UsefulBufC) {"haven token", 11}));
556 QCBOREncode_CloseArray(&ECtx);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800557
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -0800558 if(QCBOREncode_FinishGetSize(&ECtx, pEncodedLen))
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800559 goto Done;
560
561 if(*pEncoded != NULL) {
562 nReturn = 0;
563 goto Done;
564 }
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -0800565
566 // Use static buffer to avoid dependency on malloc()
567 if(*pEncodedLen > sizeof(spSimpleArrayBuffer)) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800568 goto Done;
569 }
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -0800570 *pEncoded = spSimpleArrayBuffer;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800571
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800572 } while(1);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800573
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -0800574Done:
575 return nReturn;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800576}
577
578
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800579/*
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800580 Some basic CBOR with map and array used in a lot of tests.
581 The map labels are all strings
582
Laurence Lundbladecc7da412020-12-27 00:09:07 -0800583 {
584 "first integer": 42,
585 "an array of two strings": [
586 "string1", "string2"
587 ],
588 "map in a map": {
589 "bytes 1": h'78787878',
590 "bytes 2": h'79797979',
591 "another int": 98,
592 "text 2": "lies, damn lies and statistics"
593 }
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900594 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800595 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800596static const uint8_t pValidMapEncoded[] = {
Laurence Lundbladecc7da412020-12-27 00:09:07 -0800597 0xa3, 0x6d, 0x66, 0x69, 0x72, 0x73, 0x74, 0x20,
598 0x69, 0x6e, 0x74, 0x65, 0x67, 0x65, 0x72, 0x18,
599 0x2a, 0x77, 0x61, 0x6e, 0x20, 0x61, 0x72, 0x72,
600 0x61, 0x79, 0x20, 0x6f, 0x66, 0x20, 0x74, 0x77,
601 0x6f, 0x20, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67,
602 0x73, 0x82, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e,
603 0x67, 0x31, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e,
604 0x67, 0x32, 0x6c, 0x6d, 0x61, 0x70, 0x20, 0x69,
605 0x6e, 0x20, 0x61, 0x20, 0x6d, 0x61, 0x70, 0xa4,
606 0x67, 0x62, 0x79, 0x74, 0x65, 0x73, 0x20, 0x31,
607 0x44, 0x78, 0x78, 0x78, 0x78, 0x67, 0x62, 0x79,
608 0x74, 0x65, 0x73, 0x20, 0x32, 0x44, 0x79, 0x79,
609 0x79, 0x79, 0x6b, 0x61, 0x6e, 0x6f, 0x74, 0x68,
610 0x65, 0x72, 0x20, 0x69, 0x6e, 0x74, 0x18, 0x62,
611 0x66, 0x74, 0x65, 0x78, 0x74, 0x20, 0x32, 0x78,
612 0x1e, 0x6c, 0x69, 0x65, 0x73, 0x2c, 0x20, 0x64,
613 0x61, 0x6d, 0x6e, 0x20, 0x6c, 0x69, 0x65, 0x73,
614 0x20, 0x61, 0x6e, 0x64, 0x20, 0x73, 0x74, 0x61,
615 0x74, 0x69, 0x73, 0x74, 0x69, 0x63, 0x73 };
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800616
Laurence Lundbladee2c893c2020-12-26 17:41:53 -0800617
618#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundblade2c1faf92020-06-26 22:43:56 -0700619// Same as above, but with indefinite lengths.
620static const uint8_t pValidMapIndefEncoded[] = {
Laurence Lundbladecc7da412020-12-27 00:09:07 -0800621 0xbf, 0x6d, 0x66, 0x69, 0x72, 0x73, 0x74, 0x20,
622 0x69, 0x6e, 0x74, 0x65, 0x67, 0x65, 0x72, 0x18,
623 0x2a, 0x77, 0x61, 0x6e, 0x20, 0x61, 0x72, 0x72,
624 0x61, 0x79, 0x20, 0x6f, 0x66, 0x20, 0x74, 0x77,
625 0x6f, 0x20, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67,
626 0x73, 0x9f, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e,
627 0x67, 0x31, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e,
628 0x67, 0x32, 0xff, 0x6c, 0x6d, 0x61, 0x70, 0x20,
629 0x69, 0x6e, 0x20, 0x61, 0x20, 0x6d, 0x61, 0x70,
630 0xbf, 0x67, 0x62, 0x79, 0x74, 0x65, 0x73, 0x20,
631 0x31, 0x44, 0x78, 0x78, 0x78, 0x78, 0x67, 0x62,
632 0x79, 0x74, 0x65, 0x73, 0x20, 0x32, 0x44, 0x79,
633 0x79, 0x79, 0x79, 0x6b, 0x61, 0x6e, 0x6f, 0x74,
634 0x68, 0x65, 0x72, 0x20, 0x69, 0x6e, 0x74, 0x18,
635 0x62, 0x66, 0x74, 0x65, 0x78, 0x74, 0x20, 0x32,
636 0x78, 0x1e, 0x6c, 0x69, 0x65, 0x73, 0x2c, 0x20,
637 0x64, 0x61, 0x6d, 0x6e, 0x20, 0x6c, 0x69, 0x65,
638 0x73, 0x20, 0x61, 0x6e, 0x64, 0x20, 0x73, 0x74,
639 0x61, 0x74, 0x69, 0x73, 0x74, 0x69, 0x63, 0x73,
640 0xff, 0xff};
Laurence Lundbladee2c893c2020-12-26 17:41:53 -0800641#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
Laurence Lundblade2c1faf92020-06-26 22:43:56 -0700642
643
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800644static int32_t ParseOrderedArray(const uint8_t *pEncoded,
Laurence Lundblade02625d42020-06-25 14:41:41 -0700645 size_t nLen,
646 int64_t *pInt1,
647 int64_t *pInt2,
648 const uint8_t **pBuf3,
649 size_t *pBuf3Len,
650 const uint8_t **pBuf4,
651 size_t *pBuf4Len)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800652{
653 QCBORDecodeContext DCtx;
654 QCBORItem Item;
655 int nReturn = -1; // assume error until success
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800656
Laurence Lundbladeee851742020-01-08 08:37:05 -0800657 QCBORDecode_Init(&DCtx,
658 (UsefulBufC){pEncoded, nLen},
659 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800660
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800661 // Make sure the first thing is a map
Laurence Lundblade9b334962020-08-27 10:55:53 -0700662 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
663 Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800664 goto Done;
Laurence Lundblade9b334962020-08-27 10:55:53 -0700665 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800666
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800667 // First integer
Laurence Lundblade9b334962020-08-27 10:55:53 -0700668 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
669 Item.uDataType != QCBOR_TYPE_INT64) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800670 goto Done;
Laurence Lundblade9b334962020-08-27 10:55:53 -0700671 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800672 *pInt1 = Item.val.int64;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800673
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800674 // Second integer
Laurence Lundblade9b334962020-08-27 10:55:53 -0700675 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
676 Item.uDataType != QCBOR_TYPE_INT64) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800677 goto Done;
Laurence Lundblade9b334962020-08-27 10:55:53 -0700678 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800679 *pInt2 = Item.val.int64;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800680
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800681 // First string
Laurence Lundblade9b334962020-08-27 10:55:53 -0700682 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
683 Item.uDataType != QCBOR_TYPE_BYTE_STRING) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800684 goto Done;
Laurence Lundblade9b334962020-08-27 10:55:53 -0700685 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800686 *pBuf3 = Item.val.string.ptr;
687 *pBuf3Len = Item.val.string.len;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800688
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800689 // Second string
Laurence Lundblade9b334962020-08-27 10:55:53 -0700690 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
691 Item.uDataType != QCBOR_TYPE_BYTE_STRING) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800692 goto Done;
Laurence Lundblade9b334962020-08-27 10:55:53 -0700693 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800694 *pBuf4 = Item.val.string.ptr;
695 *pBuf4Len = Item.val.string.len;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800696
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800697 nReturn = 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800698
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800699Done:
700 return(nReturn);
701}
702
703
704
Laurence Lundblade9e3651c2018-10-10 11:49:55 +0800705
Maxim Zhukovd538f0a2022-12-20 20:40:38 +0300706int32_t SimpleArrayTest(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800707{
708 uint8_t *pEncoded;
709 size_t nEncodedLen;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800710
Laurence Lundblade5e390822019-01-06 12:35:01 -0800711 int64_t i1=0, i2=0;
712 size_t i3=0, i4=0;
713 const uint8_t *s3= (uint8_t *)"";
714 const uint8_t *s4= (uint8_t *)"";
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800715
716
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800717 if(CreateSimpleArray(23, 6000, &pEncoded, &nEncodedLen) < 0) {
718 return(-1);
719 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800720
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800721 ParseOrderedArray(pEncoded, nEncodedLen, &i1, &i2, &s3, &i3, &s4, &i4);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800722
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800723 if(i1 != 23 ||
724 i2 != 6000 ||
725 i3 != 8 ||
726 i4 != 11 ||
Laurence Lundbladea44d5062018-10-17 18:45:12 +0530727 memcmp("galactic", s3, 8) !=0 ||
728 memcmp("haven token", s4, 11) !=0) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800729 return(-1);
730 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800731
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800732 return(0);
733}
734
735
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700736/*
737 [
738 0,
739 [],
740 [
741 [],
742 [
743 0
744 ],
745 {},
746 {
747 1: {},
748 2: {},
749 3: []
750 }
751 ]
752 ]
753 */
Laurence Lundbladecc7da412020-12-27 00:09:07 -0800754static uint8_t sEmpties[] = {
755 0x83, 0x00, 0x80, 0x84, 0x80, 0x81, 0x00, 0xa0,
756 0xa3, 0x01, 0xa0, 0x02, 0xa0, 0x03, 0x80};
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700757
Laurence Lundbladee2c893c2020-12-26 17:41:53 -0800758#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundblade02625d42020-06-25 14:41:41 -0700759/* Same as above, but with indefinte lengths */
Laurence Lundbladecc7da412020-12-27 00:09:07 -0800760static const uint8_t sEmptiesIndef[] = {
Laurence Lundblade02625d42020-06-25 14:41:41 -07007610x9F,
Laurence Lundblade2c1faf92020-06-26 22:43:56 -0700762 0x00,
763 0x9F,
764 0xFF,
765 0x9F,
766 0x9F,
767 0xFF,
768 0x9F,
769 0x00,
770 0xFF,
771 0xBF,
772 0xFF,
773 0xBF,
774 0x01,
775 0xBF,
776 0xFF,
777 0x02,
778 0xBF,
779 0xFF,
780 0x03,
781 0x9F,
782 0xFF,
783 0xFF,
784 0xFF,
Laurence Lundblade02625d42020-06-25 14:41:41 -0700785 0xFF};
Laurence Lundbladee2c893c2020-12-26 17:41:53 -0800786#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
Laurence Lundblade02625d42020-06-25 14:41:41 -0700787
788
789static int32_t CheckEmpties(UsefulBufC input, bool bCheckCounts)
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700790{
791 QCBORDecodeContext DCtx;
792 QCBORItem Item;
793
Laurence Lundbladeee851742020-01-08 08:37:05 -0800794 QCBORDecode_Init(&DCtx,
Laurence Lundblade02625d42020-06-25 14:41:41 -0700795 input,
Laurence Lundbladeee851742020-01-08 08:37:05 -0800796 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700797
798 // Array with 3 items
799 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
800 Item.uDataType != QCBOR_TYPE_ARRAY ||
801 Item.uNestingLevel != 0 ||
802 Item.uNextNestLevel != 1 ||
Laurence Lundblade02625d42020-06-25 14:41:41 -0700803 (bCheckCounts && Item.val.uCount != 3)) {
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700804 return -1;
805 }
806
807 // An integer 0
808 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
809 Item.uDataType != QCBOR_TYPE_INT64 ||
810 Item.uNestingLevel != 1 ||
811 Item.uNextNestLevel != 1 ||
812 Item.val.uint64 != 0) {
813 return -2;
814 }
815
816 // An empty array
817 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
818 Item.uDataType != QCBOR_TYPE_ARRAY ||
819 Item.uNestingLevel != 1 ||
820 Item.uNextNestLevel != 1 ||
Laurence Lundblade02625d42020-06-25 14:41:41 -0700821 (bCheckCounts && Item.val.uCount != 0)) {
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700822 return -3;
823 }
824
825 // An array with 4 items
826 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
827 Item.uDataType != QCBOR_TYPE_ARRAY ||
828 Item.uNestingLevel != 1 ||
829 Item.uNextNestLevel != 2 ||
Laurence Lundblade02625d42020-06-25 14:41:41 -0700830 (bCheckCounts && Item.val.uCount != 4)) {
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700831 return -4;
832 }
833
834 // An empty array
835 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
836 Item.uDataType != QCBOR_TYPE_ARRAY ||
837 Item.uNestingLevel != 2 ||
838 Item.uNextNestLevel != 2 ||
Laurence Lundblade02625d42020-06-25 14:41:41 -0700839 (bCheckCounts && Item.val.uCount != 0)) {
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700840 return -5;
841 }
842
843 // An array with 1 item
844 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
845 Item.uDataType != QCBOR_TYPE_ARRAY ||
846 Item.uNestingLevel != 2 ||
847 Item.uNextNestLevel != 3 ||
Laurence Lundblade02625d42020-06-25 14:41:41 -0700848 (bCheckCounts && Item.val.uCount != 1)) {
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700849 return -6;
850 }
851
852 // An integer 0
853 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
854 Item.uDataType != QCBOR_TYPE_INT64 ||
855 Item.uNestingLevel != 3 ||
856 Item.uNextNestLevel != 2 ||
857 Item.val.uint64 != 0) {
858 return -7;
859 }
860
861 // An empty map
862 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
863 Item.uDataType != QCBOR_TYPE_MAP ||
864 Item.uNestingLevel != 2 ||
865 Item.uNextNestLevel != 2 ||
Laurence Lundblade02625d42020-06-25 14:41:41 -0700866 (bCheckCounts && Item.val.uCount != 0)) {
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700867 return -8;
868 }
869
Laurence Lundblade5e87da62020-06-07 03:24:28 -0700870 // A map with 3 items
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700871 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
872 Item.uDataType != QCBOR_TYPE_MAP ||
873 Item.uNestingLevel != 2 ||
874 Item.uNextNestLevel != 3 ||
Laurence Lundblade02625d42020-06-25 14:41:41 -0700875 (bCheckCounts && Item.val.uCount != 3)) {
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700876 return -9;
877 }
878
879 // An empty map
880 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
881 Item.uDataType != QCBOR_TYPE_MAP ||
882 Item.uNestingLevel != 3 ||
883 Item.uNextNestLevel != 3 ||
Laurence Lundblade02625d42020-06-25 14:41:41 -0700884 (bCheckCounts && Item.val.uCount != 0)) {
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700885 return -10;
886 }
887
888 // An empty map
889 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
890 Item.uDataType != QCBOR_TYPE_MAP ||
891 Item.uNestingLevel != 3 ||
892 Item.uNextNestLevel != 3 ||
Laurence Lundblade02625d42020-06-25 14:41:41 -0700893 (bCheckCounts && Item.val.uCount != 0)) {
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700894 return -11;
895 }
896
897 // An empty array
898 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
899 Item.uDataType != QCBOR_TYPE_ARRAY ||
900 Item.uNestingLevel != 3 ||
901 Item.uNextNestLevel != 0 ||
Laurence Lundblade02625d42020-06-25 14:41:41 -0700902 (bCheckCounts && Item.val.uCount != 0)) {
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700903 return -12;
904 }
905
906 if(QCBORDecode_Finish(&DCtx) != QCBOR_SUCCESS) {
907 return -13;
908 }
Laurence Lundblade02625d42020-06-25 14:41:41 -0700909 return 0;
910}
911
912
Maxim Zhukovd538f0a2022-12-20 20:40:38 +0300913int32_t EmptyMapsAndArraysTest(void)
Laurence Lundblade02625d42020-06-25 14:41:41 -0700914{
915 int nResult;
916 nResult = CheckEmpties(UsefulBuf_FROM_BYTE_ARRAY_LITERAL(sEmpties),
917 true);
918 if(nResult) {
919 return nResult;
920 }
921
Laurence Lundbladee2c893c2020-12-26 17:41:53 -0800922#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundblade02625d42020-06-25 14:41:41 -0700923 nResult = CheckEmpties(UsefulBuf_FROM_BYTE_ARRAY_LITERAL(sEmptiesIndef),
924 false);
925
926 if(nResult) {
927 return nResult -100;
928 }
Laurence Lundbladee2c893c2020-12-26 17:41:53 -0800929#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700930
931 return 0;
932}
933
Laurence Lundblade4c532ca2021-02-18 21:31:49 -0700934
935static const uint8_t sEmptyMap[] = {
Michael Richardson87de9af2021-02-18 23:13:31 -0500936 0xA1, //# map(1)
937 0x02, //# unsigned(2)
938 0xA0, //# map(0)
939};
940
941int32_t ParseEmptyMapInMapTest(void)
942{
943 QCBORDecodeContext DCtx;
944 QCBORItem Item;
945 int nReturn = 0;
Laurence Lundblade4c532ca2021-02-18 21:31:49 -0700946 QCBORError uErr;
Michael Richardson87de9af2021-02-18 23:13:31 -0500947
948 QCBORDecode_Init(&DCtx,
949 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(sEmptyMap),
950 QCBOR_DECODE_MODE_NORMAL);
951
952 /* now open the first Map */
Laurence Lundblade4c532ca2021-02-18 21:31:49 -0700953 uErr = QCBORDecode_GetNext(&DCtx, &Item);
954 if(uErr != QCBOR_SUCCESS ||
Michael Richardson87de9af2021-02-18 23:13:31 -0500955 Item.uDataType != QCBOR_TYPE_MAP) {
956 nReturn = -3;
957 goto done;
958 }
959
960 if(QCBORDecode_GetNext(&DCtx, &Item) != 0) {
961 nReturn = -1;
962 goto done;
963 }
964 if(Item.uDataType != QCBOR_TYPE_MAP ||
965 Item.uNestingLevel != 1 ||
966 Item.label.int64 != 2) {
967 nReturn = -2;
968 goto done;
969 }
970
971 done:
972 return(nReturn);
973}
974
Laurence Lundblade4c532ca2021-02-18 21:31:49 -0700975
Michael Richardson87de9af2021-02-18 23:13:31 -0500976/* [[[[[[[[[[]]]]]]]]]] */
Laurence Lundbladecc7da412020-12-27 00:09:07 -0800977static const uint8_t spDeepArrays[] = {
978 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81,
979 0x81, 0x80};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800980
Maxim Zhukovd538f0a2022-12-20 20:40:38 +0300981int32_t ParseDeepArrayTest(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800982{
983 QCBORDecodeContext DCtx;
984 int nReturn = 0;
985 int i;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800986
Laurence Lundbladeee851742020-01-08 08:37:05 -0800987 QCBORDecode_Init(&DCtx,
988 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spDeepArrays),
989 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800990
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800991 for(i = 0; i < 10; i++) {
992 QCBORItem Item;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800993
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800994 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
995 Item.uDataType != QCBOR_TYPE_ARRAY ||
996 Item.uNestingLevel != i) {
997 nReturn = -1;
998 break;
999 }
1000 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001001
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001002 return(nReturn);
1003}
1004
Laurence Lundbladecc7da412020-12-27 00:09:07 -08001005/* Big enough to test nesting to the depth of 24
1006 [[[[[[[[[[[[[[[[[[[[[[[[[]]]]]]]]]]]]]]]]]]]]]]]]]
1007 */
1008static const uint8_t spTooDeepArrays[] = {
1009 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81,
1010 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81,
1011 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81,
1012 0x80};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001013
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03001014int32_t ParseTooDeepArrayTest(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001015{
1016 QCBORDecodeContext DCtx;
1017 int nReturn = 0;
1018 int i;
1019 QCBORItem Item;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001020
1021
Laurence Lundbladeee851742020-01-08 08:37:05 -08001022 QCBORDecode_Init(&DCtx,
1023 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTooDeepArrays),
1024 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001025
Laurence Lundblade3e0b2302023-12-04 14:02:38 -07001026 for(i = 0; i < QCBOR_MAX_ARRAY_NESTING; i++) {
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001027
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001028 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
1029 Item.uDataType != QCBOR_TYPE_ARRAY ||
1030 Item.uNestingLevel != i) {
1031 nReturn = -1;
1032 break;
1033 }
1034 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001035
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001036 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_ARRAY_DECODE_NESTING_TOO_DEEP)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001037 nReturn = -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001038
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001039 return(nReturn);
1040}
1041
1042
1043
1044
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03001045int32_t ShortBufferParseTest(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001046{
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001047 int nResult = 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001048
Laurence Lundblade06350ea2020-01-27 19:32:40 -08001049 for(size_t nNum = sizeof(spExpectedEncodedInts)-1; nNum; nNum--) {
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001050 QCBORDecodeContext DCtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001051
Laurence Lundbladeee851742020-01-08 08:37:05 -08001052 QCBORDecode_Init(&DCtx,
1053 (UsefulBufC){spExpectedEncodedInts, nNum},
1054 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001055
Laurence Lundblade06350ea2020-01-27 19:32:40 -08001056 const int nErr = IntegerValuesParseTestInternal(&DCtx);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001057
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001058 if(nErr != QCBOR_ERR_HIT_END && nErr != QCBOR_ERR_NO_MORE_ITEMS) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001059 nResult = -1;
1060 goto Done;
1061 }
1062 }
1063Done:
1064 return nResult;
1065}
1066
1067
Laurence Lundblade9e3651c2018-10-10 11:49:55 +08001068
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03001069int32_t ShortBufferParseTest2(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001070{
1071 uint8_t *pEncoded;
1072 int nReturn;
1073 size_t nEncodedLen;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001074
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001075 int64_t i1, i2;
1076 size_t i3, i4;
1077 const uint8_t *s3, *s4;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001078
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001079 nReturn = 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001080
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001081 if(CreateSimpleArray(23, 6000, &pEncoded, &nEncodedLen) < 0) {
1082 return(-1);
1083 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001084
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001085 for(nEncodedLen--; nEncodedLen; nEncodedLen--) {
Laurence Lundblade9b334962020-08-27 10:55:53 -07001086 int nResult = ParseOrderedArray(pEncoded, (uint32_t)nEncodedLen, &i1,
1087 &i2, &s3, &i3, &s4, &i4);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001088 if(nResult == 0) {
1089 nReturn = -1;
1090 }
1091 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001092
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001093 return(nReturn);
1094}
1095
Laurence Lundblade5db34da2024-05-30 03:14:35 -07001096
1097/* This test requires indef strings, HW float and preferred float,... */
1098#if !defined(QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS) && \
1099 !defined(QCBOR_DISABLE_FLOAT_HW_USE) && \
1100 !defined(QCBOR_DISABLE_PREFERRED_FLOAT) && \
1101 !defined(QCBOR_DISABLE_TAGS) && \
1102 !defined(QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS)
1103
1104static const uint8_t pPerverseLabels[] = {
1105 0xae,
1106
1107 0xf5, 0x61, 0x61,
1108
1109 0xf6, 0x61, 0x62,
1110
1111 0xf8, 0xff, 0x61, 0x63,
1112
1113 0xf9, 0x7e, 0x00, 0x61, 0x64,
1114
1115 0xfa, 0x7f, 0x7f, 0xff, 0xff, 0x61, 0x65,
1116
1117 0xfb, 0xff, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x61, 0x66,
1118
1119 0xa1, 0x19, 0x03, 0xe8, 0x10, 0x61, 0x67,
1120
1121 0x81, 0x81, 0x81, 0x80, 0x61, 0x68,
1122
1123 0xc1, 0x09, 0x61, 0x69,
1124
1125 0x82, 0x05, 0xa2, 0x01, 0x02, 0x03, 0x04, 0x61, 0x6a,
1126
1127 0xbf, 0xff, 0x61, 0x6b,
1128
1129 0x9f, 0x11, 0x12, 0x13, 0xff, 0x61, 0x6c,
1130
1131 0x7f, 0x62, 0x41, 0x42, 0x62, 0x43, 0x44, 0xff, 0x61, 0x6d,
1132
1133 0xd9, 0x01, 0x02, 0xbf, 0x7f, 0x61, 0x4a, 0x61, 0x4b, 0xff, 0x00, 0xf4, 0xd7, 0x80 ,0xff, 0x61, 0x6e
1134};
1135#endif
1136
1137
Laurence Lundbladeec290b82024-06-10 11:10:54 -07001138#ifndef QCBOR_DISABLE_NON_INTEGER_LABELS
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301139/*
1140 Decode and thoroughly check a moderately complex
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001141 set of maps. Can be run in QCBOR_DECODE_MODE_NORMAL or in
1142 QCBOR_DECODE_MODE_MAP_STRINGS_ONLY.
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301143 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001144static int32_t ParseMapTest1(QCBORDecodeMode nMode)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001145{
1146 QCBORDecodeContext DCtx;
1147 QCBORItem Item;
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001148 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001149
Laurence Lundbladeee851742020-01-08 08:37:05 -08001150 QCBORDecode_Init(&DCtx,
1151 (UsefulBufC){pValidMapEncoded, sizeof(pValidMapEncoded)},
1152 nMode);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001153
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001154 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001155 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001156 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001157 if(Item.uDataType != QCBOR_TYPE_MAP ||
1158 Item.val.uCount != 3)
1159 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001160
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001161 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001162 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001163 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07001164
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001165 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001166 Item.uDataType != QCBOR_TYPE_INT64 ||
1167 Item.val.int64 != 42 ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301168 Item.uDataAlloc ||
1169 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001170 UsefulBufCompareToSZ(Item.label.string, "first integer")) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001171 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001172 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001173
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001174 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001175 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001176 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001177 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301178 Item.uDataAlloc ||
1179 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001180 UsefulBufCompareToSZ(Item.label.string, "an array of two strings") ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001181 Item.uDataType != QCBOR_TYPE_ARRAY ||
1182 Item.val.uCount != 2)
1183 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001184
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001185 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001186 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001187 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001188 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301189 Item.uDataAlloc ||
1190 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001191 UsefulBufCompareToSZ(Item.val.string, "string1")) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001192 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001193 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001194
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001195 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001196 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001197 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001198 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301199 Item.uDataAlloc ||
1200 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001201 UsefulBufCompareToSZ(Item.val.string, "string2")) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001202 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001203 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001204
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001205 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001206 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001207 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001208 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301209 Item.uDataAlloc ||
1210 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001211 UsefulBufCompareToSZ(Item.label.string, "map in a map") ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001212 Item.uDataType != QCBOR_TYPE_MAP ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001213 Item.val.uCount != 4) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001214 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001215 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001216
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001217 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001218 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001219 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001220 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001221 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("bytes 1"))||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001222 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301223 Item.uDataAlloc ||
1224 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001225 UsefulBufCompareToSZ(Item.val.string, "xxxx")) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001226 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001227 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001228
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001229 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001230 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001231 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001232 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001233 UsefulBufCompareToSZ(Item.label.string, "bytes 2") ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001234 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301235 Item.uDataAlloc ||
1236 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001237 UsefulBufCompareToSZ(Item.val.string, "yyyy")) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001238 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001239 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001240
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001241 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001242 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001243 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001244 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301245 Item.uDataAlloc ||
1246 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001247 UsefulBufCompareToSZ(Item.label.string, "another int") ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001248 Item.uDataType != QCBOR_TYPE_INT64 ||
1249 Item.val.int64 != 98)
1250 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001251
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001252 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001253 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001254 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001255 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001256 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("text 2"))||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001257 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301258 Item.uDataAlloc ||
1259 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001260 UsefulBufCompareToSZ(Item.val.string, "lies, damn lies and statistics")) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001261 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001262 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001263
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001264 return 0;
1265}
Laurence Lundbladeec290b82024-06-10 11:10:54 -07001266#endif /* ! QCBOR_DISABLE_NON_INTEGER_LABELS */
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001267
Laurence Lundblade5db34da2024-05-30 03:14:35 -07001268/* This test requires indef strings, HW float and preferred float,... */
1269#if !defined(QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS) && \
1270 !defined(QCBOR_DISABLE_FLOAT_HW_USE) && \
1271 !defined(QCBOR_DISABLE_PREFERRED_FLOAT) && \
1272 !defined(QCBOR_DISABLE_TAGS) && \
1273 !defined(QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS)
1274
1275/* Utility to decode a one byte string and match to letter. */
1276static QCBORError
1277CheckOneLetterString(QCBORDecodeContext *pDecode, uint8_t letter)
1278{
1279 UsefulBufC Text;
1280 QCBORError uErr;
1281
1282 QCBORDecode_GetTextString(pDecode, &Text);
1283 uErr = QCBORDecode_GetError(pDecode);
1284 if(uErr) {
1285 return uErr;
1286 }
1287
1288 if(Text.len != 1) {
1289 return QCBOR_ERR_FIRST_USER_DEFINED;
1290 }
1291
1292 if(*(const uint8_t *)Text.ptr != letter) {
1293 return QCBOR_ERR_FIRST_USER_DEFINED;
1294 }
1295
1296 return QCBOR_SUCCESS;
1297}
1298#endif
1299
1300
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001301/*
1302 Decode and thoroughly check a moderately complex
Laurence Lundbladed4cc1032020-10-12 04:19:47 -07001303 set of maps in the QCBOR_DECODE_MODE_MAP_AS_ARRAY mode.
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001304 */
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03001305int32_t ParseMapAsArrayTest(void)
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001306{
1307 QCBORDecodeContext DCtx;
Laurence Lundblade5db34da2024-05-30 03:14:35 -07001308 QCBORItem Item;
1309 QCBORError uErr;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001310
Laurence Lundbladeee851742020-01-08 08:37:05 -08001311 QCBORDecode_Init(&DCtx,
1312 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded),
1313 QCBOR_DECODE_MODE_MAP_AS_ARRAY);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001314
Laurence Lundblade5db34da2024-05-30 03:14:35 -07001315 if((uErr = QCBORDecode_GetNext(&DCtx, &Item))) {
1316 return MakeTestResultCode(1, 1, uErr);
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001317 }
Laurence Lundbladed61cbf32018-12-09 11:42:21 -08001318 if(Item.uDataType != QCBOR_TYPE_MAP_AS_ARRAY ||
1319 Item.val.uCount != 6) {
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001320 return -1;
1321 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001322
Laurence Lundblade5db34da2024-05-30 03:14:35 -07001323 if((uErr = QCBORDecode_GetNext(&DCtx, &Item))) {
1324 return (int32_t)uErr;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001325 }
1326 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1327 Item.uDataAlloc ||
1328 Item.uLabelAlloc ||
1329 Item.uLabelType != QCBOR_TYPE_NONE ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001330 UsefulBufCompareToSZ(Item.val.string, "first integer")) {
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001331 return -2;
1332 }
1333
Laurence Lundblade5db34da2024-05-30 03:14:35 -07001334 if((uErr = QCBORDecode_GetNext(&DCtx, &Item))) {
1335 return (int32_t)uErr;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001336 }
1337 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1338 Item.uDataType != QCBOR_TYPE_INT64 ||
1339 Item.val.int64 != 42 ||
1340 Item.uDataAlloc ||
1341 Item.uLabelAlloc) {
1342 return -3;
1343 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001344
Laurence Lundblade5db34da2024-05-30 03:14:35 -07001345 if((uErr = QCBORDecode_GetNext(&DCtx, &Item))) {
1346 return (int32_t)uErr;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001347 }
1348 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1349 Item.uDataAlloc ||
1350 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001351 UsefulBufCompareToSZ(Item.val.string, "an array of two strings") ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001352 Item.uDataType != QCBOR_TYPE_TEXT_STRING) {
1353 return -4;
1354 }
1355
Laurence Lundblade5db34da2024-05-30 03:14:35 -07001356 if((uErr = QCBORDecode_GetNext(&DCtx, &Item))) {
1357 return (int32_t)uErr;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001358 }
1359 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1360 Item.uDataAlloc ||
1361 Item.uLabelAlloc ||
1362 Item.uDataType != QCBOR_TYPE_ARRAY ||
1363 Item.val.uCount != 2) {
1364 return -5;
1365 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001366
Laurence Lundblade5db34da2024-05-30 03:14:35 -07001367 if((uErr = QCBORDecode_GetNext(&DCtx, &Item))) {
1368 return (int32_t)uErr;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001369 }
1370 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1371 Item.val.string.len != 7 ||
1372 Item.uDataAlloc ||
1373 Item.uLabelAlloc ||
1374 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("string1"))) {
1375 return -6;
1376 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001377
Laurence Lundblade5db34da2024-05-30 03:14:35 -07001378 if((uErr = QCBORDecode_GetNext(&DCtx, &Item))) {
1379 return (int32_t)uErr;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001380 }
1381 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1382 Item.uDataAlloc ||
1383 Item.uLabelAlloc ||
1384 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("string2"))) {
1385 return -7;
1386 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001387
1388
Laurence Lundblade5db34da2024-05-30 03:14:35 -07001389 if((uErr = QCBORDecode_GetNext(&DCtx, &Item))) {
1390 return (int32_t)uErr;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001391 }
1392 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1393 Item.uDataAlloc ||
1394 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001395 UsefulBufCompareToSZ(Item.val.string, "map in a map")) {
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001396 return -8;
1397 }
1398
Laurence Lundblade5db34da2024-05-30 03:14:35 -07001399 if((uErr = QCBORDecode_GetNext(&DCtx, &Item))) {
1400 return (int32_t)uErr;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001401 }
1402 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1403 Item.uDataAlloc ||
1404 Item.uLabelAlloc ||
Laurence Lundbladed61cbf32018-12-09 11:42:21 -08001405 Item.uDataType != QCBOR_TYPE_MAP_AS_ARRAY ||
1406 Item.val.uCount != 8) {
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001407 return -9;
1408 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001409
Laurence Lundblade5db34da2024-05-30 03:14:35 -07001410 if((uErr = QCBORDecode_GetNext(&DCtx, &Item))) {
1411 return (int32_t)uErr;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001412 }
1413 if(Item.uLabelType != QCBOR_TYPE_NONE ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001414 UsefulBufCompareToSZ(Item.val.string, "bytes 1") ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001415 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1416 Item.uDataAlloc ||
1417 Item.uLabelAlloc) {
1418 return -10;
1419 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001420
Laurence Lundblade5db34da2024-05-30 03:14:35 -07001421 if((uErr = QCBORDecode_GetNext(&DCtx, &Item))) {
1422 return (int32_t)uErr;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001423 }
1424 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1425 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
1426 Item.uDataAlloc ||
1427 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001428 UsefulBufCompareToSZ(Item.val.string, "xxxx")) {
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001429 return -11;
1430 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001431
Laurence Lundblade5db34da2024-05-30 03:14:35 -07001432 if((uErr = QCBORDecode_GetNext(&DCtx, &Item))) {
1433 return (int32_t)uErr;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001434 }
1435 if(Item.uLabelType != QCBOR_TYPE_NONE ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001436 UsefulBufCompareToSZ(Item.val.string, "bytes 2") ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001437 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1438 Item.uDataAlloc ||
1439 Item.uLabelAlloc) {
1440 return -12;
1441 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001442
Laurence Lundblade5db34da2024-05-30 03:14:35 -07001443 if((uErr = QCBORDecode_GetNext(&DCtx, &Item))) {
1444 return (int32_t)uErr;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001445 }
1446 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1447 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
1448 Item.uDataAlloc ||
1449 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001450 UsefulBufCompareToSZ(Item.val.string, "yyyy")) {
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001451 return -13;
1452 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001453
Laurence Lundblade5db34da2024-05-30 03:14:35 -07001454 if((uErr = QCBORDecode_GetNext(&DCtx, &Item))) {
1455 return (int32_t)uErr;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001456 }
1457 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1458 Item.uDataAlloc ||
1459 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001460 UsefulBufCompareToSZ(Item.val.string, "another int") ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001461 Item.uDataType != QCBOR_TYPE_TEXT_STRING) {
1462 return -14;
1463 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001464
Laurence Lundblade5db34da2024-05-30 03:14:35 -07001465 if((uErr = QCBORDecode_GetNext(&DCtx, &Item))) {
1466 return (int32_t)uErr;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001467 }
1468 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1469 Item.uDataAlloc ||
1470 Item.uLabelAlloc ||
1471 Item.uDataType != QCBOR_TYPE_INT64 ||
1472 Item.val.int64 != 98) {
1473 return -15;
1474 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001475
Laurence Lundblade5db34da2024-05-30 03:14:35 -07001476 if((uErr = QCBORDecode_GetNext(&DCtx, &Item))) {
1477 return (int32_t)uErr;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001478 }
1479 if(Item.uLabelType != QCBOR_TYPE_NONE ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001480 UsefulBufCompareToSZ(Item.val.string, "text 2") ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001481 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1482 Item.uDataAlloc ||
1483 Item.uLabelAlloc) {
1484 return -16;
1485 }
1486
Laurence Lundblade5db34da2024-05-30 03:14:35 -07001487 if((uErr = QCBORDecode_GetNext(&DCtx, &Item))) {
1488 return (int32_t)uErr;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001489 }
1490 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1491 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1492 Item.uDataAlloc ||
1493 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001494 UsefulBufCompareToSZ(Item.val.string, "lies, damn lies and statistics")) {
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001495 return -17;
1496 }
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07001497
1498
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001499 /*
1500 Test with map that nearly QCBOR_MAX_ITEMS_IN_ARRAY items in a
1501 map that when interpreted as an array will be too many. Test
1502 data just has the start of the map, not all the items in the map.
1503 */
1504 static const uint8_t pTooLargeMap[] = {0xb9, 0xff, 0xfd};
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07001505
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001506 QCBORDecode_Init(&DCtx,
1507 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pTooLargeMap),
1508 QCBOR_DECODE_MODE_MAP_AS_ARRAY);
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07001509
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001510 if((QCBOR_ERR_ARRAY_DECODE_TOO_LONG != QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001511 return -50;
1512 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001513
Laurence Lundblade5db34da2024-05-30 03:14:35 -07001514 /* This test requires indef strings, HW float and preferred float,... */
1515#if !defined(QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS) && \
1516 !defined(QCBOR_DISABLE_FLOAT_HW_USE) && \
1517 !defined(QCBOR_DISABLE_PREFERRED_FLOAT) && \
1518 !defined(QCBOR_DISABLE_TAGS) && \
1519 !defined(QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS)
1520
1521 UsefulBufC Encoded;
1522
1523 /* Big decode of a map with a wide variety or labels */
1524 QCBORDecode_Init(&DCtx,
1525 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pPerverseLabels),
1526 QCBOR_DECODE_MODE_MAP_AS_ARRAY);
1527 UsefulBuf_MAKE_STACK_UB(Pool, 100);
1528 QCBORDecode_SetMemPool(&DCtx, Pool, 0);
1529
1530 uErr = QCBORDecode_GetNext(&DCtx, &Item);
1531 if(uErr) {
1532 return MakeTestResultCode(10, 1, uErr);
1533 }
1534 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1535 Item.uDataType != QCBOR_TYPE_MAP_AS_ARRAY) {
1536 return MakeTestResultCode(10, 2, 0);
1537 }
1538
1539 uErr = QCBORDecode_GetNext(&DCtx, &Item);
1540 if(uErr) {
1541 return MakeTestResultCode(10, 3, uErr);
1542 }
1543 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1544 Item.uDataType != QCBOR_TYPE_TRUE) {
1545 return MakeTestResultCode(10, 4, 0);
1546 }
1547
1548 uErr = QCBORDecode_GetNext(&DCtx, &Item);
1549 if(uErr) {
1550 return MakeTestResultCode(10, 5, uErr);
1551 }
1552 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1553 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1554 ((const char *)Item.val.string.ptr)[0] != 'a') {
1555 return MakeTestResultCode(10, 6, 0);
1556 }
1557
1558 uErr = QCBORDecode_GetNext(&DCtx, &Item);
1559 if(uErr) {
1560 return MakeTestResultCode(10, 7, uErr);
1561 }
1562 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1563 Item.uDataType != QCBOR_TYPE_NULL) {
1564 return MakeTestResultCode(10, 8, 0);
1565 }
1566
1567 uErr = QCBORDecode_GetNext(&DCtx, &Item);
1568 if(uErr) {
1569 return MakeTestResultCode(10, 9, uErr);
1570 }
1571 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1572 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1573 ((const char *)Item.val.string.ptr)[0] != 'b') {
1574 return MakeTestResultCode(10, 10, 0);
1575 }
1576
1577 uErr = QCBORDecode_GetNext(&DCtx, &Item);
1578 if(uErr) {
1579 return MakeTestResultCode(10, 11, uErr);
1580 }
1581 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1582 Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE ||
1583 Item.val.int64 != 255) {
1584 return MakeTestResultCode(10, 12, 0);
1585 }
1586
1587 uErr = QCBORDecode_GetNext(&DCtx, &Item);
1588 if(uErr) {
1589 return MakeTestResultCode(10, 13, uErr);
1590 }
1591 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1592 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1593 ((const char *)Item.val.string.ptr)[0] != 'c') {
1594 return MakeTestResultCode(10, 14, 0);
1595 }
1596
1597 uErr = QCBORDecode_GetNext(&DCtx, &Item);
1598 if(uErr) {
1599 return MakeTestResultCode(10, 15, uErr);
1600 }
1601 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1602 Item.uDataType != QCBOR_TYPE_DOUBLE ||
1603 !isnan(Item.val.dfnum)) {
1604 return MakeTestResultCode(10, 16, 0);
1605 }
1606
1607 uErr = QCBORDecode_GetNext(&DCtx, &Item);
1608 if(uErr) {
1609 return MakeTestResultCode(10, 17, uErr);
1610 }
1611 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1612 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1613 ((const char *)Item.val.string.ptr)[0] != 'd') {
1614 return MakeTestResultCode(10, 18, 0);
1615 }
1616
1617
1618 uErr = QCBORDecode_GetNext(&DCtx, &Item);
1619 if(uErr) {
1620 return MakeTestResultCode(10, 19, uErr);
1621 }
1622 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1623 Item.uDataType != QCBOR_TYPE_DOUBLE ||
1624 Item.val.dfnum != 3.4028234663852886E+38) {
1625 return MakeTestResultCode(10, 20, 0);
1626 }
1627
1628 uErr = QCBORDecode_GetNext(&DCtx, &Item);
1629 if(uErr) {
1630 return MakeTestResultCode(10, 21, uErr);
1631 }
1632 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1633 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1634 ((const char *)Item.val.string.ptr)[0] != 'e') {
1635 return MakeTestResultCode(10, 22, 0);
1636 }
1637
1638 uErr = QCBORDecode_GetNext(&DCtx, &Item);
1639 if(uErr) {
1640 return MakeTestResultCode(10, 23, uErr);
1641 }
1642 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1643 Item.uDataType != QCBOR_TYPE_DOUBLE ||
1644 Item.val.dfnum != -INFINITY) {
1645 return MakeTestResultCode(10, 24, 0);
1646 }
1647
1648 uErr = QCBORDecode_GetNext(&DCtx, &Item);
1649 if(uErr) {
1650 return MakeTestResultCode(10, 25, uErr);
1651 }
1652 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1653 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1654 ((const char *)Item.val.string.ptr)[0] != 'f') {
1655 return MakeTestResultCode(10, 26, 0);
1656 }
1657
1658 uErr = QCBORDecode_GetNext(&DCtx, &Item);
1659 if(uErr) {
1660 return MakeTestResultCode(10, 26, uErr);
1661 }
1662 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1663 Item.uDataType != QCBOR_TYPE_MAP_AS_ARRAY ||
1664 Item.val.uCount != 2) {
1665 return MakeTestResultCode(10, 27, 0);
1666 }
1667
1668 uErr = QCBORDecode_GetNext(&DCtx, &Item);
1669 if(uErr) {
1670 return MakeTestResultCode(10, 28, uErr);
1671 }
1672 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1673 Item.uDataType != QCBOR_TYPE_INT64 ||
1674 Item.val.int64 != 1000) {
1675 return MakeTestResultCode(10, 29, 0);
1676 }
1677
1678 uErr = QCBORDecode_GetNext(&DCtx, &Item);
1679 if(uErr) {
1680 return MakeTestResultCode(10, 30, uErr);
1681 }
1682 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1683 Item.uDataType != QCBOR_TYPE_INT64 ||
1684 Item.val.int64 != 16) {
1685 return MakeTestResultCode(10, 31, 0);
1686 }
1687
1688 uErr = QCBORDecode_GetNext(&DCtx, &Item);
1689 if(uErr) {
1690 return MakeTestResultCode(10, 32, uErr);
1691 }
1692 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1693 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1694 ((const char *)Item.val.string.ptr)[0] != 'g') {
1695 return MakeTestResultCode(10, 33, 0);
1696 }
1697
1698 for(int i = 0 ; i < 4; i++) {
1699 uErr = QCBORDecode_GetNext(&DCtx, &Item);
1700 if(uErr) {
1701 return MakeTestResultCode(10, 34, uErr);
1702 }
1703 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1704 Item.uDataType != QCBOR_TYPE_ARRAY) {
1705 return MakeTestResultCode(10, 35, 0);
1706 }
1707 if(i != 3) {
1708 if(Item.val.uCount != 1) {
1709 return MakeTestResultCode(10, 35, 0);
1710 }
1711 }
1712 }
1713 uErr = QCBORDecode_GetNext(&DCtx, &Item);
1714 if(uErr) {
1715 return MakeTestResultCode(10, 36, uErr);
1716 }
1717 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1718 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1719 ((const char *)Item.val.string.ptr)[0] != 'h') {
1720 return MakeTestResultCode(10, 37, 0);
1721 }
1722
1723 uErr = QCBORDecode_GetNext(&DCtx, &Item);
1724 if(uErr) {
1725 return MakeTestResultCode(10, 38, uErr);
1726 }
1727 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1728 Item.uDataType != QCBOR_TYPE_DATE_EPOCH) {
1729 return MakeTestResultCode(10, 39, 0);
1730 }
1731
1732 uErr = QCBORDecode_GetNext(&DCtx, &Item);
1733 if(uErr) {
1734 return MakeTestResultCode(10, 40, uErr);
1735 }
1736 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1737 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1738 ((const char *)Item.val.string.ptr)[0] != 'i') {
1739 return MakeTestResultCode(10, 41, 0);
1740 }
1741
1742 uErr = QCBORDecode_GetNext(&DCtx, &Item);
1743 if(uErr) {
1744 return MakeTestResultCode(10, 42, uErr);
1745 }
1746 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1747 Item.uDataType != QCBOR_TYPE_ARRAY ||
1748 Item.val.uCount != 2) {
1749 return MakeTestResultCode(10, 31, 0);
1750 }
1751
1752 uErr = QCBORDecode_GetNext(&DCtx, &Item);
1753 if(uErr) {
1754 return MakeTestResultCode(10, 43, uErr);
1755 }
1756 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1757 Item.uDataType != QCBOR_TYPE_INT64) {
1758 return MakeTestResultCode(10, 31, 0);
1759 }
1760
1761 uErr = QCBORDecode_GetNext(&DCtx, &Item);
1762 if(uErr) {
1763 return MakeTestResultCode(10, 44, uErr);
1764 }
1765 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1766 Item.uDataType != QCBOR_TYPE_MAP_AS_ARRAY ||
1767 Item.val.uCount != 4) {
1768 return MakeTestResultCode(10, 45, 0);
1769 }
1770
1771 for(int i = 0 ; i < 4; i++) {
1772 uErr = QCBORDecode_GetNext(&DCtx, &Item);
1773 if(uErr) {
1774 return MakeTestResultCode(10, 46, uErr);
1775 }
1776 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1777 Item.uDataType != QCBOR_TYPE_INT64) {
1778 return MakeTestResultCode(10, 47, 0);
1779 }
1780 }
1781 uErr = QCBORDecode_GetNext(&DCtx, &Item);
1782 if(uErr) {
1783 return MakeTestResultCode(10, 48, uErr);
1784 }
1785 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1786 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1787 ((const char *)Item.val.string.ptr)[0] != 'j') {
1788 return MakeTestResultCode(10, 49, 0);
1789 }
1790
1791 uErr = QCBORDecode_GetNext(&DCtx, &Item);
1792 if(uErr) {
1793 return MakeTestResultCode(10, 50, uErr);
1794 }
1795 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1796 Item.uDataType != QCBOR_TYPE_MAP_AS_ARRAY ||
1797 Item.val.uCount != UINT16_MAX) {
1798 return MakeTestResultCode(10, 51, 0);
1799 }
1800
1801 uErr = QCBORDecode_GetNext(&DCtx, &Item);
1802 if(uErr) {
1803 return MakeTestResultCode(10, 52, uErr);
1804 }
1805 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1806 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1807 ((const char *)Item.val.string.ptr)[0] != 'k') {
1808 return MakeTestResultCode(10, 53, 0);
1809 }
1810
1811 uErr = QCBORDecode_GetNext(&DCtx, &Item);
1812 if(uErr) {
1813 return MakeTestResultCode(10, 54, uErr);
1814 }
1815 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1816 Item.uDataType != QCBOR_TYPE_ARRAY ||
1817 Item.val.uCount != UINT16_MAX) {
1818 return MakeTestResultCode(10, 55, 0);
1819 }
1820
1821 for(int i = 0 ; i < 3; i++) {
1822 uErr = QCBORDecode_GetNext(&DCtx, &Item);
1823 if(uErr) {
1824 return MakeTestResultCode(10, 56, uErr);
1825 }
1826 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1827 Item.uDataType != QCBOR_TYPE_INT64) {
1828 return MakeTestResultCode(10, 57, 0);
1829 }
1830 }
1831 uErr = QCBORDecode_GetNext(&DCtx, &Item);
1832 if(uErr) {
1833 return MakeTestResultCode(10, 58, uErr);
1834 }
1835 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1836 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1837 ((const char *)Item.val.string.ptr)[0] != 'l') {
1838 return MakeTestResultCode(10, 59, 0);
1839 }
1840
1841 uErr = QCBORDecode_GetNext(&DCtx, &Item);
1842 if(uErr) {
1843 return MakeTestResultCode(10, 60, uErr);
1844 }
1845 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1846 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1847 Item.val.string.len != 4) {
1848 return MakeTestResultCode(10, 61, 0);
1849 }
1850
1851 uErr = QCBORDecode_GetNext(&DCtx, &Item);
1852 if(uErr) {
1853 return MakeTestResultCode(10, 62, uErr);
1854 }
1855 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1856 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1857 ((const char *)Item.val.string.ptr)[0] != 'm') {
1858 return MakeTestResultCode(10, 63, 0);
1859 }
1860
1861 uErr = QCBORDecode_GetNext(&DCtx, &Item);
1862 if(uErr) {
1863 return MakeTestResultCode(10, 64, uErr);
1864 }
1865 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1866 Item.uDataType != QCBOR_TYPE_MAP_AS_ARRAY ||
1867 !QCBORDecode_IsTagged(&DCtx, &Item, 258) ||
1868 Item.val.uCount != UINT16_MAX) {
1869 return MakeTestResultCode(10, 65, 0);
1870 }
1871
1872 uErr = QCBORDecode_GetNext(&DCtx, &Item);
1873 if(uErr) {
1874 return MakeTestResultCode(10, 66, uErr);
1875 }
1876 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1877 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1878 Item.val.string.len != 2) {
1879 return MakeTestResultCode(10, 67, 0);
1880 }
1881
1882 uErr = QCBORDecode_GetNext(&DCtx, &Item);
1883 if(uErr) {
1884 return MakeTestResultCode(10, 68, uErr);
1885 }
1886 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1887 Item.uDataType != QCBOR_TYPE_INT64 ||
1888 Item.val.int64 != 0) {
1889 return MakeTestResultCode(10, 69, 0);
1890 }
1891
1892 uErr = QCBORDecode_GetNext(&DCtx, &Item);
1893 if(uErr) {
1894 return MakeTestResultCode(10, 70, uErr);
1895 }
1896 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1897 Item.uDataType != QCBOR_TYPE_FALSE) {
1898 return MakeTestResultCode(10, 71, 0);
1899 }
1900
1901 uErr = QCBORDecode_GetNext(&DCtx, &Item);
1902 if(uErr) {
1903 return MakeTestResultCode(10, 72, uErr);
1904 }
1905 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1906 Item.uDataType != QCBOR_TYPE_ARRAY ||
1907 !QCBORDecode_IsTagged(&DCtx, &Item, 23) ||
1908 Item.val.uCount != 0) {
1909 return MakeTestResultCode(10, 73, 0);
1910 }
1911
1912 uErr = QCBORDecode_GetNext(&DCtx, &Item);
1913 if(uErr) {
1914 return MakeTestResultCode(10, 74, uErr);
1915 }
1916 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1917 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1918 ((const char *)Item.val.string.ptr)[0] != 'n') {
1919 return MakeTestResultCode(10, 75, 0);
1920 }
1921
1922
1923 /* Big decode of a map with a wide variety or labels */
1924 QCBORDecode_Init(&DCtx,
1925 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pPerverseLabels),
1926 QCBOR_DECODE_MODE_MAP_AS_ARRAY);
1927 QCBORDecode_SetMemPool(&DCtx, Pool, 0);
1928
1929 QCBORDecode_EnterArray(&DCtx, &Item);
1930 bool b;
1931 QCBORDecode_GetBool(&DCtx, &b);
1932
1933 uErr = CheckOneLetterString(&DCtx, 'a');
1934 if(uErr) {
1935 return MakeTestResultCode(11, 1, uErr);
1936 }
1937
1938 QCBORDecode_GetNull(&DCtx);
1939 uErr = CheckOneLetterString(&DCtx, 'b');
1940 if(uErr) {
1941 return MakeTestResultCode(11, 2, uErr);
1942 }
1943
1944 QCBORDecode_VGetNext(&DCtx, &Item);
1945 uErr = CheckOneLetterString(&DCtx, 'c');
1946 if(uErr) {
1947 return MakeTestResultCode(11, 3, uErr);
1948 }
1949
1950 double dNum;
1951 QCBORDecode_GetDouble(&DCtx, &dNum);
1952 if(!isnan(dNum)) {
1953 return MakeTestResultCode(11, 4, 0);
1954 }
1955 uErr = CheckOneLetterString(&DCtx, 'd');
1956 if(uErr) {
1957 return MakeTestResultCode(11, 5, uErr);
1958 }
1959
1960 QCBORDecode_GetDouble(&DCtx, &dNum);
1961 if( dNum != 3.4028234663852886E+38 ) {
1962 return MakeTestResultCode(11, 6, 0);
1963 }
1964 uErr = CheckOneLetterString(&DCtx, 'e');
1965 if(uErr) {
1966 return MakeTestResultCode(11, 7, uErr);
1967 }
1968
1969 QCBORDecode_GetDouble(&DCtx, &dNum);
1970 if(dNum != -INFINITY) {
1971 return MakeTestResultCode(11, 8, 0);
1972 }
1973 uErr = CheckOneLetterString(&DCtx, 'f');
1974 if(uErr) {
1975 return MakeTestResultCode(11, 9, uErr);
1976 }
1977
1978 int64_t nInt;
1979 QCBORDecode_EnterArray(&DCtx, &Item);
1980 QCBORDecode_GetInt64(&DCtx, &nInt);
1981 QCBORDecode_GetInt64(&DCtx, &nInt);
1982 QCBORDecode_ExitArray(&DCtx);
1983 uErr = CheckOneLetterString(&DCtx, 'g');
1984 if(uErr) {
1985 return MakeTestResultCode(11, 10, uErr);
1986 }
1987
1988 QCBORDecode_EnterArray(&DCtx, &Item);
1989 QCBORDecode_EnterArray(&DCtx, &Item);
1990 QCBORDecode_EnterArray(&DCtx, &Item);
1991 QCBORDecode_EnterArray(&DCtx, &Item);
1992 QCBORDecode_ExitArray(&DCtx);
1993 QCBORDecode_ExitArray(&DCtx);
1994 QCBORDecode_ExitArray(&DCtx);
1995 QCBORDecode_ExitArray(&DCtx);
1996 uErr = CheckOneLetterString(&DCtx, 'h');
1997 if(uErr) {
1998 return MakeTestResultCode(11, 11, uErr);
1999 }
2000 QCBORDecode_GetEpochDate(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &nInt);
2001 uErr = CheckOneLetterString(&DCtx, 'i');
2002 if(uErr) {
2003 return MakeTestResultCode(11, 12, uErr);
2004 }
2005
2006 QCBORDecode_EnterArray(&DCtx, &Item);
2007 QCBORDecode_GetInt64(&DCtx, &nInt);
2008 QCBORDecode_EnterArray(&DCtx, &Item);
2009 QCBORDecode_GetInt64(&DCtx, &nInt);
2010 QCBORDecode_GetInt64(&DCtx, &nInt);
2011 QCBORDecode_GetInt64(&DCtx, &nInt);
2012 QCBORDecode_GetInt64(&DCtx, &nInt);
2013 QCBORDecode_ExitArray(&DCtx);
2014 QCBORDecode_ExitArray(&DCtx);
2015 uErr = CheckOneLetterString(&DCtx, 'j');
2016 if(uErr) {
2017 return MakeTestResultCode(11, 13, uErr);
2018 }
2019
2020 QCBORDecode_GetArray(&DCtx, &Item, &Encoded);
2021 uErr = CheckOneLetterString(&DCtx, 'k');
2022 if(uErr) {
2023 return MakeTestResultCode(11, 14, uErr);
2024 }
2025
2026 QCBORDecode_EnterArray(&DCtx, &Item);
2027 QCBORDecode_GetInt64(&DCtx, &nInt);
2028 QCBORDecode_GetInt64(&DCtx, &nInt);
2029 QCBORDecode_GetInt64(&DCtx, &nInt);
2030 QCBORDecode_ExitArray(&DCtx);
2031 uErr = CheckOneLetterString(&DCtx, 'l');
2032 if(uErr) {
2033 return MakeTestResultCode(11, 15, uErr);
2034 }
2035
2036 QCBORDecode_GetTextString(&DCtx, &Encoded);
2037 uErr = CheckOneLetterString(&DCtx, 'm');
2038 if(uErr) {
2039 return MakeTestResultCode(11, 16, uErr);
2040 }
2041
2042 QCBORDecode_EnterArray(&DCtx, &Item);
2043 if(!QCBORDecode_IsTagged(&DCtx, &Item, 258)) {
2044 return MakeTestResultCode(11, 17, 0);
2045 }
2046 if(Item.uDataType != QCBOR_TYPE_MAP_AS_ARRAY) {
2047 return MakeTestResultCode(11, 18, 0);
2048 }
2049 if(Item.val.uCount != UINT16_MAX) {
2050 return MakeTestResultCode(11, 19, 0);
2051 }
2052 QCBORDecode_GetTextString(&DCtx, &Encoded);
2053 if(Encoded.len != 2) {
2054 return MakeTestResultCode(11, 20, 0);
2055 } QCBORDecode_GetInt64(&DCtx, &nInt);
2056 QCBORDecode_GetBool(&DCtx, &b);
2057 if(b != false) {
2058 return MakeTestResultCode(11, 21, 0);
2059 }
2060 QCBORDecode_EnterArray(&DCtx, &Item);
2061 if(!QCBORDecode_IsTagged(&DCtx, &Item, 23)) {
2062 return MakeTestResultCode(11, 22, 0);
2063 }
2064 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
2065 return MakeTestResultCode(11, 23, 0);
2066 }
2067 if(Item.val.uCount != 0) {
2068 return MakeTestResultCode(11, 24, 0);
2069 }
2070 QCBORDecode_ExitArray(&DCtx);
2071 QCBORDecode_ExitArray(&DCtx);
2072 uErr = CheckOneLetterString(&DCtx, 'n');
2073 if(uErr) {
2074 return MakeTestResultCode(11, 25, uErr);
2075 }
2076
2077 QCBORDecode_ExitArray(&DCtx);
2078 uErr = QCBORDecode_Finish(&DCtx);
2079 if(uErr) {
2080 return MakeTestResultCode(11, 26, uErr);
2081 }
2082#endif /* QCBOR_DISABLE_... */
Laurence Lundbladed4cc1032020-10-12 04:19:47 -07002083
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09002084 return 0;
2085}
2086
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002087
Laurence Lundblade5db34da2024-05-30 03:14:35 -07002088
Laurence Lundbladeec290b82024-06-10 11:10:54 -07002089#ifndef QCBOR_DISABLE_NON_INTEGER_LABELS
Laurence Lundblade5db34da2024-05-30 03:14:35 -07002090
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002091/*
Laurence Lundbladefab1b522018-10-19 13:40:52 +05302092 Fully or partially decode pValidMapEncoded. When
2093 partially decoding check for the right error code.
2094 How much partial decoding depends on nLevel.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002095
Laurence Lundbladefab1b522018-10-19 13:40:52 +05302096 The partial decodes test error conditions of
2097 incomplete encoded input.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002098
Laurence Lundbladefab1b522018-10-19 13:40:52 +05302099 This could be combined with the above test
2100 and made prettier and maybe a little more
2101 thorough.
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002102 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002103static int32_t ExtraBytesTest(int nLevel)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002104{
2105 QCBORDecodeContext DCtx;
2106 QCBORItem Item;
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07002107 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002108
Laurence Lundbladeee851742020-01-08 08:37:05 -08002109 QCBORDecode_Init(&DCtx,
2110 (UsefulBufC){pValidMapEncoded, sizeof(pValidMapEncoded)},
2111 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002112
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08002113 if(nLevel < 1) {
2114 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_EXTRA_BYTES) {
2115 return -1;
2116 } else {
2117 return 0;
2118 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002119 }
Laurence Lundbladefab1b522018-10-19 13:40:52 +05302120
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002121
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09002122 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07002123 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09002124 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002125 if(Item.uDataType != QCBOR_TYPE_MAP ||
2126 Item.val.uCount != 3)
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002127 return -2;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002128
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08002129 if(nLevel < 2) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002130 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
2131 return -3;
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08002132 } else {
2133 return 0;
2134 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002135 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002136
2137
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09002138 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07002139 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09002140 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002141 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002142 Item.uDataType != QCBOR_TYPE_INT64 ||
2143 Item.val.uCount != 42 ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07002144 UsefulBufCompareToSZ(Item.label.string, "first integer")) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002145 return -4;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09002146 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002147
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08002148 if(nLevel < 3) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002149 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
2150 return -5;
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08002151 } else {
2152 return 0;
2153 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002154 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002155
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09002156 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07002157 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09002158 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002159 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07002160 UsefulBufCompareToSZ(Item.label.string, "an array of two strings") ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002161 Item.uDataType != QCBOR_TYPE_ARRAY ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09002162 Item.val.uCount != 2) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002163 return -6;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09002164 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002165
2166
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08002167 if(nLevel < 4) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002168 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
2169 return -7;
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08002170 } else {
2171 return 0;
2172 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002173 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002174
2175
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09002176 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07002177 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09002178 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002179 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07002180 UsefulBufCompareToSZ(Item.val.string, "string1")) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002181 return -8;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09002182 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002183
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08002184 if(nLevel < 5) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002185 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
2186 return -9;
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08002187 } else {
2188 return 0;
2189 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002190 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002191
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09002192 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07002193 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09002194 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002195 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07002196 UsefulBufCompareToSZ(Item.val.string, "string2")) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002197 return -10;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09002198 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002199
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08002200 if(nLevel < 6) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002201 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
2202 return -11;
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08002203 } else {
2204 return 0;
2205 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002206 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002207
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09002208 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07002209 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09002210 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002211 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07002212 UsefulBufCompareToSZ(Item.label.string, "map in a map") ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002213 Item.uDataType != QCBOR_TYPE_MAP ||
2214 Item.val.uCount != 4)
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002215 return -12;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002216
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08002217 if(nLevel < 7) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002218 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
2219 return -13;
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08002220 } else {
2221 return 0;
2222 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002223 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002224
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09002225 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07002226 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09002227 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002228 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07002229 UsefulBufCompareToSZ(Item.label.string, "bytes 1") ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002230 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07002231 UsefulBufCompareToSZ(Item.val.string, "xxxx")) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002232 return -14;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09002233 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002234
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08002235 if(nLevel < 8) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002236 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
2237 return -15;
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08002238 } else {
2239 return 0;
2240 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002241 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002242
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09002243 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07002244 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09002245 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002246 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07002247 UsefulBufCompareToSZ(Item.label.string, "bytes 2") ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002248 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07002249 UsefulBufCompareToSZ(Item.val.string, "yyyy")) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002250 return -16;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09002251 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002252
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08002253 if(nLevel < 9) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002254 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
2255 return -17;
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08002256 } else {
2257 return 0;
2258 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002259 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002260
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09002261 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07002262 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09002263 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002264 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07002265 UsefulBufCompareToSZ(Item.label.string, "another int") ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002266 Item.uDataType != QCBOR_TYPE_INT64 ||
2267 Item.val.int64 != 98)
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002268 return -18;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002269
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08002270 if(nLevel < 10) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002271 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
2272 return -19;
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08002273 } else {
2274 return 0;
2275 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002276 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002277
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09002278 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07002279 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09002280 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002281 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09002282 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("text 2"))||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002283 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07002284 UsefulBufCompareToSZ(Item.val.string, "lies, damn lies and statistics")) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002285 return -20;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09002286 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002287
Laurence Lundbladefab1b522018-10-19 13:40:52 +05302288 if(QCBORDecode_Finish(&DCtx)) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002289 return -21;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002290 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002291
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002292 return 0;
2293}
2294
2295
2296
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03002297int32_t ParseMapTest(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002298{
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002299 // Parse a moderatly complex map structure very thoroughly
2300 int32_t nResult = ParseMapTest1(QCBOR_DECODE_MODE_NORMAL);
2301 if(nResult) {
2302 return nResult;
2303 }
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08002304
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002305 // Again, but in strings-only mode. It should succeed since the input
2306 // map has only string labels.
2307 nResult = ParseMapTest1(QCBOR_DECODE_MODE_MAP_STRINGS_ONLY);
2308 if(nResult) {
2309 return nResult;
2310 }
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08002311
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002312 // Again, but try to finish the decoding before the end of the
2313 // input at 10 different place and see that the right error code
2314 // is returned.
2315 for(int i = 0; i < 10; i++) {
2316 nResult = ExtraBytesTest(i);
2317 if(nResult) {
2318 break;
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08002319 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002320 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002321
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002322 return nResult;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002323}
Laurence Lundbladeec290b82024-06-10 11:10:54 -07002324#endif /* ! QCBOR_DISABLE_NON_INTEGER_LABELS */
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002325
2326
Laurence Lundbladecc7da412020-12-27 00:09:07 -08002327/* The simple-values including some not well formed */
2328static const uint8_t spSimpleValues[] = {
2329 0x8a, 0xf4, 0xf5, 0xf6, 0xf7, 0xff, 0xe0, 0xf3,
2330 0xf8, 0x00, 0xf8, 0x13, 0xf8, 0x1f, 0xf8, 0x20,
2331 0xf8, 0xff};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002332
Laurence Lundblade3888f002024-06-12 21:20:56 -07002333/* A map of good simple values, plus one well-formed integer */
2334static const uint8_t spGoodSimpleValues[] = {
2335 0xa9, 0x01, 0xf4, 0x02, 0xf5, 0x03, 0xf6, 0x04, 0xf7,
2336 0x05, 0xe0, 0x06, 0xf3, 0x07, 0xf8, 0x20, 0x61, 0x40,
2337 0xf8, 0xff, 0x0f, 0x0f};
2338
2339int32_t SimpleValueDecodeTests(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002340{
2341 QCBORDecodeContext DCtx;
Laurence Lundblade3888f002024-06-12 21:20:56 -07002342 QCBORItem Item;
2343 QCBORError uErr;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002344
Laurence Lundbladeee851742020-01-08 08:37:05 -08002345 QCBORDecode_Init(&DCtx,
2346 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSimpleValues),
2347 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002348
2349
Laurence Lundblade3888f002024-06-12 21:20:56 -07002350 if((uErr = QCBORDecode_GetNext(&DCtx, &Item)))
2351 return (int32_t)uErr;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002352 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
2353 Item.val.uCount != 10)
Laurence Lundblade3888f002024-06-12 21:20:56 -07002354 return 1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002355
Laurence Lundblade3888f002024-06-12 21:20:56 -07002356 if((uErr = QCBORDecode_GetNext(&DCtx, &Item)))
2357 return (int32_t)uErr;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002358 if(Item.uDataType != QCBOR_TYPE_FALSE)
Laurence Lundblade3888f002024-06-12 21:20:56 -07002359 return 2;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002360
Laurence Lundblade3888f002024-06-12 21:20:56 -07002361 if((uErr = QCBORDecode_GetNext(&DCtx, &Item)))
2362 return (int32_t)uErr;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002363 if(Item.uDataType != QCBOR_TYPE_TRUE)
Laurence Lundblade3888f002024-06-12 21:20:56 -07002364 return 3;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002365
Laurence Lundblade3888f002024-06-12 21:20:56 -07002366 if((uErr = QCBORDecode_GetNext(&DCtx, &Item)))
2367 return (int32_t)uErr;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002368 if(Item.uDataType != QCBOR_TYPE_NULL)
Laurence Lundblade3888f002024-06-12 21:20:56 -07002369 return 4;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002370
Laurence Lundblade3888f002024-06-12 21:20:56 -07002371 if((uErr = QCBORDecode_GetNext(&DCtx, &Item)))
2372 return (int32_t)uErr;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002373 if(Item.uDataType != QCBOR_TYPE_UNDEF)
Laurence Lundblade3888f002024-06-12 21:20:56 -07002374 return 5;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002375
2376 // A break
Laurence Lundblade9e3651c2018-10-10 11:49:55 +08002377 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_BREAK)
Laurence Lundblade3888f002024-06-12 21:20:56 -07002378 return 6;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002379
Laurence Lundblade3888f002024-06-12 21:20:56 -07002380 if((uErr = QCBORDecode_GetNext(&DCtx, &Item)))
2381 return (int32_t)uErr;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002382 if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 0)
Laurence Lundblade3888f002024-06-12 21:20:56 -07002383 return 7;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002384
Laurence Lundblade3888f002024-06-12 21:20:56 -07002385 if((uErr = QCBORDecode_GetNext(&DCtx, &Item)))
2386 return (int32_t)uErr;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002387 if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 19)
Laurence Lundblade3888f002024-06-12 21:20:56 -07002388 return 8;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002389
Laurence Lundblade077475f2019-04-26 09:06:33 -07002390 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_TYPE_7)
Laurence Lundblade3888f002024-06-12 21:20:56 -07002391 return 9;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002392
Laurence Lundblade077475f2019-04-26 09:06:33 -07002393 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_TYPE_7)
Laurence Lundblade3888f002024-06-12 21:20:56 -07002394 return 10;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002395
Laurence Lundblade077475f2019-04-26 09:06:33 -07002396 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_TYPE_7)
Laurence Lundblade3888f002024-06-12 21:20:56 -07002397 return 11;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002398
Laurence Lundblade3888f002024-06-12 21:20:56 -07002399 if((uErr = QCBORDecode_GetNext(&DCtx, &Item)))
2400 return (int32_t)uErr;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002401 if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 32)
Laurence Lundblade3888f002024-06-12 21:20:56 -07002402 return 12;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002403
Laurence Lundblade3888f002024-06-12 21:20:56 -07002404 if((uErr = QCBORDecode_GetNext(&DCtx, &Item)))
2405 return (int32_t)uErr;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002406 if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 255)
Laurence Lundblade3888f002024-06-12 21:20:56 -07002407 return 13;
2408
2409
2410 QCBORDecode_Init(&DCtx,
2411 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spGoodSimpleValues),
2412 QCBOR_DECODE_MODE_NORMAL);
2413
2414 uint8_t uSimple;
2415
2416 QCBORDecode_EnterMap(&DCtx, &Item);
2417 QCBORDecode_GetSimple(&DCtx, &uSimple);
2418 if(QCBORDecode_GetError(&DCtx) || uSimple != CBOR_SIMPLEV_FALSE) {
2419 return 20;
2420 }
2421 QCBORDecode_GetSimple(&DCtx, &uSimple);
2422 if(QCBORDecode_GetError(&DCtx) || uSimple != CBOR_SIMPLEV_TRUE) {
2423 return 21;
2424 }
2425 QCBORDecode_GetSimple(&DCtx, &uSimple);
2426 if(QCBORDecode_GetError(&DCtx) || uSimple != CBOR_SIMPLEV_NULL) {
2427 return 22;
2428 }
2429 QCBORDecode_GetSimple(&DCtx, &uSimple);
2430 if(QCBORDecode_GetError(&DCtx) || uSimple != CBOR_SIMPLEV_UNDEF) {
2431 return 23;
2432 }
2433 QCBORDecode_GetSimple(&DCtx, &uSimple);
2434 if(QCBORDecode_GetError(&DCtx) || uSimple != 0) {
2435 return 24;
2436 }
2437 QCBORDecode_GetSimple(&DCtx, &uSimple);
2438 if(QCBORDecode_GetError(&DCtx) || uSimple != 19) {
2439 return 25;
2440 }
2441 QCBORDecode_GetSimple(&DCtx, &uSimple);
2442 if(QCBORDecode_GetError(&DCtx) || uSimple != 32) {
2443 return 26;
2444 }
2445 QCBORDecode_GetSimple(&DCtx, &uSimple);
2446 if(QCBORDecode_GetError(&DCtx) || uSimple != 255) {
2447 return 27;
2448 }
2449 QCBORDecode_VGetNext(&DCtx, &Item);
2450 QCBORDecode_GetSimple(&DCtx, &uSimple);
2451 if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_NO_MORE_ITEMS) {
2452 return 28;
2453 }
2454
2455 QCBORDecode_Rewind(&DCtx);
2456
2457 QCBORDecode_GetSimpleInMapN(&DCtx, 6, &uSimple);
2458 if(QCBORDecode_GetError(&DCtx) || uSimple != 19) {
2459 return 30;
2460 }
2461
2462 QCBORDecode_GetSimpleInMapSZ(&DCtx, "@", &uSimple);
2463 if(QCBORDecode_GetError(&DCtx) || uSimple != 255) {
2464 return 31;
2465 }
2466
2467 QCBORDecode_GetSimpleInMapN(&DCtx, 99, &uSimple);
2468 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_LABEL_NOT_FOUND) {
2469 return 32;
2470 }
2471
2472 QCBORDecode_GetSimpleInMapSZ(&DCtx, "xx", &uSimple);
2473 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_LABEL_NOT_FOUND) {
2474 return 33;
2475 }
2476
2477 QCBORDecode_GetSimpleInMapN(&DCtx, 15, &uSimple);
2478 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
2479 return 34;
2480 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002481
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002482 return 0;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002483}
2484
2485
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03002486int32_t NotWellFormedTests(void)
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002487{
2488 // Loop over all the not-well-formed instance of CBOR
2489 // that are test vectors in not_well_formed_cbor.h
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08002490 const uint16_t nArraySize = C_ARRAY_COUNT(paNotWellFormedCBOR,
2491 struct someBinaryBytes);
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002492 for(uint16_t nIterate = 0; nIterate < nArraySize; nIterate++) {
2493 const struct someBinaryBytes *pBytes = &paNotWellFormedCBOR[nIterate];
2494 const UsefulBufC Input = (UsefulBufC){pBytes->p, pBytes->n};
2495
Laurence Lundbladeee851742020-01-08 08:37:05 -08002496 // Set up decoder context. String allocator needed for indefinite
2497 // string test cases
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002498 QCBORDecodeContext DCtx;
2499 QCBORDecode_Init(&DCtx, Input, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08002500#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002501 UsefulBuf_MAKE_STACK_UB(Pool, 100);
2502 QCBORDecode_SetMemPool(&DCtx, Pool, 0);
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08002503#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002504
2505 // Loop getting items until no more to get
Laurence Lundbladef71e1622020-08-06 18:52:13 -07002506 QCBORError uCBORError;
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002507 do {
2508 QCBORItem Item;
2509
Laurence Lundbladef71e1622020-08-06 18:52:13 -07002510 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
2511 } while(uCBORError == QCBOR_SUCCESS);
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002512
2513 // Every test vector must fail with
2514 // a not-well-formed error. If not
2515 // this test fails.
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002516 if(!QCBORDecode_IsNotWellFormedError(uCBORError) &&
Laurence Lundbladef71e1622020-08-06 18:52:13 -07002517 uCBORError != QCBOR_ERR_NO_MORE_ITEMS) {
Laurence Lundblade4bf6e712020-12-10 11:53:21 -08002518 /* Return index of failure and QCBOR error in the result */
2519 return (int32_t)(nIterate * 100 + uCBORError);
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002520 }
2521 }
2522 return 0;
2523}
2524
2525
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002526struct DecodeFailTestInput {
2527 const char *szDescription; /* Description of the test */
2528 QCBORDecodeMode DecoderMode; /* The QCBOR Decoder Mode for test */
2529 UsefulBufC Input; /* Chunk of CBOR that cases error */
2530 QCBORError nError; /* The expected error */
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002531};
2532
Laurence Lundblade59289e52019-12-30 13:44:37 -08002533
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002534static int32_t
2535ProcessDecodeFailures(const struct DecodeFailTestInput *pFailInputs, const int nNumFails)
Laurence Lundblade59289e52019-12-30 13:44:37 -08002536{
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002537 int nIndex;
2538 QCBORDecodeContext DCtx;
2539 QCBORError uCBORError;
2540 QCBORItem Item;
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08002541
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002542 for(nIndex = 0; nIndex < nNumFails; nIndex++) {
2543 const struct DecodeFailTestInput *pF = &pFailInputs[nIndex];
2544
2545 QCBORDecode_Init(&DCtx, pF->Input, pF->DecoderMode);
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08002546
2547#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002548 /* Set up the decoding context including a memory pool so that
2549 * indefinite length items can be checked.
2550 */
Laurence Lundblade59289e52019-12-30 13:44:37 -08002551 UsefulBuf_MAKE_STACK_UB(Pool, 100);
Laurence Lundblade830fbf92020-05-31 17:22:33 -07002552
Laurence Lundblade4bf6e712020-12-10 11:53:21 -08002553 uCBORError = QCBORDecode_SetMemPool(&DCtx, Pool, 0);
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002554 if(uCBORError != QCBOR_SUCCESS) {
2555 return -1;
Laurence Lundblade59289e52019-12-30 13:44:37 -08002556 }
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08002557#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
2558
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002559 if(nIndex == 8) {
2560 uCBORError = 9; /* For setting break points */
Laurence Lundblade37286c02022-09-03 10:05:02 -07002561 }
2562
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002563 /* Iterate until there is an error of some sort of error */
Laurence Lundblade59289e52019-12-30 13:44:37 -08002564 do {
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002565 /* Set to something none-zero, something other than QCBOR_TYPE_NONE */
Laurence Lundblade59289e52019-12-30 13:44:37 -08002566 memset(&Item, 0x33, sizeof(Item));
2567
Laurence Lundblade4bf6e712020-12-10 11:53:21 -08002568 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
2569 } while(uCBORError == QCBOR_SUCCESS);
2570
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002571 /* Must get the expected error or the this test fails
2572 * The data and label type must also be QCBOR_TYPE_NONE.
2573 */
Laurence Lundblade4bf6e712020-12-10 11:53:21 -08002574 if(uCBORError != pF->nError ||
Laurence Lundblade59289e52019-12-30 13:44:37 -08002575 Item.uDataType != QCBOR_TYPE_NONE ||
2576 Item.uLabelType != QCBOR_TYPE_NONE) {
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002577 return (int32_t)(nIndex * 1000 + (int)uCBORError);
Laurence Lundblade59289e52019-12-30 13:44:37 -08002578 }
2579 }
2580
2581 return 0;
2582}
2583
2584
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002585static const struct DecodeFailTestInput Failures[] = {
2586 /* Most of this is copied from not_well_formed.h. Here the error
2587 * code returned is also checked.
2588 */
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002589
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08002590#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002591 /* Indefinite length strings must be closed off */
2592 { "An indefinite length byte string not closed off",
2593 QCBOR_DECODE_MODE_NORMAL,
2594 {"0x5f\x41\x00", 3},
2595 QCBOR_ERR_HIT_END
2596 },
2597 { "An indefinite length text string not closed off",
2598 QCBOR_DECODE_MODE_NORMAL,
2599 {"\x7f\x61\x00", 3},
2600 QCBOR_ERR_HIT_END
2601 },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002602
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002603 /* All the chunks in an indefinite length string must be of the
2604 * type of indefinite length string
2605 */
2606 { "Indefinite length byte string with text string chunk",
2607 QCBOR_DECODE_MODE_NORMAL,
2608 {"\x5f\x61\x00\xff", 4},
2609 QCBOR_ERR_INDEFINITE_STRING_CHUNK
2610 },
2611 { "Indefinite length text string with a byte string chunk",
2612 QCBOR_DECODE_MODE_NORMAL,
2613 {"\x7f\x41\x00\xff", 4},
2614 QCBOR_ERR_INDEFINITE_STRING_CHUNK
2615 },
2616 { "Indefinite length byte string with a positive integer chunk",
2617 QCBOR_DECODE_MODE_NORMAL,
2618 {"\x5f\x00\xff", 3},
2619 QCBOR_ERR_INDEFINITE_STRING_CHUNK
2620 },
2621 { "Indefinite length byte string with an negative integer chunk",
2622 QCBOR_DECODE_MODE_NORMAL,
2623 {"\x5f\x21\xff", 3},
2624 QCBOR_ERR_INDEFINITE_STRING_CHUNK
2625 },
2626 { "Indefinite length byte string with an array chunk",
2627 QCBOR_DECODE_MODE_NORMAL,
2628 {"\x5f\x80\xff", 3},
2629 QCBOR_ERR_INDEFINITE_STRING_CHUNK
2630 },
2631 { "Indefinite length byte string with an map chunk",
2632 QCBOR_DECODE_MODE_NORMAL,
2633 {"\x5f\xa0\xff", 3},
2634 QCBOR_ERR_INDEFINITE_STRING_CHUNK
2635 },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002636
Laurence Lundblade37286c02022-09-03 10:05:02 -07002637#ifndef QCBOR_DISABLE_TAGS
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002638 { "Indefinite length byte string with tagged integer chunk",
2639 QCBOR_DECODE_MODE_NORMAL,
2640 {"\x5f\xc0\x00\xff", 4},
2641 QCBOR_ERR_INDEFINITE_STRING_CHUNK
2642 },
Laurence Lundblade37286c02022-09-03 10:05:02 -07002643#else
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002644 { "Indefinite length byte string with tagged integer chunk",
2645 QCBOR_DECODE_MODE_NORMAL,
2646 {"\x5f\xc0\x00\xff", 4},
2647 QCBOR_ERR_TAGS_DISABLED
2648 },
Laurence Lundblade37286c02022-09-03 10:05:02 -07002649#endif /* QCBOR_DISABLE_TAGS */
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002650
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002651 { "Indefinite length byte string with an simple type chunk",
2652 QCBOR_DECODE_MODE_NORMAL,
2653 {"\x5f\xe0\xff", 3},
2654 QCBOR_ERR_INDEFINITE_STRING_CHUNK
2655 },
2656 { "???",
2657 QCBOR_DECODE_MODE_NORMAL,
2658 {"\x5f\x5f\x41\x00\xff\xff", 6},
2659 QCBOR_ERR_INDEFINITE_STRING_CHUNK
2660 },
2661 { "indefinite length text string with indefinite string inside",
2662 QCBOR_DECODE_MODE_NORMAL,
2663 {"\x7f\x7f\x61\x00\xff\xff", 6},
2664 QCBOR_ERR_INDEFINITE_STRING_CHUNK
2665 },
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08002666#else /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
2667
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08002668#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
2669
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002670 /* Definte length maps and arrays must be closed by having the right number of items */
2671 { "A definte length array that is supposed to have 1 item, but has none",
2672 QCBOR_DECODE_MODE_NORMAL,
2673 {"\x81", 1},
2674 QCBOR_ERR_NO_MORE_ITEMS
2675 },
2676 { "A definte length array that is supposed to have 2 items, but has only 1",
2677 QCBOR_DECODE_MODE_NORMAL,
2678 {"\x82\x00", 2},
2679 QCBOR_ERR_NO_MORE_ITEMS
2680 },
2681 { "A definte length array that is supposed to have 511 items, but has only 1",
2682 QCBOR_DECODE_MODE_NORMAL,
2683 {"\x9a\x01\xff\x00", 4},
2684 QCBOR_ERR_HIT_END
2685 },
2686 { "A definte length map that is supposed to have 1 item, but has none",
2687 QCBOR_DECODE_MODE_NORMAL,
2688 {"\xa1", 1},
2689 QCBOR_ERR_NO_MORE_ITEMS
2690 },
2691 { "A definte length map that is supposed to have s item, but has only 1",
2692 QCBOR_DECODE_MODE_NORMAL,
2693 {"\xa2\x01\x02", 3},
2694 QCBOR_ERR_NO_MORE_ITEMS
2695 },
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08002696#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002697 /* Indefinte length maps and arrays must be ended by a break */
2698 { "Indefinite length array with zero items and no break",
2699 QCBOR_DECODE_MODE_NORMAL,
2700 {"\x9f", 1},
2701 QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002702
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002703 { "Indefinite length array with two items and no break",
2704 QCBOR_DECODE_MODE_NORMAL,
2705 {"\x9\x01\x02", 3},
2706 QCBOR_ERR_NO_MORE_ITEMS
2707 },
2708 { "Indefinite length map with zero items and no break",
2709 QCBOR_DECODE_MODE_NORMAL,
2710 {"\xbf", 1},
2711 QCBOR_ERR_NO_MORE_ITEMS
2712 },
2713 { "Indefinite length map with two items and no break",
2714 QCBOR_DECODE_MODE_NORMAL,
2715 {"\xbf\x01\x02\x01\x02", 5},
2716 QCBOR_ERR_NO_MORE_ITEMS
2717 },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002718
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002719 /* Nested maps and arrays must be closed off (some extra nested test vectors) */
2720 { "Unclosed indefinite array containing a closed definite length array",
2721 QCBOR_DECODE_MODE_NORMAL,
2722 {"\x9f\x80\x00", 3},
2723 QCBOR_ERR_NO_MORE_ITEMS
2724 },
2725
2726 { "Definite length array containing an unclosed indefinite length array",
2727 QCBOR_DECODE_MODE_NORMAL,
2728 {"\x81\x9f", 2},
2729 QCBOR_ERR_NO_MORE_ITEMS
2730 },
2731 { "Unclosed indefinite map containing a closed definite length array",
2732 QCBOR_DECODE_MODE_NORMAL,
2733 {"\xbf\x01\x80\x00\xa0", 5},
2734 QCBOR_ERR_NO_MORE_ITEMS
2735 },
2736 { "Definite length map containing an unclosed indefinite length array",
2737 QCBOR_DECODE_MODE_NORMAL,
2738 {"\xa1\x02\x9f", 3},
2739 QCBOR_ERR_NO_MORE_ITEMS
2740 },
2741 { "Deeply nested definite length arrays with deepest one unclosed",
2742 QCBOR_DECODE_MODE_NORMAL,
2743 {"\x81\x81\x81\x81\x81\x81\x81\x81\x81", 9},
2744 QCBOR_ERR_NO_MORE_ITEMS
2745 },
2746 { "Deeply nested indefinite length arrays with deepest one unclosed",
2747 QCBOR_DECODE_MODE_NORMAL,
2748 {"\x9f\x9f\x9f\x9f\x9f\xff\xff\xff\xff", 9},
2749 QCBOR_ERR_NO_MORE_ITEMS
2750 },
2751 { "Mixed nesting with indefinite unclosed",
2752 QCBOR_DECODE_MODE_NORMAL,
2753 {"\x9f\x81\x9f\x81\x9f\x9f\xff\xff\xff", 9},
2754 QCBOR_ERR_NO_MORE_ITEMS },
2755 { "Mixed nesting with definite unclosed",
2756 QCBOR_DECODE_MODE_NORMAL,
2757 {"\x9f\x82\x9f\x81\x9f\x9f\xff\xff\xff\xff", 10},
2758 QCBOR_ERR_BAD_BREAK
2759 },
2760 { "Unclosed indefinite length map in definite length maps",
2761 QCBOR_DECODE_MODE_NORMAL,
2762 {"\xa1\x01\xa2\x02\xbf\xff\x02\xbf", 8},
2763 QCBOR_ERR_NO_MORE_ITEMS
2764 },
2765 { "Unclosed definite length map in indefinite length maps",
2766 QCBOR_DECODE_MODE_NORMAL,
2767 {"\xbf\x01\xbf\x02\xa1", 5},
2768 QCBOR_ERR_NO_MORE_ITEMS
2769 },
2770 { "Unclosed indefinite length array in definite length maps",
2771 QCBOR_DECODE_MODE_NORMAL,
2772 {"\xa1\x01\xa2\x02\x9f\xff\x02\x9f", 8},
2773 QCBOR_ERR_NO_MORE_ITEMS
2774 },
2775 { "Unclosed definite length array in indefinite length maps",
2776 QCBOR_DECODE_MODE_NORMAL,
2777 {"\xbf\x01\xbf\x02\x81", 5},
2778 QCBOR_ERR_NO_MORE_ITEMS
2779 },
2780 { "Unclosed indefinite length map in definite length arrays",
2781 QCBOR_DECODE_MODE_NORMAL,
2782 {"\x81\x82\xbf\xff\xbf", 5},
2783 QCBOR_ERR_NO_MORE_ITEMS
2784 },
2785 { "Unclosed definite length map in indefinite length arrays",
2786 QCBOR_DECODE_MODE_NORMAL,
2787 {"\x9f\x9f\xa1", 3},
2788 QCBOR_ERR_NO_MORE_ITEMS
2789 },
2790
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08002791#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002792
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002793 /* The "argument" for the data item is incomplete */
2794 { "Positive integer missing 1 byte argument",
2795 QCBOR_DECODE_MODE_NORMAL,
2796 {"\x18", 1},
2797 QCBOR_ERR_HIT_END
2798 },
2799 { "Positive integer missing 2 byte argument",
2800 QCBOR_DECODE_MODE_NORMAL,
2801 {"\x19", 1},
2802 QCBOR_ERR_HIT_END
2803 },
2804 { "Positive integer missing 4 byte argument",
2805 QCBOR_DECODE_MODE_NORMAL,
2806 {"\x1a", 1},
2807 QCBOR_ERR_HIT_END
2808 },
2809 { "Positive integer missing 8 byte argument",
2810 QCBOR_DECODE_MODE_NORMAL,
2811 {"\x1b", 1},
2812 QCBOR_ERR_HIT_END
2813 },
2814 { "Positive integer missing 1 byte of 2 byte argument",
2815 QCBOR_DECODE_MODE_NORMAL,
2816 {"\x19\x01", 2},
2817 QCBOR_ERR_HIT_END
2818 },
2819 { "Positive integer missing 2 bytes of 4 byte argument",
2820 QCBOR_DECODE_MODE_NORMAL,
2821 {"\x1a\x01\x02", 3},
2822 QCBOR_ERR_HIT_END
2823 },
2824 { "Positive integer missing 1 bytes of 7 byte argument",
2825 QCBOR_DECODE_MODE_NORMAL,
2826 {"\x1b\x01\x02\x03\x04\x05\x06\x07", 8},
2827 QCBOR_ERR_HIT_END
2828 },
2829 { "Negative integer missing 1 byte argument",
2830 QCBOR_DECODE_MODE_NORMAL,
2831 {"\x38", 1},
2832 QCBOR_ERR_HIT_END
2833 },
2834 { "Binary string missing 1 byte argument",
2835 QCBOR_DECODE_MODE_NORMAL,
2836 {"\x58", 1},
2837 QCBOR_ERR_HIT_END
2838 },
2839 { "Text string missing 1 byte argument",
2840 QCBOR_DECODE_MODE_NORMAL,
2841 {"\x78", 1},
2842 QCBOR_ERR_HIT_END
2843 },
2844 { "Array missing 1 byte argument",
2845 QCBOR_DECODE_MODE_NORMAL,
2846 {"\x98", 1},
2847 QCBOR_ERR_HIT_END
2848 },
2849 { "Map missing 1 byte argument",
2850 QCBOR_DECODE_MODE_NORMAL,
2851 {"\xb8", 1},
2852 QCBOR_ERR_HIT_END
2853 },
2854 { "Tag missing 1 byte argument",
2855 QCBOR_DECODE_MODE_NORMAL,
2856 {"\xd8", 1},
2857 QCBOR_ERR_HIT_END
2858 },
2859 { "Simple missing 1 byte argument",
2860 QCBOR_DECODE_MODE_NORMAL,
2861 {"\xf8", 1},
2862 QCBOR_ERR_HIT_END
2863 },
2864 { "half-precision with 1 byte argument",
2865 QCBOR_DECODE_MODE_NORMAL,
2866 {"\xf9\x00", 2},
2867 QCBOR_ERR_HIT_END
2868 },
2869 { "single-precision with 2 byte argument",
2870 QCBOR_DECODE_MODE_NORMAL,
2871 {"\0xfa\x00\x00", 3},
2872 QCBOR_ERR_HIT_END
2873 },
2874 { "double-precision with 3 byte argument",
2875 QCBOR_DECODE_MODE_NORMAL,
2876 {"\xfb\x00\x00\x00", 4},
2877 QCBOR_ERR_HIT_END
2878 },
Laurence Lundblade2f467f92020-10-09 17:50:11 -07002879
Laurence Lundblade37286c02022-09-03 10:05:02 -07002880#ifndef QCBOR_DISABLE_TAGS
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002881 { "Tag with no content",
2882 QCBOR_DECODE_MODE_NORMAL,
2883 {"\xc0", 1},
2884 QCBOR_ERR_HIT_END
2885 },
Laurence Lundblade37286c02022-09-03 10:05:02 -07002886#else /* QCBOR_DISABLE_TAGS */
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002887 { "Tag with no content",
2888 QCBOR_DECODE_MODE_NORMAL,
2889 {"\xc0", 1},
2890 QCBOR_ERR_TAGS_DISABLED
2891 },
Laurence Lundblade37286c02022-09-03 10:05:02 -07002892#endif /* QCBOR_DISABLE_TAGS */
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002893
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002894 /* Breaks must not occur in definite length arrays and maps */
2895 { "Array of length 1 with sole member replaced by a break",
2896 QCBOR_DECODE_MODE_NORMAL,
2897 {"\x81\xff", 2},
2898 QCBOR_ERR_BAD_BREAK
2899 },
2900 { "Array of length 2 with 2nd member replaced by a break",
2901 QCBOR_DECODE_MODE_NORMAL,
2902 {"\x82\x00\xff", 3},
2903 QCBOR_ERR_BAD_BREAK
2904 },
2905 { "Map of length 1 with sole member label replaced by a break",
2906 QCBOR_DECODE_MODE_NORMAL,
2907 {"\xa1\xff", 2},
2908 QCBOR_ERR_BAD_BREAK
2909 },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002910
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002911 /* Map of length 1 with sole member label replaced by break */
2912 { "Alternate representation that some decoders handle differently",
2913 QCBOR_DECODE_MODE_NORMAL,
2914 {"\xa1\xff\x00", 3},
2915 QCBOR_ERR_BAD_BREAK
2916 },
2917 { "Array of length 1 with 2nd member value replaced by a break",
2918 QCBOR_DECODE_MODE_NORMAL,
2919 {"\xa1\x00\xff", 3},
2920 QCBOR_ERR_BAD_BREAK
2921 },
Laurence Lundbladeec290b82024-06-10 11:10:54 -07002922 { "Map of length 2 with 2nd entry label replaced by a break",
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002923 QCBOR_DECODE_MODE_NORMAL,
Laurence Lundbladeec290b82024-06-10 11:10:54 -07002924 {"\xa2\x00\x00\xff\x00", 5},
2925 QCBOR_ERR_BAD_BREAK
2926 },
2927 { "Map of length 2 with 2nd entry value replaced by a break",
2928 QCBOR_DECODE_MODE_NORMAL,
2929 {"\xa2\x00\x00\x01\xff", 5},
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002930 QCBOR_ERR_BAD_BREAK
2931 },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002932
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002933 /* Breaks must not occur on their own out of an indefinite length data item */
2934 { "A bare break is not well formed",
2935 QCBOR_DECODE_MODE_NORMAL,
2936 {"\xff", 1},
2937 QCBOR_ERR_BAD_BREAK
2938 },
2939 { "A bare break after a zero length definite length array",
2940 QCBOR_DECODE_MODE_NORMAL,
2941 {"\x80\xff", 2},
2942 QCBOR_ERR_BAD_BREAK
2943 },
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08002944#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002945 { "A bare break after a zero length indefinite length map",
2946 QCBOR_DECODE_MODE_NORMAL,
2947 {"\x9f\xff\xff", 3},
2948 QCBOR_ERR_BAD_BREAK
2949 },
2950 { "A break inside a definite length array inside an indefenite length array",
2951 QCBOR_DECODE_MODE_NORMAL,
2952 {"\x9f\x81\xff", 3},
2953 QCBOR_ERR_BAD_BREAK
2954 },
2955 { "Complicated mixed nesting with break outside indefinite length array",
2956 QCBOR_DECODE_MODE_NORMAL,
2957 {"\x9f\x82\x9f\x81\x9f\x9f\xff\xff\xff\xff", 10},
2958 QCBOR_ERR_BAD_BREAK },
2959#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
2960
2961 /* Forbidden two byte encodings of simple types */
2962 { "Must use 0xe0 instead",
2963 QCBOR_DECODE_MODE_NORMAL,
2964 {"\xf8\x00", 2},
2965 QCBOR_ERR_BAD_TYPE_7
2966 },
2967 { "Should use 0xe1 instead",
2968 QCBOR_DECODE_MODE_NORMAL,
2969 {"\xf8\x01", 2},
2970 QCBOR_ERR_BAD_TYPE_7
2971 },
2972 { "Should use 0xe2 instead",
2973 QCBOR_DECODE_MODE_NORMAL,
2974 {"\xf8\x02", 2},
2975 QCBOR_ERR_BAD_TYPE_7
2976 }, { "Should use 0xe3 instead",
2977 QCBOR_DECODE_MODE_NORMAL,
2978 {"\xf8\x03", 2},
2979 QCBOR_ERR_BAD_TYPE_7
2980 },
2981 { "Should use 0xe4 instead",
2982 QCBOR_DECODE_MODE_NORMAL,
2983 {"\xf8\x04", 2},
2984 QCBOR_ERR_BAD_TYPE_7
2985 },
2986 { "Should use 0xe5 instead",
2987 QCBOR_DECODE_MODE_NORMAL,
2988 {"\xf8\x05", 2},
2989 QCBOR_ERR_BAD_TYPE_7
2990 },
2991 { "Should use 0xe6 instead",
2992 QCBOR_DECODE_MODE_NORMAL,
2993 {"\xf8\x06", 2},
2994 QCBOR_ERR_BAD_TYPE_7
2995 },
2996 { "Should use 0xe7 instead",
2997 QCBOR_DECODE_MODE_NORMAL,
2998 {"\xf8\x07", 2},
2999 QCBOR_ERR_BAD_TYPE_7
3000 },
3001 { "Should use 0xe8 instead",
3002 QCBOR_DECODE_MODE_NORMAL,
3003 {"\xf8\x08", 2},
3004 QCBOR_ERR_BAD_TYPE_7
3005 },
3006 { "Should use 0xe9 instead",
3007 QCBOR_DECODE_MODE_NORMAL,
3008 {"\xf8\x09", 2},
3009 QCBOR_ERR_BAD_TYPE_7
3010 },
3011 { "Should use 0xea instead",
3012 QCBOR_DECODE_MODE_NORMAL,
3013 {"\xf8\x0a", 2},
3014 QCBOR_ERR_BAD_TYPE_7
3015 },
3016 { "Should use 0xeb instead",
3017 QCBOR_DECODE_MODE_NORMAL,
3018 {"\xf8\x0b", 2},
3019 QCBOR_ERR_BAD_TYPE_7
3020 },
3021 { "Should use 0xec instead",
3022 QCBOR_DECODE_MODE_NORMAL,
3023 {"\xf8\x0c", 2},
3024 QCBOR_ERR_BAD_TYPE_7
3025 },
3026 { "Should use 0xed instead",
3027 QCBOR_DECODE_MODE_NORMAL,
3028 {"\xf8\x0d", 2},
3029 QCBOR_ERR_BAD_TYPE_7
3030 },
3031 { "Should use 0xee instead",
3032 QCBOR_DECODE_MODE_NORMAL,
3033 {"\xf8\x0e", 2},
3034 QCBOR_ERR_BAD_TYPE_7
3035 },
3036 { "Should use 0xef instead",
3037 QCBOR_DECODE_MODE_NORMAL,
3038 {"\xf8\x0f", 2},
3039 QCBOR_ERR_BAD_TYPE_7
3040 },
3041 { "Should use 0xf0 instead",
3042 QCBOR_DECODE_MODE_NORMAL,
3043 {"\xf8\x10", 2},
3044 QCBOR_ERR_BAD_TYPE_7
3045 },
3046 { "Should use 0xf1 instead",
3047 QCBOR_DECODE_MODE_NORMAL,
3048 {"\xf8\x11", 2},
3049 QCBOR_ERR_BAD_TYPE_7
3050 },
3051 { "Should use 0xf2 instead",
3052 QCBOR_DECODE_MODE_NORMAL,
3053 {"\xf8\x12", 2},
3054 QCBOR_ERR_BAD_TYPE_7
3055 },
3056 { "Should use 0xf3 instead",
3057 QCBOR_DECODE_MODE_NORMAL,
3058 {"\xf8\x13", 2},
3059 QCBOR_ERR_BAD_TYPE_7
3060 },
3061 { "Should use 0xf4 instead",
3062 QCBOR_DECODE_MODE_NORMAL,
3063 {"\xf8\x14", 2},
3064 QCBOR_ERR_BAD_TYPE_7
3065 },
3066 { "Should use 0xf5 instead",
3067 QCBOR_DECODE_MODE_NORMAL,
3068 {"\xf8\x15", 2},
3069 QCBOR_ERR_BAD_TYPE_7
3070 },
3071 { "Should use 0xf6 instead",
3072 QCBOR_DECODE_MODE_NORMAL,
3073 {"\xf8\x16", 2},
3074 QCBOR_ERR_BAD_TYPE_7
3075 },
3076 { "Should use 0xef7 instead",
3077 QCBOR_DECODE_MODE_NORMAL,
3078 {"\xf8\x17", 2},
3079 QCBOR_ERR_BAD_TYPE_7
3080 },
3081 { "Should use 0xef8 instead",
3082 QCBOR_DECODE_MODE_NORMAL,
3083 {"\xf8\x18", 2},
3084 QCBOR_ERR_BAD_TYPE_7
3085 },
3086 { "Reserved",
3087 QCBOR_DECODE_MODE_NORMAL,
3088 {"\xf8\x18", 2},
3089 QCBOR_ERR_BAD_TYPE_7
3090 },
3091
3092 /* Maps must have an even number of data items (key & value) */
3093 { "Map with 1 item when it should have 2",
3094 QCBOR_DECODE_MODE_NORMAL,
3095 {"\xa1\x00", 2},
3096 QCBOR_ERR_HIT_END
3097 },
3098 { "Map with 3 item when it should have 4",
3099 QCBOR_DECODE_MODE_NORMAL,
3100 {"\xa2\x00\x00\x00", 2},
3101 QCBOR_ERR_HIT_END
3102 },
3103#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
3104 { "Map with 1 item when it should have 2",
3105 QCBOR_DECODE_MODE_NORMAL,
3106 {"\xbf\x00\xff", 3},
3107 QCBOR_ERR_BAD_BREAK
3108 },
3109 { "Map with 3 item when it should have 4",
3110 QCBOR_DECODE_MODE_NORMAL,
3111 {"\xbf\x00\x00\x00\xff", 5},
3112 QCBOR_ERR_BAD_BREAK
3113 },
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08003114#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07003115
3116
Laurence Lundblade37286c02022-09-03 10:05:02 -07003117#ifndef QCBOR_DISABLE_TAGS
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08003118 /* In addition to not-well-formed, some invalid CBOR */
3119 { "Text-based date, with an integer",
3120 QCBOR_DECODE_MODE_NORMAL,
3121 {"\xc0\x00", 2},
3122 QCBOR_ERR_BAD_OPT_TAG
3123 },
3124 { "Epoch date, with an byte string",
3125 QCBOR_DECODE_MODE_NORMAL,
3126 {"\xc1\x41\x33", 3},
3127 QCBOR_ERR_BAD_OPT_TAG
3128 },
3129 { "tagged as both epoch and string dates",
3130 QCBOR_DECODE_MODE_NORMAL,
3131 {"\xc1\xc0\x00", 3},
3132 QCBOR_ERR_BAD_OPT_TAG
3133 },
3134 { "big num tagged an int, not a byte string",
3135 QCBOR_DECODE_MODE_NORMAL,
3136 {"\xc2\x00", 2},
3137 QCBOR_ERR_BAD_OPT_TAG
3138 },
Laurence Lundblade37286c02022-09-03 10:05:02 -07003139#else /* QCBOR_DISABLE_TAGS */
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08003140 /* In addition to not-well-formed, some invalid CBOR */
3141 { "Text-based date, with an integer",
3142 QCBOR_DECODE_MODE_NORMAL,
3143 {"\xc0\x00", 2},
3144 QCBOR_ERR_TAGS_DISABLED
3145 },
3146 { "Epoch date, with an byte string",
3147 QCBOR_DECODE_MODE_NORMAL,
3148 {"\xc1\x41\x33", 3},
3149 QCBOR_ERR_TAGS_DISABLED
3150 },
3151 { "tagged as both epoch and string dates",
3152 QCBOR_DECODE_MODE_NORMAL,
3153 {"\xc1\xc0\x00", 3},
3154 QCBOR_ERR_TAGS_DISABLED
3155 },
3156 { "big num tagged an int, not a byte string",
3157 QCBOR_DECODE_MODE_NORMAL,
3158 {"\xc2\x00", 2},
3159 QCBOR_ERR_TAGS_DISABLED
3160 },
Laurence Lundblade37286c02022-09-03 10:05:02 -07003161#endif /* QCBOR_DISABLE_TAGS */
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003162};
3163
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08003164
3165
3166int32_t
3167DecodeFailureTests(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003168{
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003169 int32_t nResult;
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07003170
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08003171 nResult = ProcessDecodeFailures(Failures ,C_ARRAY_COUNT(Failures, struct DecodeFailTestInput));
Laurence Lundblade59289e52019-12-30 13:44:37 -08003172 if(nResult) {
3173 return nResult;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003174 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003175
Laurence Lundblade3a6042e2019-06-28 19:58:04 -07003176 // Corrupt the UsefulInputBuf and see that
3177 // it reflected correctly for CBOR decoding
Laurence Lundbladee6f15112020-07-23 18:44:16 -07003178 QCBORDecodeContext DCtx;
3179 QCBORItem Item;
3180 QCBORError uQCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003181
Laurence Lundbladee6f15112020-07-23 18:44:16 -07003182 QCBORDecode_Init(&DCtx,
3183 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSimpleValues),
3184 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003185
Laurence Lundbladee6f15112020-07-23 18:44:16 -07003186 if((uQCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
3187 return (int32_t)uQCBORError;
3188 }
3189 if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.val.uCount != 10) {
3190 // This wasn't supposed to happen
3191 return -1;
3192 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003193
Laurence Lundbladee6f15112020-07-23 18:44:16 -07003194 DCtx.InBuf.magic = 0; // Reach in and corrupt the UsefulInputBuf
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003195
Laurence Lundbladee6f15112020-07-23 18:44:16 -07003196 uQCBORError = QCBORDecode_GetNext(&DCtx, &Item);
3197 if(uQCBORError != QCBOR_ERR_NO_MORE_ITEMS) {
3198 // Did not get back the error expected
3199 return -2;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003200 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003201
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003202
Laurence Lundblade98427e92020-09-28 21:33:23 -07003203 /*
3204 The max size of a string for QCBOR is SIZE_MAX - 4 so this
3205 tests here can be performed to see that the max length
3206 error check works correctly. See DecodeBytes(). If the max
3207 size was SIZE_MAX, it wouldn't be possible to test this.
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003208
Laurence Lundblade98427e92020-09-28 21:33:23 -07003209 This test will automatocally adapt the all CPU sizes
3210 through the use of SIZE_MAX.
3211 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003212
Laurence Lundblade8510f8c2020-12-01 11:31:16 -08003213 UsefulBuf_MAKE_STACK_UB( HeadBuf, QCBOR_HEAD_BUFFER_SIZE);
Laurence Lundblade98427e92020-09-28 21:33:23 -07003214 UsefulBufC EncodedHead;
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003215
Laurence Lundblade98427e92020-09-28 21:33:23 -07003216 // This makes a CBOR head with a text string that is very long
3217 // but doesn't fill in the bytes of the text string as that is
3218 // not needed to test this part of QCBOR.
3219 EncodedHead = QCBOREncode_EncodeHead(HeadBuf, CBOR_MAJOR_TYPE_TEXT_STRING, 0, SIZE_MAX);
3220
3221 QCBORDecode_Init(&DCtx, EncodedHead, QCBOR_DECODE_MODE_NORMAL);
3222
3223 if(QCBOR_ERR_STRING_TOO_LONG != QCBORDecode_GetNext(&DCtx, &Item)) {
3224 return -4;
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003225 }
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003226
Laurence Lundblade3a6042e2019-06-28 19:58:04 -07003227 return 0;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003228}
3229
3230
Laurence Lundbladea2e29072018-12-30 09:20:06 -08003231/* Try all 256 values of the byte at nLen including recursing for
3232 each of the values to try values at nLen+1 ... up to nLenMax
3233 */
Laurence Lundblade06350ea2020-01-27 19:32:40 -08003234static void ComprehensiveInputRecurser(uint8_t *pBuf, size_t nLen, size_t nLenMax)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003235{
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003236 if(nLen >= nLenMax) {
3237 return;
3238 }
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08003239
Laurence Lundbladea2e29072018-12-30 09:20:06 -08003240 for(int inputByte = 0; inputByte < 256; inputByte++) {
3241 // Set up the input
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003242 pBuf[nLen] = (uint8_t)inputByte;
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08003243 const UsefulBufC Input = {pBuf, nLen+1};
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08003244
Laurence Lundbladea2e29072018-12-30 09:20:06 -08003245 // Get ready to parse
3246 QCBORDecodeContext DCtx;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003247 QCBORDecode_Init(&DCtx, Input, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003248
Laurence Lundbladea2e29072018-12-30 09:20:06 -08003249 // Parse by getting the next item until an error occurs
3250 // Just about every possible decoder error can occur here
3251 // The goal of this test is not to check for the correct
3252 // error since that is not really possible. It is to
3253 // see that there is no crash on hostile input.
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003254 while(1) {
Laurence Lundbladea2e29072018-12-30 09:20:06 -08003255 QCBORItem Item;
3256 QCBORError nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003257 if(nCBORError != QCBOR_SUCCESS) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003258 break;
3259 }
3260 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003261
Laurence Lundbladea2e29072018-12-30 09:20:06 -08003262 ComprehensiveInputRecurser(pBuf, nLen+1, nLenMax);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003263 }
3264}
3265
3266
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03003267int32_t ComprehensiveInputTest(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003268{
Laurence Lundbladea2e29072018-12-30 09:20:06 -08003269 // Size 2 tests 64K inputs and runs quickly
3270 uint8_t pBuf[2];
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003271
Laurence Lundbladea2e29072018-12-30 09:20:06 -08003272 ComprehensiveInputRecurser(pBuf, 0, sizeof(pBuf));
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003273
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003274 return 0;
3275}
3276
Laurence Lundbladea2e29072018-12-30 09:20:06 -08003277
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03003278int32_t BigComprehensiveInputTest(void)
Laurence Lundbladea2e29072018-12-30 09:20:06 -08003279{
3280 // size 3 tests 16 million inputs and runs OK
3281 // in seconds on fast machines. Size 4 takes
3282 // 10+ minutes and 5 half a day on fast
3283 // machines. This test is kept separate from
3284 // the others so as to no slow down the use
3285 // of them as a very frequent regression.
3286 uint8_t pBuf[3]; //
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08003287
Laurence Lundbladea2e29072018-12-30 09:20:06 -08003288 ComprehensiveInputRecurser(pBuf, 0, sizeof(pBuf));
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08003289
Laurence Lundbladea2e29072018-12-30 09:20:06 -08003290 return 0;
3291}
3292
3293
Laurence Lundbladecc7da412020-12-27 00:09:07 -08003294static const uint8_t spDateTestInput[] = {
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003295 /* 1. The valid date string "1985-04-12" */
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003296 0xc0, // tag for string date
3297 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003298
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003299 /* 2. An invalid date string due to wrong tag content type */
Laurence Lundbladec7114722020-08-13 05:11:40 -07003300 0xc0, // tag for string date
3301 0x00, // Wrong type for a string date
3302
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003303 /* 3. A valid epoch date, 1400000000; Tue, 13 May 2014 16:53:20 GMT */
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003304 0xc1, // tag for epoch date
3305 0x1a, 0x53, 0x72, 0x4E, 0x00, // Epoch date 1400000000; Tue, 13 May 2014 16:53:20 GMT
3306
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003307 /* 4. An invalid epoch date due to wrong tag content type */
Laurence Lundbladec7114722020-08-13 05:11:40 -07003308 0xc1,
3309 0x62, 'h', 'i', // wrong type tagged
3310
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003311 /* 5. Valid epoch date tag as content for a two other nested tags */
Laurence Lundblade99615302020-11-29 11:19:47 -08003312 // CBOR_TAG_ENC_AS_B64
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003313 0xcf, 0xd8, 0x16, 0xc1, // Epoch date with extra tags
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003314 0x1a, 0x53, 0x72, 0x4E, 0x01,
3315
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003316 /* 6. Epoch date with value to large to fit into int64 */
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003317 0xc1, // tag for epoch date
3318 0x1b, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, // Too large integer
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003319
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003320 /* 7. Epoch date with single-precision value of 1.1. */
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003321 0xc1, // tag for epoch date
Laurence Lundblade3ed0bca2020-07-14 22:50:10 -07003322 0xfa, 0x3f, 0x8c, 0xcc, 0xcd, // single with value 1.1
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003323
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003324 /* 8. Epoch date with too-large single precision float */
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003325 0xc1, // tag for epoch date
Laurence Lundbladea1ad8782019-11-08 00:12:11 -08003326 0xfa, 0x7f, 0x7f, 0xff, 0xff, // 3.4028234663852886e+38 too large
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003327
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003328 /* 9. Epoch date with slightly too-large double precision value */
Laurence Lundbladea1ad8782019-11-08 00:12:11 -08003329 0xc1, // tag for epoch date
3330 0xfb, 0x43, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 9223372036854775808.000000 just barely too large
3331 //0xfa, 0x7f, 0x7f, 0xff, 0xff // 3.4028234663852886e+38 too large
3332
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003333 /* 10. Epoch date with largest supported double precision value */
Laurence Lundbladea1ad8782019-11-08 00:12:11 -08003334 0xc1, // tag for epoch date
Laurence Lundbladec7114722020-08-13 05:11:40 -07003335 0xfb, 0x43, 0xdf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, // 9223372036854773760 largest supported
3336
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003337 /* 11. Epoch date with single-precision NaN */
Laurence Lundbladec7114722020-08-13 05:11:40 -07003338 0xc1, // tag for epoch date
3339 0xfa, 0x7f, 0xc0, 0x00, 0x00, // Single-precision NaN
3340
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003341 /* 12. Epoch date with double precision plus infinity */
Laurence Lundbladec7114722020-08-13 05:11:40 -07003342 0xc1,
3343 0xfb, 0x7f, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // +infinity
3344
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003345 /* 13. Epoch date with half-precision negative infinity */
Laurence Lundbladec7114722020-08-13 05:11:40 -07003346 0xc1, // tag for epoch date
3347 0xf9, 0xfc, 0x00, // -Infinity
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003348};
3349
3350
Laurence Lundbladec7114722020-08-13 05:11:40 -07003351
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003352// have to check float expected only to within an epsilon
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07003353#ifndef QCBOR_DISABLE_FLOAT_HW_USE
Laurence Lundblade02fcf312020-07-17 02:49:46 -07003354static int CHECK_EXPECTED_DOUBLE(double val, double expected) {
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003355
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003356 double diff = val - expected;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003357
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003358 diff = fabs(diff);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003359
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003360 return diff > 0.0000001;
3361}
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07003362#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003363
3364
Laurence Lundblade37286c02022-09-03 10:05:02 -07003365/* Test date decoding using GetNext() */
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03003366int32_t DateParseTest(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003367{
3368 QCBORDecodeContext DCtx;
Laurence Lundbladec7114722020-08-13 05:11:40 -07003369 QCBORItem Item;
3370 QCBORError uError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003371
Laurence Lundbladeee851742020-01-08 08:37:05 -08003372 QCBORDecode_Init(&DCtx,
3373 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spDateTestInput),
3374 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003375
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003376 /* 1. The valid date string "1985-04-12" */
Laurence Lundbladec7114722020-08-13 05:11:40 -07003377 if((uError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003378 return -1;
Laurence Lundbladec7114722020-08-13 05:11:40 -07003379 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003380 if(Item.uDataType != QCBOR_TYPE_DATE_STRING ||
Laurence Lundbladeba587682024-02-07 16:46:43 -08003381 UsefulBufCompareToSZ(Item.val.string, "1985-04-12")){
Laurence Lundblade67bd5512018-11-02 21:44:06 +07003382 return -2;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003383 }
3384
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003385 /* 2. An invalid date string due to wrong tag content type */
Laurence Lundbladec7114722020-08-13 05:11:40 -07003386 uError = QCBORDecode_GetNext(&DCtx, &Item);
3387 if(uError != QCBOR_ERR_BAD_OPT_TAG) {
Laurence Lundblade67bd5512018-11-02 21:44:06 +07003388 return -3;
Laurence Lundbladec7114722020-08-13 05:11:40 -07003389 }
3390
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003391 /* 3. A valid epoch date, 1400000000; Tue, 13 May 2014 16:53:20 GMT */
3392 uError = QCBORDecode_GetNext(&DCtx, &Item);
3393 if(uError != QCBOR_SUCCESS) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07003394 return -4;
3395 }
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003396 if(uError == QCBOR_SUCCESS) {
3397 if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH ||
3398 Item.val.epochDate.nSeconds != 1400000000
Máté Tóth-Pálef5f07a2021-09-17 19:31:37 +02003399#ifndef USEFULBUF_DISABLE_ALL_FLOAT
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003400 || Item.val.epochDate.fSecondsFraction != 0
Máté Tóth-Pálef5f07a2021-09-17 19:31:37 +02003401#endif /* USEFULBUF_DISABLE_ALL_FLOAT */
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003402 ) {
3403 return -5;
3404 }
Laurence Lundbladec7114722020-08-13 05:11:40 -07003405 }
3406
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003407 /* 4. An invalid epoch date due to wrong tag content type */
Laurence Lundbladec7114722020-08-13 05:11:40 -07003408 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_OPT_TAG) {
3409 return -6;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003410 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003411
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003412 /* 5. Valid epoch date tag as content for a two other nested tags */
Laurence Lundblade99615302020-11-29 11:19:47 -08003413 // Epoch date wrapped in an CBOR_TAG_ENC_AS_B64 and an unknown tag.
3414 // The date is decoded and the two tags are returned. This is to
3415 // make sure the wrapping of epoch date in another tag works OK.
Laurence Lundbladec7114722020-08-13 05:11:40 -07003416 if((uError = QCBORDecode_GetNext(&DCtx, &Item))) {
3417 return -7;
3418 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003419 if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH ||
3420 Item.val.epochDate.nSeconds != 1400000001 ||
Máté Tóth-Pálef5f07a2021-09-17 19:31:37 +02003421#ifndef USEFULBUF_DISABLE_ALL_FLOAT
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003422 Item.val.epochDate.fSecondsFraction != 0 ||
Máté Tóth-Pálef5f07a2021-09-17 19:31:37 +02003423#endif /* USEFULBUF_DISABLE_ALL_FLOAT */
Laurence Lundblade99615302020-11-29 11:19:47 -08003424 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_ENC_AS_B64)) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07003425 return -8;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003426 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003427
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003428 /* 6. Epoch date with value to large to fit into int64 */
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003429 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_DATE_OVERFLOW) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07003430 return -9;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003431 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003432
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003433 /* 7. Epoch date with single-precision value of 1.1. */
3434 uError = QCBORDecode_GetNext(&DCtx, &Item);
3435 if(uError != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07003436 return -10;
3437 }
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003438 if(uError == QCBOR_SUCCESS) {
3439 if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH ||
3440 Item.val.epochDate.nSeconds != 1
3441#ifndef QCBOR_DISABLE_FLOAT_HW_USE
3442 || CHECK_EXPECTED_DOUBLE(Item.val.epochDate.fSecondsFraction, 0.1)
Máté Tóth-Pálef5f07a2021-09-17 19:31:37 +02003443#endif /* USEFULBUF_DISABLE_ALL_FLOAT */
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003444 ) {
3445 return -11;
3446 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003447 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003448
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003449 /* 8. Epoch date with too-large single-precision float */
3450 uError = QCBORDecode_GetNext(&DCtx, &Item);
3451 if(uError != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_DATE_OVERFLOW)) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07003452 return -12;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003453 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003454
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003455 /* 9. Epoch date with slightly too-large double-precision value */
3456 uError = QCBORDecode_GetNext(&DCtx, &Item);
3457 if(uError != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_DATE_OVERFLOW)) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07003458 return -13;
Laurence Lundbladea1ad8782019-11-08 00:12:11 -08003459 }
3460
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003461 /* 10. Epoch date with largest supported double-precision value */
3462 uError = QCBORDecode_GetNext(&DCtx, &Item);
3463 if(uError != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)) {
3464 return -14;
3465 }
3466 if(uError == QCBOR_SUCCESS) {
3467 if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH ||
3468 Item.val.epochDate.nSeconds != 9223372036854773760
3469#ifndef QCBOR_DISABLE_FLOAT_HW_USE
3470 || Item.val.epochDate.fSecondsFraction != 0.0
3471#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
3472 ) {
3473 return -14;
3474 }
3475 }
Laurence Lundbladec7114722020-08-13 05:11:40 -07003476
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003477 /* 11. Epoch date with single-precision NaN */
3478 if(QCBORDecode_GetNext(&DCtx, &Item) != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_DATE_OVERFLOW)) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07003479 return -15;
3480 }
3481
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003482 /* 12. Epoch date with double-precision plus infinity */
3483 if(QCBORDecode_GetNext(&DCtx, &Item) != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_DATE_OVERFLOW)) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07003484 return -16;
3485 }
3486
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003487 /* 13. Epoch date with half-precision negative infinity */
3488 uError = QCBORDecode_GetNext(&DCtx, &Item);
3489 if(uError != FLOAT_ERR_CODE_NO_HALF_PREC_NO_FLOAT_HW(QCBOR_ERR_DATE_OVERFLOW)) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07003490 return -17;
3491 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003492
3493 return 0;
3494}
3495
Laurence Lundblade37286c02022-09-03 10:05:02 -07003496
Laurence Lundblade4b270642020-08-14 12:53:07 -07003497/*
3498 Test cases covered here. Some items cover more than one of these.
3499 positive integer (zero counts as a positive integer)
3500 negative integer
3501 half-precision float
3502 single-precision float
3503 double-precision float
Laurence Lundbladec7114722020-08-13 05:11:40 -07003504
Laurence Lundblade4b270642020-08-14 12:53:07 -07003505 float Overflow error
3506 Wrong type error for epoch
3507 Wrong type error for date string
3508 float disabled error
3509 half-precision disabled error
3510 -Infinity
3511 Slightly too large integer
3512 Slightly too far from zero
Laurence Lundbladec7114722020-08-13 05:11:40 -07003513
Laurence Lundblade4b270642020-08-14 12:53:07 -07003514 Get epoch by int
3515 Get string by int
3516 Get epoch by string
3517 Get string by string
3518 Fail to get epoch by wrong int label
3519 Fail to get string by wrong string label
3520 Fail to get epoch by string because it is invalid
3521 Fail to get epoch by int because it is invalid
3522
3523 Untagged values
3524 */
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08003525static const uint8_t spSpiffyDateTestInput[] = {
Laurence Lundblade37286c02022-09-03 10:05:02 -07003526 0x87, // array of 7 items
3527
3528 0xa6, // Open a map for tests involving untagged items with labels.
3529
3530 // Untagged integer 0
3531 0x08,
3532 0x00,
3533
3534 // Utagged date string with string label y
3535 0x61, 0x79,
3536 0x6a, '2','0','8','5','-','0','4','-','1','2', // Untagged date string
3537
3538 // Untagged single-precision float with value 3.14 with string label x
3539 0x61, 0x78,
3540 0xFA, 0x40, 0x48, 0xF5, 0xC3,
3541
3542 // Untagged half-precision float with value -2
3543 0x09,
3544 0xF9, 0xC0, 0x00,
3545
3546 /* Untagged date-only date string */
3547 0x18, 0x63,
3548 0x6A, 0x31, 0x39, 0x38, 0x35, 0x2D, 0x30, 0x34, 0x2D, 0x31, 0x32, /* "1985-04-12" */
3549
3550 /* Untagged days-count epoch date */
3551 0x11,
3552 0x19, 0x0F, 0x9A, /* 3994 */
3553
3554 // End of map, back to array
3555
3556 0xa7, // Open map of tagged items with labels
3557
3558 0x00,
3559 0xc0, // tag for string date
3560 0x6a, '1','9','8','5','-','0','4','-','1','2', // Tagged date string
3561
3562
3563 0x01,
3564 0xda, 0x03, 0x03, 0x03, 0x03, // An additional tag
3565 0xc1, // tag for epoch date
3566 0x1a, 0x53, 0x72, 0x4E, 0x00, // Epoch date 1400000000; Tue, 13 May 2014 16:53:20 GMT
3567
3568 0x05,
3569 0xc1,
3570 0xfb, 0xc3, 0xdf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, // -9223372036854773760 largest negative
3571
3572
3573 0x07,
3574 0xc1, // tag for epoch date
3575 0xfb, 0x43, 0xdf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, // 9223372036854773760 largest supported
3576
3577 /* Tagged days-count epoch date */
3578 0x63, 0x53, 0x44, 0x45,
3579 0xD8, 0x64, /* tag(100) */
3580 0x39, 0x29, 0xB3, /* -10676 */
3581
3582 // Untagged -1000 with label z
3583 0x61, 0x7a,
3584 0xda, 0x01, 0x01, 0x01, 0x01, // An additional tag
3585 0x39, 0x03, 0xe7,
3586
3587 /* Tagged date-only date string */
3588 0x63, 0x53, 0x44, 0x53,
3589 0xD9, 0x03, 0xEC,
3590 0x6A, 0x31, 0x39, 0x38, 0x35, 0x2D, 0x30, 0x34, 0x2D, 0x31, 0x32, /* "1985-04-12" */
3591
3592 // End of map of tagged items
Laurence Lundblade4b270642020-08-14 12:53:07 -07003593
3594 0xc1,
3595 0xfb, 0xc3, 0xdf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, // -9.2233720368547748E+18, too negative
3596
Laurence Lundbladec7114722020-08-13 05:11:40 -07003597 0xc1, // tag for epoch date
Laurence Lundblade4b270642020-08-14 12:53:07 -07003598 0x1b, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, // Too-large integer
3599
3600 0xc1, // tag for epoch date
3601 0xf9, 0xfc, 0x00, // Half-precision -Infinity
3602
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07003603 // These two at the end because they are unrecoverable errors
3604 0xc1, // tag for epoch date
3605 0x80, // Erroneous empty array as content for date
3606
3607 0xc0, // tag for string date
3608 0xa0 // Erroneous empty map as content for date
Laurence Lundbladec7114722020-08-13 05:11:40 -07003609};
3610
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03003611int32_t SpiffyDateDecodeTest(void)
Laurence Lundbladec7114722020-08-13 05:11:40 -07003612{
3613 QCBORDecodeContext DC;
Laurence Lundblade4b270642020-08-14 12:53:07 -07003614 QCBORError uError;
Laurence Lundblade37286c02022-09-03 10:05:02 -07003615 int64_t nEpochDate3, nEpochDate5,
3616 nEpochDate4, nEpochDate6,
3617 nEpochDays2;
3618 UsefulBufC StringDate1, StringDate2, StringDays2;
Laurence Lundbladec7114722020-08-13 05:11:40 -07003619
3620 QCBORDecode_Init(&DC,
Laurence Lundblade4b270642020-08-14 12:53:07 -07003621 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSpiffyDateTestInput),
Laurence Lundbladec7114722020-08-13 05:11:40 -07003622 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade37286c02022-09-03 10:05:02 -07003623
3624 /* Items are in an array or map to test look up by label and other
3625 * that might not occur in isolated items. But it does make the
3626 * test a bit messy. */
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07003627 QCBORDecode_EnterArray(&DC, NULL);
Laurence Lundbladec7114722020-08-13 05:11:40 -07003628
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07003629 QCBORDecode_EnterMap(&DC, NULL);
Laurence Lundblade4b270642020-08-14 12:53:07 -07003630
Laurence Lundblade4b270642020-08-14 12:53:07 -07003631 // A single-precision date
Laurence Lundblade9b334962020-08-27 10:55:53 -07003632 QCBORDecode_GetEpochDateInMapSZ(&DC, "x", QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
3633 &nEpochDate5);
Laurence Lundblade4b270642020-08-14 12:53:07 -07003634 uError = QCBORDecode_GetAndResetError(&DC);
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003635 if(uError != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07003636 return 104;
3637 }
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003638 if(uError == QCBOR_SUCCESS) {
3639 if(nEpochDate5 != 3) {
3640 return 103;
3641 }
3642 }
Laurence Lundbladec7114722020-08-13 05:11:40 -07003643
Laurence Lundblade9b334962020-08-27 10:55:53 -07003644 // A half-precision date with value -2 FFF
3645 QCBORDecode_GetEpochDateInMapN(&DC, 9, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
3646 &nEpochDate4);
Laurence Lundblade4b270642020-08-14 12:53:07 -07003647 uError = QCBORDecode_GetAndResetError(&DC);
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003648 if(uError != FLOAT_ERR_CODE_NO_HALF_PREC_NO_FLOAT_HW(QCBOR_SUCCESS)) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07003649 return 106;
3650 }
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003651 if(uError == QCBOR_SUCCESS) {
3652 if(nEpochDate4 != -2) {
3653 return 105;
3654 }
3655 }
Laurence Lundblade4b270642020-08-14 12:53:07 -07003656
3657 // Fail to get an epoch date by string label
Laurence Lundblade9b334962020-08-27 10:55:53 -07003658 QCBORDecode_GetEpochDateInMapSZ(&DC, "no-label",
3659 QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
3660 &nEpochDate6);
Laurence Lundblade4b270642020-08-14 12:53:07 -07003661 uError = QCBORDecode_GetAndResetError(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07003662 if(uError != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07003663 return 107;
3664 }
3665
3666 // Fail to get an epoch date by integer label
Laurence Lundblade9b334962020-08-27 10:55:53 -07003667 QCBORDecode_GetEpochDateInMapN(&DC, 99999, QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
3668 &nEpochDate6);
Laurence Lundblade4b270642020-08-14 12:53:07 -07003669 uError = QCBORDecode_GetAndResetError(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07003670 if(uError != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07003671 return 108;
3672 }
3673
3674 // Fail to get a string date by string label
Laurence Lundblade9b334962020-08-27 10:55:53 -07003675 QCBORDecode_GetDateStringInMapSZ(&DC, "no-label",
3676 QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
3677 &StringDate1);
Laurence Lundblade4b270642020-08-14 12:53:07 -07003678 uError = QCBORDecode_GetAndResetError(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07003679 if(uError != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07003680 return 109;
3681 }
3682
3683 // Fail to get a string date by integer label
Laurence Lundblade9b334962020-08-27 10:55:53 -07003684 QCBORDecode_GetDateStringInMapN(&DC, 99999, QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
3685 &StringDate1);
Laurence Lundblade4b270642020-08-14 12:53:07 -07003686 uError = QCBORDecode_GetAndResetError(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07003687 if(uError != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07003688 return 110;
3689 }
3690
3691 // The rest of these succeed even if float features are disabled
Laurence Lundbladea9489f82020-09-12 13:50:56 -07003692
Laurence Lundblade37286c02022-09-03 10:05:02 -07003693
3694 // Untagged integer 0
3695 QCBORDecode_GetEpochDateInMapN(&DC, 8, QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
3696 &nEpochDate3);
3697 // Untagged date string
3698 QCBORDecode_GetDateStringInMapSZ(&DC, "y", QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
3699 &StringDate2);
3700
3701 QCBORDecode_GetDaysStringInMapN(&DC, 99, QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
3702 &StringDays2);
3703
3704 QCBORDecode_GetEpochDaysInMapN(&DC, 17, QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
3705 &nEpochDays2);
3706
3707 QCBORDecode_ExitMap(&DC);
3708 if(QCBORDecode_GetError(&DC) != QCBOR_SUCCESS) {
3709 return 3001;
3710 }
3711
3712 // The map of tagged items
3713 QCBORDecode_EnterMap(&DC, NULL);
3714
3715#ifndef QCBOR_DISABLE_TAGS
3716 int64_t nEpochDate2,
3717 nEpochDateFail,
3718 nEpochDate1400000000, nEpochDays1;
3719 UsefulBufC StringDays1;
3720 uint64_t uTag1, uTag2;
3721
3722 // Tagged date string
3723 QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
3724 &StringDate1);
3725
Laurence Lundblade4b270642020-08-14 12:53:07 -07003726 // Epoch date 1400000000; Tue, 13 May 2014 16:53:20 GMT
Laurence Lundblade9b334962020-08-27 10:55:53 -07003727 QCBORDecode_GetEpochDateInMapN(&DC,
3728 1,
3729 QCBOR_TAG_REQUIREMENT_TAG |
3730 QCBOR_TAG_REQUIREMENT_ALLOW_ADDITIONAL_TAGS,
3731 &nEpochDate1400000000);
3732 uTag1 = QCBORDecode_GetNthTagOfLast(&DC, 0);
Laurence Lundblade37286c02022-09-03 10:05:02 -07003733
3734 // Get largest negative double precision epoch date allowed
3735 QCBORDecode_GetEpochDateInMapN(&DC,
3736 5,
3737 QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG |
3738 QCBOR_TAG_REQUIREMENT_ALLOW_ADDITIONAL_TAGS,
3739 &nEpochDate2);
3740 uError = QCBORDecode_GetAndResetError(&DC);
3741 if(uError != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)) {
3742 return 102;
3743 }
3744 if(uError == QCBOR_SUCCESS) {
3745 if(nEpochDate2 != -9223372036854773760LL) {
3746 return 101;
3747 }
3748 }
3749
Laurence Lundblade4b270642020-08-14 12:53:07 -07003750 // Untagged -1000 with label z
Laurence Lundblade9b334962020-08-27 10:55:53 -07003751 QCBORDecode_GetEpochDateInMapSZ(&DC,
3752 "z",
3753 QCBOR_TAG_REQUIREMENT_NOT_A_TAG |
3754 QCBOR_TAG_REQUIREMENT_ALLOW_ADDITIONAL_TAGS,
3755 &nEpochDate6);
3756 uTag2 = QCBORDecode_GetNthTagOfLast(&DC, 0);
Laurence Lundblade4b270642020-08-14 12:53:07 -07003757
Laurence Lundblade37286c02022-09-03 10:05:02 -07003758
3759 // Get largest double precision epoch date allowed
3760 QCBORDecode_GetEpochDateInMapN(&DC, 7, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
3761 &nEpochDate2);
3762 uError = QCBORDecode_GetAndResetError(&DC);
3763 if(uError != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)) {
3764 return 112;
3765 }
3766 if(uError == QCBOR_SUCCESS) {
3767 if(nEpochDate2 != 9223372036854773760ULL) {
3768 return 111;
3769 }
3770 }
3771
Laurence Lundblade46d63e92021-05-13 11:37:10 -07003772 /* The days format is much simpler than the date format
3773 * because it can't be a floating point value. The test
3774 * of the spiffy decode functions sufficiently covers
3775 * the test of the non-spiffy decode days date decoding.
3776 * There is no full fan out of the error conditions
3777 * and decode options as that is implemented by code
3778 * that is tested well by the date testing above.
3779 */
3780 QCBORDecode_GetDaysStringInMapSZ(&DC, "SDS", QCBOR_TAG_REQUIREMENT_TAG,
3781 &StringDays1);
3782
Laurence Lundblade46d63e92021-05-13 11:37:10 -07003783 QCBORDecode_GetEpochDaysInMapSZ(&DC, "SDE", QCBOR_TAG_REQUIREMENT_TAG,
3784 &nEpochDays1);
3785
Laurence Lundblade4b270642020-08-14 12:53:07 -07003786 QCBORDecode_ExitMap(&DC);
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07003787 if(QCBORDecode_GetError(&DC) != QCBOR_SUCCESS) {
3788 return 3001;
3789 }
3790
Laurence Lundblade37286c02022-09-03 10:05:02 -07003791 // Too-negative float, -9.2233720368547748E+18
3792 QCBORDecode_GetEpochDate(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nEpochDateFail);
3793 uError = QCBORDecode_GetAndResetError(&DC);
3794 if(uError != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_DATE_OVERFLOW)) {
3795 return 1111;
3796 }
3797
3798 // Too-large integer
3799 QCBORDecode_GetEpochDate(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nEpochDateFail);
3800 uError = QCBORDecode_GetAndResetError(&DC);
3801 if(uError != QCBOR_ERR_DATE_OVERFLOW) {
3802 return 1;
3803 }
3804
3805 // Half-precision minus infinity
3806 QCBORDecode_GetEpochDate(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nEpochDateFail);
3807 uError = QCBORDecode_GetAndResetError(&DC);
3808 if(uError != FLOAT_ERR_CODE_NO_HALF_PREC_NO_FLOAT_HW(QCBOR_ERR_DATE_OVERFLOW)) {
3809 return 2;
3810 }
3811
3812
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07003813 // Bad content for epoch date
3814 QCBORDecode_GetEpochDate(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nEpochDateFail);
3815 uError = QCBORDecode_GetAndResetError(&DC);
3816 if(uError != QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT) {
3817 return 3;
3818 }
3819
3820 // Bad content for string date
3821 QCBORDecode_GetDateString(&DC, QCBOR_TAG_REQUIREMENT_TAG, &StringDate1);
3822 uError = QCBORDecode_GetAndResetError(&DC);
3823 if(uError != QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT) {
3824 return 4;
3825 }
3826
Laurence Lundblade4b270642020-08-14 12:53:07 -07003827 QCBORDecode_ExitArray(&DC);
3828 uError = QCBORDecode_Finish(&DC);
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07003829 if(uError != QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT) {
Laurence Lundblade9b334962020-08-27 10:55:53 -07003830 return 1000 + (int32_t)uError;
Laurence Lundblade4b270642020-08-14 12:53:07 -07003831 }
Laurence Lundblade37286c02022-09-03 10:05:02 -07003832#else /* QCBOR_DISABLE_TAGS */
3833 QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
3834 &StringDate1);
3835 uError = QCBORDecode_GetAndResetError(&DC);
3836 if(uError != QCBOR_ERR_TAGS_DISABLED) {
3837 return 4;
3838 }
3839#endif /* QCBOR_DISABLE_TAGS */
3840
3841
3842#ifndef QCBOR_DISABLE_TAGS
Laurence Lundblade4b270642020-08-14 12:53:07 -07003843
Laurence Lundblade9b334962020-08-27 10:55:53 -07003844 if(nEpochDate1400000000 != 1400000000) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07003845 return 200;
3846 }
3847
Laurence Lundblade9b334962020-08-27 10:55:53 -07003848 if(uTag1 != 0x03030303) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07003849 return 201;
3850 }
3851
Laurence Lundblade37286c02022-09-03 10:05:02 -07003852 if(nEpochDays1 != -10676) {
3853 return 205;
Laurence Lundblade4b270642020-08-14 12:53:07 -07003854 }
3855
Laurence Lundblade37286c02022-09-03 10:05:02 -07003856 if(UsefulBuf_Compare(StringDays1, UsefulBuf_FromSZ("1985-04-12"))) {
3857 return 207;
Laurence Lundblade4b270642020-08-14 12:53:07 -07003858 }
3859
Laurence Lundblade9b334962020-08-27 10:55:53 -07003860 if(uTag2 != 0x01010101) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07003861 return 204;
3862 }
3863
Laurence Lundblade37286c02022-09-03 10:05:02 -07003864 if(nEpochDate6 != -1000) {
3865 return 203;
Laurence Lundblade46d63e92021-05-13 11:37:10 -07003866 }
3867
Laurence Lundblade9b334962020-08-27 10:55:53 -07003868 if(UsefulBuf_Compare(StringDate1, UsefulBuf_FromSZ("1985-04-12"))) {
3869 return 205;
3870 }
3871
Laurence Lundblade37286c02022-09-03 10:05:02 -07003872#endif /* QCBOR_DISABLE_TAGS */
3873
3874 if(nEpochDate3 != 0) {
3875 return 202;
3876 }
3877
3878 if(nEpochDays2 != 3994) {
Laurence Lundblade9b334962020-08-27 10:55:53 -07003879 return 206;
3880 }
3881
Laurence Lundblade37286c02022-09-03 10:05:02 -07003882 if(UsefulBuf_Compare(StringDate2, UsefulBuf_FromSZ("2085-04-12"))) {
3883 return 206;
Laurence Lundblade46d63e92021-05-13 11:37:10 -07003884 }
3885
3886 if(UsefulBuf_Compare(StringDays2, UsefulBuf_FromSZ("1985-04-12"))) {
3887 return 208;
3888 }
3889
Laurence Lundbladec7114722020-08-13 05:11:40 -07003890 return 0;
3891}
3892
3893
Laurence Lundblade9b334962020-08-27 10:55:53 -07003894// Input for one of the tagging tests
Laurence Lundbladecc7da412020-12-27 00:09:07 -08003895static const uint8_t spTagInput[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003896 0xd9, 0xd9, 0xf7, // CBOR magic number
Laurence Lundblade9b334962020-08-27 10:55:53 -07003897 0x81, // Array of one
3898 0xd8, 0x04, // non-preferred serialization of tag 4, decimal fraction
3899 0x82, // Array of two that is the faction 1/3
3900 0x01,
3901 0x03,
3902
3903 /*
3904 More than 4 tags on an item 225(226(227(228(229([])))))
3905 */
3906 0xd8, 0xe1,
3907 0xd8, 0xe2,
3908 0xd8, 0xe3,
3909 0xd8, 0xe4,
3910 0xd8, 0xe5,
3911 0x80,
3912
3913 /* tag 10489608748473423768(
3914 2442302356(
3915 21590(
3916 240(
3917 []))))
3918 */
3919 0xdb, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98,
3920 0xda, 0x91, 0x92, 0x93, 0x94,
3921 0xd9, 0x54, 0x56,
3922 0xd8, 0xf0,
3923 0x80,
3924
3925 /* tag 21590(
3926 10489608748473423768(
3927 2442302357(
3928 65534(
3929 []))))
3930 */
3931 0xdb, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x54, 0x56,
3932 0xdb, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98,
3933 0xda, 0x91, 0x92, 0x93, 0x95,
3934 0xd9, 0xff, 0xfe,
3935 0x80,
3936
3937 /* Make sure to blow past the limit of tags that must be mapped.
3938 works in conjuntion with entries above.
3939 269488144(269488145(269488146(269488147([]))))
3940 */
3941 0xda, 0x10, 0x10, 0x10, 0x10,
3942 0xda, 0x10, 0x10, 0x10, 0x11,
3943 0xda, 0x10, 0x10, 0x10, 0x12,
3944 0xda, 0x10, 0x10, 0x10, 0x13,
3945 0x80,
3946
3947 /* An invalid decimal fraction with an additional tag */
3948 0xd9, 0xff, 0xfa,
3949 0xd8, 0x02, // non-preferred serialization of tag 2, a big num
3950 0x00, // the integer 0; should be a byte string
3951};
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003952
Laurence Lundblade59289e52019-12-30 13:44:37 -08003953/*
3954 DB 9192939495969798 # tag(10489608748473423768)
Laurence Lundblade9b334962020-08-27 10:55:53 -07003955 80 # array(0)
Laurence Lundblade59289e52019-12-30 13:44:37 -08003956 */
Laurence Lundbladecc7da412020-12-27 00:09:07 -08003957static const uint8_t spEncodedLargeTag[] = {0xdb, 0x91, 0x92, 0x93, 0x94, 0x95,
Laurence Lundbladeee851742020-01-08 08:37:05 -08003958 0x96, 0x97, 0x98, 0x80};
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003959
Laurence Lundblade59289e52019-12-30 13:44:37 -08003960/*
3961DB 9192939495969798 # tag(10489608748473423768)
3962 D8 88 # tag(136)
3963 C6 # tag(6)
3964 C7 # tag(7)
3965 80 # array(0)
3966*/
Laurence Lundbladecc7da412020-12-27 00:09:07 -08003967static const uint8_t spLotsOfTags[] = {0xdb, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96,
Laurence Lundbladeee851742020-01-08 08:37:05 -08003968 0x97, 0x98, 0xd8, 0x88, 0xc6, 0xc7, 0x80};
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003969
3970/*
Laurence Lundblade9b334962020-08-27 10:55:53 -07003971 55799(55799(55799({
3972 6(7(-23)): 5859837686836516696(7({
3973 7(-20): 11({
3974 17(-18): 17(17(17("Organization"))),
3975 9(-17): 773("SSG"),
3976 -15: 16(17(6(7("Confusion")))),
3977 17(-16): 17("San Diego"),
3978 17(-14): 17("US")
3979 }),
3980 23(-19): 19({
3981 -11: 9({
3982 -9: -7
3983 }),
3984 90599561(90599561(90599561(-10))): 12(h'0102030405060708090A')
3985 })
3986 })),
3987 16(-22): 23({
3988 11(8(7(-5))): 8(-3)
3989 })
3990 })))
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003991 */
Laurence Lundbladecc7da412020-12-27 00:09:07 -08003992static const uint8_t spCSRWithTags[] = {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003993 0xd9, 0xd9, 0xf7, 0xd9, 0xd9, 0xf7, 0xd9, 0xd9, 0xf7, 0xa2,
3994 0xc6, 0xc7, 0x36,
3995 0xdb, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0xc7, 0xa2,
3996 0xda, 0x00, 0x00, 0x00, 0x07, 0x33,
3997 0xcb, 0xa5,
3998 0xd1, 0x31,
3999 0xd1, 0xd1, 0xd1, 0x6c,
4000 0x4f, 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74, 0x69, 0x6f, 0x6e,
4001 0xc9, 0x30,
4002 0xd9, 0x03, 0x05, 0x63,
4003 0x53, 0x53, 0x47,
4004 0x2e,
Laurence Lundblade9b334962020-08-27 10:55:53 -07004005 0xd0, 0xd1, 0xc6, 0xc7,
4006 0x69,
4007 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73, 0x69, 0x6f, 0x6e,
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004008 0xd1, 0x2f,
4009 0xd1, 0x69,
4010 0x53, 0x61, 0x6e, 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f,
4011 0xd1, 0x2d,
4012 0xd1, 0x62,
4013 0x55, 0x53,
4014 0xd7, 0x32,
4015 0xd3, 0xa2,
4016 0x2a,
4017 0xc9, 0xa1,
4018 0x28,
4019 0x26,
4020 0xda, 0x05, 0x66, 0x70, 0x89, 0xda, 0x05, 0x66, 0x70, 0x89, 0xda, 0x05, 0x66, 0x70, 0x89, 0x29,
4021 0xcc, 0x4a,
4022 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,0x07, 0x08, 0x09, 0x0a,
4023 0xd0, 0x35,
4024 0xd7, 0xa1,
4025 0xcb, 0xc8, 0xc7, 0x24,
4026 0xc8, 0x22};
4027
Laurence Lundblade9b334962020-08-27 10:55:53 -07004028
Laurence Lundbladecc7da412020-12-27 00:09:07 -08004029static const uint8_t spSpiffyTagInput[] = {
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08004030 0x85, // Open array
Laurence Lundblade9b334962020-08-27 10:55:53 -07004031
4032 0xc0, // tag for string date
4033 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string
4034
4035 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string
4036
4037 0x4a, '1','9','8','5','-','0','4','-','1','2', // Date string in byte string
4038
4039 0xd8, 0x23, // tag for regex
4040 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string
4041
4042 0xc0, // tag for string date
4043 0x4a, '1','9','8','5','-','0','4','-','1','2', // Date string in byte string
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07004044
4045 // This last case makes the array untraversable because it is
4046 // an uncrecoverable error. Make sure it stays last and is the only
4047 // instance so the other tests can work.
Laurence Lundblade9b334962020-08-27 10:55:53 -07004048};
4049
4050
Laurence Lundbladec5fef682020-01-25 11:38:45 -08004051static int32_t CheckCSRMaps(QCBORDecodeContext *pDC);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004052
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004053
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03004054int32_t OptTagParseTest(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004055{
4056 QCBORDecodeContext DCtx;
Laurence Lundblade9b334962020-08-27 10:55:53 -07004057 QCBORItem Item;
4058 QCBORError uError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004059
Laurence Lundbladeee851742020-01-08 08:37:05 -08004060 QCBORDecode_Init(&DCtx,
Laurence Lundblade9b334962020-08-27 10:55:53 -07004061 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTagInput),
Laurence Lundbladeee851742020-01-08 08:37:05 -08004062 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004063
Laurence Lundblade9b334962020-08-27 10:55:53 -07004064 /*
4065 This test matches the magic number tag and the fraction tag
4066 55799([...])
4067 */
4068 uError = QCBORDecode_GetNext(&DCtx, &Item);
4069 if(uError != QCBOR_SUCCESS) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004070 return -2;
4071 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004072 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004073 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC)) {
4074 return -3;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004075 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004076
Laurence Lundblade9b334962020-08-27 10:55:53 -07004077 /*
4078 4([1,3])
4079 */
4080 uError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundbladedd6e76e2021-03-10 01:54:01 -07004081#ifdef QCBOR_DISABLE_EXP_AND_MANTISSA
Laurence Lundblade9b334962020-08-27 10:55:53 -07004082 if(uError != QCBOR_SUCCESS ||
4083 Item.uDataType != QCBOR_TYPE_ARRAY ||
4084 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_DECIMAL_FRACTION) ||
4085 QCBORDecode_GetNthTag(&DCtx, &Item, 0) != CBOR_TAG_DECIMAL_FRACTION ||
4086 QCBORDecode_GetNthTag(&DCtx, &Item, 1) != CBOR_TAG_INVALID64 ||
4087 QCBORDecode_GetNthTag(&DCtx, &Item, 2) != CBOR_TAG_INVALID64 ||
4088 QCBORDecode_GetNthTag(&DCtx, &Item, 3) != CBOR_TAG_INVALID64 ||
4089 QCBORDecode_GetNthTag(&DCtx, &Item, 4) != CBOR_TAG_INVALID64 ||
4090 Item.val.uCount != 2) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004091 return -4;
4092 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07004093 // consume the items in the array
4094 uError = QCBORDecode_GetNext(&DCtx, &Item);
4095 uError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundblade59289e52019-12-30 13:44:37 -08004096
Laurence Lundbladedd6e76e2021-03-10 01:54:01 -07004097#else /* QCBOR_DISABLE_EXP_AND_MANTISSA */
Laurence Lundblade9b334962020-08-27 10:55:53 -07004098 if(uError != QCBOR_SUCCESS ||
4099 Item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION ||
4100 QCBORDecode_GetNthTag(&DCtx, &Item, 0) != CBOR_TAG_INVALID64 ||
4101 QCBORDecode_GetNthTag(&DCtx, &Item, 1) != CBOR_TAG_INVALID64 ||
4102 QCBORDecode_GetNthTag(&DCtx, &Item, 2) != CBOR_TAG_INVALID64 ||
4103 QCBORDecode_GetNthTag(&DCtx, &Item, 3) != CBOR_TAG_INVALID64 ||
4104 QCBORDecode_GetNthTag(&DCtx, &Item, 4) != CBOR_TAG_INVALID64 ) {
4105 return -5;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004106 }
Laurence Lundbladedd6e76e2021-03-10 01:54:01 -07004107#endif /* QCBOR_DISABLE_EXP_AND_MANTISSA */
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004108
Laurence Lundblade9b334962020-08-27 10:55:53 -07004109 /*
4110 More than 4 tags on an item 225(226(227(228(229([])))))
4111 */
4112 uError = QCBORDecode_GetNext(&DCtx, &Item);
4113 if(uError != QCBOR_ERR_TOO_MANY_TAGS) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004114 return -6;
4115 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07004116
Laurence Lundblade88e9db22020-11-02 03:56:33 -08004117 if(QCBORDecode_GetNthTag(&DCtx, &Item, 0) != CBOR_TAG_INVALID64) {
4118 return -106;
4119 }
4120
4121
Laurence Lundblade9b334962020-08-27 10:55:53 -07004122 /* tag 10489608748473423768(
4123 2442302356(
4124 21590(
4125 240(
4126 []))))
4127 */
4128 uError = QCBORDecode_GetNext(&DCtx, &Item);
4129 if(uError != QCBOR_SUCCESS ||
4130 Item.uDataType != QCBOR_TYPE_ARRAY ||
4131 QCBORDecode_GetNthTag(&DCtx, &Item, 3) != 10489608748473423768ULL ||
4132 QCBORDecode_GetNthTag(&DCtx, &Item, 2) != 2442302356ULL ||
4133 QCBORDecode_GetNthTag(&DCtx, &Item, 1) != 21590ULL ||
4134 QCBORDecode_GetNthTag(&DCtx, &Item, 0) != 240ULL) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004135 return -7;
Laurence Lundblade9b334962020-08-27 10:55:53 -07004136 }
4137
4138 /* tag 21590(
4139 10489608748473423768(
4140 2442302357(
4141 21591(
4142 []))))
4143 */
4144 uError = QCBORDecode_GetNext(&DCtx, &Item);
4145 if(uError != QCBOR_SUCCESS ||
4146 Item.uDataType != QCBOR_TYPE_ARRAY ||
4147 QCBORDecode_GetNthTag(&DCtx, &Item, 0) != 65534ULL ||
4148 QCBORDecode_GetNthTag(&DCtx, &Item, 1) != 2442302357ULL ||
4149 QCBORDecode_GetNthTag(&DCtx, &Item, 2) != 10489608748473423768ULL ||
4150 QCBORDecode_GetNthTag(&DCtx, &Item, 3) != 21590ULL) {
4151 return -8;
4152 }
4153
4154 /* Make sure to blow past the limit of tags that must be mapped.
4155 works in conjuntion with entries above.
4156 269488144(269488145(269488146(269488147([]))))
4157 */
4158 uError = QCBORDecode_GetNext(&DCtx, &Item);
4159 if(uError != QCBOR_ERR_TOO_MANY_TAGS) {
4160 return -9;
4161 }
4162
4163 uError = QCBORDecode_GetNext(&DCtx, &Item);
4164 if(uError == QCBOR_SUCCESS) {
4165 return -10;
4166 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004167
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004168 // ----------------------------------
Laurence Lundbladeee851742020-01-08 08:37:05 -08004169 // This test sets up a caller-config list that includes the very large
Laurence Lundblade9b334962020-08-27 10:55:53 -07004170 // tage and then matches it. Caller-config lists are no longer
4171 // used or needed. This tests backwards compatibility with them.
Laurence Lundbladeee851742020-01-08 08:37:05 -08004172 QCBORDecode_Init(&DCtx,
4173 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEncodedLargeTag),
4174 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004175 const uint64_t puList[] = {0x9192939495969798, 257};
4176 const QCBORTagListIn TL = {2, puList};
4177 QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004178
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004179 if(QCBORDecode_GetNext(&DCtx, &Item)) {
4180 return -8;
4181 }
4182 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
4183 !QCBORDecode_IsTagged(&DCtx, &Item, 0x9192939495969798) ||
4184 QCBORDecode_IsTagged(&DCtx, &Item, 257) ||
4185 QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_BIGFLOAT) ||
4186 Item.val.uCount != 0) {
4187 return -9;
4188 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004189
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004190 //------------------------
Laurence Lundbladeee851742020-01-08 08:37:05 -08004191 // Sets up a caller-configured list and look up something not in it
Laurence Lundblade9b334962020-08-27 10:55:53 -07004192 // Another backwards compatibility test.
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004193 const uint64_t puLongList[17] = {1,2,1};
4194 const QCBORTagListIn TLLong = {17, puLongList};
Laurence Lundbladeee851742020-01-08 08:37:05 -08004195 QCBORDecode_Init(&DCtx,
4196 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEncodedLargeTag),
4197 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004198 QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TLLong);
4199 if(QCBORDecode_GetNext(&DCtx, &Item)) {
4200 return -11;
4201 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004202
Laurence Lundbladef7a70bc2020-10-24 12:23:25 -07004203 uint64_t puTags[4];
Laurence Lundblade9b334962020-08-27 10:55:53 -07004204 QCBORTagListOut Out = {0, 4, puTags};
4205
4206
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004207 // This tests retrievel of the full tag list
Laurence Lundbladeee851742020-01-08 08:37:05 -08004208 QCBORDecode_Init(&DCtx,
4209 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spLotsOfTags),
4210 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004211 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
4212 return -12;
4213 }
4214 if(puTags[0] != 0x9192939495969798 ||
4215 puTags[1] != 0x88 ||
Laurence Lundblade59289e52019-12-30 13:44:37 -08004216 puTags[2] != 0x06 ||
4217 puTags[3] != 0x07) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004218 return -13;
4219 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004220
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004221 // ----------------------
Laurence Lundblade9b334962020-08-27 10:55:53 -07004222 // This tests too small of an out list
Laurence Lundbladeee851742020-01-08 08:37:05 -08004223 QCBORDecode_Init(&DCtx,
4224 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spLotsOfTags),
4225 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004226 QCBORTagListOut OutSmall = {0, 3, puTags};
4227 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &OutSmall) != QCBOR_ERR_TOO_MANY_TAGS) {
4228 return -14;
4229 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004230
Laurence Lundblade9b334962020-08-27 10:55:53 -07004231
4232
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004233 // ---------------
Laurence Lundblade9b334962020-08-27 10:55:53 -07004234 // Decode a version of the "CSR" that has had a ton of tags randomly inserted
4235 // It is a bit of a messy test and maybe could be improved, but
4236 // it is retained as a backwards compatibility check.
Laurence Lundbladeee851742020-01-08 08:37:05 -08004237 QCBORDecode_Init(&DCtx,
4238 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRWithTags),
4239 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004240 int n = CheckCSRMaps(&DCtx);
4241 if(n) {
4242 return n-2000;
4243 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004244
Laurence Lundblade59289e52019-12-30 13:44:37 -08004245 Out = (QCBORTagListOut){0, 16, puTags};
Laurence Lundbladeee851742020-01-08 08:37:05 -08004246 QCBORDecode_Init(&DCtx,
4247 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRWithTags),
4248 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004249
Laurence Lundblade9b334962020-08-27 10:55:53 -07004250 /* With the spiffy decode revision, this tag list is not used.
4251 It doesn't matter if a tag is in this list or not so some
4252 tests that couldn't process a tag because it isn't in this list
4253 now can process these unlisted tags. The tests have been
4254 adjusted for this. */
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004255 const uint64_t puTagList[] = {773, 1, 90599561};
4256 const QCBORTagListIn TagList = {3, puTagList};
4257 QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TagList);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004258
4259
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004260 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
4261 return -100;
4262 }
4263 if(Item.uDataType != QCBOR_TYPE_MAP ||
4264 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC) ||
4265 QCBORDecode_IsTagged(&DCtx, &Item, 90599561) ||
4266 QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_DATE_EPOCH) ||
4267 Item.val.uCount != 2 ||
4268 puTags[0] != CBOR_TAG_CBOR_MAGIC ||
4269 puTags[1] != CBOR_TAG_CBOR_MAGIC ||
4270 puTags[2] != CBOR_TAG_CBOR_MAGIC ||
4271 Out.uNumUsed != 3) {
4272 return -101;
4273 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004274
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004275 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
4276 return -102;
4277 }
4278 if(Item.uDataType != QCBOR_TYPE_MAP ||
4279 QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC) ||
4280 QCBORDecode_IsTagged(&DCtx, &Item, 6) ||
Laurence Lundbladef7a70bc2020-10-24 12:23:25 -07004281 !QCBORDecode_IsTagged(&DCtx, &Item, 7) ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004282 Item.val.uCount != 2 ||
4283 puTags[0] != 5859837686836516696 ||
4284 puTags[1] != 7 ||
4285 Out.uNumUsed != 2) {
4286 return -103;
4287 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004288
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004289 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
4290 return -104;
4291 }
4292 if(Item.uDataType != QCBOR_TYPE_MAP ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004293 Item.val.uCount != 5 ||
4294 puTags[0] != 0x0b ||
4295 Out.uNumUsed != 1) {
4296 return -105;
4297 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004298
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004299 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
4300 return -106;
4301 }
4302 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
4303 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_COSE_MAC0) ||
4304 Item.val.string.len != 12 ||
4305 puTags[0] != CBOR_TAG_COSE_MAC0 ||
4306 puTags[1] != CBOR_TAG_COSE_MAC0 ||
4307 puTags[2] != CBOR_TAG_COSE_MAC0 ||
4308 Out.uNumUsed != 3) {
4309 return -105;
4310 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004311
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004312 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
4313 return -107;
4314 }
4315 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
4316 !QCBORDecode_IsTagged(&DCtx, &Item, 773) ||
4317 Item.val.string.len != 3 ||
4318 puTags[0] != 773 ||
4319 Out.uNumUsed != 1) {
4320 return -108;
4321 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004322
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004323 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
4324 return -109;
4325 }
4326 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade59289e52019-12-30 13:44:37 -08004327 !QCBORDecode_IsTagged(&DCtx, &Item, 16) ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004328 Item.val.string.len != 9 ||
Laurence Lundblade59289e52019-12-30 13:44:37 -08004329 puTags[0] != 16 ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07004330 puTags[3] != 7 ||
4331 Out.uNumUsed != 4) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004332 return -110;
4333 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004334
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004335 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
4336 return -111;
4337 }
4338 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
4339 !QCBORDecode_IsTagged(&DCtx, &Item, 17) ||
4340 Item.val.string.len != 9 ||
4341 puTags[0] != 17 ||
4342 Out.uNumUsed != 1) {
4343 return -112;
4344 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004345
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004346 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
4347 return -111;
4348 }
4349 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
4350 !QCBORDecode_IsTagged(&DCtx, &Item, 17) ||
4351 Item.val.string.len != 2 ||
4352 puTags[0] != 17 ||
4353 Out.uNumUsed != 1) {
4354 return -112;
4355 }
4356
4357 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
4358 return -113;
4359 }
4360 if(Item.uDataType != QCBOR_TYPE_MAP ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07004361 !QCBORDecode_IsTagged(&DCtx, &Item, 19) ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004362 Item.val.uCount != 2 ||
4363 puTags[0] != 19 ||
4364 Out.uNumUsed != 1) {
4365 return -114;
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 -115;
4370 }
4371 if(Item.uDataType != QCBOR_TYPE_MAP ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07004372 !QCBORDecode_IsTagged(&DCtx, &Item, 9) ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004373 Item.val.uCount != 1 ||
4374 puTags[0] != 9 ||
4375 Out.uNumUsed != 1) {
4376 return -116;
4377 }
4378
4379 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
4380 return -116;
4381 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004382 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004383 Item.val.int64 != -7 ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004384 Out.uNumUsed != 0) {
4385 return -117;
4386 }
4387
4388 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
4389 return -118;
4390 }
4391 if(Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
4392 Item.val.string.len != 10 ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004393 puTags[0] != 12 ||
4394 Out.uNumUsed != 1) {
4395 return -119;
4396 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004397
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004398 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
4399 return -120;
4400 }
4401 if(Item.uDataType != QCBOR_TYPE_MAP ||
4402 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_ENC_AS_B16) ||
4403 Item.val.uCount != 1 ||
4404 puTags[0] != 0x17 ||
4405 Out.uNumUsed != 1) {
4406 return -121;
4407 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004408
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004409 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
4410 return -122;
4411 }
4412 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07004413 !QCBORDecode_IsTagged(&DCtx, &Item, 8) ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004414 Item.val.int64 != -3 ||
4415 puTags[0] != 8 ||
4416 Out.uNumUsed != 1) {
4417 return -123;
4418 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004419
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004420 if(QCBORDecode_Finish(&DCtx)) {
4421 return -124;
4422 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07004423
4424 UsefulBufC DateString;
4425 QCBORDecode_Init(&DCtx,
4426 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSpiffyTagInput),
4427 QCBOR_DECODE_MODE_NORMAL);
4428
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07004429 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade9b334962020-08-27 10:55:53 -07004430 // tagged date string
4431 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString);
4432 // untagged date string
4433 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString);
4434 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_SUCCESS) {
4435 return 100;
4436 }
4437 // untagged byte string
4438 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString);
4439 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
4440 return 101;
4441 }
4442 // tagged regex
4443 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString);
4444 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
4445 return 102;
4446 }
4447 // tagged date string with a byte string
4448 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString);
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07004449 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT) {
Laurence Lundblade9b334962020-08-27 10:55:53 -07004450 return 103;
4451 }
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07004452 // The exit errors out because the last item, the date string with
4453 // bad content makes the array untraversable (the bad date string
4454 // could have tag content of an array or such that is not consumed
4455 // by the date decoding).
Laurence Lundblade9b334962020-08-27 10:55:53 -07004456 QCBORDecode_ExitArray(&DCtx);
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07004457 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT) {
Laurence Lundblade9b334962020-08-27 10:55:53 -07004458 return 104;
4459 }
4460
4461
4462 QCBORDecode_Init(&DCtx,
4463 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSpiffyTagInput),
4464 QCBOR_DECODE_MODE_NORMAL);
4465
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07004466 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade9b334962020-08-27 10:55:53 -07004467 // tagged date string
4468 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString);
4469 // untagged date string
4470 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString);
4471 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_SUCCESS) {
4472 return 200;
4473 }
4474 // untagged byte string
4475 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString);
4476 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
4477 return 201;
4478 }
4479 // tagged regex
4480 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString);
4481 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
4482 return 202;
4483 }
4484 // tagged date string with a byte string
4485 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString);
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07004486 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT) {
Laurence Lundblade9b334962020-08-27 10:55:53 -07004487 return 203;
4488 }
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07004489 // See comments above
Laurence Lundblade9b334962020-08-27 10:55:53 -07004490 QCBORDecode_ExitArray(&DCtx);
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07004491 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT) {
Laurence Lundblade9b334962020-08-27 10:55:53 -07004492 return 204;
4493 }
4494
4495 QCBORDecode_Init(&DCtx,
4496 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSpiffyTagInput),
4497 QCBOR_DECODE_MODE_NORMAL);
4498
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07004499 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade9b334962020-08-27 10:55:53 -07004500 // tagged date string
4501 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString);
4502 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
4503 return 300;
4504 }
4505 // untagged date string
4506 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString);
4507 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
4508 return 301;
4509 }
4510 // untagged byte string
4511 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString);
4512 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
4513 return 302;
4514 }
4515 // tagged regex
4516 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString);
4517 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
4518 return 303;
4519 }
4520 // tagged date string with a byte string
4521 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString);
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07004522 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT) {
Laurence Lundblade9b334962020-08-27 10:55:53 -07004523 return 304;
4524 }
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07004525 // See comments above
Laurence Lundblade9b334962020-08-27 10:55:53 -07004526 QCBORDecode_ExitArray(&DCtx);
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07004527 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT) {
Laurence Lundblade9b334962020-08-27 10:55:53 -07004528 return 305;
4529 }
4530
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004531 return 0;
4532}
4533
Laurence Lundblade37286c02022-09-03 10:05:02 -07004534/*
4535 * These are showing the big numbers converted to integers.
4536 * The tag numbers are not shown.
4537 *
Laurence Lundbladeec290b82024-06-10 11:10:54 -07004538 * [
4539 * 18446744073709551616,
4540 * -18446744073709551617,
4541 * {
4542 * -64: -18446744073709551617,
4543 * 64: 18446744073709551616,
4544 * "BN+": 18446744073709551616,
4545 * "BN-": -18446744073709551617
Laurence Lundblade37286c02022-09-03 10:05:02 -07004546 * }
4547 * ]
4548 */
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004549
Laurence Lundbladecc7da412020-12-27 00:09:07 -08004550static const uint8_t spBigNumInput[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004551 0x83,
4552 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
4553 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
4554 0xA4,
Laurence Lundbladeec290b82024-06-10 11:10:54 -07004555 0x38, 0x3F,
4556 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004557 0x18, 0x40,
4558 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
Laurence Lundbladeec290b82024-06-10 11:10:54 -07004559 0x63, 0x42, 0x4E, 0x2B,
4560 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
4561 0x63, 0x42, 0x4E, 0x2D,
4562 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
4563};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004564
Laurence Lundblade37286c02022-09-03 10:05:02 -07004565#ifndef QCBOR_DISABLE_TAGS
Laurence Lundbladecc7da412020-12-27 00:09:07 -08004566/* The expected big num */
4567static const uint8_t spBigNum[] = {
4568 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
4569 0x00};
Laurence Lundblade37286c02022-09-03 10:05:02 -07004570#endif /* QCBOR_DISABLE_TAGS */
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004571
4572
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03004573int32_t BignumParseTest(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004574{
4575 QCBORDecodeContext DCtx;
4576 QCBORItem Item;
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07004577 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004578
Laurence Lundbladeee851742020-01-08 08:37:05 -08004579 QCBORDecode_Init(&DCtx,
4580 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNumInput),
4581 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004582
4583
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004584 //
4585 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
4586 return -1;
4587 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade830fbf92020-05-31 17:22:33 -07004588 return -2;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004589 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004590
Laurence Lundblade37286c02022-09-03 10:05:02 -07004591#ifndef QCBOR_DISABLE_TAGS
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004592 //
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004593 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07004594 return -3;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004595 if(Item.uDataType != QCBOR_TYPE_POSBIGNUM ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07004596 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07004597 return -4;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004598 }
4599
4600 //
4601 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07004602 return -5;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004603 if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07004604 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07004605 return -6;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004606 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004607
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004608 //
4609 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07004610 return -7;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004611 if(Item.uDataType != QCBOR_TYPE_MAP) {
Laurence Lundblade830fbf92020-05-31 17:22:33 -07004612 return -8;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004613 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004614
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004615 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundbladeec290b82024-06-10 11:10:54 -07004616 return -15;
4617 if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM ||
4618 Item.uLabelType != QCBOR_TYPE_INT64 ||
4619 Item.label.int64 != -64 ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07004620 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundbladeec290b82024-06-10 11:10:54 -07004621 return -16;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004622 }
4623
4624 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07004625 return -11;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004626 if(Item.uDataType != QCBOR_TYPE_POSBIGNUM ||
4627 Item.uLabelType != QCBOR_TYPE_INT64 ||
4628 Item.label.int64 != 64 ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07004629 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07004630 return -12;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004631 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004632
Laurence Lundbladeec290b82024-06-10 11:10:54 -07004633#ifndef QCBOR_DISABLE_NON_INTEGER_LABELS
4634 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
4635 return -9;
4636 if(Item.uDataType != QCBOR_TYPE_POSBIGNUM ||
4637 Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
4638 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
4639 return -10;
4640 }
4641
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004642 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07004643 return -13;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004644 if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM ||
4645 Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07004646 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07004647 return -14;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004648 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004649
Laurence Lundbladeec290b82024-06-10 11:10:54 -07004650
4651#endif /* ! QCBOR_DISABLE_NON_INTEGER_LABELS */
4652
Laurence Lundblade37286c02022-09-03 10:05:02 -07004653#else
4654
4655 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_TAGS_DISABLED) {
4656 return -100;
4657 }
4658#endif /* QCBOR_DISABLE_TAGS */
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004659
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004660 return 0;
4661}
4662
4663
Laurence Lundbladec5fef682020-01-25 11:38:45 -08004664static int32_t CheckItemWithIntLabel(QCBORDecodeContext *pCtx,
Laurence Lundbladeee851742020-01-08 08:37:05 -08004665 uint8_t uDataType,
4666 uint8_t uNestingLevel,
4667 uint8_t uNextNest,
4668 int64_t nLabel,
4669 QCBORItem *pItem)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004670{
4671 QCBORItem Item;
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07004672 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004673
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004674 if((nCBORError = QCBORDecode_GetNext(pCtx, &Item))) return -1;
4675 if(Item.uDataType != uDataType) return -1;
4676 if(uNestingLevel > 0) {
Laurence Lundbladeec290b82024-06-10 11:10:54 -07004677 if(Item.uLabelType != QCBOR_TYPE_INT64) {
Laurence Lundbladeee851742020-01-08 08:37:05 -08004678 return -1;
4679 }
Laurence Lundbladeec290b82024-06-10 11:10:54 -07004680 if(Item.label.int64 != nLabel) {
4681 return -1;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004682 }
Laurence Lundbladeec290b82024-06-10 11:10:54 -07004683
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004684 }
4685 if(Item.uNestingLevel != uNestingLevel) return -1;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304686 if(Item.uNextNestLevel != uNextNest) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004687
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004688 if(pItem) {
4689 *pItem = Item;
4690 }
4691 return 0;
4692}
4693
Laurence Lundblade742df4a2018-10-13 20:07:17 +08004694// Same code checks definite and indefinite length versions of the map
Laurence Lundbladec5fef682020-01-25 11:38:45 -08004695static int32_t CheckCSRMaps(QCBORDecodeContext *pDC)
Laurence Lundblade742df4a2018-10-13 20:07:17 +08004696{
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304697 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 0, 1, 0, NULL)) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004698
Laurence Lundblade9b334962020-08-27 10:55:53 -07004699 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 1, 2, -23, NULL)) return -2;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004700
Laurence Lundblade9b334962020-08-27 10:55:53 -07004701 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 2, 3, -20, NULL)) return -3;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004702
Laurence Lundblade9b334962020-08-27 10:55:53 -07004703 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -18, NULL)) return -4;
4704 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -17, NULL)) return -5;
4705 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -15, NULL)) return -6;
4706 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -16, NULL)) return -7;
4707 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 2, -14, NULL)) return -8;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004708
Laurence Lundblade9b334962020-08-27 10:55:53 -07004709 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 2, 3, -19, NULL)) return -9;
4710 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 3, 4, -11, NULL)) return -10;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004711
Laurence Lundblade9b334962020-08-27 10:55:53 -07004712 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_INT64, 4, 3, -9, NULL)) return -11;
4713 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_BYTE_STRING, 3, 1, -10, NULL)) return -12;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004714
Laurence Lundblade9b334962020-08-27 10:55:53 -07004715 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 1, 2, -22, NULL)) return -13;
4716 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_INT64, 2, 0, -5, NULL)) return -14;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004717
Laurence Lundblade9b334962020-08-27 10:55:53 -07004718 if(QCBORDecode_Finish(pDC)) return -20;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004719
Laurence Lundblade742df4a2018-10-13 20:07:17 +08004720 return 0;
4721}
4722
4723
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004724/*
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004725{
4726 -23: {
4727 -20: {
4728 -18: "Organization",
4729 -17: "SSG",
4730 -15: "Confusion",
4731 -16: "San Diego",
4732 -14: "US"
4733 },
4734 -19: {
4735 -11: {
4736 -9: -7
4737 },
4738 -10: '\u0001\u0002\u0003\u0004\u0005\u0006\a\b\t\n'
4739 }
4740 },
4741 -22: {
4742 -5: -3
4743 }
4744}
Laurence Lundbladecc7da412020-12-27 00:09:07 -08004745*/
4746static const uint8_t spCSRInput[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004747 0xa2, 0x36, 0xa2, 0x33, 0xa5, 0x31, 0x6c, 0x4f,
4748 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74,
4749 0x69, 0x6f, 0x6e, 0x30, 0x63, 0x53, 0x53, 0x47,
4750 0x2e, 0x69, 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73,
4751 0x69, 0x6f, 0x6e, 0x2f, 0x69, 0x53, 0x61, 0x6e,
4752 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f, 0x2d, 0x62,
4753 0x55, 0x53, 0x32, 0xa2, 0x2a, 0xa1, 0x28, 0x26,
4754 0x29, 0x4a, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
4755 0x07, 0x08, 0x09, 0x0a, 0x35, 0xa1, 0x24, 0x22};
4756
Laurence Lundbladecc7da412020-12-27 00:09:07 -08004757// Same map as above, but using indefinite lengths
4758static const uint8_t spCSRInputIndefLen[] = {
4759 0xbf, 0x36, 0xbf, 0x33, 0xbf, 0x31, 0x6c, 0x4f,
4760 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74,
4761 0x69, 0x6f, 0x6e, 0x30, 0x63, 0x53, 0x53, 0x47,
4762 0x2e, 0x69, 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73,
4763 0x69, 0x6f, 0x6e, 0x2f, 0x69, 0x53, 0x61, 0x6e,
4764 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f, 0x2d, 0x62,
4765 0x55, 0x53, 0xff, 0x32, 0xbf, 0x2a, 0xbf, 0x28,
4766 0x26, 0xff, 0x29, 0x4a, 0x01, 0x02, 0x03, 0x04,
4767 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0xff, 0xff,
4768 0x35, 0xbf, 0x24, 0x22, 0xff, 0xff};
4769
4770
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03004771int32_t NestedMapTest(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004772{
4773 QCBORDecodeContext DCtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004774
Laurence Lundbladeee851742020-01-08 08:37:05 -08004775 QCBORDecode_Init(&DCtx,
4776 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput),
4777 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004778
Laurence Lundblade742df4a2018-10-13 20:07:17 +08004779 return CheckCSRMaps(&DCtx);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004780}
4781
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08004782
4783
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03004784int32_t StringDecoderModeFailTest(void)
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08004785{
4786 QCBORDecodeContext DCtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004787
Laurence Lundbladeee851742020-01-08 08:37:05 -08004788 QCBORDecode_Init(&DCtx,
4789 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput),
4790 QCBOR_DECODE_MODE_MAP_STRINGS_ONLY);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004791
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08004792 QCBORItem Item;
4793 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004794
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08004795 if(QCBORDecode_GetNext(&DCtx, &Item)) {
4796 return -1;
4797 }
4798 if(Item.uDataType != QCBOR_TYPE_MAP) {
4799 return -2;
4800 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004801
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08004802 nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
4803 if(nCBORError != QCBOR_ERR_MAP_LABEL_TYPE) {
4804 return -3;
4805 }
4806
4807 return 0;
4808}
4809
4810
Laurence Lundblade742df4a2018-10-13 20:07:17 +08004811
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03004812int32_t NestedMapTestIndefLen(void)
Laurence Lundblade742df4a2018-10-13 20:07:17 +08004813{
4814 QCBORDecodeContext DCtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004815
Laurence Lundbladeee851742020-01-08 08:37:05 -08004816 QCBORDecode_Init(&DCtx,
4817 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInputIndefLen),
4818 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004819
Laurence Lundblade742df4a2018-10-13 20:07:17 +08004820 return CheckCSRMaps(&DCtx);
4821}
4822
4823
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004824
Laurence Lundblade17ede402018-10-13 11:43:07 +08004825static UsefulBufC make_nested_indefinite_arrays(int n, UsefulBuf Storage)
4826{
4827 UsefulOutBuf UOB;
4828 UsefulOutBuf_Init(&UOB, Storage);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004829
Laurence Lundblade17ede402018-10-13 11:43:07 +08004830 int i;
4831 for(i = 0; i < n; i++) {
4832 UsefulOutBuf_AppendByte(&UOB, 0x9f);
4833 }
4834
4835 for(i = 0; i < n; i++) {
4836 UsefulOutBuf_AppendByte(&UOB, 0xff);
4837 }
4838 return UsefulOutBuf_OutUBuf(&UOB);
4839}
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08004840
4841
Laurence Lundbladec5fef682020-01-25 11:38:45 -08004842static int32_t parse_indeflen_nested(UsefulBufC Nested, int nNestLevel)
Laurence Lundblade17ede402018-10-13 11:43:07 +08004843{
4844 QCBORDecodeContext DC;
4845 QCBORDecode_Init(&DC, Nested, 0);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004846
Laurence Lundblade17ede402018-10-13 11:43:07 +08004847 int j;
4848 for(j = 0; j < nNestLevel; j++) {
4849 QCBORItem Item;
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07004850 QCBORError nReturn = QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade17ede402018-10-13 11:43:07 +08004851 if(j >= QCBOR_MAX_ARRAY_NESTING) {
4852 // Should be in error
Laurence Lundbladea9489f82020-09-12 13:50:56 -07004853 if(nReturn != QCBOR_ERR_ARRAY_DECODE_NESTING_TOO_DEEP) {
Laurence Lundblade17ede402018-10-13 11:43:07 +08004854 return -4;
4855 } else {
4856 return 0; // Decoding doesn't recover after an error
4857 }
4858 } else {
4859 // Should be no error
4860 if(nReturn) {
4861 return -9; // Should not have got an error
4862 }
4863 }
4864 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
4865 return -7;
4866 }
4867 }
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07004868 QCBORError nReturn = QCBORDecode_Finish(&DC);
Laurence Lundblade17ede402018-10-13 11:43:07 +08004869 if(nReturn) {
4870 return -3;
4871 }
4872 return 0;
4873}
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08004874
4875
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03004876int32_t IndefiniteLengthNestTest(void)
Laurence Lundblade17ede402018-10-13 11:43:07 +08004877{
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05304878 UsefulBuf_MAKE_STACK_UB(Storage, 50);
Laurence Lundblade17ede402018-10-13 11:43:07 +08004879 int i;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004880 for(i=1; i < QCBOR_MAX_ARRAY_NESTING+4; i++) {
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08004881 const UsefulBufC Nested = make_nested_indefinite_arrays(i, Storage);
Laurence Lundblade17ede402018-10-13 11:43:07 +08004882 int nReturn = parse_indeflen_nested(Nested, i);
4883 if(nReturn) {
4884 return nReturn;
4885 }
4886 }
4887 return 0;
4888}
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08004889
Laurence Lundbladeee851742020-01-08 08:37:05 -08004890// [1, [2, 3]]
4891static const uint8_t spIndefiniteArray[] = {0x9f, 0x01, 0x82, 0x02, 0x03, 0xff};
4892// No closing break
4893static const uint8_t spIndefiniteArrayBad1[] = {0x9f};
4894// Not enough closing breaks
4895static const uint8_t spIndefiniteArrayBad2[] = {0x9f, 0x9f, 0x02, 0xff};
4896// Too many closing breaks
4897static const uint8_t spIndefiniteArrayBad3[] = {0x9f, 0x02, 0xff, 0xff};
4898// Unclosed indeflen inside def len
4899static const uint8_t spIndefiniteArrayBad4[] = {0x81, 0x9f};
4900// confused tag
4901static const uint8_t spIndefiniteArrayBad5[] = {0x9f, 0xd1, 0xff};
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08004902
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03004903int32_t IndefiniteLengthArrayMapTest(void)
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08004904{
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07004905 QCBORError nResult;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08004906 // --- first test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07004907 UsefulBufC IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArray);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004908
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08004909 // Decode it and see if it is OK
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08004910 QCBORDecodeContext DC;
4911 QCBORItem Item;
4912 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004913
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08004914 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05304915
4916 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
4917 Item.uNestingLevel != 0 ||
4918 Item.uNextNestLevel != 1) {
4919 return -111;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08004920 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004921
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08004922 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05304923 if(Item.uDataType != QCBOR_TYPE_INT64 ||
4924 Item.uNestingLevel != 1 ||
4925 Item.uNextNestLevel != 1) {
4926 return -2;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08004927 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004928
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08004929 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05304930 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
4931 Item.uNestingLevel != 1 ||
4932 Item.uNextNestLevel != 2) {
4933 return -3;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08004934 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004935
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08004936 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade12b495d2018-12-17 11:15:54 -08004937 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade6de37062018-10-15 12:22:42 +05304938 Item.uNestingLevel != 2 ||
4939 Item.uNextNestLevel != 2) {
4940 return -4;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08004941 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004942
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08004943 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade12b495d2018-12-17 11:15:54 -08004944 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade6de37062018-10-15 12:22:42 +05304945 Item.uNestingLevel != 2 ||
4946 Item.uNextNestLevel != 0) {
4947 return -5;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08004948 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004949
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08004950 if(QCBORDecode_Finish(&DC)) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05304951 return -6;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08004952 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004953
Laurence Lundblade19e0c802018-10-13 12:19:55 +08004954 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07004955 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad1);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004956
Laurence Lundblade19e0c802018-10-13 12:19:55 +08004957 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004958
Laurence Lundblade19e0c802018-10-13 12:19:55 +08004959 nResult = QCBORDecode_GetNext(&DC, &Item);
4960 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05304961 return -7;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08004962 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004963
Laurence Lundblade570fab52018-10-13 18:28:27 +08004964 nResult = QCBORDecode_Finish(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07004965 if(nResult != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05304966 return -8;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08004967 }
4968
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004969
Laurence Lundblade19e0c802018-10-13 12:19:55 +08004970 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07004971 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad2);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004972
Laurence Lundblade19e0c802018-10-13 12:19:55 +08004973 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004974
Laurence Lundblade19e0c802018-10-13 12:19:55 +08004975 nResult = QCBORDecode_GetNext(&DC, &Item);
4976 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05304977 return -9;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08004978 }
4979
4980 nResult = QCBORDecode_GetNext(&DC, &Item);
4981 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05304982 return -10;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08004983 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004984
Laurence Lundblade19e0c802018-10-13 12:19:55 +08004985 nResult = QCBORDecode_GetNext(&DC, &Item);
4986 if(nResult || Item.uDataType != QCBOR_TYPE_INT64) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05304987 return -11;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08004988 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004989
Laurence Lundblade19e0c802018-10-13 12:19:55 +08004990 nResult = QCBORDecode_Finish(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07004991 if(nResult != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05304992 return -12;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08004993 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004994
4995
Laurence Lundblade19e0c802018-10-13 12:19:55 +08004996 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07004997 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad3);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004998
Laurence Lundblade19e0c802018-10-13 12:19:55 +08004999 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005000
Laurence Lundblade19e0c802018-10-13 12:19:55 +08005001 nResult = QCBORDecode_GetNext(&DC, &Item);
5002 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05305003 return -13;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08005004 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005005
Laurence Lundblade19e0c802018-10-13 12:19:55 +08005006 nResult = QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade642282a2020-06-23 12:00:33 -07005007 if(nResult != QCBOR_SUCCESS) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05305008 return -14;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08005009 }
Laurence Lundblade6de37062018-10-15 12:22:42 +05305010
Laurence Lundblade642282a2020-06-23 12:00:33 -07005011 nResult = QCBORDecode_GetNext(&DC, &Item);
5012 if(nResult != QCBOR_ERR_BAD_BREAK) {
5013 return -140;
5014 }
5015
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005016
Laurence Lundblade570fab52018-10-13 18:28:27 +08005017 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07005018 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad4);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005019
Laurence Lundblade570fab52018-10-13 18:28:27 +08005020 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005021
Laurence Lundblade570fab52018-10-13 18:28:27 +08005022 nResult = QCBORDecode_GetNext(&DC, &Item);
5023 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05305024 return -15;
Laurence Lundblade570fab52018-10-13 18:28:27 +08005025 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005026
Laurence Lundblade570fab52018-10-13 18:28:27 +08005027 nResult = QCBORDecode_GetNext(&DC, &Item);
5028 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05305029 return -16;
Laurence Lundblade570fab52018-10-13 18:28:27 +08005030 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005031
Laurence Lundblade570fab52018-10-13 18:28:27 +08005032 nResult = QCBORDecode_Finish(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07005033 if(nResult != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05305034 return -17;
Laurence Lundblade570fab52018-10-13 18:28:27 +08005035 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005036
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05305037 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07005038 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad5);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005039
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05305040 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005041
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05305042 nResult = QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade37286c02022-09-03 10:05:02 -07005043
5044#ifndef QCBOR_DISABLE_TAGS
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05305045 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05305046 return -18;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05305047 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005048
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05305049 nResult = QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05305050 if(nResult != QCBOR_ERR_BAD_BREAK) {
5051 return -19;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05305052 }
Laurence Lundblade37286c02022-09-03 10:05:02 -07005053#else /* QCBOR_DISABLE_TAGS */
5054 if(nResult != QCBOR_ERR_TAGS_DISABLED) {
5055 return -20;
5056 }
5057#endif /* QCBOR_DISABLE_TAGS */
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005058
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08005059 return 0;
5060}
5061
Laurence Lundblade17ede402018-10-13 11:43:07 +08005062
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08005063#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
5064
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07005065static const uint8_t spIndefiniteLenString[] = {
Laurence Lundblade17ede402018-10-13 11:43:07 +08005066 0x81, // Array of length one
5067 0x7f, // text string marked with indefinite length
5068 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment
5069 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment
5070 0xff // ending break
5071};
5072
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07005073static const uint8_t spIndefiniteLenStringBad2[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305074 0x81, // Array of length one
5075 0x7f, // text string marked with indefinite length
5076 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment
5077 0x44, 0x6d, 0x69, 0x6e, 0x67, // second segment of wrong type
5078 0xff // ending break
5079};
5080
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07005081static const uint8_t spIndefiniteLenStringBad3[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305082 0x81, // Array of length one
5083 0x7f, // text string marked with indefinite length
5084 0x01, 0x02, // Not a string
5085 0xff // ending break
5086};
5087
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07005088static const uint8_t spIndefiniteLenStringBad4[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305089 0x81, // Array of length one
5090 0x7f, // text string marked with indefinite length
5091 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment
5092 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment
5093 // missing end of string
5094};
5095
Laurence Lundbladeec290b82024-06-10 11:10:54 -07005096#ifndef QCBOR_DISABLE_NON_INTEGER_LABELS
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07005097static const uint8_t spIndefiniteLenStringLabel[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305098 0xa1, // Array of length one
5099 0x7f, // text string marked with indefinite length
5100 0x65, 0x73, 0x74, 0x72, 0x75, 0x75, // first segment
5101 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment
5102 0xff, // ending break
5103 0x01 // integer being labeled.
5104};
Laurence Lundbladeec290b82024-06-10 11:10:54 -07005105#endif /* ! QCBOR_DISABLE_NON_INTEGER_LABELS */
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305106
Laurence Lundbladec5fef682020-01-25 11:38:45 -08005107/**
5108 Make an indefinite length string
5109
5110 @param Storage Storage for string, must be 144 bytes in size
5111 @return The indefinite length string
5112
5113 This makes an array with one indefinite length string that has 7 chunks
5114 from size of 1 byte up to 64 bytes.
5115 */
5116static UsefulBufC MakeIndefiniteBigBstr(UsefulBuf Storage)
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305117{
5118 UsefulOutBuf UOB;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005119
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305120 UsefulOutBuf_Init(&UOB, Storage);
5121 UsefulOutBuf_AppendByte(&UOB, 0x81);
5122 UsefulOutBuf_AppendByte(&UOB, 0x5f);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005123
Laurence Lundbladec5fef682020-01-25 11:38:45 -08005124 uint8_t uStringByte = 0;
5125 // Use of type int is intentional
5126 for(int uChunkSize = 1; uChunkSize <= 128; uChunkSize *= 2) {
5127 // Not using preferred encoding here, but that is OK.
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305128 UsefulOutBuf_AppendByte(&UOB, 0x58);
Laurence Lundbladec5fef682020-01-25 11:38:45 -08005129 UsefulOutBuf_AppendByte(&UOB, (uint8_t)uChunkSize);
5130 for(int j = 0; j < uChunkSize; j++) {
5131 UsefulOutBuf_AppendByte(&UOB, uStringByte);
5132 uStringByte++;
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305133 }
5134 }
5135 UsefulOutBuf_AppendByte(&UOB, 0xff);
5136
5137 return UsefulOutBuf_OutUBuf(&UOB);
5138}
5139
5140static int CheckBigString(UsefulBufC BigString)
5141{
5142 if(BigString.len != 255) {
5143 return 1;
5144 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005145
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305146 for(uint8_t i = 0; i < 255; i++){
5147 if(((const uint8_t *)BigString.ptr)[i] != i) {
5148 return 1;
5149 }
5150 }
5151 return 0;
5152}
5153
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05305154
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03005155int32_t IndefiniteLengthStringTest(void)
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05305156{
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305157 QCBORDecodeContext DC;
5158 QCBORItem Item;
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05305159 // big enough for MakeIndefiniteBigBstr() + MemPool overhead
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08005160 UsefulBuf_MAKE_STACK_UB(MemPool, 350);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005161
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305162 // --- Simple normal indefinite length string ------
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07005163 UsefulBufC IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenString);
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305164 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005165
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05305166 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305167 return -1;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05305168 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005169
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305170 if(QCBORDecode_GetNext(&DC, &Item)) {
5171 return -2;
5172 }
5173 if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.uDataAlloc) {
5174 return -3;
5175 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005176
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305177 if(QCBORDecode_GetNext(&DC, &Item)) {
5178 return -4;
5179 }
5180 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || !Item.uDataAlloc) {
5181 return -5;
5182 }
5183 if(QCBORDecode_Finish(&DC)) {
5184 return -6;
5185 }
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05305186
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305187 // ----- types mismatch ---
Laurence Lundbladeee851742020-01-08 08:37:05 -08005188 QCBORDecode_Init(&DC,
5189 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad2),
5190 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005191
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305192 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
5193 return -7;
5194 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005195
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305196 if(QCBORDecode_GetNext(&DC, &Item)) {
5197 return -8;
5198 }
5199 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
5200 return -9;
5201 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005202
Laurence Lundblade30816f22018-11-10 13:40:22 +07005203 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_INDEFINITE_STRING_CHUNK) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305204 return -10;
5205 }
5206
5207 // ----- not a string ---
Laurence Lundbladeee851742020-01-08 08:37:05 -08005208 QCBORDecode_Init(&DC,
5209 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad3),
5210 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005211
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305212 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
5213 return -11;
5214 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005215
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305216 if(QCBORDecode_GetNext(&DC, &Item)) {
5217 return -12;
5218 }
5219 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
5220 return -13;
5221 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005222
Laurence Lundblade30816f22018-11-10 13:40:22 +07005223 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_INDEFINITE_STRING_CHUNK) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305224 return -14;
5225 }
5226
5227 // ----- no end -----
Laurence Lundbladeee851742020-01-08 08:37:05 -08005228 QCBORDecode_Init(&DC,
5229 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad4),
5230 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005231
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305232 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
5233 return -15;
5234 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005235
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305236 if(QCBORDecode_GetNext(&DC, &Item)) {
5237 return -16;
5238 }
5239 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
5240 return -17;
5241 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005242
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305243 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_HIT_END) {
5244 return -18;
5245 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005246
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305247 // ------ Don't set a string allocator and see an error -----
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05305248 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005249
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05305250 QCBORDecode_GetNext(&DC, &Item);
5251 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305252 return -19;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05305253 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005254
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05305255 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_NO_STRING_ALLOCATOR) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305256 return -20;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05305257 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005258
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05305259 // ----- Mempool is way too small -----
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08005260 UsefulBuf_MAKE_STACK_UB(MemPoolTooSmall, QCBOR_DECODE_MIN_MEM_POOL_SIZE-1);
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05305261
5262 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
5263 if(!QCBORDecode_SetMemPool(&DC, MemPoolTooSmall, false)) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305264 return -21;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05305265 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005266
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05305267 // ----- Mempool is way too small -----
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05305268 UsefulBuf_MAKE_STACK_UB(BigIndefBStrStorage, 290);
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08005269 const UsefulBufC BigIndefBStr = MakeIndefiniteBigBstr(BigIndefBStrStorage);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005270
Laurence Lundbladeee851742020-01-08 08:37:05 -08005271 // 80 is big enough for MemPool overhead, but not BigIndefBStr
5272 UsefulBuf_MAKE_STACK_UB(MemPoolSmall, 80);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005273
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305274 QCBORDecode_Init(&DC, BigIndefBStr, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05305275 if(QCBORDecode_SetMemPool(&DC, MemPoolSmall, false)) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305276 return -22;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05305277 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005278
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05305279 QCBORDecode_GetNext(&DC, &Item);
5280 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305281 return -23;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05305282 }
Laurence Lundblade30816f22018-11-10 13:40:22 +07005283 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_STRING_ALLOCATE) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305284 return -24;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05305285 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005286
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305287 // ---- big bstr -----
5288 QCBORDecode_Init(&DC, BigIndefBStr, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005289
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305290 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
5291 return -25;
5292 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005293
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05305294 if(QCBORDecode_GetNext(&DC, &Item)) {
5295 return -26;
5296 }
5297 if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.uDataAlloc) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305298 return -26;
5299 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005300
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305301 if(QCBORDecode_GetNext(&DC, &Item)) {
5302 return -27;
5303 }
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05305304 if(Item.uDataType != QCBOR_TYPE_BYTE_STRING || !Item.uDataAlloc || Item.uNestingLevel != 1) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305305 return -28;
5306 }
5307 if(CheckBigString(Item.val.string)) {
5308 return -3;
5309 }
5310 if(QCBORDecode_Finish(&DC)) {
5311 return -29;
5312 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005313
Laurence Lundbladeec290b82024-06-10 11:10:54 -07005314#ifndef QCBOR_DISABLE_NON_INTEGER_LABELS
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305315 // --- label is an indefinite length string ------
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07005316 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringLabel), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005317
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305318 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
5319 return -30;
5320 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005321
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305322 QCBORDecode_GetNext(&DC, &Item);
5323 if(Item.uDataType != QCBOR_TYPE_MAP) {
5324 return -31;
5325 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005326
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305327 if(QCBORDecode_GetNext(&DC, &Item)){
5328 return -32;
5329 }
Laurence Lundbladeee851742020-01-08 08:37:05 -08005330 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
5331 Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305332 Item.uDataAlloc || !Item.uLabelAlloc ||
5333 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("struuming"))) {
5334 return -33;
5335 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005336
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305337 if(QCBORDecode_Finish(&DC)) {
5338 return -34;
5339 }
Laurence Lundbladeec290b82024-06-10 11:10:54 -07005340#endif /* ! QCBOR_DISABLE_NON_INTEGER_LABELS */
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005341
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08005342 return 0;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08005343}
5344
5345
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03005346int32_t AllocAllStringsTest(void)
Laurence Lundbladea44d5062018-10-17 18:45:12 +05305347{
5348 QCBORDecodeContext DC;
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08005349 QCBORError nCBORError;
5350
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005351
Laurence Lundbladea44d5062018-10-17 18:45:12 +05305352 // First test, use the "CSRMap" as easy input and checking
Laurence Lundbladeee851742020-01-08 08:37:05 -08005353 QCBORDecode_Init(&DC,
5354 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput),
5355 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005356
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08005357 UsefulBuf_MAKE_STACK_UB(Pool, sizeof(spCSRInput) + QCBOR_DECODE_MIN_MEM_POOL_SIZE);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005358
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08005359 nCBORError = QCBORDecode_SetMemPool(&DC, Pool, 1); // Turn on copying.
5360 if(nCBORError) {
Laurence Lundbladea44d5062018-10-17 18:45:12 +05305361 return -1;
5362 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005363
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08005364 if(CheckCSRMaps(&DC)) {
5365 return -2;
5366 }
5367
Laurence Lundbladeec290b82024-06-10 11:10:54 -07005368#ifndef QCBOR_DISABLE_NON_INTEGER_LABELS
Laurence Lundblade2f467f92020-10-09 17:50:11 -07005369 // Next parse, save pointers to a few strings, destroy original and
5370 // see all is OK.
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08005371 UsefulBuf_MAKE_STACK_UB(CopyOfStorage, sizeof(pValidMapEncoded) + QCBOR_DECODE_MIN_MEM_POOL_SIZE);
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08005372 const UsefulBufC CopyOf = UsefulBuf_Copy(CopyOfStorage, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded));
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08005373
Laurence Lundbladea44d5062018-10-17 18:45:12 +05305374 QCBORDecode_Init(&DC, CopyOf, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08005375 UsefulBuf_Set(Pool, '/');
Laurence Lundbladea44d5062018-10-17 18:45:12 +05305376 QCBORDecode_SetMemPool(&DC, Pool, 1); // Turn on copying.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005377
Laurence Lundbladea44d5062018-10-17 18:45:12 +05305378 QCBORItem Item1, Item2, Item3, Item4;
5379 if((nCBORError = QCBORDecode_GetNext(&DC, &Item1)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07005380 return (int32_t)nCBORError;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05305381 if(Item1.uDataType != QCBOR_TYPE_MAP ||
5382 Item1.val.uCount != 3)
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08005383 return -3;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05305384 if((nCBORError = QCBORDecode_GetNext(&DC, &Item1)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07005385 return (int32_t)nCBORError;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05305386 if((nCBORError = QCBORDecode_GetNext(&DC, &Item2)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07005387 return (int32_t)nCBORError;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05305388 if((nCBORError = QCBORDecode_GetNext(&DC, &Item3)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07005389 return (int32_t)nCBORError;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05305390 if((nCBORError = QCBORDecode_GetNext(&DC, &Item4)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07005391 return (int32_t)nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005392
Laurence Lundblade05ec57b2018-10-21 01:50:03 +05305393 UsefulBuf_Set(CopyOfStorage, '_');
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005394
Laurence Lundbladea44d5062018-10-17 18:45:12 +05305395 if(Item1.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladea44d5062018-10-17 18:45:12 +05305396 Item1.uDataType != QCBOR_TYPE_INT64 ||
5397 Item1.val.int64 != 42 ||
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08005398 Item1.uDataAlloc != 0 ||
5399 Item1.uLabelAlloc == 0 ||
Laurence Lundbladeec290b82024-06-10 11:10:54 -07005400 UsefulBufCompareToSZ(Item1.label.string, "first integer") ||
5401 Item1.label.string.ptr < Pool.ptr ||
5402 Item1.label.string.ptr > (const void *)((const uint8_t *)Pool.ptr + Pool.len)) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08005403 return -4;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09005404 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005405
Laurence Lundbladea44d5062018-10-17 18:45:12 +05305406
5407 if(Item2.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07005408 UsefulBufCompareToSZ(Item2.label.string, "an array of two strings") ||
Laurence Lundbladea44d5062018-10-17 18:45:12 +05305409 Item2.uDataType != QCBOR_TYPE_ARRAY ||
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08005410 Item2.uDataAlloc != 0 ||
5411 Item2.uLabelAlloc == 0 ||
Laurence Lundbladea44d5062018-10-17 18:45:12 +05305412 Item2.val.uCount != 2)
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08005413 return -5;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005414
Laurence Lundbladea44d5062018-10-17 18:45:12 +05305415 if(Item3.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08005416 Item3.uDataAlloc == 0 ||
5417 Item3.uLabelAlloc != 0 ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07005418 UsefulBufCompareToSZ(Item3.val.string, "string1")) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08005419 return -6;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09005420 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005421
Laurence Lundbladea44d5062018-10-17 18:45:12 +05305422 if(Item4.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08005423 Item4.uDataAlloc == 0 ||
5424 Item4.uLabelAlloc != 0 ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07005425 UsefulBufCompareToSZ(Item4.val.string, "string2")) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08005426 return -7;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09005427 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005428
Laurence Lundbladea44d5062018-10-17 18:45:12 +05305429 // Next parse with a pool that is too small
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08005430 UsefulBuf_MAKE_STACK_UB(SmallPool, QCBOR_DECODE_MIN_MEM_POOL_SIZE + 1);
Laurence Lundbladeee851742020-01-08 08:37:05 -08005431 QCBORDecode_Init(&DC,
5432 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded),
5433 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladea44d5062018-10-17 18:45:12 +05305434 QCBORDecode_SetMemPool(&DC, SmallPool, 1); // Turn on copying.
5435 if((nCBORError = QCBORDecode_GetNext(&DC, &Item1)))
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08005436 return -8;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05305437 if(Item1.uDataType != QCBOR_TYPE_MAP ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09005438 Item1.val.uCount != 3) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08005439 return -9;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09005440 }
Laurence Lundbladea44d5062018-10-17 18:45:12 +05305441 if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item1))){
5442 if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item2))) {
5443 if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item3))) {
5444 nCBORError = QCBORDecode_GetNext(&DC, &Item4);
5445 }
5446 }
5447 }
Laurence Lundblade30816f22018-11-10 13:40:22 +07005448 if(nCBORError != QCBOR_ERR_STRING_ALLOCATE) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08005449 return -10;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05305450 }
Laurence Lundbladeec290b82024-06-10 11:10:54 -07005451#endif /* ! QCBOR_DISABLE_NON_INTEGER_LABELS */
Laurence Lundbladea44d5062018-10-17 18:45:12 +05305452
5453 return 0;
5454}
5455
Laurence Lundbladef6531662018-12-04 10:42:22 +09005456
Laurence Lundbladec5fef682020-01-25 11:38:45 -08005457int32_t MemPoolTest(void)
Laurence Lundblade0155b622018-10-12 20:04:37 +08005458{
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08005459 // Set up the decoder with a tiny bit of CBOR to parse because
5460 // nothing can be done with it unless that is set up.
Laurence Lundbladef6531662018-12-04 10:42:22 +09005461 QCBORDecodeContext DC;
5462 const uint8_t pMinimalCBOR[] = {0xa0}; // One empty map
5463 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pMinimalCBOR),0);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005464
Laurence Lundbladef6531662018-12-04 10:42:22 +09005465 // Set up an memory pool of 100 bytes
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08005466 // Then fish into the internals of the decode context
5467 // to get the allocator function so it can be called directly.
5468 // Also figure out how much pool is available for use
5469 // buy subtracting out the overhead.
Laurence Lundbladef6531662018-12-04 10:42:22 +09005470 UsefulBuf_MAKE_STACK_UB(Pool, 100);
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08005471 QCBORError nError = QCBORDecode_SetMemPool(&DC, Pool, 0);
5472 if(nError) {
5473 return -9;
5474 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08005475 QCBORStringAllocate pAlloc = DC.StringAllocator.pfAllocator;
5476 void *pAllocCtx = DC.StringAllocator.pAllocateCxt;
5477 size_t uAvailPool = Pool.len - QCBOR_DECODE_MIN_MEM_POOL_SIZE;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005478
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08005479 // First test -- ask for one more byte than available and see failure
5480 UsefulBuf Allocated = (*pAlloc)(pAllocCtx, NULL, uAvailPool+1);
Laurence Lundbladef6531662018-12-04 10:42:22 +09005481 if(!UsefulBuf_IsNULL(Allocated)) {
5482 return -1;
5483 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005484
Laurence Lundbladef6531662018-12-04 10:42:22 +09005485 // Re do the set up for the next test that will do a successful alloc,
5486 // a fail, a free and then success
Laurence Lundbladef6531662018-12-04 10:42:22 +09005487 QCBORDecode_SetMemPool(&DC, Pool, 0);
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08005488 pAlloc = DC.StringAllocator.pfAllocator;
5489 pAllocCtx = DC.StringAllocator.pAllocateCxt;
5490 uAvailPool = Pool.len - QCBOR_DECODE_MIN_MEM_POOL_SIZE;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005491
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08005492 // Allocate one byte less than available and see success
5493 Allocated = (pAlloc)(pAllocCtx, NULL, uAvailPool-1);
Laurence Lundbladef6531662018-12-04 10:42:22 +09005494 if(UsefulBuf_IsNULL(Allocated)) { // expected to succeed
5495 return -2;
5496 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08005497 // Ask for some more and see failure
5498 UsefulBuf Allocated2 = (*pAlloc)(pAllocCtx, NULL, uAvailPool/2);
Laurence Lundbladef6531662018-12-04 10:42:22 +09005499 if(!UsefulBuf_IsNULL(Allocated2)) { // expected to fail
5500 return -3;
5501 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08005502 // Free the first allocate, retry the second and see success
5503 (*pAlloc)(pAllocCtx, Allocated.ptr, 0); // Free
5504 Allocated = (*pAlloc)(pAllocCtx, NULL, uAvailPool/2);
Laurence Lundbladef6531662018-12-04 10:42:22 +09005505 if(UsefulBuf_IsNULL(Allocated)) { // succeed because of the free
5506 return -4;
5507 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005508
Laurence Lundbladef6531662018-12-04 10:42:22 +09005509 // Re do set up for next test that involves a successful alloc,
5510 // and a successful realloc and a failed realloc
5511 QCBORDecode_SetMemPool(&DC, Pool, 0);
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08005512 pAlloc = DC.StringAllocator.pfAllocator;
5513 pAllocCtx = DC.StringAllocator.pAllocateCxt;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005514
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08005515 // Allocate half the pool and see success
5516 Allocated = (*pAlloc)(pAllocCtx, NULL, uAvailPool/2);
Laurence Lundbladef6531662018-12-04 10:42:22 +09005517 if(UsefulBuf_IsNULL(Allocated)) { // expected to succeed
5518 return -5;
5519 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08005520 // Reallocate to take up the whole pool and see success
5521 Allocated2 = (*pAlloc)(pAllocCtx, Allocated.ptr, uAvailPool);
Laurence Lundbladef6531662018-12-04 10:42:22 +09005522 if(UsefulBuf_IsNULL(Allocated2)) {
5523 return -6;
5524 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08005525 // Make sure its the same pointer and the size is right
Laurence Lundbladef6531662018-12-04 10:42:22 +09005526 if(Allocated2.ptr != Allocated.ptr || Allocated2.len != uAvailPool) {
5527 return -7;
5528 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08005529 // Try to allocate more to be sure there is failure after a realloc
5530 UsefulBuf Allocated3 = (*pAlloc)(pAllocCtx, Allocated.ptr, uAvailPool+1);
5531 if(!UsefulBuf_IsNULL(Allocated3)) {
Laurence Lundbladef6531662018-12-04 10:42:22 +09005532 return -8;
5533 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005534
Laurence Lundbladef6531662018-12-04 10:42:22 +09005535 return 0;
5536}
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08005537
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08005538
5539/* Just enough of an allocator to test configuration of one */
5540static UsefulBuf AllocateTestFunction(void *pCtx, void *pOldMem, size_t uNewSize)
5541{
5542 (void)pOldMem; // unused variable
5543
5544 if(uNewSize) {
5545 // Assumes the context pointer is the buffer and
5546 // nothing too big will ever be asked for.
5547 // This is only good for this basic test!
5548 return (UsefulBuf) {pCtx, uNewSize};
5549 } else {
5550 return NULLUsefulBuf;
5551 }
5552}
5553
5554
Laurence Lundbladec5fef682020-01-25 11:38:45 -08005555int32_t SetUpAllocatorTest(void)
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08005556{
5557 // Set up the decoder with a tiny bit of CBOR to parse because
5558 // nothing can be done with it unless that is set up.
5559 QCBORDecodeContext DC;
5560 const uint8_t pMinimalCBOR[] = {0x62, 0x48, 0x69}; // "Hi"
5561 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pMinimalCBOR),0);
5562
5563 uint8_t pAllocatorBuffer[50];
5564
5565 // This is really just to test that this call works.
5566 // The full functionality of string allocators is tested
5567 // elsewhere with the MemPool internal allocator.
5568 QCBORDecode_SetUpAllocator(&DC, AllocateTestFunction, pAllocatorBuffer, 1);
5569
5570 QCBORItem Item;
5571 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_SUCCESS) {
5572 return -1;
5573 }
5574
5575 if(Item.uDataAlloc == 0 ||
5576 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
5577 Item.val.string.ptr != pAllocatorBuffer) {
5578 return -2;
5579 }
5580
5581 if(QCBORDecode_Finish(&DC) != QCBOR_SUCCESS) {
5582 return -3;
5583 }
5584
5585 return 0;
5586}
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08005587#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
5588
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08005589
Laurence Lundbladedd6e76e2021-03-10 01:54:01 -07005590#ifndef QCBOR_DISABLE_EXP_AND_MANTISSA
Laurence Lundblade59289e52019-12-30 13:44:37 -08005591
Laurence Lundblade37286c02022-09-03 10:05:02 -07005592struct EaMTest {
5593 const char *szName;
5594 UsefulBufC Input;
5595 uint8_t uTagRequirement;
5596 bool bHasTags;
5597
5598 /* Expected values for GetNext */
5599 QCBORError uExpectedErrorGN;
5600 uint8_t uQCBORTypeGN;
5601 int64_t nExponentGN;
5602 int64_t nMantissaGN;
5603 UsefulBufC MantissaGN;
5604
5605 /* Expected values for GetDecimalFraction */
5606 QCBORError uExpectedErrorGDF;
5607 int64_t nExponentGDF;
5608 int64_t nMantissaGDF;
5609
5610 /* Expected values for GetDecimalFractionBig */
5611 QCBORError uExpectedErrorGDFB;
5612 int64_t nExponentGDFB;
5613 UsefulBufC MantissaGDFB;
5614 bool IsNegativeGDFB;
5615
5616 /* Expected values for GetBigFloat */
5617 QCBORError uExpectedErrorGBF;
5618 int64_t nExponentGBF;
5619 int64_t nMantissaGBF;
5620
5621 /* Expected values for GetBigFloatBig */
5622 QCBORError uExpectedErrorGBFB;
5623 int64_t nExponentGBFB;
5624 UsefulBufC MantissaGBFB;
5625 bool IsNegativeGBFB;
Laurence Lundblade59289e52019-12-30 13:44:37 -08005626};
5627
Laurence Lundbladefaec39f2020-08-02 21:53:53 -07005628
Laurence Lundblade37286c02022-09-03 10:05:02 -07005629
5630static const struct EaMTest pEaMTests[] = {
5631 {
5632 "1. Untagged pair (big float or decimal fraction), no tag required",
5633 {(const uint8_t []){0x82, 0x20, 0x03}, 3},
5634 QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
5635 false,
5636
5637 QCBOR_SUCCESS, /* for GetNext */
5638 QCBOR_TYPE_ARRAY,
5639 0,
5640 0,
5641 {(const uint8_t []){0x00}, 1},
5642
5643 QCBOR_SUCCESS, /* GetDecimalFraction */
5644 -1,
5645 3,
5646
5647 QCBOR_SUCCESS, /* for GetDecimalFractionBig */
5648 -1,
5649 {(const uint8_t []){0x03}, 1},
5650 false,
5651
5652 QCBOR_SUCCESS, /* for GetBigFloat */
5653 -1,
5654 3,
5655
5656 QCBOR_SUCCESS, /* for GetBigFloatBig */
5657 -1,
5658 {(const uint8_t []){0x03}, 1},
5659 false
5660 },
5661
5662 {
5663 "2. Untagged pair (big float or decimal fraction), tag required",
5664 {(const uint8_t []){0x82, 0x20, 0x03}, 3},
5665 QCBOR_TAG_REQUIREMENT_TAG,
5666 false,
5667
5668 QCBOR_SUCCESS, /* for GetNext */
5669 QCBOR_TYPE_ARRAY,
5670 0,
5671 0,
5672 {(const uint8_t []){0x00}, 1},
5673
5674 QCBOR_ERR_UNEXPECTED_TYPE, /* for GetDecimalFraction */
5675 0,
5676 0,
5677
5678 QCBOR_ERR_UNEXPECTED_TYPE, /* for GetDecimalFractionBig */
5679 0,
5680 {(const uint8_t []){0x00}, 1},
5681 false,
5682
5683 QCBOR_ERR_UNEXPECTED_TYPE, /* for GetBigFloat */
5684 0,
5685 0,
5686
5687 QCBOR_ERR_UNEXPECTED_TYPE, /* for GetBigFloatBig */
5688 0,
5689 {(const uint8_t []){0x00}, 1},
5690 false
5691
5692 },
5693
5694 {
5695 "3. Tagged 1.5 decimal fraction, tag 4 optional",
5696 {(const uint8_t []){0xC4, 0x82, 0x20, 0x03}, 4},
5697 QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
5698 true,
5699
5700 QCBOR_SUCCESS, /* for GetNext */
5701 QCBOR_TYPE_DECIMAL_FRACTION,
5702 -1,
5703 3,
5704 {(const uint8_t []){0x00}, 1},
5705
5706
5707 QCBOR_SUCCESS, /* for GetDecimalFraction */
5708 -1,
5709 3,
5710
5711 QCBOR_SUCCESS, /* for GetDecimalFractionBig */
5712 -1,
5713 {(const uint8_t []){0x03}, 1},
5714 false,
5715
5716 QCBOR_ERR_UNEXPECTED_TYPE, /* for GetBigFloat */
5717 0,
5718 0,
5719
5720 QCBOR_ERR_UNEXPECTED_TYPE, /* for GetBigFloatBig */
5721 0,
5722 {(const uint8_t []){0x00}, 1},
5723 false
5724 },
5725 {
5726 "4. Tagged 100 * 2^300 big float, tag 5 optional",
5727 {(const uint8_t []){0xC5, 0x82, 0x19, 0x01, 0x2C, 0x18, 0x64}, 7},
5728 QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
5729 true,
5730
5731 QCBOR_SUCCESS, /* for GetNext */
5732 QCBOR_TYPE_BIGFLOAT,
5733 300,
5734 100,
5735 {(const uint8_t []){0x00}, 1},
5736
5737
5738 QCBOR_ERR_UNEXPECTED_TYPE, /* for GetDecimalFraction */
5739 0,
5740 0,
5741
5742 QCBOR_ERR_UNEXPECTED_TYPE, /* for GetDecimalFractionBig */
5743 0,
5744 {(const uint8_t []){0x03}, 1},
5745 false,
5746
5747 QCBOR_SUCCESS, /* for GetBigFloat */
5748 300,
5749 100,
5750
5751 QCBOR_SUCCESS, /* for GetBigFloatBig */
5752 300,
5753 {(const uint8_t []){0x64}, 1},
5754 false
5755 },
5756
5757 {
5758 "5. Tagged 4([-20, 4759477275222530853136]) decimal fraction, tag 4 required",
5759 {(const uint8_t []){0xC4, 0x82, 0x33,
5760 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,}, 15},
5761 QCBOR_TAG_REQUIREMENT_TAG,
5762 true,
5763
5764 QCBOR_SUCCESS, /* for GetNext */
5765 QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM,
5766 -20,
5767 0,
5768 {(const uint8_t []){0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10}, 10},
5769
5770 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW, /* for GetDecimalFraction */
5771 0,
5772 0,
5773
5774 QCBOR_SUCCESS, /* for GetDecimalFractionBig */
5775 -20,
5776 {(const uint8_t []){0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10}, 10},
5777 false,
5778
5779 QCBOR_ERR_UNEXPECTED_TYPE, /* for GetBigFloat */
5780 0,
5781 0,
5782
5783 QCBOR_ERR_UNEXPECTED_TYPE, /* for GetBigFloatBig */
5784 0,
5785 {(const uint8_t []){0x00}, 0},
5786 false
5787 },
5788
5789 {
5790 "6. Error: Mantissa and exponent inside a Mantissa and exponent",
5791 {(const uint8_t []){0xC4, 0x82, 0x33,
5792 0xC5, 0x82, 0x19, 0x01, 0x2C, 0x18, 0x64}, 10},
5793 QCBOR_TAG_REQUIREMENT_TAG,
5794 true,
5795
5796 QCBOR_ERR_BAD_EXP_AND_MANTISSA, /* for GetNext */
5797 QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM,
5798 0,
5799 0,
5800 {(const uint8_t []){0x00}, 0},
5801
5802 QCBOR_ERR_BAD_EXP_AND_MANTISSA, /* for GetDecimalFraction */
5803 0,
5804 0,
5805
5806 QCBOR_ERR_BAD_EXP_AND_MANTISSA, /* for GetDecimalFractionBig */
5807 0,
5808 {(const uint8_t []){0x00}, 0},
5809 false,
5810
5811 QCBOR_ERR_BAD_EXP_AND_MANTISSA, /* for GetBigFloat */
5812 0,
5813 0,
5814
5815 QCBOR_ERR_BAD_EXP_AND_MANTISSA, /* for GetBigFloatBig */
5816 0,
5817 {(const uint8_t []){0x00}, 0},
5818 false
5819 },
5820 {
5821 "7. Tagged 5([-20, 4294967295]) big float, big num mantissa, tag 5 required",
5822 {(const uint8_t []){0xC5, 0x82, 0x33,
5823 0xC2, 0x44, 0xff, 0xff, 0xff, 0xff}, 9},
5824 QCBOR_TAG_REQUIREMENT_TAG,
5825 true,
5826
5827 QCBOR_SUCCESS, /* for GetNext */
5828 QCBOR_TYPE_BIGFLOAT_POS_BIGNUM,
5829 -20,
5830 0,
5831 {(const uint8_t []){0xff, 0xff, 0xff, 0xff}, 4},
5832
5833 QCBOR_ERR_UNEXPECTED_TYPE, /* for GetDecimalFraction */
5834 0,
5835 0,
5836
5837 QCBOR_ERR_UNEXPECTED_TYPE, /* for GetDecimalFractionBig */
5838 -20,
5839 {(const uint8_t []){0x00}, 1},
5840 false,
5841
5842 QCBOR_SUCCESS, /* for GetBigFloat */
5843 -20,
5844 4294967295,
5845
5846 QCBOR_SUCCESS, /* for GetBigFloatBig */
5847 -20,
5848 {(const uint8_t []){0xff, 0xff, 0xff, 0xff}, 4},
5849 false
5850 },
5851
5852 {
5853 /* Special case for test 8. Don't renumber it. */
5854 "8. Untagged pair with big num (big float or decimal fraction), tag optional",
5855 {(const uint8_t []){0x82, 0x33, 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10}, 14},
5856 QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
5857 true,
5858
5859 QCBOR_SUCCESS, /* for GetNext */
5860 QCBOR_TYPE_ARRAY,
5861 0,
5862 0,
5863 {(const uint8_t []){0x00}, 1},
5864
5865 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW, /* GetDecimalFraction */
5866 0,
5867 0,
5868
5869 QCBOR_SUCCESS, /* for GetDecimalFractionBig */
5870 -20,
5871 {(const uint8_t []){0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10}, 10},
5872 false,
5873
5874 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW, /* for GetBigFloat */
5875 0,
5876 0,
5877
5878 QCBOR_SUCCESS, /* for GetBigFloatBig */
5879 -20,
5880 {(const uint8_t []){0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10}, 10},
5881 false
5882 },
5883
5884 {
5885 "9. decimal fraction with large exponent and negative big num mantissa",
5886 {(const uint8_t []){0xC4, 0x82, 0x1B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
5887 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10}, 23},
5888 QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
5889 true,
5890
5891 QCBOR_SUCCESS, /* for GetNext */
5892 QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM,
5893 9223372036854775807,
5894 0,
5895 {(const uint8_t []){0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10}, 10},
5896
5897 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW, /* GetDecimalFraction */
5898 0,
5899 0,
5900
5901 QCBOR_SUCCESS, /* for GetDecimalFractionBig */
5902 9223372036854775807,
5903 {(const uint8_t []){0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10}, 10},
5904 true,
5905
5906 QCBOR_ERR_UNEXPECTED_TYPE, /* for GetBigFloat */
5907 0,
5908 0,
5909
5910 QCBOR_ERR_UNEXPECTED_TYPE, /* for GetBigFloatBig */
5911 0,
5912 {(const uint8_t []){0x00}, 1},
5913 false
5914 },
5915};
5916
5917
5918
5919int32_t ProcessEaMTests(void)
Laurence Lundblade59289e52019-12-30 13:44:37 -08005920{
Laurence Lundblade37286c02022-09-03 10:05:02 -07005921 size_t uIndex;
5922 QCBORDecodeContext DCtx;
5923 QCBORItem Item;
5924 QCBORError uError;
5925 int64_t nMantissa, nExponent;
5926 MakeUsefulBufOnStack( MantissaBuf, 200);
5927 UsefulBufC Mantissa;
5928 bool bMantissaIsNegative;
5929
5930 for(uIndex = 0; uIndex < C_ARRAY_COUNT(pEaMTests, struct EaMTest); uIndex++) {
5931 const struct EaMTest *pT = &pEaMTests[uIndex];
5932 /* Decode with GetNext */
5933 QCBORDecode_Init(&DCtx, pT->Input, 0);
5934
5935 if(uIndex + 1 == 9) {
5936 nExponent = 99; // just to set a break point
5937 }
5938
5939 uError = QCBORDecode_GetNext(&DCtx, &Item);
5940#ifdef QCBOR_DISABLE_TAGS
5941 /* Test 8 is a special case when tags are disabled */
5942 if(pT->bHasTags && uIndex + 1 != 8) {
5943 if(uError != QCBOR_ERR_TAGS_DISABLED) {
5944 return (int32_t)(1+uIndex) * 1000 + 9;
5945 }
5946 } else {
5947#endif
5948 /* Now check return code, data type, mantissa and exponent */
5949 if(pT->uExpectedErrorGN != uError) {
5950 return (int32_t)(1+uIndex) * 1000 + 1;
5951 }
5952 if(uError == QCBOR_SUCCESS && pT->uQCBORTypeGN != QCBOR_TYPE_ARRAY) {
5953 if(pT->uQCBORTypeGN != Item.uDataType) {
5954 return (int32_t)(1+uIndex) * 1000 + 2;
5955 }
5956 if(pT->nExponentGN != Item.val.expAndMantissa.nExponent) {
5957 return (int32_t)(1+uIndex) * 1000 + 3;
5958 }
5959 if(Item.uDataType == QCBOR_TYPE_DECIMAL_FRACTION || Item.uDataType == QCBOR_TYPE_BIGFLOAT ) {
5960 if(pT->nMantissaGN != Item.val.expAndMantissa.Mantissa.nInt) {
5961 return (int32_t)(1+uIndex) * 1000 + 4;
5962 }
5963 } else {
5964 if(UsefulBuf_Compare(Item.val.expAndMantissa.Mantissa.bigNum, pT->MantissaGN)) {
5965 return (int32_t)(1+uIndex) * 1000 + 5;
5966 }
5967 }
5968 }
5969#ifdef QCBOR_DISABLE_TAGS
5970 }
5971#endif
5972
5973 /* Decode with GetDecimalFraction */
5974 QCBORDecode_Init(&DCtx, pT->Input, 0);
5975 QCBORDecode_GetDecimalFraction(&DCtx,
5976 pT->uTagRequirement,
5977 &nMantissa,
5978 &nExponent);
5979 uError = QCBORDecode_GetAndResetError(&DCtx);
5980#ifdef QCBOR_DISABLE_TAGS
5981 if(pT->bHasTags) {
5982 if(uError != QCBOR_ERR_TAGS_DISABLED) {
5983 return (int32_t)(1+uIndex) * 1000 + 39;
5984 }
5985 } else {
5986#endif
5987 /* Now check return code, mantissa and exponent */
5988 if(pT->uExpectedErrorGDF != uError) {
5989 return (int32_t)(1+uIndex) * 1000 + 31;
5990 }
5991 if(uError == QCBOR_SUCCESS) {
5992 if(pT->nExponentGDF != nExponent) {
5993 return (int32_t)(1+uIndex) * 1000 + 32;
5994 }
5995 if(pT->nMantissaGDF != nMantissa) {
5996 return (int32_t)(1+uIndex) * 1000 + 33;
5997 }
5998 }
5999#ifdef QCBOR_DISABLE_TAGS
6000 }
6001#endif
6002
6003 /* Decode with GetDecimalFractionBig */
6004 QCBORDecode_Init(&DCtx, pT->Input, 0);
6005 QCBORDecode_GetDecimalFractionBig(&DCtx,
6006 pT->uTagRequirement,
6007 MantissaBuf,
6008 &Mantissa,
6009 &bMantissaIsNegative,
6010 &nExponent);
6011 uError = QCBORDecode_GetAndResetError(&DCtx);
6012#ifdef QCBOR_DISABLE_TAGS
6013 if(pT->bHasTags) {
6014 if(uError != QCBOR_ERR_TAGS_DISABLED) {
6015 return (int32_t)(1+uIndex) * 1000 + 49;
6016 }
6017 } else {
6018#endif
6019 /* Now check return code, mantissa (bytes and sign) and exponent */
6020 if(pT->uExpectedErrorGDFB != uError) {
6021 return (int32_t)(1+uIndex) * 1000 + 41;
6022 }
6023 if(uError == QCBOR_SUCCESS) {
6024 if(pT->nExponentGDFB != nExponent) {
6025 return (int32_t)(1+uIndex) * 1000 + 42;
6026 }
6027 if(pT->IsNegativeGDFB != bMantissaIsNegative) {
6028 return (int32_t)(1+uIndex) * 1000 + 43;
6029 }
6030 if(UsefulBuf_Compare(Mantissa, pT->MantissaGDFB)) {
6031 return (int32_t)(1+uIndex) * 1000 + 44;
6032 }
6033 }
6034#ifdef QCBOR_DISABLE_TAGS
6035 }
6036#endif
6037
6038 /* Decode with GetBigFloat */
6039 QCBORDecode_Init(&DCtx, pT->Input, 0);
6040 QCBORDecode_GetBigFloat(&DCtx,
6041 pT->uTagRequirement,
6042 &nMantissa,
6043 &nExponent);
6044 uError = QCBORDecode_GetAndResetError(&DCtx);
6045#ifdef QCBOR_DISABLE_TAGS
6046 if(pT->bHasTags) {
6047 if(uError != QCBOR_ERR_TAGS_DISABLED) {
6048 return (int32_t)(1+uIndex) * 1000 + 19;
6049 }
6050 } else {
6051#endif
6052 /* Now check return code, mantissa and exponent */
6053 if(pT->uExpectedErrorGBF != uError) {
6054 return (int32_t)(1+uIndex) * 1000 + 11;
6055 }
6056 if(uError == QCBOR_SUCCESS) {
6057 if(pT->nExponentGBF != nExponent) {
6058 return (int32_t)(1+uIndex) * 1000 + 12;
6059 }
6060 if(pT->nMantissaGBF != nMantissa) {
6061 return (int32_t)(1+uIndex) * 1000 + 13;
6062 }
6063 }
6064#ifdef QCBOR_DISABLE_TAGS
6065 }
6066#endif
6067
6068 /* Decode with GetBigFloatBig */
6069 QCBORDecode_Init(&DCtx, pT->Input, 0);
6070 QCBORDecode_GetBigFloatBig(&DCtx,
6071 pT->uTagRequirement,
6072 MantissaBuf,
6073 &Mantissa,
6074 &bMantissaIsNegative,
6075 &nExponent);
6076 uError = QCBORDecode_GetAndResetError(&DCtx);
6077#ifdef QCBOR_DISABLE_TAGS
6078 if(pT->bHasTags) {
6079 if(uError != QCBOR_ERR_TAGS_DISABLED) {
6080 return (int32_t)(1+uIndex) * 1000 + 29;
6081 }
6082 } else {
6083#endif
6084 /* Now check return code, mantissa (bytes and sign) and exponent */
6085 if(pT->uExpectedErrorGBFB != uError) {
6086 return (int32_t)(1+uIndex) * 1000 + 21;
6087 }
6088 if(uError == QCBOR_SUCCESS) {
6089 if(pT->nExponentGBFB != nExponent) {
6090 return (int32_t)(1+uIndex) * 1000 + 22;
6091 }
6092 if(pT->IsNegativeGBFB != bMantissaIsNegative) {
6093 return (int32_t)(1+uIndex) * 1000 + 23;
6094 }
6095 if(UsefulBuf_Compare(Mantissa, pT->MantissaGBFB)) {
6096 return (int32_t)(1+uIndex) * 1000 + 24;
6097 }
6098 }
6099#ifdef QCBOR_DISABLE_TAGS
6100 }
6101#endif
6102 }
6103
6104 return 0;
6105}
6106
6107
6108int32_t ExponentAndMantissaDecodeTestsSecondary(void)
6109{
6110#ifndef QCBOR_DISABLE_TAGS
Laurence Lundblade59289e52019-12-30 13:44:37 -08006111 QCBORDecodeContext DC;
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07006112 QCBORError uErr;
Laurence Lundblade59289e52019-12-30 13:44:37 -08006113 QCBORItem item;
6114
Laurence Lundblade17af4902020-01-07 19:11:55 -08006115 static const uint8_t spBigNumMantissa[] = {0x01, 0x02, 0x03, 0x04, 0x05,
6116 0x06, 0x07, 0x08, 0x09, 0x010};
6117 UsefulBufC BN = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNumMantissa);
Laurence Lundblade59289e52019-12-30 13:44:37 -08006118
6119
Laurence Lundblade59289e52019-12-30 13:44:37 -08006120
6121 /* Now encode some stuff and then decode it */
6122 uint8_t pBuf[40];
6123 QCBOREncodeContext EC;
6124 UsefulBufC Encoded;
6125
6126 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(pBuf));
6127 QCBOREncode_OpenArray(&EC);
6128 QCBOREncode_AddDecimalFraction(&EC, 999, 1000); // 999 * (10 ^ 1000)
6129 QCBOREncode_AddBigFloat(&EC, 100, INT32_MIN);
6130 QCBOREncode_AddDecimalFractionBigNum(&EC, BN, false, INT32_MAX);
6131 QCBOREncode_CloseArray(&EC);
6132 QCBOREncode_Finish(&EC, &Encoded);
6133
6134
6135 QCBORDecode_Init(&DC, Encoded, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07006136 uErr = QCBORDecode_GetNext(&DC, &item);
6137 if(uErr != QCBOR_SUCCESS) {
6138 return 100;
Laurence Lundblade59289e52019-12-30 13:44:37 -08006139 }
6140
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07006141 uErr = QCBORDecode_GetNext(&DC, &item);
6142 if(uErr != QCBOR_SUCCESS) {
6143 return 101;
Laurence Lundblade59289e52019-12-30 13:44:37 -08006144 }
6145
6146 if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION ||
6147 item.val.expAndMantissa.nExponent != 1000 ||
6148 item.val.expAndMantissa.Mantissa.nInt != 999) {
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07006149 return 102;
Laurence Lundblade59289e52019-12-30 13:44:37 -08006150 }
6151
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07006152 uErr = QCBORDecode_GetNext(&DC, &item);
6153 if(uErr != QCBOR_SUCCESS) {
6154 return 103;
Laurence Lundblade59289e52019-12-30 13:44:37 -08006155 }
6156
6157 if(item.uDataType != QCBOR_TYPE_BIGFLOAT ||
6158 item.val.expAndMantissa.nExponent != INT32_MIN ||
6159 item.val.expAndMantissa.Mantissa.nInt != 100) {
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07006160 return 104;
Laurence Lundblade59289e52019-12-30 13:44:37 -08006161 }
6162
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07006163 uErr = QCBORDecode_GetNext(&DC, &item);
6164 if(uErr != QCBOR_SUCCESS) {
6165 return 105;
Laurence Lundblade59289e52019-12-30 13:44:37 -08006166 }
6167
6168 if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM ||
6169 item.val.expAndMantissa.nExponent != INT32_MAX ||
6170 UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) {
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07006171 return 106;
6172 }
6173
Laurence Lundblade37286c02022-09-03 10:05:02 -07006174#endif /* QCBOR_TAGS_DISABLED */
Laurence Lundblade59289e52019-12-30 13:44:37 -08006175
6176 return 0;
6177}
6178
6179
Laurence Lundblade37286c02022-09-03 10:05:02 -07006180int32_t ExponentAndMantissaDecodeTests(void)
6181{
6182 int32_t rv = ProcessEaMTests();
6183 if(rv) {
6184 return rv;
6185 }
6186
6187 return ExponentAndMantissaDecodeTestsSecondary();
6188}
6189
6190
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08006191static const struct DecodeFailTestInput ExponentAndMantissaFailures[] = {
6192 { "Exponent > INT64_MAX",
6193 QCBOR_DECODE_MODE_NORMAL,
6194 {"\xC4\x82\x1B\x7f\xFF\xFF\xFF\xFF\xFF\xFF\xFF\x1B\x80\xFF\xFF\xFF\xFF\xFF\xFF\xFF", 20},
6195 QCBOR_ERR_BAD_EXP_AND_MANTISSA
6196 },
6197 { "Mantissa > INT64_MAX",
6198 QCBOR_DECODE_MODE_NORMAL,
6199 {"\xC4\x82\x1B\x80\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xC3\x4A\x01\x02\x03\x04\x05\x06\x07\x08\x09\x10", 23},
6200 QCBOR_ERR_BAD_EXP_AND_MANTISSA
6201 },
6202 {
6203 "End of input",
6204 QCBOR_DECODE_MODE_NORMAL,
6205 {"\xC4\x82", 2},
6206 QCBOR_ERR_NO_MORE_ITEMS
6207 },
6208 {"bad content for big num",
6209 QCBOR_DECODE_MODE_NORMAL,
6210 {"\xC4\x82\x01\xc3\x01", 5},
6211 QCBOR_ERR_BAD_OPT_TAG
6212 },
6213 {"bad content for big num",
6214 QCBOR_DECODE_MODE_NORMAL,
6215 {"\xC4\x82\xc2\x01\x1f", 5},
6216 QCBOR_ERR_BAD_INT
6217 },
6218 {"Bad integer for exponent",
6219 QCBOR_DECODE_MODE_NORMAL,
6220 {"\xC4\x82\x01\x1f", 4},
6221 QCBOR_ERR_BAD_INT
6222 },
6223 {"Bad integer for mantissa",
6224 QCBOR_DECODE_MODE_NORMAL,
6225 {"\xC4\x82\x1f\x01", 4},
6226 QCBOR_ERR_BAD_INT
6227 },
6228 {"3 items in array",
6229 QCBOR_DECODE_MODE_NORMAL,
6230 {"\xC4\x83\x03\x01\x02", 5},
6231 QCBOR_ERR_BAD_EXP_AND_MANTISSA},
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08006232#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08006233 {"unterminated indefinite length array",
6234 QCBOR_DECODE_MODE_NORMAL,
6235 {"\xC4\x9f\x03\x01\x02", 5},
6236 QCBOR_ERR_BAD_EXP_AND_MANTISSA
6237 },
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08006238#else /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08006239 {"unterminated indefinite length array",
6240 QCBOR_DECODE_MODE_NORMAL,
6241 {"\xC4\x9f\x03\x01\x02", 5},
6242 QCBOR_ERR_INDEF_LEN_ARRAYS_DISABLED
6243 },
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08006244#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08006245 {"Empty array",
6246 QCBOR_DECODE_MODE_NORMAL,
6247 {"\xC4\x80", 2},
6248 QCBOR_ERR_NO_MORE_ITEMS
6249 },
6250 {"Second is not an integer",
6251 QCBOR_DECODE_MODE_NORMAL,
6252 {"\xC4\x82\x03\x40", 4},
6253 QCBOR_ERR_BAD_EXP_AND_MANTISSA
6254 },
6255 {"First is not an integer",
6256 QCBOR_DECODE_MODE_NORMAL,
6257 {"\xC4\x82\x40", 3},
6258 QCBOR_ERR_BAD_EXP_AND_MANTISSA
6259 },
6260 {"Not an array",
6261 QCBOR_DECODE_MODE_NORMAL,
6262 {"\xC4\xA2", 2},
6263 QCBOR_ERR_BAD_EXP_AND_MANTISSA
6264 }
Laurence Lundblade59289e52019-12-30 13:44:37 -08006265};
6266
6267
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08006268int32_t
6269ExponentAndMantissaDecodeFailTests(void)
Laurence Lundblade59289e52019-12-30 13:44:37 -08006270{
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08006271 return ProcessDecodeFailures(ExponentAndMantissaFailures,
6272 C_ARRAY_COUNT(ExponentAndMantissaFailures,
6273 struct DecodeFailTestInput));
Laurence Lundblade59289e52019-12-30 13:44:37 -08006274}
6275
Laurence Lundbladedd6e76e2021-03-10 01:54:01 -07006276#endif /* QCBOR_DISABLE_EXP_AND_MANTISSA */
Laurence Lundbladebb87be22020-04-09 19:15:32 -07006277
6278
6279
6280/*
6281 Some basic CBOR with map and array used in a lot of tests.
6282 The map labels are all strings
6283
Laurence Lundblade8ffdb742020-05-07 02:49:18 -07006284 {
6285 "first integer": 42,
Laurence Lundbladebb87be22020-04-09 19:15:32 -07006286 "an array of two strings": [
6287 "string1", "string2"
6288 ],
6289 "map in a map": {
6290 "bytes 1": h'78787878',
6291 "bytes 2": h'79797979',
6292 "another int": 98,
6293 "text 2": "lies, damn lies and statistics"
6294 }
Laurence Lundbladeddebabe2020-11-22 11:32:17 -08006295 }
Laurence Lundbladebb87be22020-04-09 19:15:32 -07006296 */
Laurence Lundblade9b334962020-08-27 10:55:53 -07006297
Laurence Lundbladef7a70bc2020-10-24 12:23:25 -07006298int32_t SpiffyDecodeBasicMap(UsefulBufC input)
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07006299{
6300 QCBORItem Item1, Item2, Item3;
6301 int64_t nDecodedInt1, nDecodedInt2;
6302 UsefulBufC B1, B2, S1, S2, S3;
6303
6304 QCBORDecodeContext DCtx;
6305 QCBORError nCBORError;
6306
6307 QCBORDecode_Init(&DCtx, input, 0);
6308
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006309 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07006310
6311 QCBORDecode_GetInt64InMapSZ(&DCtx, "first integer", &nDecodedInt1);
6312
6313 QCBORDecode_EnterMapFromMapSZ(&DCtx, "map in a map");
6314 QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2);
Laurence Lundblade323f8a92020-09-06 19:43:09 -07006315 QCBORDecode_GetByteStringInMapSZ(&DCtx, "bytes 1", &B1);
6316 QCBORDecode_GetByteStringInMapSZ(&DCtx, "bytes 2", &B2);
6317 QCBORDecode_GetTextStringInMapSZ(&DCtx, "text 2", &S1);
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07006318 QCBORDecode_ExitMap(&DCtx);
6319
6320 QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings");
6321 QCBORDecode_GetNext(&DCtx, &Item1);
6322 QCBORDecode_GetNext(&DCtx, &Item2);
6323 if(QCBORDecode_GetNext(&DCtx, &Item3) != QCBOR_ERR_NO_MORE_ITEMS) {
6324 return -400;
6325 }
6326 QCBORDecode_ExitArray(&DCtx);
6327
6328 // Parse the same array again using GetText() instead of GetItem()
6329 QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings");
Laurence Lundblade323f8a92020-09-06 19:43:09 -07006330 QCBORDecode_GetTextString(&DCtx, &S2);
6331 QCBORDecode_GetTextString(&DCtx, &S3);
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07006332 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) {
6333 return 5000;
6334 }
6335 /* QCBORDecode_GetText(&DCtx, &S3);
6336 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_NO_MORE_ITEMS) {
6337 return 5001;
6338 } */
6339
6340 QCBORDecode_ExitArray(&DCtx);
6341
6342 QCBORDecode_ExitMap(&DCtx);
6343
6344 nCBORError = QCBORDecode_Finish(&DCtx);
6345
6346 if(nCBORError) {
6347 return (int32_t)nCBORError;
6348 }
6349
6350 if(nDecodedInt1 != 42) {
6351 return 1001;
6352 }
6353
6354 if(nDecodedInt2 != 98) {
6355 return 1002;
6356 }
6357
6358 if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07006359 UsefulBufCompareToSZ(Item1.val.string, "string1")) {
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07006360 return 1003;
6361 }
6362
6363 if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07006364 UsefulBufCompareToSZ(Item2.val.string, "string2")) {
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07006365 return 1004;
6366 }
6367
Laurence Lundblade9b334962020-08-27 10:55:53 -07006368 if(UsefulBufCompareToSZ(S1, "lies, damn lies and statistics")) {
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07006369 return 1005;
6370 }
6371
6372 if(UsefulBuf_Compare(B1, UsefulBuf_FromSZ("xxxx"))){
6373 return 1006;
6374 }
6375
6376 if(UsefulBuf_Compare(B2, UsefulBuf_FromSZ("yyyy"))){
6377 return 1007;
6378 }
6379
6380 if(UsefulBuf_Compare(S2, UsefulBuf_FromSZ("string1"))){
6381 return 1008;
6382 }
6383
6384 if(UsefulBuf_Compare(S3, UsefulBuf_FromSZ("string2"))){
6385 return 1009;
6386 }
6387
6388 return 0;
6389}
6390
Laurence Lundbladeddebabe2020-11-22 11:32:17 -08006391/*
Laurence Lundbladecc7da412020-12-27 00:09:07 -08006392 {
6393 -75008: h'05083399',
6394 88: [],
6395 100100: {
6396 "sub1": {
6397 10: [
6398 0
6399 ],
6400 -75009: h'A46823990001',
6401 100100: {
6402 "json": "{ \"ueid\", \"xyz\"}",
6403 "subsub": {
6404 100002: h'141813191001'
6405 }
6406 }
Laurence Lundbladeddebabe2020-11-22 11:32:17 -08006407 }
Laurence Lundbladecc7da412020-12-27 00:09:07 -08006408 }
Laurence Lundbladeddebabe2020-11-22 11:32:17 -08006409 }
Laurence Lundbladeddebabe2020-11-22 11:32:17 -08006410 */
6411
6412static const uint8_t spNestedCBOR[] = {
Laurence Lundbladecc7da412020-12-27 00:09:07 -08006413 0xa3, 0x3a, 0x00, 0x01, 0x24, 0xff, 0x44, 0x05,
6414 0x08, 0x33, 0x99, 0x18, 0x58, 0x80, 0x1a, 0x00,
6415 0x01, 0x87, 0x04, 0xa1, 0x64, 0x73, 0x75, 0x62,
6416 0x31, 0xa3, 0x0a, 0x81, 0x00, 0x3a, 0x00, 0x01,
6417 0x25, 0x00, 0x46, 0xa4, 0x68, 0x23, 0x99, 0x00,
6418 0x01, 0x1a, 0x00, 0x01, 0x87, 0x04, 0xa2, 0x64,
6419 0x6a, 0x73, 0x6f, 0x6e, 0x70, 0x7b, 0x20, 0x22,
6420 0x75, 0x65, 0x69, 0x64, 0x22, 0x2c, 0x20, 0x22,
6421 0x78, 0x79, 0x7a, 0x22, 0x7d, 0x66, 0x73, 0x75,
6422 0x62, 0x73, 0x75, 0x62, 0xa1, 0x1a, 0x00, 0x01,
6423 0x86, 0xa2, 0x46, 0x14, 0x18, 0x13, 0x19, 0x10,
6424 0x01
Laurence Lundbladeddebabe2020-11-22 11:32:17 -08006425};
6426
6427/* Get item in multi-level nesting in spNestedCBOR */
6428static int32_t DecodeNestedGetSubSub(QCBORDecodeContext *pDCtx)
6429{
6430 UsefulBufC String;
6431
6432 uint8_t test_oemid_bytes[] = {0x14, 0x18, 0x13, 0x19, 0x10, 0x01};
6433 const struct q_useful_buf_c test_oemid = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(test_oemid_bytes);
6434
6435 QCBORDecode_EnterMapFromMapN(pDCtx, 100100);
6436 QCBORDecode_EnterMap(pDCtx, NULL);
6437 QCBORDecode_EnterMapFromMapN(pDCtx, 100100);
6438 QCBORDecode_EnterMapFromMapSZ(pDCtx, "subsub");
6439 QCBORDecode_GetByteStringInMapN(pDCtx, 100002, &String);
6440 if(QCBORDecode_GetError(pDCtx)) {
6441 return 4001;
6442 }
6443 if(UsefulBuf_Compare(String, test_oemid)) {
6444 return 4002;
6445 }
6446 QCBORDecode_ExitMap(pDCtx);
6447 QCBORDecode_ExitMap(pDCtx);
6448 QCBORDecode_ExitMap(pDCtx);
6449 QCBORDecode_ExitMap(pDCtx);
6450
6451 return 0;
6452}
6453
6454/* Iterations on the zero-length array in spNestedCBOR */
6455static int32_t DecodeNestedGetEmpty(QCBORDecodeContext *pDCtx)
6456{
6457 QCBORItem Item;
6458 QCBORError uErr;
6459
6460 QCBORDecode_EnterArrayFromMapN(pDCtx, 88);
6461 for(int x = 0; x < 20; x++) {
6462 uErr = QCBORDecode_GetNext(pDCtx, &Item);
6463 if(uErr != QCBOR_ERR_NO_MORE_ITEMS) {
6464 return 4100;
6465
6466 }
6467 }
6468 QCBORDecode_ExitArray(pDCtx);
6469 if(QCBORDecode_GetError(pDCtx)) {
6470 return 4101;
6471 }
6472
6473 return 0;
6474}
6475
6476/* Various iterations on the array that contains a zero in spNestedCBOR */
6477static int32_t DecodeNestedGetZero(QCBORDecodeContext *pDCtx)
6478{
6479 QCBORError uErr;
6480
6481 QCBORDecode_EnterMapFromMapN(pDCtx, 100100);
6482 QCBORDecode_EnterMapFromMapSZ(pDCtx, "sub1");
6483 QCBORDecode_EnterArrayFromMapN(pDCtx, 10);
6484 int64_t nInt = 99;
6485 QCBORDecode_GetInt64(pDCtx, &nInt);
6486 if(nInt != 0) {
6487 return 4200;
6488 }
6489 for(int x = 0; x < 20; x++) {
6490 QCBORItem Item;
6491 uErr = QCBORDecode_GetNext(pDCtx, &Item);
6492 if(uErr != QCBOR_ERR_NO_MORE_ITEMS) {
6493 return 4201;
6494
6495 }
6496 }
6497 QCBORDecode_ExitArray(pDCtx);
6498 if(QCBORDecode_GetAndResetError(pDCtx)) {
6499 return 4202;
6500 }
6501 QCBORDecode_EnterArrayFromMapN(pDCtx, 10);
6502 UsefulBufC dD;
6503 QCBORDecode_GetByteString(pDCtx, &dD);
6504 if(QCBORDecode_GetAndResetError(pDCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
6505 return 4203;
6506 }
6507 for(int x = 0; x < 20; x++) {
6508 QCBORDecode_GetByteString(pDCtx, &dD);
6509 uErr = QCBORDecode_GetAndResetError(pDCtx);
6510 if(uErr != QCBOR_ERR_NO_MORE_ITEMS) {
6511 return 4204;
6512 }
6513 }
6514 QCBORDecode_ExitArray(pDCtx);
6515 QCBORDecode_ExitMap(pDCtx);
6516 QCBORDecode_ExitMap(pDCtx);
6517
6518 return 0;
6519}
6520
6521/* Repeatedly enter and exit maps and arrays, go off the end of maps
6522 and arrays and such. */
Laurence Lundbladeb9702452021-03-08 21:02:57 -08006523static int32_t DecodeNestedIterate(void)
Laurence Lundbladeddebabe2020-11-22 11:32:17 -08006524{
6525 QCBORDecodeContext DCtx;
6526 int32_t nReturn;
6527 QCBORError uErr;
6528
6529 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spNestedCBOR), 0);
6530 QCBORDecode_EnterMap(&DCtx, NULL);
6531
6532 for(int j = 0; j < 5; j++) {
6533 for(int i = 0; i < 20; i++) {
6534 nReturn = DecodeNestedGetSubSub(&DCtx);
6535 if(nReturn) {
6536 return nReturn;
6537 }
6538 }
6539
6540 for(int i = 0; i < 20; i++) {
6541 nReturn = DecodeNestedGetEmpty(&DCtx);
6542 if(nReturn ) {
6543 return nReturn;
6544 }
6545 }
6546
6547 for(int i = 0; i < 20; i++) {
6548 nReturn = DecodeNestedGetZero(&DCtx);
6549 if(nReturn ) {
6550 return nReturn;
6551 }
6552 }
6553 }
6554
6555 QCBORDecode_ExitMap(&DCtx);
6556 uErr = QCBORDecode_Finish(&DCtx);
6557 if(uErr) {
6558 return (int32_t)uErr + 4100;
6559 }
6560
6561 return 0;
6562}
6563
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07006564
6565/*
Laurence Lundbladecc7da412020-12-27 00:09:07 -08006566 [
6567 23,
6568 6000,
6569 h'67616C6163746963',
6570 h'686176656E20746F6B656E'
6571 ]
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07006572 */
6573static const uint8_t spSimpleArray[] = {
Laurence Lundblade9b334962020-08-27 10:55:53 -07006574 0x84,
6575 0x17,
6576 0x19, 0x17, 0x70,
6577 0x48, 0x67, 0x61, 0x6C, 0x61, 0x63, 0x74, 0x69, 0x63,
6578 0x4B, 0x68, 0x61, 0x76, 0x65, 0x6E, 0x20, 0x74, 0x6F, 0x6B, 0x65, 0x6E};
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07006579
Laurence Lundbladecc7da412020-12-27 00:09:07 -08006580/* [h'', {}, [], 0] */
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08006581static const uint8_t spArrayOfEmpty[] = {0x84, 0x40, 0xa0, 0x80, 0x00};
6582
Laurence Lundbladecc7da412020-12-27 00:09:07 -08006583/* {} */
Laurence Lundbladee6f15112020-07-23 18:44:16 -07006584static const uint8_t spEmptyMap[] = {0xa0};
6585
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08006586#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundbladecc7da412020-12-27 00:09:07 -08006587/* {} */
Laurence Lundbladee6f15112020-07-23 18:44:16 -07006588static const uint8_t spEmptyInDefinteLengthMap[] = {0xbf, 0xff};
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07006589
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08006590
Laurence Lundbladef0499502020-08-01 11:55:57 -07006591/*
6592 {
6593 0: [],
6594 9: [
6595 [],
6596 []
6597 ],
6598 8: {
6599 1: [],
6600 2: {},
6601 3: []
6602 },
6603 4: {},
6604 5: [],
6605 6: [
6606 [],
6607 []
6608 ]
Laurence Lundbladecc7da412020-12-27 00:09:07 -08006609 }
Laurence Lundbladef0499502020-08-01 11:55:57 -07006610 */
6611static const uint8_t spMapOfEmpty[] = {
Laurence Lundbladecc7da412020-12-27 00:09:07 -08006612 0xa6, 0x00, 0x80, 0x09, 0x82, 0x80, 0x80, 0x08,
6613 0xa3, 0x01, 0x80, 0x02, 0xa0, 0x03, 0x80, 0x04,
6614 0xa0, 0x05, 0x9f, 0xff, 0x06, 0x9f, 0x80, 0x9f,
6615 0xff, 0xff};
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07006616
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08006617#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
6618
6619
Laurence Lundblade93d3f532020-09-28 21:09:12 -07006620/*
6621 Too many tags
Laurence Lundblade93d3f532020-09-28 21:09:12 -07006622 Duplicate label
6623 Integer overflow
6624 Date overflow
6625
Laurence Lundbladecc7da412020-12-27 00:09:07 -08006626 {
6627 1: 224(225(226(227(4(0))))),
Laurence Lundbladecc7da412020-12-27 00:09:07 -08006628 3: -18446744073709551616,
6629 4: 1(1.0e+300),
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07006630 5: 0,
6631 8: 8
Laurence Lundbladecc7da412020-12-27 00:09:07 -08006632 }
Laurence Lundblade93d3f532020-09-28 21:09:12 -07006633 */
6634static const uint8_t spRecoverableMapErrors[] = {
Laurence Lundblade37286c02022-09-03 10:05:02 -07006635#ifndef QCBOR_DISABLE_TAGS
Laurence Lundblade2d493002024-02-01 11:09:17 -07006636 0xa5,
Laurence Lundblade93d3f532020-09-28 21:09:12 -07006637 0x04, 0xc1, 0xfb, 0x7e, 0x37, 0xe4, 0x3c, 0x88, 0x00, 0x75, 0x9c,
Laurence Lundblade37286c02022-09-03 10:05:02 -07006638 0x01, 0xd8, 0xe0, 0xd8, 0xe1, 0xd8, 0xe2, 0xd8, 0xe3, 0xd8, 0x04, 0x00,
6639#else
6640 0xa4,
6641#endif
Laurence Lundblade93d3f532020-09-28 21:09:12 -07006642 0x05, 0x00,
6643 0x05, 0x00,
6644 0x08, 0x08,
Laurence Lundblade93d3f532020-09-28 21:09:12 -07006645};
6646
Laurence Lundbladecc7da412020-12-27 00:09:07 -08006647/* Bad break */
Laurence Lundblade93d3f532020-09-28 21:09:12 -07006648static const uint8_t spUnRecoverableMapError1[] = {
6649 0xa2, 0xff, 0x01, 0x00, 0x02, 0x00
6650};
6651
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08006652#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundbladecc7da412020-12-27 00:09:07 -08006653/* No more items */
Laurence Lundblade93d3f532020-09-28 21:09:12 -07006654static const uint8_t spUnRecoverableMapError2[] = {
6655 0xbf, 0x02, 0xbf, 0xff, 0x01, 0x00, 0x02, 0x00
6656};
6657
Laurence Lundbladecc7da412020-12-27 00:09:07 -08006658/* Hit end because string is too long */
Laurence Lundblade93d3f532020-09-28 21:09:12 -07006659static const uint8_t spUnRecoverableMapError3[] = {
6660 0xbf, 0x02, 0x69, 0x64, 0x64, 0xff
6661};
6662
Laurence Lundbladecc7da412020-12-27 00:09:07 -08006663/* Hit end because string is too long */
Laurence Lundblade93d3f532020-09-28 21:09:12 -07006664static const uint8_t spUnRecoverableMapError4[] = {
6665 0xbf,
6666 0x02, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f,
6667 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f,
6668 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
6669 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
6670 0xff
6671};
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08006672#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
Laurence Lundblade93d3f532020-09-28 21:09:12 -07006673
Laurence Lundblade63926052021-03-29 16:05:51 -07006674const unsigned char not_well_formed_submod_section[] = {
6675 0xa1, 0x14, 0x1f,
6676};
Laurence Lundblade93d3f532020-09-28 21:09:12 -07006677
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07006678
6679/* Array of length 3, but only two items. */
6680const unsigned char spBadConsumeInput[] = {
6681 0x83, 0x00, 0x00
6682};
6683
6684/* Tag nesting too deep. */
6685const unsigned char spBadConsumeInput2[] = {
6686 0x81,
6687 0xD8, 0x37,
6688 0xD8, 0x2C,
6689 0xD8, 0x21,
6690 0xD6,
6691 0xCB,
6692 00
6693};
6694
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07006695
6696const unsigned char spBadConsumeInput4[] = {
6697 0x81, 0x9f, 0x00, 0xff
6698};
6699
6700const unsigned char spBadConsumeInput5[] = {
6701 0xa1, 0x80, 0x00
6702};
6703
Laurence Lundbladec5f45e42023-12-18 09:23:20 -07006704/*
6705 Lots of nesting for various nesting tests.
6706 { 1:1,
6707 2:{
6708 21:21,
6709 22:{
6710 221:[2111, 2112, 2113],
6711 222:222,
6712 223: {}
6713 },
6714 23: 23
6715 },
6716 3:3,
6717 4: [ {} ]
6718 }
6719 */
6720static const uint8_t spNested[] = {
67210xA4, /* Map of 4 */
6722 0x01, 0x01, /* Map entry 1 : 1 */
6723 0x02, 0xA3, /* Map entry 2 : {, an array of 3 */
6724 0x15, 0x15, /* Map entry 21 : 21 */
6725 0x16, 0xA3, /* Map entry 22 : {, a map of 3 */
6726 0x18, 0xDD, 0x83, /* Map entry 221 : [ an array of 3 */
6727 0x19, 0x08, 0x3F, /* Array item 2111 */
6728 0x19, 0x08, 0x40, /* Array item 2112 */
6729 0x19, 0x08, 0x41, /* Array item 2113 */
6730 0x18, 0xDE, 0x18, 0xDE, /* Map entry 222 : 222 */
6731 0x18, 0xDF, 0xA0, /* Map entry 223 : {} */
6732 0x17, 0x17, /* Map entry 23 : 23 */
6733 0x03, 0x03, /* Map entry 3 : 3 */
6734 0x04, 0x81, /* Map entry 4: [, an array of 1 */
6735 0xA0 /* Array entry {}, an empty map */
6736};
6737
6738
6739static int32_t EnterMapCursorTest(void)
6740{
6741 QCBORDecodeContext DCtx;
6742 QCBORItem Item1;
Laurence Lundblade11654912024-05-09 11:49:24 -07006743 int64_t nInt;
6744 QCBORError uErr;
6745
6746 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spNested), 0);
6747 QCBORDecode_EnterMap(&DCtx, NULL);
6748 QCBORDecode_GetInt64InMapN (&DCtx, 3, &nInt);
6749 uErr = QCBORDecode_GetNext(&DCtx, &Item1);
Laurence Lundbladea29f45a2024-05-14 15:55:19 -07006750 if(uErr != QCBOR_SUCCESS) {
6751 return 701;
6752 }
Laurence Lundblade11654912024-05-09 11:49:24 -07006753 if(Item1.uDataType != QCBOR_TYPE_INT64) {
6754 return 700;
6755 }
6756
Laurence Lundbladec5f45e42023-12-18 09:23:20 -07006757
6758 int i;
6759 for(i = 0; i < 13; i++) {
6760 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spNested), 0);
6761 QCBORDecode_EnterMap(&DCtx, NULL);
6762 int j;
6763 /* Move travesal cursor */
6764 for(j = 0; j < i; j++) {
6765 QCBORDecode_GetNext(&DCtx, &Item1);
6766 }
6767 QCBORDecode_EnterMapFromMapN(&DCtx, 2);
6768 QCBORDecode_ExitMap(&DCtx);
6769 QCBORDecode_GetNext(&DCtx, &Item1);
6770 if(Item1.label.int64 != 3) {
6771 return 8000;
6772 }
6773 }
6774
6775 for(i = 0; i < 13; i++) {
6776 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spNested), 0);
6777 QCBORDecode_EnterMap(&DCtx, NULL);
6778 int j;
6779 /* Move travesal cursor */
6780 for(j = 0; j < i; j++) {
6781 QCBORDecode_GetNext(&DCtx, &Item1);
6782 }
6783 QCBORDecode_EnterMapFromMapN(&DCtx, 2);
6784 QCBORDecode_EnterMapFromMapN(&DCtx, 22);
6785 QCBORDecode_ExitMap(&DCtx);
6786 QCBORDecode_GetNext(&DCtx, &Item1);
6787 if(Item1.label.int64 != 23) {
6788 return 8000;
6789 }
6790 }
6791
6792 for(i = 0; i < 13; i++) {
6793 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spNested), 0);
6794 QCBORDecode_EnterMap(&DCtx, NULL);
6795 int j;
6796 /* Move travesal cursor */
6797 for(j = 0; j < i; j++) {
6798 QCBORDecode_GetNext(&DCtx, &Item1);
6799 }
6800 QCBORDecode_EnterMapFromMapN(&DCtx, 2);
6801 QCBORDecode_EnterMapFromMapN(&DCtx, 22);
6802 for(j = 0; j < i; j++) {
6803 QCBORDecode_GetNext(&DCtx, &Item1);
6804 }
6805 QCBORDecode_EnterArrayFromMapN(&DCtx, 221);
6806 QCBORDecode_ExitArray(&DCtx);
6807 QCBORDecode_ExitMap(&DCtx);
6808 QCBORDecode_GetNext(&DCtx, &Item1);
6809 if(Item1.label.int64 != 23) {
6810 return 8000;
6811 }
6812 QCBORDecode_ExitMap(&DCtx);
6813 QCBORDecode_GetNext(&DCtx, &Item1);
6814 if(Item1.label.int64 != 3) {
6815 return 8000;
6816 }
6817 }
6818
6819 return 0;
6820}
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07006821
6822
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03006823int32_t EnterMapTest(void)
Laurence Lundbladebb87be22020-04-09 19:15:32 -07006824{
Laurence Lundbladef0499502020-08-01 11:55:57 -07006825 QCBORItem Item1;
Laurence Lundbladebb87be22020-04-09 19:15:32 -07006826 QCBORDecodeContext DCtx;
Laurence Lundbladef0499502020-08-01 11:55:57 -07006827 int32_t nReturn;
6828 QCBORError uErr;
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07006829
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08006830#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07006831 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spMapOfEmpty), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006832 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07006833
Laurence Lundbladef0499502020-08-01 11:55:57 -07006834
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006835 QCBORDecode_EnterArray(&DCtx, NULL); // Label 0
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07006836 QCBORDecode_ExitArray(&DCtx);
6837
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006838 QCBORDecode_EnterArray(&DCtx, NULL); // Label 9
6839 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07006840 QCBORDecode_ExitArray(&DCtx);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006841 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07006842 QCBORDecode_ExitArray(&DCtx);
6843 QCBORDecode_ExitArray(&DCtx);
6844
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006845 QCBORDecode_EnterMap(&DCtx, NULL); // Label 8
6846 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07006847 QCBORDecode_ExitArray(&DCtx);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006848 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07006849 QCBORDecode_ExitMap(&DCtx);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006850 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07006851 QCBORDecode_ExitArray(&DCtx);
6852 QCBORDecode_ExitMap(&DCtx);
6853
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006854 QCBORDecode_EnterMap(&DCtx, NULL); // Label4
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07006855 QCBORDecode_ExitMap(&DCtx);
6856
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006857 QCBORDecode_EnterArray(&DCtx, NULL); // Label 5
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07006858 QCBORDecode_ExitArray(&DCtx);
6859
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006860 QCBORDecode_EnterArray(&DCtx, NULL); // Label 6
6861 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07006862 QCBORDecode_ExitArray(&DCtx);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006863 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07006864 QCBORDecode_ExitArray(&DCtx);
6865 QCBORDecode_ExitArray(&DCtx);
6866
6867 QCBORDecode_ExitMap(&DCtx);
6868
6869 uErr = QCBORDecode_Finish(&DCtx);
6870 if(uErr != QCBOR_SUCCESS){
6871 return 3011;
6872 }
6873
Laurence Lundbladeec290b82024-06-10 11:10:54 -07006874#ifndef QCBOR_DISABLE_NON_INTEGER_LABELS
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07006875 (void)pValidMapIndefEncoded;
Laurence Lundbladef7a70bc2020-10-24 12:23:25 -07006876 nReturn = SpiffyDecodeBasicMap(UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapIndefEncoded));
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07006877 if(nReturn) {
6878 return nReturn + 20000;
Laurence Lundbladebb87be22020-04-09 19:15:32 -07006879 }
Laurence Lundbladeec290b82024-06-10 11:10:54 -07006880#endif /* ! QCBOR_DISABLE_NON_INTEGER_LABELS */
6881#endif /* ! QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08006882
Laurence Lundbladeec290b82024-06-10 11:10:54 -07006883#ifndef QCBOR_DISABLE_NON_INTEGER_LABELS
6884 QCBORItem ArrayItem;
Laurence Lundbladebb87be22020-04-09 19:15:32 -07006885
Laurence Lundbladef7a70bc2020-10-24 12:23:25 -07006886 nReturn = SpiffyDecodeBasicMap(UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded));
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006887 if(nReturn) {
6888 return nReturn;
6889 }
Laurence Lundbladebb87be22020-04-09 19:15:32 -07006890
Laurence Lundblade8ffdb742020-05-07 02:49:18 -07006891
Laurence Lundblade937ea812020-05-08 11:38:23 -07006892
Laurence Lundblade2f467f92020-10-09 17:50:11 -07006893 // These tests confirm the cursor is at the right place after entering
6894 // a map or array
Laurence Lundblade9b334962020-08-27 10:55:53 -07006895 const UsefulBufC ValidEncodedMap = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded);
Laurence Lundblade937ea812020-05-08 11:38:23 -07006896
6897 // Confirm cursor is at right place
Laurence Lundblade9b334962020-08-27 10:55:53 -07006898 QCBORDecode_Init(&DCtx, ValidEncodedMap, 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006899 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade937ea812020-05-08 11:38:23 -07006900 QCBORDecode_GetNext(&DCtx, &Item1);
6901 if(Item1.uDataType != QCBOR_TYPE_INT64) {
6902 return 2001;
6903 }
6904
6905
Laurence Lundblade9b334962020-08-27 10:55:53 -07006906 QCBORDecode_Init(&DCtx, ValidEncodedMap, 0);
Laurence Lundblade6f3f78e2020-08-31 13:09:14 -07006907 QCBORDecode_VGetNext(&DCtx, &Item1);
6908 QCBORDecode_VGetNext(&DCtx, &Item1);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006909 QCBORDecode_EnterArray(&DCtx, &ArrayItem);
6910 if(ArrayItem.uLabelType != QCBOR_TYPE_TEXT_STRING ||
6911 UsefulBuf_Compare(ArrayItem.label.string,
6912 UsefulBuf_FROM_SZ_LITERAL("an array of two strings"))) {
6913 return 2051;
6914 }
Laurence Lundblade937ea812020-05-08 11:38:23 -07006915 QCBORDecode_GetNext(&DCtx, &Item1);
6916 if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING) {
6917 return 2002;
6918 }
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006919 QCBORDecode_ExitArray(&DCtx);
6920 QCBORDecode_EnterMap(&DCtx, &ArrayItem);
6921 if(ArrayItem.uLabelType != QCBOR_TYPE_TEXT_STRING ||
6922 UsefulBuf_Compare(ArrayItem.label.string,
6923 UsefulBuf_FROM_SZ_LITERAL("map in a map"))) {
6924 return 2052;
6925 }
6926
Laurence Lundblade937ea812020-05-08 11:38:23 -07006927
Laurence Lundblade9b334962020-08-27 10:55:53 -07006928 QCBORDecode_Init(&DCtx, ValidEncodedMap, 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006929 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade64b607e2020-05-13 13:05:57 -07006930 QCBORDecode_GetNext(&DCtx, &Item1);
6931 QCBORDecode_GetNext(&DCtx, &Item1);
6932 QCBORDecode_GetNext(&DCtx, &Item1);
Laurence Lundblade937ea812020-05-08 11:38:23 -07006933 QCBORDecode_EnterMapFromMapSZ(&DCtx, "map in a map");
6934 QCBORDecode_GetNext(&DCtx, &Item1);
6935 if(Item1.uDataType != QCBOR_TYPE_BYTE_STRING) {
6936 return 2003;
6937 }
6938
Laurence Lundblade9b334962020-08-27 10:55:53 -07006939 QCBORDecode_Init(&DCtx, ValidEncodedMap, 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006940 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade937ea812020-05-08 11:38:23 -07006941 QCBORDecode_GetNext(&DCtx, &Item1);
6942 QCBORDecode_GetNext(&DCtx, &Item1);
6943 QCBORDecode_GetNext(&DCtx, &Item1);
6944 QCBORDecode_GetNext(&DCtx, &Item1);
6945 QCBORDecode_GetNext(&DCtx, &Item1);
6946 QCBORDecode_GetNext(&DCtx, &Item1);
6947 QCBORDecode_GetNext(&DCtx, &Item1);
6948 QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings");
6949 QCBORDecode_GetNext(&DCtx, &Item1);
6950 if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING) {
Laurence Lundblade64b607e2020-05-13 13:05:57 -07006951 return 2004;
Laurence Lundblade937ea812020-05-08 11:38:23 -07006952 }
6953
Laurence Lundblade9b334962020-08-27 10:55:53 -07006954 QCBORDecode_Init(&DCtx, ValidEncodedMap, 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006955 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade2b843b52020-06-16 20:51:03 -07006956 QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings");
6957 QCBORDecode_ExitArray(&DCtx);
6958 QCBORDecode_GetNext(&DCtx, &Item1);
6959 if(Item1.uDataType != QCBOR_TYPE_MAP && Item1.uLabelAlloc != QCBOR_TYPE_TEXT_STRING) {
6960 return 2006;
6961 }
6962 QCBORDecode_ExitMap(&DCtx);
6963 if(QCBORDecode_GetNext(&DCtx, &Item1) != QCBOR_ERR_NO_MORE_ITEMS) {
6964 return 2007;
6965 }
Laurence Lundbladeec290b82024-06-10 11:10:54 -07006966#endif /* !QCBOR_DISABLE_NON_INTEGER_LABELS */
Laurence Lundblade2b843b52020-06-16 20:51:03 -07006967
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07006968 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSimpleArray), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006969 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07006970 int64_t nDecodedInt2;
Laurence Lundbladeec290b82024-06-10 11:10:54 -07006971
6972 UsefulBufC String;
6973 QCBORDecode_GetTextStringInMapN(&DCtx, 88, &String);
6974 uErr = QCBORDecode_GetAndResetError(&DCtx);
6975 if(uErr != QCBOR_ERR_MAP_NOT_ENTERED){
6976 return 2009;
6977 }
6978#ifndef QCBOR_DISABLE_NON_INTEGER_LABELS
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07006979 QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2);
6980 uErr = QCBORDecode_GetAndResetError(&DCtx);
6981 if(uErr != QCBOR_ERR_MAP_NOT_ENTERED){
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07006982 return 2008;
6983 }
Laurence Lundbladeec290b82024-06-10 11:10:54 -07006984#endif /* ! QCBOR_DISABLE_NON_INTEGER_LABELS */
Laurence Lundblade937ea812020-05-08 11:38:23 -07006985
Laurence Lundbladee6f15112020-07-23 18:44:16 -07006986
6987 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEmptyMap), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006988 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundbladee6f15112020-07-23 18:44:16 -07006989 // This will fail because the map is empty.
6990 QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2);
6991 uErr = QCBORDecode_GetAndResetError(&DCtx);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006992 if(uErr != QCBOR_ERR_LABEL_NOT_FOUND){
Laurence Lundbladee6f15112020-07-23 18:44:16 -07006993 return 2010;
6994 }
6995 QCBORDecode_ExitMap(&DCtx);
6996 uErr = QCBORDecode_Finish(&DCtx);
6997 if(uErr != QCBOR_SUCCESS){
6998 return 2011;
6999 }
7000
7001
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08007002#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundbladee6f15112020-07-23 18:44:16 -07007003 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEmptyInDefinteLengthMap), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07007004 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundbladee6f15112020-07-23 18:44:16 -07007005 // This will fail because the map is empty.
7006 QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2);
7007 uErr = QCBORDecode_GetAndResetError(&DCtx);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07007008 if(uErr != QCBOR_ERR_LABEL_NOT_FOUND){
Laurence Lundblade085d7952020-07-24 10:26:30 -07007009 return 2012;
Laurence Lundbladee6f15112020-07-23 18:44:16 -07007010 }
7011 QCBORDecode_ExitMap(&DCtx);
7012 uErr = QCBORDecode_Finish(&DCtx);
7013 if(uErr != QCBOR_SUCCESS){
Laurence Lundblade085d7952020-07-24 10:26:30 -07007014 return 2013;
Laurence Lundbladee6f15112020-07-23 18:44:16 -07007015 }
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08007016#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
Laurence Lundbladee6f15112020-07-23 18:44:16 -07007017
Laurence Lundblade5f4e8712020-07-25 11:44:43 -07007018
7019 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spArrayOfEmpty), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07007020 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade323f8a92020-09-06 19:43:09 -07007021 QCBORDecode_GetByteString(&DCtx, &String);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07007022 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade5f4e8712020-07-25 11:44:43 -07007023 QCBORDecode_ExitMap(&DCtx);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07007024 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade5f4e8712020-07-25 11:44:43 -07007025 QCBORDecode_ExitArray(&DCtx);
7026 QCBORDecode_GetInt64(&DCtx, &nDecodedInt2);
7027 QCBORDecode_ExitArray(&DCtx);
7028 uErr = QCBORDecode_Finish(&DCtx);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07007029 if(uErr != QCBOR_SUCCESS) {
Laurence Lundblade5f4e8712020-07-25 11:44:43 -07007030 return 2014;
7031 }
7032
Laurence Lundblade93d3f532020-09-28 21:09:12 -07007033 int64_t nInt;
7034 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spRecoverableMapErrors), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07007035 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade37286c02022-09-03 10:05:02 -07007036#ifndef QCBOR_DISABLE_TAGS
Laurence Lundblade93d3f532020-09-28 21:09:12 -07007037 QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt);
Laurence Lundblade88e9db22020-11-02 03:56:33 -08007038 uErr = QCBORDecode_GetError(&DCtx);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07007039 if(uErr != QCBOR_ERR_TOO_MANY_TAGS) {
7040 return 2021;
7041 }
Laurence Lundblade88e9db22020-11-02 03:56:33 -08007042 if(QCBORDecode_GetNthTagOfLast(&DCtx, 0) != CBOR_TAG_INVALID64) {
7043 return 2121;
7044 }
7045 (void)QCBORDecode_GetAndResetError(&DCtx);
Laurence Lundblade37286c02022-09-03 10:05:02 -07007046#endif
Laurence Lundblade88e9db22020-11-02 03:56:33 -08007047
Laurence Lundblade93d3f532020-09-28 21:09:12 -07007048
Laurence Lundblade37286c02022-09-03 10:05:02 -07007049#ifndef QCBOR_DISABLE_TAGS
Laurence Lundblade93d3f532020-09-28 21:09:12 -07007050 QCBORDecode_GetEpochDateInMapN(&DCtx, 0x04, QCBOR_TAG_REQUIREMENT_TAG, &nInt);
7051 uErr = QCBORDecode_GetAndResetError(&DCtx);
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007052 if(uErr != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_DATE_OVERFLOW)) {
Laurence Lundblade93d3f532020-09-28 21:09:12 -07007053 return 2024;
7054 }
Laurence Lundblade37286c02022-09-03 10:05:02 -07007055#endif
Laurence Lundblade93d3f532020-09-28 21:09:12 -07007056
7057 QCBORDecode_GetInt64InMapN(&DCtx, 0x05, &nInt);
7058 uErr = QCBORDecode_GetAndResetError(&DCtx);
7059 if(uErr != QCBOR_ERR_DUPLICATE_LABEL) {
7060 return 2025;
7061 }
7062
7063 QCBORDecode_GetInt64InMapN(&DCtx, 0x08, &nInt);
7064
7065 QCBORDecode_ExitMap(&DCtx);
7066 uErr = QCBORDecode_Finish(&DCtx);
7067 if(uErr != QCBOR_SUCCESS) {
7068 return 2026;
7069 }
7070
7071 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUnRecoverableMapError1), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07007072 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07007073 QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt);
7074 uErr = QCBORDecode_GetAndResetError(&DCtx);
7075 if(uErr != QCBOR_ERR_BAD_BREAK) {
7076 return 2030;
7077 }
7078
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08007079#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundblade93d3f532020-09-28 21:09:12 -07007080 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUnRecoverableMapError2), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07007081 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07007082 QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt);
7083 uErr = QCBORDecode_GetAndResetError(&DCtx);
7084 if(uErr != QCBOR_ERR_NO_MORE_ITEMS) {
7085 return 2031;
7086 }
7087
7088 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUnRecoverableMapError3), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07007089 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07007090 QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt);
7091 uErr = QCBORDecode_GetAndResetError(&DCtx);
7092 if(uErr != QCBOR_ERR_HIT_END) {
7093 return 2032;
7094 }
7095
7096 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUnRecoverableMapError4), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07007097 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07007098 QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt);
7099 uErr = QCBORDecode_GetAndResetError(&DCtx);
7100 if(uErr != QCBOR_ERR_ARRAY_DECODE_NESTING_TOO_DEEP) {
7101 return 2033;
7102 }
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08007103#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
7104
Laurence Lundbladeec290b82024-06-10 11:10:54 -07007105#ifndef QCBOR_DISABLE_NON_INTEGER_LABELS
Laurence Lundblade732e52d2021-02-22 20:11:01 -07007106 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0);
7107 QCBORDecode_VGetNextConsume(&DCtx, &Item1);
7108 if(Item1.uDataType != QCBOR_TYPE_MAP) {
7109 return 2401;
7110 }
7111 if(QCBORDecode_GetError(&DCtx)) {
7112 return 2402;
7113 }
7114
7115 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0);
7116 QCBORDecode_VGetNext(&DCtx, &Item1);
7117 if(Item1.uDataType != QCBOR_TYPE_MAP ||
7118 Item1.val.uCount != 3 ||
7119 Item1.uNextNestLevel != 1) {
7120 return 2403;
7121 }
7122 if(QCBORDecode_GetError(&DCtx)) {
7123 return 2404;
7124 }
7125 QCBORDecode_VGetNextConsume(&DCtx, &Item1);
7126 if(Item1.uDataType != QCBOR_TYPE_INT64 ||
7127 Item1.uNextNestLevel != 1 ||
7128 Item1.val.int64 != 42) {
7129 return 2405;
7130 }
7131 if(QCBORDecode_GetError(&DCtx)) {
7132 return 2406;
7133 }
7134 QCBORDecode_VGetNextConsume(&DCtx, &Item1);
7135 if(Item1.uDataType != QCBOR_TYPE_ARRAY ||
7136 Item1.uNestingLevel != 1 ||
7137 Item1.uNextNestLevel != 1 ||
7138 Item1.val.uCount != 2) {
7139 return 2407;
7140 }
7141 if(QCBORDecode_GetError(&DCtx)) {
7142 return 2408;
7143 }
7144 QCBORDecode_VGetNextConsume(&DCtx, &Item1);
7145 if(Item1.uDataType != QCBOR_TYPE_MAP ||
7146 Item1.uNestingLevel != 1 ||
7147 Item1.uNextNestLevel != 0 ||
7148 Item1.val.uCount != 4) {
7149 return 2409;
7150 }
7151 if(QCBORDecode_GetError(&DCtx)) {
7152 return 2410;
7153 }
Laurence Lundbladeec290b82024-06-10 11:10:54 -07007154#endif /* ! QCBOR_DISABLE_NON_INTEGER_LABELS */
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07007155
Laurence Lundbladeddebabe2020-11-22 11:32:17 -08007156 nReturn = DecodeNestedIterate();
7157
Laurence Lundblade63926052021-03-29 16:05:51 -07007158
7159 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(not_well_formed_submod_section), 0);
7160 QCBORDecode_EnterMap(&DCtx, NULL);
7161 QCBORDecode_EnterMapFromMapN(&DCtx, 20);
7162 if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_BAD_INT) {
7163 return 2500;
7164 }
7165
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07007166 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBadConsumeInput), 0);
7167 QCBORDecode_VGetNextConsume(&DCtx, &Item1);
7168 if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_NO_MORE_ITEMS) {
7169 return 2600;
7170 }
7171
Laurence Lundblade37286c02022-09-03 10:05:02 -07007172#ifndef QCBOR_DISABLE_TAGS
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07007173 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBadConsumeInput2), 0);
7174 QCBORDecode_VGetNextConsume(&DCtx, &Item1);
7175 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) {
7176 return 2700;
7177 }
Laurence Lundblade37286c02022-09-03 10:05:02 -07007178#endif
7179
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07007180
7181 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBadConsumeInput4), 0);
7182 QCBORDecode_VGetNextConsume(&DCtx, &Item1);
7183#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
7184 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) {
7185 return 2900;
7186 }
7187#else
7188 if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_INDEF_LEN_ARRAYS_DISABLED) {
7189 return 2901;
7190 }
7191#endif
7192
7193 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBadConsumeInput5), 0);
7194 QCBORDecode_VGetNextConsume(&DCtx, &Item1);
7195 if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_MAP_LABEL_TYPE) {
7196 return 3000;
7197 }
7198
Laurence Lundbladec5f45e42023-12-18 09:23:20 -07007199 nReturn = EnterMapCursorTest();
7200
Laurence Lundbladeddebabe2020-11-22 11:32:17 -08007201 return nReturn;
Laurence Lundblade9c905e82020-04-25 11:31:38 -07007202}
7203
7204
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007205struct NumberConversion {
7206 char *szDescription;
7207 UsefulBufC CBOR;
7208 int64_t nConvertedToInt64;
7209 QCBORError uErrorInt64;
7210 uint64_t uConvertToUInt64;
7211 QCBORError uErrorUint64;
7212 double dConvertToDouble;
7213 QCBORError uErrorDouble;
7214};
7215
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007216#ifndef QCBOR_DISABLE_EXP_AND_MANTISSA
7217#define EXP_AND_MANTISSA_ERROR(x) x
7218#else
7219#define EXP_AND_MANTISSA_ERROR(x) QCBOR_ERR_UNEXPECTED_TYPE
7220#endif
7221
7222
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07007223static const struct NumberConversion NumberConversions[] = {
Laurence Lundblade37286c02022-09-03 10:05:02 -07007224#ifndef QCBOR_DISABLE_TAGS
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007225 {
Laurence Lundblade4e808ba2022-12-29 12:45:20 -07007226 "Big float: INT64_MIN * 2e-1 to test handling of INT64_MIN",
7227 {(uint8_t[]){0xC5, 0x82, 0x20,
7228 0x3B, 0x7f, 0xff, 0xff, 0xff, 0xff, 0x0ff, 0xff, 0xff,
7229 }, 15},
7230 -4611686018427387904, /* INT64_MIN / 2 */
7231 EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS),
7232 0,
7233 EXP_AND_MANTISSA_ERROR(QCBOR_ERR_NUMBER_SIGN_CONVERSION),
7234 -4.6116860184273879E+18,
7235 FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS))
7236 },
7237 {
Laurence Lundblade784b54b2020-08-10 01:24:52 -07007238 "too large to fit into int64_t",
7239 {(uint8_t[]){0xc3, 0x48, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 10},
7240 0,
7241 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
7242 0,
7243 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
7244 ((double)INT64_MIN) + 1 ,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007245 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)
Laurence Lundblade784b54b2020-08-10 01:24:52 -07007246 },
7247 {
7248 "largest negative int that fits in int64_t",
7249 {(uint8_t[]){0xc3, 0x48, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, 10},
7250 INT64_MIN,
7251 QCBOR_SUCCESS,
7252 0,
7253 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
7254 (double)INT64_MIN,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007255 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)
Laurence Lundblade784b54b2020-08-10 01:24:52 -07007256 },
7257 {
Laurence Lundbladeda095972020-06-06 18:35:33 -07007258 "negative bignum -1",
7259 {(uint8_t[]){0xc3, 0x41, 0x00}, 3},
7260 -1,
7261 QCBOR_SUCCESS,
7262 0,
7263 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
7264 -1.0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007265 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)
Laurence Lundbladeda095972020-06-06 18:35:33 -07007266 },
7267 {
Laurence Lundblade9ab5abb2020-05-20 12:10:45 -07007268 "Decimal Fraction with positive bignum 257 * 10e3",
Laurence Lundblade887add82020-05-17 05:50:34 -07007269 {(uint8_t[]){0xC4, 0x82, 0x1B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03,
7270 0xC2, 0x42, 0x01, 0x01}, 15},
7271 257000,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007272 EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS),
Laurence Lundblade887add82020-05-17 05:50:34 -07007273 257000,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007274 EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS),
Laurence Lundblade887add82020-05-17 05:50:34 -07007275 257000.0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007276 FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS))
Laurence Lundblade887add82020-05-17 05:50:34 -07007277 },
7278 {
Laurence Lundbladeda095972020-06-06 18:35:33 -07007279 "bigfloat with negative bignum -258 * 2e3",
Laurence Lundblade887add82020-05-17 05:50:34 -07007280 {(uint8_t[]){0xC5, 0x82, 0x1B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03,
7281 0xC3, 0x42, 0x01, 0x01}, 15},
Laurence Lundbladeda095972020-06-06 18:35:33 -07007282 -2064,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007283 EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS),
Laurence Lundblade887add82020-05-17 05:50:34 -07007284 0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007285 EXP_AND_MANTISSA_ERROR(QCBOR_ERR_NUMBER_SIGN_CONVERSION),
Laurence Lundbladeda095972020-06-06 18:35:33 -07007286 -2064.0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007287 FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS))
Laurence Lundblade887add82020-05-17 05:50:34 -07007288 },
7289 {
Laurence Lundblade9ab5abb2020-05-20 12:10:45 -07007290 "bigfloat with positive bignum 257 * 2e3",
Laurence Lundblade887add82020-05-17 05:50:34 -07007291 {(uint8_t[]){0xC5, 0x82, 0x1B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03,
7292 0xC2, 0x42, 0x01, 0x01}, 15},
7293 2056,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007294 EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS),
Laurence Lundblade887add82020-05-17 05:50:34 -07007295 2056,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007296 EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS),
Laurence Lundblade887add82020-05-17 05:50:34 -07007297 2056.0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007298 FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS))
Laurence Lundblade887add82020-05-17 05:50:34 -07007299 },
7300 {
Laurence Lundbladeda095972020-06-06 18:35:33 -07007301 "negative bignum 0xc349010000000000000000 -18446744073709551617",
Laurence Lundblade887add82020-05-17 05:50:34 -07007302 {(uint8_t[]){0xc3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 11},
7303 0,
7304 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
7305 0,
7306 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
7307 -18446744073709551617.0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007308 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)
Laurence Lundblade887add82020-05-17 05:50:34 -07007309 },
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08007310#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
Laurence Lundblade887add82020-05-17 05:50:34 -07007311 {
Laurence Lundblade313b2862020-05-16 01:23:06 -07007312 "Positive bignum 0x01020304 indefinite length string",
7313 {(uint8_t[]){0xC2, 0x5f, 0x42, 0x01, 0x02, 0x41, 0x03, 0x41, 0x04, 0xff}, 10},
7314 0x01020304,
7315 QCBOR_SUCCESS,
7316 0x01020304,
7317 QCBOR_SUCCESS,
7318 16909060.0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007319 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)
Laurence Lundblade313b2862020-05-16 01:23:06 -07007320 },
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08007321#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
Laurence Lundblade313b2862020-05-16 01:23:06 -07007322 {
Laurence Lundblade887add82020-05-17 05:50:34 -07007323 "Decimal Fraction with neg bignum [9223372036854775807, -4759477275222530853137]",
Laurence Lundblade313b2862020-05-16 01:23:06 -07007324 {(uint8_t[]){0xC4, 0x82, 0x1B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
7325 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,}, 23},
7326 0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007327 EXP_AND_MANTISSA_ERROR(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW),
Laurence Lundblade313b2862020-05-16 01:23:06 -07007328 0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007329 EXP_AND_MANTISSA_ERROR(QCBOR_ERR_NUMBER_SIGN_CONVERSION),
Laurence Lundblade313b2862020-05-16 01:23:06 -07007330 -INFINITY,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007331 FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS))
Laurence Lundblade313b2862020-05-16 01:23:06 -07007332 },
7333 {
7334 "big float [9223372036854775806, 9223372036854775806]",
7335 {(uint8_t[]){0xC5, 0x82, 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE,
7336 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE}, 20},
7337 0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007338 EXP_AND_MANTISSA_ERROR(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW),
Laurence Lundblade313b2862020-05-16 01:23:06 -07007339 0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007340 EXP_AND_MANTISSA_ERROR(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW),
Laurence Lundblade313b2862020-05-16 01:23:06 -07007341 INFINITY,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007342 FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS))
Laurence Lundblade313b2862020-05-16 01:23:06 -07007343 },
7344 {
Laurence Lundblade983500d2020-05-14 11:49:34 -07007345 "Big float 3 * 2^^2",
7346 {(uint8_t[]){0xC5, 0x82, 0x02, 0x03}, 4},
7347 12,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007348 EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS),
Laurence Lundblade983500d2020-05-14 11:49:34 -07007349 12,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007350 EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS),
Laurence Lundblade983500d2020-05-14 11:49:34 -07007351 12.0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007352 FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS))
Laurence Lundblade983500d2020-05-14 11:49:34 -07007353 },
Laurence Lundblade983500d2020-05-14 11:49:34 -07007354 {
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007355 "Decimal fraction 3/10",
7356 {(uint8_t[]){0xC4, 0x82, 0x20, 0x03}, 4},
7357 0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007358 EXP_AND_MANTISSA_ERROR(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW),
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007359 0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007360 EXP_AND_MANTISSA_ERROR(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW),
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007361 0.30000000000000004,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007362 FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS))
Laurence Lundbladedfd49fc2020-09-01 14:17:16 -07007363 },
7364 {
Laurence Lundblade11fd78b2020-09-01 22:13:27 -07007365 "extreme pos bignum",
7366 {(uint8_t[]){0xc2, 0x59, 0x01, 0x90,
7367 // 50 rows of 8 is 400 digits.
7368 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7369 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7370 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7371 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7372 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7373 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7374 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7375 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7376 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7377 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7378 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7379 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7380 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7381 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7382 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7383 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7384 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7385 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7386 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7387 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7388 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7389 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7390 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7391 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7392 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7393 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7394 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7395 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7396 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7397 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7398 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7399 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7400 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7401 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7402 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7403 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7404 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7405 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7406 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7407 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7408 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7409 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7410 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7411 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7412 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7413 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7414 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7415 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7416 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
gtravisb787e82a2023-11-30 18:38:21 -08007417 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0},
Laurence Lundblade11fd78b2020-09-01 22:13:27 -07007418 404},
7419 0,
7420 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
7421 0,
7422 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
Laurence Lundblade11fd78b2020-09-01 22:13:27 -07007423 INFINITY,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007424 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS),
Laurence Lundblade11fd78b2020-09-01 22:13:27 -07007425 },
7426
7427 {
7428 "extreme neg bignum",
7429 {(uint8_t[]){0xc3, 0x59, 0x01, 0x90,
7430 // 50 rows of 8 is 400 digits.
7431 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7432 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7433 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7434 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7435 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7436 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7437 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7438 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7439 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7440 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7441 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7442 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7443 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7444 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7445 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7446 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7447 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7448 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7449 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7450 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7451 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7452 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7453 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7454 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7455 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7456 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7457 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7458 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7459 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7460 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7461 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7462 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7463 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7464 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7465 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7466 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7467 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7468 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7469 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7470 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7471 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7472 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7473 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7474 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7475 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7476 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7477 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7478 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7479 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
gtravisb787e82a2023-11-30 18:38:21 -08007480 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0},
Laurence Lundblade11fd78b2020-09-01 22:13:27 -07007481 404},
7482 0,
7483 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
7484 0,
7485 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
Laurence Lundblade11fd78b2020-09-01 22:13:27 -07007486 -INFINITY,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007487 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)
Laurence Lundblade11fd78b2020-09-01 22:13:27 -07007488 },
Laurence Lundblade51722fd2020-09-02 13:01:33 -07007489
7490 {
7491 "big float underflow [9223372036854775806, -9223372036854775806]",
7492 {(uint8_t[]){
7493 0xC5, 0x82,
7494 0x3B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE,
7495 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE}, 20},
Laurence Lundblade51722fd2020-09-02 13:01:33 -07007496 0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007497 EXP_AND_MANTISSA_ERROR(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW),
Laurence Lundblade51722fd2020-09-02 13:01:33 -07007498 0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007499 EXP_AND_MANTISSA_ERROR(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW),
Laurence Lundblade51722fd2020-09-02 13:01:33 -07007500 0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007501 FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS))
Laurence Lundblade51722fd2020-09-02 13:01:33 -07007502 },
7503
7504 {
7505 "bigfloat that evaluates to -INFINITY",
7506 {(uint8_t[]){
7507 0xC5, 0x82,
7508 0x1B, 0x70, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03,
7509 0xC3, 0x42, 0x01, 0x01}, 15},
Laurence Lundblade51722fd2020-09-02 13:01:33 -07007510 0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007511 EXP_AND_MANTISSA_ERROR(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW),
Laurence Lundblade51722fd2020-09-02 13:01:33 -07007512 0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007513 EXP_AND_MANTISSA_ERROR(QCBOR_ERR_NUMBER_SIGN_CONVERSION),
Laurence Lundblade51722fd2020-09-02 13:01:33 -07007514 -INFINITY,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007515 FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS))
Laurence Lundblade51722fd2020-09-02 13:01:33 -07007516 },
Laurence Lundblade37286c02022-09-03 10:05:02 -07007517 {
7518 "Positive bignum 0xffff",
7519 {(uint8_t[]){0xC2, 0x42, 0xff, 0xff}, 4},
7520 65536-1,
7521 QCBOR_SUCCESS,
7522 0xffff,
7523 QCBOR_SUCCESS,
7524 65535.0,
7525 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)
7526 },
7527#endif /* QCBOR_DISABLE_TAGS */
7528 {
7529 "Positive integer 18446744073709551615",
7530 {(uint8_t[]){0x1b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, 9},
7531 0,
7532 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
7533 18446744073709551615ULL,
7534 QCBOR_SUCCESS,
7535 18446744073709551615.0,
7536 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)
7537 },
7538
7539 {
7540 "Postive integer 0",
7541 {(uint8_t[]){0x0}, 1},
7542 0LL,
7543 QCBOR_SUCCESS,
7544 0ULL,
7545 QCBOR_SUCCESS,
7546 0.0,
7547 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)
7548 },
7549 {
Laurence Lundblade2d493002024-02-01 11:09:17 -07007550 "Negative integer -9223372036854775808",
7551 {(uint8_t[]){0x3b, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, 9},
Laurence Lundblade37286c02022-09-03 10:05:02 -07007552 -9223372036854775807-1, // INT64_MIN
7553 QCBOR_SUCCESS,
7554 0ULL,
7555 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
7556 -9223372036854775808.0,
7557 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)
7558 },
7559 {
Laurence Lundblade2d493002024-02-01 11:09:17 -07007560 "Negative integer -18446744073709551616",
7561 {(uint8_t[]){0x3b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, 9},
7562 0ULL,
7563 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
7564 0ULL,
7565 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
7566 -18446744073709551616.0,
7567 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)
7568 },
7569 {
Laurence Lundblade37286c02022-09-03 10:05:02 -07007570 "Double Floating point value 100.3",
7571 {(uint8_t[]){0xfb, 0x40, 0x59, 0x13, 0x33, 0x33, 0x33, 0x33, 0x33}, 9},
7572 100L,
7573 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS),
7574 100ULL,
7575 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS),
7576 100.3,
7577 FLOAT_ERR_CODE_NO_FLOAT(QCBOR_SUCCESS),
7578 },
7579 {
7580 "Floating point value NaN 0xfa7fc00000",
7581 {(uint8_t[]){0xfa, 0x7f, 0xc0, 0x00, 0x00}, 5},
7582 0,
7583 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_FLOAT_EXCEPTION),
7584 0,
7585 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_FLOAT_EXCEPTION),
7586 NAN,
7587 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS),
7588 },
7589 {
7590 "half-precision Floating point value -4",
7591 {(uint8_t[]){0xf9, 0xc4, 0x00}, 3},
7592 // Normal case with all enabled.
7593 -4,
7594 FLOAT_ERR_CODE_NO_HALF_PREC_NO_FLOAT_HW(QCBOR_SUCCESS),
7595 0,
7596 FLOAT_ERR_CODE_NO_HALF_PREC_NO_FLOAT_HW(QCBOR_ERR_NUMBER_SIGN_CONVERSION),
7597 -4.0,
7598 FLOAT_ERR_CODE_NO_HALF_PREC(QCBOR_SUCCESS)
7599 },
7600 {
7601 "+inifinity single precision",
7602 {(uint8_t[]){0xfa, 0x7f, 0x80, 0x00, 0x00}, 5},
7603 0,
7604 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_FLOAT_EXCEPTION),
7605 0,
7606 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW),
7607 INFINITY,
7608 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)
7609 },
7610
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007611};
Laurence Lundblade9c905e82020-04-25 11:31:38 -07007612
7613
7614
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08007615
7616static int32_t SetUpDecoder(QCBORDecodeContext *DCtx, UsefulBufC CBOR, UsefulBuf Pool)
7617{
7618 QCBORDecode_Init(DCtx, CBOR, QCBOR_DECODE_MODE_NORMAL);
7619#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
7620 if(QCBORDecode_SetMemPool(DCtx, Pool, 0)) {
7621 return 1;
7622 }
7623#else /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
7624 (void)Pool;
7625#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
7626 return 0;
7627}
7628
7629
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03007630int32_t IntegerConvertTest(void)
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007631{
Laurence Lundbladeeb3cdef2024-02-17 20:38:55 -08007632 uint64_t uInt;
7633
7634
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08007635 const int nNumTests = C_ARRAY_COUNT(NumberConversions,
7636 struct NumberConversion);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007637
Laurence Lundblade9ab5abb2020-05-20 12:10:45 -07007638 for(int nIndex = 0; nIndex < nNumTests; nIndex++) {
7639 const struct NumberConversion *pF = &NumberConversions[nIndex];
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07007640
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007641 // Set up the decoding context including a memory pool so that
7642 // indefinite length items can be checked
7643 QCBORDecodeContext DCtx;
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007644 UsefulBuf_MAKE_STACK_UB(Pool, 100);
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07007645
7646 /* ----- test conversion to int64_t ------ */
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08007647 if(SetUpDecoder(&DCtx, pF->CBOR, Pool)) {
7648 return (int32_t)(3333+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007649 }
7650
7651 int64_t nInt;
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007652 QCBORDecode_GetInt64ConvertAll(&DCtx, 0xffff, &nInt);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07007653 if(QCBORDecode_GetError(&DCtx) != pF->uErrorInt64) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07007654 return (int32_t)(2000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007655 }
7656 if(pF->uErrorInt64 == QCBOR_SUCCESS && pF->nConvertedToInt64 != nInt) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07007657 return (int32_t)(3000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007658 }
7659
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07007660 /* ----- test conversion to uint64_t ------ */
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08007661 if(SetUpDecoder(&DCtx, pF->CBOR, Pool)) {
7662 return (int32_t)(3333+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007663 }
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08007664
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007665 QCBORDecode_GetUInt64ConvertAll(&DCtx, 0xffff, &uInt);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07007666 if(QCBORDecode_GetError(&DCtx) != pF->uErrorUint64) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07007667 return (int32_t)(4000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007668 }
7669 if(pF->uErrorUint64 == QCBOR_SUCCESS && pF->uConvertToUInt64 != uInt) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07007670 return (int32_t)(5000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007671 }
7672
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07007673 /* ----- test conversion to double ------ */
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08007674 if(SetUpDecoder(&DCtx, pF->CBOR, Pool)) {
7675 return (int32_t)(3333+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007676 }
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007677
7678#ifndef USEFULBUF_DISABLE_ALL_FLOAT
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007679 double d;
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007680 QCBORDecode_GetDoubleConvertAll(&DCtx, 0xffff, &d);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07007681 if(QCBORDecode_GetError(&DCtx) != pF->uErrorDouble) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07007682 return (int32_t)(6000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007683 }
7684 if(pF->uErrorDouble == QCBOR_SUCCESS) {
7685 if(isnan(pF->dConvertToDouble)) {
Laurence Lundblade983500d2020-05-14 11:49:34 -07007686 // NaN's can't be compared for equality. A NaN is
7687 // never equal to anything including another NaN
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007688 if(!isnan(d)) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07007689 return (int32_t)(7000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007690 }
7691 } else {
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007692 if(pF->dConvertToDouble != d) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07007693 return (int32_t)(8000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007694 }
7695 }
7696 }
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007697#endif /* USEFULBUF_DISABLE_ALL_FLOAT */
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007698 }
7699
7700 return 0;
7701}
7702
Laurence Lundbladea8758502022-05-15 17:57:46 -07007703#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
7704
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03007705int32_t CBORTestIssue134(void)
David Navarro9123e5b2022-03-28 16:04:03 +02007706{
7707 QCBORDecodeContext DCtx;
7708 QCBORItem Item;
7709 QCBORError uCBORError;
7710 const uint8_t spTestIssue134[] = { 0x5F, 0x40, 0xFF };
Laurence Lundblade9c905e82020-04-25 11:31:38 -07007711
David Navarro9123e5b2022-03-28 16:04:03 +02007712 QCBORDecode_Init(&DCtx,
7713 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTestIssue134),
7714 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade97c61bf2020-05-02 11:24:06 -07007715
David Navarro9123e5b2022-03-28 16:04:03 +02007716 UsefulBuf_MAKE_STACK_UB(StringBuf, 200);
7717 QCBORDecode_SetMemPool(&DCtx, StringBuf, false);
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03007718
David Navarro9123e5b2022-03-28 16:04:03 +02007719 do {
7720 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
7721 } while (QCBOR_SUCCESS == uCBORError);
7722
7723 uCBORError = QCBORDecode_Finish(&DCtx);
7724
Laurence Lundblade11ea3612022-07-01 13:26:23 -07007725 return (int32_t)uCBORError;
David Navarro9123e5b2022-03-28 16:04:03 +02007726}
Laurence Lundblade97c61bf2020-05-02 11:24:06 -07007727
Laurence Lundbladea8758502022-05-15 17:57:46 -07007728#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
7729
Laurence Lundblade37286c02022-09-03 10:05:02 -07007730
7731
7732static const uint8_t spSequenceTestInput[] = {
7733 /* 1. The valid date string "1985-04-12" */
7734 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string
7735
7736 /* 2. */
7737 0x00,
7738
7739 /* 3. A valid epoch date, 1400000000; Tue, 13 May 2014 16:53:20 GMT */
7740 0x1a, 0x53, 0x72, 0x4E, 0x00,
7741
7742 /* 4. */
7743 0x62, 'h', 'i',
7744};
7745
7746
Laurence Lundbladee3553422020-05-02 11:11:17 -07007747int32_t CBORSequenceDecodeTests(void)
7748{
7749 QCBORDecodeContext DCtx;
Laurence Lundblade87495732021-02-26 10:05:55 -07007750 QCBORItem Item;
7751 QCBORError uCBORError;
7752 size_t uConsumed;
Laurence Lundbladee3553422020-05-02 11:11:17 -07007753
7754 // --- Test a sequence with extra bytes ---
Laurence Lundblade9b334962020-08-27 10:55:53 -07007755
Laurence Lundbladee3553422020-05-02 11:11:17 -07007756 QCBORDecode_Init(&DCtx,
Laurence Lundblade37286c02022-09-03 10:05:02 -07007757 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSequenceTestInput),
Laurence Lundbladee3553422020-05-02 11:11:17 -07007758 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade9b334962020-08-27 10:55:53 -07007759
Laurence Lundblade37286c02022-09-03 10:05:02 -07007760 // Get 1.
Laurence Lundbladee3553422020-05-02 11:11:17 -07007761 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
7762 if(uCBORError != QCBOR_SUCCESS) {
7763 return 1;
7764 }
Laurence Lundblade37286c02022-09-03 10:05:02 -07007765 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ) {
Laurence Lundbladee3553422020-05-02 11:11:17 -07007766 return 2;
7767 }
Laurence Lundbladec7114722020-08-13 05:11:40 -07007768
Laurence Lundblade87495732021-02-26 10:05:55 -07007769 uCBORError = QCBORDecode_PartialFinish(&DCtx, &uConsumed);
7770 if(uCBORError != QCBOR_ERR_EXTRA_BYTES ||
Laurence Lundblade37286c02022-09-03 10:05:02 -07007771 uConsumed != 11) {
7772 return 102;
7773 }
7774
7775 // Get 2.
7776 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
7777 if(uCBORError != QCBOR_SUCCESS) {
7778 return 66;
7779 }
7780
7781 uCBORError = QCBORDecode_PartialFinish(&DCtx, &uConsumed);
7782 if(uCBORError != QCBOR_ERR_EXTRA_BYTES ||
Laurence Lundblade87495732021-02-26 10:05:55 -07007783 uConsumed != 12) {
7784 return 102;
7785 }
7786
Laurence Lundblade37286c02022-09-03 10:05:02 -07007787 // Get 3.
Laurence Lundbladec7114722020-08-13 05:11:40 -07007788 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundbladee3553422020-05-02 11:11:17 -07007789 if(uCBORError != QCBOR_SUCCESS) {
7790 return 2;
7791 }
Laurence Lundblade37286c02022-09-03 10:05:02 -07007792 if(Item.uDataType != QCBOR_TYPE_INT64) {
Laurence Lundbladee3553422020-05-02 11:11:17 -07007793 return 3;
7794 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07007795
Laurence Lundbladee3553422020-05-02 11:11:17 -07007796 // A sequence can have stuff at the end that may
7797 // or may not be valid CBOR. The protocol decoder knows
7798 // when to stop by definition of the protocol, not
7799 // when the top-level map or array is ended.
7800 // Finish still has to be called to know that
7801 // maps and arrays (if there were any) were closed
7802 // off correctly. When called like this it
7803 // must return the error QCBOR_ERR_EXTRA_BYTES.
7804 uCBORError = QCBORDecode_Finish(&DCtx);
7805 if(uCBORError != QCBOR_ERR_EXTRA_BYTES) {
7806 return 4;
7807 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07007808
Laurence Lundbladee3553422020-05-02 11:11:17 -07007809 // --- Test an empty input ----
7810 uint8_t empty[1];
7811 UsefulBufC Empty = {empty, 0};
7812 QCBORDecode_Init(&DCtx,
7813 Empty,
7814 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade9b334962020-08-27 10:55:53 -07007815
Laurence Lundbladee3553422020-05-02 11:11:17 -07007816 uCBORError = QCBORDecode_Finish(&DCtx);
7817 if(uCBORError != QCBOR_SUCCESS) {
7818 return 5;
7819 }
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07007820
7821
Laurence Lundbladee3553422020-05-02 11:11:17 -07007822 // --- Sequence with unclosed indefinite length array ---
7823 static const uint8_t xx[] = {0x01, 0x9f, 0x02};
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07007824
Laurence Lundbladee3553422020-05-02 11:11:17 -07007825 QCBORDecode_Init(&DCtx,
7826 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(xx),
7827 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07007828
Laurence Lundbladee3553422020-05-02 11:11:17 -07007829 // Get the first item
7830 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
7831 if(uCBORError != QCBOR_SUCCESS) {
7832 return 7;
7833 }
7834 if(Item.uDataType != QCBOR_TYPE_INT64) {
7835 return 8;
7836 }
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07007837
Laurence Lundbladee3553422020-05-02 11:11:17 -07007838 // Get a second item
7839 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08007840#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundbladee3553422020-05-02 11:11:17 -07007841 if(uCBORError != QCBOR_SUCCESS) {
7842 return 9;
7843 }
7844 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
7845 return 10;
7846 }
7847
7848 // Try to finish before consuming all bytes to confirm
7849 // that the still-open error is returned.
7850 uCBORError = QCBORDecode_Finish(&DCtx);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07007851 if(uCBORError != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
Laurence Lundbladee3553422020-05-02 11:11:17 -07007852 return 11;
7853 }
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08007854#else /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
7855 if(uCBORError != QCBOR_ERR_INDEF_LEN_ARRAYS_DISABLED) {
7856 return 20;
7857 }
7858#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
Laurence Lundbladee3553422020-05-02 11:11:17 -07007859
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07007860
Laurence Lundbladee3553422020-05-02 11:11:17 -07007861 // --- Sequence with a closed indefinite length array ---
7862 static const uint8_t yy[] = {0x01, 0x9f, 0xff};
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07007863
Laurence Lundbladee3553422020-05-02 11:11:17 -07007864 QCBORDecode_Init(&DCtx,
7865 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(yy),
7866 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07007867
Laurence Lundbladee3553422020-05-02 11:11:17 -07007868 // Get the first item
7869 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
7870 if(uCBORError != QCBOR_SUCCESS) {
7871 return 12;
7872 }
7873 if(Item.uDataType != QCBOR_TYPE_INT64) {
7874 return 13;
7875 }
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07007876
Laurence Lundbladee3553422020-05-02 11:11:17 -07007877 // Get a second item
7878 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08007879#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
7880
Laurence Lundbladee3553422020-05-02 11:11:17 -07007881 if(uCBORError != QCBOR_SUCCESS) {
7882 return 14;
7883 }
7884 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
7885 return 15;
7886 }
7887
7888 // Try to finish before consuming all bytes to confirm
7889 // that the still-open error is returned.
7890 uCBORError = QCBORDecode_Finish(&DCtx);
7891 if(uCBORError != QCBOR_SUCCESS) {
7892 return 16;
7893 }
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08007894#else /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
7895 if(uCBORError != QCBOR_ERR_INDEF_LEN_ARRAYS_DISABLED) {
7896 return 20;
7897 }
7898#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
Laurence Lundbladee3553422020-05-02 11:11:17 -07007899
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07007900
Laurence Lundbladee3553422020-05-02 11:11:17 -07007901 return 0;
7902}
7903
Laurence Lundbladee15326f2020-06-15 15:50:23 -07007904
Laurence Lundblade70ecead2020-06-15 19:40:06 -07007905
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03007906int32_t IntToTests(void)
Laurence Lundbladee15326f2020-06-15 15:50:23 -07007907{
7908 int nErrCode;
7909 int32_t n32;
7910 int16_t n16;
7911 int8_t n8;
7912 uint32_t u32;
7913 uint16_t u16;
7914 uint8_t u8;
7915 uint64_t u64;
7916
7917 nErrCode = QCBOR_Int64ToInt32(1, &n32);
7918 if(nErrCode == -1 || n32 != 1) {
7919 return 1;
7920 }
7921
7922 nErrCode = QCBOR_Int64ToInt32((int64_t)INT32_MAX, &n32);
7923 if(nErrCode == -1 || n32 != INT32_MAX) {
7924 return 2;
7925 }
7926
7927 nErrCode = QCBOR_Int64ToInt32((int64_t)INT32_MIN, &n32);
7928 if(nErrCode == -1 || n32 != INT32_MIN) {
7929 return 3;
7930 }
7931
7932 nErrCode = QCBOR_Int64ToInt32(((int64_t)INT32_MAX)+1, &n32);
7933 if(nErrCode != -1) {
7934 return 4;
7935 }
7936
7937 nErrCode = QCBOR_Int64ToInt32(((int64_t)INT32_MIN)-1, &n32);
7938 if(nErrCode != -1) {
7939 return 5;
7940 }
7941
7942
7943 nErrCode = QCBOR_Int64ToInt16((int64_t)INT16_MAX, &n16);
7944 if(nErrCode == -1 || n16 != INT16_MAX) {
7945 return 6;
7946 }
7947
7948 nErrCode = QCBOR_Int64ToInt16((int64_t)INT16_MIN, &n16);
7949 if(nErrCode == -1 || n16 != INT16_MIN) {
7950 return 7;
7951 }
7952
7953 nErrCode = QCBOR_Int64ToInt16(1, &n16);
7954 if(nErrCode == -1 || n16 != 1) {
7955 return 8;
7956 }
7957
7958 nErrCode = QCBOR_Int64ToInt16(((int64_t)INT16_MAX)+1, &n16);
7959 if(nErrCode != -1) {
7960 return 9;
7961 }
7962
7963 nErrCode = QCBOR_Int64ToInt16(((int64_t)INT16_MIN)-1, &n16);
7964 if(nErrCode != -1) {
7965 return 10;
7966 }
7967
7968
7969 nErrCode = QCBOR_Int64ToInt8(1, &n8);
7970 if(nErrCode == -1 || n8 != 1) {
7971 return 11;
7972 }
7973
7974 nErrCode = QCBOR_Int64ToInt8((int64_t)INT8_MAX, &n8);
7975 if(nErrCode == -1 || n8 != INT8_MAX) {
7976 return 12;
7977 }
7978
7979 nErrCode = QCBOR_Int64ToInt8((int64_t)INT8_MIN, &n8);
7980 if(nErrCode == -1 || n8 != INT8_MIN) {
7981 return 13;
7982 }
7983
7984 nErrCode = QCBOR_Int64ToInt8(((int64_t)INT8_MAX)+1, &n8);
7985 if(nErrCode != -1) {
7986 return 14;
7987 }
7988
7989 nErrCode = QCBOR_Int64ToInt8(((int64_t)INT8_MIN)-1, &n8);
7990 if(nErrCode != -1) {
7991 return 15;
7992 }
7993
7994
7995 nErrCode = QCBOR_Int64ToUInt32(1, &u32);
7996 if(nErrCode == -1 || u32 != 1) {
7997 return 16;
7998 }
7999
8000 nErrCode = QCBOR_Int64ToUInt32((int64_t)UINT32_MAX, &u32);
8001 if(nErrCode == -1 || u32 != UINT32_MAX) {
8002 return 17;
8003 }
8004
8005 nErrCode = QCBOR_Int64ToUInt32((int64_t)0, &u32);
8006 if(nErrCode == -1 || u32 != 0) {
8007 return 18;
8008 }
8009
8010 nErrCode = QCBOR_Int64ToUInt32(((int64_t)UINT32_MAX)+1, &u32);
8011 if(nErrCode != -1) {
8012 return 19;
8013 }
8014
8015 nErrCode = QCBOR_Int64ToUInt32((int64_t)-1, &u32);
8016 if(nErrCode != -1) {
8017 return 20;
8018 }
8019
8020
8021 nErrCode = QCBOR_Int64UToInt16((int64_t)UINT16_MAX, &u16);
8022 if(nErrCode == -1 || u16 != UINT16_MAX) {
8023 return 21;
8024 }
8025
8026 nErrCode = QCBOR_Int64UToInt16((int64_t)0, &u16);
8027 if(nErrCode == -1 || u16 != 0) {
8028 return 22;
8029 }
8030
8031 nErrCode = QCBOR_Int64UToInt16(1, &u16);
8032 if(nErrCode == -1 || u16 != 1) {
8033 return 23;
8034 }
8035
8036 nErrCode = QCBOR_Int64UToInt16(((int64_t)UINT16_MAX)+1, &u16);
8037 if(nErrCode != -1) {
8038 return 24;
8039 }
8040
8041 nErrCode = QCBOR_Int64UToInt16((int64_t)-1, &u16);
8042 if(nErrCode != -1) {
8043 return 25;
8044 }
8045
8046
8047 nErrCode = QCBOR_Int64ToUInt8((int64_t)UINT8_MAX, &u8);
8048 if(nErrCode == -1 || u8 != UINT8_MAX) {
8049 return 26;
8050 }
8051
8052 nErrCode = QCBOR_Int64ToUInt8((int64_t)0, &u8);
8053 if(nErrCode == -1 || u8 != 0) {
8054 return 27;
8055 }
8056
8057 nErrCode = QCBOR_Int64ToUInt8(1, &u8);
8058 if(nErrCode == -1 || u8 != 1) {
8059 return 28;
8060 }
8061
8062 nErrCode = QCBOR_Int64ToUInt8(((int64_t)UINT16_MAX)+1, &u8);
8063 if(nErrCode != -1) {
8064 return 29;
8065 }
8066
8067 nErrCode = QCBOR_Int64ToUInt8((int64_t)-1, &u8);
8068 if(nErrCode != -1) {
8069 return 30;
8070 }
8071
8072
8073 nErrCode = QCBOR_Int64ToUInt64(1, &u64);
8074 if(nErrCode == -1 || u64 != 1) {
8075 return 31;
8076 }
8077
8078 nErrCode = QCBOR_Int64ToUInt64(INT64_MAX, &u64);
8079 if(nErrCode == -1 || u64 != INT64_MAX) {
8080 return 32;
8081 }
8082
8083 nErrCode = QCBOR_Int64ToUInt64((int64_t)0, &u64);
8084 if(nErrCode == -1 || u64 != 0) {
8085 return 33;
8086 }
8087
8088 nErrCode = QCBOR_Int64ToUInt64((int64_t)-1, &u64);
8089 if(nErrCode != -1) {
8090 return 34;
8091 }
8092
8093 return 0;
8094}
8095
Laurence Lundblade0750fc42020-06-20 21:02:34 -07008096
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07008097
8098
Laurence Lundblade0750fc42020-06-20 21:02:34 -07008099/*
8100A sequence with
8101 A wrapping bstr
8102 containing a map
8103 1
8104 2
8105 A wrapping bstr
8106 containing an array
8107 3
8108 wrapping bstr
8109 4
8110 5
8111 6
8112 array
8113 7
8114 8
Laurence Lundblade0750fc42020-06-20 21:02:34 -07008115 */
8116
Laurence Lundblade55013642020-09-23 05:39:22 -07008117static UsefulBufC EncodeBstrWrapTestData(UsefulBuf OutputBuffer)
Laurence Lundblade0750fc42020-06-20 21:02:34 -07008118{
Laurence Lundblade55013642020-09-23 05:39:22 -07008119 UsefulBufC Encoded;
Laurence Lundblade0750fc42020-06-20 21:02:34 -07008120 QCBOREncodeContext EC;
Laurence Lundblade55013642020-09-23 05:39:22 -07008121 QCBORError uErr;
Laurence Lundblade0750fc42020-06-20 21:02:34 -07008122
Laurence Lundblade55013642020-09-23 05:39:22 -07008123 QCBOREncode_Init(&EC, OutputBuffer);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07008124
8125 QCBOREncode_BstrWrap(&EC);
8126 QCBOREncode_OpenMap(&EC);
8127 QCBOREncode_AddInt64ToMapN(&EC, 100, 1);
8128 QCBOREncode_AddInt64ToMapN(&EC, 200, 2);
8129 QCBOREncode_CloseMap(&EC);
8130 QCBOREncode_BstrWrap(&EC);
8131 QCBOREncode_OpenArray(&EC);
8132 QCBOREncode_AddInt64(&EC, 3);
8133 QCBOREncode_BstrWrap(&EC);
8134 QCBOREncode_AddInt64(&EC, 4);
8135 QCBOREncode_CloseBstrWrap(&EC, NULL);
8136 QCBOREncode_AddInt64(&EC, 5);
8137 QCBOREncode_CloseArray(&EC);
8138 QCBOREncode_CloseBstrWrap(&EC, NULL);
8139 QCBOREncode_AddInt64(&EC, 6);
8140 QCBOREncode_CloseBstrWrap(&EC, NULL);
8141 QCBOREncode_OpenArray(&EC);
8142 QCBOREncode_AddInt64(&EC, 7);
8143 QCBOREncode_AddInt64(&EC, 8);
8144 QCBOREncode_CloseArray(&EC);
8145
8146 uErr = QCBOREncode_Finish(&EC, &Encoded);
Laurence Lundblade40a04322020-06-27 22:52:52 -07008147 if(uErr) {
8148 Encoded = NULLUsefulBufC;
8149 }
Laurence Lundblade0750fc42020-06-20 21:02:34 -07008150
8151 return Encoded;
8152}
8153
Laurence Lundbladecc7da412020-12-27 00:09:07 -08008154/* h'FF' */
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08008155static const uint8_t spBreakInByteString[] = {
8156 0x41, 0xff
8157};
8158
Laurence Lundblade0750fc42020-06-20 21:02:34 -07008159
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03008160int32_t EnterBstrTest(void)
Laurence Lundblade0750fc42020-06-20 21:02:34 -07008161{
Laurence Lundblade8510f8c2020-12-01 11:31:16 -08008162 UsefulBuf_MAKE_STACK_UB(OutputBuffer, 100);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07008163
8164 QCBORDecodeContext DC;
8165
Laurence Lundblade55013642020-09-23 05:39:22 -07008166 QCBORDecode_Init(&DC, EncodeBstrWrapTestData(OutputBuffer), 0);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07008167
Laurence Lundblade55013642020-09-23 05:39:22 -07008168 int64_t n1, n2, n3, n4, n5, n6, n7, n8;
Laurence Lundblade0750fc42020-06-20 21:02:34 -07008169
8170
Laurence Lundblade9b334962020-08-27 10:55:53 -07008171 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07008172 QCBORDecode_EnterMap(&DC, NULL);
Laurence Lundblade55013642020-09-23 05:39:22 -07008173 QCBORDecode_GetInt64InMapN(&DC, 100, &n1);
8174 QCBORDecode_GetInt64InMapN(&DC, 200, &n2);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07008175 QCBORDecode_ExitMap(&DC);
Laurence Lundblade9b334962020-08-27 10:55:53 -07008176 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07008177 QCBORDecode_EnterArray(&DC, NULL);
Laurence Lundblade55013642020-09-23 05:39:22 -07008178 QCBORDecode_GetInt64(&DC, &n3);
Laurence Lundblade9b334962020-08-27 10:55:53 -07008179 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
Laurence Lundblade55013642020-09-23 05:39:22 -07008180 QCBORDecode_GetInt64(&DC, &n4);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07008181 QCBORDecode_ExitBstrWrapped(&DC);
Laurence Lundblade55013642020-09-23 05:39:22 -07008182 QCBORDecode_GetInt64(&DC, &n5);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07008183 QCBORDecode_ExitArray(&DC);
8184 QCBORDecode_ExitBstrWrapped(&DC);
Laurence Lundblade55013642020-09-23 05:39:22 -07008185 QCBORDecode_GetInt64(&DC, &n6);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07008186 QCBORDecode_ExitBstrWrapped(&DC);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07008187 QCBORDecode_EnterArray(&DC, NULL);
Laurence Lundblade55013642020-09-23 05:39:22 -07008188 QCBORDecode_GetInt64(&DC, &n7);
8189 QCBORDecode_GetInt64(&DC, &n8);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07008190 QCBORDecode_ExitArray(&DC);
8191
8192 QCBORError uErr = QCBORDecode_Finish(&DC);
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08008193 if(uErr) {
8194 return (int32_t)uErr;
8195 }
Laurence Lundblade0750fc42020-06-20 21:02:34 -07008196
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08008197
8198 /* Enter and exit byte string wrapped CBOR that is bad. It has just a break.
8199 * Successful because no items are fetched from byte string.
8200 */
8201 QCBORDecode_Init(&DC,
8202 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBreakInByteString),
8203 0);
8204 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
8205 uErr = QCBORDecode_GetError(&DC);
8206 if(uErr) {
8207 return 100 + (int32_t)uErr;
8208 }
8209
8210 QCBORDecode_ExitBstrWrapped(&DC);
8211 uErr = QCBORDecode_GetError(&DC);
8212 if(uErr) {
8213 return 200 + (int32_t)uErr;
8214 }
8215
8216 /* Try to get item that is a break out of a byte string wrapped CBOR.
8217 * It fails because there should be no break.
8218 */
8219 QCBORDecode_Init(&DC,
8220 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBreakInByteString),
8221 0);
8222 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
8223 QCBORItem Item;
8224 uErr = QCBORDecode_GetNext(&DC, &Item);
8225 if(uErr != QCBOR_ERR_BAD_BREAK) {
8226 return 300 + (int32_t)uErr;
8227 }
8228
8229 return 0;
Laurence Lundblade0750fc42020-06-20 21:02:34 -07008230}
Laurence Lundblade37f46e52020-08-04 03:32:14 -07008231
8232
8233
8234
8235static const uint8_t spTaggedTypes[] = {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008236 0xb2,
Laurence Lundblade37f46e52020-08-04 03:32:14 -07008237
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07008238 // Date string
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008239 0x00,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07008240 0xc0, 0x74, 0x32, 0x30, 0x30, 0x33, 0x2D, 0x31, 0x32, 0x2D,
8241 0x31, 0x33, 0x54, 0x31, 0x38, 0x3A, 0x33, 0x30, 0x3A, 0x30,
8242 0x32, 0x5A,
Laurence Lundblade37f46e52020-08-04 03:32:14 -07008243
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008244 0x01,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07008245 0x74, 0x32, 0x30, 0x30, 0x33, 0x2D, 0x31, 0x32, 0x2D, 0x31,
8246 0x33, 0x54, 0x31, 0x38, 0x3A, 0x33, 0x30, 0x3A, 0x30, 0x32,
8247 0x5A,
Laurence Lundblade37f46e52020-08-04 03:32:14 -07008248
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07008249 // Bignum
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008250 10,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07008251 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
8252 0x09, 0x10,
Laurence Lundblade37f46e52020-08-04 03:32:14 -07008253
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008254 11,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07008255 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
8256 0x09, 0x10,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008257
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07008258 // URL
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008259 20,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07008260 0xd8, 0x20, 0x6f, 0x68, 0x74, 0x74, 0x70, 0x3A, 0x2F, 0x2F,
8261 0x63, 0x62, 0x6F, 0x72, 0x2E, 0x6D, 0x65, 0x2F,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008262
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07008263 21,
8264 0x6f, 0x68, 0x74, 0x74, 0x70, 0x3A, 0x2F, 0x2F, 0x63, 0x62,
8265 0x6F, 0x72, 0x2E, 0x6D, 0x65, 0x2F,
8266
8267 // B64
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008268 0x18, 0x1e,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07008269 0xd8, 0x22, 0x6c, 0x63, 0x47, 0x78, 0x6C, 0x59, 0x58, 0x4E,
8270 0x31, 0x63, 0x6D, 0x55, 0x75,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008271
8272 0x18, 0x1f,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07008273 0x6c, 0x63, 0x47, 0x78, 0x6C, 0x59, 0x58, 0x4E, 0x31, 0x63,
8274 0x6D, 0x55, 0x75,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008275
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07008276 // B64URL
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008277 0x18, 0x28,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07008278 0xd8, 0x21, 0x6c, 0x63, 0x47, 0x78, 0x6C, 0x59, 0x58, 0x4E,
8279 0x31, 0x63, 0x6D, 0x55, 0x75,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008280
8281 0x18, 0x29,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07008282 0x6c, 0x63, 0x47, 0x78, 0x6C, 0x59, 0x58, 0x4E, 0x31, 0x63,
8283 0x6D, 0x55, 0x75,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008284
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07008285 // Regex
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008286 0x18, 0x32,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07008287 0xd8, 0x23, 0x68, 0x31, 0x30, 0x30, 0x5C, 0x73, 0x2A, 0x6D,
8288 0x6B,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008289
8290 0x18, 0x33,
8291 0x68, 0x31, 0x30, 0x30, 0x5C, 0x73, 0x2A, 0x6D, 0x6B,
8292
8293 // MIME
8294 0x18, 0x3c,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07008295 0xd8, 0x24, 0x72, 0x4D, 0x49, 0x4D, 0x45, 0x2D, 0x56, 0x65,
8296 0x72, 0x73, 0x69, 0x6F, 0x6E, 0x3A, 0x20, 0x31, 0x2E, 0x30,
8297 0x0A,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008298
8299 0x18, 0x3d,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07008300 0x72, 0x4D, 0x49, 0x4D, 0x45, 0x2D, 0x56, 0x65, 0x72, 0x73,
8301 0x69, 0x6F, 0x6E, 0x3A, 0x20, 0x31, 0x2E, 0x30, 0x0A,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008302
8303 0x18, 0x3e,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07008304 0xd9, 0x01, 0x01, 0x52, 0x4D, 0x49, 0x4D, 0x45, 0x2D, 0x56,
8305 0x65, 0x72, 0x73, 0x69, 0x6F, 0x6E, 0x3A, 0x20, 0x31, 0x2E,
8306 0x30, 0x0A,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008307
8308 0x18, 0x3f,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07008309 0x52, 0x4D, 0x49, 0x4D, 0x45, 0x2D, 0x56, 0x65, 0x72, 0x73,
8310 0x69, 0x6F, 0x6E, 0x3A, 0x20, 0x31, 0x2E, 0x30, 0x0A,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008311
8312 // UUID
8313 0x18, 0x46,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07008314 0xd8, 0x25, 0x50, 0x53, 0x4D, 0x41, 0x52, 0x54, 0x43, 0x53,
8315 0x4C, 0x54, 0x54, 0x43, 0x46, 0x49, 0x43, 0x41, 0x32,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008316
8317 0x18, 0x47,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07008318 0x50, 0x53, 0x4D, 0x41, 0x52, 0x54, 0x43, 0x53, 0x4C, 0x54,
8319 0x54, 0x43, 0x46, 0x49, 0x43, 0x41, 0x32
Laurence Lundblade37f46e52020-08-04 03:32:14 -07008320};
8321
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03008322int32_t DecodeTaggedTypeTests(void)
Laurence Lundblade37f46e52020-08-04 03:32:14 -07008323{
8324 QCBORDecodeContext DC;
8325 QCBORError uErr;
8326
8327 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTaggedTypes), 0);
8328
8329 UsefulBufC String;
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008330 bool bNeg;
Laurence Lundblade37f46e52020-08-04 03:32:14 -07008331
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07008332 QCBORDecode_EnterMap(&DC, NULL);
Laurence Lundblade9b334962020-08-27 10:55:53 -07008333 QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008334 QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &String);
Laurence Lundblade37f46e52020-08-04 03:32:14 -07008335 if(QCBORDecode_GetError(&DC) != QCBOR_SUCCESS) {
8336 return 1;
8337 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07008338 QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade37f46e52020-08-04 03:32:14 -07008339 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_UNEXPECTED_TYPE) {
8340 return 2;
8341 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07008342 QCBORDecode_GetDateStringInMapN(&DC, 1, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade37f46e52020-08-04 03:32:14 -07008343 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_UNEXPECTED_TYPE) {
8344 return 3;
8345 }
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008346 QCBORDecode_GetDateStringInMapN(&DC, 1, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &String);
Laurence Lundblade9b334962020-08-27 10:55:53 -07008347 QCBORDecode_GetDateStringInMapN(&DC, 1, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade37f46e52020-08-04 03:32:14 -07008348 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
8349 return 4;
8350 }
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008351 QCBORDecode_GetDateStringInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07008352 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008353 return 5;
8354 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07008355
Laurence Lundblade9b334962020-08-27 10:55:53 -07008356 QCBORDecode_GetBignumInMapN(&DC, 10, QCBOR_TAG_REQUIREMENT_TAG, &String, &bNeg);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008357 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
8358 bNeg != false) {
8359 return 10;
8360 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07008361 QCBORDecode_GetBignumInMapN(&DC, 11, QCBOR_TAG_REQUIREMENT_TAG, &String, &bNeg);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008362 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
8363 bNeg != true) {
8364 return 11;
8365 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07008366 QCBORDecode_GetBignumInMapN(&DC, 11, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bNeg);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008367 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_UNEXPECTED_TYPE) {
8368 return 12;
8369 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07008370 QCBORDecode_GetBignumInMapN(&DC, 14, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bNeg);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07008371 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008372 return 13;
8373 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07008374 QCBORDecode_GetBignumInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bNeg);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07008375 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008376 return 14;
8377 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07008378
Laurence Lundblade9b334962020-08-27 10:55:53 -07008379 QCBORDecode_GetURIInMapN(&DC, 20, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008380 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
8381 return 20;
8382 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07008383 QCBORDecode_GetURIInMapN(&DC, 21, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008384 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
8385 return 21;
8386 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07008387 QCBORDecode_GetURIInMapN(&DC, 22, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07008388 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008389 return 22;
8390 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07008391 QCBORDecode_GetURIInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07008392 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008393 return 23;
8394 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07008395
Laurence Lundblade24bd7e12021-01-09 00:05:11 -08008396#ifndef QCBOR_DISABLE_UNCOMMON_TAGS
Laurence Lundblade9b334962020-08-27 10:55:53 -07008397 QCBORDecode_GetB64InMapN(&DC, 30, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008398 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
8399 return 30;
8400 }
Laurence Lundblade24bd7e12021-01-09 00:05:11 -08008401#endif
Laurence Lundblade9b334962020-08-27 10:55:53 -07008402 QCBORDecode_GetB64InMapN(&DC, 31, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008403 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
8404 return 31;
8405 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07008406 QCBORDecode_GetB64InMapN(&DC, 32, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07008407 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008408 return 32;
8409 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07008410 QCBORDecode_GetB64InMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07008411 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008412 return 33;
8413 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07008414
Laurence Lundblade24bd7e12021-01-09 00:05:11 -08008415#ifndef QCBOR_DISABLE_UNCOMMON_TAGS
Laurence Lundblade9b334962020-08-27 10:55:53 -07008416 QCBORDecode_GetB64URLInMapN(&DC, 40, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008417 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
8418 return 40;
8419 }
Laurence Lundblade24bd7e12021-01-09 00:05:11 -08008420#endif
Laurence Lundblade9b334962020-08-27 10:55:53 -07008421 QCBORDecode_GetB64URLInMapN(&DC, 41, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008422 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
8423 return 41;
8424 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07008425 QCBORDecode_GetB64URLInMapN(&DC, 42, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07008426 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008427 return 42;
8428 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07008429 QCBORDecode_GetB64URLInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07008430 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008431 return 43;
8432 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07008433
Laurence Lundblade24bd7e12021-01-09 00:05:11 -08008434#ifndef QCBOR_DISABLE_UNCOMMON_TAGS
Laurence Lundblade9b334962020-08-27 10:55:53 -07008435 QCBORDecode_GetRegexInMapN(&DC, 50, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008436 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
8437 return 50;
8438 }
Laurence Lundblade24bd7e12021-01-09 00:05:11 -08008439#endif
Laurence Lundblade9b334962020-08-27 10:55:53 -07008440 QCBORDecode_GetRegexInMapN(&DC, 51, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008441 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
8442 return 51;
8443 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07008444 QCBORDecode_GetRegexInMapN(&DC, 52, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07008445 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008446 return 52;
8447 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07008448 QCBORDecode_GetRegexInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07008449 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008450 return 53;
8451 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07008452
Laurence Lundblade24bd7e12021-01-09 00:05:11 -08008453#ifndef QCBOR_DISABLE_UNCOMMON_TAGS
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008454 // MIME
8455 bool bIsNot7Bit;
Laurence Lundblade9b334962020-08-27 10:55:53 -07008456 QCBORDecode_GetMIMEMessageInMapN(&DC, 60, QCBOR_TAG_REQUIREMENT_TAG, &String, &bIsNot7Bit);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008457 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
8458 bIsNot7Bit == true) {
8459 return 60;
8460 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07008461 QCBORDecode_GetMIMEMessageInMapN(&DC, 61, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bIsNot7Bit);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008462 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
8463 bIsNot7Bit == true) {
8464 return 61;
8465 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07008466 QCBORDecode_GetMIMEMessageInMapN(&DC, 62, QCBOR_TAG_REQUIREMENT_TAG, &String, &bIsNot7Bit);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008467 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
8468 bIsNot7Bit == false) {
8469 return 62;
8470 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07008471 QCBORDecode_GetMIMEMessageInMapN(&DC, 63, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bIsNot7Bit);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008472 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
8473 bIsNot7Bit == false) {
8474 return 63;
8475 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07008476 QCBORDecode_GetMIMEMessageInMapN(&DC, 64, QCBOR_TAG_REQUIREMENT_TAG, &String, &bNeg);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07008477 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008478 return 64;
8479 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07008480 QCBORDecode_GetMIMEMessageInMapSZ(&DC, "zzz", QCBOR_TAG_REQUIREMENT_TAG, &String, &bNeg);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07008481 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008482 return 65;
8483 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07008484
Laurence Lundblade24bd7e12021-01-09 00:05:11 -08008485
Laurence Lundblade9b334962020-08-27 10:55:53 -07008486 QCBORDecode_GetBinaryUUIDInMapN(&DC, 70, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008487 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
8488 return 70;
8489 }
Laurence Lundblade24bd7e12021-01-09 00:05:11 -08008490#endif /* #ifndef QCBOR_DISABLE_UNCOMMON_TAGS */
8491
Laurence Lundblade9b334962020-08-27 10:55:53 -07008492 QCBORDecode_GetBinaryUUIDInMapN(&DC, 71, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008493 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
8494 return 71;
8495 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07008496 QCBORDecode_GetBinaryUUIDInMapN(&DC, 72, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07008497 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008498 return 72;
8499 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07008500 QCBORDecode_GetBinaryUUIDInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07008501 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008502 return 73;
8503 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07008504
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07008505 // Improvement: add some more error test cases
8506
Laurence Lundblade37f46e52020-08-04 03:32:14 -07008507 QCBORDecode_ExitMap(&DC);
8508
8509 uErr = QCBORDecode_Finish(&DC);
8510 if(uErr != QCBOR_SUCCESS) {
8511 return 100;
8512 }
8513
8514 return 0;
8515}
Laurence Lundbladea4308a82020-10-03 18:08:57 -07008516
8517
8518
8519
8520/*
Laurence Lundbladecc7da412020-12-27 00:09:07 -08008521 [
8522 "aaaaaaaaaa",
8523 {}
8524 ]
Laurence Lundbladea4308a82020-10-03 18:08:57 -07008525 */
8526static const uint8_t spTooLarge1[] = {
8527 0x9f,
8528 0x6a, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61,
8529 0xa0,
8530 0xff
8531};
8532
8533/*
Laurence Lundbladecc7da412020-12-27 00:09:07 -08008534 [
8535 {
8536 0: "aaaaaaaaaa"
8537 }
8538 ]
Laurence Lundbladea4308a82020-10-03 18:08:57 -07008539 */
8540static const uint8_t spTooLarge2[] = {
8541 0x9f,
8542 0xa1,
8543 0x00,
8544 0x6a, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61,
8545 0xff
8546};
8547
8548/*
Laurence Lundbladecc7da412020-12-27 00:09:07 -08008549 h'A1006A61616161616161616161'
Laurence Lundbladea4308a82020-10-03 18:08:57 -07008550
Laurence Lundbladecc7da412020-12-27 00:09:07 -08008551 {
8552 0: "aaaaaaaaaa"
8553 }
Laurence Lundbladea4308a82020-10-03 18:08:57 -07008554 */
8555static const uint8_t spTooLarge3[] = {
8556 0x4d,
8557 0xa1,
8558 0x00,
8559 0x6a, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61,
8560};
8561
8562int32_t TooLargeInputTest(void)
8563{
8564 QCBORDecodeContext DC;
8565 QCBORError uErr;
8566 UsefulBufC String;
8567
8568 // These tests require a build with QCBOR_MAX_DECODE_INPUT_SIZE set
8569 // to 10 There's not really any way to test this error
8570 // condition. The error condition is not complex, so setting
8571 // QCBOR_MAX_DECODE_INPUT_SIZE gives an OK test.
8572
8573 // The input CBOR is only too large because the
8574 // QCBOR_MAX_DECODE_INPUT_SIZE is 10.
8575 //
8576 // This test is disabled for the normal test runs because of the
8577 // special build requirement.
8578
8579
8580 // Tests the start of a map being too large
8581 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTooLarge1), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07008582 QCBORDecode_EnterArray(&DC, NULL);
Laurence Lundbladea4308a82020-10-03 18:08:57 -07008583 QCBORDecode_GetTextString(&DC, &String);
8584 uErr = QCBORDecode_GetError(&DC);
8585 if(uErr != QCBOR_SUCCESS) {
8586 return 1;
8587 }
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07008588 QCBORDecode_EnterMap(&DC, NULL);
Laurence Lundbladea4308a82020-10-03 18:08:57 -07008589 uErr = QCBORDecode_GetError(&DC);
8590 if(uErr != QCBOR_ERR_INPUT_TOO_LARGE) {
8591 return 2;
8592 }
8593
8594 // Tests the end of a map being too large
8595 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTooLarge2), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07008596 QCBORDecode_EnterArray(&DC, NULL);
8597 QCBORDecode_EnterMap(&DC, NULL);
Laurence Lundbladea4308a82020-10-03 18:08:57 -07008598 uErr = QCBORDecode_GetError(&DC);
8599 if(uErr != QCBOR_SUCCESS) {
8600 return 3;
8601 }
8602 QCBORDecode_ExitMap(&DC);
8603 uErr = QCBORDecode_GetError(&DC);
8604 if(uErr != QCBOR_ERR_INPUT_TOO_LARGE) {
8605 return 4;
8606 }
8607
8608 // Tests the entire input CBOR being too large when processing bstr wrapping
8609 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTooLarge3), QCBOR_DECODE_MODE_NORMAL);
8610 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
8611 uErr = QCBORDecode_GetError(&DC);
8612 if(uErr != QCBOR_ERR_INPUT_TOO_LARGE) {
8613 return 5;
8614 }
8615
8616 return 0;
8617}
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07008618
8619
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08008620#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
8621
Laurence Lundblade37286c02022-09-03 10:05:02 -07008622/*
8623 An array of three map entries
8624 1) Indefinite length string label for indefinite lenght byte string
8625 2) Indefinite length string label for an integer
8626 3) Indefinite length string label for an indefinite-length negative big num
8627 */
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07008628static const uint8_t spMapWithIndefLenStrings[] = {
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08008629 0xa3,
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07008630 0x7f, 0x61, 'l', 0x64, 'a', 'b', 'e', 'l' , 0x61, '1', 0xff,
8631 0x5f, 0x42, 0x01, 0x02, 0x43, 0x03, 0x04, 0x05, 0xff,
8632 0x7f, 0x62, 'd', 'y', 0x61, 'm', 0x61, 'o', 0xff,
8633 0x03,
8634 0x7f, 0x62, 'l', 'a', 0x63, 'b', 'e', 'l', 0x61, '2', 0xff,
8635 0xc3,
8636 0x5f, 0x42, 0x00, 0x01, 0x42, 0x00, 0x01, 0x41, 0x01, 0xff,
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07008637};
8638
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03008639int32_t SpiffyIndefiniteLengthStringsTests(void)
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07008640{
8641 QCBORDecodeContext DCtx;
8642
8643 QCBORDecode_Init(&DCtx,
8644 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spMapWithIndefLenStrings),
8645 QCBOR_DECODE_MODE_NORMAL);
8646
Laurence Lundblade8510f8c2020-12-01 11:31:16 -08008647 UsefulBuf_MAKE_STACK_UB(StringBuf, 200);
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07008648 QCBORDecode_SetMemPool(&DCtx, StringBuf, false);
8649
8650 UsefulBufC ByteString;
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07008651 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07008652 QCBORDecode_GetByteStringInMapSZ(&DCtx, "label1", &ByteString);
Laurence Lundblade37286c02022-09-03 10:05:02 -07008653
8654#ifndef QCBOR_DISABLE_TAGS
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07008655 if(QCBORDecode_GetAndResetError(&DCtx)) {
8656 return 1;
8657 }
8658
8659 const uint8_t pExectedBytes[] = {0x01, 0x02, 0x03, 0x04, 0x05};
8660 if(UsefulBuf_Compare(ByteString, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pExectedBytes))) {
8661 return 2;
8662 }
8663
8664 uint64_t uInt;
8665 QCBORDecode_GetUInt64InMapSZ(&DCtx, "dymo", &uInt);
8666 if(QCBORDecode_GetAndResetError(&DCtx)) {
8667 return 3;
8668 }
8669 if(uInt != 3) {
8670 return 4;
8671 }
8672
Máté Tóth-Pálef5f07a2021-09-17 19:31:37 +02008673#ifndef USEFULBUF_DISABLE_ALL_FLOAT
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07008674 double uDouble;
8675 QCBORDecode_GetDoubleConvertAllInMapSZ(&DCtx,
8676 "label2",
8677 0xff,
8678 &uDouble);
Laurence Lundblade37286c02022-09-03 10:05:02 -07008679
Laurence Lundbladeb8e19aa2020-10-07 20:59:11 -07008680#ifndef QCBOR_DISABLE_FLOAT_HW_USE
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07008681 if(QCBORDecode_GetAndResetError(&DCtx)) {
8682 return 5;
8683 }
8684 if(uDouble != -16777474) {
8685 return 6;
8686 }
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08008687#else /* QCBOR_DISABLE_FLOAT_HW_USE */
Laurence Lundbladeb8e19aa2020-10-07 20:59:11 -07008688 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_HW_FLOAT_DISABLED) {
8689 return 7;
8690 }
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08008691#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
Máté Tóth-Pálef5f07a2021-09-17 19:31:37 +02008692#endif /* USEFULBUF_DISABLE_ALL_FLOAT */
Laurence Lundbladeb8e19aa2020-10-07 20:59:11 -07008693
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07008694 QCBORDecode_ExitMap(&DCtx);
8695
8696 if(QCBORDecode_Finish(&DCtx)) {
8697 return 99;
8698 }
8699
Laurence Lundblade37286c02022-09-03 10:05:02 -07008700#else /* QCBOR_DISABLE_TAGS */
8701 /* The big num in the input is a CBOR tag and you can't do
8702 * map lookups in a map with a tag so this test does very little
8703 * when tags are disabled. That is OK, the test coverage is still
8704 * good when they are not.
8705 */
8706 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_TAGS_DISABLED) {
8707 return 1002;
8708 }
8709#endif /*QCBOR_DISABLE_TAGS */
8710
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07008711 return 0;
8712}
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08008713#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
Laurence Lundblade2a26abb2020-11-05 19:06:54 -08008714
8715
Laurence Lundbladeec290b82024-06-10 11:10:54 -07008716#ifndef QCBOR_DISABLE_NON_INTEGER_LABELS
Laurence Lundbladecf41c522021-02-20 10:19:07 -07008717/*
8718 * An array of an integer and an array. The second array contains
8719 * a bstr-wrapped map.
8720 *
8721 * [7, [h'A36D6669... (see next lines) 73']]
8722 *
8723 * {"first integer": 42,
8724 * "an array of two strings": ["string1", "string2"],
8725 * "map in a map":
8726 * { "bytes 1": h'78787878',
8727 * "bytes 2": h'79797979',
8728 * "another int": 98,
8729 * "text 2": "lies, damn lies and statistics"
8730 * }
8731 * }
8732 */
Laurence Lundblade2a26abb2020-11-05 19:06:54 -08008733
Laurence Lundbladecf41c522021-02-20 10:19:07 -07008734static const uint8_t pValidWrappedMapEncoded[] = {
8735 0x82, 0x07, 0x81, 0x58, 0x97,
8736 0xa3, 0x6d, 0x66, 0x69, 0x72, 0x73, 0x74, 0x20, 0x69, 0x6e,
8737 0x74, 0x65, 0x67, 0x65, 0x72, 0x18, 0x2a, 0x77, 0x61, 0x6e,
8738 0x20, 0x61, 0x72, 0x72, 0x61, 0x79, 0x20, 0x6f, 0x66, 0x20,
8739 0x74, 0x77, 0x6f, 0x20, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67,
8740 0x73, 0x82, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x31,
8741 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x32, 0x6c, 0x6d,
8742 0x61, 0x70, 0x20, 0x69, 0x6e, 0x20, 0x61, 0x20, 0x6d, 0x61,
8743 0x70, 0xa4, 0x67, 0x62, 0x79, 0x74, 0x65, 0x73, 0x20, 0x31,
8744 0x44, 0x78, 0x78, 0x78, 0x78, 0x67, 0x62, 0x79, 0x74, 0x65,
8745 0x73, 0x20, 0x32, 0x44, 0x79, 0x79, 0x79, 0x79, 0x6b, 0x61,
8746 0x6e, 0x6f, 0x74, 0x68, 0x65, 0x72, 0x20, 0x69, 0x6e, 0x74,
8747 0x18, 0x62, 0x66, 0x74, 0x65, 0x78, 0x74, 0x20, 0x32, 0x78,
8748 0x1e, 0x6c, 0x69, 0x65, 0x73, 0x2c, 0x20, 0x64, 0x61, 0x6d,
8749 0x6e, 0x20, 0x6c, 0x69, 0x65, 0x73, 0x20, 0x61, 0x6e, 0x64,
8750 0x20, 0x73, 0x74, 0x61, 0x74, 0x69, 0x73, 0x74, 0x69, 0x63,
8751 0x73
8752};
8753
8754#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
8755
8756/* As above, but the arrays are indefinite length */
8757static const uint8_t pValidIndefWrappedMapEncoded[] = {
8758 0x9f, 0x07, 0x9f, 0x58, 0x97,
8759 0xa3, 0x6d, 0x66, 0x69, 0x72, 0x73, 0x74, 0x20, 0x69, 0x6e,
8760 0x74, 0x65, 0x67, 0x65, 0x72, 0x18, 0x2a, 0x77, 0x61, 0x6e,
8761 0x20, 0x61, 0x72, 0x72, 0x61, 0x79, 0x20, 0x6f, 0x66, 0x20,
8762 0x74, 0x77, 0x6f, 0x20, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67,
8763 0x73, 0x82, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x31,
8764 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x32, 0x6c, 0x6d,
8765 0x61, 0x70, 0x20, 0x69, 0x6e, 0x20, 0x61, 0x20, 0x6d, 0x61,
8766 0x70, 0xa4, 0x67, 0x62, 0x79, 0x74, 0x65, 0x73, 0x20, 0x31,
8767 0x44, 0x78, 0x78, 0x78, 0x78, 0x67, 0x62, 0x79, 0x74, 0x65,
8768 0x73, 0x20, 0x32, 0x44, 0x79, 0x79, 0x79, 0x79, 0x6b, 0x61,
8769 0x6e, 0x6f, 0x74, 0x68, 0x65, 0x72, 0x20, 0x69, 0x6e, 0x74,
8770 0x18, 0x62, 0x66, 0x74, 0x65, 0x78, 0x74, 0x20, 0x32, 0x78,
8771 0x1e, 0x6c, 0x69, 0x65, 0x73, 0x2c, 0x20, 0x64, 0x61, 0x6d,
8772 0x6e, 0x20, 0x6c, 0x69, 0x65, 0x73, 0x20, 0x61, 0x6e, 0x64,
8773 0x20, 0x73, 0x74, 0x61, 0x74, 0x69, 0x73, 0x74, 0x69, 0x63,
8774 0x73,
8775 0xff, 0xff
8776};
Laurence Lundbladeec290b82024-06-10 11:10:54 -07008777#endif /* ! QCBOR_DISABLE_NON_INTEGER_LABELS */
Laurence Lundbladecf41c522021-02-20 10:19:07 -07008778
8779static const uint8_t pWithEmptyMap[] = {0x82, 0x18, 0x64, 0xa0};
8780
Laurence Lundbladeec290b82024-06-10 11:10:54 -07008781
Laurence Lundbladecf41c522021-02-20 10:19:07 -07008782#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
8783static const uint8_t pWithEmptyMapInDef[] = {0x9f, 0x18, 0x64, 0xbf, 0xff, 0xff};
8784#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
8785
8786#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
Laurence Lundblade37286c02022-09-03 10:05:02 -07008787/*
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08008788 * An array of one that contains a byte string that is an indefinite
8789 * length string that CBOR wraps an array of three numbers [42, 43,
8790 * 44]. The byte string is an implicit tag 24.
8791 *
8792 * [
8793 * (_ h'83', h'18', h'2A182B', h'182C')
8794 * ]
Laurence Lundblade37286c02022-09-03 10:05:02 -07008795 */
Laurence Lundbladecf41c522021-02-20 10:19:07 -07008796static const uint8_t pWrappedByIndefiniteLength[] = {
8797 0x81,
Laurence Lundbladecf41c522021-02-20 10:19:07 -07008798 0x5f,
8799 0x41, 0x83,
8800 0x41, 0x18,
8801 0x43, 0x2A, 0x18, 0x2B,
8802 0x42, 0x18, 0x2C,
8803 0xff
8804};
8805#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
8806
Laurence Lundbladeec290b82024-06-10 11:10:54 -07008807#endif
Laurence Lundbladecf41c522021-02-20 10:19:07 -07008808
Laurence Lundbladeec290b82024-06-10 11:10:54 -07008809#ifndef QCBOR_DISABLE_NON_INTEGER_LABELS
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03008810int32_t PeekAndRewindTest(void)
Laurence Lundblade2a26abb2020-11-05 19:06:54 -08008811{
8812 QCBORItem Item;
8813 QCBORError nCBORError;
8814 QCBORDecodeContext DCtx;
8815
Laurence Lundbladeec290b82024-06-10 11:10:54 -07008816 // Improvement: rework this test to use only integer labels.
8817
Laurence Lundblade2a26abb2020-11-05 19:06:54 -08008818 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0);
8819
8820 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
8821 return 100+(int32_t)nCBORError;
8822 }
8823 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
8824 return 200;
8825 }
8826
Laurence Lundblade3427dee2021-06-20 11:11:24 -07008827 QCBORDecode_VPeekNext(&DCtx, &Item);
8828 if((nCBORError = QCBORDecode_GetError(&DCtx))) {
8829 return 150+(int32_t)nCBORError;
8830 }
8831 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
8832 return 250;
8833 }
8834
Laurence Lundblade2a26abb2020-11-05 19:06:54 -08008835 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
8836 return (int32_t)nCBORError;
8837 }
8838 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
8839 return 300;
8840 }
8841
8842 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
8843 return 400 + (int32_t)nCBORError;
8844 }
8845 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
8846 return 500;
8847 }
8848
8849 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8850 return (int32_t)nCBORError;
8851 }
8852 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
8853 return 600;
8854 }
8855
8856 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
8857 return 900 + (int32_t)nCBORError;
8858 }
8859 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
8860 Item.uDataType != QCBOR_TYPE_INT64 ||
8861 Item.val.int64 != 42 ||
8862 Item.uDataAlloc ||
8863 Item.uLabelAlloc ||
8864 UsefulBufCompareToSZ(Item.label.string, "first integer")) {
8865 return 1000;
8866 }
8867
8868 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8869 return 1100 + (int32_t)nCBORError;
8870 }
8871
8872 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
8873 Item.uDataType != QCBOR_TYPE_INT64 ||
8874 Item.val.int64 != 42 ||
8875 Item.uDataAlloc ||
8876 Item.uLabelAlloc ||
8877 UsefulBufCompareToSZ(Item.label.string, "first integer")) {
8878 return 1200;
8879 }
8880
8881
8882 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8883 return 1300 + (int32_t)nCBORError;
8884 }
8885 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
8886 Item.uDataAlloc ||
8887 Item.uLabelAlloc ||
8888 UsefulBufCompareToSZ(Item.label.string, "an array of two strings") ||
8889 Item.uDataType != QCBOR_TYPE_ARRAY ||
Laurence Lundbladecf41c522021-02-20 10:19:07 -07008890 Item.val.uCount != 2) {
Laurence Lundblade2a26abb2020-11-05 19:06:54 -08008891 return 1400;
Laurence Lundbladecf41c522021-02-20 10:19:07 -07008892 }
Laurence Lundblade2a26abb2020-11-05 19:06:54 -08008893
8894 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8895 return 1500 + (int32_t)nCBORError;
8896 }
8897 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
8898 Item.uDataAlloc ||
8899 Item.uLabelAlloc ||
8900 UsefulBufCompareToSZ(Item.val.string, "string1")) {
8901 return 1600;
8902 }
8903
8904 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
8905 return 1700 + (int32_t)nCBORError;
8906 }
8907 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
8908 Item.uDataAlloc ||
8909 Item.uLabelAlloc ||
8910 UsefulBufCompareToSZ(Item.val.string, "string2")) {
8911 return 1800;
8912 }
8913
8914 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
8915 return (int32_t)nCBORError;
8916 }
8917 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
8918 Item.uDataAlloc ||
8919 Item.uLabelAlloc ||
8920 UsefulBufCompareToSZ(Item.val.string, "string2")) {
8921 return 1900;
8922 }
8923
8924 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8925 return (int32_t)nCBORError;
8926 }
8927 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
8928 Item.uDataAlloc ||
8929 Item.uLabelAlloc ||
8930 UsefulBufCompareToSZ(Item.val.string, "string2")) {
8931 return 2000;
8932 }
8933
8934
8935 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8936 return 2100 + (int32_t)nCBORError;
8937 }
8938 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
8939 Item.uDataAlloc ||
8940 Item.uLabelAlloc ||
8941 UsefulBufCompareToSZ(Item.label.string, "map in a map") ||
8942 Item.uDataType != QCBOR_TYPE_MAP ||
8943 Item.val.uCount != 4) {
8944 return 2100;
8945 }
8946
8947 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8948 return 2200 + (int32_t)nCBORError;
8949 }
8950 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
8951 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("bytes 1"))||
8952 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
8953 Item.uDataAlloc ||
8954 Item.uLabelAlloc ||
8955 UsefulBufCompareToSZ(Item.val.string, "xxxx")) {
8956 return 2300;
8957 }
8958
8959 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
8960 return 2400 + (int32_t)nCBORError;
8961 }
8962 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
8963 UsefulBufCompareToSZ(Item.label.string, "bytes 2") ||
8964 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
8965 Item.uDataAlloc ||
8966 Item.uLabelAlloc ||
8967 UsefulBufCompareToSZ(Item.val.string, "yyyy")) {
8968 return 2500;
8969 }
8970
8971 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8972 return 2600 + (int32_t)nCBORError;
8973 }
8974 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
8975 UsefulBufCompareToSZ(Item.label.string, "bytes 2") ||
8976 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
8977 Item.uDataAlloc ||
8978 Item.uLabelAlloc ||
8979 UsefulBufCompareToSZ(Item.val.string, "yyyy")) {
8980 return 2700;
8981 }
8982
8983 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8984 return 2800 + (int32_t)nCBORError;
8985 }
8986 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
8987 Item.uDataAlloc ||
8988 Item.uLabelAlloc ||
8989 UsefulBufCompareToSZ(Item.label.string, "another int") ||
8990 Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundbladecf41c522021-02-20 10:19:07 -07008991 Item.val.int64 != 98) {
Laurence Lundblade2a26abb2020-11-05 19:06:54 -08008992 return 2900;
Laurence Lundbladecf41c522021-02-20 10:19:07 -07008993 }
Laurence Lundblade2a26abb2020-11-05 19:06:54 -08008994
8995 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
8996 return 3000 + (int32_t)nCBORError;
8997 }
8998 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
8999 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("text 2"))||
9000 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
9001 Item.uDataAlloc ||
9002 Item.uLabelAlloc ||
9003 UsefulBufCompareToSZ(Item.val.string, "lies, damn lies and statistics")) {
9004 return 3100;
9005 }
9006
9007 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
9008 return 3200 + (int32_t)nCBORError;
9009 }
9010 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
9011 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("text 2"))||
9012 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
9013 Item.uDataAlloc ||
9014 Item.uLabelAlloc ||
9015 UsefulBufCompareToSZ(Item.val.string, "lies, damn lies and statistics")) {
9016 return 3300;
9017 }
9018
Laurence Lundblade3427dee2021-06-20 11:11:24 -07009019 nCBORError = QCBORDecode_PeekNext(&DCtx, &Item);
9020 if(nCBORError != QCBOR_ERR_NO_MORE_ITEMS) {
9021 return 3300 + (int32_t)nCBORError;
9022 }
9023
9024 QCBORDecode_VPeekNext(&DCtx, &Item);
9025 nCBORError = QCBORDecode_GetError(&DCtx);
9026 if(nCBORError != QCBOR_ERR_NO_MORE_ITEMS) {
9027 return 3400 + (int32_t)nCBORError;
9028 }
9029
9030 QCBORDecode_VPeekNext(&DCtx, &Item);
9031 nCBORError = QCBORDecode_GetError(&DCtx);
9032 if(nCBORError != QCBOR_ERR_NO_MORE_ITEMS) {
9033 return 3500 + (int32_t)nCBORError;
9034 }
Laurence Lundbladecf41c522021-02-20 10:19:07 -07009035
9036
9037 // Rewind to top level after entering several maps
9038 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0);
9039
9040 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
9041 return (int32_t)nCBORError;
9042 }
9043 if(Item.uDataType != QCBOR_TYPE_MAP ||
9044 Item.val.uCount != 3) {
9045 return 400;
9046 }
9047
9048 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
9049 return 4000+(int32_t)nCBORError;
9050 }
9051
9052 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
9053 Item.uDataType != QCBOR_TYPE_INT64 ||
9054 Item.val.int64 != 42 ||
9055 Item.uDataAlloc ||
9056 Item.uLabelAlloc ||
9057 UsefulBufCompareToSZ(Item.label.string, "first integer")) {
9058 return 4100;
9059 }
9060
9061 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
9062 return 4100+(int32_t)nCBORError;
9063 }
9064 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
9065 Item.uDataAlloc ||
9066 Item.uLabelAlloc ||
9067 UsefulBufCompareToSZ(Item.label.string, "an array of two strings") ||
9068 Item.uDataType != QCBOR_TYPE_ARRAY ||
9069 Item.val.uCount != 2) {
9070 return 4200;
9071 }
9072
9073 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
9074 return 4200+(int32_t)nCBORError;
9075 }
9076 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
9077 Item.uDataAlloc ||
9078 Item.uLabelAlloc ||
9079 UsefulBufCompareToSZ(Item.val.string, "string1")) {
9080 return 4300;
9081 }
9082
9083 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
9084 return 4300+(int32_t)nCBORError;
9085 }
9086 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
9087 Item.uDataAlloc ||
9088 Item.uLabelAlloc ||
9089 UsefulBufCompareToSZ(Item.val.string, "string2")) {
9090 return 4400;
9091 }
9092
9093 QCBORDecode_Rewind(&DCtx);
9094
9095 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
9096 return 4400+(int32_t)nCBORError;
9097 }
9098 if(Item.uDataType != QCBOR_TYPE_MAP ||
9099 Item.val.uCount != 3) {
9100 return 4500;
9101 }
9102
9103 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
9104 return (int32_t)nCBORError;
9105 }
9106
9107 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
9108 Item.uDataType != QCBOR_TYPE_INT64 ||
9109 Item.val.int64 != 42 ||
9110 Item.uDataAlloc ||
9111 Item.uLabelAlloc ||
9112 UsefulBufCompareToSZ(Item.label.string, "first integer")) {
9113 return 4600;
9114 }
9115
9116 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
9117 return (int32_t)nCBORError;
9118 }
9119 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
9120 Item.uDataAlloc ||
9121 Item.uLabelAlloc ||
9122 UsefulBufCompareToSZ(Item.label.string, "an array of two strings") ||
9123 Item.uDataType != QCBOR_TYPE_ARRAY ||
9124 Item.val.uCount != 2) {
9125 return 4700;
9126 }
9127
9128 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
9129 return (int32_t)nCBORError;
9130 }
9131 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
9132 Item.uDataAlloc ||
9133 Item.uLabelAlloc ||
9134 UsefulBufCompareToSZ(Item.val.string, "string1")) {
9135 return 4800;
9136 }
9137
9138 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
9139 return 4900+(int32_t)nCBORError;
9140 }
9141 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
9142 Item.uDataAlloc ||
9143 Item.uLabelAlloc ||
9144 UsefulBufCompareToSZ(Item.val.string, "string2")) {
9145 return 5000;
9146 }
9147
9148
9149 // Rewind an entered map
9150 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0);
9151
9152 QCBORDecode_EnterMap(&DCtx, NULL);
9153
9154 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
9155 return 5100+(int32_t)nCBORError;
9156 }
9157
9158 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
9159 Item.uDataType != QCBOR_TYPE_INT64 ||
9160 Item.val.int64 != 42 ||
9161 Item.uDataAlloc ||
9162 Item.uLabelAlloc ||
9163 UsefulBufCompareToSZ(Item.label.string, "first integer")) {
9164 return 5200;
9165 }
9166
9167 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
9168 return 5200+(int32_t)nCBORError;
9169 }
9170 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
9171 Item.uDataAlloc ||
9172 Item.uLabelAlloc ||
9173 UsefulBufCompareToSZ(Item.label.string, "an array of two strings") ||
9174 Item.uDataType != QCBOR_TYPE_ARRAY ||
9175 Item.val.uCount != 2) {
9176 return -5300;
9177 }
9178
9179 QCBORDecode_Rewind(&DCtx);
9180
9181 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
9182 return 5300+(int32_t)nCBORError;
9183 }
9184
9185 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
9186 Item.uDataType != QCBOR_TYPE_INT64 ||
9187 Item.val.int64 != 42 ||
9188 Item.uDataAlloc ||
9189 Item.uLabelAlloc ||
9190 UsefulBufCompareToSZ(Item.label.string, "first integer")) {
9191 return 5400;
9192 }
9193
9194 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
9195 return 5400+(int32_t)nCBORError;
9196 }
9197 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
9198 Item.uDataAlloc ||
9199 Item.uLabelAlloc ||
9200 UsefulBufCompareToSZ(Item.label.string, "an array of two strings") ||
9201 Item.uDataType != QCBOR_TYPE_ARRAY ||
9202 Item.val.uCount != 2) {
9203 return 5500;
9204 }
9205
9206
9207 // Rewind and entered array inside an entered map
9208 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0);
9209
9210 QCBORDecode_EnterMap(&DCtx, NULL);
9211
9212 QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings");
9213
9214 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
9215 return 5600+(int32_t)nCBORError;
9216 }
9217 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
9218 Item.uDataAlloc ||
9219 Item.uLabelAlloc ||
9220 UsefulBufCompareToSZ(Item.val.string, "string1")) {
9221 return 5700;
9222 }
9223
9224 QCBORDecode_Rewind(&DCtx);
9225
9226 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
9227 return 5700+(int32_t)nCBORError;
9228 }
9229 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
9230 Item.uDataAlloc ||
9231 Item.uLabelAlloc ||
9232 UsefulBufCompareToSZ(Item.val.string, "string1")) {
9233 return 5800;
9234 }
9235
9236 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
9237 return (int32_t)nCBORError;
9238 }
9239 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
9240 Item.uDataAlloc ||
9241 Item.uLabelAlloc ||
9242 UsefulBufCompareToSZ(Item.val.string, "string2")) {
9243 return 5900;
9244 }
9245
9246 QCBORDecode_Rewind(&DCtx);
9247
9248 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
9249 return 5900+(int32_t)nCBORError;
9250 }
9251 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
9252 Item.uDataAlloc ||
9253 Item.uLabelAlloc ||
9254 UsefulBufCompareToSZ(Item.val.string, "string1")) {
9255 return 6000;
9256 }
9257
9258
9259 // Rewind a byte string inside an array inside an array
9260 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidWrappedMapEncoded), 0);
9261
9262 QCBORDecode_EnterArray(&DCtx, NULL);
9263
9264 uint64_t i;
9265 QCBORDecode_GetUInt64(&DCtx, &i);
9266
9267 QCBORDecode_EnterArray(&DCtx, NULL);
9268
9269 QCBORDecode_EnterBstrWrapped(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
9270 if(QCBORDecode_GetError(&DCtx)) {
9271 return 6100;
9272 }
9273
9274 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
9275 return (int32_t)nCBORError;
9276 }
9277 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
9278 return 6200;
9279 }
9280
9281 QCBORDecode_Rewind(&DCtx);
9282
9283 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
9284 return 6300+(int32_t)nCBORError;
9285 }
9286 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
9287 return 6400;
9288 }
9289
9290#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
9291 // Rewind a byte string inside an indefinite-length array inside
9292 // indefinite-length array
9293
9294 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidIndefWrappedMapEncoded), 0);
9295
9296 QCBORDecode_EnterArray(&DCtx, NULL);
9297
9298 QCBORDecode_GetUInt64(&DCtx, &i);
9299
9300 QCBORDecode_EnterArray(&DCtx, NULL);
9301
9302 QCBORDecode_EnterBstrWrapped(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
9303 if(QCBORDecode_GetError(&DCtx)) {
9304 return 6500;
9305 }
9306
9307 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
9308 return 6600+(int32_t)nCBORError;
9309 }
9310 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
9311 return 6700;
9312 }
9313
9314 QCBORDecode_Rewind(&DCtx);
9315
9316 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
9317 return 6800+(int32_t)nCBORError;
9318 }
9319 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
9320 return 6900;
9321 }
9322#endif
9323
9324 // Rewind an empty map
9325 // [100, {}]
9326 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pWithEmptyMap), 0);
9327 QCBORDecode_EnterArray(&DCtx, NULL);
9328 QCBORDecode_GetUInt64(&DCtx, &i);
9329 if(i != 100) {
9330 return 7010;
9331 }
9332 QCBORDecode_EnterMap(&DCtx, NULL);
9333
9334 /* Do it 5 times to be sure multiple rewinds work */
9335 for(int n = 0; n < 5; n++) {
9336 nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
9337 if(nCBORError != QCBOR_ERR_NO_MORE_ITEMS) {
9338 return 7000 + n;
9339 }
9340 QCBORDecode_Rewind(&DCtx);
9341 }
9342 QCBORDecode_ExitMap(&DCtx);
9343 QCBORDecode_Rewind(&DCtx);
9344 QCBORDecode_GetUInt64(&DCtx, &i);
9345 if(i != 100) {
9346 return 7010;
9347 }
9348 QCBORDecode_ExitArray(&DCtx);
9349 QCBORDecode_Rewind(&DCtx);
9350 QCBORDecode_EnterArray(&DCtx, NULL);
9351 i = 9;
9352 QCBORDecode_GetUInt64(&DCtx, &i);
9353 if(i != 100) {
9354 return 7020;
9355 }
9356 if(QCBORDecode_GetError(&DCtx)){
9357 return 7030;
9358 }
9359
9360 // Rewind an empty indefinite length map
9361#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
9362 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pWithEmptyMapInDef), 0);
9363 QCBORDecode_EnterArray(&DCtx, NULL);
9364 QCBORDecode_GetUInt64(&DCtx, &i);
9365 if(i != 100) {
9366 return 7810;
9367 }
9368 QCBORDecode_EnterMap(&DCtx, NULL);
9369
9370 /* Do it 5 times to be sure multiple rewinds work */
9371 for(int n = 0; n < 5; n++) {
9372 nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
9373 if(nCBORError != QCBOR_ERR_NO_MORE_ITEMS) {
9374 return 7800 + n;
9375 }
9376 QCBORDecode_Rewind(&DCtx);
9377 }
9378 QCBORDecode_ExitMap(&DCtx);
9379 QCBORDecode_Rewind(&DCtx);
9380 QCBORDecode_GetUInt64(&DCtx, &i);
9381 if(i != 100) {
9382 return 7810;
9383 }
9384 QCBORDecode_ExitArray(&DCtx);
9385 QCBORDecode_Rewind(&DCtx);
9386 QCBORDecode_EnterArray(&DCtx, NULL);
9387 i = 9;
9388 QCBORDecode_GetUInt64(&DCtx, &i);
9389 if(i != 100) {
9390 return 7820;
9391 }
9392 if(QCBORDecode_GetError(&DCtx)){
9393 return 7830;
9394 }
9395#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
9396
9397 // Rewind an indefnite length byte-string wrapped sequence
9398#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
9399 QCBORDecode_Init(&DCtx,
9400 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pWrappedByIndefiniteLength),
9401 0);
9402 UsefulBuf_MAKE_STACK_UB(Pool, 100);
9403 QCBORDecode_SetMemPool(&DCtx, Pool, 0);
9404
9405 QCBORDecode_EnterArray(&DCtx, NULL);
9406 QCBORDecode_EnterBstrWrapped(&DCtx, 2, NULL);
Laurence Lundblade31fddb72024-05-13 13:03:35 -07009407 if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_CANNOT_ENTER_ALLOCATED_STRING) {
Laurence Lundbladecf41c522021-02-20 10:19:07 -07009408 return 7300;
9409 }
9410
9411 /*
Laurence Lundblade31fddb72024-05-13 13:03:35 -07009412 Improvement: Fix QCBORDecode_EnterBstrWrapped() so it can work on
9413 allocated strings. This is a fairly big job because of all the
9414 UsefulBuf internal book keeping that needs tweaking.
Laurence Lundbladecf41c522021-02-20 10:19:07 -07009415 QCBORDecode_GetUInt64(&DCtx, &i);
9416 if(i != 42) {
9417 return 7110;
9418 }
9419 QCBORDecode_Rewind(&DCtx);
9420 QCBORDecode_GetUInt64(&DCtx, &i);
9421 if(i != 42) {
9422 return 7220;
Laurence Lundblade31fddb72024-05-13 13:03:35 -07009423 }
9424 */
Laurence Lundblade37286c02022-09-03 10:05:02 -07009425
Laurence Lundbladeec290b82024-06-10 11:10:54 -07009426#endif /* ! QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
Laurence Lundbladecf41c522021-02-20 10:19:07 -07009427
9428
9429 // Rewind an indefnite length byte-string wrapped sequence
9430
Laurence Lundblade2a26abb2020-11-05 19:06:54 -08009431 return 0;
9432}
Laurence Lundblade9f9c3732021-03-23 09:38:46 -07009433
Laurence Lundbladeec290b82024-06-10 11:10:54 -07009434#endif /* ! QCBOR_DISABLE_NON_INTEGER_LABELS */
Laurence Lundblade9f9c3732021-03-23 09:38:46 -07009435
9436
9437static const uint8_t spBooleansInMap[] =
9438{
9439 0xa1, 0x08, 0xf5
9440};
9441
9442static const uint8_t spBooleansInMapWrongType[] =
9443{
9444 0xa1, 0x08, 0xf6
9445};
9446
9447static const uint8_t spBooleansInMapNWF[] =
9448{
9449 0xa1, 0x08, 0x1a
9450};
9451
Laurence Lundblade8782dd32021-04-27 04:15:37 -07009452static const uint8_t spNullInMap[] =
9453{
9454 0xa1, 0x08, 0xf6
9455};
9456
9457static const uint8_t spUndefinedInMap[] =
9458{
9459 0xa1, 0x08, 0xf7
9460};
9461
Laurence Lundblade9f9c3732021-03-23 09:38:46 -07009462
9463int32_t BoolTest(void)
9464{
9465 QCBORDecodeContext DCtx;
9466 bool b;
9467
Laurence Lundblade8782dd32021-04-27 04:15:37 -07009468 QCBORDecode_Init(&DCtx,
9469 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMap),
9470 0);
Laurence Lundblade9f9c3732021-03-23 09:38:46 -07009471 QCBORDecode_EnterMap(&DCtx, NULL);
9472 QCBORDecode_GetBool(&DCtx, &b);
9473 if(QCBORDecode_GetAndResetError(&DCtx) || !b) {
9474 return 1;
9475 }
9476
9477 QCBORDecode_GetBoolInMapN(&DCtx, 7, &b);
9478 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_LABEL_NOT_FOUND) {
9479 return 2;
9480 }
9481
9482 QCBORDecode_GetBoolInMapN(&DCtx, 8, &b);
9483 if(QCBORDecode_GetAndResetError(&DCtx) || !b) {
9484 return 3;
9485 }
9486
9487
9488 QCBORDecode_GetBoolInMapSZ(&DCtx, "xx", &b);
9489 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_LABEL_NOT_FOUND) {
9490 return 4;
9491 }
9492
Laurence Lundblade8782dd32021-04-27 04:15:37 -07009493 QCBORDecode_Init(&DCtx,
9494 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMapWrongType),
9495 0);
Laurence Lundblade9f9c3732021-03-23 09:38:46 -07009496 QCBORDecode_EnterMap(&DCtx, NULL);
9497 QCBORDecode_GetBool(&DCtx, &b);
9498 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
9499 return 5;
9500 }
9501
Laurence Lundblade8782dd32021-04-27 04:15:37 -07009502 QCBORDecode_Init(&DCtx,
9503 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMapNWF),
9504 0);
Laurence Lundblade9f9c3732021-03-23 09:38:46 -07009505 QCBORDecode_EnterMap(&DCtx, NULL);
9506 QCBORDecode_GetBool(&DCtx, &b);
9507 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_HIT_END) {
9508 return 6;
9509 }
9510
Laurence Lundblade8782dd32021-04-27 04:15:37 -07009511
9512 QCBORDecode_Init(&DCtx,
9513 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spNullInMap),
9514 0);
9515 QCBORDecode_EnterMap(&DCtx, NULL);
9516 QCBORDecode_GetNull(&DCtx);
9517 if(QCBORDecode_GetAndResetError(&DCtx)) {
9518 return 7;
9519 }
9520
9521 QCBORDecode_Init(&DCtx,
9522 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMap),
9523 0);
9524 QCBORDecode_EnterMap(&DCtx, NULL);
9525 QCBORDecode_GetNull(&DCtx);
9526 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
9527 return 8;
9528 }
9529
9530 QCBORDecode_Init(&DCtx,
9531 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spNullInMap),
9532 0);
9533 QCBORDecode_EnterMap(&DCtx, NULL);
9534 QCBORDecode_GetNullInMapN(&DCtx, 8);
9535 if(QCBORDecode_GetAndResetError(&DCtx)) {
9536 return 9;
9537 }
9538
9539 QCBORDecode_Init(&DCtx,
9540 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMap),
9541 0);
9542 QCBORDecode_EnterMap(&DCtx, NULL);
9543 QCBORDecode_GetNullInMapN(&DCtx, 8);
9544 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
9545 return 10;
9546 }
9547
9548 QCBORDecode_Init(&DCtx,
9549 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMapNWF),
9550 0);
9551 QCBORDecode_EnterMap(&DCtx, NULL);
9552 QCBORDecode_GetUndefined(&DCtx);
9553 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_HIT_END) {
9554 return 11;
9555 }
9556
9557 QCBORDecode_Init(&DCtx,
9558 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUndefinedInMap),
9559 0);
9560 QCBORDecode_EnterMap(&DCtx, NULL);
9561 QCBORDecode_GetUndefined(&DCtx);
9562 if(QCBORDecode_GetAndResetError(&DCtx)) {
9563 return 12;
9564 }
9565
9566 QCBORDecode_Init(&DCtx,
9567 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMap),
9568 0);
9569 QCBORDecode_EnterMap(&DCtx, NULL);
9570 QCBORDecode_GetUndefined(&DCtx);
9571 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
9572 return 13;
9573 }
9574
9575 QCBORDecode_Init(&DCtx,
9576 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUndefinedInMap),
9577 0);
9578 QCBORDecode_EnterMap(&DCtx, NULL);
9579 QCBORDecode_GetUndefinedInMapN(&DCtx, 8);
9580 if(QCBORDecode_GetAndResetError(&DCtx)) {
9581 return 14;
9582 }
9583
9584 QCBORDecode_Init(&DCtx,
9585 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMap),
9586 0);
9587 QCBORDecode_EnterMap(&DCtx, NULL);
9588 QCBORDecode_GetUndefinedInMapN(&DCtx, 8);
9589 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
9590 return 15;
9591 }
9592
9593 QCBORDecode_Init(&DCtx,
9594 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMapNWF),
9595 0);
9596 QCBORDecode_EnterMap(&DCtx, NULL);
9597 QCBORDecode_GetUndefined(&DCtx);
9598 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_HIT_END) {
9599 return 15;
9600 }
9601
Laurence Lundblade9f9c3732021-03-23 09:38:46 -07009602 return 0;
9603}
Laurence Lundbladef00b8be2024-03-08 10:34:33 -08009604
9605
Laurence Lundbladed883ad32024-03-23 22:37:37 -07009606#if !defined(USEFULBUF_DISABLE_ALL_FLOAT) && !defined(QCBOR_DISABLE_PREFERRED_FLOAT)
9607
9608struct PreciseNumberConversion {
9609 char *szDescription;
9610 UsefulBufC CBOR;
9611 QCBORError uError;
9612 uint8_t qcborType;
9613 struct {
9614 int64_t int64;
9615 uint64_t uint64;
9616 double d;
9617 } number;
9618};
9619
9620
9621static const struct PreciseNumberConversion PreciseNumberConversions[] = {
9622 {
9623 "-0.00",
9624 {"\xf9\x80\x00", 3},
9625 QCBOR_SUCCESS,
9626 QCBOR_TYPE_INT64,
9627 {0, 0, 0}
9628 },
9629 {
9630 "NaN",
9631 {"\xf9\x7e\x00", 3},
9632 QCBOR_SUCCESS,
9633 QCBOR_TYPE_DOUBLE,
9634 {0, 0, NAN}
9635 },
9636 {
9637 "NaN payload",
9638 {"\xFB\x7F\xFF\xFF\xFF\xFF\xFF\xFF\xFF", 9},
9639 QCBOR_SUCCESS,
9640 QCBOR_TYPE_DOUBLE,
9641 {0, 0, NAN}
9642 },
9643 {
9644 "65536.0 single",
9645 {"\xFA\x47\x80\x00\x00", 5},
9646 QCBOR_SUCCESS,
9647 QCBOR_TYPE_INT64,
9648 {65536, 0, 0}
9649 },
9650 {
9651 "Infinity",
9652 {"\xf9\x7c\x00", 3},
9653 QCBOR_SUCCESS,
9654 QCBOR_TYPE_DOUBLE,
9655 {0, 0, INFINITY}
9656 },
9657 {
9658 "1.0",
9659 {"\xf9\x3c\x00", 3},
9660 QCBOR_SUCCESS,
9661 QCBOR_TYPE_INT64,
9662 {1, 0, 0}
9663 },
9664 {
9665 "UINT64_MAX",
9666 {"\x1B\xff\xff\xff\xff\xff\xff\xff\xff", 9},
9667 QCBOR_SUCCESS,
9668 QCBOR_TYPE_UINT64,
9669 {0, UINT64_MAX, 0}
9670 },
9671 {
9672 "INT64_MIN",
9673 {"\x3B\x7f\xff\xff\xff\xff\xff\xff\xff", 9},
9674 QCBOR_SUCCESS,
9675 QCBOR_TYPE_INT64,
9676 {INT64_MIN, 0, 0}
9677 },
9678 {
9679 "18446742974197923840",
9680 {"\xFB\x43\xEF\xFF\xFF\xE0\x00\x00\x00", 9},
9681 QCBOR_SUCCESS,
9682 QCBOR_TYPE_UINT64,
9683 {0, 18446742974197923840ULL, 0}
9684 },
9685 {
9686 "65-bit neg, too much precision",
9687 {"\x3B\x80\x00\x00\x00\x00\x00\x00\x01", 9},
9688 QCBOR_SUCCESS,
9689 QCBOR_TYPE_65BIT_NEG_INT,
9690 {0, 0x8000000000000001, 0}
9691 },
9692 {
9693 "65-bit neg lots of precision",
9694 {"\x3B\xff\xff\xff\xff\xff\xff\xf0\x00", 9},
9695 QCBOR_SUCCESS,
9696 QCBOR_TYPE_DOUBLE,
9697 {0, 0, -18446744073709547521.0}
9698 },
9699 {
9700 "65-bit neg very precise",
9701 {"\x3B\xff\xff\xff\xff\xff\xff\xf8\x00", 9},
9702 QCBOR_SUCCESS,
9703 QCBOR_TYPE_DOUBLE,
9704 {0, 0, -18446744073709549569.0}
9705 },
9706 {
9707 "65-bit neg too precise",
9708 {"\x3B\xff\xff\xff\xff\xff\xff\xfc\x00", 9},
9709 QCBOR_SUCCESS,
9710 QCBOR_TYPE_65BIT_NEG_INT,
9711 {0, 18446744073709550592ULL, 0.0}
9712 },
9713 {
9714 "65-bit neg, power of two",
9715 {"\x3B\x80\x00\x00\x00\x00\x00\x00\x00", 9},
9716 QCBOR_SUCCESS,
9717 QCBOR_TYPE_DOUBLE,
9718 {0, 0, -9223372036854775809.0}
9719 },
9720 {
9721 "Zero",
9722 {"\x00", 1},
9723 QCBOR_SUCCESS,
9724 QCBOR_TYPE_INT64,
9725 {0, 0, 0}
9726 },
9727 {
9728 "Pi",
9729 {"\xFB\x40\x09\x2A\xDB\x40\x2D\x16\xB9", 9},
9730 QCBOR_SUCCESS,
9731 QCBOR_TYPE_DOUBLE,
9732 {0, 0, 3.145926}
9733 },
9734 {
9735 "String",
9736 {"\x60", 1},
9737 QCBOR_ERR_UNEXPECTED_TYPE,
9738 QCBOR_TYPE_NONE,
9739 {0, 0, 0}
9740 }
9741};
9742
9743
9744int32_t
9745PreciseNumbersTest(void)
9746{
9747 int i;
9748 QCBORError uErr;
9749 QCBORItem Item;
9750 QCBORDecodeContext DCtx;
9751 const struct PreciseNumberConversion *pTest;
9752
9753 const int count = (int)C_ARRAY_COUNT(PreciseNumberConversions, struct PreciseNumberConversion);
9754
9755 for(i = 0; i < count; i++) {
9756 pTest = &PreciseNumberConversions[i];
9757
9758 if(i == 11) {
9759 uErr = 99; // For break point only
9760 }
9761
9762 QCBORDecode_Init(&DCtx, pTest->CBOR, 0);
9763
9764 QCBORDecode_GetNumberConvertPrecisely(&DCtx, &Item);
9765
9766 uErr = QCBORDecode_GetError(&DCtx);
9767
9768 if(uErr != pTest->uError) {
9769 return i * 1000 + (int)uErr;
9770 }
9771
9772 if(pTest->qcborType != Item.uDataType) {
9773 return i * 1000 + 200;
9774 }
9775
9776 if(pTest->qcborType == QCBOR_TYPE_NONE) {
9777 continue;
9778 }
9779
9780 switch(pTest->qcborType) {
9781 case QCBOR_TYPE_INT64:
9782 if(Item.val.int64 != pTest->number.int64) {
9783 return i * 1000 + 300;
9784 }
9785 break;
9786
9787 case QCBOR_TYPE_UINT64:
9788 case QCBOR_TYPE_65BIT_NEG_INT:
9789 if(Item.val.uint64 != pTest->number.uint64) {
9790 return i * 1000 + 400;
9791 }
9792 break;
9793
9794 case QCBOR_TYPE_DOUBLE:
9795 if(isnan(pTest->number.d)) {
9796 if(!isnan(Item.val.dfnum)) {
9797 return i * 1000 + 600;
9798 }
9799 } else {
9800 if(Item.val.dfnum != pTest->number.d) {
9801 return i * 1000 + 500;
9802 }
9803 }
9804 break;
9805 }
9806 }
9807 return 0;
9808}
9809
9810#endif /* ! USEFULBUF_DISABLE_ALL_FLOAT && ! QCBOR_DISABLE_PREFERRED_FLOAT */
9811
9812
Laurence Lundbladea29f45a2024-05-14 15:55:19 -07009813
Laurence Lundbladeec290b82024-06-10 11:10:54 -07009814#ifndef QCBOR_DISABLE_NON_INTEGER_LABELS
Laurence Lundbladea29f45a2024-05-14 15:55:19 -07009815static const uint8_t spExpectedArray2s[] = {
9816 0x82, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67,
9817 0x31, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67,
9818 0x32};
9819
9820#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
9821static const uint8_t spExpectedArray2sIndef[] = {
9822 0x9f, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67,
9823 0x31, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67,
9824 0x32, 0xff};
9825#endif
9826
9827static const uint8_t spExpectedMap4[] = {
9828 0xa4, 0x67, 0x62, 0x79, 0x74, 0x65, 0x73, 0x20,
9829 0x31, 0x44, 0x78, 0x78, 0x78, 0x78, 0x67, 0x62,
9830 0x79, 0x74, 0x65, 0x73, 0x20, 0x32, 0x44, 0x79,
9831 0x79, 0x79, 0x79, 0x6b, 0x61, 0x6e, 0x6f, 0x74,
9832 0x68, 0x65, 0x72, 0x20, 0x69, 0x6e, 0x74, 0x18,
9833 0x62, 0x66, 0x74, 0x65, 0x78, 0x74, 0x20, 0x32,
9834 0x78, 0x1e, 0x6c, 0x69, 0x65, 0x73, 0x2c, 0x20,
9835 0x64, 0x61, 0x6d, 0x6e, 0x20, 0x6c, 0x69, 0x65,
9836 0x73, 0x20, 0x61, 0x6e, 0x64, 0x20, 0x73, 0x74,
9837 0x61, 0x74, 0x69, 0x73, 0x74, 0x69, 0x63, 0x73};
9838
9839
9840#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
9841
9842static const uint8_t spExpectedMap4Indef[] = {
9843 0xbf, 0x67, 0x62, 0x79, 0x74, 0x65, 0x73, 0x20,
9844 0x31, 0x44, 0x78, 0x78, 0x78, 0x78, 0x67, 0x62,
9845 0x79, 0x74, 0x65, 0x73, 0x20, 0x32, 0x44, 0x79,
9846 0x79, 0x79, 0x79, 0x6b, 0x61, 0x6e, 0x6f, 0x74,
9847 0x68, 0x65, 0x72, 0x20, 0x69, 0x6e, 0x74, 0x18,
9848 0x62, 0x66, 0x74, 0x65, 0x78, 0x74, 0x20, 0x32,
9849 0x78, 0x1e, 0x6c, 0x69, 0x65, 0x73, 0x2c, 0x20,
9850 0x64, 0x61, 0x6d, 0x6e, 0x20, 0x6c, 0x69, 0x65,
9851 0x73, 0x20, 0x61, 0x6e, 0x64, 0x20, 0x73, 0x74,
9852 0x61, 0x74, 0x69, 0x73, 0x74, 0x69, 0x63, 0x73,
9853 0xff};
9854
9855/*
9856 * [[[[[0, []]]]], 0]
9857 */
9858static const uint8_t spDefAndIndef[] = {
9859 0x82,
9860 0x9f, 0x9f, 0x9f, 0x82, 0x00, 0x9f, 0xff, 0xff, 0xff, 0xff, 0x00
9861};
9862#endif /* !QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
9863
Laurence Lundbladea29f45a2024-05-14 15:55:19 -07009864#ifndef QCBOR_DISABLE_TAGS
9865/* An exp / mant tag in two nested arrays */
9866static const uint8_t spExpMant[] = {0x81, 0x81, 0xC4, 0x82, 0x20, 0x03};
9867#endif /* !QCBOR_DISABLE_TAGS */
Laurence Lundbladeec290b82024-06-10 11:10:54 -07009868#endif
Laurence Lundbladea29f45a2024-05-14 15:55:19 -07009869
Laurence Lundbladeec290b82024-06-10 11:10:54 -07009870#ifndef QCBOR_DISABLE_NON_INTEGER_LABELS
Laurence Lundbladea29f45a2024-05-14 15:55:19 -07009871int32_t GetMapAndArrayTest(void)
9872{
9873 QCBORDecodeContext DCtx;
9874 size_t uPosition ;
9875 QCBORItem Item;
9876 UsefulBufC ReturnedEncodedCBOR;
9877
Laurence Lundbladeec290b82024-06-10 11:10:54 -07009878 // Improvement: rework so it can run with QCBOR_DISABLE_NON_INTEGER_LABELS
Laurence Lundbladea29f45a2024-05-14 15:55:19 -07009879 QCBORDecode_Init(&DCtx,
9880 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded),
9881 0);
9882
9883 QCBORDecode_EnterMap(&DCtx, NULL);
9884 QCBORDecode_VGetNextConsume(&DCtx, &Item);
9885 QCBORDecode_GetArray(&DCtx, &Item, &ReturnedEncodedCBOR);
9886 if(QCBORDecode_GetError(&DCtx)) {
9887 return 1;
9888 }
9889 if(Item.val.uCount != 2) {
9890 return 2;
9891 }
9892 if(UsefulBuf_Compare(ReturnedEncodedCBOR, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedArray2s))) {
9893 return 3;
9894 }
9895
9896 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
9897 UsefulBuf_Compare(Item.label.string, UsefulBuf_FROM_SZ_LITERAL("an array of two strings"))) {
9898 return 4;
9899 }
9900
9901 uPosition = QCBORDecode_Tell(&DCtx);
9902
9903
9904 QCBORDecode_GetMap(&DCtx, &Item, &ReturnedEncodedCBOR);
9905 if(QCBORDecode_GetError(&DCtx)) {
9906 return 10;
9907 }
9908 if(Item.val.uCount != 4) {
9909 return 11;
9910 }
9911 if(UsefulBuf_Compare(ReturnedEncodedCBOR, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedMap4))) {
9912 return 12;
9913 }
9914 uPosition = QCBORDecode_Tell(&DCtx);
9915 QCBORDecode_GetArrayFromMapSZ(&DCtx,
9916 "an array of two strings",
9917 &Item,
9918 &ReturnedEncodedCBOR);
9919 if(QCBORDecode_GetError(&DCtx)) {
9920 return 20;
9921 }
9922 if(Item.val.uCount != 2) {
9923 return 21;
9924 }
9925 if(UsefulBuf_Compare(ReturnedEncodedCBOR, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedArray2s))) {
9926 return 22;
9927 }
9928 if(uPosition != QCBORDecode_Tell(&DCtx)) {
9929 return 23;
9930 }
9931
9932 QCBORDecode_Rewind(&DCtx);
9933
9934 uPosition = QCBORDecode_Tell(&DCtx);
9935 QCBORDecode_GetMapFromMapSZ(&DCtx, "map in a map", &Item, &ReturnedEncodedCBOR);
9936 if(QCBORDecode_GetError(&DCtx)) {
9937 return 30;
9938 }
9939 if(Item.val.uCount != 4) {
9940 return 31;
9941 }
9942 if(UsefulBuf_Compare(ReturnedEncodedCBOR, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedMap4))) {
9943 return 32;
9944 }
9945 if(uPosition != QCBORDecode_Tell(&DCtx)) {
9946 return 33;
9947 }
9948
9949 uPosition = QCBORDecode_Tell(&DCtx);
9950 QCBORDecode_GetArrayFromMapSZ(&DCtx, "map in a map", &Item, &ReturnedEncodedCBOR);
9951 if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
9952 return 40;
9953 }
9954 if(UINT32_MAX != QCBORDecode_Tell(&DCtx)) {
9955 return 41;
9956 }
9957 QCBORDecode_GetAndResetError(&DCtx);
9958 if(uPosition != QCBORDecode_Tell(&DCtx)) {
9959 return 42;
9960 }
9961
9962
9963#ifndef QCBOR_DISABLE_TAGS
9964 UsefulBufC ExpMant = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpMant);
9965 QCBORDecode_Init(&DCtx, ExpMant, 0);
9966 QCBORDecode_EnterArray(&DCtx, NULL);
9967 QCBORDecode_EnterArray(&DCtx, NULL);
9968 QCBORDecode_GetArray(&DCtx, &Item, &ReturnedEncodedCBOR);
9969 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) {
9970 return 200;
9971 }
9972 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
9973 return 201;
9974 }
9975 if(!QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_DECIMAL_FRACTION)) {
9976 return 202;
9977 }
9978 if(Item.val.uCount != 2) {
9979 return 201;
9980 }
9981 if(UsefulBuf_Compare(ReturnedEncodedCBOR, UsefulBuf_Tail(ExpMant, 2))) {
9982 return 205;
9983 }
9984#endif /* !QCBOR_DISABLE_TAGS */
9985
9986
9987#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
9988
9989 UsefulBufC DefAndIndef = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spDefAndIndef);
9990 QCBORDecode_Init(&DCtx, DefAndIndef, 0);
9991 QCBORDecode_EnterArray(&DCtx, NULL);
9992 QCBORDecode_GetArray(&DCtx, &Item, &ReturnedEncodedCBOR);
9993 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) {
9994 return 50;
9995 }
9996 if(UsefulBuf_Compare(ReturnedEncodedCBOR, UsefulBuf_Tail(UsefulBuf_Head(DefAndIndef, 11), 1))) {
9997 return 51;
9998 }
9999
10000 QCBORDecode_Init(&DCtx, DefAndIndef, 0);
10001 QCBORDecode_EnterArray(&DCtx, NULL);
10002 QCBORDecode_EnterArray(&DCtx, NULL);
10003 QCBORDecode_GetArray(&DCtx, &Item, &ReturnedEncodedCBOR);
10004 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) {
10005 return 52;
10006 }
10007 if(UsefulBuf_Compare(ReturnedEncodedCBOR, UsefulBuf_Tail(UsefulBuf_Head(DefAndIndef, 10), 2))) {
10008 return 53;
10009 }
10010
10011 QCBORDecode_Init(&DCtx, DefAndIndef, 0);
10012 QCBORDecode_EnterArray(&DCtx, NULL);
10013 QCBORDecode_EnterArray(&DCtx, NULL);
10014 QCBORDecode_EnterArray(&DCtx, NULL);
10015 QCBORDecode_GetArray(&DCtx, &Item, &ReturnedEncodedCBOR);
10016 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) {
10017 return 54;
10018 }
10019 if(UsefulBuf_Compare(ReturnedEncodedCBOR, UsefulBuf_Tail(UsefulBuf_Head(DefAndIndef, 9), 3))) {
10020 return 55;
10021 }
10022 QCBORDecode_Init(&DCtx, DefAndIndef, 0);
10023 QCBORDecode_EnterArray(&DCtx, NULL);
10024 QCBORDecode_EnterArray(&DCtx, NULL);
10025 QCBORDecode_EnterArray(&DCtx, NULL);
10026 QCBORDecode_EnterArray(&DCtx, NULL);
10027 QCBORDecode_GetArray(&DCtx, &Item, &ReturnedEncodedCBOR);
10028 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) {
10029 return 56;
10030 }
10031 if(UsefulBuf_Compare(ReturnedEncodedCBOR, UsefulBuf_Tail(UsefulBuf_Head(DefAndIndef, 8), 4))) {
10032 return 57;
10033 }
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_EnterArray(&DCtx, NULL);
10041 QCBORDecode_VGetNextConsume(&DCtx, &Item);
10042 QCBORDecode_GetArray(&DCtx, &Item, &ReturnedEncodedCBOR);
10043 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) {
10044 return 58;
10045 }
10046 if(UsefulBuf_Compare(ReturnedEncodedCBOR, UsefulBuf_Tail(UsefulBuf_Head(DefAndIndef, 8), 6))) {
10047 return 59;
10048 }
10049
10050
10051 QCBORDecode_Init(&DCtx,
10052 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapIndefEncoded),
10053 0);
10054
10055 QCBORDecode_EnterMap(&DCtx, NULL);
10056 QCBORDecode_VGetNextConsume(&DCtx, &Item);
10057 QCBORDecode_GetArray(&DCtx, &Item, &ReturnedEncodedCBOR);
10058 if(QCBORDecode_GetError(&DCtx)) {
10059 return 60;
10060 }
10061 if(Item.val.uCount != UINT16_MAX) {
10062 return 61;
10063 }
10064 if(UsefulBuf_Compare(ReturnedEncodedCBOR, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedArray2sIndef))) {
10065 return 62;
10066 }
10067
10068 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
10069 UsefulBuf_Compare(Item.label.string, UsefulBuf_FROM_SZ_LITERAL("an array of two strings"))) {
10070 return 63;
10071 }
10072
10073 uPosition = QCBORDecode_Tell(&DCtx);
10074
10075
10076 QCBORDecode_GetMap(&DCtx, &Item, &ReturnedEncodedCBOR);
10077 if(QCBORDecode_GetError(&DCtx)) {
10078 return 70;
10079 }
10080 if(Item.val.uCount != UINT16_MAX) {
10081 return 71;
10082 }
10083 if(UsefulBuf_Compare(ReturnedEncodedCBOR, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedMap4Indef))) {
10084 return 72;
10085 }
10086
10087
10088 uPosition = QCBORDecode_Tell(&DCtx);
10089 QCBORDecode_GetArrayFromMapSZ(&DCtx,
10090 "an array of two strings",
10091 &Item,
10092 &ReturnedEncodedCBOR);
10093 if(QCBORDecode_GetError(&DCtx)) {
10094 return 80;
10095 }
10096 if(Item.val.uCount != UINT16_MAX) {
10097 return 81;
10098 }
10099 if(UsefulBuf_Compare(ReturnedEncodedCBOR, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedArray2sIndef))) {
10100 return 82;
10101 }
10102 if(uPosition != QCBORDecode_Tell(&DCtx)) {
10103 return 83;
10104 }
10105
10106 QCBORDecode_Rewind(&DCtx);
10107
10108 uPosition = QCBORDecode_Tell(&DCtx);
10109 QCBORDecode_GetMapFromMapSZ(&DCtx, "map in a map", &Item, &ReturnedEncodedCBOR);
10110 if(QCBORDecode_GetError(&DCtx)) {
10111 return 90;
10112 }
10113 if(Item.val.uCount != UINT16_MAX) {
10114 return 91;
10115 }
10116 if(UsefulBuf_Compare(ReturnedEncodedCBOR, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedMap4Indef))) {
10117 return 92;
10118 }
10119 if(uPosition != QCBORDecode_Tell(&DCtx)) {
10120 return 93;
10121 }
10122
10123 uPosition = QCBORDecode_Tell(&DCtx);
10124 QCBORDecode_GetArrayFromMapSZ(&DCtx, "map in a map", &Item, &ReturnedEncodedCBOR);
10125 if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
10126 return 100;
10127 }
10128 if(UINT32_MAX != QCBORDecode_Tell(&DCtx)) {
10129 return 101;
10130 }
10131 QCBORDecode_GetAndResetError(&DCtx);
10132 if(uPosition != QCBORDecode_Tell(&DCtx)) {
10133 return 102;
10134 }
Laurence Lundbladeec290b82024-06-10 11:10:54 -070010135#endif /* ! QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
Laurence Lundbladea29f45a2024-05-14 15:55:19 -070010136
10137 return 0;
10138}
Laurence Lundbladeec290b82024-06-10 11:10:54 -070010139#endif /* ! QCBOR_DISABLE_NON_INTEGER_LABELS */
Laurence Lundbladea29f45a2024-05-14 15:55:19 -070010140
10141
Laurence Lundbladef00b8be2024-03-08 10:34:33 -080010142int32_t
10143ErrorHandlingTests(void)
10144{
10145 QCBORDecodeContext DCtx;
10146 QCBORItem Item;
10147 QCBORError uError;
10148 int64_t integer;
10149
10150 /* Test QCBORDecode_SetError() */
10151 QCBORDecode_Init(&DCtx,
10152 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded),
10153 QCBOR_DECODE_MODE_NORMAL);
10154
10155 QCBORDecode_SetError(&DCtx, QCBOR_ERR_FIRST_USER_DEFINED);
10156
10157 QCBORDecode_VGetNext(&DCtx, &Item);
10158
10159 uError = QCBORDecode_GetError(&DCtx);
10160
10161 if(uError != QCBOR_ERR_FIRST_USER_DEFINED) {
10162 return -1;
10163 }
10164
10165 if(Item.uLabelType != QCBOR_TYPE_NONE ||
10166 Item.uDataType != QCBOR_TYPE_NONE) {
10167 return -2;
10168 }
10169
10170
10171 /* Test data type returned from previous error */
10172 QCBORDecode_Init(&DCtx,
10173 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded),
10174 QCBOR_DECODE_MODE_NORMAL);
10175 QCBORDecode_GetInt64(&DCtx, &integer);
10176 uError = QCBORDecode_GetError(&DCtx);
10177 if(uError != QCBOR_ERR_UNEXPECTED_TYPE) {
10178 return -3;
10179 }
10180
10181 QCBORDecode_VGetNext(&DCtx, &Item);
10182 if(Item.uLabelType != QCBOR_TYPE_NONE ||
10183 Item.uDataType != QCBOR_TYPE_NONE) {
10184 return -2;
10185 }
10186 uError = QCBORDecode_GetError(&DCtx);
10187 if(uError != QCBOR_ERR_UNEXPECTED_TYPE) {
10188 return -3;
10189 }
10190
10191
10192 /* Test error classification functions */
10193
10194 if(!QCBORDecode_IsUnrecoverableError(QCBOR_ERR_INDEFINITE_STRING_CHUNK)) {
10195 return -10;
10196 }
10197 if(QCBORDecode_IsUnrecoverableError(QCBOR_SUCCESS)) {
10198 return -11;
10199 }
10200 if(!QCBORDecode_IsUnrecoverableError(QCBOR_ERR_INDEFINITE_STRING_CHUNK)) {
10201 return -12;
10202 }
10203 if(QCBORDecode_IsUnrecoverableError(QCBOR_ERR_DUPLICATE_LABEL)) {
10204 return -13;
10205 }
10206
10207 if(!QCBORDecode_IsNotWellFormedError(QCBOR_ERR_BAD_TYPE_7)) {
10208 return -20;
10209 }
10210 if(!QCBORDecode_IsNotWellFormedError(QCBOR_ERR_BAD_BREAK)) {
10211 return -21;
10212 }
10213 if(QCBORDecode_IsNotWellFormedError(QCBOR_SUCCESS)) {
10214 return -22;
10215 }
10216 if(QCBORDecode_IsNotWellFormedError(QCBOR_ERR_ARRAY_DECODE_TOO_LONG)) {
10217 return -23;
10218 }
10219
Laurence Lundbladecd34e582024-06-06 10:55:14 -070010220 /* Test error strings */
10221 const char *szErrString;
10222
10223 szErrString = qcbor_err_to_str(QCBOR_ERR_ARRAY_DECODE_TOO_LONG);
10224 if(szErrString == NULL) {
10225 return -100;
10226 }
10227 if(strcmp(szErrString, "QCBOR_ERR_ARRAY_DECODE_TOO_LONG")) {
10228 return -101;
10229 }
10230
10231 szErrString = qcbor_err_to_str(QCBOR_SUCCESS);
10232 if(szErrString == NULL) {
10233 return -102;
10234 }
10235 if(strcmp(szErrString, "QCBOR_SUCCESS")) {
10236 return -103;
10237 }
10238
10239 szErrString = qcbor_err_to_str(100);
10240 if(szErrString == NULL) {
10241 return -104;
10242 }
10243 if(strcmp(szErrString, "Unidentified QCBOR error")) {
10244 return -105;
10245 }
10246
10247 szErrString = qcbor_err_to_str(200);
10248 if(szErrString == NULL) {
10249 return -106;
10250 }
10251 if(strcmp(szErrString, "USER_DEFINED_200")) {
10252 return -107;
10253 }
10254
Laurence Lundbladef00b8be2024-03-08 10:34:33 -080010255 return 0;
10256}
Laurence Lundblade11654912024-05-09 11:49:24 -070010257
10258
10259int32_t TellTests(void)
10260{
10261 QCBORDecodeContext DCtx;
10262 QCBORItem Item;
10263 uint32_t uPosition;
10264 int nIndex;
10265 int64_t nDecodedInt;
10266
Laurence Lundbladeec290b82024-06-10 11:10:54 -070010267 // Improvement: rewrite so this can run with only integer labels
Laurence Lundblade11654912024-05-09 11:49:24 -070010268 static const uint32_t aPos[] =
10269 {0, 1, 17, 42, 50, 58, 72, 85, 98, 112, UINT32_MAX};
10270 QCBORDecode_Init(&DCtx,
10271 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded),
10272 0);
10273 for(nIndex = 0; ; nIndex++) {
10274 uPosition = QCBORDecode_Tell(&DCtx);
10275 if(uPosition != aPos[nIndex]) {
10276 return nIndex;
10277 }
10278
10279 if(uPosition == UINT32_MAX) {
10280 break;
10281 }
10282
10283 QCBORDecode_VGetNext(&DCtx, &Item);
10284 }
10285
10286#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
10287 static const uint32_t aPosIndef[] =
10288 {0, 1, 17, 42, 50, 59, 73, 86, 99, 113, UINT32_MAX};
10289 QCBORDecode_Init(&DCtx,
10290 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapIndefEncoded),
10291 0);
10292
10293 for(nIndex = 0; ; nIndex++) {
10294 uPosition = QCBORDecode_Tell(&DCtx);
10295 if(uPosition != aPosIndef[nIndex]) {
10296 return nIndex + 100;
10297 }
10298
10299 if(uPosition == UINT32_MAX) {
10300 break;
10301 }
10302
10303 QCBORDecode_VGetNext(&DCtx, &Item);
10304 }
10305#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
10306
10307 /* Next, some tests with entered maps and arrays */
10308 QCBORDecode_Init(&DCtx,
10309 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded),
10310 0);
10311 QCBORDecode_EnterMap(&DCtx, &Item);
10312 if(QCBORDecode_Tell(&DCtx) != 1) {
10313 return 1001;
10314 }
10315 QCBORDecode_GetInt64InMapSZ(&DCtx, "first integer", &nDecodedInt);
10316 if(QCBORDecode_Tell(&DCtx) != 1) {
10317 return 1002;
10318 }
10319 QCBORDecode_EnterMapFromMapSZ(&DCtx, "map in a map");
10320 if(QCBORDecode_Tell(&DCtx) != 72) {
10321 return 1003;
10322 }
10323
10324 QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt);
10325 if(nDecodedInt != 98) {
10326 return 1004;
10327 }
10328 /* Getting non-aggregate types doesn't affect cursor position. */
10329 if(QCBORDecode_Tell(&DCtx) != 72) {
10330 return 1005;
10331 }
10332 QCBORDecode_VGetNext(&DCtx, &Item);
10333 if(QCBORDecode_Tell(&DCtx) != 85) {
10334 return 1006;
10335 }
10336 QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt);
10337 if(nDecodedInt != 98) {
10338 return 1007;
10339 }
10340 /* Getting non-aggregate types doesn't affect cursor position. */
10341 if(QCBORDecode_Tell(&DCtx) != 85) {
10342 return 1008;
10343 }
10344
10345 QCBORDecode_ExitMap(&DCtx);
10346 if(QCBORDecode_Tell(&DCtx) != UINT32_MAX) {
10347 return 1009;
10348 }
10349 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_NO_MORE_ITEMS) {
10350 return 1010;
10351 }
10352
10353#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
10354 /* Next, some tests with entered maps and arrays */
10355 QCBORDecode_Init(&DCtx,
10356 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapIndefEncoded),
10357 0);
10358 QCBORDecode_EnterMap(&DCtx, &Item);
10359 if(QCBORDecode_Tell(&DCtx) != 1) {
10360 return 2000;
10361 }
10362 QCBORDecode_GetInt64InMapSZ(&DCtx, "first integer", &nDecodedInt);
10363 if(QCBORDecode_Tell(&DCtx) != 1) {
10364 return 2001;
10365 }
10366 QCBORDecode_EnterMapFromMapSZ(&DCtx, "map in a map");
10367 if(QCBORDecode_Tell(&DCtx) != 73) {
10368 return 2002;
10369 }
10370
10371 QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt);
10372 if(nDecodedInt != 98) {
10373 return 2003;
10374 }
10375 /* Getting non-aggregate types doesn't affect cursor position. */
10376 if(QCBORDecode_Tell(&DCtx) != 73) {
10377 return 2004;
10378 }
10379 QCBORDecode_VGetNext(&DCtx, &Item);
10380 if(QCBORDecode_Tell(&DCtx) != 86) {
10381 return 2005;
10382 }
10383 QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt);
10384 if(nDecodedInt != 98) {
10385 return 2006;
10386 }
10387 /* Getting non-aggregate types doesn't affect cursor position. */
10388 if(QCBORDecode_Tell(&DCtx) != 86) {
10389 return 2007;
10390 }
10391
10392 QCBORDecode_ExitMap(&DCtx);
10393 if(QCBORDecode_Tell(&DCtx) != UINT32_MAX) {
10394 return 2008;
10395 }
10396 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_NO_MORE_ITEMS) {
10397 return 2010;
10398 }
10399#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
10400
10401
10402
10403 /* Error state test */
10404 QCBORDecode_Init(&DCtx,
10405 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded),
10406 0);
10407 /* Cause an error */
10408 QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt);
10409 if(QCBORDecode_Tell(&DCtx) != UINT32_MAX) {
10410 return 3000;
10411 }
10412
10413 /* Empties tests */
10414 const uint8_t pMinimalCBOR[] = {0xa0}; // One empty map
10415 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pMinimalCBOR),0);
10416 if(QCBORDecode_Tell(&DCtx) != 0) {
10417 return 4000;
10418 }
10419 QCBORDecode_EnterMap(&DCtx, &Item);
10420 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) {
10421 return 4001;
10422 }
10423 if(QCBORDecode_Tell(&DCtx) != UINT32_MAX) {
10424 return 4002;
10425 }
10426 QCBORDecode_ExitMap(&DCtx);
10427 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) {
10428 return 4001;
10429 }
10430 if(QCBORDecode_Tell(&DCtx) != UINT32_MAX) {
10431 return 4002;
10432 }
10433 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_NO_MORE_ITEMS) {
10434 return 4010;
10435 }
10436
10437#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
10438 const uint8_t pMinimalIndefCBOR[] = {0xbf, 0xff}; // One empty map
10439 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pMinimalIndefCBOR),0);
10440 if(QCBORDecode_Tell(&DCtx) != 0) {
10441 return 4100;
10442 }
10443 QCBORDecode_EnterMap(&DCtx, &Item);
10444 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) {
10445 return 4101;
10446 }
10447 if(QCBORDecode_Tell(&DCtx) != UINT32_MAX) {
10448 return 4102;
10449 }
10450 QCBORDecode_ExitMap(&DCtx);
10451 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) {
10452 return 4101;
10453 }
10454 if(QCBORDecode_Tell(&DCtx) != UINT32_MAX) {
10455 return 4102;
10456 }
10457 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_NO_MORE_ITEMS) {
10458 return 4110;
10459 }
10460#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
10461
10462 /* Test on a CBOR sequence */
10463 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSequenceTestInput),0);
10464 if(QCBORDecode_Tell(&DCtx) != 0) {
10465 return 5000;
10466 }
10467 QCBORDecode_VGetNext(&DCtx, &Item);
10468 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) {
10469 return 5001;
10470 }
10471 if(QCBORDecode_Tell(&DCtx) != 11) {
10472 return 5002;
10473 }
10474 QCBORDecode_VGetNext(&DCtx, &Item);
10475 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) {
10476 return 5003;
10477 }
10478 if(QCBORDecode_Tell(&DCtx) != 12) {
10479 return 5004;
10480 }
10481 QCBORDecode_VGetNext(&DCtx, &Item);
10482 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) {
10483 return 5005;
10484 }
10485 if(QCBORDecode_Tell(&DCtx) != 17) {
10486 return 5006;
10487 }
10488 QCBORDecode_VGetNext(&DCtx, &Item);
10489 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) {
10490 return 5007;
10491 }
10492 if(QCBORDecode_Tell(&DCtx) != UINT32_MAX) {
10493 return 5008;
10494 }
10495 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_NO_MORE_ITEMS) {
10496 return 5010;
10497 }
10498
10499
10500 QCBORDecode_Init(&DCtx,
10501 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded),
10502 0);
10503 QCBORDecode_EnterMap(&DCtx, &Item);
10504 QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings");
10505 if(QCBORDecode_Tell(&DCtx) != 42) {
10506 return 6001;
10507 }
10508 QCBORDecode_VGetNext(&DCtx, &Item);
10509 if(QCBORDecode_Tell(&DCtx) != 50) {
10510 return 6002;
10511 }
10512 QCBORDecode_VGetNext(&DCtx, &Item);
10513 if(QCBORDecode_Tell(&DCtx) != 58) {
10514 return 6008;
10515 }
10516 QCBORDecode_VGetNext(&DCtx, &Item);
10517 (void)QCBORDecode_GetAndResetError(&DCtx);
10518 if(QCBORDecode_Tell(&DCtx) != 58) {
10519 return 6003;
10520 }
10521 QCBORDecode_ExitArray(&DCtx);
10522 if(QCBORDecode_Tell(&DCtx) != 58) {
10523 return 6004;
10524 }
10525
10526 static const uint32_t aEmptiesPos[] =
10527 {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 13, UINT32_MAX};
10528 QCBORDecode_Init(&DCtx,
10529 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(sEmpties),
10530 0);
10531 for(nIndex = 0; ; nIndex++) {
10532 uPosition = QCBORDecode_Tell(&DCtx);
10533 if(uPosition != aEmptiesPos[nIndex]) {
10534 return nIndex + 200;
10535 }
10536
10537 if(uPosition == UINT32_MAX) {
10538 break;
10539 }
10540
10541 QCBORDecode_VGetNext(&DCtx, &Item);
10542 }
10543
10544#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
10545 static const uint32_t aIndefEmptiesPos[] =
10546 {0, 1, 2, 4, 5, 7, 8, 10, 12, 13, 16, 19, UINT32_MAX};
10547 QCBORDecode_Init(&DCtx,
10548 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(sEmptiesIndef),
10549 0);
10550 for(nIndex = 0; ; nIndex++) {
10551 uPosition = QCBORDecode_Tell(&DCtx);
10552 if(uPosition != aIndefEmptiesPos[nIndex]) {
10553 return nIndex + 300;
10554 }
10555
10556 if(uPosition == UINT32_MAX) {
10557 break;
10558 }
10559
10560 QCBORDecode_VGetNext(&DCtx, &Item);
10561 }
10562#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
10563
10564
10565 return 0;
10566}