blob: 910d43af7efc0908de2a189edbf5db9c815015f2 [file] [log] [blame]
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001/*==============================================================================
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002 * Copyright (c) 2016-2018, The Linux Foundation.
3 * Copyright (c) 2018-2024, Laurence Lundblade.
4 * Copyright (c) 2021, Arm Limited.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are
9 * met:
Laurence Lundblade0dbc9172018-11-01 14:17:21 +070010 * Redistributions of source code must retain the above copyright
11 notice, this list of conditions and the following disclaimer.
12 * Redistributions in binary form must reproduce the above
13 copyright notice, this list of conditions and the following
14 disclaimer in the documentation and/or other materials provided
15 with the distribution.
16 * Neither the name of The Linux Foundation nor the names of its
17 contributors, nor the name "Laurence Lundblade" may be used to
18 endorse or promote products derived from this software without
19 specific prior written permission.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -080020
Laurence Lundblade0dbc9172018-11-01 14:17:21 +070021THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
22WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
23MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
24ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
25BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
28BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
30OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
31IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Laurence Lundbladeee851742020-01-08 08:37:05 -080032 =============================================================================*/
Laurence Lundblade9e3651c2018-10-10 11:49:55 +080033
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080034#include "qcbor_decode_tests.h"
Laurence Lundblade844bb5c2020-03-01 17:27:25 -080035#include "qcbor/qcbor_encode.h"
36#include "qcbor/qcbor_decode.h"
Laurence Lundblade67257dc2020-07-27 03:33:37 -070037#include "qcbor/qcbor_spiffy_decode.h"
Laurence Lundbladed4728fd2018-12-17 15:15:56 -080038#include <string.h>
Laurence Lundblade9e3651c2018-10-10 11:49:55 +080039#include <math.h> // for fabs()
Laurence Lundbladebb1062e2019-08-12 23:28:54 -070040#include "not_well_formed_cbor.h"
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -080041
Laurence Lundblade9b334962020-08-27 10:55:53 -070042// Handy macro to compare a UsefulBuf to a C string
43#define UsefulBufCompareToSZ(x, y) \
44 UsefulBuf_Compare(x, UsefulBuf_FromSZ(y))
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080045
Laurence Lundbladea2e29072018-12-30 09:20:06 -080046#ifdef PRINT_FUNCTIONS_FOR_DEBUGGING
Laurence Lundblade20db9c92018-12-17 11:40:37 -080047#include <stdio.h>
Laurence Lundbladea2e29072018-12-30 09:20:06 -080048
49static void PrintUsefulBufC(const char *szLabel, UsefulBufC Buf)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080050{
51 if(szLabel) {
52 printf("%s ", szLabel);
53 }
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -080054
Laurence Lundblade570fab52018-10-13 18:28:27 +080055 size_t i;
Laurence Lundbladea2e29072018-12-30 09:20:06 -080056 for(i = 0; i < Buf.len; i++) {
57 uint8_t Z = ((uint8_t *)Buf.ptr)[i];
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080058 printf("%02x ", Z);
59 }
60 printf("\n");
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -080061
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080062 fflush(stdout);
63}
Laurence Lundbladee2c893c2020-12-26 17:41:53 -080064#endif /* PRINT_FUNCTIONS_FOR_DEBUGGING */
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080065
Laurence Lundblade5db34da2024-05-30 03:14:35 -070066
67/* Make a test results code that includes three components. Return code
68 * is xxxyyyzzz where zz is the error code, yy is the test number and
69 * zz is check being performed
70 */
71static int32_t
72MakeTestResultCode(uint32_t uTestCase,
73 uint32_t uTestNumber,
74 QCBORError uErrorCode)
75{
76 uint32_t uCode = (uTestCase * 1000000) +
77 (uTestNumber * 1000) +
78 (uint32_t)uErrorCode;
79 return (int32_t)uCode;
80}
81
Laurence Lundbladecc7da412020-12-27 00:09:07 -080082/*
83 [
84 -9223372036854775808,
85 -4294967297,
86 -4294967296,
87 -4294967295,
88 -4294967294,
89 -2147483648,
90 -2147483647,
91 -65538,
92 -65537,
93 -65536,
94 -65535,
95 -65534,
96 -257,
97 -256,
98 -255,
99 -254,
100 -25,
101 -24,
102 -23,
103 -1,
104 0,
105 0,
106 1,
107 22,
108 23,
109 24,
110 25,
111 26,
112 254,
113 255,
114 256,
115 257,
116 65534,
117 65535,
118 65536,
119 65537,
120 65538,
121 2147483647,
122 2147483647,
123 2147483648,
124 2147483649,
125 4294967294,
126 4294967295,
127 4294967296,
128 4294967297,
129 9223372036854775807,
130 18446744073709551615
131 ]
132 */
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800133
Laurence Lundbladeb836efb2018-10-28 20:09:58 +0700134static const uint8_t spExpectedEncodedInts[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800135 0x98, 0x2f, 0x3b, 0x7f, 0xff, 0xff, 0xff, 0xff,
136 0xff, 0xff, 0xff, 0x3b, 0x00, 0x00, 0x00, 0x01,
137 0x00, 0x00, 0x00, 0x00, 0x3a, 0xff, 0xff, 0xff,
138 0xff, 0x3a, 0xff, 0xff, 0xff, 0xfe, 0x3a, 0xff,
139 0xff, 0xff, 0xfd, 0x3a, 0x7f, 0xff, 0xff, 0xff,
140 0x3a, 0x7f, 0xff, 0xff, 0xfe, 0x3a, 0x00, 0x01,
141 0x00, 0x01, 0x3a, 0x00, 0x01, 0x00, 0x00, 0x39,
142 0xff, 0xff, 0x39, 0xff, 0xfe, 0x39, 0xff, 0xfd,
143 0x39, 0x01, 0x00, 0x38, 0xff, 0x38, 0xfe, 0x38,
144 0xfd, 0x38, 0x18, 0x37, 0x36, 0x20, 0x00, 0x00,
145 0x01, 0x16, 0x17, 0x18, 0x18, 0x18, 0x19, 0x18,
146 0x1a, 0x18, 0xfe, 0x18, 0xff, 0x19, 0x01, 0x00,
147 0x19, 0x01, 0x01, 0x19, 0xff, 0xfe, 0x19, 0xff,
148 0xff, 0x1a, 0x00, 0x01, 0x00, 0x00, 0x1a, 0x00,
149 0x01, 0x00, 0x01, 0x1a, 0x00, 0x01, 0x00, 0x02,
150 0x1a, 0x7f, 0xff, 0xff, 0xff, 0x1a, 0x7f, 0xff,
151 0xff, 0xff, 0x1a, 0x80, 0x00, 0x00, 0x00, 0x1a,
152 0x80, 0x00, 0x00, 0x01, 0x1a, 0xff, 0xff, 0xff,
153 0xfe, 0x1a, 0xff, 0xff, 0xff, 0xff, 0x1b, 0x00,
154 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x1b,
155 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01,
156 0x1b, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
157 0xff, 0x1b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
158 0xff, 0xff};
159
160
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800161// return CBOR error or -1 if type of value doesn't match
162
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800163static int32_t IntegerValuesParseTestInternal(QCBORDecodeContext *pDCtx)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800164{
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700165 QCBORItem Item;
166 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800167
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800168 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700169 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800170 if(Item.uDataType != QCBOR_TYPE_ARRAY)
171 return -1;
172
173 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700174 return (int32_t)nCBORError;
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800175 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade570fab52018-10-13 18:28:27 +0800176 Item.val.int64 != -9223372036854775807LL - 1)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800177 return -1;
178
179 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700180 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800181 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade570fab52018-10-13 18:28:27 +0800182 Item.val.int64 != -4294967297)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800183 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800184
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800185 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700186 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800187 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade570fab52018-10-13 18:28:27 +0800188 Item.val.int64 != -4294967296)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800189 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800190
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800191 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700192 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800193 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade570fab52018-10-13 18:28:27 +0800194 Item.val.int64 != -4294967295)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800195 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800196
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800197 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700198 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800199 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade570fab52018-10-13 18:28:27 +0800200 Item.val.int64 != -4294967294)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800201 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800202
203
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800204 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700205 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800206 if(Item.uDataType != QCBOR_TYPE_INT64 ||
207 Item.val.int64 != -2147483648)
208 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800209
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800210 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700211 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800212 if(Item.uDataType != QCBOR_TYPE_INT64 ||
213 Item.val.int64 != -2147483647)
214 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800215
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800216 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700217 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800218 if(Item.uDataType != QCBOR_TYPE_INT64 ||
219 Item.val.int64 != -65538)
220 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800221
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800222 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700223 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800224 if(Item.uDataType != QCBOR_TYPE_INT64 ||
225 Item.val.int64 != -65537)
226 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800227
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800228 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700229 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800230 if(Item.uDataType != QCBOR_TYPE_INT64 ||
231 Item.val.int64 != -65536)
232 return -1;
233
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800234
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800235 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700236 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800237 if(Item.uDataType != QCBOR_TYPE_INT64 ||
238 Item.val.int64 != -65535)
239 return -1;
240
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800241
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800242 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700243 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800244 if(Item.uDataType != QCBOR_TYPE_INT64 ||
245 Item.val.int64 != -65534)
246 return -1;
247
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800248
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800249 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700250 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800251 if(Item.uDataType != QCBOR_TYPE_INT64 ||
252 Item.val.int64 != -257)
253 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800254
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800255 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700256 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800257 if(Item.uDataType != QCBOR_TYPE_INT64 ||
258 Item.val.int64 != -256)
259 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800260
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800261 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700262 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800263 if(Item.uDataType != QCBOR_TYPE_INT64 ||
264 Item.val.int64 != -255)
265 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800266
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800267 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700268 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800269 if(Item.uDataType != QCBOR_TYPE_INT64 ||
270 Item.val.int64 != -254)
271 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800272
273
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800274 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700275 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800276 if(Item.uDataType != QCBOR_TYPE_INT64 ||
277 Item.val.int64 != -25)
278 return -1;
279
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800280
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800281 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700282 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800283 if(Item.uDataType != QCBOR_TYPE_INT64 ||
284 Item.val.int64 != -24)
285 return -1;
286
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800287
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800288 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700289 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800290 if(Item.uDataType != QCBOR_TYPE_INT64 ||
291 Item.val.int64 != -23)
292 return -1;
293
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800294
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800295 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700296 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800297 if(Item.uDataType != QCBOR_TYPE_INT64 ||
298 Item.val.int64 != -1)
299 return -1;
300
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800301
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800302 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700303 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800304 if(Item.uDataType != QCBOR_TYPE_INT64 ||
305 Item.val.int64 != 0)
306 return -1;
307
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800308
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800309 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700310 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800311 if(Item.uDataType != QCBOR_TYPE_INT64 ||
312 Item.val.int64 != 0)
313 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800314
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800315 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700316 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800317 if(Item.uDataType != QCBOR_TYPE_INT64 ||
318 Item.val.int64 != 1)
319 return -1;
320
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800321
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800322 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700323 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800324 if(Item.uDataType != QCBOR_TYPE_INT64 ||
325 Item.val.int64 != 22)
326 return -1;
327
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800328
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800329 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700330 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800331 if(Item.uDataType != QCBOR_TYPE_INT64 ||
332 Item.val.int64 != 23)
333 return -1;
334
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800335
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800336 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700337 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800338 if(Item.uDataType != QCBOR_TYPE_INT64 ||
339 Item.val.int64 != 24)
340 return -1;
341
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800342
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800343 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700344 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800345 if(Item.uDataType != QCBOR_TYPE_INT64 ||
346 Item.val.int64 != 25)
347 return -1;
348
349 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700350 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800351 if(Item.uDataType != QCBOR_TYPE_INT64 ||
352 Item.val.int64 != 26)
353 return -1;
354
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800355
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800356 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700357 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800358 if(Item.uDataType != QCBOR_TYPE_INT64 ||
359 Item.val.int64 != 254)
360 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800361
362
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800363 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700364 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800365 if(Item.uDataType != QCBOR_TYPE_INT64 ||
366 Item.val.int64 != 255)
367 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800368
369
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800370 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700371 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800372 if(Item.uDataType != QCBOR_TYPE_INT64 ||
373 Item.val.int64 != 256)
374 return -1;
375
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800376
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800377 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700378 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800379 if(Item.uDataType != QCBOR_TYPE_INT64 ||
380 Item.val.int64 != 257)
381 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800382
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800383 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700384 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800385 if(Item.uDataType != QCBOR_TYPE_INT64 ||
386 Item.val.int64 != 65534)
387 return -1;
388
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800389
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800390 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700391 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800392 if(Item.uDataType != QCBOR_TYPE_INT64 ||
393 Item.val.int64 != 65535)
394 return -1;
395
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800396
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800397 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700398 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800399 if(Item.uDataType != QCBOR_TYPE_INT64 ||
400 Item.val.int64 != 65536)
401 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800402
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800403 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700404 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800405 if(Item.uDataType != QCBOR_TYPE_INT64 ||
406 Item.val.int64 != 65537)
407 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800408
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800409 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700410 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800411 if(Item.uDataType != QCBOR_TYPE_INT64 ||
412 Item.val.int64 != 65538)
413 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800414
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800415 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700416 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800417 if(Item.uDataType != QCBOR_TYPE_INT64 ||
418 Item.val.int64 != 2147483647)
419 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800420
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800421 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700422 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800423 if(Item.uDataType != QCBOR_TYPE_INT64 ||
424 Item.val.int64 != 2147483647)
425 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800426
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800427 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700428 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800429 if(Item.uDataType != QCBOR_TYPE_INT64 ||
430 Item.val.int64 != 2147483648)
431 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800432
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800433 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700434 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800435 if(Item.uDataType != QCBOR_TYPE_INT64 ||
436 Item.val.int64 != 2147483649)
437 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800438
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800439 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700440 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800441 if(Item.uDataType != QCBOR_TYPE_INT64 ||
442 Item.val.int64 != 4294967294)
443 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800444
445
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800446 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700447 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800448 if(Item.uDataType != QCBOR_TYPE_INT64 ||
449 Item.val.int64 != 4294967295)
450 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800451
452
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800453 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700454 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800455 if(Item.uDataType != QCBOR_TYPE_INT64 ||
456 Item.val.int64 != 4294967296)
457 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800458
459
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800460 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700461 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800462 if(Item.uDataType != QCBOR_TYPE_INT64 ||
463 Item.val.int64 != 4294967297)
464 return -1;
465
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800466
467
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800468 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700469 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800470 if(Item.uDataType != QCBOR_TYPE_INT64 ||
471 Item.val.int64 != 9223372036854775807LL)
472 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800473
474
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800475 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700476 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800477 if(Item.uDataType != QCBOR_TYPE_UINT64 ||
478 Item.val.uint64 != 18446744073709551615ULL)
479 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800480
481
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800482 if(QCBORDecode_Finish(pDCtx) != QCBOR_SUCCESS) {
483 return -1;
484 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800485
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800486 return 0;
487}
488
489
Laurence Lundbladecc7da412020-12-27 00:09:07 -0800490/* One less than the smallest negative integer allowed in C. Decoding
491 this should fail.
492 -9223372036854775809
493 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800494static const uint8_t spTooSmallNegative[] = {
495 0x3b, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
Laurence Lundblade21d1d812019-09-28 22:47:49 -1000496};
497
498
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800499/*
500 Tests the decoding of lots of different integers sizes
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +0800501 and values.
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800502 */
Maxim Zhukovd538f0a2022-12-20 20:40:38 +0300503int32_t IntegerValuesParseTest(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800504{
Laurence Lundblade21d1d812019-09-28 22:47:49 -1000505 int nReturn;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800506 QCBORDecodeContext DCtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800507
Laurence Lundblade21d1d812019-09-28 22:47:49 -1000508 QCBORDecode_Init(&DCtx,
509 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedEncodedInts),
510 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800511
Laurence Lundblade21d1d812019-09-28 22:47:49 -1000512 // The really big test of all successes
513 nReturn = IntegerValuesParseTestInternal(&DCtx);
514 if(nReturn) {
515 return nReturn;
516 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800517
Laurence Lundblade21d1d812019-09-28 22:47:49 -1000518 // The one large negative integer that can be parsed
519 QCBORDecode_Init(&DCtx,
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800520 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTooSmallNegative),
Laurence Lundblade21d1d812019-09-28 22:47:49 -1000521 QCBOR_DECODE_MODE_NORMAL);
522
523 QCBORItem item;
524 if(QCBORDecode_GetNext(&DCtx, &item) != QCBOR_ERR_INT_OVERFLOW) {
525 nReturn = -4000;
526 }
527
528 return(nReturn);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800529}
530
531
532/*
Laurence Lundbladeee851742020-01-08 08:37:05 -0800533 Creates a simple CBOR array and returns it in *pEncoded. The array is
534 malloced and needs to be freed. This is used by several tests.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800535
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800536 Two of the inputs can be set. Two other items in the array are fixed.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800537
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800538 */
539
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -0800540static uint8_t spSimpleArrayBuffer[50];
541
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800542static int32_t CreateSimpleArray(int nInt1, int nInt2, uint8_t **pEncoded, size_t *pEncodedLen)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800543{
544 QCBOREncodeContext ECtx;
545 int nReturn = -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800546
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800547 *pEncoded = NULL;
548 *pEncodedLen = INT32_MAX;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800549
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800550 // loop runs CBOR encoding twice. First with no buffer to
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -0800551 // calculate the length so buffer can be allocated correctly,
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800552 // and last with the buffer to do the actual encoding
553 do {
Laurence Lundblade0595e932018-11-02 22:22:47 +0700554 QCBOREncode_Init(&ECtx, (UsefulBuf){*pEncoded, *pEncodedLen});
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800555 QCBOREncode_OpenArray(&ECtx);
556 QCBOREncode_AddInt64(&ECtx, nInt1);
557 QCBOREncode_AddInt64(&ECtx, nInt2);
558 QCBOREncode_AddBytes(&ECtx, ((UsefulBufC) {"galactic", 8}));
559 QCBOREncode_AddBytes(&ECtx, ((UsefulBufC) {"haven token", 11}));
560 QCBOREncode_CloseArray(&ECtx);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800561
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -0800562 if(QCBOREncode_FinishGetSize(&ECtx, pEncodedLen))
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800563 goto Done;
564
565 if(*pEncoded != NULL) {
566 nReturn = 0;
567 goto Done;
568 }
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -0800569
570 // Use static buffer to avoid dependency on malloc()
571 if(*pEncodedLen > sizeof(spSimpleArrayBuffer)) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800572 goto Done;
573 }
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -0800574 *pEncoded = spSimpleArrayBuffer;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800575
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800576 } while(1);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800577
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -0800578Done:
579 return nReturn;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800580}
581
582
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800583/*
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800584 Some basic CBOR with map and array used in a lot of tests.
585 The map labels are all strings
586
Laurence Lundbladecc7da412020-12-27 00:09:07 -0800587 {
588 "first integer": 42,
589 "an array of two strings": [
590 "string1", "string2"
591 ],
592 "map in a map": {
593 "bytes 1": h'78787878',
594 "bytes 2": h'79797979',
595 "another int": 98,
596 "text 2": "lies, damn lies and statistics"
597 }
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900598 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800599 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800600static const uint8_t pValidMapEncoded[] = {
Laurence Lundbladecc7da412020-12-27 00:09:07 -0800601 0xa3, 0x6d, 0x66, 0x69, 0x72, 0x73, 0x74, 0x20,
602 0x69, 0x6e, 0x74, 0x65, 0x67, 0x65, 0x72, 0x18,
603 0x2a, 0x77, 0x61, 0x6e, 0x20, 0x61, 0x72, 0x72,
604 0x61, 0x79, 0x20, 0x6f, 0x66, 0x20, 0x74, 0x77,
605 0x6f, 0x20, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67,
606 0x73, 0x82, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e,
607 0x67, 0x31, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e,
608 0x67, 0x32, 0x6c, 0x6d, 0x61, 0x70, 0x20, 0x69,
609 0x6e, 0x20, 0x61, 0x20, 0x6d, 0x61, 0x70, 0xa4,
610 0x67, 0x62, 0x79, 0x74, 0x65, 0x73, 0x20, 0x31,
611 0x44, 0x78, 0x78, 0x78, 0x78, 0x67, 0x62, 0x79,
612 0x74, 0x65, 0x73, 0x20, 0x32, 0x44, 0x79, 0x79,
613 0x79, 0x79, 0x6b, 0x61, 0x6e, 0x6f, 0x74, 0x68,
614 0x65, 0x72, 0x20, 0x69, 0x6e, 0x74, 0x18, 0x62,
615 0x66, 0x74, 0x65, 0x78, 0x74, 0x20, 0x32, 0x78,
616 0x1e, 0x6c, 0x69, 0x65, 0x73, 0x2c, 0x20, 0x64,
617 0x61, 0x6d, 0x6e, 0x20, 0x6c, 0x69, 0x65, 0x73,
618 0x20, 0x61, 0x6e, 0x64, 0x20, 0x73, 0x74, 0x61,
619 0x74, 0x69, 0x73, 0x74, 0x69, 0x63, 0x73 };
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800620
Laurence Lundbladee2c893c2020-12-26 17:41:53 -0800621
622#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundblade2c1faf92020-06-26 22:43:56 -0700623// Same as above, but with indefinite lengths.
624static const uint8_t pValidMapIndefEncoded[] = {
Laurence Lundbladecc7da412020-12-27 00:09:07 -0800625 0xbf, 0x6d, 0x66, 0x69, 0x72, 0x73, 0x74, 0x20,
626 0x69, 0x6e, 0x74, 0x65, 0x67, 0x65, 0x72, 0x18,
627 0x2a, 0x77, 0x61, 0x6e, 0x20, 0x61, 0x72, 0x72,
628 0x61, 0x79, 0x20, 0x6f, 0x66, 0x20, 0x74, 0x77,
629 0x6f, 0x20, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67,
630 0x73, 0x9f, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e,
631 0x67, 0x31, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e,
632 0x67, 0x32, 0xff, 0x6c, 0x6d, 0x61, 0x70, 0x20,
633 0x69, 0x6e, 0x20, 0x61, 0x20, 0x6d, 0x61, 0x70,
634 0xbf, 0x67, 0x62, 0x79, 0x74, 0x65, 0x73, 0x20,
635 0x31, 0x44, 0x78, 0x78, 0x78, 0x78, 0x67, 0x62,
636 0x79, 0x74, 0x65, 0x73, 0x20, 0x32, 0x44, 0x79,
637 0x79, 0x79, 0x79, 0x6b, 0x61, 0x6e, 0x6f, 0x74,
638 0x68, 0x65, 0x72, 0x20, 0x69, 0x6e, 0x74, 0x18,
639 0x62, 0x66, 0x74, 0x65, 0x78, 0x74, 0x20, 0x32,
640 0x78, 0x1e, 0x6c, 0x69, 0x65, 0x73, 0x2c, 0x20,
641 0x64, 0x61, 0x6d, 0x6e, 0x20, 0x6c, 0x69, 0x65,
642 0x73, 0x20, 0x61, 0x6e, 0x64, 0x20, 0x73, 0x74,
643 0x61, 0x74, 0x69, 0x73, 0x74, 0x69, 0x63, 0x73,
644 0xff, 0xff};
Laurence Lundbladee2c893c2020-12-26 17:41:53 -0800645#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
Laurence Lundblade2c1faf92020-06-26 22:43:56 -0700646
647
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800648static int32_t ParseOrderedArray(const uint8_t *pEncoded,
Laurence Lundblade02625d42020-06-25 14:41:41 -0700649 size_t nLen,
650 int64_t *pInt1,
651 int64_t *pInt2,
652 const uint8_t **pBuf3,
653 size_t *pBuf3Len,
654 const uint8_t **pBuf4,
655 size_t *pBuf4Len)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800656{
657 QCBORDecodeContext DCtx;
658 QCBORItem Item;
659 int nReturn = -1; // assume error until success
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800660
Laurence Lundbladeee851742020-01-08 08:37:05 -0800661 QCBORDecode_Init(&DCtx,
662 (UsefulBufC){pEncoded, nLen},
663 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800664
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800665 // Make sure the first thing is a map
Laurence Lundblade9b334962020-08-27 10:55:53 -0700666 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
667 Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800668 goto Done;
Laurence Lundblade9b334962020-08-27 10:55:53 -0700669 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800670
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800671 // First integer
Laurence Lundblade9b334962020-08-27 10:55:53 -0700672 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
673 Item.uDataType != QCBOR_TYPE_INT64) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800674 goto Done;
Laurence Lundblade9b334962020-08-27 10:55:53 -0700675 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800676 *pInt1 = Item.val.int64;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800677
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800678 // Second integer
Laurence Lundblade9b334962020-08-27 10:55:53 -0700679 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
680 Item.uDataType != QCBOR_TYPE_INT64) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800681 goto Done;
Laurence Lundblade9b334962020-08-27 10:55:53 -0700682 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800683 *pInt2 = Item.val.int64;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800684
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800685 // First string
Laurence Lundblade9b334962020-08-27 10:55:53 -0700686 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
687 Item.uDataType != QCBOR_TYPE_BYTE_STRING) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800688 goto Done;
Laurence Lundblade9b334962020-08-27 10:55:53 -0700689 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800690 *pBuf3 = Item.val.string.ptr;
691 *pBuf3Len = Item.val.string.len;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800692
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800693 // Second string
Laurence Lundblade9b334962020-08-27 10:55:53 -0700694 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
695 Item.uDataType != QCBOR_TYPE_BYTE_STRING) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800696 goto Done;
Laurence Lundblade9b334962020-08-27 10:55:53 -0700697 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800698 *pBuf4 = Item.val.string.ptr;
699 *pBuf4Len = Item.val.string.len;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800700
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800701 nReturn = 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800702
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800703Done:
704 return(nReturn);
705}
706
707
708
Laurence Lundblade9e3651c2018-10-10 11:49:55 +0800709
Maxim Zhukovd538f0a2022-12-20 20:40:38 +0300710int32_t SimpleArrayTest(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800711{
712 uint8_t *pEncoded;
713 size_t nEncodedLen;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800714
Laurence Lundblade5e390822019-01-06 12:35:01 -0800715 int64_t i1=0, i2=0;
716 size_t i3=0, i4=0;
717 const uint8_t *s3= (uint8_t *)"";
718 const uint8_t *s4= (uint8_t *)"";
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800719
720
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800721 if(CreateSimpleArray(23, 6000, &pEncoded, &nEncodedLen) < 0) {
722 return(-1);
723 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800724
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800725 ParseOrderedArray(pEncoded, nEncodedLen, &i1, &i2, &s3, &i3, &s4, &i4);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800726
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800727 if(i1 != 23 ||
728 i2 != 6000 ||
729 i3 != 8 ||
730 i4 != 11 ||
Laurence Lundbladea44d5062018-10-17 18:45:12 +0530731 memcmp("galactic", s3, 8) !=0 ||
732 memcmp("haven token", s4, 11) !=0) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800733 return(-1);
734 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800735
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800736 return(0);
737}
738
739
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700740/*
741 [
742 0,
743 [],
744 [
745 [],
746 [
747 0
748 ],
749 {},
750 {
751 1: {},
752 2: {},
753 3: []
754 }
755 ]
756 ]
757 */
Laurence Lundbladecc7da412020-12-27 00:09:07 -0800758static uint8_t sEmpties[] = {
759 0x83, 0x00, 0x80, 0x84, 0x80, 0x81, 0x00, 0xa0,
760 0xa3, 0x01, 0xa0, 0x02, 0xa0, 0x03, 0x80};
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700761
Laurence Lundbladee2c893c2020-12-26 17:41:53 -0800762#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundblade02625d42020-06-25 14:41:41 -0700763/* Same as above, but with indefinte lengths */
Laurence Lundbladecc7da412020-12-27 00:09:07 -0800764static const uint8_t sEmptiesIndef[] = {
Laurence Lundblade02625d42020-06-25 14:41:41 -07007650x9F,
Laurence Lundblade2c1faf92020-06-26 22:43:56 -0700766 0x00,
767 0x9F,
768 0xFF,
769 0x9F,
770 0x9F,
771 0xFF,
772 0x9F,
773 0x00,
774 0xFF,
775 0xBF,
776 0xFF,
777 0xBF,
778 0x01,
779 0xBF,
780 0xFF,
781 0x02,
782 0xBF,
783 0xFF,
784 0x03,
785 0x9F,
786 0xFF,
787 0xFF,
788 0xFF,
Laurence Lundblade02625d42020-06-25 14:41:41 -0700789 0xFF};
Laurence Lundbladee2c893c2020-12-26 17:41:53 -0800790#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
Laurence Lundblade02625d42020-06-25 14:41:41 -0700791
792
793static int32_t CheckEmpties(UsefulBufC input, bool bCheckCounts)
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700794{
795 QCBORDecodeContext DCtx;
796 QCBORItem Item;
797
Laurence Lundbladeee851742020-01-08 08:37:05 -0800798 QCBORDecode_Init(&DCtx,
Laurence Lundblade02625d42020-06-25 14:41:41 -0700799 input,
Laurence Lundbladeee851742020-01-08 08:37:05 -0800800 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700801
802 // Array with 3 items
803 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
804 Item.uDataType != QCBOR_TYPE_ARRAY ||
805 Item.uNestingLevel != 0 ||
806 Item.uNextNestLevel != 1 ||
Laurence Lundblade02625d42020-06-25 14:41:41 -0700807 (bCheckCounts && Item.val.uCount != 3)) {
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700808 return -1;
809 }
810
811 // An integer 0
812 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
813 Item.uDataType != QCBOR_TYPE_INT64 ||
814 Item.uNestingLevel != 1 ||
815 Item.uNextNestLevel != 1 ||
816 Item.val.uint64 != 0) {
817 return -2;
818 }
819
820 // An empty array
821 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
822 Item.uDataType != QCBOR_TYPE_ARRAY ||
823 Item.uNestingLevel != 1 ||
824 Item.uNextNestLevel != 1 ||
Laurence Lundblade02625d42020-06-25 14:41:41 -0700825 (bCheckCounts && Item.val.uCount != 0)) {
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700826 return -3;
827 }
828
829 // An array with 4 items
830 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
831 Item.uDataType != QCBOR_TYPE_ARRAY ||
832 Item.uNestingLevel != 1 ||
833 Item.uNextNestLevel != 2 ||
Laurence Lundblade02625d42020-06-25 14:41:41 -0700834 (bCheckCounts && Item.val.uCount != 4)) {
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700835 return -4;
836 }
837
838 // An empty array
839 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
840 Item.uDataType != QCBOR_TYPE_ARRAY ||
841 Item.uNestingLevel != 2 ||
842 Item.uNextNestLevel != 2 ||
Laurence Lundblade02625d42020-06-25 14:41:41 -0700843 (bCheckCounts && Item.val.uCount != 0)) {
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700844 return -5;
845 }
846
847 // An array with 1 item
848 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
849 Item.uDataType != QCBOR_TYPE_ARRAY ||
850 Item.uNestingLevel != 2 ||
851 Item.uNextNestLevel != 3 ||
Laurence Lundblade02625d42020-06-25 14:41:41 -0700852 (bCheckCounts && Item.val.uCount != 1)) {
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700853 return -6;
854 }
855
856 // An integer 0
857 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
858 Item.uDataType != QCBOR_TYPE_INT64 ||
859 Item.uNestingLevel != 3 ||
860 Item.uNextNestLevel != 2 ||
861 Item.val.uint64 != 0) {
862 return -7;
863 }
864
865 // An empty map
866 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
867 Item.uDataType != QCBOR_TYPE_MAP ||
868 Item.uNestingLevel != 2 ||
869 Item.uNextNestLevel != 2 ||
Laurence Lundblade02625d42020-06-25 14:41:41 -0700870 (bCheckCounts && Item.val.uCount != 0)) {
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700871 return -8;
872 }
873
Laurence Lundblade5e87da62020-06-07 03:24:28 -0700874 // A map with 3 items
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700875 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
876 Item.uDataType != QCBOR_TYPE_MAP ||
877 Item.uNestingLevel != 2 ||
878 Item.uNextNestLevel != 3 ||
Laurence Lundblade02625d42020-06-25 14:41:41 -0700879 (bCheckCounts && Item.val.uCount != 3)) {
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700880 return -9;
881 }
882
883 // An empty map
884 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
885 Item.uDataType != QCBOR_TYPE_MAP ||
886 Item.uNestingLevel != 3 ||
887 Item.uNextNestLevel != 3 ||
Laurence Lundblade02625d42020-06-25 14:41:41 -0700888 (bCheckCounts && Item.val.uCount != 0)) {
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700889 return -10;
890 }
891
892 // An empty map
893 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
894 Item.uDataType != QCBOR_TYPE_MAP ||
895 Item.uNestingLevel != 3 ||
896 Item.uNextNestLevel != 3 ||
Laurence Lundblade02625d42020-06-25 14:41:41 -0700897 (bCheckCounts && Item.val.uCount != 0)) {
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700898 return -11;
899 }
900
901 // An empty array
902 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
903 Item.uDataType != QCBOR_TYPE_ARRAY ||
904 Item.uNestingLevel != 3 ||
905 Item.uNextNestLevel != 0 ||
Laurence Lundblade02625d42020-06-25 14:41:41 -0700906 (bCheckCounts && Item.val.uCount != 0)) {
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700907 return -12;
908 }
909
910 if(QCBORDecode_Finish(&DCtx) != QCBOR_SUCCESS) {
911 return -13;
912 }
Laurence Lundblade02625d42020-06-25 14:41:41 -0700913 return 0;
914}
915
916
Maxim Zhukovd538f0a2022-12-20 20:40:38 +0300917int32_t EmptyMapsAndArraysTest(void)
Laurence Lundblade02625d42020-06-25 14:41:41 -0700918{
919 int nResult;
920 nResult = CheckEmpties(UsefulBuf_FROM_BYTE_ARRAY_LITERAL(sEmpties),
921 true);
922 if(nResult) {
923 return nResult;
924 }
925
Laurence Lundbladee2c893c2020-12-26 17:41:53 -0800926#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundblade02625d42020-06-25 14:41:41 -0700927 nResult = CheckEmpties(UsefulBuf_FROM_BYTE_ARRAY_LITERAL(sEmptiesIndef),
928 false);
929
930 if(nResult) {
931 return nResult -100;
932 }
Laurence Lundbladee2c893c2020-12-26 17:41:53 -0800933#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700934
935 return 0;
936}
937
Laurence Lundblade4c532ca2021-02-18 21:31:49 -0700938
939static const uint8_t sEmptyMap[] = {
Michael Richardson87de9af2021-02-18 23:13:31 -0500940 0xA1, //# map(1)
941 0x02, //# unsigned(2)
942 0xA0, //# map(0)
943};
944
945int32_t ParseEmptyMapInMapTest(void)
946{
947 QCBORDecodeContext DCtx;
948 QCBORItem Item;
949 int nReturn = 0;
Laurence Lundblade4c532ca2021-02-18 21:31:49 -0700950 QCBORError uErr;
Michael Richardson87de9af2021-02-18 23:13:31 -0500951
952 QCBORDecode_Init(&DCtx,
953 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(sEmptyMap),
954 QCBOR_DECODE_MODE_NORMAL);
955
956 /* now open the first Map */
Laurence Lundblade4c532ca2021-02-18 21:31:49 -0700957 uErr = QCBORDecode_GetNext(&DCtx, &Item);
958 if(uErr != QCBOR_SUCCESS ||
Michael Richardson87de9af2021-02-18 23:13:31 -0500959 Item.uDataType != QCBOR_TYPE_MAP) {
960 nReturn = -3;
961 goto done;
962 }
963
964 if(QCBORDecode_GetNext(&DCtx, &Item) != 0) {
965 nReturn = -1;
966 goto done;
967 }
968 if(Item.uDataType != QCBOR_TYPE_MAP ||
969 Item.uNestingLevel != 1 ||
970 Item.label.int64 != 2) {
971 nReturn = -2;
972 goto done;
973 }
974
975 done:
976 return(nReturn);
977}
978
Laurence Lundblade4c532ca2021-02-18 21:31:49 -0700979
Michael Richardson87de9af2021-02-18 23:13:31 -0500980/* [[[[[[[[[[]]]]]]]]]] */
Laurence Lundbladecc7da412020-12-27 00:09:07 -0800981static const uint8_t spDeepArrays[] = {
982 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81,
983 0x81, 0x80};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800984
Maxim Zhukovd538f0a2022-12-20 20:40:38 +0300985int32_t ParseDeepArrayTest(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800986{
987 QCBORDecodeContext DCtx;
988 int nReturn = 0;
989 int i;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800990
Laurence Lundbladeee851742020-01-08 08:37:05 -0800991 QCBORDecode_Init(&DCtx,
992 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spDeepArrays),
993 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800994
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800995 for(i = 0; i < 10; i++) {
996 QCBORItem Item;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800997
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800998 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
999 Item.uDataType != QCBOR_TYPE_ARRAY ||
1000 Item.uNestingLevel != i) {
1001 nReturn = -1;
1002 break;
1003 }
1004 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001005
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001006 return(nReturn);
1007}
1008
Laurence Lundbladecc7da412020-12-27 00:09:07 -08001009/* Big enough to test nesting to the depth of 24
1010 [[[[[[[[[[[[[[[[[[[[[[[[[]]]]]]]]]]]]]]]]]]]]]]]]]
1011 */
1012static const uint8_t spTooDeepArrays[] = {
1013 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81,
1014 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81,
1015 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81,
1016 0x80};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001017
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03001018int32_t ParseTooDeepArrayTest(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001019{
1020 QCBORDecodeContext DCtx;
1021 int nReturn = 0;
1022 int i;
1023 QCBORItem Item;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001024
1025
Laurence Lundbladeee851742020-01-08 08:37:05 -08001026 QCBORDecode_Init(&DCtx,
1027 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTooDeepArrays),
1028 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001029
Laurence Lundblade3e0b2302023-12-04 14:02:38 -07001030 for(i = 0; i < QCBOR_MAX_ARRAY_NESTING; i++) {
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001031
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001032 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
1033 Item.uDataType != QCBOR_TYPE_ARRAY ||
1034 Item.uNestingLevel != i) {
1035 nReturn = -1;
1036 break;
1037 }
1038 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001039
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001040 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_ARRAY_DECODE_NESTING_TOO_DEEP)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001041 nReturn = -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001042
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001043 return(nReturn);
1044}
1045
1046
1047
1048
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03001049int32_t ShortBufferParseTest(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001050{
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001051 int nResult = 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001052
Laurence Lundblade06350ea2020-01-27 19:32:40 -08001053 for(size_t nNum = sizeof(spExpectedEncodedInts)-1; nNum; nNum--) {
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001054 QCBORDecodeContext DCtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001055
Laurence Lundbladeee851742020-01-08 08:37:05 -08001056 QCBORDecode_Init(&DCtx,
1057 (UsefulBufC){spExpectedEncodedInts, nNum},
1058 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001059
Laurence Lundblade06350ea2020-01-27 19:32:40 -08001060 const int nErr = IntegerValuesParseTestInternal(&DCtx);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001061
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001062 if(nErr != QCBOR_ERR_HIT_END && nErr != QCBOR_ERR_NO_MORE_ITEMS) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001063 nResult = -1;
1064 goto Done;
1065 }
1066 }
1067Done:
1068 return nResult;
1069}
1070
1071
Laurence Lundblade9e3651c2018-10-10 11:49:55 +08001072
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03001073int32_t ShortBufferParseTest2(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001074{
1075 uint8_t *pEncoded;
1076 int nReturn;
1077 size_t nEncodedLen;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001078
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001079 int64_t i1, i2;
1080 size_t i3, i4;
1081 const uint8_t *s3, *s4;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001082
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001083 nReturn = 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001084
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001085 if(CreateSimpleArray(23, 6000, &pEncoded, &nEncodedLen) < 0) {
1086 return(-1);
1087 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001088
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001089 for(nEncodedLen--; nEncodedLen; nEncodedLen--) {
Laurence Lundblade9b334962020-08-27 10:55:53 -07001090 int nResult = ParseOrderedArray(pEncoded, (uint32_t)nEncodedLen, &i1,
1091 &i2, &s3, &i3, &s4, &i4);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001092 if(nResult == 0) {
1093 nReturn = -1;
1094 }
1095 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001096
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001097 return(nReturn);
1098}
1099
Laurence Lundblade5db34da2024-05-30 03:14:35 -07001100
1101/* This test requires indef strings, HW float and preferred float,... */
1102#if !defined(QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS) && \
1103 !defined(QCBOR_DISABLE_FLOAT_HW_USE) && \
1104 !defined(QCBOR_DISABLE_PREFERRED_FLOAT) && \
1105 !defined(QCBOR_DISABLE_TAGS) && \
1106 !defined(QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS)
1107
1108static const uint8_t pPerverseLabels[] = {
1109 0xae,
1110
1111 0xf5, 0x61, 0x61,
1112
1113 0xf6, 0x61, 0x62,
1114
1115 0xf8, 0xff, 0x61, 0x63,
1116
1117 0xf9, 0x7e, 0x00, 0x61, 0x64,
1118
1119 0xfa, 0x7f, 0x7f, 0xff, 0xff, 0x61, 0x65,
1120
1121 0xfb, 0xff, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x61, 0x66,
1122
1123 0xa1, 0x19, 0x03, 0xe8, 0x10, 0x61, 0x67,
1124
1125 0x81, 0x81, 0x81, 0x80, 0x61, 0x68,
1126
1127 0xc1, 0x09, 0x61, 0x69,
1128
1129 0x82, 0x05, 0xa2, 0x01, 0x02, 0x03, 0x04, 0x61, 0x6a,
1130
1131 0xbf, 0xff, 0x61, 0x6b,
1132
1133 0x9f, 0x11, 0x12, 0x13, 0xff, 0x61, 0x6c,
1134
1135 0x7f, 0x62, 0x41, 0x42, 0x62, 0x43, 0x44, 0xff, 0x61, 0x6d,
1136
1137 0xd9, 0x01, 0x02, 0xbf, 0x7f, 0x61, 0x4a, 0x61, 0x4b, 0xff, 0x00, 0xf4, 0xd7, 0x80 ,0xff, 0x61, 0x6e
1138};
1139#endif
1140
1141
Laurence Lundbladeec290b82024-06-10 11:10:54 -07001142#ifndef QCBOR_DISABLE_NON_INTEGER_LABELS
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301143/*
1144 Decode and thoroughly check a moderately complex
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001145 set of maps. Can be run in QCBOR_DECODE_MODE_NORMAL or in
1146 QCBOR_DECODE_MODE_MAP_STRINGS_ONLY.
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301147 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001148static int32_t ParseMapTest1(QCBORDecodeMode nMode)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001149{
1150 QCBORDecodeContext DCtx;
1151 QCBORItem Item;
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001152 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001153
Laurence Lundbladeee851742020-01-08 08:37:05 -08001154 QCBORDecode_Init(&DCtx,
1155 (UsefulBufC){pValidMapEncoded, sizeof(pValidMapEncoded)},
1156 nMode);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001157
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001158 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001159 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001160 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001161 if(Item.uDataType != QCBOR_TYPE_MAP ||
1162 Item.val.uCount != 3)
1163 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001164
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001165 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001166 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001167 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07001168
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001169 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001170 Item.uDataType != QCBOR_TYPE_INT64 ||
1171 Item.val.int64 != 42 ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301172 Item.uDataAlloc ||
1173 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001174 UsefulBufCompareToSZ(Item.label.string, "first integer")) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001175 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001176 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001177
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001178 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001179 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001180 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001181 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301182 Item.uDataAlloc ||
1183 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001184 UsefulBufCompareToSZ(Item.label.string, "an array of two strings") ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001185 Item.uDataType != QCBOR_TYPE_ARRAY ||
1186 Item.val.uCount != 2)
1187 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001188
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001189 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001190 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001191 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001192 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301193 Item.uDataAlloc ||
1194 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001195 UsefulBufCompareToSZ(Item.val.string, "string1")) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001196 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001197 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001198
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001199 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001200 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001201 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001202 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301203 Item.uDataAlloc ||
1204 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001205 UsefulBufCompareToSZ(Item.val.string, "string2")) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001206 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001207 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001208
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001209 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001210 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001211 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001212 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301213 Item.uDataAlloc ||
1214 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001215 UsefulBufCompareToSZ(Item.label.string, "map in a map") ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001216 Item.uDataType != QCBOR_TYPE_MAP ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001217 Item.val.uCount != 4) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001218 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001219 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001220
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001221 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001222 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001223 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001224 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001225 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("bytes 1"))||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001226 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301227 Item.uDataAlloc ||
1228 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001229 UsefulBufCompareToSZ(Item.val.string, "xxxx")) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001230 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001231 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001232
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001233 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001234 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001235 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001236 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001237 UsefulBufCompareToSZ(Item.label.string, "bytes 2") ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001238 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301239 Item.uDataAlloc ||
1240 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001241 UsefulBufCompareToSZ(Item.val.string, "yyyy")) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001242 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001243 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001244
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001245 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001246 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001247 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001248 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301249 Item.uDataAlloc ||
1250 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001251 UsefulBufCompareToSZ(Item.label.string, "another int") ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001252 Item.uDataType != QCBOR_TYPE_INT64 ||
1253 Item.val.int64 != 98)
1254 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001255
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001256 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001257 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001258 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001259 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001260 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("text 2"))||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001261 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301262 Item.uDataAlloc ||
1263 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001264 UsefulBufCompareToSZ(Item.val.string, "lies, damn lies and statistics")) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001265 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001266 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001267
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001268 return 0;
1269}
Laurence Lundbladeec290b82024-06-10 11:10:54 -07001270#endif /* ! QCBOR_DISABLE_NON_INTEGER_LABELS */
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001271
Laurence Lundblade5db34da2024-05-30 03:14:35 -07001272/* This test requires indef strings, HW float and preferred float,... */
1273#if !defined(QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS) && \
1274 !defined(QCBOR_DISABLE_FLOAT_HW_USE) && \
1275 !defined(QCBOR_DISABLE_PREFERRED_FLOAT) && \
1276 !defined(QCBOR_DISABLE_TAGS) && \
1277 !defined(QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS)
1278
1279/* Utility to decode a one byte string and match to letter. */
1280static QCBORError
1281CheckOneLetterString(QCBORDecodeContext *pDecode, uint8_t letter)
1282{
1283 UsefulBufC Text;
1284 QCBORError uErr;
1285
1286 QCBORDecode_GetTextString(pDecode, &Text);
1287 uErr = QCBORDecode_GetError(pDecode);
1288 if(uErr) {
1289 return uErr;
1290 }
1291
1292 if(Text.len != 1) {
1293 return QCBOR_ERR_FIRST_USER_DEFINED;
1294 }
1295
1296 if(*(const uint8_t *)Text.ptr != letter) {
1297 return QCBOR_ERR_FIRST_USER_DEFINED;
1298 }
1299
1300 return QCBOR_SUCCESS;
1301}
1302#endif
1303
1304
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001305/*
1306 Decode and thoroughly check a moderately complex
Laurence Lundbladed4cc1032020-10-12 04:19:47 -07001307 set of maps in the QCBOR_DECODE_MODE_MAP_AS_ARRAY mode.
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001308 */
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03001309int32_t ParseMapAsArrayTest(void)
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001310{
1311 QCBORDecodeContext DCtx;
Laurence Lundblade5db34da2024-05-30 03:14:35 -07001312 QCBORItem Item;
1313 QCBORError uErr;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001314
Laurence Lundbladeee851742020-01-08 08:37:05 -08001315 QCBORDecode_Init(&DCtx,
1316 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded),
1317 QCBOR_DECODE_MODE_MAP_AS_ARRAY);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001318
Laurence Lundblade5db34da2024-05-30 03:14:35 -07001319 if((uErr = QCBORDecode_GetNext(&DCtx, &Item))) {
1320 return MakeTestResultCode(1, 1, uErr);
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001321 }
Laurence Lundbladed61cbf32018-12-09 11:42:21 -08001322 if(Item.uDataType != QCBOR_TYPE_MAP_AS_ARRAY ||
1323 Item.val.uCount != 6) {
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001324 return -1;
1325 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001326
Laurence Lundblade5db34da2024-05-30 03:14:35 -07001327 if((uErr = QCBORDecode_GetNext(&DCtx, &Item))) {
1328 return (int32_t)uErr;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001329 }
1330 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1331 Item.uDataAlloc ||
1332 Item.uLabelAlloc ||
1333 Item.uLabelType != QCBOR_TYPE_NONE ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001334 UsefulBufCompareToSZ(Item.val.string, "first integer")) {
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001335 return -2;
1336 }
1337
Laurence Lundblade5db34da2024-05-30 03:14:35 -07001338 if((uErr = QCBORDecode_GetNext(&DCtx, &Item))) {
1339 return (int32_t)uErr;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001340 }
1341 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1342 Item.uDataType != QCBOR_TYPE_INT64 ||
1343 Item.val.int64 != 42 ||
1344 Item.uDataAlloc ||
1345 Item.uLabelAlloc) {
1346 return -3;
1347 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001348
Laurence Lundblade5db34da2024-05-30 03:14:35 -07001349 if((uErr = QCBORDecode_GetNext(&DCtx, &Item))) {
1350 return (int32_t)uErr;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001351 }
1352 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1353 Item.uDataAlloc ||
1354 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001355 UsefulBufCompareToSZ(Item.val.string, "an array of two strings") ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001356 Item.uDataType != QCBOR_TYPE_TEXT_STRING) {
1357 return -4;
1358 }
1359
Laurence Lundblade5db34da2024-05-30 03:14:35 -07001360 if((uErr = QCBORDecode_GetNext(&DCtx, &Item))) {
1361 return (int32_t)uErr;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001362 }
1363 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1364 Item.uDataAlloc ||
1365 Item.uLabelAlloc ||
1366 Item.uDataType != QCBOR_TYPE_ARRAY ||
1367 Item.val.uCount != 2) {
1368 return -5;
1369 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001370
Laurence Lundblade5db34da2024-05-30 03:14:35 -07001371 if((uErr = QCBORDecode_GetNext(&DCtx, &Item))) {
1372 return (int32_t)uErr;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001373 }
1374 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1375 Item.val.string.len != 7 ||
1376 Item.uDataAlloc ||
1377 Item.uLabelAlloc ||
1378 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("string1"))) {
1379 return -6;
1380 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001381
Laurence Lundblade5db34da2024-05-30 03:14:35 -07001382 if((uErr = QCBORDecode_GetNext(&DCtx, &Item))) {
1383 return (int32_t)uErr;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001384 }
1385 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1386 Item.uDataAlloc ||
1387 Item.uLabelAlloc ||
1388 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("string2"))) {
1389 return -7;
1390 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001391
1392
Laurence Lundblade5db34da2024-05-30 03:14:35 -07001393 if((uErr = QCBORDecode_GetNext(&DCtx, &Item))) {
1394 return (int32_t)uErr;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001395 }
1396 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1397 Item.uDataAlloc ||
1398 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001399 UsefulBufCompareToSZ(Item.val.string, "map in a map")) {
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001400 return -8;
1401 }
1402
Laurence Lundblade5db34da2024-05-30 03:14:35 -07001403 if((uErr = QCBORDecode_GetNext(&DCtx, &Item))) {
1404 return (int32_t)uErr;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001405 }
1406 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1407 Item.uDataAlloc ||
1408 Item.uLabelAlloc ||
Laurence Lundbladed61cbf32018-12-09 11:42:21 -08001409 Item.uDataType != QCBOR_TYPE_MAP_AS_ARRAY ||
1410 Item.val.uCount != 8) {
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001411 return -9;
1412 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001413
Laurence Lundblade5db34da2024-05-30 03:14:35 -07001414 if((uErr = QCBORDecode_GetNext(&DCtx, &Item))) {
1415 return (int32_t)uErr;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001416 }
1417 if(Item.uLabelType != QCBOR_TYPE_NONE ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001418 UsefulBufCompareToSZ(Item.val.string, "bytes 1") ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001419 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1420 Item.uDataAlloc ||
1421 Item.uLabelAlloc) {
1422 return -10;
1423 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001424
Laurence Lundblade5db34da2024-05-30 03:14:35 -07001425 if((uErr = QCBORDecode_GetNext(&DCtx, &Item))) {
1426 return (int32_t)uErr;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001427 }
1428 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1429 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
1430 Item.uDataAlloc ||
1431 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001432 UsefulBufCompareToSZ(Item.val.string, "xxxx")) {
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001433 return -11;
1434 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001435
Laurence Lundblade5db34da2024-05-30 03:14:35 -07001436 if((uErr = QCBORDecode_GetNext(&DCtx, &Item))) {
1437 return (int32_t)uErr;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001438 }
1439 if(Item.uLabelType != QCBOR_TYPE_NONE ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001440 UsefulBufCompareToSZ(Item.val.string, "bytes 2") ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001441 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1442 Item.uDataAlloc ||
1443 Item.uLabelAlloc) {
1444 return -12;
1445 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001446
Laurence Lundblade5db34da2024-05-30 03:14:35 -07001447 if((uErr = QCBORDecode_GetNext(&DCtx, &Item))) {
1448 return (int32_t)uErr;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001449 }
1450 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1451 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
1452 Item.uDataAlloc ||
1453 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001454 UsefulBufCompareToSZ(Item.val.string, "yyyy")) {
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001455 return -13;
1456 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001457
Laurence Lundblade5db34da2024-05-30 03:14:35 -07001458 if((uErr = QCBORDecode_GetNext(&DCtx, &Item))) {
1459 return (int32_t)uErr;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001460 }
1461 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1462 Item.uDataAlloc ||
1463 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001464 UsefulBufCompareToSZ(Item.val.string, "another int") ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001465 Item.uDataType != QCBOR_TYPE_TEXT_STRING) {
1466 return -14;
1467 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001468
Laurence Lundblade5db34da2024-05-30 03:14:35 -07001469 if((uErr = QCBORDecode_GetNext(&DCtx, &Item))) {
1470 return (int32_t)uErr;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001471 }
1472 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1473 Item.uDataAlloc ||
1474 Item.uLabelAlloc ||
1475 Item.uDataType != QCBOR_TYPE_INT64 ||
1476 Item.val.int64 != 98) {
1477 return -15;
1478 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001479
Laurence Lundblade5db34da2024-05-30 03:14:35 -07001480 if((uErr = QCBORDecode_GetNext(&DCtx, &Item))) {
1481 return (int32_t)uErr;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001482 }
1483 if(Item.uLabelType != QCBOR_TYPE_NONE ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001484 UsefulBufCompareToSZ(Item.val.string, "text 2") ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001485 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1486 Item.uDataAlloc ||
1487 Item.uLabelAlloc) {
1488 return -16;
1489 }
1490
Laurence Lundblade5db34da2024-05-30 03:14:35 -07001491 if((uErr = QCBORDecode_GetNext(&DCtx, &Item))) {
1492 return (int32_t)uErr;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001493 }
1494 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1495 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1496 Item.uDataAlloc ||
1497 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001498 UsefulBufCompareToSZ(Item.val.string, "lies, damn lies and statistics")) {
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001499 return -17;
1500 }
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07001501
1502
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001503 /*
1504 Test with map that nearly QCBOR_MAX_ITEMS_IN_ARRAY items in a
1505 map that when interpreted as an array will be too many. Test
1506 data just has the start of the map, not all the items in the map.
1507 */
1508 static const uint8_t pTooLargeMap[] = {0xb9, 0xff, 0xfd};
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07001509
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001510 QCBORDecode_Init(&DCtx,
1511 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pTooLargeMap),
1512 QCBOR_DECODE_MODE_MAP_AS_ARRAY);
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07001513
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001514 if((QCBOR_ERR_ARRAY_DECODE_TOO_LONG != QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001515 return -50;
1516 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001517
Laurence Lundblade5db34da2024-05-30 03:14:35 -07001518 /* This test requires indef strings, HW float and preferred float,... */
1519#if !defined(QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS) && \
1520 !defined(QCBOR_DISABLE_FLOAT_HW_USE) && \
1521 !defined(QCBOR_DISABLE_PREFERRED_FLOAT) && \
1522 !defined(QCBOR_DISABLE_TAGS) && \
1523 !defined(QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS)
1524
1525 UsefulBufC Encoded;
1526
1527 /* Big decode of a map with a wide variety or labels */
1528 QCBORDecode_Init(&DCtx,
1529 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pPerverseLabels),
1530 QCBOR_DECODE_MODE_MAP_AS_ARRAY);
1531 UsefulBuf_MAKE_STACK_UB(Pool, 100);
1532 QCBORDecode_SetMemPool(&DCtx, Pool, 0);
1533
1534 uErr = QCBORDecode_GetNext(&DCtx, &Item);
1535 if(uErr) {
1536 return MakeTestResultCode(10, 1, uErr);
1537 }
1538 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1539 Item.uDataType != QCBOR_TYPE_MAP_AS_ARRAY) {
1540 return MakeTestResultCode(10, 2, 0);
1541 }
1542
1543 uErr = QCBORDecode_GetNext(&DCtx, &Item);
1544 if(uErr) {
1545 return MakeTestResultCode(10, 3, uErr);
1546 }
1547 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1548 Item.uDataType != QCBOR_TYPE_TRUE) {
1549 return MakeTestResultCode(10, 4, 0);
1550 }
1551
1552 uErr = QCBORDecode_GetNext(&DCtx, &Item);
1553 if(uErr) {
1554 return MakeTestResultCode(10, 5, uErr);
1555 }
1556 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1557 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1558 ((const char *)Item.val.string.ptr)[0] != 'a') {
1559 return MakeTestResultCode(10, 6, 0);
1560 }
1561
1562 uErr = QCBORDecode_GetNext(&DCtx, &Item);
1563 if(uErr) {
1564 return MakeTestResultCode(10, 7, uErr);
1565 }
1566 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1567 Item.uDataType != QCBOR_TYPE_NULL) {
1568 return MakeTestResultCode(10, 8, 0);
1569 }
1570
1571 uErr = QCBORDecode_GetNext(&DCtx, &Item);
1572 if(uErr) {
1573 return MakeTestResultCode(10, 9, uErr);
1574 }
1575 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1576 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1577 ((const char *)Item.val.string.ptr)[0] != 'b') {
1578 return MakeTestResultCode(10, 10, 0);
1579 }
1580
1581 uErr = QCBORDecode_GetNext(&DCtx, &Item);
1582 if(uErr) {
1583 return MakeTestResultCode(10, 11, uErr);
1584 }
1585 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1586 Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE ||
1587 Item.val.int64 != 255) {
1588 return MakeTestResultCode(10, 12, 0);
1589 }
1590
1591 uErr = QCBORDecode_GetNext(&DCtx, &Item);
1592 if(uErr) {
1593 return MakeTestResultCode(10, 13, uErr);
1594 }
1595 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1596 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1597 ((const char *)Item.val.string.ptr)[0] != 'c') {
1598 return MakeTestResultCode(10, 14, 0);
1599 }
1600
1601 uErr = QCBORDecode_GetNext(&DCtx, &Item);
1602 if(uErr) {
1603 return MakeTestResultCode(10, 15, uErr);
1604 }
1605 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1606 Item.uDataType != QCBOR_TYPE_DOUBLE ||
1607 !isnan(Item.val.dfnum)) {
1608 return MakeTestResultCode(10, 16, 0);
1609 }
1610
1611 uErr = QCBORDecode_GetNext(&DCtx, &Item);
1612 if(uErr) {
1613 return MakeTestResultCode(10, 17, uErr);
1614 }
1615 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1616 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1617 ((const char *)Item.val.string.ptr)[0] != 'd') {
1618 return MakeTestResultCode(10, 18, 0);
1619 }
1620
1621
1622 uErr = QCBORDecode_GetNext(&DCtx, &Item);
1623 if(uErr) {
1624 return MakeTestResultCode(10, 19, uErr);
1625 }
1626 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1627 Item.uDataType != QCBOR_TYPE_DOUBLE ||
1628 Item.val.dfnum != 3.4028234663852886E+38) {
1629 return MakeTestResultCode(10, 20, 0);
1630 }
1631
1632 uErr = QCBORDecode_GetNext(&DCtx, &Item);
1633 if(uErr) {
1634 return MakeTestResultCode(10, 21, uErr);
1635 }
1636 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1637 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1638 ((const char *)Item.val.string.ptr)[0] != 'e') {
1639 return MakeTestResultCode(10, 22, 0);
1640 }
1641
1642 uErr = QCBORDecode_GetNext(&DCtx, &Item);
1643 if(uErr) {
1644 return MakeTestResultCode(10, 23, uErr);
1645 }
1646 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1647 Item.uDataType != QCBOR_TYPE_DOUBLE ||
1648 Item.val.dfnum != -INFINITY) {
1649 return MakeTestResultCode(10, 24, 0);
1650 }
1651
1652 uErr = QCBORDecode_GetNext(&DCtx, &Item);
1653 if(uErr) {
1654 return MakeTestResultCode(10, 25, uErr);
1655 }
1656 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1657 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1658 ((const char *)Item.val.string.ptr)[0] != 'f') {
1659 return MakeTestResultCode(10, 26, 0);
1660 }
1661
1662 uErr = QCBORDecode_GetNext(&DCtx, &Item);
1663 if(uErr) {
1664 return MakeTestResultCode(10, 26, uErr);
1665 }
1666 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1667 Item.uDataType != QCBOR_TYPE_MAP_AS_ARRAY ||
1668 Item.val.uCount != 2) {
1669 return MakeTestResultCode(10, 27, 0);
1670 }
1671
1672 uErr = QCBORDecode_GetNext(&DCtx, &Item);
1673 if(uErr) {
1674 return MakeTestResultCode(10, 28, uErr);
1675 }
1676 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1677 Item.uDataType != QCBOR_TYPE_INT64 ||
1678 Item.val.int64 != 1000) {
1679 return MakeTestResultCode(10, 29, 0);
1680 }
1681
1682 uErr = QCBORDecode_GetNext(&DCtx, &Item);
1683 if(uErr) {
1684 return MakeTestResultCode(10, 30, uErr);
1685 }
1686 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1687 Item.uDataType != QCBOR_TYPE_INT64 ||
1688 Item.val.int64 != 16) {
1689 return MakeTestResultCode(10, 31, 0);
1690 }
1691
1692 uErr = QCBORDecode_GetNext(&DCtx, &Item);
1693 if(uErr) {
1694 return MakeTestResultCode(10, 32, uErr);
1695 }
1696 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1697 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1698 ((const char *)Item.val.string.ptr)[0] != 'g') {
1699 return MakeTestResultCode(10, 33, 0);
1700 }
1701
1702 for(int i = 0 ; i < 4; i++) {
1703 uErr = QCBORDecode_GetNext(&DCtx, &Item);
1704 if(uErr) {
1705 return MakeTestResultCode(10, 34, uErr);
1706 }
1707 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1708 Item.uDataType != QCBOR_TYPE_ARRAY) {
1709 return MakeTestResultCode(10, 35, 0);
1710 }
1711 if(i != 3) {
1712 if(Item.val.uCount != 1) {
1713 return MakeTestResultCode(10, 35, 0);
1714 }
1715 }
1716 }
1717 uErr = QCBORDecode_GetNext(&DCtx, &Item);
1718 if(uErr) {
1719 return MakeTestResultCode(10, 36, uErr);
1720 }
1721 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1722 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1723 ((const char *)Item.val.string.ptr)[0] != 'h') {
1724 return MakeTestResultCode(10, 37, 0);
1725 }
1726
1727 uErr = QCBORDecode_GetNext(&DCtx, &Item);
1728 if(uErr) {
1729 return MakeTestResultCode(10, 38, uErr);
1730 }
1731 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1732 Item.uDataType != QCBOR_TYPE_DATE_EPOCH) {
1733 return MakeTestResultCode(10, 39, 0);
1734 }
1735
1736 uErr = QCBORDecode_GetNext(&DCtx, &Item);
1737 if(uErr) {
1738 return MakeTestResultCode(10, 40, uErr);
1739 }
1740 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1741 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1742 ((const char *)Item.val.string.ptr)[0] != 'i') {
1743 return MakeTestResultCode(10, 41, 0);
1744 }
1745
1746 uErr = QCBORDecode_GetNext(&DCtx, &Item);
1747 if(uErr) {
1748 return MakeTestResultCode(10, 42, uErr);
1749 }
1750 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1751 Item.uDataType != QCBOR_TYPE_ARRAY ||
1752 Item.val.uCount != 2) {
1753 return MakeTestResultCode(10, 31, 0);
1754 }
1755
1756 uErr = QCBORDecode_GetNext(&DCtx, &Item);
1757 if(uErr) {
1758 return MakeTestResultCode(10, 43, uErr);
1759 }
1760 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1761 Item.uDataType != QCBOR_TYPE_INT64) {
1762 return MakeTestResultCode(10, 31, 0);
1763 }
1764
1765 uErr = QCBORDecode_GetNext(&DCtx, &Item);
1766 if(uErr) {
1767 return MakeTestResultCode(10, 44, uErr);
1768 }
1769 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1770 Item.uDataType != QCBOR_TYPE_MAP_AS_ARRAY ||
1771 Item.val.uCount != 4) {
1772 return MakeTestResultCode(10, 45, 0);
1773 }
1774
1775 for(int i = 0 ; i < 4; i++) {
1776 uErr = QCBORDecode_GetNext(&DCtx, &Item);
1777 if(uErr) {
1778 return MakeTestResultCode(10, 46, uErr);
1779 }
1780 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1781 Item.uDataType != QCBOR_TYPE_INT64) {
1782 return MakeTestResultCode(10, 47, 0);
1783 }
1784 }
1785 uErr = QCBORDecode_GetNext(&DCtx, &Item);
1786 if(uErr) {
1787 return MakeTestResultCode(10, 48, uErr);
1788 }
1789 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1790 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1791 ((const char *)Item.val.string.ptr)[0] != 'j') {
1792 return MakeTestResultCode(10, 49, 0);
1793 }
1794
1795 uErr = QCBORDecode_GetNext(&DCtx, &Item);
1796 if(uErr) {
1797 return MakeTestResultCode(10, 50, uErr);
1798 }
1799 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1800 Item.uDataType != QCBOR_TYPE_MAP_AS_ARRAY ||
1801 Item.val.uCount != UINT16_MAX) {
1802 return MakeTestResultCode(10, 51, 0);
1803 }
1804
1805 uErr = QCBORDecode_GetNext(&DCtx, &Item);
1806 if(uErr) {
1807 return MakeTestResultCode(10, 52, uErr);
1808 }
1809 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1810 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1811 ((const char *)Item.val.string.ptr)[0] != 'k') {
1812 return MakeTestResultCode(10, 53, 0);
1813 }
1814
1815 uErr = QCBORDecode_GetNext(&DCtx, &Item);
1816 if(uErr) {
1817 return MakeTestResultCode(10, 54, uErr);
1818 }
1819 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1820 Item.uDataType != QCBOR_TYPE_ARRAY ||
1821 Item.val.uCount != UINT16_MAX) {
1822 return MakeTestResultCode(10, 55, 0);
1823 }
1824
1825 for(int i = 0 ; i < 3; i++) {
1826 uErr = QCBORDecode_GetNext(&DCtx, &Item);
1827 if(uErr) {
1828 return MakeTestResultCode(10, 56, uErr);
1829 }
1830 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1831 Item.uDataType != QCBOR_TYPE_INT64) {
1832 return MakeTestResultCode(10, 57, 0);
1833 }
1834 }
1835 uErr = QCBORDecode_GetNext(&DCtx, &Item);
1836 if(uErr) {
1837 return MakeTestResultCode(10, 58, uErr);
1838 }
1839 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1840 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1841 ((const char *)Item.val.string.ptr)[0] != 'l') {
1842 return MakeTestResultCode(10, 59, 0);
1843 }
1844
1845 uErr = QCBORDecode_GetNext(&DCtx, &Item);
1846 if(uErr) {
1847 return MakeTestResultCode(10, 60, uErr);
1848 }
1849 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1850 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1851 Item.val.string.len != 4) {
1852 return MakeTestResultCode(10, 61, 0);
1853 }
1854
1855 uErr = QCBORDecode_GetNext(&DCtx, &Item);
1856 if(uErr) {
1857 return MakeTestResultCode(10, 62, uErr);
1858 }
1859 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1860 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1861 ((const char *)Item.val.string.ptr)[0] != 'm') {
1862 return MakeTestResultCode(10, 63, 0);
1863 }
1864
1865 uErr = QCBORDecode_GetNext(&DCtx, &Item);
1866 if(uErr) {
1867 return MakeTestResultCode(10, 64, uErr);
1868 }
1869 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1870 Item.uDataType != QCBOR_TYPE_MAP_AS_ARRAY ||
1871 !QCBORDecode_IsTagged(&DCtx, &Item, 258) ||
1872 Item.val.uCount != UINT16_MAX) {
1873 return MakeTestResultCode(10, 65, 0);
1874 }
1875
1876 uErr = QCBORDecode_GetNext(&DCtx, &Item);
1877 if(uErr) {
1878 return MakeTestResultCode(10, 66, uErr);
1879 }
1880 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1881 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1882 Item.val.string.len != 2) {
1883 return MakeTestResultCode(10, 67, 0);
1884 }
1885
1886 uErr = QCBORDecode_GetNext(&DCtx, &Item);
1887 if(uErr) {
1888 return MakeTestResultCode(10, 68, uErr);
1889 }
1890 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1891 Item.uDataType != QCBOR_TYPE_INT64 ||
1892 Item.val.int64 != 0) {
1893 return MakeTestResultCode(10, 69, 0);
1894 }
1895
1896 uErr = QCBORDecode_GetNext(&DCtx, &Item);
1897 if(uErr) {
1898 return MakeTestResultCode(10, 70, uErr);
1899 }
1900 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1901 Item.uDataType != QCBOR_TYPE_FALSE) {
1902 return MakeTestResultCode(10, 71, 0);
1903 }
1904
1905 uErr = QCBORDecode_GetNext(&DCtx, &Item);
1906 if(uErr) {
1907 return MakeTestResultCode(10, 72, uErr);
1908 }
1909 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1910 Item.uDataType != QCBOR_TYPE_ARRAY ||
1911 !QCBORDecode_IsTagged(&DCtx, &Item, 23) ||
1912 Item.val.uCount != 0) {
1913 return MakeTestResultCode(10, 73, 0);
1914 }
1915
1916 uErr = QCBORDecode_GetNext(&DCtx, &Item);
1917 if(uErr) {
1918 return MakeTestResultCode(10, 74, uErr);
1919 }
1920 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1921 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1922 ((const char *)Item.val.string.ptr)[0] != 'n') {
1923 return MakeTestResultCode(10, 75, 0);
1924 }
1925
1926
1927 /* Big decode of a map with a wide variety or labels */
1928 QCBORDecode_Init(&DCtx,
1929 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pPerverseLabels),
1930 QCBOR_DECODE_MODE_MAP_AS_ARRAY);
1931 QCBORDecode_SetMemPool(&DCtx, Pool, 0);
1932
1933 QCBORDecode_EnterArray(&DCtx, &Item);
1934 bool b;
1935 QCBORDecode_GetBool(&DCtx, &b);
1936
1937 uErr = CheckOneLetterString(&DCtx, 'a');
1938 if(uErr) {
1939 return MakeTestResultCode(11, 1, uErr);
1940 }
1941
1942 QCBORDecode_GetNull(&DCtx);
1943 uErr = CheckOneLetterString(&DCtx, 'b');
1944 if(uErr) {
1945 return MakeTestResultCode(11, 2, uErr);
1946 }
1947
1948 QCBORDecode_VGetNext(&DCtx, &Item);
1949 uErr = CheckOneLetterString(&DCtx, 'c');
1950 if(uErr) {
1951 return MakeTestResultCode(11, 3, uErr);
1952 }
1953
1954 double dNum;
1955 QCBORDecode_GetDouble(&DCtx, &dNum);
1956 if(!isnan(dNum)) {
1957 return MakeTestResultCode(11, 4, 0);
1958 }
1959 uErr = CheckOneLetterString(&DCtx, 'd');
1960 if(uErr) {
1961 return MakeTestResultCode(11, 5, uErr);
1962 }
1963
1964 QCBORDecode_GetDouble(&DCtx, &dNum);
1965 if( dNum != 3.4028234663852886E+38 ) {
1966 return MakeTestResultCode(11, 6, 0);
1967 }
1968 uErr = CheckOneLetterString(&DCtx, 'e');
1969 if(uErr) {
1970 return MakeTestResultCode(11, 7, uErr);
1971 }
1972
1973 QCBORDecode_GetDouble(&DCtx, &dNum);
1974 if(dNum != -INFINITY) {
1975 return MakeTestResultCode(11, 8, 0);
1976 }
1977 uErr = CheckOneLetterString(&DCtx, 'f');
1978 if(uErr) {
1979 return MakeTestResultCode(11, 9, uErr);
1980 }
1981
1982 int64_t nInt;
1983 QCBORDecode_EnterArray(&DCtx, &Item);
1984 QCBORDecode_GetInt64(&DCtx, &nInt);
1985 QCBORDecode_GetInt64(&DCtx, &nInt);
1986 QCBORDecode_ExitArray(&DCtx);
1987 uErr = CheckOneLetterString(&DCtx, 'g');
1988 if(uErr) {
1989 return MakeTestResultCode(11, 10, uErr);
1990 }
1991
1992 QCBORDecode_EnterArray(&DCtx, &Item);
1993 QCBORDecode_EnterArray(&DCtx, &Item);
1994 QCBORDecode_EnterArray(&DCtx, &Item);
1995 QCBORDecode_EnterArray(&DCtx, &Item);
1996 QCBORDecode_ExitArray(&DCtx);
1997 QCBORDecode_ExitArray(&DCtx);
1998 QCBORDecode_ExitArray(&DCtx);
1999 QCBORDecode_ExitArray(&DCtx);
2000 uErr = CheckOneLetterString(&DCtx, 'h');
2001 if(uErr) {
2002 return MakeTestResultCode(11, 11, uErr);
2003 }
2004 QCBORDecode_GetEpochDate(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &nInt);
2005 uErr = CheckOneLetterString(&DCtx, 'i');
2006 if(uErr) {
2007 return MakeTestResultCode(11, 12, uErr);
2008 }
2009
2010 QCBORDecode_EnterArray(&DCtx, &Item);
2011 QCBORDecode_GetInt64(&DCtx, &nInt);
2012 QCBORDecode_EnterArray(&DCtx, &Item);
2013 QCBORDecode_GetInt64(&DCtx, &nInt);
2014 QCBORDecode_GetInt64(&DCtx, &nInt);
2015 QCBORDecode_GetInt64(&DCtx, &nInt);
2016 QCBORDecode_GetInt64(&DCtx, &nInt);
2017 QCBORDecode_ExitArray(&DCtx);
2018 QCBORDecode_ExitArray(&DCtx);
2019 uErr = CheckOneLetterString(&DCtx, 'j');
2020 if(uErr) {
2021 return MakeTestResultCode(11, 13, uErr);
2022 }
2023
2024 QCBORDecode_GetArray(&DCtx, &Item, &Encoded);
2025 uErr = CheckOneLetterString(&DCtx, 'k');
2026 if(uErr) {
2027 return MakeTestResultCode(11, 14, uErr);
2028 }
2029
2030 QCBORDecode_EnterArray(&DCtx, &Item);
2031 QCBORDecode_GetInt64(&DCtx, &nInt);
2032 QCBORDecode_GetInt64(&DCtx, &nInt);
2033 QCBORDecode_GetInt64(&DCtx, &nInt);
2034 QCBORDecode_ExitArray(&DCtx);
2035 uErr = CheckOneLetterString(&DCtx, 'l');
2036 if(uErr) {
2037 return MakeTestResultCode(11, 15, uErr);
2038 }
2039
2040 QCBORDecode_GetTextString(&DCtx, &Encoded);
2041 uErr = CheckOneLetterString(&DCtx, 'm');
2042 if(uErr) {
2043 return MakeTestResultCode(11, 16, uErr);
2044 }
2045
2046 QCBORDecode_EnterArray(&DCtx, &Item);
2047 if(!QCBORDecode_IsTagged(&DCtx, &Item, 258)) {
2048 return MakeTestResultCode(11, 17, 0);
2049 }
2050 if(Item.uDataType != QCBOR_TYPE_MAP_AS_ARRAY) {
2051 return MakeTestResultCode(11, 18, 0);
2052 }
2053 if(Item.val.uCount != UINT16_MAX) {
2054 return MakeTestResultCode(11, 19, 0);
2055 }
2056 QCBORDecode_GetTextString(&DCtx, &Encoded);
2057 if(Encoded.len != 2) {
2058 return MakeTestResultCode(11, 20, 0);
2059 } QCBORDecode_GetInt64(&DCtx, &nInt);
2060 QCBORDecode_GetBool(&DCtx, &b);
2061 if(b != false) {
2062 return MakeTestResultCode(11, 21, 0);
2063 }
2064 QCBORDecode_EnterArray(&DCtx, &Item);
2065 if(!QCBORDecode_IsTagged(&DCtx, &Item, 23)) {
2066 return MakeTestResultCode(11, 22, 0);
2067 }
2068 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
2069 return MakeTestResultCode(11, 23, 0);
2070 }
2071 if(Item.val.uCount != 0) {
2072 return MakeTestResultCode(11, 24, 0);
2073 }
2074 QCBORDecode_ExitArray(&DCtx);
2075 QCBORDecode_ExitArray(&DCtx);
2076 uErr = CheckOneLetterString(&DCtx, 'n');
2077 if(uErr) {
2078 return MakeTestResultCode(11, 25, uErr);
2079 }
2080
2081 QCBORDecode_ExitArray(&DCtx);
2082 uErr = QCBORDecode_Finish(&DCtx);
2083 if(uErr) {
2084 return MakeTestResultCode(11, 26, uErr);
2085 }
2086#endif /* QCBOR_DISABLE_... */
Laurence Lundbladed4cc1032020-10-12 04:19:47 -07002087
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09002088 return 0;
2089}
2090
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002091
Laurence Lundblade5db34da2024-05-30 03:14:35 -07002092
Laurence Lundbladeec290b82024-06-10 11:10:54 -07002093#ifndef QCBOR_DISABLE_NON_INTEGER_LABELS
Laurence Lundblade5db34da2024-05-30 03:14:35 -07002094
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002095/*
Laurence Lundbladefab1b522018-10-19 13:40:52 +05302096 Fully or partially decode pValidMapEncoded. When
2097 partially decoding check for the right error code.
2098 How much partial decoding depends on nLevel.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002099
Laurence Lundbladefab1b522018-10-19 13:40:52 +05302100 The partial decodes test error conditions of
2101 incomplete encoded input.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002102
Laurence Lundbladefab1b522018-10-19 13:40:52 +05302103 This could be combined with the above test
2104 and made prettier and maybe a little more
2105 thorough.
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002106 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002107static int32_t ExtraBytesTest(int nLevel)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002108{
2109 QCBORDecodeContext DCtx;
2110 QCBORItem Item;
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07002111 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002112
Laurence Lundbladeee851742020-01-08 08:37:05 -08002113 QCBORDecode_Init(&DCtx,
2114 (UsefulBufC){pValidMapEncoded, sizeof(pValidMapEncoded)},
2115 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002116
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08002117 if(nLevel < 1) {
2118 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_EXTRA_BYTES) {
2119 return -1;
2120 } else {
2121 return 0;
2122 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002123 }
Laurence Lundbladefab1b522018-10-19 13:40:52 +05302124
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002125
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09002126 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07002127 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09002128 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002129 if(Item.uDataType != QCBOR_TYPE_MAP ||
2130 Item.val.uCount != 3)
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002131 return -2;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002132
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08002133 if(nLevel < 2) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002134 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
2135 return -3;
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08002136 } else {
2137 return 0;
2138 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002139 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002140
2141
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09002142 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07002143 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09002144 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002145 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002146 Item.uDataType != QCBOR_TYPE_INT64 ||
2147 Item.val.uCount != 42 ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07002148 UsefulBufCompareToSZ(Item.label.string, "first integer")) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002149 return -4;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09002150 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002151
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08002152 if(nLevel < 3) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002153 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
2154 return -5;
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08002155 } else {
2156 return 0;
2157 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002158 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002159
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09002160 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07002161 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09002162 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002163 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07002164 UsefulBufCompareToSZ(Item.label.string, "an array of two strings") ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002165 Item.uDataType != QCBOR_TYPE_ARRAY ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09002166 Item.val.uCount != 2) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002167 return -6;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09002168 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002169
2170
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08002171 if(nLevel < 4) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002172 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
2173 return -7;
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08002174 } else {
2175 return 0;
2176 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002177 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002178
2179
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09002180 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07002181 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09002182 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002183 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07002184 UsefulBufCompareToSZ(Item.val.string, "string1")) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002185 return -8;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09002186 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002187
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08002188 if(nLevel < 5) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002189 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
2190 return -9;
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08002191 } else {
2192 return 0;
2193 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002194 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002195
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09002196 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07002197 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09002198 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002199 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07002200 UsefulBufCompareToSZ(Item.val.string, "string2")) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002201 return -10;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09002202 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002203
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08002204 if(nLevel < 6) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002205 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
2206 return -11;
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08002207 } else {
2208 return 0;
2209 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002210 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002211
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09002212 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07002213 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09002214 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002215 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07002216 UsefulBufCompareToSZ(Item.label.string, "map in a map") ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002217 Item.uDataType != QCBOR_TYPE_MAP ||
2218 Item.val.uCount != 4)
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002219 return -12;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002220
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08002221 if(nLevel < 7) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002222 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
2223 return -13;
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08002224 } else {
2225 return 0;
2226 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002227 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002228
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09002229 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07002230 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09002231 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002232 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07002233 UsefulBufCompareToSZ(Item.label.string, "bytes 1") ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002234 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07002235 UsefulBufCompareToSZ(Item.val.string, "xxxx")) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002236 return -14;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09002237 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002238
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08002239 if(nLevel < 8) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002240 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
2241 return -15;
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08002242 } else {
2243 return 0;
2244 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002245 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002246
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09002247 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07002248 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09002249 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002250 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07002251 UsefulBufCompareToSZ(Item.label.string, "bytes 2") ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002252 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07002253 UsefulBufCompareToSZ(Item.val.string, "yyyy")) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002254 return -16;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09002255 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002256
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08002257 if(nLevel < 9) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002258 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
2259 return -17;
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08002260 } else {
2261 return 0;
2262 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002263 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002264
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09002265 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07002266 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09002267 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002268 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07002269 UsefulBufCompareToSZ(Item.label.string, "another int") ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002270 Item.uDataType != QCBOR_TYPE_INT64 ||
2271 Item.val.int64 != 98)
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002272 return -18;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002273
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08002274 if(nLevel < 10) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002275 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
2276 return -19;
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08002277 } else {
2278 return 0;
2279 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002280 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002281
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09002282 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07002283 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09002284 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002285 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09002286 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("text 2"))||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002287 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07002288 UsefulBufCompareToSZ(Item.val.string, "lies, damn lies and statistics")) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002289 return -20;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09002290 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002291
Laurence Lundbladefab1b522018-10-19 13:40:52 +05302292 if(QCBORDecode_Finish(&DCtx)) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002293 return -21;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002294 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002295
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002296 return 0;
2297}
2298
2299
Laurence Lundbladeb95afec2024-08-26 10:51:28 -07002300/* These are just the item that open large maps and arrays, not
2301 * the items in the array. This is sufficient to test the
2302 * boundary condition. */
2303static const uint8_t spLargeArrayFake[] = {
2304 0x99, 0xff, 0xfe};
2305
2306static const uint8_t spTooLargeArrayFake[] = {
2307 0x99, 0xff, 0xff};
2308
2309static const uint8_t spLargeMapFake[] = {
2310 0xb9, 0x7f, 0xff};
2311
2312static const uint8_t spTooLargeMapFake[] = {
2313 0xba, 0x00, 0x00, 0x80, 0x00};
2314
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002315
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03002316int32_t ParseMapTest(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002317{
Laurence Lundbladeb95afec2024-08-26 10:51:28 -07002318 QCBORDecodeContext DCtx;
2319 QCBORItem Item;
2320 QCBORError uErr;
2321
2322 QCBORDecode_Init(&DCtx,
2323 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spLargeArrayFake),
2324 QCBOR_DECODE_MODE_NORMAL);
2325 uErr = QCBORDecode_GetNext(&DCtx, &Item);
2326 if(uErr != QCBOR_SUCCESS || Item.val.uCount != QCBOR_MAX_ITEMS_IN_ARRAY) {
2327 return -100;
2328 }
2329
2330 QCBORDecode_Init(&DCtx,
2331 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTooLargeArrayFake),
2332 QCBOR_DECODE_MODE_NORMAL);
2333 uErr = QCBORDecode_GetNext(&DCtx, &Item);
2334 if(uErr != QCBOR_ERR_ARRAY_DECODE_TOO_LONG) {
2335 return -101;
2336 }
2337
2338 QCBORDecode_Init(&DCtx,
2339 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spLargeMapFake),
2340 QCBOR_DECODE_MODE_NORMAL);
2341 uErr = QCBORDecode_GetNext(&DCtx, &Item);
2342 if(uErr != QCBOR_SUCCESS || Item.val.uCount != QCBOR_MAX_ITEMS_IN_MAP) {
2343 return -110;
2344 }
2345
2346 QCBORDecode_Init(&DCtx,
2347 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTooLargeMapFake),
2348 QCBOR_DECODE_MODE_NORMAL);
2349 uErr = QCBORDecode_GetNext(&DCtx, &Item);
2350 if(uErr != QCBOR_ERR_ARRAY_DECODE_TOO_LONG) {
2351 return -111;
2352 }
2353
2354
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002355 // Parse a moderatly complex map structure very thoroughly
2356 int32_t nResult = ParseMapTest1(QCBOR_DECODE_MODE_NORMAL);
2357 if(nResult) {
2358 return nResult;
2359 }
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08002360
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002361 // Again, but in strings-only mode. It should succeed since the input
2362 // map has only string labels.
2363 nResult = ParseMapTest1(QCBOR_DECODE_MODE_MAP_STRINGS_ONLY);
2364 if(nResult) {
2365 return nResult;
2366 }
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08002367
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002368 // Again, but try to finish the decoding before the end of the
2369 // input at 10 different place and see that the right error code
2370 // is returned.
2371 for(int i = 0; i < 10; i++) {
2372 nResult = ExtraBytesTest(i);
2373 if(nResult) {
2374 break;
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08002375 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002376 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002377
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002378 return nResult;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002379}
Laurence Lundbladeec290b82024-06-10 11:10:54 -07002380#endif /* ! QCBOR_DISABLE_NON_INTEGER_LABELS */
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002381
2382
Laurence Lundbladecc7da412020-12-27 00:09:07 -08002383/* The simple-values including some not well formed */
2384static const uint8_t spSimpleValues[] = {
2385 0x8a, 0xf4, 0xf5, 0xf6, 0xf7, 0xff, 0xe0, 0xf3,
2386 0xf8, 0x00, 0xf8, 0x13, 0xf8, 0x1f, 0xf8, 0x20,
2387 0xf8, 0xff};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002388
Laurence Lundblade3888f002024-06-12 21:20:56 -07002389/* A map of good simple values, plus one well-formed integer */
2390static const uint8_t spGoodSimpleValues[] = {
2391 0xa9, 0x01, 0xf4, 0x02, 0xf5, 0x03, 0xf6, 0x04, 0xf7,
2392 0x05, 0xe0, 0x06, 0xf3, 0x07, 0xf8, 0x20, 0x61, 0x40,
2393 0xf8, 0xff, 0x0f, 0x0f};
2394
2395int32_t SimpleValueDecodeTests(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002396{
2397 QCBORDecodeContext DCtx;
Laurence Lundblade3888f002024-06-12 21:20:56 -07002398 QCBORItem Item;
2399 QCBORError uErr;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002400
Laurence Lundbladeee851742020-01-08 08:37:05 -08002401 QCBORDecode_Init(&DCtx,
2402 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSimpleValues),
2403 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002404
2405
Laurence Lundblade3888f002024-06-12 21:20:56 -07002406 if((uErr = QCBORDecode_GetNext(&DCtx, &Item)))
2407 return (int32_t)uErr;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002408 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
2409 Item.val.uCount != 10)
Laurence Lundblade3888f002024-06-12 21:20:56 -07002410 return 1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002411
Laurence Lundblade3888f002024-06-12 21:20:56 -07002412 if((uErr = QCBORDecode_GetNext(&DCtx, &Item)))
2413 return (int32_t)uErr;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002414 if(Item.uDataType != QCBOR_TYPE_FALSE)
Laurence Lundblade3888f002024-06-12 21:20:56 -07002415 return 2;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002416
Laurence Lundblade3888f002024-06-12 21:20:56 -07002417 if((uErr = QCBORDecode_GetNext(&DCtx, &Item)))
2418 return (int32_t)uErr;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002419 if(Item.uDataType != QCBOR_TYPE_TRUE)
Laurence Lundblade3888f002024-06-12 21:20:56 -07002420 return 3;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002421
Laurence Lundblade3888f002024-06-12 21:20:56 -07002422 if((uErr = QCBORDecode_GetNext(&DCtx, &Item)))
2423 return (int32_t)uErr;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002424 if(Item.uDataType != QCBOR_TYPE_NULL)
Laurence Lundblade3888f002024-06-12 21:20:56 -07002425 return 4;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002426
Laurence Lundblade3888f002024-06-12 21:20:56 -07002427 if((uErr = QCBORDecode_GetNext(&DCtx, &Item)))
2428 return (int32_t)uErr;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002429 if(Item.uDataType != QCBOR_TYPE_UNDEF)
Laurence Lundblade3888f002024-06-12 21:20:56 -07002430 return 5;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002431
2432 // A break
Laurence Lundblade9e3651c2018-10-10 11:49:55 +08002433 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_BREAK)
Laurence Lundblade3888f002024-06-12 21:20:56 -07002434 return 6;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002435
Laurence Lundblade3888f002024-06-12 21:20:56 -07002436 if((uErr = QCBORDecode_GetNext(&DCtx, &Item)))
2437 return (int32_t)uErr;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002438 if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 0)
Laurence Lundblade3888f002024-06-12 21:20:56 -07002439 return 7;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002440
Laurence Lundblade3888f002024-06-12 21:20:56 -07002441 if((uErr = QCBORDecode_GetNext(&DCtx, &Item)))
2442 return (int32_t)uErr;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002443 if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 19)
Laurence Lundblade3888f002024-06-12 21:20:56 -07002444 return 8;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002445
Laurence Lundblade077475f2019-04-26 09:06:33 -07002446 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_TYPE_7)
Laurence Lundblade3888f002024-06-12 21:20:56 -07002447 return 9;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002448
Laurence Lundblade077475f2019-04-26 09:06:33 -07002449 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_TYPE_7)
Laurence Lundblade3888f002024-06-12 21:20:56 -07002450 return 10;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002451
Laurence Lundblade077475f2019-04-26 09:06:33 -07002452 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_TYPE_7)
Laurence Lundblade3888f002024-06-12 21:20:56 -07002453 return 11;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002454
Laurence Lundblade3888f002024-06-12 21:20:56 -07002455 if((uErr = QCBORDecode_GetNext(&DCtx, &Item)))
2456 return (int32_t)uErr;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002457 if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 32)
Laurence Lundblade3888f002024-06-12 21:20:56 -07002458 return 12;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002459
Laurence Lundblade3888f002024-06-12 21:20:56 -07002460 if((uErr = QCBORDecode_GetNext(&DCtx, &Item)))
2461 return (int32_t)uErr;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002462 if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 255)
Laurence Lundblade3888f002024-06-12 21:20:56 -07002463 return 13;
2464
2465
2466 QCBORDecode_Init(&DCtx,
2467 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spGoodSimpleValues),
2468 QCBOR_DECODE_MODE_NORMAL);
2469
2470 uint8_t uSimple;
2471
2472 QCBORDecode_EnterMap(&DCtx, &Item);
2473 QCBORDecode_GetSimple(&DCtx, &uSimple);
2474 if(QCBORDecode_GetError(&DCtx) || uSimple != CBOR_SIMPLEV_FALSE) {
2475 return 20;
2476 }
2477 QCBORDecode_GetSimple(&DCtx, &uSimple);
2478 if(QCBORDecode_GetError(&DCtx) || uSimple != CBOR_SIMPLEV_TRUE) {
2479 return 21;
2480 }
2481 QCBORDecode_GetSimple(&DCtx, &uSimple);
2482 if(QCBORDecode_GetError(&DCtx) || uSimple != CBOR_SIMPLEV_NULL) {
2483 return 22;
2484 }
2485 QCBORDecode_GetSimple(&DCtx, &uSimple);
2486 if(QCBORDecode_GetError(&DCtx) || uSimple != CBOR_SIMPLEV_UNDEF) {
2487 return 23;
2488 }
2489 QCBORDecode_GetSimple(&DCtx, &uSimple);
2490 if(QCBORDecode_GetError(&DCtx) || uSimple != 0) {
2491 return 24;
2492 }
2493 QCBORDecode_GetSimple(&DCtx, &uSimple);
2494 if(QCBORDecode_GetError(&DCtx) || uSimple != 19) {
2495 return 25;
2496 }
2497 QCBORDecode_GetSimple(&DCtx, &uSimple);
2498 if(QCBORDecode_GetError(&DCtx) || uSimple != 32) {
2499 return 26;
2500 }
Laurence Lundbladecdbbc192024-06-28 15:13:04 -07002501#ifndef QCBOR_DISABLE_NON_INTEGER_LABELS
Laurence Lundblade3888f002024-06-12 21:20:56 -07002502 QCBORDecode_GetSimple(&DCtx, &uSimple);
2503 if(QCBORDecode_GetError(&DCtx) || uSimple != 255) {
2504 return 27;
2505 }
2506 QCBORDecode_VGetNext(&DCtx, &Item);
2507 QCBORDecode_GetSimple(&DCtx, &uSimple);
2508 if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_NO_MORE_ITEMS) {
2509 return 28;
2510 }
2511
2512 QCBORDecode_Rewind(&DCtx);
2513
2514 QCBORDecode_GetSimpleInMapN(&DCtx, 6, &uSimple);
2515 if(QCBORDecode_GetError(&DCtx) || uSimple != 19) {
2516 return 30;
2517 }
2518
2519 QCBORDecode_GetSimpleInMapSZ(&DCtx, "@", &uSimple);
2520 if(QCBORDecode_GetError(&DCtx) || uSimple != 255) {
2521 return 31;
2522 }
2523
2524 QCBORDecode_GetSimpleInMapN(&DCtx, 99, &uSimple);
2525 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_LABEL_NOT_FOUND) {
2526 return 32;
2527 }
2528
2529 QCBORDecode_GetSimpleInMapSZ(&DCtx, "xx", &uSimple);
2530 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_LABEL_NOT_FOUND) {
2531 return 33;
2532 }
2533
2534 QCBORDecode_GetSimpleInMapN(&DCtx, 15, &uSimple);
2535 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
2536 return 34;
2537 }
Laurence Lundbladecdbbc192024-06-28 15:13:04 -07002538#endif /* ! QCBOR_DISABLE_NON_INTEGER_LABELS */
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002539
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002540 return 0;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002541}
2542
2543
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03002544int32_t NotWellFormedTests(void)
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002545{
2546 // Loop over all the not-well-formed instance of CBOR
2547 // that are test vectors in not_well_formed_cbor.h
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08002548 const uint16_t nArraySize = C_ARRAY_COUNT(paNotWellFormedCBOR,
2549 struct someBinaryBytes);
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002550 for(uint16_t nIterate = 0; nIterate < nArraySize; nIterate++) {
2551 const struct someBinaryBytes *pBytes = &paNotWellFormedCBOR[nIterate];
2552 const UsefulBufC Input = (UsefulBufC){pBytes->p, pBytes->n};
2553
Laurence Lundbladeee851742020-01-08 08:37:05 -08002554 // Set up decoder context. String allocator needed for indefinite
2555 // string test cases
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002556 QCBORDecodeContext DCtx;
2557 QCBORDecode_Init(&DCtx, Input, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08002558#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002559 UsefulBuf_MAKE_STACK_UB(Pool, 100);
2560 QCBORDecode_SetMemPool(&DCtx, Pool, 0);
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08002561#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002562
2563 // Loop getting items until no more to get
Laurence Lundbladef71e1622020-08-06 18:52:13 -07002564 QCBORError uCBORError;
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002565 do {
2566 QCBORItem Item;
2567
Laurence Lundbladef71e1622020-08-06 18:52:13 -07002568 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
2569 } while(uCBORError == QCBOR_SUCCESS);
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002570
2571 // Every test vector must fail with
2572 // a not-well-formed error. If not
2573 // this test fails.
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002574 if(!QCBORDecode_IsNotWellFormedError(uCBORError) &&
Laurence Lundbladef71e1622020-08-06 18:52:13 -07002575 uCBORError != QCBOR_ERR_NO_MORE_ITEMS) {
Laurence Lundblade4bf6e712020-12-10 11:53:21 -08002576 /* Return index of failure and QCBOR error in the result */
2577 return (int32_t)(nIterate * 100 + uCBORError);
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002578 }
2579 }
2580 return 0;
2581}
2582
2583
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002584struct DecodeFailTestInput {
2585 const char *szDescription; /* Description of the test */
2586 QCBORDecodeMode DecoderMode; /* The QCBOR Decoder Mode for test */
2587 UsefulBufC Input; /* Chunk of CBOR that cases error */
2588 QCBORError nError; /* The expected error */
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002589};
2590
Laurence Lundblade59289e52019-12-30 13:44:37 -08002591
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002592static int32_t
2593ProcessDecodeFailures(const struct DecodeFailTestInput *pFailInputs, const int nNumFails)
Laurence Lundblade59289e52019-12-30 13:44:37 -08002594{
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002595 int nIndex;
2596 QCBORDecodeContext DCtx;
2597 QCBORError uCBORError;
2598 QCBORItem Item;
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08002599
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002600 for(nIndex = 0; nIndex < nNumFails; nIndex++) {
2601 const struct DecodeFailTestInput *pF = &pFailInputs[nIndex];
2602
2603 QCBORDecode_Init(&DCtx, pF->Input, pF->DecoderMode);
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08002604
2605#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002606 /* Set up the decoding context including a memory pool so that
2607 * indefinite length items can be checked.
2608 */
Laurence Lundblade59289e52019-12-30 13:44:37 -08002609 UsefulBuf_MAKE_STACK_UB(Pool, 100);
Laurence Lundblade830fbf92020-05-31 17:22:33 -07002610
Laurence Lundblade4bf6e712020-12-10 11:53:21 -08002611 uCBORError = QCBORDecode_SetMemPool(&DCtx, Pool, 0);
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002612 if(uCBORError != QCBOR_SUCCESS) {
2613 return -1;
Laurence Lundblade59289e52019-12-30 13:44:37 -08002614 }
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08002615#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
2616
Laurence Lundbladecdbbc192024-06-28 15:13:04 -07002617 if(nIndex == 4) {
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002618 uCBORError = 9; /* For setting break points */
Laurence Lundblade37286c02022-09-03 10:05:02 -07002619 }
2620
Laurence Lundbladecdbbc192024-06-28 15:13:04 -07002621 /* Iterate until there is an error of some sort */
Laurence Lundblade59289e52019-12-30 13:44:37 -08002622 do {
Laurence Lundbladecdbbc192024-06-28 15:13:04 -07002623 /* Set to something non-zero, something other than QCBOR_TYPE_NONE */
Laurence Lundblade59289e52019-12-30 13:44:37 -08002624 memset(&Item, 0x33, sizeof(Item));
2625
Laurence Lundblade4bf6e712020-12-10 11:53:21 -08002626 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
2627 } while(uCBORError == QCBOR_SUCCESS);
2628
Laurence Lundbladecdbbc192024-06-28 15:13:04 -07002629 /* Must get the expected error or the this test fails.
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002630 * The data and label type must also be QCBOR_TYPE_NONE.
2631 */
Laurence Lundblade4bf6e712020-12-10 11:53:21 -08002632 if(uCBORError != pF->nError ||
Laurence Lundblade59289e52019-12-30 13:44:37 -08002633 Item.uDataType != QCBOR_TYPE_NONE ||
2634 Item.uLabelType != QCBOR_TYPE_NONE) {
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002635 return (int32_t)(nIndex * 1000 + (int)uCBORError);
Laurence Lundblade59289e52019-12-30 13:44:37 -08002636 }
2637 }
2638
2639 return 0;
2640}
2641
2642
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002643static const struct DecodeFailTestInput Failures[] = {
2644 /* Most of this is copied from not_well_formed.h. Here the error
2645 * code returned is also checked.
2646 */
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002647
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08002648#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002649 /* Indefinite length strings must be closed off */
2650 { "An indefinite length byte string not closed off",
2651 QCBOR_DECODE_MODE_NORMAL,
2652 {"0x5f\x41\x00", 3},
2653 QCBOR_ERR_HIT_END
2654 },
2655 { "An indefinite length text string not closed off",
2656 QCBOR_DECODE_MODE_NORMAL,
2657 {"\x7f\x61\x00", 3},
2658 QCBOR_ERR_HIT_END
2659 },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002660
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002661 /* All the chunks in an indefinite length string must be of the
2662 * type of indefinite length string
2663 */
2664 { "Indefinite length byte string with text string chunk",
2665 QCBOR_DECODE_MODE_NORMAL,
2666 {"\x5f\x61\x00\xff", 4},
2667 QCBOR_ERR_INDEFINITE_STRING_CHUNK
2668 },
2669 { "Indefinite length text string with a byte string chunk",
2670 QCBOR_DECODE_MODE_NORMAL,
2671 {"\x7f\x41\x00\xff", 4},
2672 QCBOR_ERR_INDEFINITE_STRING_CHUNK
2673 },
2674 { "Indefinite length byte string with a positive integer chunk",
2675 QCBOR_DECODE_MODE_NORMAL,
2676 {"\x5f\x00\xff", 3},
2677 QCBOR_ERR_INDEFINITE_STRING_CHUNK
2678 },
2679 { "Indefinite length byte string with an negative integer chunk",
2680 QCBOR_DECODE_MODE_NORMAL,
2681 {"\x5f\x21\xff", 3},
2682 QCBOR_ERR_INDEFINITE_STRING_CHUNK
2683 },
2684 { "Indefinite length byte string with an array chunk",
2685 QCBOR_DECODE_MODE_NORMAL,
2686 {"\x5f\x80\xff", 3},
2687 QCBOR_ERR_INDEFINITE_STRING_CHUNK
2688 },
2689 { "Indefinite length byte string with an map chunk",
2690 QCBOR_DECODE_MODE_NORMAL,
2691 {"\x5f\xa0\xff", 3},
2692 QCBOR_ERR_INDEFINITE_STRING_CHUNK
2693 },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002694
Laurence Lundblade37286c02022-09-03 10:05:02 -07002695#ifndef QCBOR_DISABLE_TAGS
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002696 { "Indefinite length byte string with tagged integer chunk",
2697 QCBOR_DECODE_MODE_NORMAL,
2698 {"\x5f\xc0\x00\xff", 4},
2699 QCBOR_ERR_INDEFINITE_STRING_CHUNK
2700 },
Laurence Lundblade37286c02022-09-03 10:05:02 -07002701#else
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002702 { "Indefinite length byte string with tagged integer chunk",
2703 QCBOR_DECODE_MODE_NORMAL,
2704 {"\x5f\xc0\x00\xff", 4},
2705 QCBOR_ERR_TAGS_DISABLED
2706 },
Laurence Lundblade37286c02022-09-03 10:05:02 -07002707#endif /* QCBOR_DISABLE_TAGS */
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002708
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002709 { "Indefinite length byte string with an simple type chunk",
2710 QCBOR_DECODE_MODE_NORMAL,
2711 {"\x5f\xe0\xff", 3},
2712 QCBOR_ERR_INDEFINITE_STRING_CHUNK
2713 },
2714 { "???",
2715 QCBOR_DECODE_MODE_NORMAL,
2716 {"\x5f\x5f\x41\x00\xff\xff", 6},
2717 QCBOR_ERR_INDEFINITE_STRING_CHUNK
2718 },
2719 { "indefinite length text string with indefinite string inside",
2720 QCBOR_DECODE_MODE_NORMAL,
2721 {"\x7f\x7f\x61\x00\xff\xff", 6},
2722 QCBOR_ERR_INDEFINITE_STRING_CHUNK
2723 },
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08002724#else /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
2725
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08002726#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
2727
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002728 /* Definte length maps and arrays must be closed by having the right number of items */
2729 { "A definte length array that is supposed to have 1 item, but has none",
2730 QCBOR_DECODE_MODE_NORMAL,
2731 {"\x81", 1},
2732 QCBOR_ERR_NO_MORE_ITEMS
2733 },
2734 { "A definte length array that is supposed to have 2 items, but has only 1",
2735 QCBOR_DECODE_MODE_NORMAL,
2736 {"\x82\x00", 2},
2737 QCBOR_ERR_NO_MORE_ITEMS
2738 },
2739 { "A definte length array that is supposed to have 511 items, but has only 1",
2740 QCBOR_DECODE_MODE_NORMAL,
2741 {"\x9a\x01\xff\x00", 4},
2742 QCBOR_ERR_HIT_END
2743 },
2744 { "A definte length map that is supposed to have 1 item, but has none",
2745 QCBOR_DECODE_MODE_NORMAL,
2746 {"\xa1", 1},
2747 QCBOR_ERR_NO_MORE_ITEMS
2748 },
2749 { "A definte length map that is supposed to have s item, but has only 1",
2750 QCBOR_DECODE_MODE_NORMAL,
2751 {"\xa2\x01\x02", 3},
2752 QCBOR_ERR_NO_MORE_ITEMS
2753 },
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08002754#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002755 /* Indefinte length maps and arrays must be ended by a break */
2756 { "Indefinite length array with zero items and no break",
2757 QCBOR_DECODE_MODE_NORMAL,
2758 {"\x9f", 1},
2759 QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002760
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002761 { "Indefinite length array with two items and no break",
2762 QCBOR_DECODE_MODE_NORMAL,
2763 {"\x9\x01\x02", 3},
2764 QCBOR_ERR_NO_MORE_ITEMS
2765 },
2766 { "Indefinite length map with zero items and no break",
2767 QCBOR_DECODE_MODE_NORMAL,
2768 {"\xbf", 1},
2769 QCBOR_ERR_NO_MORE_ITEMS
2770 },
2771 { "Indefinite length map with two items and no break",
2772 QCBOR_DECODE_MODE_NORMAL,
2773 {"\xbf\x01\x02\x01\x02", 5},
2774 QCBOR_ERR_NO_MORE_ITEMS
2775 },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002776
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002777 /* Nested maps and arrays must be closed off (some extra nested test vectors) */
2778 { "Unclosed indefinite array containing a closed definite length array",
2779 QCBOR_DECODE_MODE_NORMAL,
2780 {"\x9f\x80\x00", 3},
2781 QCBOR_ERR_NO_MORE_ITEMS
2782 },
2783
2784 { "Definite length array containing an unclosed indefinite length array",
2785 QCBOR_DECODE_MODE_NORMAL,
2786 {"\x81\x9f", 2},
2787 QCBOR_ERR_NO_MORE_ITEMS
2788 },
2789 { "Unclosed indefinite map containing a closed definite length array",
2790 QCBOR_DECODE_MODE_NORMAL,
2791 {"\xbf\x01\x80\x00\xa0", 5},
2792 QCBOR_ERR_NO_MORE_ITEMS
2793 },
2794 { "Definite length map containing an unclosed indefinite length array",
2795 QCBOR_DECODE_MODE_NORMAL,
2796 {"\xa1\x02\x9f", 3},
2797 QCBOR_ERR_NO_MORE_ITEMS
2798 },
2799 { "Deeply nested definite length arrays with deepest one unclosed",
2800 QCBOR_DECODE_MODE_NORMAL,
2801 {"\x81\x81\x81\x81\x81\x81\x81\x81\x81", 9},
2802 QCBOR_ERR_NO_MORE_ITEMS
2803 },
2804 { "Deeply nested indefinite length arrays with deepest one unclosed",
2805 QCBOR_DECODE_MODE_NORMAL,
2806 {"\x9f\x9f\x9f\x9f\x9f\xff\xff\xff\xff", 9},
2807 QCBOR_ERR_NO_MORE_ITEMS
2808 },
2809 { "Mixed nesting with indefinite unclosed",
2810 QCBOR_DECODE_MODE_NORMAL,
2811 {"\x9f\x81\x9f\x81\x9f\x9f\xff\xff\xff", 9},
2812 QCBOR_ERR_NO_MORE_ITEMS },
2813 { "Mixed nesting with definite unclosed",
2814 QCBOR_DECODE_MODE_NORMAL,
2815 {"\x9f\x82\x9f\x81\x9f\x9f\xff\xff\xff\xff", 10},
2816 QCBOR_ERR_BAD_BREAK
2817 },
2818 { "Unclosed indefinite length map in definite length maps",
2819 QCBOR_DECODE_MODE_NORMAL,
2820 {"\xa1\x01\xa2\x02\xbf\xff\x02\xbf", 8},
2821 QCBOR_ERR_NO_MORE_ITEMS
2822 },
2823 { "Unclosed definite length map in indefinite length maps",
2824 QCBOR_DECODE_MODE_NORMAL,
2825 {"\xbf\x01\xbf\x02\xa1", 5},
2826 QCBOR_ERR_NO_MORE_ITEMS
2827 },
2828 { "Unclosed indefinite length array in definite length maps",
2829 QCBOR_DECODE_MODE_NORMAL,
2830 {"\xa1\x01\xa2\x02\x9f\xff\x02\x9f", 8},
2831 QCBOR_ERR_NO_MORE_ITEMS
2832 },
2833 { "Unclosed definite length array in indefinite length maps",
2834 QCBOR_DECODE_MODE_NORMAL,
2835 {"\xbf\x01\xbf\x02\x81", 5},
2836 QCBOR_ERR_NO_MORE_ITEMS
2837 },
2838 { "Unclosed indefinite length map in definite length arrays",
2839 QCBOR_DECODE_MODE_NORMAL,
2840 {"\x81\x82\xbf\xff\xbf", 5},
2841 QCBOR_ERR_NO_MORE_ITEMS
2842 },
2843 { "Unclosed definite length map in indefinite length arrays",
2844 QCBOR_DECODE_MODE_NORMAL,
2845 {"\x9f\x9f\xa1", 3},
2846 QCBOR_ERR_NO_MORE_ITEMS
2847 },
2848
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08002849#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002850
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002851 /* The "argument" for the data item is incomplete */
2852 { "Positive integer missing 1 byte argument",
2853 QCBOR_DECODE_MODE_NORMAL,
2854 {"\x18", 1},
2855 QCBOR_ERR_HIT_END
2856 },
2857 { "Positive integer missing 2 byte argument",
2858 QCBOR_DECODE_MODE_NORMAL,
2859 {"\x19", 1},
2860 QCBOR_ERR_HIT_END
2861 },
2862 { "Positive integer missing 4 byte argument",
2863 QCBOR_DECODE_MODE_NORMAL,
2864 {"\x1a", 1},
2865 QCBOR_ERR_HIT_END
2866 },
2867 { "Positive integer missing 8 byte argument",
2868 QCBOR_DECODE_MODE_NORMAL,
2869 {"\x1b", 1},
2870 QCBOR_ERR_HIT_END
2871 },
2872 { "Positive integer missing 1 byte of 2 byte argument",
2873 QCBOR_DECODE_MODE_NORMAL,
2874 {"\x19\x01", 2},
2875 QCBOR_ERR_HIT_END
2876 },
2877 { "Positive integer missing 2 bytes of 4 byte argument",
2878 QCBOR_DECODE_MODE_NORMAL,
2879 {"\x1a\x01\x02", 3},
2880 QCBOR_ERR_HIT_END
2881 },
2882 { "Positive integer missing 1 bytes of 7 byte argument",
2883 QCBOR_DECODE_MODE_NORMAL,
2884 {"\x1b\x01\x02\x03\x04\x05\x06\x07", 8},
2885 QCBOR_ERR_HIT_END
2886 },
2887 { "Negative integer missing 1 byte argument",
2888 QCBOR_DECODE_MODE_NORMAL,
2889 {"\x38", 1},
2890 QCBOR_ERR_HIT_END
2891 },
2892 { "Binary string missing 1 byte argument",
2893 QCBOR_DECODE_MODE_NORMAL,
2894 {"\x58", 1},
2895 QCBOR_ERR_HIT_END
2896 },
2897 { "Text string missing 1 byte argument",
2898 QCBOR_DECODE_MODE_NORMAL,
2899 {"\x78", 1},
2900 QCBOR_ERR_HIT_END
2901 },
2902 { "Array missing 1 byte argument",
2903 QCBOR_DECODE_MODE_NORMAL,
2904 {"\x98", 1},
2905 QCBOR_ERR_HIT_END
2906 },
2907 { "Map missing 1 byte argument",
2908 QCBOR_DECODE_MODE_NORMAL,
2909 {"\xb8", 1},
2910 QCBOR_ERR_HIT_END
2911 },
2912 { "Tag missing 1 byte argument",
2913 QCBOR_DECODE_MODE_NORMAL,
2914 {"\xd8", 1},
2915 QCBOR_ERR_HIT_END
2916 },
2917 { "Simple missing 1 byte argument",
2918 QCBOR_DECODE_MODE_NORMAL,
2919 {"\xf8", 1},
2920 QCBOR_ERR_HIT_END
2921 },
2922 { "half-precision with 1 byte argument",
2923 QCBOR_DECODE_MODE_NORMAL,
2924 {"\xf9\x00", 2},
2925 QCBOR_ERR_HIT_END
2926 },
2927 { "single-precision with 2 byte argument",
2928 QCBOR_DECODE_MODE_NORMAL,
2929 {"\0xfa\x00\x00", 3},
2930 QCBOR_ERR_HIT_END
2931 },
2932 { "double-precision with 3 byte argument",
2933 QCBOR_DECODE_MODE_NORMAL,
2934 {"\xfb\x00\x00\x00", 4},
2935 QCBOR_ERR_HIT_END
2936 },
Laurence Lundblade2f467f92020-10-09 17:50:11 -07002937
Laurence Lundblade37286c02022-09-03 10:05:02 -07002938#ifndef QCBOR_DISABLE_TAGS
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002939 { "Tag with no content",
2940 QCBOR_DECODE_MODE_NORMAL,
2941 {"\xc0", 1},
2942 QCBOR_ERR_HIT_END
2943 },
Laurence Lundblade37286c02022-09-03 10:05:02 -07002944#else /* QCBOR_DISABLE_TAGS */
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002945 { "Tag with no content",
2946 QCBOR_DECODE_MODE_NORMAL,
2947 {"\xc0", 1},
2948 QCBOR_ERR_TAGS_DISABLED
2949 },
Laurence Lundblade37286c02022-09-03 10:05:02 -07002950#endif /* QCBOR_DISABLE_TAGS */
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002951
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002952 /* Breaks must not occur in definite length arrays and maps */
2953 { "Array of length 1 with sole member replaced by a break",
2954 QCBOR_DECODE_MODE_NORMAL,
2955 {"\x81\xff", 2},
2956 QCBOR_ERR_BAD_BREAK
2957 },
2958 { "Array of length 2 with 2nd member replaced by a break",
2959 QCBOR_DECODE_MODE_NORMAL,
2960 {"\x82\x00\xff", 3},
2961 QCBOR_ERR_BAD_BREAK
2962 },
2963 { "Map of length 1 with sole member label replaced by a break",
2964 QCBOR_DECODE_MODE_NORMAL,
2965 {"\xa1\xff", 2},
2966 QCBOR_ERR_BAD_BREAK
2967 },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002968
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002969 /* Map of length 1 with sole member label replaced by break */
2970 { "Alternate representation that some decoders handle differently",
2971 QCBOR_DECODE_MODE_NORMAL,
2972 {"\xa1\xff\x00", 3},
2973 QCBOR_ERR_BAD_BREAK
2974 },
2975 { "Array of length 1 with 2nd member value replaced by a break",
2976 QCBOR_DECODE_MODE_NORMAL,
2977 {"\xa1\x00\xff", 3},
2978 QCBOR_ERR_BAD_BREAK
2979 },
Laurence Lundbladeec290b82024-06-10 11:10:54 -07002980 { "Map of length 2 with 2nd entry label replaced by a break",
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002981 QCBOR_DECODE_MODE_NORMAL,
Laurence Lundbladeec290b82024-06-10 11:10:54 -07002982 {"\xa2\x00\x00\xff\x00", 5},
2983 QCBOR_ERR_BAD_BREAK
2984 },
2985 { "Map of length 2 with 2nd entry value replaced by a break",
2986 QCBOR_DECODE_MODE_NORMAL,
2987 {"\xa2\x00\x00\x01\xff", 5},
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002988 QCBOR_ERR_BAD_BREAK
2989 },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002990
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002991 /* Breaks must not occur on their own out of an indefinite length data item */
2992 { "A bare break is not well formed",
2993 QCBOR_DECODE_MODE_NORMAL,
2994 {"\xff", 1},
2995 QCBOR_ERR_BAD_BREAK
2996 },
2997 { "A bare break after a zero length definite length array",
2998 QCBOR_DECODE_MODE_NORMAL,
2999 {"\x80\xff", 2},
3000 QCBOR_ERR_BAD_BREAK
3001 },
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08003002#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08003003 { "A bare break after a zero length indefinite length map",
3004 QCBOR_DECODE_MODE_NORMAL,
3005 {"\x9f\xff\xff", 3},
3006 QCBOR_ERR_BAD_BREAK
3007 },
3008 { "A break inside a definite length array inside an indefenite length array",
3009 QCBOR_DECODE_MODE_NORMAL,
3010 {"\x9f\x81\xff", 3},
3011 QCBOR_ERR_BAD_BREAK
3012 },
3013 { "Complicated mixed nesting with break outside indefinite length array",
3014 QCBOR_DECODE_MODE_NORMAL,
3015 {"\x9f\x82\x9f\x81\x9f\x9f\xff\xff\xff\xff", 10},
3016 QCBOR_ERR_BAD_BREAK },
3017#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
3018
3019 /* Forbidden two byte encodings of simple types */
3020 { "Must use 0xe0 instead",
3021 QCBOR_DECODE_MODE_NORMAL,
3022 {"\xf8\x00", 2},
3023 QCBOR_ERR_BAD_TYPE_7
3024 },
3025 { "Should use 0xe1 instead",
3026 QCBOR_DECODE_MODE_NORMAL,
3027 {"\xf8\x01", 2},
3028 QCBOR_ERR_BAD_TYPE_7
3029 },
3030 { "Should use 0xe2 instead",
3031 QCBOR_DECODE_MODE_NORMAL,
3032 {"\xf8\x02", 2},
3033 QCBOR_ERR_BAD_TYPE_7
3034 }, { "Should use 0xe3 instead",
3035 QCBOR_DECODE_MODE_NORMAL,
3036 {"\xf8\x03", 2},
3037 QCBOR_ERR_BAD_TYPE_7
3038 },
3039 { "Should use 0xe4 instead",
3040 QCBOR_DECODE_MODE_NORMAL,
3041 {"\xf8\x04", 2},
3042 QCBOR_ERR_BAD_TYPE_7
3043 },
3044 { "Should use 0xe5 instead",
3045 QCBOR_DECODE_MODE_NORMAL,
3046 {"\xf8\x05", 2},
3047 QCBOR_ERR_BAD_TYPE_7
3048 },
3049 { "Should use 0xe6 instead",
3050 QCBOR_DECODE_MODE_NORMAL,
3051 {"\xf8\x06", 2},
3052 QCBOR_ERR_BAD_TYPE_7
3053 },
3054 { "Should use 0xe7 instead",
3055 QCBOR_DECODE_MODE_NORMAL,
3056 {"\xf8\x07", 2},
3057 QCBOR_ERR_BAD_TYPE_7
3058 },
3059 { "Should use 0xe8 instead",
3060 QCBOR_DECODE_MODE_NORMAL,
3061 {"\xf8\x08", 2},
3062 QCBOR_ERR_BAD_TYPE_7
3063 },
3064 { "Should use 0xe9 instead",
3065 QCBOR_DECODE_MODE_NORMAL,
3066 {"\xf8\x09", 2},
3067 QCBOR_ERR_BAD_TYPE_7
3068 },
3069 { "Should use 0xea instead",
3070 QCBOR_DECODE_MODE_NORMAL,
3071 {"\xf8\x0a", 2},
3072 QCBOR_ERR_BAD_TYPE_7
3073 },
3074 { "Should use 0xeb instead",
3075 QCBOR_DECODE_MODE_NORMAL,
3076 {"\xf8\x0b", 2},
3077 QCBOR_ERR_BAD_TYPE_7
3078 },
3079 { "Should use 0xec instead",
3080 QCBOR_DECODE_MODE_NORMAL,
3081 {"\xf8\x0c", 2},
3082 QCBOR_ERR_BAD_TYPE_7
3083 },
3084 { "Should use 0xed instead",
3085 QCBOR_DECODE_MODE_NORMAL,
3086 {"\xf8\x0d", 2},
3087 QCBOR_ERR_BAD_TYPE_7
3088 },
3089 { "Should use 0xee instead",
3090 QCBOR_DECODE_MODE_NORMAL,
3091 {"\xf8\x0e", 2},
3092 QCBOR_ERR_BAD_TYPE_7
3093 },
3094 { "Should use 0xef instead",
3095 QCBOR_DECODE_MODE_NORMAL,
3096 {"\xf8\x0f", 2},
3097 QCBOR_ERR_BAD_TYPE_7
3098 },
3099 { "Should use 0xf0 instead",
3100 QCBOR_DECODE_MODE_NORMAL,
3101 {"\xf8\x10", 2},
3102 QCBOR_ERR_BAD_TYPE_7
3103 },
3104 { "Should use 0xf1 instead",
3105 QCBOR_DECODE_MODE_NORMAL,
3106 {"\xf8\x11", 2},
3107 QCBOR_ERR_BAD_TYPE_7
3108 },
3109 { "Should use 0xf2 instead",
3110 QCBOR_DECODE_MODE_NORMAL,
3111 {"\xf8\x12", 2},
3112 QCBOR_ERR_BAD_TYPE_7
3113 },
3114 { "Should use 0xf3 instead",
3115 QCBOR_DECODE_MODE_NORMAL,
3116 {"\xf8\x13", 2},
3117 QCBOR_ERR_BAD_TYPE_7
3118 },
3119 { "Should use 0xf4 instead",
3120 QCBOR_DECODE_MODE_NORMAL,
3121 {"\xf8\x14", 2},
3122 QCBOR_ERR_BAD_TYPE_7
3123 },
3124 { "Should use 0xf5 instead",
3125 QCBOR_DECODE_MODE_NORMAL,
3126 {"\xf8\x15", 2},
3127 QCBOR_ERR_BAD_TYPE_7
3128 },
3129 { "Should use 0xf6 instead",
3130 QCBOR_DECODE_MODE_NORMAL,
3131 {"\xf8\x16", 2},
3132 QCBOR_ERR_BAD_TYPE_7
3133 },
3134 { "Should use 0xef7 instead",
3135 QCBOR_DECODE_MODE_NORMAL,
3136 {"\xf8\x17", 2},
3137 QCBOR_ERR_BAD_TYPE_7
3138 },
3139 { "Should use 0xef8 instead",
3140 QCBOR_DECODE_MODE_NORMAL,
3141 {"\xf8\x18", 2},
3142 QCBOR_ERR_BAD_TYPE_7
3143 },
3144 { "Reserved",
3145 QCBOR_DECODE_MODE_NORMAL,
3146 {"\xf8\x18", 2},
3147 QCBOR_ERR_BAD_TYPE_7
3148 },
3149
3150 /* Maps must have an even number of data items (key & value) */
3151 { "Map with 1 item when it should have 2",
3152 QCBOR_DECODE_MODE_NORMAL,
3153 {"\xa1\x00", 2},
3154 QCBOR_ERR_HIT_END
3155 },
3156 { "Map with 3 item when it should have 4",
3157 QCBOR_DECODE_MODE_NORMAL,
3158 {"\xa2\x00\x00\x00", 2},
3159 QCBOR_ERR_HIT_END
3160 },
3161#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
3162 { "Map with 1 item when it should have 2",
3163 QCBOR_DECODE_MODE_NORMAL,
3164 {"\xbf\x00\xff", 3},
3165 QCBOR_ERR_BAD_BREAK
3166 },
3167 { "Map with 3 item when it should have 4",
3168 QCBOR_DECODE_MODE_NORMAL,
3169 {"\xbf\x00\x00\x00\xff", 5},
3170 QCBOR_ERR_BAD_BREAK
3171 },
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08003172#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07003173
3174
Laurence Lundblade37286c02022-09-03 10:05:02 -07003175#ifndef QCBOR_DISABLE_TAGS
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08003176 /* In addition to not-well-formed, some invalid CBOR */
3177 { "Text-based date, with an integer",
3178 QCBOR_DECODE_MODE_NORMAL,
3179 {"\xc0\x00", 2},
3180 QCBOR_ERR_BAD_OPT_TAG
3181 },
3182 { "Epoch date, with an byte string",
3183 QCBOR_DECODE_MODE_NORMAL,
3184 {"\xc1\x41\x33", 3},
3185 QCBOR_ERR_BAD_OPT_TAG
3186 },
3187 { "tagged as both epoch and string dates",
3188 QCBOR_DECODE_MODE_NORMAL,
3189 {"\xc1\xc0\x00", 3},
3190 QCBOR_ERR_BAD_OPT_TAG
3191 },
3192 { "big num tagged an int, not a byte string",
3193 QCBOR_DECODE_MODE_NORMAL,
3194 {"\xc2\x00", 2},
3195 QCBOR_ERR_BAD_OPT_TAG
3196 },
Laurence Lundblade37286c02022-09-03 10:05:02 -07003197#else /* QCBOR_DISABLE_TAGS */
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08003198 /* In addition to not-well-formed, some invalid CBOR */
3199 { "Text-based date, with an integer",
3200 QCBOR_DECODE_MODE_NORMAL,
3201 {"\xc0\x00", 2},
3202 QCBOR_ERR_TAGS_DISABLED
3203 },
3204 { "Epoch date, with an byte string",
3205 QCBOR_DECODE_MODE_NORMAL,
3206 {"\xc1\x41\x33", 3},
3207 QCBOR_ERR_TAGS_DISABLED
3208 },
3209 { "tagged as both epoch and string dates",
3210 QCBOR_DECODE_MODE_NORMAL,
3211 {"\xc1\xc0\x00", 3},
3212 QCBOR_ERR_TAGS_DISABLED
3213 },
3214 { "big num tagged an int, not a byte string",
3215 QCBOR_DECODE_MODE_NORMAL,
3216 {"\xc2\x00", 2},
3217 QCBOR_ERR_TAGS_DISABLED
3218 },
Laurence Lundblade37286c02022-09-03 10:05:02 -07003219#endif /* QCBOR_DISABLE_TAGS */
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003220};
3221
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08003222
3223
3224int32_t
3225DecodeFailureTests(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003226{
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003227 int32_t nResult;
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07003228
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08003229 nResult = ProcessDecodeFailures(Failures ,C_ARRAY_COUNT(Failures, struct DecodeFailTestInput));
Laurence Lundblade59289e52019-12-30 13:44:37 -08003230 if(nResult) {
3231 return nResult;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003232 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003233
Laurence Lundblade3a6042e2019-06-28 19:58:04 -07003234 // Corrupt the UsefulInputBuf and see that
3235 // it reflected correctly for CBOR decoding
Laurence Lundbladee6f15112020-07-23 18:44:16 -07003236 QCBORDecodeContext DCtx;
3237 QCBORItem Item;
3238 QCBORError uQCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003239
Laurence Lundbladee6f15112020-07-23 18:44:16 -07003240 QCBORDecode_Init(&DCtx,
3241 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSimpleValues),
3242 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003243
Laurence Lundbladee6f15112020-07-23 18:44:16 -07003244 if((uQCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
3245 return (int32_t)uQCBORError;
3246 }
3247 if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.val.uCount != 10) {
3248 // This wasn't supposed to happen
3249 return -1;
3250 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003251
Laurence Lundbladee6f15112020-07-23 18:44:16 -07003252 DCtx.InBuf.magic = 0; // Reach in and corrupt the UsefulInputBuf
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003253
Laurence Lundbladee6f15112020-07-23 18:44:16 -07003254 uQCBORError = QCBORDecode_GetNext(&DCtx, &Item);
3255 if(uQCBORError != QCBOR_ERR_NO_MORE_ITEMS) {
3256 // Did not get back the error expected
3257 return -2;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003258 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003259
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003260
Laurence Lundblade98427e92020-09-28 21:33:23 -07003261 /*
3262 The max size of a string for QCBOR is SIZE_MAX - 4 so this
3263 tests here can be performed to see that the max length
3264 error check works correctly. See DecodeBytes(). If the max
3265 size was SIZE_MAX, it wouldn't be possible to test this.
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003266
Laurence Lundblade98427e92020-09-28 21:33:23 -07003267 This test will automatocally adapt the all CPU sizes
3268 through the use of SIZE_MAX.
3269 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003270
Laurence Lundblade8510f8c2020-12-01 11:31:16 -08003271 UsefulBuf_MAKE_STACK_UB( HeadBuf, QCBOR_HEAD_BUFFER_SIZE);
Laurence Lundblade98427e92020-09-28 21:33:23 -07003272 UsefulBufC EncodedHead;
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003273
Laurence Lundblade98427e92020-09-28 21:33:23 -07003274 // This makes a CBOR head with a text string that is very long
3275 // but doesn't fill in the bytes of the text string as that is
3276 // not needed to test this part of QCBOR.
3277 EncodedHead = QCBOREncode_EncodeHead(HeadBuf, CBOR_MAJOR_TYPE_TEXT_STRING, 0, SIZE_MAX);
3278
3279 QCBORDecode_Init(&DCtx, EncodedHead, QCBOR_DECODE_MODE_NORMAL);
3280
3281 if(QCBOR_ERR_STRING_TOO_LONG != QCBORDecode_GetNext(&DCtx, &Item)) {
3282 return -4;
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003283 }
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003284
Laurence Lundblade3a6042e2019-06-28 19:58:04 -07003285 return 0;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003286}
3287
3288
Laurence Lundbladea2e29072018-12-30 09:20:06 -08003289/* Try all 256 values of the byte at nLen including recursing for
3290 each of the values to try values at nLen+1 ... up to nLenMax
3291 */
Laurence Lundblade06350ea2020-01-27 19:32:40 -08003292static void ComprehensiveInputRecurser(uint8_t *pBuf, size_t nLen, size_t nLenMax)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003293{
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003294 if(nLen >= nLenMax) {
3295 return;
3296 }
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08003297
Laurence Lundbladea2e29072018-12-30 09:20:06 -08003298 for(int inputByte = 0; inputByte < 256; inputByte++) {
3299 // Set up the input
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003300 pBuf[nLen] = (uint8_t)inputByte;
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08003301 const UsefulBufC Input = {pBuf, nLen+1};
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08003302
Laurence Lundbladea2e29072018-12-30 09:20:06 -08003303 // Get ready to parse
3304 QCBORDecodeContext DCtx;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003305 QCBORDecode_Init(&DCtx, Input, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003306
Laurence Lundbladea2e29072018-12-30 09:20:06 -08003307 // Parse by getting the next item until an error occurs
3308 // Just about every possible decoder error can occur here
3309 // The goal of this test is not to check for the correct
3310 // error since that is not really possible. It is to
3311 // see that there is no crash on hostile input.
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003312 while(1) {
Laurence Lundbladea2e29072018-12-30 09:20:06 -08003313 QCBORItem Item;
3314 QCBORError nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003315 if(nCBORError != QCBOR_SUCCESS) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003316 break;
3317 }
3318 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003319
Laurence Lundbladea2e29072018-12-30 09:20:06 -08003320 ComprehensiveInputRecurser(pBuf, nLen+1, nLenMax);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003321 }
3322}
3323
3324
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03003325int32_t ComprehensiveInputTest(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003326{
Laurence Lundbladea2e29072018-12-30 09:20:06 -08003327 // Size 2 tests 64K inputs and runs quickly
3328 uint8_t pBuf[2];
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003329
Laurence Lundbladea2e29072018-12-30 09:20:06 -08003330 ComprehensiveInputRecurser(pBuf, 0, sizeof(pBuf));
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003331
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003332 return 0;
3333}
3334
Laurence Lundbladea2e29072018-12-30 09:20:06 -08003335
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03003336int32_t BigComprehensiveInputTest(void)
Laurence Lundbladea2e29072018-12-30 09:20:06 -08003337{
3338 // size 3 tests 16 million inputs and runs OK
3339 // in seconds on fast machines. Size 4 takes
3340 // 10+ minutes and 5 half a day on fast
3341 // machines. This test is kept separate from
3342 // the others so as to no slow down the use
3343 // of them as a very frequent regression.
3344 uint8_t pBuf[3]; //
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08003345
Laurence Lundbladea2e29072018-12-30 09:20:06 -08003346 ComprehensiveInputRecurser(pBuf, 0, sizeof(pBuf));
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08003347
Laurence Lundbladea2e29072018-12-30 09:20:06 -08003348 return 0;
3349}
3350
3351
Laurence Lundbladecc7da412020-12-27 00:09:07 -08003352static const uint8_t spDateTestInput[] = {
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003353 /* 1. The valid date string "1985-04-12" */
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003354 0xc0, // tag for string date
3355 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003356
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003357 /* 2. An invalid date string due to wrong tag content type */
Laurence Lundbladec7114722020-08-13 05:11:40 -07003358 0xc0, // tag for string date
3359 0x00, // Wrong type for a string date
3360
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003361 /* 3. A valid epoch date, 1400000000; Tue, 13 May 2014 16:53:20 GMT */
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003362 0xc1, // tag for epoch date
3363 0x1a, 0x53, 0x72, 0x4E, 0x00, // Epoch date 1400000000; Tue, 13 May 2014 16:53:20 GMT
3364
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003365 /* 4. An invalid epoch date due to wrong tag content type */
Laurence Lundbladec7114722020-08-13 05:11:40 -07003366 0xc1,
3367 0x62, 'h', 'i', // wrong type tagged
3368
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003369 /* 5. Valid epoch date tag as content for a two other nested tags */
Laurence Lundblade99615302020-11-29 11:19:47 -08003370 // CBOR_TAG_ENC_AS_B64
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003371 0xcf, 0xd8, 0x16, 0xc1, // Epoch date with extra tags
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003372 0x1a, 0x53, 0x72, 0x4E, 0x01,
3373
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003374 /* 6. Epoch date with value to large to fit into int64 */
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003375 0xc1, // tag for epoch date
3376 0x1b, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, // Too large integer
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003377
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003378 /* 7. Epoch date with single-precision value of 1.1. */
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003379 0xc1, // tag for epoch date
Laurence Lundblade3ed0bca2020-07-14 22:50:10 -07003380 0xfa, 0x3f, 0x8c, 0xcc, 0xcd, // single with value 1.1
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003381
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003382 /* 8. Epoch date with too-large single precision float */
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003383 0xc1, // tag for epoch date
Laurence Lundbladea1ad8782019-11-08 00:12:11 -08003384 0xfa, 0x7f, 0x7f, 0xff, 0xff, // 3.4028234663852886e+38 too large
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003385
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003386 /* 9. Epoch date with slightly too-large double precision value */
Laurence Lundbladea1ad8782019-11-08 00:12:11 -08003387 0xc1, // tag for epoch date
3388 0xfb, 0x43, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 9223372036854775808.000000 just barely too large
3389 //0xfa, 0x7f, 0x7f, 0xff, 0xff // 3.4028234663852886e+38 too large
3390
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003391 /* 10. Epoch date with largest supported double precision value */
Laurence Lundbladea1ad8782019-11-08 00:12:11 -08003392 0xc1, // tag for epoch date
Laurence Lundbladec7114722020-08-13 05:11:40 -07003393 0xfb, 0x43, 0xdf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, // 9223372036854773760 largest supported
3394
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003395 /* 11. Epoch date with single-precision NaN */
Laurence Lundbladec7114722020-08-13 05:11:40 -07003396 0xc1, // tag for epoch date
3397 0xfa, 0x7f, 0xc0, 0x00, 0x00, // Single-precision NaN
3398
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003399 /* 12. Epoch date with double precision plus infinity */
Laurence Lundbladec7114722020-08-13 05:11:40 -07003400 0xc1,
3401 0xfb, 0x7f, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // +infinity
3402
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003403 /* 13. Epoch date with half-precision negative infinity */
Laurence Lundbladec7114722020-08-13 05:11:40 -07003404 0xc1, // tag for epoch date
3405 0xf9, 0xfc, 0x00, // -Infinity
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003406};
3407
3408
Laurence Lundbladec7114722020-08-13 05:11:40 -07003409
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003410// have to check float expected only to within an epsilon
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07003411#ifndef QCBOR_DISABLE_FLOAT_HW_USE
Laurence Lundblade02fcf312020-07-17 02:49:46 -07003412static int CHECK_EXPECTED_DOUBLE(double val, double expected) {
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003413
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003414 double diff = val - expected;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003415
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003416 diff = fabs(diff);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003417
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003418 return diff > 0.0000001;
3419}
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07003420#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003421
3422
Laurence Lundblade37286c02022-09-03 10:05:02 -07003423/* Test date decoding using GetNext() */
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03003424int32_t DateParseTest(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003425{
3426 QCBORDecodeContext DCtx;
Laurence Lundbladec7114722020-08-13 05:11:40 -07003427 QCBORItem Item;
3428 QCBORError uError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003429
Laurence Lundbladeee851742020-01-08 08:37:05 -08003430 QCBORDecode_Init(&DCtx,
3431 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spDateTestInput),
3432 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003433
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003434 /* 1. The valid date string "1985-04-12" */
Laurence Lundbladec7114722020-08-13 05:11:40 -07003435 if((uError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003436 return -1;
Laurence Lundbladec7114722020-08-13 05:11:40 -07003437 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003438 if(Item.uDataType != QCBOR_TYPE_DATE_STRING ||
Laurence Lundbladeba587682024-02-07 16:46:43 -08003439 UsefulBufCompareToSZ(Item.val.string, "1985-04-12")){
Laurence Lundblade67bd5512018-11-02 21:44:06 +07003440 return -2;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003441 }
3442
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003443 /* 2. An invalid date string due to wrong tag content type */
Laurence Lundbladec7114722020-08-13 05:11:40 -07003444 uError = QCBORDecode_GetNext(&DCtx, &Item);
3445 if(uError != QCBOR_ERR_BAD_OPT_TAG) {
Laurence Lundblade67bd5512018-11-02 21:44:06 +07003446 return -3;
Laurence Lundbladec7114722020-08-13 05:11:40 -07003447 }
3448
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003449 /* 3. A valid epoch date, 1400000000; Tue, 13 May 2014 16:53:20 GMT */
3450 uError = QCBORDecode_GetNext(&DCtx, &Item);
3451 if(uError != QCBOR_SUCCESS) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07003452 return -4;
3453 }
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003454 if(uError == QCBOR_SUCCESS) {
3455 if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH ||
3456 Item.val.epochDate.nSeconds != 1400000000
Máté Tóth-Pálef5f07a2021-09-17 19:31:37 +02003457#ifndef USEFULBUF_DISABLE_ALL_FLOAT
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003458 || Item.val.epochDate.fSecondsFraction != 0
Máté Tóth-Pálef5f07a2021-09-17 19:31:37 +02003459#endif /* USEFULBUF_DISABLE_ALL_FLOAT */
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003460 ) {
3461 return -5;
3462 }
Laurence Lundbladec7114722020-08-13 05:11:40 -07003463 }
3464
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003465 /* 4. An invalid epoch date due to wrong tag content type */
Laurence Lundbladec7114722020-08-13 05:11:40 -07003466 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_OPT_TAG) {
3467 return -6;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003468 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003469
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003470 /* 5. Valid epoch date tag as content for a two other nested tags */
Laurence Lundblade99615302020-11-29 11:19:47 -08003471 // Epoch date wrapped in an CBOR_TAG_ENC_AS_B64 and an unknown tag.
3472 // The date is decoded and the two tags are returned. This is to
3473 // make sure the wrapping of epoch date in another tag works OK.
Laurence Lundbladec7114722020-08-13 05:11:40 -07003474 if((uError = QCBORDecode_GetNext(&DCtx, &Item))) {
3475 return -7;
3476 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003477 if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH ||
3478 Item.val.epochDate.nSeconds != 1400000001 ||
Máté Tóth-Pálef5f07a2021-09-17 19:31:37 +02003479#ifndef USEFULBUF_DISABLE_ALL_FLOAT
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003480 Item.val.epochDate.fSecondsFraction != 0 ||
Máté Tóth-Pálef5f07a2021-09-17 19:31:37 +02003481#endif /* USEFULBUF_DISABLE_ALL_FLOAT */
Laurence Lundblade99615302020-11-29 11:19:47 -08003482 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_ENC_AS_B64)) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07003483 return -8;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003484 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003485
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003486 /* 6. Epoch date with value to large to fit into int64 */
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003487 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_DATE_OVERFLOW) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07003488 return -9;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003489 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003490
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003491 /* 7. Epoch date with single-precision value of 1.1. */
3492 uError = QCBORDecode_GetNext(&DCtx, &Item);
3493 if(uError != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07003494 return -10;
3495 }
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003496 if(uError == QCBOR_SUCCESS) {
3497 if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH ||
3498 Item.val.epochDate.nSeconds != 1
3499#ifndef QCBOR_DISABLE_FLOAT_HW_USE
3500 || CHECK_EXPECTED_DOUBLE(Item.val.epochDate.fSecondsFraction, 0.1)
Máté Tóth-Pálef5f07a2021-09-17 19:31:37 +02003501#endif /* USEFULBUF_DISABLE_ALL_FLOAT */
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003502 ) {
3503 return -11;
3504 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003505 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003506
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003507 /* 8. Epoch date with too-large single-precision float */
3508 uError = QCBORDecode_GetNext(&DCtx, &Item);
3509 if(uError != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_DATE_OVERFLOW)) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07003510 return -12;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003511 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003512
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003513 /* 9. Epoch date with slightly too-large double-precision value */
3514 uError = QCBORDecode_GetNext(&DCtx, &Item);
3515 if(uError != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_DATE_OVERFLOW)) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07003516 return -13;
Laurence Lundbladea1ad8782019-11-08 00:12:11 -08003517 }
3518
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003519 /* 10. Epoch date with largest supported double-precision value */
3520 uError = QCBORDecode_GetNext(&DCtx, &Item);
3521 if(uError != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)) {
3522 return -14;
3523 }
3524 if(uError == QCBOR_SUCCESS) {
3525 if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH ||
3526 Item.val.epochDate.nSeconds != 9223372036854773760
3527#ifndef QCBOR_DISABLE_FLOAT_HW_USE
3528 || Item.val.epochDate.fSecondsFraction != 0.0
3529#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
3530 ) {
3531 return -14;
3532 }
3533 }
Laurence Lundbladec7114722020-08-13 05:11:40 -07003534
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003535 /* 11. Epoch date with single-precision NaN */
3536 if(QCBORDecode_GetNext(&DCtx, &Item) != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_DATE_OVERFLOW)) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07003537 return -15;
3538 }
3539
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003540 /* 12. Epoch date with double-precision plus infinity */
3541 if(QCBORDecode_GetNext(&DCtx, &Item) != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_DATE_OVERFLOW)) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07003542 return -16;
3543 }
3544
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003545 /* 13. Epoch date with half-precision negative infinity */
3546 uError = QCBORDecode_GetNext(&DCtx, &Item);
3547 if(uError != FLOAT_ERR_CODE_NO_HALF_PREC_NO_FLOAT_HW(QCBOR_ERR_DATE_OVERFLOW)) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07003548 return -17;
3549 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003550
3551 return 0;
3552}
3553
Laurence Lundblade37286c02022-09-03 10:05:02 -07003554
Laurence Lundblade4b270642020-08-14 12:53:07 -07003555/*
3556 Test cases covered here. Some items cover more than one of these.
3557 positive integer (zero counts as a positive integer)
3558 negative integer
3559 half-precision float
3560 single-precision float
3561 double-precision float
Laurence Lundbladec7114722020-08-13 05:11:40 -07003562
Laurence Lundblade4b270642020-08-14 12:53:07 -07003563 float Overflow error
3564 Wrong type error for epoch
3565 Wrong type error for date string
3566 float disabled error
3567 half-precision disabled error
3568 -Infinity
3569 Slightly too large integer
3570 Slightly too far from zero
Laurence Lundbladec7114722020-08-13 05:11:40 -07003571
Laurence Lundblade4b270642020-08-14 12:53:07 -07003572 Get epoch by int
3573 Get string by int
3574 Get epoch by string
3575 Get string by string
3576 Fail to get epoch by wrong int label
3577 Fail to get string by wrong string label
3578 Fail to get epoch by string because it is invalid
3579 Fail to get epoch by int because it is invalid
3580
3581 Untagged values
3582 */
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08003583static const uint8_t spSpiffyDateTestInput[] = {
Laurence Lundblade37286c02022-09-03 10:05:02 -07003584 0x87, // array of 7 items
3585
3586 0xa6, // Open a map for tests involving untagged items with labels.
3587
3588 // Untagged integer 0
3589 0x08,
3590 0x00,
3591
3592 // Utagged date string with string label y
3593 0x61, 0x79,
3594 0x6a, '2','0','8','5','-','0','4','-','1','2', // Untagged date string
3595
3596 // Untagged single-precision float with value 3.14 with string label x
3597 0x61, 0x78,
3598 0xFA, 0x40, 0x48, 0xF5, 0xC3,
3599
3600 // Untagged half-precision float with value -2
3601 0x09,
3602 0xF9, 0xC0, 0x00,
3603
3604 /* Untagged date-only date string */
3605 0x18, 0x63,
3606 0x6A, 0x31, 0x39, 0x38, 0x35, 0x2D, 0x30, 0x34, 0x2D, 0x31, 0x32, /* "1985-04-12" */
3607
3608 /* Untagged days-count epoch date */
3609 0x11,
3610 0x19, 0x0F, 0x9A, /* 3994 */
3611
3612 // End of map, back to array
3613
3614 0xa7, // Open map of tagged items with labels
3615
3616 0x00,
3617 0xc0, // tag for string date
3618 0x6a, '1','9','8','5','-','0','4','-','1','2', // Tagged date string
3619
3620
3621 0x01,
3622 0xda, 0x03, 0x03, 0x03, 0x03, // An additional tag
3623 0xc1, // tag for epoch date
3624 0x1a, 0x53, 0x72, 0x4E, 0x00, // Epoch date 1400000000; Tue, 13 May 2014 16:53:20 GMT
3625
3626 0x05,
3627 0xc1,
3628 0xfb, 0xc3, 0xdf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, // -9223372036854773760 largest negative
3629
3630
3631 0x07,
3632 0xc1, // tag for epoch date
3633 0xfb, 0x43, 0xdf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, // 9223372036854773760 largest supported
3634
3635 /* Tagged days-count epoch date */
3636 0x63, 0x53, 0x44, 0x45,
3637 0xD8, 0x64, /* tag(100) */
3638 0x39, 0x29, 0xB3, /* -10676 */
3639
3640 // Untagged -1000 with label z
3641 0x61, 0x7a,
3642 0xda, 0x01, 0x01, 0x01, 0x01, // An additional tag
3643 0x39, 0x03, 0xe7,
3644
3645 /* Tagged date-only date string */
3646 0x63, 0x53, 0x44, 0x53,
3647 0xD9, 0x03, 0xEC,
3648 0x6A, 0x31, 0x39, 0x38, 0x35, 0x2D, 0x30, 0x34, 0x2D, 0x31, 0x32, /* "1985-04-12" */
3649
3650 // End of map of tagged items
Laurence Lundblade4b270642020-08-14 12:53:07 -07003651
3652 0xc1,
3653 0xfb, 0xc3, 0xdf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, // -9.2233720368547748E+18, too negative
3654
Laurence Lundbladec7114722020-08-13 05:11:40 -07003655 0xc1, // tag for epoch date
Laurence Lundblade4b270642020-08-14 12:53:07 -07003656 0x1b, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, // Too-large integer
3657
3658 0xc1, // tag for epoch date
3659 0xf9, 0xfc, 0x00, // Half-precision -Infinity
3660
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07003661 // These two at the end because they are unrecoverable errors
3662 0xc1, // tag for epoch date
3663 0x80, // Erroneous empty array as content for date
3664
3665 0xc0, // tag for string date
3666 0xa0 // Erroneous empty map as content for date
Laurence Lundbladec7114722020-08-13 05:11:40 -07003667};
3668
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03003669int32_t SpiffyDateDecodeTest(void)
Laurence Lundbladec7114722020-08-13 05:11:40 -07003670{
3671 QCBORDecodeContext DC;
Laurence Lundblade4b270642020-08-14 12:53:07 -07003672 QCBORError uError;
Laurence Lundblade37286c02022-09-03 10:05:02 -07003673 int64_t nEpochDate3, nEpochDate5,
3674 nEpochDate4, nEpochDate6,
3675 nEpochDays2;
3676 UsefulBufC StringDate1, StringDate2, StringDays2;
Laurence Lundbladec7114722020-08-13 05:11:40 -07003677
3678 QCBORDecode_Init(&DC,
Laurence Lundblade4b270642020-08-14 12:53:07 -07003679 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSpiffyDateTestInput),
Laurence Lundbladec7114722020-08-13 05:11:40 -07003680 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade37286c02022-09-03 10:05:02 -07003681
3682 /* Items are in an array or map to test look up by label and other
3683 * that might not occur in isolated items. But it does make the
3684 * test a bit messy. */
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07003685 QCBORDecode_EnterArray(&DC, NULL);
Laurence Lundbladec7114722020-08-13 05:11:40 -07003686
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07003687 QCBORDecode_EnterMap(&DC, NULL);
Laurence Lundblade4b270642020-08-14 12:53:07 -07003688
Laurence Lundblade4b270642020-08-14 12:53:07 -07003689 // A single-precision date
Laurence Lundblade9b334962020-08-27 10:55:53 -07003690 QCBORDecode_GetEpochDateInMapSZ(&DC, "x", QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
3691 &nEpochDate5);
Laurence Lundblade4b270642020-08-14 12:53:07 -07003692 uError = QCBORDecode_GetAndResetError(&DC);
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003693 if(uError != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07003694 return 104;
3695 }
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003696 if(uError == QCBOR_SUCCESS) {
3697 if(nEpochDate5 != 3) {
3698 return 103;
3699 }
3700 }
Laurence Lundbladec7114722020-08-13 05:11:40 -07003701
Laurence Lundblade9b334962020-08-27 10:55:53 -07003702 // A half-precision date with value -2 FFF
3703 QCBORDecode_GetEpochDateInMapN(&DC, 9, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
3704 &nEpochDate4);
Laurence Lundblade4b270642020-08-14 12:53:07 -07003705 uError = QCBORDecode_GetAndResetError(&DC);
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003706 if(uError != FLOAT_ERR_CODE_NO_HALF_PREC_NO_FLOAT_HW(QCBOR_SUCCESS)) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07003707 return 106;
3708 }
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003709 if(uError == QCBOR_SUCCESS) {
3710 if(nEpochDate4 != -2) {
3711 return 105;
3712 }
3713 }
Laurence Lundblade4b270642020-08-14 12:53:07 -07003714
3715 // Fail to get an epoch date by string label
Laurence Lundblade9b334962020-08-27 10:55:53 -07003716 QCBORDecode_GetEpochDateInMapSZ(&DC, "no-label",
3717 QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
3718 &nEpochDate6);
Laurence Lundblade4b270642020-08-14 12:53:07 -07003719 uError = QCBORDecode_GetAndResetError(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07003720 if(uError != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07003721 return 107;
3722 }
3723
3724 // Fail to get an epoch date by integer label
Laurence Lundblade9b334962020-08-27 10:55:53 -07003725 QCBORDecode_GetEpochDateInMapN(&DC, 99999, QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
3726 &nEpochDate6);
Laurence Lundblade4b270642020-08-14 12:53:07 -07003727 uError = QCBORDecode_GetAndResetError(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07003728 if(uError != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07003729 return 108;
3730 }
3731
3732 // Fail to get a string date by string label
Laurence Lundblade9b334962020-08-27 10:55:53 -07003733 QCBORDecode_GetDateStringInMapSZ(&DC, "no-label",
3734 QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
3735 &StringDate1);
Laurence Lundblade4b270642020-08-14 12:53:07 -07003736 uError = QCBORDecode_GetAndResetError(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07003737 if(uError != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07003738 return 109;
3739 }
3740
3741 // Fail to get a string date by integer label
Laurence Lundblade9b334962020-08-27 10:55:53 -07003742 QCBORDecode_GetDateStringInMapN(&DC, 99999, QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
3743 &StringDate1);
Laurence Lundblade4b270642020-08-14 12:53:07 -07003744 uError = QCBORDecode_GetAndResetError(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07003745 if(uError != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07003746 return 110;
3747 }
3748
3749 // The rest of these succeed even if float features are disabled
Laurence Lundbladea9489f82020-09-12 13:50:56 -07003750
Laurence Lundblade37286c02022-09-03 10:05:02 -07003751
3752 // Untagged integer 0
3753 QCBORDecode_GetEpochDateInMapN(&DC, 8, QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
3754 &nEpochDate3);
3755 // Untagged date string
3756 QCBORDecode_GetDateStringInMapSZ(&DC, "y", QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
3757 &StringDate2);
3758
3759 QCBORDecode_GetDaysStringInMapN(&DC, 99, QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
3760 &StringDays2);
3761
3762 QCBORDecode_GetEpochDaysInMapN(&DC, 17, QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
3763 &nEpochDays2);
3764
3765 QCBORDecode_ExitMap(&DC);
3766 if(QCBORDecode_GetError(&DC) != QCBOR_SUCCESS) {
3767 return 3001;
3768 }
3769
3770 // The map of tagged items
3771 QCBORDecode_EnterMap(&DC, NULL);
3772
3773#ifndef QCBOR_DISABLE_TAGS
3774 int64_t nEpochDate2,
3775 nEpochDateFail,
3776 nEpochDate1400000000, nEpochDays1;
3777 UsefulBufC StringDays1;
3778 uint64_t uTag1, uTag2;
3779
3780 // Tagged date string
3781 QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
3782 &StringDate1);
3783
Laurence Lundblade4b270642020-08-14 12:53:07 -07003784 // Epoch date 1400000000; Tue, 13 May 2014 16:53:20 GMT
Laurence Lundblade9b334962020-08-27 10:55:53 -07003785 QCBORDecode_GetEpochDateInMapN(&DC,
3786 1,
3787 QCBOR_TAG_REQUIREMENT_TAG |
3788 QCBOR_TAG_REQUIREMENT_ALLOW_ADDITIONAL_TAGS,
3789 &nEpochDate1400000000);
3790 uTag1 = QCBORDecode_GetNthTagOfLast(&DC, 0);
Laurence Lundblade37286c02022-09-03 10:05:02 -07003791
3792 // Get largest negative double precision epoch date allowed
3793 QCBORDecode_GetEpochDateInMapN(&DC,
3794 5,
3795 QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG |
3796 QCBOR_TAG_REQUIREMENT_ALLOW_ADDITIONAL_TAGS,
3797 &nEpochDate2);
3798 uError = QCBORDecode_GetAndResetError(&DC);
3799 if(uError != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)) {
3800 return 102;
3801 }
3802 if(uError == QCBOR_SUCCESS) {
3803 if(nEpochDate2 != -9223372036854773760LL) {
3804 return 101;
3805 }
3806 }
3807
Laurence Lundblade4b270642020-08-14 12:53:07 -07003808 // Untagged -1000 with label z
Laurence Lundblade9b334962020-08-27 10:55:53 -07003809 QCBORDecode_GetEpochDateInMapSZ(&DC,
3810 "z",
3811 QCBOR_TAG_REQUIREMENT_NOT_A_TAG |
3812 QCBOR_TAG_REQUIREMENT_ALLOW_ADDITIONAL_TAGS,
3813 &nEpochDate6);
3814 uTag2 = QCBORDecode_GetNthTagOfLast(&DC, 0);
Laurence Lundblade4b270642020-08-14 12:53:07 -07003815
Laurence Lundblade37286c02022-09-03 10:05:02 -07003816
3817 // Get largest double precision epoch date allowed
3818 QCBORDecode_GetEpochDateInMapN(&DC, 7, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
3819 &nEpochDate2);
3820 uError = QCBORDecode_GetAndResetError(&DC);
3821 if(uError != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)) {
3822 return 112;
3823 }
3824 if(uError == QCBOR_SUCCESS) {
3825 if(nEpochDate2 != 9223372036854773760ULL) {
3826 return 111;
3827 }
3828 }
3829
Laurence Lundblade46d63e92021-05-13 11:37:10 -07003830 /* The days format is much simpler than the date format
3831 * because it can't be a floating point value. The test
3832 * of the spiffy decode functions sufficiently covers
3833 * the test of the non-spiffy decode days date decoding.
3834 * There is no full fan out of the error conditions
3835 * and decode options as that is implemented by code
3836 * that is tested well by the date testing above.
3837 */
3838 QCBORDecode_GetDaysStringInMapSZ(&DC, "SDS", QCBOR_TAG_REQUIREMENT_TAG,
3839 &StringDays1);
3840
Laurence Lundblade46d63e92021-05-13 11:37:10 -07003841 QCBORDecode_GetEpochDaysInMapSZ(&DC, "SDE", QCBOR_TAG_REQUIREMENT_TAG,
3842 &nEpochDays1);
3843
Laurence Lundblade4b270642020-08-14 12:53:07 -07003844 QCBORDecode_ExitMap(&DC);
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07003845 if(QCBORDecode_GetError(&DC) != QCBOR_SUCCESS) {
3846 return 3001;
3847 }
3848
Laurence Lundblade37286c02022-09-03 10:05:02 -07003849 // Too-negative float, -9.2233720368547748E+18
3850 QCBORDecode_GetEpochDate(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nEpochDateFail);
3851 uError = QCBORDecode_GetAndResetError(&DC);
3852 if(uError != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_DATE_OVERFLOW)) {
3853 return 1111;
3854 }
3855
3856 // Too-large integer
3857 QCBORDecode_GetEpochDate(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nEpochDateFail);
3858 uError = QCBORDecode_GetAndResetError(&DC);
3859 if(uError != QCBOR_ERR_DATE_OVERFLOW) {
3860 return 1;
3861 }
3862
3863 // Half-precision minus infinity
3864 QCBORDecode_GetEpochDate(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nEpochDateFail);
3865 uError = QCBORDecode_GetAndResetError(&DC);
3866 if(uError != FLOAT_ERR_CODE_NO_HALF_PREC_NO_FLOAT_HW(QCBOR_ERR_DATE_OVERFLOW)) {
3867 return 2;
3868 }
3869
3870
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07003871 // Bad content for epoch date
3872 QCBORDecode_GetEpochDate(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nEpochDateFail);
3873 uError = QCBORDecode_GetAndResetError(&DC);
3874 if(uError != QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT) {
3875 return 3;
3876 }
3877
3878 // Bad content for string date
3879 QCBORDecode_GetDateString(&DC, QCBOR_TAG_REQUIREMENT_TAG, &StringDate1);
3880 uError = QCBORDecode_GetAndResetError(&DC);
3881 if(uError != QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT) {
3882 return 4;
3883 }
3884
Laurence Lundblade4b270642020-08-14 12:53:07 -07003885 QCBORDecode_ExitArray(&DC);
3886 uError = QCBORDecode_Finish(&DC);
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07003887 if(uError != QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT) {
Laurence Lundblade9b334962020-08-27 10:55:53 -07003888 return 1000 + (int32_t)uError;
Laurence Lundblade4b270642020-08-14 12:53:07 -07003889 }
Laurence Lundblade37286c02022-09-03 10:05:02 -07003890#else /* QCBOR_DISABLE_TAGS */
3891 QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
3892 &StringDate1);
3893 uError = QCBORDecode_GetAndResetError(&DC);
3894 if(uError != QCBOR_ERR_TAGS_DISABLED) {
3895 return 4;
3896 }
3897#endif /* QCBOR_DISABLE_TAGS */
3898
3899
3900#ifndef QCBOR_DISABLE_TAGS
Laurence Lundblade4b270642020-08-14 12:53:07 -07003901
Laurence Lundblade9b334962020-08-27 10:55:53 -07003902 if(nEpochDate1400000000 != 1400000000) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07003903 return 200;
3904 }
3905
Laurence Lundblade9b334962020-08-27 10:55:53 -07003906 if(uTag1 != 0x03030303) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07003907 return 201;
3908 }
3909
Laurence Lundblade37286c02022-09-03 10:05:02 -07003910 if(nEpochDays1 != -10676) {
3911 return 205;
Laurence Lundblade4b270642020-08-14 12:53:07 -07003912 }
3913
Laurence Lundblade37286c02022-09-03 10:05:02 -07003914 if(UsefulBuf_Compare(StringDays1, UsefulBuf_FromSZ("1985-04-12"))) {
3915 return 207;
Laurence Lundblade4b270642020-08-14 12:53:07 -07003916 }
3917
Laurence Lundblade9b334962020-08-27 10:55:53 -07003918 if(uTag2 != 0x01010101) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07003919 return 204;
3920 }
3921
Laurence Lundblade37286c02022-09-03 10:05:02 -07003922 if(nEpochDate6 != -1000) {
3923 return 203;
Laurence Lundblade46d63e92021-05-13 11:37:10 -07003924 }
3925
Laurence Lundblade9b334962020-08-27 10:55:53 -07003926 if(UsefulBuf_Compare(StringDate1, UsefulBuf_FromSZ("1985-04-12"))) {
3927 return 205;
3928 }
3929
Laurence Lundblade37286c02022-09-03 10:05:02 -07003930#endif /* QCBOR_DISABLE_TAGS */
3931
3932 if(nEpochDate3 != 0) {
3933 return 202;
3934 }
3935
3936 if(nEpochDays2 != 3994) {
Laurence Lundblade9b334962020-08-27 10:55:53 -07003937 return 206;
3938 }
3939
Laurence Lundblade37286c02022-09-03 10:05:02 -07003940 if(UsefulBuf_Compare(StringDate2, UsefulBuf_FromSZ("2085-04-12"))) {
3941 return 206;
Laurence Lundblade46d63e92021-05-13 11:37:10 -07003942 }
3943
3944 if(UsefulBuf_Compare(StringDays2, UsefulBuf_FromSZ("1985-04-12"))) {
3945 return 208;
3946 }
3947
Laurence Lundbladec7114722020-08-13 05:11:40 -07003948 return 0;
3949}
3950
3951
Laurence Lundblade9b334962020-08-27 10:55:53 -07003952// Input for one of the tagging tests
Laurence Lundbladecc7da412020-12-27 00:09:07 -08003953static const uint8_t spTagInput[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003954 0xd9, 0xd9, 0xf7, // CBOR magic number
Laurence Lundblade9b334962020-08-27 10:55:53 -07003955 0x81, // Array of one
3956 0xd8, 0x04, // non-preferred serialization of tag 4, decimal fraction
3957 0x82, // Array of two that is the faction 1/3
3958 0x01,
3959 0x03,
3960
3961 /*
3962 More than 4 tags on an item 225(226(227(228(229([])))))
3963 */
3964 0xd8, 0xe1,
3965 0xd8, 0xe2,
3966 0xd8, 0xe3,
3967 0xd8, 0xe4,
3968 0xd8, 0xe5,
3969 0x80,
3970
3971 /* tag 10489608748473423768(
3972 2442302356(
3973 21590(
3974 240(
3975 []))))
3976 */
3977 0xdb, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98,
3978 0xda, 0x91, 0x92, 0x93, 0x94,
3979 0xd9, 0x54, 0x56,
3980 0xd8, 0xf0,
3981 0x80,
3982
3983 /* tag 21590(
3984 10489608748473423768(
3985 2442302357(
3986 65534(
3987 []))))
3988 */
3989 0xdb, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x54, 0x56,
3990 0xdb, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98,
3991 0xda, 0x91, 0x92, 0x93, 0x95,
3992 0xd9, 0xff, 0xfe,
3993 0x80,
3994
3995 /* Make sure to blow past the limit of tags that must be mapped.
3996 works in conjuntion with entries above.
3997 269488144(269488145(269488146(269488147([]))))
3998 */
3999 0xda, 0x10, 0x10, 0x10, 0x10,
4000 0xda, 0x10, 0x10, 0x10, 0x11,
4001 0xda, 0x10, 0x10, 0x10, 0x12,
4002 0xda, 0x10, 0x10, 0x10, 0x13,
4003 0x80,
4004
4005 /* An invalid decimal fraction with an additional tag */
4006 0xd9, 0xff, 0xfa,
4007 0xd8, 0x02, // non-preferred serialization of tag 2, a big num
4008 0x00, // the integer 0; should be a byte string
4009};
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004010
Laurence Lundblade59289e52019-12-30 13:44:37 -08004011/*
4012 DB 9192939495969798 # tag(10489608748473423768)
Laurence Lundblade9b334962020-08-27 10:55:53 -07004013 80 # array(0)
Laurence Lundblade59289e52019-12-30 13:44:37 -08004014 */
Laurence Lundbladecc7da412020-12-27 00:09:07 -08004015static const uint8_t spEncodedLargeTag[] = {0xdb, 0x91, 0x92, 0x93, 0x94, 0x95,
Laurence Lundbladeee851742020-01-08 08:37:05 -08004016 0x96, 0x97, 0x98, 0x80};
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004017
Laurence Lundblade59289e52019-12-30 13:44:37 -08004018/*
4019DB 9192939495969798 # tag(10489608748473423768)
4020 D8 88 # tag(136)
4021 C6 # tag(6)
4022 C7 # tag(7)
4023 80 # array(0)
4024*/
Laurence Lundbladecc7da412020-12-27 00:09:07 -08004025static const uint8_t spLotsOfTags[] = {0xdb, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96,
Laurence Lundbladeee851742020-01-08 08:37:05 -08004026 0x97, 0x98, 0xd8, 0x88, 0xc6, 0xc7, 0x80};
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004027
4028/*
Laurence Lundblade9b334962020-08-27 10:55:53 -07004029 55799(55799(55799({
4030 6(7(-23)): 5859837686836516696(7({
4031 7(-20): 11({
4032 17(-18): 17(17(17("Organization"))),
4033 9(-17): 773("SSG"),
4034 -15: 16(17(6(7("Confusion")))),
4035 17(-16): 17("San Diego"),
4036 17(-14): 17("US")
4037 }),
4038 23(-19): 19({
4039 -11: 9({
4040 -9: -7
4041 }),
4042 90599561(90599561(90599561(-10))): 12(h'0102030405060708090A')
4043 })
4044 })),
4045 16(-22): 23({
4046 11(8(7(-5))): 8(-3)
4047 })
4048 })))
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004049 */
Laurence Lundbladecc7da412020-12-27 00:09:07 -08004050static const uint8_t spCSRWithTags[] = {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004051 0xd9, 0xd9, 0xf7, 0xd9, 0xd9, 0xf7, 0xd9, 0xd9, 0xf7, 0xa2,
4052 0xc6, 0xc7, 0x36,
4053 0xdb, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0xc7, 0xa2,
4054 0xda, 0x00, 0x00, 0x00, 0x07, 0x33,
4055 0xcb, 0xa5,
4056 0xd1, 0x31,
4057 0xd1, 0xd1, 0xd1, 0x6c,
4058 0x4f, 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74, 0x69, 0x6f, 0x6e,
4059 0xc9, 0x30,
4060 0xd9, 0x03, 0x05, 0x63,
4061 0x53, 0x53, 0x47,
4062 0x2e,
Laurence Lundblade9b334962020-08-27 10:55:53 -07004063 0xd0, 0xd1, 0xc6, 0xc7,
4064 0x69,
4065 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73, 0x69, 0x6f, 0x6e,
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004066 0xd1, 0x2f,
4067 0xd1, 0x69,
4068 0x53, 0x61, 0x6e, 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f,
4069 0xd1, 0x2d,
4070 0xd1, 0x62,
4071 0x55, 0x53,
4072 0xd7, 0x32,
4073 0xd3, 0xa2,
4074 0x2a,
4075 0xc9, 0xa1,
4076 0x28,
4077 0x26,
4078 0xda, 0x05, 0x66, 0x70, 0x89, 0xda, 0x05, 0x66, 0x70, 0x89, 0xda, 0x05, 0x66, 0x70, 0x89, 0x29,
4079 0xcc, 0x4a,
4080 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,0x07, 0x08, 0x09, 0x0a,
4081 0xd0, 0x35,
4082 0xd7, 0xa1,
4083 0xcb, 0xc8, 0xc7, 0x24,
4084 0xc8, 0x22};
4085
Laurence Lundblade9b334962020-08-27 10:55:53 -07004086
Laurence Lundbladecc7da412020-12-27 00:09:07 -08004087static const uint8_t spSpiffyTagInput[] = {
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08004088 0x85, // Open array
Laurence Lundblade9b334962020-08-27 10:55:53 -07004089
4090 0xc0, // tag for string date
4091 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string
4092
4093 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string
4094
4095 0x4a, '1','9','8','5','-','0','4','-','1','2', // Date string in byte string
4096
4097 0xd8, 0x23, // tag for regex
4098 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string
4099
4100 0xc0, // tag for string date
4101 0x4a, '1','9','8','5','-','0','4','-','1','2', // Date string in byte string
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07004102
4103 // This last case makes the array untraversable because it is
4104 // an uncrecoverable error. Make sure it stays last and is the only
4105 // instance so the other tests can work.
Laurence Lundblade9b334962020-08-27 10:55:53 -07004106};
4107
4108
Laurence Lundbladec4bded32024-10-09 10:34:39 -07004109static const uint8_t spTaggedString[] = {
4110 0xd8, 0xf0, 0x61, 0x40,
4111};
4112
4113static const uint8_t spTaggedInt[] = {
4114 0xd8, 0xf4, 0x01,
4115};
4116
Laurence Lundbladec5fef682020-01-25 11:38:45 -08004117static int32_t CheckCSRMaps(QCBORDecodeContext *pDC);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004118
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004119
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03004120int32_t OptTagParseTest(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004121{
4122 QCBORDecodeContext DCtx;
Laurence Lundblade9b334962020-08-27 10:55:53 -07004123 QCBORItem Item;
4124 QCBORError uError;
Laurence Lundbladec4bded32024-10-09 10:34:39 -07004125 UsefulBufC UBC;
4126 int64_t nInt;
4127
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004128
Laurence Lundbladeee851742020-01-08 08:37:05 -08004129 QCBORDecode_Init(&DCtx,
Laurence Lundblade9b334962020-08-27 10:55:53 -07004130 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTagInput),
Laurence Lundbladeee851742020-01-08 08:37:05 -08004131 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004132
Laurence Lundblade9b334962020-08-27 10:55:53 -07004133 /*
4134 This test matches the magic number tag and the fraction tag
4135 55799([...])
4136 */
4137 uError = QCBORDecode_GetNext(&DCtx, &Item);
4138 if(uError != QCBOR_SUCCESS) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004139 return -2;
4140 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004141 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004142 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC)) {
4143 return -3;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004144 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004145
Laurence Lundblade9b334962020-08-27 10:55:53 -07004146 /*
4147 4([1,3])
4148 */
4149 uError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundbladedd6e76e2021-03-10 01:54:01 -07004150#ifdef QCBOR_DISABLE_EXP_AND_MANTISSA
Laurence Lundblade9b334962020-08-27 10:55:53 -07004151 if(uError != QCBOR_SUCCESS ||
4152 Item.uDataType != QCBOR_TYPE_ARRAY ||
4153 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_DECIMAL_FRACTION) ||
4154 QCBORDecode_GetNthTag(&DCtx, &Item, 0) != CBOR_TAG_DECIMAL_FRACTION ||
4155 QCBORDecode_GetNthTag(&DCtx, &Item, 1) != CBOR_TAG_INVALID64 ||
4156 QCBORDecode_GetNthTag(&DCtx, &Item, 2) != CBOR_TAG_INVALID64 ||
4157 QCBORDecode_GetNthTag(&DCtx, &Item, 3) != CBOR_TAG_INVALID64 ||
4158 QCBORDecode_GetNthTag(&DCtx, &Item, 4) != CBOR_TAG_INVALID64 ||
4159 Item.val.uCount != 2) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004160 return -4;
4161 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07004162 // consume the items in the array
4163 uError = QCBORDecode_GetNext(&DCtx, &Item);
4164 uError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundblade59289e52019-12-30 13:44:37 -08004165
Laurence Lundbladedd6e76e2021-03-10 01:54:01 -07004166#else /* QCBOR_DISABLE_EXP_AND_MANTISSA */
Laurence Lundblade9b334962020-08-27 10:55:53 -07004167 if(uError != QCBOR_SUCCESS ||
4168 Item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION ||
4169 QCBORDecode_GetNthTag(&DCtx, &Item, 0) != CBOR_TAG_INVALID64 ||
4170 QCBORDecode_GetNthTag(&DCtx, &Item, 1) != CBOR_TAG_INVALID64 ||
4171 QCBORDecode_GetNthTag(&DCtx, &Item, 2) != CBOR_TAG_INVALID64 ||
4172 QCBORDecode_GetNthTag(&DCtx, &Item, 3) != CBOR_TAG_INVALID64 ||
4173 QCBORDecode_GetNthTag(&DCtx, &Item, 4) != CBOR_TAG_INVALID64 ) {
4174 return -5;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004175 }
Laurence Lundbladedd6e76e2021-03-10 01:54:01 -07004176#endif /* QCBOR_DISABLE_EXP_AND_MANTISSA */
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004177
Laurence Lundblade9b334962020-08-27 10:55:53 -07004178 /*
4179 More than 4 tags on an item 225(226(227(228(229([])))))
4180 */
4181 uError = QCBORDecode_GetNext(&DCtx, &Item);
4182 if(uError != QCBOR_ERR_TOO_MANY_TAGS) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004183 return -6;
4184 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07004185
Laurence Lundblade88e9db22020-11-02 03:56:33 -08004186 if(QCBORDecode_GetNthTag(&DCtx, &Item, 0) != CBOR_TAG_INVALID64) {
4187 return -106;
4188 }
4189
4190
Laurence Lundblade9b334962020-08-27 10:55:53 -07004191 /* tag 10489608748473423768(
4192 2442302356(
4193 21590(
4194 240(
4195 []))))
4196 */
4197 uError = QCBORDecode_GetNext(&DCtx, &Item);
4198 if(uError != QCBOR_SUCCESS ||
4199 Item.uDataType != QCBOR_TYPE_ARRAY ||
4200 QCBORDecode_GetNthTag(&DCtx, &Item, 3) != 10489608748473423768ULL ||
4201 QCBORDecode_GetNthTag(&DCtx, &Item, 2) != 2442302356ULL ||
4202 QCBORDecode_GetNthTag(&DCtx, &Item, 1) != 21590ULL ||
4203 QCBORDecode_GetNthTag(&DCtx, &Item, 0) != 240ULL) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004204 return -7;
Laurence Lundblade9b334962020-08-27 10:55:53 -07004205 }
4206
4207 /* tag 21590(
4208 10489608748473423768(
4209 2442302357(
4210 21591(
4211 []))))
4212 */
4213 uError = QCBORDecode_GetNext(&DCtx, &Item);
4214 if(uError != QCBOR_SUCCESS ||
4215 Item.uDataType != QCBOR_TYPE_ARRAY ||
4216 QCBORDecode_GetNthTag(&DCtx, &Item, 0) != 65534ULL ||
4217 QCBORDecode_GetNthTag(&DCtx, &Item, 1) != 2442302357ULL ||
4218 QCBORDecode_GetNthTag(&DCtx, &Item, 2) != 10489608748473423768ULL ||
4219 QCBORDecode_GetNthTag(&DCtx, &Item, 3) != 21590ULL) {
4220 return -8;
4221 }
4222
4223 /* Make sure to blow past the limit of tags that must be mapped.
4224 works in conjuntion with entries above.
4225 269488144(269488145(269488146(269488147([]))))
4226 */
4227 uError = QCBORDecode_GetNext(&DCtx, &Item);
4228 if(uError != QCBOR_ERR_TOO_MANY_TAGS) {
4229 return -9;
4230 }
4231
4232 uError = QCBORDecode_GetNext(&DCtx, &Item);
4233 if(uError == QCBOR_SUCCESS) {
4234 return -10;
4235 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004236
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004237 // ----------------------------------
Laurence Lundbladeee851742020-01-08 08:37:05 -08004238 // This test sets up a caller-config list that includes the very large
Laurence Lundblade9b334962020-08-27 10:55:53 -07004239 // tage and then matches it. Caller-config lists are no longer
4240 // used or needed. This tests backwards compatibility with them.
Laurence Lundbladeee851742020-01-08 08:37:05 -08004241 QCBORDecode_Init(&DCtx,
4242 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEncodedLargeTag),
4243 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004244 const uint64_t puList[] = {0x9192939495969798, 257};
4245 const QCBORTagListIn TL = {2, puList};
4246 QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004247
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004248 if(QCBORDecode_GetNext(&DCtx, &Item)) {
4249 return -8;
4250 }
4251 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
4252 !QCBORDecode_IsTagged(&DCtx, &Item, 0x9192939495969798) ||
4253 QCBORDecode_IsTagged(&DCtx, &Item, 257) ||
4254 QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_BIGFLOAT) ||
4255 Item.val.uCount != 0) {
4256 return -9;
4257 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004258
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004259 //------------------------
Laurence Lundbladeee851742020-01-08 08:37:05 -08004260 // Sets up a caller-configured list and look up something not in it
Laurence Lundblade9b334962020-08-27 10:55:53 -07004261 // Another backwards compatibility test.
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004262 const uint64_t puLongList[17] = {1,2,1};
4263 const QCBORTagListIn TLLong = {17, puLongList};
Laurence Lundbladeee851742020-01-08 08:37:05 -08004264 QCBORDecode_Init(&DCtx,
4265 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEncodedLargeTag),
4266 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004267 QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TLLong);
4268 if(QCBORDecode_GetNext(&DCtx, &Item)) {
4269 return -11;
4270 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004271
Laurence Lundbladef7a70bc2020-10-24 12:23:25 -07004272 uint64_t puTags[4];
Laurence Lundblade9b334962020-08-27 10:55:53 -07004273 QCBORTagListOut Out = {0, 4, puTags};
4274
4275
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004276 // This tests retrievel of the full tag list
Laurence Lundbladeee851742020-01-08 08:37:05 -08004277 QCBORDecode_Init(&DCtx,
4278 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spLotsOfTags),
4279 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004280 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
4281 return -12;
4282 }
4283 if(puTags[0] != 0x9192939495969798 ||
4284 puTags[1] != 0x88 ||
Laurence Lundblade59289e52019-12-30 13:44:37 -08004285 puTags[2] != 0x06 ||
4286 puTags[3] != 0x07) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004287 return -13;
4288 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004289
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004290 // ----------------------
Laurence Lundblade9b334962020-08-27 10:55:53 -07004291 // This tests too small of an out list
Laurence Lundbladeee851742020-01-08 08:37:05 -08004292 QCBORDecode_Init(&DCtx,
4293 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spLotsOfTags),
4294 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004295 QCBORTagListOut OutSmall = {0, 3, puTags};
4296 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &OutSmall) != QCBOR_ERR_TOO_MANY_TAGS) {
4297 return -14;
4298 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004299
Laurence Lundblade9b334962020-08-27 10:55:53 -07004300
4301
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004302 // ---------------
Laurence Lundblade9b334962020-08-27 10:55:53 -07004303 // Decode a version of the "CSR" that has had a ton of tags randomly inserted
4304 // It is a bit of a messy test and maybe could be improved, but
4305 // it is retained as a backwards compatibility check.
Laurence Lundbladeee851742020-01-08 08:37:05 -08004306 QCBORDecode_Init(&DCtx,
4307 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRWithTags),
4308 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004309 int n = CheckCSRMaps(&DCtx);
4310 if(n) {
4311 return n-2000;
4312 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004313
Laurence Lundblade59289e52019-12-30 13:44:37 -08004314 Out = (QCBORTagListOut){0, 16, puTags};
Laurence Lundbladeee851742020-01-08 08:37:05 -08004315 QCBORDecode_Init(&DCtx,
4316 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRWithTags),
4317 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004318
Laurence Lundblade9b334962020-08-27 10:55:53 -07004319 /* With the spiffy decode revision, this tag list is not used.
4320 It doesn't matter if a tag is in this list or not so some
4321 tests that couldn't process a tag because it isn't in this list
4322 now can process these unlisted tags. The tests have been
4323 adjusted for this. */
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004324 const uint64_t puTagList[] = {773, 1, 90599561};
4325 const QCBORTagListIn TagList = {3, puTagList};
4326 QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TagList);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004327
4328
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004329 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
4330 return -100;
4331 }
4332 if(Item.uDataType != QCBOR_TYPE_MAP ||
4333 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC) ||
4334 QCBORDecode_IsTagged(&DCtx, &Item, 90599561) ||
4335 QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_DATE_EPOCH) ||
4336 Item.val.uCount != 2 ||
4337 puTags[0] != CBOR_TAG_CBOR_MAGIC ||
4338 puTags[1] != CBOR_TAG_CBOR_MAGIC ||
4339 puTags[2] != CBOR_TAG_CBOR_MAGIC ||
4340 Out.uNumUsed != 3) {
4341 return -101;
4342 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004343
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004344 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
4345 return -102;
4346 }
4347 if(Item.uDataType != QCBOR_TYPE_MAP ||
4348 QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC) ||
4349 QCBORDecode_IsTagged(&DCtx, &Item, 6) ||
Laurence Lundbladef7a70bc2020-10-24 12:23:25 -07004350 !QCBORDecode_IsTagged(&DCtx, &Item, 7) ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004351 Item.val.uCount != 2 ||
4352 puTags[0] != 5859837686836516696 ||
4353 puTags[1] != 7 ||
4354 Out.uNumUsed != 2) {
4355 return -103;
4356 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004357
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004358 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
4359 return -104;
4360 }
4361 if(Item.uDataType != QCBOR_TYPE_MAP ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004362 Item.val.uCount != 5 ||
4363 puTags[0] != 0x0b ||
4364 Out.uNumUsed != 1) {
4365 return -105;
4366 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004367
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004368 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
4369 return -106;
4370 }
4371 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
4372 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_COSE_MAC0) ||
4373 Item.val.string.len != 12 ||
4374 puTags[0] != CBOR_TAG_COSE_MAC0 ||
4375 puTags[1] != CBOR_TAG_COSE_MAC0 ||
4376 puTags[2] != CBOR_TAG_COSE_MAC0 ||
4377 Out.uNumUsed != 3) {
4378 return -105;
4379 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004380
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004381 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
4382 return -107;
4383 }
4384 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
4385 !QCBORDecode_IsTagged(&DCtx, &Item, 773) ||
4386 Item.val.string.len != 3 ||
4387 puTags[0] != 773 ||
4388 Out.uNumUsed != 1) {
4389 return -108;
4390 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004391
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004392 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
4393 return -109;
4394 }
4395 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade59289e52019-12-30 13:44:37 -08004396 !QCBORDecode_IsTagged(&DCtx, &Item, 16) ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004397 Item.val.string.len != 9 ||
Laurence Lundblade59289e52019-12-30 13:44:37 -08004398 puTags[0] != 16 ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07004399 puTags[3] != 7 ||
4400 Out.uNumUsed != 4) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004401 return -110;
4402 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004403
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004404 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
4405 return -111;
4406 }
4407 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
4408 !QCBORDecode_IsTagged(&DCtx, &Item, 17) ||
4409 Item.val.string.len != 9 ||
4410 puTags[0] != 17 ||
4411 Out.uNumUsed != 1) {
4412 return -112;
4413 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004414
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004415 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
4416 return -111;
4417 }
4418 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
4419 !QCBORDecode_IsTagged(&DCtx, &Item, 17) ||
4420 Item.val.string.len != 2 ||
4421 puTags[0] != 17 ||
4422 Out.uNumUsed != 1) {
4423 return -112;
4424 }
4425
4426 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
4427 return -113;
4428 }
4429 if(Item.uDataType != QCBOR_TYPE_MAP ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07004430 !QCBORDecode_IsTagged(&DCtx, &Item, 19) ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004431 Item.val.uCount != 2 ||
4432 puTags[0] != 19 ||
4433 Out.uNumUsed != 1) {
4434 return -114;
4435 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004436
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004437 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
4438 return -115;
4439 }
4440 if(Item.uDataType != QCBOR_TYPE_MAP ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07004441 !QCBORDecode_IsTagged(&DCtx, &Item, 9) ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004442 Item.val.uCount != 1 ||
4443 puTags[0] != 9 ||
4444 Out.uNumUsed != 1) {
4445 return -116;
4446 }
4447
4448 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
4449 return -116;
4450 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004451 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004452 Item.val.int64 != -7 ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004453 Out.uNumUsed != 0) {
4454 return -117;
4455 }
4456
4457 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
4458 return -118;
4459 }
4460 if(Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
4461 Item.val.string.len != 10 ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004462 puTags[0] != 12 ||
4463 Out.uNumUsed != 1) {
4464 return -119;
4465 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004466
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004467 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
4468 return -120;
4469 }
4470 if(Item.uDataType != QCBOR_TYPE_MAP ||
4471 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_ENC_AS_B16) ||
4472 Item.val.uCount != 1 ||
4473 puTags[0] != 0x17 ||
4474 Out.uNumUsed != 1) {
4475 return -121;
4476 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004477
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004478 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
4479 return -122;
4480 }
4481 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07004482 !QCBORDecode_IsTagged(&DCtx, &Item, 8) ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004483 Item.val.int64 != -3 ||
4484 puTags[0] != 8 ||
4485 Out.uNumUsed != 1) {
4486 return -123;
4487 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004488
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004489 if(QCBORDecode_Finish(&DCtx)) {
4490 return -124;
4491 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07004492
4493 UsefulBufC DateString;
4494 QCBORDecode_Init(&DCtx,
4495 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSpiffyTagInput),
4496 QCBOR_DECODE_MODE_NORMAL);
4497
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07004498 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade9b334962020-08-27 10:55:53 -07004499 // tagged date string
4500 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString);
4501 // untagged date string
4502 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString);
4503 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_SUCCESS) {
4504 return 100;
4505 }
4506 // untagged byte string
4507 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString);
4508 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
4509 return 101;
4510 }
4511 // tagged regex
4512 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString);
4513 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
4514 return 102;
4515 }
4516 // tagged date string with a byte string
4517 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString);
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07004518 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT) {
Laurence Lundblade9b334962020-08-27 10:55:53 -07004519 return 103;
4520 }
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07004521 // The exit errors out because the last item, the date string with
4522 // bad content makes the array untraversable (the bad date string
4523 // could have tag content of an array or such that is not consumed
4524 // by the date decoding).
Laurence Lundblade9b334962020-08-27 10:55:53 -07004525 QCBORDecode_ExitArray(&DCtx);
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07004526 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT) {
Laurence Lundblade9b334962020-08-27 10:55:53 -07004527 return 104;
4528 }
4529
4530
4531 QCBORDecode_Init(&DCtx,
Laurence Lundbladecdbbc192024-06-28 15:13:04 -07004532 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRWithTags),
4533 QCBOR_DECODE_MODE_NORMAL);
4534 QCBORDecode_EnterMap(&DCtx, NULL);
4535 if(QCBORDecode_GetNthTagOfLast(&DCtx, 0) != 55799) {
4536 return 200;
4537 }
4538 if(QCBORDecode_GetNthTagOfLast(&DCtx, 1) != 55799) {
4539 return 202;
4540 }
4541 if(QCBORDecode_GetNthTagOfLast(&DCtx, 2) != 55799) {
4542 return 203;
4543 }
4544 if(QCBORDecode_GetNthTagOfLast(&DCtx, 3) != CBOR_TAG_INVALID64) {
4545 return 204;
4546 }
4547
4548 QCBORDecode_EnterMap(&DCtx, NULL);
4549 if(QCBORDecode_GetNthTagOfLast(&DCtx, 0) != 7) {
4550 return 210;
4551 }
4552 if(QCBORDecode_GetNthTagOfLast(&DCtx, 1) != 5859837686836516696) {
4553 return 212;
4554 }
4555 if(QCBORDecode_GetNthTagOfLast(&DCtx, 2) != CBOR_TAG_INVALID64) {
4556 return 213;
4557 }
4558 if(QCBORDecode_GetNthTagOfLast(&DCtx, 3) != CBOR_TAG_INVALID64) {
4559 return 214;
4560 }
4561
4562
4563 QCBORDecode_Init(&DCtx,
4564 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRWithTags),
4565 QCBOR_DECODE_MODE_NORMAL);
4566 QCBORDecode_EnterMap(&DCtx, NULL);
4567 QCBORDecode_EnterMapFromMapN(&DCtx, -23);
4568 if(QCBORDecode_GetNthTagOfLast(&DCtx, 0) != 7) {
4569 return 220;
4570 }
4571 if(QCBORDecode_GetNthTagOfLast(&DCtx, 1) != 5859837686836516696) {
4572 return 221;
4573 }
4574 if(QCBORDecode_GetNthTagOfLast(&DCtx, 2) != CBOR_TAG_INVALID64) {
4575 return 222;
4576 }
4577
4578#ifndef QCBOR_DISABLE_NON_INTEGER_LABELS
4579 QCBORDecode_Init(&DCtx,
4580 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRWithTags),
4581 QCBOR_DECODE_MODE_MAP_AS_ARRAY);
4582 QCBORDecode_EnterArray(&DCtx, NULL);
4583 if(QCBORDecode_GetNthTagOfLast(&DCtx, 0) != 55799) {
4584 return 230;
4585 }
4586 if(QCBORDecode_GetNthTagOfLast(&DCtx, 1) != 55799) {
4587 return 231;
4588 }
4589 if(QCBORDecode_GetNthTagOfLast(&DCtx, 2) != 55799) {
4590 return 232;
4591 }
4592 if(QCBORDecode_GetNthTagOfLast(&DCtx, 3) != CBOR_TAG_INVALID64) {
4593 return 234;
4594 }
Laurence Lundbladecdbbc192024-06-28 15:13:04 -07004595 QCBORDecode_GetInt64(&DCtx, &nInt);
4596 if(QCBORDecode_GetNthTagOfLast(&DCtx, 0) != 7) {
4597 return 240;
4598 }
4599 if(QCBORDecode_GetNthTagOfLast(&DCtx, 1) != 6) {
4600 return 241;
4601 }
4602 if(QCBORDecode_GetNthTagOfLast(&DCtx, 2) != CBOR_TAG_INVALID64) {
4603 return 242;
4604 }
4605 if(QCBORDecode_GetNthTagOfLast(&DCtx, 3) != CBOR_TAG_INVALID64) {
4606 return 243;
4607 }
4608#endif /* ! QCBOR_DISABLE_NON_INTEGER_LABELS */
4609
Laurence Lundbladec4bded32024-10-09 10:34:39 -07004610
4611
Laurence Lundbladecdbbc192024-06-28 15:13:04 -07004612 QCBORDecode_Init(&DCtx,
Laurence Lundblade9b334962020-08-27 10:55:53 -07004613 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSpiffyTagInput),
4614 QCBOR_DECODE_MODE_NORMAL);
4615
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07004616 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade9b334962020-08-27 10:55:53 -07004617 // tagged date string
4618 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString);
4619 // untagged date string
4620 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString);
4621 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_SUCCESS) {
Laurence Lundbladecdbbc192024-06-28 15:13:04 -07004622 return 250;
Laurence Lundblade9b334962020-08-27 10:55:53 -07004623 }
4624 // untagged byte string
4625 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString);
4626 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
Laurence Lundbladecdbbc192024-06-28 15:13:04 -07004627 return 251;
Laurence Lundblade9b334962020-08-27 10:55:53 -07004628 }
4629 // tagged regex
4630 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString);
4631 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
Laurence Lundbladecdbbc192024-06-28 15:13:04 -07004632 return 252;
Laurence Lundblade9b334962020-08-27 10:55:53 -07004633 }
4634 // tagged date string with a byte string
4635 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString);
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07004636 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT) {
Laurence Lundbladecdbbc192024-06-28 15:13:04 -07004637 return 253;
Laurence Lundblade9b334962020-08-27 10:55:53 -07004638 }
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07004639 // See comments above
Laurence Lundblade9b334962020-08-27 10:55:53 -07004640 QCBORDecode_ExitArray(&DCtx);
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07004641 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT) {
Laurence Lundbladecdbbc192024-06-28 15:13:04 -07004642 return 254;
Laurence Lundblade9b334962020-08-27 10:55:53 -07004643 }
4644
4645 QCBORDecode_Init(&DCtx,
4646 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSpiffyTagInput),
4647 QCBOR_DECODE_MODE_NORMAL);
4648
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07004649 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade9b334962020-08-27 10:55:53 -07004650 // tagged date string
4651 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString);
4652 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
4653 return 300;
4654 }
4655 // untagged date string
4656 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString);
4657 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
4658 return 301;
4659 }
4660 // untagged byte string
4661 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString);
4662 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
4663 return 302;
4664 }
4665 // tagged regex
4666 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString);
4667 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
4668 return 303;
4669 }
4670 // tagged date string with a byte string
4671 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString);
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07004672 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT) {
Laurence Lundblade9b334962020-08-27 10:55:53 -07004673 return 304;
4674 }
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07004675 // See comments above
Laurence Lundblade9b334962020-08-27 10:55:53 -07004676 QCBORDecode_ExitArray(&DCtx);
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07004677 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT) {
Laurence Lundblade9b334962020-08-27 10:55:53 -07004678 return 305;
4679 }
4680
Laurence Lundbladec4bded32024-10-09 10:34:39 -07004681 QCBORDecode_Init(&DCtx,
4682 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTaggedString),
4683 QCBOR_DECODE_MODE_NORMAL);
4684
4685 /* See that QCBORDecode_GetTextString() ignores tags */
4686 QCBORDecode_GetTextString(&DCtx, &UBC);
4687 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) {
4688 return 400;
4689 }
4690 if(UBC.len != 1) {
4691 return 401;
4692 }
4693
4694 uint64_t uTagNumber = QCBORDecode_GetNthTagOfLast(&DCtx, 0);
4695 if(uTagNumber != 240) {
4696 return 404;
4697 }
4698
4699
4700 QCBORDecode_Init(&DCtx,
4701 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTaggedInt),
4702 QCBOR_DECODE_MODE_NORMAL);
4703 /* See that QCBORDecode_GetInt64() ignores tags */
4704 QCBORDecode_GetInt64(&DCtx, &nInt);
4705 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) {
4706 return 410;
4707 }
4708 if(nInt != 1) {
4709 return 411;
4710 }
4711
4712 uTagNumber = QCBORDecode_GetNthTagOfLast(&DCtx, 0);
4713 if(uTagNumber != 244) {
4714 return 414;
4715 }
4716
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004717 return 0;
4718}
4719
Laurence Lundblade37286c02022-09-03 10:05:02 -07004720/*
4721 * These are showing the big numbers converted to integers.
4722 * The tag numbers are not shown.
4723 *
Laurence Lundbladeec290b82024-06-10 11:10:54 -07004724 * [
4725 * 18446744073709551616,
4726 * -18446744073709551617,
4727 * {
4728 * -64: -18446744073709551617,
4729 * 64: 18446744073709551616,
4730 * "BN+": 18446744073709551616,
4731 * "BN-": -18446744073709551617
Laurence Lundblade37286c02022-09-03 10:05:02 -07004732 * }
4733 * ]
4734 */
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004735
Laurence Lundbladecc7da412020-12-27 00:09:07 -08004736static const uint8_t spBigNumInput[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004737 0x83,
4738 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
4739 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
4740 0xA4,
Laurence Lundbladeec290b82024-06-10 11:10:54 -07004741 0x38, 0x3F,
4742 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004743 0x18, 0x40,
4744 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
Laurence Lundbladeec290b82024-06-10 11:10:54 -07004745 0x63, 0x42, 0x4E, 0x2B,
4746 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
4747 0x63, 0x42, 0x4E, 0x2D,
4748 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
4749};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004750
Laurence Lundblade37286c02022-09-03 10:05:02 -07004751#ifndef QCBOR_DISABLE_TAGS
Laurence Lundbladecc7da412020-12-27 00:09:07 -08004752/* The expected big num */
4753static const uint8_t spBigNum[] = {
4754 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
4755 0x00};
Laurence Lundblade37286c02022-09-03 10:05:02 -07004756#endif /* QCBOR_DISABLE_TAGS */
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004757
4758
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03004759int32_t BignumParseTest(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004760{
4761 QCBORDecodeContext DCtx;
4762 QCBORItem Item;
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07004763 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004764
Laurence Lundbladeee851742020-01-08 08:37:05 -08004765 QCBORDecode_Init(&DCtx,
4766 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNumInput),
4767 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004768
4769
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004770 //
4771 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
4772 return -1;
4773 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade830fbf92020-05-31 17:22:33 -07004774 return -2;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004775 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004776
Laurence Lundblade37286c02022-09-03 10:05:02 -07004777#ifndef QCBOR_DISABLE_TAGS
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004778 //
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004779 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07004780 return -3;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004781 if(Item.uDataType != QCBOR_TYPE_POSBIGNUM ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07004782 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07004783 return -4;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004784 }
4785
4786 //
4787 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07004788 return -5;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004789 if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07004790 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07004791 return -6;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004792 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004793
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004794 //
4795 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07004796 return -7;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004797 if(Item.uDataType != QCBOR_TYPE_MAP) {
Laurence Lundblade830fbf92020-05-31 17:22:33 -07004798 return -8;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004799 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004800
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004801 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundbladeec290b82024-06-10 11:10:54 -07004802 return -15;
4803 if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM ||
4804 Item.uLabelType != QCBOR_TYPE_INT64 ||
4805 Item.label.int64 != -64 ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07004806 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundbladeec290b82024-06-10 11:10:54 -07004807 return -16;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004808 }
4809
4810 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07004811 return -11;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004812 if(Item.uDataType != QCBOR_TYPE_POSBIGNUM ||
4813 Item.uLabelType != QCBOR_TYPE_INT64 ||
4814 Item.label.int64 != 64 ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07004815 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07004816 return -12;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004817 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004818
Laurence Lundbladeec290b82024-06-10 11:10:54 -07004819#ifndef QCBOR_DISABLE_NON_INTEGER_LABELS
4820 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
4821 return -9;
4822 if(Item.uDataType != QCBOR_TYPE_POSBIGNUM ||
4823 Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
4824 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
4825 return -10;
4826 }
4827
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004828 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07004829 return -13;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004830 if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM ||
4831 Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07004832 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07004833 return -14;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004834 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004835
Laurence Lundbladeec290b82024-06-10 11:10:54 -07004836
4837#endif /* ! QCBOR_DISABLE_NON_INTEGER_LABELS */
4838
Laurence Lundblade37286c02022-09-03 10:05:02 -07004839#else
4840
4841 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_TAGS_DISABLED) {
4842 return -100;
4843 }
4844#endif /* QCBOR_DISABLE_TAGS */
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004845
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004846 return 0;
4847}
4848
4849
Laurence Lundbladec5fef682020-01-25 11:38:45 -08004850static int32_t CheckItemWithIntLabel(QCBORDecodeContext *pCtx,
Laurence Lundbladeee851742020-01-08 08:37:05 -08004851 uint8_t uDataType,
4852 uint8_t uNestingLevel,
4853 uint8_t uNextNest,
4854 int64_t nLabel,
4855 QCBORItem *pItem)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004856{
4857 QCBORItem Item;
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07004858 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004859
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004860 if((nCBORError = QCBORDecode_GetNext(pCtx, &Item))) return -1;
4861 if(Item.uDataType != uDataType) return -1;
4862 if(uNestingLevel > 0) {
Laurence Lundbladeec290b82024-06-10 11:10:54 -07004863 if(Item.uLabelType != QCBOR_TYPE_INT64) {
Laurence Lundbladeee851742020-01-08 08:37:05 -08004864 return -1;
4865 }
Laurence Lundbladeec290b82024-06-10 11:10:54 -07004866 if(Item.label.int64 != nLabel) {
4867 return -1;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004868 }
Laurence Lundbladeec290b82024-06-10 11:10:54 -07004869
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004870 }
4871 if(Item.uNestingLevel != uNestingLevel) return -1;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304872 if(Item.uNextNestLevel != uNextNest) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004873
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004874 if(pItem) {
4875 *pItem = Item;
4876 }
4877 return 0;
4878}
4879
Laurence Lundblade742df4a2018-10-13 20:07:17 +08004880// Same code checks definite and indefinite length versions of the map
Laurence Lundbladec5fef682020-01-25 11:38:45 -08004881static int32_t CheckCSRMaps(QCBORDecodeContext *pDC)
Laurence Lundblade742df4a2018-10-13 20:07:17 +08004882{
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304883 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 0, 1, 0, NULL)) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004884
Laurence Lundblade9b334962020-08-27 10:55:53 -07004885 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 1, 2, -23, NULL)) return -2;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004886
Laurence Lundblade9b334962020-08-27 10:55:53 -07004887 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 2, 3, -20, NULL)) return -3;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004888
Laurence Lundblade9b334962020-08-27 10:55:53 -07004889 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -18, NULL)) return -4;
4890 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -17, NULL)) return -5;
4891 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -15, NULL)) return -6;
4892 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -16, NULL)) return -7;
4893 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 2, -14, NULL)) return -8;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004894
Laurence Lundblade9b334962020-08-27 10:55:53 -07004895 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 2, 3, -19, NULL)) return -9;
4896 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 3, 4, -11, NULL)) return -10;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004897
Laurence Lundblade9b334962020-08-27 10:55:53 -07004898 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_INT64, 4, 3, -9, NULL)) return -11;
4899 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_BYTE_STRING, 3, 1, -10, NULL)) return -12;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004900
Laurence Lundblade9b334962020-08-27 10:55:53 -07004901 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 1, 2, -22, NULL)) return -13;
4902 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_INT64, 2, 0, -5, NULL)) return -14;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004903
Laurence Lundblade9b334962020-08-27 10:55:53 -07004904 if(QCBORDecode_Finish(pDC)) return -20;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004905
Laurence Lundblade742df4a2018-10-13 20:07:17 +08004906 return 0;
4907}
4908
4909
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004910/*
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004911{
4912 -23: {
4913 -20: {
4914 -18: "Organization",
4915 -17: "SSG",
4916 -15: "Confusion",
4917 -16: "San Diego",
4918 -14: "US"
4919 },
4920 -19: {
4921 -11: {
4922 -9: -7
4923 },
4924 -10: '\u0001\u0002\u0003\u0004\u0005\u0006\a\b\t\n'
4925 }
4926 },
4927 -22: {
4928 -5: -3
4929 }
4930}
Laurence Lundbladecc7da412020-12-27 00:09:07 -08004931*/
4932static const uint8_t spCSRInput[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004933 0xa2, 0x36, 0xa2, 0x33, 0xa5, 0x31, 0x6c, 0x4f,
4934 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74,
4935 0x69, 0x6f, 0x6e, 0x30, 0x63, 0x53, 0x53, 0x47,
4936 0x2e, 0x69, 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73,
4937 0x69, 0x6f, 0x6e, 0x2f, 0x69, 0x53, 0x61, 0x6e,
4938 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f, 0x2d, 0x62,
4939 0x55, 0x53, 0x32, 0xa2, 0x2a, 0xa1, 0x28, 0x26,
4940 0x29, 0x4a, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
4941 0x07, 0x08, 0x09, 0x0a, 0x35, 0xa1, 0x24, 0x22};
4942
Laurence Lundbladecc7da412020-12-27 00:09:07 -08004943// Same map as above, but using indefinite lengths
4944static const uint8_t spCSRInputIndefLen[] = {
4945 0xbf, 0x36, 0xbf, 0x33, 0xbf, 0x31, 0x6c, 0x4f,
4946 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74,
4947 0x69, 0x6f, 0x6e, 0x30, 0x63, 0x53, 0x53, 0x47,
4948 0x2e, 0x69, 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73,
4949 0x69, 0x6f, 0x6e, 0x2f, 0x69, 0x53, 0x61, 0x6e,
4950 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f, 0x2d, 0x62,
4951 0x55, 0x53, 0xff, 0x32, 0xbf, 0x2a, 0xbf, 0x28,
4952 0x26, 0xff, 0x29, 0x4a, 0x01, 0x02, 0x03, 0x04,
4953 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0xff, 0xff,
4954 0x35, 0xbf, 0x24, 0x22, 0xff, 0xff};
4955
4956
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03004957int32_t NestedMapTest(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004958{
4959 QCBORDecodeContext DCtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004960
Laurence Lundbladeee851742020-01-08 08:37:05 -08004961 QCBORDecode_Init(&DCtx,
4962 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput),
4963 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004964
Laurence Lundblade742df4a2018-10-13 20:07:17 +08004965 return CheckCSRMaps(&DCtx);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004966}
4967
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08004968
4969
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03004970int32_t StringDecoderModeFailTest(void)
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08004971{
4972 QCBORDecodeContext DCtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004973
Laurence Lundbladeee851742020-01-08 08:37:05 -08004974 QCBORDecode_Init(&DCtx,
4975 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput),
4976 QCBOR_DECODE_MODE_MAP_STRINGS_ONLY);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004977
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08004978 QCBORItem Item;
4979 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004980
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08004981 if(QCBORDecode_GetNext(&DCtx, &Item)) {
4982 return -1;
4983 }
4984 if(Item.uDataType != QCBOR_TYPE_MAP) {
4985 return -2;
4986 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004987
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08004988 nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
4989 if(nCBORError != QCBOR_ERR_MAP_LABEL_TYPE) {
4990 return -3;
4991 }
4992
4993 return 0;
4994}
4995
4996
Laurence Lundblade742df4a2018-10-13 20:07:17 +08004997
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03004998int32_t NestedMapTestIndefLen(void)
Laurence Lundblade742df4a2018-10-13 20:07:17 +08004999{
5000 QCBORDecodeContext DCtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005001
Laurence Lundbladeee851742020-01-08 08:37:05 -08005002 QCBORDecode_Init(&DCtx,
5003 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInputIndefLen),
5004 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005005
Laurence Lundblade742df4a2018-10-13 20:07:17 +08005006 return CheckCSRMaps(&DCtx);
5007}
5008
5009
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08005010
Laurence Lundblade17ede402018-10-13 11:43:07 +08005011static UsefulBufC make_nested_indefinite_arrays(int n, UsefulBuf Storage)
5012{
5013 UsefulOutBuf UOB;
5014 UsefulOutBuf_Init(&UOB, Storage);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005015
Laurence Lundblade17ede402018-10-13 11:43:07 +08005016 int i;
5017 for(i = 0; i < n; i++) {
5018 UsefulOutBuf_AppendByte(&UOB, 0x9f);
5019 }
5020
5021 for(i = 0; i < n; i++) {
5022 UsefulOutBuf_AppendByte(&UOB, 0xff);
5023 }
5024 return UsefulOutBuf_OutUBuf(&UOB);
5025}
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08005026
5027
Laurence Lundbladec5fef682020-01-25 11:38:45 -08005028static int32_t parse_indeflen_nested(UsefulBufC Nested, int nNestLevel)
Laurence Lundblade17ede402018-10-13 11:43:07 +08005029{
5030 QCBORDecodeContext DC;
5031 QCBORDecode_Init(&DC, Nested, 0);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005032
Laurence Lundblade17ede402018-10-13 11:43:07 +08005033 int j;
5034 for(j = 0; j < nNestLevel; j++) {
5035 QCBORItem Item;
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07005036 QCBORError nReturn = QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade17ede402018-10-13 11:43:07 +08005037 if(j >= QCBOR_MAX_ARRAY_NESTING) {
5038 // Should be in error
Laurence Lundbladea9489f82020-09-12 13:50:56 -07005039 if(nReturn != QCBOR_ERR_ARRAY_DECODE_NESTING_TOO_DEEP) {
Laurence Lundblade17ede402018-10-13 11:43:07 +08005040 return -4;
5041 } else {
5042 return 0; // Decoding doesn't recover after an error
5043 }
5044 } else {
5045 // Should be no error
5046 if(nReturn) {
5047 return -9; // Should not have got an error
5048 }
5049 }
5050 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
5051 return -7;
5052 }
5053 }
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07005054 QCBORError nReturn = QCBORDecode_Finish(&DC);
Laurence Lundblade17ede402018-10-13 11:43:07 +08005055 if(nReturn) {
5056 return -3;
5057 }
5058 return 0;
5059}
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08005060
5061
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03005062int32_t IndefiniteLengthNestTest(void)
Laurence Lundblade17ede402018-10-13 11:43:07 +08005063{
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05305064 UsefulBuf_MAKE_STACK_UB(Storage, 50);
Laurence Lundblade17ede402018-10-13 11:43:07 +08005065 int i;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005066 for(i=1; i < QCBOR_MAX_ARRAY_NESTING+4; i++) {
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08005067 const UsefulBufC Nested = make_nested_indefinite_arrays(i, Storage);
Laurence Lundblade17ede402018-10-13 11:43:07 +08005068 int nReturn = parse_indeflen_nested(Nested, i);
5069 if(nReturn) {
5070 return nReturn;
5071 }
5072 }
5073 return 0;
5074}
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08005075
Laurence Lundbladeee851742020-01-08 08:37:05 -08005076// [1, [2, 3]]
5077static const uint8_t spIndefiniteArray[] = {0x9f, 0x01, 0x82, 0x02, 0x03, 0xff};
5078// No closing break
5079static const uint8_t spIndefiniteArrayBad1[] = {0x9f};
5080// Not enough closing breaks
5081static const uint8_t spIndefiniteArrayBad2[] = {0x9f, 0x9f, 0x02, 0xff};
5082// Too many closing breaks
5083static const uint8_t spIndefiniteArrayBad3[] = {0x9f, 0x02, 0xff, 0xff};
5084// Unclosed indeflen inside def len
5085static const uint8_t spIndefiniteArrayBad4[] = {0x81, 0x9f};
5086// confused tag
5087static const uint8_t spIndefiniteArrayBad5[] = {0x9f, 0xd1, 0xff};
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08005088
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03005089int32_t IndefiniteLengthArrayMapTest(void)
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08005090{
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07005091 QCBORError nResult;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08005092 // --- first test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07005093 UsefulBufC IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArray);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005094
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08005095 // Decode it and see if it is OK
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08005096 QCBORDecodeContext DC;
5097 QCBORItem Item;
5098 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005099
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08005100 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05305101
5102 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
5103 Item.uNestingLevel != 0 ||
5104 Item.uNextNestLevel != 1) {
5105 return -111;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08005106 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005107
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08005108 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05305109 if(Item.uDataType != QCBOR_TYPE_INT64 ||
5110 Item.uNestingLevel != 1 ||
5111 Item.uNextNestLevel != 1) {
5112 return -2;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08005113 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005114
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08005115 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05305116 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
5117 Item.uNestingLevel != 1 ||
5118 Item.uNextNestLevel != 2) {
5119 return -3;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08005120 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005121
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08005122 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade12b495d2018-12-17 11:15:54 -08005123 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade6de37062018-10-15 12:22:42 +05305124 Item.uNestingLevel != 2 ||
5125 Item.uNextNestLevel != 2) {
5126 return -4;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08005127 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005128
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08005129 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade12b495d2018-12-17 11:15:54 -08005130 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade6de37062018-10-15 12:22:42 +05305131 Item.uNestingLevel != 2 ||
5132 Item.uNextNestLevel != 0) {
5133 return -5;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08005134 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005135
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08005136 if(QCBORDecode_Finish(&DC)) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05305137 return -6;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08005138 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005139
Laurence Lundblade19e0c802018-10-13 12:19:55 +08005140 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07005141 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad1);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005142
Laurence Lundblade19e0c802018-10-13 12:19:55 +08005143 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005144
Laurence Lundblade19e0c802018-10-13 12:19:55 +08005145 nResult = QCBORDecode_GetNext(&DC, &Item);
5146 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05305147 return -7;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08005148 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005149
Laurence Lundblade570fab52018-10-13 18:28:27 +08005150 nResult = QCBORDecode_Finish(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07005151 if(nResult != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05305152 return -8;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08005153 }
5154
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005155
Laurence Lundblade19e0c802018-10-13 12:19:55 +08005156 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07005157 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad2);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005158
Laurence Lundblade19e0c802018-10-13 12:19:55 +08005159 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005160
Laurence Lundblade19e0c802018-10-13 12:19:55 +08005161 nResult = QCBORDecode_GetNext(&DC, &Item);
5162 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05305163 return -9;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08005164 }
5165
5166 nResult = QCBORDecode_GetNext(&DC, &Item);
5167 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05305168 return -10;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08005169 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005170
Laurence Lundblade19e0c802018-10-13 12:19:55 +08005171 nResult = QCBORDecode_GetNext(&DC, &Item);
5172 if(nResult || Item.uDataType != QCBOR_TYPE_INT64) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05305173 return -11;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08005174 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005175
Laurence Lundblade19e0c802018-10-13 12:19:55 +08005176 nResult = QCBORDecode_Finish(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07005177 if(nResult != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05305178 return -12;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08005179 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005180
5181
Laurence Lundblade19e0c802018-10-13 12:19:55 +08005182 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07005183 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad3);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005184
Laurence Lundblade19e0c802018-10-13 12:19:55 +08005185 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005186
Laurence Lundblade19e0c802018-10-13 12:19:55 +08005187 nResult = QCBORDecode_GetNext(&DC, &Item);
5188 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05305189 return -13;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08005190 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005191
Laurence Lundblade19e0c802018-10-13 12:19:55 +08005192 nResult = QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade642282a2020-06-23 12:00:33 -07005193 if(nResult != QCBOR_SUCCESS) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05305194 return -14;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08005195 }
Laurence Lundblade6de37062018-10-15 12:22:42 +05305196
Laurence Lundblade642282a2020-06-23 12:00:33 -07005197 nResult = QCBORDecode_GetNext(&DC, &Item);
5198 if(nResult != QCBOR_ERR_BAD_BREAK) {
5199 return -140;
5200 }
5201
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005202
Laurence Lundblade570fab52018-10-13 18:28:27 +08005203 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07005204 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad4);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005205
Laurence Lundblade570fab52018-10-13 18:28:27 +08005206 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005207
Laurence Lundblade570fab52018-10-13 18:28:27 +08005208 nResult = QCBORDecode_GetNext(&DC, &Item);
5209 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05305210 return -15;
Laurence Lundblade570fab52018-10-13 18:28:27 +08005211 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005212
Laurence Lundblade570fab52018-10-13 18:28:27 +08005213 nResult = QCBORDecode_GetNext(&DC, &Item);
5214 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05305215 return -16;
Laurence Lundblade570fab52018-10-13 18:28:27 +08005216 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005217
Laurence Lundblade570fab52018-10-13 18:28:27 +08005218 nResult = QCBORDecode_Finish(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07005219 if(nResult != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05305220 return -17;
Laurence Lundblade570fab52018-10-13 18:28:27 +08005221 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005222
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05305223 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07005224 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad5);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005225
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05305226 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005227
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05305228 nResult = QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade37286c02022-09-03 10:05:02 -07005229
5230#ifndef QCBOR_DISABLE_TAGS
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05305231 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05305232 return -18;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05305233 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005234
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05305235 nResult = QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05305236 if(nResult != QCBOR_ERR_BAD_BREAK) {
5237 return -19;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05305238 }
Laurence Lundblade37286c02022-09-03 10:05:02 -07005239#else /* QCBOR_DISABLE_TAGS */
5240 if(nResult != QCBOR_ERR_TAGS_DISABLED) {
5241 return -20;
5242 }
5243#endif /* QCBOR_DISABLE_TAGS */
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005244
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08005245 return 0;
5246}
5247
Laurence Lundblade17ede402018-10-13 11:43:07 +08005248
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08005249#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
5250
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07005251static const uint8_t spIndefiniteLenString[] = {
Laurence Lundblade17ede402018-10-13 11:43:07 +08005252 0x81, // Array of length one
5253 0x7f, // text string marked with indefinite length
5254 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment
5255 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment
5256 0xff // ending break
5257};
5258
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07005259static const uint8_t spIndefiniteLenStringBad2[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305260 0x81, // Array of length one
5261 0x7f, // text string marked with indefinite length
5262 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment
5263 0x44, 0x6d, 0x69, 0x6e, 0x67, // second segment of wrong type
5264 0xff // ending break
5265};
5266
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07005267static const uint8_t spIndefiniteLenStringBad3[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305268 0x81, // Array of length one
5269 0x7f, // text string marked with indefinite length
5270 0x01, 0x02, // Not a string
5271 0xff // ending break
5272};
5273
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07005274static const uint8_t spIndefiniteLenStringBad4[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305275 0x81, // Array of length one
5276 0x7f, // text string marked with indefinite length
5277 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment
5278 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment
5279 // missing end of string
5280};
5281
Laurence Lundbladeec290b82024-06-10 11:10:54 -07005282#ifndef QCBOR_DISABLE_NON_INTEGER_LABELS
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07005283static const uint8_t spIndefiniteLenStringLabel[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305284 0xa1, // Array of length one
5285 0x7f, // text string marked with indefinite length
5286 0x65, 0x73, 0x74, 0x72, 0x75, 0x75, // first segment
5287 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment
5288 0xff, // ending break
5289 0x01 // integer being labeled.
5290};
Laurence Lundbladeec290b82024-06-10 11:10:54 -07005291#endif /* ! QCBOR_DISABLE_NON_INTEGER_LABELS */
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305292
Laurence Lundbladec5fef682020-01-25 11:38:45 -08005293/**
5294 Make an indefinite length string
5295
5296 @param Storage Storage for string, must be 144 bytes in size
5297 @return The indefinite length string
5298
5299 This makes an array with one indefinite length string that has 7 chunks
5300 from size of 1 byte up to 64 bytes.
5301 */
5302static UsefulBufC MakeIndefiniteBigBstr(UsefulBuf Storage)
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305303{
5304 UsefulOutBuf UOB;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005305
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305306 UsefulOutBuf_Init(&UOB, Storage);
5307 UsefulOutBuf_AppendByte(&UOB, 0x81);
5308 UsefulOutBuf_AppendByte(&UOB, 0x5f);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005309
Laurence Lundbladec5fef682020-01-25 11:38:45 -08005310 uint8_t uStringByte = 0;
5311 // Use of type int is intentional
5312 for(int uChunkSize = 1; uChunkSize <= 128; uChunkSize *= 2) {
5313 // Not using preferred encoding here, but that is OK.
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305314 UsefulOutBuf_AppendByte(&UOB, 0x58);
Laurence Lundbladec5fef682020-01-25 11:38:45 -08005315 UsefulOutBuf_AppendByte(&UOB, (uint8_t)uChunkSize);
5316 for(int j = 0; j < uChunkSize; j++) {
5317 UsefulOutBuf_AppendByte(&UOB, uStringByte);
5318 uStringByte++;
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305319 }
5320 }
5321 UsefulOutBuf_AppendByte(&UOB, 0xff);
5322
5323 return UsefulOutBuf_OutUBuf(&UOB);
5324}
5325
5326static int CheckBigString(UsefulBufC BigString)
5327{
5328 if(BigString.len != 255) {
5329 return 1;
5330 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005331
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305332 for(uint8_t i = 0; i < 255; i++){
5333 if(((const uint8_t *)BigString.ptr)[i] != i) {
5334 return 1;
5335 }
5336 }
5337 return 0;
5338}
5339
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05305340
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03005341int32_t IndefiniteLengthStringTest(void)
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05305342{
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305343 QCBORDecodeContext DC;
5344 QCBORItem Item;
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05305345 // big enough for MakeIndefiniteBigBstr() + MemPool overhead
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08005346 UsefulBuf_MAKE_STACK_UB(MemPool, 350);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005347
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305348 // --- Simple normal indefinite length string ------
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07005349 UsefulBufC IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenString);
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305350 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005351
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05305352 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305353 return -1;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05305354 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005355
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305356 if(QCBORDecode_GetNext(&DC, &Item)) {
5357 return -2;
5358 }
5359 if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.uDataAlloc) {
5360 return -3;
5361 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005362
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305363 if(QCBORDecode_GetNext(&DC, &Item)) {
5364 return -4;
5365 }
5366 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || !Item.uDataAlloc) {
5367 return -5;
5368 }
5369 if(QCBORDecode_Finish(&DC)) {
5370 return -6;
5371 }
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05305372
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305373 // ----- types mismatch ---
Laurence Lundbladeee851742020-01-08 08:37:05 -08005374 QCBORDecode_Init(&DC,
5375 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad2),
5376 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005377
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305378 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
5379 return -7;
5380 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005381
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305382 if(QCBORDecode_GetNext(&DC, &Item)) {
5383 return -8;
5384 }
5385 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
5386 return -9;
5387 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005388
Laurence Lundblade30816f22018-11-10 13:40:22 +07005389 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_INDEFINITE_STRING_CHUNK) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305390 return -10;
5391 }
5392
5393 // ----- not a string ---
Laurence Lundbladeee851742020-01-08 08:37:05 -08005394 QCBORDecode_Init(&DC,
5395 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad3),
5396 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005397
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305398 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
5399 return -11;
5400 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005401
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305402 if(QCBORDecode_GetNext(&DC, &Item)) {
5403 return -12;
5404 }
5405 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
5406 return -13;
5407 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005408
Laurence Lundblade30816f22018-11-10 13:40:22 +07005409 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_INDEFINITE_STRING_CHUNK) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305410 return -14;
5411 }
5412
5413 // ----- no end -----
Laurence Lundbladeee851742020-01-08 08:37:05 -08005414 QCBORDecode_Init(&DC,
5415 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad4),
5416 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005417
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305418 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
5419 return -15;
5420 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005421
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305422 if(QCBORDecode_GetNext(&DC, &Item)) {
5423 return -16;
5424 }
5425 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
5426 return -17;
5427 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005428
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305429 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_HIT_END) {
5430 return -18;
5431 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005432
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305433 // ------ Don't set a string allocator and see an error -----
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05305434 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005435
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05305436 QCBORDecode_GetNext(&DC, &Item);
5437 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305438 return -19;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05305439 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005440
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05305441 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_NO_STRING_ALLOCATOR) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305442 return -20;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05305443 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005444
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05305445 // ----- Mempool is way too small -----
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08005446 UsefulBuf_MAKE_STACK_UB(MemPoolTooSmall, QCBOR_DECODE_MIN_MEM_POOL_SIZE-1);
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05305447
5448 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
5449 if(!QCBORDecode_SetMemPool(&DC, MemPoolTooSmall, false)) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305450 return -21;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05305451 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005452
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05305453 // ----- Mempool is way too small -----
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05305454 UsefulBuf_MAKE_STACK_UB(BigIndefBStrStorage, 290);
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08005455 const UsefulBufC BigIndefBStr = MakeIndefiniteBigBstr(BigIndefBStrStorage);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005456
Laurence Lundbladeee851742020-01-08 08:37:05 -08005457 // 80 is big enough for MemPool overhead, but not BigIndefBStr
5458 UsefulBuf_MAKE_STACK_UB(MemPoolSmall, 80);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005459
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305460 QCBORDecode_Init(&DC, BigIndefBStr, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05305461 if(QCBORDecode_SetMemPool(&DC, MemPoolSmall, false)) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305462 return -22;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05305463 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005464
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05305465 QCBORDecode_GetNext(&DC, &Item);
5466 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305467 return -23;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05305468 }
Laurence Lundblade30816f22018-11-10 13:40:22 +07005469 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_STRING_ALLOCATE) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305470 return -24;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05305471 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005472
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305473 // ---- big bstr -----
5474 QCBORDecode_Init(&DC, BigIndefBStr, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005475
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305476 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
5477 return -25;
5478 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005479
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05305480 if(QCBORDecode_GetNext(&DC, &Item)) {
5481 return -26;
5482 }
5483 if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.uDataAlloc) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305484 return -26;
5485 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005486
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305487 if(QCBORDecode_GetNext(&DC, &Item)) {
5488 return -27;
5489 }
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05305490 if(Item.uDataType != QCBOR_TYPE_BYTE_STRING || !Item.uDataAlloc || Item.uNestingLevel != 1) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305491 return -28;
5492 }
5493 if(CheckBigString(Item.val.string)) {
5494 return -3;
5495 }
5496 if(QCBORDecode_Finish(&DC)) {
5497 return -29;
5498 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005499
Laurence Lundbladeec290b82024-06-10 11:10:54 -07005500#ifndef QCBOR_DISABLE_NON_INTEGER_LABELS
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305501 // --- label is an indefinite length string ------
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07005502 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringLabel), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005503
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305504 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
5505 return -30;
5506 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005507
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305508 QCBORDecode_GetNext(&DC, &Item);
5509 if(Item.uDataType != QCBOR_TYPE_MAP) {
5510 return -31;
5511 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005512
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305513 if(QCBORDecode_GetNext(&DC, &Item)){
5514 return -32;
5515 }
Laurence Lundbladeee851742020-01-08 08:37:05 -08005516 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
5517 Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305518 Item.uDataAlloc || !Item.uLabelAlloc ||
5519 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("struuming"))) {
5520 return -33;
5521 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005522
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305523 if(QCBORDecode_Finish(&DC)) {
5524 return -34;
5525 }
Laurence Lundbladeec290b82024-06-10 11:10:54 -07005526#endif /* ! QCBOR_DISABLE_NON_INTEGER_LABELS */
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005527
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08005528 return 0;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08005529}
5530
5531
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03005532int32_t AllocAllStringsTest(void)
Laurence Lundbladea44d5062018-10-17 18:45:12 +05305533{
5534 QCBORDecodeContext DC;
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08005535 QCBORError nCBORError;
5536
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005537
Laurence Lundbladea44d5062018-10-17 18:45:12 +05305538 // First test, use the "CSRMap" as easy input and checking
Laurence Lundbladeee851742020-01-08 08:37:05 -08005539 QCBORDecode_Init(&DC,
5540 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput),
5541 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005542
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08005543 UsefulBuf_MAKE_STACK_UB(Pool, sizeof(spCSRInput) + QCBOR_DECODE_MIN_MEM_POOL_SIZE);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005544
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08005545 nCBORError = QCBORDecode_SetMemPool(&DC, Pool, 1); // Turn on copying.
5546 if(nCBORError) {
Laurence Lundbladea44d5062018-10-17 18:45:12 +05305547 return -1;
5548 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005549
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08005550 if(CheckCSRMaps(&DC)) {
5551 return -2;
5552 }
5553
Laurence Lundbladeec290b82024-06-10 11:10:54 -07005554#ifndef QCBOR_DISABLE_NON_INTEGER_LABELS
Laurence Lundblade2f467f92020-10-09 17:50:11 -07005555 // Next parse, save pointers to a few strings, destroy original and
5556 // see all is OK.
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08005557 UsefulBuf_MAKE_STACK_UB(CopyOfStorage, sizeof(pValidMapEncoded) + QCBOR_DECODE_MIN_MEM_POOL_SIZE);
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08005558 const UsefulBufC CopyOf = UsefulBuf_Copy(CopyOfStorage, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded));
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08005559
Laurence Lundbladea44d5062018-10-17 18:45:12 +05305560 QCBORDecode_Init(&DC, CopyOf, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08005561 UsefulBuf_Set(Pool, '/');
Laurence Lundbladea44d5062018-10-17 18:45:12 +05305562 QCBORDecode_SetMemPool(&DC, Pool, 1); // Turn on copying.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005563
Laurence Lundbladea44d5062018-10-17 18:45:12 +05305564 QCBORItem Item1, Item2, Item3, Item4;
5565 if((nCBORError = QCBORDecode_GetNext(&DC, &Item1)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07005566 return (int32_t)nCBORError;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05305567 if(Item1.uDataType != QCBOR_TYPE_MAP ||
5568 Item1.val.uCount != 3)
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08005569 return -3;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05305570 if((nCBORError = QCBORDecode_GetNext(&DC, &Item1)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07005571 return (int32_t)nCBORError;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05305572 if((nCBORError = QCBORDecode_GetNext(&DC, &Item2)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07005573 return (int32_t)nCBORError;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05305574 if((nCBORError = QCBORDecode_GetNext(&DC, &Item3)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07005575 return (int32_t)nCBORError;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05305576 if((nCBORError = QCBORDecode_GetNext(&DC, &Item4)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07005577 return (int32_t)nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005578
Laurence Lundblade05ec57b2018-10-21 01:50:03 +05305579 UsefulBuf_Set(CopyOfStorage, '_');
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005580
Laurence Lundbladea44d5062018-10-17 18:45:12 +05305581 if(Item1.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladea44d5062018-10-17 18:45:12 +05305582 Item1.uDataType != QCBOR_TYPE_INT64 ||
5583 Item1.val.int64 != 42 ||
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08005584 Item1.uDataAlloc != 0 ||
5585 Item1.uLabelAlloc == 0 ||
Laurence Lundbladeec290b82024-06-10 11:10:54 -07005586 UsefulBufCompareToSZ(Item1.label.string, "first integer") ||
5587 Item1.label.string.ptr < Pool.ptr ||
5588 Item1.label.string.ptr > (const void *)((const uint8_t *)Pool.ptr + Pool.len)) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08005589 return -4;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09005590 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005591
Laurence Lundbladea44d5062018-10-17 18:45:12 +05305592
5593 if(Item2.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07005594 UsefulBufCompareToSZ(Item2.label.string, "an array of two strings") ||
Laurence Lundbladea44d5062018-10-17 18:45:12 +05305595 Item2.uDataType != QCBOR_TYPE_ARRAY ||
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08005596 Item2.uDataAlloc != 0 ||
5597 Item2.uLabelAlloc == 0 ||
Laurence Lundbladea44d5062018-10-17 18:45:12 +05305598 Item2.val.uCount != 2)
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08005599 return -5;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005600
Laurence Lundbladea44d5062018-10-17 18:45:12 +05305601 if(Item3.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08005602 Item3.uDataAlloc == 0 ||
5603 Item3.uLabelAlloc != 0 ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07005604 UsefulBufCompareToSZ(Item3.val.string, "string1")) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08005605 return -6;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09005606 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005607
Laurence Lundbladea44d5062018-10-17 18:45:12 +05305608 if(Item4.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08005609 Item4.uDataAlloc == 0 ||
5610 Item4.uLabelAlloc != 0 ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07005611 UsefulBufCompareToSZ(Item4.val.string, "string2")) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08005612 return -7;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09005613 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005614
Laurence Lundbladea44d5062018-10-17 18:45:12 +05305615 // Next parse with a pool that is too small
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08005616 UsefulBuf_MAKE_STACK_UB(SmallPool, QCBOR_DECODE_MIN_MEM_POOL_SIZE + 1);
Laurence Lundbladeee851742020-01-08 08:37:05 -08005617 QCBORDecode_Init(&DC,
5618 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded),
5619 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladea44d5062018-10-17 18:45:12 +05305620 QCBORDecode_SetMemPool(&DC, SmallPool, 1); // Turn on copying.
5621 if((nCBORError = QCBORDecode_GetNext(&DC, &Item1)))
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08005622 return -8;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05305623 if(Item1.uDataType != QCBOR_TYPE_MAP ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09005624 Item1.val.uCount != 3) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08005625 return -9;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09005626 }
Laurence Lundbladea44d5062018-10-17 18:45:12 +05305627 if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item1))){
5628 if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item2))) {
5629 if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item3))) {
5630 nCBORError = QCBORDecode_GetNext(&DC, &Item4);
5631 }
5632 }
5633 }
Laurence Lundblade30816f22018-11-10 13:40:22 +07005634 if(nCBORError != QCBOR_ERR_STRING_ALLOCATE) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08005635 return -10;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05305636 }
Laurence Lundbladeec290b82024-06-10 11:10:54 -07005637#endif /* ! QCBOR_DISABLE_NON_INTEGER_LABELS */
Laurence Lundbladea44d5062018-10-17 18:45:12 +05305638
5639 return 0;
5640}
5641
Laurence Lundbladef6531662018-12-04 10:42:22 +09005642
Laurence Lundbladec5fef682020-01-25 11:38:45 -08005643int32_t MemPoolTest(void)
Laurence Lundblade0155b622018-10-12 20:04:37 +08005644{
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08005645 // Set up the decoder with a tiny bit of CBOR to parse because
5646 // nothing can be done with it unless that is set up.
Laurence Lundbladef6531662018-12-04 10:42:22 +09005647 QCBORDecodeContext DC;
5648 const uint8_t pMinimalCBOR[] = {0xa0}; // One empty map
5649 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pMinimalCBOR),0);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005650
Laurence Lundbladef6531662018-12-04 10:42:22 +09005651 // Set up an memory pool of 100 bytes
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08005652 // Then fish into the internals of the decode context
5653 // to get the allocator function so it can be called directly.
5654 // Also figure out how much pool is available for use
5655 // buy subtracting out the overhead.
Laurence Lundbladef6531662018-12-04 10:42:22 +09005656 UsefulBuf_MAKE_STACK_UB(Pool, 100);
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08005657 QCBORError nError = QCBORDecode_SetMemPool(&DC, Pool, 0);
5658 if(nError) {
5659 return -9;
5660 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08005661 QCBORStringAllocate pAlloc = DC.StringAllocator.pfAllocator;
5662 void *pAllocCtx = DC.StringAllocator.pAllocateCxt;
5663 size_t uAvailPool = Pool.len - QCBOR_DECODE_MIN_MEM_POOL_SIZE;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005664
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08005665 // First test -- ask for one more byte than available and see failure
5666 UsefulBuf Allocated = (*pAlloc)(pAllocCtx, NULL, uAvailPool+1);
Laurence Lundbladef6531662018-12-04 10:42:22 +09005667 if(!UsefulBuf_IsNULL(Allocated)) {
5668 return -1;
5669 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005670
Laurence Lundbladef6531662018-12-04 10:42:22 +09005671 // Re do the set up for the next test that will do a successful alloc,
5672 // a fail, a free and then success
Laurence Lundbladef6531662018-12-04 10:42:22 +09005673 QCBORDecode_SetMemPool(&DC, Pool, 0);
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08005674 pAlloc = DC.StringAllocator.pfAllocator;
5675 pAllocCtx = DC.StringAllocator.pAllocateCxt;
5676 uAvailPool = Pool.len - QCBOR_DECODE_MIN_MEM_POOL_SIZE;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005677
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08005678 // Allocate one byte less than available and see success
5679 Allocated = (pAlloc)(pAllocCtx, NULL, uAvailPool-1);
Laurence Lundbladef6531662018-12-04 10:42:22 +09005680 if(UsefulBuf_IsNULL(Allocated)) { // expected to succeed
5681 return -2;
5682 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08005683 // Ask for some more and see failure
5684 UsefulBuf Allocated2 = (*pAlloc)(pAllocCtx, NULL, uAvailPool/2);
Laurence Lundbladef6531662018-12-04 10:42:22 +09005685 if(!UsefulBuf_IsNULL(Allocated2)) { // expected to fail
5686 return -3;
5687 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08005688 // Free the first allocate, retry the second and see success
5689 (*pAlloc)(pAllocCtx, Allocated.ptr, 0); // Free
5690 Allocated = (*pAlloc)(pAllocCtx, NULL, uAvailPool/2);
Laurence Lundbladef6531662018-12-04 10:42:22 +09005691 if(UsefulBuf_IsNULL(Allocated)) { // succeed because of the free
5692 return -4;
5693 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005694
Laurence Lundbladef6531662018-12-04 10:42:22 +09005695 // Re do set up for next test that involves a successful alloc,
5696 // and a successful realloc and a failed realloc
5697 QCBORDecode_SetMemPool(&DC, Pool, 0);
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08005698 pAlloc = DC.StringAllocator.pfAllocator;
5699 pAllocCtx = DC.StringAllocator.pAllocateCxt;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005700
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08005701 // Allocate half the pool and see success
5702 Allocated = (*pAlloc)(pAllocCtx, NULL, uAvailPool/2);
Laurence Lundbladef6531662018-12-04 10:42:22 +09005703 if(UsefulBuf_IsNULL(Allocated)) { // expected to succeed
5704 return -5;
5705 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08005706 // Reallocate to take up the whole pool and see success
5707 Allocated2 = (*pAlloc)(pAllocCtx, Allocated.ptr, uAvailPool);
Laurence Lundbladef6531662018-12-04 10:42:22 +09005708 if(UsefulBuf_IsNULL(Allocated2)) {
5709 return -6;
5710 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08005711 // Make sure its the same pointer and the size is right
Laurence Lundbladef6531662018-12-04 10:42:22 +09005712 if(Allocated2.ptr != Allocated.ptr || Allocated2.len != uAvailPool) {
5713 return -7;
5714 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08005715 // Try to allocate more to be sure there is failure after a realloc
5716 UsefulBuf Allocated3 = (*pAlloc)(pAllocCtx, Allocated.ptr, uAvailPool+1);
5717 if(!UsefulBuf_IsNULL(Allocated3)) {
Laurence Lundbladef6531662018-12-04 10:42:22 +09005718 return -8;
5719 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005720
Laurence Lundbladef6531662018-12-04 10:42:22 +09005721 return 0;
5722}
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08005723
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08005724
5725/* Just enough of an allocator to test configuration of one */
5726static UsefulBuf AllocateTestFunction(void *pCtx, void *pOldMem, size_t uNewSize)
5727{
5728 (void)pOldMem; // unused variable
5729
5730 if(uNewSize) {
5731 // Assumes the context pointer is the buffer and
5732 // nothing too big will ever be asked for.
5733 // This is only good for this basic test!
5734 return (UsefulBuf) {pCtx, uNewSize};
5735 } else {
5736 return NULLUsefulBuf;
5737 }
5738}
5739
5740
Laurence Lundbladec5fef682020-01-25 11:38:45 -08005741int32_t SetUpAllocatorTest(void)
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08005742{
5743 // Set up the decoder with a tiny bit of CBOR to parse because
5744 // nothing can be done with it unless that is set up.
5745 QCBORDecodeContext DC;
5746 const uint8_t pMinimalCBOR[] = {0x62, 0x48, 0x69}; // "Hi"
5747 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pMinimalCBOR),0);
5748
5749 uint8_t pAllocatorBuffer[50];
5750
5751 // This is really just to test that this call works.
5752 // The full functionality of string allocators is tested
5753 // elsewhere with the MemPool internal allocator.
5754 QCBORDecode_SetUpAllocator(&DC, AllocateTestFunction, pAllocatorBuffer, 1);
5755
5756 QCBORItem Item;
5757 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_SUCCESS) {
5758 return -1;
5759 }
5760
5761 if(Item.uDataAlloc == 0 ||
5762 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
5763 Item.val.string.ptr != pAllocatorBuffer) {
5764 return -2;
5765 }
5766
5767 if(QCBORDecode_Finish(&DC) != QCBOR_SUCCESS) {
5768 return -3;
5769 }
5770
5771 return 0;
5772}
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08005773#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
5774
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08005775
Laurence Lundbladedd6e76e2021-03-10 01:54:01 -07005776#ifndef QCBOR_DISABLE_EXP_AND_MANTISSA
Laurence Lundblade59289e52019-12-30 13:44:37 -08005777
Laurence Lundblade37286c02022-09-03 10:05:02 -07005778struct EaMTest {
5779 const char *szName;
5780 UsefulBufC Input;
5781 uint8_t uTagRequirement;
5782 bool bHasTags;
5783
5784 /* Expected values for GetNext */
5785 QCBORError uExpectedErrorGN;
5786 uint8_t uQCBORTypeGN;
5787 int64_t nExponentGN;
5788 int64_t nMantissaGN;
5789 UsefulBufC MantissaGN;
5790
5791 /* Expected values for GetDecimalFraction */
5792 QCBORError uExpectedErrorGDF;
5793 int64_t nExponentGDF;
5794 int64_t nMantissaGDF;
5795
5796 /* Expected values for GetDecimalFractionBig */
5797 QCBORError uExpectedErrorGDFB;
5798 int64_t nExponentGDFB;
5799 UsefulBufC MantissaGDFB;
5800 bool IsNegativeGDFB;
5801
5802 /* Expected values for GetBigFloat */
5803 QCBORError uExpectedErrorGBF;
5804 int64_t nExponentGBF;
5805 int64_t nMantissaGBF;
5806
5807 /* Expected values for GetBigFloatBig */
5808 QCBORError uExpectedErrorGBFB;
5809 int64_t nExponentGBFB;
5810 UsefulBufC MantissaGBFB;
5811 bool IsNegativeGBFB;
Laurence Lundblade59289e52019-12-30 13:44:37 -08005812};
5813
Laurence Lundbladefaec39f2020-08-02 21:53:53 -07005814
Laurence Lundblade37286c02022-09-03 10:05:02 -07005815
5816static const struct EaMTest pEaMTests[] = {
5817 {
5818 "1. Untagged pair (big float or decimal fraction), no tag required",
5819 {(const uint8_t []){0x82, 0x20, 0x03}, 3},
5820 QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
5821 false,
5822
5823 QCBOR_SUCCESS, /* for GetNext */
5824 QCBOR_TYPE_ARRAY,
5825 0,
5826 0,
5827 {(const uint8_t []){0x00}, 1},
5828
5829 QCBOR_SUCCESS, /* GetDecimalFraction */
5830 -1,
5831 3,
5832
5833 QCBOR_SUCCESS, /* for GetDecimalFractionBig */
5834 -1,
Laurence Lundblade20fba4f2024-10-30 07:52:33 -07005835 {(const uint8_t []){0x02}, 1},
Laurence Lundblade37286c02022-09-03 10:05:02 -07005836 false,
5837
5838 QCBOR_SUCCESS, /* for GetBigFloat */
5839 -1,
5840 3,
5841
5842 QCBOR_SUCCESS, /* for GetBigFloatBig */
5843 -1,
Laurence Lundblade20fba4f2024-10-30 07:52:33 -07005844 {(const uint8_t []){0x02}, 1},
Laurence Lundblade37286c02022-09-03 10:05:02 -07005845 false
5846 },
5847
5848 {
5849 "2. Untagged pair (big float or decimal fraction), tag required",
5850 {(const uint8_t []){0x82, 0x20, 0x03}, 3},
5851 QCBOR_TAG_REQUIREMENT_TAG,
5852 false,
5853
5854 QCBOR_SUCCESS, /* for GetNext */
5855 QCBOR_TYPE_ARRAY,
5856 0,
5857 0,
5858 {(const uint8_t []){0x00}, 1},
5859
5860 QCBOR_ERR_UNEXPECTED_TYPE, /* for GetDecimalFraction */
5861 0,
5862 0,
5863
5864 QCBOR_ERR_UNEXPECTED_TYPE, /* for GetDecimalFractionBig */
5865 0,
5866 {(const uint8_t []){0x00}, 1},
5867 false,
5868
5869 QCBOR_ERR_UNEXPECTED_TYPE, /* for GetBigFloat */
5870 0,
5871 0,
5872
5873 QCBOR_ERR_UNEXPECTED_TYPE, /* for GetBigFloatBig */
5874 0,
5875 {(const uint8_t []){0x00}, 1},
5876 false
5877
5878 },
5879
5880 {
5881 "3. Tagged 1.5 decimal fraction, tag 4 optional",
5882 {(const uint8_t []){0xC4, 0x82, 0x20, 0x03}, 4},
5883 QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
5884 true,
5885
5886 QCBOR_SUCCESS, /* for GetNext */
5887 QCBOR_TYPE_DECIMAL_FRACTION,
5888 -1,
5889 3,
5890 {(const uint8_t []){0x00}, 1},
5891
5892
5893 QCBOR_SUCCESS, /* for GetDecimalFraction */
5894 -1,
5895 3,
5896
5897 QCBOR_SUCCESS, /* for GetDecimalFractionBig */
5898 -1,
Laurence Lundblade20fba4f2024-10-30 07:52:33 -07005899 {(const uint8_t []){0x02}, 1},
Laurence Lundblade37286c02022-09-03 10:05:02 -07005900 false,
5901
5902 QCBOR_ERR_UNEXPECTED_TYPE, /* for GetBigFloat */
5903 0,
5904 0,
5905
5906 QCBOR_ERR_UNEXPECTED_TYPE, /* for GetBigFloatBig */
5907 0,
5908 {(const uint8_t []){0x00}, 1},
5909 false
5910 },
5911 {
5912 "4. Tagged 100 * 2^300 big float, tag 5 optional",
5913 {(const uint8_t []){0xC5, 0x82, 0x19, 0x01, 0x2C, 0x18, 0x64}, 7},
5914 QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
5915 true,
5916
5917 QCBOR_SUCCESS, /* for GetNext */
5918 QCBOR_TYPE_BIGFLOAT,
5919 300,
5920 100,
5921 {(const uint8_t []){0x00}, 1},
5922
5923
5924 QCBOR_ERR_UNEXPECTED_TYPE, /* for GetDecimalFraction */
5925 0,
5926 0,
5927
5928 QCBOR_ERR_UNEXPECTED_TYPE, /* for GetDecimalFractionBig */
5929 0,
Laurence Lundblade20fba4f2024-10-30 07:52:33 -07005930 {(const uint8_t []){0x02}, 1},
Laurence Lundblade37286c02022-09-03 10:05:02 -07005931 false,
5932
5933 QCBOR_SUCCESS, /* for GetBigFloat */
5934 300,
5935 100,
5936
5937 QCBOR_SUCCESS, /* for GetBigFloatBig */
5938 300,
Laurence Lundblade20fba4f2024-10-30 07:52:33 -07005939 {(const uint8_t []){0x63}, 1},
Laurence Lundblade37286c02022-09-03 10:05:02 -07005940 false
5941 },
5942
5943 {
5944 "5. Tagged 4([-20, 4759477275222530853136]) decimal fraction, tag 4 required",
5945 {(const uint8_t []){0xC4, 0x82, 0x33,
5946 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,}, 15},
5947 QCBOR_TAG_REQUIREMENT_TAG,
5948 true,
5949
5950 QCBOR_SUCCESS, /* for GetNext */
5951 QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM,
5952 -20,
5953 0,
5954 {(const uint8_t []){0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10}, 10},
5955
5956 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW, /* for GetDecimalFraction */
5957 0,
5958 0,
5959
5960 QCBOR_SUCCESS, /* for GetDecimalFractionBig */
5961 -20,
5962 {(const uint8_t []){0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10}, 10},
5963 false,
5964
5965 QCBOR_ERR_UNEXPECTED_TYPE, /* for GetBigFloat */
5966 0,
5967 0,
5968
5969 QCBOR_ERR_UNEXPECTED_TYPE, /* for GetBigFloatBig */
5970 0,
5971 {(const uint8_t []){0x00}, 0},
5972 false
5973 },
5974
5975 {
5976 "6. Error: Mantissa and exponent inside a Mantissa and exponent",
5977 {(const uint8_t []){0xC4, 0x82, 0x33,
5978 0xC5, 0x82, 0x19, 0x01, 0x2C, 0x18, 0x64}, 10},
5979 QCBOR_TAG_REQUIREMENT_TAG,
5980 true,
5981
5982 QCBOR_ERR_BAD_EXP_AND_MANTISSA, /* for GetNext */
5983 QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM,
5984 0,
5985 0,
5986 {(const uint8_t []){0x00}, 0},
5987
5988 QCBOR_ERR_BAD_EXP_AND_MANTISSA, /* for GetDecimalFraction */
5989 0,
5990 0,
5991
5992 QCBOR_ERR_BAD_EXP_AND_MANTISSA, /* for GetDecimalFractionBig */
5993 0,
5994 {(const uint8_t []){0x00}, 0},
5995 false,
5996
5997 QCBOR_ERR_BAD_EXP_AND_MANTISSA, /* for GetBigFloat */
5998 0,
5999 0,
6000
6001 QCBOR_ERR_BAD_EXP_AND_MANTISSA, /* for GetBigFloatBig */
6002 0,
6003 {(const uint8_t []){0x00}, 0},
6004 false
6005 },
6006 {
6007 "7. Tagged 5([-20, 4294967295]) big float, big num mantissa, tag 5 required",
6008 {(const uint8_t []){0xC5, 0x82, 0x33,
6009 0xC2, 0x44, 0xff, 0xff, 0xff, 0xff}, 9},
6010 QCBOR_TAG_REQUIREMENT_TAG,
6011 true,
6012
6013 QCBOR_SUCCESS, /* for GetNext */
6014 QCBOR_TYPE_BIGFLOAT_POS_BIGNUM,
6015 -20,
6016 0,
6017 {(const uint8_t []){0xff, 0xff, 0xff, 0xff}, 4},
6018
6019 QCBOR_ERR_UNEXPECTED_TYPE, /* for GetDecimalFraction */
6020 0,
6021 0,
6022
6023 QCBOR_ERR_UNEXPECTED_TYPE, /* for GetDecimalFractionBig */
6024 -20,
6025 {(const uint8_t []){0x00}, 1},
6026 false,
6027
6028 QCBOR_SUCCESS, /* for GetBigFloat */
6029 -20,
6030 4294967295,
6031
6032 QCBOR_SUCCESS, /* for GetBigFloatBig */
6033 -20,
6034 {(const uint8_t []){0xff, 0xff, 0xff, 0xff}, 4},
6035 false
6036 },
6037
6038 {
6039 /* Special case for test 8. Don't renumber it. */
6040 "8. Untagged pair with big num (big float or decimal fraction), tag optional",
6041 {(const uint8_t []){0x82, 0x33, 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10}, 14},
6042 QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
6043 true,
6044
6045 QCBOR_SUCCESS, /* for GetNext */
6046 QCBOR_TYPE_ARRAY,
6047 0,
6048 0,
6049 {(const uint8_t []){0x00}, 1},
6050
6051 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW, /* GetDecimalFraction */
6052 0,
6053 0,
6054
6055 QCBOR_SUCCESS, /* for GetDecimalFractionBig */
6056 -20,
6057 {(const uint8_t []){0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10}, 10},
6058 false,
6059
6060 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW, /* for GetBigFloat */
6061 0,
6062 0,
6063
6064 QCBOR_SUCCESS, /* for GetBigFloatBig */
6065 -20,
6066 {(const uint8_t []){0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10}, 10},
6067 false
6068 },
6069
6070 {
6071 "9. decimal fraction with large exponent and negative big num mantissa",
6072 {(const uint8_t []){0xC4, 0x82, 0x1B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
6073 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10}, 23},
6074 QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
6075 true,
6076
6077 QCBOR_SUCCESS, /* for GetNext */
6078 QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM,
6079 9223372036854775807,
6080 0,
6081 {(const uint8_t []){0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10}, 10},
6082
6083 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW, /* GetDecimalFraction */
6084 0,
6085 0,
6086
6087 QCBOR_SUCCESS, /* for GetDecimalFractionBig */
6088 9223372036854775807,
6089 {(const uint8_t []){0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10}, 10},
6090 true,
6091
6092 QCBOR_ERR_UNEXPECTED_TYPE, /* for GetBigFloat */
6093 0,
6094 0,
6095
6096 QCBOR_ERR_UNEXPECTED_TYPE, /* for GetBigFloatBig */
6097 0,
6098 {(const uint8_t []){0x00}, 1},
6099 false
6100 },
6101};
6102
6103
6104
6105int32_t ProcessEaMTests(void)
Laurence Lundblade59289e52019-12-30 13:44:37 -08006106{
Laurence Lundblade37286c02022-09-03 10:05:02 -07006107 size_t uIndex;
6108 QCBORDecodeContext DCtx;
6109 QCBORItem Item;
6110 QCBORError uError;
6111 int64_t nMantissa, nExponent;
6112 MakeUsefulBufOnStack( MantissaBuf, 200);
6113 UsefulBufC Mantissa;
6114 bool bMantissaIsNegative;
6115
6116 for(uIndex = 0; uIndex < C_ARRAY_COUNT(pEaMTests, struct EaMTest); uIndex++) {
6117 const struct EaMTest *pT = &pEaMTests[uIndex];
6118 /* Decode with GetNext */
6119 QCBORDecode_Init(&DCtx, pT->Input, 0);
6120
6121 if(uIndex + 1 == 9) {
6122 nExponent = 99; // just to set a break point
6123 }
6124
6125 uError = QCBORDecode_GetNext(&DCtx, &Item);
6126#ifdef QCBOR_DISABLE_TAGS
6127 /* Test 8 is a special case when tags are disabled */
6128 if(pT->bHasTags && uIndex + 1 != 8) {
6129 if(uError != QCBOR_ERR_TAGS_DISABLED) {
6130 return (int32_t)(1+uIndex) * 1000 + 9;
6131 }
6132 } else {
6133#endif
6134 /* Now check return code, data type, mantissa and exponent */
6135 if(pT->uExpectedErrorGN != uError) {
6136 return (int32_t)(1+uIndex) * 1000 + 1;
6137 }
6138 if(uError == QCBOR_SUCCESS && pT->uQCBORTypeGN != QCBOR_TYPE_ARRAY) {
6139 if(pT->uQCBORTypeGN != Item.uDataType) {
6140 return (int32_t)(1+uIndex) * 1000 + 2;
6141 }
6142 if(pT->nExponentGN != Item.val.expAndMantissa.nExponent) {
6143 return (int32_t)(1+uIndex) * 1000 + 3;
6144 }
6145 if(Item.uDataType == QCBOR_TYPE_DECIMAL_FRACTION || Item.uDataType == QCBOR_TYPE_BIGFLOAT ) {
6146 if(pT->nMantissaGN != Item.val.expAndMantissa.Mantissa.nInt) {
6147 return (int32_t)(1+uIndex) * 1000 + 4;
6148 }
6149 } else {
6150 if(UsefulBuf_Compare(Item.val.expAndMantissa.Mantissa.bigNum, pT->MantissaGN)) {
6151 return (int32_t)(1+uIndex) * 1000 + 5;
6152 }
6153 }
6154 }
6155#ifdef QCBOR_DISABLE_TAGS
6156 }
6157#endif
6158
6159 /* Decode with GetDecimalFraction */
6160 QCBORDecode_Init(&DCtx, pT->Input, 0);
6161 QCBORDecode_GetDecimalFraction(&DCtx,
6162 pT->uTagRequirement,
6163 &nMantissa,
6164 &nExponent);
6165 uError = QCBORDecode_GetAndResetError(&DCtx);
6166#ifdef QCBOR_DISABLE_TAGS
6167 if(pT->bHasTags) {
6168 if(uError != QCBOR_ERR_TAGS_DISABLED) {
6169 return (int32_t)(1+uIndex) * 1000 + 39;
6170 }
6171 } else {
6172#endif
6173 /* Now check return code, mantissa and exponent */
6174 if(pT->uExpectedErrorGDF != uError) {
6175 return (int32_t)(1+uIndex) * 1000 + 31;
6176 }
6177 if(uError == QCBOR_SUCCESS) {
6178 if(pT->nExponentGDF != nExponent) {
6179 return (int32_t)(1+uIndex) * 1000 + 32;
6180 }
6181 if(pT->nMantissaGDF != nMantissa) {
6182 return (int32_t)(1+uIndex) * 1000 + 33;
6183 }
6184 }
6185#ifdef QCBOR_DISABLE_TAGS
6186 }
6187#endif
6188
6189 /* Decode with GetDecimalFractionBig */
6190 QCBORDecode_Init(&DCtx, pT->Input, 0);
6191 QCBORDecode_GetDecimalFractionBig(&DCtx,
6192 pT->uTagRequirement,
6193 MantissaBuf,
6194 &Mantissa,
6195 &bMantissaIsNegative,
6196 &nExponent);
6197 uError = QCBORDecode_GetAndResetError(&DCtx);
6198#ifdef QCBOR_DISABLE_TAGS
6199 if(pT->bHasTags) {
6200 if(uError != QCBOR_ERR_TAGS_DISABLED) {
6201 return (int32_t)(1+uIndex) * 1000 + 49;
6202 }
6203 } else {
6204#endif
6205 /* Now check return code, mantissa (bytes and sign) and exponent */
6206 if(pT->uExpectedErrorGDFB != uError) {
6207 return (int32_t)(1+uIndex) * 1000 + 41;
6208 }
6209 if(uError == QCBOR_SUCCESS) {
6210 if(pT->nExponentGDFB != nExponent) {
6211 return (int32_t)(1+uIndex) * 1000 + 42;
6212 }
6213 if(pT->IsNegativeGDFB != bMantissaIsNegative) {
6214 return (int32_t)(1+uIndex) * 1000 + 43;
6215 }
6216 if(UsefulBuf_Compare(Mantissa, pT->MantissaGDFB)) {
6217 return (int32_t)(1+uIndex) * 1000 + 44;
6218 }
6219 }
6220#ifdef QCBOR_DISABLE_TAGS
6221 }
6222#endif
6223
6224 /* Decode with GetBigFloat */
6225 QCBORDecode_Init(&DCtx, pT->Input, 0);
6226 QCBORDecode_GetBigFloat(&DCtx,
6227 pT->uTagRequirement,
6228 &nMantissa,
6229 &nExponent);
6230 uError = QCBORDecode_GetAndResetError(&DCtx);
6231#ifdef QCBOR_DISABLE_TAGS
6232 if(pT->bHasTags) {
6233 if(uError != QCBOR_ERR_TAGS_DISABLED) {
6234 return (int32_t)(1+uIndex) * 1000 + 19;
6235 }
6236 } else {
6237#endif
6238 /* Now check return code, mantissa and exponent */
6239 if(pT->uExpectedErrorGBF != uError) {
6240 return (int32_t)(1+uIndex) * 1000 + 11;
6241 }
6242 if(uError == QCBOR_SUCCESS) {
6243 if(pT->nExponentGBF != nExponent) {
6244 return (int32_t)(1+uIndex) * 1000 + 12;
6245 }
6246 if(pT->nMantissaGBF != nMantissa) {
6247 return (int32_t)(1+uIndex) * 1000 + 13;
6248 }
6249 }
6250#ifdef QCBOR_DISABLE_TAGS
6251 }
6252#endif
6253
6254 /* Decode with GetBigFloatBig */
6255 QCBORDecode_Init(&DCtx, pT->Input, 0);
6256 QCBORDecode_GetBigFloatBig(&DCtx,
6257 pT->uTagRequirement,
6258 MantissaBuf,
6259 &Mantissa,
6260 &bMantissaIsNegative,
6261 &nExponent);
6262 uError = QCBORDecode_GetAndResetError(&DCtx);
6263#ifdef QCBOR_DISABLE_TAGS
6264 if(pT->bHasTags) {
6265 if(uError != QCBOR_ERR_TAGS_DISABLED) {
6266 return (int32_t)(1+uIndex) * 1000 + 29;
6267 }
6268 } else {
6269#endif
6270 /* Now check return code, mantissa (bytes and sign) and exponent */
6271 if(pT->uExpectedErrorGBFB != uError) {
6272 return (int32_t)(1+uIndex) * 1000 + 21;
6273 }
6274 if(uError == QCBOR_SUCCESS) {
6275 if(pT->nExponentGBFB != nExponent) {
6276 return (int32_t)(1+uIndex) * 1000 + 22;
6277 }
6278 if(pT->IsNegativeGBFB != bMantissaIsNegative) {
6279 return (int32_t)(1+uIndex) * 1000 + 23;
6280 }
6281 if(UsefulBuf_Compare(Mantissa, pT->MantissaGBFB)) {
6282 return (int32_t)(1+uIndex) * 1000 + 24;
6283 }
6284 }
6285#ifdef QCBOR_DISABLE_TAGS
6286 }
6287#endif
6288 }
6289
6290 return 0;
6291}
6292
6293
6294int32_t ExponentAndMantissaDecodeTestsSecondary(void)
6295{
6296#ifndef QCBOR_DISABLE_TAGS
Laurence Lundblade59289e52019-12-30 13:44:37 -08006297 QCBORDecodeContext DC;
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07006298 QCBORError uErr;
Laurence Lundblade59289e52019-12-30 13:44:37 -08006299 QCBORItem item;
6300
Laurence Lundblade17af4902020-01-07 19:11:55 -08006301 static const uint8_t spBigNumMantissa[] = {0x01, 0x02, 0x03, 0x04, 0x05,
6302 0x06, 0x07, 0x08, 0x09, 0x010};
6303 UsefulBufC BN = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNumMantissa);
Laurence Lundblade59289e52019-12-30 13:44:37 -08006304
6305
Laurence Lundblade59289e52019-12-30 13:44:37 -08006306
6307 /* Now encode some stuff and then decode it */
6308 uint8_t pBuf[40];
6309 QCBOREncodeContext EC;
6310 UsefulBufC Encoded;
6311
6312 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(pBuf));
6313 QCBOREncode_OpenArray(&EC);
6314 QCBOREncode_AddDecimalFraction(&EC, 999, 1000); // 999 * (10 ^ 1000)
Laurence Lundblade0f143652024-10-06 11:06:19 -07006315 QCBOREncode_AddTBigFloat(&EC, QCBOR_ENCODE_AS_TAG, 100, INT32_MIN);
6316 QCBOREncode_AddTDecimalFractionBigNum(&EC, QCBOR_ENCODE_AS_TAG, BN, false, INT32_MAX);
Laurence Lundblade59289e52019-12-30 13:44:37 -08006317 QCBOREncode_CloseArray(&EC);
6318 QCBOREncode_Finish(&EC, &Encoded);
6319
6320
6321 QCBORDecode_Init(&DC, Encoded, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07006322 uErr = QCBORDecode_GetNext(&DC, &item);
6323 if(uErr != QCBOR_SUCCESS) {
6324 return 100;
Laurence Lundblade59289e52019-12-30 13:44:37 -08006325 }
6326
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07006327 uErr = QCBORDecode_GetNext(&DC, &item);
6328 if(uErr != QCBOR_SUCCESS) {
6329 return 101;
Laurence Lundblade59289e52019-12-30 13:44:37 -08006330 }
6331
6332 if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION ||
6333 item.val.expAndMantissa.nExponent != 1000 ||
6334 item.val.expAndMantissa.Mantissa.nInt != 999) {
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07006335 return 102;
Laurence Lundblade59289e52019-12-30 13:44:37 -08006336 }
6337
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07006338 uErr = QCBORDecode_GetNext(&DC, &item);
6339 if(uErr != QCBOR_SUCCESS) {
6340 return 103;
Laurence Lundblade59289e52019-12-30 13:44:37 -08006341 }
6342
6343 if(item.uDataType != QCBOR_TYPE_BIGFLOAT ||
6344 item.val.expAndMantissa.nExponent != INT32_MIN ||
6345 item.val.expAndMantissa.Mantissa.nInt != 100) {
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07006346 return 104;
Laurence Lundblade59289e52019-12-30 13:44:37 -08006347 }
6348
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07006349 uErr = QCBORDecode_GetNext(&DC, &item);
6350 if(uErr != QCBOR_SUCCESS) {
6351 return 105;
Laurence Lundblade59289e52019-12-30 13:44:37 -08006352 }
6353
6354 if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM ||
6355 item.val.expAndMantissa.nExponent != INT32_MAX ||
6356 UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) {
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07006357 return 106;
6358 }
6359
Laurence Lundblade37286c02022-09-03 10:05:02 -07006360#endif /* QCBOR_TAGS_DISABLED */
Laurence Lundblade59289e52019-12-30 13:44:37 -08006361
6362 return 0;
6363}
6364
6365
Laurence Lundblade37286c02022-09-03 10:05:02 -07006366int32_t ExponentAndMantissaDecodeTests(void)
6367{
6368 int32_t rv = ProcessEaMTests();
6369 if(rv) {
6370 return rv;
6371 }
6372
6373 return ExponentAndMantissaDecodeTestsSecondary();
6374}
6375
6376
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08006377static const struct DecodeFailTestInput ExponentAndMantissaFailures[] = {
6378 { "Exponent > INT64_MAX",
6379 QCBOR_DECODE_MODE_NORMAL,
6380 {"\xC4\x82\x1B\x7f\xFF\xFF\xFF\xFF\xFF\xFF\xFF\x1B\x80\xFF\xFF\xFF\xFF\xFF\xFF\xFF", 20},
6381 QCBOR_ERR_BAD_EXP_AND_MANTISSA
6382 },
6383 { "Mantissa > INT64_MAX",
6384 QCBOR_DECODE_MODE_NORMAL,
6385 {"\xC4\x82\x1B\x80\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xC3\x4A\x01\x02\x03\x04\x05\x06\x07\x08\x09\x10", 23},
6386 QCBOR_ERR_BAD_EXP_AND_MANTISSA
6387 },
6388 {
6389 "End of input",
6390 QCBOR_DECODE_MODE_NORMAL,
6391 {"\xC4\x82", 2},
6392 QCBOR_ERR_NO_MORE_ITEMS
6393 },
6394 {"bad content for big num",
6395 QCBOR_DECODE_MODE_NORMAL,
6396 {"\xC4\x82\x01\xc3\x01", 5},
6397 QCBOR_ERR_BAD_OPT_TAG
6398 },
6399 {"bad content for big num",
6400 QCBOR_DECODE_MODE_NORMAL,
Laurence Lundbladecdbbc192024-06-28 15:13:04 -07006401 {"\xC4\x82\xC2\x01\x1F", 5},
6402 QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08006403 },
6404 {"Bad integer for exponent",
6405 QCBOR_DECODE_MODE_NORMAL,
6406 {"\xC4\x82\x01\x1f", 4},
6407 QCBOR_ERR_BAD_INT
6408 },
6409 {"Bad integer for mantissa",
6410 QCBOR_DECODE_MODE_NORMAL,
6411 {"\xC4\x82\x1f\x01", 4},
6412 QCBOR_ERR_BAD_INT
6413 },
6414 {"3 items in array",
6415 QCBOR_DECODE_MODE_NORMAL,
6416 {"\xC4\x83\x03\x01\x02", 5},
6417 QCBOR_ERR_BAD_EXP_AND_MANTISSA},
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08006418#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08006419 {"unterminated indefinite length array",
6420 QCBOR_DECODE_MODE_NORMAL,
6421 {"\xC4\x9f\x03\x01\x02", 5},
6422 QCBOR_ERR_BAD_EXP_AND_MANTISSA
6423 },
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08006424#else /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08006425 {"unterminated indefinite length array",
6426 QCBOR_DECODE_MODE_NORMAL,
6427 {"\xC4\x9f\x03\x01\x02", 5},
6428 QCBOR_ERR_INDEF_LEN_ARRAYS_DISABLED
6429 },
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08006430#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08006431 {"Empty array",
6432 QCBOR_DECODE_MODE_NORMAL,
6433 {"\xC4\x80", 2},
6434 QCBOR_ERR_NO_MORE_ITEMS
6435 },
6436 {"Second is not an integer",
6437 QCBOR_DECODE_MODE_NORMAL,
6438 {"\xC4\x82\x03\x40", 4},
6439 QCBOR_ERR_BAD_EXP_AND_MANTISSA
6440 },
6441 {"First is not an integer",
6442 QCBOR_DECODE_MODE_NORMAL,
6443 {"\xC4\x82\x40", 3},
6444 QCBOR_ERR_BAD_EXP_AND_MANTISSA
6445 },
6446 {"Not an array",
6447 QCBOR_DECODE_MODE_NORMAL,
6448 {"\xC4\xA2", 2},
6449 QCBOR_ERR_BAD_EXP_AND_MANTISSA
6450 }
Laurence Lundblade59289e52019-12-30 13:44:37 -08006451};
6452
6453
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08006454int32_t
6455ExponentAndMantissaDecodeFailTests(void)
Laurence Lundblade59289e52019-12-30 13:44:37 -08006456{
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08006457 return ProcessDecodeFailures(ExponentAndMantissaFailures,
6458 C_ARRAY_COUNT(ExponentAndMantissaFailures,
6459 struct DecodeFailTestInput));
Laurence Lundblade59289e52019-12-30 13:44:37 -08006460}
6461
Laurence Lundbladedd6e76e2021-03-10 01:54:01 -07006462#endif /* QCBOR_DISABLE_EXP_AND_MANTISSA */
Laurence Lundbladebb87be22020-04-09 19:15:32 -07006463
6464
6465
6466/*
6467 Some basic CBOR with map and array used in a lot of tests.
6468 The map labels are all strings
6469
Laurence Lundblade8ffdb742020-05-07 02:49:18 -07006470 {
6471 "first integer": 42,
Laurence Lundbladebb87be22020-04-09 19:15:32 -07006472 "an array of two strings": [
6473 "string1", "string2"
6474 ],
6475 "map in a map": {
6476 "bytes 1": h'78787878',
6477 "bytes 2": h'79797979',
6478 "another int": 98,
6479 "text 2": "lies, damn lies and statistics"
6480 }
Laurence Lundbladeddebabe2020-11-22 11:32:17 -08006481 }
Laurence Lundbladebb87be22020-04-09 19:15:32 -07006482 */
Laurence Lundblade9b334962020-08-27 10:55:53 -07006483
Laurence Lundbladef7a70bc2020-10-24 12:23:25 -07006484int32_t SpiffyDecodeBasicMap(UsefulBufC input)
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07006485{
6486 QCBORItem Item1, Item2, Item3;
6487 int64_t nDecodedInt1, nDecodedInt2;
6488 UsefulBufC B1, B2, S1, S2, S3;
6489
6490 QCBORDecodeContext DCtx;
6491 QCBORError nCBORError;
6492
6493 QCBORDecode_Init(&DCtx, input, 0);
6494
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006495 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07006496
6497 QCBORDecode_GetInt64InMapSZ(&DCtx, "first integer", &nDecodedInt1);
6498
6499 QCBORDecode_EnterMapFromMapSZ(&DCtx, "map in a map");
6500 QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2);
Laurence Lundblade323f8a92020-09-06 19:43:09 -07006501 QCBORDecode_GetByteStringInMapSZ(&DCtx, "bytes 1", &B1);
6502 QCBORDecode_GetByteStringInMapSZ(&DCtx, "bytes 2", &B2);
6503 QCBORDecode_GetTextStringInMapSZ(&DCtx, "text 2", &S1);
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07006504 QCBORDecode_ExitMap(&DCtx);
6505
6506 QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings");
6507 QCBORDecode_GetNext(&DCtx, &Item1);
6508 QCBORDecode_GetNext(&DCtx, &Item2);
6509 if(QCBORDecode_GetNext(&DCtx, &Item3) != QCBOR_ERR_NO_MORE_ITEMS) {
6510 return -400;
6511 }
6512 QCBORDecode_ExitArray(&DCtx);
6513
6514 // Parse the same array again using GetText() instead of GetItem()
6515 QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings");
Laurence Lundblade323f8a92020-09-06 19:43:09 -07006516 QCBORDecode_GetTextString(&DCtx, &S2);
6517 QCBORDecode_GetTextString(&DCtx, &S3);
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07006518 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) {
6519 return 5000;
6520 }
6521 /* QCBORDecode_GetText(&DCtx, &S3);
6522 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_NO_MORE_ITEMS) {
6523 return 5001;
6524 } */
6525
6526 QCBORDecode_ExitArray(&DCtx);
6527
6528 QCBORDecode_ExitMap(&DCtx);
6529
6530 nCBORError = QCBORDecode_Finish(&DCtx);
6531
6532 if(nCBORError) {
6533 return (int32_t)nCBORError;
6534 }
6535
6536 if(nDecodedInt1 != 42) {
6537 return 1001;
6538 }
6539
6540 if(nDecodedInt2 != 98) {
6541 return 1002;
6542 }
6543
6544 if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07006545 UsefulBufCompareToSZ(Item1.val.string, "string1")) {
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07006546 return 1003;
6547 }
6548
6549 if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07006550 UsefulBufCompareToSZ(Item2.val.string, "string2")) {
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07006551 return 1004;
6552 }
6553
Laurence Lundblade9b334962020-08-27 10:55:53 -07006554 if(UsefulBufCompareToSZ(S1, "lies, damn lies and statistics")) {
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07006555 return 1005;
6556 }
6557
6558 if(UsefulBuf_Compare(B1, UsefulBuf_FromSZ("xxxx"))){
6559 return 1006;
6560 }
6561
6562 if(UsefulBuf_Compare(B2, UsefulBuf_FromSZ("yyyy"))){
6563 return 1007;
6564 }
6565
6566 if(UsefulBuf_Compare(S2, UsefulBuf_FromSZ("string1"))){
6567 return 1008;
6568 }
6569
6570 if(UsefulBuf_Compare(S3, UsefulBuf_FromSZ("string2"))){
6571 return 1009;
6572 }
6573
6574 return 0;
6575}
6576
Laurence Lundbladeddebabe2020-11-22 11:32:17 -08006577/*
Laurence Lundbladecc7da412020-12-27 00:09:07 -08006578 {
6579 -75008: h'05083399',
6580 88: [],
6581 100100: {
6582 "sub1": {
6583 10: [
6584 0
6585 ],
6586 -75009: h'A46823990001',
6587 100100: {
6588 "json": "{ \"ueid\", \"xyz\"}",
6589 "subsub": {
6590 100002: h'141813191001'
6591 }
6592 }
Laurence Lundbladeddebabe2020-11-22 11:32:17 -08006593 }
Laurence Lundbladecc7da412020-12-27 00:09:07 -08006594 }
Laurence Lundbladeddebabe2020-11-22 11:32:17 -08006595 }
Laurence Lundbladeddebabe2020-11-22 11:32:17 -08006596 */
6597
6598static const uint8_t spNestedCBOR[] = {
Laurence Lundbladecc7da412020-12-27 00:09:07 -08006599 0xa3, 0x3a, 0x00, 0x01, 0x24, 0xff, 0x44, 0x05,
6600 0x08, 0x33, 0x99, 0x18, 0x58, 0x80, 0x1a, 0x00,
6601 0x01, 0x87, 0x04, 0xa1, 0x64, 0x73, 0x75, 0x62,
6602 0x31, 0xa3, 0x0a, 0x81, 0x00, 0x3a, 0x00, 0x01,
6603 0x25, 0x00, 0x46, 0xa4, 0x68, 0x23, 0x99, 0x00,
6604 0x01, 0x1a, 0x00, 0x01, 0x87, 0x04, 0xa2, 0x64,
6605 0x6a, 0x73, 0x6f, 0x6e, 0x70, 0x7b, 0x20, 0x22,
6606 0x75, 0x65, 0x69, 0x64, 0x22, 0x2c, 0x20, 0x22,
6607 0x78, 0x79, 0x7a, 0x22, 0x7d, 0x66, 0x73, 0x75,
6608 0x62, 0x73, 0x75, 0x62, 0xa1, 0x1a, 0x00, 0x01,
6609 0x86, 0xa2, 0x46, 0x14, 0x18, 0x13, 0x19, 0x10,
6610 0x01
Laurence Lundbladeddebabe2020-11-22 11:32:17 -08006611};
6612
6613/* Get item in multi-level nesting in spNestedCBOR */
6614static int32_t DecodeNestedGetSubSub(QCBORDecodeContext *pDCtx)
6615{
6616 UsefulBufC String;
6617
6618 uint8_t test_oemid_bytes[] = {0x14, 0x18, 0x13, 0x19, 0x10, 0x01};
6619 const struct q_useful_buf_c test_oemid = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(test_oemid_bytes);
6620
6621 QCBORDecode_EnterMapFromMapN(pDCtx, 100100);
6622 QCBORDecode_EnterMap(pDCtx, NULL);
6623 QCBORDecode_EnterMapFromMapN(pDCtx, 100100);
6624 QCBORDecode_EnterMapFromMapSZ(pDCtx, "subsub");
6625 QCBORDecode_GetByteStringInMapN(pDCtx, 100002, &String);
6626 if(QCBORDecode_GetError(pDCtx)) {
6627 return 4001;
6628 }
6629 if(UsefulBuf_Compare(String, test_oemid)) {
6630 return 4002;
6631 }
6632 QCBORDecode_ExitMap(pDCtx);
6633 QCBORDecode_ExitMap(pDCtx);
6634 QCBORDecode_ExitMap(pDCtx);
6635 QCBORDecode_ExitMap(pDCtx);
6636
6637 return 0;
6638}
6639
6640/* Iterations on the zero-length array in spNestedCBOR */
6641static int32_t DecodeNestedGetEmpty(QCBORDecodeContext *pDCtx)
6642{
6643 QCBORItem Item;
6644 QCBORError uErr;
6645
6646 QCBORDecode_EnterArrayFromMapN(pDCtx, 88);
6647 for(int x = 0; x < 20; x++) {
6648 uErr = QCBORDecode_GetNext(pDCtx, &Item);
6649 if(uErr != QCBOR_ERR_NO_MORE_ITEMS) {
6650 return 4100;
6651
6652 }
6653 }
6654 QCBORDecode_ExitArray(pDCtx);
6655 if(QCBORDecode_GetError(pDCtx)) {
6656 return 4101;
6657 }
6658
6659 return 0;
6660}
6661
6662/* Various iterations on the array that contains a zero in spNestedCBOR */
6663static int32_t DecodeNestedGetZero(QCBORDecodeContext *pDCtx)
6664{
6665 QCBORError uErr;
6666
6667 QCBORDecode_EnterMapFromMapN(pDCtx, 100100);
6668 QCBORDecode_EnterMapFromMapSZ(pDCtx, "sub1");
6669 QCBORDecode_EnterArrayFromMapN(pDCtx, 10);
6670 int64_t nInt = 99;
6671 QCBORDecode_GetInt64(pDCtx, &nInt);
6672 if(nInt != 0) {
6673 return 4200;
6674 }
6675 for(int x = 0; x < 20; x++) {
6676 QCBORItem Item;
6677 uErr = QCBORDecode_GetNext(pDCtx, &Item);
6678 if(uErr != QCBOR_ERR_NO_MORE_ITEMS) {
6679 return 4201;
6680
6681 }
6682 }
6683 QCBORDecode_ExitArray(pDCtx);
6684 if(QCBORDecode_GetAndResetError(pDCtx)) {
6685 return 4202;
6686 }
6687 QCBORDecode_EnterArrayFromMapN(pDCtx, 10);
6688 UsefulBufC dD;
6689 QCBORDecode_GetByteString(pDCtx, &dD);
6690 if(QCBORDecode_GetAndResetError(pDCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
6691 return 4203;
6692 }
6693 for(int x = 0; x < 20; x++) {
6694 QCBORDecode_GetByteString(pDCtx, &dD);
6695 uErr = QCBORDecode_GetAndResetError(pDCtx);
6696 if(uErr != QCBOR_ERR_NO_MORE_ITEMS) {
6697 return 4204;
6698 }
6699 }
6700 QCBORDecode_ExitArray(pDCtx);
6701 QCBORDecode_ExitMap(pDCtx);
6702 QCBORDecode_ExitMap(pDCtx);
6703
6704 return 0;
6705}
6706
6707/* Repeatedly enter and exit maps and arrays, go off the end of maps
6708 and arrays and such. */
Laurence Lundbladeb9702452021-03-08 21:02:57 -08006709static int32_t DecodeNestedIterate(void)
Laurence Lundbladeddebabe2020-11-22 11:32:17 -08006710{
6711 QCBORDecodeContext DCtx;
6712 int32_t nReturn;
6713 QCBORError uErr;
6714
6715 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spNestedCBOR), 0);
6716 QCBORDecode_EnterMap(&DCtx, NULL);
6717
6718 for(int j = 0; j < 5; j++) {
6719 for(int i = 0; i < 20; i++) {
6720 nReturn = DecodeNestedGetSubSub(&DCtx);
6721 if(nReturn) {
6722 return nReturn;
6723 }
6724 }
6725
6726 for(int i = 0; i < 20; i++) {
6727 nReturn = DecodeNestedGetEmpty(&DCtx);
6728 if(nReturn ) {
6729 return nReturn;
6730 }
6731 }
6732
6733 for(int i = 0; i < 20; i++) {
6734 nReturn = DecodeNestedGetZero(&DCtx);
6735 if(nReturn ) {
6736 return nReturn;
6737 }
6738 }
6739 }
6740
6741 QCBORDecode_ExitMap(&DCtx);
6742 uErr = QCBORDecode_Finish(&DCtx);
6743 if(uErr) {
6744 return (int32_t)uErr + 4100;
6745 }
6746
6747 return 0;
6748}
6749
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07006750
6751/*
Laurence Lundbladecc7da412020-12-27 00:09:07 -08006752 [
6753 23,
6754 6000,
6755 h'67616C6163746963',
6756 h'686176656E20746F6B656E'
6757 ]
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07006758 */
6759static const uint8_t spSimpleArray[] = {
Laurence Lundblade9b334962020-08-27 10:55:53 -07006760 0x84,
6761 0x17,
6762 0x19, 0x17, 0x70,
6763 0x48, 0x67, 0x61, 0x6C, 0x61, 0x63, 0x74, 0x69, 0x63,
6764 0x4B, 0x68, 0x61, 0x76, 0x65, 0x6E, 0x20, 0x74, 0x6F, 0x6B, 0x65, 0x6E};
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07006765
Laurence Lundbladecc7da412020-12-27 00:09:07 -08006766/* [h'', {}, [], 0] */
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08006767static const uint8_t spArrayOfEmpty[] = {0x84, 0x40, 0xa0, 0x80, 0x00};
6768
Laurence Lundbladecc7da412020-12-27 00:09:07 -08006769/* {} */
Laurence Lundbladee6f15112020-07-23 18:44:16 -07006770static const uint8_t spEmptyMap[] = {0xa0};
6771
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08006772#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundbladecc7da412020-12-27 00:09:07 -08006773/* {} */
Laurence Lundbladee6f15112020-07-23 18:44:16 -07006774static const uint8_t spEmptyInDefinteLengthMap[] = {0xbf, 0xff};
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07006775
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08006776
Laurence Lundbladef0499502020-08-01 11:55:57 -07006777/*
6778 {
6779 0: [],
6780 9: [
6781 [],
6782 []
6783 ],
6784 8: {
6785 1: [],
6786 2: {},
6787 3: []
6788 },
6789 4: {},
6790 5: [],
6791 6: [
6792 [],
6793 []
6794 ]
Laurence Lundbladecc7da412020-12-27 00:09:07 -08006795 }
Laurence Lundbladef0499502020-08-01 11:55:57 -07006796 */
6797static const uint8_t spMapOfEmpty[] = {
Laurence Lundbladecc7da412020-12-27 00:09:07 -08006798 0xa6, 0x00, 0x80, 0x09, 0x82, 0x80, 0x80, 0x08,
6799 0xa3, 0x01, 0x80, 0x02, 0xa0, 0x03, 0x80, 0x04,
6800 0xa0, 0x05, 0x9f, 0xff, 0x06, 0x9f, 0x80, 0x9f,
6801 0xff, 0xff};
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07006802
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08006803#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
6804
6805
Laurence Lundblade93d3f532020-09-28 21:09:12 -07006806/*
6807 Too many tags
Laurence Lundblade93d3f532020-09-28 21:09:12 -07006808 Duplicate label
6809 Integer overflow
6810 Date overflow
6811
Laurence Lundbladecc7da412020-12-27 00:09:07 -08006812 {
6813 1: 224(225(226(227(4(0))))),
Laurence Lundbladecc7da412020-12-27 00:09:07 -08006814 3: -18446744073709551616,
6815 4: 1(1.0e+300),
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07006816 5: 0,
6817 8: 8
Laurence Lundbladecc7da412020-12-27 00:09:07 -08006818 }
Laurence Lundblade93d3f532020-09-28 21:09:12 -07006819 */
6820static const uint8_t spRecoverableMapErrors[] = {
Laurence Lundblade37286c02022-09-03 10:05:02 -07006821#ifndef QCBOR_DISABLE_TAGS
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07006822 0xa6,
Laurence Lundblade93d3f532020-09-28 21:09:12 -07006823 0x04, 0xc1, 0xfb, 0x7e, 0x37, 0xe4, 0x3c, 0x88, 0x00, 0x75, 0x9c,
Laurence Lundblade37286c02022-09-03 10:05:02 -07006824 0x01, 0xd8, 0xe0, 0xd8, 0xe1, 0xd8, 0xe2, 0xd8, 0xe3, 0xd8, 0x04, 0x00,
6825#else
6826 0xa4,
6827#endif
6828 0x03, 0x3b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
Laurence Lundblade93d3f532020-09-28 21:09:12 -07006829 0x05, 0x00,
6830 0x05, 0x00,
6831 0x08, 0x08,
Laurence Lundblade93d3f532020-09-28 21:09:12 -07006832};
6833
Laurence Lundbladecc7da412020-12-27 00:09:07 -08006834/* Bad break */
Laurence Lundblade93d3f532020-09-28 21:09:12 -07006835static const uint8_t spUnRecoverableMapError1[] = {
6836 0xa2, 0xff, 0x01, 0x00, 0x02, 0x00
6837};
6838
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08006839#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundbladecc7da412020-12-27 00:09:07 -08006840/* No more items */
Laurence Lundblade93d3f532020-09-28 21:09:12 -07006841static const uint8_t spUnRecoverableMapError2[] = {
6842 0xbf, 0x02, 0xbf, 0xff, 0x01, 0x00, 0x02, 0x00
6843};
6844
Laurence Lundbladecc7da412020-12-27 00:09:07 -08006845/* Hit end because string is too long */
Laurence Lundblade93d3f532020-09-28 21:09:12 -07006846static const uint8_t spUnRecoverableMapError3[] = {
6847 0xbf, 0x02, 0x69, 0x64, 0x64, 0xff
6848};
6849
Laurence Lundbladecc7da412020-12-27 00:09:07 -08006850/* Hit end because string is too long */
Laurence Lundblade93d3f532020-09-28 21:09:12 -07006851static const uint8_t spUnRecoverableMapError4[] = {
6852 0xbf,
6853 0x02, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f,
6854 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f,
6855 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
6856 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
6857 0xff
6858};
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08006859#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
Laurence Lundblade93d3f532020-09-28 21:09:12 -07006860
Laurence Lundblade63926052021-03-29 16:05:51 -07006861const unsigned char not_well_formed_submod_section[] = {
6862 0xa1, 0x14, 0x1f,
6863};
Laurence Lundblade93d3f532020-09-28 21:09:12 -07006864
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07006865
6866/* Array of length 3, but only two items. */
6867const unsigned char spBadConsumeInput[] = {
6868 0x83, 0x00, 0x00
6869};
6870
6871/* Tag nesting too deep. */
6872const unsigned char spBadConsumeInput2[] = {
6873 0x81,
6874 0xD8, 0x37,
6875 0xD8, 0x2C,
6876 0xD8, 0x21,
6877 0xD6,
6878 0xCB,
6879 00
6880};
6881
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07006882
6883const unsigned char spBadConsumeInput4[] = {
6884 0x81, 0x9f, 0x00, 0xff
6885};
6886
6887const unsigned char spBadConsumeInput5[] = {
6888 0xa1, 0x80, 0x00
6889};
6890
Laurence Lundbladec5f45e42023-12-18 09:23:20 -07006891/*
6892 Lots of nesting for various nesting tests.
6893 { 1:1,
6894 2:{
6895 21:21,
6896 22:{
6897 221:[2111, 2112, 2113],
6898 222:222,
6899 223: {}
6900 },
6901 23: 23
6902 },
6903 3:3,
6904 4: [ {} ]
6905 }
6906 */
6907static const uint8_t spNested[] = {
69080xA4, /* Map of 4 */
6909 0x01, 0x01, /* Map entry 1 : 1 */
6910 0x02, 0xA3, /* Map entry 2 : {, an array of 3 */
6911 0x15, 0x15, /* Map entry 21 : 21 */
6912 0x16, 0xA3, /* Map entry 22 : {, a map of 3 */
6913 0x18, 0xDD, 0x83, /* Map entry 221 : [ an array of 3 */
6914 0x19, 0x08, 0x3F, /* Array item 2111 */
6915 0x19, 0x08, 0x40, /* Array item 2112 */
6916 0x19, 0x08, 0x41, /* Array item 2113 */
6917 0x18, 0xDE, 0x18, 0xDE, /* Map entry 222 : 222 */
6918 0x18, 0xDF, 0xA0, /* Map entry 223 : {} */
6919 0x17, 0x17, /* Map entry 23 : 23 */
6920 0x03, 0x03, /* Map entry 3 : 3 */
6921 0x04, 0x81, /* Map entry 4: [, an array of 1 */
6922 0xA0 /* Array entry {}, an empty map */
6923};
6924
6925
6926static int32_t EnterMapCursorTest(void)
6927{
6928 QCBORDecodeContext DCtx;
6929 QCBORItem Item1;
Laurence Lundblade11654912024-05-09 11:49:24 -07006930 int64_t nInt;
6931 QCBORError uErr;
6932
6933 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spNested), 0);
6934 QCBORDecode_EnterMap(&DCtx, NULL);
6935 QCBORDecode_GetInt64InMapN (&DCtx, 3, &nInt);
6936 uErr = QCBORDecode_GetNext(&DCtx, &Item1);
Laurence Lundbladea29f45a2024-05-14 15:55:19 -07006937 if(uErr != QCBOR_SUCCESS) {
6938 return 701;
6939 }
Laurence Lundblade11654912024-05-09 11:49:24 -07006940 if(Item1.uDataType != QCBOR_TYPE_INT64) {
6941 return 700;
6942 }
6943
Laurence Lundbladec5f45e42023-12-18 09:23:20 -07006944
6945 int i;
6946 for(i = 0; i < 13; i++) {
6947 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spNested), 0);
6948 QCBORDecode_EnterMap(&DCtx, NULL);
6949 int j;
6950 /* Move travesal cursor */
6951 for(j = 0; j < i; j++) {
6952 QCBORDecode_GetNext(&DCtx, &Item1);
6953 }
6954 QCBORDecode_EnterMapFromMapN(&DCtx, 2);
6955 QCBORDecode_ExitMap(&DCtx);
6956 QCBORDecode_GetNext(&DCtx, &Item1);
6957 if(Item1.label.int64 != 3) {
6958 return 8000;
6959 }
6960 }
6961
6962 for(i = 0; i < 13; i++) {
6963 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spNested), 0);
6964 QCBORDecode_EnterMap(&DCtx, NULL);
6965 int j;
6966 /* Move travesal cursor */
6967 for(j = 0; j < i; j++) {
6968 QCBORDecode_GetNext(&DCtx, &Item1);
6969 }
6970 QCBORDecode_EnterMapFromMapN(&DCtx, 2);
6971 QCBORDecode_EnterMapFromMapN(&DCtx, 22);
6972 QCBORDecode_ExitMap(&DCtx);
6973 QCBORDecode_GetNext(&DCtx, &Item1);
6974 if(Item1.label.int64 != 23) {
6975 return 8000;
6976 }
6977 }
6978
6979 for(i = 0; i < 13; i++) {
6980 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spNested), 0);
6981 QCBORDecode_EnterMap(&DCtx, NULL);
6982 int j;
6983 /* Move travesal cursor */
6984 for(j = 0; j < i; j++) {
6985 QCBORDecode_GetNext(&DCtx, &Item1);
6986 }
6987 QCBORDecode_EnterMapFromMapN(&DCtx, 2);
6988 QCBORDecode_EnterMapFromMapN(&DCtx, 22);
6989 for(j = 0; j < i; j++) {
6990 QCBORDecode_GetNext(&DCtx, &Item1);
6991 }
6992 QCBORDecode_EnterArrayFromMapN(&DCtx, 221);
6993 QCBORDecode_ExitArray(&DCtx);
6994 QCBORDecode_ExitMap(&DCtx);
6995 QCBORDecode_GetNext(&DCtx, &Item1);
6996 if(Item1.label.int64 != 23) {
6997 return 8000;
6998 }
6999 QCBORDecode_ExitMap(&DCtx);
7000 QCBORDecode_GetNext(&DCtx, &Item1);
7001 if(Item1.label.int64 != 3) {
7002 return 8000;
7003 }
7004 }
7005
7006 return 0;
7007}
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07007008
7009
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03007010int32_t EnterMapTest(void)
Laurence Lundbladebb87be22020-04-09 19:15:32 -07007011{
Laurence Lundbladef0499502020-08-01 11:55:57 -07007012 QCBORItem Item1;
Laurence Lundbladebb87be22020-04-09 19:15:32 -07007013 QCBORDecodeContext DCtx;
Laurence Lundbladef0499502020-08-01 11:55:57 -07007014 int32_t nReturn;
7015 QCBORError uErr;
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07007016
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08007017#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07007018 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spMapOfEmpty), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07007019 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07007020
Laurence Lundbladef0499502020-08-01 11:55:57 -07007021
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07007022 QCBORDecode_EnterArray(&DCtx, NULL); // Label 0
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07007023 QCBORDecode_ExitArray(&DCtx);
7024
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07007025 QCBORDecode_EnterArray(&DCtx, NULL); // Label 9
7026 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07007027 QCBORDecode_ExitArray(&DCtx);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07007028 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07007029 QCBORDecode_ExitArray(&DCtx);
7030 QCBORDecode_ExitArray(&DCtx);
7031
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07007032 QCBORDecode_EnterMap(&DCtx, NULL); // Label 8
7033 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07007034 QCBORDecode_ExitArray(&DCtx);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07007035 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07007036 QCBORDecode_ExitMap(&DCtx);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07007037 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07007038 QCBORDecode_ExitArray(&DCtx);
7039 QCBORDecode_ExitMap(&DCtx);
7040
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07007041 QCBORDecode_EnterMap(&DCtx, NULL); // Label4
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07007042 QCBORDecode_ExitMap(&DCtx);
7043
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07007044 QCBORDecode_EnterArray(&DCtx, NULL); // Label 5
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07007045 QCBORDecode_ExitArray(&DCtx);
7046
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07007047 QCBORDecode_EnterArray(&DCtx, NULL); // Label 6
7048 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07007049 QCBORDecode_ExitArray(&DCtx);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07007050 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07007051 QCBORDecode_ExitArray(&DCtx);
7052 QCBORDecode_ExitArray(&DCtx);
7053
7054 QCBORDecode_ExitMap(&DCtx);
7055
7056 uErr = QCBORDecode_Finish(&DCtx);
7057 if(uErr != QCBOR_SUCCESS){
7058 return 3011;
7059 }
7060
Laurence Lundbladeec290b82024-06-10 11:10:54 -07007061#ifndef QCBOR_DISABLE_NON_INTEGER_LABELS
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07007062 (void)pValidMapIndefEncoded;
Laurence Lundbladef7a70bc2020-10-24 12:23:25 -07007063 nReturn = SpiffyDecodeBasicMap(UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapIndefEncoded));
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07007064 if(nReturn) {
7065 return nReturn + 20000;
Laurence Lundbladebb87be22020-04-09 19:15:32 -07007066 }
Laurence Lundbladeec290b82024-06-10 11:10:54 -07007067#endif /* ! QCBOR_DISABLE_NON_INTEGER_LABELS */
7068#endif /* ! QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08007069
Laurence Lundbladeec290b82024-06-10 11:10:54 -07007070#ifndef QCBOR_DISABLE_NON_INTEGER_LABELS
7071 QCBORItem ArrayItem;
Laurence Lundbladebb87be22020-04-09 19:15:32 -07007072
Laurence Lundbladef7a70bc2020-10-24 12:23:25 -07007073 nReturn = SpiffyDecodeBasicMap(UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded));
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07007074 if(nReturn) {
7075 return nReturn;
7076 }
Laurence Lundbladebb87be22020-04-09 19:15:32 -07007077
Laurence Lundblade8ffdb742020-05-07 02:49:18 -07007078
Laurence Lundblade937ea812020-05-08 11:38:23 -07007079
Laurence Lundblade2f467f92020-10-09 17:50:11 -07007080 // These tests confirm the cursor is at the right place after entering
7081 // a map or array
Laurence Lundblade9b334962020-08-27 10:55:53 -07007082 const UsefulBufC ValidEncodedMap = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded);
Laurence Lundblade937ea812020-05-08 11:38:23 -07007083
7084 // Confirm cursor is at right place
Laurence Lundblade9b334962020-08-27 10:55:53 -07007085 QCBORDecode_Init(&DCtx, ValidEncodedMap, 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07007086 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade937ea812020-05-08 11:38:23 -07007087 QCBORDecode_GetNext(&DCtx, &Item1);
7088 if(Item1.uDataType != QCBOR_TYPE_INT64) {
7089 return 2001;
7090 }
7091
7092
Laurence Lundblade9b334962020-08-27 10:55:53 -07007093 QCBORDecode_Init(&DCtx, ValidEncodedMap, 0);
Laurence Lundblade6f3f78e2020-08-31 13:09:14 -07007094 QCBORDecode_VGetNext(&DCtx, &Item1);
7095 QCBORDecode_VGetNext(&DCtx, &Item1);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07007096 QCBORDecode_EnterArray(&DCtx, &ArrayItem);
7097 if(ArrayItem.uLabelType != QCBOR_TYPE_TEXT_STRING ||
7098 UsefulBuf_Compare(ArrayItem.label.string,
7099 UsefulBuf_FROM_SZ_LITERAL("an array of two strings"))) {
7100 return 2051;
7101 }
Laurence Lundblade937ea812020-05-08 11:38:23 -07007102 QCBORDecode_GetNext(&DCtx, &Item1);
7103 if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING) {
7104 return 2002;
7105 }
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07007106 QCBORDecode_ExitArray(&DCtx);
7107 QCBORDecode_EnterMap(&DCtx, &ArrayItem);
7108 if(ArrayItem.uLabelType != QCBOR_TYPE_TEXT_STRING ||
7109 UsefulBuf_Compare(ArrayItem.label.string,
7110 UsefulBuf_FROM_SZ_LITERAL("map in a map"))) {
7111 return 2052;
7112 }
7113
Laurence Lundblade937ea812020-05-08 11:38:23 -07007114
Laurence Lundblade9b334962020-08-27 10:55:53 -07007115 QCBORDecode_Init(&DCtx, ValidEncodedMap, 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07007116 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade64b607e2020-05-13 13:05:57 -07007117 QCBORDecode_GetNext(&DCtx, &Item1);
7118 QCBORDecode_GetNext(&DCtx, &Item1);
7119 QCBORDecode_GetNext(&DCtx, &Item1);
Laurence Lundblade937ea812020-05-08 11:38:23 -07007120 QCBORDecode_EnterMapFromMapSZ(&DCtx, "map in a map");
7121 QCBORDecode_GetNext(&DCtx, &Item1);
7122 if(Item1.uDataType != QCBOR_TYPE_BYTE_STRING) {
7123 return 2003;
7124 }
7125
Laurence Lundblade9b334962020-08-27 10:55:53 -07007126 QCBORDecode_Init(&DCtx, ValidEncodedMap, 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07007127 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade937ea812020-05-08 11:38:23 -07007128 QCBORDecode_GetNext(&DCtx, &Item1);
7129 QCBORDecode_GetNext(&DCtx, &Item1);
7130 QCBORDecode_GetNext(&DCtx, &Item1);
7131 QCBORDecode_GetNext(&DCtx, &Item1);
7132 QCBORDecode_GetNext(&DCtx, &Item1);
7133 QCBORDecode_GetNext(&DCtx, &Item1);
7134 QCBORDecode_GetNext(&DCtx, &Item1);
7135 QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings");
7136 QCBORDecode_GetNext(&DCtx, &Item1);
7137 if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING) {
Laurence Lundblade64b607e2020-05-13 13:05:57 -07007138 return 2004;
Laurence Lundblade937ea812020-05-08 11:38:23 -07007139 }
7140
Laurence Lundblade9b334962020-08-27 10:55:53 -07007141 QCBORDecode_Init(&DCtx, ValidEncodedMap, 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07007142 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade2b843b52020-06-16 20:51:03 -07007143 QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings");
7144 QCBORDecode_ExitArray(&DCtx);
7145 QCBORDecode_GetNext(&DCtx, &Item1);
7146 if(Item1.uDataType != QCBOR_TYPE_MAP && Item1.uLabelAlloc != QCBOR_TYPE_TEXT_STRING) {
7147 return 2006;
7148 }
7149 QCBORDecode_ExitMap(&DCtx);
7150 if(QCBORDecode_GetNext(&DCtx, &Item1) != QCBOR_ERR_NO_MORE_ITEMS) {
7151 return 2007;
7152 }
Laurence Lundbladeec290b82024-06-10 11:10:54 -07007153#endif /* !QCBOR_DISABLE_NON_INTEGER_LABELS */
Laurence Lundblade2b843b52020-06-16 20:51:03 -07007154
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07007155 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSimpleArray), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07007156 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07007157 int64_t nDecodedInt2;
Laurence Lundbladeec290b82024-06-10 11:10:54 -07007158
7159 UsefulBufC String;
7160 QCBORDecode_GetTextStringInMapN(&DCtx, 88, &String);
7161 uErr = QCBORDecode_GetAndResetError(&DCtx);
7162 if(uErr != QCBOR_ERR_MAP_NOT_ENTERED){
7163 return 2009;
7164 }
7165#ifndef QCBOR_DISABLE_NON_INTEGER_LABELS
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07007166 QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2);
7167 uErr = QCBORDecode_GetAndResetError(&DCtx);
7168 if(uErr != QCBOR_ERR_MAP_NOT_ENTERED){
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07007169 return 2008;
7170 }
Laurence Lundbladeec290b82024-06-10 11:10:54 -07007171#endif /* ! QCBOR_DISABLE_NON_INTEGER_LABELS */
Laurence Lundblade937ea812020-05-08 11:38:23 -07007172
Laurence Lundbladee6f15112020-07-23 18:44:16 -07007173
7174 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEmptyMap), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07007175 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundbladee6f15112020-07-23 18:44:16 -07007176 // This will fail because the map is empty.
7177 QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2);
7178 uErr = QCBORDecode_GetAndResetError(&DCtx);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07007179 if(uErr != QCBOR_ERR_LABEL_NOT_FOUND){
Laurence Lundbladee6f15112020-07-23 18:44:16 -07007180 return 2010;
7181 }
7182 QCBORDecode_ExitMap(&DCtx);
7183 uErr = QCBORDecode_Finish(&DCtx);
7184 if(uErr != QCBOR_SUCCESS){
7185 return 2011;
7186 }
7187
7188
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08007189#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundbladee6f15112020-07-23 18:44:16 -07007190 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEmptyInDefinteLengthMap), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07007191 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundbladee6f15112020-07-23 18:44:16 -07007192 // This will fail because the map is empty.
7193 QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2);
7194 uErr = QCBORDecode_GetAndResetError(&DCtx);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07007195 if(uErr != QCBOR_ERR_LABEL_NOT_FOUND){
Laurence Lundblade085d7952020-07-24 10:26:30 -07007196 return 2012;
Laurence Lundbladee6f15112020-07-23 18:44:16 -07007197 }
7198 QCBORDecode_ExitMap(&DCtx);
7199 uErr = QCBORDecode_Finish(&DCtx);
7200 if(uErr != QCBOR_SUCCESS){
Laurence Lundblade085d7952020-07-24 10:26:30 -07007201 return 2013;
Laurence Lundbladee6f15112020-07-23 18:44:16 -07007202 }
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08007203#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
Laurence Lundbladee6f15112020-07-23 18:44:16 -07007204
Laurence Lundblade5f4e8712020-07-25 11:44:43 -07007205
7206 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spArrayOfEmpty), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07007207 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade323f8a92020-09-06 19:43:09 -07007208 QCBORDecode_GetByteString(&DCtx, &String);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07007209 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade5f4e8712020-07-25 11:44:43 -07007210 QCBORDecode_ExitMap(&DCtx);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07007211 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade5f4e8712020-07-25 11:44:43 -07007212 QCBORDecode_ExitArray(&DCtx);
7213 QCBORDecode_GetInt64(&DCtx, &nDecodedInt2);
7214 QCBORDecode_ExitArray(&DCtx);
7215 uErr = QCBORDecode_Finish(&DCtx);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07007216 if(uErr != QCBOR_SUCCESS) {
Laurence Lundblade5f4e8712020-07-25 11:44:43 -07007217 return 2014;
7218 }
7219
Laurence Lundblade93d3f532020-09-28 21:09:12 -07007220 int64_t nInt;
7221 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spRecoverableMapErrors), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07007222 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade37286c02022-09-03 10:05:02 -07007223#ifndef QCBOR_DISABLE_TAGS
Laurence Lundblade93d3f532020-09-28 21:09:12 -07007224 QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt);
Laurence Lundblade88e9db22020-11-02 03:56:33 -08007225 uErr = QCBORDecode_GetError(&DCtx);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07007226 if(uErr != QCBOR_ERR_TOO_MANY_TAGS) {
7227 return 2021;
7228 }
Laurence Lundblade88e9db22020-11-02 03:56:33 -08007229 if(QCBORDecode_GetNthTagOfLast(&DCtx, 0) != CBOR_TAG_INVALID64) {
7230 return 2121;
7231 }
7232 (void)QCBORDecode_GetAndResetError(&DCtx);
Laurence Lundblade37286c02022-09-03 10:05:02 -07007233#endif
Laurence Lundblade88e9db22020-11-02 03:56:33 -08007234
Laurence Lundblade93d3f532020-09-28 21:09:12 -07007235
Laurence Lundblade93d3f532020-09-28 21:09:12 -07007236 QCBORDecode_GetInt64InMapN(&DCtx, 0x03, &nInt);
7237 uErr = QCBORDecode_GetAndResetError(&DCtx);
7238 if(uErr != QCBOR_ERR_INT_OVERFLOW) {
7239 return 2023;
7240 }
7241
Laurence Lundblade37286c02022-09-03 10:05:02 -07007242#ifndef QCBOR_DISABLE_TAGS
Laurence Lundblade93d3f532020-09-28 21:09:12 -07007243 QCBORDecode_GetEpochDateInMapN(&DCtx, 0x04, QCBOR_TAG_REQUIREMENT_TAG, &nInt);
7244 uErr = QCBORDecode_GetAndResetError(&DCtx);
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007245 if(uErr != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_DATE_OVERFLOW)) {
Laurence Lundblade93d3f532020-09-28 21:09:12 -07007246 return 2024;
7247 }
Laurence Lundblade37286c02022-09-03 10:05:02 -07007248#endif
Laurence Lundblade93d3f532020-09-28 21:09:12 -07007249
7250 QCBORDecode_GetInt64InMapN(&DCtx, 0x05, &nInt);
7251 uErr = QCBORDecode_GetAndResetError(&DCtx);
7252 if(uErr != QCBOR_ERR_DUPLICATE_LABEL) {
7253 return 2025;
7254 }
7255
7256 QCBORDecode_GetInt64InMapN(&DCtx, 0x08, &nInt);
7257
7258 QCBORDecode_ExitMap(&DCtx);
7259 uErr = QCBORDecode_Finish(&DCtx);
7260 if(uErr != QCBOR_SUCCESS) {
7261 return 2026;
7262 }
7263
7264 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUnRecoverableMapError1), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07007265 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07007266 QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt);
7267 uErr = QCBORDecode_GetAndResetError(&DCtx);
7268 if(uErr != QCBOR_ERR_BAD_BREAK) {
7269 return 2030;
7270 }
7271
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08007272#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundblade93d3f532020-09-28 21:09:12 -07007273 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUnRecoverableMapError2), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07007274 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07007275 QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt);
7276 uErr = QCBORDecode_GetAndResetError(&DCtx);
7277 if(uErr != QCBOR_ERR_NO_MORE_ITEMS) {
7278 return 2031;
7279 }
7280
7281 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUnRecoverableMapError3), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07007282 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07007283 QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt);
7284 uErr = QCBORDecode_GetAndResetError(&DCtx);
7285 if(uErr != QCBOR_ERR_HIT_END) {
7286 return 2032;
7287 }
7288
7289 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUnRecoverableMapError4), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07007290 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07007291 QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt);
7292 uErr = QCBORDecode_GetAndResetError(&DCtx);
7293 if(uErr != QCBOR_ERR_ARRAY_DECODE_NESTING_TOO_DEEP) {
7294 return 2033;
7295 }
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08007296#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
7297
Laurence Lundbladeec290b82024-06-10 11:10:54 -07007298#ifndef QCBOR_DISABLE_NON_INTEGER_LABELS
Laurence Lundblade732e52d2021-02-22 20:11:01 -07007299 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0);
7300 QCBORDecode_VGetNextConsume(&DCtx, &Item1);
7301 if(Item1.uDataType != QCBOR_TYPE_MAP) {
7302 return 2401;
7303 }
7304 if(QCBORDecode_GetError(&DCtx)) {
7305 return 2402;
7306 }
7307
7308 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0);
7309 QCBORDecode_VGetNext(&DCtx, &Item1);
7310 if(Item1.uDataType != QCBOR_TYPE_MAP ||
7311 Item1.val.uCount != 3 ||
7312 Item1.uNextNestLevel != 1) {
7313 return 2403;
7314 }
7315 if(QCBORDecode_GetError(&DCtx)) {
7316 return 2404;
7317 }
7318 QCBORDecode_VGetNextConsume(&DCtx, &Item1);
7319 if(Item1.uDataType != QCBOR_TYPE_INT64 ||
7320 Item1.uNextNestLevel != 1 ||
7321 Item1.val.int64 != 42) {
7322 return 2405;
7323 }
7324 if(QCBORDecode_GetError(&DCtx)) {
7325 return 2406;
7326 }
7327 QCBORDecode_VGetNextConsume(&DCtx, &Item1);
7328 if(Item1.uDataType != QCBOR_TYPE_ARRAY ||
7329 Item1.uNestingLevel != 1 ||
7330 Item1.uNextNestLevel != 1 ||
7331 Item1.val.uCount != 2) {
7332 return 2407;
7333 }
7334 if(QCBORDecode_GetError(&DCtx)) {
7335 return 2408;
7336 }
7337 QCBORDecode_VGetNextConsume(&DCtx, &Item1);
7338 if(Item1.uDataType != QCBOR_TYPE_MAP ||
7339 Item1.uNestingLevel != 1 ||
7340 Item1.uNextNestLevel != 0 ||
7341 Item1.val.uCount != 4) {
7342 return 2409;
7343 }
7344 if(QCBORDecode_GetError(&DCtx)) {
7345 return 2410;
7346 }
Laurence Lundbladeec290b82024-06-10 11:10:54 -07007347#endif /* ! QCBOR_DISABLE_NON_INTEGER_LABELS */
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07007348
Laurence Lundbladeddebabe2020-11-22 11:32:17 -08007349 nReturn = DecodeNestedIterate();
7350
Laurence Lundblade63926052021-03-29 16:05:51 -07007351
7352 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(not_well_formed_submod_section), 0);
7353 QCBORDecode_EnterMap(&DCtx, NULL);
7354 QCBORDecode_EnterMapFromMapN(&DCtx, 20);
7355 if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_BAD_INT) {
7356 return 2500;
7357 }
7358
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07007359 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBadConsumeInput), 0);
7360 QCBORDecode_VGetNextConsume(&DCtx, &Item1);
7361 if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_NO_MORE_ITEMS) {
7362 return 2600;
7363 }
7364
Laurence Lundblade37286c02022-09-03 10:05:02 -07007365#ifndef QCBOR_DISABLE_TAGS
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07007366 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBadConsumeInput2), 0);
7367 QCBORDecode_VGetNextConsume(&DCtx, &Item1);
7368 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) {
7369 return 2700;
7370 }
Laurence Lundblade37286c02022-09-03 10:05:02 -07007371#endif
7372
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07007373
7374 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBadConsumeInput4), 0);
7375 QCBORDecode_VGetNextConsume(&DCtx, &Item1);
7376#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
7377 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) {
7378 return 2900;
7379 }
7380#else
7381 if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_INDEF_LEN_ARRAYS_DISABLED) {
7382 return 2901;
7383 }
7384#endif
7385
7386 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBadConsumeInput5), 0);
7387 QCBORDecode_VGetNextConsume(&DCtx, &Item1);
7388 if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_MAP_LABEL_TYPE) {
7389 return 3000;
7390 }
7391
Laurence Lundbladec5f45e42023-12-18 09:23:20 -07007392 nReturn = EnterMapCursorTest();
7393
Laurence Lundbladeddebabe2020-11-22 11:32:17 -08007394 return nReturn;
Laurence Lundblade9c905e82020-04-25 11:31:38 -07007395}
7396
7397
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007398struct NumberConversion {
7399 char *szDescription;
7400 UsefulBufC CBOR;
7401 int64_t nConvertedToInt64;
7402 QCBORError uErrorInt64;
7403 uint64_t uConvertToUInt64;
7404 QCBORError uErrorUint64;
7405 double dConvertToDouble;
7406 QCBORError uErrorDouble;
7407};
7408
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007409#ifndef QCBOR_DISABLE_EXP_AND_MANTISSA
7410#define EXP_AND_MANTISSA_ERROR(x) x
7411#else
7412#define EXP_AND_MANTISSA_ERROR(x) QCBOR_ERR_UNEXPECTED_TYPE
7413#endif
7414
7415
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07007416static const struct NumberConversion NumberConversions[] = {
Laurence Lundblade37286c02022-09-03 10:05:02 -07007417#ifndef QCBOR_DISABLE_TAGS
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007418 {
Laurence Lundblade4e808ba2022-12-29 12:45:20 -07007419 "Big float: INT64_MIN * 2e-1 to test handling of INT64_MIN",
7420 {(uint8_t[]){0xC5, 0x82, 0x20,
7421 0x3B, 0x7f, 0xff, 0xff, 0xff, 0xff, 0x0ff, 0xff, 0xff,
7422 }, 15},
7423 -4611686018427387904, /* INT64_MIN / 2 */
7424 EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS),
7425 0,
7426 EXP_AND_MANTISSA_ERROR(QCBOR_ERR_NUMBER_SIGN_CONVERSION),
7427 -4.6116860184273879E+18,
7428 FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS))
7429 },
7430 {
Laurence Lundblade784b54b2020-08-10 01:24:52 -07007431 "too large to fit into int64_t",
7432 {(uint8_t[]){0xc3, 0x48, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 10},
7433 0,
7434 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
7435 0,
7436 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
7437 ((double)INT64_MIN) + 1 ,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007438 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)
Laurence Lundblade784b54b2020-08-10 01:24:52 -07007439 },
7440 {
7441 "largest negative int that fits in int64_t",
7442 {(uint8_t[]){0xc3, 0x48, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, 10},
7443 INT64_MIN,
7444 QCBOR_SUCCESS,
7445 0,
7446 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
7447 (double)INT64_MIN,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007448 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)
Laurence Lundblade784b54b2020-08-10 01:24:52 -07007449 },
7450 {
Laurence Lundbladeda095972020-06-06 18:35:33 -07007451 "negative bignum -1",
7452 {(uint8_t[]){0xc3, 0x41, 0x00}, 3},
7453 -1,
7454 QCBOR_SUCCESS,
7455 0,
7456 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
7457 -1.0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007458 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)
Laurence Lundbladeda095972020-06-06 18:35:33 -07007459 },
7460 {
Laurence Lundblade9ab5abb2020-05-20 12:10:45 -07007461 "Decimal Fraction with positive bignum 257 * 10e3",
Laurence Lundblade887add82020-05-17 05:50:34 -07007462 {(uint8_t[]){0xC4, 0x82, 0x1B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03,
7463 0xC2, 0x42, 0x01, 0x01}, 15},
7464 257000,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007465 EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS),
Laurence Lundblade887add82020-05-17 05:50:34 -07007466 257000,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007467 EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS),
Laurence Lundblade887add82020-05-17 05:50:34 -07007468 257000.0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007469 FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS))
Laurence Lundblade887add82020-05-17 05:50:34 -07007470 },
7471 {
Laurence Lundbladeda095972020-06-06 18:35:33 -07007472 "bigfloat with negative bignum -258 * 2e3",
Laurence Lundblade887add82020-05-17 05:50:34 -07007473 {(uint8_t[]){0xC5, 0x82, 0x1B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03,
7474 0xC3, 0x42, 0x01, 0x01}, 15},
Laurence Lundbladeda095972020-06-06 18:35:33 -07007475 -2064,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007476 EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS),
Laurence Lundblade887add82020-05-17 05:50:34 -07007477 0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007478 EXP_AND_MANTISSA_ERROR(QCBOR_ERR_NUMBER_SIGN_CONVERSION),
Laurence Lundbladeda095972020-06-06 18:35:33 -07007479 -2064.0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007480 FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS))
Laurence Lundblade887add82020-05-17 05:50:34 -07007481 },
7482 {
Laurence Lundblade9ab5abb2020-05-20 12:10:45 -07007483 "bigfloat with positive bignum 257 * 2e3",
Laurence Lundblade887add82020-05-17 05:50:34 -07007484 {(uint8_t[]){0xC5, 0x82, 0x1B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03,
7485 0xC2, 0x42, 0x01, 0x01}, 15},
7486 2056,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007487 EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS),
Laurence Lundblade887add82020-05-17 05:50:34 -07007488 2056,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007489 EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS),
Laurence Lundblade887add82020-05-17 05:50:34 -07007490 2056.0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007491 FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS))
Laurence Lundblade887add82020-05-17 05:50:34 -07007492 },
7493 {
Laurence Lundbladeda095972020-06-06 18:35:33 -07007494 "negative bignum 0xc349010000000000000000 -18446744073709551617",
Laurence Lundblade887add82020-05-17 05:50:34 -07007495 {(uint8_t[]){0xc3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 11},
7496 0,
7497 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
7498 0,
7499 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
7500 -18446744073709551617.0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007501 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)
Laurence Lundblade887add82020-05-17 05:50:34 -07007502 },
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08007503#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
Laurence Lundblade887add82020-05-17 05:50:34 -07007504 {
Laurence Lundblade313b2862020-05-16 01:23:06 -07007505 "Positive bignum 0x01020304 indefinite length string",
7506 {(uint8_t[]){0xC2, 0x5f, 0x42, 0x01, 0x02, 0x41, 0x03, 0x41, 0x04, 0xff}, 10},
7507 0x01020304,
7508 QCBOR_SUCCESS,
7509 0x01020304,
7510 QCBOR_SUCCESS,
7511 16909060.0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007512 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)
Laurence Lundblade313b2862020-05-16 01:23:06 -07007513 },
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08007514#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
Laurence Lundblade313b2862020-05-16 01:23:06 -07007515 {
Laurence Lundblade887add82020-05-17 05:50:34 -07007516 "Decimal Fraction with neg bignum [9223372036854775807, -4759477275222530853137]",
Laurence Lundblade313b2862020-05-16 01:23:06 -07007517 {(uint8_t[]){0xC4, 0x82, 0x1B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
7518 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,}, 23},
7519 0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007520 EXP_AND_MANTISSA_ERROR(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW),
Laurence Lundblade313b2862020-05-16 01:23:06 -07007521 0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007522 EXP_AND_MANTISSA_ERROR(QCBOR_ERR_NUMBER_SIGN_CONVERSION),
Laurence Lundblade313b2862020-05-16 01:23:06 -07007523 -INFINITY,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007524 FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS))
Laurence Lundblade313b2862020-05-16 01:23:06 -07007525 },
7526 {
7527 "big float [9223372036854775806, 9223372036854775806]",
7528 {(uint8_t[]){0xC5, 0x82, 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE,
7529 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE}, 20},
7530 0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007531 EXP_AND_MANTISSA_ERROR(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW),
Laurence Lundblade313b2862020-05-16 01:23:06 -07007532 0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007533 EXP_AND_MANTISSA_ERROR(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW),
Laurence Lundblade313b2862020-05-16 01:23:06 -07007534 INFINITY,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007535 FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS))
Laurence Lundblade313b2862020-05-16 01:23:06 -07007536 },
7537 {
Laurence Lundblade983500d2020-05-14 11:49:34 -07007538 "Big float 3 * 2^^2",
7539 {(uint8_t[]){0xC5, 0x82, 0x02, 0x03}, 4},
7540 12,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007541 EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS),
Laurence Lundblade983500d2020-05-14 11:49:34 -07007542 12,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007543 EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS),
Laurence Lundblade983500d2020-05-14 11:49:34 -07007544 12.0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007545 FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS))
Laurence Lundblade983500d2020-05-14 11:49:34 -07007546 },
Laurence Lundblade983500d2020-05-14 11:49:34 -07007547 {
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007548 "Decimal fraction 3/10",
7549 {(uint8_t[]){0xC4, 0x82, 0x20, 0x03}, 4},
7550 0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007551 EXP_AND_MANTISSA_ERROR(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW),
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007552 0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007553 EXP_AND_MANTISSA_ERROR(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW),
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007554 0.30000000000000004,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007555 FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS))
Laurence Lundbladedfd49fc2020-09-01 14:17:16 -07007556 },
7557 {
Laurence Lundblade20fba4f2024-10-30 07:52:33 -07007558 "Decimal fraction -3/10",
7559 {(uint8_t[]){0xC4, 0x82, 0x20, 0x22}, 4},
7560 0,
7561 EXP_AND_MANTISSA_ERROR(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW),
7562 0,
7563 EXP_AND_MANTISSA_ERROR(QCBOR_ERR_NUMBER_SIGN_CONVERSION),
7564 -0.30000000000000004,
7565 FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS))
7566 },
7567 {
7568 "Decimal fraction -3/10, neg bignum mantissa",
7569 {(uint8_t[]){0xC4, 0x82, 0x20, 0xc3, 0x41, 0x02}, 6},
7570 0,
7571 EXP_AND_MANTISSA_ERROR(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW),
7572 0,
7573 EXP_AND_MANTISSA_ERROR(QCBOR_ERR_NUMBER_SIGN_CONVERSION),
7574 -0.30000000000000004,
7575 FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS))
7576 },
7577 {
Laurence Lundblade11fd78b2020-09-01 22:13:27 -07007578 "extreme pos bignum",
7579 {(uint8_t[]){0xc2, 0x59, 0x01, 0x90,
7580 // 50 rows of 8 is 400 digits.
7581 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7582 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7583 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7584 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7585 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7586 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7587 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7588 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7589 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7590 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7591 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7592 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7593 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7594 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7595 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7596 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7597 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7598 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7599 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7600 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7601 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7602 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7603 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7604 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7605 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7606 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7607 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7608 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7609 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7610 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7611 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7612 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7613 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7614 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7615 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7616 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7617 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7618 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7619 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7620 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7621 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7622 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7623 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7624 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7625 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7626 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7627 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7628 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7629 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
gtravisb787e82a2023-11-30 18:38:21 -08007630 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0},
Laurence Lundblade11fd78b2020-09-01 22:13:27 -07007631 404},
7632 0,
7633 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
7634 0,
7635 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
Laurence Lundblade11fd78b2020-09-01 22:13:27 -07007636 INFINITY,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007637 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS),
Laurence Lundblade11fd78b2020-09-01 22:13:27 -07007638 },
7639
7640 {
7641 "extreme neg bignum",
7642 {(uint8_t[]){0xc3, 0x59, 0x01, 0x90,
7643 // 50 rows of 8 is 400 digits.
7644 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7645 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7646 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7647 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7648 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7649 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7650 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7651 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7652 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7653 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7654 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7655 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7656 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7657 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7658 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7659 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7660 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7661 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7662 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7663 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7664 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7665 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7666 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7667 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7668 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7669 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7670 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7671 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7672 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7673 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7674 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7675 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7676 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7677 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7678 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7679 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7680 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7681 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7682 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7683 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7684 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7685 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7686 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7687 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7688 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7689 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7690 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7691 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7692 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
gtravisb787e82a2023-11-30 18:38:21 -08007693 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0},
Laurence Lundblade11fd78b2020-09-01 22:13:27 -07007694 404},
7695 0,
7696 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
7697 0,
7698 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
Laurence Lundblade11fd78b2020-09-01 22:13:27 -07007699 -INFINITY,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007700 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)
Laurence Lundblade11fd78b2020-09-01 22:13:27 -07007701 },
Laurence Lundblade51722fd2020-09-02 13:01:33 -07007702
7703 {
7704 "big float underflow [9223372036854775806, -9223372036854775806]",
7705 {(uint8_t[]){
7706 0xC5, 0x82,
7707 0x3B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE,
7708 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE}, 20},
Laurence Lundblade51722fd2020-09-02 13:01:33 -07007709 0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007710 EXP_AND_MANTISSA_ERROR(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW),
Laurence Lundblade51722fd2020-09-02 13:01:33 -07007711 0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007712 EXP_AND_MANTISSA_ERROR(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW),
Laurence Lundblade51722fd2020-09-02 13:01:33 -07007713 0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007714 FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS))
Laurence Lundblade51722fd2020-09-02 13:01:33 -07007715 },
7716
7717 {
7718 "bigfloat that evaluates to -INFINITY",
7719 {(uint8_t[]){
7720 0xC5, 0x82,
7721 0x1B, 0x70, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03,
7722 0xC3, 0x42, 0x01, 0x01}, 15},
Laurence Lundblade51722fd2020-09-02 13:01:33 -07007723 0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007724 EXP_AND_MANTISSA_ERROR(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW),
Laurence Lundblade51722fd2020-09-02 13:01:33 -07007725 0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007726 EXP_AND_MANTISSA_ERROR(QCBOR_ERR_NUMBER_SIGN_CONVERSION),
Laurence Lundblade51722fd2020-09-02 13:01:33 -07007727 -INFINITY,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007728 FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS))
Laurence Lundblade51722fd2020-09-02 13:01:33 -07007729 },
Laurence Lundblade37286c02022-09-03 10:05:02 -07007730 {
7731 "Positive bignum 0xffff",
7732 {(uint8_t[]){0xC2, 0x42, 0xff, 0xff}, 4},
7733 65536-1,
7734 QCBOR_SUCCESS,
7735 0xffff,
7736 QCBOR_SUCCESS,
7737 65535.0,
7738 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)
7739 },
7740#endif /* QCBOR_DISABLE_TAGS */
7741 {
7742 "Positive integer 18446744073709551615",
7743 {(uint8_t[]){0x1b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, 9},
7744 0,
7745 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
7746 18446744073709551615ULL,
7747 QCBOR_SUCCESS,
7748 18446744073709551615.0,
7749 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)
7750 },
7751
7752 {
7753 "Postive integer 0",
7754 {(uint8_t[]){0x0}, 1},
7755 0LL,
7756 QCBOR_SUCCESS,
7757 0ULL,
7758 QCBOR_SUCCESS,
7759 0.0,
7760 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)
7761 },
7762 {
7763 "Negative integer -18446744073709551616",
7764 {(uint8_t[]){0x3b, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }, 9},
7765 -9223372036854775807-1, // INT64_MIN
7766 QCBOR_SUCCESS,
7767 0ULL,
7768 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
7769 -9223372036854775808.0,
7770 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)
7771 },
7772 {
7773 "Double Floating point value 100.3",
7774 {(uint8_t[]){0xfb, 0x40, 0x59, 0x13, 0x33, 0x33, 0x33, 0x33, 0x33}, 9},
7775 100L,
7776 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS),
7777 100ULL,
7778 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS),
7779 100.3,
7780 FLOAT_ERR_CODE_NO_FLOAT(QCBOR_SUCCESS),
7781 },
7782 {
7783 "Floating point value NaN 0xfa7fc00000",
7784 {(uint8_t[]){0xfa, 0x7f, 0xc0, 0x00, 0x00}, 5},
7785 0,
7786 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_FLOAT_EXCEPTION),
7787 0,
7788 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_FLOAT_EXCEPTION),
7789 NAN,
7790 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS),
7791 },
7792 {
7793 "half-precision Floating point value -4",
7794 {(uint8_t[]){0xf9, 0xc4, 0x00}, 3},
7795 // Normal case with all enabled.
7796 -4,
7797 FLOAT_ERR_CODE_NO_HALF_PREC_NO_FLOAT_HW(QCBOR_SUCCESS),
7798 0,
7799 FLOAT_ERR_CODE_NO_HALF_PREC_NO_FLOAT_HW(QCBOR_ERR_NUMBER_SIGN_CONVERSION),
7800 -4.0,
7801 FLOAT_ERR_CODE_NO_HALF_PREC(QCBOR_SUCCESS)
7802 },
7803 {
7804 "+inifinity single precision",
7805 {(uint8_t[]){0xfa, 0x7f, 0x80, 0x00, 0x00}, 5},
7806 0,
7807 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_FLOAT_EXCEPTION),
7808 0,
7809 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW),
7810 INFINITY,
7811 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)
7812 },
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007813};
Laurence Lundblade9c905e82020-04-25 11:31:38 -07007814
7815
7816
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08007817
7818static int32_t SetUpDecoder(QCBORDecodeContext *DCtx, UsefulBufC CBOR, UsefulBuf Pool)
7819{
7820 QCBORDecode_Init(DCtx, CBOR, QCBOR_DECODE_MODE_NORMAL);
7821#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
7822 if(QCBORDecode_SetMemPool(DCtx, Pool, 0)) {
7823 return 1;
7824 }
7825#else /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
7826 (void)Pool;
7827#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
7828 return 0;
7829}
7830
7831
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03007832int32_t IntegerConvertTest(void)
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007833{
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08007834 const int nNumTests = C_ARRAY_COUNT(NumberConversions,
7835 struct NumberConversion);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007836
Laurence Lundblade9ab5abb2020-05-20 12:10:45 -07007837 for(int nIndex = 0; nIndex < nNumTests; nIndex++) {
7838 const struct NumberConversion *pF = &NumberConversions[nIndex];
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07007839
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007840 // Set up the decoding context including a memory pool so that
7841 // indefinite length items can be checked
7842 QCBORDecodeContext DCtx;
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007843 UsefulBuf_MAKE_STACK_UB(Pool, 100);
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07007844
7845 /* ----- test conversion to int64_t ------ */
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08007846 if(SetUpDecoder(&DCtx, pF->CBOR, Pool)) {
7847 return (int32_t)(3333+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007848 }
7849
7850 int64_t nInt;
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007851 QCBORDecode_GetInt64ConvertAll(&DCtx, 0xffff, &nInt);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07007852 if(QCBORDecode_GetError(&DCtx) != pF->uErrorInt64) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07007853 return (int32_t)(2000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007854 }
7855 if(pF->uErrorInt64 == QCBOR_SUCCESS && pF->nConvertedToInt64 != nInt) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07007856 return (int32_t)(3000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007857 }
7858
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07007859 /* ----- test conversion to uint64_t ------ */
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08007860 if(SetUpDecoder(&DCtx, pF->CBOR, Pool)) {
7861 return (int32_t)(3333+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007862 }
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08007863
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007864 uint64_t uInt;
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007865 QCBORDecode_GetUInt64ConvertAll(&DCtx, 0xffff, &uInt);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07007866 if(QCBORDecode_GetError(&DCtx) != pF->uErrorUint64) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07007867 return (int32_t)(4000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007868 }
7869 if(pF->uErrorUint64 == QCBOR_SUCCESS && pF->uConvertToUInt64 != uInt) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07007870 return (int32_t)(5000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007871 }
7872
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07007873 /* ----- test conversion to double ------ */
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08007874 if(SetUpDecoder(&DCtx, pF->CBOR, Pool)) {
7875 return (int32_t)(3333+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007876 }
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007877
7878#ifndef USEFULBUF_DISABLE_ALL_FLOAT
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007879 double d;
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007880 QCBORDecode_GetDoubleConvertAll(&DCtx, 0xffff, &d);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07007881 if(QCBORDecode_GetError(&DCtx) != pF->uErrorDouble) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07007882 return (int32_t)(6000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007883 }
7884 if(pF->uErrorDouble == QCBOR_SUCCESS) {
7885 if(isnan(pF->dConvertToDouble)) {
Laurence Lundblade983500d2020-05-14 11:49:34 -07007886 // NaN's can't be compared for equality. A NaN is
7887 // never equal to anything including another NaN
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007888 if(!isnan(d)) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07007889 return (int32_t)(7000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007890 }
7891 } else {
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007892 if(pF->dConvertToDouble != d) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07007893 return (int32_t)(8000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007894 }
7895 }
7896 }
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007897#endif /* USEFULBUF_DISABLE_ALL_FLOAT */
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007898 }
7899
7900 return 0;
7901}
7902
Laurence Lundbladea8758502022-05-15 17:57:46 -07007903#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
7904
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03007905int32_t CBORTestIssue134(void)
David Navarro9123e5b2022-03-28 16:04:03 +02007906{
7907 QCBORDecodeContext DCtx;
7908 QCBORItem Item;
7909 QCBORError uCBORError;
7910 const uint8_t spTestIssue134[] = { 0x5F, 0x40, 0xFF };
Laurence Lundblade9c905e82020-04-25 11:31:38 -07007911
David Navarro9123e5b2022-03-28 16:04:03 +02007912 QCBORDecode_Init(&DCtx,
7913 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTestIssue134),
7914 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade97c61bf2020-05-02 11:24:06 -07007915
David Navarro9123e5b2022-03-28 16:04:03 +02007916 UsefulBuf_MAKE_STACK_UB(StringBuf, 200);
7917 QCBORDecode_SetMemPool(&DCtx, StringBuf, false);
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03007918
David Navarro9123e5b2022-03-28 16:04:03 +02007919 do {
7920 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
7921 } while (QCBOR_SUCCESS == uCBORError);
7922
7923 uCBORError = QCBORDecode_Finish(&DCtx);
7924
Laurence Lundblade11ea3612022-07-01 13:26:23 -07007925 return (int32_t)uCBORError;
David Navarro9123e5b2022-03-28 16:04:03 +02007926}
Laurence Lundblade97c61bf2020-05-02 11:24:06 -07007927
Laurence Lundbladea8758502022-05-15 17:57:46 -07007928#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
7929
Laurence Lundblade37286c02022-09-03 10:05:02 -07007930
7931
7932static const uint8_t spSequenceTestInput[] = {
7933 /* 1. The valid date string "1985-04-12" */
7934 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string
7935
7936 /* 2. */
7937 0x00,
7938
7939 /* 3. A valid epoch date, 1400000000; Tue, 13 May 2014 16:53:20 GMT */
7940 0x1a, 0x53, 0x72, 0x4E, 0x00,
7941
7942 /* 4. */
7943 0x62, 'h', 'i',
7944};
7945
7946
Laurence Lundbladee3553422020-05-02 11:11:17 -07007947int32_t CBORSequenceDecodeTests(void)
7948{
7949 QCBORDecodeContext DCtx;
Laurence Lundblade87495732021-02-26 10:05:55 -07007950 QCBORItem Item;
7951 QCBORError uCBORError;
7952 size_t uConsumed;
Laurence Lundbladee3553422020-05-02 11:11:17 -07007953
7954 // --- Test a sequence with extra bytes ---
Laurence Lundblade9b334962020-08-27 10:55:53 -07007955
Laurence Lundbladee3553422020-05-02 11:11:17 -07007956 QCBORDecode_Init(&DCtx,
Laurence Lundblade37286c02022-09-03 10:05:02 -07007957 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSequenceTestInput),
Laurence Lundbladee3553422020-05-02 11:11:17 -07007958 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade9b334962020-08-27 10:55:53 -07007959
Laurence Lundblade37286c02022-09-03 10:05:02 -07007960 // Get 1.
Laurence Lundbladee3553422020-05-02 11:11:17 -07007961 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
7962 if(uCBORError != QCBOR_SUCCESS) {
7963 return 1;
7964 }
Laurence Lundblade37286c02022-09-03 10:05:02 -07007965 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ) {
Laurence Lundbladee3553422020-05-02 11:11:17 -07007966 return 2;
7967 }
Laurence Lundbladec7114722020-08-13 05:11:40 -07007968
Laurence Lundblade87495732021-02-26 10:05:55 -07007969 uCBORError = QCBORDecode_PartialFinish(&DCtx, &uConsumed);
7970 if(uCBORError != QCBOR_ERR_EXTRA_BYTES ||
Laurence Lundblade37286c02022-09-03 10:05:02 -07007971 uConsumed != 11) {
7972 return 102;
7973 }
7974
7975 // Get 2.
7976 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
7977 if(uCBORError != QCBOR_SUCCESS) {
7978 return 66;
7979 }
7980
7981 uCBORError = QCBORDecode_PartialFinish(&DCtx, &uConsumed);
7982 if(uCBORError != QCBOR_ERR_EXTRA_BYTES ||
Laurence Lundblade87495732021-02-26 10:05:55 -07007983 uConsumed != 12) {
7984 return 102;
7985 }
7986
Laurence Lundblade37286c02022-09-03 10:05:02 -07007987 // Get 3.
Laurence Lundbladec7114722020-08-13 05:11:40 -07007988 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundbladee3553422020-05-02 11:11:17 -07007989 if(uCBORError != QCBOR_SUCCESS) {
7990 return 2;
7991 }
Laurence Lundblade37286c02022-09-03 10:05:02 -07007992 if(Item.uDataType != QCBOR_TYPE_INT64) {
Laurence Lundbladee3553422020-05-02 11:11:17 -07007993 return 3;
7994 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07007995
Laurence Lundbladee3553422020-05-02 11:11:17 -07007996 // A sequence can have stuff at the end that may
7997 // or may not be valid CBOR. The protocol decoder knows
7998 // when to stop by definition of the protocol, not
7999 // when the top-level map or array is ended.
8000 // Finish still has to be called to know that
8001 // maps and arrays (if there were any) were closed
8002 // off correctly. When called like this it
8003 // must return the error QCBOR_ERR_EXTRA_BYTES.
8004 uCBORError = QCBORDecode_Finish(&DCtx);
8005 if(uCBORError != QCBOR_ERR_EXTRA_BYTES) {
8006 return 4;
8007 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07008008
Laurence Lundbladee3553422020-05-02 11:11:17 -07008009 // --- Test an empty input ----
8010 uint8_t empty[1];
8011 UsefulBufC Empty = {empty, 0};
8012 QCBORDecode_Init(&DCtx,
8013 Empty,
8014 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade9b334962020-08-27 10:55:53 -07008015
Laurence Lundbladee3553422020-05-02 11:11:17 -07008016 uCBORError = QCBORDecode_Finish(&DCtx);
8017 if(uCBORError != QCBOR_SUCCESS) {
8018 return 5;
8019 }
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07008020
8021
Laurence Lundbladee3553422020-05-02 11:11:17 -07008022 // --- Sequence with unclosed indefinite length array ---
8023 static const uint8_t xx[] = {0x01, 0x9f, 0x02};
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07008024
Laurence Lundbladee3553422020-05-02 11:11:17 -07008025 QCBORDecode_Init(&DCtx,
8026 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(xx),
8027 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07008028
Laurence Lundbladee3553422020-05-02 11:11:17 -07008029 // Get the first item
8030 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
8031 if(uCBORError != QCBOR_SUCCESS) {
8032 return 7;
8033 }
8034 if(Item.uDataType != QCBOR_TYPE_INT64) {
8035 return 8;
8036 }
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07008037
Laurence Lundbladee3553422020-05-02 11:11:17 -07008038 // Get a second item
8039 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08008040#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundbladee3553422020-05-02 11:11:17 -07008041 if(uCBORError != QCBOR_SUCCESS) {
8042 return 9;
8043 }
8044 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
8045 return 10;
8046 }
8047
8048 // Try to finish before consuming all bytes to confirm
8049 // that the still-open error is returned.
8050 uCBORError = QCBORDecode_Finish(&DCtx);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07008051 if(uCBORError != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
Laurence Lundbladee3553422020-05-02 11:11:17 -07008052 return 11;
8053 }
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08008054#else /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
8055 if(uCBORError != QCBOR_ERR_INDEF_LEN_ARRAYS_DISABLED) {
8056 return 20;
8057 }
8058#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
Laurence Lundbladee3553422020-05-02 11:11:17 -07008059
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07008060
Laurence Lundbladee3553422020-05-02 11:11:17 -07008061 // --- Sequence with a closed indefinite length array ---
8062 static const uint8_t yy[] = {0x01, 0x9f, 0xff};
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07008063
Laurence Lundbladee3553422020-05-02 11:11:17 -07008064 QCBORDecode_Init(&DCtx,
8065 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(yy),
8066 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07008067
Laurence Lundbladee3553422020-05-02 11:11:17 -07008068 // Get the first item
8069 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
8070 if(uCBORError != QCBOR_SUCCESS) {
8071 return 12;
8072 }
8073 if(Item.uDataType != QCBOR_TYPE_INT64) {
8074 return 13;
8075 }
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07008076
Laurence Lundbladee3553422020-05-02 11:11:17 -07008077 // Get a second item
8078 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08008079#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
8080
Laurence Lundbladee3553422020-05-02 11:11:17 -07008081 if(uCBORError != QCBOR_SUCCESS) {
8082 return 14;
8083 }
8084 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
8085 return 15;
8086 }
8087
8088 // Try to finish before consuming all bytes to confirm
8089 // that the still-open error is returned.
8090 uCBORError = QCBORDecode_Finish(&DCtx);
8091 if(uCBORError != QCBOR_SUCCESS) {
8092 return 16;
8093 }
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08008094#else /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
8095 if(uCBORError != QCBOR_ERR_INDEF_LEN_ARRAYS_DISABLED) {
8096 return 20;
8097 }
8098#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
Laurence Lundbladee3553422020-05-02 11:11:17 -07008099
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07008100
Laurence Lundbladee3553422020-05-02 11:11:17 -07008101 return 0;
8102}
8103
Laurence Lundbladee15326f2020-06-15 15:50:23 -07008104
Laurence Lundblade70ecead2020-06-15 19:40:06 -07008105
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03008106int32_t IntToTests(void)
Laurence Lundbladee15326f2020-06-15 15:50:23 -07008107{
8108 int nErrCode;
8109 int32_t n32;
8110 int16_t n16;
8111 int8_t n8;
8112 uint32_t u32;
8113 uint16_t u16;
8114 uint8_t u8;
8115 uint64_t u64;
8116
8117 nErrCode = QCBOR_Int64ToInt32(1, &n32);
8118 if(nErrCode == -1 || n32 != 1) {
8119 return 1;
8120 }
8121
8122 nErrCode = QCBOR_Int64ToInt32((int64_t)INT32_MAX, &n32);
8123 if(nErrCode == -1 || n32 != INT32_MAX) {
8124 return 2;
8125 }
8126
8127 nErrCode = QCBOR_Int64ToInt32((int64_t)INT32_MIN, &n32);
8128 if(nErrCode == -1 || n32 != INT32_MIN) {
8129 return 3;
8130 }
8131
8132 nErrCode = QCBOR_Int64ToInt32(((int64_t)INT32_MAX)+1, &n32);
8133 if(nErrCode != -1) {
8134 return 4;
8135 }
8136
8137 nErrCode = QCBOR_Int64ToInt32(((int64_t)INT32_MIN)-1, &n32);
8138 if(nErrCode != -1) {
8139 return 5;
8140 }
8141
8142
8143 nErrCode = QCBOR_Int64ToInt16((int64_t)INT16_MAX, &n16);
8144 if(nErrCode == -1 || n16 != INT16_MAX) {
8145 return 6;
8146 }
8147
8148 nErrCode = QCBOR_Int64ToInt16((int64_t)INT16_MIN, &n16);
8149 if(nErrCode == -1 || n16 != INT16_MIN) {
8150 return 7;
8151 }
8152
8153 nErrCode = QCBOR_Int64ToInt16(1, &n16);
8154 if(nErrCode == -1 || n16 != 1) {
8155 return 8;
8156 }
8157
8158 nErrCode = QCBOR_Int64ToInt16(((int64_t)INT16_MAX)+1, &n16);
8159 if(nErrCode != -1) {
8160 return 9;
8161 }
8162
8163 nErrCode = QCBOR_Int64ToInt16(((int64_t)INT16_MIN)-1, &n16);
8164 if(nErrCode != -1) {
8165 return 10;
8166 }
8167
8168
8169 nErrCode = QCBOR_Int64ToInt8(1, &n8);
8170 if(nErrCode == -1 || n8 != 1) {
8171 return 11;
8172 }
8173
8174 nErrCode = QCBOR_Int64ToInt8((int64_t)INT8_MAX, &n8);
8175 if(nErrCode == -1 || n8 != INT8_MAX) {
8176 return 12;
8177 }
8178
8179 nErrCode = QCBOR_Int64ToInt8((int64_t)INT8_MIN, &n8);
8180 if(nErrCode == -1 || n8 != INT8_MIN) {
8181 return 13;
8182 }
8183
8184 nErrCode = QCBOR_Int64ToInt8(((int64_t)INT8_MAX)+1, &n8);
8185 if(nErrCode != -1) {
8186 return 14;
8187 }
8188
8189 nErrCode = QCBOR_Int64ToInt8(((int64_t)INT8_MIN)-1, &n8);
8190 if(nErrCode != -1) {
8191 return 15;
8192 }
8193
8194
8195 nErrCode = QCBOR_Int64ToUInt32(1, &u32);
8196 if(nErrCode == -1 || u32 != 1) {
8197 return 16;
8198 }
8199
8200 nErrCode = QCBOR_Int64ToUInt32((int64_t)UINT32_MAX, &u32);
8201 if(nErrCode == -1 || u32 != UINT32_MAX) {
8202 return 17;
8203 }
8204
8205 nErrCode = QCBOR_Int64ToUInt32((int64_t)0, &u32);
8206 if(nErrCode == -1 || u32 != 0) {
8207 return 18;
8208 }
8209
8210 nErrCode = QCBOR_Int64ToUInt32(((int64_t)UINT32_MAX)+1, &u32);
8211 if(nErrCode != -1) {
8212 return 19;
8213 }
8214
8215 nErrCode = QCBOR_Int64ToUInt32((int64_t)-1, &u32);
8216 if(nErrCode != -1) {
8217 return 20;
8218 }
8219
8220
Sebastian Burck5a0ab732024-06-29 06:31:20 +02008221 nErrCode = QCBOR_Int64ToUInt16((int64_t)UINT16_MAX, &u16);
Laurence Lundbladee15326f2020-06-15 15:50:23 -07008222 if(nErrCode == -1 || u16 != UINT16_MAX) {
8223 return 21;
8224 }
8225
Sebastian Burck5a0ab732024-06-29 06:31:20 +02008226 nErrCode = QCBOR_Int64ToUInt16((int64_t)0, &u16);
Laurence Lundbladee15326f2020-06-15 15:50:23 -07008227 if(nErrCode == -1 || u16 != 0) {
8228 return 22;
8229 }
8230
Sebastian Burck5a0ab732024-06-29 06:31:20 +02008231 nErrCode = QCBOR_Int64ToUInt16(1, &u16);
Laurence Lundbladee15326f2020-06-15 15:50:23 -07008232 if(nErrCode == -1 || u16 != 1) {
8233 return 23;
8234 }
8235
Sebastian Burck5a0ab732024-06-29 06:31:20 +02008236 nErrCode = QCBOR_Int64ToUInt16(((int64_t)UINT16_MAX)+1, &u16);
Laurence Lundbladee15326f2020-06-15 15:50:23 -07008237 if(nErrCode != -1) {
8238 return 24;
8239 }
8240
Sebastian Burck5a0ab732024-06-29 06:31:20 +02008241 nErrCode = QCBOR_Int64ToUInt16((int64_t)-1, &u16);
Laurence Lundbladee15326f2020-06-15 15:50:23 -07008242 if(nErrCode != -1) {
8243 return 25;
8244 }
8245
8246
8247 nErrCode = QCBOR_Int64ToUInt8((int64_t)UINT8_MAX, &u8);
8248 if(nErrCode == -1 || u8 != UINT8_MAX) {
8249 return 26;
8250 }
8251
8252 nErrCode = QCBOR_Int64ToUInt8((int64_t)0, &u8);
8253 if(nErrCode == -1 || u8 != 0) {
8254 return 27;
8255 }
8256
8257 nErrCode = QCBOR_Int64ToUInt8(1, &u8);
8258 if(nErrCode == -1 || u8 != 1) {
8259 return 28;
8260 }
8261
8262 nErrCode = QCBOR_Int64ToUInt8(((int64_t)UINT16_MAX)+1, &u8);
8263 if(nErrCode != -1) {
8264 return 29;
8265 }
8266
8267 nErrCode = QCBOR_Int64ToUInt8((int64_t)-1, &u8);
8268 if(nErrCode != -1) {
8269 return 30;
8270 }
8271
8272
8273 nErrCode = QCBOR_Int64ToUInt64(1, &u64);
8274 if(nErrCode == -1 || u64 != 1) {
8275 return 31;
8276 }
8277
8278 nErrCode = QCBOR_Int64ToUInt64(INT64_MAX, &u64);
8279 if(nErrCode == -1 || u64 != INT64_MAX) {
8280 return 32;
8281 }
8282
8283 nErrCode = QCBOR_Int64ToUInt64((int64_t)0, &u64);
8284 if(nErrCode == -1 || u64 != 0) {
8285 return 33;
8286 }
8287
8288 nErrCode = QCBOR_Int64ToUInt64((int64_t)-1, &u64);
8289 if(nErrCode != -1) {
8290 return 34;
8291 }
8292
8293 return 0;
8294}
8295
Laurence Lundblade0750fc42020-06-20 21:02:34 -07008296
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07008297
8298
Laurence Lundblade0750fc42020-06-20 21:02:34 -07008299/*
8300A sequence with
8301 A wrapping bstr
8302 containing a map
8303 1
8304 2
8305 A wrapping bstr
8306 containing an array
8307 3
8308 wrapping bstr
8309 4
8310 5
8311 6
8312 array
8313 7
8314 8
Laurence Lundblade0750fc42020-06-20 21:02:34 -07008315 */
8316
Laurence Lundblade55013642020-09-23 05:39:22 -07008317static UsefulBufC EncodeBstrWrapTestData(UsefulBuf OutputBuffer)
Laurence Lundblade0750fc42020-06-20 21:02:34 -07008318{
Laurence Lundblade55013642020-09-23 05:39:22 -07008319 UsefulBufC Encoded;
Laurence Lundblade0750fc42020-06-20 21:02:34 -07008320 QCBOREncodeContext EC;
Laurence Lundblade55013642020-09-23 05:39:22 -07008321 QCBORError uErr;
Laurence Lundblade0750fc42020-06-20 21:02:34 -07008322
Laurence Lundblade55013642020-09-23 05:39:22 -07008323 QCBOREncode_Init(&EC, OutputBuffer);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07008324
Laurence Lundblade07ea9252024-10-26 17:13:21 -07008325#ifndef QCBOR_DISABLE_TAGS
8326 QCBOREncode_AddTag(&EC, CBOR_TAG_CBOR);
8327#endif /* ! QCBOR_DISABLE_TAGS */
Laurence Lundblade0750fc42020-06-20 21:02:34 -07008328 QCBOREncode_BstrWrap(&EC);
8329 QCBOREncode_OpenMap(&EC);
8330 QCBOREncode_AddInt64ToMapN(&EC, 100, 1);
8331 QCBOREncode_AddInt64ToMapN(&EC, 200, 2);
8332 QCBOREncode_CloseMap(&EC);
8333 QCBOREncode_BstrWrap(&EC);
8334 QCBOREncode_OpenArray(&EC);
8335 QCBOREncode_AddInt64(&EC, 3);
8336 QCBOREncode_BstrWrap(&EC);
8337 QCBOREncode_AddInt64(&EC, 4);
8338 QCBOREncode_CloseBstrWrap(&EC, NULL);
8339 QCBOREncode_AddInt64(&EC, 5);
8340 QCBOREncode_CloseArray(&EC);
8341 QCBOREncode_CloseBstrWrap(&EC, NULL);
8342 QCBOREncode_AddInt64(&EC, 6);
8343 QCBOREncode_CloseBstrWrap(&EC, NULL);
8344 QCBOREncode_OpenArray(&EC);
8345 QCBOREncode_AddInt64(&EC, 7);
8346 QCBOREncode_AddInt64(&EC, 8);
8347 QCBOREncode_CloseArray(&EC);
8348
8349 uErr = QCBOREncode_Finish(&EC, &Encoded);
Laurence Lundblade40a04322020-06-27 22:52:52 -07008350 if(uErr) {
8351 Encoded = NULLUsefulBufC;
8352 }
Laurence Lundblade0750fc42020-06-20 21:02:34 -07008353
8354 return Encoded;
8355}
8356
Laurence Lundbladecc7da412020-12-27 00:09:07 -08008357/* h'FF' */
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08008358static const uint8_t spBreakInByteString[] = {
8359 0x41, 0xff
8360};
8361
Laurence Lundblade0750fc42020-06-20 21:02:34 -07008362
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03008363int32_t EnterBstrTest(void)
Laurence Lundblade0750fc42020-06-20 21:02:34 -07008364{
Laurence Lundblade8510f8c2020-12-01 11:31:16 -08008365 UsefulBuf_MAKE_STACK_UB(OutputBuffer, 100);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07008366
8367 QCBORDecodeContext DC;
8368
Laurence Lundblade55013642020-09-23 05:39:22 -07008369 QCBORDecode_Init(&DC, EncodeBstrWrapTestData(OutputBuffer), 0);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07008370
Laurence Lundblade55013642020-09-23 05:39:22 -07008371 int64_t n1, n2, n3, n4, n5, n6, n7, n8;
Laurence Lundblade0750fc42020-06-20 21:02:34 -07008372
Laurence Lundblade07ea9252024-10-26 17:13:21 -07008373#ifndef QCBOR_DISABLE_TAGS
8374 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_TAG, NULL);
8375#else
Laurence Lundblade9b334962020-08-27 10:55:53 -07008376 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
Laurence Lundblade07ea9252024-10-26 17:13:21 -07008377#endif /* ! QCBOR_DISABLE_TAGS */
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07008378 QCBORDecode_EnterMap(&DC, NULL);
Laurence Lundblade55013642020-09-23 05:39:22 -07008379 QCBORDecode_GetInt64InMapN(&DC, 100, &n1);
8380 QCBORDecode_GetInt64InMapN(&DC, 200, &n2);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07008381 QCBORDecode_ExitMap(&DC);
Laurence Lundblade9b334962020-08-27 10:55:53 -07008382 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07008383 QCBORDecode_EnterArray(&DC, NULL);
Laurence Lundblade55013642020-09-23 05:39:22 -07008384 QCBORDecode_GetInt64(&DC, &n3);
Laurence Lundblade9b334962020-08-27 10:55:53 -07008385 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
Laurence Lundblade55013642020-09-23 05:39:22 -07008386 QCBORDecode_GetInt64(&DC, &n4);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07008387 QCBORDecode_ExitBstrWrapped(&DC);
Laurence Lundblade55013642020-09-23 05:39:22 -07008388 QCBORDecode_GetInt64(&DC, &n5);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07008389 QCBORDecode_ExitArray(&DC);
8390 QCBORDecode_ExitBstrWrapped(&DC);
Laurence Lundblade55013642020-09-23 05:39:22 -07008391 QCBORDecode_GetInt64(&DC, &n6);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07008392 QCBORDecode_ExitBstrWrapped(&DC);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07008393 QCBORDecode_EnterArray(&DC, NULL);
Laurence Lundblade55013642020-09-23 05:39:22 -07008394 QCBORDecode_GetInt64(&DC, &n7);
8395 QCBORDecode_GetInt64(&DC, &n8);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07008396 QCBORDecode_ExitArray(&DC);
8397
8398 QCBORError uErr = QCBORDecode_Finish(&DC);
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08008399 if(uErr) {
8400 return (int32_t)uErr;
8401 }
Laurence Lundblade0750fc42020-06-20 21:02:34 -07008402
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08008403
8404 /* Enter and exit byte string wrapped CBOR that is bad. It has just a break.
8405 * Successful because no items are fetched from byte string.
8406 */
8407 QCBORDecode_Init(&DC,
8408 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBreakInByteString),
8409 0);
8410 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
8411 uErr = QCBORDecode_GetError(&DC);
8412 if(uErr) {
8413 return 100 + (int32_t)uErr;
8414 }
8415
8416 QCBORDecode_ExitBstrWrapped(&DC);
8417 uErr = QCBORDecode_GetError(&DC);
8418 if(uErr) {
8419 return 200 + (int32_t)uErr;
8420 }
8421
8422 /* Try to get item that is a break out of a byte string wrapped CBOR.
8423 * It fails because there should be no break.
8424 */
8425 QCBORDecode_Init(&DC,
8426 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBreakInByteString),
8427 0);
8428 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
8429 QCBORItem Item;
8430 uErr = QCBORDecode_GetNext(&DC, &Item);
8431 if(uErr != QCBOR_ERR_BAD_BREAK) {
8432 return 300 + (int32_t)uErr;
8433 }
8434
8435 return 0;
Laurence Lundblade0750fc42020-06-20 21:02:34 -07008436}
Laurence Lundblade37f46e52020-08-04 03:32:14 -07008437
8438
8439
8440
8441static const uint8_t spTaggedTypes[] = {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008442 0xb2,
Laurence Lundblade37f46e52020-08-04 03:32:14 -07008443
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07008444 // Date string
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008445 0x00,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07008446 0xc0, 0x74, 0x32, 0x30, 0x30, 0x33, 0x2D, 0x31, 0x32, 0x2D,
8447 0x31, 0x33, 0x54, 0x31, 0x38, 0x3A, 0x33, 0x30, 0x3A, 0x30,
8448 0x32, 0x5A,
Laurence Lundblade37f46e52020-08-04 03:32:14 -07008449
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008450 0x01,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07008451 0x74, 0x32, 0x30, 0x30, 0x33, 0x2D, 0x31, 0x32, 0x2D, 0x31,
8452 0x33, 0x54, 0x31, 0x38, 0x3A, 0x33, 0x30, 0x3A, 0x30, 0x32,
8453 0x5A,
Laurence Lundblade37f46e52020-08-04 03:32:14 -07008454
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07008455 // Bignum
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008456 10,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07008457 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
8458 0x09, 0x10,
Laurence Lundblade37f46e52020-08-04 03:32:14 -07008459
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008460 11,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07008461 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
8462 0x09, 0x10,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008463
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07008464 // URL
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008465 20,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07008466 0xd8, 0x20, 0x6f, 0x68, 0x74, 0x74, 0x70, 0x3A, 0x2F, 0x2F,
8467 0x63, 0x62, 0x6F, 0x72, 0x2E, 0x6D, 0x65, 0x2F,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008468
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07008469 21,
8470 0x6f, 0x68, 0x74, 0x74, 0x70, 0x3A, 0x2F, 0x2F, 0x63, 0x62,
8471 0x6F, 0x72, 0x2E, 0x6D, 0x65, 0x2F,
8472
8473 // B64
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008474 0x18, 0x1e,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07008475 0xd8, 0x22, 0x6c, 0x63, 0x47, 0x78, 0x6C, 0x59, 0x58, 0x4E,
8476 0x31, 0x63, 0x6D, 0x55, 0x75,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008477
8478 0x18, 0x1f,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07008479 0x6c, 0x63, 0x47, 0x78, 0x6C, 0x59, 0x58, 0x4E, 0x31, 0x63,
8480 0x6D, 0x55, 0x75,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008481
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07008482 // B64URL
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008483 0x18, 0x28,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07008484 0xd8, 0x21, 0x6c, 0x63, 0x47, 0x78, 0x6C, 0x59, 0x58, 0x4E,
8485 0x31, 0x63, 0x6D, 0x55, 0x75,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008486
8487 0x18, 0x29,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07008488 0x6c, 0x63, 0x47, 0x78, 0x6C, 0x59, 0x58, 0x4E, 0x31, 0x63,
8489 0x6D, 0x55, 0x75,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008490
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07008491 // Regex
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008492 0x18, 0x32,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07008493 0xd8, 0x23, 0x68, 0x31, 0x30, 0x30, 0x5C, 0x73, 0x2A, 0x6D,
8494 0x6B,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008495
8496 0x18, 0x33,
8497 0x68, 0x31, 0x30, 0x30, 0x5C, 0x73, 0x2A, 0x6D, 0x6B,
8498
8499 // MIME
8500 0x18, 0x3c,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07008501 0xd8, 0x24, 0x72, 0x4D, 0x49, 0x4D, 0x45, 0x2D, 0x56, 0x65,
8502 0x72, 0x73, 0x69, 0x6F, 0x6E, 0x3A, 0x20, 0x31, 0x2E, 0x30,
8503 0x0A,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008504
8505 0x18, 0x3d,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07008506 0x72, 0x4D, 0x49, 0x4D, 0x45, 0x2D, 0x56, 0x65, 0x72, 0x73,
8507 0x69, 0x6F, 0x6E, 0x3A, 0x20, 0x31, 0x2E, 0x30, 0x0A,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008508
8509 0x18, 0x3e,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07008510 0xd9, 0x01, 0x01, 0x52, 0x4D, 0x49, 0x4D, 0x45, 0x2D, 0x56,
8511 0x65, 0x72, 0x73, 0x69, 0x6F, 0x6E, 0x3A, 0x20, 0x31, 0x2E,
8512 0x30, 0x0A,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008513
8514 0x18, 0x3f,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07008515 0x52, 0x4D, 0x49, 0x4D, 0x45, 0x2D, 0x56, 0x65, 0x72, 0x73,
8516 0x69, 0x6F, 0x6E, 0x3A, 0x20, 0x31, 0x2E, 0x30, 0x0A,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008517
8518 // UUID
8519 0x18, 0x46,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07008520 0xd8, 0x25, 0x50, 0x53, 0x4D, 0x41, 0x52, 0x54, 0x43, 0x53,
8521 0x4C, 0x54, 0x54, 0x43, 0x46, 0x49, 0x43, 0x41, 0x32,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008522
8523 0x18, 0x47,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07008524 0x50, 0x53, 0x4D, 0x41, 0x52, 0x54, 0x43, 0x53, 0x4C, 0x54,
8525 0x54, 0x43, 0x46, 0x49, 0x43, 0x41, 0x32
Laurence Lundblade37f46e52020-08-04 03:32:14 -07008526};
8527
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03008528int32_t DecodeTaggedTypeTests(void)
Laurence Lundblade37f46e52020-08-04 03:32:14 -07008529{
8530 QCBORDecodeContext DC;
8531 QCBORError uErr;
8532
8533 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTaggedTypes), 0);
8534
8535 UsefulBufC String;
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008536 bool bNeg;
Laurence Lundblade37f46e52020-08-04 03:32:14 -07008537
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07008538 QCBORDecode_EnterMap(&DC, NULL);
Laurence Lundblade9b334962020-08-27 10:55:53 -07008539 QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008540 QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &String);
Laurence Lundblade37f46e52020-08-04 03:32:14 -07008541 if(QCBORDecode_GetError(&DC) != QCBOR_SUCCESS) {
8542 return 1;
8543 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07008544 QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade37f46e52020-08-04 03:32:14 -07008545 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_UNEXPECTED_TYPE) {
8546 return 2;
8547 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07008548 QCBORDecode_GetDateStringInMapN(&DC, 1, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade37f46e52020-08-04 03:32:14 -07008549 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_UNEXPECTED_TYPE) {
8550 return 3;
8551 }
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008552 QCBORDecode_GetDateStringInMapN(&DC, 1, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &String);
Laurence Lundblade9b334962020-08-27 10:55:53 -07008553 QCBORDecode_GetDateStringInMapN(&DC, 1, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade37f46e52020-08-04 03:32:14 -07008554 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
8555 return 4;
8556 }
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008557 QCBORDecode_GetDateStringInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07008558 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008559 return 5;
8560 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07008561
Laurence Lundblade9b334962020-08-27 10:55:53 -07008562 QCBORDecode_GetBignumInMapN(&DC, 10, QCBOR_TAG_REQUIREMENT_TAG, &String, &bNeg);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008563 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
8564 bNeg != false) {
8565 return 10;
8566 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07008567 QCBORDecode_GetBignumInMapN(&DC, 11, QCBOR_TAG_REQUIREMENT_TAG, &String, &bNeg);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008568 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
8569 bNeg != true) {
8570 return 11;
8571 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07008572 QCBORDecode_GetBignumInMapN(&DC, 11, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bNeg);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008573 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_UNEXPECTED_TYPE) {
8574 return 12;
8575 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07008576 QCBORDecode_GetBignumInMapN(&DC, 14, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bNeg);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07008577 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008578 return 13;
8579 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07008580 QCBORDecode_GetBignumInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bNeg);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07008581 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008582 return 14;
8583 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07008584
Laurence Lundblade9b334962020-08-27 10:55:53 -07008585 QCBORDecode_GetURIInMapN(&DC, 20, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008586 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
8587 return 20;
8588 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07008589 QCBORDecode_GetURIInMapN(&DC, 21, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008590 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
8591 return 21;
8592 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07008593 QCBORDecode_GetURIInMapN(&DC, 22, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07008594 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008595 return 22;
8596 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07008597 QCBORDecode_GetURIInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07008598 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008599 return 23;
8600 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07008601
Laurence Lundblade24bd7e12021-01-09 00:05:11 -08008602#ifndef QCBOR_DISABLE_UNCOMMON_TAGS
Laurence Lundblade9b334962020-08-27 10:55:53 -07008603 QCBORDecode_GetB64InMapN(&DC, 30, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008604 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
8605 return 30;
8606 }
Laurence Lundblade24bd7e12021-01-09 00:05:11 -08008607#endif
Laurence Lundblade9b334962020-08-27 10:55:53 -07008608 QCBORDecode_GetB64InMapN(&DC, 31, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008609 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
8610 return 31;
8611 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07008612 QCBORDecode_GetB64InMapN(&DC, 32, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07008613 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008614 return 32;
8615 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07008616 QCBORDecode_GetB64InMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07008617 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008618 return 33;
8619 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07008620
Laurence Lundblade24bd7e12021-01-09 00:05:11 -08008621#ifndef QCBOR_DISABLE_UNCOMMON_TAGS
Laurence Lundblade9b334962020-08-27 10:55:53 -07008622 QCBORDecode_GetB64URLInMapN(&DC, 40, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008623 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
8624 return 40;
8625 }
Laurence Lundblade24bd7e12021-01-09 00:05:11 -08008626#endif
Laurence Lundblade9b334962020-08-27 10:55:53 -07008627 QCBORDecode_GetB64URLInMapN(&DC, 41, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008628 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
8629 return 41;
8630 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07008631 QCBORDecode_GetB64URLInMapN(&DC, 42, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07008632 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008633 return 42;
8634 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07008635 QCBORDecode_GetB64URLInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07008636 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008637 return 43;
8638 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07008639
Laurence Lundblade24bd7e12021-01-09 00:05:11 -08008640#ifndef QCBOR_DISABLE_UNCOMMON_TAGS
Laurence Lundblade9b334962020-08-27 10:55:53 -07008641 QCBORDecode_GetRegexInMapN(&DC, 50, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008642 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
8643 return 50;
8644 }
Laurence Lundblade24bd7e12021-01-09 00:05:11 -08008645#endif
Laurence Lundblade9b334962020-08-27 10:55:53 -07008646 QCBORDecode_GetRegexInMapN(&DC, 51, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008647 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
8648 return 51;
8649 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07008650 QCBORDecode_GetRegexInMapN(&DC, 52, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07008651 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008652 return 52;
8653 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07008654 QCBORDecode_GetRegexInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07008655 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008656 return 53;
8657 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07008658
Laurence Lundblade24bd7e12021-01-09 00:05:11 -08008659#ifndef QCBOR_DISABLE_UNCOMMON_TAGS
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008660 // MIME
8661 bool bIsNot7Bit;
Laurence Lundblade9b334962020-08-27 10:55:53 -07008662 QCBORDecode_GetMIMEMessageInMapN(&DC, 60, QCBOR_TAG_REQUIREMENT_TAG, &String, &bIsNot7Bit);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008663 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
8664 bIsNot7Bit == true) {
8665 return 60;
8666 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07008667 QCBORDecode_GetMIMEMessageInMapN(&DC, 61, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bIsNot7Bit);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008668 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
8669 bIsNot7Bit == true) {
8670 return 61;
8671 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07008672 QCBORDecode_GetMIMEMessageInMapN(&DC, 62, QCBOR_TAG_REQUIREMENT_TAG, &String, &bIsNot7Bit);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008673 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
8674 bIsNot7Bit == false) {
8675 return 62;
8676 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07008677 QCBORDecode_GetMIMEMessageInMapN(&DC, 63, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bIsNot7Bit);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008678 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
8679 bIsNot7Bit == false) {
8680 return 63;
8681 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07008682 QCBORDecode_GetMIMEMessageInMapN(&DC, 64, QCBOR_TAG_REQUIREMENT_TAG, &String, &bNeg);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07008683 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008684 return 64;
8685 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07008686 QCBORDecode_GetMIMEMessageInMapSZ(&DC, "zzz", QCBOR_TAG_REQUIREMENT_TAG, &String, &bNeg);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07008687 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008688 return 65;
8689 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07008690
Laurence Lundblade24bd7e12021-01-09 00:05:11 -08008691
Laurence Lundblade9b334962020-08-27 10:55:53 -07008692 QCBORDecode_GetBinaryUUIDInMapN(&DC, 70, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008693 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
8694 return 70;
8695 }
Laurence Lundblade24bd7e12021-01-09 00:05:11 -08008696#endif /* #ifndef QCBOR_DISABLE_UNCOMMON_TAGS */
8697
Laurence Lundblade9b334962020-08-27 10:55:53 -07008698 QCBORDecode_GetBinaryUUIDInMapN(&DC, 71, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008699 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
8700 return 71;
8701 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07008702 QCBORDecode_GetBinaryUUIDInMapN(&DC, 72, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07008703 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008704 return 72;
8705 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07008706 QCBORDecode_GetBinaryUUIDInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07008707 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008708 return 73;
8709 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07008710
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07008711 // Improvement: add some more error test cases
8712
Laurence Lundblade37f46e52020-08-04 03:32:14 -07008713 QCBORDecode_ExitMap(&DC);
8714
8715 uErr = QCBORDecode_Finish(&DC);
8716 if(uErr != QCBOR_SUCCESS) {
8717 return 100;
8718 }
8719
8720 return 0;
8721}
Laurence Lundbladea4308a82020-10-03 18:08:57 -07008722
8723
8724
8725
8726/*
Laurence Lundbladecc7da412020-12-27 00:09:07 -08008727 [
8728 "aaaaaaaaaa",
8729 {}
8730 ]
Laurence Lundbladea4308a82020-10-03 18:08:57 -07008731 */
8732static const uint8_t spTooLarge1[] = {
8733 0x9f,
8734 0x6a, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61,
8735 0xa0,
8736 0xff
8737};
8738
8739/*
Laurence Lundbladecc7da412020-12-27 00:09:07 -08008740 [
8741 {
8742 0: "aaaaaaaaaa"
8743 }
8744 ]
Laurence Lundbladea4308a82020-10-03 18:08:57 -07008745 */
8746static const uint8_t spTooLarge2[] = {
8747 0x9f,
8748 0xa1,
8749 0x00,
8750 0x6a, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61,
8751 0xff
8752};
8753
8754/*
Laurence Lundbladecc7da412020-12-27 00:09:07 -08008755 h'A1006A61616161616161616161'
Laurence Lundbladea4308a82020-10-03 18:08:57 -07008756
Laurence Lundbladecc7da412020-12-27 00:09:07 -08008757 {
8758 0: "aaaaaaaaaa"
8759 }
Laurence Lundbladea4308a82020-10-03 18:08:57 -07008760 */
8761static const uint8_t spTooLarge3[] = {
8762 0x4d,
8763 0xa1,
8764 0x00,
8765 0x6a, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61,
8766};
8767
8768int32_t TooLargeInputTest(void)
8769{
8770 QCBORDecodeContext DC;
8771 QCBORError uErr;
8772 UsefulBufC String;
8773
8774 // These tests require a build with QCBOR_MAX_DECODE_INPUT_SIZE set
8775 // to 10 There's not really any way to test this error
8776 // condition. The error condition is not complex, so setting
8777 // QCBOR_MAX_DECODE_INPUT_SIZE gives an OK test.
8778
8779 // The input CBOR is only too large because the
8780 // QCBOR_MAX_DECODE_INPUT_SIZE is 10.
8781 //
8782 // This test is disabled for the normal test runs because of the
8783 // special build requirement.
8784
8785
8786 // Tests the start of a map being too large
8787 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTooLarge1), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07008788 QCBORDecode_EnterArray(&DC, NULL);
Laurence Lundbladea4308a82020-10-03 18:08:57 -07008789 QCBORDecode_GetTextString(&DC, &String);
8790 uErr = QCBORDecode_GetError(&DC);
8791 if(uErr != QCBOR_SUCCESS) {
8792 return 1;
8793 }
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07008794 QCBORDecode_EnterMap(&DC, NULL);
Laurence Lundbladea4308a82020-10-03 18:08:57 -07008795 uErr = QCBORDecode_GetError(&DC);
8796 if(uErr != QCBOR_ERR_INPUT_TOO_LARGE) {
8797 return 2;
8798 }
8799
8800 // Tests the end of a map being too large
8801 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTooLarge2), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07008802 QCBORDecode_EnterArray(&DC, NULL);
8803 QCBORDecode_EnterMap(&DC, NULL);
Laurence Lundbladea4308a82020-10-03 18:08:57 -07008804 uErr = QCBORDecode_GetError(&DC);
8805 if(uErr != QCBOR_SUCCESS) {
8806 return 3;
8807 }
8808 QCBORDecode_ExitMap(&DC);
8809 uErr = QCBORDecode_GetError(&DC);
8810 if(uErr != QCBOR_ERR_INPUT_TOO_LARGE) {
8811 return 4;
8812 }
8813
8814 // Tests the entire input CBOR being too large when processing bstr wrapping
8815 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTooLarge3), QCBOR_DECODE_MODE_NORMAL);
8816 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
8817 uErr = QCBORDecode_GetError(&DC);
8818 if(uErr != QCBOR_ERR_INPUT_TOO_LARGE) {
8819 return 5;
8820 }
8821
8822 return 0;
8823}
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07008824
8825
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08008826#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
8827
Laurence Lundblade37286c02022-09-03 10:05:02 -07008828/*
8829 An array of three map entries
8830 1) Indefinite length string label for indefinite lenght byte string
8831 2) Indefinite length string label for an integer
8832 3) Indefinite length string label for an indefinite-length negative big num
8833 */
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07008834static const uint8_t spMapWithIndefLenStrings[] = {
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08008835 0xa3,
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07008836 0x7f, 0x61, 'l', 0x64, 'a', 'b', 'e', 'l' , 0x61, '1', 0xff,
8837 0x5f, 0x42, 0x01, 0x02, 0x43, 0x03, 0x04, 0x05, 0xff,
8838 0x7f, 0x62, 'd', 'y', 0x61, 'm', 0x61, 'o', 0xff,
8839 0x03,
8840 0x7f, 0x62, 'l', 'a', 0x63, 'b', 'e', 'l', 0x61, '2', 0xff,
8841 0xc3,
8842 0x5f, 0x42, 0x00, 0x01, 0x42, 0x00, 0x01, 0x41, 0x01, 0xff,
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07008843};
8844
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03008845int32_t SpiffyIndefiniteLengthStringsTests(void)
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07008846{
8847 QCBORDecodeContext DCtx;
8848
8849 QCBORDecode_Init(&DCtx,
8850 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spMapWithIndefLenStrings),
8851 QCBOR_DECODE_MODE_NORMAL);
8852
Laurence Lundblade8510f8c2020-12-01 11:31:16 -08008853 UsefulBuf_MAKE_STACK_UB(StringBuf, 200);
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07008854 QCBORDecode_SetMemPool(&DCtx, StringBuf, false);
8855
8856 UsefulBufC ByteString;
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07008857 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07008858 QCBORDecode_GetByteStringInMapSZ(&DCtx, "label1", &ByteString);
Laurence Lundblade37286c02022-09-03 10:05:02 -07008859
8860#ifndef QCBOR_DISABLE_TAGS
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07008861 if(QCBORDecode_GetAndResetError(&DCtx)) {
8862 return 1;
8863 }
8864
8865 const uint8_t pExectedBytes[] = {0x01, 0x02, 0x03, 0x04, 0x05};
8866 if(UsefulBuf_Compare(ByteString, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pExectedBytes))) {
8867 return 2;
8868 }
8869
8870 uint64_t uInt;
8871 QCBORDecode_GetUInt64InMapSZ(&DCtx, "dymo", &uInt);
8872 if(QCBORDecode_GetAndResetError(&DCtx)) {
8873 return 3;
8874 }
8875 if(uInt != 3) {
8876 return 4;
8877 }
8878
Máté Tóth-Pálef5f07a2021-09-17 19:31:37 +02008879#ifndef USEFULBUF_DISABLE_ALL_FLOAT
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07008880 double uDouble;
8881 QCBORDecode_GetDoubleConvertAllInMapSZ(&DCtx,
8882 "label2",
8883 0xff,
8884 &uDouble);
Laurence Lundblade37286c02022-09-03 10:05:02 -07008885
Laurence Lundbladeb8e19aa2020-10-07 20:59:11 -07008886#ifndef QCBOR_DISABLE_FLOAT_HW_USE
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07008887 if(QCBORDecode_GetAndResetError(&DCtx)) {
8888 return 5;
8889 }
8890 if(uDouble != -16777474) {
8891 return 6;
8892 }
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08008893#else /* QCBOR_DISABLE_FLOAT_HW_USE */
Laurence Lundbladeb8e19aa2020-10-07 20:59:11 -07008894 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_HW_FLOAT_DISABLED) {
8895 return 7;
8896 }
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08008897#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
Máté Tóth-Pálef5f07a2021-09-17 19:31:37 +02008898#endif /* USEFULBUF_DISABLE_ALL_FLOAT */
Laurence Lundbladeb8e19aa2020-10-07 20:59:11 -07008899
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07008900 QCBORDecode_ExitMap(&DCtx);
8901
8902 if(QCBORDecode_Finish(&DCtx)) {
8903 return 99;
8904 }
8905
Laurence Lundblade37286c02022-09-03 10:05:02 -07008906#else /* QCBOR_DISABLE_TAGS */
8907 /* The big num in the input is a CBOR tag and you can't do
8908 * map lookups in a map with a tag so this test does very little
8909 * when tags are disabled. That is OK, the test coverage is still
8910 * good when they are not.
8911 */
8912 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_TAGS_DISABLED) {
8913 return 1002;
8914 }
8915#endif /*QCBOR_DISABLE_TAGS */
8916
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07008917 return 0;
8918}
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08008919#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
Laurence Lundblade2a26abb2020-11-05 19:06:54 -08008920
8921
Laurence Lundbladeec290b82024-06-10 11:10:54 -07008922#ifndef QCBOR_DISABLE_NON_INTEGER_LABELS
Laurence Lundbladecf41c522021-02-20 10:19:07 -07008923/*
8924 * An array of an integer and an array. The second array contains
8925 * a bstr-wrapped map.
8926 *
8927 * [7, [h'A36D6669... (see next lines) 73']]
8928 *
8929 * {"first integer": 42,
8930 * "an array of two strings": ["string1", "string2"],
8931 * "map in a map":
8932 * { "bytes 1": h'78787878',
8933 * "bytes 2": h'79797979',
8934 * "another int": 98,
8935 * "text 2": "lies, damn lies and statistics"
8936 * }
8937 * }
8938 */
Laurence Lundblade2a26abb2020-11-05 19:06:54 -08008939
Laurence Lundbladecf41c522021-02-20 10:19:07 -07008940static const uint8_t pValidWrappedMapEncoded[] = {
8941 0x82, 0x07, 0x81, 0x58, 0x97,
8942 0xa3, 0x6d, 0x66, 0x69, 0x72, 0x73, 0x74, 0x20, 0x69, 0x6e,
8943 0x74, 0x65, 0x67, 0x65, 0x72, 0x18, 0x2a, 0x77, 0x61, 0x6e,
8944 0x20, 0x61, 0x72, 0x72, 0x61, 0x79, 0x20, 0x6f, 0x66, 0x20,
8945 0x74, 0x77, 0x6f, 0x20, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67,
8946 0x73, 0x82, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x31,
8947 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x32, 0x6c, 0x6d,
8948 0x61, 0x70, 0x20, 0x69, 0x6e, 0x20, 0x61, 0x20, 0x6d, 0x61,
8949 0x70, 0xa4, 0x67, 0x62, 0x79, 0x74, 0x65, 0x73, 0x20, 0x31,
8950 0x44, 0x78, 0x78, 0x78, 0x78, 0x67, 0x62, 0x79, 0x74, 0x65,
8951 0x73, 0x20, 0x32, 0x44, 0x79, 0x79, 0x79, 0x79, 0x6b, 0x61,
8952 0x6e, 0x6f, 0x74, 0x68, 0x65, 0x72, 0x20, 0x69, 0x6e, 0x74,
8953 0x18, 0x62, 0x66, 0x74, 0x65, 0x78, 0x74, 0x20, 0x32, 0x78,
8954 0x1e, 0x6c, 0x69, 0x65, 0x73, 0x2c, 0x20, 0x64, 0x61, 0x6d,
8955 0x6e, 0x20, 0x6c, 0x69, 0x65, 0x73, 0x20, 0x61, 0x6e, 0x64,
8956 0x20, 0x73, 0x74, 0x61, 0x74, 0x69, 0x73, 0x74, 0x69, 0x63,
8957 0x73
8958};
8959
8960#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
8961
8962/* As above, but the arrays are indefinite length */
8963static const uint8_t pValidIndefWrappedMapEncoded[] = {
8964 0x9f, 0x07, 0x9f, 0x58, 0x97,
8965 0xa3, 0x6d, 0x66, 0x69, 0x72, 0x73, 0x74, 0x20, 0x69, 0x6e,
8966 0x74, 0x65, 0x67, 0x65, 0x72, 0x18, 0x2a, 0x77, 0x61, 0x6e,
8967 0x20, 0x61, 0x72, 0x72, 0x61, 0x79, 0x20, 0x6f, 0x66, 0x20,
8968 0x74, 0x77, 0x6f, 0x20, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67,
8969 0x73, 0x82, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x31,
8970 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x32, 0x6c, 0x6d,
8971 0x61, 0x70, 0x20, 0x69, 0x6e, 0x20, 0x61, 0x20, 0x6d, 0x61,
8972 0x70, 0xa4, 0x67, 0x62, 0x79, 0x74, 0x65, 0x73, 0x20, 0x31,
8973 0x44, 0x78, 0x78, 0x78, 0x78, 0x67, 0x62, 0x79, 0x74, 0x65,
8974 0x73, 0x20, 0x32, 0x44, 0x79, 0x79, 0x79, 0x79, 0x6b, 0x61,
8975 0x6e, 0x6f, 0x74, 0x68, 0x65, 0x72, 0x20, 0x69, 0x6e, 0x74,
8976 0x18, 0x62, 0x66, 0x74, 0x65, 0x78, 0x74, 0x20, 0x32, 0x78,
8977 0x1e, 0x6c, 0x69, 0x65, 0x73, 0x2c, 0x20, 0x64, 0x61, 0x6d,
8978 0x6e, 0x20, 0x6c, 0x69, 0x65, 0x73, 0x20, 0x61, 0x6e, 0x64,
8979 0x20, 0x73, 0x74, 0x61, 0x74, 0x69, 0x73, 0x74, 0x69, 0x63,
8980 0x73,
8981 0xff, 0xff
8982};
Laurence Lundbladeec290b82024-06-10 11:10:54 -07008983#endif /* ! QCBOR_DISABLE_NON_INTEGER_LABELS */
Laurence Lundbladecf41c522021-02-20 10:19:07 -07008984
8985static const uint8_t pWithEmptyMap[] = {0x82, 0x18, 0x64, 0xa0};
8986
Laurence Lundbladeec290b82024-06-10 11:10:54 -07008987
Laurence Lundbladecf41c522021-02-20 10:19:07 -07008988#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
8989static const uint8_t pWithEmptyMapInDef[] = {0x9f, 0x18, 0x64, 0xbf, 0xff, 0xff};
8990#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
8991
8992#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
Laurence Lundblade37286c02022-09-03 10:05:02 -07008993/*
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08008994 * An array of one that contains a byte string that is an indefinite
8995 * length string that CBOR wraps an array of three numbers [42, 43,
8996 * 44]. The byte string is an implicit tag 24.
8997 *
8998 * [
8999 * (_ h'83', h'18', h'2A182B', h'182C')
9000 * ]
Laurence Lundblade37286c02022-09-03 10:05:02 -07009001 */
Laurence Lundbladecf41c522021-02-20 10:19:07 -07009002static const uint8_t pWrappedByIndefiniteLength[] = {
9003 0x81,
Laurence Lundbladecf41c522021-02-20 10:19:07 -07009004 0x5f,
9005 0x41, 0x83,
9006 0x41, 0x18,
9007 0x43, 0x2A, 0x18, 0x2B,
9008 0x42, 0x18, 0x2C,
9009 0xff
9010};
9011#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
9012
Laurence Lundbladeec290b82024-06-10 11:10:54 -07009013#endif
Laurence Lundbladecf41c522021-02-20 10:19:07 -07009014
Laurence Lundbladeec290b82024-06-10 11:10:54 -07009015#ifndef QCBOR_DISABLE_NON_INTEGER_LABELS
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03009016int32_t PeekAndRewindTest(void)
Laurence Lundblade2a26abb2020-11-05 19:06:54 -08009017{
9018 QCBORItem Item;
9019 QCBORError nCBORError;
9020 QCBORDecodeContext DCtx;
9021
Laurence Lundbladeec290b82024-06-10 11:10:54 -07009022 // Improvement: rework this test to use only integer labels.
9023
Laurence Lundblade2a26abb2020-11-05 19:06:54 -08009024 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0);
9025
9026 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
9027 return 100+(int32_t)nCBORError;
9028 }
9029 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
9030 return 200;
9031 }
9032
Laurence Lundblade3427dee2021-06-20 11:11:24 -07009033 QCBORDecode_VPeekNext(&DCtx, &Item);
9034 if((nCBORError = QCBORDecode_GetError(&DCtx))) {
9035 return 150+(int32_t)nCBORError;
9036 }
9037 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
9038 return 250;
9039 }
9040
Laurence Lundblade2a26abb2020-11-05 19:06:54 -08009041 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
9042 return (int32_t)nCBORError;
9043 }
9044 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
9045 return 300;
9046 }
9047
9048 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
9049 return 400 + (int32_t)nCBORError;
9050 }
9051 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
9052 return 500;
9053 }
9054
9055 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
9056 return (int32_t)nCBORError;
9057 }
9058 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
9059 return 600;
9060 }
9061
9062 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
9063 return 900 + (int32_t)nCBORError;
9064 }
9065 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
9066 Item.uDataType != QCBOR_TYPE_INT64 ||
9067 Item.val.int64 != 42 ||
9068 Item.uDataAlloc ||
9069 Item.uLabelAlloc ||
9070 UsefulBufCompareToSZ(Item.label.string, "first integer")) {
9071 return 1000;
9072 }
9073
9074 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
9075 return 1100 + (int32_t)nCBORError;
9076 }
9077
9078 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
9079 Item.uDataType != QCBOR_TYPE_INT64 ||
9080 Item.val.int64 != 42 ||
9081 Item.uDataAlloc ||
9082 Item.uLabelAlloc ||
9083 UsefulBufCompareToSZ(Item.label.string, "first integer")) {
9084 return 1200;
9085 }
9086
9087
9088 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
9089 return 1300 + (int32_t)nCBORError;
9090 }
9091 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
9092 Item.uDataAlloc ||
9093 Item.uLabelAlloc ||
9094 UsefulBufCompareToSZ(Item.label.string, "an array of two strings") ||
9095 Item.uDataType != QCBOR_TYPE_ARRAY ||
Laurence Lundbladecf41c522021-02-20 10:19:07 -07009096 Item.val.uCount != 2) {
Laurence Lundblade2a26abb2020-11-05 19:06:54 -08009097 return 1400;
Laurence Lundbladecf41c522021-02-20 10:19:07 -07009098 }
Laurence Lundblade2a26abb2020-11-05 19:06:54 -08009099
9100 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
9101 return 1500 + (int32_t)nCBORError;
9102 }
9103 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
9104 Item.uDataAlloc ||
9105 Item.uLabelAlloc ||
9106 UsefulBufCompareToSZ(Item.val.string, "string1")) {
9107 return 1600;
9108 }
9109
9110 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
9111 return 1700 + (int32_t)nCBORError;
9112 }
9113 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
9114 Item.uDataAlloc ||
9115 Item.uLabelAlloc ||
9116 UsefulBufCompareToSZ(Item.val.string, "string2")) {
9117 return 1800;
9118 }
9119
9120 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
9121 return (int32_t)nCBORError;
9122 }
9123 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
9124 Item.uDataAlloc ||
9125 Item.uLabelAlloc ||
9126 UsefulBufCompareToSZ(Item.val.string, "string2")) {
9127 return 1900;
9128 }
9129
9130 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
9131 return (int32_t)nCBORError;
9132 }
9133 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
9134 Item.uDataAlloc ||
9135 Item.uLabelAlloc ||
9136 UsefulBufCompareToSZ(Item.val.string, "string2")) {
9137 return 2000;
9138 }
9139
9140
9141 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
9142 return 2100 + (int32_t)nCBORError;
9143 }
9144 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
9145 Item.uDataAlloc ||
9146 Item.uLabelAlloc ||
9147 UsefulBufCompareToSZ(Item.label.string, "map in a map") ||
9148 Item.uDataType != QCBOR_TYPE_MAP ||
9149 Item.val.uCount != 4) {
9150 return 2100;
9151 }
9152
9153 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
9154 return 2200 + (int32_t)nCBORError;
9155 }
9156 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
9157 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("bytes 1"))||
9158 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
9159 Item.uDataAlloc ||
9160 Item.uLabelAlloc ||
9161 UsefulBufCompareToSZ(Item.val.string, "xxxx")) {
9162 return 2300;
9163 }
9164
9165 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
9166 return 2400 + (int32_t)nCBORError;
9167 }
9168 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
9169 UsefulBufCompareToSZ(Item.label.string, "bytes 2") ||
9170 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
9171 Item.uDataAlloc ||
9172 Item.uLabelAlloc ||
9173 UsefulBufCompareToSZ(Item.val.string, "yyyy")) {
9174 return 2500;
9175 }
9176
9177 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
9178 return 2600 + (int32_t)nCBORError;
9179 }
9180 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
9181 UsefulBufCompareToSZ(Item.label.string, "bytes 2") ||
9182 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
9183 Item.uDataAlloc ||
9184 Item.uLabelAlloc ||
9185 UsefulBufCompareToSZ(Item.val.string, "yyyy")) {
9186 return 2700;
9187 }
9188
9189 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
9190 return 2800 + (int32_t)nCBORError;
9191 }
9192 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
9193 Item.uDataAlloc ||
9194 Item.uLabelAlloc ||
9195 UsefulBufCompareToSZ(Item.label.string, "another int") ||
9196 Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundbladecf41c522021-02-20 10:19:07 -07009197 Item.val.int64 != 98) {
Laurence Lundblade2a26abb2020-11-05 19:06:54 -08009198 return 2900;
Laurence Lundbladecf41c522021-02-20 10:19:07 -07009199 }
Laurence Lundblade2a26abb2020-11-05 19:06:54 -08009200
9201 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
9202 return 3000 + (int32_t)nCBORError;
9203 }
9204 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
9205 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("text 2"))||
9206 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
9207 Item.uDataAlloc ||
9208 Item.uLabelAlloc ||
9209 UsefulBufCompareToSZ(Item.val.string, "lies, damn lies and statistics")) {
9210 return 3100;
9211 }
9212
9213 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
9214 return 3200 + (int32_t)nCBORError;
9215 }
9216 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
9217 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("text 2"))||
9218 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
9219 Item.uDataAlloc ||
9220 Item.uLabelAlloc ||
9221 UsefulBufCompareToSZ(Item.val.string, "lies, damn lies and statistics")) {
9222 return 3300;
9223 }
9224
Laurence Lundblade3427dee2021-06-20 11:11:24 -07009225 nCBORError = QCBORDecode_PeekNext(&DCtx, &Item);
9226 if(nCBORError != QCBOR_ERR_NO_MORE_ITEMS) {
9227 return 3300 + (int32_t)nCBORError;
9228 }
9229
9230 QCBORDecode_VPeekNext(&DCtx, &Item);
9231 nCBORError = QCBORDecode_GetError(&DCtx);
9232 if(nCBORError != QCBOR_ERR_NO_MORE_ITEMS) {
9233 return 3400 + (int32_t)nCBORError;
9234 }
9235
9236 QCBORDecode_VPeekNext(&DCtx, &Item);
9237 nCBORError = QCBORDecode_GetError(&DCtx);
9238 if(nCBORError != QCBOR_ERR_NO_MORE_ITEMS) {
9239 return 3500 + (int32_t)nCBORError;
9240 }
Laurence Lundbladecf41c522021-02-20 10:19:07 -07009241
9242
9243 // Rewind to top level after entering several maps
9244 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0);
9245
9246 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
9247 return (int32_t)nCBORError;
9248 }
9249 if(Item.uDataType != QCBOR_TYPE_MAP ||
9250 Item.val.uCount != 3) {
9251 return 400;
9252 }
9253
9254 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
9255 return 4000+(int32_t)nCBORError;
9256 }
9257
9258 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
9259 Item.uDataType != QCBOR_TYPE_INT64 ||
9260 Item.val.int64 != 42 ||
9261 Item.uDataAlloc ||
9262 Item.uLabelAlloc ||
9263 UsefulBufCompareToSZ(Item.label.string, "first integer")) {
9264 return 4100;
9265 }
9266
9267 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
9268 return 4100+(int32_t)nCBORError;
9269 }
9270 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
9271 Item.uDataAlloc ||
9272 Item.uLabelAlloc ||
9273 UsefulBufCompareToSZ(Item.label.string, "an array of two strings") ||
9274 Item.uDataType != QCBOR_TYPE_ARRAY ||
9275 Item.val.uCount != 2) {
9276 return 4200;
9277 }
9278
9279 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
9280 return 4200+(int32_t)nCBORError;
9281 }
9282 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
9283 Item.uDataAlloc ||
9284 Item.uLabelAlloc ||
9285 UsefulBufCompareToSZ(Item.val.string, "string1")) {
9286 return 4300;
9287 }
9288
9289 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
9290 return 4300+(int32_t)nCBORError;
9291 }
9292 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
9293 Item.uDataAlloc ||
9294 Item.uLabelAlloc ||
9295 UsefulBufCompareToSZ(Item.val.string, "string2")) {
9296 return 4400;
9297 }
9298
9299 QCBORDecode_Rewind(&DCtx);
9300
9301 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
9302 return 4400+(int32_t)nCBORError;
9303 }
9304 if(Item.uDataType != QCBOR_TYPE_MAP ||
9305 Item.val.uCount != 3) {
9306 return 4500;
9307 }
9308
9309 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
9310 return (int32_t)nCBORError;
9311 }
9312
9313 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
9314 Item.uDataType != QCBOR_TYPE_INT64 ||
9315 Item.val.int64 != 42 ||
9316 Item.uDataAlloc ||
9317 Item.uLabelAlloc ||
9318 UsefulBufCompareToSZ(Item.label.string, "first integer")) {
9319 return 4600;
9320 }
9321
9322 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
9323 return (int32_t)nCBORError;
9324 }
9325 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
9326 Item.uDataAlloc ||
9327 Item.uLabelAlloc ||
9328 UsefulBufCompareToSZ(Item.label.string, "an array of two strings") ||
9329 Item.uDataType != QCBOR_TYPE_ARRAY ||
9330 Item.val.uCount != 2) {
9331 return 4700;
9332 }
9333
9334 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
9335 return (int32_t)nCBORError;
9336 }
9337 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
9338 Item.uDataAlloc ||
9339 Item.uLabelAlloc ||
9340 UsefulBufCompareToSZ(Item.val.string, "string1")) {
9341 return 4800;
9342 }
9343
9344 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
9345 return 4900+(int32_t)nCBORError;
9346 }
9347 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
9348 Item.uDataAlloc ||
9349 Item.uLabelAlloc ||
9350 UsefulBufCompareToSZ(Item.val.string, "string2")) {
9351 return 5000;
9352 }
9353
9354
9355 // Rewind an entered map
9356 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0);
9357
9358 QCBORDecode_EnterMap(&DCtx, NULL);
9359
9360 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
9361 return 5100+(int32_t)nCBORError;
9362 }
9363
9364 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
9365 Item.uDataType != QCBOR_TYPE_INT64 ||
9366 Item.val.int64 != 42 ||
9367 Item.uDataAlloc ||
9368 Item.uLabelAlloc ||
9369 UsefulBufCompareToSZ(Item.label.string, "first integer")) {
9370 return 5200;
9371 }
9372
9373 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
9374 return 5200+(int32_t)nCBORError;
9375 }
9376 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
9377 Item.uDataAlloc ||
9378 Item.uLabelAlloc ||
9379 UsefulBufCompareToSZ(Item.label.string, "an array of two strings") ||
9380 Item.uDataType != QCBOR_TYPE_ARRAY ||
9381 Item.val.uCount != 2) {
9382 return -5300;
9383 }
9384
9385 QCBORDecode_Rewind(&DCtx);
9386
9387 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
9388 return 5300+(int32_t)nCBORError;
9389 }
9390
9391 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
9392 Item.uDataType != QCBOR_TYPE_INT64 ||
9393 Item.val.int64 != 42 ||
9394 Item.uDataAlloc ||
9395 Item.uLabelAlloc ||
9396 UsefulBufCompareToSZ(Item.label.string, "first integer")) {
9397 return 5400;
9398 }
9399
9400 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
9401 return 5400+(int32_t)nCBORError;
9402 }
9403 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
9404 Item.uDataAlloc ||
9405 Item.uLabelAlloc ||
9406 UsefulBufCompareToSZ(Item.label.string, "an array of two strings") ||
9407 Item.uDataType != QCBOR_TYPE_ARRAY ||
9408 Item.val.uCount != 2) {
9409 return 5500;
9410 }
9411
9412
9413 // Rewind and entered array inside an entered map
9414 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0);
9415
9416 QCBORDecode_EnterMap(&DCtx, NULL);
9417
9418 QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings");
9419
9420 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
9421 return 5600+(int32_t)nCBORError;
9422 }
9423 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
9424 Item.uDataAlloc ||
9425 Item.uLabelAlloc ||
9426 UsefulBufCompareToSZ(Item.val.string, "string1")) {
9427 return 5700;
9428 }
9429
9430 QCBORDecode_Rewind(&DCtx);
9431
9432 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
9433 return 5700+(int32_t)nCBORError;
9434 }
9435 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
9436 Item.uDataAlloc ||
9437 Item.uLabelAlloc ||
9438 UsefulBufCompareToSZ(Item.val.string, "string1")) {
9439 return 5800;
9440 }
9441
9442 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
9443 return (int32_t)nCBORError;
9444 }
9445 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
9446 Item.uDataAlloc ||
9447 Item.uLabelAlloc ||
9448 UsefulBufCompareToSZ(Item.val.string, "string2")) {
9449 return 5900;
9450 }
9451
9452 QCBORDecode_Rewind(&DCtx);
9453
9454 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
9455 return 5900+(int32_t)nCBORError;
9456 }
9457 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
9458 Item.uDataAlloc ||
9459 Item.uLabelAlloc ||
9460 UsefulBufCompareToSZ(Item.val.string, "string1")) {
9461 return 6000;
9462 }
9463
9464
9465 // Rewind a byte string inside an array inside an array
9466 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidWrappedMapEncoded), 0);
9467
9468 QCBORDecode_EnterArray(&DCtx, NULL);
9469
9470 uint64_t i;
9471 QCBORDecode_GetUInt64(&DCtx, &i);
9472
9473 QCBORDecode_EnterArray(&DCtx, NULL);
9474
9475 QCBORDecode_EnterBstrWrapped(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
9476 if(QCBORDecode_GetError(&DCtx)) {
9477 return 6100;
9478 }
9479
9480 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
9481 return (int32_t)nCBORError;
9482 }
9483 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
9484 return 6200;
9485 }
9486
9487 QCBORDecode_Rewind(&DCtx);
9488
9489 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
9490 return 6300+(int32_t)nCBORError;
9491 }
9492 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
9493 return 6400;
9494 }
9495
9496#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
9497 // Rewind a byte string inside an indefinite-length array inside
9498 // indefinite-length array
9499
9500 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidIndefWrappedMapEncoded), 0);
9501
9502 QCBORDecode_EnterArray(&DCtx, NULL);
9503
9504 QCBORDecode_GetUInt64(&DCtx, &i);
9505
9506 QCBORDecode_EnterArray(&DCtx, NULL);
9507
9508 QCBORDecode_EnterBstrWrapped(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
9509 if(QCBORDecode_GetError(&DCtx)) {
9510 return 6500;
9511 }
9512
9513 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
9514 return 6600+(int32_t)nCBORError;
9515 }
9516 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
9517 return 6700;
9518 }
9519
9520 QCBORDecode_Rewind(&DCtx);
9521
9522 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
9523 return 6800+(int32_t)nCBORError;
9524 }
9525 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
9526 return 6900;
9527 }
9528#endif
9529
9530 // Rewind an empty map
9531 // [100, {}]
9532 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pWithEmptyMap), 0);
9533 QCBORDecode_EnterArray(&DCtx, NULL);
9534 QCBORDecode_GetUInt64(&DCtx, &i);
9535 if(i != 100) {
9536 return 7010;
9537 }
9538 QCBORDecode_EnterMap(&DCtx, NULL);
9539
9540 /* Do it 5 times to be sure multiple rewinds work */
9541 for(int n = 0; n < 5; n++) {
9542 nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
9543 if(nCBORError != QCBOR_ERR_NO_MORE_ITEMS) {
9544 return 7000 + n;
9545 }
9546 QCBORDecode_Rewind(&DCtx);
9547 }
9548 QCBORDecode_ExitMap(&DCtx);
9549 QCBORDecode_Rewind(&DCtx);
9550 QCBORDecode_GetUInt64(&DCtx, &i);
9551 if(i != 100) {
9552 return 7010;
9553 }
9554 QCBORDecode_ExitArray(&DCtx);
9555 QCBORDecode_Rewind(&DCtx);
9556 QCBORDecode_EnterArray(&DCtx, NULL);
9557 i = 9;
9558 QCBORDecode_GetUInt64(&DCtx, &i);
9559 if(i != 100) {
9560 return 7020;
9561 }
9562 if(QCBORDecode_GetError(&DCtx)){
9563 return 7030;
9564 }
9565
9566 // Rewind an empty indefinite length map
9567#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
9568 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pWithEmptyMapInDef), 0);
9569 QCBORDecode_EnterArray(&DCtx, NULL);
9570 QCBORDecode_GetUInt64(&DCtx, &i);
9571 if(i != 100) {
9572 return 7810;
9573 }
9574 QCBORDecode_EnterMap(&DCtx, NULL);
9575
9576 /* Do it 5 times to be sure multiple rewinds work */
9577 for(int n = 0; n < 5; n++) {
9578 nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
9579 if(nCBORError != QCBOR_ERR_NO_MORE_ITEMS) {
9580 return 7800 + n;
9581 }
9582 QCBORDecode_Rewind(&DCtx);
9583 }
9584 QCBORDecode_ExitMap(&DCtx);
9585 QCBORDecode_Rewind(&DCtx);
9586 QCBORDecode_GetUInt64(&DCtx, &i);
9587 if(i != 100) {
9588 return 7810;
9589 }
9590 QCBORDecode_ExitArray(&DCtx);
9591 QCBORDecode_Rewind(&DCtx);
9592 QCBORDecode_EnterArray(&DCtx, NULL);
9593 i = 9;
9594 QCBORDecode_GetUInt64(&DCtx, &i);
9595 if(i != 100) {
9596 return 7820;
9597 }
9598 if(QCBORDecode_GetError(&DCtx)){
9599 return 7830;
9600 }
9601#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
9602
9603 // Rewind an indefnite length byte-string wrapped sequence
9604#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
9605 QCBORDecode_Init(&DCtx,
9606 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pWrappedByIndefiniteLength),
9607 0);
9608 UsefulBuf_MAKE_STACK_UB(Pool, 100);
9609 QCBORDecode_SetMemPool(&DCtx, Pool, 0);
9610
9611 QCBORDecode_EnterArray(&DCtx, NULL);
9612 QCBORDecode_EnterBstrWrapped(&DCtx, 2, NULL);
Laurence Lundblade31fddb72024-05-13 13:03:35 -07009613 if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_CANNOT_ENTER_ALLOCATED_STRING) {
Laurence Lundbladecf41c522021-02-20 10:19:07 -07009614 return 7300;
9615 }
9616
9617 /*
Laurence Lundblade31fddb72024-05-13 13:03:35 -07009618 Improvement: Fix QCBORDecode_EnterBstrWrapped() so it can work on
9619 allocated strings. This is a fairly big job because of all the
9620 UsefulBuf internal book keeping that needs tweaking.
Laurence Lundbladecf41c522021-02-20 10:19:07 -07009621 QCBORDecode_GetUInt64(&DCtx, &i);
9622 if(i != 42) {
9623 return 7110;
9624 }
9625 QCBORDecode_Rewind(&DCtx);
9626 QCBORDecode_GetUInt64(&DCtx, &i);
9627 if(i != 42) {
9628 return 7220;
Laurence Lundblade31fddb72024-05-13 13:03:35 -07009629 }
9630 */
Laurence Lundblade37286c02022-09-03 10:05:02 -07009631
Laurence Lundbladeec290b82024-06-10 11:10:54 -07009632#endif /* ! QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
Laurence Lundbladecf41c522021-02-20 10:19:07 -07009633
9634
9635 // Rewind an indefnite length byte-string wrapped sequence
9636
Laurence Lundblade2a26abb2020-11-05 19:06:54 -08009637 return 0;
9638}
Laurence Lundblade9f9c3732021-03-23 09:38:46 -07009639
Laurence Lundbladeec290b82024-06-10 11:10:54 -07009640#endif /* ! QCBOR_DISABLE_NON_INTEGER_LABELS */
Laurence Lundblade9f9c3732021-03-23 09:38:46 -07009641
9642
9643static const uint8_t spBooleansInMap[] =
9644{
9645 0xa1, 0x08, 0xf5
9646};
9647
9648static const uint8_t spBooleansInMapWrongType[] =
9649{
9650 0xa1, 0x08, 0xf6
9651};
9652
9653static const uint8_t spBooleansInMapNWF[] =
9654{
9655 0xa1, 0x08, 0x1a
9656};
9657
Laurence Lundblade8782dd32021-04-27 04:15:37 -07009658static const uint8_t spNullInMap[] =
9659{
9660 0xa1, 0x08, 0xf6
9661};
9662
9663static const uint8_t spUndefinedInMap[] =
9664{
9665 0xa1, 0x08, 0xf7
9666};
9667
Laurence Lundblade9f9c3732021-03-23 09:38:46 -07009668
Laurence Lundbladecdbbc192024-06-28 15:13:04 -07009669#ifndef QCBOR_DISABLE_TAGS
9670static const uint8_t spTaggedSimples[] =
9671{
9672 0xd8, 0x58, 0xd8, 0x2c, 0xd6, 0xf5,
9673 0xd9, 0x0f, 0xA0, 0xf7
9674};
9675#endif /* ! QCBOR_DISABLE_TAGS */
9676
9677
9678
Laurence Lundblade9f9c3732021-03-23 09:38:46 -07009679int32_t BoolTest(void)
9680{
9681 QCBORDecodeContext DCtx;
9682 bool b;
9683
Laurence Lundblade8782dd32021-04-27 04:15:37 -07009684 QCBORDecode_Init(&DCtx,
9685 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMap),
9686 0);
Laurence Lundblade9f9c3732021-03-23 09:38:46 -07009687 QCBORDecode_EnterMap(&DCtx, NULL);
9688 QCBORDecode_GetBool(&DCtx, &b);
9689 if(QCBORDecode_GetAndResetError(&DCtx) || !b) {
9690 return 1;
9691 }
9692
9693 QCBORDecode_GetBoolInMapN(&DCtx, 7, &b);
9694 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_LABEL_NOT_FOUND) {
9695 return 2;
9696 }
9697
9698 QCBORDecode_GetBoolInMapN(&DCtx, 8, &b);
9699 if(QCBORDecode_GetAndResetError(&DCtx) || !b) {
9700 return 3;
9701 }
9702
9703
9704 QCBORDecode_GetBoolInMapSZ(&DCtx, "xx", &b);
9705 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_LABEL_NOT_FOUND) {
9706 return 4;
9707 }
9708
Laurence Lundblade8782dd32021-04-27 04:15:37 -07009709 QCBORDecode_Init(&DCtx,
9710 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMapWrongType),
9711 0);
Laurence Lundblade9f9c3732021-03-23 09:38:46 -07009712 QCBORDecode_EnterMap(&DCtx, NULL);
9713 QCBORDecode_GetBool(&DCtx, &b);
9714 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
9715 return 5;
9716 }
9717
Laurence Lundblade8782dd32021-04-27 04:15:37 -07009718 QCBORDecode_Init(&DCtx,
9719 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMapNWF),
9720 0);
Laurence Lundblade9f9c3732021-03-23 09:38:46 -07009721 QCBORDecode_EnterMap(&DCtx, NULL);
9722 QCBORDecode_GetBool(&DCtx, &b);
9723 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_HIT_END) {
9724 return 6;
9725 }
9726
Laurence Lundblade8782dd32021-04-27 04:15:37 -07009727
9728 QCBORDecode_Init(&DCtx,
9729 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spNullInMap),
9730 0);
9731 QCBORDecode_EnterMap(&DCtx, NULL);
9732 QCBORDecode_GetNull(&DCtx);
9733 if(QCBORDecode_GetAndResetError(&DCtx)) {
9734 return 7;
9735 }
9736
9737 QCBORDecode_Init(&DCtx,
9738 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMap),
9739 0);
9740 QCBORDecode_EnterMap(&DCtx, NULL);
9741 QCBORDecode_GetNull(&DCtx);
9742 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
9743 return 8;
9744 }
9745
9746 QCBORDecode_Init(&DCtx,
9747 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spNullInMap),
9748 0);
9749 QCBORDecode_EnterMap(&DCtx, NULL);
9750 QCBORDecode_GetNullInMapN(&DCtx, 8);
9751 if(QCBORDecode_GetAndResetError(&DCtx)) {
9752 return 9;
9753 }
9754
9755 QCBORDecode_Init(&DCtx,
9756 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMap),
9757 0);
9758 QCBORDecode_EnterMap(&DCtx, NULL);
9759 QCBORDecode_GetNullInMapN(&DCtx, 8);
9760 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
9761 return 10;
9762 }
9763
9764 QCBORDecode_Init(&DCtx,
9765 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMapNWF),
9766 0);
9767 QCBORDecode_EnterMap(&DCtx, NULL);
9768 QCBORDecode_GetUndefined(&DCtx);
9769 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_HIT_END) {
9770 return 11;
9771 }
9772
9773 QCBORDecode_Init(&DCtx,
9774 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUndefinedInMap),
9775 0);
9776 QCBORDecode_EnterMap(&DCtx, NULL);
9777 QCBORDecode_GetUndefined(&DCtx);
9778 if(QCBORDecode_GetAndResetError(&DCtx)) {
9779 return 12;
9780 }
9781
9782 QCBORDecode_Init(&DCtx,
9783 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMap),
9784 0);
9785 QCBORDecode_EnterMap(&DCtx, NULL);
9786 QCBORDecode_GetUndefined(&DCtx);
9787 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
9788 return 13;
9789 }
9790
9791 QCBORDecode_Init(&DCtx,
9792 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUndefinedInMap),
9793 0);
9794 QCBORDecode_EnterMap(&DCtx, NULL);
9795 QCBORDecode_GetUndefinedInMapN(&DCtx, 8);
9796 if(QCBORDecode_GetAndResetError(&DCtx)) {
9797 return 14;
9798 }
9799
9800 QCBORDecode_Init(&DCtx,
9801 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMap),
9802 0);
9803 QCBORDecode_EnterMap(&DCtx, NULL);
9804 QCBORDecode_GetUndefinedInMapN(&DCtx, 8);
9805 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
9806 return 15;
9807 }
9808
9809 QCBORDecode_Init(&DCtx,
9810 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMapNWF),
9811 0);
9812 QCBORDecode_EnterMap(&DCtx, NULL);
9813 QCBORDecode_GetUndefined(&DCtx);
9814 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_HIT_END) {
9815 return 15;
9816 }
9817
Laurence Lundbladecdbbc192024-06-28 15:13:04 -07009818#ifndef QCBOR_DISABLE_TAGS
9819 QCBORDecode_Init(&DCtx,
9820 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTaggedSimples),
9821 0);
9822 QCBORDecode_GetBool(&DCtx, &b);
9823 if(QCBORDecode_GetNthTagOfLast(&DCtx, 0) != 22) {
9824 return 401;
9825 }
9826 if(QCBORDecode_GetNthTagOfLast(&DCtx, 1) != 44) {
9827 return 402;
9828 }
9829 if(QCBORDecode_GetNthTagOfLast(&DCtx, 2) != 88) {
9830 return 403;
9831 }
9832 if(QCBORDecode_GetNthTagOfLast(&DCtx, 3) != CBOR_TAG_INVALID64) {
9833 return 404;
9834 }
9835 QCBORDecode_GetUndefined(&DCtx);
9836 if(QCBORDecode_GetNthTagOfLast(&DCtx, 0) != 4000) {
9837 return 405;
9838 }
9839 if(QCBORDecode_GetNthTagOfLast(&DCtx, 1) != CBOR_TAG_INVALID64) {
9840 return 406;
9841 }
9842 QCBORDecode_GetNull(&DCtx); /* Off the end */
9843 if(QCBORDecode_GetNthTagOfLast(&DCtx, 0) != CBOR_TAG_INVALID64) {
9844 return 407;
9845 }
9846#endif /* ! QCBOR_DISABLE_TAGS */
9847
Laurence Lundblade9f9c3732021-03-23 09:38:46 -07009848 return 0;
9849}
Laurence Lundbladef00b8be2024-03-08 10:34:33 -08009850
9851
Laurence Lundbladeec290b82024-06-10 11:10:54 -07009852#ifndef QCBOR_DISABLE_NON_INTEGER_LABELS
Laurence Lundbladea29f45a2024-05-14 15:55:19 -07009853static const uint8_t spExpectedArray2s[] = {
9854 0x82, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67,
9855 0x31, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67,
9856 0x32};
9857
9858#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
9859static const uint8_t spExpectedArray2sIndef[] = {
9860 0x9f, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67,
9861 0x31, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67,
9862 0x32, 0xff};
9863#endif
9864
9865static const uint8_t spExpectedMap4[] = {
9866 0xa4, 0x67, 0x62, 0x79, 0x74, 0x65, 0x73, 0x20,
9867 0x31, 0x44, 0x78, 0x78, 0x78, 0x78, 0x67, 0x62,
9868 0x79, 0x74, 0x65, 0x73, 0x20, 0x32, 0x44, 0x79,
9869 0x79, 0x79, 0x79, 0x6b, 0x61, 0x6e, 0x6f, 0x74,
9870 0x68, 0x65, 0x72, 0x20, 0x69, 0x6e, 0x74, 0x18,
9871 0x62, 0x66, 0x74, 0x65, 0x78, 0x74, 0x20, 0x32,
9872 0x78, 0x1e, 0x6c, 0x69, 0x65, 0x73, 0x2c, 0x20,
9873 0x64, 0x61, 0x6d, 0x6e, 0x20, 0x6c, 0x69, 0x65,
9874 0x73, 0x20, 0x61, 0x6e, 0x64, 0x20, 0x73, 0x74,
9875 0x61, 0x74, 0x69, 0x73, 0x74, 0x69, 0x63, 0x73};
9876
9877
9878#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
9879
9880static const uint8_t spExpectedMap4Indef[] = {
9881 0xbf, 0x67, 0x62, 0x79, 0x74, 0x65, 0x73, 0x20,
9882 0x31, 0x44, 0x78, 0x78, 0x78, 0x78, 0x67, 0x62,
9883 0x79, 0x74, 0x65, 0x73, 0x20, 0x32, 0x44, 0x79,
9884 0x79, 0x79, 0x79, 0x6b, 0x61, 0x6e, 0x6f, 0x74,
9885 0x68, 0x65, 0x72, 0x20, 0x69, 0x6e, 0x74, 0x18,
9886 0x62, 0x66, 0x74, 0x65, 0x78, 0x74, 0x20, 0x32,
9887 0x78, 0x1e, 0x6c, 0x69, 0x65, 0x73, 0x2c, 0x20,
9888 0x64, 0x61, 0x6d, 0x6e, 0x20, 0x6c, 0x69, 0x65,
9889 0x73, 0x20, 0x61, 0x6e, 0x64, 0x20, 0x73, 0x74,
9890 0x61, 0x74, 0x69, 0x73, 0x74, 0x69, 0x63, 0x73,
9891 0xff};
9892
9893/*
9894 * [[[[[0, []]]]], 0]
9895 */
9896static const uint8_t spDefAndIndef[] = {
9897 0x82,
9898 0x9f, 0x9f, 0x9f, 0x82, 0x00, 0x9f, 0xff, 0xff, 0xff, 0xff, 0x00
9899};
9900#endif /* !QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
9901
Laurence Lundbladea29f45a2024-05-14 15:55:19 -07009902#ifndef QCBOR_DISABLE_TAGS
9903/* An exp / mant tag in two nested arrays */
9904static const uint8_t spExpMant[] = {0x81, 0x81, 0xC4, 0x82, 0x20, 0x03};
9905#endif /* !QCBOR_DISABLE_TAGS */
Laurence Lundbladeec290b82024-06-10 11:10:54 -07009906#endif
Laurence Lundbladea29f45a2024-05-14 15:55:19 -07009907
Laurence Lundbladeec290b82024-06-10 11:10:54 -07009908#ifndef QCBOR_DISABLE_NON_INTEGER_LABELS
Laurence Lundbladea29f45a2024-05-14 15:55:19 -07009909int32_t GetMapAndArrayTest(void)
9910{
9911 QCBORDecodeContext DCtx;
9912 size_t uPosition ;
9913 QCBORItem Item;
9914 UsefulBufC ReturnedEncodedCBOR;
9915
Laurence Lundbladeec290b82024-06-10 11:10:54 -07009916 // Improvement: rework so it can run with QCBOR_DISABLE_NON_INTEGER_LABELS
Laurence Lundbladea29f45a2024-05-14 15:55:19 -07009917 QCBORDecode_Init(&DCtx,
9918 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded),
9919 0);
9920
9921 QCBORDecode_EnterMap(&DCtx, NULL);
9922 QCBORDecode_VGetNextConsume(&DCtx, &Item);
9923 QCBORDecode_GetArray(&DCtx, &Item, &ReturnedEncodedCBOR);
9924 if(QCBORDecode_GetError(&DCtx)) {
9925 return 1;
9926 }
9927 if(Item.val.uCount != 2) {
9928 return 2;
9929 }
9930 if(UsefulBuf_Compare(ReturnedEncodedCBOR, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedArray2s))) {
9931 return 3;
9932 }
9933
9934 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
9935 UsefulBuf_Compare(Item.label.string, UsefulBuf_FROM_SZ_LITERAL("an array of two strings"))) {
9936 return 4;
9937 }
9938
9939 uPosition = QCBORDecode_Tell(&DCtx);
9940
9941
9942 QCBORDecode_GetMap(&DCtx, &Item, &ReturnedEncodedCBOR);
9943 if(QCBORDecode_GetError(&DCtx)) {
9944 return 10;
9945 }
9946 if(Item.val.uCount != 4) {
9947 return 11;
9948 }
9949 if(UsefulBuf_Compare(ReturnedEncodedCBOR, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedMap4))) {
9950 return 12;
9951 }
9952 uPosition = QCBORDecode_Tell(&DCtx);
9953 QCBORDecode_GetArrayFromMapSZ(&DCtx,
9954 "an array of two strings",
9955 &Item,
9956 &ReturnedEncodedCBOR);
9957 if(QCBORDecode_GetError(&DCtx)) {
9958 return 20;
9959 }
9960 if(Item.val.uCount != 2) {
9961 return 21;
9962 }
9963 if(UsefulBuf_Compare(ReturnedEncodedCBOR, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedArray2s))) {
9964 return 22;
9965 }
9966 if(uPosition != QCBORDecode_Tell(&DCtx)) {
9967 return 23;
9968 }
9969
9970 QCBORDecode_Rewind(&DCtx);
9971
9972 uPosition = QCBORDecode_Tell(&DCtx);
9973 QCBORDecode_GetMapFromMapSZ(&DCtx, "map in a map", &Item, &ReturnedEncodedCBOR);
9974 if(QCBORDecode_GetError(&DCtx)) {
9975 return 30;
9976 }
9977 if(Item.val.uCount != 4) {
9978 return 31;
9979 }
9980 if(UsefulBuf_Compare(ReturnedEncodedCBOR, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedMap4))) {
9981 return 32;
9982 }
9983 if(uPosition != QCBORDecode_Tell(&DCtx)) {
9984 return 33;
9985 }
9986
9987 uPosition = QCBORDecode_Tell(&DCtx);
9988 QCBORDecode_GetArrayFromMapSZ(&DCtx, "map in a map", &Item, &ReturnedEncodedCBOR);
9989 if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
9990 return 40;
9991 }
9992 if(UINT32_MAX != QCBORDecode_Tell(&DCtx)) {
9993 return 41;
9994 }
9995 QCBORDecode_GetAndResetError(&DCtx);
9996 if(uPosition != QCBORDecode_Tell(&DCtx)) {
9997 return 42;
9998 }
9999
10000
10001#ifndef QCBOR_DISABLE_TAGS
10002 UsefulBufC ExpMant = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpMant);
10003 QCBORDecode_Init(&DCtx, ExpMant, 0);
10004 QCBORDecode_EnterArray(&DCtx, NULL);
10005 QCBORDecode_EnterArray(&DCtx, NULL);
10006 QCBORDecode_GetArray(&DCtx, &Item, &ReturnedEncodedCBOR);
10007 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) {
10008 return 200;
10009 }
10010 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
10011 return 201;
10012 }
10013 if(!QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_DECIMAL_FRACTION)) {
10014 return 202;
10015 }
10016 if(Item.val.uCount != 2) {
10017 return 201;
10018 }
10019 if(UsefulBuf_Compare(ReturnedEncodedCBOR, UsefulBuf_Tail(ExpMant, 2))) {
10020 return 205;
10021 }
10022#endif /* !QCBOR_DISABLE_TAGS */
10023
10024
10025#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
10026
10027 UsefulBufC DefAndIndef = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spDefAndIndef);
10028 QCBORDecode_Init(&DCtx, DefAndIndef, 0);
10029 QCBORDecode_EnterArray(&DCtx, NULL);
10030 QCBORDecode_GetArray(&DCtx, &Item, &ReturnedEncodedCBOR);
10031 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) {
10032 return 50;
10033 }
10034 if(UsefulBuf_Compare(ReturnedEncodedCBOR, UsefulBuf_Tail(UsefulBuf_Head(DefAndIndef, 11), 1))) {
10035 return 51;
10036 }
10037
10038 QCBORDecode_Init(&DCtx, DefAndIndef, 0);
10039 QCBORDecode_EnterArray(&DCtx, NULL);
10040 QCBORDecode_EnterArray(&DCtx, NULL);
10041 QCBORDecode_GetArray(&DCtx, &Item, &ReturnedEncodedCBOR);
10042 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) {
10043 return 52;
10044 }
10045 if(UsefulBuf_Compare(ReturnedEncodedCBOR, UsefulBuf_Tail(UsefulBuf_Head(DefAndIndef, 10), 2))) {
10046 return 53;
10047 }
10048
10049 QCBORDecode_Init(&DCtx, DefAndIndef, 0);
10050 QCBORDecode_EnterArray(&DCtx, NULL);
10051 QCBORDecode_EnterArray(&DCtx, NULL);
10052 QCBORDecode_EnterArray(&DCtx, NULL);
10053 QCBORDecode_GetArray(&DCtx, &Item, &ReturnedEncodedCBOR);
10054 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) {
10055 return 54;
10056 }
10057 if(UsefulBuf_Compare(ReturnedEncodedCBOR, UsefulBuf_Tail(UsefulBuf_Head(DefAndIndef, 9), 3))) {
10058 return 55;
10059 }
10060 QCBORDecode_Init(&DCtx, DefAndIndef, 0);
10061 QCBORDecode_EnterArray(&DCtx, NULL);
10062 QCBORDecode_EnterArray(&DCtx, NULL);
10063 QCBORDecode_EnterArray(&DCtx, NULL);
10064 QCBORDecode_EnterArray(&DCtx, NULL);
10065 QCBORDecode_GetArray(&DCtx, &Item, &ReturnedEncodedCBOR);
10066 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) {
10067 return 56;
10068 }
10069 if(UsefulBuf_Compare(ReturnedEncodedCBOR, UsefulBuf_Tail(UsefulBuf_Head(DefAndIndef, 8), 4))) {
10070 return 57;
10071 }
10072
10073 QCBORDecode_Init(&DCtx, DefAndIndef, 0);
10074 QCBORDecode_EnterArray(&DCtx, NULL);
10075 QCBORDecode_EnterArray(&DCtx, NULL);
10076 QCBORDecode_EnterArray(&DCtx, NULL);
10077 QCBORDecode_EnterArray(&DCtx, NULL);
10078 QCBORDecode_EnterArray(&DCtx, NULL);
10079 QCBORDecode_VGetNextConsume(&DCtx, &Item);
10080 QCBORDecode_GetArray(&DCtx, &Item, &ReturnedEncodedCBOR);
10081 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) {
10082 return 58;
10083 }
10084 if(UsefulBuf_Compare(ReturnedEncodedCBOR, UsefulBuf_Tail(UsefulBuf_Head(DefAndIndef, 8), 6))) {
10085 return 59;
10086 }
10087
10088
10089 QCBORDecode_Init(&DCtx,
10090 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapIndefEncoded),
10091 0);
10092
10093 QCBORDecode_EnterMap(&DCtx, NULL);
10094 QCBORDecode_VGetNextConsume(&DCtx, &Item);
10095 QCBORDecode_GetArray(&DCtx, &Item, &ReturnedEncodedCBOR);
10096 if(QCBORDecode_GetError(&DCtx)) {
10097 return 60;
10098 }
10099 if(Item.val.uCount != UINT16_MAX) {
10100 return 61;
10101 }
10102 if(UsefulBuf_Compare(ReturnedEncodedCBOR, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedArray2sIndef))) {
10103 return 62;
10104 }
10105
10106 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
10107 UsefulBuf_Compare(Item.label.string, UsefulBuf_FROM_SZ_LITERAL("an array of two strings"))) {
10108 return 63;
10109 }
10110
10111 uPosition = QCBORDecode_Tell(&DCtx);
10112
10113
10114 QCBORDecode_GetMap(&DCtx, &Item, &ReturnedEncodedCBOR);
10115 if(QCBORDecode_GetError(&DCtx)) {
10116 return 70;
10117 }
10118 if(Item.val.uCount != UINT16_MAX) {
10119 return 71;
10120 }
10121 if(UsefulBuf_Compare(ReturnedEncodedCBOR, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedMap4Indef))) {
10122 return 72;
10123 }
10124
10125
10126 uPosition = QCBORDecode_Tell(&DCtx);
10127 QCBORDecode_GetArrayFromMapSZ(&DCtx,
10128 "an array of two strings",
10129 &Item,
10130 &ReturnedEncodedCBOR);
10131 if(QCBORDecode_GetError(&DCtx)) {
10132 return 80;
10133 }
10134 if(Item.val.uCount != UINT16_MAX) {
10135 return 81;
10136 }
10137 if(UsefulBuf_Compare(ReturnedEncodedCBOR, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedArray2sIndef))) {
10138 return 82;
10139 }
10140 if(uPosition != QCBORDecode_Tell(&DCtx)) {
10141 return 83;
10142 }
10143
10144 QCBORDecode_Rewind(&DCtx);
10145
10146 uPosition = QCBORDecode_Tell(&DCtx);
10147 QCBORDecode_GetMapFromMapSZ(&DCtx, "map in a map", &Item, &ReturnedEncodedCBOR);
10148 if(QCBORDecode_GetError(&DCtx)) {
10149 return 90;
10150 }
10151 if(Item.val.uCount != UINT16_MAX) {
10152 return 91;
10153 }
10154 if(UsefulBuf_Compare(ReturnedEncodedCBOR, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedMap4Indef))) {
10155 return 92;
10156 }
10157 if(uPosition != QCBORDecode_Tell(&DCtx)) {
10158 return 93;
10159 }
10160
10161 uPosition = QCBORDecode_Tell(&DCtx);
10162 QCBORDecode_GetArrayFromMapSZ(&DCtx, "map in a map", &Item, &ReturnedEncodedCBOR);
10163 if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
10164 return 100;
10165 }
10166 if(UINT32_MAX != QCBORDecode_Tell(&DCtx)) {
10167 return 101;
10168 }
10169 QCBORDecode_GetAndResetError(&DCtx);
10170 if(uPosition != QCBORDecode_Tell(&DCtx)) {
10171 return 102;
10172 }
Laurence Lundbladeec290b82024-06-10 11:10:54 -070010173#endif /* ! QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
Laurence Lundbladea29f45a2024-05-14 15:55:19 -070010174
10175 return 0;
10176}
Laurence Lundbladeec290b82024-06-10 11:10:54 -070010177#endif /* ! QCBOR_DISABLE_NON_INTEGER_LABELS */
Laurence Lundbladea29f45a2024-05-14 15:55:19 -070010178
10179
Laurence Lundbladef00b8be2024-03-08 10:34:33 -080010180int32_t
10181ErrorHandlingTests(void)
10182{
10183 QCBORDecodeContext DCtx;
10184 QCBORItem Item;
10185 QCBORError uError;
10186 int64_t integer;
10187
10188 /* Test QCBORDecode_SetError() */
10189 QCBORDecode_Init(&DCtx,
10190 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded),
10191 QCBOR_DECODE_MODE_NORMAL);
10192
10193 QCBORDecode_SetError(&DCtx, QCBOR_ERR_FIRST_USER_DEFINED);
10194
10195 QCBORDecode_VGetNext(&DCtx, &Item);
10196
10197 uError = QCBORDecode_GetError(&DCtx);
10198
10199 if(uError != QCBOR_ERR_FIRST_USER_DEFINED) {
10200 return -1;
10201 }
10202
10203 if(Item.uLabelType != QCBOR_TYPE_NONE ||
10204 Item.uDataType != QCBOR_TYPE_NONE) {
10205 return -2;
10206 }
10207
10208
10209 /* Test data type returned from previous error */
10210 QCBORDecode_Init(&DCtx,
10211 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded),
10212 QCBOR_DECODE_MODE_NORMAL);
10213 QCBORDecode_GetInt64(&DCtx, &integer);
10214 uError = QCBORDecode_GetError(&DCtx);
10215 if(uError != QCBOR_ERR_UNEXPECTED_TYPE) {
10216 return -3;
10217 }
10218
10219 QCBORDecode_VGetNext(&DCtx, &Item);
10220 if(Item.uLabelType != QCBOR_TYPE_NONE ||
10221 Item.uDataType != QCBOR_TYPE_NONE) {
10222 return -2;
10223 }
10224 uError = QCBORDecode_GetError(&DCtx);
10225 if(uError != QCBOR_ERR_UNEXPECTED_TYPE) {
10226 return -3;
10227 }
10228
10229
10230 /* Test error classification functions */
10231
10232 if(!QCBORDecode_IsUnrecoverableError(QCBOR_ERR_INDEFINITE_STRING_CHUNK)) {
10233 return -10;
10234 }
10235 if(QCBORDecode_IsUnrecoverableError(QCBOR_SUCCESS)) {
10236 return -11;
10237 }
10238 if(!QCBORDecode_IsUnrecoverableError(QCBOR_ERR_INDEFINITE_STRING_CHUNK)) {
10239 return -12;
10240 }
10241 if(QCBORDecode_IsUnrecoverableError(QCBOR_ERR_DUPLICATE_LABEL)) {
10242 return -13;
10243 }
10244
10245 if(!QCBORDecode_IsNotWellFormedError(QCBOR_ERR_BAD_TYPE_7)) {
10246 return -20;
10247 }
10248 if(!QCBORDecode_IsNotWellFormedError(QCBOR_ERR_BAD_BREAK)) {
10249 return -21;
10250 }
10251 if(QCBORDecode_IsNotWellFormedError(QCBOR_SUCCESS)) {
10252 return -22;
10253 }
10254 if(QCBORDecode_IsNotWellFormedError(QCBOR_ERR_ARRAY_DECODE_TOO_LONG)) {
10255 return -23;
10256 }
10257
Laurence Lundbladecd34e582024-06-06 10:55:14 -070010258 /* Test error strings */
10259 const char *szErrString;
10260
10261 szErrString = qcbor_err_to_str(QCBOR_ERR_ARRAY_DECODE_TOO_LONG);
10262 if(szErrString == NULL) {
10263 return -100;
10264 }
10265 if(strcmp(szErrString, "QCBOR_ERR_ARRAY_DECODE_TOO_LONG")) {
10266 return -101;
10267 }
10268
10269 szErrString = qcbor_err_to_str(QCBOR_SUCCESS);
10270 if(szErrString == NULL) {
10271 return -102;
10272 }
10273 if(strcmp(szErrString, "QCBOR_SUCCESS")) {
10274 return -103;
10275 }
10276
10277 szErrString = qcbor_err_to_str(100);
10278 if(szErrString == NULL) {
10279 return -104;
10280 }
10281 if(strcmp(szErrString, "Unidentified QCBOR error")) {
10282 return -105;
10283 }
10284
10285 szErrString = qcbor_err_to_str(200);
10286 if(szErrString == NULL) {
10287 return -106;
10288 }
10289 if(strcmp(szErrString, "USER_DEFINED_200")) {
10290 return -107;
10291 }
10292
Laurence Lundbladee2226742024-08-16 10:50:23 -070010293 QCBORDecode_Init(&DCtx,
10294 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded),
10295 QCBOR_DECODE_MODE_NORMAL);
10296
10297 UsefulBufC Xx = QCBORDecode_RetrieveUndecodedInput(&DCtx);
10298 if(Xx.ptr != pValidMapEncoded) {
10299 return -200;
10300 }
10301 if(Xx.len != sizeof(pValidMapEncoded)) {
10302 return -201;
10303 }
10304
Laurence Lundbladef00b8be2024-03-08 10:34:33 -080010305 return 0;
10306}
Laurence Lundblade11654912024-05-09 11:49:24 -070010307
10308
10309int32_t TellTests(void)
10310{
10311 QCBORDecodeContext DCtx;
10312 QCBORItem Item;
10313 uint32_t uPosition;
10314 int nIndex;
10315 int64_t nDecodedInt;
10316
Laurence Lundbladeec290b82024-06-10 11:10:54 -070010317 // Improvement: rewrite so this can run with only integer labels
Laurence Lundblade11654912024-05-09 11:49:24 -070010318 static const uint32_t aPos[] =
Laurence Lundblade535bb5e2024-06-16 12:06:32 -070010319 {0, 1, 17, 42, 50, 58, 72, 85, 98, 112, 151};
Laurence Lundblade11654912024-05-09 11:49:24 -070010320 QCBORDecode_Init(&DCtx,
10321 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded),
10322 0);
10323 for(nIndex = 0; ; nIndex++) {
10324 uPosition = QCBORDecode_Tell(&DCtx);
10325 if(uPosition != aPos[nIndex]) {
10326 return nIndex;
10327 }
10328
Laurence Lundblade535bb5e2024-06-16 12:06:32 -070010329 if(QCBORDecode_EndCheck(&DCtx)) {
Laurence Lundblade11654912024-05-09 11:49:24 -070010330 break;
10331 }
10332
10333 QCBORDecode_VGetNext(&DCtx, &Item);
10334 }
10335
10336#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
10337 static const uint32_t aPosIndef[] =
Laurence Lundblade535bb5e2024-06-16 12:06:32 -070010338 {0, 1, 17, 42, 50, 59, 73, 86, 99, 113, 154};
Laurence Lundblade11654912024-05-09 11:49:24 -070010339 QCBORDecode_Init(&DCtx,
10340 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapIndefEncoded),
10341 0);
10342
10343 for(nIndex = 0; ; nIndex++) {
10344 uPosition = QCBORDecode_Tell(&DCtx);
10345 if(uPosition != aPosIndef[nIndex]) {
10346 return nIndex + 100;
10347 }
10348
Laurence Lundblade535bb5e2024-06-16 12:06:32 -070010349 if(QCBORDecode_EndCheck(&DCtx)) {
Laurence Lundblade11654912024-05-09 11:49:24 -070010350 break;
10351 }
10352
10353 QCBORDecode_VGetNext(&DCtx, &Item);
10354 }
10355#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
10356
10357 /* Next, some tests with entered maps and arrays */
10358 QCBORDecode_Init(&DCtx,
10359 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded),
10360 0);
10361 QCBORDecode_EnterMap(&DCtx, &Item);
10362 if(QCBORDecode_Tell(&DCtx) != 1) {
10363 return 1001;
10364 }
10365 QCBORDecode_GetInt64InMapSZ(&DCtx, "first integer", &nDecodedInt);
10366 if(QCBORDecode_Tell(&DCtx) != 1) {
10367 return 1002;
10368 }
10369 QCBORDecode_EnterMapFromMapSZ(&DCtx, "map in a map");
10370 if(QCBORDecode_Tell(&DCtx) != 72) {
10371 return 1003;
10372 }
10373
10374 QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt);
10375 if(nDecodedInt != 98) {
10376 return 1004;
10377 }
10378 /* Getting non-aggregate types doesn't affect cursor position. */
10379 if(QCBORDecode_Tell(&DCtx) != 72) {
10380 return 1005;
10381 }
10382 QCBORDecode_VGetNext(&DCtx, &Item);
10383 if(QCBORDecode_Tell(&DCtx) != 85) {
10384 return 1006;
10385 }
10386 QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt);
10387 if(nDecodedInt != 98) {
10388 return 1007;
10389 }
10390 /* Getting non-aggregate types doesn't affect cursor position. */
10391 if(QCBORDecode_Tell(&DCtx) != 85) {
10392 return 1008;
10393 }
10394
10395 QCBORDecode_ExitMap(&DCtx);
Laurence Lundblade535bb5e2024-06-16 12:06:32 -070010396 if(QCBORDecode_Tell(&DCtx) != 151) {
Laurence Lundblade11654912024-05-09 11:49:24 -070010397 return 1009;
10398 }
10399 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_NO_MORE_ITEMS) {
10400 return 1010;
10401 }
10402
10403#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
10404 /* Next, some tests with entered maps and arrays */
10405 QCBORDecode_Init(&DCtx,
10406 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapIndefEncoded),
10407 0);
10408 QCBORDecode_EnterMap(&DCtx, &Item);
10409 if(QCBORDecode_Tell(&DCtx) != 1) {
10410 return 2000;
10411 }
10412 QCBORDecode_GetInt64InMapSZ(&DCtx, "first integer", &nDecodedInt);
10413 if(QCBORDecode_Tell(&DCtx) != 1) {
10414 return 2001;
10415 }
10416 QCBORDecode_EnterMapFromMapSZ(&DCtx, "map in a map");
10417 if(QCBORDecode_Tell(&DCtx) != 73) {
10418 return 2002;
10419 }
10420
10421 QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt);
10422 if(nDecodedInt != 98) {
10423 return 2003;
10424 }
10425 /* Getting non-aggregate types doesn't affect cursor position. */
10426 if(QCBORDecode_Tell(&DCtx) != 73) {
10427 return 2004;
10428 }
10429 QCBORDecode_VGetNext(&DCtx, &Item);
10430 if(QCBORDecode_Tell(&DCtx) != 86) {
10431 return 2005;
10432 }
10433 QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt);
10434 if(nDecodedInt != 98) {
10435 return 2006;
10436 }
10437 /* Getting non-aggregate types doesn't affect cursor position. */
10438 if(QCBORDecode_Tell(&DCtx) != 86) {
10439 return 2007;
10440 }
10441
10442 QCBORDecode_ExitMap(&DCtx);
Laurence Lundblade535bb5e2024-06-16 12:06:32 -070010443 if(QCBORDecode_Tell(&DCtx) != 154) {
Laurence Lundblade11654912024-05-09 11:49:24 -070010444 return 2008;
10445 }
10446 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_NO_MORE_ITEMS) {
10447 return 2010;
10448 }
10449#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
10450
10451
10452
10453 /* Error state test */
10454 QCBORDecode_Init(&DCtx,
10455 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded),
10456 0);
10457 /* Cause an error */
10458 QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt);
10459 if(QCBORDecode_Tell(&DCtx) != UINT32_MAX) {
10460 return 3000;
10461 }
Laurence Lundblade535bb5e2024-06-16 12:06:32 -070010462 if(QCBORDecode_EndCheck(&DCtx) != QCBOR_ERR_MAP_NOT_ENTERED) {
10463 return 3001;
10464 }
10465
Laurence Lundblade11654912024-05-09 11:49:24 -070010466
10467 /* Empties tests */
10468 const uint8_t pMinimalCBOR[] = {0xa0}; // One empty map
10469 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pMinimalCBOR),0);
10470 if(QCBORDecode_Tell(&DCtx) != 0) {
10471 return 4000;
10472 }
Laurence Lundblade535bb5e2024-06-16 12:06:32 -070010473 if(QCBORDecode_EndCheck(&DCtx) != QCBOR_SUCCESS) {
10474 return 4008;
10475 }
Laurence Lundblade11654912024-05-09 11:49:24 -070010476 QCBORDecode_EnterMap(&DCtx, &Item);
10477 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) {
10478 return 4001;
10479 }
Laurence Lundblade535bb5e2024-06-16 12:06:32 -070010480 if(QCBORDecode_Tell(&DCtx) != 1) {
Laurence Lundblade11654912024-05-09 11:49:24 -070010481 return 4002;
10482 }
10483 QCBORDecode_ExitMap(&DCtx);
10484 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) {
Laurence Lundblade535bb5e2024-06-16 12:06:32 -070010485 return 4003;
Laurence Lundblade11654912024-05-09 11:49:24 -070010486 }
Laurence Lundblade535bb5e2024-06-16 12:06:32 -070010487 if(QCBORDecode_Tell(&DCtx) != 1) {
10488 return 4004;
10489 }
10490 if(QCBORDecode_EndCheck(&DCtx) != QCBOR_ERR_NO_MORE_ITEMS) {
10491 return 4005;
Laurence Lundblade11654912024-05-09 11:49:24 -070010492 }
10493 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_NO_MORE_ITEMS) {
10494 return 4010;
10495 }
10496
10497#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
10498 const uint8_t pMinimalIndefCBOR[] = {0xbf, 0xff}; // One empty map
10499 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pMinimalIndefCBOR),0);
10500 if(QCBORDecode_Tell(&DCtx) != 0) {
10501 return 4100;
10502 }
10503 QCBORDecode_EnterMap(&DCtx, &Item);
10504 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) {
10505 return 4101;
10506 }
Laurence Lundblade535bb5e2024-06-16 12:06:32 -070010507 if(QCBORDecode_Tell(&DCtx) != 2) {
Laurence Lundblade11654912024-05-09 11:49:24 -070010508 return 4102;
10509 }
10510 QCBORDecode_ExitMap(&DCtx);
10511 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) {
Laurence Lundblade535bb5e2024-06-16 12:06:32 -070010512 return 4103;
Laurence Lundblade11654912024-05-09 11:49:24 -070010513 }
Laurence Lundblade535bb5e2024-06-16 12:06:32 -070010514 if(QCBORDecode_Tell(&DCtx) != 2) {
10515 return 4104;
10516 }
10517 if(QCBORDecode_EndCheck(&DCtx) != QCBOR_ERR_NO_MORE_ITEMS) {
10518 return 4005;
Laurence Lundblade11654912024-05-09 11:49:24 -070010519 }
10520 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_NO_MORE_ITEMS) {
10521 return 4110;
10522 }
10523#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
10524
10525 /* Test on a CBOR sequence */
10526 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSequenceTestInput),0);
10527 if(QCBORDecode_Tell(&DCtx) != 0) {
10528 return 5000;
10529 }
10530 QCBORDecode_VGetNext(&DCtx, &Item);
10531 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) {
10532 return 5001;
10533 }
10534 if(QCBORDecode_Tell(&DCtx) != 11) {
10535 return 5002;
10536 }
10537 QCBORDecode_VGetNext(&DCtx, &Item);
10538 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) {
10539 return 5003;
10540 }
10541 if(QCBORDecode_Tell(&DCtx) != 12) {
10542 return 5004;
10543 }
10544 QCBORDecode_VGetNext(&DCtx, &Item);
10545 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) {
10546 return 5005;
10547 }
10548 if(QCBORDecode_Tell(&DCtx) != 17) {
10549 return 5006;
10550 }
10551 QCBORDecode_VGetNext(&DCtx, &Item);
10552 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) {
10553 return 5007;
10554 }
Laurence Lundblade535bb5e2024-06-16 12:06:32 -070010555 if(QCBORDecode_Tell(&DCtx) != 20) {
Laurence Lundblade11654912024-05-09 11:49:24 -070010556 return 5008;
10557 }
10558 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_NO_MORE_ITEMS) {
10559 return 5010;
10560 }
10561
10562
10563 QCBORDecode_Init(&DCtx,
10564 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded),
10565 0);
10566 QCBORDecode_EnterMap(&DCtx, &Item);
10567 QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings");
10568 if(QCBORDecode_Tell(&DCtx) != 42) {
10569 return 6001;
10570 }
10571 QCBORDecode_VGetNext(&DCtx, &Item);
10572 if(QCBORDecode_Tell(&DCtx) != 50) {
10573 return 6002;
10574 }
10575 QCBORDecode_VGetNext(&DCtx, &Item);
10576 if(QCBORDecode_Tell(&DCtx) != 58) {
10577 return 6008;
10578 }
10579 QCBORDecode_VGetNext(&DCtx, &Item);
10580 (void)QCBORDecode_GetAndResetError(&DCtx);
10581 if(QCBORDecode_Tell(&DCtx) != 58) {
10582 return 6003;
10583 }
10584 QCBORDecode_ExitArray(&DCtx);
10585 if(QCBORDecode_Tell(&DCtx) != 58) {
10586 return 6004;
10587 }
10588
10589 static const uint32_t aEmptiesPos[] =
Laurence Lundblade535bb5e2024-06-16 12:06:32 -070010590 {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 13, 15};
Laurence Lundblade11654912024-05-09 11:49:24 -070010591 QCBORDecode_Init(&DCtx,
10592 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(sEmpties),
10593 0);
10594 for(nIndex = 0; ; nIndex++) {
10595 uPosition = QCBORDecode_Tell(&DCtx);
10596 if(uPosition != aEmptiesPos[nIndex]) {
10597 return nIndex + 200;
10598 }
10599
Laurence Lundblade535bb5e2024-06-16 12:06:32 -070010600 if(QCBORDecode_EndCheck(&DCtx)) {
Laurence Lundblade11654912024-05-09 11:49:24 -070010601 break;
10602 }
10603
10604 QCBORDecode_VGetNext(&DCtx, &Item);
10605 }
10606
10607#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
10608 static const uint32_t aIndefEmptiesPos[] =
Laurence Lundblade535bb5e2024-06-16 12:06:32 -070010609 {0, 1, 2, 4, 5, 7, 8, 10, 12, 13, 16, 19, 25};
Laurence Lundblade11654912024-05-09 11:49:24 -070010610 QCBORDecode_Init(&DCtx,
10611 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(sEmptiesIndef),
10612 0);
10613 for(nIndex = 0; ; nIndex++) {
10614 uPosition = QCBORDecode_Tell(&DCtx);
10615 if(uPosition != aIndefEmptiesPos[nIndex]) {
10616 return nIndex + 300;
10617 }
10618
Laurence Lundblade535bb5e2024-06-16 12:06:32 -070010619 if(QCBORDecode_EndCheck(&DCtx)) {
Laurence Lundblade11654912024-05-09 11:49:24 -070010620 break;
10621 }
10622
10623 QCBORDecode_VGetNext(&DCtx, &Item);
10624 }
10625#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
10626
10627
10628 return 0;
10629}