blob: c8ef6989d7810ccb2a583539498957207922a2c3 [file] [log] [blame]
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001/*==============================================================================
Laurence Lundbladed92a6162018-11-01 11:38:35 +07002 Copyright (c) 2016-2018, The Linux Foundation.
Laurence Lundbladeb24faef2022-04-26 11:03:08 -06003 Copyright (c) 2018-2022, Laurence Lundblade.
Máté Tóth-Pálef5f07a2021-09-17 19:31:37 +02004 Copyright (c) 2021, Arm Limited.
Laurence Lundbladed92a6162018-11-01 11:38:35 +07005 All rights reserved.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08006
Laurence Lundblade0dbc9172018-11-01 14:17:21 +07007Redistribution and use in source and binary forms, with or without
8modification, are permitted provided that the following conditions are
9met:
10 * 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 Lundbladecc7da412020-12-27 00:09:07 -080066/*
67 [
68 -9223372036854775808,
69 -4294967297,
70 -4294967296,
71 -4294967295,
72 -4294967294,
73 -2147483648,
74 -2147483647,
75 -65538,
76 -65537,
77 -65536,
78 -65535,
79 -65534,
80 -257,
81 -256,
82 -255,
83 -254,
84 -25,
85 -24,
86 -23,
87 -1,
88 0,
89 0,
90 1,
91 22,
92 23,
93 24,
94 25,
95 26,
96 254,
97 255,
98 256,
99 257,
100 65534,
101 65535,
102 65536,
103 65537,
104 65538,
105 2147483647,
106 2147483647,
107 2147483648,
108 2147483649,
109 4294967294,
110 4294967295,
111 4294967296,
112 4294967297,
113 9223372036854775807,
114 18446744073709551615
115 ]
116 */
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800117
Laurence Lundbladeb836efb2018-10-28 20:09:58 +0700118static const uint8_t spExpectedEncodedInts[] = {
Laurence Lundblade2d493002024-02-01 11:09:17 -0700119 0x98, 0x31, 0x3b, 0xff, 0xff, 0xff, 0xff, 0xff,
120 0xff, 0xff, 0xff, 0x3b, 0xFf, 0xff, 0xff, 0xff, 0xff,
121 0xff, 0xff, 0xfe, 0x3b, 0x7f, 0xff, 0xff, 0xff, 0xff,
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800122 0xff, 0xff, 0xff, 0x3b, 0x00, 0x00, 0x00, 0x01,
123 0x00, 0x00, 0x00, 0x00, 0x3a, 0xff, 0xff, 0xff,
124 0xff, 0x3a, 0xff, 0xff, 0xff, 0xfe, 0x3a, 0xff,
125 0xff, 0xff, 0xfd, 0x3a, 0x7f, 0xff, 0xff, 0xff,
126 0x3a, 0x7f, 0xff, 0xff, 0xfe, 0x3a, 0x00, 0x01,
127 0x00, 0x01, 0x3a, 0x00, 0x01, 0x00, 0x00, 0x39,
128 0xff, 0xff, 0x39, 0xff, 0xfe, 0x39, 0xff, 0xfd,
129 0x39, 0x01, 0x00, 0x38, 0xff, 0x38, 0xfe, 0x38,
130 0xfd, 0x38, 0x18, 0x37, 0x36, 0x20, 0x00, 0x00,
131 0x01, 0x16, 0x17, 0x18, 0x18, 0x18, 0x19, 0x18,
132 0x1a, 0x18, 0xfe, 0x18, 0xff, 0x19, 0x01, 0x00,
133 0x19, 0x01, 0x01, 0x19, 0xff, 0xfe, 0x19, 0xff,
134 0xff, 0x1a, 0x00, 0x01, 0x00, 0x00, 0x1a, 0x00,
135 0x01, 0x00, 0x01, 0x1a, 0x00, 0x01, 0x00, 0x02,
136 0x1a, 0x7f, 0xff, 0xff, 0xff, 0x1a, 0x7f, 0xff,
137 0xff, 0xff, 0x1a, 0x80, 0x00, 0x00, 0x00, 0x1a,
138 0x80, 0x00, 0x00, 0x01, 0x1a, 0xff, 0xff, 0xff,
139 0xfe, 0x1a, 0xff, 0xff, 0xff, 0xff, 0x1b, 0x00,
140 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x1b,
141 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01,
142 0x1b, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
143 0xff, 0x1b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
144 0xff, 0xff};
145
146
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800147// return CBOR error or -1 if type of value doesn't match
148
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800149static int32_t IntegerValuesParseTestInternal(QCBORDecodeContext *pDCtx)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800150{
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700151 QCBORItem Item;
152 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800153
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800154 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700155 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800156 if(Item.uDataType != QCBOR_TYPE_ARRAY)
157 return -1;
158
159 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700160 return (int32_t)nCBORError;
Laurence Lundblade2d493002024-02-01 11:09:17 -0700161 if(Item.uDataType != QCBOR_TYPE_65BIT_NEG_INT ||
162 Item.val.uint64 != 18446744073709551615ULL)
163 return -1;
164
165 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
166 return (int32_t)nCBORError;
167 if(Item.uDataType != QCBOR_TYPE_65BIT_NEG_INT ||
168 Item.val.uint64 != 18446744073709551614ULL)
169 return -1;
170
171 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
172 return (int32_t)nCBORError;
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800173 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade570fab52018-10-13 18:28:27 +0800174 Item.val.int64 != -9223372036854775807LL - 1)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800175 return -1;
176
177 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700178 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800179 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade570fab52018-10-13 18:28:27 +0800180 Item.val.int64 != -4294967297)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800181 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800182
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800183 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700184 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800185 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade570fab52018-10-13 18:28:27 +0800186 Item.val.int64 != -4294967296)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800187 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800188
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800189 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700190 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800191 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade570fab52018-10-13 18:28:27 +0800192 Item.val.int64 != -4294967295)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800193 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800194
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800195 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700196 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800197 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade570fab52018-10-13 18:28:27 +0800198 Item.val.int64 != -4294967294)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800199 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800200
201
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800202 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700203 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800204 if(Item.uDataType != QCBOR_TYPE_INT64 ||
205 Item.val.int64 != -2147483648)
206 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800207
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800208 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700209 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800210 if(Item.uDataType != QCBOR_TYPE_INT64 ||
211 Item.val.int64 != -2147483647)
212 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800213
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800214 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700215 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800216 if(Item.uDataType != QCBOR_TYPE_INT64 ||
217 Item.val.int64 != -65538)
218 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800219
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800220 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700221 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800222 if(Item.uDataType != QCBOR_TYPE_INT64 ||
223 Item.val.int64 != -65537)
224 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800225
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800226 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700227 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800228 if(Item.uDataType != QCBOR_TYPE_INT64 ||
229 Item.val.int64 != -65536)
230 return -1;
231
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800232
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800233 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700234 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800235 if(Item.uDataType != QCBOR_TYPE_INT64 ||
236 Item.val.int64 != -65535)
237 return -1;
238
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800239
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800240 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700241 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800242 if(Item.uDataType != QCBOR_TYPE_INT64 ||
243 Item.val.int64 != -65534)
244 return -1;
245
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800246
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800247 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700248 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800249 if(Item.uDataType != QCBOR_TYPE_INT64 ||
250 Item.val.int64 != -257)
251 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800252
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800253 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700254 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800255 if(Item.uDataType != QCBOR_TYPE_INT64 ||
256 Item.val.int64 != -256)
257 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800258
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800259 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700260 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800261 if(Item.uDataType != QCBOR_TYPE_INT64 ||
262 Item.val.int64 != -255)
263 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800264
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800265 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700266 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800267 if(Item.uDataType != QCBOR_TYPE_INT64 ||
268 Item.val.int64 != -254)
269 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800270
271
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800272 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700273 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800274 if(Item.uDataType != QCBOR_TYPE_INT64 ||
275 Item.val.int64 != -25)
276 return -1;
277
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800278
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800279 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700280 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800281 if(Item.uDataType != QCBOR_TYPE_INT64 ||
282 Item.val.int64 != -24)
283 return -1;
284
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800285
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800286 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700287 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800288 if(Item.uDataType != QCBOR_TYPE_INT64 ||
289 Item.val.int64 != -23)
290 return -1;
291
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800292
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800293 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700294 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800295 if(Item.uDataType != QCBOR_TYPE_INT64 ||
296 Item.val.int64 != -1)
297 return -1;
298
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800299
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800300 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700301 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800302 if(Item.uDataType != QCBOR_TYPE_INT64 ||
303 Item.val.int64 != 0)
304 return -1;
305
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800306
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800307 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700308 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800309 if(Item.uDataType != QCBOR_TYPE_INT64 ||
310 Item.val.int64 != 0)
311 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800312
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800313 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700314 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800315 if(Item.uDataType != QCBOR_TYPE_INT64 ||
316 Item.val.int64 != 1)
317 return -1;
318
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800319
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800320 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700321 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800322 if(Item.uDataType != QCBOR_TYPE_INT64 ||
323 Item.val.int64 != 22)
324 return -1;
325
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800326
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800327 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700328 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800329 if(Item.uDataType != QCBOR_TYPE_INT64 ||
330 Item.val.int64 != 23)
331 return -1;
332
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800333
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800334 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700335 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800336 if(Item.uDataType != QCBOR_TYPE_INT64 ||
337 Item.val.int64 != 24)
338 return -1;
339
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800340
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800341 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700342 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800343 if(Item.uDataType != QCBOR_TYPE_INT64 ||
344 Item.val.int64 != 25)
345 return -1;
346
347 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700348 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800349 if(Item.uDataType != QCBOR_TYPE_INT64 ||
350 Item.val.int64 != 26)
351 return -1;
352
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800353
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800354 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700355 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800356 if(Item.uDataType != QCBOR_TYPE_INT64 ||
357 Item.val.int64 != 254)
358 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800359
360
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800361 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700362 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800363 if(Item.uDataType != QCBOR_TYPE_INT64 ||
364 Item.val.int64 != 255)
365 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800366
367
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800368 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700369 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800370 if(Item.uDataType != QCBOR_TYPE_INT64 ||
371 Item.val.int64 != 256)
372 return -1;
373
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800374
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800375 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700376 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800377 if(Item.uDataType != QCBOR_TYPE_INT64 ||
378 Item.val.int64 != 257)
379 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800380
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800381 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700382 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800383 if(Item.uDataType != QCBOR_TYPE_INT64 ||
384 Item.val.int64 != 65534)
385 return -1;
386
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800387
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800388 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700389 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800390 if(Item.uDataType != QCBOR_TYPE_INT64 ||
391 Item.val.int64 != 65535)
392 return -1;
393
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800394
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800395 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700396 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800397 if(Item.uDataType != QCBOR_TYPE_INT64 ||
398 Item.val.int64 != 65536)
399 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800400
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800401 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700402 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800403 if(Item.uDataType != QCBOR_TYPE_INT64 ||
404 Item.val.int64 != 65537)
405 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800406
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800407 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700408 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800409 if(Item.uDataType != QCBOR_TYPE_INT64 ||
410 Item.val.int64 != 65538)
411 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800412
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800413 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700414 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800415 if(Item.uDataType != QCBOR_TYPE_INT64 ||
416 Item.val.int64 != 2147483647)
417 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800418
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800419 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700420 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800421 if(Item.uDataType != QCBOR_TYPE_INT64 ||
422 Item.val.int64 != 2147483647)
423 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800424
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800425 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700426 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800427 if(Item.uDataType != QCBOR_TYPE_INT64 ||
428 Item.val.int64 != 2147483648)
429 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800430
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800431 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700432 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800433 if(Item.uDataType != QCBOR_TYPE_INT64 ||
434 Item.val.int64 != 2147483649)
435 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800436
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800437 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700438 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800439 if(Item.uDataType != QCBOR_TYPE_INT64 ||
440 Item.val.int64 != 4294967294)
441 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800442
443
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800444 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700445 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800446 if(Item.uDataType != QCBOR_TYPE_INT64 ||
447 Item.val.int64 != 4294967295)
448 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800449
450
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800451 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700452 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800453 if(Item.uDataType != QCBOR_TYPE_INT64 ||
454 Item.val.int64 != 4294967296)
455 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800456
457
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800458 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700459 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800460 if(Item.uDataType != QCBOR_TYPE_INT64 ||
461 Item.val.int64 != 4294967297)
462 return -1;
463
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800464
465
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800466 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700467 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800468 if(Item.uDataType != QCBOR_TYPE_INT64 ||
469 Item.val.int64 != 9223372036854775807LL)
470 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800471
472
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800473 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700474 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800475 if(Item.uDataType != QCBOR_TYPE_UINT64 ||
476 Item.val.uint64 != 18446744073709551615ULL)
477 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800478
479
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800480 if(QCBORDecode_Finish(pDCtx) != QCBOR_SUCCESS) {
481 return -1;
482 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800483
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800484 return 0;
485}
486
487
Laurence Lundblade21d1d812019-09-28 22:47:49 -1000488
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800489/*
490 Tests the decoding of lots of different integers sizes
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +0800491 and values.
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800492 */
Maxim Zhukovd538f0a2022-12-20 20:40:38 +0300493int32_t IntegerValuesParseTest(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800494{
Laurence Lundblade21d1d812019-09-28 22:47:49 -1000495 int nReturn;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800496 QCBORDecodeContext DCtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800497
Laurence Lundblade21d1d812019-09-28 22:47:49 -1000498 QCBORDecode_Init(&DCtx,
499 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedEncodedInts),
500 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800501
Laurence Lundblade21d1d812019-09-28 22:47:49 -1000502 // The really big test of all successes
503 nReturn = IntegerValuesParseTestInternal(&DCtx);
504 if(nReturn) {
505 return nReturn;
506 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800507
Laurence Lundblade21d1d812019-09-28 22:47:49 -1000508 return(nReturn);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800509}
510
511
512/*
Laurence Lundbladeee851742020-01-08 08:37:05 -0800513 Creates a simple CBOR array and returns it in *pEncoded. The array is
514 malloced and needs to be freed. This is used by several tests.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800515
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800516 Two of the inputs can be set. Two other items in the array are fixed.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800517
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800518 */
519
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -0800520static uint8_t spSimpleArrayBuffer[50];
521
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800522static int32_t CreateSimpleArray(int nInt1, int nInt2, uint8_t **pEncoded, size_t *pEncodedLen)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800523{
524 QCBOREncodeContext ECtx;
525 int nReturn = -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800526
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800527 *pEncoded = NULL;
528 *pEncodedLen = INT32_MAX;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800529
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800530 // loop runs CBOR encoding twice. First with no buffer to
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -0800531 // calculate the length so buffer can be allocated correctly,
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800532 // and last with the buffer to do the actual encoding
533 do {
Laurence Lundblade0595e932018-11-02 22:22:47 +0700534 QCBOREncode_Init(&ECtx, (UsefulBuf){*pEncoded, *pEncodedLen});
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800535 QCBOREncode_OpenArray(&ECtx);
536 QCBOREncode_AddInt64(&ECtx, nInt1);
537 QCBOREncode_AddInt64(&ECtx, nInt2);
538 QCBOREncode_AddBytes(&ECtx, ((UsefulBufC) {"galactic", 8}));
539 QCBOREncode_AddBytes(&ECtx, ((UsefulBufC) {"haven token", 11}));
540 QCBOREncode_CloseArray(&ECtx);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800541
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -0800542 if(QCBOREncode_FinishGetSize(&ECtx, pEncodedLen))
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800543 goto Done;
544
545 if(*pEncoded != NULL) {
546 nReturn = 0;
547 goto Done;
548 }
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -0800549
550 // Use static buffer to avoid dependency on malloc()
551 if(*pEncodedLen > sizeof(spSimpleArrayBuffer)) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800552 goto Done;
553 }
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -0800554 *pEncoded = spSimpleArrayBuffer;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800555
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800556 } while(1);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800557
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -0800558Done:
559 return nReturn;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800560}
561
562
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800563/*
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800564 Some basic CBOR with map and array used in a lot of tests.
565 The map labels are all strings
566
Laurence Lundbladecc7da412020-12-27 00:09:07 -0800567 {
568 "first integer": 42,
569 "an array of two strings": [
570 "string1", "string2"
571 ],
572 "map in a map": {
573 "bytes 1": h'78787878',
574 "bytes 2": h'79797979',
575 "another int": 98,
576 "text 2": "lies, damn lies and statistics"
577 }
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900578 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800579 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800580static const uint8_t pValidMapEncoded[] = {
Laurence Lundbladecc7da412020-12-27 00:09:07 -0800581 0xa3, 0x6d, 0x66, 0x69, 0x72, 0x73, 0x74, 0x20,
582 0x69, 0x6e, 0x74, 0x65, 0x67, 0x65, 0x72, 0x18,
583 0x2a, 0x77, 0x61, 0x6e, 0x20, 0x61, 0x72, 0x72,
584 0x61, 0x79, 0x20, 0x6f, 0x66, 0x20, 0x74, 0x77,
585 0x6f, 0x20, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67,
586 0x73, 0x82, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e,
587 0x67, 0x31, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e,
588 0x67, 0x32, 0x6c, 0x6d, 0x61, 0x70, 0x20, 0x69,
589 0x6e, 0x20, 0x61, 0x20, 0x6d, 0x61, 0x70, 0xa4,
590 0x67, 0x62, 0x79, 0x74, 0x65, 0x73, 0x20, 0x31,
591 0x44, 0x78, 0x78, 0x78, 0x78, 0x67, 0x62, 0x79,
592 0x74, 0x65, 0x73, 0x20, 0x32, 0x44, 0x79, 0x79,
593 0x79, 0x79, 0x6b, 0x61, 0x6e, 0x6f, 0x74, 0x68,
594 0x65, 0x72, 0x20, 0x69, 0x6e, 0x74, 0x18, 0x62,
595 0x66, 0x74, 0x65, 0x78, 0x74, 0x20, 0x32, 0x78,
596 0x1e, 0x6c, 0x69, 0x65, 0x73, 0x2c, 0x20, 0x64,
597 0x61, 0x6d, 0x6e, 0x20, 0x6c, 0x69, 0x65, 0x73,
598 0x20, 0x61, 0x6e, 0x64, 0x20, 0x73, 0x74, 0x61,
599 0x74, 0x69, 0x73, 0x74, 0x69, 0x63, 0x73 };
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800600
Laurence Lundbladee2c893c2020-12-26 17:41:53 -0800601
602#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundblade2c1faf92020-06-26 22:43:56 -0700603// Same as above, but with indefinite lengths.
604static const uint8_t pValidMapIndefEncoded[] = {
Laurence Lundbladecc7da412020-12-27 00:09:07 -0800605 0xbf, 0x6d, 0x66, 0x69, 0x72, 0x73, 0x74, 0x20,
606 0x69, 0x6e, 0x74, 0x65, 0x67, 0x65, 0x72, 0x18,
607 0x2a, 0x77, 0x61, 0x6e, 0x20, 0x61, 0x72, 0x72,
608 0x61, 0x79, 0x20, 0x6f, 0x66, 0x20, 0x74, 0x77,
609 0x6f, 0x20, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67,
610 0x73, 0x9f, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e,
611 0x67, 0x31, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e,
612 0x67, 0x32, 0xff, 0x6c, 0x6d, 0x61, 0x70, 0x20,
613 0x69, 0x6e, 0x20, 0x61, 0x20, 0x6d, 0x61, 0x70,
614 0xbf, 0x67, 0x62, 0x79, 0x74, 0x65, 0x73, 0x20,
615 0x31, 0x44, 0x78, 0x78, 0x78, 0x78, 0x67, 0x62,
616 0x79, 0x74, 0x65, 0x73, 0x20, 0x32, 0x44, 0x79,
617 0x79, 0x79, 0x79, 0x6b, 0x61, 0x6e, 0x6f, 0x74,
618 0x68, 0x65, 0x72, 0x20, 0x69, 0x6e, 0x74, 0x18,
619 0x62, 0x66, 0x74, 0x65, 0x78, 0x74, 0x20, 0x32,
620 0x78, 0x1e, 0x6c, 0x69, 0x65, 0x73, 0x2c, 0x20,
621 0x64, 0x61, 0x6d, 0x6e, 0x20, 0x6c, 0x69, 0x65,
622 0x73, 0x20, 0x61, 0x6e, 0x64, 0x20, 0x73, 0x74,
623 0x61, 0x74, 0x69, 0x73, 0x74, 0x69, 0x63, 0x73,
624 0xff, 0xff};
Laurence Lundbladee2c893c2020-12-26 17:41:53 -0800625#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
Laurence Lundblade2c1faf92020-06-26 22:43:56 -0700626
627
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800628static int32_t ParseOrderedArray(const uint8_t *pEncoded,
Laurence Lundblade02625d42020-06-25 14:41:41 -0700629 size_t nLen,
630 int64_t *pInt1,
631 int64_t *pInt2,
632 const uint8_t **pBuf3,
633 size_t *pBuf3Len,
634 const uint8_t **pBuf4,
635 size_t *pBuf4Len)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800636{
637 QCBORDecodeContext DCtx;
638 QCBORItem Item;
639 int nReturn = -1; // assume error until success
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800640
Laurence Lundbladeee851742020-01-08 08:37:05 -0800641 QCBORDecode_Init(&DCtx,
642 (UsefulBufC){pEncoded, nLen},
643 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800644
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800645 // Make sure the first thing is a map
Laurence Lundblade9b334962020-08-27 10:55:53 -0700646 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
647 Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800648 goto Done;
Laurence Lundblade9b334962020-08-27 10:55:53 -0700649 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800650
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800651 // First integer
Laurence Lundblade9b334962020-08-27 10:55:53 -0700652 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
653 Item.uDataType != QCBOR_TYPE_INT64) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800654 goto Done;
Laurence Lundblade9b334962020-08-27 10:55:53 -0700655 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800656 *pInt1 = Item.val.int64;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800657
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800658 // Second integer
Laurence Lundblade9b334962020-08-27 10:55:53 -0700659 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
660 Item.uDataType != QCBOR_TYPE_INT64) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800661 goto Done;
Laurence Lundblade9b334962020-08-27 10:55:53 -0700662 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800663 *pInt2 = Item.val.int64;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800664
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800665 // First string
Laurence Lundblade9b334962020-08-27 10:55:53 -0700666 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
667 Item.uDataType != QCBOR_TYPE_BYTE_STRING) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800668 goto Done;
Laurence Lundblade9b334962020-08-27 10:55:53 -0700669 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800670 *pBuf3 = Item.val.string.ptr;
671 *pBuf3Len = Item.val.string.len;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800672
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800673 // Second string
Laurence Lundblade9b334962020-08-27 10:55:53 -0700674 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
675 Item.uDataType != QCBOR_TYPE_BYTE_STRING) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800676 goto Done;
Laurence Lundblade9b334962020-08-27 10:55:53 -0700677 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800678 *pBuf4 = Item.val.string.ptr;
679 *pBuf4Len = Item.val.string.len;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800680
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800681 nReturn = 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800682
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800683Done:
684 return(nReturn);
685}
686
687
688
Laurence Lundblade9e3651c2018-10-10 11:49:55 +0800689
Maxim Zhukovd538f0a2022-12-20 20:40:38 +0300690int32_t SimpleArrayTest(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800691{
692 uint8_t *pEncoded;
693 size_t nEncodedLen;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800694
Laurence Lundblade5e390822019-01-06 12:35:01 -0800695 int64_t i1=0, i2=0;
696 size_t i3=0, i4=0;
697 const uint8_t *s3= (uint8_t *)"";
698 const uint8_t *s4= (uint8_t *)"";
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800699
700
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800701 if(CreateSimpleArray(23, 6000, &pEncoded, &nEncodedLen) < 0) {
702 return(-1);
703 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800704
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800705 ParseOrderedArray(pEncoded, nEncodedLen, &i1, &i2, &s3, &i3, &s4, &i4);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800706
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800707 if(i1 != 23 ||
708 i2 != 6000 ||
709 i3 != 8 ||
710 i4 != 11 ||
Laurence Lundbladea44d5062018-10-17 18:45:12 +0530711 memcmp("galactic", s3, 8) !=0 ||
712 memcmp("haven token", s4, 11) !=0) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800713 return(-1);
714 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800715
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800716 return(0);
717}
718
719
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700720/*
721 [
722 0,
723 [],
724 [
725 [],
726 [
727 0
728 ],
729 {},
730 {
731 1: {},
732 2: {},
733 3: []
734 }
735 ]
736 ]
737 */
Laurence Lundbladecc7da412020-12-27 00:09:07 -0800738static uint8_t sEmpties[] = {
739 0x83, 0x00, 0x80, 0x84, 0x80, 0x81, 0x00, 0xa0,
740 0xa3, 0x01, 0xa0, 0x02, 0xa0, 0x03, 0x80};
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700741
Laurence Lundbladee2c893c2020-12-26 17:41:53 -0800742#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundblade02625d42020-06-25 14:41:41 -0700743/* Same as above, but with indefinte lengths */
Laurence Lundbladecc7da412020-12-27 00:09:07 -0800744static const uint8_t sEmptiesIndef[] = {
Laurence Lundblade02625d42020-06-25 14:41:41 -07007450x9F,
Laurence Lundblade2c1faf92020-06-26 22:43:56 -0700746 0x00,
747 0x9F,
748 0xFF,
749 0x9F,
750 0x9F,
751 0xFF,
752 0x9F,
753 0x00,
754 0xFF,
755 0xBF,
756 0xFF,
757 0xBF,
758 0x01,
759 0xBF,
760 0xFF,
761 0x02,
762 0xBF,
763 0xFF,
764 0x03,
765 0x9F,
766 0xFF,
767 0xFF,
768 0xFF,
Laurence Lundblade02625d42020-06-25 14:41:41 -0700769 0xFF};
Laurence Lundbladee2c893c2020-12-26 17:41:53 -0800770#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
Laurence Lundblade02625d42020-06-25 14:41:41 -0700771
772
773static int32_t CheckEmpties(UsefulBufC input, bool bCheckCounts)
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700774{
775 QCBORDecodeContext DCtx;
776 QCBORItem Item;
777
Laurence Lundbladeee851742020-01-08 08:37:05 -0800778 QCBORDecode_Init(&DCtx,
Laurence Lundblade02625d42020-06-25 14:41:41 -0700779 input,
Laurence Lundbladeee851742020-01-08 08:37:05 -0800780 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700781
782 // Array with 3 items
783 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
784 Item.uDataType != QCBOR_TYPE_ARRAY ||
785 Item.uNestingLevel != 0 ||
786 Item.uNextNestLevel != 1 ||
Laurence Lundblade02625d42020-06-25 14:41:41 -0700787 (bCheckCounts && Item.val.uCount != 3)) {
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700788 return -1;
789 }
790
791 // An integer 0
792 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
793 Item.uDataType != QCBOR_TYPE_INT64 ||
794 Item.uNestingLevel != 1 ||
795 Item.uNextNestLevel != 1 ||
796 Item.val.uint64 != 0) {
797 return -2;
798 }
799
800 // An empty array
801 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
802 Item.uDataType != QCBOR_TYPE_ARRAY ||
803 Item.uNestingLevel != 1 ||
804 Item.uNextNestLevel != 1 ||
Laurence Lundblade02625d42020-06-25 14:41:41 -0700805 (bCheckCounts && Item.val.uCount != 0)) {
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700806 return -3;
807 }
808
809 // An array with 4 items
810 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
811 Item.uDataType != QCBOR_TYPE_ARRAY ||
812 Item.uNestingLevel != 1 ||
813 Item.uNextNestLevel != 2 ||
Laurence Lundblade02625d42020-06-25 14:41:41 -0700814 (bCheckCounts && Item.val.uCount != 4)) {
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700815 return -4;
816 }
817
818 // An empty array
819 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
820 Item.uDataType != QCBOR_TYPE_ARRAY ||
821 Item.uNestingLevel != 2 ||
822 Item.uNextNestLevel != 2 ||
Laurence Lundblade02625d42020-06-25 14:41:41 -0700823 (bCheckCounts && Item.val.uCount != 0)) {
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700824 return -5;
825 }
826
827 // An array with 1 item
828 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
829 Item.uDataType != QCBOR_TYPE_ARRAY ||
830 Item.uNestingLevel != 2 ||
831 Item.uNextNestLevel != 3 ||
Laurence Lundblade02625d42020-06-25 14:41:41 -0700832 (bCheckCounts && Item.val.uCount != 1)) {
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700833 return -6;
834 }
835
836 // An integer 0
837 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
838 Item.uDataType != QCBOR_TYPE_INT64 ||
839 Item.uNestingLevel != 3 ||
840 Item.uNextNestLevel != 2 ||
841 Item.val.uint64 != 0) {
842 return -7;
843 }
844
845 // An empty map
846 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
847 Item.uDataType != QCBOR_TYPE_MAP ||
848 Item.uNestingLevel != 2 ||
849 Item.uNextNestLevel != 2 ||
Laurence Lundblade02625d42020-06-25 14:41:41 -0700850 (bCheckCounts && Item.val.uCount != 0)) {
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700851 return -8;
852 }
853
Laurence Lundblade5e87da62020-06-07 03:24:28 -0700854 // A map with 3 items
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700855 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
856 Item.uDataType != QCBOR_TYPE_MAP ||
857 Item.uNestingLevel != 2 ||
858 Item.uNextNestLevel != 3 ||
Laurence Lundblade02625d42020-06-25 14:41:41 -0700859 (bCheckCounts && Item.val.uCount != 3)) {
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700860 return -9;
861 }
862
863 // An empty map
864 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
865 Item.uDataType != QCBOR_TYPE_MAP ||
866 Item.uNestingLevel != 3 ||
867 Item.uNextNestLevel != 3 ||
Laurence Lundblade02625d42020-06-25 14:41:41 -0700868 (bCheckCounts && Item.val.uCount != 0)) {
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700869 return -10;
870 }
871
872 // An empty map
873 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
874 Item.uDataType != QCBOR_TYPE_MAP ||
875 Item.uNestingLevel != 3 ||
876 Item.uNextNestLevel != 3 ||
Laurence Lundblade02625d42020-06-25 14:41:41 -0700877 (bCheckCounts && Item.val.uCount != 0)) {
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700878 return -11;
879 }
880
881 // An empty array
882 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
883 Item.uDataType != QCBOR_TYPE_ARRAY ||
884 Item.uNestingLevel != 3 ||
885 Item.uNextNestLevel != 0 ||
Laurence Lundblade02625d42020-06-25 14:41:41 -0700886 (bCheckCounts && Item.val.uCount != 0)) {
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700887 return -12;
888 }
889
890 if(QCBORDecode_Finish(&DCtx) != QCBOR_SUCCESS) {
891 return -13;
892 }
Laurence Lundblade02625d42020-06-25 14:41:41 -0700893 return 0;
894}
895
896
Maxim Zhukovd538f0a2022-12-20 20:40:38 +0300897int32_t EmptyMapsAndArraysTest(void)
Laurence Lundblade02625d42020-06-25 14:41:41 -0700898{
899 int nResult;
900 nResult = CheckEmpties(UsefulBuf_FROM_BYTE_ARRAY_LITERAL(sEmpties),
901 true);
902 if(nResult) {
903 return nResult;
904 }
905
Laurence Lundbladee2c893c2020-12-26 17:41:53 -0800906#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundblade02625d42020-06-25 14:41:41 -0700907 nResult = CheckEmpties(UsefulBuf_FROM_BYTE_ARRAY_LITERAL(sEmptiesIndef),
908 false);
909
910 if(nResult) {
911 return nResult -100;
912 }
Laurence Lundbladee2c893c2020-12-26 17:41:53 -0800913#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700914
915 return 0;
916}
917
Laurence Lundblade4c532ca2021-02-18 21:31:49 -0700918
919static const uint8_t sEmptyMap[] = {
Michael Richardson87de9af2021-02-18 23:13:31 -0500920 0xA1, //# map(1)
921 0x02, //# unsigned(2)
922 0xA0, //# map(0)
923};
924
925int32_t ParseEmptyMapInMapTest(void)
926{
927 QCBORDecodeContext DCtx;
928 QCBORItem Item;
929 int nReturn = 0;
Laurence Lundblade4c532ca2021-02-18 21:31:49 -0700930 QCBORError uErr;
Michael Richardson87de9af2021-02-18 23:13:31 -0500931
932 QCBORDecode_Init(&DCtx,
933 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(sEmptyMap),
934 QCBOR_DECODE_MODE_NORMAL);
935
936 /* now open the first Map */
Laurence Lundblade4c532ca2021-02-18 21:31:49 -0700937 uErr = QCBORDecode_GetNext(&DCtx, &Item);
938 if(uErr != QCBOR_SUCCESS ||
Michael Richardson87de9af2021-02-18 23:13:31 -0500939 Item.uDataType != QCBOR_TYPE_MAP) {
940 nReturn = -3;
941 goto done;
942 }
943
944 if(QCBORDecode_GetNext(&DCtx, &Item) != 0) {
945 nReturn = -1;
946 goto done;
947 }
948 if(Item.uDataType != QCBOR_TYPE_MAP ||
949 Item.uNestingLevel != 1 ||
950 Item.label.int64 != 2) {
951 nReturn = -2;
952 goto done;
953 }
954
955 done:
956 return(nReturn);
957}
958
Laurence Lundblade4c532ca2021-02-18 21:31:49 -0700959
Michael Richardson87de9af2021-02-18 23:13:31 -0500960/* [[[[[[[[[[]]]]]]]]]] */
Laurence Lundbladecc7da412020-12-27 00:09:07 -0800961static const uint8_t spDeepArrays[] = {
962 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81,
963 0x81, 0x80};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800964
Maxim Zhukovd538f0a2022-12-20 20:40:38 +0300965int32_t ParseDeepArrayTest(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800966{
967 QCBORDecodeContext DCtx;
968 int nReturn = 0;
969 int i;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800970
Laurence Lundbladeee851742020-01-08 08:37:05 -0800971 QCBORDecode_Init(&DCtx,
972 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spDeepArrays),
973 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800974
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800975 for(i = 0; i < 10; i++) {
976 QCBORItem Item;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800977
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800978 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
979 Item.uDataType != QCBOR_TYPE_ARRAY ||
980 Item.uNestingLevel != i) {
981 nReturn = -1;
982 break;
983 }
984 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800985
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800986 return(nReturn);
987}
988
Laurence Lundbladecc7da412020-12-27 00:09:07 -0800989/* Big enough to test nesting to the depth of 24
990 [[[[[[[[[[[[[[[[[[[[[[[[[]]]]]]]]]]]]]]]]]]]]]]]]]
991 */
992static const uint8_t spTooDeepArrays[] = {
993 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81,
994 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81,
995 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81,
996 0x80};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800997
Maxim Zhukovd538f0a2022-12-20 20:40:38 +0300998int32_t ParseTooDeepArrayTest(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800999{
1000 QCBORDecodeContext DCtx;
1001 int nReturn = 0;
1002 int i;
1003 QCBORItem Item;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001004
1005
Laurence Lundbladeee851742020-01-08 08:37:05 -08001006 QCBORDecode_Init(&DCtx,
1007 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTooDeepArrays),
1008 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001009
Laurence Lundblade3e0b2302023-12-04 14:02:38 -07001010 for(i = 0; i < QCBOR_MAX_ARRAY_NESTING; i++) {
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001011
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001012 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
1013 Item.uDataType != QCBOR_TYPE_ARRAY ||
1014 Item.uNestingLevel != i) {
1015 nReturn = -1;
1016 break;
1017 }
1018 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001019
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001020 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_ARRAY_DECODE_NESTING_TOO_DEEP)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001021 nReturn = -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001022
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001023 return(nReturn);
1024}
1025
1026
1027
1028
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03001029int32_t ShortBufferParseTest(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001030{
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001031 int nResult = 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001032
Laurence Lundblade06350ea2020-01-27 19:32:40 -08001033 for(size_t nNum = sizeof(spExpectedEncodedInts)-1; nNum; nNum--) {
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001034 QCBORDecodeContext DCtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001035
Laurence Lundbladeee851742020-01-08 08:37:05 -08001036 QCBORDecode_Init(&DCtx,
1037 (UsefulBufC){spExpectedEncodedInts, nNum},
1038 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001039
Laurence Lundblade06350ea2020-01-27 19:32:40 -08001040 const int nErr = IntegerValuesParseTestInternal(&DCtx);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001041
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001042 if(nErr != QCBOR_ERR_HIT_END && nErr != QCBOR_ERR_NO_MORE_ITEMS) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001043 nResult = -1;
1044 goto Done;
1045 }
1046 }
1047Done:
1048 return nResult;
1049}
1050
1051
Laurence Lundblade9e3651c2018-10-10 11:49:55 +08001052
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03001053int32_t ShortBufferParseTest2(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001054{
1055 uint8_t *pEncoded;
1056 int nReturn;
1057 size_t nEncodedLen;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001058
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001059 int64_t i1, i2;
1060 size_t i3, i4;
1061 const uint8_t *s3, *s4;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001062
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001063 nReturn = 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001064
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001065 if(CreateSimpleArray(23, 6000, &pEncoded, &nEncodedLen) < 0) {
1066 return(-1);
1067 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001068
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001069 for(nEncodedLen--; nEncodedLen; nEncodedLen--) {
Laurence Lundblade9b334962020-08-27 10:55:53 -07001070 int nResult = ParseOrderedArray(pEncoded, (uint32_t)nEncodedLen, &i1,
1071 &i2, &s3, &i3, &s4, &i4);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001072 if(nResult == 0) {
1073 nReturn = -1;
1074 }
1075 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001076
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001077 return(nReturn);
1078}
1079
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301080/*
1081 Decode and thoroughly check a moderately complex
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001082 set of maps. Can be run in QCBOR_DECODE_MODE_NORMAL or in
1083 QCBOR_DECODE_MODE_MAP_STRINGS_ONLY.
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301084 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001085static int32_t ParseMapTest1(QCBORDecodeMode nMode)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001086{
1087 QCBORDecodeContext DCtx;
1088 QCBORItem Item;
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001089 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001090
Laurence Lundbladeee851742020-01-08 08:37:05 -08001091 QCBORDecode_Init(&DCtx,
1092 (UsefulBufC){pValidMapEncoded, sizeof(pValidMapEncoded)},
1093 nMode);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001094
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001095 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001096 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001097 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001098 if(Item.uDataType != QCBOR_TYPE_MAP ||
1099 Item.val.uCount != 3)
1100 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001101
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001102 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001103 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001104 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07001105
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001106 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001107 Item.uDataType != QCBOR_TYPE_INT64 ||
1108 Item.val.int64 != 42 ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301109 Item.uDataAlloc ||
1110 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001111 UsefulBufCompareToSZ(Item.label.string, "first integer")) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001112 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001113 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001114
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001115 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001116 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001117 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001118 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301119 Item.uDataAlloc ||
1120 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001121 UsefulBufCompareToSZ(Item.label.string, "an array of two strings") ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001122 Item.uDataType != QCBOR_TYPE_ARRAY ||
1123 Item.val.uCount != 2)
1124 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001125
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001126 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001127 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001128 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001129 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301130 Item.uDataAlloc ||
1131 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001132 UsefulBufCompareToSZ(Item.val.string, "string1")) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001133 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001134 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001135
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001136 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001137 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001138 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001139 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301140 Item.uDataAlloc ||
1141 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001142 UsefulBufCompareToSZ(Item.val.string, "string2")) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001143 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001144 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001145
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001146 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001147 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001148 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001149 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301150 Item.uDataAlloc ||
1151 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001152 UsefulBufCompareToSZ(Item.label.string, "map in a map") ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001153 Item.uDataType != QCBOR_TYPE_MAP ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001154 Item.val.uCount != 4) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001155 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001156 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001157
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001158 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001159 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001160 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001161 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001162 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("bytes 1"))||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001163 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301164 Item.uDataAlloc ||
1165 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001166 UsefulBufCompareToSZ(Item.val.string, "xxxx")) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001167 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001168 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001169
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001170 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001171 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001172 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001173 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001174 UsefulBufCompareToSZ(Item.label.string, "bytes 2") ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001175 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301176 Item.uDataAlloc ||
1177 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001178 UsefulBufCompareToSZ(Item.val.string, "yyyy")) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001179 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001180 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001181
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001182 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001183 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001184 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001185 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301186 Item.uDataAlloc ||
1187 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001188 UsefulBufCompareToSZ(Item.label.string, "another int") ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001189 Item.uDataType != QCBOR_TYPE_INT64 ||
1190 Item.val.int64 != 98)
1191 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001192
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001193 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001194 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001195 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001196 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001197 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("text 2"))||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001198 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301199 Item.uDataAlloc ||
1200 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001201 UsefulBufCompareToSZ(Item.val.string, "lies, damn lies and statistics")) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001202 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001203 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001204
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001205 return 0;
1206}
1207
1208
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001209/*
1210 Decode and thoroughly check a moderately complex
Laurence Lundbladed4cc1032020-10-12 04:19:47 -07001211 set of maps in the QCBOR_DECODE_MODE_MAP_AS_ARRAY mode.
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001212 */
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03001213int32_t ParseMapAsArrayTest(void)
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001214{
1215 QCBORDecodeContext DCtx;
1216 QCBORItem Item;
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001217 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001218
Laurence Lundbladeee851742020-01-08 08:37:05 -08001219 QCBORDecode_Init(&DCtx,
1220 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded),
1221 QCBOR_DECODE_MODE_MAP_AS_ARRAY);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001222
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001223 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001224 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001225 }
Laurence Lundbladed61cbf32018-12-09 11:42:21 -08001226 if(Item.uDataType != QCBOR_TYPE_MAP_AS_ARRAY ||
1227 Item.val.uCount != 6) {
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001228 return -1;
1229 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001230
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001231 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001232 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001233 }
1234 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1235 Item.uDataAlloc ||
1236 Item.uLabelAlloc ||
1237 Item.uLabelType != QCBOR_TYPE_NONE ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001238 UsefulBufCompareToSZ(Item.val.string, "first integer")) {
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001239 return -2;
1240 }
1241
1242 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001243 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001244 }
1245 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1246 Item.uDataType != QCBOR_TYPE_INT64 ||
1247 Item.val.int64 != 42 ||
1248 Item.uDataAlloc ||
1249 Item.uLabelAlloc) {
1250 return -3;
1251 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001252
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001253 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001254 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001255 }
1256 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1257 Item.uDataAlloc ||
1258 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001259 UsefulBufCompareToSZ(Item.val.string, "an array of two strings") ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001260 Item.uDataType != QCBOR_TYPE_TEXT_STRING) {
1261 return -4;
1262 }
1263
1264 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001265 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001266 }
1267 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1268 Item.uDataAlloc ||
1269 Item.uLabelAlloc ||
1270 Item.uDataType != QCBOR_TYPE_ARRAY ||
1271 Item.val.uCount != 2) {
1272 return -5;
1273 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001274
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001275 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001276 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001277 }
1278 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1279 Item.val.string.len != 7 ||
1280 Item.uDataAlloc ||
1281 Item.uLabelAlloc ||
1282 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("string1"))) {
1283 return -6;
1284 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001285
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001286 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001287 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001288 }
1289 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1290 Item.uDataAlloc ||
1291 Item.uLabelAlloc ||
1292 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("string2"))) {
1293 return -7;
1294 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001295
1296
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001297 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001298 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001299 }
1300 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1301 Item.uDataAlloc ||
1302 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001303 UsefulBufCompareToSZ(Item.val.string, "map in a map")) {
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001304 return -8;
1305 }
1306
1307 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001308 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001309 }
1310 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1311 Item.uDataAlloc ||
1312 Item.uLabelAlloc ||
Laurence Lundbladed61cbf32018-12-09 11:42:21 -08001313 Item.uDataType != QCBOR_TYPE_MAP_AS_ARRAY ||
1314 Item.val.uCount != 8) {
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001315 return -9;
1316 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001317
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001318 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001319 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001320 }
1321 if(Item.uLabelType != QCBOR_TYPE_NONE ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001322 UsefulBufCompareToSZ(Item.val.string, "bytes 1") ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001323 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1324 Item.uDataAlloc ||
1325 Item.uLabelAlloc) {
1326 return -10;
1327 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001328
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001329 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001330 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001331 }
1332 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1333 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
1334 Item.uDataAlloc ||
1335 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001336 UsefulBufCompareToSZ(Item.val.string, "xxxx")) {
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001337 return -11;
1338 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001339
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001340 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001341 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001342 }
1343 if(Item.uLabelType != QCBOR_TYPE_NONE ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001344 UsefulBufCompareToSZ(Item.val.string, "bytes 2") ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001345 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1346 Item.uDataAlloc ||
1347 Item.uLabelAlloc) {
1348 return -12;
1349 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001350
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001351 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001352 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001353 }
1354 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1355 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
1356 Item.uDataAlloc ||
1357 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001358 UsefulBufCompareToSZ(Item.val.string, "yyyy")) {
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001359 return -13;
1360 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001361
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001362 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001363 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001364 }
1365 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1366 Item.uDataAlloc ||
1367 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001368 UsefulBufCompareToSZ(Item.val.string, "another int") ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001369 Item.uDataType != QCBOR_TYPE_TEXT_STRING) {
1370 return -14;
1371 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001372
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001373 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001374 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001375 }
1376 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1377 Item.uDataAlloc ||
1378 Item.uLabelAlloc ||
1379 Item.uDataType != QCBOR_TYPE_INT64 ||
1380 Item.val.int64 != 98) {
1381 return -15;
1382 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001383
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001384 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001385 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001386 }
1387 if(Item.uLabelType != QCBOR_TYPE_NONE ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001388 UsefulBufCompareToSZ(Item.val.string, "text 2") ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001389 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1390 Item.uDataAlloc ||
1391 Item.uLabelAlloc) {
1392 return -16;
1393 }
1394
1395 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001396 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001397 }
1398 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1399 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1400 Item.uDataAlloc ||
1401 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001402 UsefulBufCompareToSZ(Item.val.string, "lies, damn lies and statistics")) {
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001403 return -17;
1404 }
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07001405
1406
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001407 /*
1408 Test with map that nearly QCBOR_MAX_ITEMS_IN_ARRAY items in a
1409 map that when interpreted as an array will be too many. Test
1410 data just has the start of the map, not all the items in the map.
1411 */
1412 static const uint8_t pTooLargeMap[] = {0xb9, 0xff, 0xfd};
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07001413
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001414 QCBORDecode_Init(&DCtx,
1415 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pTooLargeMap),
1416 QCBOR_DECODE_MODE_MAP_AS_ARRAY);
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07001417
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001418 if((QCBOR_ERR_ARRAY_DECODE_TOO_LONG != QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001419 return -50;
1420 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001421
Laurence Lundbladed4cc1032020-10-12 04:19:47 -07001422 // TODO: test decoding of labels that are arrays or such
1423 // TODO: test spiffy decoding of QCBOR_DECODE_MODE_MAP_AS_ARRAY
1424
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001425 return 0;
1426}
1427
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001428
1429/*
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301430 Fully or partially decode pValidMapEncoded. When
1431 partially decoding check for the right error code.
1432 How much partial decoding depends on nLevel.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001433
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301434 The partial decodes test error conditions of
1435 incomplete encoded input.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001436
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301437 This could be combined with the above test
1438 and made prettier and maybe a little more
1439 thorough.
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001440 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001441static int32_t ExtraBytesTest(int nLevel)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001442{
1443 QCBORDecodeContext DCtx;
1444 QCBORItem Item;
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001445 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001446
Laurence Lundbladeee851742020-01-08 08:37:05 -08001447 QCBORDecode_Init(&DCtx,
1448 (UsefulBufC){pValidMapEncoded, sizeof(pValidMapEncoded)},
1449 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001450
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001451 if(nLevel < 1) {
1452 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_EXTRA_BYTES) {
1453 return -1;
1454 } else {
1455 return 0;
1456 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001457 }
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301458
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001459
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001460 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001461 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001462 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001463 if(Item.uDataType != QCBOR_TYPE_MAP ||
1464 Item.val.uCount != 3)
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001465 return -2;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001466
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001467 if(nLevel < 2) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001468 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
1469 return -3;
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001470 } else {
1471 return 0;
1472 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001473 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001474
1475
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001476 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001477 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001478 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001479 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001480 Item.uDataType != QCBOR_TYPE_INT64 ||
1481 Item.val.uCount != 42 ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001482 UsefulBufCompareToSZ(Item.label.string, "first integer")) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001483 return -4;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001484 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001485
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001486 if(nLevel < 3) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001487 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
1488 return -5;
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001489 } else {
1490 return 0;
1491 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001492 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001493
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001494 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001495 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001496 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001497 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001498 UsefulBufCompareToSZ(Item.label.string, "an array of two strings") ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001499 Item.uDataType != QCBOR_TYPE_ARRAY ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001500 Item.val.uCount != 2) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001501 return -6;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001502 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001503
1504
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001505 if(nLevel < 4) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001506 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
1507 return -7;
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001508 } else {
1509 return 0;
1510 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001511 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001512
1513
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001514 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001515 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001516 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001517 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001518 UsefulBufCompareToSZ(Item.val.string, "string1")) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001519 return -8;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001520 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001521
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001522 if(nLevel < 5) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001523 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
1524 return -9;
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001525 } else {
1526 return 0;
1527 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001528 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001529
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001530 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001531 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001532 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001533 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001534 UsefulBufCompareToSZ(Item.val.string, "string2")) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001535 return -10;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001536 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001537
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001538 if(nLevel < 6) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001539 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
1540 return -11;
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001541 } else {
1542 return 0;
1543 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001544 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001545
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001546 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001547 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001548 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001549 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001550 UsefulBufCompareToSZ(Item.label.string, "map in a map") ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001551 Item.uDataType != QCBOR_TYPE_MAP ||
1552 Item.val.uCount != 4)
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001553 return -12;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001554
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001555 if(nLevel < 7) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001556 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
1557 return -13;
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001558 } else {
1559 return 0;
1560 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001561 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001562
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001563 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001564 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001565 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001566 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001567 UsefulBufCompareToSZ(Item.label.string, "bytes 1") ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001568 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001569 UsefulBufCompareToSZ(Item.val.string, "xxxx")) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001570 return -14;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001571 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001572
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001573 if(nLevel < 8) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001574 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
1575 return -15;
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001576 } else {
1577 return 0;
1578 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001579 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001580
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001581 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001582 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001583 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001584 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001585 UsefulBufCompareToSZ(Item.label.string, "bytes 2") ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001586 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001587 UsefulBufCompareToSZ(Item.val.string, "yyyy")) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001588 return -16;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001589 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001590
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001591 if(nLevel < 9) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001592 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
1593 return -17;
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001594 } else {
1595 return 0;
1596 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001597 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001598
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001599 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001600 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001601 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001602 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001603 UsefulBufCompareToSZ(Item.label.string, "another int") ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001604 Item.uDataType != QCBOR_TYPE_INT64 ||
1605 Item.val.int64 != 98)
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001606 return -18;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001607
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001608 if(nLevel < 10) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001609 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
1610 return -19;
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001611 } else {
1612 return 0;
1613 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001614 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001615
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001616 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001617 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001618 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001619 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001620 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("text 2"))||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001621 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001622 UsefulBufCompareToSZ(Item.val.string, "lies, damn lies and statistics")) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001623 return -20;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001624 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001625
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301626 if(QCBORDecode_Finish(&DCtx)) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001627 return -21;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001628 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001629
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001630 return 0;
1631}
1632
1633
1634
Laurence Lundblade844bb5c2020-03-01 17:27:25 -08001635
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03001636int32_t ParseMapTest(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001637{
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001638 // Parse a moderatly complex map structure very thoroughly
1639 int32_t nResult = ParseMapTest1(QCBOR_DECODE_MODE_NORMAL);
1640 if(nResult) {
1641 return nResult;
1642 }
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08001643
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001644 // Again, but in strings-only mode. It should succeed since the input
1645 // map has only string labels.
1646 nResult = ParseMapTest1(QCBOR_DECODE_MODE_MAP_STRINGS_ONLY);
1647 if(nResult) {
1648 return nResult;
1649 }
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08001650
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001651 // Again, but try to finish the decoding before the end of the
1652 // input at 10 different place and see that the right error code
1653 // is returned.
1654 for(int i = 0; i < 10; i++) {
1655 nResult = ExtraBytesTest(i);
1656 if(nResult) {
1657 break;
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001658 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001659 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001660
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001661 return nResult;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001662}
1663
1664
Laurence Lundbladecc7da412020-12-27 00:09:07 -08001665/* The simple-values including some not well formed */
1666static const uint8_t spSimpleValues[] = {
1667 0x8a, 0xf4, 0xf5, 0xf6, 0xf7, 0xff, 0xe0, 0xf3,
1668 0xf8, 0x00, 0xf8, 0x13, 0xf8, 0x1f, 0xf8, 0x20,
1669 0xf8, 0xff};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001670
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03001671int32_t ParseSimpleTest(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001672{
1673 QCBORDecodeContext DCtx;
1674 QCBORItem Item;
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001675 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001676
1677
Laurence Lundbladeee851742020-01-08 08:37:05 -08001678 QCBORDecode_Init(&DCtx,
1679 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSimpleValues),
1680 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001681
1682
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001683 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001684 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001685 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
1686 Item.val.uCount != 10)
1687 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001688
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001689 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001690 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001691 if(Item.uDataType != QCBOR_TYPE_FALSE)
1692 return -1;
1693
1694 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001695 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001696 if(Item.uDataType != QCBOR_TYPE_TRUE)
1697 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001698
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001699 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001700 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001701 if(Item.uDataType != QCBOR_TYPE_NULL)
1702 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001703
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001704 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001705 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001706 if(Item.uDataType != QCBOR_TYPE_UNDEF)
1707 return -1;
1708
1709 // A break
Laurence Lundblade9e3651c2018-10-10 11:49:55 +08001710 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_BREAK)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001711 return -1;
1712
1713 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001714 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001715 if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 0)
1716 return -1;
1717
1718 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001719 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001720 if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 19)
1721 return -1;
1722
Laurence Lundblade077475f2019-04-26 09:06:33 -07001723 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_TYPE_7)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001724 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001725
Laurence Lundblade077475f2019-04-26 09:06:33 -07001726 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_TYPE_7)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001727 return -1;
1728
Laurence Lundblade077475f2019-04-26 09:06:33 -07001729 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_TYPE_7)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001730 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001731
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001732 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001733 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001734 if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 32)
1735 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001736
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001737 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001738 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001739 if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 255)
1740 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001741
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001742 return 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001743
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001744}
1745
1746
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03001747int32_t NotWellFormedTests(void)
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001748{
1749 // Loop over all the not-well-formed instance of CBOR
1750 // that are test vectors in not_well_formed_cbor.h
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08001751 const uint16_t nArraySize = C_ARRAY_COUNT(paNotWellFormedCBOR,
1752 struct someBinaryBytes);
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001753 for(uint16_t nIterate = 0; nIterate < nArraySize; nIterate++) {
1754 const struct someBinaryBytes *pBytes = &paNotWellFormedCBOR[nIterate];
1755 const UsefulBufC Input = (UsefulBufC){pBytes->p, pBytes->n};
1756
Laurence Lundblade37286c02022-09-03 10:05:02 -07001757 if(nIterate == 86) {
1758 nIterate = 86;
1759 }
1760
Laurence Lundbladeee851742020-01-08 08:37:05 -08001761 // Set up decoder context. String allocator needed for indefinite
1762 // string test cases
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001763 QCBORDecodeContext DCtx;
1764 QCBORDecode_Init(&DCtx, Input, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08001765#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001766 UsefulBuf_MAKE_STACK_UB(Pool, 100);
1767 QCBORDecode_SetMemPool(&DCtx, Pool, 0);
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08001768#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001769
1770 // Loop getting items until no more to get
Laurence Lundbladef71e1622020-08-06 18:52:13 -07001771 QCBORError uCBORError;
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001772 do {
1773 QCBORItem Item;
1774
Laurence Lundbladef71e1622020-08-06 18:52:13 -07001775 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
1776 } while(uCBORError == QCBOR_SUCCESS);
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001777
1778 // Every test vector must fail with
1779 // a not-well-formed error. If not
1780 // this test fails.
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001781 if(!QCBORDecode_IsNotWellFormedError(uCBORError) &&
Laurence Lundbladef71e1622020-08-06 18:52:13 -07001782 uCBORError != QCBOR_ERR_NO_MORE_ITEMS) {
Laurence Lundblade4bf6e712020-12-10 11:53:21 -08001783 /* Return index of failure and QCBOR error in the result */
1784 return (int32_t)(nIterate * 100 + uCBORError);
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001785 }
1786 }
1787 return 0;
1788}
1789
1790
Laurence Lundblade4bf6e712020-12-10 11:53:21 -08001791// TODO: add a test index and report it so it is eaier to figure out which test failed.
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001792struct FailInput {
Laurence Lundblade59289e52019-12-30 13:44:37 -08001793 UsefulBufC Input;
1794 QCBORError nError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001795};
1796
Laurence Lundblade59289e52019-12-30 13:44:37 -08001797
Laurence Lundbladecc7da412020-12-27 00:09:07 -08001798static int32_t ProcessFailures(const struct FailInput *pFailInputs, size_t nNumFails)
Laurence Lundblade59289e52019-12-30 13:44:37 -08001799{
Laurence Lundbladecc7da412020-12-27 00:09:07 -08001800 for(const struct FailInput *pF = pFailInputs; pF < pFailInputs + nNumFails; pF++) {
Laurence Lundblade59289e52019-12-30 13:44:37 -08001801 QCBORDecodeContext DCtx;
Laurence Lundblade4bf6e712020-12-10 11:53:21 -08001802 QCBORError uCBORError;
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08001803
Laurence Lundblade59289e52019-12-30 13:44:37 -08001804 QCBORDecode_Init(&DCtx, pF->Input, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08001805
1806#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
Laurence Lundblade4bf6e712020-12-10 11:53:21 -08001807 // Set up the decoding context including a memory pool so that
1808 // indefinite length items can be checked
Laurence Lundblade59289e52019-12-30 13:44:37 -08001809 UsefulBuf_MAKE_STACK_UB(Pool, 100);
Laurence Lundblade830fbf92020-05-31 17:22:33 -07001810
Laurence Lundblade4bf6e712020-12-10 11:53:21 -08001811 uCBORError = QCBORDecode_SetMemPool(&DCtx, Pool, 0);
1812 if(uCBORError) {
Laurence Lundblade59289e52019-12-30 13:44:37 -08001813 return -9;
1814 }
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08001815#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
1816
Laurence Lundblade37286c02022-09-03 10:05:02 -07001817 const size_t nIndexx = (size_t)(pF - pFailInputs);
1818 if(nIndexx == 8) {
1819 uCBORError = 9;
1820 }
1821
Laurence Lundbladecf41c522021-02-20 10:19:07 -07001822
Laurence Lundblade59289e52019-12-30 13:44:37 -08001823 // Iterate until there is an error of some sort error
1824 QCBORItem Item;
1825 do {
Laurence Lundblade02625d42020-06-25 14:41:41 -07001826 // Set to something none-zero, something other than QCBOR_TYPE_NONE
Laurence Lundblade59289e52019-12-30 13:44:37 -08001827 memset(&Item, 0x33, sizeof(Item));
1828
Laurence Lundblade4bf6e712020-12-10 11:53:21 -08001829 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
1830 } while(uCBORError == QCBOR_SUCCESS);
1831
1832
Laurence Lundblade59289e52019-12-30 13:44:37 -08001833
1834 // Must get the expected error or the this test fails
1835 // The data and label type must also be QCBOR_TYPE_NONE
Laurence Lundblade4bf6e712020-12-10 11:53:21 -08001836 if(uCBORError != pF->nError ||
Laurence Lundblade59289e52019-12-30 13:44:37 -08001837 Item.uDataType != QCBOR_TYPE_NONE ||
1838 Item.uLabelType != QCBOR_TYPE_NONE) {
Laurence Lundblade06350ea2020-01-27 19:32:40 -08001839 // return index of CBOR + 100
Laurence Lundblade830fbf92020-05-31 17:22:33 -07001840 const size_t nIndex = (size_t)(pF - pFailInputs);
Laurence Lundblade4bf6e712020-12-10 11:53:21 -08001841 return (int32_t)(nIndex * 100 + uCBORError);
Laurence Lundblade59289e52019-12-30 13:44:37 -08001842 }
1843 }
1844
1845 return 0;
1846}
1847
1848
Laurence Lundbladecc7da412020-12-27 00:09:07 -08001849static const struct FailInput Failures[] = {
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001850 // Most of this is copied from not_well_formed.h. Here the error code
1851 // returned is also checked.
1852
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08001853#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001854 // Indefinite length strings must be closed off
1855 // An indefinite length byte string not closed off
1856 { {(uint8_t[]){0x5f, 0x41, 0x00}, 3}, QCBOR_ERR_HIT_END },
1857 // An indefinite length text string not closed off
1858 { {(uint8_t[]){0x7f, 0x61, 0x00}, 3}, QCBOR_ERR_HIT_END },
1859
1860
Laurence Lundblade2f467f92020-10-09 17:50:11 -07001861 // All the chunks in an indefinite length string must be of the type of
1862 // indefinite length string
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001863 // indefinite length byte string with text string chunk
1864 { {(uint8_t[]){0x5f, 0x61, 0x00, 0xff}, 4}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1865 // indefinite length text string with a byte string chunk
1866 { {(uint8_t[]){0x7f, 0x41, 0x00, 0xff}, 4}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1867 // indefinite length byte string with an positive integer chunk
1868 { {(uint8_t[]){0x5f, 0x00, 0xff}, 3}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1869 // indefinite length byte string with an negative integer chunk
1870 { {(uint8_t[]){0x5f, 0x21, 0xff}, 3}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1871 // indefinite length byte string with an array chunk
1872 { {(uint8_t[]){0x5f, 0x80, 0xff}, 3}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1873 // indefinite length byte string with an map chunk
1874 { {(uint8_t[]){0x5f, 0xa0, 0xff}, 3}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
Laurence Lundblade37286c02022-09-03 10:05:02 -07001875#ifndef QCBOR_DISABLE_TAGS
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001876 // indefinite length byte string with tagged integer chunk
1877 { {(uint8_t[]){0x5f, 0xc0, 0x00, 0xff}, 4}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
Laurence Lundblade37286c02022-09-03 10:05:02 -07001878#else
1879 // indefinite length byte string with tagged integer chunk
1880 { {(uint8_t[]){0x5f, 0xc0, 0x00, 0xff}, 4}, QCBOR_ERR_TAGS_DISABLED },
1881#endif /* QCBOR_DISABLE_TAGS */
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001882 // indefinite length byte string with an simple type chunk
1883 { {(uint8_t[]){0x5f, 0xe0, 0xff}, 3}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1884 { {(uint8_t[]){0x5f, 0x5f, 0x41, 0x00, 0xff, 0xff}, 6}, QCBOR_ERR_INDEFINITE_STRING_CHUNK},
1885 // indefinite length text string with indefinite string inside
1886 { {(uint8_t[]){0x7f, 0x7f, 0x61, 0x00, 0xff, 0xff}, 6}, QCBOR_ERR_INDEFINITE_STRING_CHUNK},
1887
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08001888#else /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
1889
1890 { {(uint8_t[]){0x5f, 0x41, 0x00}, 3}, QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED },
1891 // An indefinite length text string not closed off
1892 { {(uint8_t[]){0x7f, 0x61, 0x00}, 3}, QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED },
1893
1894
1895 // All the chunks in an indefinite length string must be of the type of
1896 // indefinite length string
1897 // indefinite length byte string with text string chunk
1898 { {(uint8_t[]){0x5f, 0x61, 0x00, 0xff}, 4}, QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED },
1899 // indefinite length text string with a byte string chunk
1900 { {(uint8_t[]){0x7f, 0x41, 0x00, 0xff}, 4}, QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED },
1901 // indefinite length byte string with an positive integer chunk
1902 { {(uint8_t[]){0x5f, 0x00, 0xff}, 3}, QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED },
1903 // indefinite length byte string with an negative integer chunk
1904 { {(uint8_t[]){0x5f, 0x21, 0xff}, 3}, QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED },
1905 // indefinite length byte string with an array chunk
1906 { {(uint8_t[]){0x5f, 0x80, 0xff}, 3}, QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED },
1907 // indefinite length byte string with an map chunk
1908 { {(uint8_t[]){0x5f, 0xa0, 0xff}, 3}, QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED },
1909 // indefinite length byte string with tagged integer chunk
1910 { {(uint8_t[]){0x5f, 0xc0, 0x00, 0xff}, 4}, QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED },
1911 // indefinite length byte string with an simple type chunk
1912 { {(uint8_t[]){0x5f, 0xe0, 0xff}, 3}, QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED },
1913 { {(uint8_t[]){0x5f, 0x5f, 0x41, 0x00, 0xff, 0xff}, 6}, QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED},
1914 // indefinite length text string with indefinite string inside
1915 { {(uint8_t[]){0x7f, 0x7f, 0x61, 0x00, 0xff, 0xff}, 6}, QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED},
1916#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
1917
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001918
1919 // Definte length maps and arrays must be closed by having the right number of items
1920 // A definte length array that is supposed to have 1 item, but has none
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001921 { {(uint8_t[]){0x81}, 1}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001922 // A definte length array that is supposed to have 2 items, but has only 1
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001923 { {(uint8_t[]){0x82, 0x00}, 2}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001924 // A definte length array that is supposed to have 511 items, but has only 1
1925 { {(uint8_t[]){0x9a, 0x01, 0xff, 0x00}, 4}, QCBOR_ERR_HIT_END },
1926 // A definte length map that is supposed to have 1 item, but has none
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001927 { {(uint8_t[]){0xa1}, 1}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001928 // A definte length map that is supposed to have s item, but has only 1
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001929 { {(uint8_t[]){0xa2, 0x01, 0x02}, 3}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001930
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08001931#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001932 // Indefinte length maps and arrays must be ended by a break
1933 // Indefinite length array with zero items and no break
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001934 { {(uint8_t[]){0x9f}, 1}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001935 // Indefinite length array with two items and no break
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001936 { {(uint8_t[]){0x9f, 0x01, 0x02}, 3}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001937 // Indefinite length map with zero items and no break
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001938 { {(uint8_t[]){0xbf}, 1}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001939 // Indefinite length map with two items and no break
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001940 { {(uint8_t[]){0xbf, 0x01, 0x02, 0x01, 0x02}, 5}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001941
1942
1943 // Nested maps and arrays must be closed off (some extra nested test vectors)
Laurence Lundblade642282a2020-06-23 12:00:33 -07001944 // Unclosed indefinite array containing a closed definite length array
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001945 { {(uint8_t[]){0x9f, 0x80, 0x00}, 3}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundblade642282a2020-06-23 12:00:33 -07001946 // Definite length array containing an unclosed indefinite length array
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001947 { {(uint8_t[]){0x81, 0x9f}, 2}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundblade2f467f92020-10-09 17:50:11 -07001948 // Unclosed indefinite map containing a closed definite length array
1949 { {(uint8_t[]){0xbf, 0x01, 0x80, 0x00, 0xa0}, 5}, QCBOR_ERR_NO_MORE_ITEMS },
1950 // Definite length map containing an unclosed indefinite length array
1951 { {(uint8_t[]){0xa1, 0x02, 0x9f}, 3}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001952 // Deeply nested definite length arrays with deepest one unclosed
Laurence Lundblade93d89472020-10-03 22:30:50 -07001953 { {(uint8_t[]){0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81}, 9}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001954 // Deeply nested indefinite length arrays with deepest one unclosed
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001955 { {(uint8_t[]){0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0xff, 0xff, 0xff, 0xff}, 9}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001956 // Mixed nesting with indefinite unclosed
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001957 { {(uint8_t[]){0x9f, 0x81, 0x9f, 0x81, 0x9f, 0x9f, 0xff, 0xff, 0xff}, 9}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001958 // Mixed nesting with definite unclosed
Laurence Lundbladeee851742020-01-08 08:37:05 -08001959 { {(uint8_t[]){0x9f, 0x82, 0x9f, 0x81, 0x9f, 0x9f, 0xff, 0xff, 0xff, 0xff}, 10}, QCBOR_ERR_BAD_BREAK },
Laurence Lundblade2f467f92020-10-09 17:50:11 -07001960 // Unclosed indefinite length map in definite length maps
1961 { {(uint8_t[]){0xa1, 0x01, 0xa2, 0x02, 0xbf, 0xff, 0x02, 0xbf}, 8},
1962 QCBOR_ERR_NO_MORE_ITEMS},
1963 // Unclosed definite length map in indefinite length maps
1964 { {(uint8_t[]){0xbf, 0x01, 0xbf, 0x02, 0xa1}, 5}, QCBOR_ERR_NO_MORE_ITEMS},
1965 // Unclosed indefinite length array in definite length maps
1966 { {(uint8_t[]){0xa1, 0x01, 0xa2, 0x02, 0x9f, 0xff, 0x02, 0x9f}, 8},
1967 QCBOR_ERR_NO_MORE_ITEMS},
1968 // Unclosed definite length array in indefinite length maps
1969 { {(uint8_t[]){0xbf, 0x01, 0xbf, 0x02, 0x81}, 5}, QCBOR_ERR_NO_MORE_ITEMS},
1970 // Unclosed indefinite length map in definite length arrays
1971 { {(uint8_t[]){0x81, 0x82, 0xbf, 0xff, 0xbf}, 5}, QCBOR_ERR_NO_MORE_ITEMS},
1972 // Unclosed definite length map in indefinite length arrays
1973 { {(uint8_t[]){0x9f, 0x9f, 0xa1}, 3}, QCBOR_ERR_NO_MORE_ITEMS},
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08001974#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001975
1976 // The "argument" for the data item is incomplete
1977 // Positive integer missing 1 byte argument
1978 { {(uint8_t[]){0x18}, 1}, QCBOR_ERR_HIT_END },
1979 // Positive integer missing 2 byte argument
1980 { {(uint8_t[]){0x19}, 1}, QCBOR_ERR_HIT_END },
1981 // Positive integer missing 4 byte argument
1982 { {(uint8_t[]){0x1a}, 1}, QCBOR_ERR_HIT_END },
1983 // Positive integer missing 8 byte argument
1984 { {(uint8_t[]){0x1b}, 1}, QCBOR_ERR_HIT_END },
1985 // Positive integer missing 1 byte of 2 byte argument
1986 { {(uint8_t[]){0x19, 0x01}, 2}, QCBOR_ERR_HIT_END },
1987 // Positive integer missing 2 bytes of 4 byte argument
1988 { {(uint8_t[]){0x1a, 0x01, 0x02}, 3}, QCBOR_ERR_HIT_END },
1989 // Positive integer missing 1 bytes of 7 byte argument
1990 { {(uint8_t[]){0x1b, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07}, 8}, QCBOR_ERR_HIT_END },
1991 // Negative integer missing 1 byte argument
1992 { {(uint8_t[]){0x38}, 1}, QCBOR_ERR_HIT_END },
1993 // Binary string missing 1 byte argument
1994 { {(uint8_t[]){0x58}, 1}, QCBOR_ERR_HIT_END },
1995 // Text string missing 1 byte argument
1996 { {(uint8_t[]){0x78}, 1}, QCBOR_ERR_HIT_END },
1997 // Array missing 1 byte argument
1998 { {(uint8_t[]){0x98}, 1}, QCBOR_ERR_HIT_END },
1999 // Map missing 1 byte argument
2000 { {(uint8_t[]){0xb8}, 1}, QCBOR_ERR_HIT_END },
2001 // Tag missing 1 byte argument
2002 { {(uint8_t[]){0xd8}, 1}, QCBOR_ERR_HIT_END },
2003 // Simple missing 1 byte argument
2004 { {(uint8_t[]){0xf8}, 1}, QCBOR_ERR_HIT_END },
Laurence Lundblade2f467f92020-10-09 17:50:11 -07002005 // half-precision with 1 byte argument
2006 { {(uint8_t[]){0xf9, 0x00}, 2}, QCBOR_ERR_HIT_END },
2007 // single-precision with 2 byte argument
2008 { {(uint8_t[]){0xfa, 0x00, 0x00}, 3}, QCBOR_ERR_HIT_END },
2009 // double-precision with 3 byte argument
2010 { {(uint8_t[]){0xfb, 0x00, 0x00, 0x00}, 4}, QCBOR_ERR_HIT_END },
2011
Laurence Lundblade37286c02022-09-03 10:05:02 -07002012#ifndef QCBOR_DISABLE_TAGS
Laurence Lundblade2f467f92020-10-09 17:50:11 -07002013 // Tag with no content
2014 { {(uint8_t[]){0xc0}, 1}, QCBOR_ERR_HIT_END },
Laurence Lundblade37286c02022-09-03 10:05:02 -07002015#else /* QCBOR_DISABLE_TAGS */
2016 { {(uint8_t[]){0xc0}, 1}, QCBOR_ERR_TAGS_DISABLED },
2017#endif /* QCBOR_DISABLE_TAGS */
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002018
2019 // Breaks must not occur in definite length arrays and maps
2020 // Array of length 1 with sole member replaced by a break
2021 { {(uint8_t[]){0x81, 0xff}, 2}, QCBOR_ERR_BAD_BREAK },
2022 // Array of length 2 with 2nd member replaced by a break
2023 { {(uint8_t[]){0x82, 0x00, 0xff}, 3}, QCBOR_ERR_BAD_BREAK },
2024 // Map of length 1 with sole member label replaced by a break
2025 { {(uint8_t[]){0xa1, 0xff}, 2}, QCBOR_ERR_BAD_BREAK },
2026 // Map of length 1 with sole member label replaced by break
Laurence Lundblade2f467f92020-10-09 17:50:11 -07002027 // Alternate representation that some decoders handle differently
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002028 { {(uint8_t[]){0xa1, 0xff, 0x00}, 3}, QCBOR_ERR_BAD_BREAK },
2029 // Array of length 1 with 2nd member value replaced by a break
2030 { {(uint8_t[]){0xa1, 0x00, 0xff}, 3}, QCBOR_ERR_BAD_BREAK },
2031 // Map of length 2 with 2nd member replaced by a break
2032 { {(uint8_t[]){0xa2, 0x00, 0x00, 0xff}, 4}, QCBOR_ERR_BAD_BREAK },
2033
2034
2035 // Breaks must not occur on their own out of an indefinite length data item
2036 // A bare break is not well formed
2037 { {(uint8_t[]){0xff}, 1}, QCBOR_ERR_BAD_BREAK },
2038 // A bare break after a zero length definite length array
2039 { {(uint8_t[]){0x80, 0xff}, 2}, QCBOR_ERR_BAD_BREAK },
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08002040#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002041 // A bare break after a zero length indefinite length map
2042 { {(uint8_t[]){0x9f, 0xff, 0xff}, 3}, QCBOR_ERR_BAD_BREAK },
Laurence Lundblade2f467f92020-10-09 17:50:11 -07002043 // A break inside a definite length array inside an indefenite length array
2044 { {(uint8_t[]){0x9f, 0x81, 0xff}, 3}, QCBOR_ERR_BAD_BREAK },
2045 // Complicated mixed nesting with break outside indefinite length array
2046 { {(uint8_t[]){0x9f, 0x82, 0x9f, 0x81, 0x9f, 0x9f, 0xff, 0xff, 0xff, 0xff}, 10}, QCBOR_ERR_BAD_BREAK },
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08002047#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002048
2049
2050 // Forbidden two byte encodings of simple types
2051 // Must use 0xe0 instead
2052 { {(uint8_t[]){0xf8, 0x00}, 2}, QCBOR_ERR_BAD_TYPE_7 },
2053 // Should use 0xe1 instead
2054 { {(uint8_t[]){0xf8, 0x01}, 2}, QCBOR_ERR_BAD_TYPE_7 },
2055 // Should use 0xe2 instead
2056 { {(uint8_t[]){0xf8, 0x02}, 2}, QCBOR_ERR_BAD_TYPE_7 },
2057 // Should use 0xe3 instead
2058 { {(uint8_t[]){0xf8, 0x03}, 2}, QCBOR_ERR_BAD_TYPE_7 },
2059 // Should use 0xe4 instead
2060 { {(uint8_t[]){0xf8, 0x04}, 2}, QCBOR_ERR_BAD_TYPE_7 },
2061 // Should use 0xe5 instead
2062 { {(uint8_t[]){0xf8, 0x05}, 2}, QCBOR_ERR_BAD_TYPE_7 },
2063 // Should use 0xe6 instead
2064 { {(uint8_t[]){0xf8, 0x06}, 2}, QCBOR_ERR_BAD_TYPE_7 },
2065 // Should use 0xe7 instead
2066 { {(uint8_t[]){0xf8, 0x07}, 2}, QCBOR_ERR_BAD_TYPE_7 },
2067 // Should use 0xe8 instead
2068 { {(uint8_t[]){0xf8, 0x08}, 2}, QCBOR_ERR_BAD_TYPE_7 },
2069 // Should use 0xe9 instead
2070 { {(uint8_t[]){0xf8, 0x09}, 2}, QCBOR_ERR_BAD_TYPE_7 },
2071 // Should use 0xea instead
2072 { {(uint8_t[]){0xf8, 0x0a}, 2}, QCBOR_ERR_BAD_TYPE_7 },
2073 // Should use 0xeb instead
2074 { {(uint8_t[]){0xf8, 0x0b}, 2}, QCBOR_ERR_BAD_TYPE_7 },
2075 // Should use 0xec instead
2076 { {(uint8_t[]){0xf8, 0x0c}, 2}, QCBOR_ERR_BAD_TYPE_7 },
2077 // Should use 0xed instead
2078 { {(uint8_t[]){0xf8, 0x0d}, 2}, QCBOR_ERR_BAD_TYPE_7 },
2079 // Should use 0xee instead
2080 { {(uint8_t[]){0xf8, 0x0e}, 2}, QCBOR_ERR_BAD_TYPE_7 },
2081 // Should use 0xef instead
2082 { {(uint8_t[]){0xf8, 0x0f}, 2}, QCBOR_ERR_BAD_TYPE_7 },
2083 // Should use 0xf0 instead
2084 { {(uint8_t[]){0xf8, 0x10}, 2}, QCBOR_ERR_BAD_TYPE_7 },
2085 // Should use 0xf1 instead
2086 { {(uint8_t[]){0xf8, 0x11}, 2}, QCBOR_ERR_BAD_TYPE_7 },
2087 // Should use 0xf2 instead
2088 { {(uint8_t[]){0xf8, 0x12}, 2}, QCBOR_ERR_BAD_TYPE_7 },
2089 // Must use 0xf3 instead
2090 { {(uint8_t[]){0xf8, 0x13}, 2}, QCBOR_ERR_BAD_TYPE_7 },
2091 // Must use 0xf4 instead
2092 { {(uint8_t[]){0xf8, 0x14}, 2}, QCBOR_ERR_BAD_TYPE_7 },
2093 // Must use 0xf5 instead
2094 { {(uint8_t[]){0xf8, 0x15}, 2}, QCBOR_ERR_BAD_TYPE_7 },
2095 // Must use 0xf6 instead
2096 { {(uint8_t[]){0xf8, 0x16}, 2}, QCBOR_ERR_BAD_TYPE_7 },
2097 // Must use 0xf7 instead
2098 { {(uint8_t[]){0xf8, 0x17}, 2}, QCBOR_ERR_BAD_TYPE_7 },
2099 // Must use 0xf8 instead
2100 { {(uint8_t[]){0xf8, 0x18}, 2}, QCBOR_ERR_BAD_TYPE_7 },
Laurence Lundblade2f467f92020-10-09 17:50:11 -07002101 // Reserved
2102 { {(uint8_t[]){0xf8, 0x1f}, 2}, QCBOR_ERR_BAD_TYPE_7 },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002103
2104 // Integers with additional info indefinite length
2105 // Positive integer with additional info indefinite length
2106 { {(uint8_t[]){0x1f}, 1}, QCBOR_ERR_BAD_INT },
2107 // Negative integer with additional info indefinite length
2108 { {(uint8_t[]){0x3f}, 1}, QCBOR_ERR_BAD_INT },
Laurence Lundblade37286c02022-09-03 10:05:02 -07002109
2110#ifndef QCBOR_DISABLE_TAGS
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002111 // CBOR tag with "argument" an indefinite length
2112 { {(uint8_t[]){0xdf, 0x00}, 2}, QCBOR_ERR_BAD_INT },
2113 // CBOR tag with "argument" an indefinite length alternate vector
2114 { {(uint8_t[]){0xdf}, 1}, QCBOR_ERR_BAD_INT },
Laurence Lundblade37286c02022-09-03 10:05:02 -07002115#else /* QCBOR_DISABLE_TAGS */
2116 { {(uint8_t[]){0xdf, 0x00}, 2}, QCBOR_ERR_TAGS_DISABLED },
2117 { {(uint8_t[]){0xdf}, 1}, QCBOR_ERR_TAGS_DISABLED },
2118#endif /* QCBOR_DISABLE_TAGS */
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002119
2120 // Missing bytes from a deterministic length string
2121 // A byte string is of length 1 without the 1 byte
2122 { {(uint8_t[]){0x41}, 1}, QCBOR_ERR_HIT_END },
2123 // A text string is of length 1 without the 1 byte
2124 { {(uint8_t[]){0x61}, 1}, QCBOR_ERR_HIT_END },
Laurence Lundblade4bf6e712020-12-10 11:53:21 -08002125
2126#if SIZE_MAX > 2147483647
Laurence Lundblade42272e42020-01-31 07:50:53 -08002127 // Byte string should have 2^32-15 bytes, but has one
2128 { {(uint8_t[]){0x5a, 0xff, 0xff, 0xff, 0xf0, 0x00}, 6}, QCBOR_ERR_HIT_END },
2129 // Byte string should have 2^32-15 bytes, but has one
2130 { {(uint8_t[]){0x7a, 0xff, 0xff, 0xff, 0xf0, 0x00}, 6}, QCBOR_ERR_HIT_END },
Laurence Lundblade2f467f92020-10-09 17:50:11 -07002131 // Byte string should have 2^64 bytes, but has 3
2132 { {(uint8_t[]){0x5b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
2133 0x01, 0x02, 0x03}, 6}, QCBOR_ERR_HIT_END },
2134 // Text string should have 2^64 bytes, but has 3
2135 { {(uint8_t[]){0x7b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
2136 0x01, 0x02, 0x03}, 6}, QCBOR_ERR_HIT_END },
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03002137#else
Laurence Lundblade4bf6e712020-12-10 11:53:21 -08002138 // Byte string should have 2^32-15 bytes, but has one
2139 { {(uint8_t[]){0x5a, 0x00, 0x00, 0xff, 0xf0, 0x00}, 6}, QCBOR_ERR_HIT_END },
2140 // Byte string should have 2^32-15 bytes, but has one
2141 { {(uint8_t[]){0x7a, 0x00, 0x00, 0xff, 0xf0, 0x00}, 6}, QCBOR_ERR_HIT_END },
2142 // Byte string should have 2^16 bytes, but has 3
2143 { {(uint8_t[]){0x5b, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0x01, 0x02, 0x03}, 6}, QCBOR_ERR_HIT_END },
2144 // Text string should have 2^64 bytes, but has 3
2145 { {(uint8_t[]){0x7b, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0x01, 0x02, 0x03}, 6}, QCBOR_ERR_HIT_END },
2146#endif
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002147
2148 // Use of unassigned additional information values
2149 // Major type positive integer with reserved value 28
2150 { {(uint8_t[]){0x1c}, 1}, QCBOR_ERR_UNSUPPORTED },
2151 // Major type positive integer with reserved value 29
2152 { {(uint8_t[]){0x1d}, 1}, QCBOR_ERR_UNSUPPORTED },
2153 // Major type positive integer with reserved value 30
2154 { {(uint8_t[]){0x1e}, 1}, QCBOR_ERR_UNSUPPORTED },
2155 // Major type negative integer with reserved value 28
2156 { {(uint8_t[]){0x3c}, 1}, QCBOR_ERR_UNSUPPORTED },
2157 // Major type negative integer with reserved value 29
2158 { {(uint8_t[]){0x3d}, 1}, QCBOR_ERR_UNSUPPORTED },
2159 // Major type negative integer with reserved value 30
2160 { {(uint8_t[]){0x3e}, 1}, QCBOR_ERR_UNSUPPORTED },
2161 // Major type byte string with reserved value 28 length
2162 { {(uint8_t[]){0x5c}, 1}, QCBOR_ERR_UNSUPPORTED },
2163 // Major type byte string with reserved value 29 length
2164 { {(uint8_t[]){0x5d}, 1}, QCBOR_ERR_UNSUPPORTED },
2165 // Major type byte string with reserved value 30 length
2166 { {(uint8_t[]){0x5e}, 1}, QCBOR_ERR_UNSUPPORTED },
2167 // Major type text string with reserved value 28 length
2168 { {(uint8_t[]){0x7c}, 1}, QCBOR_ERR_UNSUPPORTED },
2169 // Major type text string with reserved value 29 length
2170 { {(uint8_t[]){0x7d}, 1}, QCBOR_ERR_UNSUPPORTED },
2171 // Major type text string with reserved value 30 length
2172 { {(uint8_t[]){0x7e}, 1}, QCBOR_ERR_UNSUPPORTED },
2173 // Major type array with reserved value 28 length
2174 { {(uint8_t[]){0x9c}, 1}, QCBOR_ERR_UNSUPPORTED },
2175 // Major type array with reserved value 29 length
2176 { {(uint8_t[]){0x9d}, 1}, QCBOR_ERR_UNSUPPORTED },
2177 // Major type array with reserved value 30 length
2178 { {(uint8_t[]){0x9e}, 1}, QCBOR_ERR_UNSUPPORTED },
2179 // Major type map with reserved value 28 length
2180 { {(uint8_t[]){0xbc}, 1}, QCBOR_ERR_UNSUPPORTED },
2181 // Major type map with reserved value 29 length
2182 { {(uint8_t[]){0xbd}, 1}, QCBOR_ERR_UNSUPPORTED },
2183 // Major type map with reserved value 30 length
2184 { {(uint8_t[]){0xbe}, 1}, QCBOR_ERR_UNSUPPORTED },
2185 // Major type tag with reserved value 28 length
2186 { {(uint8_t[]){0xdc}, 1}, QCBOR_ERR_UNSUPPORTED },
2187 // Major type tag with reserved value 29 length
2188 { {(uint8_t[]){0xdd}, 1}, QCBOR_ERR_UNSUPPORTED },
2189 // Major type tag with reserved value 30 length
2190 { {(uint8_t[]){0xde}, 1}, QCBOR_ERR_UNSUPPORTED },
2191 // Major type simple with reserved value 28 length
2192 { {(uint8_t[]){0xfc}, 1}, QCBOR_ERR_UNSUPPORTED },
2193 // Major type simple with reserved value 29 length
2194 { {(uint8_t[]){0xfd}, 1}, QCBOR_ERR_UNSUPPORTED },
2195 // Major type simple with reserved value 30 length
2196 { {(uint8_t[]){0xfe}, 1}, QCBOR_ERR_UNSUPPORTED },
2197
2198
2199 // Maps must have an even number of data items (key & value)
2200 // Map with 1 item when it should have 2
2201 { {(uint8_t[]){0xa1, 0x00}, 2}, QCBOR_ERR_HIT_END },
2202 // Map with 3 item when it should have 4
2203 { {(uint8_t[]){0xa2, 0x00, 0x00, 0x00}, 2}, QCBOR_ERR_HIT_END },
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08002204#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002205 // Map with 1 item when it should have 2
2206 { {(uint8_t[]){0xbf, 0x00, 0xff}, 3}, QCBOR_ERR_BAD_BREAK },
2207 // Map with 3 item when it should have 4
2208 { {(uint8_t[]){0xbf, 0x00, 0x00, 0x00, 0xff}, 5}, QCBOR_ERR_BAD_BREAK },
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08002209#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002210
Laurence Lundblade37286c02022-09-03 10:05:02 -07002211#ifndef QCBOR_DISABLE_TAGS
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002212 // In addition to not-well-formed, some invalid CBOR
Laurence Lundbladeee851742020-01-08 08:37:05 -08002213 // Text-based date, with an integer
2214 { {(uint8_t[]){0xc0, 0x00}, 2}, QCBOR_ERR_BAD_OPT_TAG },
2215 // Epoch date, with an byte string
2216 { {(uint8_t[]){0xc1, 0x41, 0x33}, 3}, QCBOR_ERR_BAD_OPT_TAG },
2217 // tagged as both epoch and string dates
2218 { {(uint8_t[]){0xc1, 0xc0, 0x00}, 3}, QCBOR_ERR_BAD_OPT_TAG },
2219 // big num tagged an int, not a byte string
2220 { {(uint8_t[]){0xc2, 0x00}, 2}, QCBOR_ERR_BAD_OPT_TAG },
Laurence Lundblade37286c02022-09-03 10:05:02 -07002221#else /* QCBOR_DISABLE_TAGS */
2222 // Text-based date, with an integer
2223 { {(uint8_t[]){0xc0, 0x00}, 2}, QCBOR_ERR_TAGS_DISABLED },
2224 // Epoch date, with an byte string
2225 { {(uint8_t[]){0xc1, 0x41, 0x33}, 3}, QCBOR_ERR_TAGS_DISABLED },
2226 // tagged as both epoch and string dates
2227 { {(uint8_t[]){0xc1, 0xc0, 0x00}, 3}, QCBOR_ERR_TAGS_DISABLED },
2228 // big num tagged an int, not a byte string
2229 { {(uint8_t[]){0xc2, 0x00}, 2}, QCBOR_ERR_TAGS_DISABLED },
2230#endif /* QCBOR_DISABLE_TAGS */
2231
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002232};
2233
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03002234int32_t DecodeFailureTests(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002235{
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002236 int32_t nResult;
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002237
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08002238 nResult = ProcessFailures(Failures,C_ARRAY_COUNT(Failures,struct FailInput));
Laurence Lundblade59289e52019-12-30 13:44:37 -08002239 if(nResult) {
2240 return nResult;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002241 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002242
Laurence Lundblade3a6042e2019-06-28 19:58:04 -07002243 // Corrupt the UsefulInputBuf and see that
2244 // it reflected correctly for CBOR decoding
Laurence Lundbladee6f15112020-07-23 18:44:16 -07002245 QCBORDecodeContext DCtx;
2246 QCBORItem Item;
2247 QCBORError uQCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002248
Laurence Lundbladee6f15112020-07-23 18:44:16 -07002249 QCBORDecode_Init(&DCtx,
2250 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSimpleValues),
2251 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002252
Laurence Lundbladee6f15112020-07-23 18:44:16 -07002253 if((uQCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
2254 return (int32_t)uQCBORError;
2255 }
2256 if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.val.uCount != 10) {
2257 // This wasn't supposed to happen
2258 return -1;
2259 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002260
Laurence Lundbladee6f15112020-07-23 18:44:16 -07002261 DCtx.InBuf.magic = 0; // Reach in and corrupt the UsefulInputBuf
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002262
Laurence Lundbladee6f15112020-07-23 18:44:16 -07002263 uQCBORError = QCBORDecode_GetNext(&DCtx, &Item);
2264 if(uQCBORError != QCBOR_ERR_NO_MORE_ITEMS) {
2265 // Did not get back the error expected
2266 return -2;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002267 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002268
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002269
Laurence Lundblade98427e92020-09-28 21:33:23 -07002270 /*
2271 The max size of a string for QCBOR is SIZE_MAX - 4 so this
2272 tests here can be performed to see that the max length
2273 error check works correctly. See DecodeBytes(). If the max
2274 size was SIZE_MAX, it wouldn't be possible to test this.
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002275
Laurence Lundblade98427e92020-09-28 21:33:23 -07002276 This test will automatocally adapt the all CPU sizes
2277 through the use of SIZE_MAX.
2278 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002279
Laurence Lundblade8510f8c2020-12-01 11:31:16 -08002280 UsefulBuf_MAKE_STACK_UB( HeadBuf, QCBOR_HEAD_BUFFER_SIZE);
Laurence Lundblade98427e92020-09-28 21:33:23 -07002281 UsefulBufC EncodedHead;
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002282
Laurence Lundblade98427e92020-09-28 21:33:23 -07002283 // This makes a CBOR head with a text string that is very long
2284 // but doesn't fill in the bytes of the text string as that is
2285 // not needed to test this part of QCBOR.
2286 EncodedHead = QCBOREncode_EncodeHead(HeadBuf, CBOR_MAJOR_TYPE_TEXT_STRING, 0, SIZE_MAX);
2287
2288 QCBORDecode_Init(&DCtx, EncodedHead, QCBOR_DECODE_MODE_NORMAL);
2289
2290 if(QCBOR_ERR_STRING_TOO_LONG != QCBORDecode_GetNext(&DCtx, &Item)) {
2291 return -4;
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002292 }
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002293
Laurence Lundblade3a6042e2019-06-28 19:58:04 -07002294 return 0;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002295}
2296
2297
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002298/* Try all 256 values of the byte at nLen including recursing for
2299 each of the values to try values at nLen+1 ... up to nLenMax
2300 */
Laurence Lundblade06350ea2020-01-27 19:32:40 -08002301static void ComprehensiveInputRecurser(uint8_t *pBuf, size_t nLen, size_t nLenMax)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002302{
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002303 if(nLen >= nLenMax) {
2304 return;
2305 }
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08002306
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002307 for(int inputByte = 0; inputByte < 256; inputByte++) {
2308 // Set up the input
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002309 pBuf[nLen] = (uint8_t)inputByte;
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08002310 const UsefulBufC Input = {pBuf, nLen+1};
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08002311
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002312 // Get ready to parse
2313 QCBORDecodeContext DCtx;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002314 QCBORDecode_Init(&DCtx, Input, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002315
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002316 // Parse by getting the next item until an error occurs
2317 // Just about every possible decoder error can occur here
2318 // The goal of this test is not to check for the correct
2319 // error since that is not really possible. It is to
2320 // see that there is no crash on hostile input.
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002321 while(1) {
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002322 QCBORItem Item;
2323 QCBORError nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002324 if(nCBORError != QCBOR_SUCCESS) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002325 break;
2326 }
2327 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002328
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002329 ComprehensiveInputRecurser(pBuf, nLen+1, nLenMax);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002330 }
2331}
2332
2333
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03002334int32_t ComprehensiveInputTest(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002335{
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002336 // Size 2 tests 64K inputs and runs quickly
2337 uint8_t pBuf[2];
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002338
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002339 ComprehensiveInputRecurser(pBuf, 0, sizeof(pBuf));
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002340
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002341 return 0;
2342}
2343
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002344
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03002345int32_t BigComprehensiveInputTest(void)
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002346{
2347 // size 3 tests 16 million inputs and runs OK
2348 // in seconds on fast machines. Size 4 takes
2349 // 10+ minutes and 5 half a day on fast
2350 // machines. This test is kept separate from
2351 // the others so as to no slow down the use
2352 // of them as a very frequent regression.
2353 uint8_t pBuf[3]; //
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08002354
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002355 ComprehensiveInputRecurser(pBuf, 0, sizeof(pBuf));
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08002356
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002357 return 0;
2358}
2359
2360
Laurence Lundbladecc7da412020-12-27 00:09:07 -08002361static const uint8_t spDateTestInput[] = {
Laurence Lundblade16a207a2021-09-18 17:22:46 -07002362 /* 1. The valid date string "1985-04-12" */
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002363 0xc0, // tag for string date
2364 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002365
Laurence Lundblade16a207a2021-09-18 17:22:46 -07002366 /* 2. An invalid date string due to wrong tag content type */
Laurence Lundbladec7114722020-08-13 05:11:40 -07002367 0xc0, // tag for string date
2368 0x00, // Wrong type for a string date
2369
Laurence Lundblade16a207a2021-09-18 17:22:46 -07002370 /* 3. A valid epoch date, 1400000000; Tue, 13 May 2014 16:53:20 GMT */
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002371 0xc1, // tag for epoch date
2372 0x1a, 0x53, 0x72, 0x4E, 0x00, // Epoch date 1400000000; Tue, 13 May 2014 16:53:20 GMT
2373
Laurence Lundblade16a207a2021-09-18 17:22:46 -07002374 /* 4. An invalid epoch date due to wrong tag content type */
Laurence Lundbladec7114722020-08-13 05:11:40 -07002375 0xc1,
2376 0x62, 'h', 'i', // wrong type tagged
2377
Laurence Lundblade16a207a2021-09-18 17:22:46 -07002378 /* 5. Valid epoch date tag as content for a two other nested tags */
Laurence Lundblade99615302020-11-29 11:19:47 -08002379 // CBOR_TAG_ENC_AS_B64
Laurence Lundblade16a207a2021-09-18 17:22:46 -07002380 0xcf, 0xd8, 0x16, 0xc1, // Epoch date with extra tags
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002381 0x1a, 0x53, 0x72, 0x4E, 0x01,
2382
Laurence Lundblade16a207a2021-09-18 17:22:46 -07002383 /* 6. Epoch date with value to large to fit into int64 */
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002384 0xc1, // tag for epoch date
2385 0x1b, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, // Too large integer
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002386
Laurence Lundblade16a207a2021-09-18 17:22:46 -07002387 /* 7. Epoch date with single-precision value of 1.1. */
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002388 0xc1, // tag for epoch date
Laurence Lundblade3ed0bca2020-07-14 22:50:10 -07002389 0xfa, 0x3f, 0x8c, 0xcc, 0xcd, // single with value 1.1
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002390
Laurence Lundblade16a207a2021-09-18 17:22:46 -07002391 /* 8. Epoch date with too-large single precision float */
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002392 0xc1, // tag for epoch date
Laurence Lundbladea1ad8782019-11-08 00:12:11 -08002393 0xfa, 0x7f, 0x7f, 0xff, 0xff, // 3.4028234663852886e+38 too large
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002394
Laurence Lundblade16a207a2021-09-18 17:22:46 -07002395 /* 9. Epoch date with slightly too-large double precision value */
Laurence Lundbladea1ad8782019-11-08 00:12:11 -08002396 0xc1, // tag for epoch date
2397 0xfb, 0x43, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 9223372036854775808.000000 just barely too large
2398 //0xfa, 0x7f, 0x7f, 0xff, 0xff // 3.4028234663852886e+38 too large
2399
Laurence Lundblade16a207a2021-09-18 17:22:46 -07002400 /* 10. Epoch date with largest supported double precision value */
Laurence Lundbladea1ad8782019-11-08 00:12:11 -08002401 0xc1, // tag for epoch date
Laurence Lundbladec7114722020-08-13 05:11:40 -07002402 0xfb, 0x43, 0xdf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, // 9223372036854773760 largest supported
2403
Laurence Lundblade16a207a2021-09-18 17:22:46 -07002404 /* 11. Epoch date with single-precision NaN */
Laurence Lundbladec7114722020-08-13 05:11:40 -07002405 0xc1, // tag for epoch date
2406 0xfa, 0x7f, 0xc0, 0x00, 0x00, // Single-precision NaN
2407
Laurence Lundblade16a207a2021-09-18 17:22:46 -07002408 /* 12. Epoch date with double precision plus infinity */
Laurence Lundbladec7114722020-08-13 05:11:40 -07002409 0xc1,
2410 0xfb, 0x7f, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // +infinity
2411
Laurence Lundblade16a207a2021-09-18 17:22:46 -07002412 /* 13. Epoch date with half-precision negative infinity */
Laurence Lundbladec7114722020-08-13 05:11:40 -07002413 0xc1, // tag for epoch date
2414 0xf9, 0xfc, 0x00, // -Infinity
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002415};
2416
2417
Laurence Lundbladec7114722020-08-13 05:11:40 -07002418
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002419// have to check float expected only to within an epsilon
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07002420#ifndef QCBOR_DISABLE_FLOAT_HW_USE
Laurence Lundblade02fcf312020-07-17 02:49:46 -07002421static int CHECK_EXPECTED_DOUBLE(double val, double expected) {
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002422
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002423 double diff = val - expected;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002424
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002425 diff = fabs(diff);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002426
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002427 return diff > 0.0000001;
2428}
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07002429#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002430
2431
Laurence Lundblade37286c02022-09-03 10:05:02 -07002432/* Test date decoding using GetNext() */
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03002433int32_t DateParseTest(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002434{
2435 QCBORDecodeContext DCtx;
Laurence Lundbladec7114722020-08-13 05:11:40 -07002436 QCBORItem Item;
2437 QCBORError uError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002438
Laurence Lundbladeee851742020-01-08 08:37:05 -08002439 QCBORDecode_Init(&DCtx,
2440 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spDateTestInput),
2441 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002442
Laurence Lundblade16a207a2021-09-18 17:22:46 -07002443 /* 1. The valid date string "1985-04-12" */
Laurence Lundbladec7114722020-08-13 05:11:40 -07002444 if((uError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002445 return -1;
Laurence Lundbladec7114722020-08-13 05:11:40 -07002446 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002447 if(Item.uDataType != QCBOR_TYPE_DATE_STRING ||
Laurence Lundbladeba587682024-02-07 16:46:43 -08002448 UsefulBufCompareToSZ(Item.val.string, "1985-04-12")){
Laurence Lundblade67bd5512018-11-02 21:44:06 +07002449 return -2;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002450 }
2451
Laurence Lundblade16a207a2021-09-18 17:22:46 -07002452 /* 2. An invalid date string due to wrong tag content type */
Laurence Lundbladec7114722020-08-13 05:11:40 -07002453 uError = QCBORDecode_GetNext(&DCtx, &Item);
2454 if(uError != QCBOR_ERR_BAD_OPT_TAG) {
Laurence Lundblade67bd5512018-11-02 21:44:06 +07002455 return -3;
Laurence Lundbladec7114722020-08-13 05:11:40 -07002456 }
2457
Laurence Lundblade16a207a2021-09-18 17:22:46 -07002458 /* 3. A valid epoch date, 1400000000; Tue, 13 May 2014 16:53:20 GMT */
2459 uError = QCBORDecode_GetNext(&DCtx, &Item);
2460 if(uError != QCBOR_SUCCESS) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002461 return -4;
2462 }
Laurence Lundblade16a207a2021-09-18 17:22:46 -07002463 if(uError == QCBOR_SUCCESS) {
2464 if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH ||
2465 Item.val.epochDate.nSeconds != 1400000000
Máté Tóth-Pálef5f07a2021-09-17 19:31:37 +02002466#ifndef USEFULBUF_DISABLE_ALL_FLOAT
Laurence Lundblade16a207a2021-09-18 17:22:46 -07002467 || Item.val.epochDate.fSecondsFraction != 0
Máté Tóth-Pálef5f07a2021-09-17 19:31:37 +02002468#endif /* USEFULBUF_DISABLE_ALL_FLOAT */
Laurence Lundblade16a207a2021-09-18 17:22:46 -07002469 ) {
2470 return -5;
2471 }
Laurence Lundbladec7114722020-08-13 05:11:40 -07002472 }
2473
Laurence Lundblade16a207a2021-09-18 17:22:46 -07002474 /* 4. An invalid epoch date due to wrong tag content type */
Laurence Lundbladec7114722020-08-13 05:11:40 -07002475 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_OPT_TAG) {
2476 return -6;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002477 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002478
Laurence Lundblade16a207a2021-09-18 17:22:46 -07002479 /* 5. Valid epoch date tag as content for a two other nested tags */
Laurence Lundblade99615302020-11-29 11:19:47 -08002480 // Epoch date wrapped in an CBOR_TAG_ENC_AS_B64 and an unknown tag.
2481 // The date is decoded and the two tags are returned. This is to
2482 // make sure the wrapping of epoch date in another tag works OK.
Laurence Lundbladec7114722020-08-13 05:11:40 -07002483 if((uError = QCBORDecode_GetNext(&DCtx, &Item))) {
2484 return -7;
2485 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002486 if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH ||
2487 Item.val.epochDate.nSeconds != 1400000001 ||
Máté Tóth-Pálef5f07a2021-09-17 19:31:37 +02002488#ifndef USEFULBUF_DISABLE_ALL_FLOAT
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002489 Item.val.epochDate.fSecondsFraction != 0 ||
Máté Tóth-Pálef5f07a2021-09-17 19:31:37 +02002490#endif /* USEFULBUF_DISABLE_ALL_FLOAT */
Laurence Lundblade99615302020-11-29 11:19:47 -08002491 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_ENC_AS_B64)) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002492 return -8;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002493 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002494
Laurence Lundblade16a207a2021-09-18 17:22:46 -07002495 /* 6. Epoch date with value to large to fit into int64 */
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002496 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_DATE_OVERFLOW) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002497 return -9;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002498 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002499
Laurence Lundblade16a207a2021-09-18 17:22:46 -07002500 /* 7. Epoch date with single-precision value of 1.1. */
2501 uError = QCBORDecode_GetNext(&DCtx, &Item);
2502 if(uError != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002503 return -10;
2504 }
Laurence Lundblade16a207a2021-09-18 17:22:46 -07002505 if(uError == QCBOR_SUCCESS) {
2506 if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH ||
2507 Item.val.epochDate.nSeconds != 1
2508#ifndef QCBOR_DISABLE_FLOAT_HW_USE
2509 || CHECK_EXPECTED_DOUBLE(Item.val.epochDate.fSecondsFraction, 0.1)
Máté Tóth-Pálef5f07a2021-09-17 19:31:37 +02002510#endif /* USEFULBUF_DISABLE_ALL_FLOAT */
Laurence Lundblade16a207a2021-09-18 17:22:46 -07002511 ) {
2512 return -11;
2513 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002514 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002515
Laurence Lundblade16a207a2021-09-18 17:22:46 -07002516 /* 8. Epoch date with too-large single-precision float */
2517 uError = QCBORDecode_GetNext(&DCtx, &Item);
2518 if(uError != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_DATE_OVERFLOW)) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002519 return -12;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002520 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002521
Laurence Lundblade16a207a2021-09-18 17:22:46 -07002522 /* 9. Epoch date with slightly too-large double-precision value */
2523 uError = QCBORDecode_GetNext(&DCtx, &Item);
2524 if(uError != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_DATE_OVERFLOW)) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002525 return -13;
Laurence Lundbladea1ad8782019-11-08 00:12:11 -08002526 }
2527
Laurence Lundblade16a207a2021-09-18 17:22:46 -07002528 /* 10. Epoch date with largest supported double-precision value */
2529 uError = QCBORDecode_GetNext(&DCtx, &Item);
2530 if(uError != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)) {
2531 return -14;
2532 }
2533 if(uError == QCBOR_SUCCESS) {
2534 if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH ||
2535 Item.val.epochDate.nSeconds != 9223372036854773760
2536#ifndef QCBOR_DISABLE_FLOAT_HW_USE
2537 || Item.val.epochDate.fSecondsFraction != 0.0
2538#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
2539 ) {
2540 return -14;
2541 }
2542 }
Laurence Lundbladec7114722020-08-13 05:11:40 -07002543
Laurence Lundblade16a207a2021-09-18 17:22:46 -07002544 /* 11. Epoch date with single-precision NaN */
2545 if(QCBORDecode_GetNext(&DCtx, &Item) != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_DATE_OVERFLOW)) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002546 return -15;
2547 }
2548
Laurence Lundblade16a207a2021-09-18 17:22:46 -07002549 /* 12. Epoch date with double-precision plus infinity */
2550 if(QCBORDecode_GetNext(&DCtx, &Item) != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_DATE_OVERFLOW)) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002551 return -16;
2552 }
2553
Laurence Lundblade16a207a2021-09-18 17:22:46 -07002554 /* 13. Epoch date with half-precision negative infinity */
2555 uError = QCBORDecode_GetNext(&DCtx, &Item);
2556 if(uError != FLOAT_ERR_CODE_NO_HALF_PREC_NO_FLOAT_HW(QCBOR_ERR_DATE_OVERFLOW)) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002557 return -17;
2558 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002559
2560 return 0;
2561}
2562
Laurence Lundblade37286c02022-09-03 10:05:02 -07002563
Laurence Lundblade4b270642020-08-14 12:53:07 -07002564/*
2565 Test cases covered here. Some items cover more than one of these.
2566 positive integer (zero counts as a positive integer)
2567 negative integer
2568 half-precision float
2569 single-precision float
2570 double-precision float
Laurence Lundbladec7114722020-08-13 05:11:40 -07002571
Laurence Lundblade4b270642020-08-14 12:53:07 -07002572 float Overflow error
2573 Wrong type error for epoch
2574 Wrong type error for date string
2575 float disabled error
2576 half-precision disabled error
2577 -Infinity
2578 Slightly too large integer
2579 Slightly too far from zero
Laurence Lundbladec7114722020-08-13 05:11:40 -07002580
Laurence Lundblade4b270642020-08-14 12:53:07 -07002581 Get epoch by int
2582 Get string by int
2583 Get epoch by string
2584 Get string by string
2585 Fail to get epoch by wrong int label
2586 Fail to get string by wrong string label
2587 Fail to get epoch by string because it is invalid
2588 Fail to get epoch by int because it is invalid
2589
2590 Untagged values
2591 */
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08002592static const uint8_t spSpiffyDateTestInput[] = {
Laurence Lundblade37286c02022-09-03 10:05:02 -07002593 0x87, // array of 7 items
2594
2595 0xa6, // Open a map for tests involving untagged items with labels.
2596
2597 // Untagged integer 0
2598 0x08,
2599 0x00,
2600
2601 // Utagged date string with string label y
2602 0x61, 0x79,
2603 0x6a, '2','0','8','5','-','0','4','-','1','2', // Untagged date string
2604
2605 // Untagged single-precision float with value 3.14 with string label x
2606 0x61, 0x78,
2607 0xFA, 0x40, 0x48, 0xF5, 0xC3,
2608
2609 // Untagged half-precision float with value -2
2610 0x09,
2611 0xF9, 0xC0, 0x00,
2612
2613 /* Untagged date-only date string */
2614 0x18, 0x63,
2615 0x6A, 0x31, 0x39, 0x38, 0x35, 0x2D, 0x30, 0x34, 0x2D, 0x31, 0x32, /* "1985-04-12" */
2616
2617 /* Untagged days-count epoch date */
2618 0x11,
2619 0x19, 0x0F, 0x9A, /* 3994 */
2620
2621 // End of map, back to array
2622
2623 0xa7, // Open map of tagged items with labels
2624
2625 0x00,
2626 0xc0, // tag for string date
2627 0x6a, '1','9','8','5','-','0','4','-','1','2', // Tagged date string
2628
2629
2630 0x01,
2631 0xda, 0x03, 0x03, 0x03, 0x03, // An additional tag
2632 0xc1, // tag for epoch date
2633 0x1a, 0x53, 0x72, 0x4E, 0x00, // Epoch date 1400000000; Tue, 13 May 2014 16:53:20 GMT
2634
2635 0x05,
2636 0xc1,
2637 0xfb, 0xc3, 0xdf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, // -9223372036854773760 largest negative
2638
2639
2640 0x07,
2641 0xc1, // tag for epoch date
2642 0xfb, 0x43, 0xdf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, // 9223372036854773760 largest supported
2643
2644 /* Tagged days-count epoch date */
2645 0x63, 0x53, 0x44, 0x45,
2646 0xD8, 0x64, /* tag(100) */
2647 0x39, 0x29, 0xB3, /* -10676 */
2648
2649 // Untagged -1000 with label z
2650 0x61, 0x7a,
2651 0xda, 0x01, 0x01, 0x01, 0x01, // An additional tag
2652 0x39, 0x03, 0xe7,
2653
2654 /* Tagged date-only date string */
2655 0x63, 0x53, 0x44, 0x53,
2656 0xD9, 0x03, 0xEC,
2657 0x6A, 0x31, 0x39, 0x38, 0x35, 0x2D, 0x30, 0x34, 0x2D, 0x31, 0x32, /* "1985-04-12" */
2658
2659 // End of map of tagged items
Laurence Lundblade4b270642020-08-14 12:53:07 -07002660
2661 0xc1,
2662 0xfb, 0xc3, 0xdf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, // -9.2233720368547748E+18, too negative
2663
Laurence Lundbladec7114722020-08-13 05:11:40 -07002664 0xc1, // tag for epoch date
Laurence Lundblade4b270642020-08-14 12:53:07 -07002665 0x1b, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, // Too-large integer
2666
2667 0xc1, // tag for epoch date
2668 0xf9, 0xfc, 0x00, // Half-precision -Infinity
2669
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07002670 // These two at the end because they are unrecoverable errors
2671 0xc1, // tag for epoch date
2672 0x80, // Erroneous empty array as content for date
2673
2674 0xc0, // tag for string date
2675 0xa0 // Erroneous empty map as content for date
Laurence Lundbladec7114722020-08-13 05:11:40 -07002676};
2677
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03002678int32_t SpiffyDateDecodeTest(void)
Laurence Lundbladec7114722020-08-13 05:11:40 -07002679{
2680 QCBORDecodeContext DC;
Laurence Lundblade4b270642020-08-14 12:53:07 -07002681 QCBORError uError;
Laurence Lundblade37286c02022-09-03 10:05:02 -07002682 int64_t nEpochDate3, nEpochDate5,
2683 nEpochDate4, nEpochDate6,
2684 nEpochDays2;
2685 UsefulBufC StringDate1, StringDate2, StringDays2;
Laurence Lundbladec7114722020-08-13 05:11:40 -07002686
2687 QCBORDecode_Init(&DC,
Laurence Lundblade4b270642020-08-14 12:53:07 -07002688 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSpiffyDateTestInput),
Laurence Lundbladec7114722020-08-13 05:11:40 -07002689 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade37286c02022-09-03 10:05:02 -07002690
2691 /* Items are in an array or map to test look up by label and other
2692 * that might not occur in isolated items. But it does make the
2693 * test a bit messy. */
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07002694 QCBORDecode_EnterArray(&DC, NULL);
Laurence Lundbladec7114722020-08-13 05:11:40 -07002695
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07002696 QCBORDecode_EnterMap(&DC, NULL);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002697
Laurence Lundblade4b270642020-08-14 12:53:07 -07002698 // A single-precision date
Laurence Lundblade9b334962020-08-27 10:55:53 -07002699 QCBORDecode_GetEpochDateInMapSZ(&DC, "x", QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
2700 &nEpochDate5);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002701 uError = QCBORDecode_GetAndResetError(&DC);
Laurence Lundblade16a207a2021-09-18 17:22:46 -07002702 if(uError != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002703 return 104;
2704 }
Laurence Lundblade16a207a2021-09-18 17:22:46 -07002705 if(uError == QCBOR_SUCCESS) {
2706 if(nEpochDate5 != 3) {
2707 return 103;
2708 }
2709 }
Laurence Lundbladec7114722020-08-13 05:11:40 -07002710
Laurence Lundblade9b334962020-08-27 10:55:53 -07002711 // A half-precision date with value -2 FFF
2712 QCBORDecode_GetEpochDateInMapN(&DC, 9, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
2713 &nEpochDate4);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002714 uError = QCBORDecode_GetAndResetError(&DC);
Laurence Lundblade16a207a2021-09-18 17:22:46 -07002715 if(uError != FLOAT_ERR_CODE_NO_HALF_PREC_NO_FLOAT_HW(QCBOR_SUCCESS)) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002716 return 106;
2717 }
Laurence Lundblade16a207a2021-09-18 17:22:46 -07002718 if(uError == QCBOR_SUCCESS) {
2719 if(nEpochDate4 != -2) {
2720 return 105;
2721 }
2722 }
Laurence Lundblade4b270642020-08-14 12:53:07 -07002723
2724 // Fail to get an epoch date by string label
Laurence Lundblade9b334962020-08-27 10:55:53 -07002725 QCBORDecode_GetEpochDateInMapSZ(&DC, "no-label",
2726 QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
2727 &nEpochDate6);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002728 uError = QCBORDecode_GetAndResetError(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002729 if(uError != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002730 return 107;
2731 }
2732
2733 // Fail to get an epoch date by integer label
Laurence Lundblade9b334962020-08-27 10:55:53 -07002734 QCBORDecode_GetEpochDateInMapN(&DC, 99999, QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
2735 &nEpochDate6);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002736 uError = QCBORDecode_GetAndResetError(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002737 if(uError != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002738 return 108;
2739 }
2740
2741 // Fail to get a string date by string label
Laurence Lundblade9b334962020-08-27 10:55:53 -07002742 QCBORDecode_GetDateStringInMapSZ(&DC, "no-label",
2743 QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
2744 &StringDate1);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002745 uError = QCBORDecode_GetAndResetError(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002746 if(uError != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002747 return 109;
2748 }
2749
2750 // Fail to get a string date by integer label
Laurence Lundblade9b334962020-08-27 10:55:53 -07002751 QCBORDecode_GetDateStringInMapN(&DC, 99999, QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
2752 &StringDate1);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002753 uError = QCBORDecode_GetAndResetError(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002754 if(uError != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002755 return 110;
2756 }
2757
2758 // The rest of these succeed even if float features are disabled
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002759
Laurence Lundblade37286c02022-09-03 10:05:02 -07002760
2761 // Untagged integer 0
2762 QCBORDecode_GetEpochDateInMapN(&DC, 8, QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
2763 &nEpochDate3);
2764 // Untagged date string
2765 QCBORDecode_GetDateStringInMapSZ(&DC, "y", QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
2766 &StringDate2);
2767
2768 QCBORDecode_GetDaysStringInMapN(&DC, 99, QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
2769 &StringDays2);
2770
2771 QCBORDecode_GetEpochDaysInMapN(&DC, 17, QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
2772 &nEpochDays2);
2773
2774 QCBORDecode_ExitMap(&DC);
2775 if(QCBORDecode_GetError(&DC) != QCBOR_SUCCESS) {
2776 return 3001;
2777 }
2778
2779 // The map of tagged items
2780 QCBORDecode_EnterMap(&DC, NULL);
2781
2782#ifndef QCBOR_DISABLE_TAGS
2783 int64_t nEpochDate2,
2784 nEpochDateFail,
2785 nEpochDate1400000000, nEpochDays1;
2786 UsefulBufC StringDays1;
2787 uint64_t uTag1, uTag2;
2788
2789 // Tagged date string
2790 QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
2791 &StringDate1);
2792
Laurence Lundblade4b270642020-08-14 12:53:07 -07002793 // Epoch date 1400000000; Tue, 13 May 2014 16:53:20 GMT
Laurence Lundblade9b334962020-08-27 10:55:53 -07002794 QCBORDecode_GetEpochDateInMapN(&DC,
2795 1,
2796 QCBOR_TAG_REQUIREMENT_TAG |
2797 QCBOR_TAG_REQUIREMENT_ALLOW_ADDITIONAL_TAGS,
2798 &nEpochDate1400000000);
2799 uTag1 = QCBORDecode_GetNthTagOfLast(&DC, 0);
Laurence Lundblade37286c02022-09-03 10:05:02 -07002800
2801 // Get largest negative double precision epoch date allowed
2802 QCBORDecode_GetEpochDateInMapN(&DC,
2803 5,
2804 QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG |
2805 QCBOR_TAG_REQUIREMENT_ALLOW_ADDITIONAL_TAGS,
2806 &nEpochDate2);
2807 uError = QCBORDecode_GetAndResetError(&DC);
2808 if(uError != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)) {
2809 return 102;
2810 }
2811 if(uError == QCBOR_SUCCESS) {
2812 if(nEpochDate2 != -9223372036854773760LL) {
2813 return 101;
2814 }
2815 }
2816
Laurence Lundblade4b270642020-08-14 12:53:07 -07002817 // Untagged -1000 with label z
Laurence Lundblade9b334962020-08-27 10:55:53 -07002818 QCBORDecode_GetEpochDateInMapSZ(&DC,
2819 "z",
2820 QCBOR_TAG_REQUIREMENT_NOT_A_TAG |
2821 QCBOR_TAG_REQUIREMENT_ALLOW_ADDITIONAL_TAGS,
2822 &nEpochDate6);
2823 uTag2 = QCBORDecode_GetNthTagOfLast(&DC, 0);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002824
Laurence Lundblade37286c02022-09-03 10:05:02 -07002825
2826 // Get largest double precision epoch date allowed
2827 QCBORDecode_GetEpochDateInMapN(&DC, 7, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
2828 &nEpochDate2);
2829 uError = QCBORDecode_GetAndResetError(&DC);
2830 if(uError != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)) {
2831 return 112;
2832 }
2833 if(uError == QCBOR_SUCCESS) {
2834 if(nEpochDate2 != 9223372036854773760ULL) {
2835 return 111;
2836 }
2837 }
2838
Laurence Lundblade46d63e92021-05-13 11:37:10 -07002839 /* The days format is much simpler than the date format
2840 * because it can't be a floating point value. The test
2841 * of the spiffy decode functions sufficiently covers
2842 * the test of the non-spiffy decode days date decoding.
2843 * There is no full fan out of the error conditions
2844 * and decode options as that is implemented by code
2845 * that is tested well by the date testing above.
2846 */
2847 QCBORDecode_GetDaysStringInMapSZ(&DC, "SDS", QCBOR_TAG_REQUIREMENT_TAG,
2848 &StringDays1);
2849
Laurence Lundblade46d63e92021-05-13 11:37:10 -07002850 QCBORDecode_GetEpochDaysInMapSZ(&DC, "SDE", QCBOR_TAG_REQUIREMENT_TAG,
2851 &nEpochDays1);
2852
Laurence Lundblade4b270642020-08-14 12:53:07 -07002853 QCBORDecode_ExitMap(&DC);
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07002854 if(QCBORDecode_GetError(&DC) != QCBOR_SUCCESS) {
2855 return 3001;
2856 }
2857
Laurence Lundblade37286c02022-09-03 10:05:02 -07002858 // Too-negative float, -9.2233720368547748E+18
2859 QCBORDecode_GetEpochDate(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nEpochDateFail);
2860 uError = QCBORDecode_GetAndResetError(&DC);
2861 if(uError != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_DATE_OVERFLOW)) {
2862 return 1111;
2863 }
2864
2865 // Too-large integer
2866 QCBORDecode_GetEpochDate(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nEpochDateFail);
2867 uError = QCBORDecode_GetAndResetError(&DC);
2868 if(uError != QCBOR_ERR_DATE_OVERFLOW) {
2869 return 1;
2870 }
2871
2872 // Half-precision minus infinity
2873 QCBORDecode_GetEpochDate(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nEpochDateFail);
2874 uError = QCBORDecode_GetAndResetError(&DC);
2875 if(uError != FLOAT_ERR_CODE_NO_HALF_PREC_NO_FLOAT_HW(QCBOR_ERR_DATE_OVERFLOW)) {
2876 return 2;
2877 }
2878
2879
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07002880 // Bad content for epoch date
2881 QCBORDecode_GetEpochDate(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nEpochDateFail);
2882 uError = QCBORDecode_GetAndResetError(&DC);
2883 if(uError != QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT) {
2884 return 3;
2885 }
2886
2887 // Bad content for string date
2888 QCBORDecode_GetDateString(&DC, QCBOR_TAG_REQUIREMENT_TAG, &StringDate1);
2889 uError = QCBORDecode_GetAndResetError(&DC);
2890 if(uError != QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT) {
2891 return 4;
2892 }
2893
Laurence Lundblade4b270642020-08-14 12:53:07 -07002894 QCBORDecode_ExitArray(&DC);
2895 uError = QCBORDecode_Finish(&DC);
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07002896 if(uError != QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT) {
Laurence Lundblade9b334962020-08-27 10:55:53 -07002897 return 1000 + (int32_t)uError;
Laurence Lundblade4b270642020-08-14 12:53:07 -07002898 }
Laurence Lundblade37286c02022-09-03 10:05:02 -07002899#else /* QCBOR_DISABLE_TAGS */
2900 QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
2901 &StringDate1);
2902 uError = QCBORDecode_GetAndResetError(&DC);
2903 if(uError != QCBOR_ERR_TAGS_DISABLED) {
2904 return 4;
2905 }
2906#endif /* QCBOR_DISABLE_TAGS */
2907
2908
2909#ifndef QCBOR_DISABLE_TAGS
Laurence Lundblade4b270642020-08-14 12:53:07 -07002910
Laurence Lundblade9b334962020-08-27 10:55:53 -07002911 if(nEpochDate1400000000 != 1400000000) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002912 return 200;
2913 }
2914
Laurence Lundblade9b334962020-08-27 10:55:53 -07002915 if(uTag1 != 0x03030303) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002916 return 201;
2917 }
2918
Laurence Lundblade37286c02022-09-03 10:05:02 -07002919 if(nEpochDays1 != -10676) {
2920 return 205;
Laurence Lundblade4b270642020-08-14 12:53:07 -07002921 }
2922
Laurence Lundblade37286c02022-09-03 10:05:02 -07002923 if(UsefulBuf_Compare(StringDays1, UsefulBuf_FromSZ("1985-04-12"))) {
2924 return 207;
Laurence Lundblade4b270642020-08-14 12:53:07 -07002925 }
2926
Laurence Lundblade9b334962020-08-27 10:55:53 -07002927 if(uTag2 != 0x01010101) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002928 return 204;
2929 }
2930
Laurence Lundblade37286c02022-09-03 10:05:02 -07002931 if(nEpochDate6 != -1000) {
2932 return 203;
Laurence Lundblade46d63e92021-05-13 11:37:10 -07002933 }
2934
Laurence Lundblade9b334962020-08-27 10:55:53 -07002935 if(UsefulBuf_Compare(StringDate1, UsefulBuf_FromSZ("1985-04-12"))) {
2936 return 205;
2937 }
2938
Laurence Lundblade37286c02022-09-03 10:05:02 -07002939#endif /* QCBOR_DISABLE_TAGS */
2940
2941 if(nEpochDate3 != 0) {
2942 return 202;
2943 }
2944
2945 if(nEpochDays2 != 3994) {
Laurence Lundblade9b334962020-08-27 10:55:53 -07002946 return 206;
2947 }
2948
Laurence Lundblade37286c02022-09-03 10:05:02 -07002949 if(UsefulBuf_Compare(StringDate2, UsefulBuf_FromSZ("2085-04-12"))) {
2950 return 206;
Laurence Lundblade46d63e92021-05-13 11:37:10 -07002951 }
2952
2953 if(UsefulBuf_Compare(StringDays2, UsefulBuf_FromSZ("1985-04-12"))) {
2954 return 208;
2955 }
2956
Laurence Lundbladec7114722020-08-13 05:11:40 -07002957 return 0;
2958}
2959
2960
Laurence Lundblade9b334962020-08-27 10:55:53 -07002961// Input for one of the tagging tests
Laurence Lundbladecc7da412020-12-27 00:09:07 -08002962static const uint8_t spTagInput[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002963 0xd9, 0xd9, 0xf7, // CBOR magic number
Laurence Lundblade9b334962020-08-27 10:55:53 -07002964 0x81, // Array of one
2965 0xd8, 0x04, // non-preferred serialization of tag 4, decimal fraction
2966 0x82, // Array of two that is the faction 1/3
2967 0x01,
2968 0x03,
2969
2970 /*
2971 More than 4 tags on an item 225(226(227(228(229([])))))
2972 */
2973 0xd8, 0xe1,
2974 0xd8, 0xe2,
2975 0xd8, 0xe3,
2976 0xd8, 0xe4,
2977 0xd8, 0xe5,
2978 0x80,
2979
2980 /* tag 10489608748473423768(
2981 2442302356(
2982 21590(
2983 240(
2984 []))))
2985 */
2986 0xdb, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98,
2987 0xda, 0x91, 0x92, 0x93, 0x94,
2988 0xd9, 0x54, 0x56,
2989 0xd8, 0xf0,
2990 0x80,
2991
2992 /* tag 21590(
2993 10489608748473423768(
2994 2442302357(
2995 65534(
2996 []))))
2997 */
2998 0xdb, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x54, 0x56,
2999 0xdb, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98,
3000 0xda, 0x91, 0x92, 0x93, 0x95,
3001 0xd9, 0xff, 0xfe,
3002 0x80,
3003
3004 /* Make sure to blow past the limit of tags that must be mapped.
3005 works in conjuntion with entries above.
3006 269488144(269488145(269488146(269488147([]))))
3007 */
3008 0xda, 0x10, 0x10, 0x10, 0x10,
3009 0xda, 0x10, 0x10, 0x10, 0x11,
3010 0xda, 0x10, 0x10, 0x10, 0x12,
3011 0xda, 0x10, 0x10, 0x10, 0x13,
3012 0x80,
3013
3014 /* An invalid decimal fraction with an additional tag */
3015 0xd9, 0xff, 0xfa,
3016 0xd8, 0x02, // non-preferred serialization of tag 2, a big num
3017 0x00, // the integer 0; should be a byte string
3018};
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003019
Laurence Lundblade59289e52019-12-30 13:44:37 -08003020/*
3021 DB 9192939495969798 # tag(10489608748473423768)
Laurence Lundblade9b334962020-08-27 10:55:53 -07003022 80 # array(0)
Laurence Lundblade59289e52019-12-30 13:44:37 -08003023 */
Laurence Lundbladecc7da412020-12-27 00:09:07 -08003024static const uint8_t spEncodedLargeTag[] = {0xdb, 0x91, 0x92, 0x93, 0x94, 0x95,
Laurence Lundbladeee851742020-01-08 08:37:05 -08003025 0x96, 0x97, 0x98, 0x80};
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003026
Laurence Lundblade59289e52019-12-30 13:44:37 -08003027/*
3028DB 9192939495969798 # tag(10489608748473423768)
3029 D8 88 # tag(136)
3030 C6 # tag(6)
3031 C7 # tag(7)
3032 80 # array(0)
3033*/
Laurence Lundbladecc7da412020-12-27 00:09:07 -08003034static const uint8_t spLotsOfTags[] = {0xdb, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96,
Laurence Lundbladeee851742020-01-08 08:37:05 -08003035 0x97, 0x98, 0xd8, 0x88, 0xc6, 0xc7, 0x80};
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003036
3037/*
Laurence Lundblade9b334962020-08-27 10:55:53 -07003038 55799(55799(55799({
3039 6(7(-23)): 5859837686836516696(7({
3040 7(-20): 11({
3041 17(-18): 17(17(17("Organization"))),
3042 9(-17): 773("SSG"),
3043 -15: 16(17(6(7("Confusion")))),
3044 17(-16): 17("San Diego"),
3045 17(-14): 17("US")
3046 }),
3047 23(-19): 19({
3048 -11: 9({
3049 -9: -7
3050 }),
3051 90599561(90599561(90599561(-10))): 12(h'0102030405060708090A')
3052 })
3053 })),
3054 16(-22): 23({
3055 11(8(7(-5))): 8(-3)
3056 })
3057 })))
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003058 */
Laurence Lundbladecc7da412020-12-27 00:09:07 -08003059static const uint8_t spCSRWithTags[] = {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003060 0xd9, 0xd9, 0xf7, 0xd9, 0xd9, 0xf7, 0xd9, 0xd9, 0xf7, 0xa2,
3061 0xc6, 0xc7, 0x36,
3062 0xdb, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0xc7, 0xa2,
3063 0xda, 0x00, 0x00, 0x00, 0x07, 0x33,
3064 0xcb, 0xa5,
3065 0xd1, 0x31,
3066 0xd1, 0xd1, 0xd1, 0x6c,
3067 0x4f, 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74, 0x69, 0x6f, 0x6e,
3068 0xc9, 0x30,
3069 0xd9, 0x03, 0x05, 0x63,
3070 0x53, 0x53, 0x47,
3071 0x2e,
Laurence Lundblade9b334962020-08-27 10:55:53 -07003072 0xd0, 0xd1, 0xc6, 0xc7,
3073 0x69,
3074 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73, 0x69, 0x6f, 0x6e,
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003075 0xd1, 0x2f,
3076 0xd1, 0x69,
3077 0x53, 0x61, 0x6e, 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f,
3078 0xd1, 0x2d,
3079 0xd1, 0x62,
3080 0x55, 0x53,
3081 0xd7, 0x32,
3082 0xd3, 0xa2,
3083 0x2a,
3084 0xc9, 0xa1,
3085 0x28,
3086 0x26,
3087 0xda, 0x05, 0x66, 0x70, 0x89, 0xda, 0x05, 0x66, 0x70, 0x89, 0xda, 0x05, 0x66, 0x70, 0x89, 0x29,
3088 0xcc, 0x4a,
3089 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,0x07, 0x08, 0x09, 0x0a,
3090 0xd0, 0x35,
3091 0xd7, 0xa1,
3092 0xcb, 0xc8, 0xc7, 0x24,
3093 0xc8, 0x22};
3094
Laurence Lundblade9b334962020-08-27 10:55:53 -07003095
Laurence Lundbladecc7da412020-12-27 00:09:07 -08003096static const uint8_t spSpiffyTagInput[] = {
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08003097 0x85, // Open array
Laurence Lundblade9b334962020-08-27 10:55:53 -07003098
3099 0xc0, // tag for string date
3100 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string
3101
3102 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string
3103
3104 0x4a, '1','9','8','5','-','0','4','-','1','2', // Date string in byte string
3105
3106 0xd8, 0x23, // tag for regex
3107 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string
3108
3109 0xc0, // tag for string date
3110 0x4a, '1','9','8','5','-','0','4','-','1','2', // Date string in byte string
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07003111
3112 // This last case makes the array untraversable because it is
3113 // an uncrecoverable error. Make sure it stays last and is the only
3114 // instance so the other tests can work.
Laurence Lundblade9b334962020-08-27 10:55:53 -07003115};
3116
3117
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003118static int32_t CheckCSRMaps(QCBORDecodeContext *pDC);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003119
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003120
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03003121int32_t OptTagParseTest(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003122{
3123 QCBORDecodeContext DCtx;
Laurence Lundblade9b334962020-08-27 10:55:53 -07003124 QCBORItem Item;
3125 QCBORError uError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003126
Laurence Lundbladeee851742020-01-08 08:37:05 -08003127 QCBORDecode_Init(&DCtx,
Laurence Lundblade9b334962020-08-27 10:55:53 -07003128 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTagInput),
Laurence Lundbladeee851742020-01-08 08:37:05 -08003129 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003130
Laurence Lundblade9b334962020-08-27 10:55:53 -07003131 /*
3132 This test matches the magic number tag and the fraction tag
3133 55799([...])
3134 */
3135 uError = QCBORDecode_GetNext(&DCtx, &Item);
3136 if(uError != QCBOR_SUCCESS) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003137 return -2;
3138 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003139 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003140 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC)) {
3141 return -3;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003142 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003143
Laurence Lundblade9b334962020-08-27 10:55:53 -07003144 /*
3145 4([1,3])
3146 */
3147 uError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundbladedd6e76e2021-03-10 01:54:01 -07003148#ifdef QCBOR_DISABLE_EXP_AND_MANTISSA
Laurence Lundblade9b334962020-08-27 10:55:53 -07003149 if(uError != QCBOR_SUCCESS ||
3150 Item.uDataType != QCBOR_TYPE_ARRAY ||
3151 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_DECIMAL_FRACTION) ||
3152 QCBORDecode_GetNthTag(&DCtx, &Item, 0) != CBOR_TAG_DECIMAL_FRACTION ||
3153 QCBORDecode_GetNthTag(&DCtx, &Item, 1) != CBOR_TAG_INVALID64 ||
3154 QCBORDecode_GetNthTag(&DCtx, &Item, 2) != CBOR_TAG_INVALID64 ||
3155 QCBORDecode_GetNthTag(&DCtx, &Item, 3) != CBOR_TAG_INVALID64 ||
3156 QCBORDecode_GetNthTag(&DCtx, &Item, 4) != CBOR_TAG_INVALID64 ||
3157 Item.val.uCount != 2) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003158 return -4;
3159 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07003160 // consume the items in the array
3161 uError = QCBORDecode_GetNext(&DCtx, &Item);
3162 uError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundblade59289e52019-12-30 13:44:37 -08003163
Laurence Lundbladedd6e76e2021-03-10 01:54:01 -07003164#else /* QCBOR_DISABLE_EXP_AND_MANTISSA */
Laurence Lundblade9b334962020-08-27 10:55:53 -07003165 if(uError != QCBOR_SUCCESS ||
3166 Item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION ||
3167 QCBORDecode_GetNthTag(&DCtx, &Item, 0) != CBOR_TAG_INVALID64 ||
3168 QCBORDecode_GetNthTag(&DCtx, &Item, 1) != CBOR_TAG_INVALID64 ||
3169 QCBORDecode_GetNthTag(&DCtx, &Item, 2) != CBOR_TAG_INVALID64 ||
3170 QCBORDecode_GetNthTag(&DCtx, &Item, 3) != CBOR_TAG_INVALID64 ||
3171 QCBORDecode_GetNthTag(&DCtx, &Item, 4) != CBOR_TAG_INVALID64 ) {
3172 return -5;
Laurence Lundblade59289e52019-12-30 13:44:37 -08003173 }
Laurence Lundbladedd6e76e2021-03-10 01:54:01 -07003174#endif /* QCBOR_DISABLE_EXP_AND_MANTISSA */
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003175
Laurence Lundblade9b334962020-08-27 10:55:53 -07003176 /*
3177 More than 4 tags on an item 225(226(227(228(229([])))))
3178 */
3179 uError = QCBORDecode_GetNext(&DCtx, &Item);
3180 if(uError != QCBOR_ERR_TOO_MANY_TAGS) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003181 return -6;
3182 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07003183
Laurence Lundblade88e9db22020-11-02 03:56:33 -08003184 if(QCBORDecode_GetNthTag(&DCtx, &Item, 0) != CBOR_TAG_INVALID64) {
3185 return -106;
3186 }
3187
3188
Laurence Lundblade9b334962020-08-27 10:55:53 -07003189 /* tag 10489608748473423768(
3190 2442302356(
3191 21590(
3192 240(
3193 []))))
3194 */
3195 uError = QCBORDecode_GetNext(&DCtx, &Item);
3196 if(uError != QCBOR_SUCCESS ||
3197 Item.uDataType != QCBOR_TYPE_ARRAY ||
3198 QCBORDecode_GetNthTag(&DCtx, &Item, 3) != 10489608748473423768ULL ||
3199 QCBORDecode_GetNthTag(&DCtx, &Item, 2) != 2442302356ULL ||
3200 QCBORDecode_GetNthTag(&DCtx, &Item, 1) != 21590ULL ||
3201 QCBORDecode_GetNthTag(&DCtx, &Item, 0) != 240ULL) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003202 return -7;
Laurence Lundblade9b334962020-08-27 10:55:53 -07003203 }
3204
3205 /* tag 21590(
3206 10489608748473423768(
3207 2442302357(
3208 21591(
3209 []))))
3210 */
3211 uError = QCBORDecode_GetNext(&DCtx, &Item);
3212 if(uError != QCBOR_SUCCESS ||
3213 Item.uDataType != QCBOR_TYPE_ARRAY ||
3214 QCBORDecode_GetNthTag(&DCtx, &Item, 0) != 65534ULL ||
3215 QCBORDecode_GetNthTag(&DCtx, &Item, 1) != 2442302357ULL ||
3216 QCBORDecode_GetNthTag(&DCtx, &Item, 2) != 10489608748473423768ULL ||
3217 QCBORDecode_GetNthTag(&DCtx, &Item, 3) != 21590ULL) {
3218 return -8;
3219 }
3220
3221 /* Make sure to blow past the limit of tags that must be mapped.
3222 works in conjuntion with entries above.
3223 269488144(269488145(269488146(269488147([]))))
3224 */
3225 uError = QCBORDecode_GetNext(&DCtx, &Item);
3226 if(uError != QCBOR_ERR_TOO_MANY_TAGS) {
3227 return -9;
3228 }
3229
3230 uError = QCBORDecode_GetNext(&DCtx, &Item);
3231 if(uError == QCBOR_SUCCESS) {
3232 return -10;
3233 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003234
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003235 // ----------------------------------
Laurence Lundbladeee851742020-01-08 08:37:05 -08003236 // This test sets up a caller-config list that includes the very large
Laurence Lundblade9b334962020-08-27 10:55:53 -07003237 // tage and then matches it. Caller-config lists are no longer
3238 // used or needed. This tests backwards compatibility with them.
Laurence Lundbladeee851742020-01-08 08:37:05 -08003239 QCBORDecode_Init(&DCtx,
3240 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEncodedLargeTag),
3241 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003242 const uint64_t puList[] = {0x9192939495969798, 257};
3243 const QCBORTagListIn TL = {2, puList};
3244 QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003245
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003246 if(QCBORDecode_GetNext(&DCtx, &Item)) {
3247 return -8;
3248 }
3249 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
3250 !QCBORDecode_IsTagged(&DCtx, &Item, 0x9192939495969798) ||
3251 QCBORDecode_IsTagged(&DCtx, &Item, 257) ||
3252 QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_BIGFLOAT) ||
3253 Item.val.uCount != 0) {
3254 return -9;
3255 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003256
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003257 //------------------------
Laurence Lundbladeee851742020-01-08 08:37:05 -08003258 // Sets up a caller-configured list and look up something not in it
Laurence Lundblade9b334962020-08-27 10:55:53 -07003259 // Another backwards compatibility test.
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003260 const uint64_t puLongList[17] = {1,2,1};
3261 const QCBORTagListIn TLLong = {17, puLongList};
Laurence Lundbladeee851742020-01-08 08:37:05 -08003262 QCBORDecode_Init(&DCtx,
3263 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEncodedLargeTag),
3264 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003265 QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TLLong);
3266 if(QCBORDecode_GetNext(&DCtx, &Item)) {
3267 return -11;
3268 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003269
Laurence Lundbladef7a70bc2020-10-24 12:23:25 -07003270 uint64_t puTags[4];
Laurence Lundblade9b334962020-08-27 10:55:53 -07003271 QCBORTagListOut Out = {0, 4, puTags};
3272
3273
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003274 // This tests retrievel of the full tag list
Laurence Lundbladeee851742020-01-08 08:37:05 -08003275 QCBORDecode_Init(&DCtx,
3276 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spLotsOfTags),
3277 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003278 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3279 return -12;
3280 }
3281 if(puTags[0] != 0x9192939495969798 ||
3282 puTags[1] != 0x88 ||
Laurence Lundblade59289e52019-12-30 13:44:37 -08003283 puTags[2] != 0x06 ||
3284 puTags[3] != 0x07) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003285 return -13;
3286 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003287
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003288 // ----------------------
Laurence Lundblade9b334962020-08-27 10:55:53 -07003289 // This tests too small of an out list
Laurence Lundbladeee851742020-01-08 08:37:05 -08003290 QCBORDecode_Init(&DCtx,
3291 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spLotsOfTags),
3292 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003293 QCBORTagListOut OutSmall = {0, 3, puTags};
3294 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &OutSmall) != QCBOR_ERR_TOO_MANY_TAGS) {
3295 return -14;
3296 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003297
Laurence Lundblade9b334962020-08-27 10:55:53 -07003298
3299
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003300 // ---------------
Laurence Lundblade9b334962020-08-27 10:55:53 -07003301 // Decode a version of the "CSR" that has had a ton of tags randomly inserted
3302 // It is a bit of a messy test and maybe could be improved, but
3303 // it is retained as a backwards compatibility check.
Laurence Lundbladeee851742020-01-08 08:37:05 -08003304 QCBORDecode_Init(&DCtx,
3305 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRWithTags),
3306 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003307 int n = CheckCSRMaps(&DCtx);
3308 if(n) {
3309 return n-2000;
3310 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003311
Laurence Lundblade59289e52019-12-30 13:44:37 -08003312 Out = (QCBORTagListOut){0, 16, puTags};
Laurence Lundbladeee851742020-01-08 08:37:05 -08003313 QCBORDecode_Init(&DCtx,
3314 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRWithTags),
3315 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003316
Laurence Lundblade9b334962020-08-27 10:55:53 -07003317 /* With the spiffy decode revision, this tag list is not used.
3318 It doesn't matter if a tag is in this list or not so some
3319 tests that couldn't process a tag because it isn't in this list
3320 now can process these unlisted tags. The tests have been
3321 adjusted for this. */
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003322 const uint64_t puTagList[] = {773, 1, 90599561};
3323 const QCBORTagListIn TagList = {3, puTagList};
3324 QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TagList);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003325
3326
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003327 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3328 return -100;
3329 }
3330 if(Item.uDataType != QCBOR_TYPE_MAP ||
3331 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC) ||
3332 QCBORDecode_IsTagged(&DCtx, &Item, 90599561) ||
3333 QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_DATE_EPOCH) ||
3334 Item.val.uCount != 2 ||
3335 puTags[0] != CBOR_TAG_CBOR_MAGIC ||
3336 puTags[1] != CBOR_TAG_CBOR_MAGIC ||
3337 puTags[2] != CBOR_TAG_CBOR_MAGIC ||
3338 Out.uNumUsed != 3) {
3339 return -101;
3340 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003341
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003342 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3343 return -102;
3344 }
3345 if(Item.uDataType != QCBOR_TYPE_MAP ||
3346 QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC) ||
3347 QCBORDecode_IsTagged(&DCtx, &Item, 6) ||
Laurence Lundbladef7a70bc2020-10-24 12:23:25 -07003348 !QCBORDecode_IsTagged(&DCtx, &Item, 7) ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003349 Item.val.uCount != 2 ||
3350 puTags[0] != 5859837686836516696 ||
3351 puTags[1] != 7 ||
3352 Out.uNumUsed != 2) {
3353 return -103;
3354 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003355
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003356 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3357 return -104;
3358 }
3359 if(Item.uDataType != QCBOR_TYPE_MAP ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003360 Item.val.uCount != 5 ||
3361 puTags[0] != 0x0b ||
3362 Out.uNumUsed != 1) {
3363 return -105;
3364 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003365
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003366 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3367 return -106;
3368 }
3369 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
3370 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_COSE_MAC0) ||
3371 Item.val.string.len != 12 ||
3372 puTags[0] != CBOR_TAG_COSE_MAC0 ||
3373 puTags[1] != CBOR_TAG_COSE_MAC0 ||
3374 puTags[2] != CBOR_TAG_COSE_MAC0 ||
3375 Out.uNumUsed != 3) {
3376 return -105;
3377 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003378
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003379 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3380 return -107;
3381 }
3382 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
3383 !QCBORDecode_IsTagged(&DCtx, &Item, 773) ||
3384 Item.val.string.len != 3 ||
3385 puTags[0] != 773 ||
3386 Out.uNumUsed != 1) {
3387 return -108;
3388 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003389
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003390 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3391 return -109;
3392 }
3393 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade59289e52019-12-30 13:44:37 -08003394 !QCBORDecode_IsTagged(&DCtx, &Item, 16) ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003395 Item.val.string.len != 9 ||
Laurence Lundblade59289e52019-12-30 13:44:37 -08003396 puTags[0] != 16 ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07003397 puTags[3] != 7 ||
3398 Out.uNumUsed != 4) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003399 return -110;
3400 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003401
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003402 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3403 return -111;
3404 }
3405 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
3406 !QCBORDecode_IsTagged(&DCtx, &Item, 17) ||
3407 Item.val.string.len != 9 ||
3408 puTags[0] != 17 ||
3409 Out.uNumUsed != 1) {
3410 return -112;
3411 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003412
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003413 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3414 return -111;
3415 }
3416 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
3417 !QCBORDecode_IsTagged(&DCtx, &Item, 17) ||
3418 Item.val.string.len != 2 ||
3419 puTags[0] != 17 ||
3420 Out.uNumUsed != 1) {
3421 return -112;
3422 }
3423
3424 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3425 return -113;
3426 }
3427 if(Item.uDataType != QCBOR_TYPE_MAP ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07003428 !QCBORDecode_IsTagged(&DCtx, &Item, 19) ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003429 Item.val.uCount != 2 ||
3430 puTags[0] != 19 ||
3431 Out.uNumUsed != 1) {
3432 return -114;
3433 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003434
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003435 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3436 return -115;
3437 }
3438 if(Item.uDataType != QCBOR_TYPE_MAP ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07003439 !QCBORDecode_IsTagged(&DCtx, &Item, 9) ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003440 Item.val.uCount != 1 ||
3441 puTags[0] != 9 ||
3442 Out.uNumUsed != 1) {
3443 return -116;
3444 }
3445
3446 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3447 return -116;
3448 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003449 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003450 Item.val.int64 != -7 ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003451 Out.uNumUsed != 0) {
3452 return -117;
3453 }
3454
3455 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3456 return -118;
3457 }
3458 if(Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
3459 Item.val.string.len != 10 ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003460 puTags[0] != 12 ||
3461 Out.uNumUsed != 1) {
3462 return -119;
3463 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003464
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003465 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3466 return -120;
3467 }
3468 if(Item.uDataType != QCBOR_TYPE_MAP ||
3469 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_ENC_AS_B16) ||
3470 Item.val.uCount != 1 ||
3471 puTags[0] != 0x17 ||
3472 Out.uNumUsed != 1) {
3473 return -121;
3474 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003475
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003476 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3477 return -122;
3478 }
3479 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07003480 !QCBORDecode_IsTagged(&DCtx, &Item, 8) ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003481 Item.val.int64 != -3 ||
3482 puTags[0] != 8 ||
3483 Out.uNumUsed != 1) {
3484 return -123;
3485 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003486
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003487 if(QCBORDecode_Finish(&DCtx)) {
3488 return -124;
3489 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07003490
3491 UsefulBufC DateString;
3492 QCBORDecode_Init(&DCtx,
3493 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSpiffyTagInput),
3494 QCBOR_DECODE_MODE_NORMAL);
3495
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07003496 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade9b334962020-08-27 10:55:53 -07003497 // tagged date string
3498 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString);
3499 // untagged date string
3500 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString);
3501 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_SUCCESS) {
3502 return 100;
3503 }
3504 // untagged byte string
3505 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString);
3506 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
3507 return 101;
3508 }
3509 // tagged regex
3510 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString);
3511 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
3512 return 102;
3513 }
3514 // tagged date string with a byte string
3515 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString);
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07003516 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT) {
Laurence Lundblade9b334962020-08-27 10:55:53 -07003517 return 103;
3518 }
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07003519 // The exit errors out because the last item, the date string with
3520 // bad content makes the array untraversable (the bad date string
3521 // could have tag content of an array or such that is not consumed
3522 // by the date decoding).
Laurence Lundblade9b334962020-08-27 10:55:53 -07003523 QCBORDecode_ExitArray(&DCtx);
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07003524 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT) {
Laurence Lundblade9b334962020-08-27 10:55:53 -07003525 return 104;
3526 }
3527
3528
3529 QCBORDecode_Init(&DCtx,
3530 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSpiffyTagInput),
3531 QCBOR_DECODE_MODE_NORMAL);
3532
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07003533 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade9b334962020-08-27 10:55:53 -07003534 // tagged date string
3535 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString);
3536 // untagged date string
3537 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString);
3538 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_SUCCESS) {
3539 return 200;
3540 }
3541 // untagged byte string
3542 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString);
3543 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
3544 return 201;
3545 }
3546 // tagged regex
3547 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString);
3548 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
3549 return 202;
3550 }
3551 // tagged date string with a byte string
3552 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString);
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07003553 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT) {
Laurence Lundblade9b334962020-08-27 10:55:53 -07003554 return 203;
3555 }
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07003556 // See comments above
Laurence Lundblade9b334962020-08-27 10:55:53 -07003557 QCBORDecode_ExitArray(&DCtx);
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07003558 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT) {
Laurence Lundblade9b334962020-08-27 10:55:53 -07003559 return 204;
3560 }
3561
3562 QCBORDecode_Init(&DCtx,
3563 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSpiffyTagInput),
3564 QCBOR_DECODE_MODE_NORMAL);
3565
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07003566 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade9b334962020-08-27 10:55:53 -07003567 // tagged date string
3568 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString);
3569 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
3570 return 300;
3571 }
3572 // untagged date string
3573 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString);
3574 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
3575 return 301;
3576 }
3577 // untagged byte string
3578 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString);
3579 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
3580 return 302;
3581 }
3582 // tagged regex
3583 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString);
3584 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
3585 return 303;
3586 }
3587 // tagged date string with a byte string
3588 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString);
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07003589 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT) {
Laurence Lundblade9b334962020-08-27 10:55:53 -07003590 return 304;
3591 }
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07003592 // See comments above
Laurence Lundblade9b334962020-08-27 10:55:53 -07003593 QCBORDecode_ExitArray(&DCtx);
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07003594 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT) {
Laurence Lundblade9b334962020-08-27 10:55:53 -07003595 return 305;
3596 }
3597
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003598 return 0;
3599}
3600
Laurence Lundblade37286c02022-09-03 10:05:02 -07003601/*
3602 * These are showing the big numbers converted to integers.
3603 * The tag numbers are not shown.
3604 *
3605 * [ 18446744073709551616,
3606 * -18446744073709551617,
3607 * {"BN+": 18446744073709551616,
3608 * 64: 18446744073709551616,
3609 * "BN-": -18446744073709551617,
3610 * -64: -18446744073709551617
3611 * }
3612 * ]
3613 */
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003614
Laurence Lundbladecc7da412020-12-27 00:09:07 -08003615static const uint8_t spBigNumInput[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003616 0x83,
3617 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3618 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3619 0xA4,
3620 0x63, 0x42, 0x4E, 0x2B,
3621 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3622 0x18, 0x40,
3623 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3624 0x63, 0x42, 0x4E, 0x2D,
3625 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3626 0x38, 0x3F,
3627 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
3628
Laurence Lundblade37286c02022-09-03 10:05:02 -07003629#ifndef QCBOR_DISABLE_TAGS
Laurence Lundbladecc7da412020-12-27 00:09:07 -08003630/* The expected big num */
3631static const uint8_t spBigNum[] = {
3632 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3633 0x00};
Laurence Lundblade37286c02022-09-03 10:05:02 -07003634#endif /* QCBOR_DISABLE_TAGS */
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003635
3636
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03003637int32_t BignumParseTest(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003638{
3639 QCBORDecodeContext DCtx;
3640 QCBORItem Item;
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07003641 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003642
Laurence Lundbladeee851742020-01-08 08:37:05 -08003643 QCBORDecode_Init(&DCtx,
3644 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNumInput),
3645 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003646
3647
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003648 //
3649 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
3650 return -1;
3651 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003652 return -2;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003653 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003654
Laurence Lundblade37286c02022-09-03 10:05:02 -07003655#ifndef QCBOR_DISABLE_TAGS
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003656 //
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003657 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003658 return -3;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003659 if(Item.uDataType != QCBOR_TYPE_POSBIGNUM ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003660 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003661 return -4;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003662 }
3663
3664 //
3665 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003666 return -5;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003667 if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003668 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003669 return -6;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003670 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003671
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003672 //
3673 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003674 return -7;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003675 if(Item.uDataType != QCBOR_TYPE_MAP) {
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003676 return -8;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003677 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003678
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003679 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003680 return -9;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003681 if(Item.uDataType != QCBOR_TYPE_POSBIGNUM ||
3682 Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003683 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003684 return -10;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003685 }
3686
3687 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003688 return -11;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003689 if(Item.uDataType != QCBOR_TYPE_POSBIGNUM ||
3690 Item.uLabelType != QCBOR_TYPE_INT64 ||
3691 Item.label.int64 != 64 ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003692 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003693 return -12;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003694 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003695
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003696 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003697 return -13;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003698 if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM ||
3699 Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003700 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003701 return -14;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003702 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003703
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003704 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003705 return -15;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003706 if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM ||
3707 Item.uLabelType != QCBOR_TYPE_INT64 ||
3708 Item.label.int64 != -64 ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003709 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003710 return -16;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003711 }
Laurence Lundblade37286c02022-09-03 10:05:02 -07003712#else
3713
3714 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_TAGS_DISABLED) {
3715 return -100;
3716 }
3717#endif /* QCBOR_DISABLE_TAGS */
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003718
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003719 return 0;
3720}
3721
3722
3723
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003724static int32_t CheckItemWithIntLabel(QCBORDecodeContext *pCtx,
Laurence Lundbladeee851742020-01-08 08:37:05 -08003725 uint8_t uDataType,
3726 uint8_t uNestingLevel,
3727 uint8_t uNextNest,
3728 int64_t nLabel,
3729 QCBORItem *pItem)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003730{
3731 QCBORItem Item;
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07003732 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003733
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003734 if((nCBORError = QCBORDecode_GetNext(pCtx, &Item))) return -1;
3735 if(Item.uDataType != uDataType) return -1;
3736 if(uNestingLevel > 0) {
Laurence Lundbladeee851742020-01-08 08:37:05 -08003737 if(Item.uLabelType != QCBOR_TYPE_INT64 &&
3738 Item.uLabelType != QCBOR_TYPE_UINT64) {
3739 return -1;
3740 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003741 if(Item.uLabelType == QCBOR_TYPE_INT64) {
3742 if(Item.label.int64 != nLabel) return -1;
3743 } else {
Laurence Lundblade570fab52018-10-13 18:28:27 +08003744 if(Item.label.uint64 != (uint64_t)nLabel) return -1;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003745 }
3746 }
3747 if(Item.uNestingLevel != uNestingLevel) return -1;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303748 if(Item.uNextNestLevel != uNextNest) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003749
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003750 if(pItem) {
3751 *pItem = Item;
3752 }
3753 return 0;
3754}
3755
3756
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003757// Same code checks definite and indefinite length versions of the map
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003758static int32_t CheckCSRMaps(QCBORDecodeContext *pDC)
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003759{
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303760 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 0, 1, 0, NULL)) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003761
Laurence Lundblade9b334962020-08-27 10:55:53 -07003762 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 1, 2, -23, NULL)) return -2;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003763
Laurence Lundblade9b334962020-08-27 10:55:53 -07003764 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 2, 3, -20, NULL)) return -3;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003765
Laurence Lundblade9b334962020-08-27 10:55:53 -07003766 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -18, NULL)) return -4;
3767 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -17, NULL)) return -5;
3768 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -15, NULL)) return -6;
3769 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -16, NULL)) return -7;
3770 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 2, -14, NULL)) return -8;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003771
Laurence Lundblade9b334962020-08-27 10:55:53 -07003772 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 2, 3, -19, NULL)) return -9;
3773 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 3, 4, -11, NULL)) return -10;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003774
Laurence Lundblade9b334962020-08-27 10:55:53 -07003775 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_INT64, 4, 3, -9, NULL)) return -11;
3776 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_BYTE_STRING, 3, 1, -10, NULL)) return -12;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003777
Laurence Lundblade9b334962020-08-27 10:55:53 -07003778 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 1, 2, -22, NULL)) return -13;
3779 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_INT64, 2, 0, -5, NULL)) return -14;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003780
Laurence Lundblade9b334962020-08-27 10:55:53 -07003781 if(QCBORDecode_Finish(pDC)) return -20;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003782
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003783 return 0;
3784}
3785
3786
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003787/*
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003788{
3789 -23: {
3790 -20: {
3791 -18: "Organization",
3792 -17: "SSG",
3793 -15: "Confusion",
3794 -16: "San Diego",
3795 -14: "US"
3796 },
3797 -19: {
3798 -11: {
3799 -9: -7
3800 },
3801 -10: '\u0001\u0002\u0003\u0004\u0005\u0006\a\b\t\n'
3802 }
3803 },
3804 -22: {
3805 -5: -3
3806 }
3807}
Laurence Lundbladecc7da412020-12-27 00:09:07 -08003808*/
3809static const uint8_t spCSRInput[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003810 0xa2, 0x36, 0xa2, 0x33, 0xa5, 0x31, 0x6c, 0x4f,
3811 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74,
3812 0x69, 0x6f, 0x6e, 0x30, 0x63, 0x53, 0x53, 0x47,
3813 0x2e, 0x69, 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73,
3814 0x69, 0x6f, 0x6e, 0x2f, 0x69, 0x53, 0x61, 0x6e,
3815 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f, 0x2d, 0x62,
3816 0x55, 0x53, 0x32, 0xa2, 0x2a, 0xa1, 0x28, 0x26,
3817 0x29, 0x4a, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
3818 0x07, 0x08, 0x09, 0x0a, 0x35, 0xa1, 0x24, 0x22};
3819
Laurence Lundbladecc7da412020-12-27 00:09:07 -08003820// Same map as above, but using indefinite lengths
3821static const uint8_t spCSRInputIndefLen[] = {
3822 0xbf, 0x36, 0xbf, 0x33, 0xbf, 0x31, 0x6c, 0x4f,
3823 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74,
3824 0x69, 0x6f, 0x6e, 0x30, 0x63, 0x53, 0x53, 0x47,
3825 0x2e, 0x69, 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73,
3826 0x69, 0x6f, 0x6e, 0x2f, 0x69, 0x53, 0x61, 0x6e,
3827 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f, 0x2d, 0x62,
3828 0x55, 0x53, 0xff, 0x32, 0xbf, 0x2a, 0xbf, 0x28,
3829 0x26, 0xff, 0x29, 0x4a, 0x01, 0x02, 0x03, 0x04,
3830 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0xff, 0xff,
3831 0x35, 0xbf, 0x24, 0x22, 0xff, 0xff};
3832
3833
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03003834int32_t NestedMapTest(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003835{
3836 QCBORDecodeContext DCtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003837
Laurence Lundbladeee851742020-01-08 08:37:05 -08003838 QCBORDecode_Init(&DCtx,
3839 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput),
3840 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003841
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003842 return CheckCSRMaps(&DCtx);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003843}
3844
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08003845
3846
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03003847int32_t StringDecoderModeFailTest(void)
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08003848{
3849 QCBORDecodeContext DCtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003850
Laurence Lundbladeee851742020-01-08 08:37:05 -08003851 QCBORDecode_Init(&DCtx,
3852 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput),
3853 QCBOR_DECODE_MODE_MAP_STRINGS_ONLY);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003854
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08003855 QCBORItem Item;
3856 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003857
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08003858 if(QCBORDecode_GetNext(&DCtx, &Item)) {
3859 return -1;
3860 }
3861 if(Item.uDataType != QCBOR_TYPE_MAP) {
3862 return -2;
3863 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003864
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08003865 nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
3866 if(nCBORError != QCBOR_ERR_MAP_LABEL_TYPE) {
3867 return -3;
3868 }
3869
3870 return 0;
3871}
3872
3873
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003874
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03003875int32_t NestedMapTestIndefLen(void)
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003876{
3877 QCBORDecodeContext DCtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003878
Laurence Lundbladeee851742020-01-08 08:37:05 -08003879 QCBORDecode_Init(&DCtx,
3880 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInputIndefLen),
3881 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003882
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003883 return CheckCSRMaps(&DCtx);
3884}
3885
3886
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003887
Laurence Lundblade17ede402018-10-13 11:43:07 +08003888static UsefulBufC make_nested_indefinite_arrays(int n, UsefulBuf Storage)
3889{
3890 UsefulOutBuf UOB;
3891 UsefulOutBuf_Init(&UOB, Storage);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003892
Laurence Lundblade17ede402018-10-13 11:43:07 +08003893 int i;
3894 for(i = 0; i < n; i++) {
3895 UsefulOutBuf_AppendByte(&UOB, 0x9f);
3896 }
3897
3898 for(i = 0; i < n; i++) {
3899 UsefulOutBuf_AppendByte(&UOB, 0xff);
3900 }
3901 return UsefulOutBuf_OutUBuf(&UOB);
3902}
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003903
3904
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003905static int32_t parse_indeflen_nested(UsefulBufC Nested, int nNestLevel)
Laurence Lundblade17ede402018-10-13 11:43:07 +08003906{
3907 QCBORDecodeContext DC;
3908 QCBORDecode_Init(&DC, Nested, 0);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003909
Laurence Lundblade17ede402018-10-13 11:43:07 +08003910 int j;
3911 for(j = 0; j < nNestLevel; j++) {
3912 QCBORItem Item;
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07003913 QCBORError nReturn = QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade17ede402018-10-13 11:43:07 +08003914 if(j >= QCBOR_MAX_ARRAY_NESTING) {
3915 // Should be in error
Laurence Lundbladea9489f82020-09-12 13:50:56 -07003916 if(nReturn != QCBOR_ERR_ARRAY_DECODE_NESTING_TOO_DEEP) {
Laurence Lundblade17ede402018-10-13 11:43:07 +08003917 return -4;
3918 } else {
3919 return 0; // Decoding doesn't recover after an error
3920 }
3921 } else {
3922 // Should be no error
3923 if(nReturn) {
3924 return -9; // Should not have got an error
3925 }
3926 }
3927 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
3928 return -7;
3929 }
3930 }
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07003931 QCBORError nReturn = QCBORDecode_Finish(&DC);
Laurence Lundblade17ede402018-10-13 11:43:07 +08003932 if(nReturn) {
3933 return -3;
3934 }
3935 return 0;
3936}
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003937
3938
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03003939int32_t IndefiniteLengthNestTest(void)
Laurence Lundblade17ede402018-10-13 11:43:07 +08003940{
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05303941 UsefulBuf_MAKE_STACK_UB(Storage, 50);
Laurence Lundblade17ede402018-10-13 11:43:07 +08003942 int i;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003943 for(i=1; i < QCBOR_MAX_ARRAY_NESTING+4; i++) {
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08003944 const UsefulBufC Nested = make_nested_indefinite_arrays(i, Storage);
Laurence Lundblade17ede402018-10-13 11:43:07 +08003945 int nReturn = parse_indeflen_nested(Nested, i);
3946 if(nReturn) {
3947 return nReturn;
3948 }
3949 }
3950 return 0;
3951}
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003952
Laurence Lundbladeee851742020-01-08 08:37:05 -08003953// [1, [2, 3]]
3954static const uint8_t spIndefiniteArray[] = {0x9f, 0x01, 0x82, 0x02, 0x03, 0xff};
3955// No closing break
3956static const uint8_t spIndefiniteArrayBad1[] = {0x9f};
3957// Not enough closing breaks
3958static const uint8_t spIndefiniteArrayBad2[] = {0x9f, 0x9f, 0x02, 0xff};
3959// Too many closing breaks
3960static const uint8_t spIndefiniteArrayBad3[] = {0x9f, 0x02, 0xff, 0xff};
3961// Unclosed indeflen inside def len
3962static const uint8_t spIndefiniteArrayBad4[] = {0x81, 0x9f};
3963// confused tag
3964static const uint8_t spIndefiniteArrayBad5[] = {0x9f, 0xd1, 0xff};
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003965
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03003966int32_t IndefiniteLengthArrayMapTest(void)
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003967{
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07003968 QCBORError nResult;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003969 // --- first test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003970 UsefulBufC IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArray);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003971
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003972 // Decode it and see if it is OK
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003973 QCBORDecodeContext DC;
3974 QCBORItem Item;
3975 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003976
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003977 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05303978
3979 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
3980 Item.uNestingLevel != 0 ||
3981 Item.uNextNestLevel != 1) {
3982 return -111;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003983 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003984
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003985 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05303986 if(Item.uDataType != QCBOR_TYPE_INT64 ||
3987 Item.uNestingLevel != 1 ||
3988 Item.uNextNestLevel != 1) {
3989 return -2;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003990 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003991
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003992 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05303993 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
3994 Item.uNestingLevel != 1 ||
3995 Item.uNextNestLevel != 2) {
3996 return -3;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003997 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003998
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003999 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade12b495d2018-12-17 11:15:54 -08004000 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade6de37062018-10-15 12:22:42 +05304001 Item.uNestingLevel != 2 ||
4002 Item.uNextNestLevel != 2) {
4003 return -4;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08004004 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004005
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08004006 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade12b495d2018-12-17 11:15:54 -08004007 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade6de37062018-10-15 12:22:42 +05304008 Item.uNestingLevel != 2 ||
4009 Item.uNextNestLevel != 0) {
4010 return -5;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08004011 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004012
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08004013 if(QCBORDecode_Finish(&DC)) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05304014 return -6;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08004015 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004016
Laurence Lundblade19e0c802018-10-13 12:19:55 +08004017 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07004018 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad1);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004019
Laurence Lundblade19e0c802018-10-13 12:19:55 +08004020 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004021
Laurence Lundblade19e0c802018-10-13 12:19:55 +08004022 nResult = QCBORDecode_GetNext(&DC, &Item);
4023 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05304024 return -7;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08004025 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004026
Laurence Lundblade570fab52018-10-13 18:28:27 +08004027 nResult = QCBORDecode_Finish(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07004028 if(nResult != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05304029 return -8;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08004030 }
4031
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004032
Laurence Lundblade19e0c802018-10-13 12:19:55 +08004033 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07004034 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad2);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004035
Laurence Lundblade19e0c802018-10-13 12:19:55 +08004036 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004037
Laurence Lundblade19e0c802018-10-13 12:19:55 +08004038 nResult = QCBORDecode_GetNext(&DC, &Item);
4039 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05304040 return -9;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08004041 }
4042
4043 nResult = QCBORDecode_GetNext(&DC, &Item);
4044 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05304045 return -10;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08004046 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004047
Laurence Lundblade19e0c802018-10-13 12:19:55 +08004048 nResult = QCBORDecode_GetNext(&DC, &Item);
4049 if(nResult || Item.uDataType != QCBOR_TYPE_INT64) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05304050 return -11;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08004051 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004052
Laurence Lundblade19e0c802018-10-13 12:19:55 +08004053 nResult = QCBORDecode_Finish(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07004054 if(nResult != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05304055 return -12;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08004056 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004057
4058
Laurence Lundblade19e0c802018-10-13 12:19:55 +08004059 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07004060 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad3);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004061
Laurence Lundblade19e0c802018-10-13 12:19:55 +08004062 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004063
Laurence Lundblade19e0c802018-10-13 12:19:55 +08004064 nResult = QCBORDecode_GetNext(&DC, &Item);
4065 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05304066 return -13;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08004067 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004068
Laurence Lundblade19e0c802018-10-13 12:19:55 +08004069 nResult = QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade642282a2020-06-23 12:00:33 -07004070 if(nResult != QCBOR_SUCCESS) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05304071 return -14;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08004072 }
Laurence Lundblade6de37062018-10-15 12:22:42 +05304073
Laurence Lundblade642282a2020-06-23 12:00:33 -07004074 nResult = QCBORDecode_GetNext(&DC, &Item);
4075 if(nResult != QCBOR_ERR_BAD_BREAK) {
4076 return -140;
4077 }
4078
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004079
Laurence Lundblade570fab52018-10-13 18:28:27 +08004080 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07004081 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad4);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004082
Laurence Lundblade570fab52018-10-13 18:28:27 +08004083 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004084
Laurence Lundblade570fab52018-10-13 18:28:27 +08004085 nResult = QCBORDecode_GetNext(&DC, &Item);
4086 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05304087 return -15;
Laurence Lundblade570fab52018-10-13 18:28:27 +08004088 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004089
Laurence Lundblade570fab52018-10-13 18:28:27 +08004090 nResult = QCBORDecode_GetNext(&DC, &Item);
4091 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05304092 return -16;
Laurence Lundblade570fab52018-10-13 18:28:27 +08004093 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004094
Laurence Lundblade570fab52018-10-13 18:28:27 +08004095 nResult = QCBORDecode_Finish(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07004096 if(nResult != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05304097 return -17;
Laurence Lundblade570fab52018-10-13 18:28:27 +08004098 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004099
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304100 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07004101 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad5);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004102
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304103 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004104
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304105 nResult = QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade37286c02022-09-03 10:05:02 -07004106
4107#ifndef QCBOR_DISABLE_TAGS
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304108 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05304109 return -18;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304110 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004111
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304112 nResult = QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05304113 if(nResult != QCBOR_ERR_BAD_BREAK) {
4114 return -19;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304115 }
Laurence Lundblade37286c02022-09-03 10:05:02 -07004116#else /* QCBOR_DISABLE_TAGS */
4117 if(nResult != QCBOR_ERR_TAGS_DISABLED) {
4118 return -20;
4119 }
4120#endif /* QCBOR_DISABLE_TAGS */
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004121
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08004122 return 0;
4123}
4124
Laurence Lundblade17ede402018-10-13 11:43:07 +08004125
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08004126#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
4127
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07004128static const uint8_t spIndefiniteLenString[] = {
Laurence Lundblade17ede402018-10-13 11:43:07 +08004129 0x81, // Array of length one
4130 0x7f, // text string marked with indefinite length
4131 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment
4132 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment
4133 0xff // ending break
4134};
4135
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07004136static const uint8_t spIndefiniteLenStringBad2[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304137 0x81, // Array of length one
4138 0x7f, // text string marked with indefinite length
4139 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment
4140 0x44, 0x6d, 0x69, 0x6e, 0x67, // second segment of wrong type
4141 0xff // ending break
4142};
4143
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07004144static const uint8_t spIndefiniteLenStringBad3[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304145 0x81, // Array of length one
4146 0x7f, // text string marked with indefinite length
4147 0x01, 0x02, // Not a string
4148 0xff // ending break
4149};
4150
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07004151static const uint8_t spIndefiniteLenStringBad4[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304152 0x81, // Array of length one
4153 0x7f, // text string marked with indefinite length
4154 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment
4155 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment
4156 // missing end of string
4157};
4158
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07004159static const uint8_t spIndefiniteLenStringLabel[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304160 0xa1, // Array of length one
4161 0x7f, // text string marked with indefinite length
4162 0x65, 0x73, 0x74, 0x72, 0x75, 0x75, // first segment
4163 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment
4164 0xff, // ending break
4165 0x01 // integer being labeled.
4166};
4167
Laurence Lundbladec5fef682020-01-25 11:38:45 -08004168/**
4169 Make an indefinite length string
4170
4171 @param Storage Storage for string, must be 144 bytes in size
4172 @return The indefinite length string
4173
4174 This makes an array with one indefinite length string that has 7 chunks
4175 from size of 1 byte up to 64 bytes.
4176 */
4177static UsefulBufC MakeIndefiniteBigBstr(UsefulBuf Storage)
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304178{
4179 UsefulOutBuf UOB;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004180
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304181 UsefulOutBuf_Init(&UOB, Storage);
4182 UsefulOutBuf_AppendByte(&UOB, 0x81);
4183 UsefulOutBuf_AppendByte(&UOB, 0x5f);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004184
Laurence Lundbladec5fef682020-01-25 11:38:45 -08004185 uint8_t uStringByte = 0;
4186 // Use of type int is intentional
4187 for(int uChunkSize = 1; uChunkSize <= 128; uChunkSize *= 2) {
4188 // Not using preferred encoding here, but that is OK.
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304189 UsefulOutBuf_AppendByte(&UOB, 0x58);
Laurence Lundbladec5fef682020-01-25 11:38:45 -08004190 UsefulOutBuf_AppendByte(&UOB, (uint8_t)uChunkSize);
4191 for(int j = 0; j < uChunkSize; j++) {
4192 UsefulOutBuf_AppendByte(&UOB, uStringByte);
4193 uStringByte++;
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304194 }
4195 }
4196 UsefulOutBuf_AppendByte(&UOB, 0xff);
4197
4198 return UsefulOutBuf_OutUBuf(&UOB);
4199}
4200
4201static int CheckBigString(UsefulBufC BigString)
4202{
4203 if(BigString.len != 255) {
4204 return 1;
4205 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004206
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304207 for(uint8_t i = 0; i < 255; i++){
4208 if(((const uint8_t *)BigString.ptr)[i] != i) {
4209 return 1;
4210 }
4211 }
4212 return 0;
4213}
4214
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05304215
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03004216int32_t IndefiniteLengthStringTest(void)
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304217{
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304218 QCBORDecodeContext DC;
4219 QCBORItem Item;
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05304220 // big enough for MakeIndefiniteBigBstr() + MemPool overhead
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004221 UsefulBuf_MAKE_STACK_UB(MemPool, 350);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004222
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304223 // --- Simple normal indefinite length string ------
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07004224 UsefulBufC IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenString);
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304225 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004226
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05304227 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304228 return -1;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304229 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004230
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304231 if(QCBORDecode_GetNext(&DC, &Item)) {
4232 return -2;
4233 }
4234 if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.uDataAlloc) {
4235 return -3;
4236 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004237
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304238 if(QCBORDecode_GetNext(&DC, &Item)) {
4239 return -4;
4240 }
4241 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || !Item.uDataAlloc) {
4242 return -5;
4243 }
4244 if(QCBORDecode_Finish(&DC)) {
4245 return -6;
4246 }
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304247
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304248 // ----- types mismatch ---
Laurence Lundbladeee851742020-01-08 08:37:05 -08004249 QCBORDecode_Init(&DC,
4250 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad2),
4251 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004252
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304253 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
4254 return -7;
4255 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004256
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304257 if(QCBORDecode_GetNext(&DC, &Item)) {
4258 return -8;
4259 }
4260 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
4261 return -9;
4262 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004263
Laurence Lundblade30816f22018-11-10 13:40:22 +07004264 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_INDEFINITE_STRING_CHUNK) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304265 return -10;
4266 }
4267
4268 // ----- not a string ---
Laurence Lundbladeee851742020-01-08 08:37:05 -08004269 QCBORDecode_Init(&DC,
4270 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad3),
4271 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004272
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304273 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
4274 return -11;
4275 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004276
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304277 if(QCBORDecode_GetNext(&DC, &Item)) {
4278 return -12;
4279 }
4280 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
4281 return -13;
4282 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004283
Laurence Lundblade30816f22018-11-10 13:40:22 +07004284 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_INDEFINITE_STRING_CHUNK) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304285 return -14;
4286 }
4287
4288 // ----- no end -----
Laurence Lundbladeee851742020-01-08 08:37:05 -08004289 QCBORDecode_Init(&DC,
4290 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad4),
4291 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004292
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304293 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
4294 return -15;
4295 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004296
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304297 if(QCBORDecode_GetNext(&DC, &Item)) {
4298 return -16;
4299 }
4300 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
4301 return -17;
4302 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004303
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304304 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_HIT_END) {
4305 return -18;
4306 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004307
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304308 // ------ Don't set a string allocator and see an error -----
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304309 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004310
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304311 QCBORDecode_GetNext(&DC, &Item);
4312 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304313 return -19;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304314 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004315
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304316 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_NO_STRING_ALLOCATOR) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304317 return -20;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304318 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004319
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304320 // ----- Mempool is way too small -----
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004321 UsefulBuf_MAKE_STACK_UB(MemPoolTooSmall, QCBOR_DECODE_MIN_MEM_POOL_SIZE-1);
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304322
4323 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
4324 if(!QCBORDecode_SetMemPool(&DC, MemPoolTooSmall, false)) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304325 return -21;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304326 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004327
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304328 // ----- Mempool is way too small -----
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05304329 UsefulBuf_MAKE_STACK_UB(BigIndefBStrStorage, 290);
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08004330 const UsefulBufC BigIndefBStr = MakeIndefiniteBigBstr(BigIndefBStrStorage);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004331
Laurence Lundbladeee851742020-01-08 08:37:05 -08004332 // 80 is big enough for MemPool overhead, but not BigIndefBStr
4333 UsefulBuf_MAKE_STACK_UB(MemPoolSmall, 80);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004334
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304335 QCBORDecode_Init(&DC, BigIndefBStr, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304336 if(QCBORDecode_SetMemPool(&DC, MemPoolSmall, false)) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304337 return -22;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304338 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004339
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304340 QCBORDecode_GetNext(&DC, &Item);
4341 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304342 return -23;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304343 }
Laurence Lundblade30816f22018-11-10 13:40:22 +07004344 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_STRING_ALLOCATE) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304345 return -24;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304346 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004347
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304348 // ---- big bstr -----
4349 QCBORDecode_Init(&DC, BigIndefBStr, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004350
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304351 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
4352 return -25;
4353 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004354
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05304355 if(QCBORDecode_GetNext(&DC, &Item)) {
4356 return -26;
4357 }
4358 if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.uDataAlloc) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304359 return -26;
4360 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004361
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304362 if(QCBORDecode_GetNext(&DC, &Item)) {
4363 return -27;
4364 }
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05304365 if(Item.uDataType != QCBOR_TYPE_BYTE_STRING || !Item.uDataAlloc || Item.uNestingLevel != 1) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304366 return -28;
4367 }
4368 if(CheckBigString(Item.val.string)) {
4369 return -3;
4370 }
4371 if(QCBORDecode_Finish(&DC)) {
4372 return -29;
4373 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004374
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304375 // --- label is an indefinite length string ------
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07004376 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringLabel), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004377
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304378 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
4379 return -30;
4380 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004381
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304382 QCBORDecode_GetNext(&DC, &Item);
4383 if(Item.uDataType != QCBOR_TYPE_MAP) {
4384 return -31;
4385 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004386
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304387 if(QCBORDecode_GetNext(&DC, &Item)){
4388 return -32;
4389 }
Laurence Lundbladeee851742020-01-08 08:37:05 -08004390 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
4391 Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304392 Item.uDataAlloc || !Item.uLabelAlloc ||
4393 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("struuming"))) {
4394 return -33;
4395 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004396
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304397 if(QCBORDecode_Finish(&DC)) {
4398 return -34;
4399 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004400
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004401 return 0;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08004402}
4403
4404
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03004405int32_t AllocAllStringsTest(void)
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304406{
4407 QCBORDecodeContext DC;
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004408 QCBORError nCBORError;
4409
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004410
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304411 // First test, use the "CSRMap" as easy input and checking
Laurence Lundbladeee851742020-01-08 08:37:05 -08004412 QCBORDecode_Init(&DC,
4413 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput),
4414 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004415
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004416 UsefulBuf_MAKE_STACK_UB(Pool, sizeof(spCSRInput) + QCBOR_DECODE_MIN_MEM_POOL_SIZE);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004417
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004418 nCBORError = QCBORDecode_SetMemPool(&DC, Pool, 1); // Turn on copying.
4419 if(nCBORError) {
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304420 return -1;
4421 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004422
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004423 if(CheckCSRMaps(&DC)) {
4424 return -2;
4425 }
4426
Laurence Lundblade2f467f92020-10-09 17:50:11 -07004427 // Next parse, save pointers to a few strings, destroy original and
4428 // see all is OK.
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004429 UsefulBuf_MAKE_STACK_UB(CopyOfStorage, sizeof(pValidMapEncoded) + QCBOR_DECODE_MIN_MEM_POOL_SIZE);
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08004430 const UsefulBufC CopyOf = UsefulBuf_Copy(CopyOfStorage, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded));
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08004431
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304432 QCBORDecode_Init(&DC, CopyOf, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08004433 UsefulBuf_Set(Pool, '/');
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304434 QCBORDecode_SetMemPool(&DC, Pool, 1); // Turn on copying.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004435
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304436 QCBORItem Item1, Item2, Item3, Item4;
4437 if((nCBORError = QCBORDecode_GetNext(&DC, &Item1)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07004438 return (int32_t)nCBORError;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304439 if(Item1.uDataType != QCBOR_TYPE_MAP ||
4440 Item1.val.uCount != 3)
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004441 return -3;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304442 if((nCBORError = QCBORDecode_GetNext(&DC, &Item1)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07004443 return (int32_t)nCBORError;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304444 if((nCBORError = QCBORDecode_GetNext(&DC, &Item2)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07004445 return (int32_t)nCBORError;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304446 if((nCBORError = QCBORDecode_GetNext(&DC, &Item3)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07004447 return (int32_t)nCBORError;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304448 if((nCBORError = QCBORDecode_GetNext(&DC, &Item4)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07004449 return (int32_t)nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004450
Laurence Lundblade05ec57b2018-10-21 01:50:03 +05304451 UsefulBuf_Set(CopyOfStorage, '_');
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004452
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304453 if(Item1.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304454 Item1.uDataType != QCBOR_TYPE_INT64 ||
4455 Item1.val.int64 != 42 ||
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004456 Item1.uDataAlloc != 0 ||
4457 Item1.uLabelAlloc == 0 ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07004458 UsefulBufCompareToSZ(Item1.label.string, "first integer")) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004459 return -4;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09004460 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004461
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304462
4463 if(Item2.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07004464 UsefulBufCompareToSZ(Item2.label.string, "an array of two strings") ||
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304465 Item2.uDataType != QCBOR_TYPE_ARRAY ||
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004466 Item2.uDataAlloc != 0 ||
4467 Item2.uLabelAlloc == 0 ||
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304468 Item2.val.uCount != 2)
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004469 return -5;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004470
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304471 if(Item3.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004472 Item3.uDataAlloc == 0 ||
4473 Item3.uLabelAlloc != 0 ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07004474 UsefulBufCompareToSZ(Item3.val.string, "string1")) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004475 return -6;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09004476 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004477
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304478 if(Item4.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004479 Item4.uDataAlloc == 0 ||
4480 Item4.uLabelAlloc != 0 ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07004481 UsefulBufCompareToSZ(Item4.val.string, "string2")) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004482 return -7;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09004483 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004484
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304485 // Next parse with a pool that is too small
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004486 UsefulBuf_MAKE_STACK_UB(SmallPool, QCBOR_DECODE_MIN_MEM_POOL_SIZE + 1);
Laurence Lundbladeee851742020-01-08 08:37:05 -08004487 QCBORDecode_Init(&DC,
4488 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded),
4489 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304490 QCBORDecode_SetMemPool(&DC, SmallPool, 1); // Turn on copying.
4491 if((nCBORError = QCBORDecode_GetNext(&DC, &Item1)))
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004492 return -8;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304493 if(Item1.uDataType != QCBOR_TYPE_MAP ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09004494 Item1.val.uCount != 3) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004495 return -9;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09004496 }
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304497 if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item1))){
4498 if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item2))) {
4499 if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item3))) {
4500 nCBORError = QCBORDecode_GetNext(&DC, &Item4);
4501 }
4502 }
4503 }
Laurence Lundblade30816f22018-11-10 13:40:22 +07004504 if(nCBORError != QCBOR_ERR_STRING_ALLOCATE) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004505 return -10;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304506 }
4507
4508 return 0;
4509}
4510
Laurence Lundbladef6531662018-12-04 10:42:22 +09004511
Laurence Lundbladec5fef682020-01-25 11:38:45 -08004512int32_t MemPoolTest(void)
Laurence Lundblade0155b622018-10-12 20:04:37 +08004513{
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004514 // Set up the decoder with a tiny bit of CBOR to parse because
4515 // nothing can be done with it unless that is set up.
Laurence Lundbladef6531662018-12-04 10:42:22 +09004516 QCBORDecodeContext DC;
4517 const uint8_t pMinimalCBOR[] = {0xa0}; // One empty map
4518 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pMinimalCBOR),0);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004519
Laurence Lundbladef6531662018-12-04 10:42:22 +09004520 // Set up an memory pool of 100 bytes
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004521 // Then fish into the internals of the decode context
4522 // to get the allocator function so it can be called directly.
4523 // Also figure out how much pool is available for use
4524 // buy subtracting out the overhead.
Laurence Lundbladef6531662018-12-04 10:42:22 +09004525 UsefulBuf_MAKE_STACK_UB(Pool, 100);
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004526 QCBORError nError = QCBORDecode_SetMemPool(&DC, Pool, 0);
4527 if(nError) {
4528 return -9;
4529 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004530 QCBORStringAllocate pAlloc = DC.StringAllocator.pfAllocator;
4531 void *pAllocCtx = DC.StringAllocator.pAllocateCxt;
4532 size_t uAvailPool = Pool.len - QCBOR_DECODE_MIN_MEM_POOL_SIZE;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004533
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004534 // First test -- ask for one more byte than available and see failure
4535 UsefulBuf Allocated = (*pAlloc)(pAllocCtx, NULL, uAvailPool+1);
Laurence Lundbladef6531662018-12-04 10:42:22 +09004536 if(!UsefulBuf_IsNULL(Allocated)) {
4537 return -1;
4538 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004539
Laurence Lundbladef6531662018-12-04 10:42:22 +09004540 // Re do the set up for the next test that will do a successful alloc,
4541 // a fail, a free and then success
Laurence Lundbladef6531662018-12-04 10:42:22 +09004542 QCBORDecode_SetMemPool(&DC, Pool, 0);
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004543 pAlloc = DC.StringAllocator.pfAllocator;
4544 pAllocCtx = DC.StringAllocator.pAllocateCxt;
4545 uAvailPool = Pool.len - QCBOR_DECODE_MIN_MEM_POOL_SIZE;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004546
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004547 // Allocate one byte less than available and see success
4548 Allocated = (pAlloc)(pAllocCtx, NULL, uAvailPool-1);
Laurence Lundbladef6531662018-12-04 10:42:22 +09004549 if(UsefulBuf_IsNULL(Allocated)) { // expected to succeed
4550 return -2;
4551 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004552 // Ask for some more and see failure
4553 UsefulBuf Allocated2 = (*pAlloc)(pAllocCtx, NULL, uAvailPool/2);
Laurence Lundbladef6531662018-12-04 10:42:22 +09004554 if(!UsefulBuf_IsNULL(Allocated2)) { // expected to fail
4555 return -3;
4556 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004557 // Free the first allocate, retry the second and see success
4558 (*pAlloc)(pAllocCtx, Allocated.ptr, 0); // Free
4559 Allocated = (*pAlloc)(pAllocCtx, NULL, uAvailPool/2);
Laurence Lundbladef6531662018-12-04 10:42:22 +09004560 if(UsefulBuf_IsNULL(Allocated)) { // succeed because of the free
4561 return -4;
4562 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004563
Laurence Lundbladef6531662018-12-04 10:42:22 +09004564 // Re do set up for next test that involves a successful alloc,
4565 // and a successful realloc and a failed realloc
4566 QCBORDecode_SetMemPool(&DC, Pool, 0);
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004567 pAlloc = DC.StringAllocator.pfAllocator;
4568 pAllocCtx = DC.StringAllocator.pAllocateCxt;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004569
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004570 // Allocate half the pool and see success
4571 Allocated = (*pAlloc)(pAllocCtx, NULL, uAvailPool/2);
Laurence Lundbladef6531662018-12-04 10:42:22 +09004572 if(UsefulBuf_IsNULL(Allocated)) { // expected to succeed
4573 return -5;
4574 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004575 // Reallocate to take up the whole pool and see success
4576 Allocated2 = (*pAlloc)(pAllocCtx, Allocated.ptr, uAvailPool);
Laurence Lundbladef6531662018-12-04 10:42:22 +09004577 if(UsefulBuf_IsNULL(Allocated2)) {
4578 return -6;
4579 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004580 // Make sure its the same pointer and the size is right
Laurence Lundbladef6531662018-12-04 10:42:22 +09004581 if(Allocated2.ptr != Allocated.ptr || Allocated2.len != uAvailPool) {
4582 return -7;
4583 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004584 // Try to allocate more to be sure there is failure after a realloc
4585 UsefulBuf Allocated3 = (*pAlloc)(pAllocCtx, Allocated.ptr, uAvailPool+1);
4586 if(!UsefulBuf_IsNULL(Allocated3)) {
Laurence Lundbladef6531662018-12-04 10:42:22 +09004587 return -8;
4588 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004589
Laurence Lundbladef6531662018-12-04 10:42:22 +09004590 return 0;
4591}
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004592
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004593
4594/* Just enough of an allocator to test configuration of one */
4595static UsefulBuf AllocateTestFunction(void *pCtx, void *pOldMem, size_t uNewSize)
4596{
4597 (void)pOldMem; // unused variable
4598
4599 if(uNewSize) {
4600 // Assumes the context pointer is the buffer and
4601 // nothing too big will ever be asked for.
4602 // This is only good for this basic test!
4603 return (UsefulBuf) {pCtx, uNewSize};
4604 } else {
4605 return NULLUsefulBuf;
4606 }
4607}
4608
4609
Laurence Lundbladec5fef682020-01-25 11:38:45 -08004610int32_t SetUpAllocatorTest(void)
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004611{
4612 // Set up the decoder with a tiny bit of CBOR to parse because
4613 // nothing can be done with it unless that is set up.
4614 QCBORDecodeContext DC;
4615 const uint8_t pMinimalCBOR[] = {0x62, 0x48, 0x69}; // "Hi"
4616 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pMinimalCBOR),0);
4617
4618 uint8_t pAllocatorBuffer[50];
4619
4620 // This is really just to test that this call works.
4621 // The full functionality of string allocators is tested
4622 // elsewhere with the MemPool internal allocator.
4623 QCBORDecode_SetUpAllocator(&DC, AllocateTestFunction, pAllocatorBuffer, 1);
4624
4625 QCBORItem Item;
4626 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_SUCCESS) {
4627 return -1;
4628 }
4629
4630 if(Item.uDataAlloc == 0 ||
4631 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
4632 Item.val.string.ptr != pAllocatorBuffer) {
4633 return -2;
4634 }
4635
4636 if(QCBORDecode_Finish(&DC) != QCBOR_SUCCESS) {
4637 return -3;
4638 }
4639
4640 return 0;
4641}
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08004642#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
4643
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004644
Laurence Lundbladedd6e76e2021-03-10 01:54:01 -07004645#ifndef QCBOR_DISABLE_EXP_AND_MANTISSA
Laurence Lundblade59289e52019-12-30 13:44:37 -08004646
Laurence Lundblade37286c02022-09-03 10:05:02 -07004647struct EaMTest {
4648 const char *szName;
4649 UsefulBufC Input;
4650 uint8_t uTagRequirement;
4651 bool bHasTags;
4652
4653 /* Expected values for GetNext */
4654 QCBORError uExpectedErrorGN;
4655 uint8_t uQCBORTypeGN;
4656 int64_t nExponentGN;
4657 int64_t nMantissaGN;
4658 UsefulBufC MantissaGN;
4659
4660 /* Expected values for GetDecimalFraction */
4661 QCBORError uExpectedErrorGDF;
4662 int64_t nExponentGDF;
4663 int64_t nMantissaGDF;
4664
4665 /* Expected values for GetDecimalFractionBig */
4666 QCBORError uExpectedErrorGDFB;
4667 int64_t nExponentGDFB;
4668 UsefulBufC MantissaGDFB;
4669 bool IsNegativeGDFB;
4670
4671 /* Expected values for GetBigFloat */
4672 QCBORError uExpectedErrorGBF;
4673 int64_t nExponentGBF;
4674 int64_t nMantissaGBF;
4675
4676 /* Expected values for GetBigFloatBig */
4677 QCBORError uExpectedErrorGBFB;
4678 int64_t nExponentGBFB;
4679 UsefulBufC MantissaGBFB;
4680 bool IsNegativeGBFB;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004681};
4682
Laurence Lundbladefaec39f2020-08-02 21:53:53 -07004683
Laurence Lundblade37286c02022-09-03 10:05:02 -07004684
4685static const struct EaMTest pEaMTests[] = {
4686 {
4687 "1. Untagged pair (big float or decimal fraction), no tag required",
4688 {(const uint8_t []){0x82, 0x20, 0x03}, 3},
4689 QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
4690 false,
4691
4692 QCBOR_SUCCESS, /* for GetNext */
4693 QCBOR_TYPE_ARRAY,
4694 0,
4695 0,
4696 {(const uint8_t []){0x00}, 1},
4697
4698 QCBOR_SUCCESS, /* GetDecimalFraction */
4699 -1,
4700 3,
4701
4702 QCBOR_SUCCESS, /* for GetDecimalFractionBig */
4703 -1,
4704 {(const uint8_t []){0x03}, 1},
4705 false,
4706
4707 QCBOR_SUCCESS, /* for GetBigFloat */
4708 -1,
4709 3,
4710
4711 QCBOR_SUCCESS, /* for GetBigFloatBig */
4712 -1,
4713 {(const uint8_t []){0x03}, 1},
4714 false
4715 },
4716
4717 {
4718 "2. Untagged pair (big float or decimal fraction), tag required",
4719 {(const uint8_t []){0x82, 0x20, 0x03}, 3},
4720 QCBOR_TAG_REQUIREMENT_TAG,
4721 false,
4722
4723 QCBOR_SUCCESS, /* for GetNext */
4724 QCBOR_TYPE_ARRAY,
4725 0,
4726 0,
4727 {(const uint8_t []){0x00}, 1},
4728
4729 QCBOR_ERR_UNEXPECTED_TYPE, /* for GetDecimalFraction */
4730 0,
4731 0,
4732
4733 QCBOR_ERR_UNEXPECTED_TYPE, /* for GetDecimalFractionBig */
4734 0,
4735 {(const uint8_t []){0x00}, 1},
4736 false,
4737
4738 QCBOR_ERR_UNEXPECTED_TYPE, /* for GetBigFloat */
4739 0,
4740 0,
4741
4742 QCBOR_ERR_UNEXPECTED_TYPE, /* for GetBigFloatBig */
4743 0,
4744 {(const uint8_t []){0x00}, 1},
4745 false
4746
4747 },
4748
4749 {
4750 "3. Tagged 1.5 decimal fraction, tag 4 optional",
4751 {(const uint8_t []){0xC4, 0x82, 0x20, 0x03}, 4},
4752 QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
4753 true,
4754
4755 QCBOR_SUCCESS, /* for GetNext */
4756 QCBOR_TYPE_DECIMAL_FRACTION,
4757 -1,
4758 3,
4759 {(const uint8_t []){0x00}, 1},
4760
4761
4762 QCBOR_SUCCESS, /* for GetDecimalFraction */
4763 -1,
4764 3,
4765
4766 QCBOR_SUCCESS, /* for GetDecimalFractionBig */
4767 -1,
4768 {(const uint8_t []){0x03}, 1},
4769 false,
4770
4771 QCBOR_ERR_UNEXPECTED_TYPE, /* for GetBigFloat */
4772 0,
4773 0,
4774
4775 QCBOR_ERR_UNEXPECTED_TYPE, /* for GetBigFloatBig */
4776 0,
4777 {(const uint8_t []){0x00}, 1},
4778 false
4779 },
4780 {
4781 "4. Tagged 100 * 2^300 big float, tag 5 optional",
4782 {(const uint8_t []){0xC5, 0x82, 0x19, 0x01, 0x2C, 0x18, 0x64}, 7},
4783 QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
4784 true,
4785
4786 QCBOR_SUCCESS, /* for GetNext */
4787 QCBOR_TYPE_BIGFLOAT,
4788 300,
4789 100,
4790 {(const uint8_t []){0x00}, 1},
4791
4792
4793 QCBOR_ERR_UNEXPECTED_TYPE, /* for GetDecimalFraction */
4794 0,
4795 0,
4796
4797 QCBOR_ERR_UNEXPECTED_TYPE, /* for GetDecimalFractionBig */
4798 0,
4799 {(const uint8_t []){0x03}, 1},
4800 false,
4801
4802 QCBOR_SUCCESS, /* for GetBigFloat */
4803 300,
4804 100,
4805
4806 QCBOR_SUCCESS, /* for GetBigFloatBig */
4807 300,
4808 {(const uint8_t []){0x64}, 1},
4809 false
4810 },
4811
4812 {
4813 "5. Tagged 4([-20, 4759477275222530853136]) decimal fraction, tag 4 required",
4814 {(const uint8_t []){0xC4, 0x82, 0x33,
4815 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,}, 15},
4816 QCBOR_TAG_REQUIREMENT_TAG,
4817 true,
4818
4819 QCBOR_SUCCESS, /* for GetNext */
4820 QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM,
4821 -20,
4822 0,
4823 {(const uint8_t []){0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10}, 10},
4824
4825 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW, /* for GetDecimalFraction */
4826 0,
4827 0,
4828
4829 QCBOR_SUCCESS, /* for GetDecimalFractionBig */
4830 -20,
4831 {(const uint8_t []){0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10}, 10},
4832 false,
4833
4834 QCBOR_ERR_UNEXPECTED_TYPE, /* for GetBigFloat */
4835 0,
4836 0,
4837
4838 QCBOR_ERR_UNEXPECTED_TYPE, /* for GetBigFloatBig */
4839 0,
4840 {(const uint8_t []){0x00}, 0},
4841 false
4842 },
4843
4844 {
4845 "6. Error: Mantissa and exponent inside a Mantissa and exponent",
4846 {(const uint8_t []){0xC4, 0x82, 0x33,
4847 0xC5, 0x82, 0x19, 0x01, 0x2C, 0x18, 0x64}, 10},
4848 QCBOR_TAG_REQUIREMENT_TAG,
4849 true,
4850
4851 QCBOR_ERR_BAD_EXP_AND_MANTISSA, /* for GetNext */
4852 QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM,
4853 0,
4854 0,
4855 {(const uint8_t []){0x00}, 0},
4856
4857 QCBOR_ERR_BAD_EXP_AND_MANTISSA, /* for GetDecimalFraction */
4858 0,
4859 0,
4860
4861 QCBOR_ERR_BAD_EXP_AND_MANTISSA, /* for GetDecimalFractionBig */
4862 0,
4863 {(const uint8_t []){0x00}, 0},
4864 false,
4865
4866 QCBOR_ERR_BAD_EXP_AND_MANTISSA, /* for GetBigFloat */
4867 0,
4868 0,
4869
4870 QCBOR_ERR_BAD_EXP_AND_MANTISSA, /* for GetBigFloatBig */
4871 0,
4872 {(const uint8_t []){0x00}, 0},
4873 false
4874 },
4875 {
4876 "7. Tagged 5([-20, 4294967295]) big float, big num mantissa, tag 5 required",
4877 {(const uint8_t []){0xC5, 0x82, 0x33,
4878 0xC2, 0x44, 0xff, 0xff, 0xff, 0xff}, 9},
4879 QCBOR_TAG_REQUIREMENT_TAG,
4880 true,
4881
4882 QCBOR_SUCCESS, /* for GetNext */
4883 QCBOR_TYPE_BIGFLOAT_POS_BIGNUM,
4884 -20,
4885 0,
4886 {(const uint8_t []){0xff, 0xff, 0xff, 0xff}, 4},
4887
4888 QCBOR_ERR_UNEXPECTED_TYPE, /* for GetDecimalFraction */
4889 0,
4890 0,
4891
4892 QCBOR_ERR_UNEXPECTED_TYPE, /* for GetDecimalFractionBig */
4893 -20,
4894 {(const uint8_t []){0x00}, 1},
4895 false,
4896
4897 QCBOR_SUCCESS, /* for GetBigFloat */
4898 -20,
4899 4294967295,
4900
4901 QCBOR_SUCCESS, /* for GetBigFloatBig */
4902 -20,
4903 {(const uint8_t []){0xff, 0xff, 0xff, 0xff}, 4},
4904 false
4905 },
4906
4907 {
4908 /* Special case for test 8. Don't renumber it. */
4909 "8. Untagged pair with big num (big float or decimal fraction), tag optional",
4910 {(const uint8_t []){0x82, 0x33, 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10}, 14},
4911 QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
4912 true,
4913
4914 QCBOR_SUCCESS, /* for GetNext */
4915 QCBOR_TYPE_ARRAY,
4916 0,
4917 0,
4918 {(const uint8_t []){0x00}, 1},
4919
4920 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW, /* GetDecimalFraction */
4921 0,
4922 0,
4923
4924 QCBOR_SUCCESS, /* for GetDecimalFractionBig */
4925 -20,
4926 {(const uint8_t []){0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10}, 10},
4927 false,
4928
4929 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW, /* for GetBigFloat */
4930 0,
4931 0,
4932
4933 QCBOR_SUCCESS, /* for GetBigFloatBig */
4934 -20,
4935 {(const uint8_t []){0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10}, 10},
4936 false
4937 },
4938
4939 {
4940 "9. decimal fraction with large exponent and negative big num mantissa",
4941 {(const uint8_t []){0xC4, 0x82, 0x1B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
4942 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10}, 23},
4943 QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
4944 true,
4945
4946 QCBOR_SUCCESS, /* for GetNext */
4947 QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM,
4948 9223372036854775807,
4949 0,
4950 {(const uint8_t []){0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10}, 10},
4951
4952 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW, /* GetDecimalFraction */
4953 0,
4954 0,
4955
4956 QCBOR_SUCCESS, /* for GetDecimalFractionBig */
4957 9223372036854775807,
4958 {(const uint8_t []){0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10}, 10},
4959 true,
4960
4961 QCBOR_ERR_UNEXPECTED_TYPE, /* for GetBigFloat */
4962 0,
4963 0,
4964
4965 QCBOR_ERR_UNEXPECTED_TYPE, /* for GetBigFloatBig */
4966 0,
4967 {(const uint8_t []){0x00}, 1},
4968 false
4969 },
4970};
4971
4972
4973
4974int32_t ProcessEaMTests(void)
Laurence Lundblade59289e52019-12-30 13:44:37 -08004975{
Laurence Lundblade37286c02022-09-03 10:05:02 -07004976 size_t uIndex;
4977 QCBORDecodeContext DCtx;
4978 QCBORItem Item;
4979 QCBORError uError;
4980 int64_t nMantissa, nExponent;
4981 MakeUsefulBufOnStack( MantissaBuf, 200);
4982 UsefulBufC Mantissa;
4983 bool bMantissaIsNegative;
4984
4985 for(uIndex = 0; uIndex < C_ARRAY_COUNT(pEaMTests, struct EaMTest); uIndex++) {
4986 const struct EaMTest *pT = &pEaMTests[uIndex];
4987 /* Decode with GetNext */
4988 QCBORDecode_Init(&DCtx, pT->Input, 0);
4989
4990 if(uIndex + 1 == 9) {
4991 nExponent = 99; // just to set a break point
4992 }
4993
4994 uError = QCBORDecode_GetNext(&DCtx, &Item);
4995#ifdef QCBOR_DISABLE_TAGS
4996 /* Test 8 is a special case when tags are disabled */
4997 if(pT->bHasTags && uIndex + 1 != 8) {
4998 if(uError != QCBOR_ERR_TAGS_DISABLED) {
4999 return (int32_t)(1+uIndex) * 1000 + 9;
5000 }
5001 } else {
5002#endif
5003 /* Now check return code, data type, mantissa and exponent */
5004 if(pT->uExpectedErrorGN != uError) {
5005 return (int32_t)(1+uIndex) * 1000 + 1;
5006 }
5007 if(uError == QCBOR_SUCCESS && pT->uQCBORTypeGN != QCBOR_TYPE_ARRAY) {
5008 if(pT->uQCBORTypeGN != Item.uDataType) {
5009 return (int32_t)(1+uIndex) * 1000 + 2;
5010 }
5011 if(pT->nExponentGN != Item.val.expAndMantissa.nExponent) {
5012 return (int32_t)(1+uIndex) * 1000 + 3;
5013 }
5014 if(Item.uDataType == QCBOR_TYPE_DECIMAL_FRACTION || Item.uDataType == QCBOR_TYPE_BIGFLOAT ) {
5015 if(pT->nMantissaGN != Item.val.expAndMantissa.Mantissa.nInt) {
5016 return (int32_t)(1+uIndex) * 1000 + 4;
5017 }
5018 } else {
5019 if(UsefulBuf_Compare(Item.val.expAndMantissa.Mantissa.bigNum, pT->MantissaGN)) {
5020 return (int32_t)(1+uIndex) * 1000 + 5;
5021 }
5022 }
5023 }
5024#ifdef QCBOR_DISABLE_TAGS
5025 }
5026#endif
5027
5028 /* Decode with GetDecimalFraction */
5029 QCBORDecode_Init(&DCtx, pT->Input, 0);
5030 QCBORDecode_GetDecimalFraction(&DCtx,
5031 pT->uTagRequirement,
5032 &nMantissa,
5033 &nExponent);
5034 uError = QCBORDecode_GetAndResetError(&DCtx);
5035#ifdef QCBOR_DISABLE_TAGS
5036 if(pT->bHasTags) {
5037 if(uError != QCBOR_ERR_TAGS_DISABLED) {
5038 return (int32_t)(1+uIndex) * 1000 + 39;
5039 }
5040 } else {
5041#endif
5042 /* Now check return code, mantissa and exponent */
5043 if(pT->uExpectedErrorGDF != uError) {
5044 return (int32_t)(1+uIndex) * 1000 + 31;
5045 }
5046 if(uError == QCBOR_SUCCESS) {
5047 if(pT->nExponentGDF != nExponent) {
5048 return (int32_t)(1+uIndex) * 1000 + 32;
5049 }
5050 if(pT->nMantissaGDF != nMantissa) {
5051 return (int32_t)(1+uIndex) * 1000 + 33;
5052 }
5053 }
5054#ifdef QCBOR_DISABLE_TAGS
5055 }
5056#endif
5057
5058 /* Decode with GetDecimalFractionBig */
5059 QCBORDecode_Init(&DCtx, pT->Input, 0);
5060 QCBORDecode_GetDecimalFractionBig(&DCtx,
5061 pT->uTagRequirement,
5062 MantissaBuf,
5063 &Mantissa,
5064 &bMantissaIsNegative,
5065 &nExponent);
5066 uError = QCBORDecode_GetAndResetError(&DCtx);
5067#ifdef QCBOR_DISABLE_TAGS
5068 if(pT->bHasTags) {
5069 if(uError != QCBOR_ERR_TAGS_DISABLED) {
5070 return (int32_t)(1+uIndex) * 1000 + 49;
5071 }
5072 } else {
5073#endif
5074 /* Now check return code, mantissa (bytes and sign) and exponent */
5075 if(pT->uExpectedErrorGDFB != uError) {
5076 return (int32_t)(1+uIndex) * 1000 + 41;
5077 }
5078 if(uError == QCBOR_SUCCESS) {
5079 if(pT->nExponentGDFB != nExponent) {
5080 return (int32_t)(1+uIndex) * 1000 + 42;
5081 }
5082 if(pT->IsNegativeGDFB != bMantissaIsNegative) {
5083 return (int32_t)(1+uIndex) * 1000 + 43;
5084 }
5085 if(UsefulBuf_Compare(Mantissa, pT->MantissaGDFB)) {
5086 return (int32_t)(1+uIndex) * 1000 + 44;
5087 }
5088 }
5089#ifdef QCBOR_DISABLE_TAGS
5090 }
5091#endif
5092
5093 /* Decode with GetBigFloat */
5094 QCBORDecode_Init(&DCtx, pT->Input, 0);
5095 QCBORDecode_GetBigFloat(&DCtx,
5096 pT->uTagRequirement,
5097 &nMantissa,
5098 &nExponent);
5099 uError = QCBORDecode_GetAndResetError(&DCtx);
5100#ifdef QCBOR_DISABLE_TAGS
5101 if(pT->bHasTags) {
5102 if(uError != QCBOR_ERR_TAGS_DISABLED) {
5103 return (int32_t)(1+uIndex) * 1000 + 19;
5104 }
5105 } else {
5106#endif
5107 /* Now check return code, mantissa and exponent */
5108 if(pT->uExpectedErrorGBF != uError) {
5109 return (int32_t)(1+uIndex) * 1000 + 11;
5110 }
5111 if(uError == QCBOR_SUCCESS) {
5112 if(pT->nExponentGBF != nExponent) {
5113 return (int32_t)(1+uIndex) * 1000 + 12;
5114 }
5115 if(pT->nMantissaGBF != nMantissa) {
5116 return (int32_t)(1+uIndex) * 1000 + 13;
5117 }
5118 }
5119#ifdef QCBOR_DISABLE_TAGS
5120 }
5121#endif
5122
5123 /* Decode with GetBigFloatBig */
5124 QCBORDecode_Init(&DCtx, pT->Input, 0);
5125 QCBORDecode_GetBigFloatBig(&DCtx,
5126 pT->uTagRequirement,
5127 MantissaBuf,
5128 &Mantissa,
5129 &bMantissaIsNegative,
5130 &nExponent);
5131 uError = QCBORDecode_GetAndResetError(&DCtx);
5132#ifdef QCBOR_DISABLE_TAGS
5133 if(pT->bHasTags) {
5134 if(uError != QCBOR_ERR_TAGS_DISABLED) {
5135 return (int32_t)(1+uIndex) * 1000 + 29;
5136 }
5137 } else {
5138#endif
5139 /* Now check return code, mantissa (bytes and sign) and exponent */
5140 if(pT->uExpectedErrorGBFB != uError) {
5141 return (int32_t)(1+uIndex) * 1000 + 21;
5142 }
5143 if(uError == QCBOR_SUCCESS) {
5144 if(pT->nExponentGBFB != nExponent) {
5145 return (int32_t)(1+uIndex) * 1000 + 22;
5146 }
5147 if(pT->IsNegativeGBFB != bMantissaIsNegative) {
5148 return (int32_t)(1+uIndex) * 1000 + 23;
5149 }
5150 if(UsefulBuf_Compare(Mantissa, pT->MantissaGBFB)) {
5151 return (int32_t)(1+uIndex) * 1000 + 24;
5152 }
5153 }
5154#ifdef QCBOR_DISABLE_TAGS
5155 }
5156#endif
5157 }
5158
5159 return 0;
5160}
5161
5162
5163int32_t ExponentAndMantissaDecodeTestsSecondary(void)
5164{
5165#ifndef QCBOR_DISABLE_TAGS
Laurence Lundblade59289e52019-12-30 13:44:37 -08005166 QCBORDecodeContext DC;
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07005167 QCBORError uErr;
Laurence Lundblade59289e52019-12-30 13:44:37 -08005168 QCBORItem item;
5169
Laurence Lundblade17af4902020-01-07 19:11:55 -08005170 static const uint8_t spBigNumMantissa[] = {0x01, 0x02, 0x03, 0x04, 0x05,
5171 0x06, 0x07, 0x08, 0x09, 0x010};
5172 UsefulBufC BN = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNumMantissa);
Laurence Lundblade59289e52019-12-30 13:44:37 -08005173
5174
Laurence Lundblade59289e52019-12-30 13:44:37 -08005175
5176 /* Now encode some stuff and then decode it */
5177 uint8_t pBuf[40];
5178 QCBOREncodeContext EC;
5179 UsefulBufC Encoded;
5180
5181 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(pBuf));
5182 QCBOREncode_OpenArray(&EC);
5183 QCBOREncode_AddDecimalFraction(&EC, 999, 1000); // 999 * (10 ^ 1000)
5184 QCBOREncode_AddBigFloat(&EC, 100, INT32_MIN);
5185 QCBOREncode_AddDecimalFractionBigNum(&EC, BN, false, INT32_MAX);
5186 QCBOREncode_CloseArray(&EC);
5187 QCBOREncode_Finish(&EC, &Encoded);
5188
5189
5190 QCBORDecode_Init(&DC, Encoded, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07005191 uErr = QCBORDecode_GetNext(&DC, &item);
5192 if(uErr != QCBOR_SUCCESS) {
5193 return 100;
Laurence Lundblade59289e52019-12-30 13:44:37 -08005194 }
5195
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07005196 uErr = QCBORDecode_GetNext(&DC, &item);
5197 if(uErr != QCBOR_SUCCESS) {
5198 return 101;
Laurence Lundblade59289e52019-12-30 13:44:37 -08005199 }
5200
5201 if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION ||
5202 item.val.expAndMantissa.nExponent != 1000 ||
5203 item.val.expAndMantissa.Mantissa.nInt != 999) {
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07005204 return 102;
Laurence Lundblade59289e52019-12-30 13:44:37 -08005205 }
5206
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07005207 uErr = QCBORDecode_GetNext(&DC, &item);
5208 if(uErr != QCBOR_SUCCESS) {
5209 return 103;
Laurence Lundblade59289e52019-12-30 13:44:37 -08005210 }
5211
5212 if(item.uDataType != QCBOR_TYPE_BIGFLOAT ||
5213 item.val.expAndMantissa.nExponent != INT32_MIN ||
5214 item.val.expAndMantissa.Mantissa.nInt != 100) {
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07005215 return 104;
Laurence Lundblade59289e52019-12-30 13:44:37 -08005216 }
5217
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07005218 uErr = QCBORDecode_GetNext(&DC, &item);
5219 if(uErr != QCBOR_SUCCESS) {
5220 return 105;
Laurence Lundblade59289e52019-12-30 13:44:37 -08005221 }
5222
5223 if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM ||
5224 item.val.expAndMantissa.nExponent != INT32_MAX ||
5225 UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) {
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07005226 return 106;
5227 }
5228
Laurence Lundblade37286c02022-09-03 10:05:02 -07005229#endif /* QCBOR_TAGS_DISABLED */
Laurence Lundblade59289e52019-12-30 13:44:37 -08005230
5231 return 0;
5232}
5233
5234
Laurence Lundblade37286c02022-09-03 10:05:02 -07005235int32_t ExponentAndMantissaDecodeTests(void)
5236{
5237 int32_t rv = ProcessEaMTests();
5238 if(rv) {
5239 return rv;
5240 }
5241
5242 return ExponentAndMantissaDecodeTestsSecondary();
5243}
5244
5245
Laurence Lundbladecc7da412020-12-27 00:09:07 -08005246static const struct FailInput ExponentAndMantissaFailures[] = {
Laurence Lundblade59289e52019-12-30 13:44:37 -08005247 // Exponent > INT64_MAX
5248 { {(uint8_t[]){0xC4, 0x82, 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
5249 0xFF, 0xFF, 0x1B, 0x80, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
5250 0xFF, 0xFF,}, 20}, QCBOR_ERR_BAD_EXP_AND_MANTISSA},
5251 // Mantissa > INT64_MAX
5252 { {(uint8_t[]){0xC4, 0x82, 0x1B, 0x80, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
5253 0xFF, 0xFF, 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05,
5254 0x06, 0x07, 0x08, 0x09, 0x10}, 23}, QCBOR_ERR_BAD_EXP_AND_MANTISSA},
5255 // End of input
Laurence Lundbladee6f15112020-07-23 18:44:16 -07005256 { {(uint8_t[]){0xC4, 0x82}, 2}, QCBOR_ERR_NO_MORE_ITEMS},
Laurence Lundblade59289e52019-12-30 13:44:37 -08005257 // End of input
Laurence Lundbladee6f15112020-07-23 18:44:16 -07005258 { {(uint8_t[]){0xC4, 0x82, 0x01}, 3}, QCBOR_ERR_NO_MORE_ITEMS},
Laurence Lundblade59289e52019-12-30 13:44:37 -08005259 // bad content for big num
5260 { {(uint8_t[]){0xC4, 0x82, 0x01, 0xc3, 0x01}, 5}, QCBOR_ERR_BAD_OPT_TAG},
5261 // bad content for big num
5262 { {(uint8_t[]){0xC4, 0x82, 0xc2, 0x01, 0x1f}, 5}, QCBOR_ERR_BAD_INT},
5263 // Bad integer for exponent
5264 { {(uint8_t[]){0xC4, 0x82, 0x01, 0x1f}, 4}, QCBOR_ERR_BAD_INT},
5265 // Bad integer for mantissa
5266 { {(uint8_t[]){0xC4, 0x82, 0x1f, 0x01}, 4}, QCBOR_ERR_BAD_INT},
5267 // 3 items in array
5268 { {(uint8_t[]){0xC4, 0x83, 0x03, 0x01, 02}, 5}, QCBOR_ERR_BAD_EXP_AND_MANTISSA},
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08005269#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundblade59289e52019-12-30 13:44:37 -08005270 // unterminated indefinite length array
5271 { {(uint8_t[]){0xC4, 0x9f, 0x03, 0x01, 0x02}, 5}, QCBOR_ERR_BAD_EXP_AND_MANTISSA},
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08005272#else /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
5273 // unterminated indefinite length array
5274 { {(uint8_t[]){0xC4, 0x9f, 0x03, 0x01, 0x02}, 5}, QCBOR_ERR_INDEF_LEN_ARRAYS_DISABLED},
5275#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
Laurence Lundblade59289e52019-12-30 13:44:37 -08005276 // Empty array
5277 { {(uint8_t[]){0xC4, 0x80}, 2}, QCBOR_ERR_NO_MORE_ITEMS},
5278 // Second is not an integer
5279 { {(uint8_t[]){0xC4, 0x82, 0x03, 0x40}, 4}, QCBOR_ERR_BAD_EXP_AND_MANTISSA},
5280 // First is not an integer
5281 { {(uint8_t[]){0xC4, 0x82, 0x40}, 3}, QCBOR_ERR_BAD_EXP_AND_MANTISSA},
5282 // Not an array
5283 { {(uint8_t[]){0xC4, 0xa2}, 2}, QCBOR_ERR_BAD_EXP_AND_MANTISSA}
5284};
5285
5286
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03005287int32_t ExponentAndMantissaDecodeFailTests(void)
Laurence Lundblade59289e52019-12-30 13:44:37 -08005288{
5289 return ProcessFailures(ExponentAndMantissaFailures,
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08005290 C_ARRAY_COUNT(ExponentAndMantissaFailures,
5291 struct FailInput));
Laurence Lundblade59289e52019-12-30 13:44:37 -08005292}
5293
Laurence Lundbladedd6e76e2021-03-10 01:54:01 -07005294#endif /* QCBOR_DISABLE_EXP_AND_MANTISSA */
Laurence Lundbladebb87be22020-04-09 19:15:32 -07005295
5296
5297
5298/*
5299 Some basic CBOR with map and array used in a lot of tests.
5300 The map labels are all strings
5301
Laurence Lundblade8ffdb742020-05-07 02:49:18 -07005302 {
5303 "first integer": 42,
Laurence Lundbladebb87be22020-04-09 19:15:32 -07005304 "an array of two strings": [
5305 "string1", "string2"
5306 ],
5307 "map in a map": {
5308 "bytes 1": h'78787878',
5309 "bytes 2": h'79797979',
5310 "another int": 98,
5311 "text 2": "lies, damn lies and statistics"
5312 }
Laurence Lundbladeddebabe2020-11-22 11:32:17 -08005313 }
Laurence Lundbladebb87be22020-04-09 19:15:32 -07005314 */
Laurence Lundblade9b334962020-08-27 10:55:53 -07005315
Laurence Lundbladef7a70bc2020-10-24 12:23:25 -07005316int32_t SpiffyDecodeBasicMap(UsefulBufC input)
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07005317{
5318 QCBORItem Item1, Item2, Item3;
5319 int64_t nDecodedInt1, nDecodedInt2;
5320 UsefulBufC B1, B2, S1, S2, S3;
5321
5322 QCBORDecodeContext DCtx;
5323 QCBORError nCBORError;
5324
5325 QCBORDecode_Init(&DCtx, input, 0);
5326
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005327 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07005328
5329 QCBORDecode_GetInt64InMapSZ(&DCtx, "first integer", &nDecodedInt1);
5330
5331 QCBORDecode_EnterMapFromMapSZ(&DCtx, "map in a map");
5332 QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2);
Laurence Lundblade323f8a92020-09-06 19:43:09 -07005333 QCBORDecode_GetByteStringInMapSZ(&DCtx, "bytes 1", &B1);
5334 QCBORDecode_GetByteStringInMapSZ(&DCtx, "bytes 2", &B2);
5335 QCBORDecode_GetTextStringInMapSZ(&DCtx, "text 2", &S1);
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07005336 QCBORDecode_ExitMap(&DCtx);
5337
5338 QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings");
5339 QCBORDecode_GetNext(&DCtx, &Item1);
5340 QCBORDecode_GetNext(&DCtx, &Item2);
5341 if(QCBORDecode_GetNext(&DCtx, &Item3) != QCBOR_ERR_NO_MORE_ITEMS) {
5342 return -400;
5343 }
5344 QCBORDecode_ExitArray(&DCtx);
5345
5346 // Parse the same array again using GetText() instead of GetItem()
5347 QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings");
Laurence Lundblade323f8a92020-09-06 19:43:09 -07005348 QCBORDecode_GetTextString(&DCtx, &S2);
5349 QCBORDecode_GetTextString(&DCtx, &S3);
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07005350 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) {
5351 return 5000;
5352 }
5353 /* QCBORDecode_GetText(&DCtx, &S3);
5354 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_NO_MORE_ITEMS) {
5355 return 5001;
5356 } */
5357
5358 QCBORDecode_ExitArray(&DCtx);
5359
5360 QCBORDecode_ExitMap(&DCtx);
5361
5362 nCBORError = QCBORDecode_Finish(&DCtx);
5363
5364 if(nCBORError) {
5365 return (int32_t)nCBORError;
5366 }
5367
5368 if(nDecodedInt1 != 42) {
5369 return 1001;
5370 }
5371
5372 if(nDecodedInt2 != 98) {
5373 return 1002;
5374 }
5375
5376 if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07005377 UsefulBufCompareToSZ(Item1.val.string, "string1")) {
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07005378 return 1003;
5379 }
5380
5381 if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07005382 UsefulBufCompareToSZ(Item2.val.string, "string2")) {
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07005383 return 1004;
5384 }
5385
Laurence Lundblade9b334962020-08-27 10:55:53 -07005386 if(UsefulBufCompareToSZ(S1, "lies, damn lies and statistics")) {
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07005387 return 1005;
5388 }
5389
5390 if(UsefulBuf_Compare(B1, UsefulBuf_FromSZ("xxxx"))){
5391 return 1006;
5392 }
5393
5394 if(UsefulBuf_Compare(B2, UsefulBuf_FromSZ("yyyy"))){
5395 return 1007;
5396 }
5397
5398 if(UsefulBuf_Compare(S2, UsefulBuf_FromSZ("string1"))){
5399 return 1008;
5400 }
5401
5402 if(UsefulBuf_Compare(S3, UsefulBuf_FromSZ("string2"))){
5403 return 1009;
5404 }
5405
5406 return 0;
5407}
5408
Laurence Lundbladeddebabe2020-11-22 11:32:17 -08005409/*
Laurence Lundbladecc7da412020-12-27 00:09:07 -08005410 {
5411 -75008: h'05083399',
5412 88: [],
5413 100100: {
5414 "sub1": {
5415 10: [
5416 0
5417 ],
5418 -75009: h'A46823990001',
5419 100100: {
5420 "json": "{ \"ueid\", \"xyz\"}",
5421 "subsub": {
5422 100002: h'141813191001'
5423 }
5424 }
Laurence Lundbladeddebabe2020-11-22 11:32:17 -08005425 }
Laurence Lundbladecc7da412020-12-27 00:09:07 -08005426 }
Laurence Lundbladeddebabe2020-11-22 11:32:17 -08005427 }
Laurence Lundbladeddebabe2020-11-22 11:32:17 -08005428 */
5429
5430static const uint8_t spNestedCBOR[] = {
Laurence Lundbladecc7da412020-12-27 00:09:07 -08005431 0xa3, 0x3a, 0x00, 0x01, 0x24, 0xff, 0x44, 0x05,
5432 0x08, 0x33, 0x99, 0x18, 0x58, 0x80, 0x1a, 0x00,
5433 0x01, 0x87, 0x04, 0xa1, 0x64, 0x73, 0x75, 0x62,
5434 0x31, 0xa3, 0x0a, 0x81, 0x00, 0x3a, 0x00, 0x01,
5435 0x25, 0x00, 0x46, 0xa4, 0x68, 0x23, 0x99, 0x00,
5436 0x01, 0x1a, 0x00, 0x01, 0x87, 0x04, 0xa2, 0x64,
5437 0x6a, 0x73, 0x6f, 0x6e, 0x70, 0x7b, 0x20, 0x22,
5438 0x75, 0x65, 0x69, 0x64, 0x22, 0x2c, 0x20, 0x22,
5439 0x78, 0x79, 0x7a, 0x22, 0x7d, 0x66, 0x73, 0x75,
5440 0x62, 0x73, 0x75, 0x62, 0xa1, 0x1a, 0x00, 0x01,
5441 0x86, 0xa2, 0x46, 0x14, 0x18, 0x13, 0x19, 0x10,
5442 0x01
Laurence Lundbladeddebabe2020-11-22 11:32:17 -08005443};
5444
5445/* Get item in multi-level nesting in spNestedCBOR */
5446static int32_t DecodeNestedGetSubSub(QCBORDecodeContext *pDCtx)
5447{
5448 UsefulBufC String;
5449
5450 uint8_t test_oemid_bytes[] = {0x14, 0x18, 0x13, 0x19, 0x10, 0x01};
5451 const struct q_useful_buf_c test_oemid = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(test_oemid_bytes);
5452
5453 QCBORDecode_EnterMapFromMapN(pDCtx, 100100);
5454 QCBORDecode_EnterMap(pDCtx, NULL);
5455 QCBORDecode_EnterMapFromMapN(pDCtx, 100100);
5456 QCBORDecode_EnterMapFromMapSZ(pDCtx, "subsub");
5457 QCBORDecode_GetByteStringInMapN(pDCtx, 100002, &String);
5458 if(QCBORDecode_GetError(pDCtx)) {
5459 return 4001;
5460 }
5461 if(UsefulBuf_Compare(String, test_oemid)) {
5462 return 4002;
5463 }
5464 QCBORDecode_ExitMap(pDCtx);
5465 QCBORDecode_ExitMap(pDCtx);
5466 QCBORDecode_ExitMap(pDCtx);
5467 QCBORDecode_ExitMap(pDCtx);
5468
5469 return 0;
5470}
5471
5472/* Iterations on the zero-length array in spNestedCBOR */
5473static int32_t DecodeNestedGetEmpty(QCBORDecodeContext *pDCtx)
5474{
5475 QCBORItem Item;
5476 QCBORError uErr;
5477
5478 QCBORDecode_EnterArrayFromMapN(pDCtx, 88);
5479 for(int x = 0; x < 20; x++) {
5480 uErr = QCBORDecode_GetNext(pDCtx, &Item);
5481 if(uErr != QCBOR_ERR_NO_MORE_ITEMS) {
5482 return 4100;
5483
5484 }
5485 }
5486 QCBORDecode_ExitArray(pDCtx);
5487 if(QCBORDecode_GetError(pDCtx)) {
5488 return 4101;
5489 }
5490
5491 return 0;
5492}
5493
5494/* Various iterations on the array that contains a zero in spNestedCBOR */
5495static int32_t DecodeNestedGetZero(QCBORDecodeContext *pDCtx)
5496{
5497 QCBORError uErr;
5498
5499 QCBORDecode_EnterMapFromMapN(pDCtx, 100100);
5500 QCBORDecode_EnterMapFromMapSZ(pDCtx, "sub1");
5501 QCBORDecode_EnterArrayFromMapN(pDCtx, 10);
5502 int64_t nInt = 99;
5503 QCBORDecode_GetInt64(pDCtx, &nInt);
5504 if(nInt != 0) {
5505 return 4200;
5506 }
5507 for(int x = 0; x < 20; x++) {
5508 QCBORItem Item;
5509 uErr = QCBORDecode_GetNext(pDCtx, &Item);
5510 if(uErr != QCBOR_ERR_NO_MORE_ITEMS) {
5511 return 4201;
5512
5513 }
5514 }
5515 QCBORDecode_ExitArray(pDCtx);
5516 if(QCBORDecode_GetAndResetError(pDCtx)) {
5517 return 4202;
5518 }
5519 QCBORDecode_EnterArrayFromMapN(pDCtx, 10);
5520 UsefulBufC dD;
5521 QCBORDecode_GetByteString(pDCtx, &dD);
5522 if(QCBORDecode_GetAndResetError(pDCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
5523 return 4203;
5524 }
5525 for(int x = 0; x < 20; x++) {
5526 QCBORDecode_GetByteString(pDCtx, &dD);
5527 uErr = QCBORDecode_GetAndResetError(pDCtx);
5528 if(uErr != QCBOR_ERR_NO_MORE_ITEMS) {
5529 return 4204;
5530 }
5531 }
5532 QCBORDecode_ExitArray(pDCtx);
5533 QCBORDecode_ExitMap(pDCtx);
5534 QCBORDecode_ExitMap(pDCtx);
5535
5536 return 0;
5537}
5538
5539/* Repeatedly enter and exit maps and arrays, go off the end of maps
5540 and arrays and such. */
Laurence Lundbladeb9702452021-03-08 21:02:57 -08005541static int32_t DecodeNestedIterate(void)
Laurence Lundbladeddebabe2020-11-22 11:32:17 -08005542{
5543 QCBORDecodeContext DCtx;
5544 int32_t nReturn;
5545 QCBORError uErr;
5546
5547 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spNestedCBOR), 0);
5548 QCBORDecode_EnterMap(&DCtx, NULL);
5549
5550 for(int j = 0; j < 5; j++) {
5551 for(int i = 0; i < 20; i++) {
5552 nReturn = DecodeNestedGetSubSub(&DCtx);
5553 if(nReturn) {
5554 return nReturn;
5555 }
5556 }
5557
5558 for(int i = 0; i < 20; i++) {
5559 nReturn = DecodeNestedGetEmpty(&DCtx);
5560 if(nReturn ) {
5561 return nReturn;
5562 }
5563 }
5564
5565 for(int i = 0; i < 20; i++) {
5566 nReturn = DecodeNestedGetZero(&DCtx);
5567 if(nReturn ) {
5568 return nReturn;
5569 }
5570 }
5571 }
5572
5573 QCBORDecode_ExitMap(&DCtx);
5574 uErr = QCBORDecode_Finish(&DCtx);
5575 if(uErr) {
5576 return (int32_t)uErr + 4100;
5577 }
5578
5579 return 0;
5580}
5581
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07005582
5583/*
Laurence Lundbladecc7da412020-12-27 00:09:07 -08005584 [
5585 23,
5586 6000,
5587 h'67616C6163746963',
5588 h'686176656E20746F6B656E'
5589 ]
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07005590 */
5591static const uint8_t spSimpleArray[] = {
Laurence Lundblade9b334962020-08-27 10:55:53 -07005592 0x84,
5593 0x17,
5594 0x19, 0x17, 0x70,
5595 0x48, 0x67, 0x61, 0x6C, 0x61, 0x63, 0x74, 0x69, 0x63,
5596 0x4B, 0x68, 0x61, 0x76, 0x65, 0x6E, 0x20, 0x74, 0x6F, 0x6B, 0x65, 0x6E};
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07005597
Laurence Lundbladecc7da412020-12-27 00:09:07 -08005598/* [h'', {}, [], 0] */
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08005599static const uint8_t spArrayOfEmpty[] = {0x84, 0x40, 0xa0, 0x80, 0x00};
5600
Laurence Lundbladecc7da412020-12-27 00:09:07 -08005601/* {} */
Laurence Lundbladee6f15112020-07-23 18:44:16 -07005602static const uint8_t spEmptyMap[] = {0xa0};
5603
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08005604#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundbladecc7da412020-12-27 00:09:07 -08005605/* {} */
Laurence Lundbladee6f15112020-07-23 18:44:16 -07005606static const uint8_t spEmptyInDefinteLengthMap[] = {0xbf, 0xff};
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07005607
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08005608
Laurence Lundbladef0499502020-08-01 11:55:57 -07005609/*
5610 {
5611 0: [],
5612 9: [
5613 [],
5614 []
5615 ],
5616 8: {
5617 1: [],
5618 2: {},
5619 3: []
5620 },
5621 4: {},
5622 5: [],
5623 6: [
5624 [],
5625 []
5626 ]
Laurence Lundbladecc7da412020-12-27 00:09:07 -08005627 }
Laurence Lundbladef0499502020-08-01 11:55:57 -07005628 */
5629static const uint8_t spMapOfEmpty[] = {
Laurence Lundbladecc7da412020-12-27 00:09:07 -08005630 0xa6, 0x00, 0x80, 0x09, 0x82, 0x80, 0x80, 0x08,
5631 0xa3, 0x01, 0x80, 0x02, 0xa0, 0x03, 0x80, 0x04,
5632 0xa0, 0x05, 0x9f, 0xff, 0x06, 0x9f, 0x80, 0x9f,
5633 0xff, 0xff};
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005634
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08005635#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
5636
5637
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005638/*
5639 Too many tags
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005640 Duplicate label
5641 Integer overflow
5642 Date overflow
5643
Laurence Lundbladecc7da412020-12-27 00:09:07 -08005644 {
5645 1: 224(225(226(227(4(0))))),
Laurence Lundbladecc7da412020-12-27 00:09:07 -08005646 3: -18446744073709551616,
5647 4: 1(1.0e+300),
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07005648 5: 0,
5649 8: 8
Laurence Lundbladecc7da412020-12-27 00:09:07 -08005650 }
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005651 */
5652static const uint8_t spRecoverableMapErrors[] = {
Laurence Lundblade37286c02022-09-03 10:05:02 -07005653#ifndef QCBOR_DISABLE_TAGS
Laurence Lundblade2d493002024-02-01 11:09:17 -07005654 0xa5,
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005655 0x04, 0xc1, 0xfb, 0x7e, 0x37, 0xe4, 0x3c, 0x88, 0x00, 0x75, 0x9c,
Laurence Lundblade37286c02022-09-03 10:05:02 -07005656 0x01, 0xd8, 0xe0, 0xd8, 0xe1, 0xd8, 0xe2, 0xd8, 0xe3, 0xd8, 0x04, 0x00,
5657#else
5658 0xa4,
5659#endif
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005660 0x05, 0x00,
5661 0x05, 0x00,
5662 0x08, 0x08,
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005663};
5664
Laurence Lundbladecc7da412020-12-27 00:09:07 -08005665/* Bad break */
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005666static const uint8_t spUnRecoverableMapError1[] = {
5667 0xa2, 0xff, 0x01, 0x00, 0x02, 0x00
5668};
5669
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08005670#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundbladecc7da412020-12-27 00:09:07 -08005671/* No more items */
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005672static const uint8_t spUnRecoverableMapError2[] = {
5673 0xbf, 0x02, 0xbf, 0xff, 0x01, 0x00, 0x02, 0x00
5674};
5675
Laurence Lundbladecc7da412020-12-27 00:09:07 -08005676/* Hit end because string is too long */
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005677static const uint8_t spUnRecoverableMapError3[] = {
5678 0xbf, 0x02, 0x69, 0x64, 0x64, 0xff
5679};
5680
Laurence Lundbladecc7da412020-12-27 00:09:07 -08005681/* Hit end because string is too long */
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005682static const uint8_t spUnRecoverableMapError4[] = {
5683 0xbf,
5684 0x02, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f,
5685 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f,
5686 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
5687 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
5688 0xff
5689};
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08005690#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005691
Laurence Lundblade63926052021-03-29 16:05:51 -07005692const unsigned char not_well_formed_submod_section[] = {
5693 0xa1, 0x14, 0x1f,
5694};
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005695
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07005696
5697/* Array of length 3, but only two items. */
5698const unsigned char spBadConsumeInput[] = {
5699 0x83, 0x00, 0x00
5700};
5701
5702/* Tag nesting too deep. */
5703const unsigned char spBadConsumeInput2[] = {
5704 0x81,
5705 0xD8, 0x37,
5706 0xD8, 0x2C,
5707 0xD8, 0x21,
5708 0xD6,
5709 0xCB,
5710 00
5711};
5712
5713const unsigned char spBadConsumeInput3[] = {
5714 0x81, 0xc0, 0x81, 0x00
5715};
5716
5717const unsigned char spBadConsumeInput4[] = {
5718 0x81, 0x9f, 0x00, 0xff
5719};
5720
5721const unsigned char spBadConsumeInput5[] = {
5722 0xa1, 0x80, 0x00
5723};
5724
Laurence Lundbladec5f45e42023-12-18 09:23:20 -07005725/*
5726 Lots of nesting for various nesting tests.
5727 { 1:1,
5728 2:{
5729 21:21,
5730 22:{
5731 221:[2111, 2112, 2113],
5732 222:222,
5733 223: {}
5734 },
5735 23: 23
5736 },
5737 3:3,
5738 4: [ {} ]
5739 }
5740 */
5741static const uint8_t spNested[] = {
57420xA4, /* Map of 4 */
5743 0x01, 0x01, /* Map entry 1 : 1 */
5744 0x02, 0xA3, /* Map entry 2 : {, an array of 3 */
5745 0x15, 0x15, /* Map entry 21 : 21 */
5746 0x16, 0xA3, /* Map entry 22 : {, a map of 3 */
5747 0x18, 0xDD, 0x83, /* Map entry 221 : [ an array of 3 */
5748 0x19, 0x08, 0x3F, /* Array item 2111 */
5749 0x19, 0x08, 0x40, /* Array item 2112 */
5750 0x19, 0x08, 0x41, /* Array item 2113 */
5751 0x18, 0xDE, 0x18, 0xDE, /* Map entry 222 : 222 */
5752 0x18, 0xDF, 0xA0, /* Map entry 223 : {} */
5753 0x17, 0x17, /* Map entry 23 : 23 */
5754 0x03, 0x03, /* Map entry 3 : 3 */
5755 0x04, 0x81, /* Map entry 4: [, an array of 1 */
5756 0xA0 /* Array entry {}, an empty map */
5757};
5758
5759
5760static int32_t EnterMapCursorTest(void)
5761{
5762 QCBORDecodeContext DCtx;
5763 QCBORItem Item1;
5764
5765 int i;
5766 for(i = 0; i < 13; i++) {
5767 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spNested), 0);
5768 QCBORDecode_EnterMap(&DCtx, NULL);
5769 int j;
5770 /* Move travesal cursor */
5771 for(j = 0; j < i; j++) {
5772 QCBORDecode_GetNext(&DCtx, &Item1);
5773 }
5774 QCBORDecode_EnterMapFromMapN(&DCtx, 2);
5775 QCBORDecode_ExitMap(&DCtx);
5776 QCBORDecode_GetNext(&DCtx, &Item1);
5777 if(Item1.label.int64 != 3) {
5778 return 8000;
5779 }
5780 }
5781
5782 for(i = 0; i < 13; i++) {
5783 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spNested), 0);
5784 QCBORDecode_EnterMap(&DCtx, NULL);
5785 int j;
5786 /* Move travesal cursor */
5787 for(j = 0; j < i; j++) {
5788 QCBORDecode_GetNext(&DCtx, &Item1);
5789 }
5790 QCBORDecode_EnterMapFromMapN(&DCtx, 2);
5791 QCBORDecode_EnterMapFromMapN(&DCtx, 22);
5792 QCBORDecode_ExitMap(&DCtx);
5793 QCBORDecode_GetNext(&DCtx, &Item1);
5794 if(Item1.label.int64 != 23) {
5795 return 8000;
5796 }
5797 }
5798
5799 for(i = 0; i < 13; i++) {
5800 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spNested), 0);
5801 QCBORDecode_EnterMap(&DCtx, NULL);
5802 int j;
5803 /* Move travesal cursor */
5804 for(j = 0; j < i; j++) {
5805 QCBORDecode_GetNext(&DCtx, &Item1);
5806 }
5807 QCBORDecode_EnterMapFromMapN(&DCtx, 2);
5808 QCBORDecode_EnterMapFromMapN(&DCtx, 22);
5809 for(j = 0; j < i; j++) {
5810 QCBORDecode_GetNext(&DCtx, &Item1);
5811 }
5812 QCBORDecode_EnterArrayFromMapN(&DCtx, 221);
5813 QCBORDecode_ExitArray(&DCtx);
5814 QCBORDecode_ExitMap(&DCtx);
5815 QCBORDecode_GetNext(&DCtx, &Item1);
5816 if(Item1.label.int64 != 23) {
5817 return 8000;
5818 }
5819 QCBORDecode_ExitMap(&DCtx);
5820 QCBORDecode_GetNext(&DCtx, &Item1);
5821 if(Item1.label.int64 != 3) {
5822 return 8000;
5823 }
5824 }
5825
5826 return 0;
5827}
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07005828
5829
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03005830int32_t EnterMapTest(void)
Laurence Lundbladebb87be22020-04-09 19:15:32 -07005831{
Laurence Lundbladef0499502020-08-01 11:55:57 -07005832 QCBORItem Item1;
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005833 QCBORItem ArrayItem;
Laurence Lundbladebb87be22020-04-09 19:15:32 -07005834 QCBORDecodeContext DCtx;
Laurence Lundbladef0499502020-08-01 11:55:57 -07005835 int32_t nReturn;
5836 QCBORError uErr;
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005837
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08005838#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005839 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spMapOfEmpty), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005840 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005841
Laurence Lundbladef0499502020-08-01 11:55:57 -07005842
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005843 QCBORDecode_EnterArray(&DCtx, NULL); // Label 0
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005844 QCBORDecode_ExitArray(&DCtx);
5845
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005846 QCBORDecode_EnterArray(&DCtx, NULL); // Label 9
5847 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005848 QCBORDecode_ExitArray(&DCtx);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005849 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005850 QCBORDecode_ExitArray(&DCtx);
5851 QCBORDecode_ExitArray(&DCtx);
5852
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005853 QCBORDecode_EnterMap(&DCtx, NULL); // Label 8
5854 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005855 QCBORDecode_ExitArray(&DCtx);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005856 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005857 QCBORDecode_ExitMap(&DCtx);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005858 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005859 QCBORDecode_ExitArray(&DCtx);
5860 QCBORDecode_ExitMap(&DCtx);
5861
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005862 QCBORDecode_EnterMap(&DCtx, NULL); // Label4
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005863 QCBORDecode_ExitMap(&DCtx);
5864
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005865 QCBORDecode_EnterArray(&DCtx, NULL); // Label 5
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005866 QCBORDecode_ExitArray(&DCtx);
5867
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005868 QCBORDecode_EnterArray(&DCtx, NULL); // Label 6
5869 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005870 QCBORDecode_ExitArray(&DCtx);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005871 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005872 QCBORDecode_ExitArray(&DCtx);
5873 QCBORDecode_ExitArray(&DCtx);
5874
5875 QCBORDecode_ExitMap(&DCtx);
5876
5877 uErr = QCBORDecode_Finish(&DCtx);
5878 if(uErr != QCBOR_SUCCESS){
5879 return 3011;
5880 }
5881
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07005882 (void)pValidMapIndefEncoded;
Laurence Lundbladef7a70bc2020-10-24 12:23:25 -07005883 nReturn = SpiffyDecodeBasicMap(UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapIndefEncoded));
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07005884 if(nReturn) {
5885 return nReturn + 20000;
Laurence Lundbladebb87be22020-04-09 19:15:32 -07005886 }
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08005887#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
5888
Laurence Lundbladebb87be22020-04-09 19:15:32 -07005889
Laurence Lundbladef7a70bc2020-10-24 12:23:25 -07005890 nReturn = SpiffyDecodeBasicMap(UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded));
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005891 if(nReturn) {
5892 return nReturn;
5893 }
Laurence Lundbladebb87be22020-04-09 19:15:32 -07005894
Laurence Lundblade8ffdb742020-05-07 02:49:18 -07005895
Laurence Lundblade937ea812020-05-08 11:38:23 -07005896
Laurence Lundblade2f467f92020-10-09 17:50:11 -07005897 // These tests confirm the cursor is at the right place after entering
5898 // a map or array
Laurence Lundblade9b334962020-08-27 10:55:53 -07005899 const UsefulBufC ValidEncodedMap = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded);
Laurence Lundblade937ea812020-05-08 11:38:23 -07005900
5901 // Confirm cursor is at right place
Laurence Lundblade9b334962020-08-27 10:55:53 -07005902 QCBORDecode_Init(&DCtx, ValidEncodedMap, 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005903 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade937ea812020-05-08 11:38:23 -07005904 QCBORDecode_GetNext(&DCtx, &Item1);
5905 if(Item1.uDataType != QCBOR_TYPE_INT64) {
5906 return 2001;
5907 }
5908
5909
Laurence Lundblade9b334962020-08-27 10:55:53 -07005910 QCBORDecode_Init(&DCtx, ValidEncodedMap, 0);
Laurence Lundblade6f3f78e2020-08-31 13:09:14 -07005911 QCBORDecode_VGetNext(&DCtx, &Item1);
5912 QCBORDecode_VGetNext(&DCtx, &Item1);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005913 QCBORDecode_EnterArray(&DCtx, &ArrayItem);
5914 if(ArrayItem.uLabelType != QCBOR_TYPE_TEXT_STRING ||
5915 UsefulBuf_Compare(ArrayItem.label.string,
5916 UsefulBuf_FROM_SZ_LITERAL("an array of two strings"))) {
5917 return 2051;
5918 }
Laurence Lundblade937ea812020-05-08 11:38:23 -07005919 QCBORDecode_GetNext(&DCtx, &Item1);
5920 if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING) {
5921 return 2002;
5922 }
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005923 QCBORDecode_ExitArray(&DCtx);
5924 QCBORDecode_EnterMap(&DCtx, &ArrayItem);
5925 if(ArrayItem.uLabelType != QCBOR_TYPE_TEXT_STRING ||
5926 UsefulBuf_Compare(ArrayItem.label.string,
5927 UsefulBuf_FROM_SZ_LITERAL("map in a map"))) {
5928 return 2052;
5929 }
5930
Laurence Lundblade937ea812020-05-08 11:38:23 -07005931
Laurence Lundblade9b334962020-08-27 10:55:53 -07005932 QCBORDecode_Init(&DCtx, ValidEncodedMap, 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005933 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade64b607e2020-05-13 13:05:57 -07005934 QCBORDecode_GetNext(&DCtx, &Item1);
5935 QCBORDecode_GetNext(&DCtx, &Item1);
5936 QCBORDecode_GetNext(&DCtx, &Item1);
Laurence Lundblade937ea812020-05-08 11:38:23 -07005937 QCBORDecode_EnterMapFromMapSZ(&DCtx, "map in a map");
5938 QCBORDecode_GetNext(&DCtx, &Item1);
5939 if(Item1.uDataType != QCBOR_TYPE_BYTE_STRING) {
5940 return 2003;
5941 }
5942
Laurence Lundblade9b334962020-08-27 10:55:53 -07005943 QCBORDecode_Init(&DCtx, ValidEncodedMap, 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005944 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade937ea812020-05-08 11:38:23 -07005945 QCBORDecode_GetNext(&DCtx, &Item1);
5946 QCBORDecode_GetNext(&DCtx, &Item1);
5947 QCBORDecode_GetNext(&DCtx, &Item1);
5948 QCBORDecode_GetNext(&DCtx, &Item1);
5949 QCBORDecode_GetNext(&DCtx, &Item1);
5950 QCBORDecode_GetNext(&DCtx, &Item1);
5951 QCBORDecode_GetNext(&DCtx, &Item1);
5952 QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings");
5953 QCBORDecode_GetNext(&DCtx, &Item1);
5954 if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING) {
Laurence Lundblade64b607e2020-05-13 13:05:57 -07005955 return 2004;
Laurence Lundblade937ea812020-05-08 11:38:23 -07005956 }
5957
Laurence Lundblade9b334962020-08-27 10:55:53 -07005958 QCBORDecode_Init(&DCtx, ValidEncodedMap, 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005959 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade2b843b52020-06-16 20:51:03 -07005960 QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings");
5961 QCBORDecode_ExitArray(&DCtx);
5962 QCBORDecode_GetNext(&DCtx, &Item1);
5963 if(Item1.uDataType != QCBOR_TYPE_MAP && Item1.uLabelAlloc != QCBOR_TYPE_TEXT_STRING) {
5964 return 2006;
5965 }
5966 QCBORDecode_ExitMap(&DCtx);
5967 if(QCBORDecode_GetNext(&DCtx, &Item1) != QCBOR_ERR_NO_MORE_ITEMS) {
5968 return 2007;
5969 }
5970
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07005971 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSimpleArray), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005972 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07005973 int64_t nDecodedInt2;
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005974 QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2);
5975 uErr = QCBORDecode_GetAndResetError(&DCtx);
5976 if(uErr != QCBOR_ERR_MAP_NOT_ENTERED){
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07005977 return 2008;
5978 }
5979 UsefulBufC String;
Laurence Lundblade323f8a92020-09-06 19:43:09 -07005980 QCBORDecode_GetTextStringInMapN(&DCtx, 88, &String);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005981 if(uErr != QCBOR_ERR_MAP_NOT_ENTERED){
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07005982 return 2009;
5983 }
Laurence Lundblade937ea812020-05-08 11:38:23 -07005984
Laurence Lundbladee6f15112020-07-23 18:44:16 -07005985
5986 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEmptyMap), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005987 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundbladee6f15112020-07-23 18:44:16 -07005988 // This will fail because the map is empty.
5989 QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2);
5990 uErr = QCBORDecode_GetAndResetError(&DCtx);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07005991 if(uErr != QCBOR_ERR_LABEL_NOT_FOUND){
Laurence Lundbladee6f15112020-07-23 18:44:16 -07005992 return 2010;
5993 }
5994 QCBORDecode_ExitMap(&DCtx);
5995 uErr = QCBORDecode_Finish(&DCtx);
5996 if(uErr != QCBOR_SUCCESS){
5997 return 2011;
5998 }
5999
6000
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08006001#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundbladee6f15112020-07-23 18:44:16 -07006002 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEmptyInDefinteLengthMap), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006003 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundbladee6f15112020-07-23 18:44:16 -07006004 // This will fail because the map is empty.
6005 QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2);
6006 uErr = QCBORDecode_GetAndResetError(&DCtx);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006007 if(uErr != QCBOR_ERR_LABEL_NOT_FOUND){
Laurence Lundblade085d7952020-07-24 10:26:30 -07006008 return 2012;
Laurence Lundbladee6f15112020-07-23 18:44:16 -07006009 }
6010 QCBORDecode_ExitMap(&DCtx);
6011 uErr = QCBORDecode_Finish(&DCtx);
6012 if(uErr != QCBOR_SUCCESS){
Laurence Lundblade085d7952020-07-24 10:26:30 -07006013 return 2013;
Laurence Lundbladee6f15112020-07-23 18:44:16 -07006014 }
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08006015#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
Laurence Lundbladee6f15112020-07-23 18:44:16 -07006016
Laurence Lundblade5f4e8712020-07-25 11:44:43 -07006017
6018 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spArrayOfEmpty), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006019 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade323f8a92020-09-06 19:43:09 -07006020 QCBORDecode_GetByteString(&DCtx, &String);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006021 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade5f4e8712020-07-25 11:44:43 -07006022 QCBORDecode_ExitMap(&DCtx);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006023 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade5f4e8712020-07-25 11:44:43 -07006024 QCBORDecode_ExitArray(&DCtx);
6025 QCBORDecode_GetInt64(&DCtx, &nDecodedInt2);
6026 QCBORDecode_ExitArray(&DCtx);
6027 uErr = QCBORDecode_Finish(&DCtx);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07006028 if(uErr != QCBOR_SUCCESS) {
Laurence Lundblade5f4e8712020-07-25 11:44:43 -07006029 return 2014;
6030 }
6031
Laurence Lundblade93d3f532020-09-28 21:09:12 -07006032 int64_t nInt;
6033 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spRecoverableMapErrors), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006034 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade37286c02022-09-03 10:05:02 -07006035#ifndef QCBOR_DISABLE_TAGS
Laurence Lundblade93d3f532020-09-28 21:09:12 -07006036 QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt);
Laurence Lundblade88e9db22020-11-02 03:56:33 -08006037 uErr = QCBORDecode_GetError(&DCtx);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07006038 if(uErr != QCBOR_ERR_TOO_MANY_TAGS) {
6039 return 2021;
6040 }
Laurence Lundblade88e9db22020-11-02 03:56:33 -08006041 if(QCBORDecode_GetNthTagOfLast(&DCtx, 0) != CBOR_TAG_INVALID64) {
6042 return 2121;
6043 }
6044 (void)QCBORDecode_GetAndResetError(&DCtx);
Laurence Lundblade37286c02022-09-03 10:05:02 -07006045#endif
Laurence Lundblade88e9db22020-11-02 03:56:33 -08006046
Laurence Lundblade93d3f532020-09-28 21:09:12 -07006047
Laurence Lundblade37286c02022-09-03 10:05:02 -07006048#ifndef QCBOR_DISABLE_TAGS
Laurence Lundblade93d3f532020-09-28 21:09:12 -07006049 QCBORDecode_GetEpochDateInMapN(&DCtx, 0x04, QCBOR_TAG_REQUIREMENT_TAG, &nInt);
6050 uErr = QCBORDecode_GetAndResetError(&DCtx);
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006051 if(uErr != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_DATE_OVERFLOW)) {
Laurence Lundblade93d3f532020-09-28 21:09:12 -07006052 return 2024;
6053 }
Laurence Lundblade37286c02022-09-03 10:05:02 -07006054#endif
Laurence Lundblade93d3f532020-09-28 21:09:12 -07006055
6056 QCBORDecode_GetInt64InMapN(&DCtx, 0x05, &nInt);
6057 uErr = QCBORDecode_GetAndResetError(&DCtx);
6058 if(uErr != QCBOR_ERR_DUPLICATE_LABEL) {
6059 return 2025;
6060 }
6061
6062 QCBORDecode_GetInt64InMapN(&DCtx, 0x08, &nInt);
6063
6064 QCBORDecode_ExitMap(&DCtx);
6065 uErr = QCBORDecode_Finish(&DCtx);
6066 if(uErr != QCBOR_SUCCESS) {
6067 return 2026;
6068 }
6069
6070 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUnRecoverableMapError1), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006071 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07006072 QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt);
6073 uErr = QCBORDecode_GetAndResetError(&DCtx);
6074 if(uErr != QCBOR_ERR_BAD_BREAK) {
6075 return 2030;
6076 }
6077
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08006078#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundblade93d3f532020-09-28 21:09:12 -07006079 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUnRecoverableMapError2), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006080 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07006081 QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt);
6082 uErr = QCBORDecode_GetAndResetError(&DCtx);
6083 if(uErr != QCBOR_ERR_NO_MORE_ITEMS) {
6084 return 2031;
6085 }
6086
6087 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUnRecoverableMapError3), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006088 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07006089 QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt);
6090 uErr = QCBORDecode_GetAndResetError(&DCtx);
6091 if(uErr != QCBOR_ERR_HIT_END) {
6092 return 2032;
6093 }
6094
6095 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUnRecoverableMapError4), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006096 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07006097 QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt);
6098 uErr = QCBORDecode_GetAndResetError(&DCtx);
6099 if(uErr != QCBOR_ERR_ARRAY_DECODE_NESTING_TOO_DEEP) {
6100 return 2033;
6101 }
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08006102#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
6103
Laurence Lundblade732e52d2021-02-22 20:11:01 -07006104 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0);
6105 QCBORDecode_VGetNextConsume(&DCtx, &Item1);
6106 if(Item1.uDataType != QCBOR_TYPE_MAP) {
6107 return 2401;
6108 }
6109 if(QCBORDecode_GetError(&DCtx)) {
6110 return 2402;
6111 }
6112
6113 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0);
6114 QCBORDecode_VGetNext(&DCtx, &Item1);
6115 if(Item1.uDataType != QCBOR_TYPE_MAP ||
6116 Item1.val.uCount != 3 ||
6117 Item1.uNextNestLevel != 1) {
6118 return 2403;
6119 }
6120 if(QCBORDecode_GetError(&DCtx)) {
6121 return 2404;
6122 }
6123 QCBORDecode_VGetNextConsume(&DCtx, &Item1);
6124 if(Item1.uDataType != QCBOR_TYPE_INT64 ||
6125 Item1.uNextNestLevel != 1 ||
6126 Item1.val.int64 != 42) {
6127 return 2405;
6128 }
6129 if(QCBORDecode_GetError(&DCtx)) {
6130 return 2406;
6131 }
6132 QCBORDecode_VGetNextConsume(&DCtx, &Item1);
6133 if(Item1.uDataType != QCBOR_TYPE_ARRAY ||
6134 Item1.uNestingLevel != 1 ||
6135 Item1.uNextNestLevel != 1 ||
6136 Item1.val.uCount != 2) {
6137 return 2407;
6138 }
6139 if(QCBORDecode_GetError(&DCtx)) {
6140 return 2408;
6141 }
6142 QCBORDecode_VGetNextConsume(&DCtx, &Item1);
6143 if(Item1.uDataType != QCBOR_TYPE_MAP ||
6144 Item1.uNestingLevel != 1 ||
6145 Item1.uNextNestLevel != 0 ||
6146 Item1.val.uCount != 4) {
6147 return 2409;
6148 }
6149 if(QCBORDecode_GetError(&DCtx)) {
6150 return 2410;
6151 }
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006152
Laurence Lundbladeddebabe2020-11-22 11:32:17 -08006153 nReturn = DecodeNestedIterate();
6154
Laurence Lundblade63926052021-03-29 16:05:51 -07006155
6156 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(not_well_formed_submod_section), 0);
6157 QCBORDecode_EnterMap(&DCtx, NULL);
6158 QCBORDecode_EnterMapFromMapN(&DCtx, 20);
6159 if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_BAD_INT) {
6160 return 2500;
6161 }
6162
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07006163 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBadConsumeInput), 0);
6164 QCBORDecode_VGetNextConsume(&DCtx, &Item1);
6165 if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_NO_MORE_ITEMS) {
6166 return 2600;
6167 }
6168
Laurence Lundblade37286c02022-09-03 10:05:02 -07006169#ifndef QCBOR_DISABLE_TAGS
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07006170 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBadConsumeInput2), 0);
6171 QCBORDecode_VGetNextConsume(&DCtx, &Item1);
6172 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) {
6173 return 2700;
6174 }
6175
6176 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBadConsumeInput3), 0);
6177 QCBORDecode_VGetNextConsume(&DCtx, &Item1);
6178 if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT) {
6179 return 2800;
6180 }
Laurence Lundblade37286c02022-09-03 10:05:02 -07006181#endif
6182
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07006183
6184 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBadConsumeInput4), 0);
6185 QCBORDecode_VGetNextConsume(&DCtx, &Item1);
6186#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
6187 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) {
6188 return 2900;
6189 }
6190#else
6191 if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_INDEF_LEN_ARRAYS_DISABLED) {
6192 return 2901;
6193 }
6194#endif
6195
6196 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBadConsumeInput5), 0);
6197 QCBORDecode_VGetNextConsume(&DCtx, &Item1);
6198 if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_MAP_LABEL_TYPE) {
6199 return 3000;
6200 }
6201
Laurence Lundbladec5f45e42023-12-18 09:23:20 -07006202 nReturn = EnterMapCursorTest();
6203
Laurence Lundbladeddebabe2020-11-22 11:32:17 -08006204 return nReturn;
Laurence Lundblade9c905e82020-04-25 11:31:38 -07006205}
6206
6207
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006208struct NumberConversion {
6209 char *szDescription;
6210 UsefulBufC CBOR;
6211 int64_t nConvertedToInt64;
6212 QCBORError uErrorInt64;
6213 uint64_t uConvertToUInt64;
6214 QCBORError uErrorUint64;
6215 double dConvertToDouble;
6216 QCBORError uErrorDouble;
6217};
6218
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006219#ifndef QCBOR_DISABLE_EXP_AND_MANTISSA
6220#define EXP_AND_MANTISSA_ERROR(x) x
6221#else
6222#define EXP_AND_MANTISSA_ERROR(x) QCBOR_ERR_UNEXPECTED_TYPE
6223#endif
6224
6225
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07006226static const struct NumberConversion NumberConversions[] = {
Laurence Lundblade37286c02022-09-03 10:05:02 -07006227#ifndef QCBOR_DISABLE_TAGS
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006228 {
Laurence Lundblade4e808ba2022-12-29 12:45:20 -07006229 "Big float: INT64_MIN * 2e-1 to test handling of INT64_MIN",
6230 {(uint8_t[]){0xC5, 0x82, 0x20,
6231 0x3B, 0x7f, 0xff, 0xff, 0xff, 0xff, 0x0ff, 0xff, 0xff,
6232 }, 15},
6233 -4611686018427387904, /* INT64_MIN / 2 */
6234 EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS),
6235 0,
6236 EXP_AND_MANTISSA_ERROR(QCBOR_ERR_NUMBER_SIGN_CONVERSION),
6237 -4.6116860184273879E+18,
6238 FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS))
6239 },
6240 {
Laurence Lundblade784b54b2020-08-10 01:24:52 -07006241 "too large to fit into int64_t",
6242 {(uint8_t[]){0xc3, 0x48, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 10},
6243 0,
6244 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
6245 0,
6246 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
6247 ((double)INT64_MIN) + 1 ,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006248 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)
Laurence Lundblade784b54b2020-08-10 01:24:52 -07006249 },
6250 {
6251 "largest negative int that fits in int64_t",
6252 {(uint8_t[]){0xc3, 0x48, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, 10},
6253 INT64_MIN,
6254 QCBOR_SUCCESS,
6255 0,
6256 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
6257 (double)INT64_MIN,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006258 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)
Laurence Lundblade784b54b2020-08-10 01:24:52 -07006259 },
6260 {
Laurence Lundbladeda095972020-06-06 18:35:33 -07006261 "negative bignum -1",
6262 {(uint8_t[]){0xc3, 0x41, 0x00}, 3},
6263 -1,
6264 QCBOR_SUCCESS,
6265 0,
6266 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
6267 -1.0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006268 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)
Laurence Lundbladeda095972020-06-06 18:35:33 -07006269 },
6270 {
Laurence Lundblade9ab5abb2020-05-20 12:10:45 -07006271 "Decimal Fraction with positive bignum 257 * 10e3",
Laurence Lundblade887add82020-05-17 05:50:34 -07006272 {(uint8_t[]){0xC4, 0x82, 0x1B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03,
6273 0xC2, 0x42, 0x01, 0x01}, 15},
6274 257000,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006275 EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS),
Laurence Lundblade887add82020-05-17 05:50:34 -07006276 257000,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006277 EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS),
Laurence Lundblade887add82020-05-17 05:50:34 -07006278 257000.0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006279 FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS))
Laurence Lundblade887add82020-05-17 05:50:34 -07006280 },
6281 {
Laurence Lundbladeda095972020-06-06 18:35:33 -07006282 "bigfloat with negative bignum -258 * 2e3",
Laurence Lundblade887add82020-05-17 05:50:34 -07006283 {(uint8_t[]){0xC5, 0x82, 0x1B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03,
6284 0xC3, 0x42, 0x01, 0x01}, 15},
Laurence Lundbladeda095972020-06-06 18:35:33 -07006285 -2064,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006286 EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS),
Laurence Lundblade887add82020-05-17 05:50:34 -07006287 0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006288 EXP_AND_MANTISSA_ERROR(QCBOR_ERR_NUMBER_SIGN_CONVERSION),
Laurence Lundbladeda095972020-06-06 18:35:33 -07006289 -2064.0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006290 FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS))
Laurence Lundblade887add82020-05-17 05:50:34 -07006291 },
6292 {
Laurence Lundblade9ab5abb2020-05-20 12:10:45 -07006293 "bigfloat with positive bignum 257 * 2e3",
Laurence Lundblade887add82020-05-17 05:50:34 -07006294 {(uint8_t[]){0xC5, 0x82, 0x1B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03,
6295 0xC2, 0x42, 0x01, 0x01}, 15},
6296 2056,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006297 EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS),
Laurence Lundblade887add82020-05-17 05:50:34 -07006298 2056,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006299 EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS),
Laurence Lundblade887add82020-05-17 05:50:34 -07006300 2056.0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006301 FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS))
Laurence Lundblade887add82020-05-17 05:50:34 -07006302 },
6303 {
Laurence Lundbladeda095972020-06-06 18:35:33 -07006304 "negative bignum 0xc349010000000000000000 -18446744073709551617",
Laurence Lundblade887add82020-05-17 05:50:34 -07006305 {(uint8_t[]){0xc3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 11},
6306 0,
6307 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
6308 0,
6309 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
6310 -18446744073709551617.0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006311 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)
Laurence Lundblade887add82020-05-17 05:50:34 -07006312 },
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08006313#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
Laurence Lundblade887add82020-05-17 05:50:34 -07006314 {
Laurence Lundblade313b2862020-05-16 01:23:06 -07006315 "Positive bignum 0x01020304 indefinite length string",
6316 {(uint8_t[]){0xC2, 0x5f, 0x42, 0x01, 0x02, 0x41, 0x03, 0x41, 0x04, 0xff}, 10},
6317 0x01020304,
6318 QCBOR_SUCCESS,
6319 0x01020304,
6320 QCBOR_SUCCESS,
6321 16909060.0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006322 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)
Laurence Lundblade313b2862020-05-16 01:23:06 -07006323 },
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08006324#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
Laurence Lundblade313b2862020-05-16 01:23:06 -07006325 {
Laurence Lundblade887add82020-05-17 05:50:34 -07006326 "Decimal Fraction with neg bignum [9223372036854775807, -4759477275222530853137]",
Laurence Lundblade313b2862020-05-16 01:23:06 -07006327 {(uint8_t[]){0xC4, 0x82, 0x1B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
6328 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,}, 23},
6329 0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006330 EXP_AND_MANTISSA_ERROR(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW),
Laurence Lundblade313b2862020-05-16 01:23:06 -07006331 0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006332 EXP_AND_MANTISSA_ERROR(QCBOR_ERR_NUMBER_SIGN_CONVERSION),
Laurence Lundblade313b2862020-05-16 01:23:06 -07006333 -INFINITY,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006334 FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS))
Laurence Lundblade313b2862020-05-16 01:23:06 -07006335 },
6336 {
6337 "big float [9223372036854775806, 9223372036854775806]",
6338 {(uint8_t[]){0xC5, 0x82, 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE,
6339 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE}, 20},
6340 0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006341 EXP_AND_MANTISSA_ERROR(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW),
Laurence Lundblade313b2862020-05-16 01:23:06 -07006342 0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006343 EXP_AND_MANTISSA_ERROR(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW),
Laurence Lundblade313b2862020-05-16 01:23:06 -07006344 INFINITY,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006345 FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS))
Laurence Lundblade313b2862020-05-16 01:23:06 -07006346 },
6347 {
Laurence Lundblade983500d2020-05-14 11:49:34 -07006348 "Big float 3 * 2^^2",
6349 {(uint8_t[]){0xC5, 0x82, 0x02, 0x03}, 4},
6350 12,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006351 EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS),
Laurence Lundblade983500d2020-05-14 11:49:34 -07006352 12,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006353 EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS),
Laurence Lundblade983500d2020-05-14 11:49:34 -07006354 12.0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006355 FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS))
Laurence Lundblade983500d2020-05-14 11:49:34 -07006356 },
Laurence Lundblade983500d2020-05-14 11:49:34 -07006357 {
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006358 "Decimal fraction 3/10",
6359 {(uint8_t[]){0xC4, 0x82, 0x20, 0x03}, 4},
6360 0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006361 EXP_AND_MANTISSA_ERROR(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW),
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006362 0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006363 EXP_AND_MANTISSA_ERROR(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW),
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006364 0.30000000000000004,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006365 FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS))
Laurence Lundbladedfd49fc2020-09-01 14:17:16 -07006366 },
6367 {
Laurence Lundblade11fd78b2020-09-01 22:13:27 -07006368 "extreme pos bignum",
6369 {(uint8_t[]){0xc2, 0x59, 0x01, 0x90,
6370 // 50 rows of 8 is 400 digits.
6371 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6372 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6373 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6374 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6375 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6376 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6377 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6378 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6379 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6380 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6381 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6382 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6383 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6384 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6385 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6386 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6387 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6388 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6389 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6390 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6391 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6392 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6393 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6394 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6395 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6396 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6397 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6398 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6399 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6400 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6401 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6402 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6403 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6404 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6405 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6406 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6407 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6408 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6409 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6410 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6411 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6412 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6413 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6414 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6415 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6416 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6417 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6418 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6419 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
gtravisb787e82a2023-11-30 18:38:21 -08006420 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0},
Laurence Lundblade11fd78b2020-09-01 22:13:27 -07006421 404},
6422 0,
6423 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
6424 0,
6425 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
Laurence Lundblade11fd78b2020-09-01 22:13:27 -07006426 INFINITY,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006427 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS),
Laurence Lundblade11fd78b2020-09-01 22:13:27 -07006428 },
6429
6430 {
6431 "extreme neg bignum",
6432 {(uint8_t[]){0xc3, 0x59, 0x01, 0x90,
6433 // 50 rows of 8 is 400 digits.
6434 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6435 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6436 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6437 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6438 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6439 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6440 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6441 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6442 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6443 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6444 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6445 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6446 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6447 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6448 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6449 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6450 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6451 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6452 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6453 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6454 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6455 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6456 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6457 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6458 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6459 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6460 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6461 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6462 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6463 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6464 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6465 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6466 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6467 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6468 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6469 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6470 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6471 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6472 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6473 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6474 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6475 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6476 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6477 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6478 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6479 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6480 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6481 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6482 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
gtravisb787e82a2023-11-30 18:38:21 -08006483 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0},
Laurence Lundblade11fd78b2020-09-01 22:13:27 -07006484 404},
6485 0,
6486 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
6487 0,
6488 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
Laurence Lundblade11fd78b2020-09-01 22:13:27 -07006489 -INFINITY,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006490 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)
Laurence Lundblade11fd78b2020-09-01 22:13:27 -07006491 },
Laurence Lundblade51722fd2020-09-02 13:01:33 -07006492
6493 {
6494 "big float underflow [9223372036854775806, -9223372036854775806]",
6495 {(uint8_t[]){
6496 0xC5, 0x82,
6497 0x3B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE,
6498 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE}, 20},
Laurence Lundblade51722fd2020-09-02 13:01:33 -07006499 0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006500 EXP_AND_MANTISSA_ERROR(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW),
Laurence Lundblade51722fd2020-09-02 13:01:33 -07006501 0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006502 EXP_AND_MANTISSA_ERROR(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW),
Laurence Lundblade51722fd2020-09-02 13:01:33 -07006503 0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006504 FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS))
Laurence Lundblade51722fd2020-09-02 13:01:33 -07006505 },
6506
6507 {
6508 "bigfloat that evaluates to -INFINITY",
6509 {(uint8_t[]){
6510 0xC5, 0x82,
6511 0x1B, 0x70, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03,
6512 0xC3, 0x42, 0x01, 0x01}, 15},
Laurence Lundblade51722fd2020-09-02 13:01:33 -07006513 0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006514 EXP_AND_MANTISSA_ERROR(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW),
Laurence Lundblade51722fd2020-09-02 13:01:33 -07006515 0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006516 EXP_AND_MANTISSA_ERROR(QCBOR_ERR_NUMBER_SIGN_CONVERSION),
Laurence Lundblade51722fd2020-09-02 13:01:33 -07006517 -INFINITY,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006518 FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS))
Laurence Lundblade51722fd2020-09-02 13:01:33 -07006519 },
Laurence Lundblade37286c02022-09-03 10:05:02 -07006520 {
6521 "Positive bignum 0xffff",
6522 {(uint8_t[]){0xC2, 0x42, 0xff, 0xff}, 4},
6523 65536-1,
6524 QCBOR_SUCCESS,
6525 0xffff,
6526 QCBOR_SUCCESS,
6527 65535.0,
6528 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)
6529 },
6530#endif /* QCBOR_DISABLE_TAGS */
6531 {
6532 "Positive integer 18446744073709551615",
6533 {(uint8_t[]){0x1b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, 9},
6534 0,
6535 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
6536 18446744073709551615ULL,
6537 QCBOR_SUCCESS,
6538 18446744073709551615.0,
6539 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)
6540 },
6541
6542 {
6543 "Postive integer 0",
6544 {(uint8_t[]){0x0}, 1},
6545 0LL,
6546 QCBOR_SUCCESS,
6547 0ULL,
6548 QCBOR_SUCCESS,
6549 0.0,
6550 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)
6551 },
6552 {
Laurence Lundblade2d493002024-02-01 11:09:17 -07006553 "Negative integer -9223372036854775808",
6554 {(uint8_t[]){0x3b, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, 9},
Laurence Lundblade37286c02022-09-03 10:05:02 -07006555 -9223372036854775807-1, // INT64_MIN
6556 QCBOR_SUCCESS,
6557 0ULL,
6558 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
6559 -9223372036854775808.0,
6560 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)
6561 },
6562 {
Laurence Lundblade2d493002024-02-01 11:09:17 -07006563 "Negative integer -18446744073709551616",
6564 {(uint8_t[]){0x3b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, 9},
6565 0ULL,
6566 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
6567 0ULL,
6568 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
6569 -18446744073709551616.0,
6570 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)
6571 },
6572 {
Laurence Lundblade37286c02022-09-03 10:05:02 -07006573 "Double Floating point value 100.3",
6574 {(uint8_t[]){0xfb, 0x40, 0x59, 0x13, 0x33, 0x33, 0x33, 0x33, 0x33}, 9},
6575 100L,
6576 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS),
6577 100ULL,
6578 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS),
6579 100.3,
6580 FLOAT_ERR_CODE_NO_FLOAT(QCBOR_SUCCESS),
6581 },
6582 {
6583 "Floating point value NaN 0xfa7fc00000",
6584 {(uint8_t[]){0xfa, 0x7f, 0xc0, 0x00, 0x00}, 5},
6585 0,
6586 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_FLOAT_EXCEPTION),
6587 0,
6588 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_FLOAT_EXCEPTION),
6589 NAN,
6590 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS),
6591 },
6592 {
6593 "half-precision Floating point value -4",
6594 {(uint8_t[]){0xf9, 0xc4, 0x00}, 3},
6595 // Normal case with all enabled.
6596 -4,
6597 FLOAT_ERR_CODE_NO_HALF_PREC_NO_FLOAT_HW(QCBOR_SUCCESS),
6598 0,
6599 FLOAT_ERR_CODE_NO_HALF_PREC_NO_FLOAT_HW(QCBOR_ERR_NUMBER_SIGN_CONVERSION),
6600 -4.0,
6601 FLOAT_ERR_CODE_NO_HALF_PREC(QCBOR_SUCCESS)
6602 },
6603 {
6604 "+inifinity single precision",
6605 {(uint8_t[]){0xfa, 0x7f, 0x80, 0x00, 0x00}, 5},
6606 0,
6607 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_FLOAT_EXCEPTION),
6608 0,
6609 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW),
6610 INFINITY,
6611 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)
6612 },
6613
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006614};
Laurence Lundblade9c905e82020-04-25 11:31:38 -07006615
6616
6617
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08006618
6619static int32_t SetUpDecoder(QCBORDecodeContext *DCtx, UsefulBufC CBOR, UsefulBuf Pool)
6620{
6621 QCBORDecode_Init(DCtx, CBOR, QCBOR_DECODE_MODE_NORMAL);
6622#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
6623 if(QCBORDecode_SetMemPool(DCtx, Pool, 0)) {
6624 return 1;
6625 }
6626#else /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
6627 (void)Pool;
6628#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
6629 return 0;
6630}
6631
6632
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03006633int32_t IntegerConvertTest(void)
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006634{
Laurence Lundbladeeb3cdef2024-02-17 20:38:55 -08006635 uint64_t uInt;
6636
6637
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08006638 const int nNumTests = C_ARRAY_COUNT(NumberConversions,
6639 struct NumberConversion);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006640
Laurence Lundblade9ab5abb2020-05-20 12:10:45 -07006641 for(int nIndex = 0; nIndex < nNumTests; nIndex++) {
6642 const struct NumberConversion *pF = &NumberConversions[nIndex];
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07006643
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006644 // Set up the decoding context including a memory pool so that
6645 // indefinite length items can be checked
6646 QCBORDecodeContext DCtx;
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006647 UsefulBuf_MAKE_STACK_UB(Pool, 100);
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07006648
6649 /* ----- test conversion to int64_t ------ */
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08006650 if(SetUpDecoder(&DCtx, pF->CBOR, Pool)) {
6651 return (int32_t)(3333+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006652 }
6653
6654 int64_t nInt;
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006655 QCBORDecode_GetInt64ConvertAll(&DCtx, 0xffff, &nInt);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07006656 if(QCBORDecode_GetError(&DCtx) != pF->uErrorInt64) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07006657 return (int32_t)(2000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006658 }
6659 if(pF->uErrorInt64 == QCBOR_SUCCESS && pF->nConvertedToInt64 != nInt) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07006660 return (int32_t)(3000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006661 }
6662
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07006663 /* ----- test conversion to uint64_t ------ */
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08006664 if(SetUpDecoder(&DCtx, pF->CBOR, Pool)) {
6665 return (int32_t)(3333+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006666 }
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08006667
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006668 QCBORDecode_GetUInt64ConvertAll(&DCtx, 0xffff, &uInt);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07006669 if(QCBORDecode_GetError(&DCtx) != pF->uErrorUint64) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07006670 return (int32_t)(4000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006671 }
6672 if(pF->uErrorUint64 == QCBOR_SUCCESS && pF->uConvertToUInt64 != uInt) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07006673 return (int32_t)(5000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006674 }
6675
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07006676 /* ----- test conversion to double ------ */
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08006677 if(SetUpDecoder(&DCtx, pF->CBOR, Pool)) {
6678 return (int32_t)(3333+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006679 }
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006680
6681#ifndef USEFULBUF_DISABLE_ALL_FLOAT
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006682 double d;
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006683 QCBORDecode_GetDoubleConvertAll(&DCtx, 0xffff, &d);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07006684 if(QCBORDecode_GetError(&DCtx) != pF->uErrorDouble) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07006685 return (int32_t)(6000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006686 }
6687 if(pF->uErrorDouble == QCBOR_SUCCESS) {
6688 if(isnan(pF->dConvertToDouble)) {
Laurence Lundblade983500d2020-05-14 11:49:34 -07006689 // NaN's can't be compared for equality. A NaN is
6690 // never equal to anything including another NaN
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006691 if(!isnan(d)) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07006692 return (int32_t)(7000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006693 }
6694 } else {
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006695 if(pF->dConvertToDouble != d) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07006696 return (int32_t)(8000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006697 }
6698 }
6699 }
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006700#endif /* USEFULBUF_DISABLE_ALL_FLOAT */
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006701 }
6702
6703 return 0;
6704}
6705
Laurence Lundbladea8758502022-05-15 17:57:46 -07006706#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
6707
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03006708int32_t CBORTestIssue134(void)
David Navarro9123e5b2022-03-28 16:04:03 +02006709{
6710 QCBORDecodeContext DCtx;
6711 QCBORItem Item;
6712 QCBORError uCBORError;
6713 const uint8_t spTestIssue134[] = { 0x5F, 0x40, 0xFF };
Laurence Lundblade9c905e82020-04-25 11:31:38 -07006714
David Navarro9123e5b2022-03-28 16:04:03 +02006715 QCBORDecode_Init(&DCtx,
6716 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTestIssue134),
6717 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade97c61bf2020-05-02 11:24:06 -07006718
David Navarro9123e5b2022-03-28 16:04:03 +02006719 UsefulBuf_MAKE_STACK_UB(StringBuf, 200);
6720 QCBORDecode_SetMemPool(&DCtx, StringBuf, false);
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03006721
David Navarro9123e5b2022-03-28 16:04:03 +02006722 do {
6723 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
6724 } while (QCBOR_SUCCESS == uCBORError);
6725
6726 uCBORError = QCBORDecode_Finish(&DCtx);
6727
Laurence Lundblade11ea3612022-07-01 13:26:23 -07006728 return (int32_t)uCBORError;
David Navarro9123e5b2022-03-28 16:04:03 +02006729}
Laurence Lundblade97c61bf2020-05-02 11:24:06 -07006730
Laurence Lundbladea8758502022-05-15 17:57:46 -07006731#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
6732
Laurence Lundblade37286c02022-09-03 10:05:02 -07006733
6734
6735static const uint8_t spSequenceTestInput[] = {
6736 /* 1. The valid date string "1985-04-12" */
6737 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string
6738
6739 /* 2. */
6740 0x00,
6741
6742 /* 3. A valid epoch date, 1400000000; Tue, 13 May 2014 16:53:20 GMT */
6743 0x1a, 0x53, 0x72, 0x4E, 0x00,
6744
6745 /* 4. */
6746 0x62, 'h', 'i',
6747};
6748
6749
Laurence Lundbladee3553422020-05-02 11:11:17 -07006750int32_t CBORSequenceDecodeTests(void)
6751{
6752 QCBORDecodeContext DCtx;
Laurence Lundblade87495732021-02-26 10:05:55 -07006753 QCBORItem Item;
6754 QCBORError uCBORError;
6755 size_t uConsumed;
Laurence Lundbladee3553422020-05-02 11:11:17 -07006756
6757 // --- Test a sequence with extra bytes ---
Laurence Lundblade9b334962020-08-27 10:55:53 -07006758
Laurence Lundbladee3553422020-05-02 11:11:17 -07006759 QCBORDecode_Init(&DCtx,
Laurence Lundblade37286c02022-09-03 10:05:02 -07006760 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSequenceTestInput),
Laurence Lundbladee3553422020-05-02 11:11:17 -07006761 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade9b334962020-08-27 10:55:53 -07006762
Laurence Lundblade37286c02022-09-03 10:05:02 -07006763 // Get 1.
Laurence Lundbladee3553422020-05-02 11:11:17 -07006764 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
6765 if(uCBORError != QCBOR_SUCCESS) {
6766 return 1;
6767 }
Laurence Lundblade37286c02022-09-03 10:05:02 -07006768 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ) {
Laurence Lundbladee3553422020-05-02 11:11:17 -07006769 return 2;
6770 }
Laurence Lundbladec7114722020-08-13 05:11:40 -07006771
Laurence Lundblade87495732021-02-26 10:05:55 -07006772 uCBORError = QCBORDecode_PartialFinish(&DCtx, &uConsumed);
6773 if(uCBORError != QCBOR_ERR_EXTRA_BYTES ||
Laurence Lundblade37286c02022-09-03 10:05:02 -07006774 uConsumed != 11) {
6775 return 102;
6776 }
6777
6778 // Get 2.
6779 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
6780 if(uCBORError != QCBOR_SUCCESS) {
6781 return 66;
6782 }
6783
6784 uCBORError = QCBORDecode_PartialFinish(&DCtx, &uConsumed);
6785 if(uCBORError != QCBOR_ERR_EXTRA_BYTES ||
Laurence Lundblade87495732021-02-26 10:05:55 -07006786 uConsumed != 12) {
6787 return 102;
6788 }
6789
Laurence Lundblade37286c02022-09-03 10:05:02 -07006790 // Get 3.
Laurence Lundbladec7114722020-08-13 05:11:40 -07006791 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundbladee3553422020-05-02 11:11:17 -07006792 if(uCBORError != QCBOR_SUCCESS) {
6793 return 2;
6794 }
Laurence Lundblade37286c02022-09-03 10:05:02 -07006795 if(Item.uDataType != QCBOR_TYPE_INT64) {
Laurence Lundbladee3553422020-05-02 11:11:17 -07006796 return 3;
6797 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006798
Laurence Lundbladee3553422020-05-02 11:11:17 -07006799 // A sequence can have stuff at the end that may
6800 // or may not be valid CBOR. The protocol decoder knows
6801 // when to stop by definition of the protocol, not
6802 // when the top-level map or array is ended.
6803 // Finish still has to be called to know that
6804 // maps and arrays (if there were any) were closed
6805 // off correctly. When called like this it
6806 // must return the error QCBOR_ERR_EXTRA_BYTES.
6807 uCBORError = QCBORDecode_Finish(&DCtx);
6808 if(uCBORError != QCBOR_ERR_EXTRA_BYTES) {
6809 return 4;
6810 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006811
Laurence Lundbladee3553422020-05-02 11:11:17 -07006812 // --- Test an empty input ----
6813 uint8_t empty[1];
6814 UsefulBufC Empty = {empty, 0};
6815 QCBORDecode_Init(&DCtx,
6816 Empty,
6817 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade9b334962020-08-27 10:55:53 -07006818
Laurence Lundbladee3553422020-05-02 11:11:17 -07006819 uCBORError = QCBORDecode_Finish(&DCtx);
6820 if(uCBORError != QCBOR_SUCCESS) {
6821 return 5;
6822 }
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07006823
6824
Laurence Lundbladee3553422020-05-02 11:11:17 -07006825 // --- Sequence with unclosed indefinite length array ---
6826 static const uint8_t xx[] = {0x01, 0x9f, 0x02};
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07006827
Laurence Lundbladee3553422020-05-02 11:11:17 -07006828 QCBORDecode_Init(&DCtx,
6829 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(xx),
6830 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07006831
Laurence Lundbladee3553422020-05-02 11:11:17 -07006832 // Get the first item
6833 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
6834 if(uCBORError != QCBOR_SUCCESS) {
6835 return 7;
6836 }
6837 if(Item.uDataType != QCBOR_TYPE_INT64) {
6838 return 8;
6839 }
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07006840
Laurence Lundbladee3553422020-05-02 11:11:17 -07006841 // Get a second item
6842 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08006843#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundbladee3553422020-05-02 11:11:17 -07006844 if(uCBORError != QCBOR_SUCCESS) {
6845 return 9;
6846 }
6847 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
6848 return 10;
6849 }
6850
6851 // Try to finish before consuming all bytes to confirm
6852 // that the still-open error is returned.
6853 uCBORError = QCBORDecode_Finish(&DCtx);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006854 if(uCBORError != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
Laurence Lundbladee3553422020-05-02 11:11:17 -07006855 return 11;
6856 }
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08006857#else /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
6858 if(uCBORError != QCBOR_ERR_INDEF_LEN_ARRAYS_DISABLED) {
6859 return 20;
6860 }
6861#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
Laurence Lundbladee3553422020-05-02 11:11:17 -07006862
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07006863
Laurence Lundbladee3553422020-05-02 11:11:17 -07006864 // --- Sequence with a closed indefinite length array ---
6865 static const uint8_t yy[] = {0x01, 0x9f, 0xff};
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07006866
Laurence Lundbladee3553422020-05-02 11:11:17 -07006867 QCBORDecode_Init(&DCtx,
6868 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(yy),
6869 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07006870
Laurence Lundbladee3553422020-05-02 11:11:17 -07006871 // Get the first item
6872 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
6873 if(uCBORError != QCBOR_SUCCESS) {
6874 return 12;
6875 }
6876 if(Item.uDataType != QCBOR_TYPE_INT64) {
6877 return 13;
6878 }
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07006879
Laurence Lundbladee3553422020-05-02 11:11:17 -07006880 // Get a second item
6881 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08006882#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
6883
Laurence Lundbladee3553422020-05-02 11:11:17 -07006884 if(uCBORError != QCBOR_SUCCESS) {
6885 return 14;
6886 }
6887 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
6888 return 15;
6889 }
6890
6891 // Try to finish before consuming all bytes to confirm
6892 // that the still-open error is returned.
6893 uCBORError = QCBORDecode_Finish(&DCtx);
6894 if(uCBORError != QCBOR_SUCCESS) {
6895 return 16;
6896 }
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08006897#else /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
6898 if(uCBORError != QCBOR_ERR_INDEF_LEN_ARRAYS_DISABLED) {
6899 return 20;
6900 }
6901#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
Laurence Lundbladee3553422020-05-02 11:11:17 -07006902
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07006903
Laurence Lundbladee3553422020-05-02 11:11:17 -07006904 return 0;
6905}
6906
Laurence Lundbladee15326f2020-06-15 15:50:23 -07006907
Laurence Lundblade70ecead2020-06-15 19:40:06 -07006908
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03006909int32_t IntToTests(void)
Laurence Lundbladee15326f2020-06-15 15:50:23 -07006910{
6911 int nErrCode;
6912 int32_t n32;
6913 int16_t n16;
6914 int8_t n8;
6915 uint32_t u32;
6916 uint16_t u16;
6917 uint8_t u8;
6918 uint64_t u64;
6919
6920 nErrCode = QCBOR_Int64ToInt32(1, &n32);
6921 if(nErrCode == -1 || n32 != 1) {
6922 return 1;
6923 }
6924
6925 nErrCode = QCBOR_Int64ToInt32((int64_t)INT32_MAX, &n32);
6926 if(nErrCode == -1 || n32 != INT32_MAX) {
6927 return 2;
6928 }
6929
6930 nErrCode = QCBOR_Int64ToInt32((int64_t)INT32_MIN, &n32);
6931 if(nErrCode == -1 || n32 != INT32_MIN) {
6932 return 3;
6933 }
6934
6935 nErrCode = QCBOR_Int64ToInt32(((int64_t)INT32_MAX)+1, &n32);
6936 if(nErrCode != -1) {
6937 return 4;
6938 }
6939
6940 nErrCode = QCBOR_Int64ToInt32(((int64_t)INT32_MIN)-1, &n32);
6941 if(nErrCode != -1) {
6942 return 5;
6943 }
6944
6945
6946 nErrCode = QCBOR_Int64ToInt16((int64_t)INT16_MAX, &n16);
6947 if(nErrCode == -1 || n16 != INT16_MAX) {
6948 return 6;
6949 }
6950
6951 nErrCode = QCBOR_Int64ToInt16((int64_t)INT16_MIN, &n16);
6952 if(nErrCode == -1 || n16 != INT16_MIN) {
6953 return 7;
6954 }
6955
6956 nErrCode = QCBOR_Int64ToInt16(1, &n16);
6957 if(nErrCode == -1 || n16 != 1) {
6958 return 8;
6959 }
6960
6961 nErrCode = QCBOR_Int64ToInt16(((int64_t)INT16_MAX)+1, &n16);
6962 if(nErrCode != -1) {
6963 return 9;
6964 }
6965
6966 nErrCode = QCBOR_Int64ToInt16(((int64_t)INT16_MIN)-1, &n16);
6967 if(nErrCode != -1) {
6968 return 10;
6969 }
6970
6971
6972 nErrCode = QCBOR_Int64ToInt8(1, &n8);
6973 if(nErrCode == -1 || n8 != 1) {
6974 return 11;
6975 }
6976
6977 nErrCode = QCBOR_Int64ToInt8((int64_t)INT8_MAX, &n8);
6978 if(nErrCode == -1 || n8 != INT8_MAX) {
6979 return 12;
6980 }
6981
6982 nErrCode = QCBOR_Int64ToInt8((int64_t)INT8_MIN, &n8);
6983 if(nErrCode == -1 || n8 != INT8_MIN) {
6984 return 13;
6985 }
6986
6987 nErrCode = QCBOR_Int64ToInt8(((int64_t)INT8_MAX)+1, &n8);
6988 if(nErrCode != -1) {
6989 return 14;
6990 }
6991
6992 nErrCode = QCBOR_Int64ToInt8(((int64_t)INT8_MIN)-1, &n8);
6993 if(nErrCode != -1) {
6994 return 15;
6995 }
6996
6997
6998 nErrCode = QCBOR_Int64ToUInt32(1, &u32);
6999 if(nErrCode == -1 || u32 != 1) {
7000 return 16;
7001 }
7002
7003 nErrCode = QCBOR_Int64ToUInt32((int64_t)UINT32_MAX, &u32);
7004 if(nErrCode == -1 || u32 != UINT32_MAX) {
7005 return 17;
7006 }
7007
7008 nErrCode = QCBOR_Int64ToUInt32((int64_t)0, &u32);
7009 if(nErrCode == -1 || u32 != 0) {
7010 return 18;
7011 }
7012
7013 nErrCode = QCBOR_Int64ToUInt32(((int64_t)UINT32_MAX)+1, &u32);
7014 if(nErrCode != -1) {
7015 return 19;
7016 }
7017
7018 nErrCode = QCBOR_Int64ToUInt32((int64_t)-1, &u32);
7019 if(nErrCode != -1) {
7020 return 20;
7021 }
7022
7023
7024 nErrCode = QCBOR_Int64UToInt16((int64_t)UINT16_MAX, &u16);
7025 if(nErrCode == -1 || u16 != UINT16_MAX) {
7026 return 21;
7027 }
7028
7029 nErrCode = QCBOR_Int64UToInt16((int64_t)0, &u16);
7030 if(nErrCode == -1 || u16 != 0) {
7031 return 22;
7032 }
7033
7034 nErrCode = QCBOR_Int64UToInt16(1, &u16);
7035 if(nErrCode == -1 || u16 != 1) {
7036 return 23;
7037 }
7038
7039 nErrCode = QCBOR_Int64UToInt16(((int64_t)UINT16_MAX)+1, &u16);
7040 if(nErrCode != -1) {
7041 return 24;
7042 }
7043
7044 nErrCode = QCBOR_Int64UToInt16((int64_t)-1, &u16);
7045 if(nErrCode != -1) {
7046 return 25;
7047 }
7048
7049
7050 nErrCode = QCBOR_Int64ToUInt8((int64_t)UINT8_MAX, &u8);
7051 if(nErrCode == -1 || u8 != UINT8_MAX) {
7052 return 26;
7053 }
7054
7055 nErrCode = QCBOR_Int64ToUInt8((int64_t)0, &u8);
7056 if(nErrCode == -1 || u8 != 0) {
7057 return 27;
7058 }
7059
7060 nErrCode = QCBOR_Int64ToUInt8(1, &u8);
7061 if(nErrCode == -1 || u8 != 1) {
7062 return 28;
7063 }
7064
7065 nErrCode = QCBOR_Int64ToUInt8(((int64_t)UINT16_MAX)+1, &u8);
7066 if(nErrCode != -1) {
7067 return 29;
7068 }
7069
7070 nErrCode = QCBOR_Int64ToUInt8((int64_t)-1, &u8);
7071 if(nErrCode != -1) {
7072 return 30;
7073 }
7074
7075
7076 nErrCode = QCBOR_Int64ToUInt64(1, &u64);
7077 if(nErrCode == -1 || u64 != 1) {
7078 return 31;
7079 }
7080
7081 nErrCode = QCBOR_Int64ToUInt64(INT64_MAX, &u64);
7082 if(nErrCode == -1 || u64 != INT64_MAX) {
7083 return 32;
7084 }
7085
7086 nErrCode = QCBOR_Int64ToUInt64((int64_t)0, &u64);
7087 if(nErrCode == -1 || u64 != 0) {
7088 return 33;
7089 }
7090
7091 nErrCode = QCBOR_Int64ToUInt64((int64_t)-1, &u64);
7092 if(nErrCode != -1) {
7093 return 34;
7094 }
7095
7096 return 0;
7097}
7098
Laurence Lundblade0750fc42020-06-20 21:02:34 -07007099
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07007100
7101
Laurence Lundblade0750fc42020-06-20 21:02:34 -07007102/*
7103A sequence with
7104 A wrapping bstr
7105 containing a map
7106 1
7107 2
7108 A wrapping bstr
7109 containing an array
7110 3
7111 wrapping bstr
7112 4
7113 5
7114 6
7115 array
7116 7
7117 8
Laurence Lundblade0750fc42020-06-20 21:02:34 -07007118 */
7119
Laurence Lundblade55013642020-09-23 05:39:22 -07007120static UsefulBufC EncodeBstrWrapTestData(UsefulBuf OutputBuffer)
Laurence Lundblade0750fc42020-06-20 21:02:34 -07007121{
Laurence Lundblade55013642020-09-23 05:39:22 -07007122 UsefulBufC Encoded;
Laurence Lundblade0750fc42020-06-20 21:02:34 -07007123 QCBOREncodeContext EC;
Laurence Lundblade55013642020-09-23 05:39:22 -07007124 QCBORError uErr;
Laurence Lundblade0750fc42020-06-20 21:02:34 -07007125
Laurence Lundblade55013642020-09-23 05:39:22 -07007126 QCBOREncode_Init(&EC, OutputBuffer);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07007127
7128 QCBOREncode_BstrWrap(&EC);
7129 QCBOREncode_OpenMap(&EC);
7130 QCBOREncode_AddInt64ToMapN(&EC, 100, 1);
7131 QCBOREncode_AddInt64ToMapN(&EC, 200, 2);
7132 QCBOREncode_CloseMap(&EC);
7133 QCBOREncode_BstrWrap(&EC);
7134 QCBOREncode_OpenArray(&EC);
7135 QCBOREncode_AddInt64(&EC, 3);
7136 QCBOREncode_BstrWrap(&EC);
7137 QCBOREncode_AddInt64(&EC, 4);
7138 QCBOREncode_CloseBstrWrap(&EC, NULL);
7139 QCBOREncode_AddInt64(&EC, 5);
7140 QCBOREncode_CloseArray(&EC);
7141 QCBOREncode_CloseBstrWrap(&EC, NULL);
7142 QCBOREncode_AddInt64(&EC, 6);
7143 QCBOREncode_CloseBstrWrap(&EC, NULL);
7144 QCBOREncode_OpenArray(&EC);
7145 QCBOREncode_AddInt64(&EC, 7);
7146 QCBOREncode_AddInt64(&EC, 8);
7147 QCBOREncode_CloseArray(&EC);
7148
7149 uErr = QCBOREncode_Finish(&EC, &Encoded);
Laurence Lundblade40a04322020-06-27 22:52:52 -07007150 if(uErr) {
7151 Encoded = NULLUsefulBufC;
7152 }
Laurence Lundblade0750fc42020-06-20 21:02:34 -07007153
7154 return Encoded;
7155}
7156
Laurence Lundbladecc7da412020-12-27 00:09:07 -08007157/* h'FF' */
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08007158static const uint8_t spBreakInByteString[] = {
7159 0x41, 0xff
7160};
7161
Laurence Lundblade0750fc42020-06-20 21:02:34 -07007162
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03007163int32_t EnterBstrTest(void)
Laurence Lundblade0750fc42020-06-20 21:02:34 -07007164{
Laurence Lundblade8510f8c2020-12-01 11:31:16 -08007165 UsefulBuf_MAKE_STACK_UB(OutputBuffer, 100);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07007166
7167 QCBORDecodeContext DC;
7168
Laurence Lundblade55013642020-09-23 05:39:22 -07007169 QCBORDecode_Init(&DC, EncodeBstrWrapTestData(OutputBuffer), 0);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07007170
Laurence Lundblade55013642020-09-23 05:39:22 -07007171 int64_t n1, n2, n3, n4, n5, n6, n7, n8;
Laurence Lundblade0750fc42020-06-20 21:02:34 -07007172
7173
Laurence Lundblade9b334962020-08-27 10:55:53 -07007174 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07007175 QCBORDecode_EnterMap(&DC, NULL);
Laurence Lundblade55013642020-09-23 05:39:22 -07007176 QCBORDecode_GetInt64InMapN(&DC, 100, &n1);
7177 QCBORDecode_GetInt64InMapN(&DC, 200, &n2);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07007178 QCBORDecode_ExitMap(&DC);
Laurence Lundblade9b334962020-08-27 10:55:53 -07007179 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07007180 QCBORDecode_EnterArray(&DC, NULL);
Laurence Lundblade55013642020-09-23 05:39:22 -07007181 QCBORDecode_GetInt64(&DC, &n3);
Laurence Lundblade9b334962020-08-27 10:55:53 -07007182 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
Laurence Lundblade55013642020-09-23 05:39:22 -07007183 QCBORDecode_GetInt64(&DC, &n4);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07007184 QCBORDecode_ExitBstrWrapped(&DC);
Laurence Lundblade55013642020-09-23 05:39:22 -07007185 QCBORDecode_GetInt64(&DC, &n5);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07007186 QCBORDecode_ExitArray(&DC);
7187 QCBORDecode_ExitBstrWrapped(&DC);
Laurence Lundblade55013642020-09-23 05:39:22 -07007188 QCBORDecode_GetInt64(&DC, &n6);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07007189 QCBORDecode_ExitBstrWrapped(&DC);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07007190 QCBORDecode_EnterArray(&DC, NULL);
Laurence Lundblade55013642020-09-23 05:39:22 -07007191 QCBORDecode_GetInt64(&DC, &n7);
7192 QCBORDecode_GetInt64(&DC, &n8);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07007193 QCBORDecode_ExitArray(&DC);
7194
7195 QCBORError uErr = QCBORDecode_Finish(&DC);
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08007196 if(uErr) {
7197 return (int32_t)uErr;
7198 }
Laurence Lundblade0750fc42020-06-20 21:02:34 -07007199
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08007200
7201 /* Enter and exit byte string wrapped CBOR that is bad. It has just a break.
7202 * Successful because no items are fetched from byte string.
7203 */
7204 QCBORDecode_Init(&DC,
7205 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBreakInByteString),
7206 0);
7207 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
7208 uErr = QCBORDecode_GetError(&DC);
7209 if(uErr) {
7210 return 100 + (int32_t)uErr;
7211 }
7212
7213 QCBORDecode_ExitBstrWrapped(&DC);
7214 uErr = QCBORDecode_GetError(&DC);
7215 if(uErr) {
7216 return 200 + (int32_t)uErr;
7217 }
7218
7219 /* Try to get item that is a break out of a byte string wrapped CBOR.
7220 * It fails because there should be no break.
7221 */
7222 QCBORDecode_Init(&DC,
7223 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBreakInByteString),
7224 0);
7225 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
7226 QCBORItem Item;
7227 uErr = QCBORDecode_GetNext(&DC, &Item);
7228 if(uErr != QCBOR_ERR_BAD_BREAK) {
7229 return 300 + (int32_t)uErr;
7230 }
7231
7232 return 0;
Laurence Lundblade0750fc42020-06-20 21:02:34 -07007233}
Laurence Lundblade37f46e52020-08-04 03:32:14 -07007234
7235
7236
7237
7238static const uint8_t spTaggedTypes[] = {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007239 0xb2,
Laurence Lundblade37f46e52020-08-04 03:32:14 -07007240
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07007241 // Date string
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007242 0x00,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07007243 0xc0, 0x74, 0x32, 0x30, 0x30, 0x33, 0x2D, 0x31, 0x32, 0x2D,
7244 0x31, 0x33, 0x54, 0x31, 0x38, 0x3A, 0x33, 0x30, 0x3A, 0x30,
7245 0x32, 0x5A,
Laurence Lundblade37f46e52020-08-04 03:32:14 -07007246
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007247 0x01,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07007248 0x74, 0x32, 0x30, 0x30, 0x33, 0x2D, 0x31, 0x32, 0x2D, 0x31,
7249 0x33, 0x54, 0x31, 0x38, 0x3A, 0x33, 0x30, 0x3A, 0x30, 0x32,
7250 0x5A,
Laurence Lundblade37f46e52020-08-04 03:32:14 -07007251
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07007252 // Bignum
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007253 10,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07007254 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
7255 0x09, 0x10,
Laurence Lundblade37f46e52020-08-04 03:32:14 -07007256
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007257 11,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07007258 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
7259 0x09, 0x10,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007260
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07007261 // URL
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007262 20,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07007263 0xd8, 0x20, 0x6f, 0x68, 0x74, 0x74, 0x70, 0x3A, 0x2F, 0x2F,
7264 0x63, 0x62, 0x6F, 0x72, 0x2E, 0x6D, 0x65, 0x2F,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007265
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07007266 21,
7267 0x6f, 0x68, 0x74, 0x74, 0x70, 0x3A, 0x2F, 0x2F, 0x63, 0x62,
7268 0x6F, 0x72, 0x2E, 0x6D, 0x65, 0x2F,
7269
7270 // B64
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007271 0x18, 0x1e,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07007272 0xd8, 0x22, 0x6c, 0x63, 0x47, 0x78, 0x6C, 0x59, 0x58, 0x4E,
7273 0x31, 0x63, 0x6D, 0x55, 0x75,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007274
7275 0x18, 0x1f,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07007276 0x6c, 0x63, 0x47, 0x78, 0x6C, 0x59, 0x58, 0x4E, 0x31, 0x63,
7277 0x6D, 0x55, 0x75,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007278
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07007279 // B64URL
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007280 0x18, 0x28,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07007281 0xd8, 0x21, 0x6c, 0x63, 0x47, 0x78, 0x6C, 0x59, 0x58, 0x4E,
7282 0x31, 0x63, 0x6D, 0x55, 0x75,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007283
7284 0x18, 0x29,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07007285 0x6c, 0x63, 0x47, 0x78, 0x6C, 0x59, 0x58, 0x4E, 0x31, 0x63,
7286 0x6D, 0x55, 0x75,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007287
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07007288 // Regex
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007289 0x18, 0x32,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07007290 0xd8, 0x23, 0x68, 0x31, 0x30, 0x30, 0x5C, 0x73, 0x2A, 0x6D,
7291 0x6B,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007292
7293 0x18, 0x33,
7294 0x68, 0x31, 0x30, 0x30, 0x5C, 0x73, 0x2A, 0x6D, 0x6B,
7295
7296 // MIME
7297 0x18, 0x3c,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07007298 0xd8, 0x24, 0x72, 0x4D, 0x49, 0x4D, 0x45, 0x2D, 0x56, 0x65,
7299 0x72, 0x73, 0x69, 0x6F, 0x6E, 0x3A, 0x20, 0x31, 0x2E, 0x30,
7300 0x0A,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007301
7302 0x18, 0x3d,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07007303 0x72, 0x4D, 0x49, 0x4D, 0x45, 0x2D, 0x56, 0x65, 0x72, 0x73,
7304 0x69, 0x6F, 0x6E, 0x3A, 0x20, 0x31, 0x2E, 0x30, 0x0A,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007305
7306 0x18, 0x3e,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07007307 0xd9, 0x01, 0x01, 0x52, 0x4D, 0x49, 0x4D, 0x45, 0x2D, 0x56,
7308 0x65, 0x72, 0x73, 0x69, 0x6F, 0x6E, 0x3A, 0x20, 0x31, 0x2E,
7309 0x30, 0x0A,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007310
7311 0x18, 0x3f,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07007312 0x52, 0x4D, 0x49, 0x4D, 0x45, 0x2D, 0x56, 0x65, 0x72, 0x73,
7313 0x69, 0x6F, 0x6E, 0x3A, 0x20, 0x31, 0x2E, 0x30, 0x0A,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007314
7315 // UUID
7316 0x18, 0x46,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07007317 0xd8, 0x25, 0x50, 0x53, 0x4D, 0x41, 0x52, 0x54, 0x43, 0x53,
7318 0x4C, 0x54, 0x54, 0x43, 0x46, 0x49, 0x43, 0x41, 0x32,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007319
7320 0x18, 0x47,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07007321 0x50, 0x53, 0x4D, 0x41, 0x52, 0x54, 0x43, 0x53, 0x4C, 0x54,
7322 0x54, 0x43, 0x46, 0x49, 0x43, 0x41, 0x32
Laurence Lundblade37f46e52020-08-04 03:32:14 -07007323};
7324
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03007325int32_t DecodeTaggedTypeTests(void)
Laurence Lundblade37f46e52020-08-04 03:32:14 -07007326{
7327 QCBORDecodeContext DC;
7328 QCBORError uErr;
7329
7330 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTaggedTypes), 0);
7331
7332 UsefulBufC String;
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007333 bool bNeg;
Laurence Lundblade37f46e52020-08-04 03:32:14 -07007334
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07007335 QCBORDecode_EnterMap(&DC, NULL);
Laurence Lundblade9b334962020-08-27 10:55:53 -07007336 QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007337 QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &String);
Laurence Lundblade37f46e52020-08-04 03:32:14 -07007338 if(QCBORDecode_GetError(&DC) != QCBOR_SUCCESS) {
7339 return 1;
7340 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07007341 QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade37f46e52020-08-04 03:32:14 -07007342 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_UNEXPECTED_TYPE) {
7343 return 2;
7344 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07007345 QCBORDecode_GetDateStringInMapN(&DC, 1, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade37f46e52020-08-04 03:32:14 -07007346 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_UNEXPECTED_TYPE) {
7347 return 3;
7348 }
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007349 QCBORDecode_GetDateStringInMapN(&DC, 1, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &String);
Laurence Lundblade9b334962020-08-27 10:55:53 -07007350 QCBORDecode_GetDateStringInMapN(&DC, 1, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade37f46e52020-08-04 03:32:14 -07007351 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
7352 return 4;
7353 }
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007354 QCBORDecode_GetDateStringInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07007355 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007356 return 5;
7357 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07007358
Laurence Lundblade9b334962020-08-27 10:55:53 -07007359 QCBORDecode_GetBignumInMapN(&DC, 10, QCBOR_TAG_REQUIREMENT_TAG, &String, &bNeg);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007360 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
7361 bNeg != false) {
7362 return 10;
7363 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07007364 QCBORDecode_GetBignumInMapN(&DC, 11, QCBOR_TAG_REQUIREMENT_TAG, &String, &bNeg);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007365 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
7366 bNeg != true) {
7367 return 11;
7368 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07007369 QCBORDecode_GetBignumInMapN(&DC, 11, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bNeg);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007370 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_UNEXPECTED_TYPE) {
7371 return 12;
7372 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07007373 QCBORDecode_GetBignumInMapN(&DC, 14, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bNeg);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07007374 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007375 return 13;
7376 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07007377 QCBORDecode_GetBignumInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bNeg);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07007378 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007379 return 14;
7380 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07007381
Laurence Lundblade9b334962020-08-27 10:55:53 -07007382 QCBORDecode_GetURIInMapN(&DC, 20, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007383 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
7384 return 20;
7385 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07007386 QCBORDecode_GetURIInMapN(&DC, 21, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007387 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
7388 return 21;
7389 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07007390 QCBORDecode_GetURIInMapN(&DC, 22, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07007391 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007392 return 22;
7393 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07007394 QCBORDecode_GetURIInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07007395 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007396 return 23;
7397 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07007398
Laurence Lundblade24bd7e12021-01-09 00:05:11 -08007399#ifndef QCBOR_DISABLE_UNCOMMON_TAGS
Laurence Lundblade9b334962020-08-27 10:55:53 -07007400 QCBORDecode_GetB64InMapN(&DC, 30, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007401 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
7402 return 30;
7403 }
Laurence Lundblade24bd7e12021-01-09 00:05:11 -08007404#endif
Laurence Lundblade9b334962020-08-27 10:55:53 -07007405 QCBORDecode_GetB64InMapN(&DC, 31, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007406 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
7407 return 31;
7408 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07007409 QCBORDecode_GetB64InMapN(&DC, 32, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07007410 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007411 return 32;
7412 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07007413 QCBORDecode_GetB64InMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07007414 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007415 return 33;
7416 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07007417
Laurence Lundblade24bd7e12021-01-09 00:05:11 -08007418#ifndef QCBOR_DISABLE_UNCOMMON_TAGS
Laurence Lundblade9b334962020-08-27 10:55:53 -07007419 QCBORDecode_GetB64URLInMapN(&DC, 40, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007420 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
7421 return 40;
7422 }
Laurence Lundblade24bd7e12021-01-09 00:05:11 -08007423#endif
Laurence Lundblade9b334962020-08-27 10:55:53 -07007424 QCBORDecode_GetB64URLInMapN(&DC, 41, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007425 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
7426 return 41;
7427 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07007428 QCBORDecode_GetB64URLInMapN(&DC, 42, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07007429 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007430 return 42;
7431 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07007432 QCBORDecode_GetB64URLInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07007433 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007434 return 43;
7435 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07007436
Laurence Lundblade24bd7e12021-01-09 00:05:11 -08007437#ifndef QCBOR_DISABLE_UNCOMMON_TAGS
Laurence Lundblade9b334962020-08-27 10:55:53 -07007438 QCBORDecode_GetRegexInMapN(&DC, 50, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007439 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
7440 return 50;
7441 }
Laurence Lundblade24bd7e12021-01-09 00:05:11 -08007442#endif
Laurence Lundblade9b334962020-08-27 10:55:53 -07007443 QCBORDecode_GetRegexInMapN(&DC, 51, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007444 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
7445 return 51;
7446 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07007447 QCBORDecode_GetRegexInMapN(&DC, 52, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07007448 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007449 return 52;
7450 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07007451 QCBORDecode_GetRegexInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07007452 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007453 return 53;
7454 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07007455
Laurence Lundblade24bd7e12021-01-09 00:05:11 -08007456#ifndef QCBOR_DISABLE_UNCOMMON_TAGS
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007457 // MIME
7458 bool bIsNot7Bit;
Laurence Lundblade9b334962020-08-27 10:55:53 -07007459 QCBORDecode_GetMIMEMessageInMapN(&DC, 60, QCBOR_TAG_REQUIREMENT_TAG, &String, &bIsNot7Bit);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007460 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
7461 bIsNot7Bit == true) {
7462 return 60;
7463 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07007464 QCBORDecode_GetMIMEMessageInMapN(&DC, 61, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bIsNot7Bit);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007465 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
7466 bIsNot7Bit == true) {
7467 return 61;
7468 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07007469 QCBORDecode_GetMIMEMessageInMapN(&DC, 62, QCBOR_TAG_REQUIREMENT_TAG, &String, &bIsNot7Bit);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007470 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
7471 bIsNot7Bit == false) {
7472 return 62;
7473 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07007474 QCBORDecode_GetMIMEMessageInMapN(&DC, 63, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bIsNot7Bit);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007475 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
7476 bIsNot7Bit == false) {
7477 return 63;
7478 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07007479 QCBORDecode_GetMIMEMessageInMapN(&DC, 64, QCBOR_TAG_REQUIREMENT_TAG, &String, &bNeg);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07007480 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007481 return 64;
7482 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07007483 QCBORDecode_GetMIMEMessageInMapSZ(&DC, "zzz", QCBOR_TAG_REQUIREMENT_TAG, &String, &bNeg);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07007484 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007485 return 65;
7486 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07007487
Laurence Lundblade24bd7e12021-01-09 00:05:11 -08007488
Laurence Lundblade9b334962020-08-27 10:55:53 -07007489 QCBORDecode_GetBinaryUUIDInMapN(&DC, 70, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007490 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
7491 return 70;
7492 }
Laurence Lundblade24bd7e12021-01-09 00:05:11 -08007493#endif /* #ifndef QCBOR_DISABLE_UNCOMMON_TAGS */
7494
Laurence Lundblade9b334962020-08-27 10:55:53 -07007495 QCBORDecode_GetBinaryUUIDInMapN(&DC, 71, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007496 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
7497 return 71;
7498 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07007499 QCBORDecode_GetBinaryUUIDInMapN(&DC, 72, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07007500 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007501 return 72;
7502 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07007503 QCBORDecode_GetBinaryUUIDInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07007504 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007505 return 73;
7506 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07007507
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07007508 // Improvement: add some more error test cases
7509
Laurence Lundblade37f46e52020-08-04 03:32:14 -07007510 QCBORDecode_ExitMap(&DC);
7511
7512 uErr = QCBORDecode_Finish(&DC);
7513 if(uErr != QCBOR_SUCCESS) {
7514 return 100;
7515 }
7516
7517 return 0;
7518}
Laurence Lundbladea4308a82020-10-03 18:08:57 -07007519
7520
7521
7522
7523/*
Laurence Lundbladecc7da412020-12-27 00:09:07 -08007524 [
7525 "aaaaaaaaaa",
7526 {}
7527 ]
Laurence Lundbladea4308a82020-10-03 18:08:57 -07007528 */
7529static const uint8_t spTooLarge1[] = {
7530 0x9f,
7531 0x6a, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61,
7532 0xa0,
7533 0xff
7534};
7535
7536/*
Laurence Lundbladecc7da412020-12-27 00:09:07 -08007537 [
7538 {
7539 0: "aaaaaaaaaa"
7540 }
7541 ]
Laurence Lundbladea4308a82020-10-03 18:08:57 -07007542 */
7543static const uint8_t spTooLarge2[] = {
7544 0x9f,
7545 0xa1,
7546 0x00,
7547 0x6a, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61,
7548 0xff
7549};
7550
7551/*
Laurence Lundbladecc7da412020-12-27 00:09:07 -08007552 h'A1006A61616161616161616161'
Laurence Lundbladea4308a82020-10-03 18:08:57 -07007553
Laurence Lundbladecc7da412020-12-27 00:09:07 -08007554 {
7555 0: "aaaaaaaaaa"
7556 }
Laurence Lundbladea4308a82020-10-03 18:08:57 -07007557 */
7558static const uint8_t spTooLarge3[] = {
7559 0x4d,
7560 0xa1,
7561 0x00,
7562 0x6a, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61,
7563};
7564
7565int32_t TooLargeInputTest(void)
7566{
7567 QCBORDecodeContext DC;
7568 QCBORError uErr;
7569 UsefulBufC String;
7570
7571 // These tests require a build with QCBOR_MAX_DECODE_INPUT_SIZE set
7572 // to 10 There's not really any way to test this error
7573 // condition. The error condition is not complex, so setting
7574 // QCBOR_MAX_DECODE_INPUT_SIZE gives an OK test.
7575
7576 // The input CBOR is only too large because the
7577 // QCBOR_MAX_DECODE_INPUT_SIZE is 10.
7578 //
7579 // This test is disabled for the normal test runs because of the
7580 // special build requirement.
7581
7582
7583 // Tests the start of a map being too large
7584 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTooLarge1), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07007585 QCBORDecode_EnterArray(&DC, NULL);
Laurence Lundbladea4308a82020-10-03 18:08:57 -07007586 QCBORDecode_GetTextString(&DC, &String);
7587 uErr = QCBORDecode_GetError(&DC);
7588 if(uErr != QCBOR_SUCCESS) {
7589 return 1;
7590 }
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07007591 QCBORDecode_EnterMap(&DC, NULL);
Laurence Lundbladea4308a82020-10-03 18:08:57 -07007592 uErr = QCBORDecode_GetError(&DC);
7593 if(uErr != QCBOR_ERR_INPUT_TOO_LARGE) {
7594 return 2;
7595 }
7596
7597 // Tests the end of a map being too large
7598 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTooLarge2), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07007599 QCBORDecode_EnterArray(&DC, NULL);
7600 QCBORDecode_EnterMap(&DC, NULL);
Laurence Lundbladea4308a82020-10-03 18:08:57 -07007601 uErr = QCBORDecode_GetError(&DC);
7602 if(uErr != QCBOR_SUCCESS) {
7603 return 3;
7604 }
7605 QCBORDecode_ExitMap(&DC);
7606 uErr = QCBORDecode_GetError(&DC);
7607 if(uErr != QCBOR_ERR_INPUT_TOO_LARGE) {
7608 return 4;
7609 }
7610
7611 // Tests the entire input CBOR being too large when processing bstr wrapping
7612 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTooLarge3), QCBOR_DECODE_MODE_NORMAL);
7613 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
7614 uErr = QCBORDecode_GetError(&DC);
7615 if(uErr != QCBOR_ERR_INPUT_TOO_LARGE) {
7616 return 5;
7617 }
7618
7619 return 0;
7620}
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07007621
7622
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08007623#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
7624
Laurence Lundblade37286c02022-09-03 10:05:02 -07007625/*
7626 An array of three map entries
7627 1) Indefinite length string label for indefinite lenght byte string
7628 2) Indefinite length string label for an integer
7629 3) Indefinite length string label for an indefinite-length negative big num
7630 */
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07007631static const uint8_t spMapWithIndefLenStrings[] = {
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08007632 0xa3,
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07007633 0x7f, 0x61, 'l', 0x64, 'a', 'b', 'e', 'l' , 0x61, '1', 0xff,
7634 0x5f, 0x42, 0x01, 0x02, 0x43, 0x03, 0x04, 0x05, 0xff,
7635 0x7f, 0x62, 'd', 'y', 0x61, 'm', 0x61, 'o', 0xff,
7636 0x03,
7637 0x7f, 0x62, 'l', 'a', 0x63, 'b', 'e', 'l', 0x61, '2', 0xff,
7638 0xc3,
7639 0x5f, 0x42, 0x00, 0x01, 0x42, 0x00, 0x01, 0x41, 0x01, 0xff,
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07007640};
7641
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03007642int32_t SpiffyIndefiniteLengthStringsTests(void)
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07007643{
7644 QCBORDecodeContext DCtx;
7645
7646 QCBORDecode_Init(&DCtx,
7647 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spMapWithIndefLenStrings),
7648 QCBOR_DECODE_MODE_NORMAL);
7649
Laurence Lundblade8510f8c2020-12-01 11:31:16 -08007650 UsefulBuf_MAKE_STACK_UB(StringBuf, 200);
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07007651 QCBORDecode_SetMemPool(&DCtx, StringBuf, false);
7652
7653 UsefulBufC ByteString;
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07007654 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07007655 QCBORDecode_GetByteStringInMapSZ(&DCtx, "label1", &ByteString);
Laurence Lundblade37286c02022-09-03 10:05:02 -07007656
7657#ifndef QCBOR_DISABLE_TAGS
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07007658 if(QCBORDecode_GetAndResetError(&DCtx)) {
7659 return 1;
7660 }
7661
7662 const uint8_t pExectedBytes[] = {0x01, 0x02, 0x03, 0x04, 0x05};
7663 if(UsefulBuf_Compare(ByteString, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pExectedBytes))) {
7664 return 2;
7665 }
7666
7667 uint64_t uInt;
7668 QCBORDecode_GetUInt64InMapSZ(&DCtx, "dymo", &uInt);
7669 if(QCBORDecode_GetAndResetError(&DCtx)) {
7670 return 3;
7671 }
7672 if(uInt != 3) {
7673 return 4;
7674 }
7675
Máté Tóth-Pálef5f07a2021-09-17 19:31:37 +02007676#ifndef USEFULBUF_DISABLE_ALL_FLOAT
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07007677 double uDouble;
7678 QCBORDecode_GetDoubleConvertAllInMapSZ(&DCtx,
7679 "label2",
7680 0xff,
7681 &uDouble);
Laurence Lundblade37286c02022-09-03 10:05:02 -07007682
Laurence Lundbladeb8e19aa2020-10-07 20:59:11 -07007683#ifndef QCBOR_DISABLE_FLOAT_HW_USE
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07007684 if(QCBORDecode_GetAndResetError(&DCtx)) {
7685 return 5;
7686 }
7687 if(uDouble != -16777474) {
7688 return 6;
7689 }
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08007690#else /* QCBOR_DISABLE_FLOAT_HW_USE */
Laurence Lundbladeb8e19aa2020-10-07 20:59:11 -07007691 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_HW_FLOAT_DISABLED) {
7692 return 7;
7693 }
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08007694#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
Máté Tóth-Pálef5f07a2021-09-17 19:31:37 +02007695#endif /* USEFULBUF_DISABLE_ALL_FLOAT */
Laurence Lundbladeb8e19aa2020-10-07 20:59:11 -07007696
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07007697 QCBORDecode_ExitMap(&DCtx);
7698
7699 if(QCBORDecode_Finish(&DCtx)) {
7700 return 99;
7701 }
7702
Laurence Lundblade37286c02022-09-03 10:05:02 -07007703#else /* QCBOR_DISABLE_TAGS */
7704 /* The big num in the input is a CBOR tag and you can't do
7705 * map lookups in a map with a tag so this test does very little
7706 * when tags are disabled. That is OK, the test coverage is still
7707 * good when they are not.
7708 */
7709 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_TAGS_DISABLED) {
7710 return 1002;
7711 }
7712#endif /*QCBOR_DISABLE_TAGS */
7713
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07007714 return 0;
7715}
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08007716#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
Laurence Lundblade2a26abb2020-11-05 19:06:54 -08007717
7718
Laurence Lundbladecf41c522021-02-20 10:19:07 -07007719/*
7720 * An array of an integer and an array. The second array contains
7721 * a bstr-wrapped map.
7722 *
7723 * [7, [h'A36D6669... (see next lines) 73']]
7724 *
7725 * {"first integer": 42,
7726 * "an array of two strings": ["string1", "string2"],
7727 * "map in a map":
7728 * { "bytes 1": h'78787878',
7729 * "bytes 2": h'79797979',
7730 * "another int": 98,
7731 * "text 2": "lies, damn lies and statistics"
7732 * }
7733 * }
7734 */
Laurence Lundblade2a26abb2020-11-05 19:06:54 -08007735
Laurence Lundbladecf41c522021-02-20 10:19:07 -07007736static const uint8_t pValidWrappedMapEncoded[] = {
7737 0x82, 0x07, 0x81, 0x58, 0x97,
7738 0xa3, 0x6d, 0x66, 0x69, 0x72, 0x73, 0x74, 0x20, 0x69, 0x6e,
7739 0x74, 0x65, 0x67, 0x65, 0x72, 0x18, 0x2a, 0x77, 0x61, 0x6e,
7740 0x20, 0x61, 0x72, 0x72, 0x61, 0x79, 0x20, 0x6f, 0x66, 0x20,
7741 0x74, 0x77, 0x6f, 0x20, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67,
7742 0x73, 0x82, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x31,
7743 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x32, 0x6c, 0x6d,
7744 0x61, 0x70, 0x20, 0x69, 0x6e, 0x20, 0x61, 0x20, 0x6d, 0x61,
7745 0x70, 0xa4, 0x67, 0x62, 0x79, 0x74, 0x65, 0x73, 0x20, 0x31,
7746 0x44, 0x78, 0x78, 0x78, 0x78, 0x67, 0x62, 0x79, 0x74, 0x65,
7747 0x73, 0x20, 0x32, 0x44, 0x79, 0x79, 0x79, 0x79, 0x6b, 0x61,
7748 0x6e, 0x6f, 0x74, 0x68, 0x65, 0x72, 0x20, 0x69, 0x6e, 0x74,
7749 0x18, 0x62, 0x66, 0x74, 0x65, 0x78, 0x74, 0x20, 0x32, 0x78,
7750 0x1e, 0x6c, 0x69, 0x65, 0x73, 0x2c, 0x20, 0x64, 0x61, 0x6d,
7751 0x6e, 0x20, 0x6c, 0x69, 0x65, 0x73, 0x20, 0x61, 0x6e, 0x64,
7752 0x20, 0x73, 0x74, 0x61, 0x74, 0x69, 0x73, 0x74, 0x69, 0x63,
7753 0x73
7754};
7755
7756#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
7757
7758/* As above, but the arrays are indefinite length */
7759static const uint8_t pValidIndefWrappedMapEncoded[] = {
7760 0x9f, 0x07, 0x9f, 0x58, 0x97,
7761 0xa3, 0x6d, 0x66, 0x69, 0x72, 0x73, 0x74, 0x20, 0x69, 0x6e,
7762 0x74, 0x65, 0x67, 0x65, 0x72, 0x18, 0x2a, 0x77, 0x61, 0x6e,
7763 0x20, 0x61, 0x72, 0x72, 0x61, 0x79, 0x20, 0x6f, 0x66, 0x20,
7764 0x74, 0x77, 0x6f, 0x20, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67,
7765 0x73, 0x82, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x31,
7766 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x32, 0x6c, 0x6d,
7767 0x61, 0x70, 0x20, 0x69, 0x6e, 0x20, 0x61, 0x20, 0x6d, 0x61,
7768 0x70, 0xa4, 0x67, 0x62, 0x79, 0x74, 0x65, 0x73, 0x20, 0x31,
7769 0x44, 0x78, 0x78, 0x78, 0x78, 0x67, 0x62, 0x79, 0x74, 0x65,
7770 0x73, 0x20, 0x32, 0x44, 0x79, 0x79, 0x79, 0x79, 0x6b, 0x61,
7771 0x6e, 0x6f, 0x74, 0x68, 0x65, 0x72, 0x20, 0x69, 0x6e, 0x74,
7772 0x18, 0x62, 0x66, 0x74, 0x65, 0x78, 0x74, 0x20, 0x32, 0x78,
7773 0x1e, 0x6c, 0x69, 0x65, 0x73, 0x2c, 0x20, 0x64, 0x61, 0x6d,
7774 0x6e, 0x20, 0x6c, 0x69, 0x65, 0x73, 0x20, 0x61, 0x6e, 0x64,
7775 0x20, 0x73, 0x74, 0x61, 0x74, 0x69, 0x73, 0x74, 0x69, 0x63,
7776 0x73,
7777 0xff, 0xff
7778};
7779#endif
7780
7781
7782static const uint8_t pWithEmptyMap[] = {0x82, 0x18, 0x64, 0xa0};
7783
7784#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
7785static const uint8_t pWithEmptyMapInDef[] = {0x9f, 0x18, 0x64, 0xbf, 0xff, 0xff};
7786#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
7787
7788#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
Laurence Lundblade37286c02022-09-03 10:05:02 -07007789
7790/*
7791An array of one that contains
7792 a byte string that is tagged 24 which means CBOR-encoded data
7793 the byte string is an indefinite length string
7794 the wrapped byte string is an array of three numbers
7795 [42, 43, 44]
7796
7797[
7798 24(
7799 (_ h'83', h'18', h'2A182B', h'182C')
7800 )
7801]
7802 */
Laurence Lundbladecf41c522021-02-20 10:19:07 -07007803static const uint8_t pWrappedByIndefiniteLength[] = {
7804 0x81,
7805 0xd8, 0x18,
7806 0x5f,
7807 0x41, 0x83,
7808 0x41, 0x18,
7809 0x43, 0x2A, 0x18, 0x2B,
7810 0x42, 0x18, 0x2C,
7811 0xff
7812};
7813#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
7814
7815
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03007816int32_t PeekAndRewindTest(void)
Laurence Lundblade2a26abb2020-11-05 19:06:54 -08007817{
7818 QCBORItem Item;
7819 QCBORError nCBORError;
7820 QCBORDecodeContext DCtx;
7821
7822 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0);
7823
7824 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
7825 return 100+(int32_t)nCBORError;
7826 }
7827 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
7828 return 200;
7829 }
7830
Laurence Lundblade3427dee2021-06-20 11:11:24 -07007831 QCBORDecode_VPeekNext(&DCtx, &Item);
7832 if((nCBORError = QCBORDecode_GetError(&DCtx))) {
7833 return 150+(int32_t)nCBORError;
7834 }
7835 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
7836 return 250;
7837 }
7838
Laurence Lundblade2a26abb2020-11-05 19:06:54 -08007839 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
7840 return (int32_t)nCBORError;
7841 }
7842 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
7843 return 300;
7844 }
7845
7846 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
7847 return 400 + (int32_t)nCBORError;
7848 }
7849 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
7850 return 500;
7851 }
7852
7853 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7854 return (int32_t)nCBORError;
7855 }
7856 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
7857 return 600;
7858 }
7859
7860 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
7861 return 900 + (int32_t)nCBORError;
7862 }
7863 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
7864 Item.uDataType != QCBOR_TYPE_INT64 ||
7865 Item.val.int64 != 42 ||
7866 Item.uDataAlloc ||
7867 Item.uLabelAlloc ||
7868 UsefulBufCompareToSZ(Item.label.string, "first integer")) {
7869 return 1000;
7870 }
7871
7872 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7873 return 1100 + (int32_t)nCBORError;
7874 }
7875
7876 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
7877 Item.uDataType != QCBOR_TYPE_INT64 ||
7878 Item.val.int64 != 42 ||
7879 Item.uDataAlloc ||
7880 Item.uLabelAlloc ||
7881 UsefulBufCompareToSZ(Item.label.string, "first integer")) {
7882 return 1200;
7883 }
7884
7885
7886 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7887 return 1300 + (int32_t)nCBORError;
7888 }
7889 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
7890 Item.uDataAlloc ||
7891 Item.uLabelAlloc ||
7892 UsefulBufCompareToSZ(Item.label.string, "an array of two strings") ||
7893 Item.uDataType != QCBOR_TYPE_ARRAY ||
Laurence Lundbladecf41c522021-02-20 10:19:07 -07007894 Item.val.uCount != 2) {
Laurence Lundblade2a26abb2020-11-05 19:06:54 -08007895 return 1400;
Laurence Lundbladecf41c522021-02-20 10:19:07 -07007896 }
Laurence Lundblade2a26abb2020-11-05 19:06:54 -08007897
7898 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7899 return 1500 + (int32_t)nCBORError;
7900 }
7901 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
7902 Item.uDataAlloc ||
7903 Item.uLabelAlloc ||
7904 UsefulBufCompareToSZ(Item.val.string, "string1")) {
7905 return 1600;
7906 }
7907
7908 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
7909 return 1700 + (int32_t)nCBORError;
7910 }
7911 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
7912 Item.uDataAlloc ||
7913 Item.uLabelAlloc ||
7914 UsefulBufCompareToSZ(Item.val.string, "string2")) {
7915 return 1800;
7916 }
7917
7918 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
7919 return (int32_t)nCBORError;
7920 }
7921 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
7922 Item.uDataAlloc ||
7923 Item.uLabelAlloc ||
7924 UsefulBufCompareToSZ(Item.val.string, "string2")) {
7925 return 1900;
7926 }
7927
7928 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7929 return (int32_t)nCBORError;
7930 }
7931 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
7932 Item.uDataAlloc ||
7933 Item.uLabelAlloc ||
7934 UsefulBufCompareToSZ(Item.val.string, "string2")) {
7935 return 2000;
7936 }
7937
7938
7939 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7940 return 2100 + (int32_t)nCBORError;
7941 }
7942 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
7943 Item.uDataAlloc ||
7944 Item.uLabelAlloc ||
7945 UsefulBufCompareToSZ(Item.label.string, "map in a map") ||
7946 Item.uDataType != QCBOR_TYPE_MAP ||
7947 Item.val.uCount != 4) {
7948 return 2100;
7949 }
7950
7951 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7952 return 2200 + (int32_t)nCBORError;
7953 }
7954 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
7955 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("bytes 1"))||
7956 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
7957 Item.uDataAlloc ||
7958 Item.uLabelAlloc ||
7959 UsefulBufCompareToSZ(Item.val.string, "xxxx")) {
7960 return 2300;
7961 }
7962
7963 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
7964 return 2400 + (int32_t)nCBORError;
7965 }
7966 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
7967 UsefulBufCompareToSZ(Item.label.string, "bytes 2") ||
7968 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
7969 Item.uDataAlloc ||
7970 Item.uLabelAlloc ||
7971 UsefulBufCompareToSZ(Item.val.string, "yyyy")) {
7972 return 2500;
7973 }
7974
7975 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7976 return 2600 + (int32_t)nCBORError;
7977 }
7978 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
7979 UsefulBufCompareToSZ(Item.label.string, "bytes 2") ||
7980 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
7981 Item.uDataAlloc ||
7982 Item.uLabelAlloc ||
7983 UsefulBufCompareToSZ(Item.val.string, "yyyy")) {
7984 return 2700;
7985 }
7986
7987 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7988 return 2800 + (int32_t)nCBORError;
7989 }
7990 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
7991 Item.uDataAlloc ||
7992 Item.uLabelAlloc ||
7993 UsefulBufCompareToSZ(Item.label.string, "another int") ||
7994 Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundbladecf41c522021-02-20 10:19:07 -07007995 Item.val.int64 != 98) {
Laurence Lundblade2a26abb2020-11-05 19:06:54 -08007996 return 2900;
Laurence Lundbladecf41c522021-02-20 10:19:07 -07007997 }
Laurence Lundblade2a26abb2020-11-05 19:06:54 -08007998
7999 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
8000 return 3000 + (int32_t)nCBORError;
8001 }
8002 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
8003 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("text 2"))||
8004 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
8005 Item.uDataAlloc ||
8006 Item.uLabelAlloc ||
8007 UsefulBufCompareToSZ(Item.val.string, "lies, damn lies and statistics")) {
8008 return 3100;
8009 }
8010
8011 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8012 return 3200 + (int32_t)nCBORError;
8013 }
8014 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
8015 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("text 2"))||
8016 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
8017 Item.uDataAlloc ||
8018 Item.uLabelAlloc ||
8019 UsefulBufCompareToSZ(Item.val.string, "lies, damn lies and statistics")) {
8020 return 3300;
8021 }
8022
Laurence Lundblade3427dee2021-06-20 11:11:24 -07008023 nCBORError = QCBORDecode_PeekNext(&DCtx, &Item);
8024 if(nCBORError != QCBOR_ERR_NO_MORE_ITEMS) {
8025 return 3300 + (int32_t)nCBORError;
8026 }
8027
8028 QCBORDecode_VPeekNext(&DCtx, &Item);
8029 nCBORError = QCBORDecode_GetError(&DCtx);
8030 if(nCBORError != QCBOR_ERR_NO_MORE_ITEMS) {
8031 return 3400 + (int32_t)nCBORError;
8032 }
8033
8034 QCBORDecode_VPeekNext(&DCtx, &Item);
8035 nCBORError = QCBORDecode_GetError(&DCtx);
8036 if(nCBORError != QCBOR_ERR_NO_MORE_ITEMS) {
8037 return 3500 + (int32_t)nCBORError;
8038 }
Laurence Lundbladecf41c522021-02-20 10:19:07 -07008039
8040
8041 // Rewind to top level after entering several maps
8042 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0);
8043
8044 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8045 return (int32_t)nCBORError;
8046 }
8047 if(Item.uDataType != QCBOR_TYPE_MAP ||
8048 Item.val.uCount != 3) {
8049 return 400;
8050 }
8051
8052 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8053 return 4000+(int32_t)nCBORError;
8054 }
8055
8056 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
8057 Item.uDataType != QCBOR_TYPE_INT64 ||
8058 Item.val.int64 != 42 ||
8059 Item.uDataAlloc ||
8060 Item.uLabelAlloc ||
8061 UsefulBufCompareToSZ(Item.label.string, "first integer")) {
8062 return 4100;
8063 }
8064
8065 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8066 return 4100+(int32_t)nCBORError;
8067 }
8068 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
8069 Item.uDataAlloc ||
8070 Item.uLabelAlloc ||
8071 UsefulBufCompareToSZ(Item.label.string, "an array of two strings") ||
8072 Item.uDataType != QCBOR_TYPE_ARRAY ||
8073 Item.val.uCount != 2) {
8074 return 4200;
8075 }
8076
8077 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8078 return 4200+(int32_t)nCBORError;
8079 }
8080 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
8081 Item.uDataAlloc ||
8082 Item.uLabelAlloc ||
8083 UsefulBufCompareToSZ(Item.val.string, "string1")) {
8084 return 4300;
8085 }
8086
8087 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8088 return 4300+(int32_t)nCBORError;
8089 }
8090 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
8091 Item.uDataAlloc ||
8092 Item.uLabelAlloc ||
8093 UsefulBufCompareToSZ(Item.val.string, "string2")) {
8094 return 4400;
8095 }
8096
8097 QCBORDecode_Rewind(&DCtx);
8098
8099 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8100 return 4400+(int32_t)nCBORError;
8101 }
8102 if(Item.uDataType != QCBOR_TYPE_MAP ||
8103 Item.val.uCount != 3) {
8104 return 4500;
8105 }
8106
8107 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8108 return (int32_t)nCBORError;
8109 }
8110
8111 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
8112 Item.uDataType != QCBOR_TYPE_INT64 ||
8113 Item.val.int64 != 42 ||
8114 Item.uDataAlloc ||
8115 Item.uLabelAlloc ||
8116 UsefulBufCompareToSZ(Item.label.string, "first integer")) {
8117 return 4600;
8118 }
8119
8120 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8121 return (int32_t)nCBORError;
8122 }
8123 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
8124 Item.uDataAlloc ||
8125 Item.uLabelAlloc ||
8126 UsefulBufCompareToSZ(Item.label.string, "an array of two strings") ||
8127 Item.uDataType != QCBOR_TYPE_ARRAY ||
8128 Item.val.uCount != 2) {
8129 return 4700;
8130 }
8131
8132 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8133 return (int32_t)nCBORError;
8134 }
8135 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
8136 Item.uDataAlloc ||
8137 Item.uLabelAlloc ||
8138 UsefulBufCompareToSZ(Item.val.string, "string1")) {
8139 return 4800;
8140 }
8141
8142 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8143 return 4900+(int32_t)nCBORError;
8144 }
8145 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
8146 Item.uDataAlloc ||
8147 Item.uLabelAlloc ||
8148 UsefulBufCompareToSZ(Item.val.string, "string2")) {
8149 return 5000;
8150 }
8151
8152
8153 // Rewind an entered map
8154 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0);
8155
8156 QCBORDecode_EnterMap(&DCtx, NULL);
8157
8158 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8159 return 5100+(int32_t)nCBORError;
8160 }
8161
8162 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
8163 Item.uDataType != QCBOR_TYPE_INT64 ||
8164 Item.val.int64 != 42 ||
8165 Item.uDataAlloc ||
8166 Item.uLabelAlloc ||
8167 UsefulBufCompareToSZ(Item.label.string, "first integer")) {
8168 return 5200;
8169 }
8170
8171 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8172 return 5200+(int32_t)nCBORError;
8173 }
8174 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
8175 Item.uDataAlloc ||
8176 Item.uLabelAlloc ||
8177 UsefulBufCompareToSZ(Item.label.string, "an array of two strings") ||
8178 Item.uDataType != QCBOR_TYPE_ARRAY ||
8179 Item.val.uCount != 2) {
8180 return -5300;
8181 }
8182
8183 QCBORDecode_Rewind(&DCtx);
8184
8185 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8186 return 5300+(int32_t)nCBORError;
8187 }
8188
8189 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
8190 Item.uDataType != QCBOR_TYPE_INT64 ||
8191 Item.val.int64 != 42 ||
8192 Item.uDataAlloc ||
8193 Item.uLabelAlloc ||
8194 UsefulBufCompareToSZ(Item.label.string, "first integer")) {
8195 return 5400;
8196 }
8197
8198 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8199 return 5400+(int32_t)nCBORError;
8200 }
8201 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
8202 Item.uDataAlloc ||
8203 Item.uLabelAlloc ||
8204 UsefulBufCompareToSZ(Item.label.string, "an array of two strings") ||
8205 Item.uDataType != QCBOR_TYPE_ARRAY ||
8206 Item.val.uCount != 2) {
8207 return 5500;
8208 }
8209
8210
8211 // Rewind and entered array inside an entered map
8212 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0);
8213
8214 QCBORDecode_EnterMap(&DCtx, NULL);
8215
8216 QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings");
8217
8218 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8219 return 5600+(int32_t)nCBORError;
8220 }
8221 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
8222 Item.uDataAlloc ||
8223 Item.uLabelAlloc ||
8224 UsefulBufCompareToSZ(Item.val.string, "string1")) {
8225 return 5700;
8226 }
8227
8228 QCBORDecode_Rewind(&DCtx);
8229
8230 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8231 return 5700+(int32_t)nCBORError;
8232 }
8233 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
8234 Item.uDataAlloc ||
8235 Item.uLabelAlloc ||
8236 UsefulBufCompareToSZ(Item.val.string, "string1")) {
8237 return 5800;
8238 }
8239
8240 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8241 return (int32_t)nCBORError;
8242 }
8243 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
8244 Item.uDataAlloc ||
8245 Item.uLabelAlloc ||
8246 UsefulBufCompareToSZ(Item.val.string, "string2")) {
8247 return 5900;
8248 }
8249
8250 QCBORDecode_Rewind(&DCtx);
8251
8252 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8253 return 5900+(int32_t)nCBORError;
8254 }
8255 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
8256 Item.uDataAlloc ||
8257 Item.uLabelAlloc ||
8258 UsefulBufCompareToSZ(Item.val.string, "string1")) {
8259 return 6000;
8260 }
8261
8262
8263 // Rewind a byte string inside an array inside an array
8264 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidWrappedMapEncoded), 0);
8265
8266 QCBORDecode_EnterArray(&DCtx, NULL);
8267
8268 uint64_t i;
8269 QCBORDecode_GetUInt64(&DCtx, &i);
8270
8271 QCBORDecode_EnterArray(&DCtx, NULL);
8272
8273 QCBORDecode_EnterBstrWrapped(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
8274 if(QCBORDecode_GetError(&DCtx)) {
8275 return 6100;
8276 }
8277
8278 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8279 return (int32_t)nCBORError;
8280 }
8281 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
8282 return 6200;
8283 }
8284
8285 QCBORDecode_Rewind(&DCtx);
8286
8287 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8288 return 6300+(int32_t)nCBORError;
8289 }
8290 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
8291 return 6400;
8292 }
8293
8294#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
8295 // Rewind a byte string inside an indefinite-length array inside
8296 // indefinite-length array
8297
8298 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidIndefWrappedMapEncoded), 0);
8299
8300 QCBORDecode_EnterArray(&DCtx, NULL);
8301
8302 QCBORDecode_GetUInt64(&DCtx, &i);
8303
8304 QCBORDecode_EnterArray(&DCtx, NULL);
8305
8306 QCBORDecode_EnterBstrWrapped(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
8307 if(QCBORDecode_GetError(&DCtx)) {
8308 return 6500;
8309 }
8310
8311 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8312 return 6600+(int32_t)nCBORError;
8313 }
8314 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
8315 return 6700;
8316 }
8317
8318 QCBORDecode_Rewind(&DCtx);
8319
8320 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8321 return 6800+(int32_t)nCBORError;
8322 }
8323 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
8324 return 6900;
8325 }
8326#endif
8327
8328 // Rewind an empty map
8329 // [100, {}]
8330 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pWithEmptyMap), 0);
8331 QCBORDecode_EnterArray(&DCtx, NULL);
8332 QCBORDecode_GetUInt64(&DCtx, &i);
8333 if(i != 100) {
8334 return 7010;
8335 }
8336 QCBORDecode_EnterMap(&DCtx, NULL);
8337
8338 /* Do it 5 times to be sure multiple rewinds work */
8339 for(int n = 0; n < 5; n++) {
8340 nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
8341 if(nCBORError != QCBOR_ERR_NO_MORE_ITEMS) {
8342 return 7000 + n;
8343 }
8344 QCBORDecode_Rewind(&DCtx);
8345 }
8346 QCBORDecode_ExitMap(&DCtx);
8347 QCBORDecode_Rewind(&DCtx);
8348 QCBORDecode_GetUInt64(&DCtx, &i);
8349 if(i != 100) {
8350 return 7010;
8351 }
8352 QCBORDecode_ExitArray(&DCtx);
8353 QCBORDecode_Rewind(&DCtx);
8354 QCBORDecode_EnterArray(&DCtx, NULL);
8355 i = 9;
8356 QCBORDecode_GetUInt64(&DCtx, &i);
8357 if(i != 100) {
8358 return 7020;
8359 }
8360 if(QCBORDecode_GetError(&DCtx)){
8361 return 7030;
8362 }
8363
8364 // Rewind an empty indefinite length map
8365#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
8366 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pWithEmptyMapInDef), 0);
8367 QCBORDecode_EnterArray(&DCtx, NULL);
8368 QCBORDecode_GetUInt64(&DCtx, &i);
8369 if(i != 100) {
8370 return 7810;
8371 }
8372 QCBORDecode_EnterMap(&DCtx, NULL);
8373
8374 /* Do it 5 times to be sure multiple rewinds work */
8375 for(int n = 0; n < 5; n++) {
8376 nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
8377 if(nCBORError != QCBOR_ERR_NO_MORE_ITEMS) {
8378 return 7800 + n;
8379 }
8380 QCBORDecode_Rewind(&DCtx);
8381 }
8382 QCBORDecode_ExitMap(&DCtx);
8383 QCBORDecode_Rewind(&DCtx);
8384 QCBORDecode_GetUInt64(&DCtx, &i);
8385 if(i != 100) {
8386 return 7810;
8387 }
8388 QCBORDecode_ExitArray(&DCtx);
8389 QCBORDecode_Rewind(&DCtx);
8390 QCBORDecode_EnterArray(&DCtx, NULL);
8391 i = 9;
8392 QCBORDecode_GetUInt64(&DCtx, &i);
8393 if(i != 100) {
8394 return 7820;
8395 }
8396 if(QCBORDecode_GetError(&DCtx)){
8397 return 7830;
8398 }
8399#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
8400
8401 // Rewind an indefnite length byte-string wrapped sequence
8402#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
Laurence Lundblade37286c02022-09-03 10:05:02 -07008403 // TODO: rewrite this test to not use tags
Laurence Lundbladecf41c522021-02-20 10:19:07 -07008404 QCBORDecode_Init(&DCtx,
8405 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pWrappedByIndefiniteLength),
8406 0);
8407 UsefulBuf_MAKE_STACK_UB(Pool, 100);
8408 QCBORDecode_SetMemPool(&DCtx, Pool, 0);
8409
8410 QCBORDecode_EnterArray(&DCtx, NULL);
8411 QCBORDecode_EnterBstrWrapped(&DCtx, 2, NULL);
Laurence Lundblade37286c02022-09-03 10:05:02 -07008412#ifndef QCBOR_DISABLE_TAGS
Laurence Lundbladecf41c522021-02-20 10:19:07 -07008413 if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_INPUT_TOO_LARGE) {
Laurence Lundblade37286c02022-09-03 10:05:02 -07008414 /* TODO: This is what happens when trying to enter
8415 * indefinite-length byte string wrapped CBOR. Tolerate for
8416 * now. Eventually it needs to be fixed so this works, but that
8417 * is not simple.
8418 */
Laurence Lundbladecf41c522021-02-20 10:19:07 -07008419 return 7300;
8420 }
8421
8422 /*
8423 QCBORDecode_GetUInt64(&DCtx, &i);
8424 if(i != 42) {
8425 return 7110;
8426 }
8427 QCBORDecode_Rewind(&DCtx);
8428 QCBORDecode_GetUInt64(&DCtx, &i);
8429 if(i != 42) {
8430 return 7220;
8431 }*/
Laurence Lundblade37286c02022-09-03 10:05:02 -07008432
8433#else /* QCBOR_DISABLE_TAGS */
8434 if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_TAGS_DISABLED) {
8435 return 7301;
8436 }
8437#endif /* QCBOR_DISABLE_TAGS */
8438
Laurence Lundbladecf41c522021-02-20 10:19:07 -07008439#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
8440
8441
8442 // Rewind an indefnite length byte-string wrapped sequence
8443
Laurence Lundblade2a26abb2020-11-05 19:06:54 -08008444 return 0;
8445}
Laurence Lundblade9f9c3732021-03-23 09:38:46 -07008446
8447
8448
8449
8450static const uint8_t spBooleansInMap[] =
8451{
8452 0xa1, 0x08, 0xf5
8453};
8454
8455static const uint8_t spBooleansInMapWrongType[] =
8456{
8457 0xa1, 0x08, 0xf6
8458};
8459
8460static const uint8_t spBooleansInMapNWF[] =
8461{
8462 0xa1, 0x08, 0x1a
8463};
8464
Laurence Lundblade8782dd32021-04-27 04:15:37 -07008465static const uint8_t spNullInMap[] =
8466{
8467 0xa1, 0x08, 0xf6
8468};
8469
8470static const uint8_t spUndefinedInMap[] =
8471{
8472 0xa1, 0x08, 0xf7
8473};
8474
Laurence Lundblade9f9c3732021-03-23 09:38:46 -07008475
8476int32_t BoolTest(void)
8477{
8478 QCBORDecodeContext DCtx;
8479 bool b;
8480
Laurence Lundblade8782dd32021-04-27 04:15:37 -07008481 QCBORDecode_Init(&DCtx,
8482 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMap),
8483 0);
Laurence Lundblade9f9c3732021-03-23 09:38:46 -07008484 QCBORDecode_EnterMap(&DCtx, NULL);
8485 QCBORDecode_GetBool(&DCtx, &b);
8486 if(QCBORDecode_GetAndResetError(&DCtx) || !b) {
8487 return 1;
8488 }
8489
8490 QCBORDecode_GetBoolInMapN(&DCtx, 7, &b);
8491 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_LABEL_NOT_FOUND) {
8492 return 2;
8493 }
8494
8495 QCBORDecode_GetBoolInMapN(&DCtx, 8, &b);
8496 if(QCBORDecode_GetAndResetError(&DCtx) || !b) {
8497 return 3;
8498 }
8499
8500
8501 QCBORDecode_GetBoolInMapSZ(&DCtx, "xx", &b);
8502 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_LABEL_NOT_FOUND) {
8503 return 4;
8504 }
8505
Laurence Lundblade8782dd32021-04-27 04:15:37 -07008506 QCBORDecode_Init(&DCtx,
8507 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMapWrongType),
8508 0);
Laurence Lundblade9f9c3732021-03-23 09:38:46 -07008509 QCBORDecode_EnterMap(&DCtx, NULL);
8510 QCBORDecode_GetBool(&DCtx, &b);
8511 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
8512 return 5;
8513 }
8514
Laurence Lundblade8782dd32021-04-27 04:15:37 -07008515 QCBORDecode_Init(&DCtx,
8516 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMapNWF),
8517 0);
Laurence Lundblade9f9c3732021-03-23 09:38:46 -07008518 QCBORDecode_EnterMap(&DCtx, NULL);
8519 QCBORDecode_GetBool(&DCtx, &b);
8520 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_HIT_END) {
8521 return 6;
8522 }
8523
Laurence Lundblade8782dd32021-04-27 04:15:37 -07008524
8525 QCBORDecode_Init(&DCtx,
8526 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spNullInMap),
8527 0);
8528 QCBORDecode_EnterMap(&DCtx, NULL);
8529 QCBORDecode_GetNull(&DCtx);
8530 if(QCBORDecode_GetAndResetError(&DCtx)) {
8531 return 7;
8532 }
8533
8534 QCBORDecode_Init(&DCtx,
8535 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMap),
8536 0);
8537 QCBORDecode_EnterMap(&DCtx, NULL);
8538 QCBORDecode_GetNull(&DCtx);
8539 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
8540 return 8;
8541 }
8542
8543 QCBORDecode_Init(&DCtx,
8544 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spNullInMap),
8545 0);
8546 QCBORDecode_EnterMap(&DCtx, NULL);
8547 QCBORDecode_GetNullInMapN(&DCtx, 8);
8548 if(QCBORDecode_GetAndResetError(&DCtx)) {
8549 return 9;
8550 }
8551
8552 QCBORDecode_Init(&DCtx,
8553 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMap),
8554 0);
8555 QCBORDecode_EnterMap(&DCtx, NULL);
8556 QCBORDecode_GetNullInMapN(&DCtx, 8);
8557 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
8558 return 10;
8559 }
8560
8561 QCBORDecode_Init(&DCtx,
8562 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMapNWF),
8563 0);
8564 QCBORDecode_EnterMap(&DCtx, NULL);
8565 QCBORDecode_GetUndefined(&DCtx);
8566 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_HIT_END) {
8567 return 11;
8568 }
8569
8570 QCBORDecode_Init(&DCtx,
8571 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUndefinedInMap),
8572 0);
8573 QCBORDecode_EnterMap(&DCtx, NULL);
8574 QCBORDecode_GetUndefined(&DCtx);
8575 if(QCBORDecode_GetAndResetError(&DCtx)) {
8576 return 12;
8577 }
8578
8579 QCBORDecode_Init(&DCtx,
8580 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMap),
8581 0);
8582 QCBORDecode_EnterMap(&DCtx, NULL);
8583 QCBORDecode_GetUndefined(&DCtx);
8584 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
8585 return 13;
8586 }
8587
8588 QCBORDecode_Init(&DCtx,
8589 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUndefinedInMap),
8590 0);
8591 QCBORDecode_EnterMap(&DCtx, NULL);
8592 QCBORDecode_GetUndefinedInMapN(&DCtx, 8);
8593 if(QCBORDecode_GetAndResetError(&DCtx)) {
8594 return 14;
8595 }
8596
8597 QCBORDecode_Init(&DCtx,
8598 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMap),
8599 0);
8600 QCBORDecode_EnterMap(&DCtx, NULL);
8601 QCBORDecode_GetUndefinedInMapN(&DCtx, 8);
8602 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
8603 return 15;
8604 }
8605
8606 QCBORDecode_Init(&DCtx,
8607 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMapNWF),
8608 0);
8609 QCBORDecode_EnterMap(&DCtx, NULL);
8610 QCBORDecode_GetUndefined(&DCtx);
8611 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_HIT_END) {
8612 return 15;
8613 }
8614
Laurence Lundblade9f9c3732021-03-23 09:38:46 -07008615 return 0;
8616}