blob: eeed2380bcc4b364a52b768e0abfa5b049ccc6d2 [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 Lundbladefab1b522018-10-19 13:40:52 +05301142/*
1143 Decode and thoroughly check a moderately complex
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001144 set of maps. Can be run in QCBOR_DECODE_MODE_NORMAL or in
1145 QCBOR_DECODE_MODE_MAP_STRINGS_ONLY.
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301146 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001147static int32_t ParseMapTest1(QCBORDecodeMode nMode)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001148{
1149 QCBORDecodeContext DCtx;
1150 QCBORItem Item;
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001151 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001152
Laurence Lundbladeee851742020-01-08 08:37:05 -08001153 QCBORDecode_Init(&DCtx,
1154 (UsefulBufC){pValidMapEncoded, sizeof(pValidMapEncoded)},
1155 nMode);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001156
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001157 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001158 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001159 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001160 if(Item.uDataType != QCBOR_TYPE_MAP ||
1161 Item.val.uCount != 3)
1162 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001163
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001164 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001165 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001166 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07001167
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001168 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001169 Item.uDataType != QCBOR_TYPE_INT64 ||
1170 Item.val.int64 != 42 ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301171 Item.uDataAlloc ||
1172 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001173 UsefulBufCompareToSZ(Item.label.string, "first integer")) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001174 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001175 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001176
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001177 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001178 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001179 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001180 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301181 Item.uDataAlloc ||
1182 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001183 UsefulBufCompareToSZ(Item.label.string, "an array of two strings") ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001184 Item.uDataType != QCBOR_TYPE_ARRAY ||
1185 Item.val.uCount != 2)
1186 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001187
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001188 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001189 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001190 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001191 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301192 Item.uDataAlloc ||
1193 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001194 UsefulBufCompareToSZ(Item.val.string, "string1")) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001195 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001196 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001197
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001198 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001199 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001200 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001201 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301202 Item.uDataAlloc ||
1203 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001204 UsefulBufCompareToSZ(Item.val.string, "string2")) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001205 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001206 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001207
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001208 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001209 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001210 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001211 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301212 Item.uDataAlloc ||
1213 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001214 UsefulBufCompareToSZ(Item.label.string, "map in a map") ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001215 Item.uDataType != QCBOR_TYPE_MAP ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001216 Item.val.uCount != 4) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001217 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001218 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001219
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001220 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001221 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001222 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001223 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001224 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("bytes 1"))||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001225 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301226 Item.uDataAlloc ||
1227 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001228 UsefulBufCompareToSZ(Item.val.string, "xxxx")) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001229 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001230 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001231
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001232 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001233 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001234 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001235 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001236 UsefulBufCompareToSZ(Item.label.string, "bytes 2") ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001237 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301238 Item.uDataAlloc ||
1239 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001240 UsefulBufCompareToSZ(Item.val.string, "yyyy")) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001241 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001242 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001243
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001244 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001245 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001246 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001247 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301248 Item.uDataAlloc ||
1249 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001250 UsefulBufCompareToSZ(Item.label.string, "another int") ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001251 Item.uDataType != QCBOR_TYPE_INT64 ||
1252 Item.val.int64 != 98)
1253 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001254
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001255 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001256 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001257 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001258 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001259 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("text 2"))||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001260 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301261 Item.uDataAlloc ||
1262 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001263 UsefulBufCompareToSZ(Item.val.string, "lies, damn lies and statistics")) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001264 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001265 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001266
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001267 return 0;
1268}
1269
1270
Laurence Lundblade5db34da2024-05-30 03:14:35 -07001271/* This test requires indef strings, HW float and preferred float,... */
1272#if !defined(QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS) && \
1273 !defined(QCBOR_DISABLE_FLOAT_HW_USE) && \
1274 !defined(QCBOR_DISABLE_PREFERRED_FLOAT) && \
1275 !defined(QCBOR_DISABLE_TAGS) && \
1276 !defined(QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS)
1277
1278/* Utility to decode a one byte string and match to letter. */
1279static QCBORError
1280CheckOneLetterString(QCBORDecodeContext *pDecode, uint8_t letter)
1281{
1282 UsefulBufC Text;
1283 QCBORError uErr;
1284
1285 QCBORDecode_GetTextString(pDecode, &Text);
1286 uErr = QCBORDecode_GetError(pDecode);
1287 if(uErr) {
1288 return uErr;
1289 }
1290
1291 if(Text.len != 1) {
1292 return QCBOR_ERR_FIRST_USER_DEFINED;
1293 }
1294
1295 if(*(const uint8_t *)Text.ptr != letter) {
1296 return QCBOR_ERR_FIRST_USER_DEFINED;
1297 }
1298
1299 return QCBOR_SUCCESS;
1300}
1301#endif
1302
1303
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001304/*
1305 Decode and thoroughly check a moderately complex
Laurence Lundbladed4cc1032020-10-12 04:19:47 -07001306 set of maps in the QCBOR_DECODE_MODE_MAP_AS_ARRAY mode.
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001307 */
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03001308int32_t ParseMapAsArrayTest(void)
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001309{
1310 QCBORDecodeContext DCtx;
Laurence Lundblade5db34da2024-05-30 03:14:35 -07001311 QCBORItem Item;
1312 QCBORError uErr;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001313
Laurence Lundbladeee851742020-01-08 08:37:05 -08001314 QCBORDecode_Init(&DCtx,
1315 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded),
1316 QCBOR_DECODE_MODE_MAP_AS_ARRAY);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001317
Laurence Lundblade5db34da2024-05-30 03:14:35 -07001318 if((uErr = QCBORDecode_GetNext(&DCtx, &Item))) {
1319 return MakeTestResultCode(1, 1, uErr);
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001320 }
Laurence Lundbladed61cbf32018-12-09 11:42:21 -08001321 if(Item.uDataType != QCBOR_TYPE_MAP_AS_ARRAY ||
1322 Item.val.uCount != 6) {
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001323 return -1;
1324 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001325
Laurence Lundblade5db34da2024-05-30 03:14:35 -07001326 if((uErr = QCBORDecode_GetNext(&DCtx, &Item))) {
1327 return (int32_t)uErr;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001328 }
1329 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1330 Item.uDataAlloc ||
1331 Item.uLabelAlloc ||
1332 Item.uLabelType != QCBOR_TYPE_NONE ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001333 UsefulBufCompareToSZ(Item.val.string, "first integer")) {
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001334 return -2;
1335 }
1336
Laurence Lundblade5db34da2024-05-30 03:14:35 -07001337 if((uErr = QCBORDecode_GetNext(&DCtx, &Item))) {
1338 return (int32_t)uErr;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001339 }
1340 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1341 Item.uDataType != QCBOR_TYPE_INT64 ||
1342 Item.val.int64 != 42 ||
1343 Item.uDataAlloc ||
1344 Item.uLabelAlloc) {
1345 return -3;
1346 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001347
Laurence Lundblade5db34da2024-05-30 03:14:35 -07001348 if((uErr = QCBORDecode_GetNext(&DCtx, &Item))) {
1349 return (int32_t)uErr;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001350 }
1351 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1352 Item.uDataAlloc ||
1353 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001354 UsefulBufCompareToSZ(Item.val.string, "an array of two strings") ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001355 Item.uDataType != QCBOR_TYPE_TEXT_STRING) {
1356 return -4;
1357 }
1358
Laurence Lundblade5db34da2024-05-30 03:14:35 -07001359 if((uErr = QCBORDecode_GetNext(&DCtx, &Item))) {
1360 return (int32_t)uErr;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001361 }
1362 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1363 Item.uDataAlloc ||
1364 Item.uLabelAlloc ||
1365 Item.uDataType != QCBOR_TYPE_ARRAY ||
1366 Item.val.uCount != 2) {
1367 return -5;
1368 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001369
Laurence Lundblade5db34da2024-05-30 03:14:35 -07001370 if((uErr = QCBORDecode_GetNext(&DCtx, &Item))) {
1371 return (int32_t)uErr;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001372 }
1373 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1374 Item.val.string.len != 7 ||
1375 Item.uDataAlloc ||
1376 Item.uLabelAlloc ||
1377 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("string1"))) {
1378 return -6;
1379 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001380
Laurence Lundblade5db34da2024-05-30 03:14:35 -07001381 if((uErr = QCBORDecode_GetNext(&DCtx, &Item))) {
1382 return (int32_t)uErr;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001383 }
1384 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1385 Item.uDataAlloc ||
1386 Item.uLabelAlloc ||
1387 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("string2"))) {
1388 return -7;
1389 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001390
1391
Laurence Lundblade5db34da2024-05-30 03:14:35 -07001392 if((uErr = QCBORDecode_GetNext(&DCtx, &Item))) {
1393 return (int32_t)uErr;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001394 }
1395 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1396 Item.uDataAlloc ||
1397 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001398 UsefulBufCompareToSZ(Item.val.string, "map in a map")) {
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001399 return -8;
1400 }
1401
Laurence Lundblade5db34da2024-05-30 03:14:35 -07001402 if((uErr = QCBORDecode_GetNext(&DCtx, &Item))) {
1403 return (int32_t)uErr;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001404 }
1405 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1406 Item.uDataAlloc ||
1407 Item.uLabelAlloc ||
Laurence Lundbladed61cbf32018-12-09 11:42:21 -08001408 Item.uDataType != QCBOR_TYPE_MAP_AS_ARRAY ||
1409 Item.val.uCount != 8) {
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001410 return -9;
1411 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001412
Laurence Lundblade5db34da2024-05-30 03:14:35 -07001413 if((uErr = QCBORDecode_GetNext(&DCtx, &Item))) {
1414 return (int32_t)uErr;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001415 }
1416 if(Item.uLabelType != QCBOR_TYPE_NONE ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001417 UsefulBufCompareToSZ(Item.val.string, "bytes 1") ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001418 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1419 Item.uDataAlloc ||
1420 Item.uLabelAlloc) {
1421 return -10;
1422 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001423
Laurence Lundblade5db34da2024-05-30 03:14:35 -07001424 if((uErr = QCBORDecode_GetNext(&DCtx, &Item))) {
1425 return (int32_t)uErr;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001426 }
1427 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1428 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
1429 Item.uDataAlloc ||
1430 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001431 UsefulBufCompareToSZ(Item.val.string, "xxxx")) {
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001432 return -11;
1433 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001434
Laurence Lundblade5db34da2024-05-30 03:14:35 -07001435 if((uErr = QCBORDecode_GetNext(&DCtx, &Item))) {
1436 return (int32_t)uErr;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001437 }
1438 if(Item.uLabelType != QCBOR_TYPE_NONE ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001439 UsefulBufCompareToSZ(Item.val.string, "bytes 2") ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001440 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1441 Item.uDataAlloc ||
1442 Item.uLabelAlloc) {
1443 return -12;
1444 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001445
Laurence Lundblade5db34da2024-05-30 03:14:35 -07001446 if((uErr = QCBORDecode_GetNext(&DCtx, &Item))) {
1447 return (int32_t)uErr;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001448 }
1449 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1450 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
1451 Item.uDataAlloc ||
1452 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001453 UsefulBufCompareToSZ(Item.val.string, "yyyy")) {
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001454 return -13;
1455 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001456
Laurence Lundblade5db34da2024-05-30 03:14:35 -07001457 if((uErr = QCBORDecode_GetNext(&DCtx, &Item))) {
1458 return (int32_t)uErr;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001459 }
1460 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1461 Item.uDataAlloc ||
1462 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001463 UsefulBufCompareToSZ(Item.val.string, "another int") ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001464 Item.uDataType != QCBOR_TYPE_TEXT_STRING) {
1465 return -14;
1466 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001467
Laurence Lundblade5db34da2024-05-30 03:14:35 -07001468 if((uErr = QCBORDecode_GetNext(&DCtx, &Item))) {
1469 return (int32_t)uErr;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001470 }
1471 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1472 Item.uDataAlloc ||
1473 Item.uLabelAlloc ||
1474 Item.uDataType != QCBOR_TYPE_INT64 ||
1475 Item.val.int64 != 98) {
1476 return -15;
1477 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001478
Laurence Lundblade5db34da2024-05-30 03:14:35 -07001479 if((uErr = QCBORDecode_GetNext(&DCtx, &Item))) {
1480 return (int32_t)uErr;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001481 }
1482 if(Item.uLabelType != QCBOR_TYPE_NONE ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001483 UsefulBufCompareToSZ(Item.val.string, "text 2") ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001484 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1485 Item.uDataAlloc ||
1486 Item.uLabelAlloc) {
1487 return -16;
1488 }
1489
Laurence Lundblade5db34da2024-05-30 03:14:35 -07001490 if((uErr = QCBORDecode_GetNext(&DCtx, &Item))) {
1491 return (int32_t)uErr;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001492 }
1493 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1494 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1495 Item.uDataAlloc ||
1496 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001497 UsefulBufCompareToSZ(Item.val.string, "lies, damn lies and statistics")) {
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001498 return -17;
1499 }
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07001500
1501
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001502 /*
1503 Test with map that nearly QCBOR_MAX_ITEMS_IN_ARRAY items in a
1504 map that when interpreted as an array will be too many. Test
1505 data just has the start of the map, not all the items in the map.
1506 */
1507 static const uint8_t pTooLargeMap[] = {0xb9, 0xff, 0xfd};
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07001508
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001509 QCBORDecode_Init(&DCtx,
1510 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pTooLargeMap),
1511 QCBOR_DECODE_MODE_MAP_AS_ARRAY);
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07001512
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001513 if((QCBOR_ERR_ARRAY_DECODE_TOO_LONG != QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001514 return -50;
1515 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001516
Laurence Lundblade5db34da2024-05-30 03:14:35 -07001517 /* This test requires indef strings, HW float and preferred float,... */
1518#if !defined(QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS) && \
1519 !defined(QCBOR_DISABLE_FLOAT_HW_USE) && \
1520 !defined(QCBOR_DISABLE_PREFERRED_FLOAT) && \
1521 !defined(QCBOR_DISABLE_TAGS) && \
1522 !defined(QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS)
1523
1524 UsefulBufC Encoded;
1525
1526 /* Big decode of a map with a wide variety or labels */
1527 QCBORDecode_Init(&DCtx,
1528 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pPerverseLabels),
1529 QCBOR_DECODE_MODE_MAP_AS_ARRAY);
1530 UsefulBuf_MAKE_STACK_UB(Pool, 100);
1531 QCBORDecode_SetMemPool(&DCtx, Pool, 0);
1532
1533 uErr = QCBORDecode_GetNext(&DCtx, &Item);
1534 if(uErr) {
1535 return MakeTestResultCode(10, 1, uErr);
1536 }
1537 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1538 Item.uDataType != QCBOR_TYPE_MAP_AS_ARRAY) {
1539 return MakeTestResultCode(10, 2, 0);
1540 }
1541
1542 uErr = QCBORDecode_GetNext(&DCtx, &Item);
1543 if(uErr) {
1544 return MakeTestResultCode(10, 3, uErr);
1545 }
1546 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1547 Item.uDataType != QCBOR_TYPE_TRUE) {
1548 return MakeTestResultCode(10, 4, 0);
1549 }
1550
1551 uErr = QCBORDecode_GetNext(&DCtx, &Item);
1552 if(uErr) {
1553 return MakeTestResultCode(10, 5, uErr);
1554 }
1555 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1556 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1557 ((const char *)Item.val.string.ptr)[0] != 'a') {
1558 return MakeTestResultCode(10, 6, 0);
1559 }
1560
1561 uErr = QCBORDecode_GetNext(&DCtx, &Item);
1562 if(uErr) {
1563 return MakeTestResultCode(10, 7, uErr);
1564 }
1565 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1566 Item.uDataType != QCBOR_TYPE_NULL) {
1567 return MakeTestResultCode(10, 8, 0);
1568 }
1569
1570 uErr = QCBORDecode_GetNext(&DCtx, &Item);
1571 if(uErr) {
1572 return MakeTestResultCode(10, 9, uErr);
1573 }
1574 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1575 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1576 ((const char *)Item.val.string.ptr)[0] != 'b') {
1577 return MakeTestResultCode(10, 10, 0);
1578 }
1579
1580 uErr = QCBORDecode_GetNext(&DCtx, &Item);
1581 if(uErr) {
1582 return MakeTestResultCode(10, 11, uErr);
1583 }
1584 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1585 Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE ||
1586 Item.val.int64 != 255) {
1587 return MakeTestResultCode(10, 12, 0);
1588 }
1589
1590 uErr = QCBORDecode_GetNext(&DCtx, &Item);
1591 if(uErr) {
1592 return MakeTestResultCode(10, 13, uErr);
1593 }
1594 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1595 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1596 ((const char *)Item.val.string.ptr)[0] != 'c') {
1597 return MakeTestResultCode(10, 14, 0);
1598 }
1599
1600 uErr = QCBORDecode_GetNext(&DCtx, &Item);
1601 if(uErr) {
1602 return MakeTestResultCode(10, 15, uErr);
1603 }
1604 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1605 Item.uDataType != QCBOR_TYPE_DOUBLE ||
1606 !isnan(Item.val.dfnum)) {
1607 return MakeTestResultCode(10, 16, 0);
1608 }
1609
1610 uErr = QCBORDecode_GetNext(&DCtx, &Item);
1611 if(uErr) {
1612 return MakeTestResultCode(10, 17, uErr);
1613 }
1614 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1615 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1616 ((const char *)Item.val.string.ptr)[0] != 'd') {
1617 return MakeTestResultCode(10, 18, 0);
1618 }
1619
1620
1621 uErr = QCBORDecode_GetNext(&DCtx, &Item);
1622 if(uErr) {
1623 return MakeTestResultCode(10, 19, uErr);
1624 }
1625 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1626 Item.uDataType != QCBOR_TYPE_DOUBLE ||
1627 Item.val.dfnum != 3.4028234663852886E+38) {
1628 return MakeTestResultCode(10, 20, 0);
1629 }
1630
1631 uErr = QCBORDecode_GetNext(&DCtx, &Item);
1632 if(uErr) {
1633 return MakeTestResultCode(10, 21, uErr);
1634 }
1635 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1636 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1637 ((const char *)Item.val.string.ptr)[0] != 'e') {
1638 return MakeTestResultCode(10, 22, 0);
1639 }
1640
1641 uErr = QCBORDecode_GetNext(&DCtx, &Item);
1642 if(uErr) {
1643 return MakeTestResultCode(10, 23, uErr);
1644 }
1645 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1646 Item.uDataType != QCBOR_TYPE_DOUBLE ||
1647 Item.val.dfnum != -INFINITY) {
1648 return MakeTestResultCode(10, 24, 0);
1649 }
1650
1651 uErr = QCBORDecode_GetNext(&DCtx, &Item);
1652 if(uErr) {
1653 return MakeTestResultCode(10, 25, uErr);
1654 }
1655 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1656 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1657 ((const char *)Item.val.string.ptr)[0] != 'f') {
1658 return MakeTestResultCode(10, 26, 0);
1659 }
1660
1661 uErr = QCBORDecode_GetNext(&DCtx, &Item);
1662 if(uErr) {
1663 return MakeTestResultCode(10, 26, uErr);
1664 }
1665 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1666 Item.uDataType != QCBOR_TYPE_MAP_AS_ARRAY ||
1667 Item.val.uCount != 2) {
1668 return MakeTestResultCode(10, 27, 0);
1669 }
1670
1671 uErr = QCBORDecode_GetNext(&DCtx, &Item);
1672 if(uErr) {
1673 return MakeTestResultCode(10, 28, uErr);
1674 }
1675 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1676 Item.uDataType != QCBOR_TYPE_INT64 ||
1677 Item.val.int64 != 1000) {
1678 return MakeTestResultCode(10, 29, 0);
1679 }
1680
1681 uErr = QCBORDecode_GetNext(&DCtx, &Item);
1682 if(uErr) {
1683 return MakeTestResultCode(10, 30, uErr);
1684 }
1685 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1686 Item.uDataType != QCBOR_TYPE_INT64 ||
1687 Item.val.int64 != 16) {
1688 return MakeTestResultCode(10, 31, 0);
1689 }
1690
1691 uErr = QCBORDecode_GetNext(&DCtx, &Item);
1692 if(uErr) {
1693 return MakeTestResultCode(10, 32, uErr);
1694 }
1695 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1696 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1697 ((const char *)Item.val.string.ptr)[0] != 'g') {
1698 return MakeTestResultCode(10, 33, 0);
1699 }
1700
1701 for(int i = 0 ; i < 4; i++) {
1702 uErr = QCBORDecode_GetNext(&DCtx, &Item);
1703 if(uErr) {
1704 return MakeTestResultCode(10, 34, uErr);
1705 }
1706 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1707 Item.uDataType != QCBOR_TYPE_ARRAY) {
1708 return MakeTestResultCode(10, 35, 0);
1709 }
1710 if(i != 3) {
1711 if(Item.val.uCount != 1) {
1712 return MakeTestResultCode(10, 35, 0);
1713 }
1714 }
1715 }
1716 uErr = QCBORDecode_GetNext(&DCtx, &Item);
1717 if(uErr) {
1718 return MakeTestResultCode(10, 36, uErr);
1719 }
1720 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1721 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1722 ((const char *)Item.val.string.ptr)[0] != 'h') {
1723 return MakeTestResultCode(10, 37, 0);
1724 }
1725
1726 uErr = QCBORDecode_GetNext(&DCtx, &Item);
1727 if(uErr) {
1728 return MakeTestResultCode(10, 38, uErr);
1729 }
1730 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1731 Item.uDataType != QCBOR_TYPE_DATE_EPOCH) {
1732 return MakeTestResultCode(10, 39, 0);
1733 }
1734
1735 uErr = QCBORDecode_GetNext(&DCtx, &Item);
1736 if(uErr) {
1737 return MakeTestResultCode(10, 40, uErr);
1738 }
1739 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1740 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1741 ((const char *)Item.val.string.ptr)[0] != 'i') {
1742 return MakeTestResultCode(10, 41, 0);
1743 }
1744
1745 uErr = QCBORDecode_GetNext(&DCtx, &Item);
1746 if(uErr) {
1747 return MakeTestResultCode(10, 42, uErr);
1748 }
1749 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1750 Item.uDataType != QCBOR_TYPE_ARRAY ||
1751 Item.val.uCount != 2) {
1752 return MakeTestResultCode(10, 31, 0);
1753 }
1754
1755 uErr = QCBORDecode_GetNext(&DCtx, &Item);
1756 if(uErr) {
1757 return MakeTestResultCode(10, 43, uErr);
1758 }
1759 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1760 Item.uDataType != QCBOR_TYPE_INT64) {
1761 return MakeTestResultCode(10, 31, 0);
1762 }
1763
1764 uErr = QCBORDecode_GetNext(&DCtx, &Item);
1765 if(uErr) {
1766 return MakeTestResultCode(10, 44, uErr);
1767 }
1768 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1769 Item.uDataType != QCBOR_TYPE_MAP_AS_ARRAY ||
1770 Item.val.uCount != 4) {
1771 return MakeTestResultCode(10, 45, 0);
1772 }
1773
1774 for(int i = 0 ; i < 4; i++) {
1775 uErr = QCBORDecode_GetNext(&DCtx, &Item);
1776 if(uErr) {
1777 return MakeTestResultCode(10, 46, uErr);
1778 }
1779 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1780 Item.uDataType != QCBOR_TYPE_INT64) {
1781 return MakeTestResultCode(10, 47, 0);
1782 }
1783 }
1784 uErr = QCBORDecode_GetNext(&DCtx, &Item);
1785 if(uErr) {
1786 return MakeTestResultCode(10, 48, uErr);
1787 }
1788 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1789 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1790 ((const char *)Item.val.string.ptr)[0] != 'j') {
1791 return MakeTestResultCode(10, 49, 0);
1792 }
1793
1794 uErr = QCBORDecode_GetNext(&DCtx, &Item);
1795 if(uErr) {
1796 return MakeTestResultCode(10, 50, uErr);
1797 }
1798 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1799 Item.uDataType != QCBOR_TYPE_MAP_AS_ARRAY ||
1800 Item.val.uCount != UINT16_MAX) {
1801 return MakeTestResultCode(10, 51, 0);
1802 }
1803
1804 uErr = QCBORDecode_GetNext(&DCtx, &Item);
1805 if(uErr) {
1806 return MakeTestResultCode(10, 52, uErr);
1807 }
1808 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1809 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1810 ((const char *)Item.val.string.ptr)[0] != 'k') {
1811 return MakeTestResultCode(10, 53, 0);
1812 }
1813
1814 uErr = QCBORDecode_GetNext(&DCtx, &Item);
1815 if(uErr) {
1816 return MakeTestResultCode(10, 54, uErr);
1817 }
1818 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1819 Item.uDataType != QCBOR_TYPE_ARRAY ||
1820 Item.val.uCount != UINT16_MAX) {
1821 return MakeTestResultCode(10, 55, 0);
1822 }
1823
1824 for(int i = 0 ; i < 3; i++) {
1825 uErr = QCBORDecode_GetNext(&DCtx, &Item);
1826 if(uErr) {
1827 return MakeTestResultCode(10, 56, uErr);
1828 }
1829 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1830 Item.uDataType != QCBOR_TYPE_INT64) {
1831 return MakeTestResultCode(10, 57, 0);
1832 }
1833 }
1834 uErr = QCBORDecode_GetNext(&DCtx, &Item);
1835 if(uErr) {
1836 return MakeTestResultCode(10, 58, uErr);
1837 }
1838 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1839 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1840 ((const char *)Item.val.string.ptr)[0] != 'l') {
1841 return MakeTestResultCode(10, 59, 0);
1842 }
1843
1844 uErr = QCBORDecode_GetNext(&DCtx, &Item);
1845 if(uErr) {
1846 return MakeTestResultCode(10, 60, uErr);
1847 }
1848 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1849 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1850 Item.val.string.len != 4) {
1851 return MakeTestResultCode(10, 61, 0);
1852 }
1853
1854 uErr = QCBORDecode_GetNext(&DCtx, &Item);
1855 if(uErr) {
1856 return MakeTestResultCode(10, 62, uErr);
1857 }
1858 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1859 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1860 ((const char *)Item.val.string.ptr)[0] != 'm') {
1861 return MakeTestResultCode(10, 63, 0);
1862 }
1863
1864 uErr = QCBORDecode_GetNext(&DCtx, &Item);
1865 if(uErr) {
1866 return MakeTestResultCode(10, 64, uErr);
1867 }
1868 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1869 Item.uDataType != QCBOR_TYPE_MAP_AS_ARRAY ||
1870 !QCBORDecode_IsTagged(&DCtx, &Item, 258) ||
1871 Item.val.uCount != UINT16_MAX) {
1872 return MakeTestResultCode(10, 65, 0);
1873 }
1874
1875 uErr = QCBORDecode_GetNext(&DCtx, &Item);
1876 if(uErr) {
1877 return MakeTestResultCode(10, 66, uErr);
1878 }
1879 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1880 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1881 Item.val.string.len != 2) {
1882 return MakeTestResultCode(10, 67, 0);
1883 }
1884
1885 uErr = QCBORDecode_GetNext(&DCtx, &Item);
1886 if(uErr) {
1887 return MakeTestResultCode(10, 68, uErr);
1888 }
1889 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1890 Item.uDataType != QCBOR_TYPE_INT64 ||
1891 Item.val.int64 != 0) {
1892 return MakeTestResultCode(10, 69, 0);
1893 }
1894
1895 uErr = QCBORDecode_GetNext(&DCtx, &Item);
1896 if(uErr) {
1897 return MakeTestResultCode(10, 70, uErr);
1898 }
1899 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1900 Item.uDataType != QCBOR_TYPE_FALSE) {
1901 return MakeTestResultCode(10, 71, 0);
1902 }
1903
1904 uErr = QCBORDecode_GetNext(&DCtx, &Item);
1905 if(uErr) {
1906 return MakeTestResultCode(10, 72, uErr);
1907 }
1908 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1909 Item.uDataType != QCBOR_TYPE_ARRAY ||
1910 !QCBORDecode_IsTagged(&DCtx, &Item, 23) ||
1911 Item.val.uCount != 0) {
1912 return MakeTestResultCode(10, 73, 0);
1913 }
1914
1915 uErr = QCBORDecode_GetNext(&DCtx, &Item);
1916 if(uErr) {
1917 return MakeTestResultCode(10, 74, uErr);
1918 }
1919 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1920 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1921 ((const char *)Item.val.string.ptr)[0] != 'n') {
1922 return MakeTestResultCode(10, 75, 0);
1923 }
1924
1925
1926 /* Big decode of a map with a wide variety or labels */
1927 QCBORDecode_Init(&DCtx,
1928 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pPerverseLabels),
1929 QCBOR_DECODE_MODE_MAP_AS_ARRAY);
1930 QCBORDecode_SetMemPool(&DCtx, Pool, 0);
1931
1932 QCBORDecode_EnterArray(&DCtx, &Item);
1933 bool b;
1934 QCBORDecode_GetBool(&DCtx, &b);
1935
1936 uErr = CheckOneLetterString(&DCtx, 'a');
1937 if(uErr) {
1938 return MakeTestResultCode(11, 1, uErr);
1939 }
1940
1941 QCBORDecode_GetNull(&DCtx);
1942 uErr = CheckOneLetterString(&DCtx, 'b');
1943 if(uErr) {
1944 return MakeTestResultCode(11, 2, uErr);
1945 }
1946
1947 QCBORDecode_VGetNext(&DCtx, &Item);
1948 uErr = CheckOneLetterString(&DCtx, 'c');
1949 if(uErr) {
1950 return MakeTestResultCode(11, 3, uErr);
1951 }
1952
1953 double dNum;
1954 QCBORDecode_GetDouble(&DCtx, &dNum);
1955 if(!isnan(dNum)) {
1956 return MakeTestResultCode(11, 4, 0);
1957 }
1958 uErr = CheckOneLetterString(&DCtx, 'd');
1959 if(uErr) {
1960 return MakeTestResultCode(11, 5, uErr);
1961 }
1962
1963 QCBORDecode_GetDouble(&DCtx, &dNum);
1964 if( dNum != 3.4028234663852886E+38 ) {
1965 return MakeTestResultCode(11, 6, 0);
1966 }
1967 uErr = CheckOneLetterString(&DCtx, 'e');
1968 if(uErr) {
1969 return MakeTestResultCode(11, 7, uErr);
1970 }
1971
1972 QCBORDecode_GetDouble(&DCtx, &dNum);
1973 if(dNum != -INFINITY) {
1974 return MakeTestResultCode(11, 8, 0);
1975 }
1976 uErr = CheckOneLetterString(&DCtx, 'f');
1977 if(uErr) {
1978 return MakeTestResultCode(11, 9, uErr);
1979 }
1980
1981 int64_t nInt;
1982 QCBORDecode_EnterArray(&DCtx, &Item);
1983 QCBORDecode_GetInt64(&DCtx, &nInt);
1984 QCBORDecode_GetInt64(&DCtx, &nInt);
1985 QCBORDecode_ExitArray(&DCtx);
1986 uErr = CheckOneLetterString(&DCtx, 'g');
1987 if(uErr) {
1988 return MakeTestResultCode(11, 10, uErr);
1989 }
1990
1991 QCBORDecode_EnterArray(&DCtx, &Item);
1992 QCBORDecode_EnterArray(&DCtx, &Item);
1993 QCBORDecode_EnterArray(&DCtx, &Item);
1994 QCBORDecode_EnterArray(&DCtx, &Item);
1995 QCBORDecode_ExitArray(&DCtx);
1996 QCBORDecode_ExitArray(&DCtx);
1997 QCBORDecode_ExitArray(&DCtx);
1998 QCBORDecode_ExitArray(&DCtx);
1999 uErr = CheckOneLetterString(&DCtx, 'h');
2000 if(uErr) {
2001 return MakeTestResultCode(11, 11, uErr);
2002 }
2003 QCBORDecode_GetEpochDate(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &nInt);
2004 uErr = CheckOneLetterString(&DCtx, 'i');
2005 if(uErr) {
2006 return MakeTestResultCode(11, 12, uErr);
2007 }
2008
2009 QCBORDecode_EnterArray(&DCtx, &Item);
2010 QCBORDecode_GetInt64(&DCtx, &nInt);
2011 QCBORDecode_EnterArray(&DCtx, &Item);
2012 QCBORDecode_GetInt64(&DCtx, &nInt);
2013 QCBORDecode_GetInt64(&DCtx, &nInt);
2014 QCBORDecode_GetInt64(&DCtx, &nInt);
2015 QCBORDecode_GetInt64(&DCtx, &nInt);
2016 QCBORDecode_ExitArray(&DCtx);
2017 QCBORDecode_ExitArray(&DCtx);
2018 uErr = CheckOneLetterString(&DCtx, 'j');
2019 if(uErr) {
2020 return MakeTestResultCode(11, 13, uErr);
2021 }
2022
2023 QCBORDecode_GetArray(&DCtx, &Item, &Encoded);
2024 uErr = CheckOneLetterString(&DCtx, 'k');
2025 if(uErr) {
2026 return MakeTestResultCode(11, 14, uErr);
2027 }
2028
2029 QCBORDecode_EnterArray(&DCtx, &Item);
2030 QCBORDecode_GetInt64(&DCtx, &nInt);
2031 QCBORDecode_GetInt64(&DCtx, &nInt);
2032 QCBORDecode_GetInt64(&DCtx, &nInt);
2033 QCBORDecode_ExitArray(&DCtx);
2034 uErr = CheckOneLetterString(&DCtx, 'l');
2035 if(uErr) {
2036 return MakeTestResultCode(11, 15, uErr);
2037 }
2038
2039 QCBORDecode_GetTextString(&DCtx, &Encoded);
2040 uErr = CheckOneLetterString(&DCtx, 'm');
2041 if(uErr) {
2042 return MakeTestResultCode(11, 16, uErr);
2043 }
2044
2045 QCBORDecode_EnterArray(&DCtx, &Item);
2046 if(!QCBORDecode_IsTagged(&DCtx, &Item, 258)) {
2047 return MakeTestResultCode(11, 17, 0);
2048 }
2049 if(Item.uDataType != QCBOR_TYPE_MAP_AS_ARRAY) {
2050 return MakeTestResultCode(11, 18, 0);
2051 }
2052 if(Item.val.uCount != UINT16_MAX) {
2053 return MakeTestResultCode(11, 19, 0);
2054 }
2055 QCBORDecode_GetTextString(&DCtx, &Encoded);
2056 if(Encoded.len != 2) {
2057 return MakeTestResultCode(11, 20, 0);
2058 } QCBORDecode_GetInt64(&DCtx, &nInt);
2059 QCBORDecode_GetBool(&DCtx, &b);
2060 if(b != false) {
2061 return MakeTestResultCode(11, 21, 0);
2062 }
2063 QCBORDecode_EnterArray(&DCtx, &Item);
2064 if(!QCBORDecode_IsTagged(&DCtx, &Item, 23)) {
2065 return MakeTestResultCode(11, 22, 0);
2066 }
2067 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
2068 return MakeTestResultCode(11, 23, 0);
2069 }
2070 if(Item.val.uCount != 0) {
2071 return MakeTestResultCode(11, 24, 0);
2072 }
2073 QCBORDecode_ExitArray(&DCtx);
2074 QCBORDecode_ExitArray(&DCtx);
2075 uErr = CheckOneLetterString(&DCtx, 'n');
2076 if(uErr) {
2077 return MakeTestResultCode(11, 25, uErr);
2078 }
2079
2080 QCBORDecode_ExitArray(&DCtx);
2081 uErr = QCBORDecode_Finish(&DCtx);
2082 if(uErr) {
2083 return MakeTestResultCode(11, 26, uErr);
2084 }
2085#endif /* QCBOR_DISABLE_... */
Laurence Lundbladed4cc1032020-10-12 04:19:47 -07002086
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09002087 return 0;
2088}
2089
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002090
Laurence Lundblade5db34da2024-05-30 03:14:35 -07002091
2092
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002093/*
Laurence Lundbladefab1b522018-10-19 13:40:52 +05302094 Fully or partially decode pValidMapEncoded. When
2095 partially decoding check for the right error code.
2096 How much partial decoding depends on nLevel.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002097
Laurence Lundbladefab1b522018-10-19 13:40:52 +05302098 The partial decodes test error conditions of
2099 incomplete encoded input.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002100
Laurence Lundbladefab1b522018-10-19 13:40:52 +05302101 This could be combined with the above test
2102 and made prettier and maybe a little more
2103 thorough.
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002104 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002105static int32_t ExtraBytesTest(int nLevel)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002106{
2107 QCBORDecodeContext DCtx;
2108 QCBORItem Item;
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07002109 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002110
Laurence Lundbladeee851742020-01-08 08:37:05 -08002111 QCBORDecode_Init(&DCtx,
2112 (UsefulBufC){pValidMapEncoded, sizeof(pValidMapEncoded)},
2113 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002114
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08002115 if(nLevel < 1) {
2116 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_EXTRA_BYTES) {
2117 return -1;
2118 } else {
2119 return 0;
2120 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002121 }
Laurence Lundbladefab1b522018-10-19 13:40:52 +05302122
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002123
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09002124 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07002125 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09002126 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002127 if(Item.uDataType != QCBOR_TYPE_MAP ||
2128 Item.val.uCount != 3)
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002129 return -2;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002130
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08002131 if(nLevel < 2) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002132 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
2133 return -3;
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08002134 } else {
2135 return 0;
2136 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002137 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002138
2139
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09002140 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07002141 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09002142 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002143 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002144 Item.uDataType != QCBOR_TYPE_INT64 ||
2145 Item.val.uCount != 42 ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07002146 UsefulBufCompareToSZ(Item.label.string, "first integer")) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002147 return -4;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09002148 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002149
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08002150 if(nLevel < 3) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002151 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
2152 return -5;
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08002153 } else {
2154 return 0;
2155 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002156 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002157
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09002158 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07002159 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09002160 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002161 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07002162 UsefulBufCompareToSZ(Item.label.string, "an array of two strings") ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002163 Item.uDataType != QCBOR_TYPE_ARRAY ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09002164 Item.val.uCount != 2) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002165 return -6;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09002166 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002167
2168
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08002169 if(nLevel < 4) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002170 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
2171 return -7;
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08002172 } else {
2173 return 0;
2174 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002175 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002176
2177
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09002178 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07002179 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09002180 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002181 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07002182 UsefulBufCompareToSZ(Item.val.string, "string1")) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002183 return -8;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09002184 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002185
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08002186 if(nLevel < 5) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002187 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
2188 return -9;
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08002189 } else {
2190 return 0;
2191 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002192 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002193
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09002194 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07002195 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09002196 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002197 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07002198 UsefulBufCompareToSZ(Item.val.string, "string2")) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002199 return -10;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09002200 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002201
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08002202 if(nLevel < 6) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002203 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
2204 return -11;
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08002205 } else {
2206 return 0;
2207 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002208 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002209
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09002210 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07002211 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09002212 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002213 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07002214 UsefulBufCompareToSZ(Item.label.string, "map in a map") ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002215 Item.uDataType != QCBOR_TYPE_MAP ||
2216 Item.val.uCount != 4)
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002217 return -12;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002218
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08002219 if(nLevel < 7) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002220 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
2221 return -13;
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08002222 } else {
2223 return 0;
2224 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002225 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002226
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09002227 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07002228 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09002229 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002230 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07002231 UsefulBufCompareToSZ(Item.label.string, "bytes 1") ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002232 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07002233 UsefulBufCompareToSZ(Item.val.string, "xxxx")) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002234 return -14;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09002235 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002236
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08002237 if(nLevel < 8) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002238 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
2239 return -15;
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08002240 } else {
2241 return 0;
2242 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002243 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002244
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09002245 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07002246 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09002247 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002248 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07002249 UsefulBufCompareToSZ(Item.label.string, "bytes 2") ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002250 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07002251 UsefulBufCompareToSZ(Item.val.string, "yyyy")) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002252 return -16;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09002253 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002254
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08002255 if(nLevel < 9) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002256 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
2257 return -17;
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08002258 } else {
2259 return 0;
2260 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002261 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002262
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09002263 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07002264 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09002265 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002266 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07002267 UsefulBufCompareToSZ(Item.label.string, "another int") ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002268 Item.uDataType != QCBOR_TYPE_INT64 ||
2269 Item.val.int64 != 98)
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002270 return -18;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002271
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08002272 if(nLevel < 10) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002273 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
2274 return -19;
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08002275 } else {
2276 return 0;
2277 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002278 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002279
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09002280 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07002281 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09002282 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002283 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09002284 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("text 2"))||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002285 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07002286 UsefulBufCompareToSZ(Item.val.string, "lies, damn lies and statistics")) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002287 return -20;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09002288 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002289
Laurence Lundbladefab1b522018-10-19 13:40:52 +05302290 if(QCBORDecode_Finish(&DCtx)) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002291 return -21;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002292 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002293
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002294 return 0;
2295}
2296
2297
2298
Laurence Lundblade844bb5c2020-03-01 17:27:25 -08002299
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03002300int32_t ParseMapTest(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002301{
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002302 // Parse a moderatly complex map structure very thoroughly
2303 int32_t nResult = ParseMapTest1(QCBOR_DECODE_MODE_NORMAL);
2304 if(nResult) {
2305 return nResult;
2306 }
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08002307
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002308 // Again, but in strings-only mode. It should succeed since the input
2309 // map has only string labels.
2310 nResult = ParseMapTest1(QCBOR_DECODE_MODE_MAP_STRINGS_ONLY);
2311 if(nResult) {
2312 return nResult;
2313 }
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08002314
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002315 // Again, but try to finish the decoding before the end of the
2316 // input at 10 different place and see that the right error code
2317 // is returned.
2318 for(int i = 0; i < 10; i++) {
2319 nResult = ExtraBytesTest(i);
2320 if(nResult) {
2321 break;
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08002322 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002323 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002324
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002325 return nResult;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002326}
2327
2328
Laurence Lundbladecc7da412020-12-27 00:09:07 -08002329/* The simple-values including some not well formed */
2330static const uint8_t spSimpleValues[] = {
2331 0x8a, 0xf4, 0xf5, 0xf6, 0xf7, 0xff, 0xe0, 0xf3,
2332 0xf8, 0x00, 0xf8, 0x13, 0xf8, 0x1f, 0xf8, 0x20,
2333 0xf8, 0xff};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002334
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03002335int32_t ParseSimpleTest(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002336{
2337 QCBORDecodeContext DCtx;
2338 QCBORItem Item;
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07002339 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002340
2341
Laurence Lundbladeee851742020-01-08 08:37:05 -08002342 QCBORDecode_Init(&DCtx,
2343 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSimpleValues),
2344 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002345
2346
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002347 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07002348 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002349 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
2350 Item.val.uCount != 10)
2351 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002352
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002353 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07002354 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002355 if(Item.uDataType != QCBOR_TYPE_FALSE)
2356 return -1;
2357
2358 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07002359 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002360 if(Item.uDataType != QCBOR_TYPE_TRUE)
2361 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002362
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002363 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07002364 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002365 if(Item.uDataType != QCBOR_TYPE_NULL)
2366 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002367
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002368 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07002369 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002370 if(Item.uDataType != QCBOR_TYPE_UNDEF)
2371 return -1;
2372
2373 // A break
Laurence Lundblade9e3651c2018-10-10 11:49:55 +08002374 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_BREAK)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002375 return -1;
2376
2377 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07002378 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002379 if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 0)
2380 return -1;
2381
2382 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07002383 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002384 if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 19)
2385 return -1;
2386
Laurence Lundblade077475f2019-04-26 09:06:33 -07002387 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_TYPE_7)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002388 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002389
Laurence Lundblade077475f2019-04-26 09:06:33 -07002390 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_TYPE_7)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002391 return -1;
2392
Laurence Lundblade077475f2019-04-26 09:06:33 -07002393 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_TYPE_7)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002394 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002395
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002396 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07002397 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002398 if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 32)
2399 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002400
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002401 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07002402 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002403 if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 255)
2404 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002405
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002406 return 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002407
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002408}
2409
2410
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03002411int32_t NotWellFormedTests(void)
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002412{
2413 // Loop over all the not-well-formed instance of CBOR
2414 // that are test vectors in not_well_formed_cbor.h
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08002415 const uint16_t nArraySize = C_ARRAY_COUNT(paNotWellFormedCBOR,
2416 struct someBinaryBytes);
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002417 for(uint16_t nIterate = 0; nIterate < nArraySize; nIterate++) {
2418 const struct someBinaryBytes *pBytes = &paNotWellFormedCBOR[nIterate];
2419 const UsefulBufC Input = (UsefulBufC){pBytes->p, pBytes->n};
2420
Laurence Lundblade37286c02022-09-03 10:05:02 -07002421 if(nIterate == 86) {
2422 nIterate = 86;
2423 }
2424
Laurence Lundbladeee851742020-01-08 08:37:05 -08002425 // Set up decoder context. String allocator needed for indefinite
2426 // string test cases
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002427 QCBORDecodeContext DCtx;
2428 QCBORDecode_Init(&DCtx, Input, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08002429#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002430 UsefulBuf_MAKE_STACK_UB(Pool, 100);
2431 QCBORDecode_SetMemPool(&DCtx, Pool, 0);
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08002432#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002433
2434 // Loop getting items until no more to get
Laurence Lundbladef71e1622020-08-06 18:52:13 -07002435 QCBORError uCBORError;
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002436 do {
2437 QCBORItem Item;
2438
Laurence Lundbladef71e1622020-08-06 18:52:13 -07002439 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
2440 } while(uCBORError == QCBOR_SUCCESS);
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002441
2442 // Every test vector must fail with
2443 // a not-well-formed error. If not
2444 // this test fails.
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002445 if(!QCBORDecode_IsNotWellFormedError(uCBORError) &&
Laurence Lundbladef71e1622020-08-06 18:52:13 -07002446 uCBORError != QCBOR_ERR_NO_MORE_ITEMS) {
Laurence Lundblade4bf6e712020-12-10 11:53:21 -08002447 /* Return index of failure and QCBOR error in the result */
2448 return (int32_t)(nIterate * 100 + uCBORError);
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002449 }
2450 }
2451 return 0;
2452}
2453
2454
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002455struct DecodeFailTestInput {
2456 const char *szDescription; /* Description of the test */
2457 QCBORDecodeMode DecoderMode; /* The QCBOR Decoder Mode for test */
2458 UsefulBufC Input; /* Chunk of CBOR that cases error */
2459 QCBORError nError; /* The expected error */
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002460};
2461
Laurence Lundblade59289e52019-12-30 13:44:37 -08002462
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002463static int32_t
2464ProcessDecodeFailures(const struct DecodeFailTestInput *pFailInputs, const int nNumFails)
Laurence Lundblade59289e52019-12-30 13:44:37 -08002465{
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002466 int nIndex;
2467 QCBORDecodeContext DCtx;
2468 QCBORError uCBORError;
2469 QCBORItem Item;
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08002470
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002471 for(nIndex = 0; nIndex < nNumFails; nIndex++) {
2472 const struct DecodeFailTestInput *pF = &pFailInputs[nIndex];
2473
2474 QCBORDecode_Init(&DCtx, pF->Input, pF->DecoderMode);
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08002475
2476#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002477 /* Set up the decoding context including a memory pool so that
2478 * indefinite length items can be checked.
2479 */
Laurence Lundblade59289e52019-12-30 13:44:37 -08002480 UsefulBuf_MAKE_STACK_UB(Pool, 100);
Laurence Lundblade830fbf92020-05-31 17:22:33 -07002481
Laurence Lundblade4bf6e712020-12-10 11:53:21 -08002482 uCBORError = QCBORDecode_SetMemPool(&DCtx, Pool, 0);
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002483 if(uCBORError != QCBOR_SUCCESS) {
2484 return -1;
Laurence Lundblade59289e52019-12-30 13:44:37 -08002485 }
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08002486#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
2487
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002488 if(nIndex == 8) {
2489 uCBORError = 9; /* For setting break points */
Laurence Lundblade37286c02022-09-03 10:05:02 -07002490 }
2491
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002492 /* Iterate until there is an error of some sort of error */
Laurence Lundblade59289e52019-12-30 13:44:37 -08002493 do {
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002494 /* Set to something none-zero, something other than QCBOR_TYPE_NONE */
Laurence Lundblade59289e52019-12-30 13:44:37 -08002495 memset(&Item, 0x33, sizeof(Item));
2496
Laurence Lundblade4bf6e712020-12-10 11:53:21 -08002497 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
2498 } while(uCBORError == QCBOR_SUCCESS);
2499
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002500 /* Must get the expected error or the this test fails
2501 * The data and label type must also be QCBOR_TYPE_NONE.
2502 */
Laurence Lundblade4bf6e712020-12-10 11:53:21 -08002503 if(uCBORError != pF->nError ||
Laurence Lundblade59289e52019-12-30 13:44:37 -08002504 Item.uDataType != QCBOR_TYPE_NONE ||
2505 Item.uLabelType != QCBOR_TYPE_NONE) {
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002506 return (int32_t)(nIndex * 1000 + (int)uCBORError);
Laurence Lundblade59289e52019-12-30 13:44:37 -08002507 }
2508 }
2509
2510 return 0;
2511}
2512
2513
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002514static const struct DecodeFailTestInput Failures[] = {
2515 /* Most of this is copied from not_well_formed.h. Here the error
2516 * code returned is also checked.
2517 */
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002518
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08002519#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002520 /* Indefinite length strings must be closed off */
2521 { "An indefinite length byte string not closed off",
2522 QCBOR_DECODE_MODE_NORMAL,
2523 {"0x5f\x41\x00", 3},
2524 QCBOR_ERR_HIT_END
2525 },
2526 { "An indefinite length text string not closed off",
2527 QCBOR_DECODE_MODE_NORMAL,
2528 {"\x7f\x61\x00", 3},
2529 QCBOR_ERR_HIT_END
2530 },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002531
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002532 /* All the chunks in an indefinite length string must be of the
2533 * type of indefinite length string
2534 */
2535 { "Indefinite length byte string with text string chunk",
2536 QCBOR_DECODE_MODE_NORMAL,
2537 {"\x5f\x61\x00\xff", 4},
2538 QCBOR_ERR_INDEFINITE_STRING_CHUNK
2539 },
2540 { "Indefinite length text string with a byte string chunk",
2541 QCBOR_DECODE_MODE_NORMAL,
2542 {"\x7f\x41\x00\xff", 4},
2543 QCBOR_ERR_INDEFINITE_STRING_CHUNK
2544 },
2545 { "Indefinite length byte string with a positive integer chunk",
2546 QCBOR_DECODE_MODE_NORMAL,
2547 {"\x5f\x00\xff", 3},
2548 QCBOR_ERR_INDEFINITE_STRING_CHUNK
2549 },
2550 { "Indefinite length byte string with an negative integer chunk",
2551 QCBOR_DECODE_MODE_NORMAL,
2552 {"\x5f\x21\xff", 3},
2553 QCBOR_ERR_INDEFINITE_STRING_CHUNK
2554 },
2555 { "Indefinite length byte string with an array chunk",
2556 QCBOR_DECODE_MODE_NORMAL,
2557 {"\x5f\x80\xff", 3},
2558 QCBOR_ERR_INDEFINITE_STRING_CHUNK
2559 },
2560 { "Indefinite length byte string with an map chunk",
2561 QCBOR_DECODE_MODE_NORMAL,
2562 {"\x5f\xa0\xff", 3},
2563 QCBOR_ERR_INDEFINITE_STRING_CHUNK
2564 },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002565
Laurence Lundblade37286c02022-09-03 10:05:02 -07002566#ifndef QCBOR_DISABLE_TAGS
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002567 { "Indefinite length byte string with tagged integer chunk",
2568 QCBOR_DECODE_MODE_NORMAL,
2569 {"\x5f\xc0\x00\xff", 4},
2570 QCBOR_ERR_INDEFINITE_STRING_CHUNK
2571 },
Laurence Lundblade37286c02022-09-03 10:05:02 -07002572#else
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002573 { "Indefinite length byte string with tagged integer chunk",
2574 QCBOR_DECODE_MODE_NORMAL,
2575 {"\x5f\xc0\x00\xff", 4},
2576 QCBOR_ERR_TAGS_DISABLED
2577 },
Laurence Lundblade37286c02022-09-03 10:05:02 -07002578#endif /* QCBOR_DISABLE_TAGS */
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002579
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002580 { "Indefinite length byte string with an simple type chunk",
2581 QCBOR_DECODE_MODE_NORMAL,
2582 {"\x5f\xe0\xff", 3},
2583 QCBOR_ERR_INDEFINITE_STRING_CHUNK
2584 },
2585 { "???",
2586 QCBOR_DECODE_MODE_NORMAL,
2587 {"\x5f\x5f\x41\x00\xff\xff", 6},
2588 QCBOR_ERR_INDEFINITE_STRING_CHUNK
2589 },
2590 { "indefinite length text string with indefinite string inside",
2591 QCBOR_DECODE_MODE_NORMAL,
2592 {"\x7f\x7f\x61\x00\xff\xff", 6},
2593 QCBOR_ERR_INDEFINITE_STRING_CHUNK
2594 },
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08002595#else /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
2596
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08002597#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
2598
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002599 /* Definte length maps and arrays must be closed by having the right number of items */
2600 { "A definte length array that is supposed to have 1 item, but has none",
2601 QCBOR_DECODE_MODE_NORMAL,
2602 {"\x81", 1},
2603 QCBOR_ERR_NO_MORE_ITEMS
2604 },
2605 { "A definte length array that is supposed to have 2 items, but has only 1",
2606 QCBOR_DECODE_MODE_NORMAL,
2607 {"\x82\x00", 2},
2608 QCBOR_ERR_NO_MORE_ITEMS
2609 },
2610 { "A definte length array that is supposed to have 511 items, but has only 1",
2611 QCBOR_DECODE_MODE_NORMAL,
2612 {"\x9a\x01\xff\x00", 4},
2613 QCBOR_ERR_HIT_END
2614 },
2615 { "A definte length map that is supposed to have 1 item, but has none",
2616 QCBOR_DECODE_MODE_NORMAL,
2617 {"\xa1", 1},
2618 QCBOR_ERR_NO_MORE_ITEMS
2619 },
2620 { "A definte length map that is supposed to have s item, but has only 1",
2621 QCBOR_DECODE_MODE_NORMAL,
2622 {"\xa2\x01\x02", 3},
2623 QCBOR_ERR_NO_MORE_ITEMS
2624 },
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08002625#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002626 /* Indefinte length maps and arrays must be ended by a break */
2627 { "Indefinite length array with zero items and no break",
2628 QCBOR_DECODE_MODE_NORMAL,
2629 {"\x9f", 1},
2630 QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002631
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002632 { "Indefinite length array with two items and no break",
2633 QCBOR_DECODE_MODE_NORMAL,
2634 {"\x9\x01\x02", 3},
2635 QCBOR_ERR_NO_MORE_ITEMS
2636 },
2637 { "Indefinite length map with zero items and no break",
2638 QCBOR_DECODE_MODE_NORMAL,
2639 {"\xbf", 1},
2640 QCBOR_ERR_NO_MORE_ITEMS
2641 },
2642 { "Indefinite length map with two items and no break",
2643 QCBOR_DECODE_MODE_NORMAL,
2644 {"\xbf\x01\x02\x01\x02", 5},
2645 QCBOR_ERR_NO_MORE_ITEMS
2646 },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002647
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002648 /* Nested maps and arrays must be closed off (some extra nested test vectors) */
2649 { "Unclosed indefinite array containing a closed definite length array",
2650 QCBOR_DECODE_MODE_NORMAL,
2651 {"\x9f\x80\x00", 3},
2652 QCBOR_ERR_NO_MORE_ITEMS
2653 },
2654
2655 { "Definite length array containing an unclosed indefinite length array",
2656 QCBOR_DECODE_MODE_NORMAL,
2657 {"\x81\x9f", 2},
2658 QCBOR_ERR_NO_MORE_ITEMS
2659 },
2660 { "Unclosed indefinite map containing a closed definite length array",
2661 QCBOR_DECODE_MODE_NORMAL,
2662 {"\xbf\x01\x80\x00\xa0", 5},
2663 QCBOR_ERR_NO_MORE_ITEMS
2664 },
2665 { "Definite length map containing an unclosed indefinite length array",
2666 QCBOR_DECODE_MODE_NORMAL,
2667 {"\xa1\x02\x9f", 3},
2668 QCBOR_ERR_NO_MORE_ITEMS
2669 },
2670 { "Deeply nested definite length arrays with deepest one unclosed",
2671 QCBOR_DECODE_MODE_NORMAL,
2672 {"\x81\x81\x81\x81\x81\x81\x81\x81\x81", 9},
2673 QCBOR_ERR_NO_MORE_ITEMS
2674 },
2675 { "Deeply nested indefinite length arrays with deepest one unclosed",
2676 QCBOR_DECODE_MODE_NORMAL,
2677 {"\x9f\x9f\x9f\x9f\x9f\xff\xff\xff\xff", 9},
2678 QCBOR_ERR_NO_MORE_ITEMS
2679 },
2680 { "Mixed nesting with indefinite unclosed",
2681 QCBOR_DECODE_MODE_NORMAL,
2682 {"\x9f\x81\x9f\x81\x9f\x9f\xff\xff\xff", 9},
2683 QCBOR_ERR_NO_MORE_ITEMS },
2684 { "Mixed nesting with definite unclosed",
2685 QCBOR_DECODE_MODE_NORMAL,
2686 {"\x9f\x82\x9f\x81\x9f\x9f\xff\xff\xff\xff", 10},
2687 QCBOR_ERR_BAD_BREAK
2688 },
2689 { "Unclosed indefinite length map in definite length maps",
2690 QCBOR_DECODE_MODE_NORMAL,
2691 {"\xa1\x01\xa2\x02\xbf\xff\x02\xbf", 8},
2692 QCBOR_ERR_NO_MORE_ITEMS
2693 },
2694 { "Unclosed definite length map in indefinite length maps",
2695 QCBOR_DECODE_MODE_NORMAL,
2696 {"\xbf\x01\xbf\x02\xa1", 5},
2697 QCBOR_ERR_NO_MORE_ITEMS
2698 },
2699 { "Unclosed indefinite length array in definite length maps",
2700 QCBOR_DECODE_MODE_NORMAL,
2701 {"\xa1\x01\xa2\x02\x9f\xff\x02\x9f", 8},
2702 QCBOR_ERR_NO_MORE_ITEMS
2703 },
2704 { "Unclosed definite length array in indefinite length maps",
2705 QCBOR_DECODE_MODE_NORMAL,
2706 {"\xbf\x01\xbf\x02\x81", 5},
2707 QCBOR_ERR_NO_MORE_ITEMS
2708 },
2709 { "Unclosed indefinite length map in definite length arrays",
2710 QCBOR_DECODE_MODE_NORMAL,
2711 {"\x81\x82\xbf\xff\xbf", 5},
2712 QCBOR_ERR_NO_MORE_ITEMS
2713 },
2714 { "Unclosed definite length map in indefinite length arrays",
2715 QCBOR_DECODE_MODE_NORMAL,
2716 {"\x9f\x9f\xa1", 3},
2717 QCBOR_ERR_NO_MORE_ITEMS
2718 },
2719
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08002720#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002721
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002722 /* The "argument" for the data item is incomplete */
2723 { "Positive integer missing 1 byte argument",
2724 QCBOR_DECODE_MODE_NORMAL,
2725 {"\x18", 1},
2726 QCBOR_ERR_HIT_END
2727 },
2728 { "Positive integer missing 2 byte argument",
2729 QCBOR_DECODE_MODE_NORMAL,
2730 {"\x19", 1},
2731 QCBOR_ERR_HIT_END
2732 },
2733 { "Positive integer missing 4 byte argument",
2734 QCBOR_DECODE_MODE_NORMAL,
2735 {"\x1a", 1},
2736 QCBOR_ERR_HIT_END
2737 },
2738 { "Positive integer missing 8 byte argument",
2739 QCBOR_DECODE_MODE_NORMAL,
2740 {"\x1b", 1},
2741 QCBOR_ERR_HIT_END
2742 },
2743 { "Positive integer missing 1 byte of 2 byte argument",
2744 QCBOR_DECODE_MODE_NORMAL,
2745 {"\x19\x01", 2},
2746 QCBOR_ERR_HIT_END
2747 },
2748 { "Positive integer missing 2 bytes of 4 byte argument",
2749 QCBOR_DECODE_MODE_NORMAL,
2750 {"\x1a\x01\x02", 3},
2751 QCBOR_ERR_HIT_END
2752 },
2753 { "Positive integer missing 1 bytes of 7 byte argument",
2754 QCBOR_DECODE_MODE_NORMAL,
2755 {"\x1b\x01\x02\x03\x04\x05\x06\x07", 8},
2756 QCBOR_ERR_HIT_END
2757 },
2758 { "Negative integer missing 1 byte argument",
2759 QCBOR_DECODE_MODE_NORMAL,
2760 {"\x38", 1},
2761 QCBOR_ERR_HIT_END
2762 },
2763 { "Binary string missing 1 byte argument",
2764 QCBOR_DECODE_MODE_NORMAL,
2765 {"\x58", 1},
2766 QCBOR_ERR_HIT_END
2767 },
2768 { "Text string missing 1 byte argument",
2769 QCBOR_DECODE_MODE_NORMAL,
2770 {"\x78", 1},
2771 QCBOR_ERR_HIT_END
2772 },
2773 { "Array missing 1 byte argument",
2774 QCBOR_DECODE_MODE_NORMAL,
2775 {"\x98", 1},
2776 QCBOR_ERR_HIT_END
2777 },
2778 { "Map missing 1 byte argument",
2779 QCBOR_DECODE_MODE_NORMAL,
2780 {"\xb8", 1},
2781 QCBOR_ERR_HIT_END
2782 },
2783 { "Tag missing 1 byte argument",
2784 QCBOR_DECODE_MODE_NORMAL,
2785 {"\xd8", 1},
2786 QCBOR_ERR_HIT_END
2787 },
2788 { "Simple missing 1 byte argument",
2789 QCBOR_DECODE_MODE_NORMAL,
2790 {"\xf8", 1},
2791 QCBOR_ERR_HIT_END
2792 },
2793 { "half-precision with 1 byte argument",
2794 QCBOR_DECODE_MODE_NORMAL,
2795 {"\xf9\x00", 2},
2796 QCBOR_ERR_HIT_END
2797 },
2798 { "single-precision with 2 byte argument",
2799 QCBOR_DECODE_MODE_NORMAL,
2800 {"\0xfa\x00\x00", 3},
2801 QCBOR_ERR_HIT_END
2802 },
2803 { "double-precision with 3 byte argument",
2804 QCBOR_DECODE_MODE_NORMAL,
2805 {"\xfb\x00\x00\x00", 4},
2806 QCBOR_ERR_HIT_END
2807 },
Laurence Lundblade2f467f92020-10-09 17:50:11 -07002808
Laurence Lundblade37286c02022-09-03 10:05:02 -07002809#ifndef QCBOR_DISABLE_TAGS
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002810 { "Tag with no content",
2811 QCBOR_DECODE_MODE_NORMAL,
2812 {"\xc0", 1},
2813 QCBOR_ERR_HIT_END
2814 },
Laurence Lundblade37286c02022-09-03 10:05:02 -07002815#else /* QCBOR_DISABLE_TAGS */
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002816 { "Tag with no content",
2817 QCBOR_DECODE_MODE_NORMAL,
2818 {"\xc0", 1},
2819 QCBOR_ERR_TAGS_DISABLED
2820 },
Laurence Lundblade37286c02022-09-03 10:05:02 -07002821#endif /* QCBOR_DISABLE_TAGS */
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002822
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002823 /* Breaks must not occur in definite length arrays and maps */
2824 { "Array of length 1 with sole member replaced by a break",
2825 QCBOR_DECODE_MODE_NORMAL,
2826 {"\x81\xff", 2},
2827 QCBOR_ERR_BAD_BREAK
2828 },
2829 { "Array of length 2 with 2nd member replaced by a break",
2830 QCBOR_DECODE_MODE_NORMAL,
2831 {"\x82\x00\xff", 3},
2832 QCBOR_ERR_BAD_BREAK
2833 },
2834 { "Map of length 1 with sole member label replaced by a break",
2835 QCBOR_DECODE_MODE_NORMAL,
2836 {"\xa1\xff", 2},
2837 QCBOR_ERR_BAD_BREAK
2838 },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002839
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002840 /* Map of length 1 with sole member label replaced by break */
2841 { "Alternate representation that some decoders handle differently",
2842 QCBOR_DECODE_MODE_NORMAL,
2843 {"\xa1\xff\x00", 3},
2844 QCBOR_ERR_BAD_BREAK
2845 },
2846 { "Array of length 1 with 2nd member value replaced by a break",
2847 QCBOR_DECODE_MODE_NORMAL,
2848 {"\xa1\x00\xff", 3},
2849 QCBOR_ERR_BAD_BREAK
2850 },
2851 { "Map of length 2 with 2nd member replaced by a break",
2852 QCBOR_DECODE_MODE_NORMAL,
2853 {"\xa2\x00\x00\xff", 4},
2854 QCBOR_ERR_BAD_BREAK
2855 },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002856
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002857 /* Breaks must not occur on their own out of an indefinite length data item */
2858 { "A bare break is not well formed",
2859 QCBOR_DECODE_MODE_NORMAL,
2860 {"\xff", 1},
2861 QCBOR_ERR_BAD_BREAK
2862 },
2863 { "A bare break after a zero length definite length array",
2864 QCBOR_DECODE_MODE_NORMAL,
2865 {"\x80\xff", 2},
2866 QCBOR_ERR_BAD_BREAK
2867 },
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08002868#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002869 { "A bare break after a zero length indefinite length map",
2870 QCBOR_DECODE_MODE_NORMAL,
2871 {"\x9f\xff\xff", 3},
2872 QCBOR_ERR_BAD_BREAK
2873 },
2874 { "A break inside a definite length array inside an indefenite length array",
2875 QCBOR_DECODE_MODE_NORMAL,
2876 {"\x9f\x81\xff", 3},
2877 QCBOR_ERR_BAD_BREAK
2878 },
2879 { "Complicated mixed nesting with break outside indefinite length array",
2880 QCBOR_DECODE_MODE_NORMAL,
2881 {"\x9f\x82\x9f\x81\x9f\x9f\xff\xff\xff\xff", 10},
2882 QCBOR_ERR_BAD_BREAK },
2883#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
2884
2885 /* Forbidden two byte encodings of simple types */
2886 { "Must use 0xe0 instead",
2887 QCBOR_DECODE_MODE_NORMAL,
2888 {"\xf8\x00", 2},
2889 QCBOR_ERR_BAD_TYPE_7
2890 },
2891 { "Should use 0xe1 instead",
2892 QCBOR_DECODE_MODE_NORMAL,
2893 {"\xf8\x01", 2},
2894 QCBOR_ERR_BAD_TYPE_7
2895 },
2896 { "Should use 0xe2 instead",
2897 QCBOR_DECODE_MODE_NORMAL,
2898 {"\xf8\x02", 2},
2899 QCBOR_ERR_BAD_TYPE_7
2900 }, { "Should use 0xe3 instead",
2901 QCBOR_DECODE_MODE_NORMAL,
2902 {"\xf8\x03", 2},
2903 QCBOR_ERR_BAD_TYPE_7
2904 },
2905 { "Should use 0xe4 instead",
2906 QCBOR_DECODE_MODE_NORMAL,
2907 {"\xf8\x04", 2},
2908 QCBOR_ERR_BAD_TYPE_7
2909 },
2910 { "Should use 0xe5 instead",
2911 QCBOR_DECODE_MODE_NORMAL,
2912 {"\xf8\x05", 2},
2913 QCBOR_ERR_BAD_TYPE_7
2914 },
2915 { "Should use 0xe6 instead",
2916 QCBOR_DECODE_MODE_NORMAL,
2917 {"\xf8\x06", 2},
2918 QCBOR_ERR_BAD_TYPE_7
2919 },
2920 { "Should use 0xe7 instead",
2921 QCBOR_DECODE_MODE_NORMAL,
2922 {"\xf8\x07", 2},
2923 QCBOR_ERR_BAD_TYPE_7
2924 },
2925 { "Should use 0xe8 instead",
2926 QCBOR_DECODE_MODE_NORMAL,
2927 {"\xf8\x08", 2},
2928 QCBOR_ERR_BAD_TYPE_7
2929 },
2930 { "Should use 0xe9 instead",
2931 QCBOR_DECODE_MODE_NORMAL,
2932 {"\xf8\x09", 2},
2933 QCBOR_ERR_BAD_TYPE_7
2934 },
2935 { "Should use 0xea instead",
2936 QCBOR_DECODE_MODE_NORMAL,
2937 {"\xf8\x0a", 2},
2938 QCBOR_ERR_BAD_TYPE_7
2939 },
2940 { "Should use 0xeb instead",
2941 QCBOR_DECODE_MODE_NORMAL,
2942 {"\xf8\x0b", 2},
2943 QCBOR_ERR_BAD_TYPE_7
2944 },
2945 { "Should use 0xec instead",
2946 QCBOR_DECODE_MODE_NORMAL,
2947 {"\xf8\x0c", 2},
2948 QCBOR_ERR_BAD_TYPE_7
2949 },
2950 { "Should use 0xed instead",
2951 QCBOR_DECODE_MODE_NORMAL,
2952 {"\xf8\x0d", 2},
2953 QCBOR_ERR_BAD_TYPE_7
2954 },
2955 { "Should use 0xee instead",
2956 QCBOR_DECODE_MODE_NORMAL,
2957 {"\xf8\x0e", 2},
2958 QCBOR_ERR_BAD_TYPE_7
2959 },
2960 { "Should use 0xef instead",
2961 QCBOR_DECODE_MODE_NORMAL,
2962 {"\xf8\x0f", 2},
2963 QCBOR_ERR_BAD_TYPE_7
2964 },
2965 { "Should use 0xf0 instead",
2966 QCBOR_DECODE_MODE_NORMAL,
2967 {"\xf8\x10", 2},
2968 QCBOR_ERR_BAD_TYPE_7
2969 },
2970 { "Should use 0xf1 instead",
2971 QCBOR_DECODE_MODE_NORMAL,
2972 {"\xf8\x11", 2},
2973 QCBOR_ERR_BAD_TYPE_7
2974 },
2975 { "Should use 0xf2 instead",
2976 QCBOR_DECODE_MODE_NORMAL,
2977 {"\xf8\x12", 2},
2978 QCBOR_ERR_BAD_TYPE_7
2979 },
2980 { "Should use 0xf3 instead",
2981 QCBOR_DECODE_MODE_NORMAL,
2982 {"\xf8\x13", 2},
2983 QCBOR_ERR_BAD_TYPE_7
2984 },
2985 { "Should use 0xf4 instead",
2986 QCBOR_DECODE_MODE_NORMAL,
2987 {"\xf8\x14", 2},
2988 QCBOR_ERR_BAD_TYPE_7
2989 },
2990 { "Should use 0xf5 instead",
2991 QCBOR_DECODE_MODE_NORMAL,
2992 {"\xf8\x15", 2},
2993 QCBOR_ERR_BAD_TYPE_7
2994 },
2995 { "Should use 0xf6 instead",
2996 QCBOR_DECODE_MODE_NORMAL,
2997 {"\xf8\x16", 2},
2998 QCBOR_ERR_BAD_TYPE_7
2999 },
3000 { "Should use 0xef7 instead",
3001 QCBOR_DECODE_MODE_NORMAL,
3002 {"\xf8\x17", 2},
3003 QCBOR_ERR_BAD_TYPE_7
3004 },
3005 { "Should use 0xef8 instead",
3006 QCBOR_DECODE_MODE_NORMAL,
3007 {"\xf8\x18", 2},
3008 QCBOR_ERR_BAD_TYPE_7
3009 },
3010 { "Reserved",
3011 QCBOR_DECODE_MODE_NORMAL,
3012 {"\xf8\x18", 2},
3013 QCBOR_ERR_BAD_TYPE_7
3014 },
3015
3016 /* Maps must have an even number of data items (key & value) */
3017 { "Map with 1 item when it should have 2",
3018 QCBOR_DECODE_MODE_NORMAL,
3019 {"\xa1\x00", 2},
3020 QCBOR_ERR_HIT_END
3021 },
3022 { "Map with 3 item when it should have 4",
3023 QCBOR_DECODE_MODE_NORMAL,
3024 {"\xa2\x00\x00\x00", 2},
3025 QCBOR_ERR_HIT_END
3026 },
3027#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
3028 { "Map with 1 item when it should have 2",
3029 QCBOR_DECODE_MODE_NORMAL,
3030 {"\xbf\x00\xff", 3},
3031 QCBOR_ERR_BAD_BREAK
3032 },
3033 { "Map with 3 item when it should have 4",
3034 QCBOR_DECODE_MODE_NORMAL,
3035 {"\xbf\x00\x00\x00\xff", 5},
3036 QCBOR_ERR_BAD_BREAK
3037 },
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08003038#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07003039
3040
Laurence Lundblade37286c02022-09-03 10:05:02 -07003041#ifndef QCBOR_DISABLE_TAGS
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08003042 /* In addition to not-well-formed, some invalid CBOR */
3043 { "Text-based date, with an integer",
3044 QCBOR_DECODE_MODE_NORMAL,
3045 {"\xc0\x00", 2},
3046 QCBOR_ERR_BAD_OPT_TAG
3047 },
3048 { "Epoch date, with an byte string",
3049 QCBOR_DECODE_MODE_NORMAL,
3050 {"\xc1\x41\x33", 3},
3051 QCBOR_ERR_BAD_OPT_TAG
3052 },
3053 { "tagged as both epoch and string dates",
3054 QCBOR_DECODE_MODE_NORMAL,
3055 {"\xc1\xc0\x00", 3},
3056 QCBOR_ERR_BAD_OPT_TAG
3057 },
3058 { "big num tagged an int, not a byte string",
3059 QCBOR_DECODE_MODE_NORMAL,
3060 {"\xc2\x00", 2},
3061 QCBOR_ERR_BAD_OPT_TAG
3062 },
Laurence Lundblade37286c02022-09-03 10:05:02 -07003063#else /* QCBOR_DISABLE_TAGS */
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08003064 /* In addition to not-well-formed, some invalid CBOR */
3065 { "Text-based date, with an integer",
3066 QCBOR_DECODE_MODE_NORMAL,
3067 {"\xc0\x00", 2},
3068 QCBOR_ERR_TAGS_DISABLED
3069 },
3070 { "Epoch date, with an byte string",
3071 QCBOR_DECODE_MODE_NORMAL,
3072 {"\xc1\x41\x33", 3},
3073 QCBOR_ERR_TAGS_DISABLED
3074 },
3075 { "tagged as both epoch and string dates",
3076 QCBOR_DECODE_MODE_NORMAL,
3077 {"\xc1\xc0\x00", 3},
3078 QCBOR_ERR_TAGS_DISABLED
3079 },
3080 { "big num tagged an int, not a byte string",
3081 QCBOR_DECODE_MODE_NORMAL,
3082 {"\xc2\x00", 2},
3083 QCBOR_ERR_TAGS_DISABLED
3084 },
Laurence Lundblade37286c02022-09-03 10:05:02 -07003085#endif /* QCBOR_DISABLE_TAGS */
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003086};
3087
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08003088
3089
3090int32_t
3091DecodeFailureTests(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003092{
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003093 int32_t nResult;
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07003094
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08003095 nResult = ProcessDecodeFailures(Failures ,C_ARRAY_COUNT(Failures, struct DecodeFailTestInput));
Laurence Lundblade59289e52019-12-30 13:44:37 -08003096 if(nResult) {
3097 return nResult;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003098 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003099
Laurence Lundblade3a6042e2019-06-28 19:58:04 -07003100 // Corrupt the UsefulInputBuf and see that
3101 // it reflected correctly for CBOR decoding
Laurence Lundbladee6f15112020-07-23 18:44:16 -07003102 QCBORDecodeContext DCtx;
3103 QCBORItem Item;
3104 QCBORError uQCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003105
Laurence Lundbladee6f15112020-07-23 18:44:16 -07003106 QCBORDecode_Init(&DCtx,
3107 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSimpleValues),
3108 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003109
Laurence Lundbladee6f15112020-07-23 18:44:16 -07003110 if((uQCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
3111 return (int32_t)uQCBORError;
3112 }
3113 if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.val.uCount != 10) {
3114 // This wasn't supposed to happen
3115 return -1;
3116 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003117
Laurence Lundbladee6f15112020-07-23 18:44:16 -07003118 DCtx.InBuf.magic = 0; // Reach in and corrupt the UsefulInputBuf
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003119
Laurence Lundbladee6f15112020-07-23 18:44:16 -07003120 uQCBORError = QCBORDecode_GetNext(&DCtx, &Item);
3121 if(uQCBORError != QCBOR_ERR_NO_MORE_ITEMS) {
3122 // Did not get back the error expected
3123 return -2;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003124 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003125
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003126
Laurence Lundblade98427e92020-09-28 21:33:23 -07003127 /*
3128 The max size of a string for QCBOR is SIZE_MAX - 4 so this
3129 tests here can be performed to see that the max length
3130 error check works correctly. See DecodeBytes(). If the max
3131 size was SIZE_MAX, it wouldn't be possible to test this.
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003132
Laurence Lundblade98427e92020-09-28 21:33:23 -07003133 This test will automatocally adapt the all CPU sizes
3134 through the use of SIZE_MAX.
3135 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003136
Laurence Lundblade8510f8c2020-12-01 11:31:16 -08003137 UsefulBuf_MAKE_STACK_UB( HeadBuf, QCBOR_HEAD_BUFFER_SIZE);
Laurence Lundblade98427e92020-09-28 21:33:23 -07003138 UsefulBufC EncodedHead;
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003139
Laurence Lundblade98427e92020-09-28 21:33:23 -07003140 // This makes a CBOR head with a text string that is very long
3141 // but doesn't fill in the bytes of the text string as that is
3142 // not needed to test this part of QCBOR.
3143 EncodedHead = QCBOREncode_EncodeHead(HeadBuf, CBOR_MAJOR_TYPE_TEXT_STRING, 0, SIZE_MAX);
3144
3145 QCBORDecode_Init(&DCtx, EncodedHead, QCBOR_DECODE_MODE_NORMAL);
3146
3147 if(QCBOR_ERR_STRING_TOO_LONG != QCBORDecode_GetNext(&DCtx, &Item)) {
3148 return -4;
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003149 }
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003150
Laurence Lundblade3a6042e2019-06-28 19:58:04 -07003151 return 0;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003152}
3153
3154
Laurence Lundbladea2e29072018-12-30 09:20:06 -08003155/* Try all 256 values of the byte at nLen including recursing for
3156 each of the values to try values at nLen+1 ... up to nLenMax
3157 */
Laurence Lundblade06350ea2020-01-27 19:32:40 -08003158static void ComprehensiveInputRecurser(uint8_t *pBuf, size_t nLen, size_t nLenMax)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003159{
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003160 if(nLen >= nLenMax) {
3161 return;
3162 }
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08003163
Laurence Lundbladea2e29072018-12-30 09:20:06 -08003164 for(int inputByte = 0; inputByte < 256; inputByte++) {
3165 // Set up the input
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003166 pBuf[nLen] = (uint8_t)inputByte;
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08003167 const UsefulBufC Input = {pBuf, nLen+1};
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08003168
Laurence Lundbladea2e29072018-12-30 09:20:06 -08003169 // Get ready to parse
3170 QCBORDecodeContext DCtx;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003171 QCBORDecode_Init(&DCtx, Input, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003172
Laurence Lundbladea2e29072018-12-30 09:20:06 -08003173 // Parse by getting the next item until an error occurs
3174 // Just about every possible decoder error can occur here
3175 // The goal of this test is not to check for the correct
3176 // error since that is not really possible. It is to
3177 // see that there is no crash on hostile input.
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003178 while(1) {
Laurence Lundbladea2e29072018-12-30 09:20:06 -08003179 QCBORItem Item;
3180 QCBORError nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003181 if(nCBORError != QCBOR_SUCCESS) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003182 break;
3183 }
3184 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003185
Laurence Lundbladea2e29072018-12-30 09:20:06 -08003186 ComprehensiveInputRecurser(pBuf, nLen+1, nLenMax);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003187 }
3188}
3189
3190
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03003191int32_t ComprehensiveInputTest(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003192{
Laurence Lundbladea2e29072018-12-30 09:20:06 -08003193 // Size 2 tests 64K inputs and runs quickly
3194 uint8_t pBuf[2];
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003195
Laurence Lundbladea2e29072018-12-30 09:20:06 -08003196 ComprehensiveInputRecurser(pBuf, 0, sizeof(pBuf));
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003197
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003198 return 0;
3199}
3200
Laurence Lundbladea2e29072018-12-30 09:20:06 -08003201
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03003202int32_t BigComprehensiveInputTest(void)
Laurence Lundbladea2e29072018-12-30 09:20:06 -08003203{
3204 // size 3 tests 16 million inputs and runs OK
3205 // in seconds on fast machines. Size 4 takes
3206 // 10+ minutes and 5 half a day on fast
3207 // machines. This test is kept separate from
3208 // the others so as to no slow down the use
3209 // of them as a very frequent regression.
3210 uint8_t pBuf[3]; //
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08003211
Laurence Lundbladea2e29072018-12-30 09:20:06 -08003212 ComprehensiveInputRecurser(pBuf, 0, sizeof(pBuf));
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08003213
Laurence Lundbladea2e29072018-12-30 09:20:06 -08003214 return 0;
3215}
3216
3217
Laurence Lundbladecc7da412020-12-27 00:09:07 -08003218static const uint8_t spDateTestInput[] = {
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003219 /* 1. The valid date string "1985-04-12" */
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003220 0xc0, // tag for string date
3221 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003222
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003223 /* 2. An invalid date string due to wrong tag content type */
Laurence Lundbladec7114722020-08-13 05:11:40 -07003224 0xc0, // tag for string date
3225 0x00, // Wrong type for a string date
3226
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003227 /* 3. A valid epoch date, 1400000000; Tue, 13 May 2014 16:53:20 GMT */
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003228 0xc1, // tag for epoch date
3229 0x1a, 0x53, 0x72, 0x4E, 0x00, // Epoch date 1400000000; Tue, 13 May 2014 16:53:20 GMT
3230
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003231 /* 4. An invalid epoch date due to wrong tag content type */
Laurence Lundbladec7114722020-08-13 05:11:40 -07003232 0xc1,
3233 0x62, 'h', 'i', // wrong type tagged
3234
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003235 /* 5. Valid epoch date tag as content for a two other nested tags */
Laurence Lundblade99615302020-11-29 11:19:47 -08003236 // CBOR_TAG_ENC_AS_B64
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003237 0xcf, 0xd8, 0x16, 0xc1, // Epoch date with extra tags
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003238 0x1a, 0x53, 0x72, 0x4E, 0x01,
3239
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003240 /* 6. Epoch date with value to large to fit into int64 */
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003241 0xc1, // tag for epoch date
3242 0x1b, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, // Too large integer
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003243
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003244 /* 7. Epoch date with single-precision value of 1.1. */
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003245 0xc1, // tag for epoch date
Laurence Lundblade3ed0bca2020-07-14 22:50:10 -07003246 0xfa, 0x3f, 0x8c, 0xcc, 0xcd, // single with value 1.1
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003247
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003248 /* 8. Epoch date with too-large single precision float */
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003249 0xc1, // tag for epoch date
Laurence Lundbladea1ad8782019-11-08 00:12:11 -08003250 0xfa, 0x7f, 0x7f, 0xff, 0xff, // 3.4028234663852886e+38 too large
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003251
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003252 /* 9. Epoch date with slightly too-large double precision value */
Laurence Lundbladea1ad8782019-11-08 00:12:11 -08003253 0xc1, // tag for epoch date
3254 0xfb, 0x43, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 9223372036854775808.000000 just barely too large
3255 //0xfa, 0x7f, 0x7f, 0xff, 0xff // 3.4028234663852886e+38 too large
3256
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003257 /* 10. Epoch date with largest supported double precision value */
Laurence Lundbladea1ad8782019-11-08 00:12:11 -08003258 0xc1, // tag for epoch date
Laurence Lundbladec7114722020-08-13 05:11:40 -07003259 0xfb, 0x43, 0xdf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, // 9223372036854773760 largest supported
3260
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003261 /* 11. Epoch date with single-precision NaN */
Laurence Lundbladec7114722020-08-13 05:11:40 -07003262 0xc1, // tag for epoch date
3263 0xfa, 0x7f, 0xc0, 0x00, 0x00, // Single-precision NaN
3264
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003265 /* 12. Epoch date with double precision plus infinity */
Laurence Lundbladec7114722020-08-13 05:11:40 -07003266 0xc1,
3267 0xfb, 0x7f, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // +infinity
3268
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003269 /* 13. Epoch date with half-precision negative infinity */
Laurence Lundbladec7114722020-08-13 05:11:40 -07003270 0xc1, // tag for epoch date
3271 0xf9, 0xfc, 0x00, // -Infinity
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003272};
3273
3274
Laurence Lundbladec7114722020-08-13 05:11:40 -07003275
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003276// have to check float expected only to within an epsilon
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07003277#ifndef QCBOR_DISABLE_FLOAT_HW_USE
Laurence Lundblade02fcf312020-07-17 02:49:46 -07003278static int CHECK_EXPECTED_DOUBLE(double val, double expected) {
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003279
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003280 double diff = val - expected;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003281
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003282 diff = fabs(diff);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003283
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003284 return diff > 0.0000001;
3285}
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07003286#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003287
3288
Laurence Lundblade37286c02022-09-03 10:05:02 -07003289/* Test date decoding using GetNext() */
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03003290int32_t DateParseTest(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003291{
3292 QCBORDecodeContext DCtx;
Laurence Lundbladec7114722020-08-13 05:11:40 -07003293 QCBORItem Item;
3294 QCBORError uError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003295
Laurence Lundbladeee851742020-01-08 08:37:05 -08003296 QCBORDecode_Init(&DCtx,
3297 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spDateTestInput),
3298 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003299
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003300 /* 1. The valid date string "1985-04-12" */
Laurence Lundbladec7114722020-08-13 05:11:40 -07003301 if((uError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003302 return -1;
Laurence Lundbladec7114722020-08-13 05:11:40 -07003303 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003304 if(Item.uDataType != QCBOR_TYPE_DATE_STRING ||
Laurence Lundbladeba587682024-02-07 16:46:43 -08003305 UsefulBufCompareToSZ(Item.val.string, "1985-04-12")){
Laurence Lundblade67bd5512018-11-02 21:44:06 +07003306 return -2;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003307 }
3308
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003309 /* 2. An invalid date string due to wrong tag content type */
Laurence Lundbladec7114722020-08-13 05:11:40 -07003310 uError = QCBORDecode_GetNext(&DCtx, &Item);
3311 if(uError != QCBOR_ERR_BAD_OPT_TAG) {
Laurence Lundblade67bd5512018-11-02 21:44:06 +07003312 return -3;
Laurence Lundbladec7114722020-08-13 05:11:40 -07003313 }
3314
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003315 /* 3. A valid epoch date, 1400000000; Tue, 13 May 2014 16:53:20 GMT */
3316 uError = QCBORDecode_GetNext(&DCtx, &Item);
3317 if(uError != QCBOR_SUCCESS) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07003318 return -4;
3319 }
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003320 if(uError == QCBOR_SUCCESS) {
3321 if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH ||
3322 Item.val.epochDate.nSeconds != 1400000000
Máté Tóth-Pálef5f07a2021-09-17 19:31:37 +02003323#ifndef USEFULBUF_DISABLE_ALL_FLOAT
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003324 || Item.val.epochDate.fSecondsFraction != 0
Máté Tóth-Pálef5f07a2021-09-17 19:31:37 +02003325#endif /* USEFULBUF_DISABLE_ALL_FLOAT */
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003326 ) {
3327 return -5;
3328 }
Laurence Lundbladec7114722020-08-13 05:11:40 -07003329 }
3330
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003331 /* 4. An invalid epoch date due to wrong tag content type */
Laurence Lundbladec7114722020-08-13 05:11:40 -07003332 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_OPT_TAG) {
3333 return -6;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003334 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003335
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003336 /* 5. Valid epoch date tag as content for a two other nested tags */
Laurence Lundblade99615302020-11-29 11:19:47 -08003337 // Epoch date wrapped in an CBOR_TAG_ENC_AS_B64 and an unknown tag.
3338 // The date is decoded and the two tags are returned. This is to
3339 // make sure the wrapping of epoch date in another tag works OK.
Laurence Lundbladec7114722020-08-13 05:11:40 -07003340 if((uError = QCBORDecode_GetNext(&DCtx, &Item))) {
3341 return -7;
3342 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003343 if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH ||
3344 Item.val.epochDate.nSeconds != 1400000001 ||
Máté Tóth-Pálef5f07a2021-09-17 19:31:37 +02003345#ifndef USEFULBUF_DISABLE_ALL_FLOAT
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003346 Item.val.epochDate.fSecondsFraction != 0 ||
Máté Tóth-Pálef5f07a2021-09-17 19:31:37 +02003347#endif /* USEFULBUF_DISABLE_ALL_FLOAT */
Laurence Lundblade99615302020-11-29 11:19:47 -08003348 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_ENC_AS_B64)) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07003349 return -8;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003350 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003351
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003352 /* 6. Epoch date with value to large to fit into int64 */
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003353 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_DATE_OVERFLOW) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07003354 return -9;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003355 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003356
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003357 /* 7. Epoch date with single-precision value of 1.1. */
3358 uError = QCBORDecode_GetNext(&DCtx, &Item);
3359 if(uError != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07003360 return -10;
3361 }
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003362 if(uError == QCBOR_SUCCESS) {
3363 if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH ||
3364 Item.val.epochDate.nSeconds != 1
3365#ifndef QCBOR_DISABLE_FLOAT_HW_USE
3366 || CHECK_EXPECTED_DOUBLE(Item.val.epochDate.fSecondsFraction, 0.1)
Máté Tóth-Pálef5f07a2021-09-17 19:31:37 +02003367#endif /* USEFULBUF_DISABLE_ALL_FLOAT */
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003368 ) {
3369 return -11;
3370 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003371 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003372
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003373 /* 8. Epoch date with too-large single-precision float */
3374 uError = QCBORDecode_GetNext(&DCtx, &Item);
3375 if(uError != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_DATE_OVERFLOW)) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07003376 return -12;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003377 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003378
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003379 /* 9. Epoch date with slightly too-large double-precision value */
3380 uError = QCBORDecode_GetNext(&DCtx, &Item);
3381 if(uError != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_DATE_OVERFLOW)) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07003382 return -13;
Laurence Lundbladea1ad8782019-11-08 00:12:11 -08003383 }
3384
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003385 /* 10. Epoch date with largest supported double-precision value */
3386 uError = QCBORDecode_GetNext(&DCtx, &Item);
3387 if(uError != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)) {
3388 return -14;
3389 }
3390 if(uError == QCBOR_SUCCESS) {
3391 if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH ||
3392 Item.val.epochDate.nSeconds != 9223372036854773760
3393#ifndef QCBOR_DISABLE_FLOAT_HW_USE
3394 || Item.val.epochDate.fSecondsFraction != 0.0
3395#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
3396 ) {
3397 return -14;
3398 }
3399 }
Laurence Lundbladec7114722020-08-13 05:11:40 -07003400
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003401 /* 11. Epoch date with single-precision NaN */
3402 if(QCBORDecode_GetNext(&DCtx, &Item) != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_DATE_OVERFLOW)) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07003403 return -15;
3404 }
3405
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003406 /* 12. Epoch date with double-precision plus infinity */
3407 if(QCBORDecode_GetNext(&DCtx, &Item) != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_DATE_OVERFLOW)) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07003408 return -16;
3409 }
3410
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003411 /* 13. Epoch date with half-precision negative infinity */
3412 uError = QCBORDecode_GetNext(&DCtx, &Item);
3413 if(uError != FLOAT_ERR_CODE_NO_HALF_PREC_NO_FLOAT_HW(QCBOR_ERR_DATE_OVERFLOW)) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07003414 return -17;
3415 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003416
3417 return 0;
3418}
3419
Laurence Lundblade37286c02022-09-03 10:05:02 -07003420
Laurence Lundblade4b270642020-08-14 12:53:07 -07003421/*
3422 Test cases covered here. Some items cover more than one of these.
3423 positive integer (zero counts as a positive integer)
3424 negative integer
3425 half-precision float
3426 single-precision float
3427 double-precision float
Laurence Lundbladec7114722020-08-13 05:11:40 -07003428
Laurence Lundblade4b270642020-08-14 12:53:07 -07003429 float Overflow error
3430 Wrong type error for epoch
3431 Wrong type error for date string
3432 float disabled error
3433 half-precision disabled error
3434 -Infinity
3435 Slightly too large integer
3436 Slightly too far from zero
Laurence Lundbladec7114722020-08-13 05:11:40 -07003437
Laurence Lundblade4b270642020-08-14 12:53:07 -07003438 Get epoch by int
3439 Get string by int
3440 Get epoch by string
3441 Get string by string
3442 Fail to get epoch by wrong int label
3443 Fail to get string by wrong string label
3444 Fail to get epoch by string because it is invalid
3445 Fail to get epoch by int because it is invalid
3446
3447 Untagged values
3448 */
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08003449static const uint8_t spSpiffyDateTestInput[] = {
Laurence Lundblade37286c02022-09-03 10:05:02 -07003450 0x87, // array of 7 items
3451
3452 0xa6, // Open a map for tests involving untagged items with labels.
3453
3454 // Untagged integer 0
3455 0x08,
3456 0x00,
3457
3458 // Utagged date string with string label y
3459 0x61, 0x79,
3460 0x6a, '2','0','8','5','-','0','4','-','1','2', // Untagged date string
3461
3462 // Untagged single-precision float with value 3.14 with string label x
3463 0x61, 0x78,
3464 0xFA, 0x40, 0x48, 0xF5, 0xC3,
3465
3466 // Untagged half-precision float with value -2
3467 0x09,
3468 0xF9, 0xC0, 0x00,
3469
3470 /* Untagged date-only date string */
3471 0x18, 0x63,
3472 0x6A, 0x31, 0x39, 0x38, 0x35, 0x2D, 0x30, 0x34, 0x2D, 0x31, 0x32, /* "1985-04-12" */
3473
3474 /* Untagged days-count epoch date */
3475 0x11,
3476 0x19, 0x0F, 0x9A, /* 3994 */
3477
3478 // End of map, back to array
3479
3480 0xa7, // Open map of tagged items with labels
3481
3482 0x00,
3483 0xc0, // tag for string date
3484 0x6a, '1','9','8','5','-','0','4','-','1','2', // Tagged date string
3485
3486
3487 0x01,
3488 0xda, 0x03, 0x03, 0x03, 0x03, // An additional tag
3489 0xc1, // tag for epoch date
3490 0x1a, 0x53, 0x72, 0x4E, 0x00, // Epoch date 1400000000; Tue, 13 May 2014 16:53:20 GMT
3491
3492 0x05,
3493 0xc1,
3494 0xfb, 0xc3, 0xdf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, // -9223372036854773760 largest negative
3495
3496
3497 0x07,
3498 0xc1, // tag for epoch date
3499 0xfb, 0x43, 0xdf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, // 9223372036854773760 largest supported
3500
3501 /* Tagged days-count epoch date */
3502 0x63, 0x53, 0x44, 0x45,
3503 0xD8, 0x64, /* tag(100) */
3504 0x39, 0x29, 0xB3, /* -10676 */
3505
3506 // Untagged -1000 with label z
3507 0x61, 0x7a,
3508 0xda, 0x01, 0x01, 0x01, 0x01, // An additional tag
3509 0x39, 0x03, 0xe7,
3510
3511 /* Tagged date-only date string */
3512 0x63, 0x53, 0x44, 0x53,
3513 0xD9, 0x03, 0xEC,
3514 0x6A, 0x31, 0x39, 0x38, 0x35, 0x2D, 0x30, 0x34, 0x2D, 0x31, 0x32, /* "1985-04-12" */
3515
3516 // End of map of tagged items
Laurence Lundblade4b270642020-08-14 12:53:07 -07003517
3518 0xc1,
3519 0xfb, 0xc3, 0xdf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, // -9.2233720368547748E+18, too negative
3520
Laurence Lundbladec7114722020-08-13 05:11:40 -07003521 0xc1, // tag for epoch date
Laurence Lundblade4b270642020-08-14 12:53:07 -07003522 0x1b, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, // Too-large integer
3523
3524 0xc1, // tag for epoch date
3525 0xf9, 0xfc, 0x00, // Half-precision -Infinity
3526
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07003527 // These two at the end because they are unrecoverable errors
3528 0xc1, // tag for epoch date
3529 0x80, // Erroneous empty array as content for date
3530
3531 0xc0, // tag for string date
3532 0xa0 // Erroneous empty map as content for date
Laurence Lundbladec7114722020-08-13 05:11:40 -07003533};
3534
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03003535int32_t SpiffyDateDecodeTest(void)
Laurence Lundbladec7114722020-08-13 05:11:40 -07003536{
3537 QCBORDecodeContext DC;
Laurence Lundblade4b270642020-08-14 12:53:07 -07003538 QCBORError uError;
Laurence Lundblade37286c02022-09-03 10:05:02 -07003539 int64_t nEpochDate3, nEpochDate5,
3540 nEpochDate4, nEpochDate6,
3541 nEpochDays2;
3542 UsefulBufC StringDate1, StringDate2, StringDays2;
Laurence Lundbladec7114722020-08-13 05:11:40 -07003543
3544 QCBORDecode_Init(&DC,
Laurence Lundblade4b270642020-08-14 12:53:07 -07003545 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSpiffyDateTestInput),
Laurence Lundbladec7114722020-08-13 05:11:40 -07003546 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade37286c02022-09-03 10:05:02 -07003547
3548 /* Items are in an array or map to test look up by label and other
3549 * that might not occur in isolated items. But it does make the
3550 * test a bit messy. */
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07003551 QCBORDecode_EnterArray(&DC, NULL);
Laurence Lundbladec7114722020-08-13 05:11:40 -07003552
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07003553 QCBORDecode_EnterMap(&DC, NULL);
Laurence Lundblade4b270642020-08-14 12:53:07 -07003554
Laurence Lundblade4b270642020-08-14 12:53:07 -07003555 // A single-precision date
Laurence Lundblade9b334962020-08-27 10:55:53 -07003556 QCBORDecode_GetEpochDateInMapSZ(&DC, "x", QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
3557 &nEpochDate5);
Laurence Lundblade4b270642020-08-14 12:53:07 -07003558 uError = QCBORDecode_GetAndResetError(&DC);
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003559 if(uError != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07003560 return 104;
3561 }
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003562 if(uError == QCBOR_SUCCESS) {
3563 if(nEpochDate5 != 3) {
3564 return 103;
3565 }
3566 }
Laurence Lundbladec7114722020-08-13 05:11:40 -07003567
Laurence Lundblade9b334962020-08-27 10:55:53 -07003568 // A half-precision date with value -2 FFF
3569 QCBORDecode_GetEpochDateInMapN(&DC, 9, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
3570 &nEpochDate4);
Laurence Lundblade4b270642020-08-14 12:53:07 -07003571 uError = QCBORDecode_GetAndResetError(&DC);
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003572 if(uError != FLOAT_ERR_CODE_NO_HALF_PREC_NO_FLOAT_HW(QCBOR_SUCCESS)) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07003573 return 106;
3574 }
Laurence Lundblade16a207a2021-09-18 17:22:46 -07003575 if(uError == QCBOR_SUCCESS) {
3576 if(nEpochDate4 != -2) {
3577 return 105;
3578 }
3579 }
Laurence Lundblade4b270642020-08-14 12:53:07 -07003580
3581 // Fail to get an epoch date by string label
Laurence Lundblade9b334962020-08-27 10:55:53 -07003582 QCBORDecode_GetEpochDateInMapSZ(&DC, "no-label",
3583 QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
3584 &nEpochDate6);
Laurence Lundblade4b270642020-08-14 12:53:07 -07003585 uError = QCBORDecode_GetAndResetError(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07003586 if(uError != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07003587 return 107;
3588 }
3589
3590 // Fail to get an epoch date by integer label
Laurence Lundblade9b334962020-08-27 10:55:53 -07003591 QCBORDecode_GetEpochDateInMapN(&DC, 99999, QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
3592 &nEpochDate6);
Laurence Lundblade4b270642020-08-14 12:53:07 -07003593 uError = QCBORDecode_GetAndResetError(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07003594 if(uError != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07003595 return 108;
3596 }
3597
3598 // Fail to get a string date by string label
Laurence Lundblade9b334962020-08-27 10:55:53 -07003599 QCBORDecode_GetDateStringInMapSZ(&DC, "no-label",
3600 QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
3601 &StringDate1);
Laurence Lundblade4b270642020-08-14 12:53:07 -07003602 uError = QCBORDecode_GetAndResetError(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07003603 if(uError != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07003604 return 109;
3605 }
3606
3607 // Fail to get a string date by integer label
Laurence Lundblade9b334962020-08-27 10:55:53 -07003608 QCBORDecode_GetDateStringInMapN(&DC, 99999, QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
3609 &StringDate1);
Laurence Lundblade4b270642020-08-14 12:53:07 -07003610 uError = QCBORDecode_GetAndResetError(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07003611 if(uError != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07003612 return 110;
3613 }
3614
3615 // The rest of these succeed even if float features are disabled
Laurence Lundbladea9489f82020-09-12 13:50:56 -07003616
Laurence Lundblade37286c02022-09-03 10:05:02 -07003617
3618 // Untagged integer 0
3619 QCBORDecode_GetEpochDateInMapN(&DC, 8, QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
3620 &nEpochDate3);
3621 // Untagged date string
3622 QCBORDecode_GetDateStringInMapSZ(&DC, "y", QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
3623 &StringDate2);
3624
3625 QCBORDecode_GetDaysStringInMapN(&DC, 99, QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
3626 &StringDays2);
3627
3628 QCBORDecode_GetEpochDaysInMapN(&DC, 17, QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
3629 &nEpochDays2);
3630
3631 QCBORDecode_ExitMap(&DC);
3632 if(QCBORDecode_GetError(&DC) != QCBOR_SUCCESS) {
3633 return 3001;
3634 }
3635
3636 // The map of tagged items
3637 QCBORDecode_EnterMap(&DC, NULL);
3638
3639#ifndef QCBOR_DISABLE_TAGS
3640 int64_t nEpochDate2,
3641 nEpochDateFail,
3642 nEpochDate1400000000, nEpochDays1;
3643 UsefulBufC StringDays1;
3644 uint64_t uTag1, uTag2;
3645
3646 // Tagged date string
3647 QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
3648 &StringDate1);
3649
Laurence Lundblade4b270642020-08-14 12:53:07 -07003650 // Epoch date 1400000000; Tue, 13 May 2014 16:53:20 GMT
Laurence Lundblade9b334962020-08-27 10:55:53 -07003651 QCBORDecode_GetEpochDateInMapN(&DC,
3652 1,
3653 QCBOR_TAG_REQUIREMENT_TAG |
3654 QCBOR_TAG_REQUIREMENT_ALLOW_ADDITIONAL_TAGS,
3655 &nEpochDate1400000000);
3656 uTag1 = QCBORDecode_GetNthTagOfLast(&DC, 0);
Laurence Lundblade37286c02022-09-03 10:05:02 -07003657
3658 // Get largest negative double precision epoch date allowed
3659 QCBORDecode_GetEpochDateInMapN(&DC,
3660 5,
3661 QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG |
3662 QCBOR_TAG_REQUIREMENT_ALLOW_ADDITIONAL_TAGS,
3663 &nEpochDate2);
3664 uError = QCBORDecode_GetAndResetError(&DC);
3665 if(uError != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)) {
3666 return 102;
3667 }
3668 if(uError == QCBOR_SUCCESS) {
3669 if(nEpochDate2 != -9223372036854773760LL) {
3670 return 101;
3671 }
3672 }
3673
Laurence Lundblade4b270642020-08-14 12:53:07 -07003674 // Untagged -1000 with label z
Laurence Lundblade9b334962020-08-27 10:55:53 -07003675 QCBORDecode_GetEpochDateInMapSZ(&DC,
3676 "z",
3677 QCBOR_TAG_REQUIREMENT_NOT_A_TAG |
3678 QCBOR_TAG_REQUIREMENT_ALLOW_ADDITIONAL_TAGS,
3679 &nEpochDate6);
3680 uTag2 = QCBORDecode_GetNthTagOfLast(&DC, 0);
Laurence Lundblade4b270642020-08-14 12:53:07 -07003681
Laurence Lundblade37286c02022-09-03 10:05:02 -07003682
3683 // Get largest double precision epoch date allowed
3684 QCBORDecode_GetEpochDateInMapN(&DC, 7, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
3685 &nEpochDate2);
3686 uError = QCBORDecode_GetAndResetError(&DC);
3687 if(uError != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)) {
3688 return 112;
3689 }
3690 if(uError == QCBOR_SUCCESS) {
3691 if(nEpochDate2 != 9223372036854773760ULL) {
3692 return 111;
3693 }
3694 }
3695
Laurence Lundblade46d63e92021-05-13 11:37:10 -07003696 /* The days format is much simpler than the date format
3697 * because it can't be a floating point value. The test
3698 * of the spiffy decode functions sufficiently covers
3699 * the test of the non-spiffy decode days date decoding.
3700 * There is no full fan out of the error conditions
3701 * and decode options as that is implemented by code
3702 * that is tested well by the date testing above.
3703 */
3704 QCBORDecode_GetDaysStringInMapSZ(&DC, "SDS", QCBOR_TAG_REQUIREMENT_TAG,
3705 &StringDays1);
3706
Laurence Lundblade46d63e92021-05-13 11:37:10 -07003707 QCBORDecode_GetEpochDaysInMapSZ(&DC, "SDE", QCBOR_TAG_REQUIREMENT_TAG,
3708 &nEpochDays1);
3709
Laurence Lundblade4b270642020-08-14 12:53:07 -07003710 QCBORDecode_ExitMap(&DC);
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07003711 if(QCBORDecode_GetError(&DC) != QCBOR_SUCCESS) {
3712 return 3001;
3713 }
3714
Laurence Lundblade37286c02022-09-03 10:05:02 -07003715 // Too-negative float, -9.2233720368547748E+18
3716 QCBORDecode_GetEpochDate(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nEpochDateFail);
3717 uError = QCBORDecode_GetAndResetError(&DC);
3718 if(uError != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_DATE_OVERFLOW)) {
3719 return 1111;
3720 }
3721
3722 // Too-large integer
3723 QCBORDecode_GetEpochDate(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nEpochDateFail);
3724 uError = QCBORDecode_GetAndResetError(&DC);
3725 if(uError != QCBOR_ERR_DATE_OVERFLOW) {
3726 return 1;
3727 }
3728
3729 // Half-precision minus infinity
3730 QCBORDecode_GetEpochDate(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nEpochDateFail);
3731 uError = QCBORDecode_GetAndResetError(&DC);
3732 if(uError != FLOAT_ERR_CODE_NO_HALF_PREC_NO_FLOAT_HW(QCBOR_ERR_DATE_OVERFLOW)) {
3733 return 2;
3734 }
3735
3736
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07003737 // Bad content for epoch date
3738 QCBORDecode_GetEpochDate(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nEpochDateFail);
3739 uError = QCBORDecode_GetAndResetError(&DC);
3740 if(uError != QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT) {
3741 return 3;
3742 }
3743
3744 // Bad content for string date
3745 QCBORDecode_GetDateString(&DC, QCBOR_TAG_REQUIREMENT_TAG, &StringDate1);
3746 uError = QCBORDecode_GetAndResetError(&DC);
3747 if(uError != QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT) {
3748 return 4;
3749 }
3750
Laurence Lundblade4b270642020-08-14 12:53:07 -07003751 QCBORDecode_ExitArray(&DC);
3752 uError = QCBORDecode_Finish(&DC);
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07003753 if(uError != QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT) {
Laurence Lundblade9b334962020-08-27 10:55:53 -07003754 return 1000 + (int32_t)uError;
Laurence Lundblade4b270642020-08-14 12:53:07 -07003755 }
Laurence Lundblade37286c02022-09-03 10:05:02 -07003756#else /* QCBOR_DISABLE_TAGS */
3757 QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
3758 &StringDate1);
3759 uError = QCBORDecode_GetAndResetError(&DC);
3760 if(uError != QCBOR_ERR_TAGS_DISABLED) {
3761 return 4;
3762 }
3763#endif /* QCBOR_DISABLE_TAGS */
3764
3765
3766#ifndef QCBOR_DISABLE_TAGS
Laurence Lundblade4b270642020-08-14 12:53:07 -07003767
Laurence Lundblade9b334962020-08-27 10:55:53 -07003768 if(nEpochDate1400000000 != 1400000000) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07003769 return 200;
3770 }
3771
Laurence Lundblade9b334962020-08-27 10:55:53 -07003772 if(uTag1 != 0x03030303) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07003773 return 201;
3774 }
3775
Laurence Lundblade37286c02022-09-03 10:05:02 -07003776 if(nEpochDays1 != -10676) {
3777 return 205;
Laurence Lundblade4b270642020-08-14 12:53:07 -07003778 }
3779
Laurence Lundblade37286c02022-09-03 10:05:02 -07003780 if(UsefulBuf_Compare(StringDays1, UsefulBuf_FromSZ("1985-04-12"))) {
3781 return 207;
Laurence Lundblade4b270642020-08-14 12:53:07 -07003782 }
3783
Laurence Lundblade9b334962020-08-27 10:55:53 -07003784 if(uTag2 != 0x01010101) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07003785 return 204;
3786 }
3787
Laurence Lundblade37286c02022-09-03 10:05:02 -07003788 if(nEpochDate6 != -1000) {
3789 return 203;
Laurence Lundblade46d63e92021-05-13 11:37:10 -07003790 }
3791
Laurence Lundblade9b334962020-08-27 10:55:53 -07003792 if(UsefulBuf_Compare(StringDate1, UsefulBuf_FromSZ("1985-04-12"))) {
3793 return 205;
3794 }
3795
Laurence Lundblade37286c02022-09-03 10:05:02 -07003796#endif /* QCBOR_DISABLE_TAGS */
3797
3798 if(nEpochDate3 != 0) {
3799 return 202;
3800 }
3801
3802 if(nEpochDays2 != 3994) {
Laurence Lundblade9b334962020-08-27 10:55:53 -07003803 return 206;
3804 }
3805
Laurence Lundblade37286c02022-09-03 10:05:02 -07003806 if(UsefulBuf_Compare(StringDate2, UsefulBuf_FromSZ("2085-04-12"))) {
3807 return 206;
Laurence Lundblade46d63e92021-05-13 11:37:10 -07003808 }
3809
3810 if(UsefulBuf_Compare(StringDays2, UsefulBuf_FromSZ("1985-04-12"))) {
3811 return 208;
3812 }
3813
Laurence Lundbladec7114722020-08-13 05:11:40 -07003814 return 0;
3815}
3816
3817
Laurence Lundblade9b334962020-08-27 10:55:53 -07003818// Input for one of the tagging tests
Laurence Lundbladecc7da412020-12-27 00:09:07 -08003819static const uint8_t spTagInput[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003820 0xd9, 0xd9, 0xf7, // CBOR magic number
Laurence Lundblade9b334962020-08-27 10:55:53 -07003821 0x81, // Array of one
3822 0xd8, 0x04, // non-preferred serialization of tag 4, decimal fraction
3823 0x82, // Array of two that is the faction 1/3
3824 0x01,
3825 0x03,
3826
3827 /*
3828 More than 4 tags on an item 225(226(227(228(229([])))))
3829 */
3830 0xd8, 0xe1,
3831 0xd8, 0xe2,
3832 0xd8, 0xe3,
3833 0xd8, 0xe4,
3834 0xd8, 0xe5,
3835 0x80,
3836
3837 /* tag 10489608748473423768(
3838 2442302356(
3839 21590(
3840 240(
3841 []))))
3842 */
3843 0xdb, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98,
3844 0xda, 0x91, 0x92, 0x93, 0x94,
3845 0xd9, 0x54, 0x56,
3846 0xd8, 0xf0,
3847 0x80,
3848
3849 /* tag 21590(
3850 10489608748473423768(
3851 2442302357(
3852 65534(
3853 []))))
3854 */
3855 0xdb, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x54, 0x56,
3856 0xdb, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98,
3857 0xda, 0x91, 0x92, 0x93, 0x95,
3858 0xd9, 0xff, 0xfe,
3859 0x80,
3860
3861 /* Make sure to blow past the limit of tags that must be mapped.
3862 works in conjuntion with entries above.
3863 269488144(269488145(269488146(269488147([]))))
3864 */
3865 0xda, 0x10, 0x10, 0x10, 0x10,
3866 0xda, 0x10, 0x10, 0x10, 0x11,
3867 0xda, 0x10, 0x10, 0x10, 0x12,
3868 0xda, 0x10, 0x10, 0x10, 0x13,
3869 0x80,
3870
3871 /* An invalid decimal fraction with an additional tag */
3872 0xd9, 0xff, 0xfa,
3873 0xd8, 0x02, // non-preferred serialization of tag 2, a big num
3874 0x00, // the integer 0; should be a byte string
3875};
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003876
Laurence Lundblade59289e52019-12-30 13:44:37 -08003877/*
3878 DB 9192939495969798 # tag(10489608748473423768)
Laurence Lundblade9b334962020-08-27 10:55:53 -07003879 80 # array(0)
Laurence Lundblade59289e52019-12-30 13:44:37 -08003880 */
Laurence Lundbladecc7da412020-12-27 00:09:07 -08003881static const uint8_t spEncodedLargeTag[] = {0xdb, 0x91, 0x92, 0x93, 0x94, 0x95,
Laurence Lundbladeee851742020-01-08 08:37:05 -08003882 0x96, 0x97, 0x98, 0x80};
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003883
Laurence Lundblade59289e52019-12-30 13:44:37 -08003884/*
3885DB 9192939495969798 # tag(10489608748473423768)
3886 D8 88 # tag(136)
3887 C6 # tag(6)
3888 C7 # tag(7)
3889 80 # array(0)
3890*/
Laurence Lundbladecc7da412020-12-27 00:09:07 -08003891static const uint8_t spLotsOfTags[] = {0xdb, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96,
Laurence Lundbladeee851742020-01-08 08:37:05 -08003892 0x97, 0x98, 0xd8, 0x88, 0xc6, 0xc7, 0x80};
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003893
3894/*
Laurence Lundblade9b334962020-08-27 10:55:53 -07003895 55799(55799(55799({
3896 6(7(-23)): 5859837686836516696(7({
3897 7(-20): 11({
3898 17(-18): 17(17(17("Organization"))),
3899 9(-17): 773("SSG"),
3900 -15: 16(17(6(7("Confusion")))),
3901 17(-16): 17("San Diego"),
3902 17(-14): 17("US")
3903 }),
3904 23(-19): 19({
3905 -11: 9({
3906 -9: -7
3907 }),
3908 90599561(90599561(90599561(-10))): 12(h'0102030405060708090A')
3909 })
3910 })),
3911 16(-22): 23({
3912 11(8(7(-5))): 8(-3)
3913 })
3914 })))
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003915 */
Laurence Lundbladecc7da412020-12-27 00:09:07 -08003916static const uint8_t spCSRWithTags[] = {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003917 0xd9, 0xd9, 0xf7, 0xd9, 0xd9, 0xf7, 0xd9, 0xd9, 0xf7, 0xa2,
3918 0xc6, 0xc7, 0x36,
3919 0xdb, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0xc7, 0xa2,
3920 0xda, 0x00, 0x00, 0x00, 0x07, 0x33,
3921 0xcb, 0xa5,
3922 0xd1, 0x31,
3923 0xd1, 0xd1, 0xd1, 0x6c,
3924 0x4f, 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74, 0x69, 0x6f, 0x6e,
3925 0xc9, 0x30,
3926 0xd9, 0x03, 0x05, 0x63,
3927 0x53, 0x53, 0x47,
3928 0x2e,
Laurence Lundblade9b334962020-08-27 10:55:53 -07003929 0xd0, 0xd1, 0xc6, 0xc7,
3930 0x69,
3931 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73, 0x69, 0x6f, 0x6e,
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003932 0xd1, 0x2f,
3933 0xd1, 0x69,
3934 0x53, 0x61, 0x6e, 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f,
3935 0xd1, 0x2d,
3936 0xd1, 0x62,
3937 0x55, 0x53,
3938 0xd7, 0x32,
3939 0xd3, 0xa2,
3940 0x2a,
3941 0xc9, 0xa1,
3942 0x28,
3943 0x26,
3944 0xda, 0x05, 0x66, 0x70, 0x89, 0xda, 0x05, 0x66, 0x70, 0x89, 0xda, 0x05, 0x66, 0x70, 0x89, 0x29,
3945 0xcc, 0x4a,
3946 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,0x07, 0x08, 0x09, 0x0a,
3947 0xd0, 0x35,
3948 0xd7, 0xa1,
3949 0xcb, 0xc8, 0xc7, 0x24,
3950 0xc8, 0x22};
3951
Laurence Lundblade9b334962020-08-27 10:55:53 -07003952
Laurence Lundbladecc7da412020-12-27 00:09:07 -08003953static const uint8_t spSpiffyTagInput[] = {
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08003954 0x85, // Open array
Laurence Lundblade9b334962020-08-27 10:55:53 -07003955
3956 0xc0, // tag for string date
3957 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string
3958
3959 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string
3960
3961 0x4a, '1','9','8','5','-','0','4','-','1','2', // Date string in byte string
3962
3963 0xd8, 0x23, // tag for regex
3964 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string
3965
3966 0xc0, // tag for string date
3967 0x4a, '1','9','8','5','-','0','4','-','1','2', // Date string in byte string
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07003968
3969 // This last case makes the array untraversable because it is
3970 // an uncrecoverable error. Make sure it stays last and is the only
3971 // instance so the other tests can work.
Laurence Lundblade9b334962020-08-27 10:55:53 -07003972};
3973
3974
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003975static int32_t CheckCSRMaps(QCBORDecodeContext *pDC);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003976
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003977
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03003978int32_t OptTagParseTest(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003979{
3980 QCBORDecodeContext DCtx;
Laurence Lundblade9b334962020-08-27 10:55:53 -07003981 QCBORItem Item;
3982 QCBORError uError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003983
Laurence Lundbladeee851742020-01-08 08:37:05 -08003984 QCBORDecode_Init(&DCtx,
Laurence Lundblade9b334962020-08-27 10:55:53 -07003985 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTagInput),
Laurence Lundbladeee851742020-01-08 08:37:05 -08003986 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003987
Laurence Lundblade9b334962020-08-27 10:55:53 -07003988 /*
3989 This test matches the magic number tag and the fraction tag
3990 55799([...])
3991 */
3992 uError = QCBORDecode_GetNext(&DCtx, &Item);
3993 if(uError != QCBOR_SUCCESS) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003994 return -2;
3995 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003996 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003997 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC)) {
3998 return -3;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003999 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004000
Laurence Lundblade9b334962020-08-27 10:55:53 -07004001 /*
4002 4([1,3])
4003 */
4004 uError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundbladedd6e76e2021-03-10 01:54:01 -07004005#ifdef QCBOR_DISABLE_EXP_AND_MANTISSA
Laurence Lundblade9b334962020-08-27 10:55:53 -07004006 if(uError != QCBOR_SUCCESS ||
4007 Item.uDataType != QCBOR_TYPE_ARRAY ||
4008 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_DECIMAL_FRACTION) ||
4009 QCBORDecode_GetNthTag(&DCtx, &Item, 0) != CBOR_TAG_DECIMAL_FRACTION ||
4010 QCBORDecode_GetNthTag(&DCtx, &Item, 1) != CBOR_TAG_INVALID64 ||
4011 QCBORDecode_GetNthTag(&DCtx, &Item, 2) != CBOR_TAG_INVALID64 ||
4012 QCBORDecode_GetNthTag(&DCtx, &Item, 3) != CBOR_TAG_INVALID64 ||
4013 QCBORDecode_GetNthTag(&DCtx, &Item, 4) != CBOR_TAG_INVALID64 ||
4014 Item.val.uCount != 2) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004015 return -4;
4016 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07004017 // consume the items in the array
4018 uError = QCBORDecode_GetNext(&DCtx, &Item);
4019 uError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundblade59289e52019-12-30 13:44:37 -08004020
Laurence Lundbladedd6e76e2021-03-10 01:54:01 -07004021#else /* QCBOR_DISABLE_EXP_AND_MANTISSA */
Laurence Lundblade9b334962020-08-27 10:55:53 -07004022 if(uError != QCBOR_SUCCESS ||
4023 Item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION ||
4024 QCBORDecode_GetNthTag(&DCtx, &Item, 0) != CBOR_TAG_INVALID64 ||
4025 QCBORDecode_GetNthTag(&DCtx, &Item, 1) != CBOR_TAG_INVALID64 ||
4026 QCBORDecode_GetNthTag(&DCtx, &Item, 2) != CBOR_TAG_INVALID64 ||
4027 QCBORDecode_GetNthTag(&DCtx, &Item, 3) != CBOR_TAG_INVALID64 ||
4028 QCBORDecode_GetNthTag(&DCtx, &Item, 4) != CBOR_TAG_INVALID64 ) {
4029 return -5;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004030 }
Laurence Lundbladedd6e76e2021-03-10 01:54:01 -07004031#endif /* QCBOR_DISABLE_EXP_AND_MANTISSA */
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004032
Laurence Lundblade9b334962020-08-27 10:55:53 -07004033 /*
4034 More than 4 tags on an item 225(226(227(228(229([])))))
4035 */
4036 uError = QCBORDecode_GetNext(&DCtx, &Item);
4037 if(uError != QCBOR_ERR_TOO_MANY_TAGS) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004038 return -6;
4039 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07004040
Laurence Lundblade88e9db22020-11-02 03:56:33 -08004041 if(QCBORDecode_GetNthTag(&DCtx, &Item, 0) != CBOR_TAG_INVALID64) {
4042 return -106;
4043 }
4044
4045
Laurence Lundblade9b334962020-08-27 10:55:53 -07004046 /* tag 10489608748473423768(
4047 2442302356(
4048 21590(
4049 240(
4050 []))))
4051 */
4052 uError = QCBORDecode_GetNext(&DCtx, &Item);
4053 if(uError != QCBOR_SUCCESS ||
4054 Item.uDataType != QCBOR_TYPE_ARRAY ||
4055 QCBORDecode_GetNthTag(&DCtx, &Item, 3) != 10489608748473423768ULL ||
4056 QCBORDecode_GetNthTag(&DCtx, &Item, 2) != 2442302356ULL ||
4057 QCBORDecode_GetNthTag(&DCtx, &Item, 1) != 21590ULL ||
4058 QCBORDecode_GetNthTag(&DCtx, &Item, 0) != 240ULL) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004059 return -7;
Laurence Lundblade9b334962020-08-27 10:55:53 -07004060 }
4061
4062 /* tag 21590(
4063 10489608748473423768(
4064 2442302357(
4065 21591(
4066 []))))
4067 */
4068 uError = QCBORDecode_GetNext(&DCtx, &Item);
4069 if(uError != QCBOR_SUCCESS ||
4070 Item.uDataType != QCBOR_TYPE_ARRAY ||
4071 QCBORDecode_GetNthTag(&DCtx, &Item, 0) != 65534ULL ||
4072 QCBORDecode_GetNthTag(&DCtx, &Item, 1) != 2442302357ULL ||
4073 QCBORDecode_GetNthTag(&DCtx, &Item, 2) != 10489608748473423768ULL ||
4074 QCBORDecode_GetNthTag(&DCtx, &Item, 3) != 21590ULL) {
4075 return -8;
4076 }
4077
4078 /* Make sure to blow past the limit of tags that must be mapped.
4079 works in conjuntion with entries above.
4080 269488144(269488145(269488146(269488147([]))))
4081 */
4082 uError = QCBORDecode_GetNext(&DCtx, &Item);
4083 if(uError != QCBOR_ERR_TOO_MANY_TAGS) {
4084 return -9;
4085 }
4086
4087 uError = QCBORDecode_GetNext(&DCtx, &Item);
4088 if(uError == QCBOR_SUCCESS) {
4089 return -10;
4090 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004091
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004092 // ----------------------------------
Laurence Lundbladeee851742020-01-08 08:37:05 -08004093 // This test sets up a caller-config list that includes the very large
Laurence Lundblade9b334962020-08-27 10:55:53 -07004094 // tage and then matches it. Caller-config lists are no longer
4095 // used or needed. This tests backwards compatibility with them.
Laurence Lundbladeee851742020-01-08 08:37:05 -08004096 QCBORDecode_Init(&DCtx,
4097 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEncodedLargeTag),
4098 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004099 const uint64_t puList[] = {0x9192939495969798, 257};
4100 const QCBORTagListIn TL = {2, puList};
4101 QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004102
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004103 if(QCBORDecode_GetNext(&DCtx, &Item)) {
4104 return -8;
4105 }
4106 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
4107 !QCBORDecode_IsTagged(&DCtx, &Item, 0x9192939495969798) ||
4108 QCBORDecode_IsTagged(&DCtx, &Item, 257) ||
4109 QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_BIGFLOAT) ||
4110 Item.val.uCount != 0) {
4111 return -9;
4112 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004113
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004114 //------------------------
Laurence Lundbladeee851742020-01-08 08:37:05 -08004115 // Sets up a caller-configured list and look up something not in it
Laurence Lundblade9b334962020-08-27 10:55:53 -07004116 // Another backwards compatibility test.
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004117 const uint64_t puLongList[17] = {1,2,1};
4118 const QCBORTagListIn TLLong = {17, puLongList};
Laurence Lundbladeee851742020-01-08 08:37:05 -08004119 QCBORDecode_Init(&DCtx,
4120 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEncodedLargeTag),
4121 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004122 QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TLLong);
4123 if(QCBORDecode_GetNext(&DCtx, &Item)) {
4124 return -11;
4125 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004126
Laurence Lundbladef7a70bc2020-10-24 12:23:25 -07004127 uint64_t puTags[4];
Laurence Lundblade9b334962020-08-27 10:55:53 -07004128 QCBORTagListOut Out = {0, 4, puTags};
4129
4130
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004131 // This tests retrievel of the full tag list
Laurence Lundbladeee851742020-01-08 08:37:05 -08004132 QCBORDecode_Init(&DCtx,
4133 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spLotsOfTags),
4134 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004135 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
4136 return -12;
4137 }
4138 if(puTags[0] != 0x9192939495969798 ||
4139 puTags[1] != 0x88 ||
Laurence Lundblade59289e52019-12-30 13:44:37 -08004140 puTags[2] != 0x06 ||
4141 puTags[3] != 0x07) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004142 return -13;
4143 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004144
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004145 // ----------------------
Laurence Lundblade9b334962020-08-27 10:55:53 -07004146 // This tests too small of an out list
Laurence Lundbladeee851742020-01-08 08:37:05 -08004147 QCBORDecode_Init(&DCtx,
4148 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spLotsOfTags),
4149 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004150 QCBORTagListOut OutSmall = {0, 3, puTags};
4151 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &OutSmall) != QCBOR_ERR_TOO_MANY_TAGS) {
4152 return -14;
4153 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004154
Laurence Lundblade9b334962020-08-27 10:55:53 -07004155
4156
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004157 // ---------------
Laurence Lundblade9b334962020-08-27 10:55:53 -07004158 // Decode a version of the "CSR" that has had a ton of tags randomly inserted
4159 // It is a bit of a messy test and maybe could be improved, but
4160 // it is retained as a backwards compatibility check.
Laurence Lundbladeee851742020-01-08 08:37:05 -08004161 QCBORDecode_Init(&DCtx,
4162 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRWithTags),
4163 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004164 int n = CheckCSRMaps(&DCtx);
4165 if(n) {
4166 return n-2000;
4167 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004168
Laurence Lundblade59289e52019-12-30 13:44:37 -08004169 Out = (QCBORTagListOut){0, 16, puTags};
Laurence Lundbladeee851742020-01-08 08:37:05 -08004170 QCBORDecode_Init(&DCtx,
4171 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRWithTags),
4172 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004173
Laurence Lundblade9b334962020-08-27 10:55:53 -07004174 /* With the spiffy decode revision, this tag list is not used.
4175 It doesn't matter if a tag is in this list or not so some
4176 tests that couldn't process a tag because it isn't in this list
4177 now can process these unlisted tags. The tests have been
4178 adjusted for this. */
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004179 const uint64_t puTagList[] = {773, 1, 90599561};
4180 const QCBORTagListIn TagList = {3, puTagList};
4181 QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TagList);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004182
4183
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004184 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
4185 return -100;
4186 }
4187 if(Item.uDataType != QCBOR_TYPE_MAP ||
4188 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC) ||
4189 QCBORDecode_IsTagged(&DCtx, &Item, 90599561) ||
4190 QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_DATE_EPOCH) ||
4191 Item.val.uCount != 2 ||
4192 puTags[0] != CBOR_TAG_CBOR_MAGIC ||
4193 puTags[1] != CBOR_TAG_CBOR_MAGIC ||
4194 puTags[2] != CBOR_TAG_CBOR_MAGIC ||
4195 Out.uNumUsed != 3) {
4196 return -101;
4197 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004198
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004199 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
4200 return -102;
4201 }
4202 if(Item.uDataType != QCBOR_TYPE_MAP ||
4203 QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC) ||
4204 QCBORDecode_IsTagged(&DCtx, &Item, 6) ||
Laurence Lundbladef7a70bc2020-10-24 12:23:25 -07004205 !QCBORDecode_IsTagged(&DCtx, &Item, 7) ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004206 Item.val.uCount != 2 ||
4207 puTags[0] != 5859837686836516696 ||
4208 puTags[1] != 7 ||
4209 Out.uNumUsed != 2) {
4210 return -103;
4211 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004212
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004213 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
4214 return -104;
4215 }
4216 if(Item.uDataType != QCBOR_TYPE_MAP ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004217 Item.val.uCount != 5 ||
4218 puTags[0] != 0x0b ||
4219 Out.uNumUsed != 1) {
4220 return -105;
4221 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004222
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004223 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
4224 return -106;
4225 }
4226 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
4227 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_COSE_MAC0) ||
4228 Item.val.string.len != 12 ||
4229 puTags[0] != CBOR_TAG_COSE_MAC0 ||
4230 puTags[1] != CBOR_TAG_COSE_MAC0 ||
4231 puTags[2] != CBOR_TAG_COSE_MAC0 ||
4232 Out.uNumUsed != 3) {
4233 return -105;
4234 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004235
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004236 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
4237 return -107;
4238 }
4239 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
4240 !QCBORDecode_IsTagged(&DCtx, &Item, 773) ||
4241 Item.val.string.len != 3 ||
4242 puTags[0] != 773 ||
4243 Out.uNumUsed != 1) {
4244 return -108;
4245 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004246
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004247 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
4248 return -109;
4249 }
4250 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade59289e52019-12-30 13:44:37 -08004251 !QCBORDecode_IsTagged(&DCtx, &Item, 16) ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004252 Item.val.string.len != 9 ||
Laurence Lundblade59289e52019-12-30 13:44:37 -08004253 puTags[0] != 16 ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07004254 puTags[3] != 7 ||
4255 Out.uNumUsed != 4) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004256 return -110;
4257 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004258
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004259 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
4260 return -111;
4261 }
4262 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
4263 !QCBORDecode_IsTagged(&DCtx, &Item, 17) ||
4264 Item.val.string.len != 9 ||
4265 puTags[0] != 17 ||
4266 Out.uNumUsed != 1) {
4267 return -112;
4268 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004269
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004270 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
4271 return -111;
4272 }
4273 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
4274 !QCBORDecode_IsTagged(&DCtx, &Item, 17) ||
4275 Item.val.string.len != 2 ||
4276 puTags[0] != 17 ||
4277 Out.uNumUsed != 1) {
4278 return -112;
4279 }
4280
4281 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
4282 return -113;
4283 }
4284 if(Item.uDataType != QCBOR_TYPE_MAP ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07004285 !QCBORDecode_IsTagged(&DCtx, &Item, 19) ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004286 Item.val.uCount != 2 ||
4287 puTags[0] != 19 ||
4288 Out.uNumUsed != 1) {
4289 return -114;
4290 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004291
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004292 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
4293 return -115;
4294 }
4295 if(Item.uDataType != QCBOR_TYPE_MAP ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07004296 !QCBORDecode_IsTagged(&DCtx, &Item, 9) ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004297 Item.val.uCount != 1 ||
4298 puTags[0] != 9 ||
4299 Out.uNumUsed != 1) {
4300 return -116;
4301 }
4302
4303 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
4304 return -116;
4305 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004306 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004307 Item.val.int64 != -7 ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004308 Out.uNumUsed != 0) {
4309 return -117;
4310 }
4311
4312 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
4313 return -118;
4314 }
4315 if(Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
4316 Item.val.string.len != 10 ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004317 puTags[0] != 12 ||
4318 Out.uNumUsed != 1) {
4319 return -119;
4320 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004321
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004322 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
4323 return -120;
4324 }
4325 if(Item.uDataType != QCBOR_TYPE_MAP ||
4326 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_ENC_AS_B16) ||
4327 Item.val.uCount != 1 ||
4328 puTags[0] != 0x17 ||
4329 Out.uNumUsed != 1) {
4330 return -121;
4331 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004332
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004333 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
4334 return -122;
4335 }
4336 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07004337 !QCBORDecode_IsTagged(&DCtx, &Item, 8) ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004338 Item.val.int64 != -3 ||
4339 puTags[0] != 8 ||
4340 Out.uNumUsed != 1) {
4341 return -123;
4342 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004343
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07004344 if(QCBORDecode_Finish(&DCtx)) {
4345 return -124;
4346 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07004347
4348 UsefulBufC DateString;
4349 QCBORDecode_Init(&DCtx,
4350 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSpiffyTagInput),
4351 QCBOR_DECODE_MODE_NORMAL);
4352
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07004353 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade9b334962020-08-27 10:55:53 -07004354 // tagged date string
4355 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString);
4356 // untagged date string
4357 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString);
4358 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_SUCCESS) {
4359 return 100;
4360 }
4361 // untagged byte string
4362 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString);
4363 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
4364 return 101;
4365 }
4366 // tagged regex
4367 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString);
4368 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
4369 return 102;
4370 }
4371 // tagged date string with a byte string
4372 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString);
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07004373 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT) {
Laurence Lundblade9b334962020-08-27 10:55:53 -07004374 return 103;
4375 }
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07004376 // The exit errors out because the last item, the date string with
4377 // bad content makes the array untraversable (the bad date string
4378 // could have tag content of an array or such that is not consumed
4379 // by the date decoding).
Laurence Lundblade9b334962020-08-27 10:55:53 -07004380 QCBORDecode_ExitArray(&DCtx);
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07004381 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT) {
Laurence Lundblade9b334962020-08-27 10:55:53 -07004382 return 104;
4383 }
4384
4385
4386 QCBORDecode_Init(&DCtx,
4387 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSpiffyTagInput),
4388 QCBOR_DECODE_MODE_NORMAL);
4389
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07004390 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade9b334962020-08-27 10:55:53 -07004391 // tagged date string
4392 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString);
4393 // untagged date string
4394 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString);
4395 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_SUCCESS) {
4396 return 200;
4397 }
4398 // untagged byte string
4399 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString);
4400 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
4401 return 201;
4402 }
4403 // tagged regex
4404 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString);
4405 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
4406 return 202;
4407 }
4408 // tagged date string with a byte string
4409 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString);
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07004410 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT) {
Laurence Lundblade9b334962020-08-27 10:55:53 -07004411 return 203;
4412 }
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07004413 // See comments above
Laurence Lundblade9b334962020-08-27 10:55:53 -07004414 QCBORDecode_ExitArray(&DCtx);
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07004415 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT) {
Laurence Lundblade9b334962020-08-27 10:55:53 -07004416 return 204;
4417 }
4418
4419 QCBORDecode_Init(&DCtx,
4420 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSpiffyTagInput),
4421 QCBOR_DECODE_MODE_NORMAL);
4422
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07004423 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade9b334962020-08-27 10:55:53 -07004424 // tagged date string
4425 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString);
4426 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
4427 return 300;
4428 }
4429 // untagged date string
4430 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString);
4431 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
4432 return 301;
4433 }
4434 // untagged byte string
4435 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString);
4436 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
4437 return 302;
4438 }
4439 // tagged regex
4440 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString);
4441 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
4442 return 303;
4443 }
4444 // tagged date string with a byte string
4445 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString);
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07004446 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT) {
Laurence Lundblade9b334962020-08-27 10:55:53 -07004447 return 304;
4448 }
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07004449 // See comments above
Laurence Lundblade9b334962020-08-27 10:55:53 -07004450 QCBORDecode_ExitArray(&DCtx);
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07004451 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT) {
Laurence Lundblade9b334962020-08-27 10:55:53 -07004452 return 305;
4453 }
4454
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004455 return 0;
4456}
4457
Laurence Lundblade37286c02022-09-03 10:05:02 -07004458/*
4459 * These are showing the big numbers converted to integers.
4460 * The tag numbers are not shown.
4461 *
4462 * [ 18446744073709551616,
4463 * -18446744073709551617,
4464 * {"BN+": 18446744073709551616,
4465 * 64: 18446744073709551616,
4466 * "BN-": -18446744073709551617,
4467 * -64: -18446744073709551617
4468 * }
4469 * ]
4470 */
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004471
Laurence Lundbladecc7da412020-12-27 00:09:07 -08004472static const uint8_t spBigNumInput[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004473 0x83,
4474 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
4475 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
4476 0xA4,
4477 0x63, 0x42, 0x4E, 0x2B,
4478 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
4479 0x18, 0x40,
4480 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
4481 0x63, 0x42, 0x4E, 0x2D,
4482 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
4483 0x38, 0x3F,
4484 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
4485
Laurence Lundblade37286c02022-09-03 10:05:02 -07004486#ifndef QCBOR_DISABLE_TAGS
Laurence Lundbladecc7da412020-12-27 00:09:07 -08004487/* The expected big num */
4488static const uint8_t spBigNum[] = {
4489 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
4490 0x00};
Laurence Lundblade37286c02022-09-03 10:05:02 -07004491#endif /* QCBOR_DISABLE_TAGS */
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004492
4493
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03004494int32_t BignumParseTest(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004495{
4496 QCBORDecodeContext DCtx;
4497 QCBORItem Item;
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07004498 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004499
Laurence Lundbladeee851742020-01-08 08:37:05 -08004500 QCBORDecode_Init(&DCtx,
4501 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNumInput),
4502 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004503
4504
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004505 //
4506 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
4507 return -1;
4508 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade830fbf92020-05-31 17:22:33 -07004509 return -2;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004510 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004511
Laurence Lundblade37286c02022-09-03 10:05:02 -07004512#ifndef QCBOR_DISABLE_TAGS
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004513 //
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004514 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07004515 return -3;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004516 if(Item.uDataType != QCBOR_TYPE_POSBIGNUM ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07004517 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07004518 return -4;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004519 }
4520
4521 //
4522 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07004523 return -5;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004524 if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07004525 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07004526 return -6;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004527 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004528
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004529 //
4530 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07004531 return -7;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004532 if(Item.uDataType != QCBOR_TYPE_MAP) {
Laurence Lundblade830fbf92020-05-31 17:22:33 -07004533 return -8;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004534 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004535
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004536 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07004537 return -9;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004538 if(Item.uDataType != QCBOR_TYPE_POSBIGNUM ||
4539 Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07004540 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07004541 return -10;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004542 }
4543
4544 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07004545 return -11;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004546 if(Item.uDataType != QCBOR_TYPE_POSBIGNUM ||
4547 Item.uLabelType != QCBOR_TYPE_INT64 ||
4548 Item.label.int64 != 64 ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07004549 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07004550 return -12;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004551 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004552
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004553 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07004554 return -13;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004555 if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM ||
4556 Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07004557 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07004558 return -14;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004559 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004560
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004561 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07004562 return -15;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004563 if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM ||
4564 Item.uLabelType != QCBOR_TYPE_INT64 ||
4565 Item.label.int64 != -64 ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07004566 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07004567 return -16;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004568 }
Laurence Lundblade37286c02022-09-03 10:05:02 -07004569#else
4570
4571 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_TAGS_DISABLED) {
4572 return -100;
4573 }
4574#endif /* QCBOR_DISABLE_TAGS */
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004575
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004576 return 0;
4577}
4578
4579
4580
Laurence Lundbladec5fef682020-01-25 11:38:45 -08004581static int32_t CheckItemWithIntLabel(QCBORDecodeContext *pCtx,
Laurence Lundbladeee851742020-01-08 08:37:05 -08004582 uint8_t uDataType,
4583 uint8_t uNestingLevel,
4584 uint8_t uNextNest,
4585 int64_t nLabel,
4586 QCBORItem *pItem)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004587{
4588 QCBORItem Item;
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07004589 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004590
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004591 if((nCBORError = QCBORDecode_GetNext(pCtx, &Item))) return -1;
4592 if(Item.uDataType != uDataType) return -1;
4593 if(uNestingLevel > 0) {
Laurence Lundbladeee851742020-01-08 08:37:05 -08004594 if(Item.uLabelType != QCBOR_TYPE_INT64 &&
4595 Item.uLabelType != QCBOR_TYPE_UINT64) {
4596 return -1;
4597 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004598 if(Item.uLabelType == QCBOR_TYPE_INT64) {
4599 if(Item.label.int64 != nLabel) return -1;
4600 } else {
Laurence Lundblade570fab52018-10-13 18:28:27 +08004601 if(Item.label.uint64 != (uint64_t)nLabel) return -1;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004602 }
4603 }
4604 if(Item.uNestingLevel != uNestingLevel) return -1;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304605 if(Item.uNextNestLevel != uNextNest) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004606
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004607 if(pItem) {
4608 *pItem = Item;
4609 }
4610 return 0;
4611}
4612
4613
Laurence Lundblade742df4a2018-10-13 20:07:17 +08004614// Same code checks definite and indefinite length versions of the map
Laurence Lundbladec5fef682020-01-25 11:38:45 -08004615static int32_t CheckCSRMaps(QCBORDecodeContext *pDC)
Laurence Lundblade742df4a2018-10-13 20:07:17 +08004616{
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304617 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 0, 1, 0, NULL)) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004618
Laurence Lundblade9b334962020-08-27 10:55:53 -07004619 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 1, 2, -23, NULL)) return -2;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004620
Laurence Lundblade9b334962020-08-27 10:55:53 -07004621 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 2, 3, -20, NULL)) return -3;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004622
Laurence Lundblade9b334962020-08-27 10:55:53 -07004623 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -18, NULL)) return -4;
4624 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -17, NULL)) return -5;
4625 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -15, NULL)) return -6;
4626 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -16, NULL)) return -7;
4627 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 2, -14, NULL)) return -8;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004628
Laurence Lundblade9b334962020-08-27 10:55:53 -07004629 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 2, 3, -19, NULL)) return -9;
4630 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 3, 4, -11, NULL)) return -10;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004631
Laurence Lundblade9b334962020-08-27 10:55:53 -07004632 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_INT64, 4, 3, -9, NULL)) return -11;
4633 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_BYTE_STRING, 3, 1, -10, NULL)) return -12;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004634
Laurence Lundblade9b334962020-08-27 10:55:53 -07004635 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 1, 2, -22, NULL)) return -13;
4636 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_INT64, 2, 0, -5, NULL)) return -14;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004637
Laurence Lundblade9b334962020-08-27 10:55:53 -07004638 if(QCBORDecode_Finish(pDC)) return -20;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004639
Laurence Lundblade742df4a2018-10-13 20:07:17 +08004640 return 0;
4641}
4642
4643
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004644/*
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004645{
4646 -23: {
4647 -20: {
4648 -18: "Organization",
4649 -17: "SSG",
4650 -15: "Confusion",
4651 -16: "San Diego",
4652 -14: "US"
4653 },
4654 -19: {
4655 -11: {
4656 -9: -7
4657 },
4658 -10: '\u0001\u0002\u0003\u0004\u0005\u0006\a\b\t\n'
4659 }
4660 },
4661 -22: {
4662 -5: -3
4663 }
4664}
Laurence Lundbladecc7da412020-12-27 00:09:07 -08004665*/
4666static const uint8_t spCSRInput[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004667 0xa2, 0x36, 0xa2, 0x33, 0xa5, 0x31, 0x6c, 0x4f,
4668 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74,
4669 0x69, 0x6f, 0x6e, 0x30, 0x63, 0x53, 0x53, 0x47,
4670 0x2e, 0x69, 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73,
4671 0x69, 0x6f, 0x6e, 0x2f, 0x69, 0x53, 0x61, 0x6e,
4672 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f, 0x2d, 0x62,
4673 0x55, 0x53, 0x32, 0xa2, 0x2a, 0xa1, 0x28, 0x26,
4674 0x29, 0x4a, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
4675 0x07, 0x08, 0x09, 0x0a, 0x35, 0xa1, 0x24, 0x22};
4676
Laurence Lundbladecc7da412020-12-27 00:09:07 -08004677// Same map as above, but using indefinite lengths
4678static const uint8_t spCSRInputIndefLen[] = {
4679 0xbf, 0x36, 0xbf, 0x33, 0xbf, 0x31, 0x6c, 0x4f,
4680 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74,
4681 0x69, 0x6f, 0x6e, 0x30, 0x63, 0x53, 0x53, 0x47,
4682 0x2e, 0x69, 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73,
4683 0x69, 0x6f, 0x6e, 0x2f, 0x69, 0x53, 0x61, 0x6e,
4684 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f, 0x2d, 0x62,
4685 0x55, 0x53, 0xff, 0x32, 0xbf, 0x2a, 0xbf, 0x28,
4686 0x26, 0xff, 0x29, 0x4a, 0x01, 0x02, 0x03, 0x04,
4687 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0xff, 0xff,
4688 0x35, 0xbf, 0x24, 0x22, 0xff, 0xff};
4689
4690
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03004691int32_t NestedMapTest(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004692{
4693 QCBORDecodeContext DCtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004694
Laurence Lundbladeee851742020-01-08 08:37:05 -08004695 QCBORDecode_Init(&DCtx,
4696 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput),
4697 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004698
Laurence Lundblade742df4a2018-10-13 20:07:17 +08004699 return CheckCSRMaps(&DCtx);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004700}
4701
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08004702
4703
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03004704int32_t StringDecoderModeFailTest(void)
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08004705{
4706 QCBORDecodeContext DCtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004707
Laurence Lundbladeee851742020-01-08 08:37:05 -08004708 QCBORDecode_Init(&DCtx,
4709 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput),
4710 QCBOR_DECODE_MODE_MAP_STRINGS_ONLY);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004711
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08004712 QCBORItem Item;
4713 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004714
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08004715 if(QCBORDecode_GetNext(&DCtx, &Item)) {
4716 return -1;
4717 }
4718 if(Item.uDataType != QCBOR_TYPE_MAP) {
4719 return -2;
4720 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004721
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08004722 nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
4723 if(nCBORError != QCBOR_ERR_MAP_LABEL_TYPE) {
4724 return -3;
4725 }
4726
4727 return 0;
4728}
4729
4730
Laurence Lundblade742df4a2018-10-13 20:07:17 +08004731
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03004732int32_t NestedMapTestIndefLen(void)
Laurence Lundblade742df4a2018-10-13 20:07:17 +08004733{
4734 QCBORDecodeContext DCtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004735
Laurence Lundbladeee851742020-01-08 08:37:05 -08004736 QCBORDecode_Init(&DCtx,
4737 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInputIndefLen),
4738 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004739
Laurence Lundblade742df4a2018-10-13 20:07:17 +08004740 return CheckCSRMaps(&DCtx);
4741}
4742
4743
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004744
Laurence Lundblade17ede402018-10-13 11:43:07 +08004745static UsefulBufC make_nested_indefinite_arrays(int n, UsefulBuf Storage)
4746{
4747 UsefulOutBuf UOB;
4748 UsefulOutBuf_Init(&UOB, Storage);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004749
Laurence Lundblade17ede402018-10-13 11:43:07 +08004750 int i;
4751 for(i = 0; i < n; i++) {
4752 UsefulOutBuf_AppendByte(&UOB, 0x9f);
4753 }
4754
4755 for(i = 0; i < n; i++) {
4756 UsefulOutBuf_AppendByte(&UOB, 0xff);
4757 }
4758 return UsefulOutBuf_OutUBuf(&UOB);
4759}
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08004760
4761
Laurence Lundbladec5fef682020-01-25 11:38:45 -08004762static int32_t parse_indeflen_nested(UsefulBufC Nested, int nNestLevel)
Laurence Lundblade17ede402018-10-13 11:43:07 +08004763{
4764 QCBORDecodeContext DC;
4765 QCBORDecode_Init(&DC, Nested, 0);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004766
Laurence Lundblade17ede402018-10-13 11:43:07 +08004767 int j;
4768 for(j = 0; j < nNestLevel; j++) {
4769 QCBORItem Item;
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07004770 QCBORError nReturn = QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade17ede402018-10-13 11:43:07 +08004771 if(j >= QCBOR_MAX_ARRAY_NESTING) {
4772 // Should be in error
Laurence Lundbladea9489f82020-09-12 13:50:56 -07004773 if(nReturn != QCBOR_ERR_ARRAY_DECODE_NESTING_TOO_DEEP) {
Laurence Lundblade17ede402018-10-13 11:43:07 +08004774 return -4;
4775 } else {
4776 return 0; // Decoding doesn't recover after an error
4777 }
4778 } else {
4779 // Should be no error
4780 if(nReturn) {
4781 return -9; // Should not have got an error
4782 }
4783 }
4784 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
4785 return -7;
4786 }
4787 }
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07004788 QCBORError nReturn = QCBORDecode_Finish(&DC);
Laurence Lundblade17ede402018-10-13 11:43:07 +08004789 if(nReturn) {
4790 return -3;
4791 }
4792 return 0;
4793}
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08004794
4795
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03004796int32_t IndefiniteLengthNestTest(void)
Laurence Lundblade17ede402018-10-13 11:43:07 +08004797{
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05304798 UsefulBuf_MAKE_STACK_UB(Storage, 50);
Laurence Lundblade17ede402018-10-13 11:43:07 +08004799 int i;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004800 for(i=1; i < QCBOR_MAX_ARRAY_NESTING+4; i++) {
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08004801 const UsefulBufC Nested = make_nested_indefinite_arrays(i, Storage);
Laurence Lundblade17ede402018-10-13 11:43:07 +08004802 int nReturn = parse_indeflen_nested(Nested, i);
4803 if(nReturn) {
4804 return nReturn;
4805 }
4806 }
4807 return 0;
4808}
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08004809
Laurence Lundbladeee851742020-01-08 08:37:05 -08004810// [1, [2, 3]]
4811static const uint8_t spIndefiniteArray[] = {0x9f, 0x01, 0x82, 0x02, 0x03, 0xff};
4812// No closing break
4813static const uint8_t spIndefiniteArrayBad1[] = {0x9f};
4814// Not enough closing breaks
4815static const uint8_t spIndefiniteArrayBad2[] = {0x9f, 0x9f, 0x02, 0xff};
4816// Too many closing breaks
4817static const uint8_t spIndefiniteArrayBad3[] = {0x9f, 0x02, 0xff, 0xff};
4818// Unclosed indeflen inside def len
4819static const uint8_t spIndefiniteArrayBad4[] = {0x81, 0x9f};
4820// confused tag
4821static const uint8_t spIndefiniteArrayBad5[] = {0x9f, 0xd1, 0xff};
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08004822
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03004823int32_t IndefiniteLengthArrayMapTest(void)
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08004824{
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07004825 QCBORError nResult;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08004826 // --- first test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07004827 UsefulBufC IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArray);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004828
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08004829 // Decode it and see if it is OK
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08004830 QCBORDecodeContext DC;
4831 QCBORItem Item;
4832 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004833
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08004834 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05304835
4836 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
4837 Item.uNestingLevel != 0 ||
4838 Item.uNextNestLevel != 1) {
4839 return -111;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08004840 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004841
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08004842 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05304843 if(Item.uDataType != QCBOR_TYPE_INT64 ||
4844 Item.uNestingLevel != 1 ||
4845 Item.uNextNestLevel != 1) {
4846 return -2;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08004847 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004848
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08004849 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05304850 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
4851 Item.uNestingLevel != 1 ||
4852 Item.uNextNestLevel != 2) {
4853 return -3;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08004854 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004855
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08004856 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade12b495d2018-12-17 11:15:54 -08004857 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade6de37062018-10-15 12:22:42 +05304858 Item.uNestingLevel != 2 ||
4859 Item.uNextNestLevel != 2) {
4860 return -4;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08004861 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004862
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08004863 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade12b495d2018-12-17 11:15:54 -08004864 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade6de37062018-10-15 12:22:42 +05304865 Item.uNestingLevel != 2 ||
4866 Item.uNextNestLevel != 0) {
4867 return -5;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08004868 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004869
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08004870 if(QCBORDecode_Finish(&DC)) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05304871 return -6;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08004872 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004873
Laurence Lundblade19e0c802018-10-13 12:19:55 +08004874 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07004875 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad1);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004876
Laurence Lundblade19e0c802018-10-13 12:19:55 +08004877 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004878
Laurence Lundblade19e0c802018-10-13 12:19:55 +08004879 nResult = QCBORDecode_GetNext(&DC, &Item);
4880 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05304881 return -7;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08004882 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004883
Laurence Lundblade570fab52018-10-13 18:28:27 +08004884 nResult = QCBORDecode_Finish(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07004885 if(nResult != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05304886 return -8;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08004887 }
4888
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004889
Laurence Lundblade19e0c802018-10-13 12:19:55 +08004890 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07004891 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad2);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004892
Laurence Lundblade19e0c802018-10-13 12:19:55 +08004893 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004894
Laurence Lundblade19e0c802018-10-13 12:19:55 +08004895 nResult = QCBORDecode_GetNext(&DC, &Item);
4896 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05304897 return -9;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08004898 }
4899
4900 nResult = QCBORDecode_GetNext(&DC, &Item);
4901 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05304902 return -10;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08004903 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004904
Laurence Lundblade19e0c802018-10-13 12:19:55 +08004905 nResult = QCBORDecode_GetNext(&DC, &Item);
4906 if(nResult || Item.uDataType != QCBOR_TYPE_INT64) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05304907 return -11;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08004908 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004909
Laurence Lundblade19e0c802018-10-13 12:19:55 +08004910 nResult = QCBORDecode_Finish(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07004911 if(nResult != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05304912 return -12;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08004913 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004914
4915
Laurence Lundblade19e0c802018-10-13 12:19:55 +08004916 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07004917 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad3);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004918
Laurence Lundblade19e0c802018-10-13 12:19:55 +08004919 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004920
Laurence Lundblade19e0c802018-10-13 12:19:55 +08004921 nResult = QCBORDecode_GetNext(&DC, &Item);
4922 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05304923 return -13;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08004924 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004925
Laurence Lundblade19e0c802018-10-13 12:19:55 +08004926 nResult = QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade642282a2020-06-23 12:00:33 -07004927 if(nResult != QCBOR_SUCCESS) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05304928 return -14;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08004929 }
Laurence Lundblade6de37062018-10-15 12:22:42 +05304930
Laurence Lundblade642282a2020-06-23 12:00:33 -07004931 nResult = QCBORDecode_GetNext(&DC, &Item);
4932 if(nResult != QCBOR_ERR_BAD_BREAK) {
4933 return -140;
4934 }
4935
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004936
Laurence Lundblade570fab52018-10-13 18:28:27 +08004937 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07004938 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad4);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004939
Laurence Lundblade570fab52018-10-13 18:28:27 +08004940 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004941
Laurence Lundblade570fab52018-10-13 18:28:27 +08004942 nResult = QCBORDecode_GetNext(&DC, &Item);
4943 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05304944 return -15;
Laurence Lundblade570fab52018-10-13 18:28:27 +08004945 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004946
Laurence Lundblade570fab52018-10-13 18:28:27 +08004947 nResult = QCBORDecode_GetNext(&DC, &Item);
4948 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05304949 return -16;
Laurence Lundblade570fab52018-10-13 18:28:27 +08004950 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004951
Laurence Lundblade570fab52018-10-13 18:28:27 +08004952 nResult = QCBORDecode_Finish(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07004953 if(nResult != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05304954 return -17;
Laurence Lundblade570fab52018-10-13 18:28:27 +08004955 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004956
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304957 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07004958 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad5);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004959
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304960 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004961
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304962 nResult = QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade37286c02022-09-03 10:05:02 -07004963
4964#ifndef QCBOR_DISABLE_TAGS
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304965 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05304966 return -18;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304967 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004968
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304969 nResult = QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05304970 if(nResult != QCBOR_ERR_BAD_BREAK) {
4971 return -19;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304972 }
Laurence Lundblade37286c02022-09-03 10:05:02 -07004973#else /* QCBOR_DISABLE_TAGS */
4974 if(nResult != QCBOR_ERR_TAGS_DISABLED) {
4975 return -20;
4976 }
4977#endif /* QCBOR_DISABLE_TAGS */
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004978
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08004979 return 0;
4980}
4981
Laurence Lundblade17ede402018-10-13 11:43:07 +08004982
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08004983#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
4984
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07004985static const uint8_t spIndefiniteLenString[] = {
Laurence Lundblade17ede402018-10-13 11:43:07 +08004986 0x81, // Array of length one
4987 0x7f, // text string marked with indefinite length
4988 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment
4989 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment
4990 0xff // ending break
4991};
4992
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07004993static const uint8_t spIndefiniteLenStringBad2[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304994 0x81, // Array of length one
4995 0x7f, // text string marked with indefinite length
4996 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment
4997 0x44, 0x6d, 0x69, 0x6e, 0x67, // second segment of wrong type
4998 0xff // ending break
4999};
5000
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07005001static const uint8_t spIndefiniteLenStringBad3[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305002 0x81, // Array of length one
5003 0x7f, // text string marked with indefinite length
5004 0x01, 0x02, // Not a string
5005 0xff // ending break
5006};
5007
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07005008static const uint8_t spIndefiniteLenStringBad4[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305009 0x81, // Array of length one
5010 0x7f, // text string marked with indefinite length
5011 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment
5012 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment
5013 // missing end of string
5014};
5015
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07005016static const uint8_t spIndefiniteLenStringLabel[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305017 0xa1, // Array of length one
5018 0x7f, // text string marked with indefinite length
5019 0x65, 0x73, 0x74, 0x72, 0x75, 0x75, // first segment
5020 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment
5021 0xff, // ending break
5022 0x01 // integer being labeled.
5023};
5024
Laurence Lundbladec5fef682020-01-25 11:38:45 -08005025/**
5026 Make an indefinite length string
5027
5028 @param Storage Storage for string, must be 144 bytes in size
5029 @return The indefinite length string
5030
5031 This makes an array with one indefinite length string that has 7 chunks
5032 from size of 1 byte up to 64 bytes.
5033 */
5034static UsefulBufC MakeIndefiniteBigBstr(UsefulBuf Storage)
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305035{
5036 UsefulOutBuf UOB;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005037
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305038 UsefulOutBuf_Init(&UOB, Storage);
5039 UsefulOutBuf_AppendByte(&UOB, 0x81);
5040 UsefulOutBuf_AppendByte(&UOB, 0x5f);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005041
Laurence Lundbladec5fef682020-01-25 11:38:45 -08005042 uint8_t uStringByte = 0;
5043 // Use of type int is intentional
5044 for(int uChunkSize = 1; uChunkSize <= 128; uChunkSize *= 2) {
5045 // Not using preferred encoding here, but that is OK.
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305046 UsefulOutBuf_AppendByte(&UOB, 0x58);
Laurence Lundbladec5fef682020-01-25 11:38:45 -08005047 UsefulOutBuf_AppendByte(&UOB, (uint8_t)uChunkSize);
5048 for(int j = 0; j < uChunkSize; j++) {
5049 UsefulOutBuf_AppendByte(&UOB, uStringByte);
5050 uStringByte++;
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305051 }
5052 }
5053 UsefulOutBuf_AppendByte(&UOB, 0xff);
5054
5055 return UsefulOutBuf_OutUBuf(&UOB);
5056}
5057
5058static int CheckBigString(UsefulBufC BigString)
5059{
5060 if(BigString.len != 255) {
5061 return 1;
5062 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005063
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305064 for(uint8_t i = 0; i < 255; i++){
5065 if(((const uint8_t *)BigString.ptr)[i] != i) {
5066 return 1;
5067 }
5068 }
5069 return 0;
5070}
5071
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05305072
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03005073int32_t IndefiniteLengthStringTest(void)
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05305074{
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305075 QCBORDecodeContext DC;
5076 QCBORItem Item;
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05305077 // big enough for MakeIndefiniteBigBstr() + MemPool overhead
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08005078 UsefulBuf_MAKE_STACK_UB(MemPool, 350);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005079
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305080 // --- Simple normal indefinite length string ------
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07005081 UsefulBufC IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenString);
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305082 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005083
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05305084 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305085 return -1;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05305086 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005087
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305088 if(QCBORDecode_GetNext(&DC, &Item)) {
5089 return -2;
5090 }
5091 if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.uDataAlloc) {
5092 return -3;
5093 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005094
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305095 if(QCBORDecode_GetNext(&DC, &Item)) {
5096 return -4;
5097 }
5098 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || !Item.uDataAlloc) {
5099 return -5;
5100 }
5101 if(QCBORDecode_Finish(&DC)) {
5102 return -6;
5103 }
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05305104
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305105 // ----- types mismatch ---
Laurence Lundbladeee851742020-01-08 08:37:05 -08005106 QCBORDecode_Init(&DC,
5107 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad2),
5108 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005109
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305110 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
5111 return -7;
5112 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005113
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305114 if(QCBORDecode_GetNext(&DC, &Item)) {
5115 return -8;
5116 }
5117 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
5118 return -9;
5119 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005120
Laurence Lundblade30816f22018-11-10 13:40:22 +07005121 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_INDEFINITE_STRING_CHUNK) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305122 return -10;
5123 }
5124
5125 // ----- not a string ---
Laurence Lundbladeee851742020-01-08 08:37:05 -08005126 QCBORDecode_Init(&DC,
5127 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad3),
5128 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005129
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305130 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
5131 return -11;
5132 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005133
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305134 if(QCBORDecode_GetNext(&DC, &Item)) {
5135 return -12;
5136 }
5137 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
5138 return -13;
5139 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005140
Laurence Lundblade30816f22018-11-10 13:40:22 +07005141 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_INDEFINITE_STRING_CHUNK) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305142 return -14;
5143 }
5144
5145 // ----- no end -----
Laurence Lundbladeee851742020-01-08 08:37:05 -08005146 QCBORDecode_Init(&DC,
5147 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad4),
5148 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005149
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305150 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
5151 return -15;
5152 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005153
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305154 if(QCBORDecode_GetNext(&DC, &Item)) {
5155 return -16;
5156 }
5157 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
5158 return -17;
5159 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005160
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305161 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_HIT_END) {
5162 return -18;
5163 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005164
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305165 // ------ Don't set a string allocator and see an error -----
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05305166 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005167
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05305168 QCBORDecode_GetNext(&DC, &Item);
5169 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305170 return -19;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05305171 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005172
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05305173 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_NO_STRING_ALLOCATOR) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305174 return -20;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05305175 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005176
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05305177 // ----- Mempool is way too small -----
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08005178 UsefulBuf_MAKE_STACK_UB(MemPoolTooSmall, QCBOR_DECODE_MIN_MEM_POOL_SIZE-1);
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05305179
5180 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
5181 if(!QCBORDecode_SetMemPool(&DC, MemPoolTooSmall, false)) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305182 return -21;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05305183 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005184
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05305185 // ----- Mempool is way too small -----
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05305186 UsefulBuf_MAKE_STACK_UB(BigIndefBStrStorage, 290);
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08005187 const UsefulBufC BigIndefBStr = MakeIndefiniteBigBstr(BigIndefBStrStorage);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005188
Laurence Lundbladeee851742020-01-08 08:37:05 -08005189 // 80 is big enough for MemPool overhead, but not BigIndefBStr
5190 UsefulBuf_MAKE_STACK_UB(MemPoolSmall, 80);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005191
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305192 QCBORDecode_Init(&DC, BigIndefBStr, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05305193 if(QCBORDecode_SetMemPool(&DC, MemPoolSmall, false)) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305194 return -22;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05305195 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005196
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05305197 QCBORDecode_GetNext(&DC, &Item);
5198 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305199 return -23;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05305200 }
Laurence Lundblade30816f22018-11-10 13:40:22 +07005201 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_STRING_ALLOCATE) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305202 return -24;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05305203 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005204
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305205 // ---- big bstr -----
5206 QCBORDecode_Init(&DC, BigIndefBStr, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005207
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305208 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
5209 return -25;
5210 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005211
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05305212 if(QCBORDecode_GetNext(&DC, &Item)) {
5213 return -26;
5214 }
5215 if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.uDataAlloc) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305216 return -26;
5217 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005218
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305219 if(QCBORDecode_GetNext(&DC, &Item)) {
5220 return -27;
5221 }
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05305222 if(Item.uDataType != QCBOR_TYPE_BYTE_STRING || !Item.uDataAlloc || Item.uNestingLevel != 1) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305223 return -28;
5224 }
5225 if(CheckBigString(Item.val.string)) {
5226 return -3;
5227 }
5228 if(QCBORDecode_Finish(&DC)) {
5229 return -29;
5230 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005231
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305232 // --- label is an indefinite length string ------
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07005233 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringLabel), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005234
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305235 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
5236 return -30;
5237 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005238
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305239 QCBORDecode_GetNext(&DC, &Item);
5240 if(Item.uDataType != QCBOR_TYPE_MAP) {
5241 return -31;
5242 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005243
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305244 if(QCBORDecode_GetNext(&DC, &Item)){
5245 return -32;
5246 }
Laurence Lundbladeee851742020-01-08 08:37:05 -08005247 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
5248 Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305249 Item.uDataAlloc || !Item.uLabelAlloc ||
5250 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("struuming"))) {
5251 return -33;
5252 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005253
Laurence Lundblade57dd1442018-10-15 20:26:28 +05305254 if(QCBORDecode_Finish(&DC)) {
5255 return -34;
5256 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005257
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08005258 return 0;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08005259}
5260
5261
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03005262int32_t AllocAllStringsTest(void)
Laurence Lundbladea44d5062018-10-17 18:45:12 +05305263{
5264 QCBORDecodeContext DC;
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08005265 QCBORError nCBORError;
5266
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005267
Laurence Lundbladea44d5062018-10-17 18:45:12 +05305268 // First test, use the "CSRMap" as easy input and checking
Laurence Lundbladeee851742020-01-08 08:37:05 -08005269 QCBORDecode_Init(&DC,
5270 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput),
5271 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005272
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08005273 UsefulBuf_MAKE_STACK_UB(Pool, sizeof(spCSRInput) + QCBOR_DECODE_MIN_MEM_POOL_SIZE);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005274
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08005275 nCBORError = QCBORDecode_SetMemPool(&DC, Pool, 1); // Turn on copying.
5276 if(nCBORError) {
Laurence Lundbladea44d5062018-10-17 18:45:12 +05305277 return -1;
5278 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005279
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08005280 if(CheckCSRMaps(&DC)) {
5281 return -2;
5282 }
5283
Laurence Lundblade2f467f92020-10-09 17:50:11 -07005284 // Next parse, save pointers to a few strings, destroy original and
5285 // see all is OK.
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08005286 UsefulBuf_MAKE_STACK_UB(CopyOfStorage, sizeof(pValidMapEncoded) + QCBOR_DECODE_MIN_MEM_POOL_SIZE);
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08005287 const UsefulBufC CopyOf = UsefulBuf_Copy(CopyOfStorage, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded));
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08005288
Laurence Lundbladea44d5062018-10-17 18:45:12 +05305289 QCBORDecode_Init(&DC, CopyOf, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08005290 UsefulBuf_Set(Pool, '/');
Laurence Lundbladea44d5062018-10-17 18:45:12 +05305291 QCBORDecode_SetMemPool(&DC, Pool, 1); // Turn on copying.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005292
Laurence Lundbladea44d5062018-10-17 18:45:12 +05305293 QCBORItem Item1, Item2, Item3, Item4;
5294 if((nCBORError = QCBORDecode_GetNext(&DC, &Item1)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07005295 return (int32_t)nCBORError;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05305296 if(Item1.uDataType != QCBOR_TYPE_MAP ||
5297 Item1.val.uCount != 3)
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08005298 return -3;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05305299 if((nCBORError = QCBORDecode_GetNext(&DC, &Item1)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07005300 return (int32_t)nCBORError;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05305301 if((nCBORError = QCBORDecode_GetNext(&DC, &Item2)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07005302 return (int32_t)nCBORError;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05305303 if((nCBORError = QCBORDecode_GetNext(&DC, &Item3)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07005304 return (int32_t)nCBORError;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05305305 if((nCBORError = QCBORDecode_GetNext(&DC, &Item4)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07005306 return (int32_t)nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005307
Laurence Lundblade05ec57b2018-10-21 01:50:03 +05305308 UsefulBuf_Set(CopyOfStorage, '_');
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005309
Laurence Lundbladea44d5062018-10-17 18:45:12 +05305310 if(Item1.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladea44d5062018-10-17 18:45:12 +05305311 Item1.uDataType != QCBOR_TYPE_INT64 ||
5312 Item1.val.int64 != 42 ||
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08005313 Item1.uDataAlloc != 0 ||
5314 Item1.uLabelAlloc == 0 ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07005315 UsefulBufCompareToSZ(Item1.label.string, "first integer")) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08005316 return -4;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09005317 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005318
Laurence Lundbladea44d5062018-10-17 18:45:12 +05305319
5320 if(Item2.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07005321 UsefulBufCompareToSZ(Item2.label.string, "an array of two strings") ||
Laurence Lundbladea44d5062018-10-17 18:45:12 +05305322 Item2.uDataType != QCBOR_TYPE_ARRAY ||
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08005323 Item2.uDataAlloc != 0 ||
5324 Item2.uLabelAlloc == 0 ||
Laurence Lundbladea44d5062018-10-17 18:45:12 +05305325 Item2.val.uCount != 2)
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08005326 return -5;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005327
Laurence Lundbladea44d5062018-10-17 18:45:12 +05305328 if(Item3.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08005329 Item3.uDataAlloc == 0 ||
5330 Item3.uLabelAlloc != 0 ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07005331 UsefulBufCompareToSZ(Item3.val.string, "string1")) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08005332 return -6;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09005333 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005334
Laurence Lundbladea44d5062018-10-17 18:45:12 +05305335 if(Item4.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08005336 Item4.uDataAlloc == 0 ||
5337 Item4.uLabelAlloc != 0 ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07005338 UsefulBufCompareToSZ(Item4.val.string, "string2")) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08005339 return -7;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09005340 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005341
Laurence Lundbladea44d5062018-10-17 18:45:12 +05305342 // Next parse with a pool that is too small
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08005343 UsefulBuf_MAKE_STACK_UB(SmallPool, QCBOR_DECODE_MIN_MEM_POOL_SIZE + 1);
Laurence Lundbladeee851742020-01-08 08:37:05 -08005344 QCBORDecode_Init(&DC,
5345 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded),
5346 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladea44d5062018-10-17 18:45:12 +05305347 QCBORDecode_SetMemPool(&DC, SmallPool, 1); // Turn on copying.
5348 if((nCBORError = QCBORDecode_GetNext(&DC, &Item1)))
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08005349 return -8;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05305350 if(Item1.uDataType != QCBOR_TYPE_MAP ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09005351 Item1.val.uCount != 3) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08005352 return -9;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09005353 }
Laurence Lundbladea44d5062018-10-17 18:45:12 +05305354 if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item1))){
5355 if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item2))) {
5356 if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item3))) {
5357 nCBORError = QCBORDecode_GetNext(&DC, &Item4);
5358 }
5359 }
5360 }
Laurence Lundblade30816f22018-11-10 13:40:22 +07005361 if(nCBORError != QCBOR_ERR_STRING_ALLOCATE) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08005362 return -10;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05305363 }
5364
5365 return 0;
5366}
5367
Laurence Lundbladef6531662018-12-04 10:42:22 +09005368
Laurence Lundbladec5fef682020-01-25 11:38:45 -08005369int32_t MemPoolTest(void)
Laurence Lundblade0155b622018-10-12 20:04:37 +08005370{
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08005371 // Set up the decoder with a tiny bit of CBOR to parse because
5372 // nothing can be done with it unless that is set up.
Laurence Lundbladef6531662018-12-04 10:42:22 +09005373 QCBORDecodeContext DC;
5374 const uint8_t pMinimalCBOR[] = {0xa0}; // One empty map
5375 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pMinimalCBOR),0);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005376
Laurence Lundbladef6531662018-12-04 10:42:22 +09005377 // Set up an memory pool of 100 bytes
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08005378 // Then fish into the internals of the decode context
5379 // to get the allocator function so it can be called directly.
5380 // Also figure out how much pool is available for use
5381 // buy subtracting out the overhead.
Laurence Lundbladef6531662018-12-04 10:42:22 +09005382 UsefulBuf_MAKE_STACK_UB(Pool, 100);
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08005383 QCBORError nError = QCBORDecode_SetMemPool(&DC, Pool, 0);
5384 if(nError) {
5385 return -9;
5386 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08005387 QCBORStringAllocate pAlloc = DC.StringAllocator.pfAllocator;
5388 void *pAllocCtx = DC.StringAllocator.pAllocateCxt;
5389 size_t uAvailPool = Pool.len - QCBOR_DECODE_MIN_MEM_POOL_SIZE;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005390
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08005391 // First test -- ask for one more byte than available and see failure
5392 UsefulBuf Allocated = (*pAlloc)(pAllocCtx, NULL, uAvailPool+1);
Laurence Lundbladef6531662018-12-04 10:42:22 +09005393 if(!UsefulBuf_IsNULL(Allocated)) {
5394 return -1;
5395 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005396
Laurence Lundbladef6531662018-12-04 10:42:22 +09005397 // Re do the set up for the next test that will do a successful alloc,
5398 // a fail, a free and then success
Laurence Lundbladef6531662018-12-04 10:42:22 +09005399 QCBORDecode_SetMemPool(&DC, Pool, 0);
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08005400 pAlloc = DC.StringAllocator.pfAllocator;
5401 pAllocCtx = DC.StringAllocator.pAllocateCxt;
5402 uAvailPool = Pool.len - QCBOR_DECODE_MIN_MEM_POOL_SIZE;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005403
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08005404 // Allocate one byte less than available and see success
5405 Allocated = (pAlloc)(pAllocCtx, NULL, uAvailPool-1);
Laurence Lundbladef6531662018-12-04 10:42:22 +09005406 if(UsefulBuf_IsNULL(Allocated)) { // expected to succeed
5407 return -2;
5408 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08005409 // Ask for some more and see failure
5410 UsefulBuf Allocated2 = (*pAlloc)(pAllocCtx, NULL, uAvailPool/2);
Laurence Lundbladef6531662018-12-04 10:42:22 +09005411 if(!UsefulBuf_IsNULL(Allocated2)) { // expected to fail
5412 return -3;
5413 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08005414 // Free the first allocate, retry the second and see success
5415 (*pAlloc)(pAllocCtx, Allocated.ptr, 0); // Free
5416 Allocated = (*pAlloc)(pAllocCtx, NULL, uAvailPool/2);
Laurence Lundbladef6531662018-12-04 10:42:22 +09005417 if(UsefulBuf_IsNULL(Allocated)) { // succeed because of the free
5418 return -4;
5419 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005420
Laurence Lundbladef6531662018-12-04 10:42:22 +09005421 // Re do set up for next test that involves a successful alloc,
5422 // and a successful realloc and a failed realloc
5423 QCBORDecode_SetMemPool(&DC, Pool, 0);
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08005424 pAlloc = DC.StringAllocator.pfAllocator;
5425 pAllocCtx = DC.StringAllocator.pAllocateCxt;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005426
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08005427 // Allocate half the pool and see success
5428 Allocated = (*pAlloc)(pAllocCtx, NULL, uAvailPool/2);
Laurence Lundbladef6531662018-12-04 10:42:22 +09005429 if(UsefulBuf_IsNULL(Allocated)) { // expected to succeed
5430 return -5;
5431 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08005432 // Reallocate to take up the whole pool and see success
5433 Allocated2 = (*pAlloc)(pAllocCtx, Allocated.ptr, uAvailPool);
Laurence Lundbladef6531662018-12-04 10:42:22 +09005434 if(UsefulBuf_IsNULL(Allocated2)) {
5435 return -6;
5436 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08005437 // Make sure its the same pointer and the size is right
Laurence Lundbladef6531662018-12-04 10:42:22 +09005438 if(Allocated2.ptr != Allocated.ptr || Allocated2.len != uAvailPool) {
5439 return -7;
5440 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08005441 // Try to allocate more to be sure there is failure after a realloc
5442 UsefulBuf Allocated3 = (*pAlloc)(pAllocCtx, Allocated.ptr, uAvailPool+1);
5443 if(!UsefulBuf_IsNULL(Allocated3)) {
Laurence Lundbladef6531662018-12-04 10:42:22 +09005444 return -8;
5445 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08005446
Laurence Lundbladef6531662018-12-04 10:42:22 +09005447 return 0;
5448}
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08005449
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08005450
5451/* Just enough of an allocator to test configuration of one */
5452static UsefulBuf AllocateTestFunction(void *pCtx, void *pOldMem, size_t uNewSize)
5453{
5454 (void)pOldMem; // unused variable
5455
5456 if(uNewSize) {
5457 // Assumes the context pointer is the buffer and
5458 // nothing too big will ever be asked for.
5459 // This is only good for this basic test!
5460 return (UsefulBuf) {pCtx, uNewSize};
5461 } else {
5462 return NULLUsefulBuf;
5463 }
5464}
5465
5466
Laurence Lundbladec5fef682020-01-25 11:38:45 -08005467int32_t SetUpAllocatorTest(void)
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08005468{
5469 // Set up the decoder with a tiny bit of CBOR to parse because
5470 // nothing can be done with it unless that is set up.
5471 QCBORDecodeContext DC;
5472 const uint8_t pMinimalCBOR[] = {0x62, 0x48, 0x69}; // "Hi"
5473 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pMinimalCBOR),0);
5474
5475 uint8_t pAllocatorBuffer[50];
5476
5477 // This is really just to test that this call works.
5478 // The full functionality of string allocators is tested
5479 // elsewhere with the MemPool internal allocator.
5480 QCBORDecode_SetUpAllocator(&DC, AllocateTestFunction, pAllocatorBuffer, 1);
5481
5482 QCBORItem Item;
5483 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_SUCCESS) {
5484 return -1;
5485 }
5486
5487 if(Item.uDataAlloc == 0 ||
5488 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
5489 Item.val.string.ptr != pAllocatorBuffer) {
5490 return -2;
5491 }
5492
5493 if(QCBORDecode_Finish(&DC) != QCBOR_SUCCESS) {
5494 return -3;
5495 }
5496
5497 return 0;
5498}
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08005499#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
5500
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08005501
Laurence Lundbladedd6e76e2021-03-10 01:54:01 -07005502#ifndef QCBOR_DISABLE_EXP_AND_MANTISSA
Laurence Lundblade59289e52019-12-30 13:44:37 -08005503
Laurence Lundblade37286c02022-09-03 10:05:02 -07005504struct EaMTest {
5505 const char *szName;
5506 UsefulBufC Input;
5507 uint8_t uTagRequirement;
5508 bool bHasTags;
5509
5510 /* Expected values for GetNext */
5511 QCBORError uExpectedErrorGN;
5512 uint8_t uQCBORTypeGN;
5513 int64_t nExponentGN;
5514 int64_t nMantissaGN;
5515 UsefulBufC MantissaGN;
5516
5517 /* Expected values for GetDecimalFraction */
5518 QCBORError uExpectedErrorGDF;
5519 int64_t nExponentGDF;
5520 int64_t nMantissaGDF;
5521
5522 /* Expected values for GetDecimalFractionBig */
5523 QCBORError uExpectedErrorGDFB;
5524 int64_t nExponentGDFB;
5525 UsefulBufC MantissaGDFB;
5526 bool IsNegativeGDFB;
5527
5528 /* Expected values for GetBigFloat */
5529 QCBORError uExpectedErrorGBF;
5530 int64_t nExponentGBF;
5531 int64_t nMantissaGBF;
5532
5533 /* Expected values for GetBigFloatBig */
5534 QCBORError uExpectedErrorGBFB;
5535 int64_t nExponentGBFB;
5536 UsefulBufC MantissaGBFB;
5537 bool IsNegativeGBFB;
Laurence Lundblade59289e52019-12-30 13:44:37 -08005538};
5539
Laurence Lundbladefaec39f2020-08-02 21:53:53 -07005540
Laurence Lundblade37286c02022-09-03 10:05:02 -07005541
5542static const struct EaMTest pEaMTests[] = {
5543 {
5544 "1. Untagged pair (big float or decimal fraction), no tag required",
5545 {(const uint8_t []){0x82, 0x20, 0x03}, 3},
5546 QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
5547 false,
5548
5549 QCBOR_SUCCESS, /* for GetNext */
5550 QCBOR_TYPE_ARRAY,
5551 0,
5552 0,
5553 {(const uint8_t []){0x00}, 1},
5554
5555 QCBOR_SUCCESS, /* GetDecimalFraction */
5556 -1,
5557 3,
5558
5559 QCBOR_SUCCESS, /* for GetDecimalFractionBig */
5560 -1,
5561 {(const uint8_t []){0x03}, 1},
5562 false,
5563
5564 QCBOR_SUCCESS, /* for GetBigFloat */
5565 -1,
5566 3,
5567
5568 QCBOR_SUCCESS, /* for GetBigFloatBig */
5569 -1,
5570 {(const uint8_t []){0x03}, 1},
5571 false
5572 },
5573
5574 {
5575 "2. Untagged pair (big float or decimal fraction), tag required",
5576 {(const uint8_t []){0x82, 0x20, 0x03}, 3},
5577 QCBOR_TAG_REQUIREMENT_TAG,
5578 false,
5579
5580 QCBOR_SUCCESS, /* for GetNext */
5581 QCBOR_TYPE_ARRAY,
5582 0,
5583 0,
5584 {(const uint8_t []){0x00}, 1},
5585
5586 QCBOR_ERR_UNEXPECTED_TYPE, /* for GetDecimalFraction */
5587 0,
5588 0,
5589
5590 QCBOR_ERR_UNEXPECTED_TYPE, /* for GetDecimalFractionBig */
5591 0,
5592 {(const uint8_t []){0x00}, 1},
5593 false,
5594
5595 QCBOR_ERR_UNEXPECTED_TYPE, /* for GetBigFloat */
5596 0,
5597 0,
5598
5599 QCBOR_ERR_UNEXPECTED_TYPE, /* for GetBigFloatBig */
5600 0,
5601 {(const uint8_t []){0x00}, 1},
5602 false
5603
5604 },
5605
5606 {
5607 "3. Tagged 1.5 decimal fraction, tag 4 optional",
5608 {(const uint8_t []){0xC4, 0x82, 0x20, 0x03}, 4},
5609 QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
5610 true,
5611
5612 QCBOR_SUCCESS, /* for GetNext */
5613 QCBOR_TYPE_DECIMAL_FRACTION,
5614 -1,
5615 3,
5616 {(const uint8_t []){0x00}, 1},
5617
5618
5619 QCBOR_SUCCESS, /* for GetDecimalFraction */
5620 -1,
5621 3,
5622
5623 QCBOR_SUCCESS, /* for GetDecimalFractionBig */
5624 -1,
5625 {(const uint8_t []){0x03}, 1},
5626 false,
5627
5628 QCBOR_ERR_UNEXPECTED_TYPE, /* for GetBigFloat */
5629 0,
5630 0,
5631
5632 QCBOR_ERR_UNEXPECTED_TYPE, /* for GetBigFloatBig */
5633 0,
5634 {(const uint8_t []){0x00}, 1},
5635 false
5636 },
5637 {
5638 "4. Tagged 100 * 2^300 big float, tag 5 optional",
5639 {(const uint8_t []){0xC5, 0x82, 0x19, 0x01, 0x2C, 0x18, 0x64}, 7},
5640 QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
5641 true,
5642
5643 QCBOR_SUCCESS, /* for GetNext */
5644 QCBOR_TYPE_BIGFLOAT,
5645 300,
5646 100,
5647 {(const uint8_t []){0x00}, 1},
5648
5649
5650 QCBOR_ERR_UNEXPECTED_TYPE, /* for GetDecimalFraction */
5651 0,
5652 0,
5653
5654 QCBOR_ERR_UNEXPECTED_TYPE, /* for GetDecimalFractionBig */
5655 0,
5656 {(const uint8_t []){0x03}, 1},
5657 false,
5658
5659 QCBOR_SUCCESS, /* for GetBigFloat */
5660 300,
5661 100,
5662
5663 QCBOR_SUCCESS, /* for GetBigFloatBig */
5664 300,
5665 {(const uint8_t []){0x64}, 1},
5666 false
5667 },
5668
5669 {
5670 "5. Tagged 4([-20, 4759477275222530853136]) decimal fraction, tag 4 required",
5671 {(const uint8_t []){0xC4, 0x82, 0x33,
5672 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,}, 15},
5673 QCBOR_TAG_REQUIREMENT_TAG,
5674 true,
5675
5676 QCBOR_SUCCESS, /* for GetNext */
5677 QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM,
5678 -20,
5679 0,
5680 {(const uint8_t []){0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10}, 10},
5681
5682 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW, /* for GetDecimalFraction */
5683 0,
5684 0,
5685
5686 QCBOR_SUCCESS, /* for GetDecimalFractionBig */
5687 -20,
5688 {(const uint8_t []){0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10}, 10},
5689 false,
5690
5691 QCBOR_ERR_UNEXPECTED_TYPE, /* for GetBigFloat */
5692 0,
5693 0,
5694
5695 QCBOR_ERR_UNEXPECTED_TYPE, /* for GetBigFloatBig */
5696 0,
5697 {(const uint8_t []){0x00}, 0},
5698 false
5699 },
5700
5701 {
5702 "6. Error: Mantissa and exponent inside a Mantissa and exponent",
5703 {(const uint8_t []){0xC4, 0x82, 0x33,
5704 0xC5, 0x82, 0x19, 0x01, 0x2C, 0x18, 0x64}, 10},
5705 QCBOR_TAG_REQUIREMENT_TAG,
5706 true,
5707
5708 QCBOR_ERR_BAD_EXP_AND_MANTISSA, /* for GetNext */
5709 QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM,
5710 0,
5711 0,
5712 {(const uint8_t []){0x00}, 0},
5713
5714 QCBOR_ERR_BAD_EXP_AND_MANTISSA, /* for GetDecimalFraction */
5715 0,
5716 0,
5717
5718 QCBOR_ERR_BAD_EXP_AND_MANTISSA, /* for GetDecimalFractionBig */
5719 0,
5720 {(const uint8_t []){0x00}, 0},
5721 false,
5722
5723 QCBOR_ERR_BAD_EXP_AND_MANTISSA, /* for GetBigFloat */
5724 0,
5725 0,
5726
5727 QCBOR_ERR_BAD_EXP_AND_MANTISSA, /* for GetBigFloatBig */
5728 0,
5729 {(const uint8_t []){0x00}, 0},
5730 false
5731 },
5732 {
5733 "7. Tagged 5([-20, 4294967295]) big float, big num mantissa, tag 5 required",
5734 {(const uint8_t []){0xC5, 0x82, 0x33,
5735 0xC2, 0x44, 0xff, 0xff, 0xff, 0xff}, 9},
5736 QCBOR_TAG_REQUIREMENT_TAG,
5737 true,
5738
5739 QCBOR_SUCCESS, /* for GetNext */
5740 QCBOR_TYPE_BIGFLOAT_POS_BIGNUM,
5741 -20,
5742 0,
5743 {(const uint8_t []){0xff, 0xff, 0xff, 0xff}, 4},
5744
5745 QCBOR_ERR_UNEXPECTED_TYPE, /* for GetDecimalFraction */
5746 0,
5747 0,
5748
5749 QCBOR_ERR_UNEXPECTED_TYPE, /* for GetDecimalFractionBig */
5750 -20,
5751 {(const uint8_t []){0x00}, 1},
5752 false,
5753
5754 QCBOR_SUCCESS, /* for GetBigFloat */
5755 -20,
5756 4294967295,
5757
5758 QCBOR_SUCCESS, /* for GetBigFloatBig */
5759 -20,
5760 {(const uint8_t []){0xff, 0xff, 0xff, 0xff}, 4},
5761 false
5762 },
5763
5764 {
5765 /* Special case for test 8. Don't renumber it. */
5766 "8. Untagged pair with big num (big float or decimal fraction), tag optional",
5767 {(const uint8_t []){0x82, 0x33, 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10}, 14},
5768 QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
5769 true,
5770
5771 QCBOR_SUCCESS, /* for GetNext */
5772 QCBOR_TYPE_ARRAY,
5773 0,
5774 0,
5775 {(const uint8_t []){0x00}, 1},
5776
5777 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW, /* GetDecimalFraction */
5778 0,
5779 0,
5780
5781 QCBOR_SUCCESS, /* for GetDecimalFractionBig */
5782 -20,
5783 {(const uint8_t []){0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10}, 10},
5784 false,
5785
5786 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW, /* for GetBigFloat */
5787 0,
5788 0,
5789
5790 QCBOR_SUCCESS, /* for GetBigFloatBig */
5791 -20,
5792 {(const uint8_t []){0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10}, 10},
5793 false
5794 },
5795
5796 {
5797 "9. decimal fraction with large exponent and negative big num mantissa",
5798 {(const uint8_t []){0xC4, 0x82, 0x1B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
5799 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10}, 23},
5800 QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
5801 true,
5802
5803 QCBOR_SUCCESS, /* for GetNext */
5804 QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM,
5805 9223372036854775807,
5806 0,
5807 {(const uint8_t []){0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10}, 10},
5808
5809 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW, /* GetDecimalFraction */
5810 0,
5811 0,
5812
5813 QCBOR_SUCCESS, /* for GetDecimalFractionBig */
5814 9223372036854775807,
5815 {(const uint8_t []){0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10}, 10},
5816 true,
5817
5818 QCBOR_ERR_UNEXPECTED_TYPE, /* for GetBigFloat */
5819 0,
5820 0,
5821
5822 QCBOR_ERR_UNEXPECTED_TYPE, /* for GetBigFloatBig */
5823 0,
5824 {(const uint8_t []){0x00}, 1},
5825 false
5826 },
5827};
5828
5829
5830
5831int32_t ProcessEaMTests(void)
Laurence Lundblade59289e52019-12-30 13:44:37 -08005832{
Laurence Lundblade37286c02022-09-03 10:05:02 -07005833 size_t uIndex;
5834 QCBORDecodeContext DCtx;
5835 QCBORItem Item;
5836 QCBORError uError;
5837 int64_t nMantissa, nExponent;
5838 MakeUsefulBufOnStack( MantissaBuf, 200);
5839 UsefulBufC Mantissa;
5840 bool bMantissaIsNegative;
5841
5842 for(uIndex = 0; uIndex < C_ARRAY_COUNT(pEaMTests, struct EaMTest); uIndex++) {
5843 const struct EaMTest *pT = &pEaMTests[uIndex];
5844 /* Decode with GetNext */
5845 QCBORDecode_Init(&DCtx, pT->Input, 0);
5846
5847 if(uIndex + 1 == 9) {
5848 nExponent = 99; // just to set a break point
5849 }
5850
5851 uError = QCBORDecode_GetNext(&DCtx, &Item);
5852#ifdef QCBOR_DISABLE_TAGS
5853 /* Test 8 is a special case when tags are disabled */
5854 if(pT->bHasTags && uIndex + 1 != 8) {
5855 if(uError != QCBOR_ERR_TAGS_DISABLED) {
5856 return (int32_t)(1+uIndex) * 1000 + 9;
5857 }
5858 } else {
5859#endif
5860 /* Now check return code, data type, mantissa and exponent */
5861 if(pT->uExpectedErrorGN != uError) {
5862 return (int32_t)(1+uIndex) * 1000 + 1;
5863 }
5864 if(uError == QCBOR_SUCCESS && pT->uQCBORTypeGN != QCBOR_TYPE_ARRAY) {
5865 if(pT->uQCBORTypeGN != Item.uDataType) {
5866 return (int32_t)(1+uIndex) * 1000 + 2;
5867 }
5868 if(pT->nExponentGN != Item.val.expAndMantissa.nExponent) {
5869 return (int32_t)(1+uIndex) * 1000 + 3;
5870 }
5871 if(Item.uDataType == QCBOR_TYPE_DECIMAL_FRACTION || Item.uDataType == QCBOR_TYPE_BIGFLOAT ) {
5872 if(pT->nMantissaGN != Item.val.expAndMantissa.Mantissa.nInt) {
5873 return (int32_t)(1+uIndex) * 1000 + 4;
5874 }
5875 } else {
5876 if(UsefulBuf_Compare(Item.val.expAndMantissa.Mantissa.bigNum, pT->MantissaGN)) {
5877 return (int32_t)(1+uIndex) * 1000 + 5;
5878 }
5879 }
5880 }
5881#ifdef QCBOR_DISABLE_TAGS
5882 }
5883#endif
5884
5885 /* Decode with GetDecimalFraction */
5886 QCBORDecode_Init(&DCtx, pT->Input, 0);
5887 QCBORDecode_GetDecimalFraction(&DCtx,
5888 pT->uTagRequirement,
5889 &nMantissa,
5890 &nExponent);
5891 uError = QCBORDecode_GetAndResetError(&DCtx);
5892#ifdef QCBOR_DISABLE_TAGS
5893 if(pT->bHasTags) {
5894 if(uError != QCBOR_ERR_TAGS_DISABLED) {
5895 return (int32_t)(1+uIndex) * 1000 + 39;
5896 }
5897 } else {
5898#endif
5899 /* Now check return code, mantissa and exponent */
5900 if(pT->uExpectedErrorGDF != uError) {
5901 return (int32_t)(1+uIndex) * 1000 + 31;
5902 }
5903 if(uError == QCBOR_SUCCESS) {
5904 if(pT->nExponentGDF != nExponent) {
5905 return (int32_t)(1+uIndex) * 1000 + 32;
5906 }
5907 if(pT->nMantissaGDF != nMantissa) {
5908 return (int32_t)(1+uIndex) * 1000 + 33;
5909 }
5910 }
5911#ifdef QCBOR_DISABLE_TAGS
5912 }
5913#endif
5914
5915 /* Decode with GetDecimalFractionBig */
5916 QCBORDecode_Init(&DCtx, pT->Input, 0);
5917 QCBORDecode_GetDecimalFractionBig(&DCtx,
5918 pT->uTagRequirement,
5919 MantissaBuf,
5920 &Mantissa,
5921 &bMantissaIsNegative,
5922 &nExponent);
5923 uError = QCBORDecode_GetAndResetError(&DCtx);
5924#ifdef QCBOR_DISABLE_TAGS
5925 if(pT->bHasTags) {
5926 if(uError != QCBOR_ERR_TAGS_DISABLED) {
5927 return (int32_t)(1+uIndex) * 1000 + 49;
5928 }
5929 } else {
5930#endif
5931 /* Now check return code, mantissa (bytes and sign) and exponent */
5932 if(pT->uExpectedErrorGDFB != uError) {
5933 return (int32_t)(1+uIndex) * 1000 + 41;
5934 }
5935 if(uError == QCBOR_SUCCESS) {
5936 if(pT->nExponentGDFB != nExponent) {
5937 return (int32_t)(1+uIndex) * 1000 + 42;
5938 }
5939 if(pT->IsNegativeGDFB != bMantissaIsNegative) {
5940 return (int32_t)(1+uIndex) * 1000 + 43;
5941 }
5942 if(UsefulBuf_Compare(Mantissa, pT->MantissaGDFB)) {
5943 return (int32_t)(1+uIndex) * 1000 + 44;
5944 }
5945 }
5946#ifdef QCBOR_DISABLE_TAGS
5947 }
5948#endif
5949
5950 /* Decode with GetBigFloat */
5951 QCBORDecode_Init(&DCtx, pT->Input, 0);
5952 QCBORDecode_GetBigFloat(&DCtx,
5953 pT->uTagRequirement,
5954 &nMantissa,
5955 &nExponent);
5956 uError = QCBORDecode_GetAndResetError(&DCtx);
5957#ifdef QCBOR_DISABLE_TAGS
5958 if(pT->bHasTags) {
5959 if(uError != QCBOR_ERR_TAGS_DISABLED) {
5960 return (int32_t)(1+uIndex) * 1000 + 19;
5961 }
5962 } else {
5963#endif
5964 /* Now check return code, mantissa and exponent */
5965 if(pT->uExpectedErrorGBF != uError) {
5966 return (int32_t)(1+uIndex) * 1000 + 11;
5967 }
5968 if(uError == QCBOR_SUCCESS) {
5969 if(pT->nExponentGBF != nExponent) {
5970 return (int32_t)(1+uIndex) * 1000 + 12;
5971 }
5972 if(pT->nMantissaGBF != nMantissa) {
5973 return (int32_t)(1+uIndex) * 1000 + 13;
5974 }
5975 }
5976#ifdef QCBOR_DISABLE_TAGS
5977 }
5978#endif
5979
5980 /* Decode with GetBigFloatBig */
5981 QCBORDecode_Init(&DCtx, pT->Input, 0);
5982 QCBORDecode_GetBigFloatBig(&DCtx,
5983 pT->uTagRequirement,
5984 MantissaBuf,
5985 &Mantissa,
5986 &bMantissaIsNegative,
5987 &nExponent);
5988 uError = QCBORDecode_GetAndResetError(&DCtx);
5989#ifdef QCBOR_DISABLE_TAGS
5990 if(pT->bHasTags) {
5991 if(uError != QCBOR_ERR_TAGS_DISABLED) {
5992 return (int32_t)(1+uIndex) * 1000 + 29;
5993 }
5994 } else {
5995#endif
5996 /* Now check return code, mantissa (bytes and sign) and exponent */
5997 if(pT->uExpectedErrorGBFB != uError) {
5998 return (int32_t)(1+uIndex) * 1000 + 21;
5999 }
6000 if(uError == QCBOR_SUCCESS) {
6001 if(pT->nExponentGBFB != nExponent) {
6002 return (int32_t)(1+uIndex) * 1000 + 22;
6003 }
6004 if(pT->IsNegativeGBFB != bMantissaIsNegative) {
6005 return (int32_t)(1+uIndex) * 1000 + 23;
6006 }
6007 if(UsefulBuf_Compare(Mantissa, pT->MantissaGBFB)) {
6008 return (int32_t)(1+uIndex) * 1000 + 24;
6009 }
6010 }
6011#ifdef QCBOR_DISABLE_TAGS
6012 }
6013#endif
6014 }
6015
6016 return 0;
6017}
6018
6019
6020int32_t ExponentAndMantissaDecodeTestsSecondary(void)
6021{
6022#ifndef QCBOR_DISABLE_TAGS
Laurence Lundblade59289e52019-12-30 13:44:37 -08006023 QCBORDecodeContext DC;
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07006024 QCBORError uErr;
Laurence Lundblade59289e52019-12-30 13:44:37 -08006025 QCBORItem item;
6026
Laurence Lundblade17af4902020-01-07 19:11:55 -08006027 static const uint8_t spBigNumMantissa[] = {0x01, 0x02, 0x03, 0x04, 0x05,
6028 0x06, 0x07, 0x08, 0x09, 0x010};
6029 UsefulBufC BN = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNumMantissa);
Laurence Lundblade59289e52019-12-30 13:44:37 -08006030
6031
Laurence Lundblade59289e52019-12-30 13:44:37 -08006032
6033 /* Now encode some stuff and then decode it */
6034 uint8_t pBuf[40];
6035 QCBOREncodeContext EC;
6036 UsefulBufC Encoded;
6037
6038 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(pBuf));
6039 QCBOREncode_OpenArray(&EC);
6040 QCBOREncode_AddDecimalFraction(&EC, 999, 1000); // 999 * (10 ^ 1000)
6041 QCBOREncode_AddBigFloat(&EC, 100, INT32_MIN);
6042 QCBOREncode_AddDecimalFractionBigNum(&EC, BN, false, INT32_MAX);
6043 QCBOREncode_CloseArray(&EC);
6044 QCBOREncode_Finish(&EC, &Encoded);
6045
6046
6047 QCBORDecode_Init(&DC, Encoded, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07006048 uErr = QCBORDecode_GetNext(&DC, &item);
6049 if(uErr != QCBOR_SUCCESS) {
6050 return 100;
Laurence Lundblade59289e52019-12-30 13:44:37 -08006051 }
6052
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07006053 uErr = QCBORDecode_GetNext(&DC, &item);
6054 if(uErr != QCBOR_SUCCESS) {
6055 return 101;
Laurence Lundblade59289e52019-12-30 13:44:37 -08006056 }
6057
6058 if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION ||
6059 item.val.expAndMantissa.nExponent != 1000 ||
6060 item.val.expAndMantissa.Mantissa.nInt != 999) {
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07006061 return 102;
Laurence Lundblade59289e52019-12-30 13:44:37 -08006062 }
6063
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07006064 uErr = QCBORDecode_GetNext(&DC, &item);
6065 if(uErr != QCBOR_SUCCESS) {
6066 return 103;
Laurence Lundblade59289e52019-12-30 13:44:37 -08006067 }
6068
6069 if(item.uDataType != QCBOR_TYPE_BIGFLOAT ||
6070 item.val.expAndMantissa.nExponent != INT32_MIN ||
6071 item.val.expAndMantissa.Mantissa.nInt != 100) {
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07006072 return 104;
Laurence Lundblade59289e52019-12-30 13:44:37 -08006073 }
6074
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07006075 uErr = QCBORDecode_GetNext(&DC, &item);
6076 if(uErr != QCBOR_SUCCESS) {
6077 return 105;
Laurence Lundblade59289e52019-12-30 13:44:37 -08006078 }
6079
6080 if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM ||
6081 item.val.expAndMantissa.nExponent != INT32_MAX ||
6082 UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) {
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07006083 return 106;
6084 }
6085
Laurence Lundblade37286c02022-09-03 10:05:02 -07006086#endif /* QCBOR_TAGS_DISABLED */
Laurence Lundblade59289e52019-12-30 13:44:37 -08006087
6088 return 0;
6089}
6090
6091
Laurence Lundblade37286c02022-09-03 10:05:02 -07006092int32_t ExponentAndMantissaDecodeTests(void)
6093{
6094 int32_t rv = ProcessEaMTests();
6095 if(rv) {
6096 return rv;
6097 }
6098
6099 return ExponentAndMantissaDecodeTestsSecondary();
6100}
6101
6102
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08006103static const struct DecodeFailTestInput ExponentAndMantissaFailures[] = {
6104 { "Exponent > INT64_MAX",
6105 QCBOR_DECODE_MODE_NORMAL,
6106 {"\xC4\x82\x1B\x7f\xFF\xFF\xFF\xFF\xFF\xFF\xFF\x1B\x80\xFF\xFF\xFF\xFF\xFF\xFF\xFF", 20},
6107 QCBOR_ERR_BAD_EXP_AND_MANTISSA
6108 },
6109 { "Mantissa > INT64_MAX",
6110 QCBOR_DECODE_MODE_NORMAL,
6111 {"\xC4\x82\x1B\x80\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xC3\x4A\x01\x02\x03\x04\x05\x06\x07\x08\x09\x10", 23},
6112 QCBOR_ERR_BAD_EXP_AND_MANTISSA
6113 },
6114 {
6115 "End of input",
6116 QCBOR_DECODE_MODE_NORMAL,
6117 {"\xC4\x82", 2},
6118 QCBOR_ERR_NO_MORE_ITEMS
6119 },
6120 {"bad content for big num",
6121 QCBOR_DECODE_MODE_NORMAL,
6122 {"\xC4\x82\x01\xc3\x01", 5},
6123 QCBOR_ERR_BAD_OPT_TAG
6124 },
6125 {"bad content for big num",
6126 QCBOR_DECODE_MODE_NORMAL,
6127 {"\xC4\x82\xc2\x01\x1f", 5},
6128 QCBOR_ERR_BAD_INT
6129 },
6130 {"Bad integer for exponent",
6131 QCBOR_DECODE_MODE_NORMAL,
6132 {"\xC4\x82\x01\x1f", 4},
6133 QCBOR_ERR_BAD_INT
6134 },
6135 {"Bad integer for mantissa",
6136 QCBOR_DECODE_MODE_NORMAL,
6137 {"\xC4\x82\x1f\x01", 4},
6138 QCBOR_ERR_BAD_INT
6139 },
6140 {"3 items in array",
6141 QCBOR_DECODE_MODE_NORMAL,
6142 {"\xC4\x83\x03\x01\x02", 5},
6143 QCBOR_ERR_BAD_EXP_AND_MANTISSA},
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08006144#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08006145 {"unterminated indefinite length array",
6146 QCBOR_DECODE_MODE_NORMAL,
6147 {"\xC4\x9f\x03\x01\x02", 5},
6148 QCBOR_ERR_BAD_EXP_AND_MANTISSA
6149 },
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08006150#else /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08006151 {"unterminated indefinite length array",
6152 QCBOR_DECODE_MODE_NORMAL,
6153 {"\xC4\x9f\x03\x01\x02", 5},
6154 QCBOR_ERR_INDEF_LEN_ARRAYS_DISABLED
6155 },
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08006156#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08006157 {"Empty array",
6158 QCBOR_DECODE_MODE_NORMAL,
6159 {"\xC4\x80", 2},
6160 QCBOR_ERR_NO_MORE_ITEMS
6161 },
6162 {"Second is not an integer",
6163 QCBOR_DECODE_MODE_NORMAL,
6164 {"\xC4\x82\x03\x40", 4},
6165 QCBOR_ERR_BAD_EXP_AND_MANTISSA
6166 },
6167 {"First is not an integer",
6168 QCBOR_DECODE_MODE_NORMAL,
6169 {"\xC4\x82\x40", 3},
6170 QCBOR_ERR_BAD_EXP_AND_MANTISSA
6171 },
6172 {"Not an array",
6173 QCBOR_DECODE_MODE_NORMAL,
6174 {"\xC4\xA2", 2},
6175 QCBOR_ERR_BAD_EXP_AND_MANTISSA
6176 }
Laurence Lundblade59289e52019-12-30 13:44:37 -08006177};
6178
6179
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08006180int32_t
6181ExponentAndMantissaDecodeFailTests(void)
Laurence Lundblade59289e52019-12-30 13:44:37 -08006182{
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08006183 return ProcessDecodeFailures(ExponentAndMantissaFailures,
6184 C_ARRAY_COUNT(ExponentAndMantissaFailures,
6185 struct DecodeFailTestInput));
Laurence Lundblade59289e52019-12-30 13:44:37 -08006186}
6187
Laurence Lundbladedd6e76e2021-03-10 01:54:01 -07006188#endif /* QCBOR_DISABLE_EXP_AND_MANTISSA */
Laurence Lundbladebb87be22020-04-09 19:15:32 -07006189
6190
6191
6192/*
6193 Some basic CBOR with map and array used in a lot of tests.
6194 The map labels are all strings
6195
Laurence Lundblade8ffdb742020-05-07 02:49:18 -07006196 {
6197 "first integer": 42,
Laurence Lundbladebb87be22020-04-09 19:15:32 -07006198 "an array of two strings": [
6199 "string1", "string2"
6200 ],
6201 "map in a map": {
6202 "bytes 1": h'78787878',
6203 "bytes 2": h'79797979',
6204 "another int": 98,
6205 "text 2": "lies, damn lies and statistics"
6206 }
Laurence Lundbladeddebabe2020-11-22 11:32:17 -08006207 }
Laurence Lundbladebb87be22020-04-09 19:15:32 -07006208 */
Laurence Lundblade9b334962020-08-27 10:55:53 -07006209
Laurence Lundbladef7a70bc2020-10-24 12:23:25 -07006210int32_t SpiffyDecodeBasicMap(UsefulBufC input)
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07006211{
6212 QCBORItem Item1, Item2, Item3;
6213 int64_t nDecodedInt1, nDecodedInt2;
6214 UsefulBufC B1, B2, S1, S2, S3;
6215
6216 QCBORDecodeContext DCtx;
6217 QCBORError nCBORError;
6218
6219 QCBORDecode_Init(&DCtx, input, 0);
6220
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006221 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07006222
6223 QCBORDecode_GetInt64InMapSZ(&DCtx, "first integer", &nDecodedInt1);
6224
6225 QCBORDecode_EnterMapFromMapSZ(&DCtx, "map in a map");
6226 QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2);
Laurence Lundblade323f8a92020-09-06 19:43:09 -07006227 QCBORDecode_GetByteStringInMapSZ(&DCtx, "bytes 1", &B1);
6228 QCBORDecode_GetByteStringInMapSZ(&DCtx, "bytes 2", &B2);
6229 QCBORDecode_GetTextStringInMapSZ(&DCtx, "text 2", &S1);
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07006230 QCBORDecode_ExitMap(&DCtx);
6231
6232 QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings");
6233 QCBORDecode_GetNext(&DCtx, &Item1);
6234 QCBORDecode_GetNext(&DCtx, &Item2);
6235 if(QCBORDecode_GetNext(&DCtx, &Item3) != QCBOR_ERR_NO_MORE_ITEMS) {
6236 return -400;
6237 }
6238 QCBORDecode_ExitArray(&DCtx);
6239
6240 // Parse the same array again using GetText() instead of GetItem()
6241 QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings");
Laurence Lundblade323f8a92020-09-06 19:43:09 -07006242 QCBORDecode_GetTextString(&DCtx, &S2);
6243 QCBORDecode_GetTextString(&DCtx, &S3);
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07006244 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) {
6245 return 5000;
6246 }
6247 /* QCBORDecode_GetText(&DCtx, &S3);
6248 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_NO_MORE_ITEMS) {
6249 return 5001;
6250 } */
6251
6252 QCBORDecode_ExitArray(&DCtx);
6253
6254 QCBORDecode_ExitMap(&DCtx);
6255
6256 nCBORError = QCBORDecode_Finish(&DCtx);
6257
6258 if(nCBORError) {
6259 return (int32_t)nCBORError;
6260 }
6261
6262 if(nDecodedInt1 != 42) {
6263 return 1001;
6264 }
6265
6266 if(nDecodedInt2 != 98) {
6267 return 1002;
6268 }
6269
6270 if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07006271 UsefulBufCompareToSZ(Item1.val.string, "string1")) {
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07006272 return 1003;
6273 }
6274
6275 if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07006276 UsefulBufCompareToSZ(Item2.val.string, "string2")) {
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07006277 return 1004;
6278 }
6279
Laurence Lundblade9b334962020-08-27 10:55:53 -07006280 if(UsefulBufCompareToSZ(S1, "lies, damn lies and statistics")) {
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07006281 return 1005;
6282 }
6283
6284 if(UsefulBuf_Compare(B1, UsefulBuf_FromSZ("xxxx"))){
6285 return 1006;
6286 }
6287
6288 if(UsefulBuf_Compare(B2, UsefulBuf_FromSZ("yyyy"))){
6289 return 1007;
6290 }
6291
6292 if(UsefulBuf_Compare(S2, UsefulBuf_FromSZ("string1"))){
6293 return 1008;
6294 }
6295
6296 if(UsefulBuf_Compare(S3, UsefulBuf_FromSZ("string2"))){
6297 return 1009;
6298 }
6299
6300 return 0;
6301}
6302
Laurence Lundbladeddebabe2020-11-22 11:32:17 -08006303/*
Laurence Lundbladecc7da412020-12-27 00:09:07 -08006304 {
6305 -75008: h'05083399',
6306 88: [],
6307 100100: {
6308 "sub1": {
6309 10: [
6310 0
6311 ],
6312 -75009: h'A46823990001',
6313 100100: {
6314 "json": "{ \"ueid\", \"xyz\"}",
6315 "subsub": {
6316 100002: h'141813191001'
6317 }
6318 }
Laurence Lundbladeddebabe2020-11-22 11:32:17 -08006319 }
Laurence Lundbladecc7da412020-12-27 00:09:07 -08006320 }
Laurence Lundbladeddebabe2020-11-22 11:32:17 -08006321 }
Laurence Lundbladeddebabe2020-11-22 11:32:17 -08006322 */
6323
6324static const uint8_t spNestedCBOR[] = {
Laurence Lundbladecc7da412020-12-27 00:09:07 -08006325 0xa3, 0x3a, 0x00, 0x01, 0x24, 0xff, 0x44, 0x05,
6326 0x08, 0x33, 0x99, 0x18, 0x58, 0x80, 0x1a, 0x00,
6327 0x01, 0x87, 0x04, 0xa1, 0x64, 0x73, 0x75, 0x62,
6328 0x31, 0xa3, 0x0a, 0x81, 0x00, 0x3a, 0x00, 0x01,
6329 0x25, 0x00, 0x46, 0xa4, 0x68, 0x23, 0x99, 0x00,
6330 0x01, 0x1a, 0x00, 0x01, 0x87, 0x04, 0xa2, 0x64,
6331 0x6a, 0x73, 0x6f, 0x6e, 0x70, 0x7b, 0x20, 0x22,
6332 0x75, 0x65, 0x69, 0x64, 0x22, 0x2c, 0x20, 0x22,
6333 0x78, 0x79, 0x7a, 0x22, 0x7d, 0x66, 0x73, 0x75,
6334 0x62, 0x73, 0x75, 0x62, 0xa1, 0x1a, 0x00, 0x01,
6335 0x86, 0xa2, 0x46, 0x14, 0x18, 0x13, 0x19, 0x10,
6336 0x01
Laurence Lundbladeddebabe2020-11-22 11:32:17 -08006337};
6338
6339/* Get item in multi-level nesting in spNestedCBOR */
6340static int32_t DecodeNestedGetSubSub(QCBORDecodeContext *pDCtx)
6341{
6342 UsefulBufC String;
6343
6344 uint8_t test_oemid_bytes[] = {0x14, 0x18, 0x13, 0x19, 0x10, 0x01};
6345 const struct q_useful_buf_c test_oemid = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(test_oemid_bytes);
6346
6347 QCBORDecode_EnterMapFromMapN(pDCtx, 100100);
6348 QCBORDecode_EnterMap(pDCtx, NULL);
6349 QCBORDecode_EnterMapFromMapN(pDCtx, 100100);
6350 QCBORDecode_EnterMapFromMapSZ(pDCtx, "subsub");
6351 QCBORDecode_GetByteStringInMapN(pDCtx, 100002, &String);
6352 if(QCBORDecode_GetError(pDCtx)) {
6353 return 4001;
6354 }
6355 if(UsefulBuf_Compare(String, test_oemid)) {
6356 return 4002;
6357 }
6358 QCBORDecode_ExitMap(pDCtx);
6359 QCBORDecode_ExitMap(pDCtx);
6360 QCBORDecode_ExitMap(pDCtx);
6361 QCBORDecode_ExitMap(pDCtx);
6362
6363 return 0;
6364}
6365
6366/* Iterations on the zero-length array in spNestedCBOR */
6367static int32_t DecodeNestedGetEmpty(QCBORDecodeContext *pDCtx)
6368{
6369 QCBORItem Item;
6370 QCBORError uErr;
6371
6372 QCBORDecode_EnterArrayFromMapN(pDCtx, 88);
6373 for(int x = 0; x < 20; x++) {
6374 uErr = QCBORDecode_GetNext(pDCtx, &Item);
6375 if(uErr != QCBOR_ERR_NO_MORE_ITEMS) {
6376 return 4100;
6377
6378 }
6379 }
6380 QCBORDecode_ExitArray(pDCtx);
6381 if(QCBORDecode_GetError(pDCtx)) {
6382 return 4101;
6383 }
6384
6385 return 0;
6386}
6387
6388/* Various iterations on the array that contains a zero in spNestedCBOR */
6389static int32_t DecodeNestedGetZero(QCBORDecodeContext *pDCtx)
6390{
6391 QCBORError uErr;
6392
6393 QCBORDecode_EnterMapFromMapN(pDCtx, 100100);
6394 QCBORDecode_EnterMapFromMapSZ(pDCtx, "sub1");
6395 QCBORDecode_EnterArrayFromMapN(pDCtx, 10);
6396 int64_t nInt = 99;
6397 QCBORDecode_GetInt64(pDCtx, &nInt);
6398 if(nInt != 0) {
6399 return 4200;
6400 }
6401 for(int x = 0; x < 20; x++) {
6402 QCBORItem Item;
6403 uErr = QCBORDecode_GetNext(pDCtx, &Item);
6404 if(uErr != QCBOR_ERR_NO_MORE_ITEMS) {
6405 return 4201;
6406
6407 }
6408 }
6409 QCBORDecode_ExitArray(pDCtx);
6410 if(QCBORDecode_GetAndResetError(pDCtx)) {
6411 return 4202;
6412 }
6413 QCBORDecode_EnterArrayFromMapN(pDCtx, 10);
6414 UsefulBufC dD;
6415 QCBORDecode_GetByteString(pDCtx, &dD);
6416 if(QCBORDecode_GetAndResetError(pDCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
6417 return 4203;
6418 }
6419 for(int x = 0; x < 20; x++) {
6420 QCBORDecode_GetByteString(pDCtx, &dD);
6421 uErr = QCBORDecode_GetAndResetError(pDCtx);
6422 if(uErr != QCBOR_ERR_NO_MORE_ITEMS) {
6423 return 4204;
6424 }
6425 }
6426 QCBORDecode_ExitArray(pDCtx);
6427 QCBORDecode_ExitMap(pDCtx);
6428 QCBORDecode_ExitMap(pDCtx);
6429
6430 return 0;
6431}
6432
6433/* Repeatedly enter and exit maps and arrays, go off the end of maps
6434 and arrays and such. */
Laurence Lundbladeb9702452021-03-08 21:02:57 -08006435static int32_t DecodeNestedIterate(void)
Laurence Lundbladeddebabe2020-11-22 11:32:17 -08006436{
6437 QCBORDecodeContext DCtx;
6438 int32_t nReturn;
6439 QCBORError uErr;
6440
6441 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spNestedCBOR), 0);
6442 QCBORDecode_EnterMap(&DCtx, NULL);
6443
6444 for(int j = 0; j < 5; j++) {
6445 for(int i = 0; i < 20; i++) {
6446 nReturn = DecodeNestedGetSubSub(&DCtx);
6447 if(nReturn) {
6448 return nReturn;
6449 }
6450 }
6451
6452 for(int i = 0; i < 20; i++) {
6453 nReturn = DecodeNestedGetEmpty(&DCtx);
6454 if(nReturn ) {
6455 return nReturn;
6456 }
6457 }
6458
6459 for(int i = 0; i < 20; i++) {
6460 nReturn = DecodeNestedGetZero(&DCtx);
6461 if(nReturn ) {
6462 return nReturn;
6463 }
6464 }
6465 }
6466
6467 QCBORDecode_ExitMap(&DCtx);
6468 uErr = QCBORDecode_Finish(&DCtx);
6469 if(uErr) {
6470 return (int32_t)uErr + 4100;
6471 }
6472
6473 return 0;
6474}
6475
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07006476
6477/*
Laurence Lundbladecc7da412020-12-27 00:09:07 -08006478 [
6479 23,
6480 6000,
6481 h'67616C6163746963',
6482 h'686176656E20746F6B656E'
6483 ]
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07006484 */
6485static const uint8_t spSimpleArray[] = {
Laurence Lundblade9b334962020-08-27 10:55:53 -07006486 0x84,
6487 0x17,
6488 0x19, 0x17, 0x70,
6489 0x48, 0x67, 0x61, 0x6C, 0x61, 0x63, 0x74, 0x69, 0x63,
6490 0x4B, 0x68, 0x61, 0x76, 0x65, 0x6E, 0x20, 0x74, 0x6F, 0x6B, 0x65, 0x6E};
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07006491
Laurence Lundbladecc7da412020-12-27 00:09:07 -08006492/* [h'', {}, [], 0] */
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08006493static const uint8_t spArrayOfEmpty[] = {0x84, 0x40, 0xa0, 0x80, 0x00};
6494
Laurence Lundbladecc7da412020-12-27 00:09:07 -08006495/* {} */
Laurence Lundbladee6f15112020-07-23 18:44:16 -07006496static const uint8_t spEmptyMap[] = {0xa0};
6497
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08006498#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundbladecc7da412020-12-27 00:09:07 -08006499/* {} */
Laurence Lundbladee6f15112020-07-23 18:44:16 -07006500static const uint8_t spEmptyInDefinteLengthMap[] = {0xbf, 0xff};
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07006501
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08006502
Laurence Lundbladef0499502020-08-01 11:55:57 -07006503/*
6504 {
6505 0: [],
6506 9: [
6507 [],
6508 []
6509 ],
6510 8: {
6511 1: [],
6512 2: {},
6513 3: []
6514 },
6515 4: {},
6516 5: [],
6517 6: [
6518 [],
6519 []
6520 ]
Laurence Lundbladecc7da412020-12-27 00:09:07 -08006521 }
Laurence Lundbladef0499502020-08-01 11:55:57 -07006522 */
6523static const uint8_t spMapOfEmpty[] = {
Laurence Lundbladecc7da412020-12-27 00:09:07 -08006524 0xa6, 0x00, 0x80, 0x09, 0x82, 0x80, 0x80, 0x08,
6525 0xa3, 0x01, 0x80, 0x02, 0xa0, 0x03, 0x80, 0x04,
6526 0xa0, 0x05, 0x9f, 0xff, 0x06, 0x9f, 0x80, 0x9f,
6527 0xff, 0xff};
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07006528
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08006529#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
6530
6531
Laurence Lundblade93d3f532020-09-28 21:09:12 -07006532/*
6533 Too many tags
Laurence Lundblade93d3f532020-09-28 21:09:12 -07006534 Duplicate label
6535 Integer overflow
6536 Date overflow
6537
Laurence Lundbladecc7da412020-12-27 00:09:07 -08006538 {
6539 1: 224(225(226(227(4(0))))),
Laurence Lundbladecc7da412020-12-27 00:09:07 -08006540 3: -18446744073709551616,
6541 4: 1(1.0e+300),
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07006542 5: 0,
6543 8: 8
Laurence Lundbladecc7da412020-12-27 00:09:07 -08006544 }
Laurence Lundblade93d3f532020-09-28 21:09:12 -07006545 */
6546static const uint8_t spRecoverableMapErrors[] = {
Laurence Lundblade37286c02022-09-03 10:05:02 -07006547#ifndef QCBOR_DISABLE_TAGS
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07006548 0xa6,
Laurence Lundblade93d3f532020-09-28 21:09:12 -07006549 0x04, 0xc1, 0xfb, 0x7e, 0x37, 0xe4, 0x3c, 0x88, 0x00, 0x75, 0x9c,
Laurence Lundblade37286c02022-09-03 10:05:02 -07006550 0x01, 0xd8, 0xe0, 0xd8, 0xe1, 0xd8, 0xe2, 0xd8, 0xe3, 0xd8, 0x04, 0x00,
6551#else
6552 0xa4,
6553#endif
6554 0x03, 0x3b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
Laurence Lundblade93d3f532020-09-28 21:09:12 -07006555 0x05, 0x00,
6556 0x05, 0x00,
6557 0x08, 0x08,
Laurence Lundblade93d3f532020-09-28 21:09:12 -07006558};
6559
Laurence Lundbladecc7da412020-12-27 00:09:07 -08006560/* Bad break */
Laurence Lundblade93d3f532020-09-28 21:09:12 -07006561static const uint8_t spUnRecoverableMapError1[] = {
6562 0xa2, 0xff, 0x01, 0x00, 0x02, 0x00
6563};
6564
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08006565#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundbladecc7da412020-12-27 00:09:07 -08006566/* No more items */
Laurence Lundblade93d3f532020-09-28 21:09:12 -07006567static const uint8_t spUnRecoverableMapError2[] = {
6568 0xbf, 0x02, 0xbf, 0xff, 0x01, 0x00, 0x02, 0x00
6569};
6570
Laurence Lundbladecc7da412020-12-27 00:09:07 -08006571/* Hit end because string is too long */
Laurence Lundblade93d3f532020-09-28 21:09:12 -07006572static const uint8_t spUnRecoverableMapError3[] = {
6573 0xbf, 0x02, 0x69, 0x64, 0x64, 0xff
6574};
6575
Laurence Lundbladecc7da412020-12-27 00:09:07 -08006576/* Hit end because string is too long */
Laurence Lundblade93d3f532020-09-28 21:09:12 -07006577static const uint8_t spUnRecoverableMapError4[] = {
6578 0xbf,
6579 0x02, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f,
6580 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f,
6581 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
6582 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
6583 0xff
6584};
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08006585#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
Laurence Lundblade93d3f532020-09-28 21:09:12 -07006586
Laurence Lundblade63926052021-03-29 16:05:51 -07006587const unsigned char not_well_formed_submod_section[] = {
6588 0xa1, 0x14, 0x1f,
6589};
Laurence Lundblade93d3f532020-09-28 21:09:12 -07006590
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07006591
6592/* Array of length 3, but only two items. */
6593const unsigned char spBadConsumeInput[] = {
6594 0x83, 0x00, 0x00
6595};
6596
6597/* Tag nesting too deep. */
6598const unsigned char spBadConsumeInput2[] = {
6599 0x81,
6600 0xD8, 0x37,
6601 0xD8, 0x2C,
6602 0xD8, 0x21,
6603 0xD6,
6604 0xCB,
6605 00
6606};
6607
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07006608
6609const unsigned char spBadConsumeInput4[] = {
6610 0x81, 0x9f, 0x00, 0xff
6611};
6612
6613const unsigned char spBadConsumeInput5[] = {
6614 0xa1, 0x80, 0x00
6615};
6616
Laurence Lundbladec5f45e42023-12-18 09:23:20 -07006617/*
6618 Lots of nesting for various nesting tests.
6619 { 1:1,
6620 2:{
6621 21:21,
6622 22:{
6623 221:[2111, 2112, 2113],
6624 222:222,
6625 223: {}
6626 },
6627 23: 23
6628 },
6629 3:3,
6630 4: [ {} ]
6631 }
6632 */
6633static const uint8_t spNested[] = {
66340xA4, /* Map of 4 */
6635 0x01, 0x01, /* Map entry 1 : 1 */
6636 0x02, 0xA3, /* Map entry 2 : {, an array of 3 */
6637 0x15, 0x15, /* Map entry 21 : 21 */
6638 0x16, 0xA3, /* Map entry 22 : {, a map of 3 */
6639 0x18, 0xDD, 0x83, /* Map entry 221 : [ an array of 3 */
6640 0x19, 0x08, 0x3F, /* Array item 2111 */
6641 0x19, 0x08, 0x40, /* Array item 2112 */
6642 0x19, 0x08, 0x41, /* Array item 2113 */
6643 0x18, 0xDE, 0x18, 0xDE, /* Map entry 222 : 222 */
6644 0x18, 0xDF, 0xA0, /* Map entry 223 : {} */
6645 0x17, 0x17, /* Map entry 23 : 23 */
6646 0x03, 0x03, /* Map entry 3 : 3 */
6647 0x04, 0x81, /* Map entry 4: [, an array of 1 */
6648 0xA0 /* Array entry {}, an empty map */
6649};
6650
6651
6652static int32_t EnterMapCursorTest(void)
6653{
6654 QCBORDecodeContext DCtx;
6655 QCBORItem Item1;
Laurence Lundblade11654912024-05-09 11:49:24 -07006656 int64_t nInt;
6657 QCBORError uErr;
6658
6659 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spNested), 0);
6660 QCBORDecode_EnterMap(&DCtx, NULL);
6661 QCBORDecode_GetInt64InMapN (&DCtx, 3, &nInt);
6662 uErr = QCBORDecode_GetNext(&DCtx, &Item1);
Laurence Lundbladea29f45a2024-05-14 15:55:19 -07006663 if(uErr != QCBOR_SUCCESS) {
6664 return 701;
6665 }
Laurence Lundblade11654912024-05-09 11:49:24 -07006666 if(Item1.uDataType != QCBOR_TYPE_INT64) {
6667 return 700;
6668 }
6669
Laurence Lundbladec5f45e42023-12-18 09:23:20 -07006670
6671 int i;
6672 for(i = 0; i < 13; i++) {
6673 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spNested), 0);
6674 QCBORDecode_EnterMap(&DCtx, NULL);
6675 int j;
6676 /* Move travesal cursor */
6677 for(j = 0; j < i; j++) {
6678 QCBORDecode_GetNext(&DCtx, &Item1);
6679 }
6680 QCBORDecode_EnterMapFromMapN(&DCtx, 2);
6681 QCBORDecode_ExitMap(&DCtx);
6682 QCBORDecode_GetNext(&DCtx, &Item1);
6683 if(Item1.label.int64 != 3) {
6684 return 8000;
6685 }
6686 }
6687
6688 for(i = 0; i < 13; i++) {
6689 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spNested), 0);
6690 QCBORDecode_EnterMap(&DCtx, NULL);
6691 int j;
6692 /* Move travesal cursor */
6693 for(j = 0; j < i; j++) {
6694 QCBORDecode_GetNext(&DCtx, &Item1);
6695 }
6696 QCBORDecode_EnterMapFromMapN(&DCtx, 2);
6697 QCBORDecode_EnterMapFromMapN(&DCtx, 22);
6698 QCBORDecode_ExitMap(&DCtx);
6699 QCBORDecode_GetNext(&DCtx, &Item1);
6700 if(Item1.label.int64 != 23) {
6701 return 8000;
6702 }
6703 }
6704
6705 for(i = 0; i < 13; i++) {
6706 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spNested), 0);
6707 QCBORDecode_EnterMap(&DCtx, NULL);
6708 int j;
6709 /* Move travesal cursor */
6710 for(j = 0; j < i; j++) {
6711 QCBORDecode_GetNext(&DCtx, &Item1);
6712 }
6713 QCBORDecode_EnterMapFromMapN(&DCtx, 2);
6714 QCBORDecode_EnterMapFromMapN(&DCtx, 22);
6715 for(j = 0; j < i; j++) {
6716 QCBORDecode_GetNext(&DCtx, &Item1);
6717 }
6718 QCBORDecode_EnterArrayFromMapN(&DCtx, 221);
6719 QCBORDecode_ExitArray(&DCtx);
6720 QCBORDecode_ExitMap(&DCtx);
6721 QCBORDecode_GetNext(&DCtx, &Item1);
6722 if(Item1.label.int64 != 23) {
6723 return 8000;
6724 }
6725 QCBORDecode_ExitMap(&DCtx);
6726 QCBORDecode_GetNext(&DCtx, &Item1);
6727 if(Item1.label.int64 != 3) {
6728 return 8000;
6729 }
6730 }
6731
6732 return 0;
6733}
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07006734
6735
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03006736int32_t EnterMapTest(void)
Laurence Lundbladebb87be22020-04-09 19:15:32 -07006737{
Laurence Lundbladef0499502020-08-01 11:55:57 -07006738 QCBORItem Item1;
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006739 QCBORItem ArrayItem;
Laurence Lundbladebb87be22020-04-09 19:15:32 -07006740 QCBORDecodeContext DCtx;
Laurence Lundbladef0499502020-08-01 11:55:57 -07006741 int32_t nReturn;
6742 QCBORError uErr;
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07006743
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08006744#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07006745 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spMapOfEmpty), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006746 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07006747
Laurence Lundbladef0499502020-08-01 11:55:57 -07006748
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006749 QCBORDecode_EnterArray(&DCtx, NULL); // Label 0
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07006750 QCBORDecode_ExitArray(&DCtx);
6751
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006752 QCBORDecode_EnterArray(&DCtx, NULL); // Label 9
6753 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07006754 QCBORDecode_ExitArray(&DCtx);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006755 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07006756 QCBORDecode_ExitArray(&DCtx);
6757 QCBORDecode_ExitArray(&DCtx);
6758
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006759 QCBORDecode_EnterMap(&DCtx, NULL); // Label 8
6760 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07006761 QCBORDecode_ExitArray(&DCtx);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006762 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07006763 QCBORDecode_ExitMap(&DCtx);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006764 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07006765 QCBORDecode_ExitArray(&DCtx);
6766 QCBORDecode_ExitMap(&DCtx);
6767
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006768 QCBORDecode_EnterMap(&DCtx, NULL); // Label4
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07006769 QCBORDecode_ExitMap(&DCtx);
6770
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006771 QCBORDecode_EnterArray(&DCtx, NULL); // Label 5
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07006772 QCBORDecode_ExitArray(&DCtx);
6773
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006774 QCBORDecode_EnterArray(&DCtx, NULL); // Label 6
6775 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07006776 QCBORDecode_ExitArray(&DCtx);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006777 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07006778 QCBORDecode_ExitArray(&DCtx);
6779 QCBORDecode_ExitArray(&DCtx);
6780
6781 QCBORDecode_ExitMap(&DCtx);
6782
6783 uErr = QCBORDecode_Finish(&DCtx);
6784 if(uErr != QCBOR_SUCCESS){
6785 return 3011;
6786 }
6787
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07006788 (void)pValidMapIndefEncoded;
Laurence Lundbladef7a70bc2020-10-24 12:23:25 -07006789 nReturn = SpiffyDecodeBasicMap(UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapIndefEncoded));
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07006790 if(nReturn) {
6791 return nReturn + 20000;
Laurence Lundbladebb87be22020-04-09 19:15:32 -07006792 }
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08006793#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
6794
Laurence Lundbladebb87be22020-04-09 19:15:32 -07006795
Laurence Lundbladef7a70bc2020-10-24 12:23:25 -07006796 nReturn = SpiffyDecodeBasicMap(UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded));
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006797 if(nReturn) {
6798 return nReturn;
6799 }
Laurence Lundbladebb87be22020-04-09 19:15:32 -07006800
Laurence Lundblade8ffdb742020-05-07 02:49:18 -07006801
Laurence Lundblade937ea812020-05-08 11:38:23 -07006802
Laurence Lundblade2f467f92020-10-09 17:50:11 -07006803 // These tests confirm the cursor is at the right place after entering
6804 // a map or array
Laurence Lundblade9b334962020-08-27 10:55:53 -07006805 const UsefulBufC ValidEncodedMap = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded);
Laurence Lundblade937ea812020-05-08 11:38:23 -07006806
6807 // Confirm cursor is at right place
Laurence Lundblade9b334962020-08-27 10:55:53 -07006808 QCBORDecode_Init(&DCtx, ValidEncodedMap, 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006809 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade937ea812020-05-08 11:38:23 -07006810 QCBORDecode_GetNext(&DCtx, &Item1);
6811 if(Item1.uDataType != QCBOR_TYPE_INT64) {
6812 return 2001;
6813 }
6814
6815
Laurence Lundblade9b334962020-08-27 10:55:53 -07006816 QCBORDecode_Init(&DCtx, ValidEncodedMap, 0);
Laurence Lundblade6f3f78e2020-08-31 13:09:14 -07006817 QCBORDecode_VGetNext(&DCtx, &Item1);
6818 QCBORDecode_VGetNext(&DCtx, &Item1);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006819 QCBORDecode_EnterArray(&DCtx, &ArrayItem);
6820 if(ArrayItem.uLabelType != QCBOR_TYPE_TEXT_STRING ||
6821 UsefulBuf_Compare(ArrayItem.label.string,
6822 UsefulBuf_FROM_SZ_LITERAL("an array of two strings"))) {
6823 return 2051;
6824 }
Laurence Lundblade937ea812020-05-08 11:38:23 -07006825 QCBORDecode_GetNext(&DCtx, &Item1);
6826 if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING) {
6827 return 2002;
6828 }
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006829 QCBORDecode_ExitArray(&DCtx);
6830 QCBORDecode_EnterMap(&DCtx, &ArrayItem);
6831 if(ArrayItem.uLabelType != QCBOR_TYPE_TEXT_STRING ||
6832 UsefulBuf_Compare(ArrayItem.label.string,
6833 UsefulBuf_FROM_SZ_LITERAL("map in a map"))) {
6834 return 2052;
6835 }
6836
Laurence Lundblade937ea812020-05-08 11:38:23 -07006837
Laurence Lundblade9b334962020-08-27 10:55:53 -07006838 QCBORDecode_Init(&DCtx, ValidEncodedMap, 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006839 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade64b607e2020-05-13 13:05:57 -07006840 QCBORDecode_GetNext(&DCtx, &Item1);
6841 QCBORDecode_GetNext(&DCtx, &Item1);
6842 QCBORDecode_GetNext(&DCtx, &Item1);
Laurence Lundblade937ea812020-05-08 11:38:23 -07006843 QCBORDecode_EnterMapFromMapSZ(&DCtx, "map in a map");
6844 QCBORDecode_GetNext(&DCtx, &Item1);
6845 if(Item1.uDataType != QCBOR_TYPE_BYTE_STRING) {
6846 return 2003;
6847 }
6848
Laurence Lundblade9b334962020-08-27 10:55:53 -07006849 QCBORDecode_Init(&DCtx, ValidEncodedMap, 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006850 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade937ea812020-05-08 11:38:23 -07006851 QCBORDecode_GetNext(&DCtx, &Item1);
6852 QCBORDecode_GetNext(&DCtx, &Item1);
6853 QCBORDecode_GetNext(&DCtx, &Item1);
6854 QCBORDecode_GetNext(&DCtx, &Item1);
6855 QCBORDecode_GetNext(&DCtx, &Item1);
6856 QCBORDecode_GetNext(&DCtx, &Item1);
6857 QCBORDecode_GetNext(&DCtx, &Item1);
6858 QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings");
6859 QCBORDecode_GetNext(&DCtx, &Item1);
6860 if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING) {
Laurence Lundblade64b607e2020-05-13 13:05:57 -07006861 return 2004;
Laurence Lundblade937ea812020-05-08 11:38:23 -07006862 }
6863
Laurence Lundblade9b334962020-08-27 10:55:53 -07006864 QCBORDecode_Init(&DCtx, ValidEncodedMap, 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006865 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade2b843b52020-06-16 20:51:03 -07006866 QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings");
6867 QCBORDecode_ExitArray(&DCtx);
6868 QCBORDecode_GetNext(&DCtx, &Item1);
6869 if(Item1.uDataType != QCBOR_TYPE_MAP && Item1.uLabelAlloc != QCBOR_TYPE_TEXT_STRING) {
6870 return 2006;
6871 }
6872 QCBORDecode_ExitMap(&DCtx);
6873 if(QCBORDecode_GetNext(&DCtx, &Item1) != QCBOR_ERR_NO_MORE_ITEMS) {
6874 return 2007;
6875 }
6876
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07006877 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSimpleArray), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006878 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07006879 int64_t nDecodedInt2;
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07006880 QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2);
6881 uErr = QCBORDecode_GetAndResetError(&DCtx);
6882 if(uErr != QCBOR_ERR_MAP_NOT_ENTERED){
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07006883 return 2008;
6884 }
6885 UsefulBufC String;
Laurence Lundblade323f8a92020-09-06 19:43:09 -07006886 QCBORDecode_GetTextStringInMapN(&DCtx, 88, &String);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07006887 if(uErr != QCBOR_ERR_MAP_NOT_ENTERED){
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07006888 return 2009;
6889 }
Laurence Lundblade937ea812020-05-08 11:38:23 -07006890
Laurence Lundbladee6f15112020-07-23 18:44:16 -07006891
6892 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEmptyMap), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006893 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundbladee6f15112020-07-23 18:44:16 -07006894 // This will fail because the map is empty.
6895 QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2);
6896 uErr = QCBORDecode_GetAndResetError(&DCtx);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006897 if(uErr != QCBOR_ERR_LABEL_NOT_FOUND){
Laurence Lundbladee6f15112020-07-23 18:44:16 -07006898 return 2010;
6899 }
6900 QCBORDecode_ExitMap(&DCtx);
6901 uErr = QCBORDecode_Finish(&DCtx);
6902 if(uErr != QCBOR_SUCCESS){
6903 return 2011;
6904 }
6905
6906
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08006907#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundbladee6f15112020-07-23 18:44:16 -07006908 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEmptyInDefinteLengthMap), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006909 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundbladee6f15112020-07-23 18:44:16 -07006910 // This will fail because the map is empty.
6911 QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2);
6912 uErr = QCBORDecode_GetAndResetError(&DCtx);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006913 if(uErr != QCBOR_ERR_LABEL_NOT_FOUND){
Laurence Lundblade085d7952020-07-24 10:26:30 -07006914 return 2012;
Laurence Lundbladee6f15112020-07-23 18:44:16 -07006915 }
6916 QCBORDecode_ExitMap(&DCtx);
6917 uErr = QCBORDecode_Finish(&DCtx);
6918 if(uErr != QCBOR_SUCCESS){
Laurence Lundblade085d7952020-07-24 10:26:30 -07006919 return 2013;
Laurence Lundbladee6f15112020-07-23 18:44:16 -07006920 }
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08006921#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
Laurence Lundbladee6f15112020-07-23 18:44:16 -07006922
Laurence Lundblade5f4e8712020-07-25 11:44:43 -07006923
6924 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spArrayOfEmpty), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006925 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade323f8a92020-09-06 19:43:09 -07006926 QCBORDecode_GetByteString(&DCtx, &String);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006927 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade5f4e8712020-07-25 11:44:43 -07006928 QCBORDecode_ExitMap(&DCtx);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006929 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade5f4e8712020-07-25 11:44:43 -07006930 QCBORDecode_ExitArray(&DCtx);
6931 QCBORDecode_GetInt64(&DCtx, &nDecodedInt2);
6932 QCBORDecode_ExitArray(&DCtx);
6933 uErr = QCBORDecode_Finish(&DCtx);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07006934 if(uErr != QCBOR_SUCCESS) {
Laurence Lundblade5f4e8712020-07-25 11:44:43 -07006935 return 2014;
6936 }
6937
Laurence Lundblade93d3f532020-09-28 21:09:12 -07006938 int64_t nInt;
6939 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spRecoverableMapErrors), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006940 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade37286c02022-09-03 10:05:02 -07006941#ifndef QCBOR_DISABLE_TAGS
Laurence Lundblade93d3f532020-09-28 21:09:12 -07006942 QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt);
Laurence Lundblade88e9db22020-11-02 03:56:33 -08006943 uErr = QCBORDecode_GetError(&DCtx);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07006944 if(uErr != QCBOR_ERR_TOO_MANY_TAGS) {
6945 return 2021;
6946 }
Laurence Lundblade88e9db22020-11-02 03:56:33 -08006947 if(QCBORDecode_GetNthTagOfLast(&DCtx, 0) != CBOR_TAG_INVALID64) {
6948 return 2121;
6949 }
6950 (void)QCBORDecode_GetAndResetError(&DCtx);
Laurence Lundblade37286c02022-09-03 10:05:02 -07006951#endif
Laurence Lundblade88e9db22020-11-02 03:56:33 -08006952
Laurence Lundblade93d3f532020-09-28 21:09:12 -07006953
Laurence Lundblade93d3f532020-09-28 21:09:12 -07006954 QCBORDecode_GetInt64InMapN(&DCtx, 0x03, &nInt);
6955 uErr = QCBORDecode_GetAndResetError(&DCtx);
6956 if(uErr != QCBOR_ERR_INT_OVERFLOW) {
6957 return 2023;
6958 }
6959
Laurence Lundblade37286c02022-09-03 10:05:02 -07006960#ifndef QCBOR_DISABLE_TAGS
Laurence Lundblade93d3f532020-09-28 21:09:12 -07006961 QCBORDecode_GetEpochDateInMapN(&DCtx, 0x04, QCBOR_TAG_REQUIREMENT_TAG, &nInt);
6962 uErr = QCBORDecode_GetAndResetError(&DCtx);
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006963 if(uErr != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_DATE_OVERFLOW)) {
Laurence Lundblade93d3f532020-09-28 21:09:12 -07006964 return 2024;
6965 }
Laurence Lundblade37286c02022-09-03 10:05:02 -07006966#endif
Laurence Lundblade93d3f532020-09-28 21:09:12 -07006967
6968 QCBORDecode_GetInt64InMapN(&DCtx, 0x05, &nInt);
6969 uErr = QCBORDecode_GetAndResetError(&DCtx);
6970 if(uErr != QCBOR_ERR_DUPLICATE_LABEL) {
6971 return 2025;
6972 }
6973
6974 QCBORDecode_GetInt64InMapN(&DCtx, 0x08, &nInt);
6975
6976 QCBORDecode_ExitMap(&DCtx);
6977 uErr = QCBORDecode_Finish(&DCtx);
6978 if(uErr != QCBOR_SUCCESS) {
6979 return 2026;
6980 }
6981
6982 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUnRecoverableMapError1), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006983 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07006984 QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt);
6985 uErr = QCBORDecode_GetAndResetError(&DCtx);
6986 if(uErr != QCBOR_ERR_BAD_BREAK) {
6987 return 2030;
6988 }
6989
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08006990#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundblade93d3f532020-09-28 21:09:12 -07006991 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUnRecoverableMapError2), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006992 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07006993 QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt);
6994 uErr = QCBORDecode_GetAndResetError(&DCtx);
6995 if(uErr != QCBOR_ERR_NO_MORE_ITEMS) {
6996 return 2031;
6997 }
6998
6999 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUnRecoverableMapError3), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07007000 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07007001 QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt);
7002 uErr = QCBORDecode_GetAndResetError(&DCtx);
7003 if(uErr != QCBOR_ERR_HIT_END) {
7004 return 2032;
7005 }
7006
7007 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUnRecoverableMapError4), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07007008 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07007009 QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt);
7010 uErr = QCBORDecode_GetAndResetError(&DCtx);
7011 if(uErr != QCBOR_ERR_ARRAY_DECODE_NESTING_TOO_DEEP) {
7012 return 2033;
7013 }
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08007014#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
7015
Laurence Lundblade732e52d2021-02-22 20:11:01 -07007016 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0);
7017 QCBORDecode_VGetNextConsume(&DCtx, &Item1);
7018 if(Item1.uDataType != QCBOR_TYPE_MAP) {
7019 return 2401;
7020 }
7021 if(QCBORDecode_GetError(&DCtx)) {
7022 return 2402;
7023 }
7024
7025 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0);
7026 QCBORDecode_VGetNext(&DCtx, &Item1);
7027 if(Item1.uDataType != QCBOR_TYPE_MAP ||
7028 Item1.val.uCount != 3 ||
7029 Item1.uNextNestLevel != 1) {
7030 return 2403;
7031 }
7032 if(QCBORDecode_GetError(&DCtx)) {
7033 return 2404;
7034 }
7035 QCBORDecode_VGetNextConsume(&DCtx, &Item1);
7036 if(Item1.uDataType != QCBOR_TYPE_INT64 ||
7037 Item1.uNextNestLevel != 1 ||
7038 Item1.val.int64 != 42) {
7039 return 2405;
7040 }
7041 if(QCBORDecode_GetError(&DCtx)) {
7042 return 2406;
7043 }
7044 QCBORDecode_VGetNextConsume(&DCtx, &Item1);
7045 if(Item1.uDataType != QCBOR_TYPE_ARRAY ||
7046 Item1.uNestingLevel != 1 ||
7047 Item1.uNextNestLevel != 1 ||
7048 Item1.val.uCount != 2) {
7049 return 2407;
7050 }
7051 if(QCBORDecode_GetError(&DCtx)) {
7052 return 2408;
7053 }
7054 QCBORDecode_VGetNextConsume(&DCtx, &Item1);
7055 if(Item1.uDataType != QCBOR_TYPE_MAP ||
7056 Item1.uNestingLevel != 1 ||
7057 Item1.uNextNestLevel != 0 ||
7058 Item1.val.uCount != 4) {
7059 return 2409;
7060 }
7061 if(QCBORDecode_GetError(&DCtx)) {
7062 return 2410;
7063 }
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07007064
Laurence Lundbladeddebabe2020-11-22 11:32:17 -08007065 nReturn = DecodeNestedIterate();
7066
Laurence Lundblade63926052021-03-29 16:05:51 -07007067
7068 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(not_well_formed_submod_section), 0);
7069 QCBORDecode_EnterMap(&DCtx, NULL);
7070 QCBORDecode_EnterMapFromMapN(&DCtx, 20);
7071 if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_BAD_INT) {
7072 return 2500;
7073 }
7074
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07007075 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBadConsumeInput), 0);
7076 QCBORDecode_VGetNextConsume(&DCtx, &Item1);
7077 if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_NO_MORE_ITEMS) {
7078 return 2600;
7079 }
7080
Laurence Lundblade37286c02022-09-03 10:05:02 -07007081#ifndef QCBOR_DISABLE_TAGS
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07007082 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBadConsumeInput2), 0);
7083 QCBORDecode_VGetNextConsume(&DCtx, &Item1);
7084 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) {
7085 return 2700;
7086 }
Laurence Lundblade37286c02022-09-03 10:05:02 -07007087#endif
7088
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07007089
7090 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBadConsumeInput4), 0);
7091 QCBORDecode_VGetNextConsume(&DCtx, &Item1);
7092#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
7093 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) {
7094 return 2900;
7095 }
7096#else
7097 if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_INDEF_LEN_ARRAYS_DISABLED) {
7098 return 2901;
7099 }
7100#endif
7101
7102 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBadConsumeInput5), 0);
7103 QCBORDecode_VGetNextConsume(&DCtx, &Item1);
7104 if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_MAP_LABEL_TYPE) {
7105 return 3000;
7106 }
7107
Laurence Lundbladec5f45e42023-12-18 09:23:20 -07007108 nReturn = EnterMapCursorTest();
7109
Laurence Lundbladeddebabe2020-11-22 11:32:17 -08007110 return nReturn;
Laurence Lundblade9c905e82020-04-25 11:31:38 -07007111}
7112
7113
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007114struct NumberConversion {
7115 char *szDescription;
7116 UsefulBufC CBOR;
7117 int64_t nConvertedToInt64;
7118 QCBORError uErrorInt64;
7119 uint64_t uConvertToUInt64;
7120 QCBORError uErrorUint64;
7121 double dConvertToDouble;
7122 QCBORError uErrorDouble;
7123};
7124
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007125#ifndef QCBOR_DISABLE_EXP_AND_MANTISSA
7126#define EXP_AND_MANTISSA_ERROR(x) x
7127#else
7128#define EXP_AND_MANTISSA_ERROR(x) QCBOR_ERR_UNEXPECTED_TYPE
7129#endif
7130
7131
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07007132static const struct NumberConversion NumberConversions[] = {
Laurence Lundblade37286c02022-09-03 10:05:02 -07007133#ifndef QCBOR_DISABLE_TAGS
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007134 {
Laurence Lundblade4e808ba2022-12-29 12:45:20 -07007135 "Big float: INT64_MIN * 2e-1 to test handling of INT64_MIN",
7136 {(uint8_t[]){0xC5, 0x82, 0x20,
7137 0x3B, 0x7f, 0xff, 0xff, 0xff, 0xff, 0x0ff, 0xff, 0xff,
7138 }, 15},
7139 -4611686018427387904, /* INT64_MIN / 2 */
7140 EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS),
7141 0,
7142 EXP_AND_MANTISSA_ERROR(QCBOR_ERR_NUMBER_SIGN_CONVERSION),
7143 -4.6116860184273879E+18,
7144 FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS))
7145 },
7146 {
Laurence Lundblade784b54b2020-08-10 01:24:52 -07007147 "too large to fit into int64_t",
7148 {(uint8_t[]){0xc3, 0x48, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 10},
7149 0,
7150 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
7151 0,
7152 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
7153 ((double)INT64_MIN) + 1 ,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007154 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)
Laurence Lundblade784b54b2020-08-10 01:24:52 -07007155 },
7156 {
7157 "largest negative int that fits in int64_t",
7158 {(uint8_t[]){0xc3, 0x48, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, 10},
7159 INT64_MIN,
7160 QCBOR_SUCCESS,
7161 0,
7162 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
7163 (double)INT64_MIN,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007164 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)
Laurence Lundblade784b54b2020-08-10 01:24:52 -07007165 },
7166 {
Laurence Lundbladeda095972020-06-06 18:35:33 -07007167 "negative bignum -1",
7168 {(uint8_t[]){0xc3, 0x41, 0x00}, 3},
7169 -1,
7170 QCBOR_SUCCESS,
7171 0,
7172 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
7173 -1.0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007174 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)
Laurence Lundbladeda095972020-06-06 18:35:33 -07007175 },
7176 {
Laurence Lundblade9ab5abb2020-05-20 12:10:45 -07007177 "Decimal Fraction with positive bignum 257 * 10e3",
Laurence Lundblade887add82020-05-17 05:50:34 -07007178 {(uint8_t[]){0xC4, 0x82, 0x1B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03,
7179 0xC2, 0x42, 0x01, 0x01}, 15},
7180 257000,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007181 EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS),
Laurence Lundblade887add82020-05-17 05:50:34 -07007182 257000,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007183 EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS),
Laurence Lundblade887add82020-05-17 05:50:34 -07007184 257000.0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007185 FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS))
Laurence Lundblade887add82020-05-17 05:50:34 -07007186 },
7187 {
Laurence Lundbladeda095972020-06-06 18:35:33 -07007188 "bigfloat with negative bignum -258 * 2e3",
Laurence Lundblade887add82020-05-17 05:50:34 -07007189 {(uint8_t[]){0xC5, 0x82, 0x1B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03,
7190 0xC3, 0x42, 0x01, 0x01}, 15},
Laurence Lundbladeda095972020-06-06 18:35:33 -07007191 -2064,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007192 EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS),
Laurence Lundblade887add82020-05-17 05:50:34 -07007193 0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007194 EXP_AND_MANTISSA_ERROR(QCBOR_ERR_NUMBER_SIGN_CONVERSION),
Laurence Lundbladeda095972020-06-06 18:35:33 -07007195 -2064.0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007196 FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS))
Laurence Lundblade887add82020-05-17 05:50:34 -07007197 },
7198 {
Laurence Lundblade9ab5abb2020-05-20 12:10:45 -07007199 "bigfloat with positive bignum 257 * 2e3",
Laurence Lundblade887add82020-05-17 05:50:34 -07007200 {(uint8_t[]){0xC5, 0x82, 0x1B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03,
7201 0xC2, 0x42, 0x01, 0x01}, 15},
7202 2056,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007203 EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS),
Laurence Lundblade887add82020-05-17 05:50:34 -07007204 2056,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007205 EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS),
Laurence Lundblade887add82020-05-17 05:50:34 -07007206 2056.0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007207 FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS))
Laurence Lundblade887add82020-05-17 05:50:34 -07007208 },
7209 {
Laurence Lundbladeda095972020-06-06 18:35:33 -07007210 "negative bignum 0xc349010000000000000000 -18446744073709551617",
Laurence Lundblade887add82020-05-17 05:50:34 -07007211 {(uint8_t[]){0xc3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 11},
7212 0,
7213 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
7214 0,
7215 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
7216 -18446744073709551617.0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007217 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)
Laurence Lundblade887add82020-05-17 05:50:34 -07007218 },
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08007219#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
Laurence Lundblade887add82020-05-17 05:50:34 -07007220 {
Laurence Lundblade313b2862020-05-16 01:23:06 -07007221 "Positive bignum 0x01020304 indefinite length string",
7222 {(uint8_t[]){0xC2, 0x5f, 0x42, 0x01, 0x02, 0x41, 0x03, 0x41, 0x04, 0xff}, 10},
7223 0x01020304,
7224 QCBOR_SUCCESS,
7225 0x01020304,
7226 QCBOR_SUCCESS,
7227 16909060.0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007228 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)
Laurence Lundblade313b2862020-05-16 01:23:06 -07007229 },
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08007230#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
Laurence Lundblade313b2862020-05-16 01:23:06 -07007231 {
Laurence Lundblade887add82020-05-17 05:50:34 -07007232 "Decimal Fraction with neg bignum [9223372036854775807, -4759477275222530853137]",
Laurence Lundblade313b2862020-05-16 01:23:06 -07007233 {(uint8_t[]){0xC4, 0x82, 0x1B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
7234 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,}, 23},
7235 0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007236 EXP_AND_MANTISSA_ERROR(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW),
Laurence Lundblade313b2862020-05-16 01:23:06 -07007237 0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007238 EXP_AND_MANTISSA_ERROR(QCBOR_ERR_NUMBER_SIGN_CONVERSION),
Laurence Lundblade313b2862020-05-16 01:23:06 -07007239 -INFINITY,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007240 FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS))
Laurence Lundblade313b2862020-05-16 01:23:06 -07007241 },
7242 {
7243 "big float [9223372036854775806, 9223372036854775806]",
7244 {(uint8_t[]){0xC5, 0x82, 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE,
7245 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE}, 20},
7246 0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007247 EXP_AND_MANTISSA_ERROR(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW),
Laurence Lundblade313b2862020-05-16 01:23:06 -07007248 0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007249 EXP_AND_MANTISSA_ERROR(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW),
Laurence Lundblade313b2862020-05-16 01:23:06 -07007250 INFINITY,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007251 FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS))
Laurence Lundblade313b2862020-05-16 01:23:06 -07007252 },
7253 {
Laurence Lundblade983500d2020-05-14 11:49:34 -07007254 "Big float 3 * 2^^2",
7255 {(uint8_t[]){0xC5, 0x82, 0x02, 0x03}, 4},
7256 12,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007257 EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS),
Laurence Lundblade983500d2020-05-14 11:49:34 -07007258 12,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007259 EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS),
Laurence Lundblade983500d2020-05-14 11:49:34 -07007260 12.0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007261 FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS))
Laurence Lundblade983500d2020-05-14 11:49:34 -07007262 },
Laurence Lundblade983500d2020-05-14 11:49:34 -07007263 {
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007264 "Decimal fraction 3/10",
7265 {(uint8_t[]){0xC4, 0x82, 0x20, 0x03}, 4},
7266 0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007267 EXP_AND_MANTISSA_ERROR(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW),
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007268 0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007269 EXP_AND_MANTISSA_ERROR(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW),
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007270 0.30000000000000004,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007271 FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS))
Laurence Lundbladedfd49fc2020-09-01 14:17:16 -07007272 },
7273 {
Laurence Lundblade11fd78b2020-09-01 22:13:27 -07007274 "extreme pos bignum",
7275 {(uint8_t[]){0xc2, 0x59, 0x01, 0x90,
7276 // 50 rows of 8 is 400 digits.
7277 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7278 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7279 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7280 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7281 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7282 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7283 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7284 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7285 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7286 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7287 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7288 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7289 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7290 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7291 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7292 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7293 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7294 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7295 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7296 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7297 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7298 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7299 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7300 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7301 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7302 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7303 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7304 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7305 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7306 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7307 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7308 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7309 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7310 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7311 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7312 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7313 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7314 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7315 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7316 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7317 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7318 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7319 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7320 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7321 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7322 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7323 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7324 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7325 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
gtravisb787e82a2023-11-30 18:38:21 -08007326 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0},
Laurence Lundblade11fd78b2020-09-01 22:13:27 -07007327 404},
7328 0,
7329 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
7330 0,
7331 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
Laurence Lundblade11fd78b2020-09-01 22:13:27 -07007332 INFINITY,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007333 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS),
Laurence Lundblade11fd78b2020-09-01 22:13:27 -07007334 },
7335
7336 {
7337 "extreme neg bignum",
7338 {(uint8_t[]){0xc3, 0x59, 0x01, 0x90,
7339 // 50 rows of 8 is 400 digits.
7340 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7341 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7342 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7343 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7344 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7345 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7346 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7347 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7348 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7349 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7350 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7351 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7352 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7353 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7354 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7355 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7356 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7357 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7358 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7359 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7360 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7361 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7362 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7363 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7364 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7365 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7366 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7367 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7368 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7369 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7370 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7371 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7372 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7373 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7374 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7375 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7376 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7377 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7378 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7379 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7380 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7381 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7382 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7383 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7384 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7385 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7386 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7387 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
7388 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
gtravisb787e82a2023-11-30 18:38:21 -08007389 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0},
Laurence Lundblade11fd78b2020-09-01 22:13:27 -07007390 404},
7391 0,
7392 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
7393 0,
7394 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
Laurence Lundblade11fd78b2020-09-01 22:13:27 -07007395 -INFINITY,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007396 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)
Laurence Lundblade11fd78b2020-09-01 22:13:27 -07007397 },
Laurence Lundblade51722fd2020-09-02 13:01:33 -07007398
7399 {
7400 "big float underflow [9223372036854775806, -9223372036854775806]",
7401 {(uint8_t[]){
7402 0xC5, 0x82,
7403 0x3B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE,
7404 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE}, 20},
Laurence Lundblade51722fd2020-09-02 13:01:33 -07007405 0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007406 EXP_AND_MANTISSA_ERROR(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW),
Laurence Lundblade51722fd2020-09-02 13:01:33 -07007407 0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007408 EXP_AND_MANTISSA_ERROR(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW),
Laurence Lundblade51722fd2020-09-02 13:01:33 -07007409 0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007410 FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS))
Laurence Lundblade51722fd2020-09-02 13:01:33 -07007411 },
7412
7413 {
7414 "bigfloat that evaluates to -INFINITY",
7415 {(uint8_t[]){
7416 0xC5, 0x82,
7417 0x1B, 0x70, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03,
7418 0xC3, 0x42, 0x01, 0x01}, 15},
Laurence Lundblade51722fd2020-09-02 13:01:33 -07007419 0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007420 EXP_AND_MANTISSA_ERROR(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW),
Laurence Lundblade51722fd2020-09-02 13:01:33 -07007421 0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007422 EXP_AND_MANTISSA_ERROR(QCBOR_ERR_NUMBER_SIGN_CONVERSION),
Laurence Lundblade51722fd2020-09-02 13:01:33 -07007423 -INFINITY,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007424 FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS))
Laurence Lundblade51722fd2020-09-02 13:01:33 -07007425 },
Laurence Lundblade37286c02022-09-03 10:05:02 -07007426 {
7427 "Positive bignum 0xffff",
7428 {(uint8_t[]){0xC2, 0x42, 0xff, 0xff}, 4},
7429 65536-1,
7430 QCBOR_SUCCESS,
7431 0xffff,
7432 QCBOR_SUCCESS,
7433 65535.0,
7434 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)
7435 },
7436#endif /* QCBOR_DISABLE_TAGS */
7437 {
7438 "Positive integer 18446744073709551615",
7439 {(uint8_t[]){0x1b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, 9},
7440 0,
7441 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
7442 18446744073709551615ULL,
7443 QCBOR_SUCCESS,
7444 18446744073709551615.0,
7445 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)
7446 },
7447
7448 {
7449 "Postive integer 0",
7450 {(uint8_t[]){0x0}, 1},
7451 0LL,
7452 QCBOR_SUCCESS,
7453 0ULL,
7454 QCBOR_SUCCESS,
7455 0.0,
7456 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)
7457 },
7458 {
7459 "Negative integer -18446744073709551616",
7460 {(uint8_t[]){0x3b, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }, 9},
7461 -9223372036854775807-1, // INT64_MIN
7462 QCBOR_SUCCESS,
7463 0ULL,
7464 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
7465 -9223372036854775808.0,
7466 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)
7467 },
7468 {
7469 "Double Floating point value 100.3",
7470 {(uint8_t[]){0xfb, 0x40, 0x59, 0x13, 0x33, 0x33, 0x33, 0x33, 0x33}, 9},
7471 100L,
7472 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS),
7473 100ULL,
7474 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS),
7475 100.3,
7476 FLOAT_ERR_CODE_NO_FLOAT(QCBOR_SUCCESS),
7477 },
7478 {
7479 "Floating point value NaN 0xfa7fc00000",
7480 {(uint8_t[]){0xfa, 0x7f, 0xc0, 0x00, 0x00}, 5},
7481 0,
7482 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_FLOAT_EXCEPTION),
7483 0,
7484 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_FLOAT_EXCEPTION),
7485 NAN,
7486 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS),
7487 },
7488 {
7489 "half-precision Floating point value -4",
7490 {(uint8_t[]){0xf9, 0xc4, 0x00}, 3},
7491 // Normal case with all enabled.
7492 -4,
7493 FLOAT_ERR_CODE_NO_HALF_PREC_NO_FLOAT_HW(QCBOR_SUCCESS),
7494 0,
7495 FLOAT_ERR_CODE_NO_HALF_PREC_NO_FLOAT_HW(QCBOR_ERR_NUMBER_SIGN_CONVERSION),
7496 -4.0,
7497 FLOAT_ERR_CODE_NO_HALF_PREC(QCBOR_SUCCESS)
7498 },
7499 {
7500 "+inifinity single precision",
7501 {(uint8_t[]){0xfa, 0x7f, 0x80, 0x00, 0x00}, 5},
7502 0,
7503 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_FLOAT_EXCEPTION),
7504 0,
7505 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW),
7506 INFINITY,
7507 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)
7508 },
7509
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007510};
Laurence Lundblade9c905e82020-04-25 11:31:38 -07007511
7512
7513
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08007514
7515static int32_t SetUpDecoder(QCBORDecodeContext *DCtx, UsefulBufC CBOR, UsefulBuf Pool)
7516{
7517 QCBORDecode_Init(DCtx, CBOR, QCBOR_DECODE_MODE_NORMAL);
7518#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
7519 if(QCBORDecode_SetMemPool(DCtx, Pool, 0)) {
7520 return 1;
7521 }
7522#else /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
7523 (void)Pool;
7524#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
7525 return 0;
7526}
7527
7528
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03007529int32_t IntegerConvertTest(void)
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007530{
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08007531 const int nNumTests = C_ARRAY_COUNT(NumberConversions,
7532 struct NumberConversion);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007533
Laurence Lundblade9ab5abb2020-05-20 12:10:45 -07007534 for(int nIndex = 0; nIndex < nNumTests; nIndex++) {
7535 const struct NumberConversion *pF = &NumberConversions[nIndex];
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07007536
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007537 // Set up the decoding context including a memory pool so that
7538 // indefinite length items can be checked
7539 QCBORDecodeContext DCtx;
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007540 UsefulBuf_MAKE_STACK_UB(Pool, 100);
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07007541
7542 /* ----- test conversion to int64_t ------ */
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08007543 if(SetUpDecoder(&DCtx, pF->CBOR, Pool)) {
7544 return (int32_t)(3333+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007545 }
7546
7547 int64_t nInt;
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007548 QCBORDecode_GetInt64ConvertAll(&DCtx, 0xffff, &nInt);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07007549 if(QCBORDecode_GetError(&DCtx) != pF->uErrorInt64) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07007550 return (int32_t)(2000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007551 }
7552 if(pF->uErrorInt64 == QCBOR_SUCCESS && pF->nConvertedToInt64 != nInt) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07007553 return (int32_t)(3000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007554 }
7555
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07007556 /* ----- test conversion to uint64_t ------ */
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08007557 if(SetUpDecoder(&DCtx, pF->CBOR, Pool)) {
7558 return (int32_t)(3333+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007559 }
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08007560
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007561 uint64_t uInt;
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007562 QCBORDecode_GetUInt64ConvertAll(&DCtx, 0xffff, &uInt);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07007563 if(QCBORDecode_GetError(&DCtx) != pF->uErrorUint64) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07007564 return (int32_t)(4000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007565 }
7566 if(pF->uErrorUint64 == QCBOR_SUCCESS && pF->uConvertToUInt64 != uInt) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07007567 return (int32_t)(5000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007568 }
7569
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07007570 /* ----- test conversion to double ------ */
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08007571 if(SetUpDecoder(&DCtx, pF->CBOR, Pool)) {
7572 return (int32_t)(3333+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007573 }
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007574
7575#ifndef USEFULBUF_DISABLE_ALL_FLOAT
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007576 double d;
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007577 QCBORDecode_GetDoubleConvertAll(&DCtx, 0xffff, &d);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07007578 if(QCBORDecode_GetError(&DCtx) != pF->uErrorDouble) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07007579 return (int32_t)(6000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007580 }
7581 if(pF->uErrorDouble == QCBOR_SUCCESS) {
7582 if(isnan(pF->dConvertToDouble)) {
Laurence Lundblade983500d2020-05-14 11:49:34 -07007583 // NaN's can't be compared for equality. A NaN is
7584 // never equal to anything including another NaN
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007585 if(!isnan(d)) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07007586 return (int32_t)(7000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007587 }
7588 } else {
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007589 if(pF->dConvertToDouble != d) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07007590 return (int32_t)(8000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007591 }
7592 }
7593 }
Laurence Lundblade16a207a2021-09-18 17:22:46 -07007594#endif /* USEFULBUF_DISABLE_ALL_FLOAT */
Laurence Lundbladef6c86662020-05-12 02:08:00 -07007595 }
7596
7597 return 0;
7598}
7599
Laurence Lundbladea8758502022-05-15 17:57:46 -07007600#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
7601
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03007602int32_t CBORTestIssue134(void)
David Navarro9123e5b2022-03-28 16:04:03 +02007603{
7604 QCBORDecodeContext DCtx;
7605 QCBORItem Item;
7606 QCBORError uCBORError;
7607 const uint8_t spTestIssue134[] = { 0x5F, 0x40, 0xFF };
Laurence Lundblade9c905e82020-04-25 11:31:38 -07007608
David Navarro9123e5b2022-03-28 16:04:03 +02007609 QCBORDecode_Init(&DCtx,
7610 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTestIssue134),
7611 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade97c61bf2020-05-02 11:24:06 -07007612
David Navarro9123e5b2022-03-28 16:04:03 +02007613 UsefulBuf_MAKE_STACK_UB(StringBuf, 200);
7614 QCBORDecode_SetMemPool(&DCtx, StringBuf, false);
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03007615
David Navarro9123e5b2022-03-28 16:04:03 +02007616 do {
7617 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
7618 } while (QCBOR_SUCCESS == uCBORError);
7619
7620 uCBORError = QCBORDecode_Finish(&DCtx);
7621
Laurence Lundblade11ea3612022-07-01 13:26:23 -07007622 return (int32_t)uCBORError;
David Navarro9123e5b2022-03-28 16:04:03 +02007623}
Laurence Lundblade97c61bf2020-05-02 11:24:06 -07007624
Laurence Lundbladea8758502022-05-15 17:57:46 -07007625#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
7626
Laurence Lundblade37286c02022-09-03 10:05:02 -07007627
7628
7629static const uint8_t spSequenceTestInput[] = {
7630 /* 1. The valid date string "1985-04-12" */
7631 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string
7632
7633 /* 2. */
7634 0x00,
7635
7636 /* 3. A valid epoch date, 1400000000; Tue, 13 May 2014 16:53:20 GMT */
7637 0x1a, 0x53, 0x72, 0x4E, 0x00,
7638
7639 /* 4. */
7640 0x62, 'h', 'i',
7641};
7642
7643
Laurence Lundbladee3553422020-05-02 11:11:17 -07007644int32_t CBORSequenceDecodeTests(void)
7645{
7646 QCBORDecodeContext DCtx;
Laurence Lundblade87495732021-02-26 10:05:55 -07007647 QCBORItem Item;
7648 QCBORError uCBORError;
7649 size_t uConsumed;
Laurence Lundbladee3553422020-05-02 11:11:17 -07007650
7651 // --- Test a sequence with extra bytes ---
Laurence Lundblade9b334962020-08-27 10:55:53 -07007652
Laurence Lundbladee3553422020-05-02 11:11:17 -07007653 QCBORDecode_Init(&DCtx,
Laurence Lundblade37286c02022-09-03 10:05:02 -07007654 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSequenceTestInput),
Laurence Lundbladee3553422020-05-02 11:11:17 -07007655 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade9b334962020-08-27 10:55:53 -07007656
Laurence Lundblade37286c02022-09-03 10:05:02 -07007657 // Get 1.
Laurence Lundbladee3553422020-05-02 11:11:17 -07007658 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
7659 if(uCBORError != QCBOR_SUCCESS) {
7660 return 1;
7661 }
Laurence Lundblade37286c02022-09-03 10:05:02 -07007662 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ) {
Laurence Lundbladee3553422020-05-02 11:11:17 -07007663 return 2;
7664 }
Laurence Lundbladec7114722020-08-13 05:11:40 -07007665
Laurence Lundblade87495732021-02-26 10:05:55 -07007666 uCBORError = QCBORDecode_PartialFinish(&DCtx, &uConsumed);
7667 if(uCBORError != QCBOR_ERR_EXTRA_BYTES ||
Laurence Lundblade37286c02022-09-03 10:05:02 -07007668 uConsumed != 11) {
7669 return 102;
7670 }
7671
7672 // Get 2.
7673 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
7674 if(uCBORError != QCBOR_SUCCESS) {
7675 return 66;
7676 }
7677
7678 uCBORError = QCBORDecode_PartialFinish(&DCtx, &uConsumed);
7679 if(uCBORError != QCBOR_ERR_EXTRA_BYTES ||
Laurence Lundblade87495732021-02-26 10:05:55 -07007680 uConsumed != 12) {
7681 return 102;
7682 }
7683
Laurence Lundblade37286c02022-09-03 10:05:02 -07007684 // Get 3.
Laurence Lundbladec7114722020-08-13 05:11:40 -07007685 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundbladee3553422020-05-02 11:11:17 -07007686 if(uCBORError != QCBOR_SUCCESS) {
7687 return 2;
7688 }
Laurence Lundblade37286c02022-09-03 10:05:02 -07007689 if(Item.uDataType != QCBOR_TYPE_INT64) {
Laurence Lundbladee3553422020-05-02 11:11:17 -07007690 return 3;
7691 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07007692
Laurence Lundbladee3553422020-05-02 11:11:17 -07007693 // A sequence can have stuff at the end that may
7694 // or may not be valid CBOR. The protocol decoder knows
7695 // when to stop by definition of the protocol, not
7696 // when the top-level map or array is ended.
7697 // Finish still has to be called to know that
7698 // maps and arrays (if there were any) were closed
7699 // off correctly. When called like this it
7700 // must return the error QCBOR_ERR_EXTRA_BYTES.
7701 uCBORError = QCBORDecode_Finish(&DCtx);
7702 if(uCBORError != QCBOR_ERR_EXTRA_BYTES) {
7703 return 4;
7704 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07007705
Laurence Lundbladee3553422020-05-02 11:11:17 -07007706 // --- Test an empty input ----
7707 uint8_t empty[1];
7708 UsefulBufC Empty = {empty, 0};
7709 QCBORDecode_Init(&DCtx,
7710 Empty,
7711 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade9b334962020-08-27 10:55:53 -07007712
Laurence Lundbladee3553422020-05-02 11:11:17 -07007713 uCBORError = QCBORDecode_Finish(&DCtx);
7714 if(uCBORError != QCBOR_SUCCESS) {
7715 return 5;
7716 }
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07007717
7718
Laurence Lundbladee3553422020-05-02 11:11:17 -07007719 // --- Sequence with unclosed indefinite length array ---
7720 static const uint8_t xx[] = {0x01, 0x9f, 0x02};
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07007721
Laurence Lundbladee3553422020-05-02 11:11:17 -07007722 QCBORDecode_Init(&DCtx,
7723 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(xx),
7724 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07007725
Laurence Lundbladee3553422020-05-02 11:11:17 -07007726 // Get the first item
7727 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
7728 if(uCBORError != QCBOR_SUCCESS) {
7729 return 7;
7730 }
7731 if(Item.uDataType != QCBOR_TYPE_INT64) {
7732 return 8;
7733 }
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07007734
Laurence Lundbladee3553422020-05-02 11:11:17 -07007735 // Get a second item
7736 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08007737#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundbladee3553422020-05-02 11:11:17 -07007738 if(uCBORError != QCBOR_SUCCESS) {
7739 return 9;
7740 }
7741 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
7742 return 10;
7743 }
7744
7745 // Try to finish before consuming all bytes to confirm
7746 // that the still-open error is returned.
7747 uCBORError = QCBORDecode_Finish(&DCtx);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07007748 if(uCBORError != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
Laurence Lundbladee3553422020-05-02 11:11:17 -07007749 return 11;
7750 }
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08007751#else /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
7752 if(uCBORError != QCBOR_ERR_INDEF_LEN_ARRAYS_DISABLED) {
7753 return 20;
7754 }
7755#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
Laurence Lundbladee3553422020-05-02 11:11:17 -07007756
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07007757
Laurence Lundbladee3553422020-05-02 11:11:17 -07007758 // --- Sequence with a closed indefinite length array ---
7759 static const uint8_t yy[] = {0x01, 0x9f, 0xff};
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07007760
Laurence Lundbladee3553422020-05-02 11:11:17 -07007761 QCBORDecode_Init(&DCtx,
7762 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(yy),
7763 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07007764
Laurence Lundbladee3553422020-05-02 11:11:17 -07007765 // Get the first item
7766 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
7767 if(uCBORError != QCBOR_SUCCESS) {
7768 return 12;
7769 }
7770 if(Item.uDataType != QCBOR_TYPE_INT64) {
7771 return 13;
7772 }
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07007773
Laurence Lundbladee3553422020-05-02 11:11:17 -07007774 // Get a second item
7775 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08007776#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
7777
Laurence Lundbladee3553422020-05-02 11:11:17 -07007778 if(uCBORError != QCBOR_SUCCESS) {
7779 return 14;
7780 }
7781 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
7782 return 15;
7783 }
7784
7785 // Try to finish before consuming all bytes to confirm
7786 // that the still-open error is returned.
7787 uCBORError = QCBORDecode_Finish(&DCtx);
7788 if(uCBORError != QCBOR_SUCCESS) {
7789 return 16;
7790 }
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08007791#else /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
7792 if(uCBORError != QCBOR_ERR_INDEF_LEN_ARRAYS_DISABLED) {
7793 return 20;
7794 }
7795#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
Laurence Lundbladee3553422020-05-02 11:11:17 -07007796
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07007797
Laurence Lundbladee3553422020-05-02 11:11:17 -07007798 return 0;
7799}
7800
Laurence Lundbladee15326f2020-06-15 15:50:23 -07007801
Laurence Lundblade70ecead2020-06-15 19:40:06 -07007802
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03007803int32_t IntToTests(void)
Laurence Lundbladee15326f2020-06-15 15:50:23 -07007804{
7805 int nErrCode;
7806 int32_t n32;
7807 int16_t n16;
7808 int8_t n8;
7809 uint32_t u32;
7810 uint16_t u16;
7811 uint8_t u8;
7812 uint64_t u64;
7813
7814 nErrCode = QCBOR_Int64ToInt32(1, &n32);
7815 if(nErrCode == -1 || n32 != 1) {
7816 return 1;
7817 }
7818
7819 nErrCode = QCBOR_Int64ToInt32((int64_t)INT32_MAX, &n32);
7820 if(nErrCode == -1 || n32 != INT32_MAX) {
7821 return 2;
7822 }
7823
7824 nErrCode = QCBOR_Int64ToInt32((int64_t)INT32_MIN, &n32);
7825 if(nErrCode == -1 || n32 != INT32_MIN) {
7826 return 3;
7827 }
7828
7829 nErrCode = QCBOR_Int64ToInt32(((int64_t)INT32_MAX)+1, &n32);
7830 if(nErrCode != -1) {
7831 return 4;
7832 }
7833
7834 nErrCode = QCBOR_Int64ToInt32(((int64_t)INT32_MIN)-1, &n32);
7835 if(nErrCode != -1) {
7836 return 5;
7837 }
7838
7839
7840 nErrCode = QCBOR_Int64ToInt16((int64_t)INT16_MAX, &n16);
7841 if(nErrCode == -1 || n16 != INT16_MAX) {
7842 return 6;
7843 }
7844
7845 nErrCode = QCBOR_Int64ToInt16((int64_t)INT16_MIN, &n16);
7846 if(nErrCode == -1 || n16 != INT16_MIN) {
7847 return 7;
7848 }
7849
7850 nErrCode = QCBOR_Int64ToInt16(1, &n16);
7851 if(nErrCode == -1 || n16 != 1) {
7852 return 8;
7853 }
7854
7855 nErrCode = QCBOR_Int64ToInt16(((int64_t)INT16_MAX)+1, &n16);
7856 if(nErrCode != -1) {
7857 return 9;
7858 }
7859
7860 nErrCode = QCBOR_Int64ToInt16(((int64_t)INT16_MIN)-1, &n16);
7861 if(nErrCode != -1) {
7862 return 10;
7863 }
7864
7865
7866 nErrCode = QCBOR_Int64ToInt8(1, &n8);
7867 if(nErrCode == -1 || n8 != 1) {
7868 return 11;
7869 }
7870
7871 nErrCode = QCBOR_Int64ToInt8((int64_t)INT8_MAX, &n8);
7872 if(nErrCode == -1 || n8 != INT8_MAX) {
7873 return 12;
7874 }
7875
7876 nErrCode = QCBOR_Int64ToInt8((int64_t)INT8_MIN, &n8);
7877 if(nErrCode == -1 || n8 != INT8_MIN) {
7878 return 13;
7879 }
7880
7881 nErrCode = QCBOR_Int64ToInt8(((int64_t)INT8_MAX)+1, &n8);
7882 if(nErrCode != -1) {
7883 return 14;
7884 }
7885
7886 nErrCode = QCBOR_Int64ToInt8(((int64_t)INT8_MIN)-1, &n8);
7887 if(nErrCode != -1) {
7888 return 15;
7889 }
7890
7891
7892 nErrCode = QCBOR_Int64ToUInt32(1, &u32);
7893 if(nErrCode == -1 || u32 != 1) {
7894 return 16;
7895 }
7896
7897 nErrCode = QCBOR_Int64ToUInt32((int64_t)UINT32_MAX, &u32);
7898 if(nErrCode == -1 || u32 != UINT32_MAX) {
7899 return 17;
7900 }
7901
7902 nErrCode = QCBOR_Int64ToUInt32((int64_t)0, &u32);
7903 if(nErrCode == -1 || u32 != 0) {
7904 return 18;
7905 }
7906
7907 nErrCode = QCBOR_Int64ToUInt32(((int64_t)UINT32_MAX)+1, &u32);
7908 if(nErrCode != -1) {
7909 return 19;
7910 }
7911
7912 nErrCode = QCBOR_Int64ToUInt32((int64_t)-1, &u32);
7913 if(nErrCode != -1) {
7914 return 20;
7915 }
7916
7917
7918 nErrCode = QCBOR_Int64UToInt16((int64_t)UINT16_MAX, &u16);
7919 if(nErrCode == -1 || u16 != UINT16_MAX) {
7920 return 21;
7921 }
7922
7923 nErrCode = QCBOR_Int64UToInt16((int64_t)0, &u16);
7924 if(nErrCode == -1 || u16 != 0) {
7925 return 22;
7926 }
7927
7928 nErrCode = QCBOR_Int64UToInt16(1, &u16);
7929 if(nErrCode == -1 || u16 != 1) {
7930 return 23;
7931 }
7932
7933 nErrCode = QCBOR_Int64UToInt16(((int64_t)UINT16_MAX)+1, &u16);
7934 if(nErrCode != -1) {
7935 return 24;
7936 }
7937
7938 nErrCode = QCBOR_Int64UToInt16((int64_t)-1, &u16);
7939 if(nErrCode != -1) {
7940 return 25;
7941 }
7942
7943
7944 nErrCode = QCBOR_Int64ToUInt8((int64_t)UINT8_MAX, &u8);
7945 if(nErrCode == -1 || u8 != UINT8_MAX) {
7946 return 26;
7947 }
7948
7949 nErrCode = QCBOR_Int64ToUInt8((int64_t)0, &u8);
7950 if(nErrCode == -1 || u8 != 0) {
7951 return 27;
7952 }
7953
7954 nErrCode = QCBOR_Int64ToUInt8(1, &u8);
7955 if(nErrCode == -1 || u8 != 1) {
7956 return 28;
7957 }
7958
7959 nErrCode = QCBOR_Int64ToUInt8(((int64_t)UINT16_MAX)+1, &u8);
7960 if(nErrCode != -1) {
7961 return 29;
7962 }
7963
7964 nErrCode = QCBOR_Int64ToUInt8((int64_t)-1, &u8);
7965 if(nErrCode != -1) {
7966 return 30;
7967 }
7968
7969
7970 nErrCode = QCBOR_Int64ToUInt64(1, &u64);
7971 if(nErrCode == -1 || u64 != 1) {
7972 return 31;
7973 }
7974
7975 nErrCode = QCBOR_Int64ToUInt64(INT64_MAX, &u64);
7976 if(nErrCode == -1 || u64 != INT64_MAX) {
7977 return 32;
7978 }
7979
7980 nErrCode = QCBOR_Int64ToUInt64((int64_t)0, &u64);
7981 if(nErrCode == -1 || u64 != 0) {
7982 return 33;
7983 }
7984
7985 nErrCode = QCBOR_Int64ToUInt64((int64_t)-1, &u64);
7986 if(nErrCode != -1) {
7987 return 34;
7988 }
7989
7990 return 0;
7991}
7992
Laurence Lundblade0750fc42020-06-20 21:02:34 -07007993
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07007994
7995
Laurence Lundblade0750fc42020-06-20 21:02:34 -07007996/*
7997A sequence with
7998 A wrapping bstr
7999 containing a map
8000 1
8001 2
8002 A wrapping bstr
8003 containing an array
8004 3
8005 wrapping bstr
8006 4
8007 5
8008 6
8009 array
8010 7
8011 8
Laurence Lundblade0750fc42020-06-20 21:02:34 -07008012 */
8013
Laurence Lundblade55013642020-09-23 05:39:22 -07008014static UsefulBufC EncodeBstrWrapTestData(UsefulBuf OutputBuffer)
Laurence Lundblade0750fc42020-06-20 21:02:34 -07008015{
Laurence Lundblade55013642020-09-23 05:39:22 -07008016 UsefulBufC Encoded;
Laurence Lundblade0750fc42020-06-20 21:02:34 -07008017 QCBOREncodeContext EC;
Laurence Lundblade55013642020-09-23 05:39:22 -07008018 QCBORError uErr;
Laurence Lundblade0750fc42020-06-20 21:02:34 -07008019
Laurence Lundblade55013642020-09-23 05:39:22 -07008020 QCBOREncode_Init(&EC, OutputBuffer);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07008021
8022 QCBOREncode_BstrWrap(&EC);
8023 QCBOREncode_OpenMap(&EC);
8024 QCBOREncode_AddInt64ToMapN(&EC, 100, 1);
8025 QCBOREncode_AddInt64ToMapN(&EC, 200, 2);
8026 QCBOREncode_CloseMap(&EC);
8027 QCBOREncode_BstrWrap(&EC);
8028 QCBOREncode_OpenArray(&EC);
8029 QCBOREncode_AddInt64(&EC, 3);
8030 QCBOREncode_BstrWrap(&EC);
8031 QCBOREncode_AddInt64(&EC, 4);
8032 QCBOREncode_CloseBstrWrap(&EC, NULL);
8033 QCBOREncode_AddInt64(&EC, 5);
8034 QCBOREncode_CloseArray(&EC);
8035 QCBOREncode_CloseBstrWrap(&EC, NULL);
8036 QCBOREncode_AddInt64(&EC, 6);
8037 QCBOREncode_CloseBstrWrap(&EC, NULL);
8038 QCBOREncode_OpenArray(&EC);
8039 QCBOREncode_AddInt64(&EC, 7);
8040 QCBOREncode_AddInt64(&EC, 8);
8041 QCBOREncode_CloseArray(&EC);
8042
8043 uErr = QCBOREncode_Finish(&EC, &Encoded);
Laurence Lundblade40a04322020-06-27 22:52:52 -07008044 if(uErr) {
8045 Encoded = NULLUsefulBufC;
8046 }
Laurence Lundblade0750fc42020-06-20 21:02:34 -07008047
8048 return Encoded;
8049}
8050
Laurence Lundbladecc7da412020-12-27 00:09:07 -08008051/* h'FF' */
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08008052static const uint8_t spBreakInByteString[] = {
8053 0x41, 0xff
8054};
8055
Laurence Lundblade0750fc42020-06-20 21:02:34 -07008056
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03008057int32_t EnterBstrTest(void)
Laurence Lundblade0750fc42020-06-20 21:02:34 -07008058{
Laurence Lundblade8510f8c2020-12-01 11:31:16 -08008059 UsefulBuf_MAKE_STACK_UB(OutputBuffer, 100);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07008060
8061 QCBORDecodeContext DC;
8062
Laurence Lundblade55013642020-09-23 05:39:22 -07008063 QCBORDecode_Init(&DC, EncodeBstrWrapTestData(OutputBuffer), 0);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07008064
Laurence Lundblade55013642020-09-23 05:39:22 -07008065 int64_t n1, n2, n3, n4, n5, n6, n7, n8;
Laurence Lundblade0750fc42020-06-20 21:02:34 -07008066
8067
Laurence Lundblade9b334962020-08-27 10:55:53 -07008068 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07008069 QCBORDecode_EnterMap(&DC, NULL);
Laurence Lundblade55013642020-09-23 05:39:22 -07008070 QCBORDecode_GetInt64InMapN(&DC, 100, &n1);
8071 QCBORDecode_GetInt64InMapN(&DC, 200, &n2);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07008072 QCBORDecode_ExitMap(&DC);
Laurence Lundblade9b334962020-08-27 10:55:53 -07008073 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07008074 QCBORDecode_EnterArray(&DC, NULL);
Laurence Lundblade55013642020-09-23 05:39:22 -07008075 QCBORDecode_GetInt64(&DC, &n3);
Laurence Lundblade9b334962020-08-27 10:55:53 -07008076 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
Laurence Lundblade55013642020-09-23 05:39:22 -07008077 QCBORDecode_GetInt64(&DC, &n4);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07008078 QCBORDecode_ExitBstrWrapped(&DC);
Laurence Lundblade55013642020-09-23 05:39:22 -07008079 QCBORDecode_GetInt64(&DC, &n5);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07008080 QCBORDecode_ExitArray(&DC);
8081 QCBORDecode_ExitBstrWrapped(&DC);
Laurence Lundblade55013642020-09-23 05:39:22 -07008082 QCBORDecode_GetInt64(&DC, &n6);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07008083 QCBORDecode_ExitBstrWrapped(&DC);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07008084 QCBORDecode_EnterArray(&DC, NULL);
Laurence Lundblade55013642020-09-23 05:39:22 -07008085 QCBORDecode_GetInt64(&DC, &n7);
8086 QCBORDecode_GetInt64(&DC, &n8);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07008087 QCBORDecode_ExitArray(&DC);
8088
8089 QCBORError uErr = QCBORDecode_Finish(&DC);
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08008090 if(uErr) {
8091 return (int32_t)uErr;
8092 }
Laurence Lundblade0750fc42020-06-20 21:02:34 -07008093
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08008094
8095 /* Enter and exit byte string wrapped CBOR that is bad. It has just a break.
8096 * Successful because no items are fetched from byte string.
8097 */
8098 QCBORDecode_Init(&DC,
8099 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBreakInByteString),
8100 0);
8101 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
8102 uErr = QCBORDecode_GetError(&DC);
8103 if(uErr) {
8104 return 100 + (int32_t)uErr;
8105 }
8106
8107 QCBORDecode_ExitBstrWrapped(&DC);
8108 uErr = QCBORDecode_GetError(&DC);
8109 if(uErr) {
8110 return 200 + (int32_t)uErr;
8111 }
8112
8113 /* Try to get item that is a break out of a byte string wrapped CBOR.
8114 * It fails because there should be no break.
8115 */
8116 QCBORDecode_Init(&DC,
8117 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBreakInByteString),
8118 0);
8119 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
8120 QCBORItem Item;
8121 uErr = QCBORDecode_GetNext(&DC, &Item);
8122 if(uErr != QCBOR_ERR_BAD_BREAK) {
8123 return 300 + (int32_t)uErr;
8124 }
8125
8126 return 0;
Laurence Lundblade0750fc42020-06-20 21:02:34 -07008127}
Laurence Lundblade37f46e52020-08-04 03:32:14 -07008128
8129
8130
8131
8132static const uint8_t spTaggedTypes[] = {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008133 0xb2,
Laurence Lundblade37f46e52020-08-04 03:32:14 -07008134
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07008135 // Date string
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008136 0x00,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07008137 0xc0, 0x74, 0x32, 0x30, 0x30, 0x33, 0x2D, 0x31, 0x32, 0x2D,
8138 0x31, 0x33, 0x54, 0x31, 0x38, 0x3A, 0x33, 0x30, 0x3A, 0x30,
8139 0x32, 0x5A,
Laurence Lundblade37f46e52020-08-04 03:32:14 -07008140
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008141 0x01,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07008142 0x74, 0x32, 0x30, 0x30, 0x33, 0x2D, 0x31, 0x32, 0x2D, 0x31,
8143 0x33, 0x54, 0x31, 0x38, 0x3A, 0x33, 0x30, 0x3A, 0x30, 0x32,
8144 0x5A,
Laurence Lundblade37f46e52020-08-04 03:32:14 -07008145
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07008146 // Bignum
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008147 10,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07008148 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
8149 0x09, 0x10,
Laurence Lundblade37f46e52020-08-04 03:32:14 -07008150
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008151 11,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07008152 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
8153 0x09, 0x10,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008154
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07008155 // URL
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008156 20,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07008157 0xd8, 0x20, 0x6f, 0x68, 0x74, 0x74, 0x70, 0x3A, 0x2F, 0x2F,
8158 0x63, 0x62, 0x6F, 0x72, 0x2E, 0x6D, 0x65, 0x2F,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008159
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07008160 21,
8161 0x6f, 0x68, 0x74, 0x74, 0x70, 0x3A, 0x2F, 0x2F, 0x63, 0x62,
8162 0x6F, 0x72, 0x2E, 0x6D, 0x65, 0x2F,
8163
8164 // B64
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008165 0x18, 0x1e,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07008166 0xd8, 0x22, 0x6c, 0x63, 0x47, 0x78, 0x6C, 0x59, 0x58, 0x4E,
8167 0x31, 0x63, 0x6D, 0x55, 0x75,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008168
8169 0x18, 0x1f,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07008170 0x6c, 0x63, 0x47, 0x78, 0x6C, 0x59, 0x58, 0x4E, 0x31, 0x63,
8171 0x6D, 0x55, 0x75,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008172
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07008173 // B64URL
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008174 0x18, 0x28,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07008175 0xd8, 0x21, 0x6c, 0x63, 0x47, 0x78, 0x6C, 0x59, 0x58, 0x4E,
8176 0x31, 0x63, 0x6D, 0x55, 0x75,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008177
8178 0x18, 0x29,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07008179 0x6c, 0x63, 0x47, 0x78, 0x6C, 0x59, 0x58, 0x4E, 0x31, 0x63,
8180 0x6D, 0x55, 0x75,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008181
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07008182 // Regex
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008183 0x18, 0x32,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07008184 0xd8, 0x23, 0x68, 0x31, 0x30, 0x30, 0x5C, 0x73, 0x2A, 0x6D,
8185 0x6B,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008186
8187 0x18, 0x33,
8188 0x68, 0x31, 0x30, 0x30, 0x5C, 0x73, 0x2A, 0x6D, 0x6B,
8189
8190 // MIME
8191 0x18, 0x3c,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07008192 0xd8, 0x24, 0x72, 0x4D, 0x49, 0x4D, 0x45, 0x2D, 0x56, 0x65,
8193 0x72, 0x73, 0x69, 0x6F, 0x6E, 0x3A, 0x20, 0x31, 0x2E, 0x30,
8194 0x0A,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008195
8196 0x18, 0x3d,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07008197 0x72, 0x4D, 0x49, 0x4D, 0x45, 0x2D, 0x56, 0x65, 0x72, 0x73,
8198 0x69, 0x6F, 0x6E, 0x3A, 0x20, 0x31, 0x2E, 0x30, 0x0A,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008199
8200 0x18, 0x3e,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07008201 0xd9, 0x01, 0x01, 0x52, 0x4D, 0x49, 0x4D, 0x45, 0x2D, 0x56,
8202 0x65, 0x72, 0x73, 0x69, 0x6F, 0x6E, 0x3A, 0x20, 0x31, 0x2E,
8203 0x30, 0x0A,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008204
8205 0x18, 0x3f,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07008206 0x52, 0x4D, 0x49, 0x4D, 0x45, 0x2D, 0x56, 0x65, 0x72, 0x73,
8207 0x69, 0x6F, 0x6E, 0x3A, 0x20, 0x31, 0x2E, 0x30, 0x0A,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008208
8209 // UUID
8210 0x18, 0x46,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07008211 0xd8, 0x25, 0x50, 0x53, 0x4D, 0x41, 0x52, 0x54, 0x43, 0x53,
8212 0x4C, 0x54, 0x54, 0x43, 0x46, 0x49, 0x43, 0x41, 0x32,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008213
8214 0x18, 0x47,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07008215 0x50, 0x53, 0x4D, 0x41, 0x52, 0x54, 0x43, 0x53, 0x4C, 0x54,
8216 0x54, 0x43, 0x46, 0x49, 0x43, 0x41, 0x32
Laurence Lundblade37f46e52020-08-04 03:32:14 -07008217};
8218
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03008219int32_t DecodeTaggedTypeTests(void)
Laurence Lundblade37f46e52020-08-04 03:32:14 -07008220{
8221 QCBORDecodeContext DC;
8222 QCBORError uErr;
8223
8224 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTaggedTypes), 0);
8225
8226 UsefulBufC String;
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008227 bool bNeg;
Laurence Lundblade37f46e52020-08-04 03:32:14 -07008228
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07008229 QCBORDecode_EnterMap(&DC, NULL);
Laurence Lundblade9b334962020-08-27 10:55:53 -07008230 QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008231 QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &String);
Laurence Lundblade37f46e52020-08-04 03:32:14 -07008232 if(QCBORDecode_GetError(&DC) != QCBOR_SUCCESS) {
8233 return 1;
8234 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07008235 QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade37f46e52020-08-04 03:32:14 -07008236 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_UNEXPECTED_TYPE) {
8237 return 2;
8238 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07008239 QCBORDecode_GetDateStringInMapN(&DC, 1, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade37f46e52020-08-04 03:32:14 -07008240 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_UNEXPECTED_TYPE) {
8241 return 3;
8242 }
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008243 QCBORDecode_GetDateStringInMapN(&DC, 1, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &String);
Laurence Lundblade9b334962020-08-27 10:55:53 -07008244 QCBORDecode_GetDateStringInMapN(&DC, 1, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade37f46e52020-08-04 03:32:14 -07008245 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
8246 return 4;
8247 }
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008248 QCBORDecode_GetDateStringInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07008249 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008250 return 5;
8251 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07008252
Laurence Lundblade9b334962020-08-27 10:55:53 -07008253 QCBORDecode_GetBignumInMapN(&DC, 10, QCBOR_TAG_REQUIREMENT_TAG, &String, &bNeg);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008254 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
8255 bNeg != false) {
8256 return 10;
8257 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07008258 QCBORDecode_GetBignumInMapN(&DC, 11, QCBOR_TAG_REQUIREMENT_TAG, &String, &bNeg);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008259 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
8260 bNeg != true) {
8261 return 11;
8262 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07008263 QCBORDecode_GetBignumInMapN(&DC, 11, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bNeg);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008264 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_UNEXPECTED_TYPE) {
8265 return 12;
8266 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07008267 QCBORDecode_GetBignumInMapN(&DC, 14, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bNeg);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07008268 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008269 return 13;
8270 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07008271 QCBORDecode_GetBignumInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bNeg);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07008272 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008273 return 14;
8274 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07008275
Laurence Lundblade9b334962020-08-27 10:55:53 -07008276 QCBORDecode_GetURIInMapN(&DC, 20, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008277 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
8278 return 20;
8279 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07008280 QCBORDecode_GetURIInMapN(&DC, 21, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008281 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
8282 return 21;
8283 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07008284 QCBORDecode_GetURIInMapN(&DC, 22, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07008285 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008286 return 22;
8287 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07008288 QCBORDecode_GetURIInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07008289 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008290 return 23;
8291 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07008292
Laurence Lundblade24bd7e12021-01-09 00:05:11 -08008293#ifndef QCBOR_DISABLE_UNCOMMON_TAGS
Laurence Lundblade9b334962020-08-27 10:55:53 -07008294 QCBORDecode_GetB64InMapN(&DC, 30, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008295 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
8296 return 30;
8297 }
Laurence Lundblade24bd7e12021-01-09 00:05:11 -08008298#endif
Laurence Lundblade9b334962020-08-27 10:55:53 -07008299 QCBORDecode_GetB64InMapN(&DC, 31, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008300 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
8301 return 31;
8302 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07008303 QCBORDecode_GetB64InMapN(&DC, 32, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07008304 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008305 return 32;
8306 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07008307 QCBORDecode_GetB64InMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07008308 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008309 return 33;
8310 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07008311
Laurence Lundblade24bd7e12021-01-09 00:05:11 -08008312#ifndef QCBOR_DISABLE_UNCOMMON_TAGS
Laurence Lundblade9b334962020-08-27 10:55:53 -07008313 QCBORDecode_GetB64URLInMapN(&DC, 40, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008314 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
8315 return 40;
8316 }
Laurence Lundblade24bd7e12021-01-09 00:05:11 -08008317#endif
Laurence Lundblade9b334962020-08-27 10:55:53 -07008318 QCBORDecode_GetB64URLInMapN(&DC, 41, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008319 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
8320 return 41;
8321 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07008322 QCBORDecode_GetB64URLInMapN(&DC, 42, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07008323 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008324 return 42;
8325 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07008326 QCBORDecode_GetB64URLInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07008327 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008328 return 43;
8329 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07008330
Laurence Lundblade24bd7e12021-01-09 00:05:11 -08008331#ifndef QCBOR_DISABLE_UNCOMMON_TAGS
Laurence Lundblade9b334962020-08-27 10:55:53 -07008332 QCBORDecode_GetRegexInMapN(&DC, 50, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008333 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
8334 return 50;
8335 }
Laurence Lundblade24bd7e12021-01-09 00:05:11 -08008336#endif
Laurence Lundblade9b334962020-08-27 10:55:53 -07008337 QCBORDecode_GetRegexInMapN(&DC, 51, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008338 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
8339 return 51;
8340 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07008341 QCBORDecode_GetRegexInMapN(&DC, 52, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07008342 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008343 return 52;
8344 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07008345 QCBORDecode_GetRegexInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07008346 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008347 return 53;
8348 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07008349
Laurence Lundblade24bd7e12021-01-09 00:05:11 -08008350#ifndef QCBOR_DISABLE_UNCOMMON_TAGS
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008351 // MIME
8352 bool bIsNot7Bit;
Laurence Lundblade9b334962020-08-27 10:55:53 -07008353 QCBORDecode_GetMIMEMessageInMapN(&DC, 60, QCBOR_TAG_REQUIREMENT_TAG, &String, &bIsNot7Bit);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008354 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
8355 bIsNot7Bit == true) {
8356 return 60;
8357 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07008358 QCBORDecode_GetMIMEMessageInMapN(&DC, 61, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bIsNot7Bit);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008359 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
8360 bIsNot7Bit == true) {
8361 return 61;
8362 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07008363 QCBORDecode_GetMIMEMessageInMapN(&DC, 62, QCBOR_TAG_REQUIREMENT_TAG, &String, &bIsNot7Bit);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008364 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
8365 bIsNot7Bit == false) {
8366 return 62;
8367 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07008368 QCBORDecode_GetMIMEMessageInMapN(&DC, 63, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bIsNot7Bit);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008369 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
8370 bIsNot7Bit == false) {
8371 return 63;
8372 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07008373 QCBORDecode_GetMIMEMessageInMapN(&DC, 64, QCBOR_TAG_REQUIREMENT_TAG, &String, &bNeg);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07008374 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008375 return 64;
8376 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07008377 QCBORDecode_GetMIMEMessageInMapSZ(&DC, "zzz", QCBOR_TAG_REQUIREMENT_TAG, &String, &bNeg);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07008378 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008379 return 65;
8380 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07008381
Laurence Lundblade24bd7e12021-01-09 00:05:11 -08008382
Laurence Lundblade9b334962020-08-27 10:55:53 -07008383 QCBORDecode_GetBinaryUUIDInMapN(&DC, 70, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008384 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
8385 return 70;
8386 }
Laurence Lundblade24bd7e12021-01-09 00:05:11 -08008387#endif /* #ifndef QCBOR_DISABLE_UNCOMMON_TAGS */
8388
Laurence Lundblade9b334962020-08-27 10:55:53 -07008389 QCBORDecode_GetBinaryUUIDInMapN(&DC, 71, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008390 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
8391 return 71;
8392 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07008393 QCBORDecode_GetBinaryUUIDInMapN(&DC, 72, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07008394 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008395 return 72;
8396 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07008397 QCBORDecode_GetBinaryUUIDInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07008398 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07008399 return 73;
8400 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07008401
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07008402 // Improvement: add some more error test cases
8403
Laurence Lundblade37f46e52020-08-04 03:32:14 -07008404 QCBORDecode_ExitMap(&DC);
8405
8406 uErr = QCBORDecode_Finish(&DC);
8407 if(uErr != QCBOR_SUCCESS) {
8408 return 100;
8409 }
8410
8411 return 0;
8412}
Laurence Lundbladea4308a82020-10-03 18:08:57 -07008413
8414
8415
8416
8417/*
Laurence Lundbladecc7da412020-12-27 00:09:07 -08008418 [
8419 "aaaaaaaaaa",
8420 {}
8421 ]
Laurence Lundbladea4308a82020-10-03 18:08:57 -07008422 */
8423static const uint8_t spTooLarge1[] = {
8424 0x9f,
8425 0x6a, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61,
8426 0xa0,
8427 0xff
8428};
8429
8430/*
Laurence Lundbladecc7da412020-12-27 00:09:07 -08008431 [
8432 {
8433 0: "aaaaaaaaaa"
8434 }
8435 ]
Laurence Lundbladea4308a82020-10-03 18:08:57 -07008436 */
8437static const uint8_t spTooLarge2[] = {
8438 0x9f,
8439 0xa1,
8440 0x00,
8441 0x6a, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61,
8442 0xff
8443};
8444
8445/*
Laurence Lundbladecc7da412020-12-27 00:09:07 -08008446 h'A1006A61616161616161616161'
Laurence Lundbladea4308a82020-10-03 18:08:57 -07008447
Laurence Lundbladecc7da412020-12-27 00:09:07 -08008448 {
8449 0: "aaaaaaaaaa"
8450 }
Laurence Lundbladea4308a82020-10-03 18:08:57 -07008451 */
8452static const uint8_t spTooLarge3[] = {
8453 0x4d,
8454 0xa1,
8455 0x00,
8456 0x6a, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61,
8457};
8458
8459int32_t TooLargeInputTest(void)
8460{
8461 QCBORDecodeContext DC;
8462 QCBORError uErr;
8463 UsefulBufC String;
8464
8465 // These tests require a build with QCBOR_MAX_DECODE_INPUT_SIZE set
8466 // to 10 There's not really any way to test this error
8467 // condition. The error condition is not complex, so setting
8468 // QCBOR_MAX_DECODE_INPUT_SIZE gives an OK test.
8469
8470 // The input CBOR is only too large because the
8471 // QCBOR_MAX_DECODE_INPUT_SIZE is 10.
8472 //
8473 // This test is disabled for the normal test runs because of the
8474 // special build requirement.
8475
8476
8477 // Tests the start of a map being too large
8478 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTooLarge1), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07008479 QCBORDecode_EnterArray(&DC, NULL);
Laurence Lundbladea4308a82020-10-03 18:08:57 -07008480 QCBORDecode_GetTextString(&DC, &String);
8481 uErr = QCBORDecode_GetError(&DC);
8482 if(uErr != QCBOR_SUCCESS) {
8483 return 1;
8484 }
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07008485 QCBORDecode_EnterMap(&DC, NULL);
Laurence Lundbladea4308a82020-10-03 18:08:57 -07008486 uErr = QCBORDecode_GetError(&DC);
8487 if(uErr != QCBOR_ERR_INPUT_TOO_LARGE) {
8488 return 2;
8489 }
8490
8491 // Tests the end of a map being too large
8492 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTooLarge2), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07008493 QCBORDecode_EnterArray(&DC, NULL);
8494 QCBORDecode_EnterMap(&DC, NULL);
Laurence Lundbladea4308a82020-10-03 18:08:57 -07008495 uErr = QCBORDecode_GetError(&DC);
8496 if(uErr != QCBOR_SUCCESS) {
8497 return 3;
8498 }
8499 QCBORDecode_ExitMap(&DC);
8500 uErr = QCBORDecode_GetError(&DC);
8501 if(uErr != QCBOR_ERR_INPUT_TOO_LARGE) {
8502 return 4;
8503 }
8504
8505 // Tests the entire input CBOR being too large when processing bstr wrapping
8506 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTooLarge3), QCBOR_DECODE_MODE_NORMAL);
8507 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
8508 uErr = QCBORDecode_GetError(&DC);
8509 if(uErr != QCBOR_ERR_INPUT_TOO_LARGE) {
8510 return 5;
8511 }
8512
8513 return 0;
8514}
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07008515
8516
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08008517#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
8518
Laurence Lundblade37286c02022-09-03 10:05:02 -07008519/*
8520 An array of three map entries
8521 1) Indefinite length string label for indefinite lenght byte string
8522 2) Indefinite length string label for an integer
8523 3) Indefinite length string label for an indefinite-length negative big num
8524 */
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07008525static const uint8_t spMapWithIndefLenStrings[] = {
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08008526 0xa3,
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07008527 0x7f, 0x61, 'l', 0x64, 'a', 'b', 'e', 'l' , 0x61, '1', 0xff,
8528 0x5f, 0x42, 0x01, 0x02, 0x43, 0x03, 0x04, 0x05, 0xff,
8529 0x7f, 0x62, 'd', 'y', 0x61, 'm', 0x61, 'o', 0xff,
8530 0x03,
8531 0x7f, 0x62, 'l', 'a', 0x63, 'b', 'e', 'l', 0x61, '2', 0xff,
8532 0xc3,
8533 0x5f, 0x42, 0x00, 0x01, 0x42, 0x00, 0x01, 0x41, 0x01, 0xff,
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07008534};
8535
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03008536int32_t SpiffyIndefiniteLengthStringsTests(void)
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07008537{
8538 QCBORDecodeContext DCtx;
8539
8540 QCBORDecode_Init(&DCtx,
8541 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spMapWithIndefLenStrings),
8542 QCBOR_DECODE_MODE_NORMAL);
8543
Laurence Lundblade8510f8c2020-12-01 11:31:16 -08008544 UsefulBuf_MAKE_STACK_UB(StringBuf, 200);
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07008545 QCBORDecode_SetMemPool(&DCtx, StringBuf, false);
8546
8547 UsefulBufC ByteString;
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07008548 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07008549 QCBORDecode_GetByteStringInMapSZ(&DCtx, "label1", &ByteString);
Laurence Lundblade37286c02022-09-03 10:05:02 -07008550
8551#ifndef QCBOR_DISABLE_TAGS
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07008552 if(QCBORDecode_GetAndResetError(&DCtx)) {
8553 return 1;
8554 }
8555
8556 const uint8_t pExectedBytes[] = {0x01, 0x02, 0x03, 0x04, 0x05};
8557 if(UsefulBuf_Compare(ByteString, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pExectedBytes))) {
8558 return 2;
8559 }
8560
8561 uint64_t uInt;
8562 QCBORDecode_GetUInt64InMapSZ(&DCtx, "dymo", &uInt);
8563 if(QCBORDecode_GetAndResetError(&DCtx)) {
8564 return 3;
8565 }
8566 if(uInt != 3) {
8567 return 4;
8568 }
8569
Máté Tóth-Pálef5f07a2021-09-17 19:31:37 +02008570#ifndef USEFULBUF_DISABLE_ALL_FLOAT
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07008571 double uDouble;
8572 QCBORDecode_GetDoubleConvertAllInMapSZ(&DCtx,
8573 "label2",
8574 0xff,
8575 &uDouble);
Laurence Lundblade37286c02022-09-03 10:05:02 -07008576
Laurence Lundbladeb8e19aa2020-10-07 20:59:11 -07008577#ifndef QCBOR_DISABLE_FLOAT_HW_USE
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07008578 if(QCBORDecode_GetAndResetError(&DCtx)) {
8579 return 5;
8580 }
8581 if(uDouble != -16777474) {
8582 return 6;
8583 }
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08008584#else /* QCBOR_DISABLE_FLOAT_HW_USE */
Laurence Lundbladeb8e19aa2020-10-07 20:59:11 -07008585 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_HW_FLOAT_DISABLED) {
8586 return 7;
8587 }
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08008588#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
Máté Tóth-Pálef5f07a2021-09-17 19:31:37 +02008589#endif /* USEFULBUF_DISABLE_ALL_FLOAT */
Laurence Lundbladeb8e19aa2020-10-07 20:59:11 -07008590
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07008591 QCBORDecode_ExitMap(&DCtx);
8592
8593 if(QCBORDecode_Finish(&DCtx)) {
8594 return 99;
8595 }
8596
Laurence Lundblade37286c02022-09-03 10:05:02 -07008597#else /* QCBOR_DISABLE_TAGS */
8598 /* The big num in the input is a CBOR tag and you can't do
8599 * map lookups in a map with a tag so this test does very little
8600 * when tags are disabled. That is OK, the test coverage is still
8601 * good when they are not.
8602 */
8603 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_TAGS_DISABLED) {
8604 return 1002;
8605 }
8606#endif /*QCBOR_DISABLE_TAGS */
8607
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07008608 return 0;
8609}
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08008610#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
Laurence Lundblade2a26abb2020-11-05 19:06:54 -08008611
8612
Laurence Lundbladecf41c522021-02-20 10:19:07 -07008613/*
8614 * An array of an integer and an array. The second array contains
8615 * a bstr-wrapped map.
8616 *
8617 * [7, [h'A36D6669... (see next lines) 73']]
8618 *
8619 * {"first integer": 42,
8620 * "an array of two strings": ["string1", "string2"],
8621 * "map in a map":
8622 * { "bytes 1": h'78787878',
8623 * "bytes 2": h'79797979',
8624 * "another int": 98,
8625 * "text 2": "lies, damn lies and statistics"
8626 * }
8627 * }
8628 */
Laurence Lundblade2a26abb2020-11-05 19:06:54 -08008629
Laurence Lundbladecf41c522021-02-20 10:19:07 -07008630static const uint8_t pValidWrappedMapEncoded[] = {
8631 0x82, 0x07, 0x81, 0x58, 0x97,
8632 0xa3, 0x6d, 0x66, 0x69, 0x72, 0x73, 0x74, 0x20, 0x69, 0x6e,
8633 0x74, 0x65, 0x67, 0x65, 0x72, 0x18, 0x2a, 0x77, 0x61, 0x6e,
8634 0x20, 0x61, 0x72, 0x72, 0x61, 0x79, 0x20, 0x6f, 0x66, 0x20,
8635 0x74, 0x77, 0x6f, 0x20, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67,
8636 0x73, 0x82, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x31,
8637 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x32, 0x6c, 0x6d,
8638 0x61, 0x70, 0x20, 0x69, 0x6e, 0x20, 0x61, 0x20, 0x6d, 0x61,
8639 0x70, 0xa4, 0x67, 0x62, 0x79, 0x74, 0x65, 0x73, 0x20, 0x31,
8640 0x44, 0x78, 0x78, 0x78, 0x78, 0x67, 0x62, 0x79, 0x74, 0x65,
8641 0x73, 0x20, 0x32, 0x44, 0x79, 0x79, 0x79, 0x79, 0x6b, 0x61,
8642 0x6e, 0x6f, 0x74, 0x68, 0x65, 0x72, 0x20, 0x69, 0x6e, 0x74,
8643 0x18, 0x62, 0x66, 0x74, 0x65, 0x78, 0x74, 0x20, 0x32, 0x78,
8644 0x1e, 0x6c, 0x69, 0x65, 0x73, 0x2c, 0x20, 0x64, 0x61, 0x6d,
8645 0x6e, 0x20, 0x6c, 0x69, 0x65, 0x73, 0x20, 0x61, 0x6e, 0x64,
8646 0x20, 0x73, 0x74, 0x61, 0x74, 0x69, 0x73, 0x74, 0x69, 0x63,
8647 0x73
8648};
8649
8650#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
8651
8652/* As above, but the arrays are indefinite length */
8653static const uint8_t pValidIndefWrappedMapEncoded[] = {
8654 0x9f, 0x07, 0x9f, 0x58, 0x97,
8655 0xa3, 0x6d, 0x66, 0x69, 0x72, 0x73, 0x74, 0x20, 0x69, 0x6e,
8656 0x74, 0x65, 0x67, 0x65, 0x72, 0x18, 0x2a, 0x77, 0x61, 0x6e,
8657 0x20, 0x61, 0x72, 0x72, 0x61, 0x79, 0x20, 0x6f, 0x66, 0x20,
8658 0x74, 0x77, 0x6f, 0x20, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67,
8659 0x73, 0x82, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x31,
8660 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x32, 0x6c, 0x6d,
8661 0x61, 0x70, 0x20, 0x69, 0x6e, 0x20, 0x61, 0x20, 0x6d, 0x61,
8662 0x70, 0xa4, 0x67, 0x62, 0x79, 0x74, 0x65, 0x73, 0x20, 0x31,
8663 0x44, 0x78, 0x78, 0x78, 0x78, 0x67, 0x62, 0x79, 0x74, 0x65,
8664 0x73, 0x20, 0x32, 0x44, 0x79, 0x79, 0x79, 0x79, 0x6b, 0x61,
8665 0x6e, 0x6f, 0x74, 0x68, 0x65, 0x72, 0x20, 0x69, 0x6e, 0x74,
8666 0x18, 0x62, 0x66, 0x74, 0x65, 0x78, 0x74, 0x20, 0x32, 0x78,
8667 0x1e, 0x6c, 0x69, 0x65, 0x73, 0x2c, 0x20, 0x64, 0x61, 0x6d,
8668 0x6e, 0x20, 0x6c, 0x69, 0x65, 0x73, 0x20, 0x61, 0x6e, 0x64,
8669 0x20, 0x73, 0x74, 0x61, 0x74, 0x69, 0x73, 0x74, 0x69, 0x63,
8670 0x73,
8671 0xff, 0xff
8672};
8673#endif
8674
8675
8676static const uint8_t pWithEmptyMap[] = {0x82, 0x18, 0x64, 0xa0};
8677
8678#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
8679static const uint8_t pWithEmptyMapInDef[] = {0x9f, 0x18, 0x64, 0xbf, 0xff, 0xff};
8680#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
8681
8682#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
Laurence Lundblade37286c02022-09-03 10:05:02 -07008683/*
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08008684 * An array of one that contains a byte string that is an indefinite
8685 * length string that CBOR wraps an array of three numbers [42, 43,
8686 * 44]. The byte string is an implicit tag 24.
8687 *
8688 * [
8689 * (_ h'83', h'18', h'2A182B', h'182C')
8690 * ]
Laurence Lundblade37286c02022-09-03 10:05:02 -07008691 */
Laurence Lundbladecf41c522021-02-20 10:19:07 -07008692static const uint8_t pWrappedByIndefiniteLength[] = {
8693 0x81,
Laurence Lundbladecf41c522021-02-20 10:19:07 -07008694 0x5f,
8695 0x41, 0x83,
8696 0x41, 0x18,
8697 0x43, 0x2A, 0x18, 0x2B,
8698 0x42, 0x18, 0x2C,
8699 0xff
8700};
8701#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
8702
8703
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03008704int32_t PeekAndRewindTest(void)
Laurence Lundblade2a26abb2020-11-05 19:06:54 -08008705{
8706 QCBORItem Item;
8707 QCBORError nCBORError;
8708 QCBORDecodeContext DCtx;
8709
8710 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0);
8711
8712 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
8713 return 100+(int32_t)nCBORError;
8714 }
8715 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
8716 return 200;
8717 }
8718
Laurence Lundblade3427dee2021-06-20 11:11:24 -07008719 QCBORDecode_VPeekNext(&DCtx, &Item);
8720 if((nCBORError = QCBORDecode_GetError(&DCtx))) {
8721 return 150+(int32_t)nCBORError;
8722 }
8723 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
8724 return 250;
8725 }
8726
Laurence Lundblade2a26abb2020-11-05 19:06:54 -08008727 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
8728 return (int32_t)nCBORError;
8729 }
8730 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
8731 return 300;
8732 }
8733
8734 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
8735 return 400 + (int32_t)nCBORError;
8736 }
8737 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
8738 return 500;
8739 }
8740
8741 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8742 return (int32_t)nCBORError;
8743 }
8744 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
8745 return 600;
8746 }
8747
8748 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
8749 return 900 + (int32_t)nCBORError;
8750 }
8751 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
8752 Item.uDataType != QCBOR_TYPE_INT64 ||
8753 Item.val.int64 != 42 ||
8754 Item.uDataAlloc ||
8755 Item.uLabelAlloc ||
8756 UsefulBufCompareToSZ(Item.label.string, "first integer")) {
8757 return 1000;
8758 }
8759
8760 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8761 return 1100 + (int32_t)nCBORError;
8762 }
8763
8764 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
8765 Item.uDataType != QCBOR_TYPE_INT64 ||
8766 Item.val.int64 != 42 ||
8767 Item.uDataAlloc ||
8768 Item.uLabelAlloc ||
8769 UsefulBufCompareToSZ(Item.label.string, "first integer")) {
8770 return 1200;
8771 }
8772
8773
8774 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8775 return 1300 + (int32_t)nCBORError;
8776 }
8777 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
8778 Item.uDataAlloc ||
8779 Item.uLabelAlloc ||
8780 UsefulBufCompareToSZ(Item.label.string, "an array of two strings") ||
8781 Item.uDataType != QCBOR_TYPE_ARRAY ||
Laurence Lundbladecf41c522021-02-20 10:19:07 -07008782 Item.val.uCount != 2) {
Laurence Lundblade2a26abb2020-11-05 19:06:54 -08008783 return 1400;
Laurence Lundbladecf41c522021-02-20 10:19:07 -07008784 }
Laurence Lundblade2a26abb2020-11-05 19:06:54 -08008785
8786 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8787 return 1500 + (int32_t)nCBORError;
8788 }
8789 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
8790 Item.uDataAlloc ||
8791 Item.uLabelAlloc ||
8792 UsefulBufCompareToSZ(Item.val.string, "string1")) {
8793 return 1600;
8794 }
8795
8796 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
8797 return 1700 + (int32_t)nCBORError;
8798 }
8799 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
8800 Item.uDataAlloc ||
8801 Item.uLabelAlloc ||
8802 UsefulBufCompareToSZ(Item.val.string, "string2")) {
8803 return 1800;
8804 }
8805
8806 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
8807 return (int32_t)nCBORError;
8808 }
8809 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
8810 Item.uDataAlloc ||
8811 Item.uLabelAlloc ||
8812 UsefulBufCompareToSZ(Item.val.string, "string2")) {
8813 return 1900;
8814 }
8815
8816 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8817 return (int32_t)nCBORError;
8818 }
8819 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
8820 Item.uDataAlloc ||
8821 Item.uLabelAlloc ||
8822 UsefulBufCompareToSZ(Item.val.string, "string2")) {
8823 return 2000;
8824 }
8825
8826
8827 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8828 return 2100 + (int32_t)nCBORError;
8829 }
8830 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
8831 Item.uDataAlloc ||
8832 Item.uLabelAlloc ||
8833 UsefulBufCompareToSZ(Item.label.string, "map in a map") ||
8834 Item.uDataType != QCBOR_TYPE_MAP ||
8835 Item.val.uCount != 4) {
8836 return 2100;
8837 }
8838
8839 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8840 return 2200 + (int32_t)nCBORError;
8841 }
8842 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
8843 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("bytes 1"))||
8844 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
8845 Item.uDataAlloc ||
8846 Item.uLabelAlloc ||
8847 UsefulBufCompareToSZ(Item.val.string, "xxxx")) {
8848 return 2300;
8849 }
8850
8851 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
8852 return 2400 + (int32_t)nCBORError;
8853 }
8854 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
8855 UsefulBufCompareToSZ(Item.label.string, "bytes 2") ||
8856 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
8857 Item.uDataAlloc ||
8858 Item.uLabelAlloc ||
8859 UsefulBufCompareToSZ(Item.val.string, "yyyy")) {
8860 return 2500;
8861 }
8862
8863 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8864 return 2600 + (int32_t)nCBORError;
8865 }
8866 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
8867 UsefulBufCompareToSZ(Item.label.string, "bytes 2") ||
8868 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
8869 Item.uDataAlloc ||
8870 Item.uLabelAlloc ||
8871 UsefulBufCompareToSZ(Item.val.string, "yyyy")) {
8872 return 2700;
8873 }
8874
8875 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8876 return 2800 + (int32_t)nCBORError;
8877 }
8878 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
8879 Item.uDataAlloc ||
8880 Item.uLabelAlloc ||
8881 UsefulBufCompareToSZ(Item.label.string, "another int") ||
8882 Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundbladecf41c522021-02-20 10:19:07 -07008883 Item.val.int64 != 98) {
Laurence Lundblade2a26abb2020-11-05 19:06:54 -08008884 return 2900;
Laurence Lundbladecf41c522021-02-20 10:19:07 -07008885 }
Laurence Lundblade2a26abb2020-11-05 19:06:54 -08008886
8887 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
8888 return 3000 + (int32_t)nCBORError;
8889 }
8890 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
8891 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("text 2"))||
8892 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
8893 Item.uDataAlloc ||
8894 Item.uLabelAlloc ||
8895 UsefulBufCompareToSZ(Item.val.string, "lies, damn lies and statistics")) {
8896 return 3100;
8897 }
8898
8899 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8900 return 3200 + (int32_t)nCBORError;
8901 }
8902 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
8903 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("text 2"))||
8904 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
8905 Item.uDataAlloc ||
8906 Item.uLabelAlloc ||
8907 UsefulBufCompareToSZ(Item.val.string, "lies, damn lies and statistics")) {
8908 return 3300;
8909 }
8910
Laurence Lundblade3427dee2021-06-20 11:11:24 -07008911 nCBORError = QCBORDecode_PeekNext(&DCtx, &Item);
8912 if(nCBORError != QCBOR_ERR_NO_MORE_ITEMS) {
8913 return 3300 + (int32_t)nCBORError;
8914 }
8915
8916 QCBORDecode_VPeekNext(&DCtx, &Item);
8917 nCBORError = QCBORDecode_GetError(&DCtx);
8918 if(nCBORError != QCBOR_ERR_NO_MORE_ITEMS) {
8919 return 3400 + (int32_t)nCBORError;
8920 }
8921
8922 QCBORDecode_VPeekNext(&DCtx, &Item);
8923 nCBORError = QCBORDecode_GetError(&DCtx);
8924 if(nCBORError != QCBOR_ERR_NO_MORE_ITEMS) {
8925 return 3500 + (int32_t)nCBORError;
8926 }
Laurence Lundbladecf41c522021-02-20 10:19:07 -07008927
8928
8929 // Rewind to top level after entering several maps
8930 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0);
8931
8932 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8933 return (int32_t)nCBORError;
8934 }
8935 if(Item.uDataType != QCBOR_TYPE_MAP ||
8936 Item.val.uCount != 3) {
8937 return 400;
8938 }
8939
8940 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8941 return 4000+(int32_t)nCBORError;
8942 }
8943
8944 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
8945 Item.uDataType != QCBOR_TYPE_INT64 ||
8946 Item.val.int64 != 42 ||
8947 Item.uDataAlloc ||
8948 Item.uLabelAlloc ||
8949 UsefulBufCompareToSZ(Item.label.string, "first integer")) {
8950 return 4100;
8951 }
8952
8953 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8954 return 4100+(int32_t)nCBORError;
8955 }
8956 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
8957 Item.uDataAlloc ||
8958 Item.uLabelAlloc ||
8959 UsefulBufCompareToSZ(Item.label.string, "an array of two strings") ||
8960 Item.uDataType != QCBOR_TYPE_ARRAY ||
8961 Item.val.uCount != 2) {
8962 return 4200;
8963 }
8964
8965 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8966 return 4200+(int32_t)nCBORError;
8967 }
8968 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
8969 Item.uDataAlloc ||
8970 Item.uLabelAlloc ||
8971 UsefulBufCompareToSZ(Item.val.string, "string1")) {
8972 return 4300;
8973 }
8974
8975 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8976 return 4300+(int32_t)nCBORError;
8977 }
8978 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
8979 Item.uDataAlloc ||
8980 Item.uLabelAlloc ||
8981 UsefulBufCompareToSZ(Item.val.string, "string2")) {
8982 return 4400;
8983 }
8984
8985 QCBORDecode_Rewind(&DCtx);
8986
8987 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8988 return 4400+(int32_t)nCBORError;
8989 }
8990 if(Item.uDataType != QCBOR_TYPE_MAP ||
8991 Item.val.uCount != 3) {
8992 return 4500;
8993 }
8994
8995 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8996 return (int32_t)nCBORError;
8997 }
8998
8999 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
9000 Item.uDataType != QCBOR_TYPE_INT64 ||
9001 Item.val.int64 != 42 ||
9002 Item.uDataAlloc ||
9003 Item.uLabelAlloc ||
9004 UsefulBufCompareToSZ(Item.label.string, "first integer")) {
9005 return 4600;
9006 }
9007
9008 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
9009 return (int32_t)nCBORError;
9010 }
9011 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
9012 Item.uDataAlloc ||
9013 Item.uLabelAlloc ||
9014 UsefulBufCompareToSZ(Item.label.string, "an array of two strings") ||
9015 Item.uDataType != QCBOR_TYPE_ARRAY ||
9016 Item.val.uCount != 2) {
9017 return 4700;
9018 }
9019
9020 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
9021 return (int32_t)nCBORError;
9022 }
9023 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
9024 Item.uDataAlloc ||
9025 Item.uLabelAlloc ||
9026 UsefulBufCompareToSZ(Item.val.string, "string1")) {
9027 return 4800;
9028 }
9029
9030 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
9031 return 4900+(int32_t)nCBORError;
9032 }
9033 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
9034 Item.uDataAlloc ||
9035 Item.uLabelAlloc ||
9036 UsefulBufCompareToSZ(Item.val.string, "string2")) {
9037 return 5000;
9038 }
9039
9040
9041 // Rewind an entered map
9042 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0);
9043
9044 QCBORDecode_EnterMap(&DCtx, NULL);
9045
9046 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
9047 return 5100+(int32_t)nCBORError;
9048 }
9049
9050 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
9051 Item.uDataType != QCBOR_TYPE_INT64 ||
9052 Item.val.int64 != 42 ||
9053 Item.uDataAlloc ||
9054 Item.uLabelAlloc ||
9055 UsefulBufCompareToSZ(Item.label.string, "first integer")) {
9056 return 5200;
9057 }
9058
9059 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
9060 return 5200+(int32_t)nCBORError;
9061 }
9062 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
9063 Item.uDataAlloc ||
9064 Item.uLabelAlloc ||
9065 UsefulBufCompareToSZ(Item.label.string, "an array of two strings") ||
9066 Item.uDataType != QCBOR_TYPE_ARRAY ||
9067 Item.val.uCount != 2) {
9068 return -5300;
9069 }
9070
9071 QCBORDecode_Rewind(&DCtx);
9072
9073 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
9074 return 5300+(int32_t)nCBORError;
9075 }
9076
9077 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
9078 Item.uDataType != QCBOR_TYPE_INT64 ||
9079 Item.val.int64 != 42 ||
9080 Item.uDataAlloc ||
9081 Item.uLabelAlloc ||
9082 UsefulBufCompareToSZ(Item.label.string, "first integer")) {
9083 return 5400;
9084 }
9085
9086 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
9087 return 5400+(int32_t)nCBORError;
9088 }
9089 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
9090 Item.uDataAlloc ||
9091 Item.uLabelAlloc ||
9092 UsefulBufCompareToSZ(Item.label.string, "an array of two strings") ||
9093 Item.uDataType != QCBOR_TYPE_ARRAY ||
9094 Item.val.uCount != 2) {
9095 return 5500;
9096 }
9097
9098
9099 // Rewind and entered array inside an entered map
9100 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0);
9101
9102 QCBORDecode_EnterMap(&DCtx, NULL);
9103
9104 QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings");
9105
9106 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
9107 return 5600+(int32_t)nCBORError;
9108 }
9109 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
9110 Item.uDataAlloc ||
9111 Item.uLabelAlloc ||
9112 UsefulBufCompareToSZ(Item.val.string, "string1")) {
9113 return 5700;
9114 }
9115
9116 QCBORDecode_Rewind(&DCtx);
9117
9118 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
9119 return 5700+(int32_t)nCBORError;
9120 }
9121 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
9122 Item.uDataAlloc ||
9123 Item.uLabelAlloc ||
9124 UsefulBufCompareToSZ(Item.val.string, "string1")) {
9125 return 5800;
9126 }
9127
9128 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
9129 return (int32_t)nCBORError;
9130 }
9131 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
9132 Item.uDataAlloc ||
9133 Item.uLabelAlloc ||
9134 UsefulBufCompareToSZ(Item.val.string, "string2")) {
9135 return 5900;
9136 }
9137
9138 QCBORDecode_Rewind(&DCtx);
9139
9140 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
9141 return 5900+(int32_t)nCBORError;
9142 }
9143 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
9144 Item.uDataAlloc ||
9145 Item.uLabelAlloc ||
9146 UsefulBufCompareToSZ(Item.val.string, "string1")) {
9147 return 6000;
9148 }
9149
9150
9151 // Rewind a byte string inside an array inside an array
9152 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidWrappedMapEncoded), 0);
9153
9154 QCBORDecode_EnterArray(&DCtx, NULL);
9155
9156 uint64_t i;
9157 QCBORDecode_GetUInt64(&DCtx, &i);
9158
9159 QCBORDecode_EnterArray(&DCtx, NULL);
9160
9161 QCBORDecode_EnterBstrWrapped(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
9162 if(QCBORDecode_GetError(&DCtx)) {
9163 return 6100;
9164 }
9165
9166 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
9167 return (int32_t)nCBORError;
9168 }
9169 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
9170 return 6200;
9171 }
9172
9173 QCBORDecode_Rewind(&DCtx);
9174
9175 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
9176 return 6300+(int32_t)nCBORError;
9177 }
9178 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
9179 return 6400;
9180 }
9181
9182#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
9183 // Rewind a byte string inside an indefinite-length array inside
9184 // indefinite-length array
9185
9186 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidIndefWrappedMapEncoded), 0);
9187
9188 QCBORDecode_EnterArray(&DCtx, NULL);
9189
9190 QCBORDecode_GetUInt64(&DCtx, &i);
9191
9192 QCBORDecode_EnterArray(&DCtx, NULL);
9193
9194 QCBORDecode_EnterBstrWrapped(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
9195 if(QCBORDecode_GetError(&DCtx)) {
9196 return 6500;
9197 }
9198
9199 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
9200 return 6600+(int32_t)nCBORError;
9201 }
9202 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
9203 return 6700;
9204 }
9205
9206 QCBORDecode_Rewind(&DCtx);
9207
9208 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
9209 return 6800+(int32_t)nCBORError;
9210 }
9211 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
9212 return 6900;
9213 }
9214#endif
9215
9216 // Rewind an empty map
9217 // [100, {}]
9218 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pWithEmptyMap), 0);
9219 QCBORDecode_EnterArray(&DCtx, NULL);
9220 QCBORDecode_GetUInt64(&DCtx, &i);
9221 if(i != 100) {
9222 return 7010;
9223 }
9224 QCBORDecode_EnterMap(&DCtx, NULL);
9225
9226 /* Do it 5 times to be sure multiple rewinds work */
9227 for(int n = 0; n < 5; n++) {
9228 nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
9229 if(nCBORError != QCBOR_ERR_NO_MORE_ITEMS) {
9230 return 7000 + n;
9231 }
9232 QCBORDecode_Rewind(&DCtx);
9233 }
9234 QCBORDecode_ExitMap(&DCtx);
9235 QCBORDecode_Rewind(&DCtx);
9236 QCBORDecode_GetUInt64(&DCtx, &i);
9237 if(i != 100) {
9238 return 7010;
9239 }
9240 QCBORDecode_ExitArray(&DCtx);
9241 QCBORDecode_Rewind(&DCtx);
9242 QCBORDecode_EnterArray(&DCtx, NULL);
9243 i = 9;
9244 QCBORDecode_GetUInt64(&DCtx, &i);
9245 if(i != 100) {
9246 return 7020;
9247 }
9248 if(QCBORDecode_GetError(&DCtx)){
9249 return 7030;
9250 }
9251
9252 // Rewind an empty indefinite length map
9253#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
9254 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pWithEmptyMapInDef), 0);
9255 QCBORDecode_EnterArray(&DCtx, NULL);
9256 QCBORDecode_GetUInt64(&DCtx, &i);
9257 if(i != 100) {
9258 return 7810;
9259 }
9260 QCBORDecode_EnterMap(&DCtx, NULL);
9261
9262 /* Do it 5 times to be sure multiple rewinds work */
9263 for(int n = 0; n < 5; n++) {
9264 nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
9265 if(nCBORError != QCBOR_ERR_NO_MORE_ITEMS) {
9266 return 7800 + n;
9267 }
9268 QCBORDecode_Rewind(&DCtx);
9269 }
9270 QCBORDecode_ExitMap(&DCtx);
9271 QCBORDecode_Rewind(&DCtx);
9272 QCBORDecode_GetUInt64(&DCtx, &i);
9273 if(i != 100) {
9274 return 7810;
9275 }
9276 QCBORDecode_ExitArray(&DCtx);
9277 QCBORDecode_Rewind(&DCtx);
9278 QCBORDecode_EnterArray(&DCtx, NULL);
9279 i = 9;
9280 QCBORDecode_GetUInt64(&DCtx, &i);
9281 if(i != 100) {
9282 return 7820;
9283 }
9284 if(QCBORDecode_GetError(&DCtx)){
9285 return 7830;
9286 }
9287#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
9288
9289 // Rewind an indefnite length byte-string wrapped sequence
9290#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
9291 QCBORDecode_Init(&DCtx,
9292 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pWrappedByIndefiniteLength),
9293 0);
9294 UsefulBuf_MAKE_STACK_UB(Pool, 100);
9295 QCBORDecode_SetMemPool(&DCtx, Pool, 0);
9296
9297 QCBORDecode_EnterArray(&DCtx, NULL);
9298 QCBORDecode_EnterBstrWrapped(&DCtx, 2, NULL);
Laurence Lundblade31fddb72024-05-13 13:03:35 -07009299 if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_CANNOT_ENTER_ALLOCATED_STRING) {
Laurence Lundbladecf41c522021-02-20 10:19:07 -07009300 return 7300;
9301 }
9302
9303 /*
Laurence Lundblade31fddb72024-05-13 13:03:35 -07009304 Improvement: Fix QCBORDecode_EnterBstrWrapped() so it can work on
9305 allocated strings. This is a fairly big job because of all the
9306 UsefulBuf internal book keeping that needs tweaking.
Laurence Lundbladecf41c522021-02-20 10:19:07 -07009307 QCBORDecode_GetUInt64(&DCtx, &i);
9308 if(i != 42) {
9309 return 7110;
9310 }
9311 QCBORDecode_Rewind(&DCtx);
9312 QCBORDecode_GetUInt64(&DCtx, &i);
9313 if(i != 42) {
9314 return 7220;
Laurence Lundblade31fddb72024-05-13 13:03:35 -07009315 }
9316 */
Laurence Lundblade37286c02022-09-03 10:05:02 -07009317
Laurence Lundbladecf41c522021-02-20 10:19:07 -07009318#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
9319
9320
9321 // Rewind an indefnite length byte-string wrapped sequence
9322
Laurence Lundblade2a26abb2020-11-05 19:06:54 -08009323 return 0;
9324}
Laurence Lundblade9f9c3732021-03-23 09:38:46 -07009325
9326
9327
9328
9329static const uint8_t spBooleansInMap[] =
9330{
9331 0xa1, 0x08, 0xf5
9332};
9333
9334static const uint8_t spBooleansInMapWrongType[] =
9335{
9336 0xa1, 0x08, 0xf6
9337};
9338
9339static const uint8_t spBooleansInMapNWF[] =
9340{
9341 0xa1, 0x08, 0x1a
9342};
9343
Laurence Lundblade8782dd32021-04-27 04:15:37 -07009344static const uint8_t spNullInMap[] =
9345{
9346 0xa1, 0x08, 0xf6
9347};
9348
9349static const uint8_t spUndefinedInMap[] =
9350{
9351 0xa1, 0x08, 0xf7
9352};
9353
Laurence Lundblade9f9c3732021-03-23 09:38:46 -07009354
9355int32_t BoolTest(void)
9356{
9357 QCBORDecodeContext DCtx;
9358 bool b;
9359
Laurence Lundblade8782dd32021-04-27 04:15:37 -07009360 QCBORDecode_Init(&DCtx,
9361 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMap),
9362 0);
Laurence Lundblade9f9c3732021-03-23 09:38:46 -07009363 QCBORDecode_EnterMap(&DCtx, NULL);
9364 QCBORDecode_GetBool(&DCtx, &b);
9365 if(QCBORDecode_GetAndResetError(&DCtx) || !b) {
9366 return 1;
9367 }
9368
9369 QCBORDecode_GetBoolInMapN(&DCtx, 7, &b);
9370 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_LABEL_NOT_FOUND) {
9371 return 2;
9372 }
9373
9374 QCBORDecode_GetBoolInMapN(&DCtx, 8, &b);
9375 if(QCBORDecode_GetAndResetError(&DCtx) || !b) {
9376 return 3;
9377 }
9378
9379
9380 QCBORDecode_GetBoolInMapSZ(&DCtx, "xx", &b);
9381 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_LABEL_NOT_FOUND) {
9382 return 4;
9383 }
9384
Laurence Lundblade8782dd32021-04-27 04:15:37 -07009385 QCBORDecode_Init(&DCtx,
9386 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMapWrongType),
9387 0);
Laurence Lundblade9f9c3732021-03-23 09:38:46 -07009388 QCBORDecode_EnterMap(&DCtx, NULL);
9389 QCBORDecode_GetBool(&DCtx, &b);
9390 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
9391 return 5;
9392 }
9393
Laurence Lundblade8782dd32021-04-27 04:15:37 -07009394 QCBORDecode_Init(&DCtx,
9395 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMapNWF),
9396 0);
Laurence Lundblade9f9c3732021-03-23 09:38:46 -07009397 QCBORDecode_EnterMap(&DCtx, NULL);
9398 QCBORDecode_GetBool(&DCtx, &b);
9399 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_HIT_END) {
9400 return 6;
9401 }
9402
Laurence Lundblade8782dd32021-04-27 04:15:37 -07009403
9404 QCBORDecode_Init(&DCtx,
9405 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spNullInMap),
9406 0);
9407 QCBORDecode_EnterMap(&DCtx, NULL);
9408 QCBORDecode_GetNull(&DCtx);
9409 if(QCBORDecode_GetAndResetError(&DCtx)) {
9410 return 7;
9411 }
9412
9413 QCBORDecode_Init(&DCtx,
9414 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMap),
9415 0);
9416 QCBORDecode_EnterMap(&DCtx, NULL);
9417 QCBORDecode_GetNull(&DCtx);
9418 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
9419 return 8;
9420 }
9421
9422 QCBORDecode_Init(&DCtx,
9423 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spNullInMap),
9424 0);
9425 QCBORDecode_EnterMap(&DCtx, NULL);
9426 QCBORDecode_GetNullInMapN(&DCtx, 8);
9427 if(QCBORDecode_GetAndResetError(&DCtx)) {
9428 return 9;
9429 }
9430
9431 QCBORDecode_Init(&DCtx,
9432 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMap),
9433 0);
9434 QCBORDecode_EnterMap(&DCtx, NULL);
9435 QCBORDecode_GetNullInMapN(&DCtx, 8);
9436 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
9437 return 10;
9438 }
9439
9440 QCBORDecode_Init(&DCtx,
9441 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMapNWF),
9442 0);
9443 QCBORDecode_EnterMap(&DCtx, NULL);
9444 QCBORDecode_GetUndefined(&DCtx);
9445 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_HIT_END) {
9446 return 11;
9447 }
9448
9449 QCBORDecode_Init(&DCtx,
9450 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUndefinedInMap),
9451 0);
9452 QCBORDecode_EnterMap(&DCtx, NULL);
9453 QCBORDecode_GetUndefined(&DCtx);
9454 if(QCBORDecode_GetAndResetError(&DCtx)) {
9455 return 12;
9456 }
9457
9458 QCBORDecode_Init(&DCtx,
9459 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMap),
9460 0);
9461 QCBORDecode_EnterMap(&DCtx, NULL);
9462 QCBORDecode_GetUndefined(&DCtx);
9463 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
9464 return 13;
9465 }
9466
9467 QCBORDecode_Init(&DCtx,
9468 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUndefinedInMap),
9469 0);
9470 QCBORDecode_EnterMap(&DCtx, NULL);
9471 QCBORDecode_GetUndefinedInMapN(&DCtx, 8);
9472 if(QCBORDecode_GetAndResetError(&DCtx)) {
9473 return 14;
9474 }
9475
9476 QCBORDecode_Init(&DCtx,
9477 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMap),
9478 0);
9479 QCBORDecode_EnterMap(&DCtx, NULL);
9480 QCBORDecode_GetUndefinedInMapN(&DCtx, 8);
9481 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
9482 return 15;
9483 }
9484
9485 QCBORDecode_Init(&DCtx,
9486 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMapNWF),
9487 0);
9488 QCBORDecode_EnterMap(&DCtx, NULL);
9489 QCBORDecode_GetUndefined(&DCtx);
9490 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_HIT_END) {
9491 return 15;
9492 }
9493
Laurence Lundblade9f9c3732021-03-23 09:38:46 -07009494 return 0;
9495}
Laurence Lundbladef00b8be2024-03-08 10:34:33 -08009496
9497
Laurence Lundbladea29f45a2024-05-14 15:55:19 -07009498
9499static const uint8_t spExpectedArray2s[] = {
9500 0x82, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67,
9501 0x31, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67,
9502 0x32};
9503
9504#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
9505static const uint8_t spExpectedArray2sIndef[] = {
9506 0x9f, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67,
9507 0x31, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67,
9508 0x32, 0xff};
9509#endif
9510
9511static const uint8_t spExpectedMap4[] = {
9512 0xa4, 0x67, 0x62, 0x79, 0x74, 0x65, 0x73, 0x20,
9513 0x31, 0x44, 0x78, 0x78, 0x78, 0x78, 0x67, 0x62,
9514 0x79, 0x74, 0x65, 0x73, 0x20, 0x32, 0x44, 0x79,
9515 0x79, 0x79, 0x79, 0x6b, 0x61, 0x6e, 0x6f, 0x74,
9516 0x68, 0x65, 0x72, 0x20, 0x69, 0x6e, 0x74, 0x18,
9517 0x62, 0x66, 0x74, 0x65, 0x78, 0x74, 0x20, 0x32,
9518 0x78, 0x1e, 0x6c, 0x69, 0x65, 0x73, 0x2c, 0x20,
9519 0x64, 0x61, 0x6d, 0x6e, 0x20, 0x6c, 0x69, 0x65,
9520 0x73, 0x20, 0x61, 0x6e, 0x64, 0x20, 0x73, 0x74,
9521 0x61, 0x74, 0x69, 0x73, 0x74, 0x69, 0x63, 0x73};
9522
9523
9524#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
9525
9526static const uint8_t spExpectedMap4Indef[] = {
9527 0xbf, 0x67, 0x62, 0x79, 0x74, 0x65, 0x73, 0x20,
9528 0x31, 0x44, 0x78, 0x78, 0x78, 0x78, 0x67, 0x62,
9529 0x79, 0x74, 0x65, 0x73, 0x20, 0x32, 0x44, 0x79,
9530 0x79, 0x79, 0x79, 0x6b, 0x61, 0x6e, 0x6f, 0x74,
9531 0x68, 0x65, 0x72, 0x20, 0x69, 0x6e, 0x74, 0x18,
9532 0x62, 0x66, 0x74, 0x65, 0x78, 0x74, 0x20, 0x32,
9533 0x78, 0x1e, 0x6c, 0x69, 0x65, 0x73, 0x2c, 0x20,
9534 0x64, 0x61, 0x6d, 0x6e, 0x20, 0x6c, 0x69, 0x65,
9535 0x73, 0x20, 0x61, 0x6e, 0x64, 0x20, 0x73, 0x74,
9536 0x61, 0x74, 0x69, 0x73, 0x74, 0x69, 0x63, 0x73,
9537 0xff};
9538
9539/*
9540 * [[[[[0, []]]]], 0]
9541 */
9542static const uint8_t spDefAndIndef[] = {
9543 0x82,
9544 0x9f, 0x9f, 0x9f, 0x82, 0x00, 0x9f, 0xff, 0xff, 0xff, 0xff, 0x00
9545};
9546#endif /* !QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
9547
9548
9549#ifndef QCBOR_DISABLE_TAGS
9550/* An exp / mant tag in two nested arrays */
9551static const uint8_t spExpMant[] = {0x81, 0x81, 0xC4, 0x82, 0x20, 0x03};
9552#endif /* !QCBOR_DISABLE_TAGS */
9553
9554
9555int32_t GetMapAndArrayTest(void)
9556{
9557 QCBORDecodeContext DCtx;
9558 size_t uPosition ;
9559 QCBORItem Item;
9560 UsefulBufC ReturnedEncodedCBOR;
9561
9562
9563 QCBORDecode_Init(&DCtx,
9564 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded),
9565 0);
9566
9567 QCBORDecode_EnterMap(&DCtx, NULL);
9568 QCBORDecode_VGetNextConsume(&DCtx, &Item);
9569 QCBORDecode_GetArray(&DCtx, &Item, &ReturnedEncodedCBOR);
9570 if(QCBORDecode_GetError(&DCtx)) {
9571 return 1;
9572 }
9573 if(Item.val.uCount != 2) {
9574 return 2;
9575 }
9576 if(UsefulBuf_Compare(ReturnedEncodedCBOR, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedArray2s))) {
9577 return 3;
9578 }
9579
9580 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
9581 UsefulBuf_Compare(Item.label.string, UsefulBuf_FROM_SZ_LITERAL("an array of two strings"))) {
9582 return 4;
9583 }
9584
9585 uPosition = QCBORDecode_Tell(&DCtx);
9586
9587
9588 QCBORDecode_GetMap(&DCtx, &Item, &ReturnedEncodedCBOR);
9589 if(QCBORDecode_GetError(&DCtx)) {
9590 return 10;
9591 }
9592 if(Item.val.uCount != 4) {
9593 return 11;
9594 }
9595 if(UsefulBuf_Compare(ReturnedEncodedCBOR, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedMap4))) {
9596 return 12;
9597 }
9598 uPosition = QCBORDecode_Tell(&DCtx);
9599 QCBORDecode_GetArrayFromMapSZ(&DCtx,
9600 "an array of two strings",
9601 &Item,
9602 &ReturnedEncodedCBOR);
9603 if(QCBORDecode_GetError(&DCtx)) {
9604 return 20;
9605 }
9606 if(Item.val.uCount != 2) {
9607 return 21;
9608 }
9609 if(UsefulBuf_Compare(ReturnedEncodedCBOR, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedArray2s))) {
9610 return 22;
9611 }
9612 if(uPosition != QCBORDecode_Tell(&DCtx)) {
9613 return 23;
9614 }
9615
9616 QCBORDecode_Rewind(&DCtx);
9617
9618 uPosition = QCBORDecode_Tell(&DCtx);
9619 QCBORDecode_GetMapFromMapSZ(&DCtx, "map in a map", &Item, &ReturnedEncodedCBOR);
9620 if(QCBORDecode_GetError(&DCtx)) {
9621 return 30;
9622 }
9623 if(Item.val.uCount != 4) {
9624 return 31;
9625 }
9626 if(UsefulBuf_Compare(ReturnedEncodedCBOR, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedMap4))) {
9627 return 32;
9628 }
9629 if(uPosition != QCBORDecode_Tell(&DCtx)) {
9630 return 33;
9631 }
9632
9633 uPosition = QCBORDecode_Tell(&DCtx);
9634 QCBORDecode_GetArrayFromMapSZ(&DCtx, "map in a map", &Item, &ReturnedEncodedCBOR);
9635 if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
9636 return 40;
9637 }
9638 if(UINT32_MAX != QCBORDecode_Tell(&DCtx)) {
9639 return 41;
9640 }
9641 QCBORDecode_GetAndResetError(&DCtx);
9642 if(uPosition != QCBORDecode_Tell(&DCtx)) {
9643 return 42;
9644 }
9645
9646
9647#ifndef QCBOR_DISABLE_TAGS
9648 UsefulBufC ExpMant = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpMant);
9649 QCBORDecode_Init(&DCtx, ExpMant, 0);
9650 QCBORDecode_EnterArray(&DCtx, NULL);
9651 QCBORDecode_EnterArray(&DCtx, NULL);
9652 QCBORDecode_GetArray(&DCtx, &Item, &ReturnedEncodedCBOR);
9653 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) {
9654 return 200;
9655 }
9656 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
9657 return 201;
9658 }
9659 if(!QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_DECIMAL_FRACTION)) {
9660 return 202;
9661 }
9662 if(Item.val.uCount != 2) {
9663 return 201;
9664 }
9665 if(UsefulBuf_Compare(ReturnedEncodedCBOR, UsefulBuf_Tail(ExpMant, 2))) {
9666 return 205;
9667 }
9668#endif /* !QCBOR_DISABLE_TAGS */
9669
9670
9671#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
9672
9673 UsefulBufC DefAndIndef = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spDefAndIndef);
9674 QCBORDecode_Init(&DCtx, DefAndIndef, 0);
9675 QCBORDecode_EnterArray(&DCtx, NULL);
9676 QCBORDecode_GetArray(&DCtx, &Item, &ReturnedEncodedCBOR);
9677 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) {
9678 return 50;
9679 }
9680 if(UsefulBuf_Compare(ReturnedEncodedCBOR, UsefulBuf_Tail(UsefulBuf_Head(DefAndIndef, 11), 1))) {
9681 return 51;
9682 }
9683
9684 QCBORDecode_Init(&DCtx, DefAndIndef, 0);
9685 QCBORDecode_EnterArray(&DCtx, NULL);
9686 QCBORDecode_EnterArray(&DCtx, NULL);
9687 QCBORDecode_GetArray(&DCtx, &Item, &ReturnedEncodedCBOR);
9688 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) {
9689 return 52;
9690 }
9691 if(UsefulBuf_Compare(ReturnedEncodedCBOR, UsefulBuf_Tail(UsefulBuf_Head(DefAndIndef, 10), 2))) {
9692 return 53;
9693 }
9694
9695 QCBORDecode_Init(&DCtx, DefAndIndef, 0);
9696 QCBORDecode_EnterArray(&DCtx, NULL);
9697 QCBORDecode_EnterArray(&DCtx, NULL);
9698 QCBORDecode_EnterArray(&DCtx, NULL);
9699 QCBORDecode_GetArray(&DCtx, &Item, &ReturnedEncodedCBOR);
9700 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) {
9701 return 54;
9702 }
9703 if(UsefulBuf_Compare(ReturnedEncodedCBOR, UsefulBuf_Tail(UsefulBuf_Head(DefAndIndef, 9), 3))) {
9704 return 55;
9705 }
9706 QCBORDecode_Init(&DCtx, DefAndIndef, 0);
9707 QCBORDecode_EnterArray(&DCtx, NULL);
9708 QCBORDecode_EnterArray(&DCtx, NULL);
9709 QCBORDecode_EnterArray(&DCtx, NULL);
9710 QCBORDecode_EnterArray(&DCtx, NULL);
9711 QCBORDecode_GetArray(&DCtx, &Item, &ReturnedEncodedCBOR);
9712 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) {
9713 return 56;
9714 }
9715 if(UsefulBuf_Compare(ReturnedEncodedCBOR, UsefulBuf_Tail(UsefulBuf_Head(DefAndIndef, 8), 4))) {
9716 return 57;
9717 }
9718
9719 QCBORDecode_Init(&DCtx, DefAndIndef, 0);
9720 QCBORDecode_EnterArray(&DCtx, NULL);
9721 QCBORDecode_EnterArray(&DCtx, NULL);
9722 QCBORDecode_EnterArray(&DCtx, NULL);
9723 QCBORDecode_EnterArray(&DCtx, NULL);
9724 QCBORDecode_EnterArray(&DCtx, NULL);
9725 QCBORDecode_VGetNextConsume(&DCtx, &Item);
9726 QCBORDecode_GetArray(&DCtx, &Item, &ReturnedEncodedCBOR);
9727 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) {
9728 return 58;
9729 }
9730 if(UsefulBuf_Compare(ReturnedEncodedCBOR, UsefulBuf_Tail(UsefulBuf_Head(DefAndIndef, 8), 6))) {
9731 return 59;
9732 }
9733
9734
9735 QCBORDecode_Init(&DCtx,
9736 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapIndefEncoded),
9737 0);
9738
9739 QCBORDecode_EnterMap(&DCtx, NULL);
9740 QCBORDecode_VGetNextConsume(&DCtx, &Item);
9741 QCBORDecode_GetArray(&DCtx, &Item, &ReturnedEncodedCBOR);
9742 if(QCBORDecode_GetError(&DCtx)) {
9743 return 60;
9744 }
9745 if(Item.val.uCount != UINT16_MAX) {
9746 return 61;
9747 }
9748 if(UsefulBuf_Compare(ReturnedEncodedCBOR, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedArray2sIndef))) {
9749 return 62;
9750 }
9751
9752 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
9753 UsefulBuf_Compare(Item.label.string, UsefulBuf_FROM_SZ_LITERAL("an array of two strings"))) {
9754 return 63;
9755 }
9756
9757 uPosition = QCBORDecode_Tell(&DCtx);
9758
9759
9760 QCBORDecode_GetMap(&DCtx, &Item, &ReturnedEncodedCBOR);
9761 if(QCBORDecode_GetError(&DCtx)) {
9762 return 70;
9763 }
9764 if(Item.val.uCount != UINT16_MAX) {
9765 return 71;
9766 }
9767 if(UsefulBuf_Compare(ReturnedEncodedCBOR, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedMap4Indef))) {
9768 return 72;
9769 }
9770
9771
9772 uPosition = QCBORDecode_Tell(&DCtx);
9773 QCBORDecode_GetArrayFromMapSZ(&DCtx,
9774 "an array of two strings",
9775 &Item,
9776 &ReturnedEncodedCBOR);
9777 if(QCBORDecode_GetError(&DCtx)) {
9778 return 80;
9779 }
9780 if(Item.val.uCount != UINT16_MAX) {
9781 return 81;
9782 }
9783 if(UsefulBuf_Compare(ReturnedEncodedCBOR, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedArray2sIndef))) {
9784 return 82;
9785 }
9786 if(uPosition != QCBORDecode_Tell(&DCtx)) {
9787 return 83;
9788 }
9789
9790 QCBORDecode_Rewind(&DCtx);
9791
9792 uPosition = QCBORDecode_Tell(&DCtx);
9793 QCBORDecode_GetMapFromMapSZ(&DCtx, "map in a map", &Item, &ReturnedEncodedCBOR);
9794 if(QCBORDecode_GetError(&DCtx)) {
9795 return 90;
9796 }
9797 if(Item.val.uCount != UINT16_MAX) {
9798 return 91;
9799 }
9800 if(UsefulBuf_Compare(ReturnedEncodedCBOR, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedMap4Indef))) {
9801 return 92;
9802 }
9803 if(uPosition != QCBORDecode_Tell(&DCtx)) {
9804 return 93;
9805 }
9806
9807 uPosition = QCBORDecode_Tell(&DCtx);
9808 QCBORDecode_GetArrayFromMapSZ(&DCtx, "map in a map", &Item, &ReturnedEncodedCBOR);
9809 if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
9810 return 100;
9811 }
9812 if(UINT32_MAX != QCBORDecode_Tell(&DCtx)) {
9813 return 101;
9814 }
9815 QCBORDecode_GetAndResetError(&DCtx);
9816 if(uPosition != QCBORDecode_Tell(&DCtx)) {
9817 return 102;
9818 }
9819#endif /* !QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
9820
9821 return 0;
9822}
9823
9824
Laurence Lundbladef00b8be2024-03-08 10:34:33 -08009825int32_t
9826ErrorHandlingTests(void)
9827{
9828 QCBORDecodeContext DCtx;
9829 QCBORItem Item;
9830 QCBORError uError;
9831 int64_t integer;
9832
9833 /* Test QCBORDecode_SetError() */
9834 QCBORDecode_Init(&DCtx,
9835 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded),
9836 QCBOR_DECODE_MODE_NORMAL);
9837
9838 QCBORDecode_SetError(&DCtx, QCBOR_ERR_FIRST_USER_DEFINED);
9839
9840 QCBORDecode_VGetNext(&DCtx, &Item);
9841
9842 uError = QCBORDecode_GetError(&DCtx);
9843
9844 if(uError != QCBOR_ERR_FIRST_USER_DEFINED) {
9845 return -1;
9846 }
9847
9848 if(Item.uLabelType != QCBOR_TYPE_NONE ||
9849 Item.uDataType != QCBOR_TYPE_NONE) {
9850 return -2;
9851 }
9852
9853
9854 /* Test data type returned from previous error */
9855 QCBORDecode_Init(&DCtx,
9856 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded),
9857 QCBOR_DECODE_MODE_NORMAL);
9858 QCBORDecode_GetInt64(&DCtx, &integer);
9859 uError = QCBORDecode_GetError(&DCtx);
9860 if(uError != QCBOR_ERR_UNEXPECTED_TYPE) {
9861 return -3;
9862 }
9863
9864 QCBORDecode_VGetNext(&DCtx, &Item);
9865 if(Item.uLabelType != QCBOR_TYPE_NONE ||
9866 Item.uDataType != QCBOR_TYPE_NONE) {
9867 return -2;
9868 }
9869 uError = QCBORDecode_GetError(&DCtx);
9870 if(uError != QCBOR_ERR_UNEXPECTED_TYPE) {
9871 return -3;
9872 }
9873
9874
9875 /* Test error classification functions */
9876
9877 if(!QCBORDecode_IsUnrecoverableError(QCBOR_ERR_INDEFINITE_STRING_CHUNK)) {
9878 return -10;
9879 }
9880 if(QCBORDecode_IsUnrecoverableError(QCBOR_SUCCESS)) {
9881 return -11;
9882 }
9883 if(!QCBORDecode_IsUnrecoverableError(QCBOR_ERR_INDEFINITE_STRING_CHUNK)) {
9884 return -12;
9885 }
9886 if(QCBORDecode_IsUnrecoverableError(QCBOR_ERR_DUPLICATE_LABEL)) {
9887 return -13;
9888 }
9889
9890 if(!QCBORDecode_IsNotWellFormedError(QCBOR_ERR_BAD_TYPE_7)) {
9891 return -20;
9892 }
9893 if(!QCBORDecode_IsNotWellFormedError(QCBOR_ERR_BAD_BREAK)) {
9894 return -21;
9895 }
9896 if(QCBORDecode_IsNotWellFormedError(QCBOR_SUCCESS)) {
9897 return -22;
9898 }
9899 if(QCBORDecode_IsNotWellFormedError(QCBOR_ERR_ARRAY_DECODE_TOO_LONG)) {
9900 return -23;
9901 }
9902
Laurence Lundbladecd34e582024-06-06 10:55:14 -07009903 /* Test error strings */
9904 const char *szErrString;
9905
9906 szErrString = qcbor_err_to_str(QCBOR_ERR_ARRAY_DECODE_TOO_LONG);
9907 if(szErrString == NULL) {
9908 return -100;
9909 }
9910 if(strcmp(szErrString, "QCBOR_ERR_ARRAY_DECODE_TOO_LONG")) {
9911 return -101;
9912 }
9913
9914 szErrString = qcbor_err_to_str(QCBOR_SUCCESS);
9915 if(szErrString == NULL) {
9916 return -102;
9917 }
9918 if(strcmp(szErrString, "QCBOR_SUCCESS")) {
9919 return -103;
9920 }
9921
9922 szErrString = qcbor_err_to_str(100);
9923 if(szErrString == NULL) {
9924 return -104;
9925 }
9926 if(strcmp(szErrString, "Unidentified QCBOR error")) {
9927 return -105;
9928 }
9929
9930 szErrString = qcbor_err_to_str(200);
9931 if(szErrString == NULL) {
9932 return -106;
9933 }
9934 if(strcmp(szErrString, "USER_DEFINED_200")) {
9935 return -107;
9936 }
9937
Laurence Lundbladef00b8be2024-03-08 10:34:33 -08009938 return 0;
9939}
Laurence Lundblade11654912024-05-09 11:49:24 -07009940
9941
9942int32_t TellTests(void)
9943{
9944 QCBORDecodeContext DCtx;
9945 QCBORItem Item;
9946 uint32_t uPosition;
9947 int nIndex;
9948 int64_t nDecodedInt;
9949
9950 static const uint32_t aPos[] =
9951 {0, 1, 17, 42, 50, 58, 72, 85, 98, 112, UINT32_MAX};
9952 QCBORDecode_Init(&DCtx,
9953 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded),
9954 0);
9955 for(nIndex = 0; ; nIndex++) {
9956 uPosition = QCBORDecode_Tell(&DCtx);
9957 if(uPosition != aPos[nIndex]) {
9958 return nIndex;
9959 }
9960
9961 if(uPosition == UINT32_MAX) {
9962 break;
9963 }
9964
9965 QCBORDecode_VGetNext(&DCtx, &Item);
9966 }
9967
9968#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
9969 static const uint32_t aPosIndef[] =
9970 {0, 1, 17, 42, 50, 59, 73, 86, 99, 113, UINT32_MAX};
9971 QCBORDecode_Init(&DCtx,
9972 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapIndefEncoded),
9973 0);
9974
9975 for(nIndex = 0; ; nIndex++) {
9976 uPosition = QCBORDecode_Tell(&DCtx);
9977 if(uPosition != aPosIndef[nIndex]) {
9978 return nIndex + 100;
9979 }
9980
9981 if(uPosition == UINT32_MAX) {
9982 break;
9983 }
9984
9985 QCBORDecode_VGetNext(&DCtx, &Item);
9986 }
9987#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
9988
9989 /* Next, some tests with entered maps and arrays */
9990 QCBORDecode_Init(&DCtx,
9991 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded),
9992 0);
9993 QCBORDecode_EnterMap(&DCtx, &Item);
9994 if(QCBORDecode_Tell(&DCtx) != 1) {
9995 return 1001;
9996 }
9997 QCBORDecode_GetInt64InMapSZ(&DCtx, "first integer", &nDecodedInt);
9998 if(QCBORDecode_Tell(&DCtx) != 1) {
9999 return 1002;
10000 }
10001 QCBORDecode_EnterMapFromMapSZ(&DCtx, "map in a map");
10002 if(QCBORDecode_Tell(&DCtx) != 72) {
10003 return 1003;
10004 }
10005
10006 QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt);
10007 if(nDecodedInt != 98) {
10008 return 1004;
10009 }
10010 /* Getting non-aggregate types doesn't affect cursor position. */
10011 if(QCBORDecode_Tell(&DCtx) != 72) {
10012 return 1005;
10013 }
10014 QCBORDecode_VGetNext(&DCtx, &Item);
10015 if(QCBORDecode_Tell(&DCtx) != 85) {
10016 return 1006;
10017 }
10018 QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt);
10019 if(nDecodedInt != 98) {
10020 return 1007;
10021 }
10022 /* Getting non-aggregate types doesn't affect cursor position. */
10023 if(QCBORDecode_Tell(&DCtx) != 85) {
10024 return 1008;
10025 }
10026
10027 QCBORDecode_ExitMap(&DCtx);
10028 if(QCBORDecode_Tell(&DCtx) != UINT32_MAX) {
10029 return 1009;
10030 }
10031 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_NO_MORE_ITEMS) {
10032 return 1010;
10033 }
10034
10035#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
10036 /* Next, some tests with entered maps and arrays */
10037 QCBORDecode_Init(&DCtx,
10038 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapIndefEncoded),
10039 0);
10040 QCBORDecode_EnterMap(&DCtx, &Item);
10041 if(QCBORDecode_Tell(&DCtx) != 1) {
10042 return 2000;
10043 }
10044 QCBORDecode_GetInt64InMapSZ(&DCtx, "first integer", &nDecodedInt);
10045 if(QCBORDecode_Tell(&DCtx) != 1) {
10046 return 2001;
10047 }
10048 QCBORDecode_EnterMapFromMapSZ(&DCtx, "map in a map");
10049 if(QCBORDecode_Tell(&DCtx) != 73) {
10050 return 2002;
10051 }
10052
10053 QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt);
10054 if(nDecodedInt != 98) {
10055 return 2003;
10056 }
10057 /* Getting non-aggregate types doesn't affect cursor position. */
10058 if(QCBORDecode_Tell(&DCtx) != 73) {
10059 return 2004;
10060 }
10061 QCBORDecode_VGetNext(&DCtx, &Item);
10062 if(QCBORDecode_Tell(&DCtx) != 86) {
10063 return 2005;
10064 }
10065 QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt);
10066 if(nDecodedInt != 98) {
10067 return 2006;
10068 }
10069 /* Getting non-aggregate types doesn't affect cursor position. */
10070 if(QCBORDecode_Tell(&DCtx) != 86) {
10071 return 2007;
10072 }
10073
10074 QCBORDecode_ExitMap(&DCtx);
10075 if(QCBORDecode_Tell(&DCtx) != UINT32_MAX) {
10076 return 2008;
10077 }
10078 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_NO_MORE_ITEMS) {
10079 return 2010;
10080 }
10081#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
10082
10083
10084
10085 /* Error state test */
10086 QCBORDecode_Init(&DCtx,
10087 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded),
10088 0);
10089 /* Cause an error */
10090 QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt);
10091 if(QCBORDecode_Tell(&DCtx) != UINT32_MAX) {
10092 return 3000;
10093 }
10094
10095 /* Empties tests */
10096 const uint8_t pMinimalCBOR[] = {0xa0}; // One empty map
10097 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pMinimalCBOR),0);
10098 if(QCBORDecode_Tell(&DCtx) != 0) {
10099 return 4000;
10100 }
10101 QCBORDecode_EnterMap(&DCtx, &Item);
10102 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) {
10103 return 4001;
10104 }
10105 if(QCBORDecode_Tell(&DCtx) != UINT32_MAX) {
10106 return 4002;
10107 }
10108 QCBORDecode_ExitMap(&DCtx);
10109 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) {
10110 return 4001;
10111 }
10112 if(QCBORDecode_Tell(&DCtx) != UINT32_MAX) {
10113 return 4002;
10114 }
10115 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_NO_MORE_ITEMS) {
10116 return 4010;
10117 }
10118
10119#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
10120 const uint8_t pMinimalIndefCBOR[] = {0xbf, 0xff}; // One empty map
10121 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pMinimalIndefCBOR),0);
10122 if(QCBORDecode_Tell(&DCtx) != 0) {
10123 return 4100;
10124 }
10125 QCBORDecode_EnterMap(&DCtx, &Item);
10126 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) {
10127 return 4101;
10128 }
10129 if(QCBORDecode_Tell(&DCtx) != UINT32_MAX) {
10130 return 4102;
10131 }
10132 QCBORDecode_ExitMap(&DCtx);
10133 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) {
10134 return 4101;
10135 }
10136 if(QCBORDecode_Tell(&DCtx) != UINT32_MAX) {
10137 return 4102;
10138 }
10139 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_NO_MORE_ITEMS) {
10140 return 4110;
10141 }
10142#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
10143
10144 /* Test on a CBOR sequence */
10145 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSequenceTestInput),0);
10146 if(QCBORDecode_Tell(&DCtx) != 0) {
10147 return 5000;
10148 }
10149 QCBORDecode_VGetNext(&DCtx, &Item);
10150 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) {
10151 return 5001;
10152 }
10153 if(QCBORDecode_Tell(&DCtx) != 11) {
10154 return 5002;
10155 }
10156 QCBORDecode_VGetNext(&DCtx, &Item);
10157 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) {
10158 return 5003;
10159 }
10160 if(QCBORDecode_Tell(&DCtx) != 12) {
10161 return 5004;
10162 }
10163 QCBORDecode_VGetNext(&DCtx, &Item);
10164 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) {
10165 return 5005;
10166 }
10167 if(QCBORDecode_Tell(&DCtx) != 17) {
10168 return 5006;
10169 }
10170 QCBORDecode_VGetNext(&DCtx, &Item);
10171 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) {
10172 return 5007;
10173 }
10174 if(QCBORDecode_Tell(&DCtx) != UINT32_MAX) {
10175 return 5008;
10176 }
10177 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_NO_MORE_ITEMS) {
10178 return 5010;
10179 }
10180
10181
10182 QCBORDecode_Init(&DCtx,
10183 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded),
10184 0);
10185 QCBORDecode_EnterMap(&DCtx, &Item);
10186 QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings");
10187 if(QCBORDecode_Tell(&DCtx) != 42) {
10188 return 6001;
10189 }
10190 QCBORDecode_VGetNext(&DCtx, &Item);
10191 if(QCBORDecode_Tell(&DCtx) != 50) {
10192 return 6002;
10193 }
10194 QCBORDecode_VGetNext(&DCtx, &Item);
10195 if(QCBORDecode_Tell(&DCtx) != 58) {
10196 return 6008;
10197 }
10198 QCBORDecode_VGetNext(&DCtx, &Item);
10199 (void)QCBORDecode_GetAndResetError(&DCtx);
10200 if(QCBORDecode_Tell(&DCtx) != 58) {
10201 return 6003;
10202 }
10203 QCBORDecode_ExitArray(&DCtx);
10204 if(QCBORDecode_Tell(&DCtx) != 58) {
10205 return 6004;
10206 }
10207
10208 static const uint32_t aEmptiesPos[] =
10209 {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 13, UINT32_MAX};
10210 QCBORDecode_Init(&DCtx,
10211 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(sEmpties),
10212 0);
10213 for(nIndex = 0; ; nIndex++) {
10214 uPosition = QCBORDecode_Tell(&DCtx);
10215 if(uPosition != aEmptiesPos[nIndex]) {
10216 return nIndex + 200;
10217 }
10218
10219 if(uPosition == UINT32_MAX) {
10220 break;
10221 }
10222
10223 QCBORDecode_VGetNext(&DCtx, &Item);
10224 }
10225
10226#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
10227 static const uint32_t aIndefEmptiesPos[] =
10228 {0, 1, 2, 4, 5, 7, 8, 10, 12, 13, 16, 19, UINT32_MAX};
10229 QCBORDecode_Init(&DCtx,
10230 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(sEmptiesIndef),
10231 0);
10232 for(nIndex = 0; ; nIndex++) {
10233 uPosition = QCBORDecode_Tell(&DCtx);
10234 if(uPosition != aIndefEmptiesPos[nIndex]) {
10235 return nIndex + 300;
10236 }
10237
10238 if(uPosition == UINT32_MAX) {
10239 break;
10240 }
10241
10242 QCBORDecode_VGetNext(&DCtx, &Item);
10243 }
10244#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
10245
10246
10247 return 0;
10248}