blob: 833b5b81e43f2fd7c2d3597d58372b0c856fbd14 [file] [log] [blame]
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001/*==============================================================================
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002 * Copyright (c) 2016-2018, The Linux Foundation.
3 * Copyright (c) 2018-2024, Laurence Lundblade.
4 * Copyright (c) 2021, Arm Limited.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are
9 * met:
Laurence Lundblade0dbc9172018-11-01 14:17:21 +070010 * Redistributions of source code must retain the above copyright
11 notice, this list of conditions and the following disclaimer.
12 * Redistributions in binary form must reproduce the above
13 copyright notice, this list of conditions and the following
14 disclaimer in the documentation and/or other materials provided
15 with the distribution.
16 * Neither the name of The Linux Foundation nor the names of its
17 contributors, nor the name "Laurence Lundblade" may be used to
18 endorse or promote products derived from this software without
19 specific prior written permission.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -080020
Laurence Lundblade0dbc9172018-11-01 14:17:21 +070021THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
22WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
23MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
24ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
25BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
28BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
30OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
31IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Laurence Lundbladeee851742020-01-08 08:37:05 -080032 =============================================================================*/
Laurence Lundblade9e3651c2018-10-10 11:49:55 +080033
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080034#include "qcbor_decode_tests.h"
Laurence Lundblade844bb5c2020-03-01 17:27:25 -080035#include "qcbor/qcbor_encode.h"
36#include "qcbor/qcbor_decode.h"
Laurence Lundblade67257dc2020-07-27 03:33:37 -070037#include "qcbor/qcbor_spiffy_decode.h"
Laurence Lundbladed4728fd2018-12-17 15:15:56 -080038#include <string.h>
Laurence Lundblade9e3651c2018-10-10 11:49:55 +080039#include <math.h> // for fabs()
Laurence Lundbladebb1062e2019-08-12 23:28:54 -070040#include "not_well_formed_cbor.h"
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -080041
Laurence Lundblade9b334962020-08-27 10:55:53 -070042// Handy macro to compare a UsefulBuf to a C string
43#define UsefulBufCompareToSZ(x, y) \
44 UsefulBuf_Compare(x, UsefulBuf_FromSZ(y))
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080045
Laurence Lundbladea2e29072018-12-30 09:20:06 -080046#ifdef PRINT_FUNCTIONS_FOR_DEBUGGING
Laurence Lundblade20db9c92018-12-17 11:40:37 -080047#include <stdio.h>
Laurence Lundbladea2e29072018-12-30 09:20:06 -080048
49static void PrintUsefulBufC(const char *szLabel, UsefulBufC Buf)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080050{
51 if(szLabel) {
52 printf("%s ", szLabel);
53 }
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -080054
Laurence Lundblade570fab52018-10-13 18:28:27 +080055 size_t i;
Laurence Lundbladea2e29072018-12-30 09:20:06 -080056 for(i = 0; i < Buf.len; i++) {
57 uint8_t Z = ((uint8_t *)Buf.ptr)[i];
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080058 printf("%02x ", Z);
59 }
60 printf("\n");
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -080061
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080062 fflush(stdout);
63}
Laurence Lundbladee2c893c2020-12-26 17:41:53 -080064#endif /* PRINT_FUNCTIONS_FOR_DEBUGGING */
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080065
Laurence 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 Lundbladed6b33f52024-03-06 12:51:15 -08001791struct DecodeFailTestInput {
1792 const char *szDescription; /* Description of the test */
1793 QCBORDecodeMode DecoderMode; /* The QCBOR Decoder Mode for test */
1794 UsefulBufC Input; /* Chunk of CBOR that cases error */
1795 QCBORError nError; /* The expected error */
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001796};
1797
Laurence Lundblade59289e52019-12-30 13:44:37 -08001798
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08001799static int32_t
1800ProcessDecodeFailures(const struct DecodeFailTestInput *pFailInputs, const int nNumFails)
Laurence Lundblade59289e52019-12-30 13:44:37 -08001801{
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08001802 int nIndex;
1803 QCBORDecodeContext DCtx;
1804 QCBORError uCBORError;
1805 QCBORItem Item;
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08001806
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08001807 for(nIndex = 0; nIndex < nNumFails; nIndex++) {
1808 const struct DecodeFailTestInput *pF = &pFailInputs[nIndex];
1809
1810 QCBORDecode_Init(&DCtx, pF->Input, pF->DecoderMode);
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08001811
1812#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08001813 /* Set up the decoding context including a memory pool so that
1814 * indefinite length items can be checked.
1815 */
Laurence Lundblade59289e52019-12-30 13:44:37 -08001816 UsefulBuf_MAKE_STACK_UB(Pool, 100);
Laurence Lundblade830fbf92020-05-31 17:22:33 -07001817
Laurence Lundblade4bf6e712020-12-10 11:53:21 -08001818 uCBORError = QCBORDecode_SetMemPool(&DCtx, Pool, 0);
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08001819 if(uCBORError != QCBOR_SUCCESS) {
1820 return -1;
Laurence Lundblade59289e52019-12-30 13:44:37 -08001821 }
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08001822#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
1823
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08001824 if(nIndex == 8) {
1825 uCBORError = 9; /* For setting break points */
Laurence Lundblade37286c02022-09-03 10:05:02 -07001826 }
1827
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08001828 /* Iterate until there is an error of some sort of error */
Laurence Lundblade59289e52019-12-30 13:44:37 -08001829 do {
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08001830 /* Set to something none-zero, something other than QCBOR_TYPE_NONE */
Laurence Lundblade59289e52019-12-30 13:44:37 -08001831 memset(&Item, 0x33, sizeof(Item));
1832
Laurence Lundblade4bf6e712020-12-10 11:53:21 -08001833 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
1834 } while(uCBORError == QCBOR_SUCCESS);
1835
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08001836 /* Must get the expected error or the this test fails
1837 * The data and label type must also be QCBOR_TYPE_NONE.
1838 */
Laurence Lundblade4bf6e712020-12-10 11:53:21 -08001839 if(uCBORError != pF->nError ||
Laurence Lundblade59289e52019-12-30 13:44:37 -08001840 Item.uDataType != QCBOR_TYPE_NONE ||
1841 Item.uLabelType != QCBOR_TYPE_NONE) {
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08001842 return (int32_t)(nIndex * 1000 + (int)uCBORError);
Laurence Lundblade59289e52019-12-30 13:44:37 -08001843 }
1844 }
1845
1846 return 0;
1847}
1848
1849
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08001850static const struct DecodeFailTestInput Failures[] = {
1851 /* Most of this is copied from not_well_formed.h. Here the error
1852 * code returned is also checked.
1853 */
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001854
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08001855#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08001856 /* Indefinite length strings must be closed off */
1857 { "An indefinite length byte string not closed off",
1858 QCBOR_DECODE_MODE_NORMAL,
1859 {"0x5f\x41\x00", 3},
1860 QCBOR_ERR_HIT_END
1861 },
1862 { "An indefinite length text string not closed off",
1863 QCBOR_DECODE_MODE_NORMAL,
1864 {"\x7f\x61\x00", 3},
1865 QCBOR_ERR_HIT_END
1866 },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001867
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08001868 /* All the chunks in an indefinite length string must be of the
1869 * type of indefinite length string
1870 */
1871 { "Indefinite length byte string with text string chunk",
1872 QCBOR_DECODE_MODE_NORMAL,
1873 {"\x5f\x61\x00\xff", 4},
1874 QCBOR_ERR_INDEFINITE_STRING_CHUNK
1875 },
1876 { "Indefinite length text string with a byte string chunk",
1877 QCBOR_DECODE_MODE_NORMAL,
1878 {"\x7f\x41\x00\xff", 4},
1879 QCBOR_ERR_INDEFINITE_STRING_CHUNK
1880 },
1881 { "Indefinite length byte string with a positive integer chunk",
1882 QCBOR_DECODE_MODE_NORMAL,
1883 {"\x5f\x00\xff", 3},
1884 QCBOR_ERR_INDEFINITE_STRING_CHUNK
1885 },
1886 { "Indefinite length byte string with an negative integer chunk",
1887 QCBOR_DECODE_MODE_NORMAL,
1888 {"\x5f\x21\xff", 3},
1889 QCBOR_ERR_INDEFINITE_STRING_CHUNK
1890 },
1891 { "Indefinite length byte string with an array chunk",
1892 QCBOR_DECODE_MODE_NORMAL,
1893 {"\x5f\x80\xff", 3},
1894 QCBOR_ERR_INDEFINITE_STRING_CHUNK
1895 },
1896 { "Indefinite length byte string with an map chunk",
1897 QCBOR_DECODE_MODE_NORMAL,
1898 {"\x5f\xa0\xff", 3},
1899 QCBOR_ERR_INDEFINITE_STRING_CHUNK
1900 },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001901
Laurence Lundblade37286c02022-09-03 10:05:02 -07001902#ifndef QCBOR_DISABLE_TAGS
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08001903 { "Indefinite length byte string with tagged integer chunk",
1904 QCBOR_DECODE_MODE_NORMAL,
1905 {"\x5f\xc0\x00\xff", 4},
1906 QCBOR_ERR_INDEFINITE_STRING_CHUNK
1907 },
Laurence Lundblade37286c02022-09-03 10:05:02 -07001908#else
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08001909 { "Indefinite length byte string with tagged integer chunk",
1910 QCBOR_DECODE_MODE_NORMAL,
1911 {"\x5f\xc0\x00\xff", 4},
1912 QCBOR_ERR_TAGS_DISABLED
1913 },
Laurence Lundblade37286c02022-09-03 10:05:02 -07001914#endif /* QCBOR_DISABLE_TAGS */
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001915
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08001916 { "Indefinite length byte string with an simple type chunk",
1917 QCBOR_DECODE_MODE_NORMAL,
1918 {"\x5f\xe0\xff", 3},
1919 QCBOR_ERR_INDEFINITE_STRING_CHUNK
1920 },
1921 { "???",
1922 QCBOR_DECODE_MODE_NORMAL,
1923 {"\x5f\x5f\x41\x00\xff\xff", 6},
1924 QCBOR_ERR_INDEFINITE_STRING_CHUNK
1925 },
1926 { "indefinite length text string with indefinite string inside",
1927 QCBOR_DECODE_MODE_NORMAL,
1928 {"\x7f\x7f\x61\x00\xff\xff", 6},
1929 QCBOR_ERR_INDEFINITE_STRING_CHUNK
1930 },
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08001931#else /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
1932
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08001933#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
1934
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08001935 /* Definte length maps and arrays must be closed by having the right number of items */
1936 { "A definte length array that is supposed to have 1 item, but has none",
1937 QCBOR_DECODE_MODE_NORMAL,
1938 {"\x81", 1},
1939 QCBOR_ERR_NO_MORE_ITEMS
1940 },
1941 { "A definte length array that is supposed to have 2 items, but has only 1",
1942 QCBOR_DECODE_MODE_NORMAL,
1943 {"\x82\x00", 2},
1944 QCBOR_ERR_NO_MORE_ITEMS
1945 },
1946 { "A definte length array that is supposed to have 511 items, but has only 1",
1947 QCBOR_DECODE_MODE_NORMAL,
1948 {"\x9a\x01\xff\x00", 4},
1949 QCBOR_ERR_HIT_END
1950 },
1951 { "A definte length map that is supposed to have 1 item, but has none",
1952 QCBOR_DECODE_MODE_NORMAL,
1953 {"\xa1", 1},
1954 QCBOR_ERR_NO_MORE_ITEMS
1955 },
1956 { "A definte length map that is supposed to have s item, but has only 1",
1957 QCBOR_DECODE_MODE_NORMAL,
1958 {"\xa2\x01\x02", 3},
1959 QCBOR_ERR_NO_MORE_ITEMS
1960 },
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08001961#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08001962 /* Indefinte length maps and arrays must be ended by a break */
1963 { "Indefinite length array with zero items and no break",
1964 QCBOR_DECODE_MODE_NORMAL,
1965 {"\x9f", 1},
1966 QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001967
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08001968 { "Indefinite length array with two items and no break",
1969 QCBOR_DECODE_MODE_NORMAL,
1970 {"\x9\x01\x02", 3},
1971 QCBOR_ERR_NO_MORE_ITEMS
1972 },
1973 { "Indefinite length map with zero items and no break",
1974 QCBOR_DECODE_MODE_NORMAL,
1975 {"\xbf", 1},
1976 QCBOR_ERR_NO_MORE_ITEMS
1977 },
1978 { "Indefinite length map with two items and no break",
1979 QCBOR_DECODE_MODE_NORMAL,
1980 {"\xbf\x01\x02\x01\x02", 5},
1981 QCBOR_ERR_NO_MORE_ITEMS
1982 },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001983
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08001984 /* Nested maps and arrays must be closed off (some extra nested test vectors) */
1985 { "Unclosed indefinite array containing a closed definite length array",
1986 QCBOR_DECODE_MODE_NORMAL,
1987 {"\x9f\x80\x00", 3},
1988 QCBOR_ERR_NO_MORE_ITEMS
1989 },
1990
1991 { "Definite length array containing an unclosed indefinite length array",
1992 QCBOR_DECODE_MODE_NORMAL,
1993 {"\x81\x9f", 2},
1994 QCBOR_ERR_NO_MORE_ITEMS
1995 },
1996 { "Unclosed indefinite map containing a closed definite length array",
1997 QCBOR_DECODE_MODE_NORMAL,
1998 {"\xbf\x01\x80\x00\xa0", 5},
1999 QCBOR_ERR_NO_MORE_ITEMS
2000 },
2001 { "Definite length map containing an unclosed indefinite length array",
2002 QCBOR_DECODE_MODE_NORMAL,
2003 {"\xa1\x02\x9f", 3},
2004 QCBOR_ERR_NO_MORE_ITEMS
2005 },
2006 { "Deeply nested definite length arrays with deepest one unclosed",
2007 QCBOR_DECODE_MODE_NORMAL,
2008 {"\x81\x81\x81\x81\x81\x81\x81\x81\x81", 9},
2009 QCBOR_ERR_NO_MORE_ITEMS
2010 },
2011 { "Deeply nested indefinite length arrays with deepest one unclosed",
2012 QCBOR_DECODE_MODE_NORMAL,
2013 {"\x9f\x9f\x9f\x9f\x9f\xff\xff\xff\xff", 9},
2014 QCBOR_ERR_NO_MORE_ITEMS
2015 },
2016 { "Mixed nesting with indefinite unclosed",
2017 QCBOR_DECODE_MODE_NORMAL,
2018 {"\x9f\x81\x9f\x81\x9f\x9f\xff\xff\xff", 9},
2019 QCBOR_ERR_NO_MORE_ITEMS },
2020 { "Mixed nesting with definite unclosed",
2021 QCBOR_DECODE_MODE_NORMAL,
2022 {"\x9f\x82\x9f\x81\x9f\x9f\xff\xff\xff\xff", 10},
2023 QCBOR_ERR_BAD_BREAK
2024 },
2025 { "Unclosed indefinite length map in definite length maps",
2026 QCBOR_DECODE_MODE_NORMAL,
2027 {"\xa1\x01\xa2\x02\xbf\xff\x02\xbf", 8},
2028 QCBOR_ERR_NO_MORE_ITEMS
2029 },
2030 { "Unclosed definite length map in indefinite length maps",
2031 QCBOR_DECODE_MODE_NORMAL,
2032 {"\xbf\x01\xbf\x02\xa1", 5},
2033 QCBOR_ERR_NO_MORE_ITEMS
2034 },
2035 { "Unclosed indefinite length array in definite length maps",
2036 QCBOR_DECODE_MODE_NORMAL,
2037 {"\xa1\x01\xa2\x02\x9f\xff\x02\x9f", 8},
2038 QCBOR_ERR_NO_MORE_ITEMS
2039 },
2040 { "Unclosed definite length array in indefinite length maps",
2041 QCBOR_DECODE_MODE_NORMAL,
2042 {"\xbf\x01\xbf\x02\x81", 5},
2043 QCBOR_ERR_NO_MORE_ITEMS
2044 },
2045 { "Unclosed indefinite length map in definite length arrays",
2046 QCBOR_DECODE_MODE_NORMAL,
2047 {"\x81\x82\xbf\xff\xbf", 5},
2048 QCBOR_ERR_NO_MORE_ITEMS
2049 },
2050 { "Unclosed definite length map in indefinite length arrays",
2051 QCBOR_DECODE_MODE_NORMAL,
2052 {"\x9f\x9f\xa1", 3},
2053 QCBOR_ERR_NO_MORE_ITEMS
2054 },
2055
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08002056#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002057
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002058 /* The "argument" for the data item is incomplete */
2059 { "Positive integer missing 1 byte argument",
2060 QCBOR_DECODE_MODE_NORMAL,
2061 {"\x18", 1},
2062 QCBOR_ERR_HIT_END
2063 },
2064 { "Positive integer missing 2 byte argument",
2065 QCBOR_DECODE_MODE_NORMAL,
2066 {"\x19", 1},
2067 QCBOR_ERR_HIT_END
2068 },
2069 { "Positive integer missing 4 byte argument",
2070 QCBOR_DECODE_MODE_NORMAL,
2071 {"\x1a", 1},
2072 QCBOR_ERR_HIT_END
2073 },
2074 { "Positive integer missing 8 byte argument",
2075 QCBOR_DECODE_MODE_NORMAL,
2076 {"\x1b", 1},
2077 QCBOR_ERR_HIT_END
2078 },
2079 { "Positive integer missing 1 byte of 2 byte argument",
2080 QCBOR_DECODE_MODE_NORMAL,
2081 {"\x19\x01", 2},
2082 QCBOR_ERR_HIT_END
2083 },
2084 { "Positive integer missing 2 bytes of 4 byte argument",
2085 QCBOR_DECODE_MODE_NORMAL,
2086 {"\x1a\x01\x02", 3},
2087 QCBOR_ERR_HIT_END
2088 },
2089 { "Positive integer missing 1 bytes of 7 byte argument",
2090 QCBOR_DECODE_MODE_NORMAL,
2091 {"\x1b\x01\x02\x03\x04\x05\x06\x07", 8},
2092 QCBOR_ERR_HIT_END
2093 },
2094 { "Negative integer missing 1 byte argument",
2095 QCBOR_DECODE_MODE_NORMAL,
2096 {"\x38", 1},
2097 QCBOR_ERR_HIT_END
2098 },
2099 { "Binary string missing 1 byte argument",
2100 QCBOR_DECODE_MODE_NORMAL,
2101 {"\x58", 1},
2102 QCBOR_ERR_HIT_END
2103 },
2104 { "Text string missing 1 byte argument",
2105 QCBOR_DECODE_MODE_NORMAL,
2106 {"\x78", 1},
2107 QCBOR_ERR_HIT_END
2108 },
2109 { "Array missing 1 byte argument",
2110 QCBOR_DECODE_MODE_NORMAL,
2111 {"\x98", 1},
2112 QCBOR_ERR_HIT_END
2113 },
2114 { "Map missing 1 byte argument",
2115 QCBOR_DECODE_MODE_NORMAL,
2116 {"\xb8", 1},
2117 QCBOR_ERR_HIT_END
2118 },
2119 { "Tag missing 1 byte argument",
2120 QCBOR_DECODE_MODE_NORMAL,
2121 {"\xd8", 1},
2122 QCBOR_ERR_HIT_END
2123 },
2124 { "Simple missing 1 byte argument",
2125 QCBOR_DECODE_MODE_NORMAL,
2126 {"\xf8", 1},
2127 QCBOR_ERR_HIT_END
2128 },
2129 { "half-precision with 1 byte argument",
2130 QCBOR_DECODE_MODE_NORMAL,
2131 {"\xf9\x00", 2},
2132 QCBOR_ERR_HIT_END
2133 },
2134 { "single-precision with 2 byte argument",
2135 QCBOR_DECODE_MODE_NORMAL,
2136 {"\0xfa\x00\x00", 3},
2137 QCBOR_ERR_HIT_END
2138 },
2139 { "double-precision with 3 byte argument",
2140 QCBOR_DECODE_MODE_NORMAL,
2141 {"\xfb\x00\x00\x00", 4},
2142 QCBOR_ERR_HIT_END
2143 },
Laurence Lundblade2f467f92020-10-09 17:50:11 -07002144
Laurence Lundblade37286c02022-09-03 10:05:02 -07002145#ifndef QCBOR_DISABLE_TAGS
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002146 { "Tag with no content",
2147 QCBOR_DECODE_MODE_NORMAL,
2148 {"\xc0", 1},
2149 QCBOR_ERR_HIT_END
2150 },
Laurence Lundblade37286c02022-09-03 10:05:02 -07002151#else /* QCBOR_DISABLE_TAGS */
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002152 { "Tag with no content",
2153 QCBOR_DECODE_MODE_NORMAL,
2154 {"\xc0", 1},
2155 QCBOR_ERR_TAGS_DISABLED
2156 },
Laurence Lundblade37286c02022-09-03 10:05:02 -07002157#endif /* QCBOR_DISABLE_TAGS */
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002158
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002159 /* Breaks must not occur in definite length arrays and maps */
2160 { "Array of length 1 with sole member replaced by a break",
2161 QCBOR_DECODE_MODE_NORMAL,
2162 {"\x81\xff", 2},
2163 QCBOR_ERR_BAD_BREAK
2164 },
2165 { "Array of length 2 with 2nd member replaced by a break",
2166 QCBOR_DECODE_MODE_NORMAL,
2167 {"\x82\x00\xff", 3},
2168 QCBOR_ERR_BAD_BREAK
2169 },
2170 { "Map of length 1 with sole member label replaced by a break",
2171 QCBOR_DECODE_MODE_NORMAL,
2172 {"\xa1\xff", 2},
2173 QCBOR_ERR_BAD_BREAK
2174 },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002175
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002176 /* Map of length 1 with sole member label replaced by break */
2177 { "Alternate representation that some decoders handle differently",
2178 QCBOR_DECODE_MODE_NORMAL,
2179 {"\xa1\xff\x00", 3},
2180 QCBOR_ERR_BAD_BREAK
2181 },
2182 { "Array of length 1 with 2nd member value replaced by a break",
2183 QCBOR_DECODE_MODE_NORMAL,
2184 {"\xa1\x00\xff", 3},
2185 QCBOR_ERR_BAD_BREAK
2186 },
2187 { "Map of length 2 with 2nd member replaced by a break",
2188 QCBOR_DECODE_MODE_NORMAL,
2189 {"\xa2\x00\x00\xff", 4},
2190 QCBOR_ERR_BAD_BREAK
2191 },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002192
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002193 /* Breaks must not occur on their own out of an indefinite length data item */
2194 { "A bare break is not well formed",
2195 QCBOR_DECODE_MODE_NORMAL,
2196 {"\xff", 1},
2197 QCBOR_ERR_BAD_BREAK
2198 },
2199 { "A bare break after a zero length definite length array",
2200 QCBOR_DECODE_MODE_NORMAL,
2201 {"\x80\xff", 2},
2202 QCBOR_ERR_BAD_BREAK
2203 },
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08002204#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002205 { "A bare break after a zero length indefinite length map",
2206 QCBOR_DECODE_MODE_NORMAL,
2207 {"\x9f\xff\xff", 3},
2208 QCBOR_ERR_BAD_BREAK
2209 },
2210 { "A break inside a definite length array inside an indefenite length array",
2211 QCBOR_DECODE_MODE_NORMAL,
2212 {"\x9f\x81\xff", 3},
2213 QCBOR_ERR_BAD_BREAK
2214 },
2215 { "Complicated mixed nesting with break outside indefinite length array",
2216 QCBOR_DECODE_MODE_NORMAL,
2217 {"\x9f\x82\x9f\x81\x9f\x9f\xff\xff\xff\xff", 10},
2218 QCBOR_ERR_BAD_BREAK },
2219#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
2220
2221 /* Forbidden two byte encodings of simple types */
2222 { "Must use 0xe0 instead",
2223 QCBOR_DECODE_MODE_NORMAL,
2224 {"\xf8\x00", 2},
2225 QCBOR_ERR_BAD_TYPE_7
2226 },
2227 { "Should use 0xe1 instead",
2228 QCBOR_DECODE_MODE_NORMAL,
2229 {"\xf8\x01", 2},
2230 QCBOR_ERR_BAD_TYPE_7
2231 },
2232 { "Should use 0xe2 instead",
2233 QCBOR_DECODE_MODE_NORMAL,
2234 {"\xf8\x02", 2},
2235 QCBOR_ERR_BAD_TYPE_7
2236 }, { "Should use 0xe3 instead",
2237 QCBOR_DECODE_MODE_NORMAL,
2238 {"\xf8\x03", 2},
2239 QCBOR_ERR_BAD_TYPE_7
2240 },
2241 { "Should use 0xe4 instead",
2242 QCBOR_DECODE_MODE_NORMAL,
2243 {"\xf8\x04", 2},
2244 QCBOR_ERR_BAD_TYPE_7
2245 },
2246 { "Should use 0xe5 instead",
2247 QCBOR_DECODE_MODE_NORMAL,
2248 {"\xf8\x05", 2},
2249 QCBOR_ERR_BAD_TYPE_7
2250 },
2251 { "Should use 0xe6 instead",
2252 QCBOR_DECODE_MODE_NORMAL,
2253 {"\xf8\x06", 2},
2254 QCBOR_ERR_BAD_TYPE_7
2255 },
2256 { "Should use 0xe7 instead",
2257 QCBOR_DECODE_MODE_NORMAL,
2258 {"\xf8\x07", 2},
2259 QCBOR_ERR_BAD_TYPE_7
2260 },
2261 { "Should use 0xe8 instead",
2262 QCBOR_DECODE_MODE_NORMAL,
2263 {"\xf8\x08", 2},
2264 QCBOR_ERR_BAD_TYPE_7
2265 },
2266 { "Should use 0xe9 instead",
2267 QCBOR_DECODE_MODE_NORMAL,
2268 {"\xf8\x09", 2},
2269 QCBOR_ERR_BAD_TYPE_7
2270 },
2271 { "Should use 0xea instead",
2272 QCBOR_DECODE_MODE_NORMAL,
2273 {"\xf8\x0a", 2},
2274 QCBOR_ERR_BAD_TYPE_7
2275 },
2276 { "Should use 0xeb instead",
2277 QCBOR_DECODE_MODE_NORMAL,
2278 {"\xf8\x0b", 2},
2279 QCBOR_ERR_BAD_TYPE_7
2280 },
2281 { "Should use 0xec instead",
2282 QCBOR_DECODE_MODE_NORMAL,
2283 {"\xf8\x0c", 2},
2284 QCBOR_ERR_BAD_TYPE_7
2285 },
2286 { "Should use 0xed instead",
2287 QCBOR_DECODE_MODE_NORMAL,
2288 {"\xf8\x0d", 2},
2289 QCBOR_ERR_BAD_TYPE_7
2290 },
2291 { "Should use 0xee instead",
2292 QCBOR_DECODE_MODE_NORMAL,
2293 {"\xf8\x0e", 2},
2294 QCBOR_ERR_BAD_TYPE_7
2295 },
2296 { "Should use 0xef instead",
2297 QCBOR_DECODE_MODE_NORMAL,
2298 {"\xf8\x0f", 2},
2299 QCBOR_ERR_BAD_TYPE_7
2300 },
2301 { "Should use 0xf0 instead",
2302 QCBOR_DECODE_MODE_NORMAL,
2303 {"\xf8\x10", 2},
2304 QCBOR_ERR_BAD_TYPE_7
2305 },
2306 { "Should use 0xf1 instead",
2307 QCBOR_DECODE_MODE_NORMAL,
2308 {"\xf8\x11", 2},
2309 QCBOR_ERR_BAD_TYPE_7
2310 },
2311 { "Should use 0xf2 instead",
2312 QCBOR_DECODE_MODE_NORMAL,
2313 {"\xf8\x12", 2},
2314 QCBOR_ERR_BAD_TYPE_7
2315 },
2316 { "Should use 0xf3 instead",
2317 QCBOR_DECODE_MODE_NORMAL,
2318 {"\xf8\x13", 2},
2319 QCBOR_ERR_BAD_TYPE_7
2320 },
2321 { "Should use 0xf4 instead",
2322 QCBOR_DECODE_MODE_NORMAL,
2323 {"\xf8\x14", 2},
2324 QCBOR_ERR_BAD_TYPE_7
2325 },
2326 { "Should use 0xf5 instead",
2327 QCBOR_DECODE_MODE_NORMAL,
2328 {"\xf8\x15", 2},
2329 QCBOR_ERR_BAD_TYPE_7
2330 },
2331 { "Should use 0xf6 instead",
2332 QCBOR_DECODE_MODE_NORMAL,
2333 {"\xf8\x16", 2},
2334 QCBOR_ERR_BAD_TYPE_7
2335 },
2336 { "Should use 0xef7 instead",
2337 QCBOR_DECODE_MODE_NORMAL,
2338 {"\xf8\x17", 2},
2339 QCBOR_ERR_BAD_TYPE_7
2340 },
2341 { "Should use 0xef8 instead",
2342 QCBOR_DECODE_MODE_NORMAL,
2343 {"\xf8\x18", 2},
2344 QCBOR_ERR_BAD_TYPE_7
2345 },
2346 { "Reserved",
2347 QCBOR_DECODE_MODE_NORMAL,
2348 {"\xf8\x18", 2},
2349 QCBOR_ERR_BAD_TYPE_7
2350 },
2351
2352 /* Maps must have an even number of data items (key & value) */
2353 { "Map with 1 item when it should have 2",
2354 QCBOR_DECODE_MODE_NORMAL,
2355 {"\xa1\x00", 2},
2356 QCBOR_ERR_HIT_END
2357 },
2358 { "Map with 3 item when it should have 4",
2359 QCBOR_DECODE_MODE_NORMAL,
2360 {"\xa2\x00\x00\x00", 2},
2361 QCBOR_ERR_HIT_END
2362 },
2363#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
2364 { "Map with 1 item when it should have 2",
2365 QCBOR_DECODE_MODE_NORMAL,
2366 {"\xbf\x00\xff", 3},
2367 QCBOR_ERR_BAD_BREAK
2368 },
2369 { "Map with 3 item when it should have 4",
2370 QCBOR_DECODE_MODE_NORMAL,
2371 {"\xbf\x00\x00\x00\xff", 5},
2372 QCBOR_ERR_BAD_BREAK
2373 },
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08002374#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002375
2376
Laurence Lundblade37286c02022-09-03 10:05:02 -07002377#ifndef QCBOR_DISABLE_TAGS
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002378 /* In addition to not-well-formed, some invalid CBOR */
2379 { "Text-based date, with an integer",
2380 QCBOR_DECODE_MODE_NORMAL,
2381 {"\xc0\x00", 2},
2382 QCBOR_ERR_BAD_OPT_TAG
2383 },
2384 { "Epoch date, with an byte string",
2385 QCBOR_DECODE_MODE_NORMAL,
2386 {"\xc1\x41\x33", 3},
2387 QCBOR_ERR_BAD_OPT_TAG
2388 },
2389 { "tagged as both epoch and string dates",
2390 QCBOR_DECODE_MODE_NORMAL,
2391 {"\xc1\xc0\x00", 3},
2392 QCBOR_ERR_BAD_OPT_TAG
2393 },
2394 { "big num tagged an int, not a byte string",
2395 QCBOR_DECODE_MODE_NORMAL,
2396 {"\xc2\x00", 2},
2397 QCBOR_ERR_BAD_OPT_TAG
2398 },
Laurence Lundblade37286c02022-09-03 10:05:02 -07002399#else /* QCBOR_DISABLE_TAGS */
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002400 /* In addition to not-well-formed, some invalid CBOR */
2401 { "Text-based date, with an integer",
2402 QCBOR_DECODE_MODE_NORMAL,
2403 {"\xc0\x00", 2},
2404 QCBOR_ERR_TAGS_DISABLED
2405 },
2406 { "Epoch date, with an byte string",
2407 QCBOR_DECODE_MODE_NORMAL,
2408 {"\xc1\x41\x33", 3},
2409 QCBOR_ERR_TAGS_DISABLED
2410 },
2411 { "tagged as both epoch and string dates",
2412 QCBOR_DECODE_MODE_NORMAL,
2413 {"\xc1\xc0\x00", 3},
2414 QCBOR_ERR_TAGS_DISABLED
2415 },
2416 { "big num tagged an int, not a byte string",
2417 QCBOR_DECODE_MODE_NORMAL,
2418 {"\xc2\x00", 2},
2419 QCBOR_ERR_TAGS_DISABLED
2420 },
Laurence Lundblade37286c02022-09-03 10:05:02 -07002421#endif /* QCBOR_DISABLE_TAGS */
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002422};
2423
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002424
2425
2426int32_t
2427DecodeFailureTests(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002428{
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002429 int32_t nResult;
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002430
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002431 nResult = ProcessDecodeFailures(Failures ,C_ARRAY_COUNT(Failures, struct DecodeFailTestInput));
Laurence Lundblade59289e52019-12-30 13:44:37 -08002432 if(nResult) {
2433 return nResult;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002434 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002435
Laurence Lundblade3a6042e2019-06-28 19:58:04 -07002436 // Corrupt the UsefulInputBuf and see that
2437 // it reflected correctly for CBOR decoding
Laurence Lundbladee6f15112020-07-23 18:44:16 -07002438 QCBORDecodeContext DCtx;
2439 QCBORItem Item;
2440 QCBORError uQCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002441
Laurence Lundbladee6f15112020-07-23 18:44:16 -07002442 QCBORDecode_Init(&DCtx,
2443 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSimpleValues),
2444 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002445
Laurence Lundbladee6f15112020-07-23 18:44:16 -07002446 if((uQCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
2447 return (int32_t)uQCBORError;
2448 }
2449 if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.val.uCount != 10) {
2450 // This wasn't supposed to happen
2451 return -1;
2452 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002453
Laurence Lundbladee6f15112020-07-23 18:44:16 -07002454 DCtx.InBuf.magic = 0; // Reach in and corrupt the UsefulInputBuf
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002455
Laurence Lundbladee6f15112020-07-23 18:44:16 -07002456 uQCBORError = QCBORDecode_GetNext(&DCtx, &Item);
2457 if(uQCBORError != QCBOR_ERR_NO_MORE_ITEMS) {
2458 // Did not get back the error expected
2459 return -2;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002460 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002461
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002462
Laurence Lundblade98427e92020-09-28 21:33:23 -07002463 /*
2464 The max size of a string for QCBOR is SIZE_MAX - 4 so this
2465 tests here can be performed to see that the max length
2466 error check works correctly. See DecodeBytes(). If the max
2467 size was SIZE_MAX, it wouldn't be possible to test this.
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002468
Laurence Lundblade98427e92020-09-28 21:33:23 -07002469 This test will automatocally adapt the all CPU sizes
2470 through the use of SIZE_MAX.
2471 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002472
Laurence Lundblade8510f8c2020-12-01 11:31:16 -08002473 UsefulBuf_MAKE_STACK_UB( HeadBuf, QCBOR_HEAD_BUFFER_SIZE);
Laurence Lundblade98427e92020-09-28 21:33:23 -07002474 UsefulBufC EncodedHead;
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002475
Laurence Lundblade98427e92020-09-28 21:33:23 -07002476 // This makes a CBOR head with a text string that is very long
2477 // but doesn't fill in the bytes of the text string as that is
2478 // not needed to test this part of QCBOR.
2479 EncodedHead = QCBOREncode_EncodeHead(HeadBuf, CBOR_MAJOR_TYPE_TEXT_STRING, 0, SIZE_MAX);
2480
2481 QCBORDecode_Init(&DCtx, EncodedHead, QCBOR_DECODE_MODE_NORMAL);
2482
2483 if(QCBOR_ERR_STRING_TOO_LONG != QCBORDecode_GetNext(&DCtx, &Item)) {
2484 return -4;
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002485 }
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002486
Laurence Lundblade3a6042e2019-06-28 19:58:04 -07002487 return 0;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002488}
2489
2490
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002491/* Try all 256 values of the byte at nLen including recursing for
2492 each of the values to try values at nLen+1 ... up to nLenMax
2493 */
Laurence Lundblade06350ea2020-01-27 19:32:40 -08002494static void ComprehensiveInputRecurser(uint8_t *pBuf, size_t nLen, size_t nLenMax)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002495{
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002496 if(nLen >= nLenMax) {
2497 return;
2498 }
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08002499
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002500 for(int inputByte = 0; inputByte < 256; inputByte++) {
2501 // Set up the input
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002502 pBuf[nLen] = (uint8_t)inputByte;
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08002503 const UsefulBufC Input = {pBuf, nLen+1};
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08002504
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002505 // Get ready to parse
2506 QCBORDecodeContext DCtx;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002507 QCBORDecode_Init(&DCtx, Input, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002508
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002509 // Parse by getting the next item until an error occurs
2510 // Just about every possible decoder error can occur here
2511 // The goal of this test is not to check for the correct
2512 // error since that is not really possible. It is to
2513 // see that there is no crash on hostile input.
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002514 while(1) {
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002515 QCBORItem Item;
2516 QCBORError nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002517 if(nCBORError != QCBOR_SUCCESS) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002518 break;
2519 }
2520 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002521
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002522 ComprehensiveInputRecurser(pBuf, nLen+1, nLenMax);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002523 }
2524}
2525
2526
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03002527int32_t ComprehensiveInputTest(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002528{
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002529 // Size 2 tests 64K inputs and runs quickly
2530 uint8_t pBuf[2];
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002531
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002532 ComprehensiveInputRecurser(pBuf, 0, sizeof(pBuf));
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002533
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002534 return 0;
2535}
2536
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002537
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03002538int32_t BigComprehensiveInputTest(void)
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002539{
2540 // size 3 tests 16 million inputs and runs OK
2541 // in seconds on fast machines. Size 4 takes
2542 // 10+ minutes and 5 half a day on fast
2543 // machines. This test is kept separate from
2544 // the others so as to no slow down the use
2545 // of them as a very frequent regression.
2546 uint8_t pBuf[3]; //
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08002547
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002548 ComprehensiveInputRecurser(pBuf, 0, sizeof(pBuf));
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08002549
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002550 return 0;
2551}
2552
2553
Laurence Lundbladecc7da412020-12-27 00:09:07 -08002554static const uint8_t spDateTestInput[] = {
Laurence Lundblade16a207a2021-09-18 17:22:46 -07002555 /* 1. The valid date string "1985-04-12" */
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002556 0xc0, // tag for string date
2557 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002558
Laurence Lundblade16a207a2021-09-18 17:22:46 -07002559 /* 2. An invalid date string due to wrong tag content type */
Laurence Lundbladec7114722020-08-13 05:11:40 -07002560 0xc0, // tag for string date
2561 0x00, // Wrong type for a string date
2562
Laurence Lundblade16a207a2021-09-18 17:22:46 -07002563 /* 3. A valid epoch date, 1400000000; Tue, 13 May 2014 16:53:20 GMT */
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002564 0xc1, // tag for epoch date
2565 0x1a, 0x53, 0x72, 0x4E, 0x00, // Epoch date 1400000000; Tue, 13 May 2014 16:53:20 GMT
2566
Laurence Lundblade16a207a2021-09-18 17:22:46 -07002567 /* 4. An invalid epoch date due to wrong tag content type */
Laurence Lundbladec7114722020-08-13 05:11:40 -07002568 0xc1,
2569 0x62, 'h', 'i', // wrong type tagged
2570
Laurence Lundblade16a207a2021-09-18 17:22:46 -07002571 /* 5. Valid epoch date tag as content for a two other nested tags */
Laurence Lundblade99615302020-11-29 11:19:47 -08002572 // CBOR_TAG_ENC_AS_B64
Laurence Lundblade16a207a2021-09-18 17:22:46 -07002573 0xcf, 0xd8, 0x16, 0xc1, // Epoch date with extra tags
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002574 0x1a, 0x53, 0x72, 0x4E, 0x01,
2575
Laurence Lundblade16a207a2021-09-18 17:22:46 -07002576 /* 6. Epoch date with value to large to fit into int64 */
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002577 0xc1, // tag for epoch date
2578 0x1b, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, // Too large integer
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002579
Laurence Lundblade16a207a2021-09-18 17:22:46 -07002580 /* 7. Epoch date with single-precision value of 1.1. */
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002581 0xc1, // tag for epoch date
Laurence Lundblade3ed0bca2020-07-14 22:50:10 -07002582 0xfa, 0x3f, 0x8c, 0xcc, 0xcd, // single with value 1.1
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002583
Laurence Lundblade16a207a2021-09-18 17:22:46 -07002584 /* 8. Epoch date with too-large single precision float */
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002585 0xc1, // tag for epoch date
Laurence Lundbladea1ad8782019-11-08 00:12:11 -08002586 0xfa, 0x7f, 0x7f, 0xff, 0xff, // 3.4028234663852886e+38 too large
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002587
Laurence Lundblade16a207a2021-09-18 17:22:46 -07002588 /* 9. Epoch date with slightly too-large double precision value */
Laurence Lundbladea1ad8782019-11-08 00:12:11 -08002589 0xc1, // tag for epoch date
2590 0xfb, 0x43, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 9223372036854775808.000000 just barely too large
2591 //0xfa, 0x7f, 0x7f, 0xff, 0xff // 3.4028234663852886e+38 too large
2592
Laurence Lundblade16a207a2021-09-18 17:22:46 -07002593 /* 10. Epoch date with largest supported double precision value */
Laurence Lundbladea1ad8782019-11-08 00:12:11 -08002594 0xc1, // tag for epoch date
Laurence Lundbladec7114722020-08-13 05:11:40 -07002595 0xfb, 0x43, 0xdf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, // 9223372036854773760 largest supported
2596
Laurence Lundblade16a207a2021-09-18 17:22:46 -07002597 /* 11. Epoch date with single-precision NaN */
Laurence Lundbladec7114722020-08-13 05:11:40 -07002598 0xc1, // tag for epoch date
2599 0xfa, 0x7f, 0xc0, 0x00, 0x00, // Single-precision NaN
2600
Laurence Lundblade16a207a2021-09-18 17:22:46 -07002601 /* 12. Epoch date with double precision plus infinity */
Laurence Lundbladec7114722020-08-13 05:11:40 -07002602 0xc1,
2603 0xfb, 0x7f, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // +infinity
2604
Laurence Lundblade16a207a2021-09-18 17:22:46 -07002605 /* 13. Epoch date with half-precision negative infinity */
Laurence Lundbladec7114722020-08-13 05:11:40 -07002606 0xc1, // tag for epoch date
2607 0xf9, 0xfc, 0x00, // -Infinity
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002608};
2609
2610
Laurence Lundbladec7114722020-08-13 05:11:40 -07002611
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002612// have to check float expected only to within an epsilon
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07002613#ifndef QCBOR_DISABLE_FLOAT_HW_USE
Laurence Lundblade02fcf312020-07-17 02:49:46 -07002614static int CHECK_EXPECTED_DOUBLE(double val, double expected) {
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002615
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002616 double diff = val - expected;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002617
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002618 diff = fabs(diff);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002619
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002620 return diff > 0.0000001;
2621}
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07002622#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002623
2624
Laurence Lundblade37286c02022-09-03 10:05:02 -07002625/* Test date decoding using GetNext() */
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03002626int32_t DateParseTest(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002627{
2628 QCBORDecodeContext DCtx;
Laurence Lundbladec7114722020-08-13 05:11:40 -07002629 QCBORItem Item;
2630 QCBORError uError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002631
Laurence Lundbladeee851742020-01-08 08:37:05 -08002632 QCBORDecode_Init(&DCtx,
2633 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spDateTestInput),
2634 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002635
Laurence Lundblade16a207a2021-09-18 17:22:46 -07002636 /* 1. The valid date string "1985-04-12" */
Laurence Lundbladec7114722020-08-13 05:11:40 -07002637 if((uError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002638 return -1;
Laurence Lundbladec7114722020-08-13 05:11:40 -07002639 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002640 if(Item.uDataType != QCBOR_TYPE_DATE_STRING ||
Laurence Lundbladeba587682024-02-07 16:46:43 -08002641 UsefulBufCompareToSZ(Item.val.string, "1985-04-12")){
Laurence Lundblade67bd5512018-11-02 21:44:06 +07002642 return -2;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002643 }
2644
Laurence Lundblade16a207a2021-09-18 17:22:46 -07002645 /* 2. An invalid date string due to wrong tag content type */
Laurence Lundbladec7114722020-08-13 05:11:40 -07002646 uError = QCBORDecode_GetNext(&DCtx, &Item);
2647 if(uError != QCBOR_ERR_BAD_OPT_TAG) {
Laurence Lundblade67bd5512018-11-02 21:44:06 +07002648 return -3;
Laurence Lundbladec7114722020-08-13 05:11:40 -07002649 }
2650
Laurence Lundblade16a207a2021-09-18 17:22:46 -07002651 /* 3. A valid epoch date, 1400000000; Tue, 13 May 2014 16:53:20 GMT */
2652 uError = QCBORDecode_GetNext(&DCtx, &Item);
2653 if(uError != QCBOR_SUCCESS) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002654 return -4;
2655 }
Laurence Lundblade16a207a2021-09-18 17:22:46 -07002656 if(uError == QCBOR_SUCCESS) {
2657 if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH ||
2658 Item.val.epochDate.nSeconds != 1400000000
Máté Tóth-Pálef5f07a2021-09-17 19:31:37 +02002659#ifndef USEFULBUF_DISABLE_ALL_FLOAT
Laurence Lundblade16a207a2021-09-18 17:22:46 -07002660 || Item.val.epochDate.fSecondsFraction != 0
Máté Tóth-Pálef5f07a2021-09-17 19:31:37 +02002661#endif /* USEFULBUF_DISABLE_ALL_FLOAT */
Laurence Lundblade16a207a2021-09-18 17:22:46 -07002662 ) {
2663 return -5;
2664 }
Laurence Lundbladec7114722020-08-13 05:11:40 -07002665 }
2666
Laurence Lundblade16a207a2021-09-18 17:22:46 -07002667 /* 4. An invalid epoch date due to wrong tag content type */
Laurence Lundbladec7114722020-08-13 05:11:40 -07002668 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_OPT_TAG) {
2669 return -6;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002670 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002671
Laurence Lundblade16a207a2021-09-18 17:22:46 -07002672 /* 5. Valid epoch date tag as content for a two other nested tags */
Laurence Lundblade99615302020-11-29 11:19:47 -08002673 // Epoch date wrapped in an CBOR_TAG_ENC_AS_B64 and an unknown tag.
2674 // The date is decoded and the two tags are returned. This is to
2675 // make sure the wrapping of epoch date in another tag works OK.
Laurence Lundbladec7114722020-08-13 05:11:40 -07002676 if((uError = QCBORDecode_GetNext(&DCtx, &Item))) {
2677 return -7;
2678 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002679 if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH ||
2680 Item.val.epochDate.nSeconds != 1400000001 ||
Máté Tóth-Pálef5f07a2021-09-17 19:31:37 +02002681#ifndef USEFULBUF_DISABLE_ALL_FLOAT
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002682 Item.val.epochDate.fSecondsFraction != 0 ||
Máté Tóth-Pálef5f07a2021-09-17 19:31:37 +02002683#endif /* USEFULBUF_DISABLE_ALL_FLOAT */
Laurence Lundblade99615302020-11-29 11:19:47 -08002684 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_ENC_AS_B64)) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002685 return -8;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002686 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002687
Laurence Lundblade16a207a2021-09-18 17:22:46 -07002688 /* 6. Epoch date with value to large to fit into int64 */
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002689 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_DATE_OVERFLOW) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002690 return -9;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002691 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002692
Laurence Lundblade16a207a2021-09-18 17:22:46 -07002693 /* 7. Epoch date with single-precision value of 1.1. */
2694 uError = QCBORDecode_GetNext(&DCtx, &Item);
2695 if(uError != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002696 return -10;
2697 }
Laurence Lundblade16a207a2021-09-18 17:22:46 -07002698 if(uError == QCBOR_SUCCESS) {
2699 if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH ||
2700 Item.val.epochDate.nSeconds != 1
2701#ifndef QCBOR_DISABLE_FLOAT_HW_USE
2702 || CHECK_EXPECTED_DOUBLE(Item.val.epochDate.fSecondsFraction, 0.1)
Máté Tóth-Pálef5f07a2021-09-17 19:31:37 +02002703#endif /* USEFULBUF_DISABLE_ALL_FLOAT */
Laurence Lundblade16a207a2021-09-18 17:22:46 -07002704 ) {
2705 return -11;
2706 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002707 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002708
Laurence Lundblade16a207a2021-09-18 17:22:46 -07002709 /* 8. Epoch date with too-large single-precision float */
2710 uError = QCBORDecode_GetNext(&DCtx, &Item);
2711 if(uError != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_DATE_OVERFLOW)) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002712 return -12;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002713 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002714
Laurence Lundblade16a207a2021-09-18 17:22:46 -07002715 /* 9. Epoch date with slightly too-large double-precision value */
2716 uError = QCBORDecode_GetNext(&DCtx, &Item);
2717 if(uError != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_DATE_OVERFLOW)) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002718 return -13;
Laurence Lundbladea1ad8782019-11-08 00:12:11 -08002719 }
2720
Laurence Lundblade16a207a2021-09-18 17:22:46 -07002721 /* 10. Epoch date with largest supported double-precision value */
2722 uError = QCBORDecode_GetNext(&DCtx, &Item);
2723 if(uError != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)) {
2724 return -14;
2725 }
2726 if(uError == QCBOR_SUCCESS) {
2727 if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH ||
2728 Item.val.epochDate.nSeconds != 9223372036854773760
2729#ifndef QCBOR_DISABLE_FLOAT_HW_USE
2730 || Item.val.epochDate.fSecondsFraction != 0.0
2731#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
2732 ) {
2733 return -14;
2734 }
2735 }
Laurence Lundbladec7114722020-08-13 05:11:40 -07002736
Laurence Lundblade16a207a2021-09-18 17:22:46 -07002737 /* 11. Epoch date with single-precision NaN */
2738 if(QCBORDecode_GetNext(&DCtx, &Item) != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_DATE_OVERFLOW)) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002739 return -15;
2740 }
2741
Laurence Lundblade16a207a2021-09-18 17:22:46 -07002742 /* 12. Epoch date with double-precision plus infinity */
2743 if(QCBORDecode_GetNext(&DCtx, &Item) != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_DATE_OVERFLOW)) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002744 return -16;
2745 }
2746
Laurence Lundblade16a207a2021-09-18 17:22:46 -07002747 /* 13. Epoch date with half-precision negative infinity */
2748 uError = QCBORDecode_GetNext(&DCtx, &Item);
2749 if(uError != FLOAT_ERR_CODE_NO_HALF_PREC_NO_FLOAT_HW(QCBOR_ERR_DATE_OVERFLOW)) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002750 return -17;
2751 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002752
2753 return 0;
2754}
2755
Laurence Lundblade37286c02022-09-03 10:05:02 -07002756
Laurence Lundblade4b270642020-08-14 12:53:07 -07002757/*
2758 Test cases covered here. Some items cover more than one of these.
2759 positive integer (zero counts as a positive integer)
2760 negative integer
2761 half-precision float
2762 single-precision float
2763 double-precision float
Laurence Lundbladec7114722020-08-13 05:11:40 -07002764
Laurence Lundblade4b270642020-08-14 12:53:07 -07002765 float Overflow error
2766 Wrong type error for epoch
2767 Wrong type error for date string
2768 float disabled error
2769 half-precision disabled error
2770 -Infinity
2771 Slightly too large integer
2772 Slightly too far from zero
Laurence Lundbladec7114722020-08-13 05:11:40 -07002773
Laurence Lundblade4b270642020-08-14 12:53:07 -07002774 Get epoch by int
2775 Get string by int
2776 Get epoch by string
2777 Get string by string
2778 Fail to get epoch by wrong int label
2779 Fail to get string by wrong string label
2780 Fail to get epoch by string because it is invalid
2781 Fail to get epoch by int because it is invalid
2782
2783 Untagged values
2784 */
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08002785static const uint8_t spSpiffyDateTestInput[] = {
Laurence Lundblade37286c02022-09-03 10:05:02 -07002786 0x87, // array of 7 items
2787
2788 0xa6, // Open a map for tests involving untagged items with labels.
2789
2790 // Untagged integer 0
2791 0x08,
2792 0x00,
2793
2794 // Utagged date string with string label y
2795 0x61, 0x79,
2796 0x6a, '2','0','8','5','-','0','4','-','1','2', // Untagged date string
2797
2798 // Untagged single-precision float with value 3.14 with string label x
2799 0x61, 0x78,
2800 0xFA, 0x40, 0x48, 0xF5, 0xC3,
2801
2802 // Untagged half-precision float with value -2
2803 0x09,
2804 0xF9, 0xC0, 0x00,
2805
2806 /* Untagged date-only date string */
2807 0x18, 0x63,
2808 0x6A, 0x31, 0x39, 0x38, 0x35, 0x2D, 0x30, 0x34, 0x2D, 0x31, 0x32, /* "1985-04-12" */
2809
2810 /* Untagged days-count epoch date */
2811 0x11,
2812 0x19, 0x0F, 0x9A, /* 3994 */
2813
2814 // End of map, back to array
2815
2816 0xa7, // Open map of tagged items with labels
2817
2818 0x00,
2819 0xc0, // tag for string date
2820 0x6a, '1','9','8','5','-','0','4','-','1','2', // Tagged date string
2821
2822
2823 0x01,
2824 0xda, 0x03, 0x03, 0x03, 0x03, // An additional tag
2825 0xc1, // tag for epoch date
2826 0x1a, 0x53, 0x72, 0x4E, 0x00, // Epoch date 1400000000; Tue, 13 May 2014 16:53:20 GMT
2827
2828 0x05,
2829 0xc1,
2830 0xfb, 0xc3, 0xdf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, // -9223372036854773760 largest negative
2831
2832
2833 0x07,
2834 0xc1, // tag for epoch date
2835 0xfb, 0x43, 0xdf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, // 9223372036854773760 largest supported
2836
2837 /* Tagged days-count epoch date */
2838 0x63, 0x53, 0x44, 0x45,
2839 0xD8, 0x64, /* tag(100) */
2840 0x39, 0x29, 0xB3, /* -10676 */
2841
2842 // Untagged -1000 with label z
2843 0x61, 0x7a,
2844 0xda, 0x01, 0x01, 0x01, 0x01, // An additional tag
2845 0x39, 0x03, 0xe7,
2846
2847 /* Tagged date-only date string */
2848 0x63, 0x53, 0x44, 0x53,
2849 0xD9, 0x03, 0xEC,
2850 0x6A, 0x31, 0x39, 0x38, 0x35, 0x2D, 0x30, 0x34, 0x2D, 0x31, 0x32, /* "1985-04-12" */
2851
2852 // End of map of tagged items
Laurence Lundblade4b270642020-08-14 12:53:07 -07002853
2854 0xc1,
2855 0xfb, 0xc3, 0xdf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, // -9.2233720368547748E+18, too negative
2856
Laurence Lundbladec7114722020-08-13 05:11:40 -07002857 0xc1, // tag for epoch date
Laurence Lundblade4b270642020-08-14 12:53:07 -07002858 0x1b, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, // Too-large integer
2859
2860 0xc1, // tag for epoch date
2861 0xf9, 0xfc, 0x00, // Half-precision -Infinity
2862
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07002863 // These two at the end because they are unrecoverable errors
2864 0xc1, // tag for epoch date
2865 0x80, // Erroneous empty array as content for date
2866
2867 0xc0, // tag for string date
2868 0xa0 // Erroneous empty map as content for date
Laurence Lundbladec7114722020-08-13 05:11:40 -07002869};
2870
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03002871int32_t SpiffyDateDecodeTest(void)
Laurence Lundbladec7114722020-08-13 05:11:40 -07002872{
2873 QCBORDecodeContext DC;
Laurence Lundblade4b270642020-08-14 12:53:07 -07002874 QCBORError uError;
Laurence Lundblade37286c02022-09-03 10:05:02 -07002875 int64_t nEpochDate3, nEpochDate5,
2876 nEpochDate4, nEpochDate6,
2877 nEpochDays2;
2878 UsefulBufC StringDate1, StringDate2, StringDays2;
Laurence Lundbladec7114722020-08-13 05:11:40 -07002879
2880 QCBORDecode_Init(&DC,
Laurence Lundblade4b270642020-08-14 12:53:07 -07002881 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSpiffyDateTestInput),
Laurence Lundbladec7114722020-08-13 05:11:40 -07002882 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade37286c02022-09-03 10:05:02 -07002883
2884 /* Items are in an array or map to test look up by label and other
2885 * that might not occur in isolated items. But it does make the
2886 * test a bit messy. */
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07002887 QCBORDecode_EnterArray(&DC, NULL);
Laurence Lundbladec7114722020-08-13 05:11:40 -07002888
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07002889 QCBORDecode_EnterMap(&DC, NULL);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002890
Laurence Lundblade4b270642020-08-14 12:53:07 -07002891 // A single-precision date
Laurence Lundblade9b334962020-08-27 10:55:53 -07002892 QCBORDecode_GetEpochDateInMapSZ(&DC, "x", QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
2893 &nEpochDate5);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002894 uError = QCBORDecode_GetAndResetError(&DC);
Laurence Lundblade16a207a2021-09-18 17:22:46 -07002895 if(uError != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002896 return 104;
2897 }
Laurence Lundblade16a207a2021-09-18 17:22:46 -07002898 if(uError == QCBOR_SUCCESS) {
2899 if(nEpochDate5 != 3) {
2900 return 103;
2901 }
2902 }
Laurence Lundbladec7114722020-08-13 05:11:40 -07002903
Laurence Lundblade9b334962020-08-27 10:55:53 -07002904 // A half-precision date with value -2 FFF
2905 QCBORDecode_GetEpochDateInMapN(&DC, 9, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
2906 &nEpochDate4);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002907 uError = QCBORDecode_GetAndResetError(&DC);
Laurence Lundblade16a207a2021-09-18 17:22:46 -07002908 if(uError != FLOAT_ERR_CODE_NO_HALF_PREC_NO_FLOAT_HW(QCBOR_SUCCESS)) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002909 return 106;
2910 }
Laurence Lundblade16a207a2021-09-18 17:22:46 -07002911 if(uError == QCBOR_SUCCESS) {
2912 if(nEpochDate4 != -2) {
2913 return 105;
2914 }
2915 }
Laurence Lundblade4b270642020-08-14 12:53:07 -07002916
2917 // Fail to get an epoch date by string label
Laurence Lundblade9b334962020-08-27 10:55:53 -07002918 QCBORDecode_GetEpochDateInMapSZ(&DC, "no-label",
2919 QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
2920 &nEpochDate6);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002921 uError = QCBORDecode_GetAndResetError(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002922 if(uError != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002923 return 107;
2924 }
2925
2926 // Fail to get an epoch date by integer label
Laurence Lundblade9b334962020-08-27 10:55:53 -07002927 QCBORDecode_GetEpochDateInMapN(&DC, 99999, QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
2928 &nEpochDate6);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002929 uError = QCBORDecode_GetAndResetError(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002930 if(uError != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002931 return 108;
2932 }
2933
2934 // Fail to get a string date by string label
Laurence Lundblade9b334962020-08-27 10:55:53 -07002935 QCBORDecode_GetDateStringInMapSZ(&DC, "no-label",
2936 QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
2937 &StringDate1);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002938 uError = QCBORDecode_GetAndResetError(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002939 if(uError != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002940 return 109;
2941 }
2942
2943 // Fail to get a string date by integer label
Laurence Lundblade9b334962020-08-27 10:55:53 -07002944 QCBORDecode_GetDateStringInMapN(&DC, 99999, QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
2945 &StringDate1);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002946 uError = QCBORDecode_GetAndResetError(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002947 if(uError != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002948 return 110;
2949 }
2950
2951 // The rest of these succeed even if float features are disabled
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002952
Laurence Lundblade37286c02022-09-03 10:05:02 -07002953
2954 // Untagged integer 0
2955 QCBORDecode_GetEpochDateInMapN(&DC, 8, QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
2956 &nEpochDate3);
2957 // Untagged date string
2958 QCBORDecode_GetDateStringInMapSZ(&DC, "y", QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
2959 &StringDate2);
2960
2961 QCBORDecode_GetDaysStringInMapN(&DC, 99, QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
2962 &StringDays2);
2963
2964 QCBORDecode_GetEpochDaysInMapN(&DC, 17, QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
2965 &nEpochDays2);
2966
2967 QCBORDecode_ExitMap(&DC);
2968 if(QCBORDecode_GetError(&DC) != QCBOR_SUCCESS) {
2969 return 3001;
2970 }
2971
2972 // The map of tagged items
2973 QCBORDecode_EnterMap(&DC, NULL);
2974
2975#ifndef QCBOR_DISABLE_TAGS
2976 int64_t nEpochDate2,
2977 nEpochDateFail,
2978 nEpochDate1400000000, nEpochDays1;
2979 UsefulBufC StringDays1;
2980 uint64_t uTag1, uTag2;
2981
2982 // Tagged date string
2983 QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
2984 &StringDate1);
2985
Laurence Lundblade4b270642020-08-14 12:53:07 -07002986 // Epoch date 1400000000; Tue, 13 May 2014 16:53:20 GMT
Laurence Lundblade9b334962020-08-27 10:55:53 -07002987 QCBORDecode_GetEpochDateInMapN(&DC,
2988 1,
2989 QCBOR_TAG_REQUIREMENT_TAG |
2990 QCBOR_TAG_REQUIREMENT_ALLOW_ADDITIONAL_TAGS,
2991 &nEpochDate1400000000);
2992 uTag1 = QCBORDecode_GetNthTagOfLast(&DC, 0);
Laurence Lundblade37286c02022-09-03 10:05:02 -07002993
2994 // Get largest negative double precision epoch date allowed
2995 QCBORDecode_GetEpochDateInMapN(&DC,
2996 5,
2997 QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG |
2998 QCBOR_TAG_REQUIREMENT_ALLOW_ADDITIONAL_TAGS,
2999 &nEpochDate2);
3000 uError = QCBORDecode_GetAndResetError(&DC);
3001 if(uError != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)) {
3002 return 102;
3003 }
3004 if(uError == QCBOR_SUCCESS) {
3005 if(nEpochDate2 != -9223372036854773760LL) {
3006 return 101;
3007 }
3008 }
3009
Laurence Lundblade4b270642020-08-14 12:53:07 -07003010 // Untagged -1000 with label z
Laurence Lundblade9b334962020-08-27 10:55:53 -07003011 QCBORDecode_GetEpochDateInMapSZ(&DC,
3012 "z",
3013 QCBOR_TAG_REQUIREMENT_NOT_A_TAG |
3014 QCBOR_TAG_REQUIREMENT_ALLOW_ADDITIONAL_TAGS,
3015 &nEpochDate6);
3016 uTag2 = QCBORDecode_GetNthTagOfLast(&DC, 0);
Laurence Lundblade4b270642020-08-14 12:53:07 -07003017
Laurence Lundblade37286c02022-09-03 10:05:02 -07003018
3019 // Get largest double precision epoch date allowed
3020 QCBORDecode_GetEpochDateInMapN(&DC, 7, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
3021 &nEpochDate2);
3022 uError = QCBORDecode_GetAndResetError(&DC);
3023 if(uError != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)) {
3024 return 112;
3025 }
3026 if(uError == QCBOR_SUCCESS) {
3027 if(nEpochDate2 != 9223372036854773760ULL) {
3028 return 111;
3029 }
3030 }
3031
Laurence Lundblade46d63e92021-05-13 11:37:10 -07003032 /* The days format is much simpler than the date format
3033 * because it can't be a floating point value. The test
3034 * of the spiffy decode functions sufficiently covers
3035 * the test of the non-spiffy decode days date decoding.
3036 * There is no full fan out of the error conditions
3037 * and decode options as that is implemented by code
3038 * that is tested well by the date testing above.
3039 */
3040 QCBORDecode_GetDaysStringInMapSZ(&DC, "SDS", QCBOR_TAG_REQUIREMENT_TAG,
3041 &StringDays1);
3042
Laurence Lundblade46d63e92021-05-13 11:37:10 -07003043 QCBORDecode_GetEpochDaysInMapSZ(&DC, "SDE", QCBOR_TAG_REQUIREMENT_TAG,
3044 &nEpochDays1);
3045
Laurence Lundblade4b270642020-08-14 12:53:07 -07003046 QCBORDecode_ExitMap(&DC);
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07003047 if(QCBORDecode_GetError(&DC) != QCBOR_SUCCESS) {
3048 return 3001;
3049 }
3050
Laurence Lundblade37286c02022-09-03 10:05:02 -07003051 // Too-negative float, -9.2233720368547748E+18
3052 QCBORDecode_GetEpochDate(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nEpochDateFail);
3053 uError = QCBORDecode_GetAndResetError(&DC);
3054 if(uError != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_DATE_OVERFLOW)) {
3055 return 1111;
3056 }
3057
3058 // Too-large integer
3059 QCBORDecode_GetEpochDate(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nEpochDateFail);
3060 uError = QCBORDecode_GetAndResetError(&DC);
3061 if(uError != QCBOR_ERR_DATE_OVERFLOW) {
3062 return 1;
3063 }
3064
3065 // Half-precision minus infinity
3066 QCBORDecode_GetEpochDate(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nEpochDateFail);
3067 uError = QCBORDecode_GetAndResetError(&DC);
3068 if(uError != FLOAT_ERR_CODE_NO_HALF_PREC_NO_FLOAT_HW(QCBOR_ERR_DATE_OVERFLOW)) {
3069 return 2;
3070 }
3071
3072
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07003073 // Bad content for epoch date
3074 QCBORDecode_GetEpochDate(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nEpochDateFail);
3075 uError = QCBORDecode_GetAndResetError(&DC);
3076 if(uError != QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT) {
3077 return 3;
3078 }
3079
3080 // Bad content for string date
3081 QCBORDecode_GetDateString(&DC, QCBOR_TAG_REQUIREMENT_TAG, &StringDate1);
3082 uError = QCBORDecode_GetAndResetError(&DC);
3083 if(uError != QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT) {
3084 return 4;
3085 }
3086
Laurence Lundblade4b270642020-08-14 12:53:07 -07003087 QCBORDecode_ExitArray(&DC);
3088 uError = QCBORDecode_Finish(&DC);
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07003089 if(uError != QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT) {
Laurence Lundblade9b334962020-08-27 10:55:53 -07003090 return 1000 + (int32_t)uError;
Laurence Lundblade4b270642020-08-14 12:53:07 -07003091 }
Laurence Lundblade37286c02022-09-03 10:05:02 -07003092#else /* QCBOR_DISABLE_TAGS */
3093 QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
3094 &StringDate1);
3095 uError = QCBORDecode_GetAndResetError(&DC);
3096 if(uError != QCBOR_ERR_TAGS_DISABLED) {
3097 return 4;
3098 }
3099#endif /* QCBOR_DISABLE_TAGS */
3100
3101
3102#ifndef QCBOR_DISABLE_TAGS
Laurence Lundblade4b270642020-08-14 12:53:07 -07003103
Laurence Lundblade9b334962020-08-27 10:55:53 -07003104 if(nEpochDate1400000000 != 1400000000) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07003105 return 200;
3106 }
3107
Laurence Lundblade9b334962020-08-27 10:55:53 -07003108 if(uTag1 != 0x03030303) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07003109 return 201;
3110 }
3111
Laurence Lundblade37286c02022-09-03 10:05:02 -07003112 if(nEpochDays1 != -10676) {
3113 return 205;
Laurence Lundblade4b270642020-08-14 12:53:07 -07003114 }
3115
Laurence Lundblade37286c02022-09-03 10:05:02 -07003116 if(UsefulBuf_Compare(StringDays1, UsefulBuf_FromSZ("1985-04-12"))) {
3117 return 207;
Laurence Lundblade4b270642020-08-14 12:53:07 -07003118 }
3119
Laurence Lundblade9b334962020-08-27 10:55:53 -07003120 if(uTag2 != 0x01010101) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07003121 return 204;
3122 }
3123
Laurence Lundblade37286c02022-09-03 10:05:02 -07003124 if(nEpochDate6 != -1000) {
3125 return 203;
Laurence Lundblade46d63e92021-05-13 11:37:10 -07003126 }
3127
Laurence Lundblade9b334962020-08-27 10:55:53 -07003128 if(UsefulBuf_Compare(StringDate1, UsefulBuf_FromSZ("1985-04-12"))) {
3129 return 205;
3130 }
3131
Laurence Lundblade37286c02022-09-03 10:05:02 -07003132#endif /* QCBOR_DISABLE_TAGS */
3133
3134 if(nEpochDate3 != 0) {
3135 return 202;
3136 }
3137
3138 if(nEpochDays2 != 3994) {
Laurence Lundblade9b334962020-08-27 10:55:53 -07003139 return 206;
3140 }
3141
Laurence Lundblade37286c02022-09-03 10:05:02 -07003142 if(UsefulBuf_Compare(StringDate2, UsefulBuf_FromSZ("2085-04-12"))) {
3143 return 206;
Laurence Lundblade46d63e92021-05-13 11:37:10 -07003144 }
3145
3146 if(UsefulBuf_Compare(StringDays2, UsefulBuf_FromSZ("1985-04-12"))) {
3147 return 208;
3148 }
3149
Laurence Lundbladec7114722020-08-13 05:11:40 -07003150 return 0;
3151}
3152
3153
Laurence Lundblade9b334962020-08-27 10:55:53 -07003154// Input for one of the tagging tests
Laurence Lundbladecc7da412020-12-27 00:09:07 -08003155static const uint8_t spTagInput[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003156 0xd9, 0xd9, 0xf7, // CBOR magic number
Laurence Lundblade9b334962020-08-27 10:55:53 -07003157 0x81, // Array of one
3158 0xd8, 0x04, // non-preferred serialization of tag 4, decimal fraction
3159 0x82, // Array of two that is the faction 1/3
3160 0x01,
3161 0x03,
3162
3163 /*
3164 More than 4 tags on an item 225(226(227(228(229([])))))
3165 */
3166 0xd8, 0xe1,
3167 0xd8, 0xe2,
3168 0xd8, 0xe3,
3169 0xd8, 0xe4,
3170 0xd8, 0xe5,
3171 0x80,
3172
3173 /* tag 10489608748473423768(
3174 2442302356(
3175 21590(
3176 240(
3177 []))))
3178 */
3179 0xdb, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98,
3180 0xda, 0x91, 0x92, 0x93, 0x94,
3181 0xd9, 0x54, 0x56,
3182 0xd8, 0xf0,
3183 0x80,
3184
3185 /* tag 21590(
3186 10489608748473423768(
3187 2442302357(
3188 65534(
3189 []))))
3190 */
3191 0xdb, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x54, 0x56,
3192 0xdb, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98,
3193 0xda, 0x91, 0x92, 0x93, 0x95,
3194 0xd9, 0xff, 0xfe,
3195 0x80,
3196
3197 /* Make sure to blow past the limit of tags that must be mapped.
3198 works in conjuntion with entries above.
3199 269488144(269488145(269488146(269488147([]))))
3200 */
3201 0xda, 0x10, 0x10, 0x10, 0x10,
3202 0xda, 0x10, 0x10, 0x10, 0x11,
3203 0xda, 0x10, 0x10, 0x10, 0x12,
3204 0xda, 0x10, 0x10, 0x10, 0x13,
3205 0x80,
3206
3207 /* An invalid decimal fraction with an additional tag */
3208 0xd9, 0xff, 0xfa,
3209 0xd8, 0x02, // non-preferred serialization of tag 2, a big num
3210 0x00, // the integer 0; should be a byte string
3211};
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003212
Laurence Lundblade59289e52019-12-30 13:44:37 -08003213/*
3214 DB 9192939495969798 # tag(10489608748473423768)
Laurence Lundblade9b334962020-08-27 10:55:53 -07003215 80 # array(0)
Laurence Lundblade59289e52019-12-30 13:44:37 -08003216 */
Laurence Lundbladecc7da412020-12-27 00:09:07 -08003217static const uint8_t spEncodedLargeTag[] = {0xdb, 0x91, 0x92, 0x93, 0x94, 0x95,
Laurence Lundbladeee851742020-01-08 08:37:05 -08003218 0x96, 0x97, 0x98, 0x80};
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003219
Laurence Lundblade59289e52019-12-30 13:44:37 -08003220/*
3221DB 9192939495969798 # tag(10489608748473423768)
3222 D8 88 # tag(136)
3223 C6 # tag(6)
3224 C7 # tag(7)
3225 80 # array(0)
3226*/
Laurence Lundbladecc7da412020-12-27 00:09:07 -08003227static const uint8_t spLotsOfTags[] = {0xdb, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96,
Laurence Lundbladeee851742020-01-08 08:37:05 -08003228 0x97, 0x98, 0xd8, 0x88, 0xc6, 0xc7, 0x80};
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003229
3230/*
Laurence Lundblade9b334962020-08-27 10:55:53 -07003231 55799(55799(55799({
3232 6(7(-23)): 5859837686836516696(7({
3233 7(-20): 11({
3234 17(-18): 17(17(17("Organization"))),
3235 9(-17): 773("SSG"),
3236 -15: 16(17(6(7("Confusion")))),
3237 17(-16): 17("San Diego"),
3238 17(-14): 17("US")
3239 }),
3240 23(-19): 19({
3241 -11: 9({
3242 -9: -7
3243 }),
3244 90599561(90599561(90599561(-10))): 12(h'0102030405060708090A')
3245 })
3246 })),
3247 16(-22): 23({
3248 11(8(7(-5))): 8(-3)
3249 })
3250 })))
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003251 */
Laurence Lundbladecc7da412020-12-27 00:09:07 -08003252static const uint8_t spCSRWithTags[] = {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003253 0xd9, 0xd9, 0xf7, 0xd9, 0xd9, 0xf7, 0xd9, 0xd9, 0xf7, 0xa2,
3254 0xc6, 0xc7, 0x36,
3255 0xdb, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0xc7, 0xa2,
3256 0xda, 0x00, 0x00, 0x00, 0x07, 0x33,
3257 0xcb, 0xa5,
3258 0xd1, 0x31,
3259 0xd1, 0xd1, 0xd1, 0x6c,
3260 0x4f, 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74, 0x69, 0x6f, 0x6e,
3261 0xc9, 0x30,
3262 0xd9, 0x03, 0x05, 0x63,
3263 0x53, 0x53, 0x47,
3264 0x2e,
Laurence Lundblade9b334962020-08-27 10:55:53 -07003265 0xd0, 0xd1, 0xc6, 0xc7,
3266 0x69,
3267 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73, 0x69, 0x6f, 0x6e,
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003268 0xd1, 0x2f,
3269 0xd1, 0x69,
3270 0x53, 0x61, 0x6e, 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f,
3271 0xd1, 0x2d,
3272 0xd1, 0x62,
3273 0x55, 0x53,
3274 0xd7, 0x32,
3275 0xd3, 0xa2,
3276 0x2a,
3277 0xc9, 0xa1,
3278 0x28,
3279 0x26,
3280 0xda, 0x05, 0x66, 0x70, 0x89, 0xda, 0x05, 0x66, 0x70, 0x89, 0xda, 0x05, 0x66, 0x70, 0x89, 0x29,
3281 0xcc, 0x4a,
3282 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,0x07, 0x08, 0x09, 0x0a,
3283 0xd0, 0x35,
3284 0xd7, 0xa1,
3285 0xcb, 0xc8, 0xc7, 0x24,
3286 0xc8, 0x22};
3287
Laurence Lundblade9b334962020-08-27 10:55:53 -07003288
Laurence Lundbladecc7da412020-12-27 00:09:07 -08003289static const uint8_t spSpiffyTagInput[] = {
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08003290 0x85, // Open array
Laurence Lundblade9b334962020-08-27 10:55:53 -07003291
3292 0xc0, // tag for string date
3293 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string
3294
3295 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string
3296
3297 0x4a, '1','9','8','5','-','0','4','-','1','2', // Date string in byte string
3298
3299 0xd8, 0x23, // tag for regex
3300 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string
3301
3302 0xc0, // tag for string date
3303 0x4a, '1','9','8','5','-','0','4','-','1','2', // Date string in byte string
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07003304
3305 // This last case makes the array untraversable because it is
3306 // an uncrecoverable error. Make sure it stays last and is the only
3307 // instance so the other tests can work.
Laurence Lundblade9b334962020-08-27 10:55:53 -07003308};
3309
3310
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003311static int32_t CheckCSRMaps(QCBORDecodeContext *pDC);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003312
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003313
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03003314int32_t OptTagParseTest(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003315{
3316 QCBORDecodeContext DCtx;
Laurence Lundblade9b334962020-08-27 10:55:53 -07003317 QCBORItem Item;
3318 QCBORError uError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003319
Laurence Lundbladeee851742020-01-08 08:37:05 -08003320 QCBORDecode_Init(&DCtx,
Laurence Lundblade9b334962020-08-27 10:55:53 -07003321 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTagInput),
Laurence Lundbladeee851742020-01-08 08:37:05 -08003322 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003323
Laurence Lundblade9b334962020-08-27 10:55:53 -07003324 /*
3325 This test matches the magic number tag and the fraction tag
3326 55799([...])
3327 */
3328 uError = QCBORDecode_GetNext(&DCtx, &Item);
3329 if(uError != QCBOR_SUCCESS) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003330 return -2;
3331 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003332 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003333 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC)) {
3334 return -3;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003335 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003336
Laurence Lundblade9b334962020-08-27 10:55:53 -07003337 /*
3338 4([1,3])
3339 */
3340 uError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundbladedd6e76e2021-03-10 01:54:01 -07003341#ifdef QCBOR_DISABLE_EXP_AND_MANTISSA
Laurence Lundblade9b334962020-08-27 10:55:53 -07003342 if(uError != QCBOR_SUCCESS ||
3343 Item.uDataType != QCBOR_TYPE_ARRAY ||
3344 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_DECIMAL_FRACTION) ||
3345 QCBORDecode_GetNthTag(&DCtx, &Item, 0) != CBOR_TAG_DECIMAL_FRACTION ||
3346 QCBORDecode_GetNthTag(&DCtx, &Item, 1) != CBOR_TAG_INVALID64 ||
3347 QCBORDecode_GetNthTag(&DCtx, &Item, 2) != CBOR_TAG_INVALID64 ||
3348 QCBORDecode_GetNthTag(&DCtx, &Item, 3) != CBOR_TAG_INVALID64 ||
3349 QCBORDecode_GetNthTag(&DCtx, &Item, 4) != CBOR_TAG_INVALID64 ||
3350 Item.val.uCount != 2) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003351 return -4;
3352 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07003353 // consume the items in the array
3354 uError = QCBORDecode_GetNext(&DCtx, &Item);
3355 uError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundblade59289e52019-12-30 13:44:37 -08003356
Laurence Lundbladedd6e76e2021-03-10 01:54:01 -07003357#else /* QCBOR_DISABLE_EXP_AND_MANTISSA */
Laurence Lundblade9b334962020-08-27 10:55:53 -07003358 if(uError != QCBOR_SUCCESS ||
3359 Item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION ||
3360 QCBORDecode_GetNthTag(&DCtx, &Item, 0) != CBOR_TAG_INVALID64 ||
3361 QCBORDecode_GetNthTag(&DCtx, &Item, 1) != CBOR_TAG_INVALID64 ||
3362 QCBORDecode_GetNthTag(&DCtx, &Item, 2) != CBOR_TAG_INVALID64 ||
3363 QCBORDecode_GetNthTag(&DCtx, &Item, 3) != CBOR_TAG_INVALID64 ||
3364 QCBORDecode_GetNthTag(&DCtx, &Item, 4) != CBOR_TAG_INVALID64 ) {
3365 return -5;
Laurence Lundblade59289e52019-12-30 13:44:37 -08003366 }
Laurence Lundbladedd6e76e2021-03-10 01:54:01 -07003367#endif /* QCBOR_DISABLE_EXP_AND_MANTISSA */
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003368
Laurence Lundblade9b334962020-08-27 10:55:53 -07003369 /*
3370 More than 4 tags on an item 225(226(227(228(229([])))))
3371 */
3372 uError = QCBORDecode_GetNext(&DCtx, &Item);
3373 if(uError != QCBOR_ERR_TOO_MANY_TAGS) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003374 return -6;
3375 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07003376
Laurence Lundblade88e9db22020-11-02 03:56:33 -08003377 if(QCBORDecode_GetNthTag(&DCtx, &Item, 0) != CBOR_TAG_INVALID64) {
3378 return -106;
3379 }
3380
3381
Laurence Lundblade9b334962020-08-27 10:55:53 -07003382 /* tag 10489608748473423768(
3383 2442302356(
3384 21590(
3385 240(
3386 []))))
3387 */
3388 uError = QCBORDecode_GetNext(&DCtx, &Item);
3389 if(uError != QCBOR_SUCCESS ||
3390 Item.uDataType != QCBOR_TYPE_ARRAY ||
3391 QCBORDecode_GetNthTag(&DCtx, &Item, 3) != 10489608748473423768ULL ||
3392 QCBORDecode_GetNthTag(&DCtx, &Item, 2) != 2442302356ULL ||
3393 QCBORDecode_GetNthTag(&DCtx, &Item, 1) != 21590ULL ||
3394 QCBORDecode_GetNthTag(&DCtx, &Item, 0) != 240ULL) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003395 return -7;
Laurence Lundblade9b334962020-08-27 10:55:53 -07003396 }
3397
3398 /* tag 21590(
3399 10489608748473423768(
3400 2442302357(
3401 21591(
3402 []))))
3403 */
3404 uError = QCBORDecode_GetNext(&DCtx, &Item);
3405 if(uError != QCBOR_SUCCESS ||
3406 Item.uDataType != QCBOR_TYPE_ARRAY ||
3407 QCBORDecode_GetNthTag(&DCtx, &Item, 0) != 65534ULL ||
3408 QCBORDecode_GetNthTag(&DCtx, &Item, 1) != 2442302357ULL ||
3409 QCBORDecode_GetNthTag(&DCtx, &Item, 2) != 10489608748473423768ULL ||
3410 QCBORDecode_GetNthTag(&DCtx, &Item, 3) != 21590ULL) {
3411 return -8;
3412 }
3413
3414 /* Make sure to blow past the limit of tags that must be mapped.
3415 works in conjuntion with entries above.
3416 269488144(269488145(269488146(269488147([]))))
3417 */
3418 uError = QCBORDecode_GetNext(&DCtx, &Item);
3419 if(uError != QCBOR_ERR_TOO_MANY_TAGS) {
3420 return -9;
3421 }
3422
3423 uError = QCBORDecode_GetNext(&DCtx, &Item);
3424 if(uError == QCBOR_SUCCESS) {
3425 return -10;
3426 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003427
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003428 // ----------------------------------
Laurence Lundbladeee851742020-01-08 08:37:05 -08003429 // This test sets up a caller-config list that includes the very large
Laurence Lundblade9b334962020-08-27 10:55:53 -07003430 // tage and then matches it. Caller-config lists are no longer
3431 // used or needed. This tests backwards compatibility with them.
Laurence Lundbladeee851742020-01-08 08:37:05 -08003432 QCBORDecode_Init(&DCtx,
3433 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEncodedLargeTag),
3434 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003435 const uint64_t puList[] = {0x9192939495969798, 257};
3436 const QCBORTagListIn TL = {2, puList};
3437 QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003438
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003439 if(QCBORDecode_GetNext(&DCtx, &Item)) {
3440 return -8;
3441 }
3442 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
3443 !QCBORDecode_IsTagged(&DCtx, &Item, 0x9192939495969798) ||
3444 QCBORDecode_IsTagged(&DCtx, &Item, 257) ||
3445 QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_BIGFLOAT) ||
3446 Item.val.uCount != 0) {
3447 return -9;
3448 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003449
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003450 //------------------------
Laurence Lundbladeee851742020-01-08 08:37:05 -08003451 // Sets up a caller-configured list and look up something not in it
Laurence Lundblade9b334962020-08-27 10:55:53 -07003452 // Another backwards compatibility test.
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003453 const uint64_t puLongList[17] = {1,2,1};
3454 const QCBORTagListIn TLLong = {17, puLongList};
Laurence Lundbladeee851742020-01-08 08:37:05 -08003455 QCBORDecode_Init(&DCtx,
3456 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEncodedLargeTag),
3457 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003458 QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TLLong);
3459 if(QCBORDecode_GetNext(&DCtx, &Item)) {
3460 return -11;
3461 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003462
Laurence Lundbladef7a70bc2020-10-24 12:23:25 -07003463 uint64_t puTags[4];
Laurence Lundblade9b334962020-08-27 10:55:53 -07003464 QCBORTagListOut Out = {0, 4, puTags};
3465
3466
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003467 // This tests retrievel of the full tag list
Laurence Lundbladeee851742020-01-08 08:37:05 -08003468 QCBORDecode_Init(&DCtx,
3469 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spLotsOfTags),
3470 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003471 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3472 return -12;
3473 }
3474 if(puTags[0] != 0x9192939495969798 ||
3475 puTags[1] != 0x88 ||
Laurence Lundblade59289e52019-12-30 13:44:37 -08003476 puTags[2] != 0x06 ||
3477 puTags[3] != 0x07) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003478 return -13;
3479 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003480
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003481 // ----------------------
Laurence Lundblade9b334962020-08-27 10:55:53 -07003482 // This tests too small of an out list
Laurence Lundbladeee851742020-01-08 08:37:05 -08003483 QCBORDecode_Init(&DCtx,
3484 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spLotsOfTags),
3485 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003486 QCBORTagListOut OutSmall = {0, 3, puTags};
3487 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &OutSmall) != QCBOR_ERR_TOO_MANY_TAGS) {
3488 return -14;
3489 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003490
Laurence Lundblade9b334962020-08-27 10:55:53 -07003491
3492
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003493 // ---------------
Laurence Lundblade9b334962020-08-27 10:55:53 -07003494 // Decode a version of the "CSR" that has had a ton of tags randomly inserted
3495 // It is a bit of a messy test and maybe could be improved, but
3496 // it is retained as a backwards compatibility check.
Laurence Lundbladeee851742020-01-08 08:37:05 -08003497 QCBORDecode_Init(&DCtx,
3498 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRWithTags),
3499 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003500 int n = CheckCSRMaps(&DCtx);
3501 if(n) {
3502 return n-2000;
3503 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003504
Laurence Lundblade59289e52019-12-30 13:44:37 -08003505 Out = (QCBORTagListOut){0, 16, puTags};
Laurence Lundbladeee851742020-01-08 08:37:05 -08003506 QCBORDecode_Init(&DCtx,
3507 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRWithTags),
3508 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003509
Laurence Lundblade9b334962020-08-27 10:55:53 -07003510 /* With the spiffy decode revision, this tag list is not used.
3511 It doesn't matter if a tag is in this list or not so some
3512 tests that couldn't process a tag because it isn't in this list
3513 now can process these unlisted tags. The tests have been
3514 adjusted for this. */
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003515 const uint64_t puTagList[] = {773, 1, 90599561};
3516 const QCBORTagListIn TagList = {3, puTagList};
3517 QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TagList);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003518
3519
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003520 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3521 return -100;
3522 }
3523 if(Item.uDataType != QCBOR_TYPE_MAP ||
3524 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC) ||
3525 QCBORDecode_IsTagged(&DCtx, &Item, 90599561) ||
3526 QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_DATE_EPOCH) ||
3527 Item.val.uCount != 2 ||
3528 puTags[0] != CBOR_TAG_CBOR_MAGIC ||
3529 puTags[1] != CBOR_TAG_CBOR_MAGIC ||
3530 puTags[2] != CBOR_TAG_CBOR_MAGIC ||
3531 Out.uNumUsed != 3) {
3532 return -101;
3533 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003534
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003535 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3536 return -102;
3537 }
3538 if(Item.uDataType != QCBOR_TYPE_MAP ||
3539 QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC) ||
3540 QCBORDecode_IsTagged(&DCtx, &Item, 6) ||
Laurence Lundbladef7a70bc2020-10-24 12:23:25 -07003541 !QCBORDecode_IsTagged(&DCtx, &Item, 7) ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003542 Item.val.uCount != 2 ||
3543 puTags[0] != 5859837686836516696 ||
3544 puTags[1] != 7 ||
3545 Out.uNumUsed != 2) {
3546 return -103;
3547 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003548
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003549 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3550 return -104;
3551 }
3552 if(Item.uDataType != QCBOR_TYPE_MAP ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003553 Item.val.uCount != 5 ||
3554 puTags[0] != 0x0b ||
3555 Out.uNumUsed != 1) {
3556 return -105;
3557 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003558
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003559 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3560 return -106;
3561 }
3562 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
3563 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_COSE_MAC0) ||
3564 Item.val.string.len != 12 ||
3565 puTags[0] != CBOR_TAG_COSE_MAC0 ||
3566 puTags[1] != CBOR_TAG_COSE_MAC0 ||
3567 puTags[2] != CBOR_TAG_COSE_MAC0 ||
3568 Out.uNumUsed != 3) {
3569 return -105;
3570 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003571
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003572 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3573 return -107;
3574 }
3575 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
3576 !QCBORDecode_IsTagged(&DCtx, &Item, 773) ||
3577 Item.val.string.len != 3 ||
3578 puTags[0] != 773 ||
3579 Out.uNumUsed != 1) {
3580 return -108;
3581 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003582
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003583 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3584 return -109;
3585 }
3586 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade59289e52019-12-30 13:44:37 -08003587 !QCBORDecode_IsTagged(&DCtx, &Item, 16) ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003588 Item.val.string.len != 9 ||
Laurence Lundblade59289e52019-12-30 13:44:37 -08003589 puTags[0] != 16 ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07003590 puTags[3] != 7 ||
3591 Out.uNumUsed != 4) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003592 return -110;
3593 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003594
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003595 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3596 return -111;
3597 }
3598 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
3599 !QCBORDecode_IsTagged(&DCtx, &Item, 17) ||
3600 Item.val.string.len != 9 ||
3601 puTags[0] != 17 ||
3602 Out.uNumUsed != 1) {
3603 return -112;
3604 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003605
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003606 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3607 return -111;
3608 }
3609 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
3610 !QCBORDecode_IsTagged(&DCtx, &Item, 17) ||
3611 Item.val.string.len != 2 ||
3612 puTags[0] != 17 ||
3613 Out.uNumUsed != 1) {
3614 return -112;
3615 }
3616
3617 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3618 return -113;
3619 }
3620 if(Item.uDataType != QCBOR_TYPE_MAP ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07003621 !QCBORDecode_IsTagged(&DCtx, &Item, 19) ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003622 Item.val.uCount != 2 ||
3623 puTags[0] != 19 ||
3624 Out.uNumUsed != 1) {
3625 return -114;
3626 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003627
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003628 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3629 return -115;
3630 }
3631 if(Item.uDataType != QCBOR_TYPE_MAP ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07003632 !QCBORDecode_IsTagged(&DCtx, &Item, 9) ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003633 Item.val.uCount != 1 ||
3634 puTags[0] != 9 ||
3635 Out.uNumUsed != 1) {
3636 return -116;
3637 }
3638
3639 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3640 return -116;
3641 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003642 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003643 Item.val.int64 != -7 ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003644 Out.uNumUsed != 0) {
3645 return -117;
3646 }
3647
3648 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3649 return -118;
3650 }
3651 if(Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
3652 Item.val.string.len != 10 ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003653 puTags[0] != 12 ||
3654 Out.uNumUsed != 1) {
3655 return -119;
3656 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003657
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003658 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3659 return -120;
3660 }
3661 if(Item.uDataType != QCBOR_TYPE_MAP ||
3662 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_ENC_AS_B16) ||
3663 Item.val.uCount != 1 ||
3664 puTags[0] != 0x17 ||
3665 Out.uNumUsed != 1) {
3666 return -121;
3667 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003668
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003669 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3670 return -122;
3671 }
3672 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07003673 !QCBORDecode_IsTagged(&DCtx, &Item, 8) ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003674 Item.val.int64 != -3 ||
3675 puTags[0] != 8 ||
3676 Out.uNumUsed != 1) {
3677 return -123;
3678 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003679
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003680 if(QCBORDecode_Finish(&DCtx)) {
3681 return -124;
3682 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07003683
3684 UsefulBufC DateString;
3685 QCBORDecode_Init(&DCtx,
3686 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSpiffyTagInput),
3687 QCBOR_DECODE_MODE_NORMAL);
3688
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07003689 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade9b334962020-08-27 10:55:53 -07003690 // tagged date string
3691 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString);
3692 // untagged date string
3693 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString);
3694 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_SUCCESS) {
3695 return 100;
3696 }
3697 // untagged byte string
3698 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString);
3699 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
3700 return 101;
3701 }
3702 // tagged regex
3703 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString);
3704 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
3705 return 102;
3706 }
3707 // tagged date string with a byte string
3708 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString);
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07003709 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT) {
Laurence Lundblade9b334962020-08-27 10:55:53 -07003710 return 103;
3711 }
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07003712 // The exit errors out because the last item, the date string with
3713 // bad content makes the array untraversable (the bad date string
3714 // could have tag content of an array or such that is not consumed
3715 // by the date decoding).
Laurence Lundblade9b334962020-08-27 10:55:53 -07003716 QCBORDecode_ExitArray(&DCtx);
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07003717 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT) {
Laurence Lundblade9b334962020-08-27 10:55:53 -07003718 return 104;
3719 }
3720
3721
3722 QCBORDecode_Init(&DCtx,
3723 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSpiffyTagInput),
3724 QCBOR_DECODE_MODE_NORMAL);
3725
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07003726 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade9b334962020-08-27 10:55:53 -07003727 // tagged date string
3728 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString);
3729 // untagged date string
3730 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString);
3731 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_SUCCESS) {
3732 return 200;
3733 }
3734 // untagged byte string
3735 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString);
3736 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
3737 return 201;
3738 }
3739 // tagged regex
3740 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString);
3741 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
3742 return 202;
3743 }
3744 // tagged date string with a byte string
3745 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString);
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07003746 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT) {
Laurence Lundblade9b334962020-08-27 10:55:53 -07003747 return 203;
3748 }
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07003749 // See comments above
Laurence Lundblade9b334962020-08-27 10:55:53 -07003750 QCBORDecode_ExitArray(&DCtx);
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07003751 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT) {
Laurence Lundblade9b334962020-08-27 10:55:53 -07003752 return 204;
3753 }
3754
3755 QCBORDecode_Init(&DCtx,
3756 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSpiffyTagInput),
3757 QCBOR_DECODE_MODE_NORMAL);
3758
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07003759 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade9b334962020-08-27 10:55:53 -07003760 // tagged date string
3761 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString);
3762 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
3763 return 300;
3764 }
3765 // untagged date string
3766 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString);
3767 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
3768 return 301;
3769 }
3770 // untagged byte string
3771 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString);
3772 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
3773 return 302;
3774 }
3775 // tagged regex
3776 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString);
3777 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
3778 return 303;
3779 }
3780 // tagged date string with a byte string
3781 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString);
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07003782 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT) {
Laurence Lundblade9b334962020-08-27 10:55:53 -07003783 return 304;
3784 }
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07003785 // See comments above
Laurence Lundblade9b334962020-08-27 10:55:53 -07003786 QCBORDecode_ExitArray(&DCtx);
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07003787 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT) {
Laurence Lundblade9b334962020-08-27 10:55:53 -07003788 return 305;
3789 }
3790
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003791 return 0;
3792}
3793
Laurence Lundblade37286c02022-09-03 10:05:02 -07003794/*
3795 * These are showing the big numbers converted to integers.
3796 * The tag numbers are not shown.
3797 *
3798 * [ 18446744073709551616,
3799 * -18446744073709551617,
3800 * {"BN+": 18446744073709551616,
3801 * 64: 18446744073709551616,
3802 * "BN-": -18446744073709551617,
3803 * -64: -18446744073709551617
3804 * }
3805 * ]
3806 */
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003807
Laurence Lundbladecc7da412020-12-27 00:09:07 -08003808static const uint8_t spBigNumInput[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003809 0x83,
3810 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3811 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3812 0xA4,
3813 0x63, 0x42, 0x4E, 0x2B,
3814 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3815 0x18, 0x40,
3816 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3817 0x63, 0x42, 0x4E, 0x2D,
3818 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3819 0x38, 0x3F,
3820 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
3821
Laurence Lundblade37286c02022-09-03 10:05:02 -07003822#ifndef QCBOR_DISABLE_TAGS
Laurence Lundbladecc7da412020-12-27 00:09:07 -08003823/* The expected big num */
3824static const uint8_t spBigNum[] = {
3825 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3826 0x00};
Laurence Lundblade37286c02022-09-03 10:05:02 -07003827#endif /* QCBOR_DISABLE_TAGS */
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003828
3829
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03003830int32_t BignumParseTest(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003831{
3832 QCBORDecodeContext DCtx;
3833 QCBORItem Item;
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07003834 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003835
Laurence Lundbladeee851742020-01-08 08:37:05 -08003836 QCBORDecode_Init(&DCtx,
3837 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNumInput),
3838 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003839
3840
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003841 //
3842 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
3843 return -1;
3844 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003845 return -2;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003846 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003847
Laurence Lundblade37286c02022-09-03 10:05:02 -07003848#ifndef QCBOR_DISABLE_TAGS
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003849 //
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003850 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003851 return -3;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003852 if(Item.uDataType != QCBOR_TYPE_POSBIGNUM ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003853 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003854 return -4;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003855 }
3856
3857 //
3858 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003859 return -5;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003860 if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003861 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003862 return -6;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003863 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003864
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003865 //
3866 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003867 return -7;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003868 if(Item.uDataType != QCBOR_TYPE_MAP) {
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003869 return -8;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003870 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003871
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003872 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003873 return -9;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003874 if(Item.uDataType != QCBOR_TYPE_POSBIGNUM ||
3875 Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003876 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003877 return -10;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003878 }
3879
3880 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003881 return -11;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003882 if(Item.uDataType != QCBOR_TYPE_POSBIGNUM ||
3883 Item.uLabelType != QCBOR_TYPE_INT64 ||
3884 Item.label.int64 != 64 ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003885 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003886 return -12;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003887 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003888
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003889 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003890 return -13;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003891 if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM ||
3892 Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003893 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003894 return -14;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003895 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003896
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003897 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003898 return -15;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003899 if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM ||
3900 Item.uLabelType != QCBOR_TYPE_INT64 ||
3901 Item.label.int64 != -64 ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003902 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003903 return -16;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003904 }
Laurence Lundblade37286c02022-09-03 10:05:02 -07003905#else
3906
3907 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_TAGS_DISABLED) {
3908 return -100;
3909 }
3910#endif /* QCBOR_DISABLE_TAGS */
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003911
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003912 return 0;
3913}
3914
3915
3916
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003917static int32_t CheckItemWithIntLabel(QCBORDecodeContext *pCtx,
Laurence Lundbladeee851742020-01-08 08:37:05 -08003918 uint8_t uDataType,
3919 uint8_t uNestingLevel,
3920 uint8_t uNextNest,
3921 int64_t nLabel,
3922 QCBORItem *pItem)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003923{
3924 QCBORItem Item;
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07003925 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003926
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003927 if((nCBORError = QCBORDecode_GetNext(pCtx, &Item))) return -1;
3928 if(Item.uDataType != uDataType) return -1;
3929 if(uNestingLevel > 0) {
Laurence Lundbladeee851742020-01-08 08:37:05 -08003930 if(Item.uLabelType != QCBOR_TYPE_INT64 &&
3931 Item.uLabelType != QCBOR_TYPE_UINT64) {
3932 return -1;
3933 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003934 if(Item.uLabelType == QCBOR_TYPE_INT64) {
3935 if(Item.label.int64 != nLabel) return -1;
3936 } else {
Laurence Lundblade570fab52018-10-13 18:28:27 +08003937 if(Item.label.uint64 != (uint64_t)nLabel) return -1;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003938 }
3939 }
3940 if(Item.uNestingLevel != uNestingLevel) return -1;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303941 if(Item.uNextNestLevel != uNextNest) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003942
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003943 if(pItem) {
3944 *pItem = Item;
3945 }
3946 return 0;
3947}
3948
3949
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003950// Same code checks definite and indefinite length versions of the map
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003951static int32_t CheckCSRMaps(QCBORDecodeContext *pDC)
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003952{
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303953 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 0, 1, 0, NULL)) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003954
Laurence Lundblade9b334962020-08-27 10:55:53 -07003955 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 1, 2, -23, NULL)) return -2;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003956
Laurence Lundblade9b334962020-08-27 10:55:53 -07003957 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 2, 3, -20, NULL)) return -3;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003958
Laurence Lundblade9b334962020-08-27 10:55:53 -07003959 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -18, NULL)) return -4;
3960 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -17, NULL)) return -5;
3961 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -15, NULL)) return -6;
3962 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -16, NULL)) return -7;
3963 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 2, -14, NULL)) return -8;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003964
Laurence Lundblade9b334962020-08-27 10:55:53 -07003965 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 2, 3, -19, NULL)) return -9;
3966 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 3, 4, -11, NULL)) return -10;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003967
Laurence Lundblade9b334962020-08-27 10:55:53 -07003968 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_INT64, 4, 3, -9, NULL)) return -11;
3969 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_BYTE_STRING, 3, 1, -10, NULL)) return -12;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003970
Laurence Lundblade9b334962020-08-27 10:55:53 -07003971 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 1, 2, -22, NULL)) return -13;
3972 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_INT64, 2, 0, -5, NULL)) return -14;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003973
Laurence Lundblade9b334962020-08-27 10:55:53 -07003974 if(QCBORDecode_Finish(pDC)) return -20;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003975
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003976 return 0;
3977}
3978
3979
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003980/*
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003981{
3982 -23: {
3983 -20: {
3984 -18: "Organization",
3985 -17: "SSG",
3986 -15: "Confusion",
3987 -16: "San Diego",
3988 -14: "US"
3989 },
3990 -19: {
3991 -11: {
3992 -9: -7
3993 },
3994 -10: '\u0001\u0002\u0003\u0004\u0005\u0006\a\b\t\n'
3995 }
3996 },
3997 -22: {
3998 -5: -3
3999 }
4000}
Laurence Lundbladecc7da412020-12-27 00:09:07 -08004001*/
4002static const uint8_t spCSRInput[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004003 0xa2, 0x36, 0xa2, 0x33, 0xa5, 0x31, 0x6c, 0x4f,
4004 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74,
4005 0x69, 0x6f, 0x6e, 0x30, 0x63, 0x53, 0x53, 0x47,
4006 0x2e, 0x69, 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73,
4007 0x69, 0x6f, 0x6e, 0x2f, 0x69, 0x53, 0x61, 0x6e,
4008 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f, 0x2d, 0x62,
4009 0x55, 0x53, 0x32, 0xa2, 0x2a, 0xa1, 0x28, 0x26,
4010 0x29, 0x4a, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
4011 0x07, 0x08, 0x09, 0x0a, 0x35, 0xa1, 0x24, 0x22};
4012
Laurence Lundbladecc7da412020-12-27 00:09:07 -08004013// Same map as above, but using indefinite lengths
4014static const uint8_t spCSRInputIndefLen[] = {
4015 0xbf, 0x36, 0xbf, 0x33, 0xbf, 0x31, 0x6c, 0x4f,
4016 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74,
4017 0x69, 0x6f, 0x6e, 0x30, 0x63, 0x53, 0x53, 0x47,
4018 0x2e, 0x69, 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73,
4019 0x69, 0x6f, 0x6e, 0x2f, 0x69, 0x53, 0x61, 0x6e,
4020 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f, 0x2d, 0x62,
4021 0x55, 0x53, 0xff, 0x32, 0xbf, 0x2a, 0xbf, 0x28,
4022 0x26, 0xff, 0x29, 0x4a, 0x01, 0x02, 0x03, 0x04,
4023 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0xff, 0xff,
4024 0x35, 0xbf, 0x24, 0x22, 0xff, 0xff};
4025
4026
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03004027int32_t NestedMapTest(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004028{
4029 QCBORDecodeContext DCtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004030
Laurence Lundbladeee851742020-01-08 08:37:05 -08004031 QCBORDecode_Init(&DCtx,
4032 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput),
4033 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004034
Laurence Lundblade742df4a2018-10-13 20:07:17 +08004035 return CheckCSRMaps(&DCtx);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004036}
4037
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08004038
4039
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03004040int32_t StringDecoderModeFailTest(void)
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08004041{
4042 QCBORDecodeContext DCtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004043
Laurence Lundbladeee851742020-01-08 08:37:05 -08004044 QCBORDecode_Init(&DCtx,
4045 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput),
4046 QCBOR_DECODE_MODE_MAP_STRINGS_ONLY);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004047
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08004048 QCBORItem Item;
4049 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004050
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08004051 if(QCBORDecode_GetNext(&DCtx, &Item)) {
4052 return -1;
4053 }
4054 if(Item.uDataType != QCBOR_TYPE_MAP) {
4055 return -2;
4056 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004057
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08004058 nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
4059 if(nCBORError != QCBOR_ERR_MAP_LABEL_TYPE) {
4060 return -3;
4061 }
4062
4063 return 0;
4064}
4065
4066
Laurence Lundblade742df4a2018-10-13 20:07:17 +08004067
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03004068int32_t NestedMapTestIndefLen(void)
Laurence Lundblade742df4a2018-10-13 20:07:17 +08004069{
4070 QCBORDecodeContext DCtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004071
Laurence Lundbladeee851742020-01-08 08:37:05 -08004072 QCBORDecode_Init(&DCtx,
4073 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInputIndefLen),
4074 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004075
Laurence Lundblade742df4a2018-10-13 20:07:17 +08004076 return CheckCSRMaps(&DCtx);
4077}
4078
4079
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004080
Laurence Lundblade17ede402018-10-13 11:43:07 +08004081static UsefulBufC make_nested_indefinite_arrays(int n, UsefulBuf Storage)
4082{
4083 UsefulOutBuf UOB;
4084 UsefulOutBuf_Init(&UOB, Storage);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004085
Laurence Lundblade17ede402018-10-13 11:43:07 +08004086 int i;
4087 for(i = 0; i < n; i++) {
4088 UsefulOutBuf_AppendByte(&UOB, 0x9f);
4089 }
4090
4091 for(i = 0; i < n; i++) {
4092 UsefulOutBuf_AppendByte(&UOB, 0xff);
4093 }
4094 return UsefulOutBuf_OutUBuf(&UOB);
4095}
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08004096
4097
Laurence Lundbladec5fef682020-01-25 11:38:45 -08004098static int32_t parse_indeflen_nested(UsefulBufC Nested, int nNestLevel)
Laurence Lundblade17ede402018-10-13 11:43:07 +08004099{
4100 QCBORDecodeContext DC;
4101 QCBORDecode_Init(&DC, Nested, 0);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004102
Laurence Lundblade17ede402018-10-13 11:43:07 +08004103 int j;
4104 for(j = 0; j < nNestLevel; j++) {
4105 QCBORItem Item;
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07004106 QCBORError nReturn = QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade17ede402018-10-13 11:43:07 +08004107 if(j >= QCBOR_MAX_ARRAY_NESTING) {
4108 // Should be in error
Laurence Lundbladea9489f82020-09-12 13:50:56 -07004109 if(nReturn != QCBOR_ERR_ARRAY_DECODE_NESTING_TOO_DEEP) {
Laurence Lundblade17ede402018-10-13 11:43:07 +08004110 return -4;
4111 } else {
4112 return 0; // Decoding doesn't recover after an error
4113 }
4114 } else {
4115 // Should be no error
4116 if(nReturn) {
4117 return -9; // Should not have got an error
4118 }
4119 }
4120 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
4121 return -7;
4122 }
4123 }
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07004124 QCBORError nReturn = QCBORDecode_Finish(&DC);
Laurence Lundblade17ede402018-10-13 11:43:07 +08004125 if(nReturn) {
4126 return -3;
4127 }
4128 return 0;
4129}
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08004130
4131
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03004132int32_t IndefiniteLengthNestTest(void)
Laurence Lundblade17ede402018-10-13 11:43:07 +08004133{
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05304134 UsefulBuf_MAKE_STACK_UB(Storage, 50);
Laurence Lundblade17ede402018-10-13 11:43:07 +08004135 int i;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004136 for(i=1; i < QCBOR_MAX_ARRAY_NESTING+4; i++) {
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08004137 const UsefulBufC Nested = make_nested_indefinite_arrays(i, Storage);
Laurence Lundblade17ede402018-10-13 11:43:07 +08004138 int nReturn = parse_indeflen_nested(Nested, i);
4139 if(nReturn) {
4140 return nReturn;
4141 }
4142 }
4143 return 0;
4144}
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08004145
Laurence Lundbladeee851742020-01-08 08:37:05 -08004146// [1, [2, 3]]
4147static const uint8_t spIndefiniteArray[] = {0x9f, 0x01, 0x82, 0x02, 0x03, 0xff};
4148// No closing break
4149static const uint8_t spIndefiniteArrayBad1[] = {0x9f};
4150// Not enough closing breaks
4151static const uint8_t spIndefiniteArrayBad2[] = {0x9f, 0x9f, 0x02, 0xff};
4152// Too many closing breaks
4153static const uint8_t spIndefiniteArrayBad3[] = {0x9f, 0x02, 0xff, 0xff};
4154// Unclosed indeflen inside def len
4155static const uint8_t spIndefiniteArrayBad4[] = {0x81, 0x9f};
4156// confused tag
4157static const uint8_t spIndefiniteArrayBad5[] = {0x9f, 0xd1, 0xff};
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08004158
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03004159int32_t IndefiniteLengthArrayMapTest(void)
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08004160{
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07004161 QCBORError nResult;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08004162 // --- first test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07004163 UsefulBufC IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArray);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004164
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08004165 // Decode it and see if it is OK
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08004166 QCBORDecodeContext DC;
4167 QCBORItem Item;
4168 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004169
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08004170 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05304171
4172 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
4173 Item.uNestingLevel != 0 ||
4174 Item.uNextNestLevel != 1) {
4175 return -111;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08004176 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004177
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08004178 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05304179 if(Item.uDataType != QCBOR_TYPE_INT64 ||
4180 Item.uNestingLevel != 1 ||
4181 Item.uNextNestLevel != 1) {
4182 return -2;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08004183 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004184
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08004185 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05304186 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
4187 Item.uNestingLevel != 1 ||
4188 Item.uNextNestLevel != 2) {
4189 return -3;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08004190 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004191
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08004192 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade12b495d2018-12-17 11:15:54 -08004193 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade6de37062018-10-15 12:22:42 +05304194 Item.uNestingLevel != 2 ||
4195 Item.uNextNestLevel != 2) {
4196 return -4;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08004197 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004198
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08004199 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade12b495d2018-12-17 11:15:54 -08004200 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade6de37062018-10-15 12:22:42 +05304201 Item.uNestingLevel != 2 ||
4202 Item.uNextNestLevel != 0) {
4203 return -5;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08004204 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004205
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08004206 if(QCBORDecode_Finish(&DC)) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05304207 return -6;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08004208 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004209
Laurence Lundblade19e0c802018-10-13 12:19:55 +08004210 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07004211 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad1);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004212
Laurence Lundblade19e0c802018-10-13 12:19:55 +08004213 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004214
Laurence Lundblade19e0c802018-10-13 12:19:55 +08004215 nResult = QCBORDecode_GetNext(&DC, &Item);
4216 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05304217 return -7;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08004218 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004219
Laurence Lundblade570fab52018-10-13 18:28:27 +08004220 nResult = QCBORDecode_Finish(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07004221 if(nResult != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05304222 return -8;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08004223 }
4224
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004225
Laurence Lundblade19e0c802018-10-13 12:19:55 +08004226 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07004227 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad2);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004228
Laurence Lundblade19e0c802018-10-13 12:19:55 +08004229 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004230
Laurence Lundblade19e0c802018-10-13 12:19:55 +08004231 nResult = QCBORDecode_GetNext(&DC, &Item);
4232 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05304233 return -9;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08004234 }
4235
4236 nResult = QCBORDecode_GetNext(&DC, &Item);
4237 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05304238 return -10;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08004239 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004240
Laurence Lundblade19e0c802018-10-13 12:19:55 +08004241 nResult = QCBORDecode_GetNext(&DC, &Item);
4242 if(nResult || Item.uDataType != QCBOR_TYPE_INT64) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05304243 return -11;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08004244 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004245
Laurence Lundblade19e0c802018-10-13 12:19:55 +08004246 nResult = QCBORDecode_Finish(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07004247 if(nResult != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05304248 return -12;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08004249 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004250
4251
Laurence Lundblade19e0c802018-10-13 12:19:55 +08004252 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07004253 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad3);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004254
Laurence Lundblade19e0c802018-10-13 12:19:55 +08004255 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004256
Laurence Lundblade19e0c802018-10-13 12:19:55 +08004257 nResult = QCBORDecode_GetNext(&DC, &Item);
4258 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05304259 return -13;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08004260 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004261
Laurence Lundblade19e0c802018-10-13 12:19:55 +08004262 nResult = QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade642282a2020-06-23 12:00:33 -07004263 if(nResult != QCBOR_SUCCESS) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05304264 return -14;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08004265 }
Laurence Lundblade6de37062018-10-15 12:22:42 +05304266
Laurence Lundblade642282a2020-06-23 12:00:33 -07004267 nResult = QCBORDecode_GetNext(&DC, &Item);
4268 if(nResult != QCBOR_ERR_BAD_BREAK) {
4269 return -140;
4270 }
4271
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004272
Laurence Lundblade570fab52018-10-13 18:28:27 +08004273 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07004274 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad4);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004275
Laurence Lundblade570fab52018-10-13 18:28:27 +08004276 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004277
Laurence Lundblade570fab52018-10-13 18:28:27 +08004278 nResult = QCBORDecode_GetNext(&DC, &Item);
4279 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05304280 return -15;
Laurence Lundblade570fab52018-10-13 18:28:27 +08004281 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004282
Laurence Lundblade570fab52018-10-13 18:28:27 +08004283 nResult = QCBORDecode_GetNext(&DC, &Item);
4284 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05304285 return -16;
Laurence Lundblade570fab52018-10-13 18:28:27 +08004286 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004287
Laurence Lundblade570fab52018-10-13 18:28:27 +08004288 nResult = QCBORDecode_Finish(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07004289 if(nResult != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05304290 return -17;
Laurence Lundblade570fab52018-10-13 18:28:27 +08004291 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004292
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304293 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07004294 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad5);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004295
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304296 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004297
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304298 nResult = QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade37286c02022-09-03 10:05:02 -07004299
4300#ifndef QCBOR_DISABLE_TAGS
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304301 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05304302 return -18;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304303 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004304
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304305 nResult = QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05304306 if(nResult != QCBOR_ERR_BAD_BREAK) {
4307 return -19;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304308 }
Laurence Lundblade37286c02022-09-03 10:05:02 -07004309#else /* QCBOR_DISABLE_TAGS */
4310 if(nResult != QCBOR_ERR_TAGS_DISABLED) {
4311 return -20;
4312 }
4313#endif /* QCBOR_DISABLE_TAGS */
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004314
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08004315 return 0;
4316}
4317
Laurence Lundblade17ede402018-10-13 11:43:07 +08004318
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08004319#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
4320
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07004321static const uint8_t spIndefiniteLenString[] = {
Laurence Lundblade17ede402018-10-13 11:43:07 +08004322 0x81, // Array of length one
4323 0x7f, // text string marked with indefinite length
4324 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment
4325 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment
4326 0xff // ending break
4327};
4328
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07004329static const uint8_t spIndefiniteLenStringBad2[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304330 0x81, // Array of length one
4331 0x7f, // text string marked with indefinite length
4332 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment
4333 0x44, 0x6d, 0x69, 0x6e, 0x67, // second segment of wrong type
4334 0xff // ending break
4335};
4336
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07004337static const uint8_t spIndefiniteLenStringBad3[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304338 0x81, // Array of length one
4339 0x7f, // text string marked with indefinite length
4340 0x01, 0x02, // Not a string
4341 0xff // ending break
4342};
4343
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07004344static const uint8_t spIndefiniteLenStringBad4[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304345 0x81, // Array of length one
4346 0x7f, // text string marked with indefinite length
4347 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment
4348 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment
4349 // missing end of string
4350};
4351
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07004352static const uint8_t spIndefiniteLenStringLabel[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304353 0xa1, // Array of length one
4354 0x7f, // text string marked with indefinite length
4355 0x65, 0x73, 0x74, 0x72, 0x75, 0x75, // first segment
4356 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment
4357 0xff, // ending break
4358 0x01 // integer being labeled.
4359};
4360
Laurence Lundbladec5fef682020-01-25 11:38:45 -08004361/**
4362 Make an indefinite length string
4363
4364 @param Storage Storage for string, must be 144 bytes in size
4365 @return The indefinite length string
4366
4367 This makes an array with one indefinite length string that has 7 chunks
4368 from size of 1 byte up to 64 bytes.
4369 */
4370static UsefulBufC MakeIndefiniteBigBstr(UsefulBuf Storage)
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304371{
4372 UsefulOutBuf UOB;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004373
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304374 UsefulOutBuf_Init(&UOB, Storage);
4375 UsefulOutBuf_AppendByte(&UOB, 0x81);
4376 UsefulOutBuf_AppendByte(&UOB, 0x5f);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004377
Laurence Lundbladec5fef682020-01-25 11:38:45 -08004378 uint8_t uStringByte = 0;
4379 // Use of type int is intentional
4380 for(int uChunkSize = 1; uChunkSize <= 128; uChunkSize *= 2) {
4381 // Not using preferred encoding here, but that is OK.
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304382 UsefulOutBuf_AppendByte(&UOB, 0x58);
Laurence Lundbladec5fef682020-01-25 11:38:45 -08004383 UsefulOutBuf_AppendByte(&UOB, (uint8_t)uChunkSize);
4384 for(int j = 0; j < uChunkSize; j++) {
4385 UsefulOutBuf_AppendByte(&UOB, uStringByte);
4386 uStringByte++;
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304387 }
4388 }
4389 UsefulOutBuf_AppendByte(&UOB, 0xff);
4390
4391 return UsefulOutBuf_OutUBuf(&UOB);
4392}
4393
4394static int CheckBigString(UsefulBufC BigString)
4395{
4396 if(BigString.len != 255) {
4397 return 1;
4398 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004399
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304400 for(uint8_t i = 0; i < 255; i++){
4401 if(((const uint8_t *)BigString.ptr)[i] != i) {
4402 return 1;
4403 }
4404 }
4405 return 0;
4406}
4407
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05304408
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03004409int32_t IndefiniteLengthStringTest(void)
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304410{
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304411 QCBORDecodeContext DC;
4412 QCBORItem Item;
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05304413 // big enough for MakeIndefiniteBigBstr() + MemPool overhead
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004414 UsefulBuf_MAKE_STACK_UB(MemPool, 350);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004415
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304416 // --- Simple normal indefinite length string ------
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07004417 UsefulBufC IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenString);
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304418 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004419
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05304420 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304421 return -1;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304422 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004423
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304424 if(QCBORDecode_GetNext(&DC, &Item)) {
4425 return -2;
4426 }
4427 if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.uDataAlloc) {
4428 return -3;
4429 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004430
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304431 if(QCBORDecode_GetNext(&DC, &Item)) {
4432 return -4;
4433 }
4434 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || !Item.uDataAlloc) {
4435 return -5;
4436 }
4437 if(QCBORDecode_Finish(&DC)) {
4438 return -6;
4439 }
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304440
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304441 // ----- types mismatch ---
Laurence Lundbladeee851742020-01-08 08:37:05 -08004442 QCBORDecode_Init(&DC,
4443 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad2),
4444 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004445
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304446 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
4447 return -7;
4448 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004449
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304450 if(QCBORDecode_GetNext(&DC, &Item)) {
4451 return -8;
4452 }
4453 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
4454 return -9;
4455 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004456
Laurence Lundblade30816f22018-11-10 13:40:22 +07004457 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_INDEFINITE_STRING_CHUNK) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304458 return -10;
4459 }
4460
4461 // ----- not a string ---
Laurence Lundbladeee851742020-01-08 08:37:05 -08004462 QCBORDecode_Init(&DC,
4463 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad3),
4464 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004465
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304466 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
4467 return -11;
4468 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004469
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304470 if(QCBORDecode_GetNext(&DC, &Item)) {
4471 return -12;
4472 }
4473 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
4474 return -13;
4475 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004476
Laurence Lundblade30816f22018-11-10 13:40:22 +07004477 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_INDEFINITE_STRING_CHUNK) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304478 return -14;
4479 }
4480
4481 // ----- no end -----
Laurence Lundbladeee851742020-01-08 08:37:05 -08004482 QCBORDecode_Init(&DC,
4483 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad4),
4484 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004485
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304486 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
4487 return -15;
4488 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004489
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304490 if(QCBORDecode_GetNext(&DC, &Item)) {
4491 return -16;
4492 }
4493 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
4494 return -17;
4495 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004496
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304497 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_HIT_END) {
4498 return -18;
4499 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004500
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304501 // ------ Don't set a string allocator and see an error -----
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304502 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004503
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304504 QCBORDecode_GetNext(&DC, &Item);
4505 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304506 return -19;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304507 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004508
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304509 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_NO_STRING_ALLOCATOR) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304510 return -20;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304511 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004512
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304513 // ----- Mempool is way too small -----
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004514 UsefulBuf_MAKE_STACK_UB(MemPoolTooSmall, QCBOR_DECODE_MIN_MEM_POOL_SIZE-1);
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304515
4516 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
4517 if(!QCBORDecode_SetMemPool(&DC, MemPoolTooSmall, false)) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304518 return -21;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304519 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004520
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304521 // ----- Mempool is way too small -----
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05304522 UsefulBuf_MAKE_STACK_UB(BigIndefBStrStorage, 290);
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08004523 const UsefulBufC BigIndefBStr = MakeIndefiniteBigBstr(BigIndefBStrStorage);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004524
Laurence Lundbladeee851742020-01-08 08:37:05 -08004525 // 80 is big enough for MemPool overhead, but not BigIndefBStr
4526 UsefulBuf_MAKE_STACK_UB(MemPoolSmall, 80);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004527
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304528 QCBORDecode_Init(&DC, BigIndefBStr, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304529 if(QCBORDecode_SetMemPool(&DC, MemPoolSmall, false)) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304530 return -22;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304531 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004532
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304533 QCBORDecode_GetNext(&DC, &Item);
4534 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304535 return -23;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304536 }
Laurence Lundblade30816f22018-11-10 13:40:22 +07004537 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_STRING_ALLOCATE) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304538 return -24;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304539 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004540
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304541 // ---- big bstr -----
4542 QCBORDecode_Init(&DC, BigIndefBStr, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004543
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304544 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
4545 return -25;
4546 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004547
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05304548 if(QCBORDecode_GetNext(&DC, &Item)) {
4549 return -26;
4550 }
4551 if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.uDataAlloc) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304552 return -26;
4553 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004554
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304555 if(QCBORDecode_GetNext(&DC, &Item)) {
4556 return -27;
4557 }
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05304558 if(Item.uDataType != QCBOR_TYPE_BYTE_STRING || !Item.uDataAlloc || Item.uNestingLevel != 1) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304559 return -28;
4560 }
4561 if(CheckBigString(Item.val.string)) {
4562 return -3;
4563 }
4564 if(QCBORDecode_Finish(&DC)) {
4565 return -29;
4566 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004567
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304568 // --- label is an indefinite length string ------
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07004569 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringLabel), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004570
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304571 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
4572 return -30;
4573 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004574
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304575 QCBORDecode_GetNext(&DC, &Item);
4576 if(Item.uDataType != QCBOR_TYPE_MAP) {
4577 return -31;
4578 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004579
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304580 if(QCBORDecode_GetNext(&DC, &Item)){
4581 return -32;
4582 }
Laurence Lundbladeee851742020-01-08 08:37:05 -08004583 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
4584 Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304585 Item.uDataAlloc || !Item.uLabelAlloc ||
4586 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("struuming"))) {
4587 return -33;
4588 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004589
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304590 if(QCBORDecode_Finish(&DC)) {
4591 return -34;
4592 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004593
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004594 return 0;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08004595}
4596
4597
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03004598int32_t AllocAllStringsTest(void)
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304599{
4600 QCBORDecodeContext DC;
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004601 QCBORError nCBORError;
4602
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004603
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304604 // First test, use the "CSRMap" as easy input and checking
Laurence Lundbladeee851742020-01-08 08:37:05 -08004605 QCBORDecode_Init(&DC,
4606 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput),
4607 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004608
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004609 UsefulBuf_MAKE_STACK_UB(Pool, sizeof(spCSRInput) + QCBOR_DECODE_MIN_MEM_POOL_SIZE);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004610
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004611 nCBORError = QCBORDecode_SetMemPool(&DC, Pool, 1); // Turn on copying.
4612 if(nCBORError) {
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304613 return -1;
4614 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004615
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004616 if(CheckCSRMaps(&DC)) {
4617 return -2;
4618 }
4619
Laurence Lundblade2f467f92020-10-09 17:50:11 -07004620 // Next parse, save pointers to a few strings, destroy original and
4621 // see all is OK.
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004622 UsefulBuf_MAKE_STACK_UB(CopyOfStorage, sizeof(pValidMapEncoded) + QCBOR_DECODE_MIN_MEM_POOL_SIZE);
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08004623 const UsefulBufC CopyOf = UsefulBuf_Copy(CopyOfStorage, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded));
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08004624
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304625 QCBORDecode_Init(&DC, CopyOf, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08004626 UsefulBuf_Set(Pool, '/');
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304627 QCBORDecode_SetMemPool(&DC, Pool, 1); // Turn on copying.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004628
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304629 QCBORItem Item1, Item2, Item3, Item4;
4630 if((nCBORError = QCBORDecode_GetNext(&DC, &Item1)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07004631 return (int32_t)nCBORError;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304632 if(Item1.uDataType != QCBOR_TYPE_MAP ||
4633 Item1.val.uCount != 3)
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004634 return -3;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304635 if((nCBORError = QCBORDecode_GetNext(&DC, &Item1)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07004636 return (int32_t)nCBORError;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304637 if((nCBORError = QCBORDecode_GetNext(&DC, &Item2)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07004638 return (int32_t)nCBORError;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304639 if((nCBORError = QCBORDecode_GetNext(&DC, &Item3)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07004640 return (int32_t)nCBORError;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304641 if((nCBORError = QCBORDecode_GetNext(&DC, &Item4)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07004642 return (int32_t)nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004643
Laurence Lundblade05ec57b2018-10-21 01:50:03 +05304644 UsefulBuf_Set(CopyOfStorage, '_');
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004645
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304646 if(Item1.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304647 Item1.uDataType != QCBOR_TYPE_INT64 ||
4648 Item1.val.int64 != 42 ||
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004649 Item1.uDataAlloc != 0 ||
4650 Item1.uLabelAlloc == 0 ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07004651 UsefulBufCompareToSZ(Item1.label.string, "first integer")) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004652 return -4;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09004653 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004654
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304655
4656 if(Item2.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07004657 UsefulBufCompareToSZ(Item2.label.string, "an array of two strings") ||
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304658 Item2.uDataType != QCBOR_TYPE_ARRAY ||
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004659 Item2.uDataAlloc != 0 ||
4660 Item2.uLabelAlloc == 0 ||
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304661 Item2.val.uCount != 2)
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004662 return -5;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004663
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304664 if(Item3.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004665 Item3.uDataAlloc == 0 ||
4666 Item3.uLabelAlloc != 0 ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07004667 UsefulBufCompareToSZ(Item3.val.string, "string1")) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004668 return -6;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09004669 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004670
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304671 if(Item4.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004672 Item4.uDataAlloc == 0 ||
4673 Item4.uLabelAlloc != 0 ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07004674 UsefulBufCompareToSZ(Item4.val.string, "string2")) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004675 return -7;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09004676 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004677
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304678 // Next parse with a pool that is too small
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004679 UsefulBuf_MAKE_STACK_UB(SmallPool, QCBOR_DECODE_MIN_MEM_POOL_SIZE + 1);
Laurence Lundbladeee851742020-01-08 08:37:05 -08004680 QCBORDecode_Init(&DC,
4681 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded),
4682 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304683 QCBORDecode_SetMemPool(&DC, SmallPool, 1); // Turn on copying.
4684 if((nCBORError = QCBORDecode_GetNext(&DC, &Item1)))
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004685 return -8;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304686 if(Item1.uDataType != QCBOR_TYPE_MAP ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09004687 Item1.val.uCount != 3) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004688 return -9;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09004689 }
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304690 if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item1))){
4691 if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item2))) {
4692 if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item3))) {
4693 nCBORError = QCBORDecode_GetNext(&DC, &Item4);
4694 }
4695 }
4696 }
Laurence Lundblade30816f22018-11-10 13:40:22 +07004697 if(nCBORError != QCBOR_ERR_STRING_ALLOCATE) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004698 return -10;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304699 }
4700
4701 return 0;
4702}
4703
Laurence Lundbladef6531662018-12-04 10:42:22 +09004704
Laurence Lundbladec5fef682020-01-25 11:38:45 -08004705int32_t MemPoolTest(void)
Laurence Lundblade0155b622018-10-12 20:04:37 +08004706{
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004707 // Set up the decoder with a tiny bit of CBOR to parse because
4708 // nothing can be done with it unless that is set up.
Laurence Lundbladef6531662018-12-04 10:42:22 +09004709 QCBORDecodeContext DC;
4710 const uint8_t pMinimalCBOR[] = {0xa0}; // One empty map
4711 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pMinimalCBOR),0);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004712
Laurence Lundbladef6531662018-12-04 10:42:22 +09004713 // Set up an memory pool of 100 bytes
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004714 // Then fish into the internals of the decode context
4715 // to get the allocator function so it can be called directly.
4716 // Also figure out how much pool is available for use
4717 // buy subtracting out the overhead.
Laurence Lundbladef6531662018-12-04 10:42:22 +09004718 UsefulBuf_MAKE_STACK_UB(Pool, 100);
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004719 QCBORError nError = QCBORDecode_SetMemPool(&DC, Pool, 0);
4720 if(nError) {
4721 return -9;
4722 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004723 QCBORStringAllocate pAlloc = DC.StringAllocator.pfAllocator;
4724 void *pAllocCtx = DC.StringAllocator.pAllocateCxt;
4725 size_t uAvailPool = Pool.len - QCBOR_DECODE_MIN_MEM_POOL_SIZE;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004726
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004727 // First test -- ask for one more byte than available and see failure
4728 UsefulBuf Allocated = (*pAlloc)(pAllocCtx, NULL, uAvailPool+1);
Laurence Lundbladef6531662018-12-04 10:42:22 +09004729 if(!UsefulBuf_IsNULL(Allocated)) {
4730 return -1;
4731 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004732
Laurence Lundbladef6531662018-12-04 10:42:22 +09004733 // Re do the set up for the next test that will do a successful alloc,
4734 // a fail, a free and then success
Laurence Lundbladef6531662018-12-04 10:42:22 +09004735 QCBORDecode_SetMemPool(&DC, Pool, 0);
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004736 pAlloc = DC.StringAllocator.pfAllocator;
4737 pAllocCtx = DC.StringAllocator.pAllocateCxt;
4738 uAvailPool = Pool.len - QCBOR_DECODE_MIN_MEM_POOL_SIZE;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004739
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004740 // Allocate one byte less than available and see success
4741 Allocated = (pAlloc)(pAllocCtx, NULL, uAvailPool-1);
Laurence Lundbladef6531662018-12-04 10:42:22 +09004742 if(UsefulBuf_IsNULL(Allocated)) { // expected to succeed
4743 return -2;
4744 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004745 // Ask for some more and see failure
4746 UsefulBuf Allocated2 = (*pAlloc)(pAllocCtx, NULL, uAvailPool/2);
Laurence Lundbladef6531662018-12-04 10:42:22 +09004747 if(!UsefulBuf_IsNULL(Allocated2)) { // expected to fail
4748 return -3;
4749 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004750 // Free the first allocate, retry the second and see success
4751 (*pAlloc)(pAllocCtx, Allocated.ptr, 0); // Free
4752 Allocated = (*pAlloc)(pAllocCtx, NULL, uAvailPool/2);
Laurence Lundbladef6531662018-12-04 10:42:22 +09004753 if(UsefulBuf_IsNULL(Allocated)) { // succeed because of the free
4754 return -4;
4755 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004756
Laurence Lundbladef6531662018-12-04 10:42:22 +09004757 // Re do set up for next test that involves a successful alloc,
4758 // and a successful realloc and a failed realloc
4759 QCBORDecode_SetMemPool(&DC, Pool, 0);
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004760 pAlloc = DC.StringAllocator.pfAllocator;
4761 pAllocCtx = DC.StringAllocator.pAllocateCxt;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004762
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004763 // Allocate half the pool and see success
4764 Allocated = (*pAlloc)(pAllocCtx, NULL, uAvailPool/2);
Laurence Lundbladef6531662018-12-04 10:42:22 +09004765 if(UsefulBuf_IsNULL(Allocated)) { // expected to succeed
4766 return -5;
4767 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004768 // Reallocate to take up the whole pool and see success
4769 Allocated2 = (*pAlloc)(pAllocCtx, Allocated.ptr, uAvailPool);
Laurence Lundbladef6531662018-12-04 10:42:22 +09004770 if(UsefulBuf_IsNULL(Allocated2)) {
4771 return -6;
4772 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004773 // Make sure its the same pointer and the size is right
Laurence Lundbladef6531662018-12-04 10:42:22 +09004774 if(Allocated2.ptr != Allocated.ptr || Allocated2.len != uAvailPool) {
4775 return -7;
4776 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004777 // Try to allocate more to be sure there is failure after a realloc
4778 UsefulBuf Allocated3 = (*pAlloc)(pAllocCtx, Allocated.ptr, uAvailPool+1);
4779 if(!UsefulBuf_IsNULL(Allocated3)) {
Laurence Lundbladef6531662018-12-04 10:42:22 +09004780 return -8;
4781 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004782
Laurence Lundbladef6531662018-12-04 10:42:22 +09004783 return 0;
4784}
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004785
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004786
4787/* Just enough of an allocator to test configuration of one */
4788static UsefulBuf AllocateTestFunction(void *pCtx, void *pOldMem, size_t uNewSize)
4789{
4790 (void)pOldMem; // unused variable
4791
4792 if(uNewSize) {
4793 // Assumes the context pointer is the buffer and
4794 // nothing too big will ever be asked for.
4795 // This is only good for this basic test!
4796 return (UsefulBuf) {pCtx, uNewSize};
4797 } else {
4798 return NULLUsefulBuf;
4799 }
4800}
4801
4802
Laurence Lundbladec5fef682020-01-25 11:38:45 -08004803int32_t SetUpAllocatorTest(void)
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004804{
4805 // Set up the decoder with a tiny bit of CBOR to parse because
4806 // nothing can be done with it unless that is set up.
4807 QCBORDecodeContext DC;
4808 const uint8_t pMinimalCBOR[] = {0x62, 0x48, 0x69}; // "Hi"
4809 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pMinimalCBOR),0);
4810
4811 uint8_t pAllocatorBuffer[50];
4812
4813 // This is really just to test that this call works.
4814 // The full functionality of string allocators is tested
4815 // elsewhere with the MemPool internal allocator.
4816 QCBORDecode_SetUpAllocator(&DC, AllocateTestFunction, pAllocatorBuffer, 1);
4817
4818 QCBORItem Item;
4819 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_SUCCESS) {
4820 return -1;
4821 }
4822
4823 if(Item.uDataAlloc == 0 ||
4824 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
4825 Item.val.string.ptr != pAllocatorBuffer) {
4826 return -2;
4827 }
4828
4829 if(QCBORDecode_Finish(&DC) != QCBOR_SUCCESS) {
4830 return -3;
4831 }
4832
4833 return 0;
4834}
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08004835#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
4836
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004837
Laurence Lundbladedd6e76e2021-03-10 01:54:01 -07004838#ifndef QCBOR_DISABLE_EXP_AND_MANTISSA
Laurence Lundblade59289e52019-12-30 13:44:37 -08004839
Laurence Lundblade37286c02022-09-03 10:05:02 -07004840struct EaMTest {
4841 const char *szName;
4842 UsefulBufC Input;
4843 uint8_t uTagRequirement;
4844 bool bHasTags;
4845
4846 /* Expected values for GetNext */
4847 QCBORError uExpectedErrorGN;
4848 uint8_t uQCBORTypeGN;
4849 int64_t nExponentGN;
4850 int64_t nMantissaGN;
4851 UsefulBufC MantissaGN;
4852
4853 /* Expected values for GetDecimalFraction */
4854 QCBORError uExpectedErrorGDF;
4855 int64_t nExponentGDF;
4856 int64_t nMantissaGDF;
4857
4858 /* Expected values for GetDecimalFractionBig */
4859 QCBORError uExpectedErrorGDFB;
4860 int64_t nExponentGDFB;
4861 UsefulBufC MantissaGDFB;
4862 bool IsNegativeGDFB;
4863
4864 /* Expected values for GetBigFloat */
4865 QCBORError uExpectedErrorGBF;
4866 int64_t nExponentGBF;
4867 int64_t nMantissaGBF;
4868
4869 /* Expected values for GetBigFloatBig */
4870 QCBORError uExpectedErrorGBFB;
4871 int64_t nExponentGBFB;
4872 UsefulBufC MantissaGBFB;
4873 bool IsNegativeGBFB;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004874};
4875
Laurence Lundbladefaec39f2020-08-02 21:53:53 -07004876
Laurence Lundblade37286c02022-09-03 10:05:02 -07004877
4878static const struct EaMTest pEaMTests[] = {
4879 {
4880 "1. Untagged pair (big float or decimal fraction), no tag required",
4881 {(const uint8_t []){0x82, 0x20, 0x03}, 3},
4882 QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
4883 false,
4884
4885 QCBOR_SUCCESS, /* for GetNext */
4886 QCBOR_TYPE_ARRAY,
4887 0,
4888 0,
4889 {(const uint8_t []){0x00}, 1},
4890
4891 QCBOR_SUCCESS, /* GetDecimalFraction */
4892 -1,
4893 3,
4894
4895 QCBOR_SUCCESS, /* for GetDecimalFractionBig */
4896 -1,
4897 {(const uint8_t []){0x03}, 1},
4898 false,
4899
4900 QCBOR_SUCCESS, /* for GetBigFloat */
4901 -1,
4902 3,
4903
4904 QCBOR_SUCCESS, /* for GetBigFloatBig */
4905 -1,
4906 {(const uint8_t []){0x03}, 1},
4907 false
4908 },
4909
4910 {
4911 "2. Untagged pair (big float or decimal fraction), tag required",
4912 {(const uint8_t []){0x82, 0x20, 0x03}, 3},
4913 QCBOR_TAG_REQUIREMENT_TAG,
4914 false,
4915
4916 QCBOR_SUCCESS, /* for GetNext */
4917 QCBOR_TYPE_ARRAY,
4918 0,
4919 0,
4920 {(const uint8_t []){0x00}, 1},
4921
4922 QCBOR_ERR_UNEXPECTED_TYPE, /* for GetDecimalFraction */
4923 0,
4924 0,
4925
4926 QCBOR_ERR_UNEXPECTED_TYPE, /* for GetDecimalFractionBig */
4927 0,
4928 {(const uint8_t []){0x00}, 1},
4929 false,
4930
4931 QCBOR_ERR_UNEXPECTED_TYPE, /* for GetBigFloat */
4932 0,
4933 0,
4934
4935 QCBOR_ERR_UNEXPECTED_TYPE, /* for GetBigFloatBig */
4936 0,
4937 {(const uint8_t []){0x00}, 1},
4938 false
4939
4940 },
4941
4942 {
4943 "3. Tagged 1.5 decimal fraction, tag 4 optional",
4944 {(const uint8_t []){0xC4, 0x82, 0x20, 0x03}, 4},
4945 QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
4946 true,
4947
4948 QCBOR_SUCCESS, /* for GetNext */
4949 QCBOR_TYPE_DECIMAL_FRACTION,
4950 -1,
4951 3,
4952 {(const uint8_t []){0x00}, 1},
4953
4954
4955 QCBOR_SUCCESS, /* for GetDecimalFraction */
4956 -1,
4957 3,
4958
4959 QCBOR_SUCCESS, /* for GetDecimalFractionBig */
4960 -1,
4961 {(const uint8_t []){0x03}, 1},
4962 false,
4963
4964 QCBOR_ERR_UNEXPECTED_TYPE, /* for GetBigFloat */
4965 0,
4966 0,
4967
4968 QCBOR_ERR_UNEXPECTED_TYPE, /* for GetBigFloatBig */
4969 0,
4970 {(const uint8_t []){0x00}, 1},
4971 false
4972 },
4973 {
4974 "4. Tagged 100 * 2^300 big float, tag 5 optional",
4975 {(const uint8_t []){0xC5, 0x82, 0x19, 0x01, 0x2C, 0x18, 0x64}, 7},
4976 QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
4977 true,
4978
4979 QCBOR_SUCCESS, /* for GetNext */
4980 QCBOR_TYPE_BIGFLOAT,
4981 300,
4982 100,
4983 {(const uint8_t []){0x00}, 1},
4984
4985
4986 QCBOR_ERR_UNEXPECTED_TYPE, /* for GetDecimalFraction */
4987 0,
4988 0,
4989
4990 QCBOR_ERR_UNEXPECTED_TYPE, /* for GetDecimalFractionBig */
4991 0,
4992 {(const uint8_t []){0x03}, 1},
4993 false,
4994
4995 QCBOR_SUCCESS, /* for GetBigFloat */
4996 300,
4997 100,
4998
4999 QCBOR_SUCCESS, /* for GetBigFloatBig */
5000 300,
5001 {(const uint8_t []){0x64}, 1},
5002 false
5003 },
5004
5005 {
5006 "5. Tagged 4([-20, 4759477275222530853136]) decimal fraction, tag 4 required",
5007 {(const uint8_t []){0xC4, 0x82, 0x33,
5008 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,}, 15},
5009 QCBOR_TAG_REQUIREMENT_TAG,
5010 true,
5011
5012 QCBOR_SUCCESS, /* for GetNext */
5013 QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM,
5014 -20,
5015 0,
5016 {(const uint8_t []){0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10}, 10},
5017
5018 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW, /* for GetDecimalFraction */
5019 0,
5020 0,
5021
5022 QCBOR_SUCCESS, /* for GetDecimalFractionBig */
5023 -20,
5024 {(const uint8_t []){0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10}, 10},
5025 false,
5026
5027 QCBOR_ERR_UNEXPECTED_TYPE, /* for GetBigFloat */
5028 0,
5029 0,
5030
5031 QCBOR_ERR_UNEXPECTED_TYPE, /* for GetBigFloatBig */
5032 0,
5033 {(const uint8_t []){0x00}, 0},
5034 false
5035 },
5036
5037 {
5038 "6. Error: Mantissa and exponent inside a Mantissa and exponent",
5039 {(const uint8_t []){0xC4, 0x82, 0x33,
5040 0xC5, 0x82, 0x19, 0x01, 0x2C, 0x18, 0x64}, 10},
5041 QCBOR_TAG_REQUIREMENT_TAG,
5042 true,
5043
5044 QCBOR_ERR_BAD_EXP_AND_MANTISSA, /* for GetNext */
5045 QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM,
5046 0,
5047 0,
5048 {(const uint8_t []){0x00}, 0},
5049
5050 QCBOR_ERR_BAD_EXP_AND_MANTISSA, /* for GetDecimalFraction */
5051 0,
5052 0,
5053
5054 QCBOR_ERR_BAD_EXP_AND_MANTISSA, /* for GetDecimalFractionBig */
5055 0,
5056 {(const uint8_t []){0x00}, 0},
5057 false,
5058
5059 QCBOR_ERR_BAD_EXP_AND_MANTISSA, /* for GetBigFloat */
5060 0,
5061 0,
5062
5063 QCBOR_ERR_BAD_EXP_AND_MANTISSA, /* for GetBigFloatBig */
5064 0,
5065 {(const uint8_t []){0x00}, 0},
5066 false
5067 },
5068 {
5069 "7. Tagged 5([-20, 4294967295]) big float, big num mantissa, tag 5 required",
5070 {(const uint8_t []){0xC5, 0x82, 0x33,
5071 0xC2, 0x44, 0xff, 0xff, 0xff, 0xff}, 9},
5072 QCBOR_TAG_REQUIREMENT_TAG,
5073 true,
5074
5075 QCBOR_SUCCESS, /* for GetNext */
5076 QCBOR_TYPE_BIGFLOAT_POS_BIGNUM,
5077 -20,
5078 0,
5079 {(const uint8_t []){0xff, 0xff, 0xff, 0xff}, 4},
5080
5081 QCBOR_ERR_UNEXPECTED_TYPE, /* for GetDecimalFraction */
5082 0,
5083 0,
5084
5085 QCBOR_ERR_UNEXPECTED_TYPE, /* for GetDecimalFractionBig */
5086 -20,
5087 {(const uint8_t []){0x00}, 1},
5088 false,
5089
5090 QCBOR_SUCCESS, /* for GetBigFloat */
5091 -20,
5092 4294967295,
5093
5094 QCBOR_SUCCESS, /* for GetBigFloatBig */
5095 -20,
5096 {(const uint8_t []){0xff, 0xff, 0xff, 0xff}, 4},
5097 false
5098 },
5099
5100 {
5101 /* Special case for test 8. Don't renumber it. */
5102 "8. Untagged pair with big num (big float or decimal fraction), tag optional",
5103 {(const uint8_t []){0x82, 0x33, 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10}, 14},
5104 QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
5105 true,
5106
5107 QCBOR_SUCCESS, /* for GetNext */
5108 QCBOR_TYPE_ARRAY,
5109 0,
5110 0,
5111 {(const uint8_t []){0x00}, 1},
5112
5113 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW, /* GetDecimalFraction */
5114 0,
5115 0,
5116
5117 QCBOR_SUCCESS, /* for GetDecimalFractionBig */
5118 -20,
5119 {(const uint8_t []){0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10}, 10},
5120 false,
5121
5122 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW, /* for GetBigFloat */
5123 0,
5124 0,
5125
5126 QCBOR_SUCCESS, /* for GetBigFloatBig */
5127 -20,
5128 {(const uint8_t []){0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10}, 10},
5129 false
5130 },
5131
5132 {
5133 "9. decimal fraction with large exponent and negative big num mantissa",
5134 {(const uint8_t []){0xC4, 0x82, 0x1B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
5135 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10}, 23},
5136 QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
5137 true,
5138
5139 QCBOR_SUCCESS, /* for GetNext */
5140 QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM,
5141 9223372036854775807,
5142 0,
5143 {(const uint8_t []){0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10}, 10},
5144
5145 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW, /* GetDecimalFraction */
5146 0,
5147 0,
5148
5149 QCBOR_SUCCESS, /* for GetDecimalFractionBig */
5150 9223372036854775807,
5151 {(const uint8_t []){0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10}, 10},
5152 true,
5153
5154 QCBOR_ERR_UNEXPECTED_TYPE, /* for GetBigFloat */
5155 0,
5156 0,
5157
5158 QCBOR_ERR_UNEXPECTED_TYPE, /* for GetBigFloatBig */
5159 0,
5160 {(const uint8_t []){0x00}, 1},
5161 false
5162 },
5163};
5164
5165
5166
5167int32_t ProcessEaMTests(void)
Laurence Lundblade59289e52019-12-30 13:44:37 -08005168{
Laurence Lundblade37286c02022-09-03 10:05:02 -07005169 size_t uIndex;
5170 QCBORDecodeContext DCtx;
5171 QCBORItem Item;
5172 QCBORError uError;
5173 int64_t nMantissa, nExponent;
5174 MakeUsefulBufOnStack( MantissaBuf, 200);
5175 UsefulBufC Mantissa;
5176 bool bMantissaIsNegative;
5177
5178 for(uIndex = 0; uIndex < C_ARRAY_COUNT(pEaMTests, struct EaMTest); uIndex++) {
5179 const struct EaMTest *pT = &pEaMTests[uIndex];
5180 /* Decode with GetNext */
5181 QCBORDecode_Init(&DCtx, pT->Input, 0);
5182
5183 if(uIndex + 1 == 9) {
5184 nExponent = 99; // just to set a break point
5185 }
5186
5187 uError = QCBORDecode_GetNext(&DCtx, &Item);
5188#ifdef QCBOR_DISABLE_TAGS
5189 /* Test 8 is a special case when tags are disabled */
5190 if(pT->bHasTags && uIndex + 1 != 8) {
5191 if(uError != QCBOR_ERR_TAGS_DISABLED) {
5192 return (int32_t)(1+uIndex) * 1000 + 9;
5193 }
5194 } else {
5195#endif
5196 /* Now check return code, data type, mantissa and exponent */
5197 if(pT->uExpectedErrorGN != uError) {
5198 return (int32_t)(1+uIndex) * 1000 + 1;
5199 }
5200 if(uError == QCBOR_SUCCESS && pT->uQCBORTypeGN != QCBOR_TYPE_ARRAY) {
5201 if(pT->uQCBORTypeGN != Item.uDataType) {
5202 return (int32_t)(1+uIndex) * 1000 + 2;
5203 }
5204 if(pT->nExponentGN != Item.val.expAndMantissa.nExponent) {
5205 return (int32_t)(1+uIndex) * 1000 + 3;
5206 }
5207 if(Item.uDataType == QCBOR_TYPE_DECIMAL_FRACTION || Item.uDataType == QCBOR_TYPE_BIGFLOAT ) {
5208 if(pT->nMantissaGN != Item.val.expAndMantissa.Mantissa.nInt) {
5209 return (int32_t)(1+uIndex) * 1000 + 4;
5210 }
5211 } else {
5212 if(UsefulBuf_Compare(Item.val.expAndMantissa.Mantissa.bigNum, pT->MantissaGN)) {
5213 return (int32_t)(1+uIndex) * 1000 + 5;
5214 }
5215 }
5216 }
5217#ifdef QCBOR_DISABLE_TAGS
5218 }
5219#endif
5220
5221 /* Decode with GetDecimalFraction */
5222 QCBORDecode_Init(&DCtx, pT->Input, 0);
5223 QCBORDecode_GetDecimalFraction(&DCtx,
5224 pT->uTagRequirement,
5225 &nMantissa,
5226 &nExponent);
5227 uError = QCBORDecode_GetAndResetError(&DCtx);
5228#ifdef QCBOR_DISABLE_TAGS
5229 if(pT->bHasTags) {
5230 if(uError != QCBOR_ERR_TAGS_DISABLED) {
5231 return (int32_t)(1+uIndex) * 1000 + 39;
5232 }
5233 } else {
5234#endif
5235 /* Now check return code, mantissa and exponent */
5236 if(pT->uExpectedErrorGDF != uError) {
5237 return (int32_t)(1+uIndex) * 1000 + 31;
5238 }
5239 if(uError == QCBOR_SUCCESS) {
5240 if(pT->nExponentGDF != nExponent) {
5241 return (int32_t)(1+uIndex) * 1000 + 32;
5242 }
5243 if(pT->nMantissaGDF != nMantissa) {
5244 return (int32_t)(1+uIndex) * 1000 + 33;
5245 }
5246 }
5247#ifdef QCBOR_DISABLE_TAGS
5248 }
5249#endif
5250
5251 /* Decode with GetDecimalFractionBig */
5252 QCBORDecode_Init(&DCtx, pT->Input, 0);
5253 QCBORDecode_GetDecimalFractionBig(&DCtx,
5254 pT->uTagRequirement,
5255 MantissaBuf,
5256 &Mantissa,
5257 &bMantissaIsNegative,
5258 &nExponent);
5259 uError = QCBORDecode_GetAndResetError(&DCtx);
5260#ifdef QCBOR_DISABLE_TAGS
5261 if(pT->bHasTags) {
5262 if(uError != QCBOR_ERR_TAGS_DISABLED) {
5263 return (int32_t)(1+uIndex) * 1000 + 49;
5264 }
5265 } else {
5266#endif
5267 /* Now check return code, mantissa (bytes and sign) and exponent */
5268 if(pT->uExpectedErrorGDFB != uError) {
5269 return (int32_t)(1+uIndex) * 1000 + 41;
5270 }
5271 if(uError == QCBOR_SUCCESS) {
5272 if(pT->nExponentGDFB != nExponent) {
5273 return (int32_t)(1+uIndex) * 1000 + 42;
5274 }
5275 if(pT->IsNegativeGDFB != bMantissaIsNegative) {
5276 return (int32_t)(1+uIndex) * 1000 + 43;
5277 }
5278 if(UsefulBuf_Compare(Mantissa, pT->MantissaGDFB)) {
5279 return (int32_t)(1+uIndex) * 1000 + 44;
5280 }
5281 }
5282#ifdef QCBOR_DISABLE_TAGS
5283 }
5284#endif
5285
5286 /* Decode with GetBigFloat */
5287 QCBORDecode_Init(&DCtx, pT->Input, 0);
5288 QCBORDecode_GetBigFloat(&DCtx,
5289 pT->uTagRequirement,
5290 &nMantissa,
5291 &nExponent);
5292 uError = QCBORDecode_GetAndResetError(&DCtx);
5293#ifdef QCBOR_DISABLE_TAGS
5294 if(pT->bHasTags) {
5295 if(uError != QCBOR_ERR_TAGS_DISABLED) {
5296 return (int32_t)(1+uIndex) * 1000 + 19;
5297 }
5298 } else {
5299#endif
5300 /* Now check return code, mantissa and exponent */
5301 if(pT->uExpectedErrorGBF != uError) {
5302 return (int32_t)(1+uIndex) * 1000 + 11;
5303 }
5304 if(uError == QCBOR_SUCCESS) {
5305 if(pT->nExponentGBF != nExponent) {
5306 return (int32_t)(1+uIndex) * 1000 + 12;
5307 }
5308 if(pT->nMantissaGBF != nMantissa) {
5309 return (int32_t)(1+uIndex) * 1000 + 13;
5310 }
5311 }
5312#ifdef QCBOR_DISABLE_TAGS
5313 }
5314#endif
5315
5316 /* Decode with GetBigFloatBig */
5317 QCBORDecode_Init(&DCtx, pT->Input, 0);
5318 QCBORDecode_GetBigFloatBig(&DCtx,
5319 pT->uTagRequirement,
5320 MantissaBuf,
5321 &Mantissa,
5322 &bMantissaIsNegative,
5323 &nExponent);
5324 uError = QCBORDecode_GetAndResetError(&DCtx);
5325#ifdef QCBOR_DISABLE_TAGS
5326 if(pT->bHasTags) {
5327 if(uError != QCBOR_ERR_TAGS_DISABLED) {
5328 return (int32_t)(1+uIndex) * 1000 + 29;
5329 }
5330 } else {
5331#endif
5332 /* Now check return code, mantissa (bytes and sign) and exponent */
5333 if(pT->uExpectedErrorGBFB != uError) {
5334 return (int32_t)(1+uIndex) * 1000 + 21;
5335 }
5336 if(uError == QCBOR_SUCCESS) {
5337 if(pT->nExponentGBFB != nExponent) {
5338 return (int32_t)(1+uIndex) * 1000 + 22;
5339 }
5340 if(pT->IsNegativeGBFB != bMantissaIsNegative) {
5341 return (int32_t)(1+uIndex) * 1000 + 23;
5342 }
5343 if(UsefulBuf_Compare(Mantissa, pT->MantissaGBFB)) {
5344 return (int32_t)(1+uIndex) * 1000 + 24;
5345 }
5346 }
5347#ifdef QCBOR_DISABLE_TAGS
5348 }
5349#endif
5350 }
5351
5352 return 0;
5353}
5354
5355
5356int32_t ExponentAndMantissaDecodeTestsSecondary(void)
5357{
5358#ifndef QCBOR_DISABLE_TAGS
Laurence Lundblade59289e52019-12-30 13:44:37 -08005359 QCBORDecodeContext DC;
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07005360 QCBORError uErr;
Laurence Lundblade59289e52019-12-30 13:44:37 -08005361 QCBORItem item;
5362
Laurence Lundblade17af4902020-01-07 19:11:55 -08005363 static const uint8_t spBigNumMantissa[] = {0x01, 0x02, 0x03, 0x04, 0x05,
5364 0x06, 0x07, 0x08, 0x09, 0x010};
5365 UsefulBufC BN = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNumMantissa);
Laurence Lundblade59289e52019-12-30 13:44:37 -08005366
5367
Laurence Lundblade59289e52019-12-30 13:44:37 -08005368
5369 /* Now encode some stuff and then decode it */
5370 uint8_t pBuf[40];
5371 QCBOREncodeContext EC;
5372 UsefulBufC Encoded;
5373
5374 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(pBuf));
5375 QCBOREncode_OpenArray(&EC);
5376 QCBOREncode_AddDecimalFraction(&EC, 999, 1000); // 999 * (10 ^ 1000)
5377 QCBOREncode_AddBigFloat(&EC, 100, INT32_MIN);
5378 QCBOREncode_AddDecimalFractionBigNum(&EC, BN, false, INT32_MAX);
5379 QCBOREncode_CloseArray(&EC);
5380 QCBOREncode_Finish(&EC, &Encoded);
5381
5382
5383 QCBORDecode_Init(&DC, Encoded, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07005384 uErr = QCBORDecode_GetNext(&DC, &item);
5385 if(uErr != QCBOR_SUCCESS) {
5386 return 100;
Laurence Lundblade59289e52019-12-30 13:44:37 -08005387 }
5388
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07005389 uErr = QCBORDecode_GetNext(&DC, &item);
5390 if(uErr != QCBOR_SUCCESS) {
5391 return 101;
Laurence Lundblade59289e52019-12-30 13:44:37 -08005392 }
5393
5394 if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION ||
5395 item.val.expAndMantissa.nExponent != 1000 ||
5396 item.val.expAndMantissa.Mantissa.nInt != 999) {
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07005397 return 102;
Laurence Lundblade59289e52019-12-30 13:44:37 -08005398 }
5399
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07005400 uErr = QCBORDecode_GetNext(&DC, &item);
5401 if(uErr != QCBOR_SUCCESS) {
5402 return 103;
Laurence Lundblade59289e52019-12-30 13:44:37 -08005403 }
5404
5405 if(item.uDataType != QCBOR_TYPE_BIGFLOAT ||
5406 item.val.expAndMantissa.nExponent != INT32_MIN ||
5407 item.val.expAndMantissa.Mantissa.nInt != 100) {
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07005408 return 104;
Laurence Lundblade59289e52019-12-30 13:44:37 -08005409 }
5410
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07005411 uErr = QCBORDecode_GetNext(&DC, &item);
5412 if(uErr != QCBOR_SUCCESS) {
5413 return 105;
Laurence Lundblade59289e52019-12-30 13:44:37 -08005414 }
5415
5416 if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM ||
5417 item.val.expAndMantissa.nExponent != INT32_MAX ||
5418 UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) {
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07005419 return 106;
5420 }
5421
Laurence Lundblade37286c02022-09-03 10:05:02 -07005422#endif /* QCBOR_TAGS_DISABLED */
Laurence Lundblade59289e52019-12-30 13:44:37 -08005423
5424 return 0;
5425}
5426
5427
Laurence Lundblade37286c02022-09-03 10:05:02 -07005428int32_t ExponentAndMantissaDecodeTests(void)
5429{
5430 int32_t rv = ProcessEaMTests();
5431 if(rv) {
5432 return rv;
5433 }
5434
5435 return ExponentAndMantissaDecodeTestsSecondary();
5436}
5437
5438
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08005439static const struct DecodeFailTestInput ExponentAndMantissaFailures[] = {
5440 { "Exponent > INT64_MAX",
5441 QCBOR_DECODE_MODE_NORMAL,
5442 {"\xC4\x82\x1B\x7f\xFF\xFF\xFF\xFF\xFF\xFF\xFF\x1B\x80\xFF\xFF\xFF\xFF\xFF\xFF\xFF", 20},
5443 QCBOR_ERR_BAD_EXP_AND_MANTISSA
5444 },
5445 { "Mantissa > INT64_MAX",
5446 QCBOR_DECODE_MODE_NORMAL,
5447 {"\xC4\x82\x1B\x80\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xC3\x4A\x01\x02\x03\x04\x05\x06\x07\x08\x09\x10", 23},
5448 QCBOR_ERR_BAD_EXP_AND_MANTISSA
5449 },
5450 {
5451 "End of input",
5452 QCBOR_DECODE_MODE_NORMAL,
5453 {"\xC4\x82", 2},
5454 QCBOR_ERR_NO_MORE_ITEMS
5455 },
5456 {"bad content for big num",
5457 QCBOR_DECODE_MODE_NORMAL,
5458 {"\xC4\x82\x01\xc3\x01", 5},
5459 QCBOR_ERR_BAD_OPT_TAG
5460 },
5461 {"bad content for big num",
5462 QCBOR_DECODE_MODE_NORMAL,
5463 {"\xC4\x82\xc2\x01\x1f", 5},
5464 QCBOR_ERR_BAD_INT
5465 },
5466 {"Bad integer for exponent",
5467 QCBOR_DECODE_MODE_NORMAL,
5468 {"\xC4\x82\x01\x1f", 4},
5469 QCBOR_ERR_BAD_INT
5470 },
5471 {"Bad integer for mantissa",
5472 QCBOR_DECODE_MODE_NORMAL,
5473 {"\xC4\x82\x1f\x01", 4},
5474 QCBOR_ERR_BAD_INT
5475 },
5476 {"3 items in array",
5477 QCBOR_DECODE_MODE_NORMAL,
5478 {"\xC4\x83\x03\x01\x02", 5},
5479 QCBOR_ERR_BAD_EXP_AND_MANTISSA},
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08005480#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08005481 {"unterminated indefinite length array",
5482 QCBOR_DECODE_MODE_NORMAL,
5483 {"\xC4\x9f\x03\x01\x02", 5},
5484 QCBOR_ERR_BAD_EXP_AND_MANTISSA
5485 },
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08005486#else /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08005487 {"unterminated indefinite length array",
5488 QCBOR_DECODE_MODE_NORMAL,
5489 {"\xC4\x9f\x03\x01\x02", 5},
5490 QCBOR_ERR_INDEF_LEN_ARRAYS_DISABLED
5491 },
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08005492#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08005493 {"Empty array",
5494 QCBOR_DECODE_MODE_NORMAL,
5495 {"\xC4\x80", 2},
5496 QCBOR_ERR_NO_MORE_ITEMS
5497 },
5498 {"Second is not an integer",
5499 QCBOR_DECODE_MODE_NORMAL,
5500 {"\xC4\x82\x03\x40", 4},
5501 QCBOR_ERR_BAD_EXP_AND_MANTISSA
5502 },
5503 {"First is not an integer",
5504 QCBOR_DECODE_MODE_NORMAL,
5505 {"\xC4\x82\x40", 3},
5506 QCBOR_ERR_BAD_EXP_AND_MANTISSA
5507 },
5508 {"Not an array",
5509 QCBOR_DECODE_MODE_NORMAL,
5510 {"\xC4\xA2", 2},
5511 QCBOR_ERR_BAD_EXP_AND_MANTISSA
5512 }
Laurence Lundblade59289e52019-12-30 13:44:37 -08005513};
5514
5515
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08005516int32_t
5517ExponentAndMantissaDecodeFailTests(void)
Laurence Lundblade59289e52019-12-30 13:44:37 -08005518{
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08005519 return ProcessDecodeFailures(ExponentAndMantissaFailures,
5520 C_ARRAY_COUNT(ExponentAndMantissaFailures,
5521 struct DecodeFailTestInput));
Laurence Lundblade59289e52019-12-30 13:44:37 -08005522}
5523
Laurence Lundbladedd6e76e2021-03-10 01:54:01 -07005524#endif /* QCBOR_DISABLE_EXP_AND_MANTISSA */
Laurence Lundbladebb87be22020-04-09 19:15:32 -07005525
5526
5527
5528/*
5529 Some basic CBOR with map and array used in a lot of tests.
5530 The map labels are all strings
5531
Laurence Lundblade8ffdb742020-05-07 02:49:18 -07005532 {
5533 "first integer": 42,
Laurence Lundbladebb87be22020-04-09 19:15:32 -07005534 "an array of two strings": [
5535 "string1", "string2"
5536 ],
5537 "map in a map": {
5538 "bytes 1": h'78787878',
5539 "bytes 2": h'79797979',
5540 "another int": 98,
5541 "text 2": "lies, damn lies and statistics"
5542 }
Laurence Lundbladeddebabe2020-11-22 11:32:17 -08005543 }
Laurence Lundbladebb87be22020-04-09 19:15:32 -07005544 */
Laurence Lundblade9b334962020-08-27 10:55:53 -07005545
Laurence Lundbladef7a70bc2020-10-24 12:23:25 -07005546int32_t SpiffyDecodeBasicMap(UsefulBufC input)
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07005547{
5548 QCBORItem Item1, Item2, Item3;
5549 int64_t nDecodedInt1, nDecodedInt2;
5550 UsefulBufC B1, B2, S1, S2, S3;
5551
5552 QCBORDecodeContext DCtx;
5553 QCBORError nCBORError;
5554
5555 QCBORDecode_Init(&DCtx, input, 0);
5556
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005557 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07005558
5559 QCBORDecode_GetInt64InMapSZ(&DCtx, "first integer", &nDecodedInt1);
5560
5561 QCBORDecode_EnterMapFromMapSZ(&DCtx, "map in a map");
5562 QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2);
Laurence Lundblade323f8a92020-09-06 19:43:09 -07005563 QCBORDecode_GetByteStringInMapSZ(&DCtx, "bytes 1", &B1);
5564 QCBORDecode_GetByteStringInMapSZ(&DCtx, "bytes 2", &B2);
5565 QCBORDecode_GetTextStringInMapSZ(&DCtx, "text 2", &S1);
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07005566 QCBORDecode_ExitMap(&DCtx);
5567
5568 QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings");
5569 QCBORDecode_GetNext(&DCtx, &Item1);
5570 QCBORDecode_GetNext(&DCtx, &Item2);
5571 if(QCBORDecode_GetNext(&DCtx, &Item3) != QCBOR_ERR_NO_MORE_ITEMS) {
5572 return -400;
5573 }
5574 QCBORDecode_ExitArray(&DCtx);
5575
5576 // Parse the same array again using GetText() instead of GetItem()
5577 QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings");
Laurence Lundblade323f8a92020-09-06 19:43:09 -07005578 QCBORDecode_GetTextString(&DCtx, &S2);
5579 QCBORDecode_GetTextString(&DCtx, &S3);
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07005580 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) {
5581 return 5000;
5582 }
5583 /* QCBORDecode_GetText(&DCtx, &S3);
5584 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_NO_MORE_ITEMS) {
5585 return 5001;
5586 } */
5587
5588 QCBORDecode_ExitArray(&DCtx);
5589
5590 QCBORDecode_ExitMap(&DCtx);
5591
5592 nCBORError = QCBORDecode_Finish(&DCtx);
5593
5594 if(nCBORError) {
5595 return (int32_t)nCBORError;
5596 }
5597
5598 if(nDecodedInt1 != 42) {
5599 return 1001;
5600 }
5601
5602 if(nDecodedInt2 != 98) {
5603 return 1002;
5604 }
5605
5606 if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07005607 UsefulBufCompareToSZ(Item1.val.string, "string1")) {
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07005608 return 1003;
5609 }
5610
5611 if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07005612 UsefulBufCompareToSZ(Item2.val.string, "string2")) {
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07005613 return 1004;
5614 }
5615
Laurence Lundblade9b334962020-08-27 10:55:53 -07005616 if(UsefulBufCompareToSZ(S1, "lies, damn lies and statistics")) {
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07005617 return 1005;
5618 }
5619
5620 if(UsefulBuf_Compare(B1, UsefulBuf_FromSZ("xxxx"))){
5621 return 1006;
5622 }
5623
5624 if(UsefulBuf_Compare(B2, UsefulBuf_FromSZ("yyyy"))){
5625 return 1007;
5626 }
5627
5628 if(UsefulBuf_Compare(S2, UsefulBuf_FromSZ("string1"))){
5629 return 1008;
5630 }
5631
5632 if(UsefulBuf_Compare(S3, UsefulBuf_FromSZ("string2"))){
5633 return 1009;
5634 }
5635
5636 return 0;
5637}
5638
Laurence Lundbladeddebabe2020-11-22 11:32:17 -08005639/*
Laurence Lundbladecc7da412020-12-27 00:09:07 -08005640 {
5641 -75008: h'05083399',
5642 88: [],
5643 100100: {
5644 "sub1": {
5645 10: [
5646 0
5647 ],
5648 -75009: h'A46823990001',
5649 100100: {
5650 "json": "{ \"ueid\", \"xyz\"}",
5651 "subsub": {
5652 100002: h'141813191001'
5653 }
5654 }
Laurence Lundbladeddebabe2020-11-22 11:32:17 -08005655 }
Laurence Lundbladecc7da412020-12-27 00:09:07 -08005656 }
Laurence Lundbladeddebabe2020-11-22 11:32:17 -08005657 }
Laurence Lundbladeddebabe2020-11-22 11:32:17 -08005658 */
5659
5660static const uint8_t spNestedCBOR[] = {
Laurence Lundbladecc7da412020-12-27 00:09:07 -08005661 0xa3, 0x3a, 0x00, 0x01, 0x24, 0xff, 0x44, 0x05,
5662 0x08, 0x33, 0x99, 0x18, 0x58, 0x80, 0x1a, 0x00,
5663 0x01, 0x87, 0x04, 0xa1, 0x64, 0x73, 0x75, 0x62,
5664 0x31, 0xa3, 0x0a, 0x81, 0x00, 0x3a, 0x00, 0x01,
5665 0x25, 0x00, 0x46, 0xa4, 0x68, 0x23, 0x99, 0x00,
5666 0x01, 0x1a, 0x00, 0x01, 0x87, 0x04, 0xa2, 0x64,
5667 0x6a, 0x73, 0x6f, 0x6e, 0x70, 0x7b, 0x20, 0x22,
5668 0x75, 0x65, 0x69, 0x64, 0x22, 0x2c, 0x20, 0x22,
5669 0x78, 0x79, 0x7a, 0x22, 0x7d, 0x66, 0x73, 0x75,
5670 0x62, 0x73, 0x75, 0x62, 0xa1, 0x1a, 0x00, 0x01,
5671 0x86, 0xa2, 0x46, 0x14, 0x18, 0x13, 0x19, 0x10,
5672 0x01
Laurence Lundbladeddebabe2020-11-22 11:32:17 -08005673};
5674
5675/* Get item in multi-level nesting in spNestedCBOR */
5676static int32_t DecodeNestedGetSubSub(QCBORDecodeContext *pDCtx)
5677{
5678 UsefulBufC String;
5679
5680 uint8_t test_oemid_bytes[] = {0x14, 0x18, 0x13, 0x19, 0x10, 0x01};
5681 const struct q_useful_buf_c test_oemid = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(test_oemid_bytes);
5682
5683 QCBORDecode_EnterMapFromMapN(pDCtx, 100100);
5684 QCBORDecode_EnterMap(pDCtx, NULL);
5685 QCBORDecode_EnterMapFromMapN(pDCtx, 100100);
5686 QCBORDecode_EnterMapFromMapSZ(pDCtx, "subsub");
5687 QCBORDecode_GetByteStringInMapN(pDCtx, 100002, &String);
5688 if(QCBORDecode_GetError(pDCtx)) {
5689 return 4001;
5690 }
5691 if(UsefulBuf_Compare(String, test_oemid)) {
5692 return 4002;
5693 }
5694 QCBORDecode_ExitMap(pDCtx);
5695 QCBORDecode_ExitMap(pDCtx);
5696 QCBORDecode_ExitMap(pDCtx);
5697 QCBORDecode_ExitMap(pDCtx);
5698
5699 return 0;
5700}
5701
5702/* Iterations on the zero-length array in spNestedCBOR */
5703static int32_t DecodeNestedGetEmpty(QCBORDecodeContext *pDCtx)
5704{
5705 QCBORItem Item;
5706 QCBORError uErr;
5707
5708 QCBORDecode_EnterArrayFromMapN(pDCtx, 88);
5709 for(int x = 0; x < 20; x++) {
5710 uErr = QCBORDecode_GetNext(pDCtx, &Item);
5711 if(uErr != QCBOR_ERR_NO_MORE_ITEMS) {
5712 return 4100;
5713
5714 }
5715 }
5716 QCBORDecode_ExitArray(pDCtx);
5717 if(QCBORDecode_GetError(pDCtx)) {
5718 return 4101;
5719 }
5720
5721 return 0;
5722}
5723
5724/* Various iterations on the array that contains a zero in spNestedCBOR */
5725static int32_t DecodeNestedGetZero(QCBORDecodeContext *pDCtx)
5726{
5727 QCBORError uErr;
5728
5729 QCBORDecode_EnterMapFromMapN(pDCtx, 100100);
5730 QCBORDecode_EnterMapFromMapSZ(pDCtx, "sub1");
5731 QCBORDecode_EnterArrayFromMapN(pDCtx, 10);
5732 int64_t nInt = 99;
5733 QCBORDecode_GetInt64(pDCtx, &nInt);
5734 if(nInt != 0) {
5735 return 4200;
5736 }
5737 for(int x = 0; x < 20; x++) {
5738 QCBORItem Item;
5739 uErr = QCBORDecode_GetNext(pDCtx, &Item);
5740 if(uErr != QCBOR_ERR_NO_MORE_ITEMS) {
5741 return 4201;
5742
5743 }
5744 }
5745 QCBORDecode_ExitArray(pDCtx);
5746 if(QCBORDecode_GetAndResetError(pDCtx)) {
5747 return 4202;
5748 }
5749 QCBORDecode_EnterArrayFromMapN(pDCtx, 10);
5750 UsefulBufC dD;
5751 QCBORDecode_GetByteString(pDCtx, &dD);
5752 if(QCBORDecode_GetAndResetError(pDCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
5753 return 4203;
5754 }
5755 for(int x = 0; x < 20; x++) {
5756 QCBORDecode_GetByteString(pDCtx, &dD);
5757 uErr = QCBORDecode_GetAndResetError(pDCtx);
5758 if(uErr != QCBOR_ERR_NO_MORE_ITEMS) {
5759 return 4204;
5760 }
5761 }
5762 QCBORDecode_ExitArray(pDCtx);
5763 QCBORDecode_ExitMap(pDCtx);
5764 QCBORDecode_ExitMap(pDCtx);
5765
5766 return 0;
5767}
5768
5769/* Repeatedly enter and exit maps and arrays, go off the end of maps
5770 and arrays and such. */
Laurence Lundbladeb9702452021-03-08 21:02:57 -08005771static int32_t DecodeNestedIterate(void)
Laurence Lundbladeddebabe2020-11-22 11:32:17 -08005772{
5773 QCBORDecodeContext DCtx;
5774 int32_t nReturn;
5775 QCBORError uErr;
5776
5777 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spNestedCBOR), 0);
5778 QCBORDecode_EnterMap(&DCtx, NULL);
5779
5780 for(int j = 0; j < 5; j++) {
5781 for(int i = 0; i < 20; i++) {
5782 nReturn = DecodeNestedGetSubSub(&DCtx);
5783 if(nReturn) {
5784 return nReturn;
5785 }
5786 }
5787
5788 for(int i = 0; i < 20; i++) {
5789 nReturn = DecodeNestedGetEmpty(&DCtx);
5790 if(nReturn ) {
5791 return nReturn;
5792 }
5793 }
5794
5795 for(int i = 0; i < 20; i++) {
5796 nReturn = DecodeNestedGetZero(&DCtx);
5797 if(nReturn ) {
5798 return nReturn;
5799 }
5800 }
5801 }
5802
5803 QCBORDecode_ExitMap(&DCtx);
5804 uErr = QCBORDecode_Finish(&DCtx);
5805 if(uErr) {
5806 return (int32_t)uErr + 4100;
5807 }
5808
5809 return 0;
5810}
5811
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07005812
5813/*
Laurence Lundbladecc7da412020-12-27 00:09:07 -08005814 [
5815 23,
5816 6000,
5817 h'67616C6163746963',
5818 h'686176656E20746F6B656E'
5819 ]
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07005820 */
5821static const uint8_t spSimpleArray[] = {
Laurence Lundblade9b334962020-08-27 10:55:53 -07005822 0x84,
5823 0x17,
5824 0x19, 0x17, 0x70,
5825 0x48, 0x67, 0x61, 0x6C, 0x61, 0x63, 0x74, 0x69, 0x63,
5826 0x4B, 0x68, 0x61, 0x76, 0x65, 0x6E, 0x20, 0x74, 0x6F, 0x6B, 0x65, 0x6E};
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07005827
Laurence Lundbladecc7da412020-12-27 00:09:07 -08005828/* [h'', {}, [], 0] */
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08005829static const uint8_t spArrayOfEmpty[] = {0x84, 0x40, 0xa0, 0x80, 0x00};
5830
Laurence Lundbladecc7da412020-12-27 00:09:07 -08005831/* {} */
Laurence Lundbladee6f15112020-07-23 18:44:16 -07005832static const uint8_t spEmptyMap[] = {0xa0};
5833
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08005834#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundbladecc7da412020-12-27 00:09:07 -08005835/* {} */
Laurence Lundbladee6f15112020-07-23 18:44:16 -07005836static const uint8_t spEmptyInDefinteLengthMap[] = {0xbf, 0xff};
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07005837
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08005838
Laurence Lundbladef0499502020-08-01 11:55:57 -07005839/*
5840 {
5841 0: [],
5842 9: [
5843 [],
5844 []
5845 ],
5846 8: {
5847 1: [],
5848 2: {},
5849 3: []
5850 },
5851 4: {},
5852 5: [],
5853 6: [
5854 [],
5855 []
5856 ]
Laurence Lundbladecc7da412020-12-27 00:09:07 -08005857 }
Laurence Lundbladef0499502020-08-01 11:55:57 -07005858 */
5859static const uint8_t spMapOfEmpty[] = {
Laurence Lundbladecc7da412020-12-27 00:09:07 -08005860 0xa6, 0x00, 0x80, 0x09, 0x82, 0x80, 0x80, 0x08,
5861 0xa3, 0x01, 0x80, 0x02, 0xa0, 0x03, 0x80, 0x04,
5862 0xa0, 0x05, 0x9f, 0xff, 0x06, 0x9f, 0x80, 0x9f,
5863 0xff, 0xff};
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005864
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08005865#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
5866
5867
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005868/*
5869 Too many tags
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005870 Duplicate label
5871 Integer overflow
5872 Date overflow
5873
Laurence Lundbladecc7da412020-12-27 00:09:07 -08005874 {
5875 1: 224(225(226(227(4(0))))),
Laurence Lundbladecc7da412020-12-27 00:09:07 -08005876 3: -18446744073709551616,
5877 4: 1(1.0e+300),
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07005878 5: 0,
5879 8: 8
Laurence Lundbladecc7da412020-12-27 00:09:07 -08005880 }
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005881 */
5882static const uint8_t spRecoverableMapErrors[] = {
Laurence Lundblade37286c02022-09-03 10:05:02 -07005883#ifndef QCBOR_DISABLE_TAGS
Laurence Lundblade2d493002024-02-01 11:09:17 -07005884 0xa5,
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005885 0x04, 0xc1, 0xfb, 0x7e, 0x37, 0xe4, 0x3c, 0x88, 0x00, 0x75, 0x9c,
Laurence Lundblade37286c02022-09-03 10:05:02 -07005886 0x01, 0xd8, 0xe0, 0xd8, 0xe1, 0xd8, 0xe2, 0xd8, 0xe3, 0xd8, 0x04, 0x00,
5887#else
5888 0xa4,
5889#endif
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005890 0x05, 0x00,
5891 0x05, 0x00,
5892 0x08, 0x08,
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005893};
5894
Laurence Lundbladecc7da412020-12-27 00:09:07 -08005895/* Bad break */
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005896static const uint8_t spUnRecoverableMapError1[] = {
5897 0xa2, 0xff, 0x01, 0x00, 0x02, 0x00
5898};
5899
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08005900#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundbladecc7da412020-12-27 00:09:07 -08005901/* No more items */
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005902static const uint8_t spUnRecoverableMapError2[] = {
5903 0xbf, 0x02, 0xbf, 0xff, 0x01, 0x00, 0x02, 0x00
5904};
5905
Laurence Lundbladecc7da412020-12-27 00:09:07 -08005906/* Hit end because string is too long */
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005907static const uint8_t spUnRecoverableMapError3[] = {
5908 0xbf, 0x02, 0x69, 0x64, 0x64, 0xff
5909};
5910
Laurence Lundbladecc7da412020-12-27 00:09:07 -08005911/* Hit end because string is too long */
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005912static const uint8_t spUnRecoverableMapError4[] = {
5913 0xbf,
5914 0x02, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f,
5915 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f,
5916 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
5917 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
5918 0xff
5919};
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08005920#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005921
Laurence Lundblade63926052021-03-29 16:05:51 -07005922const unsigned char not_well_formed_submod_section[] = {
5923 0xa1, 0x14, 0x1f,
5924};
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005925
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07005926
5927/* Array of length 3, but only two items. */
5928const unsigned char spBadConsumeInput[] = {
5929 0x83, 0x00, 0x00
5930};
5931
5932/* Tag nesting too deep. */
5933const unsigned char spBadConsumeInput2[] = {
5934 0x81,
5935 0xD8, 0x37,
5936 0xD8, 0x2C,
5937 0xD8, 0x21,
5938 0xD6,
5939 0xCB,
5940 00
5941};
5942
5943const unsigned char spBadConsumeInput3[] = {
5944 0x81, 0xc0, 0x81, 0x00
5945};
5946
5947const unsigned char spBadConsumeInput4[] = {
5948 0x81, 0x9f, 0x00, 0xff
5949};
5950
5951const unsigned char spBadConsumeInput5[] = {
5952 0xa1, 0x80, 0x00
5953};
5954
Laurence Lundbladec5f45e42023-12-18 09:23:20 -07005955/*
5956 Lots of nesting for various nesting tests.
5957 { 1:1,
5958 2:{
5959 21:21,
5960 22:{
5961 221:[2111, 2112, 2113],
5962 222:222,
5963 223: {}
5964 },
5965 23: 23
5966 },
5967 3:3,
5968 4: [ {} ]
5969 }
5970 */
5971static const uint8_t spNested[] = {
59720xA4, /* Map of 4 */
5973 0x01, 0x01, /* Map entry 1 : 1 */
5974 0x02, 0xA3, /* Map entry 2 : {, an array of 3 */
5975 0x15, 0x15, /* Map entry 21 : 21 */
5976 0x16, 0xA3, /* Map entry 22 : {, a map of 3 */
5977 0x18, 0xDD, 0x83, /* Map entry 221 : [ an array of 3 */
5978 0x19, 0x08, 0x3F, /* Array item 2111 */
5979 0x19, 0x08, 0x40, /* Array item 2112 */
5980 0x19, 0x08, 0x41, /* Array item 2113 */
5981 0x18, 0xDE, 0x18, 0xDE, /* Map entry 222 : 222 */
5982 0x18, 0xDF, 0xA0, /* Map entry 223 : {} */
5983 0x17, 0x17, /* Map entry 23 : 23 */
5984 0x03, 0x03, /* Map entry 3 : 3 */
5985 0x04, 0x81, /* Map entry 4: [, an array of 1 */
5986 0xA0 /* Array entry {}, an empty map */
5987};
5988
5989
5990static int32_t EnterMapCursorTest(void)
5991{
5992 QCBORDecodeContext DCtx;
5993 QCBORItem Item1;
5994
5995 int i;
5996 for(i = 0; i < 13; i++) {
5997 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spNested), 0);
5998 QCBORDecode_EnterMap(&DCtx, NULL);
5999 int j;
6000 /* Move travesal cursor */
6001 for(j = 0; j < i; j++) {
6002 QCBORDecode_GetNext(&DCtx, &Item1);
6003 }
6004 QCBORDecode_EnterMapFromMapN(&DCtx, 2);
6005 QCBORDecode_ExitMap(&DCtx);
6006 QCBORDecode_GetNext(&DCtx, &Item1);
6007 if(Item1.label.int64 != 3) {
6008 return 8000;
6009 }
6010 }
6011
6012 for(i = 0; i < 13; i++) {
6013 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spNested), 0);
6014 QCBORDecode_EnterMap(&DCtx, NULL);
6015 int j;
6016 /* Move travesal cursor */
6017 for(j = 0; j < i; j++) {
6018 QCBORDecode_GetNext(&DCtx, &Item1);
6019 }
6020 QCBORDecode_EnterMapFromMapN(&DCtx, 2);
6021 QCBORDecode_EnterMapFromMapN(&DCtx, 22);
6022 QCBORDecode_ExitMap(&DCtx);
6023 QCBORDecode_GetNext(&DCtx, &Item1);
6024 if(Item1.label.int64 != 23) {
6025 return 8000;
6026 }
6027 }
6028
6029 for(i = 0; i < 13; i++) {
6030 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spNested), 0);
6031 QCBORDecode_EnterMap(&DCtx, NULL);
6032 int j;
6033 /* Move travesal cursor */
6034 for(j = 0; j < i; j++) {
6035 QCBORDecode_GetNext(&DCtx, &Item1);
6036 }
6037 QCBORDecode_EnterMapFromMapN(&DCtx, 2);
6038 QCBORDecode_EnterMapFromMapN(&DCtx, 22);
6039 for(j = 0; j < i; j++) {
6040 QCBORDecode_GetNext(&DCtx, &Item1);
6041 }
6042 QCBORDecode_EnterArrayFromMapN(&DCtx, 221);
6043 QCBORDecode_ExitArray(&DCtx);
6044 QCBORDecode_ExitMap(&DCtx);
6045 QCBORDecode_GetNext(&DCtx, &Item1);
6046 if(Item1.label.int64 != 23) {
6047 return 8000;
6048 }
6049 QCBORDecode_ExitMap(&DCtx);
6050 QCBORDecode_GetNext(&DCtx, &Item1);
6051 if(Item1.label.int64 != 3) {
6052 return 8000;
6053 }
6054 }
6055
6056 return 0;
6057}
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07006058
6059
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03006060int32_t EnterMapTest(void)
Laurence Lundbladebb87be22020-04-09 19:15:32 -07006061{
Laurence Lundbladef0499502020-08-01 11:55:57 -07006062 QCBORItem Item1;
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006063 QCBORItem ArrayItem;
Laurence Lundbladebb87be22020-04-09 19:15:32 -07006064 QCBORDecodeContext DCtx;
Laurence Lundbladef0499502020-08-01 11:55:57 -07006065 int32_t nReturn;
6066 QCBORError uErr;
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07006067
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08006068#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07006069 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spMapOfEmpty), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006070 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07006071
Laurence Lundbladef0499502020-08-01 11:55:57 -07006072
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006073 QCBORDecode_EnterArray(&DCtx, NULL); // Label 0
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07006074 QCBORDecode_ExitArray(&DCtx);
6075
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006076 QCBORDecode_EnterArray(&DCtx, NULL); // Label 9
6077 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07006078 QCBORDecode_ExitArray(&DCtx);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006079 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07006080 QCBORDecode_ExitArray(&DCtx);
6081 QCBORDecode_ExitArray(&DCtx);
6082
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006083 QCBORDecode_EnterMap(&DCtx, NULL); // Label 8
6084 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07006085 QCBORDecode_ExitArray(&DCtx);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006086 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07006087 QCBORDecode_ExitMap(&DCtx);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006088 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07006089 QCBORDecode_ExitArray(&DCtx);
6090 QCBORDecode_ExitMap(&DCtx);
6091
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006092 QCBORDecode_EnterMap(&DCtx, NULL); // Label4
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07006093 QCBORDecode_ExitMap(&DCtx);
6094
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006095 QCBORDecode_EnterArray(&DCtx, NULL); // Label 5
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07006096 QCBORDecode_ExitArray(&DCtx);
6097
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006098 QCBORDecode_EnterArray(&DCtx, NULL); // Label 6
6099 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07006100 QCBORDecode_ExitArray(&DCtx);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006101 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07006102 QCBORDecode_ExitArray(&DCtx);
6103 QCBORDecode_ExitArray(&DCtx);
6104
6105 QCBORDecode_ExitMap(&DCtx);
6106
6107 uErr = QCBORDecode_Finish(&DCtx);
6108 if(uErr != QCBOR_SUCCESS){
6109 return 3011;
6110 }
6111
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07006112 (void)pValidMapIndefEncoded;
Laurence Lundbladef7a70bc2020-10-24 12:23:25 -07006113 nReturn = SpiffyDecodeBasicMap(UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapIndefEncoded));
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07006114 if(nReturn) {
6115 return nReturn + 20000;
Laurence Lundbladebb87be22020-04-09 19:15:32 -07006116 }
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08006117#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
6118
Laurence Lundbladebb87be22020-04-09 19:15:32 -07006119
Laurence Lundbladef7a70bc2020-10-24 12:23:25 -07006120 nReturn = SpiffyDecodeBasicMap(UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded));
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006121 if(nReturn) {
6122 return nReturn;
6123 }
Laurence Lundbladebb87be22020-04-09 19:15:32 -07006124
Laurence Lundblade8ffdb742020-05-07 02:49:18 -07006125
Laurence Lundblade937ea812020-05-08 11:38:23 -07006126
Laurence Lundblade2f467f92020-10-09 17:50:11 -07006127 // These tests confirm the cursor is at the right place after entering
6128 // a map or array
Laurence Lundblade9b334962020-08-27 10:55:53 -07006129 const UsefulBufC ValidEncodedMap = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded);
Laurence Lundblade937ea812020-05-08 11:38:23 -07006130
6131 // Confirm cursor is at right place
Laurence Lundblade9b334962020-08-27 10:55:53 -07006132 QCBORDecode_Init(&DCtx, ValidEncodedMap, 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006133 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade937ea812020-05-08 11:38:23 -07006134 QCBORDecode_GetNext(&DCtx, &Item1);
6135 if(Item1.uDataType != QCBOR_TYPE_INT64) {
6136 return 2001;
6137 }
6138
6139
Laurence Lundblade9b334962020-08-27 10:55:53 -07006140 QCBORDecode_Init(&DCtx, ValidEncodedMap, 0);
Laurence Lundblade6f3f78e2020-08-31 13:09:14 -07006141 QCBORDecode_VGetNext(&DCtx, &Item1);
6142 QCBORDecode_VGetNext(&DCtx, &Item1);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006143 QCBORDecode_EnterArray(&DCtx, &ArrayItem);
6144 if(ArrayItem.uLabelType != QCBOR_TYPE_TEXT_STRING ||
6145 UsefulBuf_Compare(ArrayItem.label.string,
6146 UsefulBuf_FROM_SZ_LITERAL("an array of two strings"))) {
6147 return 2051;
6148 }
Laurence Lundblade937ea812020-05-08 11:38:23 -07006149 QCBORDecode_GetNext(&DCtx, &Item1);
6150 if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING) {
6151 return 2002;
6152 }
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006153 QCBORDecode_ExitArray(&DCtx);
6154 QCBORDecode_EnterMap(&DCtx, &ArrayItem);
6155 if(ArrayItem.uLabelType != QCBOR_TYPE_TEXT_STRING ||
6156 UsefulBuf_Compare(ArrayItem.label.string,
6157 UsefulBuf_FROM_SZ_LITERAL("map in a map"))) {
6158 return 2052;
6159 }
6160
Laurence Lundblade937ea812020-05-08 11:38:23 -07006161
Laurence Lundblade9b334962020-08-27 10:55:53 -07006162 QCBORDecode_Init(&DCtx, ValidEncodedMap, 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006163 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade64b607e2020-05-13 13:05:57 -07006164 QCBORDecode_GetNext(&DCtx, &Item1);
6165 QCBORDecode_GetNext(&DCtx, &Item1);
6166 QCBORDecode_GetNext(&DCtx, &Item1);
Laurence Lundblade937ea812020-05-08 11:38:23 -07006167 QCBORDecode_EnterMapFromMapSZ(&DCtx, "map in a map");
6168 QCBORDecode_GetNext(&DCtx, &Item1);
6169 if(Item1.uDataType != QCBOR_TYPE_BYTE_STRING) {
6170 return 2003;
6171 }
6172
Laurence Lundblade9b334962020-08-27 10:55:53 -07006173 QCBORDecode_Init(&DCtx, ValidEncodedMap, 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006174 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade937ea812020-05-08 11:38:23 -07006175 QCBORDecode_GetNext(&DCtx, &Item1);
6176 QCBORDecode_GetNext(&DCtx, &Item1);
6177 QCBORDecode_GetNext(&DCtx, &Item1);
6178 QCBORDecode_GetNext(&DCtx, &Item1);
6179 QCBORDecode_GetNext(&DCtx, &Item1);
6180 QCBORDecode_GetNext(&DCtx, &Item1);
6181 QCBORDecode_GetNext(&DCtx, &Item1);
6182 QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings");
6183 QCBORDecode_GetNext(&DCtx, &Item1);
6184 if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING) {
Laurence Lundblade64b607e2020-05-13 13:05:57 -07006185 return 2004;
Laurence Lundblade937ea812020-05-08 11:38:23 -07006186 }
6187
Laurence Lundblade9b334962020-08-27 10:55:53 -07006188 QCBORDecode_Init(&DCtx, ValidEncodedMap, 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006189 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade2b843b52020-06-16 20:51:03 -07006190 QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings");
6191 QCBORDecode_ExitArray(&DCtx);
6192 QCBORDecode_GetNext(&DCtx, &Item1);
6193 if(Item1.uDataType != QCBOR_TYPE_MAP && Item1.uLabelAlloc != QCBOR_TYPE_TEXT_STRING) {
6194 return 2006;
6195 }
6196 QCBORDecode_ExitMap(&DCtx);
6197 if(QCBORDecode_GetNext(&DCtx, &Item1) != QCBOR_ERR_NO_MORE_ITEMS) {
6198 return 2007;
6199 }
6200
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07006201 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSimpleArray), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006202 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07006203 int64_t nDecodedInt2;
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07006204 QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2);
6205 uErr = QCBORDecode_GetAndResetError(&DCtx);
6206 if(uErr != QCBOR_ERR_MAP_NOT_ENTERED){
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07006207 return 2008;
6208 }
6209 UsefulBufC String;
Laurence Lundblade323f8a92020-09-06 19:43:09 -07006210 QCBORDecode_GetTextStringInMapN(&DCtx, 88, &String);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07006211 if(uErr != QCBOR_ERR_MAP_NOT_ENTERED){
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07006212 return 2009;
6213 }
Laurence Lundblade937ea812020-05-08 11:38:23 -07006214
Laurence Lundbladee6f15112020-07-23 18:44:16 -07006215
6216 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEmptyMap), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006217 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundbladee6f15112020-07-23 18:44:16 -07006218 // This will fail because the map is empty.
6219 QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2);
6220 uErr = QCBORDecode_GetAndResetError(&DCtx);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006221 if(uErr != QCBOR_ERR_LABEL_NOT_FOUND){
Laurence Lundbladee6f15112020-07-23 18:44:16 -07006222 return 2010;
6223 }
6224 QCBORDecode_ExitMap(&DCtx);
6225 uErr = QCBORDecode_Finish(&DCtx);
6226 if(uErr != QCBOR_SUCCESS){
6227 return 2011;
6228 }
6229
6230
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08006231#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundbladee6f15112020-07-23 18:44:16 -07006232 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEmptyInDefinteLengthMap), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006233 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundbladee6f15112020-07-23 18:44:16 -07006234 // This will fail because the map is empty.
6235 QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2);
6236 uErr = QCBORDecode_GetAndResetError(&DCtx);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006237 if(uErr != QCBOR_ERR_LABEL_NOT_FOUND){
Laurence Lundblade085d7952020-07-24 10:26:30 -07006238 return 2012;
Laurence Lundbladee6f15112020-07-23 18:44:16 -07006239 }
6240 QCBORDecode_ExitMap(&DCtx);
6241 uErr = QCBORDecode_Finish(&DCtx);
6242 if(uErr != QCBOR_SUCCESS){
Laurence Lundblade085d7952020-07-24 10:26:30 -07006243 return 2013;
Laurence Lundbladee6f15112020-07-23 18:44:16 -07006244 }
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08006245#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
Laurence Lundbladee6f15112020-07-23 18:44:16 -07006246
Laurence Lundblade5f4e8712020-07-25 11:44:43 -07006247
6248 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spArrayOfEmpty), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006249 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade323f8a92020-09-06 19:43:09 -07006250 QCBORDecode_GetByteString(&DCtx, &String);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006251 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade5f4e8712020-07-25 11:44:43 -07006252 QCBORDecode_ExitMap(&DCtx);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006253 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade5f4e8712020-07-25 11:44:43 -07006254 QCBORDecode_ExitArray(&DCtx);
6255 QCBORDecode_GetInt64(&DCtx, &nDecodedInt2);
6256 QCBORDecode_ExitArray(&DCtx);
6257 uErr = QCBORDecode_Finish(&DCtx);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07006258 if(uErr != QCBOR_SUCCESS) {
Laurence Lundblade5f4e8712020-07-25 11:44:43 -07006259 return 2014;
6260 }
6261
Laurence Lundblade93d3f532020-09-28 21:09:12 -07006262 int64_t nInt;
6263 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spRecoverableMapErrors), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006264 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade37286c02022-09-03 10:05:02 -07006265#ifndef QCBOR_DISABLE_TAGS
Laurence Lundblade93d3f532020-09-28 21:09:12 -07006266 QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt);
Laurence Lundblade88e9db22020-11-02 03:56:33 -08006267 uErr = QCBORDecode_GetError(&DCtx);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07006268 if(uErr != QCBOR_ERR_TOO_MANY_TAGS) {
6269 return 2021;
6270 }
Laurence Lundblade88e9db22020-11-02 03:56:33 -08006271 if(QCBORDecode_GetNthTagOfLast(&DCtx, 0) != CBOR_TAG_INVALID64) {
6272 return 2121;
6273 }
6274 (void)QCBORDecode_GetAndResetError(&DCtx);
Laurence Lundblade37286c02022-09-03 10:05:02 -07006275#endif
Laurence Lundblade88e9db22020-11-02 03:56:33 -08006276
Laurence Lundblade93d3f532020-09-28 21:09:12 -07006277
Laurence Lundblade37286c02022-09-03 10:05:02 -07006278#ifndef QCBOR_DISABLE_TAGS
Laurence Lundblade93d3f532020-09-28 21:09:12 -07006279 QCBORDecode_GetEpochDateInMapN(&DCtx, 0x04, QCBOR_TAG_REQUIREMENT_TAG, &nInt);
6280 uErr = QCBORDecode_GetAndResetError(&DCtx);
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006281 if(uErr != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_DATE_OVERFLOW)) {
Laurence Lundblade93d3f532020-09-28 21:09:12 -07006282 return 2024;
6283 }
Laurence Lundblade37286c02022-09-03 10:05:02 -07006284#endif
Laurence Lundblade93d3f532020-09-28 21:09:12 -07006285
6286 QCBORDecode_GetInt64InMapN(&DCtx, 0x05, &nInt);
6287 uErr = QCBORDecode_GetAndResetError(&DCtx);
6288 if(uErr != QCBOR_ERR_DUPLICATE_LABEL) {
6289 return 2025;
6290 }
6291
6292 QCBORDecode_GetInt64InMapN(&DCtx, 0x08, &nInt);
6293
6294 QCBORDecode_ExitMap(&DCtx);
6295 uErr = QCBORDecode_Finish(&DCtx);
6296 if(uErr != QCBOR_SUCCESS) {
6297 return 2026;
6298 }
6299
6300 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUnRecoverableMapError1), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006301 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07006302 QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt);
6303 uErr = QCBORDecode_GetAndResetError(&DCtx);
6304 if(uErr != QCBOR_ERR_BAD_BREAK) {
6305 return 2030;
6306 }
6307
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08006308#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundblade93d3f532020-09-28 21:09:12 -07006309 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUnRecoverableMapError2), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006310 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07006311 QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt);
6312 uErr = QCBORDecode_GetAndResetError(&DCtx);
6313 if(uErr != QCBOR_ERR_NO_MORE_ITEMS) {
6314 return 2031;
6315 }
6316
6317 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUnRecoverableMapError3), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006318 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07006319 QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt);
6320 uErr = QCBORDecode_GetAndResetError(&DCtx);
6321 if(uErr != QCBOR_ERR_HIT_END) {
6322 return 2032;
6323 }
6324
6325 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUnRecoverableMapError4), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006326 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07006327 QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt);
6328 uErr = QCBORDecode_GetAndResetError(&DCtx);
6329 if(uErr != QCBOR_ERR_ARRAY_DECODE_NESTING_TOO_DEEP) {
6330 return 2033;
6331 }
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08006332#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
6333
Laurence Lundblade732e52d2021-02-22 20:11:01 -07006334 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0);
6335 QCBORDecode_VGetNextConsume(&DCtx, &Item1);
6336 if(Item1.uDataType != QCBOR_TYPE_MAP) {
6337 return 2401;
6338 }
6339 if(QCBORDecode_GetError(&DCtx)) {
6340 return 2402;
6341 }
6342
6343 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0);
6344 QCBORDecode_VGetNext(&DCtx, &Item1);
6345 if(Item1.uDataType != QCBOR_TYPE_MAP ||
6346 Item1.val.uCount != 3 ||
6347 Item1.uNextNestLevel != 1) {
6348 return 2403;
6349 }
6350 if(QCBORDecode_GetError(&DCtx)) {
6351 return 2404;
6352 }
6353 QCBORDecode_VGetNextConsume(&DCtx, &Item1);
6354 if(Item1.uDataType != QCBOR_TYPE_INT64 ||
6355 Item1.uNextNestLevel != 1 ||
6356 Item1.val.int64 != 42) {
6357 return 2405;
6358 }
6359 if(QCBORDecode_GetError(&DCtx)) {
6360 return 2406;
6361 }
6362 QCBORDecode_VGetNextConsume(&DCtx, &Item1);
6363 if(Item1.uDataType != QCBOR_TYPE_ARRAY ||
6364 Item1.uNestingLevel != 1 ||
6365 Item1.uNextNestLevel != 1 ||
6366 Item1.val.uCount != 2) {
6367 return 2407;
6368 }
6369 if(QCBORDecode_GetError(&DCtx)) {
6370 return 2408;
6371 }
6372 QCBORDecode_VGetNextConsume(&DCtx, &Item1);
6373 if(Item1.uDataType != QCBOR_TYPE_MAP ||
6374 Item1.uNestingLevel != 1 ||
6375 Item1.uNextNestLevel != 0 ||
6376 Item1.val.uCount != 4) {
6377 return 2409;
6378 }
6379 if(QCBORDecode_GetError(&DCtx)) {
6380 return 2410;
6381 }
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006382
Laurence Lundbladeddebabe2020-11-22 11:32:17 -08006383 nReturn = DecodeNestedIterate();
6384
Laurence Lundblade63926052021-03-29 16:05:51 -07006385
6386 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(not_well_formed_submod_section), 0);
6387 QCBORDecode_EnterMap(&DCtx, NULL);
6388 QCBORDecode_EnterMapFromMapN(&DCtx, 20);
6389 if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_BAD_INT) {
6390 return 2500;
6391 }
6392
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07006393 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBadConsumeInput), 0);
6394 QCBORDecode_VGetNextConsume(&DCtx, &Item1);
6395 if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_NO_MORE_ITEMS) {
6396 return 2600;
6397 }
6398
Laurence Lundblade37286c02022-09-03 10:05:02 -07006399#ifndef QCBOR_DISABLE_TAGS
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07006400 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBadConsumeInput2), 0);
6401 QCBORDecode_VGetNextConsume(&DCtx, &Item1);
6402 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) {
6403 return 2700;
6404 }
6405
6406 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBadConsumeInput3), 0);
6407 QCBORDecode_VGetNextConsume(&DCtx, &Item1);
6408 if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT) {
6409 return 2800;
6410 }
Laurence Lundblade37286c02022-09-03 10:05:02 -07006411#endif
6412
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07006413
6414 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBadConsumeInput4), 0);
6415 QCBORDecode_VGetNextConsume(&DCtx, &Item1);
6416#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
6417 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) {
6418 return 2900;
6419 }
6420#else
6421 if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_INDEF_LEN_ARRAYS_DISABLED) {
6422 return 2901;
6423 }
6424#endif
6425
6426 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBadConsumeInput5), 0);
6427 QCBORDecode_VGetNextConsume(&DCtx, &Item1);
6428 if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_MAP_LABEL_TYPE) {
6429 return 3000;
6430 }
6431
Laurence Lundbladec5f45e42023-12-18 09:23:20 -07006432 nReturn = EnterMapCursorTest();
6433
Laurence Lundbladeddebabe2020-11-22 11:32:17 -08006434 return nReturn;
Laurence Lundblade9c905e82020-04-25 11:31:38 -07006435}
6436
6437
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006438struct NumberConversion {
6439 char *szDescription;
6440 UsefulBufC CBOR;
6441 int64_t nConvertedToInt64;
6442 QCBORError uErrorInt64;
6443 uint64_t uConvertToUInt64;
6444 QCBORError uErrorUint64;
6445 double dConvertToDouble;
6446 QCBORError uErrorDouble;
6447};
6448
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006449#ifndef QCBOR_DISABLE_EXP_AND_MANTISSA
6450#define EXP_AND_MANTISSA_ERROR(x) x
6451#else
6452#define EXP_AND_MANTISSA_ERROR(x) QCBOR_ERR_UNEXPECTED_TYPE
6453#endif
6454
6455
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07006456static const struct NumberConversion NumberConversions[] = {
Laurence Lundblade37286c02022-09-03 10:05:02 -07006457#ifndef QCBOR_DISABLE_TAGS
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006458 {
Laurence Lundblade4e808ba2022-12-29 12:45:20 -07006459 "Big float: INT64_MIN * 2e-1 to test handling of INT64_MIN",
6460 {(uint8_t[]){0xC5, 0x82, 0x20,
6461 0x3B, 0x7f, 0xff, 0xff, 0xff, 0xff, 0x0ff, 0xff, 0xff,
6462 }, 15},
6463 -4611686018427387904, /* INT64_MIN / 2 */
6464 EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS),
6465 0,
6466 EXP_AND_MANTISSA_ERROR(QCBOR_ERR_NUMBER_SIGN_CONVERSION),
6467 -4.6116860184273879E+18,
6468 FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS))
6469 },
6470 {
Laurence Lundblade784b54b2020-08-10 01:24:52 -07006471 "too large to fit into int64_t",
6472 {(uint8_t[]){0xc3, 0x48, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 10},
6473 0,
6474 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
6475 0,
6476 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
6477 ((double)INT64_MIN) + 1 ,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006478 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)
Laurence Lundblade784b54b2020-08-10 01:24:52 -07006479 },
6480 {
6481 "largest negative int that fits in int64_t",
6482 {(uint8_t[]){0xc3, 0x48, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, 10},
6483 INT64_MIN,
6484 QCBOR_SUCCESS,
6485 0,
6486 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
6487 (double)INT64_MIN,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006488 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)
Laurence Lundblade784b54b2020-08-10 01:24:52 -07006489 },
6490 {
Laurence Lundbladeda095972020-06-06 18:35:33 -07006491 "negative bignum -1",
6492 {(uint8_t[]){0xc3, 0x41, 0x00}, 3},
6493 -1,
6494 QCBOR_SUCCESS,
6495 0,
6496 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
6497 -1.0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006498 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)
Laurence Lundbladeda095972020-06-06 18:35:33 -07006499 },
6500 {
Laurence Lundblade9ab5abb2020-05-20 12:10:45 -07006501 "Decimal Fraction with positive bignum 257 * 10e3",
Laurence Lundblade887add82020-05-17 05:50:34 -07006502 {(uint8_t[]){0xC4, 0x82, 0x1B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03,
6503 0xC2, 0x42, 0x01, 0x01}, 15},
6504 257000,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006505 EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS),
Laurence Lundblade887add82020-05-17 05:50:34 -07006506 257000,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006507 EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS),
Laurence Lundblade887add82020-05-17 05:50:34 -07006508 257000.0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006509 FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS))
Laurence Lundblade887add82020-05-17 05:50:34 -07006510 },
6511 {
Laurence Lundbladeda095972020-06-06 18:35:33 -07006512 "bigfloat with negative bignum -258 * 2e3",
Laurence Lundblade887add82020-05-17 05:50:34 -07006513 {(uint8_t[]){0xC5, 0x82, 0x1B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03,
6514 0xC3, 0x42, 0x01, 0x01}, 15},
Laurence Lundbladeda095972020-06-06 18:35:33 -07006515 -2064,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006516 EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS),
Laurence Lundblade887add82020-05-17 05:50:34 -07006517 0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006518 EXP_AND_MANTISSA_ERROR(QCBOR_ERR_NUMBER_SIGN_CONVERSION),
Laurence Lundbladeda095972020-06-06 18:35:33 -07006519 -2064.0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006520 FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS))
Laurence Lundblade887add82020-05-17 05:50:34 -07006521 },
6522 {
Laurence Lundblade9ab5abb2020-05-20 12:10:45 -07006523 "bigfloat with positive bignum 257 * 2e3",
Laurence Lundblade887add82020-05-17 05:50:34 -07006524 {(uint8_t[]){0xC5, 0x82, 0x1B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03,
6525 0xC2, 0x42, 0x01, 0x01}, 15},
6526 2056,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006527 EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS),
Laurence Lundblade887add82020-05-17 05:50:34 -07006528 2056,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006529 EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS),
Laurence Lundblade887add82020-05-17 05:50:34 -07006530 2056.0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006531 FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS))
Laurence Lundblade887add82020-05-17 05:50:34 -07006532 },
6533 {
Laurence Lundbladeda095972020-06-06 18:35:33 -07006534 "negative bignum 0xc349010000000000000000 -18446744073709551617",
Laurence Lundblade887add82020-05-17 05:50:34 -07006535 {(uint8_t[]){0xc3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 11},
6536 0,
6537 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
6538 0,
6539 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
6540 -18446744073709551617.0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006541 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)
Laurence Lundblade887add82020-05-17 05:50:34 -07006542 },
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08006543#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
Laurence Lundblade887add82020-05-17 05:50:34 -07006544 {
Laurence Lundblade313b2862020-05-16 01:23:06 -07006545 "Positive bignum 0x01020304 indefinite length string",
6546 {(uint8_t[]){0xC2, 0x5f, 0x42, 0x01, 0x02, 0x41, 0x03, 0x41, 0x04, 0xff}, 10},
6547 0x01020304,
6548 QCBOR_SUCCESS,
6549 0x01020304,
6550 QCBOR_SUCCESS,
6551 16909060.0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006552 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)
Laurence Lundblade313b2862020-05-16 01:23:06 -07006553 },
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08006554#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
Laurence Lundblade313b2862020-05-16 01:23:06 -07006555 {
Laurence Lundblade887add82020-05-17 05:50:34 -07006556 "Decimal Fraction with neg bignum [9223372036854775807, -4759477275222530853137]",
Laurence Lundblade313b2862020-05-16 01:23:06 -07006557 {(uint8_t[]){0xC4, 0x82, 0x1B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
6558 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,}, 23},
6559 0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006560 EXP_AND_MANTISSA_ERROR(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW),
Laurence Lundblade313b2862020-05-16 01:23:06 -07006561 0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006562 EXP_AND_MANTISSA_ERROR(QCBOR_ERR_NUMBER_SIGN_CONVERSION),
Laurence Lundblade313b2862020-05-16 01:23:06 -07006563 -INFINITY,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006564 FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS))
Laurence Lundblade313b2862020-05-16 01:23:06 -07006565 },
6566 {
6567 "big float [9223372036854775806, 9223372036854775806]",
6568 {(uint8_t[]){0xC5, 0x82, 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE,
6569 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE}, 20},
6570 0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006571 EXP_AND_MANTISSA_ERROR(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW),
Laurence Lundblade313b2862020-05-16 01:23:06 -07006572 0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006573 EXP_AND_MANTISSA_ERROR(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW),
Laurence Lundblade313b2862020-05-16 01:23:06 -07006574 INFINITY,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006575 FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS))
Laurence Lundblade313b2862020-05-16 01:23:06 -07006576 },
6577 {
Laurence Lundblade983500d2020-05-14 11:49:34 -07006578 "Big float 3 * 2^^2",
6579 {(uint8_t[]){0xC5, 0x82, 0x02, 0x03}, 4},
6580 12,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006581 EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS),
Laurence Lundblade983500d2020-05-14 11:49:34 -07006582 12,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006583 EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS),
Laurence Lundblade983500d2020-05-14 11:49:34 -07006584 12.0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006585 FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS))
Laurence Lundblade983500d2020-05-14 11:49:34 -07006586 },
Laurence Lundblade983500d2020-05-14 11:49:34 -07006587 {
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006588 "Decimal fraction 3/10",
6589 {(uint8_t[]){0xC4, 0x82, 0x20, 0x03}, 4},
6590 0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006591 EXP_AND_MANTISSA_ERROR(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW),
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006592 0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006593 EXP_AND_MANTISSA_ERROR(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW),
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006594 0.30000000000000004,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006595 FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS))
Laurence Lundbladedfd49fc2020-09-01 14:17:16 -07006596 },
6597 {
Laurence Lundblade11fd78b2020-09-01 22:13:27 -07006598 "extreme pos bignum",
6599 {(uint8_t[]){0xc2, 0x59, 0x01, 0x90,
6600 // 50 rows of 8 is 400 digits.
6601 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6602 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6603 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6604 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6605 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6606 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6607 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6608 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6609 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6610 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6611 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6612 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6613 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6614 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6615 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6616 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6617 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6618 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6619 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6620 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6621 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6622 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6623 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6624 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6625 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6626 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6627 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6628 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6629 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6630 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6631 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6632 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6633 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6634 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6635 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6636 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6637 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6638 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6639 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6640 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6641 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6642 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6643 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6644 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6645 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6646 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6647 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6648 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6649 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
gtravisb787e82a2023-11-30 18:38:21 -08006650 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0},
Laurence Lundblade11fd78b2020-09-01 22:13:27 -07006651 404},
6652 0,
6653 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
6654 0,
6655 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
Laurence Lundblade11fd78b2020-09-01 22:13:27 -07006656 INFINITY,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006657 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS),
Laurence Lundblade11fd78b2020-09-01 22:13:27 -07006658 },
6659
6660 {
6661 "extreme neg bignum",
6662 {(uint8_t[]){0xc3, 0x59, 0x01, 0x90,
6663 // 50 rows of 8 is 400 digits.
6664 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6665 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6666 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6667 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6668 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6669 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6670 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6671 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6672 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6673 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6674 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6675 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6676 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6677 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6678 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6679 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6680 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6681 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6682 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6683 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6684 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6685 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6686 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6687 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6688 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6689 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6690 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6691 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6692 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6693 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6694 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6695 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6696 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6697 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6698 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6699 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6700 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6701 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6702 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6703 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6704 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6705 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6706 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6707 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6708 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6709 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6710 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6711 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6712 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
gtravisb787e82a2023-11-30 18:38:21 -08006713 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0},
Laurence Lundblade11fd78b2020-09-01 22:13:27 -07006714 404},
6715 0,
6716 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
6717 0,
6718 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
Laurence Lundblade11fd78b2020-09-01 22:13:27 -07006719 -INFINITY,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006720 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)
Laurence Lundblade11fd78b2020-09-01 22:13:27 -07006721 },
Laurence Lundblade51722fd2020-09-02 13:01:33 -07006722
6723 {
6724 "big float underflow [9223372036854775806, -9223372036854775806]",
6725 {(uint8_t[]){
6726 0xC5, 0x82,
6727 0x3B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE,
6728 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE}, 20},
Laurence Lundblade51722fd2020-09-02 13:01:33 -07006729 0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006730 EXP_AND_MANTISSA_ERROR(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW),
Laurence Lundblade51722fd2020-09-02 13:01:33 -07006731 0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006732 EXP_AND_MANTISSA_ERROR(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW),
Laurence Lundblade51722fd2020-09-02 13:01:33 -07006733 0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006734 FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS))
Laurence Lundblade51722fd2020-09-02 13:01:33 -07006735 },
6736
6737 {
6738 "bigfloat that evaluates to -INFINITY",
6739 {(uint8_t[]){
6740 0xC5, 0x82,
6741 0x1B, 0x70, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03,
6742 0xC3, 0x42, 0x01, 0x01}, 15},
Laurence Lundblade51722fd2020-09-02 13:01:33 -07006743 0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006744 EXP_AND_MANTISSA_ERROR(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW),
Laurence Lundblade51722fd2020-09-02 13:01:33 -07006745 0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006746 EXP_AND_MANTISSA_ERROR(QCBOR_ERR_NUMBER_SIGN_CONVERSION),
Laurence Lundblade51722fd2020-09-02 13:01:33 -07006747 -INFINITY,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006748 FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS))
Laurence Lundblade51722fd2020-09-02 13:01:33 -07006749 },
Laurence Lundblade37286c02022-09-03 10:05:02 -07006750 {
6751 "Positive bignum 0xffff",
6752 {(uint8_t[]){0xC2, 0x42, 0xff, 0xff}, 4},
6753 65536-1,
6754 QCBOR_SUCCESS,
6755 0xffff,
6756 QCBOR_SUCCESS,
6757 65535.0,
6758 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)
6759 },
6760#endif /* QCBOR_DISABLE_TAGS */
6761 {
6762 "Positive integer 18446744073709551615",
6763 {(uint8_t[]){0x1b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, 9},
6764 0,
6765 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
6766 18446744073709551615ULL,
6767 QCBOR_SUCCESS,
6768 18446744073709551615.0,
6769 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)
6770 },
6771
6772 {
6773 "Postive integer 0",
6774 {(uint8_t[]){0x0}, 1},
6775 0LL,
6776 QCBOR_SUCCESS,
6777 0ULL,
6778 QCBOR_SUCCESS,
6779 0.0,
6780 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)
6781 },
6782 {
Laurence Lundblade2d493002024-02-01 11:09:17 -07006783 "Negative integer -9223372036854775808",
6784 {(uint8_t[]){0x3b, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, 9},
Laurence Lundblade37286c02022-09-03 10:05:02 -07006785 -9223372036854775807-1, // INT64_MIN
6786 QCBOR_SUCCESS,
6787 0ULL,
6788 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
6789 -9223372036854775808.0,
6790 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)
6791 },
6792 {
Laurence Lundblade2d493002024-02-01 11:09:17 -07006793 "Negative integer -18446744073709551616",
6794 {(uint8_t[]){0x3b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, 9},
6795 0ULL,
6796 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
6797 0ULL,
6798 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
6799 -18446744073709551616.0,
6800 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)
6801 },
6802 {
Laurence Lundblade37286c02022-09-03 10:05:02 -07006803 "Double Floating point value 100.3",
6804 {(uint8_t[]){0xfb, 0x40, 0x59, 0x13, 0x33, 0x33, 0x33, 0x33, 0x33}, 9},
6805 100L,
6806 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS),
6807 100ULL,
6808 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS),
6809 100.3,
6810 FLOAT_ERR_CODE_NO_FLOAT(QCBOR_SUCCESS),
6811 },
6812 {
6813 "Floating point value NaN 0xfa7fc00000",
6814 {(uint8_t[]){0xfa, 0x7f, 0xc0, 0x00, 0x00}, 5},
6815 0,
6816 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_FLOAT_EXCEPTION),
6817 0,
6818 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_FLOAT_EXCEPTION),
6819 NAN,
6820 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS),
6821 },
6822 {
6823 "half-precision Floating point value -4",
6824 {(uint8_t[]){0xf9, 0xc4, 0x00}, 3},
6825 // Normal case with all enabled.
6826 -4,
6827 FLOAT_ERR_CODE_NO_HALF_PREC_NO_FLOAT_HW(QCBOR_SUCCESS),
6828 0,
6829 FLOAT_ERR_CODE_NO_HALF_PREC_NO_FLOAT_HW(QCBOR_ERR_NUMBER_SIGN_CONVERSION),
6830 -4.0,
6831 FLOAT_ERR_CODE_NO_HALF_PREC(QCBOR_SUCCESS)
6832 },
6833 {
6834 "+inifinity single precision",
6835 {(uint8_t[]){0xfa, 0x7f, 0x80, 0x00, 0x00}, 5},
6836 0,
6837 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_FLOAT_EXCEPTION),
6838 0,
6839 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW),
6840 INFINITY,
6841 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)
6842 },
6843
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006844};
Laurence Lundblade9c905e82020-04-25 11:31:38 -07006845
6846
6847
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08006848
6849static int32_t SetUpDecoder(QCBORDecodeContext *DCtx, UsefulBufC CBOR, UsefulBuf Pool)
6850{
6851 QCBORDecode_Init(DCtx, CBOR, QCBOR_DECODE_MODE_NORMAL);
6852#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
6853 if(QCBORDecode_SetMemPool(DCtx, Pool, 0)) {
6854 return 1;
6855 }
6856#else /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
6857 (void)Pool;
6858#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
6859 return 0;
6860}
6861
6862
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03006863int32_t IntegerConvertTest(void)
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006864{
Laurence Lundbladeeb3cdef2024-02-17 20:38:55 -08006865 uint64_t uInt;
6866
6867
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08006868 const int nNumTests = C_ARRAY_COUNT(NumberConversions,
6869 struct NumberConversion);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006870
Laurence Lundblade9ab5abb2020-05-20 12:10:45 -07006871 for(int nIndex = 0; nIndex < nNumTests; nIndex++) {
6872 const struct NumberConversion *pF = &NumberConversions[nIndex];
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07006873
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006874 // Set up the decoding context including a memory pool so that
6875 // indefinite length items can be checked
6876 QCBORDecodeContext DCtx;
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006877 UsefulBuf_MAKE_STACK_UB(Pool, 100);
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07006878
6879 /* ----- test conversion to int64_t ------ */
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08006880 if(SetUpDecoder(&DCtx, pF->CBOR, Pool)) {
6881 return (int32_t)(3333+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006882 }
6883
6884 int64_t nInt;
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006885 QCBORDecode_GetInt64ConvertAll(&DCtx, 0xffff, &nInt);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07006886 if(QCBORDecode_GetError(&DCtx) != pF->uErrorInt64) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07006887 return (int32_t)(2000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006888 }
6889 if(pF->uErrorInt64 == QCBOR_SUCCESS && pF->nConvertedToInt64 != nInt) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07006890 return (int32_t)(3000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006891 }
6892
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07006893 /* ----- test conversion to uint64_t ------ */
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08006894 if(SetUpDecoder(&DCtx, pF->CBOR, Pool)) {
6895 return (int32_t)(3333+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006896 }
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08006897
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006898 QCBORDecode_GetUInt64ConvertAll(&DCtx, 0xffff, &uInt);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07006899 if(QCBORDecode_GetError(&DCtx) != pF->uErrorUint64) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07006900 return (int32_t)(4000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006901 }
6902 if(pF->uErrorUint64 == QCBOR_SUCCESS && pF->uConvertToUInt64 != uInt) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07006903 return (int32_t)(5000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006904 }
6905
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07006906 /* ----- test conversion to double ------ */
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08006907 if(SetUpDecoder(&DCtx, pF->CBOR, Pool)) {
6908 return (int32_t)(3333+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006909 }
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006910
6911#ifndef USEFULBUF_DISABLE_ALL_FLOAT
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006912 double d;
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006913 QCBORDecode_GetDoubleConvertAll(&DCtx, 0xffff, &d);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07006914 if(QCBORDecode_GetError(&DCtx) != pF->uErrorDouble) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07006915 return (int32_t)(6000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006916 }
6917 if(pF->uErrorDouble == QCBOR_SUCCESS) {
6918 if(isnan(pF->dConvertToDouble)) {
Laurence Lundblade983500d2020-05-14 11:49:34 -07006919 // NaN's can't be compared for equality. A NaN is
6920 // never equal to anything including another NaN
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006921 if(!isnan(d)) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07006922 return (int32_t)(7000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006923 }
6924 } else {
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006925 if(pF->dConvertToDouble != d) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07006926 return (int32_t)(8000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006927 }
6928 }
6929 }
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006930#endif /* USEFULBUF_DISABLE_ALL_FLOAT */
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006931 }
6932
6933 return 0;
6934}
6935
Laurence Lundbladea8758502022-05-15 17:57:46 -07006936#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
6937
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03006938int32_t CBORTestIssue134(void)
David Navarro9123e5b2022-03-28 16:04:03 +02006939{
6940 QCBORDecodeContext DCtx;
6941 QCBORItem Item;
6942 QCBORError uCBORError;
6943 const uint8_t spTestIssue134[] = { 0x5F, 0x40, 0xFF };
Laurence Lundblade9c905e82020-04-25 11:31:38 -07006944
David Navarro9123e5b2022-03-28 16:04:03 +02006945 QCBORDecode_Init(&DCtx,
6946 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTestIssue134),
6947 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade97c61bf2020-05-02 11:24:06 -07006948
David Navarro9123e5b2022-03-28 16:04:03 +02006949 UsefulBuf_MAKE_STACK_UB(StringBuf, 200);
6950 QCBORDecode_SetMemPool(&DCtx, StringBuf, false);
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03006951
David Navarro9123e5b2022-03-28 16:04:03 +02006952 do {
6953 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
6954 } while (QCBOR_SUCCESS == uCBORError);
6955
6956 uCBORError = QCBORDecode_Finish(&DCtx);
6957
Laurence Lundblade11ea3612022-07-01 13:26:23 -07006958 return (int32_t)uCBORError;
David Navarro9123e5b2022-03-28 16:04:03 +02006959}
Laurence Lundblade97c61bf2020-05-02 11:24:06 -07006960
Laurence Lundbladea8758502022-05-15 17:57:46 -07006961#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
6962
Laurence Lundblade37286c02022-09-03 10:05:02 -07006963
6964
6965static const uint8_t spSequenceTestInput[] = {
6966 /* 1. The valid date string "1985-04-12" */
6967 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string
6968
6969 /* 2. */
6970 0x00,
6971
6972 /* 3. A valid epoch date, 1400000000; Tue, 13 May 2014 16:53:20 GMT */
6973 0x1a, 0x53, 0x72, 0x4E, 0x00,
6974
6975 /* 4. */
6976 0x62, 'h', 'i',
6977};
6978
6979
Laurence Lundbladee3553422020-05-02 11:11:17 -07006980int32_t CBORSequenceDecodeTests(void)
6981{
6982 QCBORDecodeContext DCtx;
Laurence Lundblade87495732021-02-26 10:05:55 -07006983 QCBORItem Item;
6984 QCBORError uCBORError;
6985 size_t uConsumed;
Laurence Lundbladee3553422020-05-02 11:11:17 -07006986
6987 // --- Test a sequence with extra bytes ---
Laurence Lundblade9b334962020-08-27 10:55:53 -07006988
Laurence Lundbladee3553422020-05-02 11:11:17 -07006989 QCBORDecode_Init(&DCtx,
Laurence Lundblade37286c02022-09-03 10:05:02 -07006990 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSequenceTestInput),
Laurence Lundbladee3553422020-05-02 11:11:17 -07006991 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade9b334962020-08-27 10:55:53 -07006992
Laurence Lundblade37286c02022-09-03 10:05:02 -07006993 // Get 1.
Laurence Lundbladee3553422020-05-02 11:11:17 -07006994 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
6995 if(uCBORError != QCBOR_SUCCESS) {
6996 return 1;
6997 }
Laurence Lundblade37286c02022-09-03 10:05:02 -07006998 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ) {
Laurence Lundbladee3553422020-05-02 11:11:17 -07006999 return 2;
7000 }
Laurence Lundbladec7114722020-08-13 05:11:40 -07007001
Laurence Lundblade87495732021-02-26 10:05:55 -07007002 uCBORError = QCBORDecode_PartialFinish(&DCtx, &uConsumed);
7003 if(uCBORError != QCBOR_ERR_EXTRA_BYTES ||
Laurence Lundblade37286c02022-09-03 10:05:02 -07007004 uConsumed != 11) {
7005 return 102;
7006 }
7007
7008 // Get 2.
7009 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
7010 if(uCBORError != QCBOR_SUCCESS) {
7011 return 66;
7012 }
7013
7014 uCBORError = QCBORDecode_PartialFinish(&DCtx, &uConsumed);
7015 if(uCBORError != QCBOR_ERR_EXTRA_BYTES ||
Laurence Lundblade87495732021-02-26 10:05:55 -07007016 uConsumed != 12) {
7017 return 102;
7018 }
7019
Laurence Lundblade37286c02022-09-03 10:05:02 -07007020 // Get 3.
Laurence Lundbladec7114722020-08-13 05:11:40 -07007021 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundbladee3553422020-05-02 11:11:17 -07007022 if(uCBORError != QCBOR_SUCCESS) {
7023 return 2;
7024 }
Laurence Lundblade37286c02022-09-03 10:05:02 -07007025 if(Item.uDataType != QCBOR_TYPE_INT64) {
Laurence Lundbladee3553422020-05-02 11:11:17 -07007026 return 3;
7027 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07007028
Laurence Lundbladee3553422020-05-02 11:11:17 -07007029 // A sequence can have stuff at the end that may
7030 // or may not be valid CBOR. The protocol decoder knows
7031 // when to stop by definition of the protocol, not
7032 // when the top-level map or array is ended.
7033 // Finish still has to be called to know that
7034 // maps and arrays (if there were any) were closed
7035 // off correctly. When called like this it
7036 // must return the error QCBOR_ERR_EXTRA_BYTES.
7037 uCBORError = QCBORDecode_Finish(&DCtx);
7038 if(uCBORError != QCBOR_ERR_EXTRA_BYTES) {
7039 return 4;
7040 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07007041
Laurence Lundbladee3553422020-05-02 11:11:17 -07007042 // --- Test an empty input ----
7043 uint8_t empty[1];
7044 UsefulBufC Empty = {empty, 0};
7045 QCBORDecode_Init(&DCtx,
7046 Empty,
7047 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade9b334962020-08-27 10:55:53 -07007048
Laurence Lundbladee3553422020-05-02 11:11:17 -07007049 uCBORError = QCBORDecode_Finish(&DCtx);
7050 if(uCBORError != QCBOR_SUCCESS) {
7051 return 5;
7052 }
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07007053
7054
Laurence Lundbladee3553422020-05-02 11:11:17 -07007055 // --- Sequence with unclosed indefinite length array ---
7056 static const uint8_t xx[] = {0x01, 0x9f, 0x02};
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07007057
Laurence Lundbladee3553422020-05-02 11:11:17 -07007058 QCBORDecode_Init(&DCtx,
7059 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(xx),
7060 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07007061
Laurence Lundbladee3553422020-05-02 11:11:17 -07007062 // Get the first item
7063 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
7064 if(uCBORError != QCBOR_SUCCESS) {
7065 return 7;
7066 }
7067 if(Item.uDataType != QCBOR_TYPE_INT64) {
7068 return 8;
7069 }
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07007070
Laurence Lundbladee3553422020-05-02 11:11:17 -07007071 // Get a second item
7072 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08007073#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundbladee3553422020-05-02 11:11:17 -07007074 if(uCBORError != QCBOR_SUCCESS) {
7075 return 9;
7076 }
7077 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
7078 return 10;
7079 }
7080
7081 // Try to finish before consuming all bytes to confirm
7082 // that the still-open error is returned.
7083 uCBORError = QCBORDecode_Finish(&DCtx);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07007084 if(uCBORError != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
Laurence Lundbladee3553422020-05-02 11:11:17 -07007085 return 11;
7086 }
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08007087#else /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
7088 if(uCBORError != QCBOR_ERR_INDEF_LEN_ARRAYS_DISABLED) {
7089 return 20;
7090 }
7091#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
Laurence Lundbladee3553422020-05-02 11:11:17 -07007092
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07007093
Laurence Lundbladee3553422020-05-02 11:11:17 -07007094 // --- Sequence with a closed indefinite length array ---
7095 static const uint8_t yy[] = {0x01, 0x9f, 0xff};
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07007096
Laurence Lundbladee3553422020-05-02 11:11:17 -07007097 QCBORDecode_Init(&DCtx,
7098 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(yy),
7099 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07007100
Laurence Lundbladee3553422020-05-02 11:11:17 -07007101 // Get the first item
7102 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
7103 if(uCBORError != QCBOR_SUCCESS) {
7104 return 12;
7105 }
7106 if(Item.uDataType != QCBOR_TYPE_INT64) {
7107 return 13;
7108 }
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07007109
Laurence Lundbladee3553422020-05-02 11:11:17 -07007110 // Get a second item
7111 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08007112#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
7113
Laurence Lundbladee3553422020-05-02 11:11:17 -07007114 if(uCBORError != QCBOR_SUCCESS) {
7115 return 14;
7116 }
7117 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
7118 return 15;
7119 }
7120
7121 // Try to finish before consuming all bytes to confirm
7122 // that the still-open error is returned.
7123 uCBORError = QCBORDecode_Finish(&DCtx);
7124 if(uCBORError != QCBOR_SUCCESS) {
7125 return 16;
7126 }
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08007127#else /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
7128 if(uCBORError != QCBOR_ERR_INDEF_LEN_ARRAYS_DISABLED) {
7129 return 20;
7130 }
7131#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
Laurence Lundbladee3553422020-05-02 11:11:17 -07007132
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07007133
Laurence Lundbladee3553422020-05-02 11:11:17 -07007134 return 0;
7135}
7136
Laurence Lundbladee15326f2020-06-15 15:50:23 -07007137
Laurence Lundblade70ecead2020-06-15 19:40:06 -07007138
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03007139int32_t IntToTests(void)
Laurence Lundbladee15326f2020-06-15 15:50:23 -07007140{
7141 int nErrCode;
7142 int32_t n32;
7143 int16_t n16;
7144 int8_t n8;
7145 uint32_t u32;
7146 uint16_t u16;
7147 uint8_t u8;
7148 uint64_t u64;
7149
7150 nErrCode = QCBOR_Int64ToInt32(1, &n32);
7151 if(nErrCode == -1 || n32 != 1) {
7152 return 1;
7153 }
7154
7155 nErrCode = QCBOR_Int64ToInt32((int64_t)INT32_MAX, &n32);
7156 if(nErrCode == -1 || n32 != INT32_MAX) {
7157 return 2;
7158 }
7159
7160 nErrCode = QCBOR_Int64ToInt32((int64_t)INT32_MIN, &n32);
7161 if(nErrCode == -1 || n32 != INT32_MIN) {
7162 return 3;
7163 }
7164
7165 nErrCode = QCBOR_Int64ToInt32(((int64_t)INT32_MAX)+1, &n32);
7166 if(nErrCode != -1) {
7167 return 4;
7168 }
7169
7170 nErrCode = QCBOR_Int64ToInt32(((int64_t)INT32_MIN)-1, &n32);
7171 if(nErrCode != -1) {
7172 return 5;
7173 }
7174
7175
7176 nErrCode = QCBOR_Int64ToInt16((int64_t)INT16_MAX, &n16);
7177 if(nErrCode == -1 || n16 != INT16_MAX) {
7178 return 6;
7179 }
7180
7181 nErrCode = QCBOR_Int64ToInt16((int64_t)INT16_MIN, &n16);
7182 if(nErrCode == -1 || n16 != INT16_MIN) {
7183 return 7;
7184 }
7185
7186 nErrCode = QCBOR_Int64ToInt16(1, &n16);
7187 if(nErrCode == -1 || n16 != 1) {
7188 return 8;
7189 }
7190
7191 nErrCode = QCBOR_Int64ToInt16(((int64_t)INT16_MAX)+1, &n16);
7192 if(nErrCode != -1) {
7193 return 9;
7194 }
7195
7196 nErrCode = QCBOR_Int64ToInt16(((int64_t)INT16_MIN)-1, &n16);
7197 if(nErrCode != -1) {
7198 return 10;
7199 }
7200
7201
7202 nErrCode = QCBOR_Int64ToInt8(1, &n8);
7203 if(nErrCode == -1 || n8 != 1) {
7204 return 11;
7205 }
7206
7207 nErrCode = QCBOR_Int64ToInt8((int64_t)INT8_MAX, &n8);
7208 if(nErrCode == -1 || n8 != INT8_MAX) {
7209 return 12;
7210 }
7211
7212 nErrCode = QCBOR_Int64ToInt8((int64_t)INT8_MIN, &n8);
7213 if(nErrCode == -1 || n8 != INT8_MIN) {
7214 return 13;
7215 }
7216
7217 nErrCode = QCBOR_Int64ToInt8(((int64_t)INT8_MAX)+1, &n8);
7218 if(nErrCode != -1) {
7219 return 14;
7220 }
7221
7222 nErrCode = QCBOR_Int64ToInt8(((int64_t)INT8_MIN)-1, &n8);
7223 if(nErrCode != -1) {
7224 return 15;
7225 }
7226
7227
7228 nErrCode = QCBOR_Int64ToUInt32(1, &u32);
7229 if(nErrCode == -1 || u32 != 1) {
7230 return 16;
7231 }
7232
7233 nErrCode = QCBOR_Int64ToUInt32((int64_t)UINT32_MAX, &u32);
7234 if(nErrCode == -1 || u32 != UINT32_MAX) {
7235 return 17;
7236 }
7237
7238 nErrCode = QCBOR_Int64ToUInt32((int64_t)0, &u32);
7239 if(nErrCode == -1 || u32 != 0) {
7240 return 18;
7241 }
7242
7243 nErrCode = QCBOR_Int64ToUInt32(((int64_t)UINT32_MAX)+1, &u32);
7244 if(nErrCode != -1) {
7245 return 19;
7246 }
7247
7248 nErrCode = QCBOR_Int64ToUInt32((int64_t)-1, &u32);
7249 if(nErrCode != -1) {
7250 return 20;
7251 }
7252
7253
7254 nErrCode = QCBOR_Int64UToInt16((int64_t)UINT16_MAX, &u16);
7255 if(nErrCode == -1 || u16 != UINT16_MAX) {
7256 return 21;
7257 }
7258
7259 nErrCode = QCBOR_Int64UToInt16((int64_t)0, &u16);
7260 if(nErrCode == -1 || u16 != 0) {
7261 return 22;
7262 }
7263
7264 nErrCode = QCBOR_Int64UToInt16(1, &u16);
7265 if(nErrCode == -1 || u16 != 1) {
7266 return 23;
7267 }
7268
7269 nErrCode = QCBOR_Int64UToInt16(((int64_t)UINT16_MAX)+1, &u16);
7270 if(nErrCode != -1) {
7271 return 24;
7272 }
7273
7274 nErrCode = QCBOR_Int64UToInt16((int64_t)-1, &u16);
7275 if(nErrCode != -1) {
7276 return 25;
7277 }
7278
7279
7280 nErrCode = QCBOR_Int64ToUInt8((int64_t)UINT8_MAX, &u8);
7281 if(nErrCode == -1 || u8 != UINT8_MAX) {
7282 return 26;
7283 }
7284
7285 nErrCode = QCBOR_Int64ToUInt8((int64_t)0, &u8);
7286 if(nErrCode == -1 || u8 != 0) {
7287 return 27;
7288 }
7289
7290 nErrCode = QCBOR_Int64ToUInt8(1, &u8);
7291 if(nErrCode == -1 || u8 != 1) {
7292 return 28;
7293 }
7294
7295 nErrCode = QCBOR_Int64ToUInt8(((int64_t)UINT16_MAX)+1, &u8);
7296 if(nErrCode != -1) {
7297 return 29;
7298 }
7299
7300 nErrCode = QCBOR_Int64ToUInt8((int64_t)-1, &u8);
7301 if(nErrCode != -1) {
7302 return 30;
7303 }
7304
7305
7306 nErrCode = QCBOR_Int64ToUInt64(1, &u64);
7307 if(nErrCode == -1 || u64 != 1) {
7308 return 31;
7309 }
7310
7311 nErrCode = QCBOR_Int64ToUInt64(INT64_MAX, &u64);
7312 if(nErrCode == -1 || u64 != INT64_MAX) {
7313 return 32;
7314 }
7315
7316 nErrCode = QCBOR_Int64ToUInt64((int64_t)0, &u64);
7317 if(nErrCode == -1 || u64 != 0) {
7318 return 33;
7319 }
7320
7321 nErrCode = QCBOR_Int64ToUInt64((int64_t)-1, &u64);
7322 if(nErrCode != -1) {
7323 return 34;
7324 }
7325
7326 return 0;
7327}
7328
Laurence Lundblade0750fc42020-06-20 21:02:34 -07007329
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07007330
7331
Laurence Lundblade0750fc42020-06-20 21:02:34 -07007332/*
7333A sequence with
7334 A wrapping bstr
7335 containing a map
7336 1
7337 2
7338 A wrapping bstr
7339 containing an array
7340 3
7341 wrapping bstr
7342 4
7343 5
7344 6
7345 array
7346 7
7347 8
Laurence Lundblade0750fc42020-06-20 21:02:34 -07007348 */
7349
Laurence Lundblade55013642020-09-23 05:39:22 -07007350static UsefulBufC EncodeBstrWrapTestData(UsefulBuf OutputBuffer)
Laurence Lundblade0750fc42020-06-20 21:02:34 -07007351{
Laurence Lundblade55013642020-09-23 05:39:22 -07007352 UsefulBufC Encoded;
Laurence Lundblade0750fc42020-06-20 21:02:34 -07007353 QCBOREncodeContext EC;
Laurence Lundblade55013642020-09-23 05:39:22 -07007354 QCBORError uErr;
Laurence Lundblade0750fc42020-06-20 21:02:34 -07007355
Laurence Lundblade55013642020-09-23 05:39:22 -07007356 QCBOREncode_Init(&EC, OutputBuffer);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07007357
7358 QCBOREncode_BstrWrap(&EC);
7359 QCBOREncode_OpenMap(&EC);
7360 QCBOREncode_AddInt64ToMapN(&EC, 100, 1);
7361 QCBOREncode_AddInt64ToMapN(&EC, 200, 2);
7362 QCBOREncode_CloseMap(&EC);
7363 QCBOREncode_BstrWrap(&EC);
7364 QCBOREncode_OpenArray(&EC);
7365 QCBOREncode_AddInt64(&EC, 3);
7366 QCBOREncode_BstrWrap(&EC);
7367 QCBOREncode_AddInt64(&EC, 4);
7368 QCBOREncode_CloseBstrWrap(&EC, NULL);
7369 QCBOREncode_AddInt64(&EC, 5);
7370 QCBOREncode_CloseArray(&EC);
7371 QCBOREncode_CloseBstrWrap(&EC, NULL);
7372 QCBOREncode_AddInt64(&EC, 6);
7373 QCBOREncode_CloseBstrWrap(&EC, NULL);
7374 QCBOREncode_OpenArray(&EC);
7375 QCBOREncode_AddInt64(&EC, 7);
7376 QCBOREncode_AddInt64(&EC, 8);
7377 QCBOREncode_CloseArray(&EC);
7378
7379 uErr = QCBOREncode_Finish(&EC, &Encoded);
Laurence Lundblade40a04322020-06-27 22:52:52 -07007380 if(uErr) {
7381 Encoded = NULLUsefulBufC;
7382 }
Laurence Lundblade0750fc42020-06-20 21:02:34 -07007383
7384 return Encoded;
7385}
7386
Laurence Lundbladecc7da412020-12-27 00:09:07 -08007387/* h'FF' */
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08007388static const uint8_t spBreakInByteString[] = {
7389 0x41, 0xff
7390};
7391
Laurence Lundblade0750fc42020-06-20 21:02:34 -07007392
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03007393int32_t EnterBstrTest(void)
Laurence Lundblade0750fc42020-06-20 21:02:34 -07007394{
Laurence Lundblade8510f8c2020-12-01 11:31:16 -08007395 UsefulBuf_MAKE_STACK_UB(OutputBuffer, 100);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07007396
7397 QCBORDecodeContext DC;
7398
Laurence Lundblade55013642020-09-23 05:39:22 -07007399 QCBORDecode_Init(&DC, EncodeBstrWrapTestData(OutputBuffer), 0);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07007400
Laurence Lundblade55013642020-09-23 05:39:22 -07007401 int64_t n1, n2, n3, n4, n5, n6, n7, n8;
Laurence Lundblade0750fc42020-06-20 21:02:34 -07007402
7403
Laurence Lundblade9b334962020-08-27 10:55:53 -07007404 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07007405 QCBORDecode_EnterMap(&DC, NULL);
Laurence Lundblade55013642020-09-23 05:39:22 -07007406 QCBORDecode_GetInt64InMapN(&DC, 100, &n1);
7407 QCBORDecode_GetInt64InMapN(&DC, 200, &n2);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07007408 QCBORDecode_ExitMap(&DC);
Laurence Lundblade9b334962020-08-27 10:55:53 -07007409 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07007410 QCBORDecode_EnterArray(&DC, NULL);
Laurence Lundblade55013642020-09-23 05:39:22 -07007411 QCBORDecode_GetInt64(&DC, &n3);
Laurence Lundblade9b334962020-08-27 10:55:53 -07007412 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
Laurence Lundblade55013642020-09-23 05:39:22 -07007413 QCBORDecode_GetInt64(&DC, &n4);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07007414 QCBORDecode_ExitBstrWrapped(&DC);
Laurence Lundblade55013642020-09-23 05:39:22 -07007415 QCBORDecode_GetInt64(&DC, &n5);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07007416 QCBORDecode_ExitArray(&DC);
7417 QCBORDecode_ExitBstrWrapped(&DC);
Laurence Lundblade55013642020-09-23 05:39:22 -07007418 QCBORDecode_GetInt64(&DC, &n6);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07007419 QCBORDecode_ExitBstrWrapped(&DC);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07007420 QCBORDecode_EnterArray(&DC, NULL);
Laurence Lundblade55013642020-09-23 05:39:22 -07007421 QCBORDecode_GetInt64(&DC, &n7);
7422 QCBORDecode_GetInt64(&DC, &n8);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07007423 QCBORDecode_ExitArray(&DC);
7424
7425 QCBORError uErr = QCBORDecode_Finish(&DC);
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08007426 if(uErr) {
7427 return (int32_t)uErr;
7428 }
Laurence Lundblade0750fc42020-06-20 21:02:34 -07007429
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08007430
7431 /* Enter and exit byte string wrapped CBOR that is bad. It has just a break.
7432 * Successful because no items are fetched from byte string.
7433 */
7434 QCBORDecode_Init(&DC,
7435 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBreakInByteString),
7436 0);
7437 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
7438 uErr = QCBORDecode_GetError(&DC);
7439 if(uErr) {
7440 return 100 + (int32_t)uErr;
7441 }
7442
7443 QCBORDecode_ExitBstrWrapped(&DC);
7444 uErr = QCBORDecode_GetError(&DC);
7445 if(uErr) {
7446 return 200 + (int32_t)uErr;
7447 }
7448
7449 /* Try to get item that is a break out of a byte string wrapped CBOR.
7450 * It fails because there should be no break.
7451 */
7452 QCBORDecode_Init(&DC,
7453 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBreakInByteString),
7454 0);
7455 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
7456 QCBORItem Item;
7457 uErr = QCBORDecode_GetNext(&DC, &Item);
7458 if(uErr != QCBOR_ERR_BAD_BREAK) {
7459 return 300 + (int32_t)uErr;
7460 }
7461
7462 return 0;
Laurence Lundblade0750fc42020-06-20 21:02:34 -07007463}
Laurence Lundblade37f46e52020-08-04 03:32:14 -07007464
7465
7466
7467
7468static const uint8_t spTaggedTypes[] = {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007469 0xb2,
Laurence Lundblade37f46e52020-08-04 03:32:14 -07007470
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07007471 // Date string
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007472 0x00,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07007473 0xc0, 0x74, 0x32, 0x30, 0x30, 0x33, 0x2D, 0x31, 0x32, 0x2D,
7474 0x31, 0x33, 0x54, 0x31, 0x38, 0x3A, 0x33, 0x30, 0x3A, 0x30,
7475 0x32, 0x5A,
Laurence Lundblade37f46e52020-08-04 03:32:14 -07007476
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007477 0x01,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07007478 0x74, 0x32, 0x30, 0x30, 0x33, 0x2D, 0x31, 0x32, 0x2D, 0x31,
7479 0x33, 0x54, 0x31, 0x38, 0x3A, 0x33, 0x30, 0x3A, 0x30, 0x32,
7480 0x5A,
Laurence Lundblade37f46e52020-08-04 03:32:14 -07007481
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07007482 // Bignum
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007483 10,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07007484 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
7485 0x09, 0x10,
Laurence Lundblade37f46e52020-08-04 03:32:14 -07007486
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007487 11,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07007488 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
7489 0x09, 0x10,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007490
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07007491 // URL
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007492 20,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07007493 0xd8, 0x20, 0x6f, 0x68, 0x74, 0x74, 0x70, 0x3A, 0x2F, 0x2F,
7494 0x63, 0x62, 0x6F, 0x72, 0x2E, 0x6D, 0x65, 0x2F,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007495
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07007496 21,
7497 0x6f, 0x68, 0x74, 0x74, 0x70, 0x3A, 0x2F, 0x2F, 0x63, 0x62,
7498 0x6F, 0x72, 0x2E, 0x6D, 0x65, 0x2F,
7499
7500 // B64
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007501 0x18, 0x1e,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07007502 0xd8, 0x22, 0x6c, 0x63, 0x47, 0x78, 0x6C, 0x59, 0x58, 0x4E,
7503 0x31, 0x63, 0x6D, 0x55, 0x75,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007504
7505 0x18, 0x1f,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07007506 0x6c, 0x63, 0x47, 0x78, 0x6C, 0x59, 0x58, 0x4E, 0x31, 0x63,
7507 0x6D, 0x55, 0x75,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007508
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07007509 // B64URL
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007510 0x18, 0x28,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07007511 0xd8, 0x21, 0x6c, 0x63, 0x47, 0x78, 0x6C, 0x59, 0x58, 0x4E,
7512 0x31, 0x63, 0x6D, 0x55, 0x75,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007513
7514 0x18, 0x29,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07007515 0x6c, 0x63, 0x47, 0x78, 0x6C, 0x59, 0x58, 0x4E, 0x31, 0x63,
7516 0x6D, 0x55, 0x75,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007517
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07007518 // Regex
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007519 0x18, 0x32,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07007520 0xd8, 0x23, 0x68, 0x31, 0x30, 0x30, 0x5C, 0x73, 0x2A, 0x6D,
7521 0x6B,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007522
7523 0x18, 0x33,
7524 0x68, 0x31, 0x30, 0x30, 0x5C, 0x73, 0x2A, 0x6D, 0x6B,
7525
7526 // MIME
7527 0x18, 0x3c,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07007528 0xd8, 0x24, 0x72, 0x4D, 0x49, 0x4D, 0x45, 0x2D, 0x56, 0x65,
7529 0x72, 0x73, 0x69, 0x6F, 0x6E, 0x3A, 0x20, 0x31, 0x2E, 0x30,
7530 0x0A,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007531
7532 0x18, 0x3d,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07007533 0x72, 0x4D, 0x49, 0x4D, 0x45, 0x2D, 0x56, 0x65, 0x72, 0x73,
7534 0x69, 0x6F, 0x6E, 0x3A, 0x20, 0x31, 0x2E, 0x30, 0x0A,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007535
7536 0x18, 0x3e,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07007537 0xd9, 0x01, 0x01, 0x52, 0x4D, 0x49, 0x4D, 0x45, 0x2D, 0x56,
7538 0x65, 0x72, 0x73, 0x69, 0x6F, 0x6E, 0x3A, 0x20, 0x31, 0x2E,
7539 0x30, 0x0A,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007540
7541 0x18, 0x3f,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07007542 0x52, 0x4D, 0x49, 0x4D, 0x45, 0x2D, 0x56, 0x65, 0x72, 0x73,
7543 0x69, 0x6F, 0x6E, 0x3A, 0x20, 0x31, 0x2E, 0x30, 0x0A,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007544
7545 // UUID
7546 0x18, 0x46,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07007547 0xd8, 0x25, 0x50, 0x53, 0x4D, 0x41, 0x52, 0x54, 0x43, 0x53,
7548 0x4C, 0x54, 0x54, 0x43, 0x46, 0x49, 0x43, 0x41, 0x32,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007549
7550 0x18, 0x47,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07007551 0x50, 0x53, 0x4D, 0x41, 0x52, 0x54, 0x43, 0x53, 0x4C, 0x54,
7552 0x54, 0x43, 0x46, 0x49, 0x43, 0x41, 0x32
Laurence Lundblade37f46e52020-08-04 03:32:14 -07007553};
7554
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03007555int32_t DecodeTaggedTypeTests(void)
Laurence Lundblade37f46e52020-08-04 03:32:14 -07007556{
7557 QCBORDecodeContext DC;
7558 QCBORError uErr;
7559
7560 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTaggedTypes), 0);
7561
7562 UsefulBufC String;
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007563 bool bNeg;
Laurence Lundblade37f46e52020-08-04 03:32:14 -07007564
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07007565 QCBORDecode_EnterMap(&DC, NULL);
Laurence Lundblade9b334962020-08-27 10:55:53 -07007566 QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007567 QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &String);
Laurence Lundblade37f46e52020-08-04 03:32:14 -07007568 if(QCBORDecode_GetError(&DC) != QCBOR_SUCCESS) {
7569 return 1;
7570 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07007571 QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade37f46e52020-08-04 03:32:14 -07007572 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_UNEXPECTED_TYPE) {
7573 return 2;
7574 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07007575 QCBORDecode_GetDateStringInMapN(&DC, 1, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade37f46e52020-08-04 03:32:14 -07007576 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_UNEXPECTED_TYPE) {
7577 return 3;
7578 }
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007579 QCBORDecode_GetDateStringInMapN(&DC, 1, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &String);
Laurence Lundblade9b334962020-08-27 10:55:53 -07007580 QCBORDecode_GetDateStringInMapN(&DC, 1, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade37f46e52020-08-04 03:32:14 -07007581 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
7582 return 4;
7583 }
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007584 QCBORDecode_GetDateStringInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07007585 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007586 return 5;
7587 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07007588
Laurence Lundblade9b334962020-08-27 10:55:53 -07007589 QCBORDecode_GetBignumInMapN(&DC, 10, QCBOR_TAG_REQUIREMENT_TAG, &String, &bNeg);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007590 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
7591 bNeg != false) {
7592 return 10;
7593 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07007594 QCBORDecode_GetBignumInMapN(&DC, 11, QCBOR_TAG_REQUIREMENT_TAG, &String, &bNeg);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007595 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
7596 bNeg != true) {
7597 return 11;
7598 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07007599 QCBORDecode_GetBignumInMapN(&DC, 11, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bNeg);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007600 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_UNEXPECTED_TYPE) {
7601 return 12;
7602 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07007603 QCBORDecode_GetBignumInMapN(&DC, 14, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bNeg);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07007604 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007605 return 13;
7606 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07007607 QCBORDecode_GetBignumInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bNeg);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07007608 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007609 return 14;
7610 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07007611
Laurence Lundblade9b334962020-08-27 10:55:53 -07007612 QCBORDecode_GetURIInMapN(&DC, 20, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007613 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
7614 return 20;
7615 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07007616 QCBORDecode_GetURIInMapN(&DC, 21, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007617 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
7618 return 21;
7619 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07007620 QCBORDecode_GetURIInMapN(&DC, 22, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07007621 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007622 return 22;
7623 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07007624 QCBORDecode_GetURIInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07007625 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007626 return 23;
7627 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07007628
Laurence Lundblade24bd7e12021-01-09 00:05:11 -08007629#ifndef QCBOR_DISABLE_UNCOMMON_TAGS
Laurence Lundblade9b334962020-08-27 10:55:53 -07007630 QCBORDecode_GetB64InMapN(&DC, 30, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007631 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
7632 return 30;
7633 }
Laurence Lundblade24bd7e12021-01-09 00:05:11 -08007634#endif
Laurence Lundblade9b334962020-08-27 10:55:53 -07007635 QCBORDecode_GetB64InMapN(&DC, 31, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007636 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
7637 return 31;
7638 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07007639 QCBORDecode_GetB64InMapN(&DC, 32, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07007640 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007641 return 32;
7642 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07007643 QCBORDecode_GetB64InMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07007644 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007645 return 33;
7646 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07007647
Laurence Lundblade24bd7e12021-01-09 00:05:11 -08007648#ifndef QCBOR_DISABLE_UNCOMMON_TAGS
Laurence Lundblade9b334962020-08-27 10:55:53 -07007649 QCBORDecode_GetB64URLInMapN(&DC, 40, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007650 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
7651 return 40;
7652 }
Laurence Lundblade24bd7e12021-01-09 00:05:11 -08007653#endif
Laurence Lundblade9b334962020-08-27 10:55:53 -07007654 QCBORDecode_GetB64URLInMapN(&DC, 41, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007655 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
7656 return 41;
7657 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07007658 QCBORDecode_GetB64URLInMapN(&DC, 42, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07007659 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007660 return 42;
7661 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07007662 QCBORDecode_GetB64URLInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07007663 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007664 return 43;
7665 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07007666
Laurence Lundblade24bd7e12021-01-09 00:05:11 -08007667#ifndef QCBOR_DISABLE_UNCOMMON_TAGS
Laurence Lundblade9b334962020-08-27 10:55:53 -07007668 QCBORDecode_GetRegexInMapN(&DC, 50, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007669 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
7670 return 50;
7671 }
Laurence Lundblade24bd7e12021-01-09 00:05:11 -08007672#endif
Laurence Lundblade9b334962020-08-27 10:55:53 -07007673 QCBORDecode_GetRegexInMapN(&DC, 51, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007674 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
7675 return 51;
7676 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07007677 QCBORDecode_GetRegexInMapN(&DC, 52, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07007678 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007679 return 52;
7680 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07007681 QCBORDecode_GetRegexInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07007682 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007683 return 53;
7684 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07007685
Laurence Lundblade24bd7e12021-01-09 00:05:11 -08007686#ifndef QCBOR_DISABLE_UNCOMMON_TAGS
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007687 // MIME
7688 bool bIsNot7Bit;
Laurence Lundblade9b334962020-08-27 10:55:53 -07007689 QCBORDecode_GetMIMEMessageInMapN(&DC, 60, QCBOR_TAG_REQUIREMENT_TAG, &String, &bIsNot7Bit);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007690 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
7691 bIsNot7Bit == true) {
7692 return 60;
7693 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07007694 QCBORDecode_GetMIMEMessageInMapN(&DC, 61, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bIsNot7Bit);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007695 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
7696 bIsNot7Bit == true) {
7697 return 61;
7698 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07007699 QCBORDecode_GetMIMEMessageInMapN(&DC, 62, QCBOR_TAG_REQUIREMENT_TAG, &String, &bIsNot7Bit);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007700 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
7701 bIsNot7Bit == false) {
7702 return 62;
7703 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07007704 QCBORDecode_GetMIMEMessageInMapN(&DC, 63, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bIsNot7Bit);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007705 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
7706 bIsNot7Bit == false) {
7707 return 63;
7708 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07007709 QCBORDecode_GetMIMEMessageInMapN(&DC, 64, QCBOR_TAG_REQUIREMENT_TAG, &String, &bNeg);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07007710 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007711 return 64;
7712 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07007713 QCBORDecode_GetMIMEMessageInMapSZ(&DC, "zzz", QCBOR_TAG_REQUIREMENT_TAG, &String, &bNeg);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07007714 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007715 return 65;
7716 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07007717
Laurence Lundblade24bd7e12021-01-09 00:05:11 -08007718
Laurence Lundblade9b334962020-08-27 10:55:53 -07007719 QCBORDecode_GetBinaryUUIDInMapN(&DC, 70, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007720 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
7721 return 70;
7722 }
Laurence Lundblade24bd7e12021-01-09 00:05:11 -08007723#endif /* #ifndef QCBOR_DISABLE_UNCOMMON_TAGS */
7724
Laurence Lundblade9b334962020-08-27 10:55:53 -07007725 QCBORDecode_GetBinaryUUIDInMapN(&DC, 71, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007726 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
7727 return 71;
7728 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07007729 QCBORDecode_GetBinaryUUIDInMapN(&DC, 72, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07007730 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007731 return 72;
7732 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07007733 QCBORDecode_GetBinaryUUIDInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07007734 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007735 return 73;
7736 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07007737
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07007738 // Improvement: add some more error test cases
7739
Laurence Lundblade37f46e52020-08-04 03:32:14 -07007740 QCBORDecode_ExitMap(&DC);
7741
7742 uErr = QCBORDecode_Finish(&DC);
7743 if(uErr != QCBOR_SUCCESS) {
7744 return 100;
7745 }
7746
7747 return 0;
7748}
Laurence Lundbladea4308a82020-10-03 18:08:57 -07007749
7750
7751
7752
7753/*
Laurence Lundbladecc7da412020-12-27 00:09:07 -08007754 [
7755 "aaaaaaaaaa",
7756 {}
7757 ]
Laurence Lundbladea4308a82020-10-03 18:08:57 -07007758 */
7759static const uint8_t spTooLarge1[] = {
7760 0x9f,
7761 0x6a, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61,
7762 0xa0,
7763 0xff
7764};
7765
7766/*
Laurence Lundbladecc7da412020-12-27 00:09:07 -08007767 [
7768 {
7769 0: "aaaaaaaaaa"
7770 }
7771 ]
Laurence Lundbladea4308a82020-10-03 18:08:57 -07007772 */
7773static const uint8_t spTooLarge2[] = {
7774 0x9f,
7775 0xa1,
7776 0x00,
7777 0x6a, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61,
7778 0xff
7779};
7780
7781/*
Laurence Lundbladecc7da412020-12-27 00:09:07 -08007782 h'A1006A61616161616161616161'
Laurence Lundbladea4308a82020-10-03 18:08:57 -07007783
Laurence Lundbladecc7da412020-12-27 00:09:07 -08007784 {
7785 0: "aaaaaaaaaa"
7786 }
Laurence Lundbladea4308a82020-10-03 18:08:57 -07007787 */
7788static const uint8_t spTooLarge3[] = {
7789 0x4d,
7790 0xa1,
7791 0x00,
7792 0x6a, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61,
7793};
7794
7795int32_t TooLargeInputTest(void)
7796{
7797 QCBORDecodeContext DC;
7798 QCBORError uErr;
7799 UsefulBufC String;
7800
7801 // These tests require a build with QCBOR_MAX_DECODE_INPUT_SIZE set
7802 // to 10 There's not really any way to test this error
7803 // condition. The error condition is not complex, so setting
7804 // QCBOR_MAX_DECODE_INPUT_SIZE gives an OK test.
7805
7806 // The input CBOR is only too large because the
7807 // QCBOR_MAX_DECODE_INPUT_SIZE is 10.
7808 //
7809 // This test is disabled for the normal test runs because of the
7810 // special build requirement.
7811
7812
7813 // Tests the start of a map being too large
7814 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTooLarge1), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07007815 QCBORDecode_EnterArray(&DC, NULL);
Laurence Lundbladea4308a82020-10-03 18:08:57 -07007816 QCBORDecode_GetTextString(&DC, &String);
7817 uErr = QCBORDecode_GetError(&DC);
7818 if(uErr != QCBOR_SUCCESS) {
7819 return 1;
7820 }
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07007821 QCBORDecode_EnterMap(&DC, NULL);
Laurence Lundbladea4308a82020-10-03 18:08:57 -07007822 uErr = QCBORDecode_GetError(&DC);
7823 if(uErr != QCBOR_ERR_INPUT_TOO_LARGE) {
7824 return 2;
7825 }
7826
7827 // Tests the end of a map being too large
7828 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTooLarge2), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07007829 QCBORDecode_EnterArray(&DC, NULL);
7830 QCBORDecode_EnterMap(&DC, NULL);
Laurence Lundbladea4308a82020-10-03 18:08:57 -07007831 uErr = QCBORDecode_GetError(&DC);
7832 if(uErr != QCBOR_SUCCESS) {
7833 return 3;
7834 }
7835 QCBORDecode_ExitMap(&DC);
7836 uErr = QCBORDecode_GetError(&DC);
7837 if(uErr != QCBOR_ERR_INPUT_TOO_LARGE) {
7838 return 4;
7839 }
7840
7841 // Tests the entire input CBOR being too large when processing bstr wrapping
7842 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTooLarge3), QCBOR_DECODE_MODE_NORMAL);
7843 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
7844 uErr = QCBORDecode_GetError(&DC);
7845 if(uErr != QCBOR_ERR_INPUT_TOO_LARGE) {
7846 return 5;
7847 }
7848
7849 return 0;
7850}
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07007851
7852
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08007853#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
7854
Laurence Lundblade37286c02022-09-03 10:05:02 -07007855/*
7856 An array of three map entries
7857 1) Indefinite length string label for indefinite lenght byte string
7858 2) Indefinite length string label for an integer
7859 3) Indefinite length string label for an indefinite-length negative big num
7860 */
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07007861static const uint8_t spMapWithIndefLenStrings[] = {
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08007862 0xa3,
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07007863 0x7f, 0x61, 'l', 0x64, 'a', 'b', 'e', 'l' , 0x61, '1', 0xff,
7864 0x5f, 0x42, 0x01, 0x02, 0x43, 0x03, 0x04, 0x05, 0xff,
7865 0x7f, 0x62, 'd', 'y', 0x61, 'm', 0x61, 'o', 0xff,
7866 0x03,
7867 0x7f, 0x62, 'l', 'a', 0x63, 'b', 'e', 'l', 0x61, '2', 0xff,
7868 0xc3,
7869 0x5f, 0x42, 0x00, 0x01, 0x42, 0x00, 0x01, 0x41, 0x01, 0xff,
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07007870};
7871
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03007872int32_t SpiffyIndefiniteLengthStringsTests(void)
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07007873{
7874 QCBORDecodeContext DCtx;
7875
7876 QCBORDecode_Init(&DCtx,
7877 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spMapWithIndefLenStrings),
7878 QCBOR_DECODE_MODE_NORMAL);
7879
Laurence Lundblade8510f8c2020-12-01 11:31:16 -08007880 UsefulBuf_MAKE_STACK_UB(StringBuf, 200);
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07007881 QCBORDecode_SetMemPool(&DCtx, StringBuf, false);
7882
7883 UsefulBufC ByteString;
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07007884 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07007885 QCBORDecode_GetByteStringInMapSZ(&DCtx, "label1", &ByteString);
Laurence Lundblade37286c02022-09-03 10:05:02 -07007886
7887#ifndef QCBOR_DISABLE_TAGS
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07007888 if(QCBORDecode_GetAndResetError(&DCtx)) {
7889 return 1;
7890 }
7891
7892 const uint8_t pExectedBytes[] = {0x01, 0x02, 0x03, 0x04, 0x05};
7893 if(UsefulBuf_Compare(ByteString, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pExectedBytes))) {
7894 return 2;
7895 }
7896
7897 uint64_t uInt;
7898 QCBORDecode_GetUInt64InMapSZ(&DCtx, "dymo", &uInt);
7899 if(QCBORDecode_GetAndResetError(&DCtx)) {
7900 return 3;
7901 }
7902 if(uInt != 3) {
7903 return 4;
7904 }
7905
Máté Tóth-Pálef5f07a2021-09-17 19:31:37 +02007906#ifndef USEFULBUF_DISABLE_ALL_FLOAT
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07007907 double uDouble;
7908 QCBORDecode_GetDoubleConvertAllInMapSZ(&DCtx,
7909 "label2",
7910 0xff,
7911 &uDouble);
Laurence Lundblade37286c02022-09-03 10:05:02 -07007912
Laurence Lundbladeb8e19aa2020-10-07 20:59:11 -07007913#ifndef QCBOR_DISABLE_FLOAT_HW_USE
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07007914 if(QCBORDecode_GetAndResetError(&DCtx)) {
7915 return 5;
7916 }
7917 if(uDouble != -16777474) {
7918 return 6;
7919 }
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08007920#else /* QCBOR_DISABLE_FLOAT_HW_USE */
Laurence Lundbladeb8e19aa2020-10-07 20:59:11 -07007921 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_HW_FLOAT_DISABLED) {
7922 return 7;
7923 }
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08007924#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
Máté Tóth-Pálef5f07a2021-09-17 19:31:37 +02007925#endif /* USEFULBUF_DISABLE_ALL_FLOAT */
Laurence Lundbladeb8e19aa2020-10-07 20:59:11 -07007926
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07007927 QCBORDecode_ExitMap(&DCtx);
7928
7929 if(QCBORDecode_Finish(&DCtx)) {
7930 return 99;
7931 }
7932
Laurence Lundblade37286c02022-09-03 10:05:02 -07007933#else /* QCBOR_DISABLE_TAGS */
7934 /* The big num in the input is a CBOR tag and you can't do
7935 * map lookups in a map with a tag so this test does very little
7936 * when tags are disabled. That is OK, the test coverage is still
7937 * good when they are not.
7938 */
7939 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_TAGS_DISABLED) {
7940 return 1002;
7941 }
7942#endif /*QCBOR_DISABLE_TAGS */
7943
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07007944 return 0;
7945}
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08007946#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
Laurence Lundblade2a26abb2020-11-05 19:06:54 -08007947
7948
Laurence Lundbladecf41c522021-02-20 10:19:07 -07007949/*
7950 * An array of an integer and an array. The second array contains
7951 * a bstr-wrapped map.
7952 *
7953 * [7, [h'A36D6669... (see next lines) 73']]
7954 *
7955 * {"first integer": 42,
7956 * "an array of two strings": ["string1", "string2"],
7957 * "map in a map":
7958 * { "bytes 1": h'78787878',
7959 * "bytes 2": h'79797979',
7960 * "another int": 98,
7961 * "text 2": "lies, damn lies and statistics"
7962 * }
7963 * }
7964 */
Laurence Lundblade2a26abb2020-11-05 19:06:54 -08007965
Laurence Lundbladecf41c522021-02-20 10:19:07 -07007966static const uint8_t pValidWrappedMapEncoded[] = {
7967 0x82, 0x07, 0x81, 0x58, 0x97,
7968 0xa3, 0x6d, 0x66, 0x69, 0x72, 0x73, 0x74, 0x20, 0x69, 0x6e,
7969 0x74, 0x65, 0x67, 0x65, 0x72, 0x18, 0x2a, 0x77, 0x61, 0x6e,
7970 0x20, 0x61, 0x72, 0x72, 0x61, 0x79, 0x20, 0x6f, 0x66, 0x20,
7971 0x74, 0x77, 0x6f, 0x20, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67,
7972 0x73, 0x82, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x31,
7973 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x32, 0x6c, 0x6d,
7974 0x61, 0x70, 0x20, 0x69, 0x6e, 0x20, 0x61, 0x20, 0x6d, 0x61,
7975 0x70, 0xa4, 0x67, 0x62, 0x79, 0x74, 0x65, 0x73, 0x20, 0x31,
7976 0x44, 0x78, 0x78, 0x78, 0x78, 0x67, 0x62, 0x79, 0x74, 0x65,
7977 0x73, 0x20, 0x32, 0x44, 0x79, 0x79, 0x79, 0x79, 0x6b, 0x61,
7978 0x6e, 0x6f, 0x74, 0x68, 0x65, 0x72, 0x20, 0x69, 0x6e, 0x74,
7979 0x18, 0x62, 0x66, 0x74, 0x65, 0x78, 0x74, 0x20, 0x32, 0x78,
7980 0x1e, 0x6c, 0x69, 0x65, 0x73, 0x2c, 0x20, 0x64, 0x61, 0x6d,
7981 0x6e, 0x20, 0x6c, 0x69, 0x65, 0x73, 0x20, 0x61, 0x6e, 0x64,
7982 0x20, 0x73, 0x74, 0x61, 0x74, 0x69, 0x73, 0x74, 0x69, 0x63,
7983 0x73
7984};
7985
7986#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
7987
7988/* As above, but the arrays are indefinite length */
7989static const uint8_t pValidIndefWrappedMapEncoded[] = {
7990 0x9f, 0x07, 0x9f, 0x58, 0x97,
7991 0xa3, 0x6d, 0x66, 0x69, 0x72, 0x73, 0x74, 0x20, 0x69, 0x6e,
7992 0x74, 0x65, 0x67, 0x65, 0x72, 0x18, 0x2a, 0x77, 0x61, 0x6e,
7993 0x20, 0x61, 0x72, 0x72, 0x61, 0x79, 0x20, 0x6f, 0x66, 0x20,
7994 0x74, 0x77, 0x6f, 0x20, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67,
7995 0x73, 0x82, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x31,
7996 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x32, 0x6c, 0x6d,
7997 0x61, 0x70, 0x20, 0x69, 0x6e, 0x20, 0x61, 0x20, 0x6d, 0x61,
7998 0x70, 0xa4, 0x67, 0x62, 0x79, 0x74, 0x65, 0x73, 0x20, 0x31,
7999 0x44, 0x78, 0x78, 0x78, 0x78, 0x67, 0x62, 0x79, 0x74, 0x65,
8000 0x73, 0x20, 0x32, 0x44, 0x79, 0x79, 0x79, 0x79, 0x6b, 0x61,
8001 0x6e, 0x6f, 0x74, 0x68, 0x65, 0x72, 0x20, 0x69, 0x6e, 0x74,
8002 0x18, 0x62, 0x66, 0x74, 0x65, 0x78, 0x74, 0x20, 0x32, 0x78,
8003 0x1e, 0x6c, 0x69, 0x65, 0x73, 0x2c, 0x20, 0x64, 0x61, 0x6d,
8004 0x6e, 0x20, 0x6c, 0x69, 0x65, 0x73, 0x20, 0x61, 0x6e, 0x64,
8005 0x20, 0x73, 0x74, 0x61, 0x74, 0x69, 0x73, 0x74, 0x69, 0x63,
8006 0x73,
8007 0xff, 0xff
8008};
8009#endif
8010
8011
8012static const uint8_t pWithEmptyMap[] = {0x82, 0x18, 0x64, 0xa0};
8013
8014#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
8015static const uint8_t pWithEmptyMapInDef[] = {0x9f, 0x18, 0x64, 0xbf, 0xff, 0xff};
8016#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
8017
8018#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
Laurence Lundblade37286c02022-09-03 10:05:02 -07008019/*
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08008020 * An array of one that contains a byte string that is an indefinite
8021 * length string that CBOR wraps an array of three numbers [42, 43,
8022 * 44]. The byte string is an implicit tag 24.
8023 *
8024 * [
8025 * (_ h'83', h'18', h'2A182B', h'182C')
8026 * ]
Laurence Lundblade37286c02022-09-03 10:05:02 -07008027 */
Laurence Lundbladecf41c522021-02-20 10:19:07 -07008028static const uint8_t pWrappedByIndefiniteLength[] = {
8029 0x81,
Laurence Lundbladecf41c522021-02-20 10:19:07 -07008030 0x5f,
8031 0x41, 0x83,
8032 0x41, 0x18,
8033 0x43, 0x2A, 0x18, 0x2B,
8034 0x42, 0x18, 0x2C,
8035 0xff
8036};
8037#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
8038
8039
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03008040int32_t PeekAndRewindTest(void)
Laurence Lundblade2a26abb2020-11-05 19:06:54 -08008041{
8042 QCBORItem Item;
8043 QCBORError nCBORError;
8044 QCBORDecodeContext DCtx;
8045
8046 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0);
8047
8048 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
8049 return 100+(int32_t)nCBORError;
8050 }
8051 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
8052 return 200;
8053 }
8054
Laurence Lundblade3427dee2021-06-20 11:11:24 -07008055 QCBORDecode_VPeekNext(&DCtx, &Item);
8056 if((nCBORError = QCBORDecode_GetError(&DCtx))) {
8057 return 150+(int32_t)nCBORError;
8058 }
8059 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
8060 return 250;
8061 }
8062
Laurence Lundblade2a26abb2020-11-05 19:06:54 -08008063 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
8064 return (int32_t)nCBORError;
8065 }
8066 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
8067 return 300;
8068 }
8069
8070 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
8071 return 400 + (int32_t)nCBORError;
8072 }
8073 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
8074 return 500;
8075 }
8076
8077 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8078 return (int32_t)nCBORError;
8079 }
8080 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
8081 return 600;
8082 }
8083
8084 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
8085 return 900 + (int32_t)nCBORError;
8086 }
8087 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
8088 Item.uDataType != QCBOR_TYPE_INT64 ||
8089 Item.val.int64 != 42 ||
8090 Item.uDataAlloc ||
8091 Item.uLabelAlloc ||
8092 UsefulBufCompareToSZ(Item.label.string, "first integer")) {
8093 return 1000;
8094 }
8095
8096 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8097 return 1100 + (int32_t)nCBORError;
8098 }
8099
8100 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
8101 Item.uDataType != QCBOR_TYPE_INT64 ||
8102 Item.val.int64 != 42 ||
8103 Item.uDataAlloc ||
8104 Item.uLabelAlloc ||
8105 UsefulBufCompareToSZ(Item.label.string, "first integer")) {
8106 return 1200;
8107 }
8108
8109
8110 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8111 return 1300 + (int32_t)nCBORError;
8112 }
8113 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
8114 Item.uDataAlloc ||
8115 Item.uLabelAlloc ||
8116 UsefulBufCompareToSZ(Item.label.string, "an array of two strings") ||
8117 Item.uDataType != QCBOR_TYPE_ARRAY ||
Laurence Lundbladecf41c522021-02-20 10:19:07 -07008118 Item.val.uCount != 2) {
Laurence Lundblade2a26abb2020-11-05 19:06:54 -08008119 return 1400;
Laurence Lundbladecf41c522021-02-20 10:19:07 -07008120 }
Laurence Lundblade2a26abb2020-11-05 19:06:54 -08008121
8122 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8123 return 1500 + (int32_t)nCBORError;
8124 }
8125 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
8126 Item.uDataAlloc ||
8127 Item.uLabelAlloc ||
8128 UsefulBufCompareToSZ(Item.val.string, "string1")) {
8129 return 1600;
8130 }
8131
8132 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
8133 return 1700 + (int32_t)nCBORError;
8134 }
8135 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
8136 Item.uDataAlloc ||
8137 Item.uLabelAlloc ||
8138 UsefulBufCompareToSZ(Item.val.string, "string2")) {
8139 return 1800;
8140 }
8141
8142 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
8143 return (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 1900;
8150 }
8151
8152 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8153 return (int32_t)nCBORError;
8154 }
8155 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
8156 Item.uDataAlloc ||
8157 Item.uLabelAlloc ||
8158 UsefulBufCompareToSZ(Item.val.string, "string2")) {
8159 return 2000;
8160 }
8161
8162
8163 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8164 return 2100 + (int32_t)nCBORError;
8165 }
8166 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
8167 Item.uDataAlloc ||
8168 Item.uLabelAlloc ||
8169 UsefulBufCompareToSZ(Item.label.string, "map in a map") ||
8170 Item.uDataType != QCBOR_TYPE_MAP ||
8171 Item.val.uCount != 4) {
8172 return 2100;
8173 }
8174
8175 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8176 return 2200 + (int32_t)nCBORError;
8177 }
8178 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
8179 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("bytes 1"))||
8180 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
8181 Item.uDataAlloc ||
8182 Item.uLabelAlloc ||
8183 UsefulBufCompareToSZ(Item.val.string, "xxxx")) {
8184 return 2300;
8185 }
8186
8187 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
8188 return 2400 + (int32_t)nCBORError;
8189 }
8190 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
8191 UsefulBufCompareToSZ(Item.label.string, "bytes 2") ||
8192 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
8193 Item.uDataAlloc ||
8194 Item.uLabelAlloc ||
8195 UsefulBufCompareToSZ(Item.val.string, "yyyy")) {
8196 return 2500;
8197 }
8198
8199 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8200 return 2600 + (int32_t)nCBORError;
8201 }
8202 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
8203 UsefulBufCompareToSZ(Item.label.string, "bytes 2") ||
8204 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
8205 Item.uDataAlloc ||
8206 Item.uLabelAlloc ||
8207 UsefulBufCompareToSZ(Item.val.string, "yyyy")) {
8208 return 2700;
8209 }
8210
8211 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8212 return 2800 + (int32_t)nCBORError;
8213 }
8214 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
8215 Item.uDataAlloc ||
8216 Item.uLabelAlloc ||
8217 UsefulBufCompareToSZ(Item.label.string, "another int") ||
8218 Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundbladecf41c522021-02-20 10:19:07 -07008219 Item.val.int64 != 98) {
Laurence Lundblade2a26abb2020-11-05 19:06:54 -08008220 return 2900;
Laurence Lundbladecf41c522021-02-20 10:19:07 -07008221 }
Laurence Lundblade2a26abb2020-11-05 19:06:54 -08008222
8223 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
8224 return 3000 + (int32_t)nCBORError;
8225 }
8226 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
8227 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("text 2"))||
8228 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
8229 Item.uDataAlloc ||
8230 Item.uLabelAlloc ||
8231 UsefulBufCompareToSZ(Item.val.string, "lies, damn lies and statistics")) {
8232 return 3100;
8233 }
8234
8235 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8236 return 3200 + (int32_t)nCBORError;
8237 }
8238 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
8239 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("text 2"))||
8240 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
8241 Item.uDataAlloc ||
8242 Item.uLabelAlloc ||
8243 UsefulBufCompareToSZ(Item.val.string, "lies, damn lies and statistics")) {
8244 return 3300;
8245 }
8246
Laurence Lundblade3427dee2021-06-20 11:11:24 -07008247 nCBORError = QCBORDecode_PeekNext(&DCtx, &Item);
8248 if(nCBORError != QCBOR_ERR_NO_MORE_ITEMS) {
8249 return 3300 + (int32_t)nCBORError;
8250 }
8251
8252 QCBORDecode_VPeekNext(&DCtx, &Item);
8253 nCBORError = QCBORDecode_GetError(&DCtx);
8254 if(nCBORError != QCBOR_ERR_NO_MORE_ITEMS) {
8255 return 3400 + (int32_t)nCBORError;
8256 }
8257
8258 QCBORDecode_VPeekNext(&DCtx, &Item);
8259 nCBORError = QCBORDecode_GetError(&DCtx);
8260 if(nCBORError != QCBOR_ERR_NO_MORE_ITEMS) {
8261 return 3500 + (int32_t)nCBORError;
8262 }
Laurence Lundbladecf41c522021-02-20 10:19:07 -07008263
8264
8265 // Rewind to top level after entering several maps
8266 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0);
8267
8268 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8269 return (int32_t)nCBORError;
8270 }
8271 if(Item.uDataType != QCBOR_TYPE_MAP ||
8272 Item.val.uCount != 3) {
8273 return 400;
8274 }
8275
8276 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8277 return 4000+(int32_t)nCBORError;
8278 }
8279
8280 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
8281 Item.uDataType != QCBOR_TYPE_INT64 ||
8282 Item.val.int64 != 42 ||
8283 Item.uDataAlloc ||
8284 Item.uLabelAlloc ||
8285 UsefulBufCompareToSZ(Item.label.string, "first integer")) {
8286 return 4100;
8287 }
8288
8289 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8290 return 4100+(int32_t)nCBORError;
8291 }
8292 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
8293 Item.uDataAlloc ||
8294 Item.uLabelAlloc ||
8295 UsefulBufCompareToSZ(Item.label.string, "an array of two strings") ||
8296 Item.uDataType != QCBOR_TYPE_ARRAY ||
8297 Item.val.uCount != 2) {
8298 return 4200;
8299 }
8300
8301 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8302 return 4200+(int32_t)nCBORError;
8303 }
8304 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
8305 Item.uDataAlloc ||
8306 Item.uLabelAlloc ||
8307 UsefulBufCompareToSZ(Item.val.string, "string1")) {
8308 return 4300;
8309 }
8310
8311 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8312 return 4300+(int32_t)nCBORError;
8313 }
8314 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
8315 Item.uDataAlloc ||
8316 Item.uLabelAlloc ||
8317 UsefulBufCompareToSZ(Item.val.string, "string2")) {
8318 return 4400;
8319 }
8320
8321 QCBORDecode_Rewind(&DCtx);
8322
8323 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8324 return 4400+(int32_t)nCBORError;
8325 }
8326 if(Item.uDataType != QCBOR_TYPE_MAP ||
8327 Item.val.uCount != 3) {
8328 return 4500;
8329 }
8330
8331 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8332 return (int32_t)nCBORError;
8333 }
8334
8335 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
8336 Item.uDataType != QCBOR_TYPE_INT64 ||
8337 Item.val.int64 != 42 ||
8338 Item.uDataAlloc ||
8339 Item.uLabelAlloc ||
8340 UsefulBufCompareToSZ(Item.label.string, "first integer")) {
8341 return 4600;
8342 }
8343
8344 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8345 return (int32_t)nCBORError;
8346 }
8347 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
8348 Item.uDataAlloc ||
8349 Item.uLabelAlloc ||
8350 UsefulBufCompareToSZ(Item.label.string, "an array of two strings") ||
8351 Item.uDataType != QCBOR_TYPE_ARRAY ||
8352 Item.val.uCount != 2) {
8353 return 4700;
8354 }
8355
8356 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8357 return (int32_t)nCBORError;
8358 }
8359 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
8360 Item.uDataAlloc ||
8361 Item.uLabelAlloc ||
8362 UsefulBufCompareToSZ(Item.val.string, "string1")) {
8363 return 4800;
8364 }
8365
8366 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8367 return 4900+(int32_t)nCBORError;
8368 }
8369 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
8370 Item.uDataAlloc ||
8371 Item.uLabelAlloc ||
8372 UsefulBufCompareToSZ(Item.val.string, "string2")) {
8373 return 5000;
8374 }
8375
8376
8377 // Rewind an entered map
8378 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0);
8379
8380 QCBORDecode_EnterMap(&DCtx, NULL);
8381
8382 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8383 return 5100+(int32_t)nCBORError;
8384 }
8385
8386 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
8387 Item.uDataType != QCBOR_TYPE_INT64 ||
8388 Item.val.int64 != 42 ||
8389 Item.uDataAlloc ||
8390 Item.uLabelAlloc ||
8391 UsefulBufCompareToSZ(Item.label.string, "first integer")) {
8392 return 5200;
8393 }
8394
8395 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8396 return 5200+(int32_t)nCBORError;
8397 }
8398 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
8399 Item.uDataAlloc ||
8400 Item.uLabelAlloc ||
8401 UsefulBufCompareToSZ(Item.label.string, "an array of two strings") ||
8402 Item.uDataType != QCBOR_TYPE_ARRAY ||
8403 Item.val.uCount != 2) {
8404 return -5300;
8405 }
8406
8407 QCBORDecode_Rewind(&DCtx);
8408
8409 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8410 return 5300+(int32_t)nCBORError;
8411 }
8412
8413 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
8414 Item.uDataType != QCBOR_TYPE_INT64 ||
8415 Item.val.int64 != 42 ||
8416 Item.uDataAlloc ||
8417 Item.uLabelAlloc ||
8418 UsefulBufCompareToSZ(Item.label.string, "first integer")) {
8419 return 5400;
8420 }
8421
8422 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8423 return 5400+(int32_t)nCBORError;
8424 }
8425 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
8426 Item.uDataAlloc ||
8427 Item.uLabelAlloc ||
8428 UsefulBufCompareToSZ(Item.label.string, "an array of two strings") ||
8429 Item.uDataType != QCBOR_TYPE_ARRAY ||
8430 Item.val.uCount != 2) {
8431 return 5500;
8432 }
8433
8434
8435 // Rewind and entered array inside an entered map
8436 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0);
8437
8438 QCBORDecode_EnterMap(&DCtx, NULL);
8439
8440 QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings");
8441
8442 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8443 return 5600+(int32_t)nCBORError;
8444 }
8445 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
8446 Item.uDataAlloc ||
8447 Item.uLabelAlloc ||
8448 UsefulBufCompareToSZ(Item.val.string, "string1")) {
8449 return 5700;
8450 }
8451
8452 QCBORDecode_Rewind(&DCtx);
8453
8454 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8455 return 5700+(int32_t)nCBORError;
8456 }
8457 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
8458 Item.uDataAlloc ||
8459 Item.uLabelAlloc ||
8460 UsefulBufCompareToSZ(Item.val.string, "string1")) {
8461 return 5800;
8462 }
8463
8464 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8465 return (int32_t)nCBORError;
8466 }
8467 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
8468 Item.uDataAlloc ||
8469 Item.uLabelAlloc ||
8470 UsefulBufCompareToSZ(Item.val.string, "string2")) {
8471 return 5900;
8472 }
8473
8474 QCBORDecode_Rewind(&DCtx);
8475
8476 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8477 return 5900+(int32_t)nCBORError;
8478 }
8479 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
8480 Item.uDataAlloc ||
8481 Item.uLabelAlloc ||
8482 UsefulBufCompareToSZ(Item.val.string, "string1")) {
8483 return 6000;
8484 }
8485
8486
8487 // Rewind a byte string inside an array inside an array
8488 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidWrappedMapEncoded), 0);
8489
8490 QCBORDecode_EnterArray(&DCtx, NULL);
8491
8492 uint64_t i;
8493 QCBORDecode_GetUInt64(&DCtx, &i);
8494
8495 QCBORDecode_EnterArray(&DCtx, NULL);
8496
8497 QCBORDecode_EnterBstrWrapped(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
8498 if(QCBORDecode_GetError(&DCtx)) {
8499 return 6100;
8500 }
8501
8502 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8503 return (int32_t)nCBORError;
8504 }
8505 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
8506 return 6200;
8507 }
8508
8509 QCBORDecode_Rewind(&DCtx);
8510
8511 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8512 return 6300+(int32_t)nCBORError;
8513 }
8514 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
8515 return 6400;
8516 }
8517
8518#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
8519 // Rewind a byte string inside an indefinite-length array inside
8520 // indefinite-length array
8521
8522 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidIndefWrappedMapEncoded), 0);
8523
8524 QCBORDecode_EnterArray(&DCtx, NULL);
8525
8526 QCBORDecode_GetUInt64(&DCtx, &i);
8527
8528 QCBORDecode_EnterArray(&DCtx, NULL);
8529
8530 QCBORDecode_EnterBstrWrapped(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
8531 if(QCBORDecode_GetError(&DCtx)) {
8532 return 6500;
8533 }
8534
8535 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8536 return 6600+(int32_t)nCBORError;
8537 }
8538 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
8539 return 6700;
8540 }
8541
8542 QCBORDecode_Rewind(&DCtx);
8543
8544 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8545 return 6800+(int32_t)nCBORError;
8546 }
8547 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
8548 return 6900;
8549 }
8550#endif
8551
8552 // Rewind an empty map
8553 // [100, {}]
8554 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pWithEmptyMap), 0);
8555 QCBORDecode_EnterArray(&DCtx, NULL);
8556 QCBORDecode_GetUInt64(&DCtx, &i);
8557 if(i != 100) {
8558 return 7010;
8559 }
8560 QCBORDecode_EnterMap(&DCtx, NULL);
8561
8562 /* Do it 5 times to be sure multiple rewinds work */
8563 for(int n = 0; n < 5; n++) {
8564 nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
8565 if(nCBORError != QCBOR_ERR_NO_MORE_ITEMS) {
8566 return 7000 + n;
8567 }
8568 QCBORDecode_Rewind(&DCtx);
8569 }
8570 QCBORDecode_ExitMap(&DCtx);
8571 QCBORDecode_Rewind(&DCtx);
8572 QCBORDecode_GetUInt64(&DCtx, &i);
8573 if(i != 100) {
8574 return 7010;
8575 }
8576 QCBORDecode_ExitArray(&DCtx);
8577 QCBORDecode_Rewind(&DCtx);
8578 QCBORDecode_EnterArray(&DCtx, NULL);
8579 i = 9;
8580 QCBORDecode_GetUInt64(&DCtx, &i);
8581 if(i != 100) {
8582 return 7020;
8583 }
8584 if(QCBORDecode_GetError(&DCtx)){
8585 return 7030;
8586 }
8587
8588 // Rewind an empty indefinite length map
8589#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
8590 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pWithEmptyMapInDef), 0);
8591 QCBORDecode_EnterArray(&DCtx, NULL);
8592 QCBORDecode_GetUInt64(&DCtx, &i);
8593 if(i != 100) {
8594 return 7810;
8595 }
8596 QCBORDecode_EnterMap(&DCtx, NULL);
8597
8598 /* Do it 5 times to be sure multiple rewinds work */
8599 for(int n = 0; n < 5; n++) {
8600 nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
8601 if(nCBORError != QCBOR_ERR_NO_MORE_ITEMS) {
8602 return 7800 + n;
8603 }
8604 QCBORDecode_Rewind(&DCtx);
8605 }
8606 QCBORDecode_ExitMap(&DCtx);
8607 QCBORDecode_Rewind(&DCtx);
8608 QCBORDecode_GetUInt64(&DCtx, &i);
8609 if(i != 100) {
8610 return 7810;
8611 }
8612 QCBORDecode_ExitArray(&DCtx);
8613 QCBORDecode_Rewind(&DCtx);
8614 QCBORDecode_EnterArray(&DCtx, NULL);
8615 i = 9;
8616 QCBORDecode_GetUInt64(&DCtx, &i);
8617 if(i != 100) {
8618 return 7820;
8619 }
8620 if(QCBORDecode_GetError(&DCtx)){
8621 return 7830;
8622 }
8623#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
8624
8625 // Rewind an indefnite length byte-string wrapped sequence
8626#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
8627 QCBORDecode_Init(&DCtx,
8628 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pWrappedByIndefiniteLength),
8629 0);
8630 UsefulBuf_MAKE_STACK_UB(Pool, 100);
8631 QCBORDecode_SetMemPool(&DCtx, Pool, 0);
8632
8633 QCBORDecode_EnterArray(&DCtx, NULL);
8634 QCBORDecode_EnterBstrWrapped(&DCtx, 2, NULL);
8635 if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_INPUT_TOO_LARGE) {
Laurence Lundblade37286c02022-09-03 10:05:02 -07008636 /* TODO: This is what happens when trying to enter
8637 * indefinite-length byte string wrapped CBOR. Tolerate for
8638 * now. Eventually it needs to be fixed so this works, but that
8639 * is not simple.
8640 */
Laurence Lundbladecf41c522021-02-20 10:19:07 -07008641 return 7300;
8642 }
8643
8644 /*
8645 QCBORDecode_GetUInt64(&DCtx, &i);
8646 if(i != 42) {
8647 return 7110;
8648 }
8649 QCBORDecode_Rewind(&DCtx);
8650 QCBORDecode_GetUInt64(&DCtx, &i);
8651 if(i != 42) {
8652 return 7220;
8653 }*/
Laurence Lundblade37286c02022-09-03 10:05:02 -07008654
Laurence Lundbladecf41c522021-02-20 10:19:07 -07008655#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
8656
8657
8658 // Rewind an indefnite length byte-string wrapped sequence
8659
Laurence Lundblade2a26abb2020-11-05 19:06:54 -08008660 return 0;
8661}
Laurence Lundblade9f9c3732021-03-23 09:38:46 -07008662
8663
8664
8665
8666static const uint8_t spBooleansInMap[] =
8667{
8668 0xa1, 0x08, 0xf5
8669};
8670
8671static const uint8_t spBooleansInMapWrongType[] =
8672{
8673 0xa1, 0x08, 0xf6
8674};
8675
8676static const uint8_t spBooleansInMapNWF[] =
8677{
8678 0xa1, 0x08, 0x1a
8679};
8680
Laurence Lundblade8782dd32021-04-27 04:15:37 -07008681static const uint8_t spNullInMap[] =
8682{
8683 0xa1, 0x08, 0xf6
8684};
8685
8686static const uint8_t spUndefinedInMap[] =
8687{
8688 0xa1, 0x08, 0xf7
8689};
8690
Laurence Lundblade9f9c3732021-03-23 09:38:46 -07008691
8692int32_t BoolTest(void)
8693{
8694 QCBORDecodeContext DCtx;
8695 bool b;
8696
Laurence Lundblade8782dd32021-04-27 04:15:37 -07008697 QCBORDecode_Init(&DCtx,
8698 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMap),
8699 0);
Laurence Lundblade9f9c3732021-03-23 09:38:46 -07008700 QCBORDecode_EnterMap(&DCtx, NULL);
8701 QCBORDecode_GetBool(&DCtx, &b);
8702 if(QCBORDecode_GetAndResetError(&DCtx) || !b) {
8703 return 1;
8704 }
8705
8706 QCBORDecode_GetBoolInMapN(&DCtx, 7, &b);
8707 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_LABEL_NOT_FOUND) {
8708 return 2;
8709 }
8710
8711 QCBORDecode_GetBoolInMapN(&DCtx, 8, &b);
8712 if(QCBORDecode_GetAndResetError(&DCtx) || !b) {
8713 return 3;
8714 }
8715
8716
8717 QCBORDecode_GetBoolInMapSZ(&DCtx, "xx", &b);
8718 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_LABEL_NOT_FOUND) {
8719 return 4;
8720 }
8721
Laurence Lundblade8782dd32021-04-27 04:15:37 -07008722 QCBORDecode_Init(&DCtx,
8723 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMapWrongType),
8724 0);
Laurence Lundblade9f9c3732021-03-23 09:38:46 -07008725 QCBORDecode_EnterMap(&DCtx, NULL);
8726 QCBORDecode_GetBool(&DCtx, &b);
8727 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
8728 return 5;
8729 }
8730
Laurence Lundblade8782dd32021-04-27 04:15:37 -07008731 QCBORDecode_Init(&DCtx,
8732 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMapNWF),
8733 0);
Laurence Lundblade9f9c3732021-03-23 09:38:46 -07008734 QCBORDecode_EnterMap(&DCtx, NULL);
8735 QCBORDecode_GetBool(&DCtx, &b);
8736 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_HIT_END) {
8737 return 6;
8738 }
8739
Laurence Lundblade8782dd32021-04-27 04:15:37 -07008740
8741 QCBORDecode_Init(&DCtx,
8742 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spNullInMap),
8743 0);
8744 QCBORDecode_EnterMap(&DCtx, NULL);
8745 QCBORDecode_GetNull(&DCtx);
8746 if(QCBORDecode_GetAndResetError(&DCtx)) {
8747 return 7;
8748 }
8749
8750 QCBORDecode_Init(&DCtx,
8751 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMap),
8752 0);
8753 QCBORDecode_EnterMap(&DCtx, NULL);
8754 QCBORDecode_GetNull(&DCtx);
8755 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
8756 return 8;
8757 }
8758
8759 QCBORDecode_Init(&DCtx,
8760 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spNullInMap),
8761 0);
8762 QCBORDecode_EnterMap(&DCtx, NULL);
8763 QCBORDecode_GetNullInMapN(&DCtx, 8);
8764 if(QCBORDecode_GetAndResetError(&DCtx)) {
8765 return 9;
8766 }
8767
8768 QCBORDecode_Init(&DCtx,
8769 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMap),
8770 0);
8771 QCBORDecode_EnterMap(&DCtx, NULL);
8772 QCBORDecode_GetNullInMapN(&DCtx, 8);
8773 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
8774 return 10;
8775 }
8776
8777 QCBORDecode_Init(&DCtx,
8778 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMapNWF),
8779 0);
8780 QCBORDecode_EnterMap(&DCtx, NULL);
8781 QCBORDecode_GetUndefined(&DCtx);
8782 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_HIT_END) {
8783 return 11;
8784 }
8785
8786 QCBORDecode_Init(&DCtx,
8787 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUndefinedInMap),
8788 0);
8789 QCBORDecode_EnterMap(&DCtx, NULL);
8790 QCBORDecode_GetUndefined(&DCtx);
8791 if(QCBORDecode_GetAndResetError(&DCtx)) {
8792 return 12;
8793 }
8794
8795 QCBORDecode_Init(&DCtx,
8796 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMap),
8797 0);
8798 QCBORDecode_EnterMap(&DCtx, NULL);
8799 QCBORDecode_GetUndefined(&DCtx);
8800 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
8801 return 13;
8802 }
8803
8804 QCBORDecode_Init(&DCtx,
8805 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUndefinedInMap),
8806 0);
8807 QCBORDecode_EnterMap(&DCtx, NULL);
8808 QCBORDecode_GetUndefinedInMapN(&DCtx, 8);
8809 if(QCBORDecode_GetAndResetError(&DCtx)) {
8810 return 14;
8811 }
8812
8813 QCBORDecode_Init(&DCtx,
8814 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMap),
8815 0);
8816 QCBORDecode_EnterMap(&DCtx, NULL);
8817 QCBORDecode_GetUndefinedInMapN(&DCtx, 8);
8818 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
8819 return 15;
8820 }
8821
8822 QCBORDecode_Init(&DCtx,
8823 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMapNWF),
8824 0);
8825 QCBORDecode_EnterMap(&DCtx, NULL);
8826 QCBORDecode_GetUndefined(&DCtx);
8827 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_HIT_END) {
8828 return 15;
8829 }
8830
Laurence Lundblade9f9c3732021-03-23 09:38:46 -07008831 return 0;
8832}
Laurence Lundbladef00b8be2024-03-08 10:34:33 -08008833
8834
Laurence Lundbladed883ad32024-03-23 22:37:37 -07008835#if !defined(USEFULBUF_DISABLE_ALL_FLOAT) && !defined(QCBOR_DISABLE_PREFERRED_FLOAT)
8836
8837struct PreciseNumberConversion {
8838 char *szDescription;
8839 UsefulBufC CBOR;
8840 QCBORError uError;
8841 uint8_t qcborType;
8842 struct {
8843 int64_t int64;
8844 uint64_t uint64;
8845 double d;
8846 } number;
8847};
8848
8849
8850static const struct PreciseNumberConversion PreciseNumberConversions[] = {
8851 {
8852 "-0.00",
8853 {"\xf9\x80\x00", 3},
8854 QCBOR_SUCCESS,
8855 QCBOR_TYPE_INT64,
8856 {0, 0, 0}
8857 },
8858 {
8859 "NaN",
8860 {"\xf9\x7e\x00", 3},
8861 QCBOR_SUCCESS,
8862 QCBOR_TYPE_DOUBLE,
8863 {0, 0, NAN}
8864 },
8865 {
8866 "NaN payload",
8867 {"\xFB\x7F\xFF\xFF\xFF\xFF\xFF\xFF\xFF", 9},
8868 QCBOR_SUCCESS,
8869 QCBOR_TYPE_DOUBLE,
8870 {0, 0, NAN}
8871 },
8872 {
8873 "65536.0 single",
8874 {"\xFA\x47\x80\x00\x00", 5},
8875 QCBOR_SUCCESS,
8876 QCBOR_TYPE_INT64,
8877 {65536, 0, 0}
8878 },
8879 {
8880 "Infinity",
8881 {"\xf9\x7c\x00", 3},
8882 QCBOR_SUCCESS,
8883 QCBOR_TYPE_DOUBLE,
8884 {0, 0, INFINITY}
8885 },
8886 {
8887 "1.0",
8888 {"\xf9\x3c\x00", 3},
8889 QCBOR_SUCCESS,
8890 QCBOR_TYPE_INT64,
8891 {1, 0, 0}
8892 },
8893 {
8894 "UINT64_MAX",
8895 {"\x1B\xff\xff\xff\xff\xff\xff\xff\xff", 9},
8896 QCBOR_SUCCESS,
8897 QCBOR_TYPE_UINT64,
8898 {0, UINT64_MAX, 0}
8899 },
8900 {
8901 "INT64_MIN",
8902 {"\x3B\x7f\xff\xff\xff\xff\xff\xff\xff", 9},
8903 QCBOR_SUCCESS,
8904 QCBOR_TYPE_INT64,
8905 {INT64_MIN, 0, 0}
8906 },
8907 {
8908 "18446742974197923840",
8909 {"\xFB\x43\xEF\xFF\xFF\xE0\x00\x00\x00", 9},
8910 QCBOR_SUCCESS,
8911 QCBOR_TYPE_UINT64,
8912 {0, 18446742974197923840ULL, 0}
8913 },
8914 {
8915 "65-bit neg, too much precision",
8916 {"\x3B\x80\x00\x00\x00\x00\x00\x00\x01", 9},
8917 QCBOR_SUCCESS,
8918 QCBOR_TYPE_65BIT_NEG_INT,
8919 {0, 0x8000000000000001, 0}
8920 },
8921 {
8922 "65-bit neg lots of precision",
8923 {"\x3B\xff\xff\xff\xff\xff\xff\xf0\x00", 9},
8924 QCBOR_SUCCESS,
8925 QCBOR_TYPE_DOUBLE,
8926 {0, 0, -18446744073709547521.0}
8927 },
8928 {
8929 "65-bit neg very precise",
8930 {"\x3B\xff\xff\xff\xff\xff\xff\xf8\x00", 9},
8931 QCBOR_SUCCESS,
8932 QCBOR_TYPE_DOUBLE,
8933 {0, 0, -18446744073709549569.0}
8934 },
8935 {
8936 "65-bit neg too precise",
8937 {"\x3B\xff\xff\xff\xff\xff\xff\xfc\x00", 9},
8938 QCBOR_SUCCESS,
8939 QCBOR_TYPE_65BIT_NEG_INT,
8940 {0, 18446744073709550592ULL, 0.0}
8941 },
8942 {
8943 "65-bit neg, power of two",
8944 {"\x3B\x80\x00\x00\x00\x00\x00\x00\x00", 9},
8945 QCBOR_SUCCESS,
8946 QCBOR_TYPE_DOUBLE,
8947 {0, 0, -9223372036854775809.0}
8948 },
8949 {
8950 "Zero",
8951 {"\x00", 1},
8952 QCBOR_SUCCESS,
8953 QCBOR_TYPE_INT64,
8954 {0, 0, 0}
8955 },
8956 {
8957 "Pi",
8958 {"\xFB\x40\x09\x2A\xDB\x40\x2D\x16\xB9", 9},
8959 QCBOR_SUCCESS,
8960 QCBOR_TYPE_DOUBLE,
8961 {0, 0, 3.145926}
8962 },
8963 {
8964 "String",
8965 {"\x60", 1},
8966 QCBOR_ERR_UNEXPECTED_TYPE,
8967 QCBOR_TYPE_NONE,
8968 {0, 0, 0}
8969 }
8970};
8971
8972
8973int32_t
8974PreciseNumbersTest(void)
8975{
8976 int i;
8977 QCBORError uErr;
8978 QCBORItem Item;
8979 QCBORDecodeContext DCtx;
8980 const struct PreciseNumberConversion *pTest;
8981
8982 const int count = (int)C_ARRAY_COUNT(PreciseNumberConversions, struct PreciseNumberConversion);
8983
8984 for(i = 0; i < count; i++) {
8985 pTest = &PreciseNumberConversions[i];
8986
8987 if(i == 11) {
8988 uErr = 99; // For break point only
8989 }
8990
8991 QCBORDecode_Init(&DCtx, pTest->CBOR, 0);
8992
8993 QCBORDecode_GetNumberConvertPrecisely(&DCtx, &Item);
8994
8995 uErr = QCBORDecode_GetError(&DCtx);
8996
8997 if(uErr != pTest->uError) {
8998 return i * 1000 + (int)uErr;
8999 }
9000
9001 if(pTest->qcborType != Item.uDataType) {
9002 return i * 1000 + 200;
9003 }
9004
9005 if(pTest->qcborType == QCBOR_TYPE_NONE) {
9006 continue;
9007 }
9008
9009 switch(pTest->qcborType) {
9010 case QCBOR_TYPE_INT64:
9011 if(Item.val.int64 != pTest->number.int64) {
9012 return i * 1000 + 300;
9013 }
9014 break;
9015
9016 case QCBOR_TYPE_UINT64:
9017 case QCBOR_TYPE_65BIT_NEG_INT:
9018 if(Item.val.uint64 != pTest->number.uint64) {
9019 return i * 1000 + 400;
9020 }
9021 break;
9022
9023 case QCBOR_TYPE_DOUBLE:
9024 if(isnan(pTest->number.d)) {
9025 if(!isnan(Item.val.dfnum)) {
9026 return i * 1000 + 600;
9027 }
9028 } else {
9029 if(Item.val.dfnum != pTest->number.d) {
9030 return i * 1000 + 500;
9031 }
9032 }
9033 break;
9034 }
9035 }
9036 return 0;
9037}
9038
9039#endif /* ! USEFULBUF_DISABLE_ALL_FLOAT && ! QCBOR_DISABLE_PREFERRED_FLOAT */
9040
9041
Laurence Lundbladef00b8be2024-03-08 10:34:33 -08009042int32_t
9043ErrorHandlingTests(void)
9044{
9045 QCBORDecodeContext DCtx;
9046 QCBORItem Item;
9047 QCBORError uError;
9048 int64_t integer;
9049
9050 /* Test QCBORDecode_SetError() */
9051 QCBORDecode_Init(&DCtx,
9052 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded),
9053 QCBOR_DECODE_MODE_NORMAL);
9054
9055 QCBORDecode_SetError(&DCtx, QCBOR_ERR_FIRST_USER_DEFINED);
9056
9057 QCBORDecode_VGetNext(&DCtx, &Item);
9058
9059 uError = QCBORDecode_GetError(&DCtx);
9060
9061 if(uError != QCBOR_ERR_FIRST_USER_DEFINED) {
9062 return -1;
9063 }
9064
9065 if(Item.uLabelType != QCBOR_TYPE_NONE ||
9066 Item.uDataType != QCBOR_TYPE_NONE) {
9067 return -2;
9068 }
9069
9070
9071 /* Test data type returned from previous error */
9072 QCBORDecode_Init(&DCtx,
9073 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded),
9074 QCBOR_DECODE_MODE_NORMAL);
9075 QCBORDecode_GetInt64(&DCtx, &integer);
9076 uError = QCBORDecode_GetError(&DCtx);
9077 if(uError != QCBOR_ERR_UNEXPECTED_TYPE) {
9078 return -3;
9079 }
9080
9081 QCBORDecode_VGetNext(&DCtx, &Item);
9082 if(Item.uLabelType != QCBOR_TYPE_NONE ||
9083 Item.uDataType != QCBOR_TYPE_NONE) {
9084 return -2;
9085 }
9086 uError = QCBORDecode_GetError(&DCtx);
9087 if(uError != QCBOR_ERR_UNEXPECTED_TYPE) {
9088 return -3;
9089 }
9090
9091
9092 /* Test error classification functions */
9093
9094 if(!QCBORDecode_IsUnrecoverableError(QCBOR_ERR_INDEFINITE_STRING_CHUNK)) {
9095 return -10;
9096 }
9097 if(QCBORDecode_IsUnrecoverableError(QCBOR_SUCCESS)) {
9098 return -11;
9099 }
9100 if(!QCBORDecode_IsUnrecoverableError(QCBOR_ERR_INDEFINITE_STRING_CHUNK)) {
9101 return -12;
9102 }
9103 if(QCBORDecode_IsUnrecoverableError(QCBOR_ERR_DUPLICATE_LABEL)) {
9104 return -13;
9105 }
9106
9107 if(!QCBORDecode_IsNotWellFormedError(QCBOR_ERR_BAD_TYPE_7)) {
9108 return -20;
9109 }
9110 if(!QCBORDecode_IsNotWellFormedError(QCBOR_ERR_BAD_BREAK)) {
9111 return -21;
9112 }
9113 if(QCBORDecode_IsNotWellFormedError(QCBOR_SUCCESS)) {
9114 return -22;
9115 }
9116 if(QCBORDecode_IsNotWellFormedError(QCBOR_ERR_ARRAY_DECODE_TOO_LONG)) {
9117 return -23;
9118 }
9119
9120 return 0;
9121}