blob: 810bb7aadb021440afb58dd79a0d17cba8bc4af0 [file] [log] [blame]
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001/*==============================================================================
Laurence Lundbladed92a6162018-11-01 11:38:35 +07002 Copyright (c) 2016-2018, The Linux Foundation.
Laurence Lundbladeb24faef2022-04-26 11:03:08 -06003 Copyright (c) 2018-2022, Laurence Lundblade.
Máté Tóth-Pálef5f07a2021-09-17 19:31:37 +02004 Copyright (c) 2021, Arm Limited.
Laurence Lundbladed92a6162018-11-01 11:38:35 +07005 All rights reserved.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08006
Laurence Lundblade0dbc9172018-11-01 14:17:21 +07007Redistribution and use in source and binary forms, with or without
8modification, are permitted provided that the following conditions are
9met:
10 * Redistributions of source code must retain the above copyright
11 notice, this list of conditions and the following disclaimer.
12 * Redistributions in binary form must reproduce the above
13 copyright notice, this list of conditions and the following
14 disclaimer in the documentation and/or other materials provided
15 with the distribution.
16 * Neither the name of The Linux Foundation nor the names of its
17 contributors, nor the name "Laurence Lundblade" may be used to
18 endorse or promote products derived from this software without
19 specific prior written permission.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -080020
Laurence Lundblade0dbc9172018-11-01 14:17:21 +070021THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
22WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
23MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
24ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
25BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
28BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
30OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
31IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Laurence Lundbladeee851742020-01-08 08:37:05 -080032 =============================================================================*/
Laurence Lundblade9e3651c2018-10-10 11:49:55 +080033
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080034#include "qcbor_decode_tests.h"
Laurence Lundblade844bb5c2020-03-01 17:27:25 -080035#include "qcbor/qcbor_encode.h"
36#include "qcbor/qcbor_decode.h"
Laurence Lundblade67257dc2020-07-27 03:33:37 -070037#include "qcbor/qcbor_spiffy_decode.h"
Laurence Lundbladed4728fd2018-12-17 15:15:56 -080038#include <string.h>
Laurence Lundblade9e3651c2018-10-10 11:49:55 +080039#include <math.h> // for fabs()
Laurence Lundbladebb1062e2019-08-12 23:28:54 -070040#include "not_well_formed_cbor.h"
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -080041
Laurence Lundblade9b334962020-08-27 10:55:53 -070042// Handy macro to compare a UsefulBuf to a C string
43#define UsefulBufCompareToSZ(x, y) \
44 UsefulBuf_Compare(x, UsefulBuf_FromSZ(y))
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080045
Laurence Lundbladea2e29072018-12-30 09:20:06 -080046#ifdef PRINT_FUNCTIONS_FOR_DEBUGGING
Laurence Lundblade20db9c92018-12-17 11:40:37 -080047#include <stdio.h>
Laurence Lundbladea2e29072018-12-30 09:20:06 -080048
49static void PrintUsefulBufC(const char *szLabel, UsefulBufC Buf)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080050{
51 if(szLabel) {
52 printf("%s ", szLabel);
53 }
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -080054
Laurence Lundblade570fab52018-10-13 18:28:27 +080055 size_t i;
Laurence Lundbladea2e29072018-12-30 09:20:06 -080056 for(i = 0; i < Buf.len; i++) {
57 uint8_t Z = ((uint8_t *)Buf.ptr)[i];
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080058 printf("%02x ", Z);
59 }
60 printf("\n");
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -080061
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080062 fflush(stdout);
63}
Laurence Lundbladee2c893c2020-12-26 17:41:53 -080064#endif /* PRINT_FUNCTIONS_FOR_DEBUGGING */
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080065
Laurence Lundbladecc7da412020-12-27 00:09:07 -080066/*
67 [
68 -9223372036854775808,
69 -4294967297,
70 -4294967296,
71 -4294967295,
72 -4294967294,
73 -2147483648,
74 -2147483647,
75 -65538,
76 -65537,
77 -65536,
78 -65535,
79 -65534,
80 -257,
81 -256,
82 -255,
83 -254,
84 -25,
85 -24,
86 -23,
87 -1,
88 0,
89 0,
90 1,
91 22,
92 23,
93 24,
94 25,
95 26,
96 254,
97 255,
98 256,
99 257,
100 65534,
101 65535,
102 65536,
103 65537,
104 65538,
105 2147483647,
106 2147483647,
107 2147483648,
108 2147483649,
109 4294967294,
110 4294967295,
111 4294967296,
112 4294967297,
113 9223372036854775807,
114 18446744073709551615
115 ]
116 */
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800117
Laurence Lundbladeb836efb2018-10-28 20:09:58 +0700118static const uint8_t spExpectedEncodedInts[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800119 0x98, 0x2f, 0x3b, 0x7f, 0xff, 0xff, 0xff, 0xff,
120 0xff, 0xff, 0xff, 0x3b, 0x00, 0x00, 0x00, 0x01,
121 0x00, 0x00, 0x00, 0x00, 0x3a, 0xff, 0xff, 0xff,
122 0xff, 0x3a, 0xff, 0xff, 0xff, 0xfe, 0x3a, 0xff,
123 0xff, 0xff, 0xfd, 0x3a, 0x7f, 0xff, 0xff, 0xff,
124 0x3a, 0x7f, 0xff, 0xff, 0xfe, 0x3a, 0x00, 0x01,
125 0x00, 0x01, 0x3a, 0x00, 0x01, 0x00, 0x00, 0x39,
126 0xff, 0xff, 0x39, 0xff, 0xfe, 0x39, 0xff, 0xfd,
127 0x39, 0x01, 0x00, 0x38, 0xff, 0x38, 0xfe, 0x38,
128 0xfd, 0x38, 0x18, 0x37, 0x36, 0x20, 0x00, 0x00,
129 0x01, 0x16, 0x17, 0x18, 0x18, 0x18, 0x19, 0x18,
130 0x1a, 0x18, 0xfe, 0x18, 0xff, 0x19, 0x01, 0x00,
131 0x19, 0x01, 0x01, 0x19, 0xff, 0xfe, 0x19, 0xff,
132 0xff, 0x1a, 0x00, 0x01, 0x00, 0x00, 0x1a, 0x00,
133 0x01, 0x00, 0x01, 0x1a, 0x00, 0x01, 0x00, 0x02,
134 0x1a, 0x7f, 0xff, 0xff, 0xff, 0x1a, 0x7f, 0xff,
135 0xff, 0xff, 0x1a, 0x80, 0x00, 0x00, 0x00, 0x1a,
136 0x80, 0x00, 0x00, 0x01, 0x1a, 0xff, 0xff, 0xff,
137 0xfe, 0x1a, 0xff, 0xff, 0xff, 0xff, 0x1b, 0x00,
138 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x1b,
139 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01,
140 0x1b, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
141 0xff, 0x1b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
142 0xff, 0xff};
143
144
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800145// return CBOR error or -1 if type of value doesn't match
146
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800147static int32_t IntegerValuesParseTestInternal(QCBORDecodeContext *pDCtx)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800148{
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700149 QCBORItem Item;
150 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800151
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800152 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700153 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800154 if(Item.uDataType != QCBOR_TYPE_ARRAY)
155 return -1;
156
157 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700158 return (int32_t)nCBORError;
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800159 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade570fab52018-10-13 18:28:27 +0800160 Item.val.int64 != -9223372036854775807LL - 1)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800161 return -1;
162
163 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700164 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800165 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade570fab52018-10-13 18:28:27 +0800166 Item.val.int64 != -4294967297)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800167 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800168
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800169 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700170 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800171 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade570fab52018-10-13 18:28:27 +0800172 Item.val.int64 != -4294967296)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800173 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800174
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800175 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700176 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800177 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade570fab52018-10-13 18:28:27 +0800178 Item.val.int64 != -4294967295)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800179 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800180
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800181 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700182 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800183 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade570fab52018-10-13 18:28:27 +0800184 Item.val.int64 != -4294967294)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800185 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800186
187
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800188 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700189 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800190 if(Item.uDataType != QCBOR_TYPE_INT64 ||
191 Item.val.int64 != -2147483648)
192 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800193
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800194 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700195 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800196 if(Item.uDataType != QCBOR_TYPE_INT64 ||
197 Item.val.int64 != -2147483647)
198 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800199
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800200 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700201 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800202 if(Item.uDataType != QCBOR_TYPE_INT64 ||
203 Item.val.int64 != -65538)
204 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800205
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800206 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700207 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800208 if(Item.uDataType != QCBOR_TYPE_INT64 ||
209 Item.val.int64 != -65537)
210 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800211
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800212 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700213 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800214 if(Item.uDataType != QCBOR_TYPE_INT64 ||
215 Item.val.int64 != -65536)
216 return -1;
217
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800218
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800219 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700220 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800221 if(Item.uDataType != QCBOR_TYPE_INT64 ||
222 Item.val.int64 != -65535)
223 return -1;
224
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 != -65534)
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 != -257)
237 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800238
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800239 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700240 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800241 if(Item.uDataType != QCBOR_TYPE_INT64 ||
242 Item.val.int64 != -256)
243 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800244
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800245 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700246 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800247 if(Item.uDataType != QCBOR_TYPE_INT64 ||
248 Item.val.int64 != -255)
249 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800250
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800251 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700252 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800253 if(Item.uDataType != QCBOR_TYPE_INT64 ||
254 Item.val.int64 != -254)
255 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800256
257
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800258 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700259 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800260 if(Item.uDataType != QCBOR_TYPE_INT64 ||
261 Item.val.int64 != -25)
262 return -1;
263
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 != -24)
269 return -1;
270
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800271
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 != -23)
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 != -1)
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 != 0)
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 != 0)
297 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800298
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800299 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700300 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800301 if(Item.uDataType != QCBOR_TYPE_INT64 ||
302 Item.val.int64 != 1)
303 return -1;
304
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800305
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800306 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700307 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800308 if(Item.uDataType != QCBOR_TYPE_INT64 ||
309 Item.val.int64 != 22)
310 return -1;
311
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 != 23)
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 != 24)
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 != 25)
331 return -1;
332
333 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700334 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800335 if(Item.uDataType != QCBOR_TYPE_INT64 ||
336 Item.val.int64 != 26)
337 return -1;
338
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800339
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800340 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700341 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800342 if(Item.uDataType != QCBOR_TYPE_INT64 ||
343 Item.val.int64 != 254)
344 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800345
346
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800347 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 != 255)
351 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800352
353
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 != 256)
358 return -1;
359
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800360
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 != 257)
365 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800366
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800367 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700368 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800369 if(Item.uDataType != QCBOR_TYPE_INT64 ||
370 Item.val.int64 != 65534)
371 return -1;
372
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800373
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800374 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700375 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800376 if(Item.uDataType != QCBOR_TYPE_INT64 ||
377 Item.val.int64 != 65535)
378 return -1;
379
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 != 65536)
385 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800386
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800387 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700388 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800389 if(Item.uDataType != QCBOR_TYPE_INT64 ||
390 Item.val.int64 != 65537)
391 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800392
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800393 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700394 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800395 if(Item.uDataType != QCBOR_TYPE_INT64 ||
396 Item.val.int64 != 65538)
397 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800398
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800399 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700400 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800401 if(Item.uDataType != QCBOR_TYPE_INT64 ||
402 Item.val.int64 != 2147483647)
403 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800404
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800405 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700406 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800407 if(Item.uDataType != QCBOR_TYPE_INT64 ||
408 Item.val.int64 != 2147483647)
409 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800410
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800411 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700412 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800413 if(Item.uDataType != QCBOR_TYPE_INT64 ||
414 Item.val.int64 != 2147483648)
415 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800416
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800417 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700418 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800419 if(Item.uDataType != QCBOR_TYPE_INT64 ||
420 Item.val.int64 != 2147483649)
421 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800422
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800423 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700424 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800425 if(Item.uDataType != QCBOR_TYPE_INT64 ||
426 Item.val.int64 != 4294967294)
427 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800428
429
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800430 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700431 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800432 if(Item.uDataType != QCBOR_TYPE_INT64 ||
433 Item.val.int64 != 4294967295)
434 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800435
436
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 != 4294967296)
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 != 4294967297)
448 return -1;
449
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800450
451
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800452 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700453 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800454 if(Item.uDataType != QCBOR_TYPE_INT64 ||
455 Item.val.int64 != 9223372036854775807LL)
456 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800457
458
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800459 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700460 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800461 if(Item.uDataType != QCBOR_TYPE_UINT64 ||
462 Item.val.uint64 != 18446744073709551615ULL)
463 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800464
465
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800466 if(QCBORDecode_Finish(pDCtx) != QCBOR_SUCCESS) {
467 return -1;
468 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800469
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800470 return 0;
471}
472
473
Laurence Lundbladecc7da412020-12-27 00:09:07 -0800474/* One less than the smallest negative integer allowed in C. Decoding
475 this should fail.
476 -9223372036854775809
477 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800478static const uint8_t spTooSmallNegative[] = {
479 0x3b, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
Laurence Lundblade21d1d812019-09-28 22:47:49 -1000480};
481
482
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800483/*
484 Tests the decoding of lots of different integers sizes
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +0800485 and values.
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800486 */
Maxim Zhukovd538f0a2022-12-20 20:40:38 +0300487int32_t IntegerValuesParseTest(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800488{
Laurence Lundblade21d1d812019-09-28 22:47:49 -1000489 int nReturn;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800490 QCBORDecodeContext DCtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800491
Laurence Lundblade21d1d812019-09-28 22:47:49 -1000492 QCBORDecode_Init(&DCtx,
493 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedEncodedInts),
494 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800495
Laurence Lundblade21d1d812019-09-28 22:47:49 -1000496 // The really big test of all successes
497 nReturn = IntegerValuesParseTestInternal(&DCtx);
498 if(nReturn) {
499 return nReturn;
500 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800501
Laurence Lundblade21d1d812019-09-28 22:47:49 -1000502 // The one large negative integer that can be parsed
503 QCBORDecode_Init(&DCtx,
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800504 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTooSmallNegative),
Laurence Lundblade21d1d812019-09-28 22:47:49 -1000505 QCBOR_DECODE_MODE_NORMAL);
506
507 QCBORItem item;
508 if(QCBORDecode_GetNext(&DCtx, &item) != QCBOR_ERR_INT_OVERFLOW) {
509 nReturn = -4000;
510 }
511
512 return(nReturn);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800513}
514
515
516/*
Laurence Lundbladeee851742020-01-08 08:37:05 -0800517 Creates a simple CBOR array and returns it in *pEncoded. The array is
518 malloced and needs to be freed. This is used by several tests.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800519
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800520 Two of the inputs can be set. Two other items in the array are fixed.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800521
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800522 */
523
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -0800524static uint8_t spSimpleArrayBuffer[50];
525
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800526static int32_t CreateSimpleArray(int nInt1, int nInt2, uint8_t **pEncoded, size_t *pEncodedLen)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800527{
528 QCBOREncodeContext ECtx;
529 int nReturn = -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800530
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800531 *pEncoded = NULL;
532 *pEncodedLen = INT32_MAX;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800533
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800534 // loop runs CBOR encoding twice. First with no buffer to
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -0800535 // calculate the length so buffer can be allocated correctly,
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800536 // and last with the buffer to do the actual encoding
537 do {
Laurence Lundblade0595e932018-11-02 22:22:47 +0700538 QCBOREncode_Init(&ECtx, (UsefulBuf){*pEncoded, *pEncodedLen});
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800539 QCBOREncode_OpenArray(&ECtx);
540 QCBOREncode_AddInt64(&ECtx, nInt1);
541 QCBOREncode_AddInt64(&ECtx, nInt2);
542 QCBOREncode_AddBytes(&ECtx, ((UsefulBufC) {"galactic", 8}));
543 QCBOREncode_AddBytes(&ECtx, ((UsefulBufC) {"haven token", 11}));
544 QCBOREncode_CloseArray(&ECtx);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800545
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -0800546 if(QCBOREncode_FinishGetSize(&ECtx, pEncodedLen))
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800547 goto Done;
548
549 if(*pEncoded != NULL) {
550 nReturn = 0;
551 goto Done;
552 }
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -0800553
554 // Use static buffer to avoid dependency on malloc()
555 if(*pEncodedLen > sizeof(spSimpleArrayBuffer)) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800556 goto Done;
557 }
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -0800558 *pEncoded = spSimpleArrayBuffer;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800559
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800560 } while(1);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800561
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -0800562Done:
563 return nReturn;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800564}
565
566
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800567/*
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800568 Some basic CBOR with map and array used in a lot of tests.
569 The map labels are all strings
570
Laurence Lundbladecc7da412020-12-27 00:09:07 -0800571 {
572 "first integer": 42,
573 "an array of two strings": [
574 "string1", "string2"
575 ],
576 "map in a map": {
577 "bytes 1": h'78787878',
578 "bytes 2": h'79797979',
579 "another int": 98,
580 "text 2": "lies, damn lies and statistics"
581 }
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +0900582 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800583 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800584static const uint8_t pValidMapEncoded[] = {
Laurence Lundbladecc7da412020-12-27 00:09:07 -0800585 0xa3, 0x6d, 0x66, 0x69, 0x72, 0x73, 0x74, 0x20,
586 0x69, 0x6e, 0x74, 0x65, 0x67, 0x65, 0x72, 0x18,
587 0x2a, 0x77, 0x61, 0x6e, 0x20, 0x61, 0x72, 0x72,
588 0x61, 0x79, 0x20, 0x6f, 0x66, 0x20, 0x74, 0x77,
589 0x6f, 0x20, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67,
590 0x73, 0x82, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e,
591 0x67, 0x31, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e,
592 0x67, 0x32, 0x6c, 0x6d, 0x61, 0x70, 0x20, 0x69,
593 0x6e, 0x20, 0x61, 0x20, 0x6d, 0x61, 0x70, 0xa4,
594 0x67, 0x62, 0x79, 0x74, 0x65, 0x73, 0x20, 0x31,
595 0x44, 0x78, 0x78, 0x78, 0x78, 0x67, 0x62, 0x79,
596 0x74, 0x65, 0x73, 0x20, 0x32, 0x44, 0x79, 0x79,
597 0x79, 0x79, 0x6b, 0x61, 0x6e, 0x6f, 0x74, 0x68,
598 0x65, 0x72, 0x20, 0x69, 0x6e, 0x74, 0x18, 0x62,
599 0x66, 0x74, 0x65, 0x78, 0x74, 0x20, 0x32, 0x78,
600 0x1e, 0x6c, 0x69, 0x65, 0x73, 0x2c, 0x20, 0x64,
601 0x61, 0x6d, 0x6e, 0x20, 0x6c, 0x69, 0x65, 0x73,
602 0x20, 0x61, 0x6e, 0x64, 0x20, 0x73, 0x74, 0x61,
603 0x74, 0x69, 0x73, 0x74, 0x69, 0x63, 0x73 };
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800604
Laurence Lundbladee2c893c2020-12-26 17:41:53 -0800605
606#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundblade2c1faf92020-06-26 22:43:56 -0700607// Same as above, but with indefinite lengths.
608static const uint8_t pValidMapIndefEncoded[] = {
Laurence Lundbladecc7da412020-12-27 00:09:07 -0800609 0xbf, 0x6d, 0x66, 0x69, 0x72, 0x73, 0x74, 0x20,
610 0x69, 0x6e, 0x74, 0x65, 0x67, 0x65, 0x72, 0x18,
611 0x2a, 0x77, 0x61, 0x6e, 0x20, 0x61, 0x72, 0x72,
612 0x61, 0x79, 0x20, 0x6f, 0x66, 0x20, 0x74, 0x77,
613 0x6f, 0x20, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67,
614 0x73, 0x9f, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e,
615 0x67, 0x31, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e,
616 0x67, 0x32, 0xff, 0x6c, 0x6d, 0x61, 0x70, 0x20,
617 0x69, 0x6e, 0x20, 0x61, 0x20, 0x6d, 0x61, 0x70,
618 0xbf, 0x67, 0x62, 0x79, 0x74, 0x65, 0x73, 0x20,
619 0x31, 0x44, 0x78, 0x78, 0x78, 0x78, 0x67, 0x62,
620 0x79, 0x74, 0x65, 0x73, 0x20, 0x32, 0x44, 0x79,
621 0x79, 0x79, 0x79, 0x6b, 0x61, 0x6e, 0x6f, 0x74,
622 0x68, 0x65, 0x72, 0x20, 0x69, 0x6e, 0x74, 0x18,
623 0x62, 0x66, 0x74, 0x65, 0x78, 0x74, 0x20, 0x32,
624 0x78, 0x1e, 0x6c, 0x69, 0x65, 0x73, 0x2c, 0x20,
625 0x64, 0x61, 0x6d, 0x6e, 0x20, 0x6c, 0x69, 0x65,
626 0x73, 0x20, 0x61, 0x6e, 0x64, 0x20, 0x73, 0x74,
627 0x61, 0x74, 0x69, 0x73, 0x74, 0x69, 0x63, 0x73,
628 0xff, 0xff};
Laurence Lundbladee2c893c2020-12-26 17:41:53 -0800629#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
Laurence Lundblade2c1faf92020-06-26 22:43:56 -0700630
631
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800632static int32_t ParseOrderedArray(const uint8_t *pEncoded,
Laurence Lundblade02625d42020-06-25 14:41:41 -0700633 size_t nLen,
634 int64_t *pInt1,
635 int64_t *pInt2,
636 const uint8_t **pBuf3,
637 size_t *pBuf3Len,
638 const uint8_t **pBuf4,
639 size_t *pBuf4Len)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800640{
641 QCBORDecodeContext DCtx;
642 QCBORItem Item;
643 int nReturn = -1; // assume error until success
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800644
Laurence Lundbladeee851742020-01-08 08:37:05 -0800645 QCBORDecode_Init(&DCtx,
646 (UsefulBufC){pEncoded, nLen},
647 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800648
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800649 // Make sure the first thing is a map
Laurence Lundblade9b334962020-08-27 10:55:53 -0700650 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
651 Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800652 goto Done;
Laurence Lundblade9b334962020-08-27 10:55:53 -0700653 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800654
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800655 // First integer
Laurence Lundblade9b334962020-08-27 10:55:53 -0700656 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
657 Item.uDataType != QCBOR_TYPE_INT64) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800658 goto Done;
Laurence Lundblade9b334962020-08-27 10:55:53 -0700659 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800660 *pInt1 = Item.val.int64;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800661
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800662 // Second integer
Laurence Lundblade9b334962020-08-27 10:55:53 -0700663 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
664 Item.uDataType != QCBOR_TYPE_INT64) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800665 goto Done;
Laurence Lundblade9b334962020-08-27 10:55:53 -0700666 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800667 *pInt2 = Item.val.int64;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800668
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800669 // First string
Laurence Lundblade9b334962020-08-27 10:55:53 -0700670 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
671 Item.uDataType != QCBOR_TYPE_BYTE_STRING) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800672 goto Done;
Laurence Lundblade9b334962020-08-27 10:55:53 -0700673 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800674 *pBuf3 = Item.val.string.ptr;
675 *pBuf3Len = Item.val.string.len;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800676
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800677 // Second string
Laurence Lundblade9b334962020-08-27 10:55:53 -0700678 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
679 Item.uDataType != QCBOR_TYPE_BYTE_STRING) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800680 goto Done;
Laurence Lundblade9b334962020-08-27 10:55:53 -0700681 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800682 *pBuf4 = Item.val.string.ptr;
683 *pBuf4Len = Item.val.string.len;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800684
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800685 nReturn = 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800686
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800687Done:
688 return(nReturn);
689}
690
691
692
Laurence Lundblade9e3651c2018-10-10 11:49:55 +0800693
Maxim Zhukovd538f0a2022-12-20 20:40:38 +0300694int32_t SimpleArrayTest(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800695{
696 uint8_t *pEncoded;
697 size_t nEncodedLen;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800698
Laurence Lundblade5e390822019-01-06 12:35:01 -0800699 int64_t i1=0, i2=0;
700 size_t i3=0, i4=0;
701 const uint8_t *s3= (uint8_t *)"";
702 const uint8_t *s4= (uint8_t *)"";
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800703
704
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800705 if(CreateSimpleArray(23, 6000, &pEncoded, &nEncodedLen) < 0) {
706 return(-1);
707 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800708
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800709 ParseOrderedArray(pEncoded, nEncodedLen, &i1, &i2, &s3, &i3, &s4, &i4);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800710
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800711 if(i1 != 23 ||
712 i2 != 6000 ||
713 i3 != 8 ||
714 i4 != 11 ||
Laurence Lundbladea44d5062018-10-17 18:45:12 +0530715 memcmp("galactic", s3, 8) !=0 ||
716 memcmp("haven token", s4, 11) !=0) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800717 return(-1);
718 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800719
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800720 return(0);
721}
722
723
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700724/*
725 [
726 0,
727 [],
728 [
729 [],
730 [
731 0
732 ],
733 {},
734 {
735 1: {},
736 2: {},
737 3: []
738 }
739 ]
740 ]
741 */
Laurence Lundbladecc7da412020-12-27 00:09:07 -0800742static uint8_t sEmpties[] = {
743 0x83, 0x00, 0x80, 0x84, 0x80, 0x81, 0x00, 0xa0,
744 0xa3, 0x01, 0xa0, 0x02, 0xa0, 0x03, 0x80};
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700745
Laurence Lundbladee2c893c2020-12-26 17:41:53 -0800746#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundblade02625d42020-06-25 14:41:41 -0700747/* Same as above, but with indefinte lengths */
Laurence Lundbladecc7da412020-12-27 00:09:07 -0800748static const uint8_t sEmptiesIndef[] = {
Laurence Lundblade02625d42020-06-25 14:41:41 -07007490x9F,
Laurence Lundblade2c1faf92020-06-26 22:43:56 -0700750 0x00,
751 0x9F,
752 0xFF,
753 0x9F,
754 0x9F,
755 0xFF,
756 0x9F,
757 0x00,
758 0xFF,
759 0xBF,
760 0xFF,
761 0xBF,
762 0x01,
763 0xBF,
764 0xFF,
765 0x02,
766 0xBF,
767 0xFF,
768 0x03,
769 0x9F,
770 0xFF,
771 0xFF,
772 0xFF,
Laurence Lundblade02625d42020-06-25 14:41:41 -0700773 0xFF};
Laurence Lundbladee2c893c2020-12-26 17:41:53 -0800774#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
Laurence Lundblade02625d42020-06-25 14:41:41 -0700775
776
777static int32_t CheckEmpties(UsefulBufC input, bool bCheckCounts)
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700778{
779 QCBORDecodeContext DCtx;
780 QCBORItem Item;
781
Laurence Lundbladeee851742020-01-08 08:37:05 -0800782 QCBORDecode_Init(&DCtx,
Laurence Lundblade02625d42020-06-25 14:41:41 -0700783 input,
Laurence Lundbladeee851742020-01-08 08:37:05 -0800784 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700785
786 // Array with 3 items
787 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
788 Item.uDataType != QCBOR_TYPE_ARRAY ||
789 Item.uNestingLevel != 0 ||
790 Item.uNextNestLevel != 1 ||
Laurence Lundblade02625d42020-06-25 14:41:41 -0700791 (bCheckCounts && Item.val.uCount != 3)) {
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700792 return -1;
793 }
794
795 // An integer 0
796 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
797 Item.uDataType != QCBOR_TYPE_INT64 ||
798 Item.uNestingLevel != 1 ||
799 Item.uNextNestLevel != 1 ||
800 Item.val.uint64 != 0) {
801 return -2;
802 }
803
804 // An empty array
805 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
806 Item.uDataType != QCBOR_TYPE_ARRAY ||
807 Item.uNestingLevel != 1 ||
808 Item.uNextNestLevel != 1 ||
Laurence Lundblade02625d42020-06-25 14:41:41 -0700809 (bCheckCounts && Item.val.uCount != 0)) {
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700810 return -3;
811 }
812
813 // An array with 4 items
814 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
815 Item.uDataType != QCBOR_TYPE_ARRAY ||
816 Item.uNestingLevel != 1 ||
817 Item.uNextNestLevel != 2 ||
Laurence Lundblade02625d42020-06-25 14:41:41 -0700818 (bCheckCounts && Item.val.uCount != 4)) {
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700819 return -4;
820 }
821
822 // An empty array
823 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
824 Item.uDataType != QCBOR_TYPE_ARRAY ||
825 Item.uNestingLevel != 2 ||
826 Item.uNextNestLevel != 2 ||
Laurence Lundblade02625d42020-06-25 14:41:41 -0700827 (bCheckCounts && Item.val.uCount != 0)) {
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700828 return -5;
829 }
830
831 // An array with 1 item
832 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
833 Item.uDataType != QCBOR_TYPE_ARRAY ||
834 Item.uNestingLevel != 2 ||
835 Item.uNextNestLevel != 3 ||
Laurence Lundblade02625d42020-06-25 14:41:41 -0700836 (bCheckCounts && Item.val.uCount != 1)) {
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700837 return -6;
838 }
839
840 // An integer 0
841 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
842 Item.uDataType != QCBOR_TYPE_INT64 ||
843 Item.uNestingLevel != 3 ||
844 Item.uNextNestLevel != 2 ||
845 Item.val.uint64 != 0) {
846 return -7;
847 }
848
849 // An empty map
850 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
851 Item.uDataType != QCBOR_TYPE_MAP ||
852 Item.uNestingLevel != 2 ||
853 Item.uNextNestLevel != 2 ||
Laurence Lundblade02625d42020-06-25 14:41:41 -0700854 (bCheckCounts && Item.val.uCount != 0)) {
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700855 return -8;
856 }
857
Laurence Lundblade5e87da62020-06-07 03:24:28 -0700858 // A map with 3 items
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700859 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
860 Item.uDataType != QCBOR_TYPE_MAP ||
861 Item.uNestingLevel != 2 ||
862 Item.uNextNestLevel != 3 ||
Laurence Lundblade02625d42020-06-25 14:41:41 -0700863 (bCheckCounts && Item.val.uCount != 3)) {
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700864 return -9;
865 }
866
867 // An empty map
868 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
869 Item.uDataType != QCBOR_TYPE_MAP ||
870 Item.uNestingLevel != 3 ||
871 Item.uNextNestLevel != 3 ||
Laurence Lundblade02625d42020-06-25 14:41:41 -0700872 (bCheckCounts && Item.val.uCount != 0)) {
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700873 return -10;
874 }
875
876 // An empty map
877 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
878 Item.uDataType != QCBOR_TYPE_MAP ||
879 Item.uNestingLevel != 3 ||
880 Item.uNextNestLevel != 3 ||
Laurence Lundblade02625d42020-06-25 14:41:41 -0700881 (bCheckCounts && Item.val.uCount != 0)) {
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700882 return -11;
883 }
884
885 // An empty array
886 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
887 Item.uDataType != QCBOR_TYPE_ARRAY ||
888 Item.uNestingLevel != 3 ||
889 Item.uNextNestLevel != 0 ||
Laurence Lundblade02625d42020-06-25 14:41:41 -0700890 (bCheckCounts && Item.val.uCount != 0)) {
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700891 return -12;
892 }
893
894 if(QCBORDecode_Finish(&DCtx) != QCBOR_SUCCESS) {
895 return -13;
896 }
Laurence Lundblade02625d42020-06-25 14:41:41 -0700897 return 0;
898}
899
900
Maxim Zhukovd538f0a2022-12-20 20:40:38 +0300901int32_t EmptyMapsAndArraysTest(void)
Laurence Lundblade02625d42020-06-25 14:41:41 -0700902{
903 int nResult;
904 nResult = CheckEmpties(UsefulBuf_FROM_BYTE_ARRAY_LITERAL(sEmpties),
905 true);
906 if(nResult) {
907 return nResult;
908 }
909
Laurence Lundbladee2c893c2020-12-26 17:41:53 -0800910#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundblade02625d42020-06-25 14:41:41 -0700911 nResult = CheckEmpties(UsefulBuf_FROM_BYTE_ARRAY_LITERAL(sEmptiesIndef),
912 false);
913
914 if(nResult) {
915 return nResult -100;
916 }
Laurence Lundbladee2c893c2020-12-26 17:41:53 -0800917#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
Laurence Lundblade9916b1b2019-09-07 22:33:25 -0700918
919 return 0;
920}
921
Laurence Lundblade4c532ca2021-02-18 21:31:49 -0700922
923static const uint8_t sEmptyMap[] = {
Michael Richardson87de9af2021-02-18 23:13:31 -0500924 0xA1, //# map(1)
925 0x02, //# unsigned(2)
926 0xA0, //# map(0)
927};
928
929int32_t ParseEmptyMapInMapTest(void)
930{
931 QCBORDecodeContext DCtx;
932 QCBORItem Item;
933 int nReturn = 0;
Laurence Lundblade4c532ca2021-02-18 21:31:49 -0700934 QCBORError uErr;
Michael Richardson87de9af2021-02-18 23:13:31 -0500935
936 QCBORDecode_Init(&DCtx,
937 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(sEmptyMap),
938 QCBOR_DECODE_MODE_NORMAL);
939
940 /* now open the first Map */
Laurence Lundblade4c532ca2021-02-18 21:31:49 -0700941 uErr = QCBORDecode_GetNext(&DCtx, &Item);
942 if(uErr != QCBOR_SUCCESS ||
Michael Richardson87de9af2021-02-18 23:13:31 -0500943 Item.uDataType != QCBOR_TYPE_MAP) {
944 nReturn = -3;
945 goto done;
946 }
947
948 if(QCBORDecode_GetNext(&DCtx, &Item) != 0) {
949 nReturn = -1;
950 goto done;
951 }
952 if(Item.uDataType != QCBOR_TYPE_MAP ||
953 Item.uNestingLevel != 1 ||
954 Item.label.int64 != 2) {
955 nReturn = -2;
956 goto done;
957 }
958
959 done:
960 return(nReturn);
961}
962
Laurence Lundblade4c532ca2021-02-18 21:31:49 -0700963
Michael Richardson87de9af2021-02-18 23:13:31 -0500964/* [[[[[[[[[[]]]]]]]]]] */
Laurence Lundbladecc7da412020-12-27 00:09:07 -0800965static const uint8_t spDeepArrays[] = {
966 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81,
967 0x81, 0x80};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800968
Maxim Zhukovd538f0a2022-12-20 20:40:38 +0300969int32_t ParseDeepArrayTest(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800970{
971 QCBORDecodeContext DCtx;
972 int nReturn = 0;
973 int i;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800974
Laurence Lundbladeee851742020-01-08 08:37:05 -0800975 QCBORDecode_Init(&DCtx,
976 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spDeepArrays),
977 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800978
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800979 for(i = 0; i < 10; i++) {
980 QCBORItem Item;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800981
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800982 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
983 Item.uDataType != QCBOR_TYPE_ARRAY ||
984 Item.uNestingLevel != i) {
985 nReturn = -1;
986 break;
987 }
988 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800989
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800990 return(nReturn);
991}
992
Laurence Lundbladecc7da412020-12-27 00:09:07 -0800993/* Big enough to test nesting to the depth of 24
994 [[[[[[[[[[[[[[[[[[[[[[[[[]]]]]]]]]]]]]]]]]]]]]]]]]
995 */
996static const uint8_t spTooDeepArrays[] = {
997 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81,
998 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81,
999 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81,
1000 0x80};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001001
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03001002int32_t ParseTooDeepArrayTest(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001003{
1004 QCBORDecodeContext DCtx;
1005 int nReturn = 0;
1006 int i;
1007 QCBORItem Item;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001008
1009
Laurence Lundbladeee851742020-01-08 08:37:05 -08001010 QCBORDecode_Init(&DCtx,
1011 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTooDeepArrays),
1012 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001013
Laurence Lundblade972e59c2018-11-11 15:57:23 +07001014 for(i = 0; i < QCBOR_MAX_ARRAY_NESTING1; i++) {
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001015
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001016 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
1017 Item.uDataType != QCBOR_TYPE_ARRAY ||
1018 Item.uNestingLevel != i) {
1019 nReturn = -1;
1020 break;
1021 }
1022 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001023
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001024 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_ARRAY_DECODE_NESTING_TOO_DEEP)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001025 nReturn = -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001026
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001027 return(nReturn);
1028}
1029
1030
1031
1032
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03001033int32_t ShortBufferParseTest(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001034{
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001035 int nResult = 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001036
Laurence Lundblade06350ea2020-01-27 19:32:40 -08001037 for(size_t nNum = sizeof(spExpectedEncodedInts)-1; nNum; nNum--) {
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001038 QCBORDecodeContext DCtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001039
Laurence Lundbladeee851742020-01-08 08:37:05 -08001040 QCBORDecode_Init(&DCtx,
1041 (UsefulBufC){spExpectedEncodedInts, nNum},
1042 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001043
Laurence Lundblade06350ea2020-01-27 19:32:40 -08001044 const int nErr = IntegerValuesParseTestInternal(&DCtx);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001045
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001046 if(nErr != QCBOR_ERR_HIT_END && nErr != QCBOR_ERR_NO_MORE_ITEMS) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001047 nResult = -1;
1048 goto Done;
1049 }
1050 }
1051Done:
1052 return nResult;
1053}
1054
1055
Laurence Lundblade9e3651c2018-10-10 11:49:55 +08001056
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03001057int32_t ShortBufferParseTest2(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001058{
1059 uint8_t *pEncoded;
1060 int nReturn;
1061 size_t nEncodedLen;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001062
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001063 int64_t i1, i2;
1064 size_t i3, i4;
1065 const uint8_t *s3, *s4;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001066
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001067 nReturn = 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001068
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001069 if(CreateSimpleArray(23, 6000, &pEncoded, &nEncodedLen) < 0) {
1070 return(-1);
1071 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001072
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001073 for(nEncodedLen--; nEncodedLen; nEncodedLen--) {
Laurence Lundblade9b334962020-08-27 10:55:53 -07001074 int nResult = ParseOrderedArray(pEncoded, (uint32_t)nEncodedLen, &i1,
1075 &i2, &s3, &i3, &s4, &i4);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001076 if(nResult == 0) {
1077 nReturn = -1;
1078 }
1079 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001080
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001081 return(nReturn);
1082}
1083
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301084/*
1085 Decode and thoroughly check a moderately complex
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001086 set of maps. Can be run in QCBOR_DECODE_MODE_NORMAL or in
1087 QCBOR_DECODE_MODE_MAP_STRINGS_ONLY.
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301088 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001089static int32_t ParseMapTest1(QCBORDecodeMode nMode)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001090{
1091 QCBORDecodeContext DCtx;
1092 QCBORItem Item;
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001093 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001094
Laurence Lundbladeee851742020-01-08 08:37:05 -08001095 QCBORDecode_Init(&DCtx,
1096 (UsefulBufC){pValidMapEncoded, sizeof(pValidMapEncoded)},
1097 nMode);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001098
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001099 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001100 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001101 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001102 if(Item.uDataType != QCBOR_TYPE_MAP ||
1103 Item.val.uCount != 3)
1104 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001105
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001106 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001107 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001108 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07001109
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001110 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001111 Item.uDataType != QCBOR_TYPE_INT64 ||
1112 Item.val.int64 != 42 ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301113 Item.uDataAlloc ||
1114 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001115 UsefulBufCompareToSZ(Item.label.string, "first integer")) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001116 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001117 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001118
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001119 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001120 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001121 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001122 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301123 Item.uDataAlloc ||
1124 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001125 UsefulBufCompareToSZ(Item.label.string, "an array of two strings") ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001126 Item.uDataType != QCBOR_TYPE_ARRAY ||
1127 Item.val.uCount != 2)
1128 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001129
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001130 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001131 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001132 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001133 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301134 Item.uDataAlloc ||
1135 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001136 UsefulBufCompareToSZ(Item.val.string, "string1")) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001137 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001138 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001139
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001140 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001141 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001142 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001143 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301144 Item.uDataAlloc ||
1145 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001146 UsefulBufCompareToSZ(Item.val.string, "string2")) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001147 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001148 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001149
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001150 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001151 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001152 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001153 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301154 Item.uDataAlloc ||
1155 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001156 UsefulBufCompareToSZ(Item.label.string, "map in a map") ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001157 Item.uDataType != QCBOR_TYPE_MAP ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001158 Item.val.uCount != 4) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001159 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001160 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001161
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001162 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001163 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001164 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001165 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001166 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("bytes 1"))||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001167 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301168 Item.uDataAlloc ||
1169 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001170 UsefulBufCompareToSZ(Item.val.string, "xxxx")) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001171 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001172 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001173
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001174 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001175 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001176 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001177 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001178 UsefulBufCompareToSZ(Item.label.string, "bytes 2") ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001179 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301180 Item.uDataAlloc ||
1181 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001182 UsefulBufCompareToSZ(Item.val.string, "yyyy")) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001183 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001184 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001185
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001186 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001187 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001188 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001189 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301190 Item.uDataAlloc ||
1191 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001192 UsefulBufCompareToSZ(Item.label.string, "another int") ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001193 Item.uDataType != QCBOR_TYPE_INT64 ||
1194 Item.val.int64 != 98)
1195 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001196
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001197 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001198 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001199 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001200 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001201 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("text 2"))||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001202 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301203 Item.uDataAlloc ||
1204 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001205 UsefulBufCompareToSZ(Item.val.string, "lies, damn lies and statistics")) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001206 return -1;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001207 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001208
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001209 return 0;
1210}
1211
1212
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001213/*
1214 Decode and thoroughly check a moderately complex
Laurence Lundbladed4cc1032020-10-12 04:19:47 -07001215 set of maps in the QCBOR_DECODE_MODE_MAP_AS_ARRAY mode.
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001216 */
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03001217int32_t ParseMapAsArrayTest(void)
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001218{
1219 QCBORDecodeContext DCtx;
1220 QCBORItem Item;
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001221 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001222
Laurence Lundbladeee851742020-01-08 08:37:05 -08001223 QCBORDecode_Init(&DCtx,
1224 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded),
1225 QCBOR_DECODE_MODE_MAP_AS_ARRAY);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001226
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001227 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001228 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001229 }
Laurence Lundbladed61cbf32018-12-09 11:42:21 -08001230 if(Item.uDataType != QCBOR_TYPE_MAP_AS_ARRAY ||
1231 Item.val.uCount != 6) {
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001232 return -1;
1233 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001234
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001235 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001236 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001237 }
1238 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1239 Item.uDataAlloc ||
1240 Item.uLabelAlloc ||
1241 Item.uLabelType != QCBOR_TYPE_NONE ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001242 UsefulBufCompareToSZ(Item.val.string, "first integer")) {
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001243 return -2;
1244 }
1245
1246 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001247 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001248 }
1249 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1250 Item.uDataType != QCBOR_TYPE_INT64 ||
1251 Item.val.int64 != 42 ||
1252 Item.uDataAlloc ||
1253 Item.uLabelAlloc) {
1254 return -3;
1255 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001256
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001257 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001258 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001259 }
1260 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1261 Item.uDataAlloc ||
1262 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001263 UsefulBufCompareToSZ(Item.val.string, "an array of two strings") ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001264 Item.uDataType != QCBOR_TYPE_TEXT_STRING) {
1265 return -4;
1266 }
1267
1268 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001269 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001270 }
1271 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1272 Item.uDataAlloc ||
1273 Item.uLabelAlloc ||
1274 Item.uDataType != QCBOR_TYPE_ARRAY ||
1275 Item.val.uCount != 2) {
1276 return -5;
1277 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001278
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001279 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001280 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001281 }
1282 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1283 Item.val.string.len != 7 ||
1284 Item.uDataAlloc ||
1285 Item.uLabelAlloc ||
1286 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("string1"))) {
1287 return -6;
1288 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001289
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001290 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001291 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001292 }
1293 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1294 Item.uDataAlloc ||
1295 Item.uLabelAlloc ||
1296 UsefulBuf_Compare(Item.val.string, UsefulBuf_FromSZ("string2"))) {
1297 return -7;
1298 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001299
1300
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001301 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001302 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001303 }
1304 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1305 Item.uDataAlloc ||
1306 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001307 UsefulBufCompareToSZ(Item.val.string, "map in a map")) {
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001308 return -8;
1309 }
1310
1311 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001312 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001313 }
1314 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1315 Item.uDataAlloc ||
1316 Item.uLabelAlloc ||
Laurence Lundbladed61cbf32018-12-09 11:42:21 -08001317 Item.uDataType != QCBOR_TYPE_MAP_AS_ARRAY ||
1318 Item.val.uCount != 8) {
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001319 return -9;
1320 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001321
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001322 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001323 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001324 }
1325 if(Item.uLabelType != QCBOR_TYPE_NONE ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001326 UsefulBufCompareToSZ(Item.val.string, "bytes 1") ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001327 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1328 Item.uDataAlloc ||
1329 Item.uLabelAlloc) {
1330 return -10;
1331 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001332
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001333 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001334 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001335 }
1336 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1337 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
1338 Item.uDataAlloc ||
1339 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001340 UsefulBufCompareToSZ(Item.val.string, "xxxx")) {
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001341 return -11;
1342 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001343
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001344 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001345 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001346 }
1347 if(Item.uLabelType != QCBOR_TYPE_NONE ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001348 UsefulBufCompareToSZ(Item.val.string, "bytes 2") ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001349 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1350 Item.uDataAlloc ||
1351 Item.uLabelAlloc) {
1352 return -12;
1353 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001354
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001355 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001356 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001357 }
1358 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1359 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
1360 Item.uDataAlloc ||
1361 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001362 UsefulBufCompareToSZ(Item.val.string, "yyyy")) {
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001363 return -13;
1364 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001365
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001366 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001367 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001368 }
1369 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1370 Item.uDataAlloc ||
1371 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001372 UsefulBufCompareToSZ(Item.val.string, "another int") ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001373 Item.uDataType != QCBOR_TYPE_TEXT_STRING) {
1374 return -14;
1375 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001376
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001377 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001378 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001379 }
1380 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1381 Item.uDataAlloc ||
1382 Item.uLabelAlloc ||
1383 Item.uDataType != QCBOR_TYPE_INT64 ||
1384 Item.val.int64 != 98) {
1385 return -15;
1386 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001387
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001388 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001389 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001390 }
1391 if(Item.uLabelType != QCBOR_TYPE_NONE ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001392 UsefulBufCompareToSZ(Item.val.string, "text 2") ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001393 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1394 Item.uDataAlloc ||
1395 Item.uLabelAlloc) {
1396 return -16;
1397 }
1398
1399 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001400 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001401 }
1402 if(Item.uLabelType != QCBOR_TYPE_NONE ||
1403 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
1404 Item.uDataAlloc ||
1405 Item.uLabelAlloc ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001406 UsefulBufCompareToSZ(Item.val.string, "lies, damn lies and statistics")) {
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001407 return -17;
1408 }
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07001409
1410
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001411 /*
1412 Test with map that nearly QCBOR_MAX_ITEMS_IN_ARRAY items in a
1413 map that when interpreted as an array will be too many. Test
1414 data just has the start of the map, not all the items in the map.
1415 */
1416 static const uint8_t pTooLargeMap[] = {0xb9, 0xff, 0xfd};
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07001417
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001418 QCBORDecode_Init(&DCtx,
1419 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pTooLargeMap),
1420 QCBOR_DECODE_MODE_MAP_AS_ARRAY);
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07001421
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001422 if((QCBOR_ERR_ARRAY_DECODE_TOO_LONG != QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001423 return -50;
1424 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001425
Laurence Lundbladed4cc1032020-10-12 04:19:47 -07001426 // TODO: test decoding of labels that are arrays or such
1427 // TODO: test spiffy decoding of QCBOR_DECODE_MODE_MAP_AS_ARRAY
1428
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001429 return 0;
1430}
1431
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001432
1433/*
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301434 Fully or partially decode pValidMapEncoded. When
1435 partially decoding check for the right error code.
1436 How much partial decoding depends on nLevel.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001437
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301438 The partial decodes test error conditions of
1439 incomplete encoded input.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001440
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301441 This could be combined with the above test
1442 and made prettier and maybe a little more
1443 thorough.
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001444 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001445static int32_t ExtraBytesTest(int nLevel)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001446{
1447 QCBORDecodeContext DCtx;
1448 QCBORItem Item;
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001449 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001450
Laurence Lundbladeee851742020-01-08 08:37:05 -08001451 QCBORDecode_Init(&DCtx,
1452 (UsefulBufC){pValidMapEncoded, sizeof(pValidMapEncoded)},
1453 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001454
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001455 if(nLevel < 1) {
1456 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_EXTRA_BYTES) {
1457 return -1;
1458 } else {
1459 return 0;
1460 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001461 }
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301462
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001463
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001464 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001465 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001466 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001467 if(Item.uDataType != QCBOR_TYPE_MAP ||
1468 Item.val.uCount != 3)
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001469 return -2;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001470
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001471 if(nLevel < 2) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001472 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
1473 return -3;
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001474 } else {
1475 return 0;
1476 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001477 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001478
1479
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001480 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001481 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001482 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001483 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001484 Item.uDataType != QCBOR_TYPE_INT64 ||
1485 Item.val.uCount != 42 ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001486 UsefulBufCompareToSZ(Item.label.string, "first integer")) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001487 return -4;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001488 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001489
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001490 if(nLevel < 3) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001491 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
1492 return -5;
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001493 } else {
1494 return 0;
1495 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001496 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001497
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001498 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001499 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001500 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001501 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001502 UsefulBufCompareToSZ(Item.label.string, "an array of two strings") ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001503 Item.uDataType != QCBOR_TYPE_ARRAY ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001504 Item.val.uCount != 2) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001505 return -6;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001506 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001507
1508
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001509 if(nLevel < 4) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001510 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
1511 return -7;
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001512 } else {
1513 return 0;
1514 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001515 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001516
1517
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001518 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001519 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001520 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001521 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001522 UsefulBufCompareToSZ(Item.val.string, "string1")) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001523 return -8;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001524 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001525
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001526 if(nLevel < 5) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001527 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
1528 return -9;
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001529 } else {
1530 return 0;
1531 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001532 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001533
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001534 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001535 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001536 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001537 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001538 UsefulBufCompareToSZ(Item.val.string, "string2")) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001539 return -10;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001540 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001541
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001542 if(nLevel < 6) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001543 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
1544 return -11;
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001545 } else {
1546 return 0;
1547 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001548 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001549
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001550 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001551 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001552 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001553 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001554 UsefulBufCompareToSZ(Item.label.string, "map in a map") ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001555 Item.uDataType != QCBOR_TYPE_MAP ||
1556 Item.val.uCount != 4)
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001557 return -12;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001558
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001559 if(nLevel < 7) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001560 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
1561 return -13;
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001562 } else {
1563 return 0;
1564 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001565 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001566
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001567 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001568 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001569 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001570 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001571 UsefulBufCompareToSZ(Item.label.string, "bytes 1") ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001572 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001573 UsefulBufCompareToSZ(Item.val.string, "xxxx")) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001574 return -14;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001575 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001576
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001577 if(nLevel < 8) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001578 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
1579 return -15;
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001580 } else {
1581 return 0;
1582 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001583 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001584
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001585 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001586 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001587 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001588 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001589 UsefulBufCompareToSZ(Item.label.string, "bytes 2") ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001590 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001591 UsefulBufCompareToSZ(Item.val.string, "yyyy")) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001592 return -16;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001593 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001594
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001595 if(nLevel < 9) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001596 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
1597 return -17;
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001598 } else {
1599 return 0;
1600 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001601 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001602
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001603 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001604 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001605 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001606 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001607 UsefulBufCompareToSZ(Item.label.string, "another int") ||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001608 Item.uDataType != QCBOR_TYPE_INT64 ||
1609 Item.val.int64 != 98)
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001610 return -18;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001611
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001612 if(nLevel < 10) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001613 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
1614 return -19;
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001615 } else {
1616 return 0;
1617 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001618 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001619
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001620 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001621 return (int32_t)nCBORError;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001622 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001623 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001624 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("text 2"))||
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001625 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07001626 UsefulBufCompareToSZ(Item.val.string, "lies, damn lies and statistics")) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001627 return -20;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09001628 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001629
Laurence Lundbladefab1b522018-10-19 13:40:52 +05301630 if(QCBORDecode_Finish(&DCtx)) {
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001631 return -21;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001632 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001633
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001634 return 0;
1635}
1636
1637
1638
Laurence Lundblade844bb5c2020-03-01 17:27:25 -08001639
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03001640int32_t ParseMapTest(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001641{
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001642 // Parse a moderatly complex map structure very thoroughly
1643 int32_t nResult = ParseMapTest1(QCBOR_DECODE_MODE_NORMAL);
1644 if(nResult) {
1645 return nResult;
1646 }
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08001647
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001648 // Again, but in strings-only mode. It should succeed since the input
1649 // map has only string labels.
1650 nResult = ParseMapTest1(QCBOR_DECODE_MODE_MAP_STRINGS_ONLY);
1651 if(nResult) {
1652 return nResult;
1653 }
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08001654
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001655 // Again, but try to finish the decoding before the end of the
1656 // input at 10 different place and see that the right error code
1657 // is returned.
1658 for(int i = 0; i < 10; i++) {
1659 nResult = ExtraBytesTest(i);
1660 if(nResult) {
1661 break;
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +08001662 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001663 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001664
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001665 return nResult;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001666}
1667
1668
Laurence Lundbladecc7da412020-12-27 00:09:07 -08001669/* The simple-values including some not well formed */
1670static const uint8_t spSimpleValues[] = {
1671 0x8a, 0xf4, 0xf5, 0xf6, 0xf7, 0xff, 0xe0, 0xf3,
1672 0xf8, 0x00, 0xf8, 0x13, 0xf8, 0x1f, 0xf8, 0x20,
1673 0xf8, 0xff};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001674
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03001675int32_t ParseSimpleTest(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001676{
1677 QCBORDecodeContext DCtx;
1678 QCBORItem Item;
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001679 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001680
1681
Laurence Lundbladeee851742020-01-08 08:37:05 -08001682 QCBORDecode_Init(&DCtx,
1683 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSimpleValues),
1684 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001685
1686
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001687 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001688 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001689 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
1690 Item.val.uCount != 10)
1691 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001692
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001693 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001694 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001695 if(Item.uDataType != QCBOR_TYPE_FALSE)
1696 return -1;
1697
1698 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001699 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001700 if(Item.uDataType != QCBOR_TYPE_TRUE)
1701 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001702
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001703 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001704 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001705 if(Item.uDataType != QCBOR_TYPE_NULL)
1706 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001707
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001708 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001709 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001710 if(Item.uDataType != QCBOR_TYPE_UNDEF)
1711 return -1;
1712
1713 // A break
Laurence Lundblade9e3651c2018-10-10 11:49:55 +08001714 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_BREAK)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001715 return -1;
1716
1717 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001718 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001719 if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 0)
1720 return -1;
1721
1722 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001723 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001724 if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 19)
1725 return -1;
1726
Laurence Lundblade077475f2019-04-26 09:06:33 -07001727 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_TYPE_7)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001728 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001729
Laurence Lundblade077475f2019-04-26 09:06:33 -07001730 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_TYPE_7)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001731 return -1;
1732
Laurence Lundblade077475f2019-04-26 09:06:33 -07001733 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_TYPE_7)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001734 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001735
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001736 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001737 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001738 if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 32)
1739 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001740
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001741 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07001742 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001743 if(Item.uDataType != QCBOR_TYPE_UKNOWN_SIMPLE || Item.val.uSimple != 255)
1744 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001745
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001746 return 0;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001747
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001748}
1749
1750
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03001751int32_t NotWellFormedTests(void)
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001752{
1753 // Loop over all the not-well-formed instance of CBOR
1754 // that are test vectors in not_well_formed_cbor.h
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08001755 const uint16_t nArraySize = C_ARRAY_COUNT(paNotWellFormedCBOR,
1756 struct someBinaryBytes);
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001757 for(uint16_t nIterate = 0; nIterate < nArraySize; nIterate++) {
1758 const struct someBinaryBytes *pBytes = &paNotWellFormedCBOR[nIterate];
1759 const UsefulBufC Input = (UsefulBufC){pBytes->p, pBytes->n};
1760
Laurence Lundblade37286c02022-09-03 10:05:02 -07001761 if(nIterate == 86) {
1762 nIterate = 86;
1763 }
1764
Laurence Lundbladeee851742020-01-08 08:37:05 -08001765 // Set up decoder context. String allocator needed for indefinite
1766 // string test cases
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001767 QCBORDecodeContext DCtx;
1768 QCBORDecode_Init(&DCtx, Input, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08001769#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001770 UsefulBuf_MAKE_STACK_UB(Pool, 100);
1771 QCBORDecode_SetMemPool(&DCtx, Pool, 0);
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08001772#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001773
1774 // Loop getting items until no more to get
Laurence Lundbladef71e1622020-08-06 18:52:13 -07001775 QCBORError uCBORError;
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001776 do {
1777 QCBORItem Item;
1778
Laurence Lundbladef71e1622020-08-06 18:52:13 -07001779 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
1780 } while(uCBORError == QCBOR_SUCCESS);
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001781
1782 // Every test vector must fail with
1783 // a not-well-formed error. If not
1784 // this test fails.
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001785 if(!QCBORDecode_IsNotWellFormedError(uCBORError) &&
Laurence Lundbladef71e1622020-08-06 18:52:13 -07001786 uCBORError != QCBOR_ERR_NO_MORE_ITEMS) {
Laurence Lundblade4bf6e712020-12-10 11:53:21 -08001787 /* Return index of failure and QCBOR error in the result */
1788 return (int32_t)(nIterate * 100 + uCBORError);
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001789 }
1790 }
1791 return 0;
1792}
1793
1794
Laurence Lundblade4bf6e712020-12-10 11:53:21 -08001795// TODO: add a test index and report it so it is eaier to figure out which test failed.
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001796struct FailInput {
Laurence Lundblade59289e52019-12-30 13:44:37 -08001797 UsefulBufC Input;
1798 QCBORError nError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001799};
1800
Laurence Lundblade59289e52019-12-30 13:44:37 -08001801
Laurence Lundbladecc7da412020-12-27 00:09:07 -08001802static int32_t ProcessFailures(const struct FailInput *pFailInputs, size_t nNumFails)
Laurence Lundblade59289e52019-12-30 13:44:37 -08001803{
Laurence Lundbladecc7da412020-12-27 00:09:07 -08001804 for(const struct FailInput *pF = pFailInputs; pF < pFailInputs + nNumFails; pF++) {
Laurence Lundblade59289e52019-12-30 13:44:37 -08001805 QCBORDecodeContext DCtx;
Laurence Lundblade4bf6e712020-12-10 11:53:21 -08001806 QCBORError uCBORError;
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08001807
Laurence Lundblade59289e52019-12-30 13:44:37 -08001808 QCBORDecode_Init(&DCtx, pF->Input, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08001809
1810#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
Laurence Lundblade4bf6e712020-12-10 11:53:21 -08001811 // Set up the decoding context including a memory pool so that
1812 // indefinite length items can be checked
Laurence Lundblade59289e52019-12-30 13:44:37 -08001813 UsefulBuf_MAKE_STACK_UB(Pool, 100);
Laurence Lundblade830fbf92020-05-31 17:22:33 -07001814
Laurence Lundblade4bf6e712020-12-10 11:53:21 -08001815 uCBORError = QCBORDecode_SetMemPool(&DCtx, Pool, 0);
1816 if(uCBORError) {
Laurence Lundblade59289e52019-12-30 13:44:37 -08001817 return -9;
1818 }
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08001819#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
1820
Laurence Lundblade37286c02022-09-03 10:05:02 -07001821 const size_t nIndexx = (size_t)(pF - pFailInputs);
1822 if(nIndexx == 8) {
1823 uCBORError = 9;
1824 }
1825
Laurence Lundbladecf41c522021-02-20 10:19:07 -07001826
Laurence Lundblade59289e52019-12-30 13:44:37 -08001827 // Iterate until there is an error of some sort error
1828 QCBORItem Item;
1829 do {
Laurence Lundblade02625d42020-06-25 14:41:41 -07001830 // 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
1836
Laurence Lundblade59289e52019-12-30 13:44:37 -08001837
1838 // Must get the expected error or the this test fails
1839 // The data and label type must also be QCBOR_TYPE_NONE
Laurence Lundblade4bf6e712020-12-10 11:53:21 -08001840 if(uCBORError != pF->nError ||
Laurence Lundblade59289e52019-12-30 13:44:37 -08001841 Item.uDataType != QCBOR_TYPE_NONE ||
1842 Item.uLabelType != QCBOR_TYPE_NONE) {
Laurence Lundblade06350ea2020-01-27 19:32:40 -08001843 // return index of CBOR + 100
Laurence Lundblade830fbf92020-05-31 17:22:33 -07001844 const size_t nIndex = (size_t)(pF - pFailInputs);
Laurence Lundblade4bf6e712020-12-10 11:53:21 -08001845 return (int32_t)(nIndex * 100 + uCBORError);
Laurence Lundblade59289e52019-12-30 13:44:37 -08001846 }
1847 }
1848
1849 return 0;
1850}
1851
1852
Laurence Lundbladecc7da412020-12-27 00:09:07 -08001853static const struct FailInput Failures[] = {
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001854 // Most of this is copied from not_well_formed.h. Here the error code
1855 // returned is also checked.
1856
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08001857#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001858 // Indefinite length strings must be closed off
1859 // An indefinite length byte string not closed off
1860 { {(uint8_t[]){0x5f, 0x41, 0x00}, 3}, QCBOR_ERR_HIT_END },
1861 // An indefinite length text string not closed off
1862 { {(uint8_t[]){0x7f, 0x61, 0x00}, 3}, QCBOR_ERR_HIT_END },
1863
1864
Laurence Lundblade2f467f92020-10-09 17:50:11 -07001865 // All the chunks in an indefinite length string must be of the type of
1866 // indefinite length string
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001867 // indefinite length byte string with text string chunk
1868 { {(uint8_t[]){0x5f, 0x61, 0x00, 0xff}, 4}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1869 // indefinite length text string with a byte string chunk
1870 { {(uint8_t[]){0x7f, 0x41, 0x00, 0xff}, 4}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1871 // indefinite length byte string with an positive integer chunk
1872 { {(uint8_t[]){0x5f, 0x00, 0xff}, 3}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1873 // indefinite length byte string with an negative integer chunk
1874 { {(uint8_t[]){0x5f, 0x21, 0xff}, 3}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1875 // indefinite length byte string with an array chunk
1876 { {(uint8_t[]){0x5f, 0x80, 0xff}, 3}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1877 // indefinite length byte string with an map chunk
1878 { {(uint8_t[]){0x5f, 0xa0, 0xff}, 3}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
Laurence Lundblade37286c02022-09-03 10:05:02 -07001879#ifndef QCBOR_DISABLE_TAGS
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001880 // indefinite length byte string with tagged integer chunk
1881 { {(uint8_t[]){0x5f, 0xc0, 0x00, 0xff}, 4}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
Laurence Lundblade37286c02022-09-03 10:05:02 -07001882#else
1883 // indefinite length byte string with tagged integer chunk
1884 { {(uint8_t[]){0x5f, 0xc0, 0x00, 0xff}, 4}, QCBOR_ERR_TAGS_DISABLED },
1885#endif /* QCBOR_DISABLE_TAGS */
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001886 // indefinite length byte string with an simple type chunk
1887 { {(uint8_t[]){0x5f, 0xe0, 0xff}, 3}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1888 { {(uint8_t[]){0x5f, 0x5f, 0x41, 0x00, 0xff, 0xff}, 6}, QCBOR_ERR_INDEFINITE_STRING_CHUNK},
1889 // indefinite length text string with indefinite string inside
1890 { {(uint8_t[]){0x7f, 0x7f, 0x61, 0x00, 0xff, 0xff}, 6}, QCBOR_ERR_INDEFINITE_STRING_CHUNK},
1891
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08001892#else /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
1893
1894 { {(uint8_t[]){0x5f, 0x41, 0x00}, 3}, QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED },
1895 // An indefinite length text string not closed off
1896 { {(uint8_t[]){0x7f, 0x61, 0x00}, 3}, QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED },
1897
1898
1899 // All the chunks in an indefinite length string must be of the type of
1900 // indefinite length string
1901 // indefinite length byte string with text string chunk
1902 { {(uint8_t[]){0x5f, 0x61, 0x00, 0xff}, 4}, QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED },
1903 // indefinite length text string with a byte string chunk
1904 { {(uint8_t[]){0x7f, 0x41, 0x00, 0xff}, 4}, QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED },
1905 // indefinite length byte string with an positive integer chunk
1906 { {(uint8_t[]){0x5f, 0x00, 0xff}, 3}, QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED },
1907 // indefinite length byte string with an negative integer chunk
1908 { {(uint8_t[]){0x5f, 0x21, 0xff}, 3}, QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED },
1909 // indefinite length byte string with an array chunk
1910 { {(uint8_t[]){0x5f, 0x80, 0xff}, 3}, QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED },
1911 // indefinite length byte string with an map chunk
1912 { {(uint8_t[]){0x5f, 0xa0, 0xff}, 3}, QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED },
1913 // indefinite length byte string with tagged integer chunk
1914 { {(uint8_t[]){0x5f, 0xc0, 0x00, 0xff}, 4}, QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED },
1915 // indefinite length byte string with an simple type chunk
1916 { {(uint8_t[]){0x5f, 0xe0, 0xff}, 3}, QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED },
1917 { {(uint8_t[]){0x5f, 0x5f, 0x41, 0x00, 0xff, 0xff}, 6}, QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED},
1918 // indefinite length text string with indefinite string inside
1919 { {(uint8_t[]){0x7f, 0x7f, 0x61, 0x00, 0xff, 0xff}, 6}, QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED},
1920#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
1921
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001922
1923 // Definte length maps and arrays must be closed by having the right number of items
1924 // A definte length array that is supposed to have 1 item, but has none
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001925 { {(uint8_t[]){0x81}, 1}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001926 // A definte length array that is supposed to have 2 items, but has only 1
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001927 { {(uint8_t[]){0x82, 0x00}, 2}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001928 // A definte length array that is supposed to have 511 items, but has only 1
1929 { {(uint8_t[]){0x9a, 0x01, 0xff, 0x00}, 4}, QCBOR_ERR_HIT_END },
1930 // A definte length map that is supposed to have 1 item, but has none
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001931 { {(uint8_t[]){0xa1}, 1}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001932 // A definte length map that is supposed to have s item, but has only 1
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001933 { {(uint8_t[]){0xa2, 0x01, 0x02}, 3}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001934
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08001935#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001936 // Indefinte length maps and arrays must be ended by a break
1937 // Indefinite length array with zero items and no break
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001938 { {(uint8_t[]){0x9f}, 1}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001939 // Indefinite length array with two items and no break
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001940 { {(uint8_t[]){0x9f, 0x01, 0x02}, 3}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001941 // Indefinite length map with zero items and no break
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001942 { {(uint8_t[]){0xbf}, 1}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001943 // Indefinite length map with two items and no break
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001944 { {(uint8_t[]){0xbf, 0x01, 0x02, 0x01, 0x02}, 5}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001945
1946
1947 // Nested maps and arrays must be closed off (some extra nested test vectors)
Laurence Lundblade642282a2020-06-23 12:00:33 -07001948 // Unclosed indefinite array containing a closed definite length array
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001949 { {(uint8_t[]){0x9f, 0x80, 0x00}, 3}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundblade642282a2020-06-23 12:00:33 -07001950 // Definite length array containing an unclosed indefinite length array
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001951 { {(uint8_t[]){0x81, 0x9f}, 2}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundblade2f467f92020-10-09 17:50:11 -07001952 // Unclosed indefinite map containing a closed definite length array
1953 { {(uint8_t[]){0xbf, 0x01, 0x80, 0x00, 0xa0}, 5}, QCBOR_ERR_NO_MORE_ITEMS },
1954 // Definite length map containing an unclosed indefinite length array
1955 { {(uint8_t[]){0xa1, 0x02, 0x9f}, 3}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001956 // Deeply nested definite length arrays with deepest one unclosed
Laurence Lundblade93d89472020-10-03 22:30:50 -07001957 { {(uint8_t[]){0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81}, 9}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001958 // Deeply nested indefinite length arrays with deepest one unclosed
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001959 { {(uint8_t[]){0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0xff, 0xff, 0xff, 0xff}, 9}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001960 // Mixed nesting with indefinite unclosed
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001961 { {(uint8_t[]){0x9f, 0x81, 0x9f, 0x81, 0x9f, 0x9f, 0xff, 0xff, 0xff}, 9}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001962 // Mixed nesting with definite unclosed
Laurence Lundbladeee851742020-01-08 08:37:05 -08001963 { {(uint8_t[]){0x9f, 0x82, 0x9f, 0x81, 0x9f, 0x9f, 0xff, 0xff, 0xff, 0xff}, 10}, QCBOR_ERR_BAD_BREAK },
Laurence Lundblade2f467f92020-10-09 17:50:11 -07001964 // Unclosed indefinite length map in definite length maps
1965 { {(uint8_t[]){0xa1, 0x01, 0xa2, 0x02, 0xbf, 0xff, 0x02, 0xbf}, 8},
1966 QCBOR_ERR_NO_MORE_ITEMS},
1967 // Unclosed definite length map in indefinite length maps
1968 { {(uint8_t[]){0xbf, 0x01, 0xbf, 0x02, 0xa1}, 5}, QCBOR_ERR_NO_MORE_ITEMS},
1969 // Unclosed indefinite length array in definite length maps
1970 { {(uint8_t[]){0xa1, 0x01, 0xa2, 0x02, 0x9f, 0xff, 0x02, 0x9f}, 8},
1971 QCBOR_ERR_NO_MORE_ITEMS},
1972 // Unclosed definite length array in indefinite length maps
1973 { {(uint8_t[]){0xbf, 0x01, 0xbf, 0x02, 0x81}, 5}, QCBOR_ERR_NO_MORE_ITEMS},
1974 // Unclosed indefinite length map in definite length arrays
1975 { {(uint8_t[]){0x81, 0x82, 0xbf, 0xff, 0xbf}, 5}, QCBOR_ERR_NO_MORE_ITEMS},
1976 // Unclosed definite length map in indefinite length arrays
1977 { {(uint8_t[]){0x9f, 0x9f, 0xa1}, 3}, QCBOR_ERR_NO_MORE_ITEMS},
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08001978#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001979
1980 // The "argument" for the data item is incomplete
1981 // Positive integer missing 1 byte argument
1982 { {(uint8_t[]){0x18}, 1}, QCBOR_ERR_HIT_END },
1983 // Positive integer missing 2 byte argument
1984 { {(uint8_t[]){0x19}, 1}, QCBOR_ERR_HIT_END },
1985 // Positive integer missing 4 byte argument
1986 { {(uint8_t[]){0x1a}, 1}, QCBOR_ERR_HIT_END },
1987 // Positive integer missing 8 byte argument
1988 { {(uint8_t[]){0x1b}, 1}, QCBOR_ERR_HIT_END },
1989 // Positive integer missing 1 byte of 2 byte argument
1990 { {(uint8_t[]){0x19, 0x01}, 2}, QCBOR_ERR_HIT_END },
1991 // Positive integer missing 2 bytes of 4 byte argument
1992 { {(uint8_t[]){0x1a, 0x01, 0x02}, 3}, QCBOR_ERR_HIT_END },
1993 // Positive integer missing 1 bytes of 7 byte argument
1994 { {(uint8_t[]){0x1b, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07}, 8}, QCBOR_ERR_HIT_END },
1995 // Negative integer missing 1 byte argument
1996 { {(uint8_t[]){0x38}, 1}, QCBOR_ERR_HIT_END },
1997 // Binary string missing 1 byte argument
1998 { {(uint8_t[]){0x58}, 1}, QCBOR_ERR_HIT_END },
1999 // Text string missing 1 byte argument
2000 { {(uint8_t[]){0x78}, 1}, QCBOR_ERR_HIT_END },
2001 // Array missing 1 byte argument
2002 { {(uint8_t[]){0x98}, 1}, QCBOR_ERR_HIT_END },
2003 // Map missing 1 byte argument
2004 { {(uint8_t[]){0xb8}, 1}, QCBOR_ERR_HIT_END },
2005 // Tag missing 1 byte argument
2006 { {(uint8_t[]){0xd8}, 1}, QCBOR_ERR_HIT_END },
2007 // Simple missing 1 byte argument
2008 { {(uint8_t[]){0xf8}, 1}, QCBOR_ERR_HIT_END },
Laurence Lundblade2f467f92020-10-09 17:50:11 -07002009 // half-precision with 1 byte argument
2010 { {(uint8_t[]){0xf9, 0x00}, 2}, QCBOR_ERR_HIT_END },
2011 // single-precision with 2 byte argument
2012 { {(uint8_t[]){0xfa, 0x00, 0x00}, 3}, QCBOR_ERR_HIT_END },
2013 // double-precision with 3 byte argument
2014 { {(uint8_t[]){0xfb, 0x00, 0x00, 0x00}, 4}, QCBOR_ERR_HIT_END },
2015
Laurence Lundblade37286c02022-09-03 10:05:02 -07002016#ifndef QCBOR_DISABLE_TAGS
Laurence Lundblade2f467f92020-10-09 17:50:11 -07002017 // Tag with no content
2018 { {(uint8_t[]){0xc0}, 1}, QCBOR_ERR_HIT_END },
Laurence Lundblade37286c02022-09-03 10:05:02 -07002019#else /* QCBOR_DISABLE_TAGS */
2020 { {(uint8_t[]){0xc0}, 1}, QCBOR_ERR_TAGS_DISABLED },
2021#endif /* QCBOR_DISABLE_TAGS */
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002022
2023 // Breaks must not occur in definite length arrays and maps
2024 // Array of length 1 with sole member replaced by a break
2025 { {(uint8_t[]){0x81, 0xff}, 2}, QCBOR_ERR_BAD_BREAK },
2026 // Array of length 2 with 2nd member replaced by a break
2027 { {(uint8_t[]){0x82, 0x00, 0xff}, 3}, QCBOR_ERR_BAD_BREAK },
2028 // Map of length 1 with sole member label replaced by a break
2029 { {(uint8_t[]){0xa1, 0xff}, 2}, QCBOR_ERR_BAD_BREAK },
2030 // Map of length 1 with sole member label replaced by break
Laurence Lundblade2f467f92020-10-09 17:50:11 -07002031 // Alternate representation that some decoders handle differently
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002032 { {(uint8_t[]){0xa1, 0xff, 0x00}, 3}, QCBOR_ERR_BAD_BREAK },
2033 // Array of length 1 with 2nd member value replaced by a break
2034 { {(uint8_t[]){0xa1, 0x00, 0xff}, 3}, QCBOR_ERR_BAD_BREAK },
2035 // Map of length 2 with 2nd member replaced by a break
2036 { {(uint8_t[]){0xa2, 0x00, 0x00, 0xff}, 4}, QCBOR_ERR_BAD_BREAK },
2037
2038
2039 // Breaks must not occur on their own out of an indefinite length data item
2040 // A bare break is not well formed
2041 { {(uint8_t[]){0xff}, 1}, QCBOR_ERR_BAD_BREAK },
2042 // A bare break after a zero length definite length array
2043 { {(uint8_t[]){0x80, 0xff}, 2}, QCBOR_ERR_BAD_BREAK },
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08002044#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002045 // A bare break after a zero length indefinite length map
2046 { {(uint8_t[]){0x9f, 0xff, 0xff}, 3}, QCBOR_ERR_BAD_BREAK },
Laurence Lundblade2f467f92020-10-09 17:50:11 -07002047 // A break inside a definite length array inside an indefenite length array
2048 { {(uint8_t[]){0x9f, 0x81, 0xff}, 3}, QCBOR_ERR_BAD_BREAK },
2049 // Complicated mixed nesting with break outside indefinite length array
2050 { {(uint8_t[]){0x9f, 0x82, 0x9f, 0x81, 0x9f, 0x9f, 0xff, 0xff, 0xff, 0xff}, 10}, QCBOR_ERR_BAD_BREAK },
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08002051#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002052
2053
2054 // Forbidden two byte encodings of simple types
2055 // Must use 0xe0 instead
2056 { {(uint8_t[]){0xf8, 0x00}, 2}, QCBOR_ERR_BAD_TYPE_7 },
2057 // Should use 0xe1 instead
2058 { {(uint8_t[]){0xf8, 0x01}, 2}, QCBOR_ERR_BAD_TYPE_7 },
2059 // Should use 0xe2 instead
2060 { {(uint8_t[]){0xf8, 0x02}, 2}, QCBOR_ERR_BAD_TYPE_7 },
2061 // Should use 0xe3 instead
2062 { {(uint8_t[]){0xf8, 0x03}, 2}, QCBOR_ERR_BAD_TYPE_7 },
2063 // Should use 0xe4 instead
2064 { {(uint8_t[]){0xf8, 0x04}, 2}, QCBOR_ERR_BAD_TYPE_7 },
2065 // Should use 0xe5 instead
2066 { {(uint8_t[]){0xf8, 0x05}, 2}, QCBOR_ERR_BAD_TYPE_7 },
2067 // Should use 0xe6 instead
2068 { {(uint8_t[]){0xf8, 0x06}, 2}, QCBOR_ERR_BAD_TYPE_7 },
2069 // Should use 0xe7 instead
2070 { {(uint8_t[]){0xf8, 0x07}, 2}, QCBOR_ERR_BAD_TYPE_7 },
2071 // Should use 0xe8 instead
2072 { {(uint8_t[]){0xf8, 0x08}, 2}, QCBOR_ERR_BAD_TYPE_7 },
2073 // Should use 0xe9 instead
2074 { {(uint8_t[]){0xf8, 0x09}, 2}, QCBOR_ERR_BAD_TYPE_7 },
2075 // Should use 0xea instead
2076 { {(uint8_t[]){0xf8, 0x0a}, 2}, QCBOR_ERR_BAD_TYPE_7 },
2077 // Should use 0xeb instead
2078 { {(uint8_t[]){0xf8, 0x0b}, 2}, QCBOR_ERR_BAD_TYPE_7 },
2079 // Should use 0xec instead
2080 { {(uint8_t[]){0xf8, 0x0c}, 2}, QCBOR_ERR_BAD_TYPE_7 },
2081 // Should use 0xed instead
2082 { {(uint8_t[]){0xf8, 0x0d}, 2}, QCBOR_ERR_BAD_TYPE_7 },
2083 // Should use 0xee instead
2084 { {(uint8_t[]){0xf8, 0x0e}, 2}, QCBOR_ERR_BAD_TYPE_7 },
2085 // Should use 0xef instead
2086 { {(uint8_t[]){0xf8, 0x0f}, 2}, QCBOR_ERR_BAD_TYPE_7 },
2087 // Should use 0xf0 instead
2088 { {(uint8_t[]){0xf8, 0x10}, 2}, QCBOR_ERR_BAD_TYPE_7 },
2089 // Should use 0xf1 instead
2090 { {(uint8_t[]){0xf8, 0x11}, 2}, QCBOR_ERR_BAD_TYPE_7 },
2091 // Should use 0xf2 instead
2092 { {(uint8_t[]){0xf8, 0x12}, 2}, QCBOR_ERR_BAD_TYPE_7 },
2093 // Must use 0xf3 instead
2094 { {(uint8_t[]){0xf8, 0x13}, 2}, QCBOR_ERR_BAD_TYPE_7 },
2095 // Must use 0xf4 instead
2096 { {(uint8_t[]){0xf8, 0x14}, 2}, QCBOR_ERR_BAD_TYPE_7 },
2097 // Must use 0xf5 instead
2098 { {(uint8_t[]){0xf8, 0x15}, 2}, QCBOR_ERR_BAD_TYPE_7 },
2099 // Must use 0xf6 instead
2100 { {(uint8_t[]){0xf8, 0x16}, 2}, QCBOR_ERR_BAD_TYPE_7 },
2101 // Must use 0xf7 instead
2102 { {(uint8_t[]){0xf8, 0x17}, 2}, QCBOR_ERR_BAD_TYPE_7 },
2103 // Must use 0xf8 instead
2104 { {(uint8_t[]){0xf8, 0x18}, 2}, QCBOR_ERR_BAD_TYPE_7 },
Laurence Lundblade2f467f92020-10-09 17:50:11 -07002105 // Reserved
2106 { {(uint8_t[]){0xf8, 0x1f}, 2}, QCBOR_ERR_BAD_TYPE_7 },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002107
2108 // Integers with additional info indefinite length
2109 // Positive integer with additional info indefinite length
2110 { {(uint8_t[]){0x1f}, 1}, QCBOR_ERR_BAD_INT },
2111 // Negative integer with additional info indefinite length
2112 { {(uint8_t[]){0x3f}, 1}, QCBOR_ERR_BAD_INT },
Laurence Lundblade37286c02022-09-03 10:05:02 -07002113
2114#ifndef QCBOR_DISABLE_TAGS
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002115 // CBOR tag with "argument" an indefinite length
2116 { {(uint8_t[]){0xdf, 0x00}, 2}, QCBOR_ERR_BAD_INT },
2117 // CBOR tag with "argument" an indefinite length alternate vector
2118 { {(uint8_t[]){0xdf}, 1}, QCBOR_ERR_BAD_INT },
Laurence Lundblade37286c02022-09-03 10:05:02 -07002119#else /* QCBOR_DISABLE_TAGS */
2120 { {(uint8_t[]){0xdf, 0x00}, 2}, QCBOR_ERR_TAGS_DISABLED },
2121 { {(uint8_t[]){0xdf}, 1}, QCBOR_ERR_TAGS_DISABLED },
2122#endif /* QCBOR_DISABLE_TAGS */
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002123
2124 // Missing bytes from a deterministic length string
2125 // A byte string is of length 1 without the 1 byte
2126 { {(uint8_t[]){0x41}, 1}, QCBOR_ERR_HIT_END },
2127 // A text string is of length 1 without the 1 byte
2128 { {(uint8_t[]){0x61}, 1}, QCBOR_ERR_HIT_END },
Laurence Lundblade4bf6e712020-12-10 11:53:21 -08002129
2130#if SIZE_MAX > 2147483647
Laurence Lundblade42272e42020-01-31 07:50:53 -08002131 // Byte string should have 2^32-15 bytes, but has one
2132 { {(uint8_t[]){0x5a, 0xff, 0xff, 0xff, 0xf0, 0x00}, 6}, QCBOR_ERR_HIT_END },
2133 // Byte string should have 2^32-15 bytes, but has one
2134 { {(uint8_t[]){0x7a, 0xff, 0xff, 0xff, 0xf0, 0x00}, 6}, QCBOR_ERR_HIT_END },
Laurence Lundblade2f467f92020-10-09 17:50:11 -07002135 // Byte string should have 2^64 bytes, but has 3
2136 { {(uint8_t[]){0x5b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
2137 0x01, 0x02, 0x03}, 6}, QCBOR_ERR_HIT_END },
2138 // Text string should have 2^64 bytes, but has 3
2139 { {(uint8_t[]){0x7b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
2140 0x01, 0x02, 0x03}, 6}, QCBOR_ERR_HIT_END },
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03002141#else
Laurence Lundblade4bf6e712020-12-10 11:53:21 -08002142 // Byte string should have 2^32-15 bytes, but has one
2143 { {(uint8_t[]){0x5a, 0x00, 0x00, 0xff, 0xf0, 0x00}, 6}, QCBOR_ERR_HIT_END },
2144 // Byte string should have 2^32-15 bytes, but has one
2145 { {(uint8_t[]){0x7a, 0x00, 0x00, 0xff, 0xf0, 0x00}, 6}, QCBOR_ERR_HIT_END },
2146 // Byte string should have 2^16 bytes, but has 3
2147 { {(uint8_t[]){0x5b, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0x01, 0x02, 0x03}, 6}, QCBOR_ERR_HIT_END },
2148 // Text string should have 2^64 bytes, but has 3
2149 { {(uint8_t[]){0x7b, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0x01, 0x02, 0x03}, 6}, QCBOR_ERR_HIT_END },
2150#endif
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002151
2152 // Use of unassigned additional information values
2153 // Major type positive integer with reserved value 28
2154 { {(uint8_t[]){0x1c}, 1}, QCBOR_ERR_UNSUPPORTED },
2155 // Major type positive integer with reserved value 29
2156 { {(uint8_t[]){0x1d}, 1}, QCBOR_ERR_UNSUPPORTED },
2157 // Major type positive integer with reserved value 30
2158 { {(uint8_t[]){0x1e}, 1}, QCBOR_ERR_UNSUPPORTED },
2159 // Major type negative integer with reserved value 28
2160 { {(uint8_t[]){0x3c}, 1}, QCBOR_ERR_UNSUPPORTED },
2161 // Major type negative integer with reserved value 29
2162 { {(uint8_t[]){0x3d}, 1}, QCBOR_ERR_UNSUPPORTED },
2163 // Major type negative integer with reserved value 30
2164 { {(uint8_t[]){0x3e}, 1}, QCBOR_ERR_UNSUPPORTED },
2165 // Major type byte string with reserved value 28 length
2166 { {(uint8_t[]){0x5c}, 1}, QCBOR_ERR_UNSUPPORTED },
2167 // Major type byte string with reserved value 29 length
2168 { {(uint8_t[]){0x5d}, 1}, QCBOR_ERR_UNSUPPORTED },
2169 // Major type byte string with reserved value 30 length
2170 { {(uint8_t[]){0x5e}, 1}, QCBOR_ERR_UNSUPPORTED },
2171 // Major type text string with reserved value 28 length
2172 { {(uint8_t[]){0x7c}, 1}, QCBOR_ERR_UNSUPPORTED },
2173 // Major type text string with reserved value 29 length
2174 { {(uint8_t[]){0x7d}, 1}, QCBOR_ERR_UNSUPPORTED },
2175 // Major type text string with reserved value 30 length
2176 { {(uint8_t[]){0x7e}, 1}, QCBOR_ERR_UNSUPPORTED },
2177 // Major type array with reserved value 28 length
2178 { {(uint8_t[]){0x9c}, 1}, QCBOR_ERR_UNSUPPORTED },
2179 // Major type array with reserved value 29 length
2180 { {(uint8_t[]){0x9d}, 1}, QCBOR_ERR_UNSUPPORTED },
2181 // Major type array with reserved value 30 length
2182 { {(uint8_t[]){0x9e}, 1}, QCBOR_ERR_UNSUPPORTED },
2183 // Major type map with reserved value 28 length
2184 { {(uint8_t[]){0xbc}, 1}, QCBOR_ERR_UNSUPPORTED },
2185 // Major type map with reserved value 29 length
2186 { {(uint8_t[]){0xbd}, 1}, QCBOR_ERR_UNSUPPORTED },
2187 // Major type map with reserved value 30 length
2188 { {(uint8_t[]){0xbe}, 1}, QCBOR_ERR_UNSUPPORTED },
2189 // Major type tag with reserved value 28 length
2190 { {(uint8_t[]){0xdc}, 1}, QCBOR_ERR_UNSUPPORTED },
2191 // Major type tag with reserved value 29 length
2192 { {(uint8_t[]){0xdd}, 1}, QCBOR_ERR_UNSUPPORTED },
2193 // Major type tag with reserved value 30 length
2194 { {(uint8_t[]){0xde}, 1}, QCBOR_ERR_UNSUPPORTED },
2195 // Major type simple with reserved value 28 length
2196 { {(uint8_t[]){0xfc}, 1}, QCBOR_ERR_UNSUPPORTED },
2197 // Major type simple with reserved value 29 length
2198 { {(uint8_t[]){0xfd}, 1}, QCBOR_ERR_UNSUPPORTED },
2199 // Major type simple with reserved value 30 length
2200 { {(uint8_t[]){0xfe}, 1}, QCBOR_ERR_UNSUPPORTED },
2201
2202
2203 // Maps must have an even number of data items (key & value)
2204 // Map with 1 item when it should have 2
2205 { {(uint8_t[]){0xa1, 0x00}, 2}, QCBOR_ERR_HIT_END },
2206 // Map with 3 item when it should have 4
2207 { {(uint8_t[]){0xa2, 0x00, 0x00, 0x00}, 2}, QCBOR_ERR_HIT_END },
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08002208#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002209 // Map with 1 item when it should have 2
2210 { {(uint8_t[]){0xbf, 0x00, 0xff}, 3}, QCBOR_ERR_BAD_BREAK },
2211 // Map with 3 item when it should have 4
2212 { {(uint8_t[]){0xbf, 0x00, 0x00, 0x00, 0xff}, 5}, QCBOR_ERR_BAD_BREAK },
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08002213#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002214
Laurence Lundblade37286c02022-09-03 10:05:02 -07002215#ifndef QCBOR_DISABLE_TAGS
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002216 // In addition to not-well-formed, some invalid CBOR
Laurence Lundbladeee851742020-01-08 08:37:05 -08002217 // Text-based date, with an integer
2218 { {(uint8_t[]){0xc0, 0x00}, 2}, QCBOR_ERR_BAD_OPT_TAG },
2219 // Epoch date, with an byte string
2220 { {(uint8_t[]){0xc1, 0x41, 0x33}, 3}, QCBOR_ERR_BAD_OPT_TAG },
2221 // tagged as both epoch and string dates
2222 { {(uint8_t[]){0xc1, 0xc0, 0x00}, 3}, QCBOR_ERR_BAD_OPT_TAG },
2223 // big num tagged an int, not a byte string
2224 { {(uint8_t[]){0xc2, 0x00}, 2}, QCBOR_ERR_BAD_OPT_TAG },
Laurence Lundblade37286c02022-09-03 10:05:02 -07002225#else /* QCBOR_DISABLE_TAGS */
2226 // Text-based date, with an integer
2227 { {(uint8_t[]){0xc0, 0x00}, 2}, QCBOR_ERR_TAGS_DISABLED },
2228 // Epoch date, with an byte string
2229 { {(uint8_t[]){0xc1, 0x41, 0x33}, 3}, QCBOR_ERR_TAGS_DISABLED },
2230 // tagged as both epoch and string dates
2231 { {(uint8_t[]){0xc1, 0xc0, 0x00}, 3}, QCBOR_ERR_TAGS_DISABLED },
2232 // big num tagged an int, not a byte string
2233 { {(uint8_t[]){0xc2, 0x00}, 2}, QCBOR_ERR_TAGS_DISABLED },
2234#endif /* QCBOR_DISABLE_TAGS */
2235
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002236};
2237
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03002238int32_t DecodeFailureTests(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002239{
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002240 int32_t nResult;
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002241
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08002242 nResult = ProcessFailures(Failures,C_ARRAY_COUNT(Failures,struct FailInput));
Laurence Lundblade59289e52019-12-30 13:44:37 -08002243 if(nResult) {
2244 return nResult;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002245 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002246
Laurence Lundblade3a6042e2019-06-28 19:58:04 -07002247 // Corrupt the UsefulInputBuf and see that
2248 // it reflected correctly for CBOR decoding
Laurence Lundbladee6f15112020-07-23 18:44:16 -07002249 QCBORDecodeContext DCtx;
2250 QCBORItem Item;
2251 QCBORError uQCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002252
Laurence Lundbladee6f15112020-07-23 18:44:16 -07002253 QCBORDecode_Init(&DCtx,
2254 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSimpleValues),
2255 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002256
Laurence Lundbladee6f15112020-07-23 18:44:16 -07002257 if((uQCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
2258 return (int32_t)uQCBORError;
2259 }
2260 if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.val.uCount != 10) {
2261 // This wasn't supposed to happen
2262 return -1;
2263 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002264
Laurence Lundbladee6f15112020-07-23 18:44:16 -07002265 DCtx.InBuf.magic = 0; // Reach in and corrupt the UsefulInputBuf
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002266
Laurence Lundbladee6f15112020-07-23 18:44:16 -07002267 uQCBORError = QCBORDecode_GetNext(&DCtx, &Item);
2268 if(uQCBORError != QCBOR_ERR_NO_MORE_ITEMS) {
2269 // Did not get back the error expected
2270 return -2;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002271 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002272
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002273
Laurence Lundblade98427e92020-09-28 21:33:23 -07002274 /*
2275 The max size of a string for QCBOR is SIZE_MAX - 4 so this
2276 tests here can be performed to see that the max length
2277 error check works correctly. See DecodeBytes(). If the max
2278 size was SIZE_MAX, it wouldn't be possible to test this.
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002279
Laurence Lundblade98427e92020-09-28 21:33:23 -07002280 This test will automatocally adapt the all CPU sizes
2281 through the use of SIZE_MAX.
2282 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002283
Laurence Lundblade8510f8c2020-12-01 11:31:16 -08002284 UsefulBuf_MAKE_STACK_UB( HeadBuf, QCBOR_HEAD_BUFFER_SIZE);
Laurence Lundblade98427e92020-09-28 21:33:23 -07002285 UsefulBufC EncodedHead;
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002286
Laurence Lundblade98427e92020-09-28 21:33:23 -07002287 // This makes a CBOR head with a text string that is very long
2288 // but doesn't fill in the bytes of the text string as that is
2289 // not needed to test this part of QCBOR.
2290 EncodedHead = QCBOREncode_EncodeHead(HeadBuf, CBOR_MAJOR_TYPE_TEXT_STRING, 0, SIZE_MAX);
2291
2292 QCBORDecode_Init(&DCtx, EncodedHead, QCBOR_DECODE_MODE_NORMAL);
2293
2294 if(QCBOR_ERR_STRING_TOO_LONG != QCBORDecode_GetNext(&DCtx, &Item)) {
2295 return -4;
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002296 }
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002297
Laurence Lundblade3a6042e2019-06-28 19:58:04 -07002298 return 0;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002299}
2300
2301
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002302/* Try all 256 values of the byte at nLen including recursing for
2303 each of the values to try values at nLen+1 ... up to nLenMax
2304 */
Laurence Lundblade06350ea2020-01-27 19:32:40 -08002305static void ComprehensiveInputRecurser(uint8_t *pBuf, size_t nLen, size_t nLenMax)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002306{
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002307 if(nLen >= nLenMax) {
2308 return;
2309 }
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08002310
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002311 for(int inputByte = 0; inputByte < 256; inputByte++) {
2312 // Set up the input
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002313 pBuf[nLen] = (uint8_t)inputByte;
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08002314 const UsefulBufC Input = {pBuf, nLen+1};
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08002315
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002316 // Get ready to parse
2317 QCBORDecodeContext DCtx;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002318 QCBORDecode_Init(&DCtx, Input, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002319
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002320 // Parse by getting the next item until an error occurs
2321 // Just about every possible decoder error can occur here
2322 // The goal of this test is not to check for the correct
2323 // error since that is not really possible. It is to
2324 // see that there is no crash on hostile input.
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002325 while(1) {
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002326 QCBORItem Item;
2327 QCBORError nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002328 if(nCBORError != QCBOR_SUCCESS) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002329 break;
2330 }
2331 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002332
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002333 ComprehensiveInputRecurser(pBuf, nLen+1, nLenMax);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002334 }
2335}
2336
2337
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03002338int32_t ComprehensiveInputTest(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002339{
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002340 // Size 2 tests 64K inputs and runs quickly
2341 uint8_t pBuf[2];
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002342
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002343 ComprehensiveInputRecurser(pBuf, 0, sizeof(pBuf));
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002344
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002345 return 0;
2346}
2347
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002348
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03002349int32_t BigComprehensiveInputTest(void)
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002350{
2351 // size 3 tests 16 million inputs and runs OK
2352 // in seconds on fast machines. Size 4 takes
2353 // 10+ minutes and 5 half a day on fast
2354 // machines. This test is kept separate from
2355 // the others so as to no slow down the use
2356 // of them as a very frequent regression.
2357 uint8_t pBuf[3]; //
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08002358
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002359 ComprehensiveInputRecurser(pBuf, 0, sizeof(pBuf));
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08002360
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002361 return 0;
2362}
2363
2364
Laurence Lundbladecc7da412020-12-27 00:09:07 -08002365static const uint8_t spDateTestInput[] = {
Laurence Lundblade16a207a2021-09-18 17:22:46 -07002366 /* 1. The valid date string "1985-04-12" */
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002367 0xc0, // tag for string date
2368 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002369
Laurence Lundblade16a207a2021-09-18 17:22:46 -07002370 /* 2. An invalid date string due to wrong tag content type */
Laurence Lundbladec7114722020-08-13 05:11:40 -07002371 0xc0, // tag for string date
2372 0x00, // Wrong type for a string date
2373
Laurence Lundblade16a207a2021-09-18 17:22:46 -07002374 /* 3. A valid epoch date, 1400000000; Tue, 13 May 2014 16:53:20 GMT */
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002375 0xc1, // tag for epoch date
2376 0x1a, 0x53, 0x72, 0x4E, 0x00, // Epoch date 1400000000; Tue, 13 May 2014 16:53:20 GMT
2377
Laurence Lundblade16a207a2021-09-18 17:22:46 -07002378 /* 4. An invalid epoch date due to wrong tag content type */
Laurence Lundbladec7114722020-08-13 05:11:40 -07002379 0xc1,
2380 0x62, 'h', 'i', // wrong type tagged
2381
Laurence Lundblade16a207a2021-09-18 17:22:46 -07002382 /* 5. Valid epoch date tag as content for a two other nested tags */
Laurence Lundblade99615302020-11-29 11:19:47 -08002383 // CBOR_TAG_ENC_AS_B64
Laurence Lundblade16a207a2021-09-18 17:22:46 -07002384 0xcf, 0xd8, 0x16, 0xc1, // Epoch date with extra tags
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002385 0x1a, 0x53, 0x72, 0x4E, 0x01,
2386
Laurence Lundblade16a207a2021-09-18 17:22:46 -07002387 /* 6. Epoch date with value to large to fit into int64 */
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002388 0xc1, // tag for epoch date
2389 0x1b, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, // Too large integer
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002390
Laurence Lundblade16a207a2021-09-18 17:22:46 -07002391 /* 7. Epoch date with single-precision value of 1.1. */
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002392 0xc1, // tag for epoch date
Laurence Lundblade3ed0bca2020-07-14 22:50:10 -07002393 0xfa, 0x3f, 0x8c, 0xcc, 0xcd, // single with value 1.1
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002394
Laurence Lundblade16a207a2021-09-18 17:22:46 -07002395 /* 8. Epoch date with too-large single precision float */
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002396 0xc1, // tag for epoch date
Laurence Lundbladea1ad8782019-11-08 00:12:11 -08002397 0xfa, 0x7f, 0x7f, 0xff, 0xff, // 3.4028234663852886e+38 too large
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002398
Laurence Lundblade16a207a2021-09-18 17:22:46 -07002399 /* 9. Epoch date with slightly too-large double precision value */
Laurence Lundbladea1ad8782019-11-08 00:12:11 -08002400 0xc1, // tag for epoch date
2401 0xfb, 0x43, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 9223372036854775808.000000 just barely too large
2402 //0xfa, 0x7f, 0x7f, 0xff, 0xff // 3.4028234663852886e+38 too large
2403
Laurence Lundblade16a207a2021-09-18 17:22:46 -07002404 /* 10. Epoch date with largest supported double precision value */
Laurence Lundbladea1ad8782019-11-08 00:12:11 -08002405 0xc1, // tag for epoch date
Laurence Lundbladec7114722020-08-13 05:11:40 -07002406 0xfb, 0x43, 0xdf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, // 9223372036854773760 largest supported
2407
Laurence Lundblade16a207a2021-09-18 17:22:46 -07002408 /* 11. Epoch date with single-precision NaN */
Laurence Lundbladec7114722020-08-13 05:11:40 -07002409 0xc1, // tag for epoch date
2410 0xfa, 0x7f, 0xc0, 0x00, 0x00, // Single-precision NaN
2411
Laurence Lundblade16a207a2021-09-18 17:22:46 -07002412 /* 12. Epoch date with double precision plus infinity */
Laurence Lundbladec7114722020-08-13 05:11:40 -07002413 0xc1,
2414 0xfb, 0x7f, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // +infinity
2415
Laurence Lundblade16a207a2021-09-18 17:22:46 -07002416 /* 13. Epoch date with half-precision negative infinity */
Laurence Lundbladec7114722020-08-13 05:11:40 -07002417 0xc1, // tag for epoch date
2418 0xf9, 0xfc, 0x00, // -Infinity
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002419};
2420
2421
Laurence Lundbladec7114722020-08-13 05:11:40 -07002422
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002423// have to check float expected only to within an epsilon
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07002424#ifndef QCBOR_DISABLE_FLOAT_HW_USE
Laurence Lundblade02fcf312020-07-17 02:49:46 -07002425static int CHECK_EXPECTED_DOUBLE(double val, double expected) {
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002426
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002427 double diff = val - expected;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002428
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002429 diff = fabs(diff);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002430
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002431 return diff > 0.0000001;
2432}
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07002433#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002434
2435
Laurence Lundblade37286c02022-09-03 10:05:02 -07002436/* Test date decoding using GetNext() */
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03002437int32_t DateParseTest(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002438{
2439 QCBORDecodeContext DCtx;
Laurence Lundbladec7114722020-08-13 05:11:40 -07002440 QCBORItem Item;
2441 QCBORError uError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002442
Laurence Lundbladeee851742020-01-08 08:37:05 -08002443 QCBORDecode_Init(&DCtx,
2444 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spDateTestInput),
2445 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002446
Laurence Lundblade16a207a2021-09-18 17:22:46 -07002447 /* 1. The valid date string "1985-04-12" */
Laurence Lundbladec7114722020-08-13 05:11:40 -07002448 if((uError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002449 return -1;
Laurence Lundbladec7114722020-08-13 05:11:40 -07002450 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002451 if(Item.uDataType != QCBOR_TYPE_DATE_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07002452 UsefulBufCompareToSZ(Item.val.dateString, "1985-04-12")){
Laurence Lundblade67bd5512018-11-02 21:44:06 +07002453 return -2;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002454 }
2455
Laurence Lundblade16a207a2021-09-18 17:22:46 -07002456 /* 2. An invalid date string due to wrong tag content type */
Laurence Lundbladec7114722020-08-13 05:11:40 -07002457 uError = QCBORDecode_GetNext(&DCtx, &Item);
2458 if(uError != QCBOR_ERR_BAD_OPT_TAG) {
Laurence Lundblade67bd5512018-11-02 21:44:06 +07002459 return -3;
Laurence Lundbladec7114722020-08-13 05:11:40 -07002460 }
2461
Laurence Lundblade16a207a2021-09-18 17:22:46 -07002462 /* 3. A valid epoch date, 1400000000; Tue, 13 May 2014 16:53:20 GMT */
2463 uError = QCBORDecode_GetNext(&DCtx, &Item);
2464 if(uError != QCBOR_SUCCESS) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002465 return -4;
2466 }
Laurence Lundblade16a207a2021-09-18 17:22:46 -07002467 if(uError == QCBOR_SUCCESS) {
2468 if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH ||
2469 Item.val.epochDate.nSeconds != 1400000000
Máté Tóth-Pálef5f07a2021-09-17 19:31:37 +02002470#ifndef USEFULBUF_DISABLE_ALL_FLOAT
Laurence Lundblade16a207a2021-09-18 17:22:46 -07002471 || Item.val.epochDate.fSecondsFraction != 0
Máté Tóth-Pálef5f07a2021-09-17 19:31:37 +02002472#endif /* USEFULBUF_DISABLE_ALL_FLOAT */
Laurence Lundblade16a207a2021-09-18 17:22:46 -07002473 ) {
2474 return -5;
2475 }
Laurence Lundbladec7114722020-08-13 05:11:40 -07002476 }
2477
Laurence Lundblade16a207a2021-09-18 17:22:46 -07002478 /* 4. An invalid epoch date due to wrong tag content type */
Laurence Lundbladec7114722020-08-13 05:11:40 -07002479 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_OPT_TAG) {
2480 return -6;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002481 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002482
Laurence Lundblade16a207a2021-09-18 17:22:46 -07002483 /* 5. Valid epoch date tag as content for a two other nested tags */
Laurence Lundblade99615302020-11-29 11:19:47 -08002484 // Epoch date wrapped in an CBOR_TAG_ENC_AS_B64 and an unknown tag.
2485 // The date is decoded and the two tags are returned. This is to
2486 // make sure the wrapping of epoch date in another tag works OK.
Laurence Lundbladec7114722020-08-13 05:11:40 -07002487 if((uError = QCBORDecode_GetNext(&DCtx, &Item))) {
2488 return -7;
2489 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002490 if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH ||
2491 Item.val.epochDate.nSeconds != 1400000001 ||
Máté Tóth-Pálef5f07a2021-09-17 19:31:37 +02002492#ifndef USEFULBUF_DISABLE_ALL_FLOAT
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002493 Item.val.epochDate.fSecondsFraction != 0 ||
Máté Tóth-Pálef5f07a2021-09-17 19:31:37 +02002494#endif /* USEFULBUF_DISABLE_ALL_FLOAT */
Laurence Lundblade99615302020-11-29 11:19:47 -08002495 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_ENC_AS_B64)) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002496 return -8;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002497 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002498
Laurence Lundblade16a207a2021-09-18 17:22:46 -07002499 /* 6. Epoch date with value to large to fit into int64 */
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002500 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_DATE_OVERFLOW) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002501 return -9;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002502 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002503
Laurence Lundblade16a207a2021-09-18 17:22:46 -07002504 /* 7. Epoch date with single-precision value of 1.1. */
2505 uError = QCBORDecode_GetNext(&DCtx, &Item);
2506 if(uError != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002507 return -10;
2508 }
Laurence Lundblade16a207a2021-09-18 17:22:46 -07002509 if(uError == QCBOR_SUCCESS) {
2510 if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH ||
2511 Item.val.epochDate.nSeconds != 1
2512#ifndef QCBOR_DISABLE_FLOAT_HW_USE
2513 || CHECK_EXPECTED_DOUBLE(Item.val.epochDate.fSecondsFraction, 0.1)
Máté Tóth-Pálef5f07a2021-09-17 19:31:37 +02002514#endif /* USEFULBUF_DISABLE_ALL_FLOAT */
Laurence Lundblade16a207a2021-09-18 17:22:46 -07002515 ) {
2516 return -11;
2517 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002518 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002519
Laurence Lundblade16a207a2021-09-18 17:22:46 -07002520 /* 8. Epoch date with too-large single-precision float */
2521 uError = QCBORDecode_GetNext(&DCtx, &Item);
2522 if(uError != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_DATE_OVERFLOW)) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002523 return -12;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002524 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002525
Laurence Lundblade16a207a2021-09-18 17:22:46 -07002526 /* 9. Epoch date with slightly too-large double-precision value */
2527 uError = QCBORDecode_GetNext(&DCtx, &Item);
2528 if(uError != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_DATE_OVERFLOW)) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002529 return -13;
Laurence Lundbladea1ad8782019-11-08 00:12:11 -08002530 }
2531
Laurence Lundblade16a207a2021-09-18 17:22:46 -07002532 /* 10. Epoch date with largest supported double-precision value */
2533 uError = QCBORDecode_GetNext(&DCtx, &Item);
2534 if(uError != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)) {
2535 return -14;
2536 }
2537 if(uError == QCBOR_SUCCESS) {
2538 if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH ||
2539 Item.val.epochDate.nSeconds != 9223372036854773760
2540#ifndef QCBOR_DISABLE_FLOAT_HW_USE
2541 || Item.val.epochDate.fSecondsFraction != 0.0
2542#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
2543 ) {
2544 return -14;
2545 }
2546 }
Laurence Lundbladec7114722020-08-13 05:11:40 -07002547
Laurence Lundblade16a207a2021-09-18 17:22:46 -07002548 /* 11. Epoch date with single-precision NaN */
2549 if(QCBORDecode_GetNext(&DCtx, &Item) != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_DATE_OVERFLOW)) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002550 return -15;
2551 }
2552
Laurence Lundblade16a207a2021-09-18 17:22:46 -07002553 /* 12. Epoch date with double-precision plus infinity */
2554 if(QCBORDecode_GetNext(&DCtx, &Item) != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_DATE_OVERFLOW)) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002555 return -16;
2556 }
2557
Laurence Lundblade16a207a2021-09-18 17:22:46 -07002558 /* 13. Epoch date with half-precision negative infinity */
2559 uError = QCBORDecode_GetNext(&DCtx, &Item);
2560 if(uError != FLOAT_ERR_CODE_NO_HALF_PREC_NO_FLOAT_HW(QCBOR_ERR_DATE_OVERFLOW)) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002561 return -17;
2562 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002563
2564 return 0;
2565}
2566
Laurence Lundblade37286c02022-09-03 10:05:02 -07002567
Laurence Lundblade4b270642020-08-14 12:53:07 -07002568/*
2569 Test cases covered here. Some items cover more than one of these.
2570 positive integer (zero counts as a positive integer)
2571 negative integer
2572 half-precision float
2573 single-precision float
2574 double-precision float
Laurence Lundbladec7114722020-08-13 05:11:40 -07002575
Laurence Lundblade4b270642020-08-14 12:53:07 -07002576 float Overflow error
2577 Wrong type error for epoch
2578 Wrong type error for date string
2579 float disabled error
2580 half-precision disabled error
2581 -Infinity
2582 Slightly too large integer
2583 Slightly too far from zero
Laurence Lundbladec7114722020-08-13 05:11:40 -07002584
Laurence Lundblade4b270642020-08-14 12:53:07 -07002585 Get epoch by int
2586 Get string by int
2587 Get epoch by string
2588 Get string by string
2589 Fail to get epoch by wrong int label
2590 Fail to get string by wrong string label
2591 Fail to get epoch by string because it is invalid
2592 Fail to get epoch by int because it is invalid
2593
2594 Untagged values
2595 */
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08002596static const uint8_t spSpiffyDateTestInput[] = {
Laurence Lundblade37286c02022-09-03 10:05:02 -07002597 0x87, // array of 7 items
2598
2599 0xa6, // Open a map for tests involving untagged items with labels.
2600
2601 // Untagged integer 0
2602 0x08,
2603 0x00,
2604
2605 // Utagged date string with string label y
2606 0x61, 0x79,
2607 0x6a, '2','0','8','5','-','0','4','-','1','2', // Untagged date string
2608
2609 // Untagged single-precision float with value 3.14 with string label x
2610 0x61, 0x78,
2611 0xFA, 0x40, 0x48, 0xF5, 0xC3,
2612
2613 // Untagged half-precision float with value -2
2614 0x09,
2615 0xF9, 0xC0, 0x00,
2616
2617 /* Untagged date-only date string */
2618 0x18, 0x63,
2619 0x6A, 0x31, 0x39, 0x38, 0x35, 0x2D, 0x30, 0x34, 0x2D, 0x31, 0x32, /* "1985-04-12" */
2620
2621 /* Untagged days-count epoch date */
2622 0x11,
2623 0x19, 0x0F, 0x9A, /* 3994 */
2624
2625 // End of map, back to array
2626
2627 0xa7, // Open map of tagged items with labels
2628
2629 0x00,
2630 0xc0, // tag for string date
2631 0x6a, '1','9','8','5','-','0','4','-','1','2', // Tagged date string
2632
2633
2634 0x01,
2635 0xda, 0x03, 0x03, 0x03, 0x03, // An additional tag
2636 0xc1, // tag for epoch date
2637 0x1a, 0x53, 0x72, 0x4E, 0x00, // Epoch date 1400000000; Tue, 13 May 2014 16:53:20 GMT
2638
2639 0x05,
2640 0xc1,
2641 0xfb, 0xc3, 0xdf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, // -9223372036854773760 largest negative
2642
2643
2644 0x07,
2645 0xc1, // tag for epoch date
2646 0xfb, 0x43, 0xdf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, // 9223372036854773760 largest supported
2647
2648 /* Tagged days-count epoch date */
2649 0x63, 0x53, 0x44, 0x45,
2650 0xD8, 0x64, /* tag(100) */
2651 0x39, 0x29, 0xB3, /* -10676 */
2652
2653 // Untagged -1000 with label z
2654 0x61, 0x7a,
2655 0xda, 0x01, 0x01, 0x01, 0x01, // An additional tag
2656 0x39, 0x03, 0xe7,
2657
2658 /* Tagged date-only date string */
2659 0x63, 0x53, 0x44, 0x53,
2660 0xD9, 0x03, 0xEC,
2661 0x6A, 0x31, 0x39, 0x38, 0x35, 0x2D, 0x30, 0x34, 0x2D, 0x31, 0x32, /* "1985-04-12" */
2662
2663 // End of map of tagged items
Laurence Lundblade4b270642020-08-14 12:53:07 -07002664
2665 0xc1,
2666 0xfb, 0xc3, 0xdf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, // -9.2233720368547748E+18, too negative
2667
Laurence Lundbladec7114722020-08-13 05:11:40 -07002668 0xc1, // tag for epoch date
Laurence Lundblade4b270642020-08-14 12:53:07 -07002669 0x1b, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, // Too-large integer
2670
2671 0xc1, // tag for epoch date
2672 0xf9, 0xfc, 0x00, // Half-precision -Infinity
2673
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07002674 // These two at the end because they are unrecoverable errors
2675 0xc1, // tag for epoch date
2676 0x80, // Erroneous empty array as content for date
2677
2678 0xc0, // tag for string date
2679 0xa0 // Erroneous empty map as content for date
Laurence Lundbladec7114722020-08-13 05:11:40 -07002680};
2681
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03002682int32_t SpiffyDateDecodeTest(void)
Laurence Lundbladec7114722020-08-13 05:11:40 -07002683{
2684 QCBORDecodeContext DC;
Laurence Lundblade4b270642020-08-14 12:53:07 -07002685 QCBORError uError;
Laurence Lundblade37286c02022-09-03 10:05:02 -07002686 int64_t nEpochDate3, nEpochDate5,
2687 nEpochDate4, nEpochDate6,
2688 nEpochDays2;
2689 UsefulBufC StringDate1, StringDate2, StringDays2;
Laurence Lundbladec7114722020-08-13 05:11:40 -07002690
2691 QCBORDecode_Init(&DC,
Laurence Lundblade4b270642020-08-14 12:53:07 -07002692 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSpiffyDateTestInput),
Laurence Lundbladec7114722020-08-13 05:11:40 -07002693 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade37286c02022-09-03 10:05:02 -07002694
2695 /* Items are in an array or map to test look up by label and other
2696 * that might not occur in isolated items. But it does make the
2697 * test a bit messy. */
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07002698 QCBORDecode_EnterArray(&DC, NULL);
Laurence Lundbladec7114722020-08-13 05:11:40 -07002699
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07002700 QCBORDecode_EnterMap(&DC, NULL);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002701
Laurence Lundblade4b270642020-08-14 12:53:07 -07002702 // A single-precision date
Laurence Lundblade9b334962020-08-27 10:55:53 -07002703 QCBORDecode_GetEpochDateInMapSZ(&DC, "x", QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
2704 &nEpochDate5);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002705 uError = QCBORDecode_GetAndResetError(&DC);
Laurence Lundblade16a207a2021-09-18 17:22:46 -07002706 if(uError != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002707 return 104;
2708 }
Laurence Lundblade16a207a2021-09-18 17:22:46 -07002709 if(uError == QCBOR_SUCCESS) {
2710 if(nEpochDate5 != 3) {
2711 return 103;
2712 }
2713 }
Laurence Lundbladec7114722020-08-13 05:11:40 -07002714
Laurence Lundblade9b334962020-08-27 10:55:53 -07002715 // A half-precision date with value -2 FFF
2716 QCBORDecode_GetEpochDateInMapN(&DC, 9, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
2717 &nEpochDate4);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002718 uError = QCBORDecode_GetAndResetError(&DC);
Laurence Lundblade16a207a2021-09-18 17:22:46 -07002719 if(uError != FLOAT_ERR_CODE_NO_HALF_PREC_NO_FLOAT_HW(QCBOR_SUCCESS)) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002720 return 106;
2721 }
Laurence Lundblade16a207a2021-09-18 17:22:46 -07002722 if(uError == QCBOR_SUCCESS) {
2723 if(nEpochDate4 != -2) {
2724 return 105;
2725 }
2726 }
Laurence Lundblade4b270642020-08-14 12:53:07 -07002727
2728 // Fail to get an epoch date by string label
Laurence Lundblade9b334962020-08-27 10:55:53 -07002729 QCBORDecode_GetEpochDateInMapSZ(&DC, "no-label",
2730 QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
2731 &nEpochDate6);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002732 uError = QCBORDecode_GetAndResetError(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002733 if(uError != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002734 return 107;
2735 }
2736
2737 // Fail to get an epoch date by integer label
Laurence Lundblade9b334962020-08-27 10:55:53 -07002738 QCBORDecode_GetEpochDateInMapN(&DC, 99999, QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
2739 &nEpochDate6);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002740 uError = QCBORDecode_GetAndResetError(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002741 if(uError != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002742 return 108;
2743 }
2744
2745 // Fail to get a string date by string label
Laurence Lundblade9b334962020-08-27 10:55:53 -07002746 QCBORDecode_GetDateStringInMapSZ(&DC, "no-label",
2747 QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
2748 &StringDate1);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002749 uError = QCBORDecode_GetAndResetError(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002750 if(uError != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002751 return 109;
2752 }
2753
2754 // Fail to get a string date by integer label
Laurence Lundblade9b334962020-08-27 10:55:53 -07002755 QCBORDecode_GetDateStringInMapN(&DC, 99999, QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
2756 &StringDate1);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002757 uError = QCBORDecode_GetAndResetError(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002758 if(uError != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002759 return 110;
2760 }
2761
2762 // The rest of these succeed even if float features are disabled
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002763
Laurence Lundblade37286c02022-09-03 10:05:02 -07002764
2765 // Untagged integer 0
2766 QCBORDecode_GetEpochDateInMapN(&DC, 8, QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
2767 &nEpochDate3);
2768 // Untagged date string
2769 QCBORDecode_GetDateStringInMapSZ(&DC, "y", QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
2770 &StringDate2);
2771
2772 QCBORDecode_GetDaysStringInMapN(&DC, 99, QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
2773 &StringDays2);
2774
2775 QCBORDecode_GetEpochDaysInMapN(&DC, 17, QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
2776 &nEpochDays2);
2777
2778 QCBORDecode_ExitMap(&DC);
2779 if(QCBORDecode_GetError(&DC) != QCBOR_SUCCESS) {
2780 return 3001;
2781 }
2782
2783 // The map of tagged items
2784 QCBORDecode_EnterMap(&DC, NULL);
2785
2786#ifndef QCBOR_DISABLE_TAGS
2787 int64_t nEpochDate2,
2788 nEpochDateFail,
2789 nEpochDate1400000000, nEpochDays1;
2790 UsefulBufC StringDays1;
2791 uint64_t uTag1, uTag2;
2792
2793 // Tagged date string
2794 QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
2795 &StringDate1);
2796
Laurence Lundblade4b270642020-08-14 12:53:07 -07002797 // Epoch date 1400000000; Tue, 13 May 2014 16:53:20 GMT
Laurence Lundblade9b334962020-08-27 10:55:53 -07002798 QCBORDecode_GetEpochDateInMapN(&DC,
2799 1,
2800 QCBOR_TAG_REQUIREMENT_TAG |
2801 QCBOR_TAG_REQUIREMENT_ALLOW_ADDITIONAL_TAGS,
2802 &nEpochDate1400000000);
2803 uTag1 = QCBORDecode_GetNthTagOfLast(&DC, 0);
Laurence Lundblade37286c02022-09-03 10:05:02 -07002804
2805 // Get largest negative double precision epoch date allowed
2806 QCBORDecode_GetEpochDateInMapN(&DC,
2807 5,
2808 QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG |
2809 QCBOR_TAG_REQUIREMENT_ALLOW_ADDITIONAL_TAGS,
2810 &nEpochDate2);
2811 uError = QCBORDecode_GetAndResetError(&DC);
2812 if(uError != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)) {
2813 return 102;
2814 }
2815 if(uError == QCBOR_SUCCESS) {
2816 if(nEpochDate2 != -9223372036854773760LL) {
2817 return 101;
2818 }
2819 }
2820
Laurence Lundblade4b270642020-08-14 12:53:07 -07002821 // Untagged -1000 with label z
Laurence Lundblade9b334962020-08-27 10:55:53 -07002822 QCBORDecode_GetEpochDateInMapSZ(&DC,
2823 "z",
2824 QCBOR_TAG_REQUIREMENT_NOT_A_TAG |
2825 QCBOR_TAG_REQUIREMENT_ALLOW_ADDITIONAL_TAGS,
2826 &nEpochDate6);
2827 uTag2 = QCBORDecode_GetNthTagOfLast(&DC, 0);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002828
Laurence Lundblade37286c02022-09-03 10:05:02 -07002829
2830 // Get largest double precision epoch date allowed
2831 QCBORDecode_GetEpochDateInMapN(&DC, 7, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
2832 &nEpochDate2);
2833 uError = QCBORDecode_GetAndResetError(&DC);
2834 if(uError != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)) {
2835 return 112;
2836 }
2837 if(uError == QCBOR_SUCCESS) {
2838 if(nEpochDate2 != 9223372036854773760ULL) {
2839 return 111;
2840 }
2841 }
2842
Laurence Lundblade46d63e92021-05-13 11:37:10 -07002843 /* The days format is much simpler than the date format
2844 * because it can't be a floating point value. The test
2845 * of the spiffy decode functions sufficiently covers
2846 * the test of the non-spiffy decode days date decoding.
2847 * There is no full fan out of the error conditions
2848 * and decode options as that is implemented by code
2849 * that is tested well by the date testing above.
2850 */
2851 QCBORDecode_GetDaysStringInMapSZ(&DC, "SDS", QCBOR_TAG_REQUIREMENT_TAG,
2852 &StringDays1);
2853
Laurence Lundblade46d63e92021-05-13 11:37:10 -07002854 QCBORDecode_GetEpochDaysInMapSZ(&DC, "SDE", QCBOR_TAG_REQUIREMENT_TAG,
2855 &nEpochDays1);
2856
Laurence Lundblade4b270642020-08-14 12:53:07 -07002857 QCBORDecode_ExitMap(&DC);
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07002858 if(QCBORDecode_GetError(&DC) != QCBOR_SUCCESS) {
2859 return 3001;
2860 }
2861
Laurence Lundblade37286c02022-09-03 10:05:02 -07002862 // Too-negative float, -9.2233720368547748E+18
2863 QCBORDecode_GetEpochDate(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nEpochDateFail);
2864 uError = QCBORDecode_GetAndResetError(&DC);
2865 if(uError != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_DATE_OVERFLOW)) {
2866 return 1111;
2867 }
2868
2869 // Too-large integer
2870 QCBORDecode_GetEpochDate(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nEpochDateFail);
2871 uError = QCBORDecode_GetAndResetError(&DC);
2872 if(uError != QCBOR_ERR_DATE_OVERFLOW) {
2873 return 1;
2874 }
2875
2876 // Half-precision minus infinity
2877 QCBORDecode_GetEpochDate(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nEpochDateFail);
2878 uError = QCBORDecode_GetAndResetError(&DC);
2879 if(uError != FLOAT_ERR_CODE_NO_HALF_PREC_NO_FLOAT_HW(QCBOR_ERR_DATE_OVERFLOW)) {
2880 return 2;
2881 }
2882
2883
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07002884 // Bad content for epoch date
2885 QCBORDecode_GetEpochDate(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nEpochDateFail);
2886 uError = QCBORDecode_GetAndResetError(&DC);
2887 if(uError != QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT) {
2888 return 3;
2889 }
2890
2891 // Bad content for string date
2892 QCBORDecode_GetDateString(&DC, QCBOR_TAG_REQUIREMENT_TAG, &StringDate1);
2893 uError = QCBORDecode_GetAndResetError(&DC);
2894 if(uError != QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT) {
2895 return 4;
2896 }
2897
Laurence Lundblade4b270642020-08-14 12:53:07 -07002898 QCBORDecode_ExitArray(&DC);
2899 uError = QCBORDecode_Finish(&DC);
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07002900 if(uError != QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT) {
Laurence Lundblade9b334962020-08-27 10:55:53 -07002901 return 1000 + (int32_t)uError;
Laurence Lundblade4b270642020-08-14 12:53:07 -07002902 }
Laurence Lundblade37286c02022-09-03 10:05:02 -07002903#else /* QCBOR_DISABLE_TAGS */
2904 QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
2905 &StringDate1);
2906 uError = QCBORDecode_GetAndResetError(&DC);
2907 if(uError != QCBOR_ERR_TAGS_DISABLED) {
2908 return 4;
2909 }
2910#endif /* QCBOR_DISABLE_TAGS */
2911
2912
2913#ifndef QCBOR_DISABLE_TAGS
Laurence Lundblade4b270642020-08-14 12:53:07 -07002914
Laurence Lundblade9b334962020-08-27 10:55:53 -07002915 if(nEpochDate1400000000 != 1400000000) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002916 return 200;
2917 }
2918
Laurence Lundblade9b334962020-08-27 10:55:53 -07002919 if(uTag1 != 0x03030303) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002920 return 201;
2921 }
2922
Laurence Lundblade37286c02022-09-03 10:05:02 -07002923 if(nEpochDays1 != -10676) {
2924 return 205;
Laurence Lundblade4b270642020-08-14 12:53:07 -07002925 }
2926
Laurence Lundblade37286c02022-09-03 10:05:02 -07002927 if(UsefulBuf_Compare(StringDays1, UsefulBuf_FromSZ("1985-04-12"))) {
2928 return 207;
Laurence Lundblade4b270642020-08-14 12:53:07 -07002929 }
2930
Laurence Lundblade9b334962020-08-27 10:55:53 -07002931 if(uTag2 != 0x01010101) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002932 return 204;
2933 }
2934
Laurence Lundblade37286c02022-09-03 10:05:02 -07002935 if(nEpochDate6 != -1000) {
2936 return 203;
Laurence Lundblade46d63e92021-05-13 11:37:10 -07002937 }
2938
Laurence Lundblade9b334962020-08-27 10:55:53 -07002939 if(UsefulBuf_Compare(StringDate1, UsefulBuf_FromSZ("1985-04-12"))) {
2940 return 205;
2941 }
2942
Laurence Lundblade37286c02022-09-03 10:05:02 -07002943#endif /* QCBOR_DISABLE_TAGS */
2944
2945 if(nEpochDate3 != 0) {
2946 return 202;
2947 }
2948
2949 if(nEpochDays2 != 3994) {
Laurence Lundblade9b334962020-08-27 10:55:53 -07002950 return 206;
2951 }
2952
Laurence Lundblade37286c02022-09-03 10:05:02 -07002953 if(UsefulBuf_Compare(StringDate2, UsefulBuf_FromSZ("2085-04-12"))) {
2954 return 206;
Laurence Lundblade46d63e92021-05-13 11:37:10 -07002955 }
2956
2957 if(UsefulBuf_Compare(StringDays2, UsefulBuf_FromSZ("1985-04-12"))) {
2958 return 208;
2959 }
2960
Laurence Lundbladec7114722020-08-13 05:11:40 -07002961 return 0;
2962}
2963
2964
Laurence Lundblade9b334962020-08-27 10:55:53 -07002965// Input for one of the tagging tests
Laurence Lundbladecc7da412020-12-27 00:09:07 -08002966static const uint8_t spTagInput[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002967 0xd9, 0xd9, 0xf7, // CBOR magic number
Laurence Lundblade9b334962020-08-27 10:55:53 -07002968 0x81, // Array of one
2969 0xd8, 0x04, // non-preferred serialization of tag 4, decimal fraction
2970 0x82, // Array of two that is the faction 1/3
2971 0x01,
2972 0x03,
2973
2974 /*
2975 More than 4 tags on an item 225(226(227(228(229([])))))
2976 */
2977 0xd8, 0xe1,
2978 0xd8, 0xe2,
2979 0xd8, 0xe3,
2980 0xd8, 0xe4,
2981 0xd8, 0xe5,
2982 0x80,
2983
2984 /* tag 10489608748473423768(
2985 2442302356(
2986 21590(
2987 240(
2988 []))))
2989 */
2990 0xdb, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98,
2991 0xda, 0x91, 0x92, 0x93, 0x94,
2992 0xd9, 0x54, 0x56,
2993 0xd8, 0xf0,
2994 0x80,
2995
2996 /* tag 21590(
2997 10489608748473423768(
2998 2442302357(
2999 65534(
3000 []))))
3001 */
3002 0xdb, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x54, 0x56,
3003 0xdb, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98,
3004 0xda, 0x91, 0x92, 0x93, 0x95,
3005 0xd9, 0xff, 0xfe,
3006 0x80,
3007
3008 /* Make sure to blow past the limit of tags that must be mapped.
3009 works in conjuntion with entries above.
3010 269488144(269488145(269488146(269488147([]))))
3011 */
3012 0xda, 0x10, 0x10, 0x10, 0x10,
3013 0xda, 0x10, 0x10, 0x10, 0x11,
3014 0xda, 0x10, 0x10, 0x10, 0x12,
3015 0xda, 0x10, 0x10, 0x10, 0x13,
3016 0x80,
3017
3018 /* An invalid decimal fraction with an additional tag */
3019 0xd9, 0xff, 0xfa,
3020 0xd8, 0x02, // non-preferred serialization of tag 2, a big num
3021 0x00, // the integer 0; should be a byte string
3022};
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003023
Laurence Lundblade59289e52019-12-30 13:44:37 -08003024/*
3025 DB 9192939495969798 # tag(10489608748473423768)
Laurence Lundblade9b334962020-08-27 10:55:53 -07003026 80 # array(0)
Laurence Lundblade59289e52019-12-30 13:44:37 -08003027 */
Laurence Lundbladecc7da412020-12-27 00:09:07 -08003028static const uint8_t spEncodedLargeTag[] = {0xdb, 0x91, 0x92, 0x93, 0x94, 0x95,
Laurence Lundbladeee851742020-01-08 08:37:05 -08003029 0x96, 0x97, 0x98, 0x80};
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003030
Laurence Lundblade59289e52019-12-30 13:44:37 -08003031/*
3032DB 9192939495969798 # tag(10489608748473423768)
3033 D8 88 # tag(136)
3034 C6 # tag(6)
3035 C7 # tag(7)
3036 80 # array(0)
3037*/
Laurence Lundbladecc7da412020-12-27 00:09:07 -08003038static const uint8_t spLotsOfTags[] = {0xdb, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96,
Laurence Lundbladeee851742020-01-08 08:37:05 -08003039 0x97, 0x98, 0xd8, 0x88, 0xc6, 0xc7, 0x80};
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003040
3041/*
Laurence Lundblade9b334962020-08-27 10:55:53 -07003042 55799(55799(55799({
3043 6(7(-23)): 5859837686836516696(7({
3044 7(-20): 11({
3045 17(-18): 17(17(17("Organization"))),
3046 9(-17): 773("SSG"),
3047 -15: 16(17(6(7("Confusion")))),
3048 17(-16): 17("San Diego"),
3049 17(-14): 17("US")
3050 }),
3051 23(-19): 19({
3052 -11: 9({
3053 -9: -7
3054 }),
3055 90599561(90599561(90599561(-10))): 12(h'0102030405060708090A')
3056 })
3057 })),
3058 16(-22): 23({
3059 11(8(7(-5))): 8(-3)
3060 })
3061 })))
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003062 */
Laurence Lundbladecc7da412020-12-27 00:09:07 -08003063static const uint8_t spCSRWithTags[] = {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003064 0xd9, 0xd9, 0xf7, 0xd9, 0xd9, 0xf7, 0xd9, 0xd9, 0xf7, 0xa2,
3065 0xc6, 0xc7, 0x36,
3066 0xdb, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0xc7, 0xa2,
3067 0xda, 0x00, 0x00, 0x00, 0x07, 0x33,
3068 0xcb, 0xa5,
3069 0xd1, 0x31,
3070 0xd1, 0xd1, 0xd1, 0x6c,
3071 0x4f, 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74, 0x69, 0x6f, 0x6e,
3072 0xc9, 0x30,
3073 0xd9, 0x03, 0x05, 0x63,
3074 0x53, 0x53, 0x47,
3075 0x2e,
Laurence Lundblade9b334962020-08-27 10:55:53 -07003076 0xd0, 0xd1, 0xc6, 0xc7,
3077 0x69,
3078 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73, 0x69, 0x6f, 0x6e,
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003079 0xd1, 0x2f,
3080 0xd1, 0x69,
3081 0x53, 0x61, 0x6e, 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f,
3082 0xd1, 0x2d,
3083 0xd1, 0x62,
3084 0x55, 0x53,
3085 0xd7, 0x32,
3086 0xd3, 0xa2,
3087 0x2a,
3088 0xc9, 0xa1,
3089 0x28,
3090 0x26,
3091 0xda, 0x05, 0x66, 0x70, 0x89, 0xda, 0x05, 0x66, 0x70, 0x89, 0xda, 0x05, 0x66, 0x70, 0x89, 0x29,
3092 0xcc, 0x4a,
3093 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,0x07, 0x08, 0x09, 0x0a,
3094 0xd0, 0x35,
3095 0xd7, 0xa1,
3096 0xcb, 0xc8, 0xc7, 0x24,
3097 0xc8, 0x22};
3098
Laurence Lundblade9b334962020-08-27 10:55:53 -07003099
Laurence Lundbladecc7da412020-12-27 00:09:07 -08003100static const uint8_t spSpiffyTagInput[] = {
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08003101 0x85, // Open array
Laurence Lundblade9b334962020-08-27 10:55:53 -07003102
3103 0xc0, // tag for string date
3104 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string
3105
3106 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string
3107
3108 0x4a, '1','9','8','5','-','0','4','-','1','2', // Date string in byte string
3109
3110 0xd8, 0x23, // tag for regex
3111 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string
3112
3113 0xc0, // tag for string date
3114 0x4a, '1','9','8','5','-','0','4','-','1','2', // Date string in byte string
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07003115
3116 // This last case makes the array untraversable because it is
3117 // an uncrecoverable error. Make sure it stays last and is the only
3118 // instance so the other tests can work.
Laurence Lundblade9b334962020-08-27 10:55:53 -07003119};
3120
3121
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003122static int32_t CheckCSRMaps(QCBORDecodeContext *pDC);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003123
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003124
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03003125int32_t OptTagParseTest(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003126{
3127 QCBORDecodeContext DCtx;
Laurence Lundblade9b334962020-08-27 10:55:53 -07003128 QCBORItem Item;
3129 QCBORError uError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003130
Laurence Lundbladeee851742020-01-08 08:37:05 -08003131 QCBORDecode_Init(&DCtx,
Laurence Lundblade9b334962020-08-27 10:55:53 -07003132 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTagInput),
Laurence Lundbladeee851742020-01-08 08:37:05 -08003133 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003134
Laurence Lundblade9b334962020-08-27 10:55:53 -07003135 /*
3136 This test matches the magic number tag and the fraction tag
3137 55799([...])
3138 */
3139 uError = QCBORDecode_GetNext(&DCtx, &Item);
3140 if(uError != QCBOR_SUCCESS) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003141 return -2;
3142 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003143 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003144 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC)) {
3145 return -3;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003146 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003147
Laurence Lundblade9b334962020-08-27 10:55:53 -07003148 /*
3149 4([1,3])
3150 */
3151 uError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundbladedd6e76e2021-03-10 01:54:01 -07003152#ifdef QCBOR_DISABLE_EXP_AND_MANTISSA
Laurence Lundblade9b334962020-08-27 10:55:53 -07003153 if(uError != QCBOR_SUCCESS ||
3154 Item.uDataType != QCBOR_TYPE_ARRAY ||
3155 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_DECIMAL_FRACTION) ||
3156 QCBORDecode_GetNthTag(&DCtx, &Item, 0) != CBOR_TAG_DECIMAL_FRACTION ||
3157 QCBORDecode_GetNthTag(&DCtx, &Item, 1) != CBOR_TAG_INVALID64 ||
3158 QCBORDecode_GetNthTag(&DCtx, &Item, 2) != CBOR_TAG_INVALID64 ||
3159 QCBORDecode_GetNthTag(&DCtx, &Item, 3) != CBOR_TAG_INVALID64 ||
3160 QCBORDecode_GetNthTag(&DCtx, &Item, 4) != CBOR_TAG_INVALID64 ||
3161 Item.val.uCount != 2) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003162 return -4;
3163 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07003164 // consume the items in the array
3165 uError = QCBORDecode_GetNext(&DCtx, &Item);
3166 uError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundblade59289e52019-12-30 13:44:37 -08003167
Laurence Lundbladedd6e76e2021-03-10 01:54:01 -07003168#else /* QCBOR_DISABLE_EXP_AND_MANTISSA */
Laurence Lundblade9b334962020-08-27 10:55:53 -07003169 if(uError != QCBOR_SUCCESS ||
3170 Item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION ||
3171 QCBORDecode_GetNthTag(&DCtx, &Item, 0) != CBOR_TAG_INVALID64 ||
3172 QCBORDecode_GetNthTag(&DCtx, &Item, 1) != CBOR_TAG_INVALID64 ||
3173 QCBORDecode_GetNthTag(&DCtx, &Item, 2) != CBOR_TAG_INVALID64 ||
3174 QCBORDecode_GetNthTag(&DCtx, &Item, 3) != CBOR_TAG_INVALID64 ||
3175 QCBORDecode_GetNthTag(&DCtx, &Item, 4) != CBOR_TAG_INVALID64 ) {
3176 return -5;
Laurence Lundblade59289e52019-12-30 13:44:37 -08003177 }
Laurence Lundbladedd6e76e2021-03-10 01:54:01 -07003178#endif /* QCBOR_DISABLE_EXP_AND_MANTISSA */
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003179
Laurence Lundblade9b334962020-08-27 10:55:53 -07003180 /*
3181 More than 4 tags on an item 225(226(227(228(229([])))))
3182 */
3183 uError = QCBORDecode_GetNext(&DCtx, &Item);
3184 if(uError != QCBOR_ERR_TOO_MANY_TAGS) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003185 return -6;
3186 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07003187
Laurence Lundblade88e9db22020-11-02 03:56:33 -08003188 if(QCBORDecode_GetNthTag(&DCtx, &Item, 0) != CBOR_TAG_INVALID64) {
3189 return -106;
3190 }
3191
3192
Laurence Lundblade9b334962020-08-27 10:55:53 -07003193 /* tag 10489608748473423768(
3194 2442302356(
3195 21590(
3196 240(
3197 []))))
3198 */
3199 uError = QCBORDecode_GetNext(&DCtx, &Item);
3200 if(uError != QCBOR_SUCCESS ||
3201 Item.uDataType != QCBOR_TYPE_ARRAY ||
3202 QCBORDecode_GetNthTag(&DCtx, &Item, 3) != 10489608748473423768ULL ||
3203 QCBORDecode_GetNthTag(&DCtx, &Item, 2) != 2442302356ULL ||
3204 QCBORDecode_GetNthTag(&DCtx, &Item, 1) != 21590ULL ||
3205 QCBORDecode_GetNthTag(&DCtx, &Item, 0) != 240ULL) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003206 return -7;
Laurence Lundblade9b334962020-08-27 10:55:53 -07003207 }
3208
3209 /* tag 21590(
3210 10489608748473423768(
3211 2442302357(
3212 21591(
3213 []))))
3214 */
3215 uError = QCBORDecode_GetNext(&DCtx, &Item);
3216 if(uError != QCBOR_SUCCESS ||
3217 Item.uDataType != QCBOR_TYPE_ARRAY ||
3218 QCBORDecode_GetNthTag(&DCtx, &Item, 0) != 65534ULL ||
3219 QCBORDecode_GetNthTag(&DCtx, &Item, 1) != 2442302357ULL ||
3220 QCBORDecode_GetNthTag(&DCtx, &Item, 2) != 10489608748473423768ULL ||
3221 QCBORDecode_GetNthTag(&DCtx, &Item, 3) != 21590ULL) {
3222 return -8;
3223 }
3224
3225 /* Make sure to blow past the limit of tags that must be mapped.
3226 works in conjuntion with entries above.
3227 269488144(269488145(269488146(269488147([]))))
3228 */
3229 uError = QCBORDecode_GetNext(&DCtx, &Item);
3230 if(uError != QCBOR_ERR_TOO_MANY_TAGS) {
3231 return -9;
3232 }
3233
3234 uError = QCBORDecode_GetNext(&DCtx, &Item);
3235 if(uError == QCBOR_SUCCESS) {
3236 return -10;
3237 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003238
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003239 // ----------------------------------
Laurence Lundbladeee851742020-01-08 08:37:05 -08003240 // This test sets up a caller-config list that includes the very large
Laurence Lundblade9b334962020-08-27 10:55:53 -07003241 // tage and then matches it. Caller-config lists are no longer
3242 // used or needed. This tests backwards compatibility with them.
Laurence Lundbladeee851742020-01-08 08:37:05 -08003243 QCBORDecode_Init(&DCtx,
3244 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEncodedLargeTag),
3245 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003246 const uint64_t puList[] = {0x9192939495969798, 257};
3247 const QCBORTagListIn TL = {2, puList};
3248 QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003249
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003250 if(QCBORDecode_GetNext(&DCtx, &Item)) {
3251 return -8;
3252 }
3253 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
3254 !QCBORDecode_IsTagged(&DCtx, &Item, 0x9192939495969798) ||
3255 QCBORDecode_IsTagged(&DCtx, &Item, 257) ||
3256 QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_BIGFLOAT) ||
3257 Item.val.uCount != 0) {
3258 return -9;
3259 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003260
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003261 //------------------------
Laurence Lundbladeee851742020-01-08 08:37:05 -08003262 // Sets up a caller-configured list and look up something not in it
Laurence Lundblade9b334962020-08-27 10:55:53 -07003263 // Another backwards compatibility test.
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003264 const uint64_t puLongList[17] = {1,2,1};
3265 const QCBORTagListIn TLLong = {17, puLongList};
Laurence Lundbladeee851742020-01-08 08:37:05 -08003266 QCBORDecode_Init(&DCtx,
3267 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEncodedLargeTag),
3268 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003269 QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TLLong);
3270 if(QCBORDecode_GetNext(&DCtx, &Item)) {
3271 return -11;
3272 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003273
Laurence Lundbladef7a70bc2020-10-24 12:23:25 -07003274 uint64_t puTags[4];
Laurence Lundblade9b334962020-08-27 10:55:53 -07003275 QCBORTagListOut Out = {0, 4, puTags};
3276
3277
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003278 // This tests retrievel of the full tag list
Laurence Lundbladeee851742020-01-08 08:37:05 -08003279 QCBORDecode_Init(&DCtx,
3280 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spLotsOfTags),
3281 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003282 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3283 return -12;
3284 }
3285 if(puTags[0] != 0x9192939495969798 ||
3286 puTags[1] != 0x88 ||
Laurence Lundblade59289e52019-12-30 13:44:37 -08003287 puTags[2] != 0x06 ||
3288 puTags[3] != 0x07) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003289 return -13;
3290 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003291
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003292 // ----------------------
Laurence Lundblade9b334962020-08-27 10:55:53 -07003293 // This tests too small of an out list
Laurence Lundbladeee851742020-01-08 08:37:05 -08003294 QCBORDecode_Init(&DCtx,
3295 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spLotsOfTags),
3296 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003297 QCBORTagListOut OutSmall = {0, 3, puTags};
3298 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &OutSmall) != QCBOR_ERR_TOO_MANY_TAGS) {
3299 return -14;
3300 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003301
Laurence Lundblade9b334962020-08-27 10:55:53 -07003302
3303
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003304 // ---------------
Laurence Lundblade9b334962020-08-27 10:55:53 -07003305 // Decode a version of the "CSR" that has had a ton of tags randomly inserted
3306 // It is a bit of a messy test and maybe could be improved, but
3307 // it is retained as a backwards compatibility check.
Laurence Lundbladeee851742020-01-08 08:37:05 -08003308 QCBORDecode_Init(&DCtx,
3309 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRWithTags),
3310 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003311 int n = CheckCSRMaps(&DCtx);
3312 if(n) {
3313 return n-2000;
3314 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003315
Laurence Lundblade59289e52019-12-30 13:44:37 -08003316 Out = (QCBORTagListOut){0, 16, puTags};
Laurence Lundbladeee851742020-01-08 08:37:05 -08003317 QCBORDecode_Init(&DCtx,
3318 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRWithTags),
3319 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003320
Laurence Lundblade9b334962020-08-27 10:55:53 -07003321 /* With the spiffy decode revision, this tag list is not used.
3322 It doesn't matter if a tag is in this list or not so some
3323 tests that couldn't process a tag because it isn't in this list
3324 now can process these unlisted tags. The tests have been
3325 adjusted for this. */
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003326 const uint64_t puTagList[] = {773, 1, 90599561};
3327 const QCBORTagListIn TagList = {3, puTagList};
3328 QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TagList);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003329
3330
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003331 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3332 return -100;
3333 }
3334 if(Item.uDataType != QCBOR_TYPE_MAP ||
3335 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC) ||
3336 QCBORDecode_IsTagged(&DCtx, &Item, 90599561) ||
3337 QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_DATE_EPOCH) ||
3338 Item.val.uCount != 2 ||
3339 puTags[0] != CBOR_TAG_CBOR_MAGIC ||
3340 puTags[1] != CBOR_TAG_CBOR_MAGIC ||
3341 puTags[2] != CBOR_TAG_CBOR_MAGIC ||
3342 Out.uNumUsed != 3) {
3343 return -101;
3344 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003345
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003346 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3347 return -102;
3348 }
3349 if(Item.uDataType != QCBOR_TYPE_MAP ||
3350 QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC) ||
3351 QCBORDecode_IsTagged(&DCtx, &Item, 6) ||
Laurence Lundbladef7a70bc2020-10-24 12:23:25 -07003352 !QCBORDecode_IsTagged(&DCtx, &Item, 7) ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003353 Item.val.uCount != 2 ||
3354 puTags[0] != 5859837686836516696 ||
3355 puTags[1] != 7 ||
3356 Out.uNumUsed != 2) {
3357 return -103;
3358 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003359
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003360 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3361 return -104;
3362 }
3363 if(Item.uDataType != QCBOR_TYPE_MAP ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003364 Item.val.uCount != 5 ||
3365 puTags[0] != 0x0b ||
3366 Out.uNumUsed != 1) {
3367 return -105;
3368 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003369
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003370 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3371 return -106;
3372 }
3373 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
3374 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_COSE_MAC0) ||
3375 Item.val.string.len != 12 ||
3376 puTags[0] != CBOR_TAG_COSE_MAC0 ||
3377 puTags[1] != CBOR_TAG_COSE_MAC0 ||
3378 puTags[2] != CBOR_TAG_COSE_MAC0 ||
3379 Out.uNumUsed != 3) {
3380 return -105;
3381 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003382
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003383 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3384 return -107;
3385 }
3386 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
3387 !QCBORDecode_IsTagged(&DCtx, &Item, 773) ||
3388 Item.val.string.len != 3 ||
3389 puTags[0] != 773 ||
3390 Out.uNumUsed != 1) {
3391 return -108;
3392 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003393
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003394 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3395 return -109;
3396 }
3397 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade59289e52019-12-30 13:44:37 -08003398 !QCBORDecode_IsTagged(&DCtx, &Item, 16) ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003399 Item.val.string.len != 9 ||
Laurence Lundblade59289e52019-12-30 13:44:37 -08003400 puTags[0] != 16 ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07003401 puTags[3] != 7 ||
3402 Out.uNumUsed != 4) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003403 return -110;
3404 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003405
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003406 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3407 return -111;
3408 }
3409 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
3410 !QCBORDecode_IsTagged(&DCtx, &Item, 17) ||
3411 Item.val.string.len != 9 ||
3412 puTags[0] != 17 ||
3413 Out.uNumUsed != 1) {
3414 return -112;
3415 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003416
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003417 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3418 return -111;
3419 }
3420 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
3421 !QCBORDecode_IsTagged(&DCtx, &Item, 17) ||
3422 Item.val.string.len != 2 ||
3423 puTags[0] != 17 ||
3424 Out.uNumUsed != 1) {
3425 return -112;
3426 }
3427
3428 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3429 return -113;
3430 }
3431 if(Item.uDataType != QCBOR_TYPE_MAP ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07003432 !QCBORDecode_IsTagged(&DCtx, &Item, 19) ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003433 Item.val.uCount != 2 ||
3434 puTags[0] != 19 ||
3435 Out.uNumUsed != 1) {
3436 return -114;
3437 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003438
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003439 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3440 return -115;
3441 }
3442 if(Item.uDataType != QCBOR_TYPE_MAP ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07003443 !QCBORDecode_IsTagged(&DCtx, &Item, 9) ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003444 Item.val.uCount != 1 ||
3445 puTags[0] != 9 ||
3446 Out.uNumUsed != 1) {
3447 return -116;
3448 }
3449
3450 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3451 return -116;
3452 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003453 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003454 Item.val.int64 != -7 ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003455 Out.uNumUsed != 0) {
3456 return -117;
3457 }
3458
3459 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3460 return -118;
3461 }
3462 if(Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
3463 Item.val.string.len != 10 ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003464 puTags[0] != 12 ||
3465 Out.uNumUsed != 1) {
3466 return -119;
3467 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003468
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003469 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3470 return -120;
3471 }
3472 if(Item.uDataType != QCBOR_TYPE_MAP ||
3473 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_ENC_AS_B16) ||
3474 Item.val.uCount != 1 ||
3475 puTags[0] != 0x17 ||
3476 Out.uNumUsed != 1) {
3477 return -121;
3478 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003479
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003480 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3481 return -122;
3482 }
3483 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07003484 !QCBORDecode_IsTagged(&DCtx, &Item, 8) ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003485 Item.val.int64 != -3 ||
3486 puTags[0] != 8 ||
3487 Out.uNumUsed != 1) {
3488 return -123;
3489 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003490
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003491 if(QCBORDecode_Finish(&DCtx)) {
3492 return -124;
3493 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07003494
3495 UsefulBufC DateString;
3496 QCBORDecode_Init(&DCtx,
3497 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSpiffyTagInput),
3498 QCBOR_DECODE_MODE_NORMAL);
3499
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07003500 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade9b334962020-08-27 10:55:53 -07003501 // tagged date string
3502 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString);
3503 // untagged date string
3504 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString);
3505 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_SUCCESS) {
3506 return 100;
3507 }
3508 // untagged byte string
3509 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString);
3510 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
3511 return 101;
3512 }
3513 // tagged regex
3514 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString);
3515 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
3516 return 102;
3517 }
3518 // tagged date string with a byte string
3519 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString);
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07003520 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT) {
Laurence Lundblade9b334962020-08-27 10:55:53 -07003521 return 103;
3522 }
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07003523 // The exit errors out because the last item, the date string with
3524 // bad content makes the array untraversable (the bad date string
3525 // could have tag content of an array or such that is not consumed
3526 // by the date decoding).
Laurence Lundblade9b334962020-08-27 10:55:53 -07003527 QCBORDecode_ExitArray(&DCtx);
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07003528 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT) {
Laurence Lundblade9b334962020-08-27 10:55:53 -07003529 return 104;
3530 }
3531
3532
3533 QCBORDecode_Init(&DCtx,
3534 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSpiffyTagInput),
3535 QCBOR_DECODE_MODE_NORMAL);
3536
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07003537 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade9b334962020-08-27 10:55:53 -07003538 // tagged date string
3539 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString);
3540 // untagged date string
3541 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString);
3542 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_SUCCESS) {
3543 return 200;
3544 }
3545 // untagged byte string
3546 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString);
3547 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
3548 return 201;
3549 }
3550 // tagged regex
3551 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString);
3552 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
3553 return 202;
3554 }
3555 // tagged date string with a byte string
3556 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString);
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07003557 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT) {
Laurence Lundblade9b334962020-08-27 10:55:53 -07003558 return 203;
3559 }
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07003560 // See comments above
Laurence Lundblade9b334962020-08-27 10:55:53 -07003561 QCBORDecode_ExitArray(&DCtx);
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07003562 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT) {
Laurence Lundblade9b334962020-08-27 10:55:53 -07003563 return 204;
3564 }
3565
3566 QCBORDecode_Init(&DCtx,
3567 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSpiffyTagInput),
3568 QCBOR_DECODE_MODE_NORMAL);
3569
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07003570 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade9b334962020-08-27 10:55:53 -07003571 // tagged date string
3572 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString);
3573 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
3574 return 300;
3575 }
3576 // untagged date string
3577 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString);
3578 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
3579 return 301;
3580 }
3581 // untagged byte string
3582 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString);
3583 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
3584 return 302;
3585 }
3586 // tagged regex
3587 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString);
3588 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
3589 return 303;
3590 }
3591 // tagged date string with a byte string
3592 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString);
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07003593 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT) {
Laurence Lundblade9b334962020-08-27 10:55:53 -07003594 return 304;
3595 }
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07003596 // See comments above
Laurence Lundblade9b334962020-08-27 10:55:53 -07003597 QCBORDecode_ExitArray(&DCtx);
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07003598 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT) {
Laurence Lundblade9b334962020-08-27 10:55:53 -07003599 return 305;
3600 }
3601
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003602 return 0;
3603}
3604
Laurence Lundblade37286c02022-09-03 10:05:02 -07003605/*
3606 * These are showing the big numbers converted to integers.
3607 * The tag numbers are not shown.
3608 *
3609 * [ 18446744073709551616,
3610 * -18446744073709551617,
3611 * {"BN+": 18446744073709551616,
3612 * 64: 18446744073709551616,
3613 * "BN-": -18446744073709551617,
3614 * -64: -18446744073709551617
3615 * }
3616 * ]
3617 */
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003618
Laurence Lundbladecc7da412020-12-27 00:09:07 -08003619static const uint8_t spBigNumInput[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003620 0x83,
3621 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3622 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3623 0xA4,
3624 0x63, 0x42, 0x4E, 0x2B,
3625 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3626 0x18, 0x40,
3627 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3628 0x63, 0x42, 0x4E, 0x2D,
3629 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3630 0x38, 0x3F,
3631 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
3632
Laurence Lundblade37286c02022-09-03 10:05:02 -07003633#ifndef QCBOR_DISABLE_TAGS
Laurence Lundbladecc7da412020-12-27 00:09:07 -08003634/* The expected big num */
3635static const uint8_t spBigNum[] = {
3636 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3637 0x00};
Laurence Lundblade37286c02022-09-03 10:05:02 -07003638#endif /* QCBOR_DISABLE_TAGS */
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003639
3640
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03003641int32_t BignumParseTest(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003642{
3643 QCBORDecodeContext DCtx;
3644 QCBORItem Item;
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07003645 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003646
Laurence Lundbladeee851742020-01-08 08:37:05 -08003647 QCBORDecode_Init(&DCtx,
3648 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNumInput),
3649 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003650
3651
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003652 //
3653 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
3654 return -1;
3655 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003656 return -2;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003657 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003658
Laurence Lundblade37286c02022-09-03 10:05:02 -07003659#ifndef QCBOR_DISABLE_TAGS
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003660 //
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003661 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003662 return -3;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003663 if(Item.uDataType != QCBOR_TYPE_POSBIGNUM ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003664 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003665 return -4;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003666 }
3667
3668 //
3669 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003670 return -5;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003671 if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003672 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003673 return -6;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003674 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003675
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003676 //
3677 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003678 return -7;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003679 if(Item.uDataType != QCBOR_TYPE_MAP) {
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003680 return -8;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003681 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003682
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003683 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003684 return -9;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003685 if(Item.uDataType != QCBOR_TYPE_POSBIGNUM ||
3686 Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003687 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003688 return -10;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003689 }
3690
3691 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003692 return -11;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003693 if(Item.uDataType != QCBOR_TYPE_POSBIGNUM ||
3694 Item.uLabelType != QCBOR_TYPE_INT64 ||
3695 Item.label.int64 != 64 ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003696 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003697 return -12;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003698 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003699
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003700 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003701 return -13;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003702 if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM ||
3703 Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003704 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003705 return -14;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003706 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003707
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003708 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003709 return -15;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003710 if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM ||
3711 Item.uLabelType != QCBOR_TYPE_INT64 ||
3712 Item.label.int64 != -64 ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003713 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003714 return -16;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003715 }
Laurence Lundblade37286c02022-09-03 10:05:02 -07003716#else
3717
3718 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_TAGS_DISABLED) {
3719 return -100;
3720 }
3721#endif /* QCBOR_DISABLE_TAGS */
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003722
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003723 return 0;
3724}
3725
3726
3727
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003728static int32_t CheckItemWithIntLabel(QCBORDecodeContext *pCtx,
Laurence Lundbladeee851742020-01-08 08:37:05 -08003729 uint8_t uDataType,
3730 uint8_t uNestingLevel,
3731 uint8_t uNextNest,
3732 int64_t nLabel,
3733 QCBORItem *pItem)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003734{
3735 QCBORItem Item;
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07003736 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003737
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003738 if((nCBORError = QCBORDecode_GetNext(pCtx, &Item))) return -1;
3739 if(Item.uDataType != uDataType) return -1;
3740 if(uNestingLevel > 0) {
Laurence Lundbladeee851742020-01-08 08:37:05 -08003741 if(Item.uLabelType != QCBOR_TYPE_INT64 &&
3742 Item.uLabelType != QCBOR_TYPE_UINT64) {
3743 return -1;
3744 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003745 if(Item.uLabelType == QCBOR_TYPE_INT64) {
3746 if(Item.label.int64 != nLabel) return -1;
3747 } else {
Laurence Lundblade570fab52018-10-13 18:28:27 +08003748 if(Item.label.uint64 != (uint64_t)nLabel) return -1;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003749 }
3750 }
3751 if(Item.uNestingLevel != uNestingLevel) return -1;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303752 if(Item.uNextNestLevel != uNextNest) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003753
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003754 if(pItem) {
3755 *pItem = Item;
3756 }
3757 return 0;
3758}
3759
3760
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003761// Same code checks definite and indefinite length versions of the map
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003762static int32_t CheckCSRMaps(QCBORDecodeContext *pDC)
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003763{
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303764 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 0, 1, 0, NULL)) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003765
Laurence Lundblade9b334962020-08-27 10:55:53 -07003766 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 1, 2, -23, NULL)) return -2;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003767
Laurence Lundblade9b334962020-08-27 10:55:53 -07003768 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 2, 3, -20, NULL)) return -3;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003769
Laurence Lundblade9b334962020-08-27 10:55:53 -07003770 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -18, NULL)) return -4;
3771 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -17, NULL)) return -5;
3772 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -15, NULL)) return -6;
3773 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -16, NULL)) return -7;
3774 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 2, -14, NULL)) return -8;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003775
Laurence Lundblade9b334962020-08-27 10:55:53 -07003776 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 2, 3, -19, NULL)) return -9;
3777 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 3, 4, -11, NULL)) return -10;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003778
Laurence Lundblade9b334962020-08-27 10:55:53 -07003779 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_INT64, 4, 3, -9, NULL)) return -11;
3780 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_BYTE_STRING, 3, 1, -10, NULL)) return -12;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003781
Laurence Lundblade9b334962020-08-27 10:55:53 -07003782 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 1, 2, -22, NULL)) return -13;
3783 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_INT64, 2, 0, -5, NULL)) return -14;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003784
Laurence Lundblade9b334962020-08-27 10:55:53 -07003785 if(QCBORDecode_Finish(pDC)) return -20;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003786
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003787 return 0;
3788}
3789
3790
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003791/*
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003792{
3793 -23: {
3794 -20: {
3795 -18: "Organization",
3796 -17: "SSG",
3797 -15: "Confusion",
3798 -16: "San Diego",
3799 -14: "US"
3800 },
3801 -19: {
3802 -11: {
3803 -9: -7
3804 },
3805 -10: '\u0001\u0002\u0003\u0004\u0005\u0006\a\b\t\n'
3806 }
3807 },
3808 -22: {
3809 -5: -3
3810 }
3811}
Laurence Lundbladecc7da412020-12-27 00:09:07 -08003812*/
3813static const uint8_t spCSRInput[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003814 0xa2, 0x36, 0xa2, 0x33, 0xa5, 0x31, 0x6c, 0x4f,
3815 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74,
3816 0x69, 0x6f, 0x6e, 0x30, 0x63, 0x53, 0x53, 0x47,
3817 0x2e, 0x69, 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73,
3818 0x69, 0x6f, 0x6e, 0x2f, 0x69, 0x53, 0x61, 0x6e,
3819 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f, 0x2d, 0x62,
3820 0x55, 0x53, 0x32, 0xa2, 0x2a, 0xa1, 0x28, 0x26,
3821 0x29, 0x4a, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
3822 0x07, 0x08, 0x09, 0x0a, 0x35, 0xa1, 0x24, 0x22};
3823
Laurence Lundbladecc7da412020-12-27 00:09:07 -08003824// Same map as above, but using indefinite lengths
3825static const uint8_t spCSRInputIndefLen[] = {
3826 0xbf, 0x36, 0xbf, 0x33, 0xbf, 0x31, 0x6c, 0x4f,
3827 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74,
3828 0x69, 0x6f, 0x6e, 0x30, 0x63, 0x53, 0x53, 0x47,
3829 0x2e, 0x69, 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73,
3830 0x69, 0x6f, 0x6e, 0x2f, 0x69, 0x53, 0x61, 0x6e,
3831 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f, 0x2d, 0x62,
3832 0x55, 0x53, 0xff, 0x32, 0xbf, 0x2a, 0xbf, 0x28,
3833 0x26, 0xff, 0x29, 0x4a, 0x01, 0x02, 0x03, 0x04,
3834 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0xff, 0xff,
3835 0x35, 0xbf, 0x24, 0x22, 0xff, 0xff};
3836
3837
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03003838int32_t NestedMapTest(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003839{
3840 QCBORDecodeContext DCtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003841
Laurence Lundbladeee851742020-01-08 08:37:05 -08003842 QCBORDecode_Init(&DCtx,
3843 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput),
3844 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003845
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003846 return CheckCSRMaps(&DCtx);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003847}
3848
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08003849
3850
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03003851int32_t StringDecoderModeFailTest(void)
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08003852{
3853 QCBORDecodeContext DCtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003854
Laurence Lundbladeee851742020-01-08 08:37:05 -08003855 QCBORDecode_Init(&DCtx,
3856 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput),
3857 QCBOR_DECODE_MODE_MAP_STRINGS_ONLY);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003858
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08003859 QCBORItem Item;
3860 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003861
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08003862 if(QCBORDecode_GetNext(&DCtx, &Item)) {
3863 return -1;
3864 }
3865 if(Item.uDataType != QCBOR_TYPE_MAP) {
3866 return -2;
3867 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003868
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08003869 nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
3870 if(nCBORError != QCBOR_ERR_MAP_LABEL_TYPE) {
3871 return -3;
3872 }
3873
3874 return 0;
3875}
3876
3877
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003878
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03003879int32_t NestedMapTestIndefLen(void)
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003880{
3881 QCBORDecodeContext DCtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003882
Laurence Lundbladeee851742020-01-08 08:37:05 -08003883 QCBORDecode_Init(&DCtx,
3884 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInputIndefLen),
3885 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003886
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003887 return CheckCSRMaps(&DCtx);
3888}
3889
3890
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003891
Laurence Lundblade17ede402018-10-13 11:43:07 +08003892static UsefulBufC make_nested_indefinite_arrays(int n, UsefulBuf Storage)
3893{
3894 UsefulOutBuf UOB;
3895 UsefulOutBuf_Init(&UOB, Storage);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003896
Laurence Lundblade17ede402018-10-13 11:43:07 +08003897 int i;
3898 for(i = 0; i < n; i++) {
3899 UsefulOutBuf_AppendByte(&UOB, 0x9f);
3900 }
3901
3902 for(i = 0; i < n; i++) {
3903 UsefulOutBuf_AppendByte(&UOB, 0xff);
3904 }
3905 return UsefulOutBuf_OutUBuf(&UOB);
3906}
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003907
3908
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003909static int32_t parse_indeflen_nested(UsefulBufC Nested, int nNestLevel)
Laurence Lundblade17ede402018-10-13 11:43:07 +08003910{
3911 QCBORDecodeContext DC;
3912 QCBORDecode_Init(&DC, Nested, 0);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003913
Laurence Lundblade17ede402018-10-13 11:43:07 +08003914 int j;
3915 for(j = 0; j < nNestLevel; j++) {
3916 QCBORItem Item;
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07003917 QCBORError nReturn = QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade17ede402018-10-13 11:43:07 +08003918 if(j >= QCBOR_MAX_ARRAY_NESTING) {
3919 // Should be in error
Laurence Lundbladea9489f82020-09-12 13:50:56 -07003920 if(nReturn != QCBOR_ERR_ARRAY_DECODE_NESTING_TOO_DEEP) {
Laurence Lundblade17ede402018-10-13 11:43:07 +08003921 return -4;
3922 } else {
3923 return 0; // Decoding doesn't recover after an error
3924 }
3925 } else {
3926 // Should be no error
3927 if(nReturn) {
3928 return -9; // Should not have got an error
3929 }
3930 }
3931 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
3932 return -7;
3933 }
3934 }
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07003935 QCBORError nReturn = QCBORDecode_Finish(&DC);
Laurence Lundblade17ede402018-10-13 11:43:07 +08003936 if(nReturn) {
3937 return -3;
3938 }
3939 return 0;
3940}
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003941
3942
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03003943int32_t IndefiniteLengthNestTest(void)
Laurence Lundblade17ede402018-10-13 11:43:07 +08003944{
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05303945 UsefulBuf_MAKE_STACK_UB(Storage, 50);
Laurence Lundblade17ede402018-10-13 11:43:07 +08003946 int i;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003947 for(i=1; i < QCBOR_MAX_ARRAY_NESTING+4; i++) {
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08003948 const UsefulBufC Nested = make_nested_indefinite_arrays(i, Storage);
Laurence Lundblade17ede402018-10-13 11:43:07 +08003949 int nReturn = parse_indeflen_nested(Nested, i);
3950 if(nReturn) {
3951 return nReturn;
3952 }
3953 }
3954 return 0;
3955}
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003956
Laurence Lundbladeee851742020-01-08 08:37:05 -08003957// [1, [2, 3]]
3958static const uint8_t spIndefiniteArray[] = {0x9f, 0x01, 0x82, 0x02, 0x03, 0xff};
3959// No closing break
3960static const uint8_t spIndefiniteArrayBad1[] = {0x9f};
3961// Not enough closing breaks
3962static const uint8_t spIndefiniteArrayBad2[] = {0x9f, 0x9f, 0x02, 0xff};
3963// Too many closing breaks
3964static const uint8_t spIndefiniteArrayBad3[] = {0x9f, 0x02, 0xff, 0xff};
3965// Unclosed indeflen inside def len
3966static const uint8_t spIndefiniteArrayBad4[] = {0x81, 0x9f};
3967// confused tag
3968static const uint8_t spIndefiniteArrayBad5[] = {0x9f, 0xd1, 0xff};
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003969
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03003970int32_t IndefiniteLengthArrayMapTest(void)
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003971{
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07003972 QCBORError nResult;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003973 // --- first test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003974 UsefulBufC IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArray);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003975
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003976 // Decode it and see if it is OK
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003977 QCBORDecodeContext DC;
3978 QCBORItem Item;
3979 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003980
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003981 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05303982
3983 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
3984 Item.uNestingLevel != 0 ||
3985 Item.uNextNestLevel != 1) {
3986 return -111;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003987 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003988
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003989 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05303990 if(Item.uDataType != QCBOR_TYPE_INT64 ||
3991 Item.uNestingLevel != 1 ||
3992 Item.uNextNestLevel != 1) {
3993 return -2;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003994 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003995
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003996 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05303997 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
3998 Item.uNestingLevel != 1 ||
3999 Item.uNextNestLevel != 2) {
4000 return -3;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08004001 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004002
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08004003 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade12b495d2018-12-17 11:15:54 -08004004 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade6de37062018-10-15 12:22:42 +05304005 Item.uNestingLevel != 2 ||
4006 Item.uNextNestLevel != 2) {
4007 return -4;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08004008 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004009
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08004010 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade12b495d2018-12-17 11:15:54 -08004011 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade6de37062018-10-15 12:22:42 +05304012 Item.uNestingLevel != 2 ||
4013 Item.uNextNestLevel != 0) {
4014 return -5;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08004015 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004016
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08004017 if(QCBORDecode_Finish(&DC)) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05304018 return -6;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08004019 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004020
Laurence Lundblade19e0c802018-10-13 12:19:55 +08004021 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07004022 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad1);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004023
Laurence Lundblade19e0c802018-10-13 12:19:55 +08004024 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004025
Laurence Lundblade19e0c802018-10-13 12:19:55 +08004026 nResult = QCBORDecode_GetNext(&DC, &Item);
4027 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05304028 return -7;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08004029 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004030
Laurence Lundblade570fab52018-10-13 18:28:27 +08004031 nResult = QCBORDecode_Finish(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07004032 if(nResult != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05304033 return -8;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08004034 }
4035
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004036
Laurence Lundblade19e0c802018-10-13 12:19:55 +08004037 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07004038 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad2);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004039
Laurence Lundblade19e0c802018-10-13 12:19:55 +08004040 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004041
Laurence Lundblade19e0c802018-10-13 12:19:55 +08004042 nResult = QCBORDecode_GetNext(&DC, &Item);
4043 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05304044 return -9;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08004045 }
4046
4047 nResult = QCBORDecode_GetNext(&DC, &Item);
4048 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05304049 return -10;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08004050 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004051
Laurence Lundblade19e0c802018-10-13 12:19:55 +08004052 nResult = QCBORDecode_GetNext(&DC, &Item);
4053 if(nResult || Item.uDataType != QCBOR_TYPE_INT64) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05304054 return -11;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08004055 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004056
Laurence Lundblade19e0c802018-10-13 12:19:55 +08004057 nResult = QCBORDecode_Finish(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07004058 if(nResult != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05304059 return -12;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08004060 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004061
4062
Laurence Lundblade19e0c802018-10-13 12:19:55 +08004063 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07004064 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad3);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004065
Laurence Lundblade19e0c802018-10-13 12:19:55 +08004066 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004067
Laurence Lundblade19e0c802018-10-13 12:19:55 +08004068 nResult = QCBORDecode_GetNext(&DC, &Item);
4069 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05304070 return -13;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08004071 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004072
Laurence Lundblade19e0c802018-10-13 12:19:55 +08004073 nResult = QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade642282a2020-06-23 12:00:33 -07004074 if(nResult != QCBOR_SUCCESS) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05304075 return -14;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08004076 }
Laurence Lundblade6de37062018-10-15 12:22:42 +05304077
Laurence Lundblade642282a2020-06-23 12:00:33 -07004078 nResult = QCBORDecode_GetNext(&DC, &Item);
4079 if(nResult != QCBOR_ERR_BAD_BREAK) {
4080 return -140;
4081 }
4082
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004083
Laurence Lundblade570fab52018-10-13 18:28:27 +08004084 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07004085 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad4);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004086
Laurence Lundblade570fab52018-10-13 18:28:27 +08004087 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004088
Laurence Lundblade570fab52018-10-13 18:28:27 +08004089 nResult = QCBORDecode_GetNext(&DC, &Item);
4090 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05304091 return -15;
Laurence Lundblade570fab52018-10-13 18:28:27 +08004092 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004093
Laurence Lundblade570fab52018-10-13 18:28:27 +08004094 nResult = QCBORDecode_GetNext(&DC, &Item);
4095 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05304096 return -16;
Laurence Lundblade570fab52018-10-13 18:28:27 +08004097 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004098
Laurence Lundblade570fab52018-10-13 18:28:27 +08004099 nResult = QCBORDecode_Finish(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07004100 if(nResult != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05304101 return -17;
Laurence Lundblade570fab52018-10-13 18:28:27 +08004102 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004103
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304104 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07004105 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad5);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004106
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304107 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004108
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304109 nResult = QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade37286c02022-09-03 10:05:02 -07004110
4111#ifndef QCBOR_DISABLE_TAGS
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304112 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05304113 return -18;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304114 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004115
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304116 nResult = QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05304117 if(nResult != QCBOR_ERR_BAD_BREAK) {
4118 return -19;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304119 }
Laurence Lundblade37286c02022-09-03 10:05:02 -07004120#else /* QCBOR_DISABLE_TAGS */
4121 if(nResult != QCBOR_ERR_TAGS_DISABLED) {
4122 return -20;
4123 }
4124#endif /* QCBOR_DISABLE_TAGS */
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004125
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08004126 return 0;
4127}
4128
Laurence Lundblade17ede402018-10-13 11:43:07 +08004129
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08004130#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
4131
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07004132static const uint8_t spIndefiniteLenString[] = {
Laurence Lundblade17ede402018-10-13 11:43:07 +08004133 0x81, // Array of length one
4134 0x7f, // text string marked with indefinite length
4135 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment
4136 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment
4137 0xff // ending break
4138};
4139
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07004140static const uint8_t spIndefiniteLenStringBad2[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304141 0x81, // Array of length one
4142 0x7f, // text string marked with indefinite length
4143 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment
4144 0x44, 0x6d, 0x69, 0x6e, 0x67, // second segment of wrong type
4145 0xff // ending break
4146};
4147
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07004148static const uint8_t spIndefiniteLenStringBad3[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304149 0x81, // Array of length one
4150 0x7f, // text string marked with indefinite length
4151 0x01, 0x02, // Not a string
4152 0xff // ending break
4153};
4154
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07004155static const uint8_t spIndefiniteLenStringBad4[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304156 0x81, // Array of length one
4157 0x7f, // text string marked with indefinite length
4158 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment
4159 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment
4160 // missing end of string
4161};
4162
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07004163static const uint8_t spIndefiniteLenStringLabel[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304164 0xa1, // Array of length one
4165 0x7f, // text string marked with indefinite length
4166 0x65, 0x73, 0x74, 0x72, 0x75, 0x75, // first segment
4167 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment
4168 0xff, // ending break
4169 0x01 // integer being labeled.
4170};
4171
Laurence Lundbladec5fef682020-01-25 11:38:45 -08004172/**
4173 Make an indefinite length string
4174
4175 @param Storage Storage for string, must be 144 bytes in size
4176 @return The indefinite length string
4177
4178 This makes an array with one indefinite length string that has 7 chunks
4179 from size of 1 byte up to 64 bytes.
4180 */
4181static UsefulBufC MakeIndefiniteBigBstr(UsefulBuf Storage)
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304182{
4183 UsefulOutBuf UOB;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004184
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304185 UsefulOutBuf_Init(&UOB, Storage);
4186 UsefulOutBuf_AppendByte(&UOB, 0x81);
4187 UsefulOutBuf_AppendByte(&UOB, 0x5f);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004188
Laurence Lundbladec5fef682020-01-25 11:38:45 -08004189 uint8_t uStringByte = 0;
4190 // Use of type int is intentional
4191 for(int uChunkSize = 1; uChunkSize <= 128; uChunkSize *= 2) {
4192 // Not using preferred encoding here, but that is OK.
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304193 UsefulOutBuf_AppendByte(&UOB, 0x58);
Laurence Lundbladec5fef682020-01-25 11:38:45 -08004194 UsefulOutBuf_AppendByte(&UOB, (uint8_t)uChunkSize);
4195 for(int j = 0; j < uChunkSize; j++) {
4196 UsefulOutBuf_AppendByte(&UOB, uStringByte);
4197 uStringByte++;
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304198 }
4199 }
4200 UsefulOutBuf_AppendByte(&UOB, 0xff);
4201
4202 return UsefulOutBuf_OutUBuf(&UOB);
4203}
4204
4205static int CheckBigString(UsefulBufC BigString)
4206{
4207 if(BigString.len != 255) {
4208 return 1;
4209 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004210
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304211 for(uint8_t i = 0; i < 255; i++){
4212 if(((const uint8_t *)BigString.ptr)[i] != i) {
4213 return 1;
4214 }
4215 }
4216 return 0;
4217}
4218
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05304219
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03004220int32_t IndefiniteLengthStringTest(void)
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304221{
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304222 QCBORDecodeContext DC;
4223 QCBORItem Item;
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05304224 // big enough for MakeIndefiniteBigBstr() + MemPool overhead
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004225 UsefulBuf_MAKE_STACK_UB(MemPool, 350);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004226
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304227 // --- Simple normal indefinite length string ------
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07004228 UsefulBufC IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenString);
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304229 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004230
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05304231 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304232 return -1;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304233 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004234
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304235 if(QCBORDecode_GetNext(&DC, &Item)) {
4236 return -2;
4237 }
4238 if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.uDataAlloc) {
4239 return -3;
4240 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004241
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304242 if(QCBORDecode_GetNext(&DC, &Item)) {
4243 return -4;
4244 }
4245 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || !Item.uDataAlloc) {
4246 return -5;
4247 }
4248 if(QCBORDecode_Finish(&DC)) {
4249 return -6;
4250 }
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304251
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304252 // ----- types mismatch ---
Laurence Lundbladeee851742020-01-08 08:37:05 -08004253 QCBORDecode_Init(&DC,
4254 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad2),
4255 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004256
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304257 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
4258 return -7;
4259 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004260
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304261 if(QCBORDecode_GetNext(&DC, &Item)) {
4262 return -8;
4263 }
4264 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
4265 return -9;
4266 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004267
Laurence Lundblade30816f22018-11-10 13:40:22 +07004268 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_INDEFINITE_STRING_CHUNK) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304269 return -10;
4270 }
4271
4272 // ----- not a string ---
Laurence Lundbladeee851742020-01-08 08:37:05 -08004273 QCBORDecode_Init(&DC,
4274 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad3),
4275 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004276
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304277 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
4278 return -11;
4279 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004280
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304281 if(QCBORDecode_GetNext(&DC, &Item)) {
4282 return -12;
4283 }
4284 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
4285 return -13;
4286 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004287
Laurence Lundblade30816f22018-11-10 13:40:22 +07004288 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_INDEFINITE_STRING_CHUNK) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304289 return -14;
4290 }
4291
4292 // ----- no end -----
Laurence Lundbladeee851742020-01-08 08:37:05 -08004293 QCBORDecode_Init(&DC,
4294 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad4),
4295 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004296
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304297 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
4298 return -15;
4299 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004300
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304301 if(QCBORDecode_GetNext(&DC, &Item)) {
4302 return -16;
4303 }
4304 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
4305 return -17;
4306 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004307
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304308 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_HIT_END) {
4309 return -18;
4310 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004311
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304312 // ------ Don't set a string allocator and see an error -----
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304313 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004314
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304315 QCBORDecode_GetNext(&DC, &Item);
4316 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304317 return -19;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304318 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004319
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304320 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_NO_STRING_ALLOCATOR) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304321 return -20;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304322 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004323
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304324 // ----- Mempool is way too small -----
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004325 UsefulBuf_MAKE_STACK_UB(MemPoolTooSmall, QCBOR_DECODE_MIN_MEM_POOL_SIZE-1);
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304326
4327 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
4328 if(!QCBORDecode_SetMemPool(&DC, MemPoolTooSmall, false)) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304329 return -21;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304330 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004331
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304332 // ----- Mempool is way too small -----
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05304333 UsefulBuf_MAKE_STACK_UB(BigIndefBStrStorage, 290);
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08004334 const UsefulBufC BigIndefBStr = MakeIndefiniteBigBstr(BigIndefBStrStorage);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004335
Laurence Lundbladeee851742020-01-08 08:37:05 -08004336 // 80 is big enough for MemPool overhead, but not BigIndefBStr
4337 UsefulBuf_MAKE_STACK_UB(MemPoolSmall, 80);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004338
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304339 QCBORDecode_Init(&DC, BigIndefBStr, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304340 if(QCBORDecode_SetMemPool(&DC, MemPoolSmall, false)) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304341 return -22;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304342 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004343
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304344 QCBORDecode_GetNext(&DC, &Item);
4345 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304346 return -23;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304347 }
Laurence Lundblade30816f22018-11-10 13:40:22 +07004348 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_STRING_ALLOCATE) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304349 return -24;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304350 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004351
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304352 // ---- big bstr -----
4353 QCBORDecode_Init(&DC, BigIndefBStr, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004354
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304355 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
4356 return -25;
4357 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004358
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05304359 if(QCBORDecode_GetNext(&DC, &Item)) {
4360 return -26;
4361 }
4362 if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.uDataAlloc) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304363 return -26;
4364 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004365
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304366 if(QCBORDecode_GetNext(&DC, &Item)) {
4367 return -27;
4368 }
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05304369 if(Item.uDataType != QCBOR_TYPE_BYTE_STRING || !Item.uDataAlloc || Item.uNestingLevel != 1) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304370 return -28;
4371 }
4372 if(CheckBigString(Item.val.string)) {
4373 return -3;
4374 }
4375 if(QCBORDecode_Finish(&DC)) {
4376 return -29;
4377 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004378
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304379 // --- label is an indefinite length string ------
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07004380 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringLabel), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004381
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304382 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
4383 return -30;
4384 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004385
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304386 QCBORDecode_GetNext(&DC, &Item);
4387 if(Item.uDataType != QCBOR_TYPE_MAP) {
4388 return -31;
4389 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004390
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304391 if(QCBORDecode_GetNext(&DC, &Item)){
4392 return -32;
4393 }
Laurence Lundbladeee851742020-01-08 08:37:05 -08004394 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
4395 Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304396 Item.uDataAlloc || !Item.uLabelAlloc ||
4397 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("struuming"))) {
4398 return -33;
4399 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004400
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304401 if(QCBORDecode_Finish(&DC)) {
4402 return -34;
4403 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004404
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004405 return 0;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08004406}
4407
4408
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03004409int32_t AllocAllStringsTest(void)
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304410{
4411 QCBORDecodeContext DC;
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004412 QCBORError nCBORError;
4413
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004414
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304415 // First test, use the "CSRMap" as easy input and checking
Laurence Lundbladeee851742020-01-08 08:37:05 -08004416 QCBORDecode_Init(&DC,
4417 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput),
4418 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004419
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004420 UsefulBuf_MAKE_STACK_UB(Pool, sizeof(spCSRInput) + QCBOR_DECODE_MIN_MEM_POOL_SIZE);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004421
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004422 nCBORError = QCBORDecode_SetMemPool(&DC, Pool, 1); // Turn on copying.
4423 if(nCBORError) {
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304424 return -1;
4425 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004426
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004427 if(CheckCSRMaps(&DC)) {
4428 return -2;
4429 }
4430
Laurence Lundblade2f467f92020-10-09 17:50:11 -07004431 // Next parse, save pointers to a few strings, destroy original and
4432 // see all is OK.
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004433 UsefulBuf_MAKE_STACK_UB(CopyOfStorage, sizeof(pValidMapEncoded) + QCBOR_DECODE_MIN_MEM_POOL_SIZE);
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08004434 const UsefulBufC CopyOf = UsefulBuf_Copy(CopyOfStorage, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded));
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08004435
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304436 QCBORDecode_Init(&DC, CopyOf, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08004437 UsefulBuf_Set(Pool, '/');
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304438 QCBORDecode_SetMemPool(&DC, Pool, 1); // Turn on copying.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004439
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304440 QCBORItem Item1, Item2, Item3, Item4;
4441 if((nCBORError = QCBORDecode_GetNext(&DC, &Item1)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07004442 return (int32_t)nCBORError;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304443 if(Item1.uDataType != QCBOR_TYPE_MAP ||
4444 Item1.val.uCount != 3)
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004445 return -3;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304446 if((nCBORError = QCBORDecode_GetNext(&DC, &Item1)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07004447 return (int32_t)nCBORError;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304448 if((nCBORError = QCBORDecode_GetNext(&DC, &Item2)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07004449 return (int32_t)nCBORError;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304450 if((nCBORError = QCBORDecode_GetNext(&DC, &Item3)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07004451 return (int32_t)nCBORError;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304452 if((nCBORError = QCBORDecode_GetNext(&DC, &Item4)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07004453 return (int32_t)nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004454
Laurence Lundblade05ec57b2018-10-21 01:50:03 +05304455 UsefulBuf_Set(CopyOfStorage, '_');
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004456
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304457 if(Item1.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304458 Item1.uDataType != QCBOR_TYPE_INT64 ||
4459 Item1.val.int64 != 42 ||
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004460 Item1.uDataAlloc != 0 ||
4461 Item1.uLabelAlloc == 0 ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07004462 UsefulBufCompareToSZ(Item1.label.string, "first integer")) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004463 return -4;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09004464 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004465
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304466
4467 if(Item2.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07004468 UsefulBufCompareToSZ(Item2.label.string, "an array of two strings") ||
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304469 Item2.uDataType != QCBOR_TYPE_ARRAY ||
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004470 Item2.uDataAlloc != 0 ||
4471 Item2.uLabelAlloc == 0 ||
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304472 Item2.val.uCount != 2)
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004473 return -5;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004474
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304475 if(Item3.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004476 Item3.uDataAlloc == 0 ||
4477 Item3.uLabelAlloc != 0 ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07004478 UsefulBufCompareToSZ(Item3.val.string, "string1")) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004479 return -6;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09004480 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004481
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304482 if(Item4.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004483 Item4.uDataAlloc == 0 ||
4484 Item4.uLabelAlloc != 0 ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07004485 UsefulBufCompareToSZ(Item4.val.string, "string2")) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004486 return -7;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09004487 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004488
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304489 // Next parse with a pool that is too small
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004490 UsefulBuf_MAKE_STACK_UB(SmallPool, QCBOR_DECODE_MIN_MEM_POOL_SIZE + 1);
Laurence Lundbladeee851742020-01-08 08:37:05 -08004491 QCBORDecode_Init(&DC,
4492 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded),
4493 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304494 QCBORDecode_SetMemPool(&DC, SmallPool, 1); // Turn on copying.
4495 if((nCBORError = QCBORDecode_GetNext(&DC, &Item1)))
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004496 return -8;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304497 if(Item1.uDataType != QCBOR_TYPE_MAP ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09004498 Item1.val.uCount != 3) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004499 return -9;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09004500 }
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304501 if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item1))){
4502 if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item2))) {
4503 if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item3))) {
4504 nCBORError = QCBORDecode_GetNext(&DC, &Item4);
4505 }
4506 }
4507 }
Laurence Lundblade30816f22018-11-10 13:40:22 +07004508 if(nCBORError != QCBOR_ERR_STRING_ALLOCATE) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004509 return -10;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304510 }
4511
4512 return 0;
4513}
4514
Laurence Lundbladef6531662018-12-04 10:42:22 +09004515
Laurence Lundbladec5fef682020-01-25 11:38:45 -08004516int32_t MemPoolTest(void)
Laurence Lundblade0155b622018-10-12 20:04:37 +08004517{
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004518 // Set up the decoder with a tiny bit of CBOR to parse because
4519 // nothing can be done with it unless that is set up.
Laurence Lundbladef6531662018-12-04 10:42:22 +09004520 QCBORDecodeContext DC;
4521 const uint8_t pMinimalCBOR[] = {0xa0}; // One empty map
4522 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pMinimalCBOR),0);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004523
Laurence Lundbladef6531662018-12-04 10:42:22 +09004524 // Set up an memory pool of 100 bytes
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004525 // Then fish into the internals of the decode context
4526 // to get the allocator function so it can be called directly.
4527 // Also figure out how much pool is available for use
4528 // buy subtracting out the overhead.
Laurence Lundbladef6531662018-12-04 10:42:22 +09004529 UsefulBuf_MAKE_STACK_UB(Pool, 100);
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004530 QCBORError nError = QCBORDecode_SetMemPool(&DC, Pool, 0);
4531 if(nError) {
4532 return -9;
4533 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004534 QCBORStringAllocate pAlloc = DC.StringAllocator.pfAllocator;
4535 void *pAllocCtx = DC.StringAllocator.pAllocateCxt;
4536 size_t uAvailPool = Pool.len - QCBOR_DECODE_MIN_MEM_POOL_SIZE;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004537
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004538 // First test -- ask for one more byte than available and see failure
4539 UsefulBuf Allocated = (*pAlloc)(pAllocCtx, NULL, uAvailPool+1);
Laurence Lundbladef6531662018-12-04 10:42:22 +09004540 if(!UsefulBuf_IsNULL(Allocated)) {
4541 return -1;
4542 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004543
Laurence Lundbladef6531662018-12-04 10:42:22 +09004544 // Re do the set up for the next test that will do a successful alloc,
4545 // a fail, a free and then success
Laurence Lundbladef6531662018-12-04 10:42:22 +09004546 QCBORDecode_SetMemPool(&DC, Pool, 0);
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004547 pAlloc = DC.StringAllocator.pfAllocator;
4548 pAllocCtx = DC.StringAllocator.pAllocateCxt;
4549 uAvailPool = Pool.len - QCBOR_DECODE_MIN_MEM_POOL_SIZE;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004550
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004551 // Allocate one byte less than available and see success
4552 Allocated = (pAlloc)(pAllocCtx, NULL, uAvailPool-1);
Laurence Lundbladef6531662018-12-04 10:42:22 +09004553 if(UsefulBuf_IsNULL(Allocated)) { // expected to succeed
4554 return -2;
4555 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004556 // Ask for some more and see failure
4557 UsefulBuf Allocated2 = (*pAlloc)(pAllocCtx, NULL, uAvailPool/2);
Laurence Lundbladef6531662018-12-04 10:42:22 +09004558 if(!UsefulBuf_IsNULL(Allocated2)) { // expected to fail
4559 return -3;
4560 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004561 // Free the first allocate, retry the second and see success
4562 (*pAlloc)(pAllocCtx, Allocated.ptr, 0); // Free
4563 Allocated = (*pAlloc)(pAllocCtx, NULL, uAvailPool/2);
Laurence Lundbladef6531662018-12-04 10:42:22 +09004564 if(UsefulBuf_IsNULL(Allocated)) { // succeed because of the free
4565 return -4;
4566 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004567
Laurence Lundbladef6531662018-12-04 10:42:22 +09004568 // Re do set up for next test that involves a successful alloc,
4569 // and a successful realloc and a failed realloc
4570 QCBORDecode_SetMemPool(&DC, Pool, 0);
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004571 pAlloc = DC.StringAllocator.pfAllocator;
4572 pAllocCtx = DC.StringAllocator.pAllocateCxt;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004573
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004574 // Allocate half the pool and see success
4575 Allocated = (*pAlloc)(pAllocCtx, NULL, uAvailPool/2);
Laurence Lundbladef6531662018-12-04 10:42:22 +09004576 if(UsefulBuf_IsNULL(Allocated)) { // expected to succeed
4577 return -5;
4578 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004579 // Reallocate to take up the whole pool and see success
4580 Allocated2 = (*pAlloc)(pAllocCtx, Allocated.ptr, uAvailPool);
Laurence Lundbladef6531662018-12-04 10:42:22 +09004581 if(UsefulBuf_IsNULL(Allocated2)) {
4582 return -6;
4583 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004584 // Make sure its the same pointer and the size is right
Laurence Lundbladef6531662018-12-04 10:42:22 +09004585 if(Allocated2.ptr != Allocated.ptr || Allocated2.len != uAvailPool) {
4586 return -7;
4587 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004588 // Try to allocate more to be sure there is failure after a realloc
4589 UsefulBuf Allocated3 = (*pAlloc)(pAllocCtx, Allocated.ptr, uAvailPool+1);
4590 if(!UsefulBuf_IsNULL(Allocated3)) {
Laurence Lundbladef6531662018-12-04 10:42:22 +09004591 return -8;
4592 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004593
Laurence Lundbladef6531662018-12-04 10:42:22 +09004594 return 0;
4595}
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004596
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004597
4598/* Just enough of an allocator to test configuration of one */
4599static UsefulBuf AllocateTestFunction(void *pCtx, void *pOldMem, size_t uNewSize)
4600{
4601 (void)pOldMem; // unused variable
4602
4603 if(uNewSize) {
4604 // Assumes the context pointer is the buffer and
4605 // nothing too big will ever be asked for.
4606 // This is only good for this basic test!
4607 return (UsefulBuf) {pCtx, uNewSize};
4608 } else {
4609 return NULLUsefulBuf;
4610 }
4611}
4612
4613
Laurence Lundbladec5fef682020-01-25 11:38:45 -08004614int32_t SetUpAllocatorTest(void)
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004615{
4616 // Set up the decoder with a tiny bit of CBOR to parse because
4617 // nothing can be done with it unless that is set up.
4618 QCBORDecodeContext DC;
4619 const uint8_t pMinimalCBOR[] = {0x62, 0x48, 0x69}; // "Hi"
4620 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pMinimalCBOR),0);
4621
4622 uint8_t pAllocatorBuffer[50];
4623
4624 // This is really just to test that this call works.
4625 // The full functionality of string allocators is tested
4626 // elsewhere with the MemPool internal allocator.
4627 QCBORDecode_SetUpAllocator(&DC, AllocateTestFunction, pAllocatorBuffer, 1);
4628
4629 QCBORItem Item;
4630 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_SUCCESS) {
4631 return -1;
4632 }
4633
4634 if(Item.uDataAlloc == 0 ||
4635 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
4636 Item.val.string.ptr != pAllocatorBuffer) {
4637 return -2;
4638 }
4639
4640 if(QCBORDecode_Finish(&DC) != QCBOR_SUCCESS) {
4641 return -3;
4642 }
4643
4644 return 0;
4645}
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08004646#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
4647
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004648
Laurence Lundbladedd6e76e2021-03-10 01:54:01 -07004649#ifndef QCBOR_DISABLE_EXP_AND_MANTISSA
Laurence Lundblade59289e52019-12-30 13:44:37 -08004650
Laurence Lundblade37286c02022-09-03 10:05:02 -07004651struct EaMTest {
4652 const char *szName;
4653 UsefulBufC Input;
4654 uint8_t uTagRequirement;
4655 bool bHasTags;
4656
4657 /* Expected values for GetNext */
4658 QCBORError uExpectedErrorGN;
4659 uint8_t uQCBORTypeGN;
4660 int64_t nExponentGN;
4661 int64_t nMantissaGN;
4662 UsefulBufC MantissaGN;
4663
4664 /* Expected values for GetDecimalFraction */
4665 QCBORError uExpectedErrorGDF;
4666 int64_t nExponentGDF;
4667 int64_t nMantissaGDF;
4668
4669 /* Expected values for GetDecimalFractionBig */
4670 QCBORError uExpectedErrorGDFB;
4671 int64_t nExponentGDFB;
4672 UsefulBufC MantissaGDFB;
4673 bool IsNegativeGDFB;
4674
4675 /* Expected values for GetBigFloat */
4676 QCBORError uExpectedErrorGBF;
4677 int64_t nExponentGBF;
4678 int64_t nMantissaGBF;
4679
4680 /* Expected values for GetBigFloatBig */
4681 QCBORError uExpectedErrorGBFB;
4682 int64_t nExponentGBFB;
4683 UsefulBufC MantissaGBFB;
4684 bool IsNegativeGBFB;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004685};
4686
Laurence Lundbladefaec39f2020-08-02 21:53:53 -07004687
Laurence Lundblade37286c02022-09-03 10:05:02 -07004688
4689static const struct EaMTest pEaMTests[] = {
4690 {
4691 "1. Untagged pair (big float or decimal fraction), no tag required",
4692 {(const uint8_t []){0x82, 0x20, 0x03}, 3},
4693 QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
4694 false,
4695
4696 QCBOR_SUCCESS, /* for GetNext */
4697 QCBOR_TYPE_ARRAY,
4698 0,
4699 0,
4700 {(const uint8_t []){0x00}, 1},
4701
4702 QCBOR_SUCCESS, /* GetDecimalFraction */
4703 -1,
4704 3,
4705
4706 QCBOR_SUCCESS, /* for GetDecimalFractionBig */
4707 -1,
4708 {(const uint8_t []){0x03}, 1},
4709 false,
4710
4711 QCBOR_SUCCESS, /* for GetBigFloat */
4712 -1,
4713 3,
4714
4715 QCBOR_SUCCESS, /* for GetBigFloatBig */
4716 -1,
4717 {(const uint8_t []){0x03}, 1},
4718 false
4719 },
4720
4721 {
4722 "2. Untagged pair (big float or decimal fraction), tag required",
4723 {(const uint8_t []){0x82, 0x20, 0x03}, 3},
4724 QCBOR_TAG_REQUIREMENT_TAG,
4725 false,
4726
4727 QCBOR_SUCCESS, /* for GetNext */
4728 QCBOR_TYPE_ARRAY,
4729 0,
4730 0,
4731 {(const uint8_t []){0x00}, 1},
4732
4733 QCBOR_ERR_UNEXPECTED_TYPE, /* for GetDecimalFraction */
4734 0,
4735 0,
4736
4737 QCBOR_ERR_UNEXPECTED_TYPE, /* for GetDecimalFractionBig */
4738 0,
4739 {(const uint8_t []){0x00}, 1},
4740 false,
4741
4742 QCBOR_ERR_UNEXPECTED_TYPE, /* for GetBigFloat */
4743 0,
4744 0,
4745
4746 QCBOR_ERR_UNEXPECTED_TYPE, /* for GetBigFloatBig */
4747 0,
4748 {(const uint8_t []){0x00}, 1},
4749 false
4750
4751 },
4752
4753 {
4754 "3. Tagged 1.5 decimal fraction, tag 4 optional",
4755 {(const uint8_t []){0xC4, 0x82, 0x20, 0x03}, 4},
4756 QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
4757 true,
4758
4759 QCBOR_SUCCESS, /* for GetNext */
4760 QCBOR_TYPE_DECIMAL_FRACTION,
4761 -1,
4762 3,
4763 {(const uint8_t []){0x00}, 1},
4764
4765
4766 QCBOR_SUCCESS, /* for GetDecimalFraction */
4767 -1,
4768 3,
4769
4770 QCBOR_SUCCESS, /* for GetDecimalFractionBig */
4771 -1,
4772 {(const uint8_t []){0x03}, 1},
4773 false,
4774
4775 QCBOR_ERR_UNEXPECTED_TYPE, /* for GetBigFloat */
4776 0,
4777 0,
4778
4779 QCBOR_ERR_UNEXPECTED_TYPE, /* for GetBigFloatBig */
4780 0,
4781 {(const uint8_t []){0x00}, 1},
4782 false
4783 },
4784 {
4785 "4. Tagged 100 * 2^300 big float, tag 5 optional",
4786 {(const uint8_t []){0xC5, 0x82, 0x19, 0x01, 0x2C, 0x18, 0x64}, 7},
4787 QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
4788 true,
4789
4790 QCBOR_SUCCESS, /* for GetNext */
4791 QCBOR_TYPE_BIGFLOAT,
4792 300,
4793 100,
4794 {(const uint8_t []){0x00}, 1},
4795
4796
4797 QCBOR_ERR_UNEXPECTED_TYPE, /* for GetDecimalFraction */
4798 0,
4799 0,
4800
4801 QCBOR_ERR_UNEXPECTED_TYPE, /* for GetDecimalFractionBig */
4802 0,
4803 {(const uint8_t []){0x03}, 1},
4804 false,
4805
4806 QCBOR_SUCCESS, /* for GetBigFloat */
4807 300,
4808 100,
4809
4810 QCBOR_SUCCESS, /* for GetBigFloatBig */
4811 300,
4812 {(const uint8_t []){0x64}, 1},
4813 false
4814 },
4815
4816 {
4817 "5. Tagged 4([-20, 4759477275222530853136]) decimal fraction, tag 4 required",
4818 {(const uint8_t []){0xC4, 0x82, 0x33,
4819 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,}, 15},
4820 QCBOR_TAG_REQUIREMENT_TAG,
4821 true,
4822
4823 QCBOR_SUCCESS, /* for GetNext */
4824 QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM,
4825 -20,
4826 0,
4827 {(const uint8_t []){0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10}, 10},
4828
4829 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW, /* for GetDecimalFraction */
4830 0,
4831 0,
4832
4833 QCBOR_SUCCESS, /* for GetDecimalFractionBig */
4834 -20,
4835 {(const uint8_t []){0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10}, 10},
4836 false,
4837
4838 QCBOR_ERR_UNEXPECTED_TYPE, /* for GetBigFloat */
4839 0,
4840 0,
4841
4842 QCBOR_ERR_UNEXPECTED_TYPE, /* for GetBigFloatBig */
4843 0,
4844 {(const uint8_t []){0x00}, 0},
4845 false
4846 },
4847
4848 {
4849 "6. Error: Mantissa and exponent inside a Mantissa and exponent",
4850 {(const uint8_t []){0xC4, 0x82, 0x33,
4851 0xC5, 0x82, 0x19, 0x01, 0x2C, 0x18, 0x64}, 10},
4852 QCBOR_TAG_REQUIREMENT_TAG,
4853 true,
4854
4855 QCBOR_ERR_BAD_EXP_AND_MANTISSA, /* for GetNext */
4856 QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM,
4857 0,
4858 0,
4859 {(const uint8_t []){0x00}, 0},
4860
4861 QCBOR_ERR_BAD_EXP_AND_MANTISSA, /* for GetDecimalFraction */
4862 0,
4863 0,
4864
4865 QCBOR_ERR_BAD_EXP_AND_MANTISSA, /* for GetDecimalFractionBig */
4866 0,
4867 {(const uint8_t []){0x00}, 0},
4868 false,
4869
4870 QCBOR_ERR_BAD_EXP_AND_MANTISSA, /* for GetBigFloat */
4871 0,
4872 0,
4873
4874 QCBOR_ERR_BAD_EXP_AND_MANTISSA, /* for GetBigFloatBig */
4875 0,
4876 {(const uint8_t []){0x00}, 0},
4877 false
4878 },
4879 {
4880 "7. Tagged 5([-20, 4294967295]) big float, big num mantissa, tag 5 required",
4881 {(const uint8_t []){0xC5, 0x82, 0x33,
4882 0xC2, 0x44, 0xff, 0xff, 0xff, 0xff}, 9},
4883 QCBOR_TAG_REQUIREMENT_TAG,
4884 true,
4885
4886 QCBOR_SUCCESS, /* for GetNext */
4887 QCBOR_TYPE_BIGFLOAT_POS_BIGNUM,
4888 -20,
4889 0,
4890 {(const uint8_t []){0xff, 0xff, 0xff, 0xff}, 4},
4891
4892 QCBOR_ERR_UNEXPECTED_TYPE, /* for GetDecimalFraction */
4893 0,
4894 0,
4895
4896 QCBOR_ERR_UNEXPECTED_TYPE, /* for GetDecimalFractionBig */
4897 -20,
4898 {(const uint8_t []){0x00}, 1},
4899 false,
4900
4901 QCBOR_SUCCESS, /* for GetBigFloat */
4902 -20,
4903 4294967295,
4904
4905 QCBOR_SUCCESS, /* for GetBigFloatBig */
4906 -20,
4907 {(const uint8_t []){0xff, 0xff, 0xff, 0xff}, 4},
4908 false
4909 },
4910
4911 {
4912 /* Special case for test 8. Don't renumber it. */
4913 "8. Untagged pair with big num (big float or decimal fraction), tag optional",
4914 {(const uint8_t []){0x82, 0x33, 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10}, 14},
4915 QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
4916 true,
4917
4918 QCBOR_SUCCESS, /* for GetNext */
4919 QCBOR_TYPE_ARRAY,
4920 0,
4921 0,
4922 {(const uint8_t []){0x00}, 1},
4923
4924 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW, /* GetDecimalFraction */
4925 0,
4926 0,
4927
4928 QCBOR_SUCCESS, /* for GetDecimalFractionBig */
4929 -20,
4930 {(const uint8_t []){0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10}, 10},
4931 false,
4932
4933 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW, /* for GetBigFloat */
4934 0,
4935 0,
4936
4937 QCBOR_SUCCESS, /* for GetBigFloatBig */
4938 -20,
4939 {(const uint8_t []){0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10}, 10},
4940 false
4941 },
4942
4943 {
4944 "9. decimal fraction with large exponent and negative big num mantissa",
4945 {(const uint8_t []){0xC4, 0x82, 0x1B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
4946 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10}, 23},
4947 QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
4948 true,
4949
4950 QCBOR_SUCCESS, /* for GetNext */
4951 QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM,
4952 9223372036854775807,
4953 0,
4954 {(const uint8_t []){0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10}, 10},
4955
4956 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW, /* GetDecimalFraction */
4957 0,
4958 0,
4959
4960 QCBOR_SUCCESS, /* for GetDecimalFractionBig */
4961 9223372036854775807,
4962 {(const uint8_t []){0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10}, 10},
4963 true,
4964
4965 QCBOR_ERR_UNEXPECTED_TYPE, /* for GetBigFloat */
4966 0,
4967 0,
4968
4969 QCBOR_ERR_UNEXPECTED_TYPE, /* for GetBigFloatBig */
4970 0,
4971 {(const uint8_t []){0x00}, 1},
4972 false
4973 },
4974};
4975
4976
4977
4978int32_t ProcessEaMTests(void)
Laurence Lundblade59289e52019-12-30 13:44:37 -08004979{
Laurence Lundblade37286c02022-09-03 10:05:02 -07004980 size_t uIndex;
4981 QCBORDecodeContext DCtx;
4982 QCBORItem Item;
4983 QCBORError uError;
4984 int64_t nMantissa, nExponent;
4985 MakeUsefulBufOnStack( MantissaBuf, 200);
4986 UsefulBufC Mantissa;
4987 bool bMantissaIsNegative;
4988
4989 for(uIndex = 0; uIndex < C_ARRAY_COUNT(pEaMTests, struct EaMTest); uIndex++) {
4990 const struct EaMTest *pT = &pEaMTests[uIndex];
4991 /* Decode with GetNext */
4992 QCBORDecode_Init(&DCtx, pT->Input, 0);
4993
4994 if(uIndex + 1 == 9) {
4995 nExponent = 99; // just to set a break point
4996 }
4997
4998 uError = QCBORDecode_GetNext(&DCtx, &Item);
4999#ifdef QCBOR_DISABLE_TAGS
5000 /* Test 8 is a special case when tags are disabled */
5001 if(pT->bHasTags && uIndex + 1 != 8) {
5002 if(uError != QCBOR_ERR_TAGS_DISABLED) {
5003 return (int32_t)(1+uIndex) * 1000 + 9;
5004 }
5005 } else {
5006#endif
5007 /* Now check return code, data type, mantissa and exponent */
5008 if(pT->uExpectedErrorGN != uError) {
5009 return (int32_t)(1+uIndex) * 1000 + 1;
5010 }
5011 if(uError == QCBOR_SUCCESS && pT->uQCBORTypeGN != QCBOR_TYPE_ARRAY) {
5012 if(pT->uQCBORTypeGN != Item.uDataType) {
5013 return (int32_t)(1+uIndex) * 1000 + 2;
5014 }
5015 if(pT->nExponentGN != Item.val.expAndMantissa.nExponent) {
5016 return (int32_t)(1+uIndex) * 1000 + 3;
5017 }
5018 if(Item.uDataType == QCBOR_TYPE_DECIMAL_FRACTION || Item.uDataType == QCBOR_TYPE_BIGFLOAT ) {
5019 if(pT->nMantissaGN != Item.val.expAndMantissa.Mantissa.nInt) {
5020 return (int32_t)(1+uIndex) * 1000 + 4;
5021 }
5022 } else {
5023 if(UsefulBuf_Compare(Item.val.expAndMantissa.Mantissa.bigNum, pT->MantissaGN)) {
5024 return (int32_t)(1+uIndex) * 1000 + 5;
5025 }
5026 }
5027 }
5028#ifdef QCBOR_DISABLE_TAGS
5029 }
5030#endif
5031
5032 /* Decode with GetDecimalFraction */
5033 QCBORDecode_Init(&DCtx, pT->Input, 0);
5034 QCBORDecode_GetDecimalFraction(&DCtx,
5035 pT->uTagRequirement,
5036 &nMantissa,
5037 &nExponent);
5038 uError = QCBORDecode_GetAndResetError(&DCtx);
5039#ifdef QCBOR_DISABLE_TAGS
5040 if(pT->bHasTags) {
5041 if(uError != QCBOR_ERR_TAGS_DISABLED) {
5042 return (int32_t)(1+uIndex) * 1000 + 39;
5043 }
5044 } else {
5045#endif
5046 /* Now check return code, mantissa and exponent */
5047 if(pT->uExpectedErrorGDF != uError) {
5048 return (int32_t)(1+uIndex) * 1000 + 31;
5049 }
5050 if(uError == QCBOR_SUCCESS) {
5051 if(pT->nExponentGDF != nExponent) {
5052 return (int32_t)(1+uIndex) * 1000 + 32;
5053 }
5054 if(pT->nMantissaGDF != nMantissa) {
5055 return (int32_t)(1+uIndex) * 1000 + 33;
5056 }
5057 }
5058#ifdef QCBOR_DISABLE_TAGS
5059 }
5060#endif
5061
5062 /* Decode with GetDecimalFractionBig */
5063 QCBORDecode_Init(&DCtx, pT->Input, 0);
5064 QCBORDecode_GetDecimalFractionBig(&DCtx,
5065 pT->uTagRequirement,
5066 MantissaBuf,
5067 &Mantissa,
5068 &bMantissaIsNegative,
5069 &nExponent);
5070 uError = QCBORDecode_GetAndResetError(&DCtx);
5071#ifdef QCBOR_DISABLE_TAGS
5072 if(pT->bHasTags) {
5073 if(uError != QCBOR_ERR_TAGS_DISABLED) {
5074 return (int32_t)(1+uIndex) * 1000 + 49;
5075 }
5076 } else {
5077#endif
5078 /* Now check return code, mantissa (bytes and sign) and exponent */
5079 if(pT->uExpectedErrorGDFB != uError) {
5080 return (int32_t)(1+uIndex) * 1000 + 41;
5081 }
5082 if(uError == QCBOR_SUCCESS) {
5083 if(pT->nExponentGDFB != nExponent) {
5084 return (int32_t)(1+uIndex) * 1000 + 42;
5085 }
5086 if(pT->IsNegativeGDFB != bMantissaIsNegative) {
5087 return (int32_t)(1+uIndex) * 1000 + 43;
5088 }
5089 if(UsefulBuf_Compare(Mantissa, pT->MantissaGDFB)) {
5090 return (int32_t)(1+uIndex) * 1000 + 44;
5091 }
5092 }
5093#ifdef QCBOR_DISABLE_TAGS
5094 }
5095#endif
5096
5097 /* Decode with GetBigFloat */
5098 QCBORDecode_Init(&DCtx, pT->Input, 0);
5099 QCBORDecode_GetBigFloat(&DCtx,
5100 pT->uTagRequirement,
5101 &nMantissa,
5102 &nExponent);
5103 uError = QCBORDecode_GetAndResetError(&DCtx);
5104#ifdef QCBOR_DISABLE_TAGS
5105 if(pT->bHasTags) {
5106 if(uError != QCBOR_ERR_TAGS_DISABLED) {
5107 return (int32_t)(1+uIndex) * 1000 + 19;
5108 }
5109 } else {
5110#endif
5111 /* Now check return code, mantissa and exponent */
5112 if(pT->uExpectedErrorGBF != uError) {
5113 return (int32_t)(1+uIndex) * 1000 + 11;
5114 }
5115 if(uError == QCBOR_SUCCESS) {
5116 if(pT->nExponentGBF != nExponent) {
5117 return (int32_t)(1+uIndex) * 1000 + 12;
5118 }
5119 if(pT->nMantissaGBF != nMantissa) {
5120 return (int32_t)(1+uIndex) * 1000 + 13;
5121 }
5122 }
5123#ifdef QCBOR_DISABLE_TAGS
5124 }
5125#endif
5126
5127 /* Decode with GetBigFloatBig */
5128 QCBORDecode_Init(&DCtx, pT->Input, 0);
5129 QCBORDecode_GetBigFloatBig(&DCtx,
5130 pT->uTagRequirement,
5131 MantissaBuf,
5132 &Mantissa,
5133 &bMantissaIsNegative,
5134 &nExponent);
5135 uError = QCBORDecode_GetAndResetError(&DCtx);
5136#ifdef QCBOR_DISABLE_TAGS
5137 if(pT->bHasTags) {
5138 if(uError != QCBOR_ERR_TAGS_DISABLED) {
5139 return (int32_t)(1+uIndex) * 1000 + 29;
5140 }
5141 } else {
5142#endif
5143 /* Now check return code, mantissa (bytes and sign) and exponent */
5144 if(pT->uExpectedErrorGBFB != uError) {
5145 return (int32_t)(1+uIndex) * 1000 + 21;
5146 }
5147 if(uError == QCBOR_SUCCESS) {
5148 if(pT->nExponentGBFB != nExponent) {
5149 return (int32_t)(1+uIndex) * 1000 + 22;
5150 }
5151 if(pT->IsNegativeGBFB != bMantissaIsNegative) {
5152 return (int32_t)(1+uIndex) * 1000 + 23;
5153 }
5154 if(UsefulBuf_Compare(Mantissa, pT->MantissaGBFB)) {
5155 return (int32_t)(1+uIndex) * 1000 + 24;
5156 }
5157 }
5158#ifdef QCBOR_DISABLE_TAGS
5159 }
5160#endif
5161 }
5162
5163 return 0;
5164}
5165
5166
5167int32_t ExponentAndMantissaDecodeTestsSecondary(void)
5168{
5169#ifndef QCBOR_DISABLE_TAGS
Laurence Lundblade59289e52019-12-30 13:44:37 -08005170 QCBORDecodeContext DC;
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07005171 QCBORError uErr;
Laurence Lundblade59289e52019-12-30 13:44:37 -08005172 QCBORItem item;
5173
Laurence Lundblade17af4902020-01-07 19:11:55 -08005174 static const uint8_t spBigNumMantissa[] = {0x01, 0x02, 0x03, 0x04, 0x05,
5175 0x06, 0x07, 0x08, 0x09, 0x010};
5176 UsefulBufC BN = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNumMantissa);
Laurence Lundblade59289e52019-12-30 13:44:37 -08005177
5178
Laurence Lundblade59289e52019-12-30 13:44:37 -08005179
5180 /* Now encode some stuff and then decode it */
5181 uint8_t pBuf[40];
5182 QCBOREncodeContext EC;
5183 UsefulBufC Encoded;
5184
5185 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(pBuf));
5186 QCBOREncode_OpenArray(&EC);
5187 QCBOREncode_AddDecimalFraction(&EC, 999, 1000); // 999 * (10 ^ 1000)
5188 QCBOREncode_AddBigFloat(&EC, 100, INT32_MIN);
5189 QCBOREncode_AddDecimalFractionBigNum(&EC, BN, false, INT32_MAX);
5190 QCBOREncode_CloseArray(&EC);
5191 QCBOREncode_Finish(&EC, &Encoded);
5192
5193
5194 QCBORDecode_Init(&DC, Encoded, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07005195 uErr = QCBORDecode_GetNext(&DC, &item);
5196 if(uErr != QCBOR_SUCCESS) {
5197 return 100;
Laurence Lundblade59289e52019-12-30 13:44:37 -08005198 }
5199
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07005200 uErr = QCBORDecode_GetNext(&DC, &item);
5201 if(uErr != QCBOR_SUCCESS) {
5202 return 101;
Laurence Lundblade59289e52019-12-30 13:44:37 -08005203 }
5204
5205 if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION ||
5206 item.val.expAndMantissa.nExponent != 1000 ||
5207 item.val.expAndMantissa.Mantissa.nInt != 999) {
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07005208 return 102;
Laurence Lundblade59289e52019-12-30 13:44:37 -08005209 }
5210
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07005211 uErr = QCBORDecode_GetNext(&DC, &item);
5212 if(uErr != QCBOR_SUCCESS) {
5213 return 103;
Laurence Lundblade59289e52019-12-30 13:44:37 -08005214 }
5215
5216 if(item.uDataType != QCBOR_TYPE_BIGFLOAT ||
5217 item.val.expAndMantissa.nExponent != INT32_MIN ||
5218 item.val.expAndMantissa.Mantissa.nInt != 100) {
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07005219 return 104;
Laurence Lundblade59289e52019-12-30 13:44:37 -08005220 }
5221
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07005222 uErr = QCBORDecode_GetNext(&DC, &item);
5223 if(uErr != QCBOR_SUCCESS) {
5224 return 105;
Laurence Lundblade59289e52019-12-30 13:44:37 -08005225 }
5226
5227 if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM ||
5228 item.val.expAndMantissa.nExponent != INT32_MAX ||
5229 UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) {
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07005230 return 106;
5231 }
5232
Laurence Lundblade37286c02022-09-03 10:05:02 -07005233#endif /* QCBOR_TAGS_DISABLED */
Laurence Lundblade59289e52019-12-30 13:44:37 -08005234
5235 return 0;
5236}
5237
5238
Laurence Lundblade37286c02022-09-03 10:05:02 -07005239int32_t ExponentAndMantissaDecodeTests(void)
5240{
5241 int32_t rv = ProcessEaMTests();
5242 if(rv) {
5243 return rv;
5244 }
5245
5246 return ExponentAndMantissaDecodeTestsSecondary();
5247}
5248
5249
Laurence Lundbladecc7da412020-12-27 00:09:07 -08005250static const struct FailInput ExponentAndMantissaFailures[] = {
Laurence Lundblade59289e52019-12-30 13:44:37 -08005251 // Exponent > INT64_MAX
5252 { {(uint8_t[]){0xC4, 0x82, 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
5253 0xFF, 0xFF, 0x1B, 0x80, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
5254 0xFF, 0xFF,}, 20}, QCBOR_ERR_BAD_EXP_AND_MANTISSA},
5255 // Mantissa > INT64_MAX
5256 { {(uint8_t[]){0xC4, 0x82, 0x1B, 0x80, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
5257 0xFF, 0xFF, 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05,
5258 0x06, 0x07, 0x08, 0x09, 0x10}, 23}, QCBOR_ERR_BAD_EXP_AND_MANTISSA},
5259 // End of input
Laurence Lundbladee6f15112020-07-23 18:44:16 -07005260 { {(uint8_t[]){0xC4, 0x82}, 2}, QCBOR_ERR_NO_MORE_ITEMS},
Laurence Lundblade59289e52019-12-30 13:44:37 -08005261 // End of input
Laurence Lundbladee6f15112020-07-23 18:44:16 -07005262 { {(uint8_t[]){0xC4, 0x82, 0x01}, 3}, QCBOR_ERR_NO_MORE_ITEMS},
Laurence Lundblade59289e52019-12-30 13:44:37 -08005263 // bad content for big num
5264 { {(uint8_t[]){0xC4, 0x82, 0x01, 0xc3, 0x01}, 5}, QCBOR_ERR_BAD_OPT_TAG},
5265 // bad content for big num
5266 { {(uint8_t[]){0xC4, 0x82, 0xc2, 0x01, 0x1f}, 5}, QCBOR_ERR_BAD_INT},
5267 // Bad integer for exponent
5268 { {(uint8_t[]){0xC4, 0x82, 0x01, 0x1f}, 4}, QCBOR_ERR_BAD_INT},
5269 // Bad integer for mantissa
5270 { {(uint8_t[]){0xC4, 0x82, 0x1f, 0x01}, 4}, QCBOR_ERR_BAD_INT},
5271 // 3 items in array
5272 { {(uint8_t[]){0xC4, 0x83, 0x03, 0x01, 02}, 5}, QCBOR_ERR_BAD_EXP_AND_MANTISSA},
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08005273#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundblade59289e52019-12-30 13:44:37 -08005274 // unterminated indefinite length array
5275 { {(uint8_t[]){0xC4, 0x9f, 0x03, 0x01, 0x02}, 5}, QCBOR_ERR_BAD_EXP_AND_MANTISSA},
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08005276#else /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
5277 // unterminated indefinite length array
5278 { {(uint8_t[]){0xC4, 0x9f, 0x03, 0x01, 0x02}, 5}, QCBOR_ERR_INDEF_LEN_ARRAYS_DISABLED},
5279#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
Laurence Lundblade59289e52019-12-30 13:44:37 -08005280 // Empty array
5281 { {(uint8_t[]){0xC4, 0x80}, 2}, QCBOR_ERR_NO_MORE_ITEMS},
5282 // Second is not an integer
5283 { {(uint8_t[]){0xC4, 0x82, 0x03, 0x40}, 4}, QCBOR_ERR_BAD_EXP_AND_MANTISSA},
5284 // First is not an integer
5285 { {(uint8_t[]){0xC4, 0x82, 0x40}, 3}, QCBOR_ERR_BAD_EXP_AND_MANTISSA},
5286 // Not an array
5287 { {(uint8_t[]){0xC4, 0xa2}, 2}, QCBOR_ERR_BAD_EXP_AND_MANTISSA}
5288};
5289
5290
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03005291int32_t ExponentAndMantissaDecodeFailTests(void)
Laurence Lundblade59289e52019-12-30 13:44:37 -08005292{
5293 return ProcessFailures(ExponentAndMantissaFailures,
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08005294 C_ARRAY_COUNT(ExponentAndMantissaFailures,
5295 struct FailInput));
Laurence Lundblade59289e52019-12-30 13:44:37 -08005296}
5297
Laurence Lundbladedd6e76e2021-03-10 01:54:01 -07005298#endif /* QCBOR_DISABLE_EXP_AND_MANTISSA */
Laurence Lundbladebb87be22020-04-09 19:15:32 -07005299
5300
5301
5302/*
5303 Some basic CBOR with map and array used in a lot of tests.
5304 The map labels are all strings
5305
Laurence Lundblade8ffdb742020-05-07 02:49:18 -07005306 {
5307 "first integer": 42,
Laurence Lundbladebb87be22020-04-09 19:15:32 -07005308 "an array of two strings": [
5309 "string1", "string2"
5310 ],
5311 "map in a map": {
5312 "bytes 1": h'78787878',
5313 "bytes 2": h'79797979',
5314 "another int": 98,
5315 "text 2": "lies, damn lies and statistics"
5316 }
Laurence Lundbladeddebabe2020-11-22 11:32:17 -08005317 }
Laurence Lundbladebb87be22020-04-09 19:15:32 -07005318 */
Laurence Lundblade9b334962020-08-27 10:55:53 -07005319
Laurence Lundbladef7a70bc2020-10-24 12:23:25 -07005320int32_t SpiffyDecodeBasicMap(UsefulBufC input)
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07005321{
5322 QCBORItem Item1, Item2, Item3;
5323 int64_t nDecodedInt1, nDecodedInt2;
5324 UsefulBufC B1, B2, S1, S2, S3;
5325
5326 QCBORDecodeContext DCtx;
5327 QCBORError nCBORError;
5328
5329 QCBORDecode_Init(&DCtx, input, 0);
5330
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005331 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07005332
5333 QCBORDecode_GetInt64InMapSZ(&DCtx, "first integer", &nDecodedInt1);
5334
5335 QCBORDecode_EnterMapFromMapSZ(&DCtx, "map in a map");
5336 QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2);
Laurence Lundblade323f8a92020-09-06 19:43:09 -07005337 QCBORDecode_GetByteStringInMapSZ(&DCtx, "bytes 1", &B1);
5338 QCBORDecode_GetByteStringInMapSZ(&DCtx, "bytes 2", &B2);
5339 QCBORDecode_GetTextStringInMapSZ(&DCtx, "text 2", &S1);
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07005340 QCBORDecode_ExitMap(&DCtx);
5341
5342 QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings");
5343 QCBORDecode_GetNext(&DCtx, &Item1);
5344 QCBORDecode_GetNext(&DCtx, &Item2);
5345 if(QCBORDecode_GetNext(&DCtx, &Item3) != QCBOR_ERR_NO_MORE_ITEMS) {
5346 return -400;
5347 }
5348 QCBORDecode_ExitArray(&DCtx);
5349
5350 // Parse the same array again using GetText() instead of GetItem()
5351 QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings");
Laurence Lundblade323f8a92020-09-06 19:43:09 -07005352 QCBORDecode_GetTextString(&DCtx, &S2);
5353 QCBORDecode_GetTextString(&DCtx, &S3);
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07005354 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) {
5355 return 5000;
5356 }
5357 /* QCBORDecode_GetText(&DCtx, &S3);
5358 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_NO_MORE_ITEMS) {
5359 return 5001;
5360 } */
5361
5362 QCBORDecode_ExitArray(&DCtx);
5363
5364 QCBORDecode_ExitMap(&DCtx);
5365
5366 nCBORError = QCBORDecode_Finish(&DCtx);
5367
5368 if(nCBORError) {
5369 return (int32_t)nCBORError;
5370 }
5371
5372 if(nDecodedInt1 != 42) {
5373 return 1001;
5374 }
5375
5376 if(nDecodedInt2 != 98) {
5377 return 1002;
5378 }
5379
5380 if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07005381 UsefulBufCompareToSZ(Item1.val.string, "string1")) {
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07005382 return 1003;
5383 }
5384
5385 if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07005386 UsefulBufCompareToSZ(Item2.val.string, "string2")) {
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07005387 return 1004;
5388 }
5389
Laurence Lundblade9b334962020-08-27 10:55:53 -07005390 if(UsefulBufCompareToSZ(S1, "lies, damn lies and statistics")) {
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07005391 return 1005;
5392 }
5393
5394 if(UsefulBuf_Compare(B1, UsefulBuf_FromSZ("xxxx"))){
5395 return 1006;
5396 }
5397
5398 if(UsefulBuf_Compare(B2, UsefulBuf_FromSZ("yyyy"))){
5399 return 1007;
5400 }
5401
5402 if(UsefulBuf_Compare(S2, UsefulBuf_FromSZ("string1"))){
5403 return 1008;
5404 }
5405
5406 if(UsefulBuf_Compare(S3, UsefulBuf_FromSZ("string2"))){
5407 return 1009;
5408 }
5409
5410 return 0;
5411}
5412
Laurence Lundbladeddebabe2020-11-22 11:32:17 -08005413/*
Laurence Lundbladecc7da412020-12-27 00:09:07 -08005414 {
5415 -75008: h'05083399',
5416 88: [],
5417 100100: {
5418 "sub1": {
5419 10: [
5420 0
5421 ],
5422 -75009: h'A46823990001',
5423 100100: {
5424 "json": "{ \"ueid\", \"xyz\"}",
5425 "subsub": {
5426 100002: h'141813191001'
5427 }
5428 }
Laurence Lundbladeddebabe2020-11-22 11:32:17 -08005429 }
Laurence Lundbladecc7da412020-12-27 00:09:07 -08005430 }
Laurence Lundbladeddebabe2020-11-22 11:32:17 -08005431 }
Laurence Lundbladeddebabe2020-11-22 11:32:17 -08005432 */
5433
5434static const uint8_t spNestedCBOR[] = {
Laurence Lundbladecc7da412020-12-27 00:09:07 -08005435 0xa3, 0x3a, 0x00, 0x01, 0x24, 0xff, 0x44, 0x05,
5436 0x08, 0x33, 0x99, 0x18, 0x58, 0x80, 0x1a, 0x00,
5437 0x01, 0x87, 0x04, 0xa1, 0x64, 0x73, 0x75, 0x62,
5438 0x31, 0xa3, 0x0a, 0x81, 0x00, 0x3a, 0x00, 0x01,
5439 0x25, 0x00, 0x46, 0xa4, 0x68, 0x23, 0x99, 0x00,
5440 0x01, 0x1a, 0x00, 0x01, 0x87, 0x04, 0xa2, 0x64,
5441 0x6a, 0x73, 0x6f, 0x6e, 0x70, 0x7b, 0x20, 0x22,
5442 0x75, 0x65, 0x69, 0x64, 0x22, 0x2c, 0x20, 0x22,
5443 0x78, 0x79, 0x7a, 0x22, 0x7d, 0x66, 0x73, 0x75,
5444 0x62, 0x73, 0x75, 0x62, 0xa1, 0x1a, 0x00, 0x01,
5445 0x86, 0xa2, 0x46, 0x14, 0x18, 0x13, 0x19, 0x10,
5446 0x01
Laurence Lundbladeddebabe2020-11-22 11:32:17 -08005447};
5448
5449/* Get item in multi-level nesting in spNestedCBOR */
5450static int32_t DecodeNestedGetSubSub(QCBORDecodeContext *pDCtx)
5451{
5452 UsefulBufC String;
5453
5454 uint8_t test_oemid_bytes[] = {0x14, 0x18, 0x13, 0x19, 0x10, 0x01};
5455 const struct q_useful_buf_c test_oemid = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(test_oemid_bytes);
5456
5457 QCBORDecode_EnterMapFromMapN(pDCtx, 100100);
5458 QCBORDecode_EnterMap(pDCtx, NULL);
5459 QCBORDecode_EnterMapFromMapN(pDCtx, 100100);
5460 QCBORDecode_EnterMapFromMapSZ(pDCtx, "subsub");
5461 QCBORDecode_GetByteStringInMapN(pDCtx, 100002, &String);
5462 if(QCBORDecode_GetError(pDCtx)) {
5463 return 4001;
5464 }
5465 if(UsefulBuf_Compare(String, test_oemid)) {
5466 return 4002;
5467 }
5468 QCBORDecode_ExitMap(pDCtx);
5469 QCBORDecode_ExitMap(pDCtx);
5470 QCBORDecode_ExitMap(pDCtx);
5471 QCBORDecode_ExitMap(pDCtx);
5472
5473 return 0;
5474}
5475
5476/* Iterations on the zero-length array in spNestedCBOR */
5477static int32_t DecodeNestedGetEmpty(QCBORDecodeContext *pDCtx)
5478{
5479 QCBORItem Item;
5480 QCBORError uErr;
5481
5482 QCBORDecode_EnterArrayFromMapN(pDCtx, 88);
5483 for(int x = 0; x < 20; x++) {
5484 uErr = QCBORDecode_GetNext(pDCtx, &Item);
5485 if(uErr != QCBOR_ERR_NO_MORE_ITEMS) {
5486 return 4100;
5487
5488 }
5489 }
5490 QCBORDecode_ExitArray(pDCtx);
5491 if(QCBORDecode_GetError(pDCtx)) {
5492 return 4101;
5493 }
5494
5495 return 0;
5496}
5497
5498/* Various iterations on the array that contains a zero in spNestedCBOR */
5499static int32_t DecodeNestedGetZero(QCBORDecodeContext *pDCtx)
5500{
5501 QCBORError uErr;
5502
5503 QCBORDecode_EnterMapFromMapN(pDCtx, 100100);
5504 QCBORDecode_EnterMapFromMapSZ(pDCtx, "sub1");
5505 QCBORDecode_EnterArrayFromMapN(pDCtx, 10);
5506 int64_t nInt = 99;
5507 QCBORDecode_GetInt64(pDCtx, &nInt);
5508 if(nInt != 0) {
5509 return 4200;
5510 }
5511 for(int x = 0; x < 20; x++) {
5512 QCBORItem Item;
5513 uErr = QCBORDecode_GetNext(pDCtx, &Item);
5514 if(uErr != QCBOR_ERR_NO_MORE_ITEMS) {
5515 return 4201;
5516
5517 }
5518 }
5519 QCBORDecode_ExitArray(pDCtx);
5520 if(QCBORDecode_GetAndResetError(pDCtx)) {
5521 return 4202;
5522 }
5523 QCBORDecode_EnterArrayFromMapN(pDCtx, 10);
5524 UsefulBufC dD;
5525 QCBORDecode_GetByteString(pDCtx, &dD);
5526 if(QCBORDecode_GetAndResetError(pDCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
5527 return 4203;
5528 }
5529 for(int x = 0; x < 20; x++) {
5530 QCBORDecode_GetByteString(pDCtx, &dD);
5531 uErr = QCBORDecode_GetAndResetError(pDCtx);
5532 if(uErr != QCBOR_ERR_NO_MORE_ITEMS) {
5533 return 4204;
5534 }
5535 }
5536 QCBORDecode_ExitArray(pDCtx);
5537 QCBORDecode_ExitMap(pDCtx);
5538 QCBORDecode_ExitMap(pDCtx);
5539
5540 return 0;
5541}
5542
5543/* Repeatedly enter and exit maps and arrays, go off the end of maps
5544 and arrays and such. */
Laurence Lundbladeb9702452021-03-08 21:02:57 -08005545static int32_t DecodeNestedIterate(void)
Laurence Lundbladeddebabe2020-11-22 11:32:17 -08005546{
5547 QCBORDecodeContext DCtx;
5548 int32_t nReturn;
5549 QCBORError uErr;
5550
5551 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spNestedCBOR), 0);
5552 QCBORDecode_EnterMap(&DCtx, NULL);
5553
5554 for(int j = 0; j < 5; j++) {
5555 for(int i = 0; i < 20; i++) {
5556 nReturn = DecodeNestedGetSubSub(&DCtx);
5557 if(nReturn) {
5558 return nReturn;
5559 }
5560 }
5561
5562 for(int i = 0; i < 20; i++) {
5563 nReturn = DecodeNestedGetEmpty(&DCtx);
5564 if(nReturn ) {
5565 return nReturn;
5566 }
5567 }
5568
5569 for(int i = 0; i < 20; i++) {
5570 nReturn = DecodeNestedGetZero(&DCtx);
5571 if(nReturn ) {
5572 return nReturn;
5573 }
5574 }
5575 }
5576
5577 QCBORDecode_ExitMap(&DCtx);
5578 uErr = QCBORDecode_Finish(&DCtx);
5579 if(uErr) {
5580 return (int32_t)uErr + 4100;
5581 }
5582
5583 return 0;
5584}
5585
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07005586
5587/*
Laurence Lundbladecc7da412020-12-27 00:09:07 -08005588 [
5589 23,
5590 6000,
5591 h'67616C6163746963',
5592 h'686176656E20746F6B656E'
5593 ]
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07005594 */
5595static const uint8_t spSimpleArray[] = {
Laurence Lundblade9b334962020-08-27 10:55:53 -07005596 0x84,
5597 0x17,
5598 0x19, 0x17, 0x70,
5599 0x48, 0x67, 0x61, 0x6C, 0x61, 0x63, 0x74, 0x69, 0x63,
5600 0x4B, 0x68, 0x61, 0x76, 0x65, 0x6E, 0x20, 0x74, 0x6F, 0x6B, 0x65, 0x6E};
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07005601
Laurence Lundbladecc7da412020-12-27 00:09:07 -08005602/* [h'', {}, [], 0] */
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08005603static const uint8_t spArrayOfEmpty[] = {0x84, 0x40, 0xa0, 0x80, 0x00};
5604
Laurence Lundbladecc7da412020-12-27 00:09:07 -08005605/* {} */
Laurence Lundbladee6f15112020-07-23 18:44:16 -07005606static const uint8_t spEmptyMap[] = {0xa0};
5607
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08005608#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundbladecc7da412020-12-27 00:09:07 -08005609/* {} */
Laurence Lundbladee6f15112020-07-23 18:44:16 -07005610static const uint8_t spEmptyInDefinteLengthMap[] = {0xbf, 0xff};
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07005611
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08005612
Laurence Lundbladef0499502020-08-01 11:55:57 -07005613/*
5614 {
5615 0: [],
5616 9: [
5617 [],
5618 []
5619 ],
5620 8: {
5621 1: [],
5622 2: {},
5623 3: []
5624 },
5625 4: {},
5626 5: [],
5627 6: [
5628 [],
5629 []
5630 ]
Laurence Lundbladecc7da412020-12-27 00:09:07 -08005631 }
Laurence Lundbladef0499502020-08-01 11:55:57 -07005632 */
5633static const uint8_t spMapOfEmpty[] = {
Laurence Lundbladecc7da412020-12-27 00:09:07 -08005634 0xa6, 0x00, 0x80, 0x09, 0x82, 0x80, 0x80, 0x08,
5635 0xa3, 0x01, 0x80, 0x02, 0xa0, 0x03, 0x80, 0x04,
5636 0xa0, 0x05, 0x9f, 0xff, 0x06, 0x9f, 0x80, 0x9f,
5637 0xff, 0xff};
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005638
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08005639#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
5640
5641
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005642/*
5643 Too many tags
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005644 Duplicate label
5645 Integer overflow
5646 Date overflow
5647
Laurence Lundbladecc7da412020-12-27 00:09:07 -08005648 {
5649 1: 224(225(226(227(4(0))))),
Laurence Lundbladecc7da412020-12-27 00:09:07 -08005650 3: -18446744073709551616,
5651 4: 1(1.0e+300),
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07005652 5: 0,
5653 8: 8
Laurence Lundbladecc7da412020-12-27 00:09:07 -08005654 }
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005655 */
5656static const uint8_t spRecoverableMapErrors[] = {
Laurence Lundblade37286c02022-09-03 10:05:02 -07005657#ifndef QCBOR_DISABLE_TAGS
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07005658 0xa6,
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005659 0x04, 0xc1, 0xfb, 0x7e, 0x37, 0xe4, 0x3c, 0x88, 0x00, 0x75, 0x9c,
Laurence Lundblade37286c02022-09-03 10:05:02 -07005660 0x01, 0xd8, 0xe0, 0xd8, 0xe1, 0xd8, 0xe2, 0xd8, 0xe3, 0xd8, 0x04, 0x00,
5661#else
5662 0xa4,
5663#endif
5664 0x03, 0x3b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005665 0x05, 0x00,
5666 0x05, 0x00,
5667 0x08, 0x08,
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005668};
5669
Laurence Lundbladecc7da412020-12-27 00:09:07 -08005670/* Bad break */
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005671static const uint8_t spUnRecoverableMapError1[] = {
5672 0xa2, 0xff, 0x01, 0x00, 0x02, 0x00
5673};
5674
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08005675#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundbladecc7da412020-12-27 00:09:07 -08005676/* No more items */
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005677static const uint8_t spUnRecoverableMapError2[] = {
5678 0xbf, 0x02, 0xbf, 0xff, 0x01, 0x00, 0x02, 0x00
5679};
5680
Laurence Lundbladecc7da412020-12-27 00:09:07 -08005681/* Hit end because string is too long */
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005682static const uint8_t spUnRecoverableMapError3[] = {
5683 0xbf, 0x02, 0x69, 0x64, 0x64, 0xff
5684};
5685
Laurence Lundbladecc7da412020-12-27 00:09:07 -08005686/* Hit end because string is too long */
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005687static const uint8_t spUnRecoverableMapError4[] = {
5688 0xbf,
5689 0x02, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f,
5690 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f,
5691 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
5692 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
5693 0xff
5694};
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08005695#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005696
Laurence Lundblade63926052021-03-29 16:05:51 -07005697const unsigned char not_well_formed_submod_section[] = {
5698 0xa1, 0x14, 0x1f,
5699};
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005700
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07005701
5702/* Array of length 3, but only two items. */
5703const unsigned char spBadConsumeInput[] = {
5704 0x83, 0x00, 0x00
5705};
5706
5707/* Tag nesting too deep. */
5708const unsigned char spBadConsumeInput2[] = {
5709 0x81,
5710 0xD8, 0x37,
5711 0xD8, 0x2C,
5712 0xD8, 0x21,
5713 0xD6,
5714 0xCB,
5715 00
5716};
5717
5718const unsigned char spBadConsumeInput3[] = {
5719 0x81, 0xc0, 0x81, 0x00
5720};
5721
5722const unsigned char spBadConsumeInput4[] = {
5723 0x81, 0x9f, 0x00, 0xff
5724};
5725
5726const unsigned char spBadConsumeInput5[] = {
5727 0xa1, 0x80, 0x00
5728};
5729
5730
5731
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03005732int32_t EnterMapTest(void)
Laurence Lundbladebb87be22020-04-09 19:15:32 -07005733{
Laurence Lundbladef0499502020-08-01 11:55:57 -07005734 QCBORItem Item1;
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005735 QCBORItem ArrayItem;
Laurence Lundbladebb87be22020-04-09 19:15:32 -07005736 QCBORDecodeContext DCtx;
Laurence Lundbladef0499502020-08-01 11:55:57 -07005737 int32_t nReturn;
5738 QCBORError uErr;
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005739
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08005740#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005741 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spMapOfEmpty), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005742 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005743
Laurence Lundbladef0499502020-08-01 11:55:57 -07005744
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005745 QCBORDecode_EnterArray(&DCtx, NULL); // Label 0
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005746 QCBORDecode_ExitArray(&DCtx);
5747
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005748 QCBORDecode_EnterArray(&DCtx, NULL); // Label 9
5749 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005750 QCBORDecode_ExitArray(&DCtx);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005751 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005752 QCBORDecode_ExitArray(&DCtx);
5753 QCBORDecode_ExitArray(&DCtx);
5754
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005755 QCBORDecode_EnterMap(&DCtx, NULL); // Label 8
5756 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005757 QCBORDecode_ExitArray(&DCtx);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005758 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005759 QCBORDecode_ExitMap(&DCtx);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005760 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005761 QCBORDecode_ExitArray(&DCtx);
5762 QCBORDecode_ExitMap(&DCtx);
5763
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005764 QCBORDecode_EnterMap(&DCtx, NULL); // Label4
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005765 QCBORDecode_ExitMap(&DCtx);
5766
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005767 QCBORDecode_EnterArray(&DCtx, NULL); // Label 5
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005768 QCBORDecode_ExitArray(&DCtx);
5769
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005770 QCBORDecode_EnterArray(&DCtx, NULL); // Label 6
5771 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005772 QCBORDecode_ExitArray(&DCtx);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005773 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005774 QCBORDecode_ExitArray(&DCtx);
5775 QCBORDecode_ExitArray(&DCtx);
5776
5777 QCBORDecode_ExitMap(&DCtx);
5778
5779 uErr = QCBORDecode_Finish(&DCtx);
5780 if(uErr != QCBOR_SUCCESS){
5781 return 3011;
5782 }
5783
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07005784 (void)pValidMapIndefEncoded;
Laurence Lundbladef7a70bc2020-10-24 12:23:25 -07005785 nReturn = SpiffyDecodeBasicMap(UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapIndefEncoded));
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07005786 if(nReturn) {
5787 return nReturn + 20000;
Laurence Lundbladebb87be22020-04-09 19:15:32 -07005788 }
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08005789#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
5790
Laurence Lundbladebb87be22020-04-09 19:15:32 -07005791
Laurence Lundbladef7a70bc2020-10-24 12:23:25 -07005792 nReturn = SpiffyDecodeBasicMap(UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded));
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005793 if(nReturn) {
5794 return nReturn;
5795 }
Laurence Lundbladebb87be22020-04-09 19:15:32 -07005796
Laurence Lundblade8ffdb742020-05-07 02:49:18 -07005797
Laurence Lundblade937ea812020-05-08 11:38:23 -07005798
Laurence Lundblade2f467f92020-10-09 17:50:11 -07005799 // These tests confirm the cursor is at the right place after entering
5800 // a map or array
Laurence Lundblade9b334962020-08-27 10:55:53 -07005801 const UsefulBufC ValidEncodedMap = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded);
Laurence Lundblade937ea812020-05-08 11:38:23 -07005802
5803 // Confirm cursor is at right place
Laurence Lundblade9b334962020-08-27 10:55:53 -07005804 QCBORDecode_Init(&DCtx, ValidEncodedMap, 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005805 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade937ea812020-05-08 11:38:23 -07005806 QCBORDecode_GetNext(&DCtx, &Item1);
5807 if(Item1.uDataType != QCBOR_TYPE_INT64) {
5808 return 2001;
5809 }
5810
5811
Laurence Lundblade9b334962020-08-27 10:55:53 -07005812 QCBORDecode_Init(&DCtx, ValidEncodedMap, 0);
Laurence Lundblade6f3f78e2020-08-31 13:09:14 -07005813 QCBORDecode_VGetNext(&DCtx, &Item1);
5814 QCBORDecode_VGetNext(&DCtx, &Item1);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005815 QCBORDecode_EnterArray(&DCtx, &ArrayItem);
5816 if(ArrayItem.uLabelType != QCBOR_TYPE_TEXT_STRING ||
5817 UsefulBuf_Compare(ArrayItem.label.string,
5818 UsefulBuf_FROM_SZ_LITERAL("an array of two strings"))) {
5819 return 2051;
5820 }
Laurence Lundblade937ea812020-05-08 11:38:23 -07005821 QCBORDecode_GetNext(&DCtx, &Item1);
5822 if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING) {
5823 return 2002;
5824 }
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005825 QCBORDecode_ExitArray(&DCtx);
5826 QCBORDecode_EnterMap(&DCtx, &ArrayItem);
5827 if(ArrayItem.uLabelType != QCBOR_TYPE_TEXT_STRING ||
5828 UsefulBuf_Compare(ArrayItem.label.string,
5829 UsefulBuf_FROM_SZ_LITERAL("map in a map"))) {
5830 return 2052;
5831 }
5832
Laurence Lundblade937ea812020-05-08 11:38:23 -07005833
Laurence Lundblade9b334962020-08-27 10:55:53 -07005834 QCBORDecode_Init(&DCtx, ValidEncodedMap, 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005835 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade64b607e2020-05-13 13:05:57 -07005836 QCBORDecode_GetNext(&DCtx, &Item1);
5837 QCBORDecode_GetNext(&DCtx, &Item1);
5838 QCBORDecode_GetNext(&DCtx, &Item1);
Laurence Lundblade937ea812020-05-08 11:38:23 -07005839 QCBORDecode_EnterMapFromMapSZ(&DCtx, "map in a map");
5840 QCBORDecode_GetNext(&DCtx, &Item1);
5841 if(Item1.uDataType != QCBOR_TYPE_BYTE_STRING) {
5842 return 2003;
5843 }
5844
Laurence Lundblade9b334962020-08-27 10:55:53 -07005845 QCBORDecode_Init(&DCtx, ValidEncodedMap, 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005846 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade937ea812020-05-08 11:38:23 -07005847 QCBORDecode_GetNext(&DCtx, &Item1);
5848 QCBORDecode_GetNext(&DCtx, &Item1);
5849 QCBORDecode_GetNext(&DCtx, &Item1);
5850 QCBORDecode_GetNext(&DCtx, &Item1);
5851 QCBORDecode_GetNext(&DCtx, &Item1);
5852 QCBORDecode_GetNext(&DCtx, &Item1);
5853 QCBORDecode_GetNext(&DCtx, &Item1);
5854 QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings");
5855 QCBORDecode_GetNext(&DCtx, &Item1);
5856 if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING) {
Laurence Lundblade64b607e2020-05-13 13:05:57 -07005857 return 2004;
Laurence Lundblade937ea812020-05-08 11:38:23 -07005858 }
5859
Laurence Lundblade9b334962020-08-27 10:55:53 -07005860 QCBORDecode_Init(&DCtx, ValidEncodedMap, 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005861 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade2b843b52020-06-16 20:51:03 -07005862 QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings");
5863 QCBORDecode_ExitArray(&DCtx);
5864 QCBORDecode_GetNext(&DCtx, &Item1);
5865 if(Item1.uDataType != QCBOR_TYPE_MAP && Item1.uLabelAlloc != QCBOR_TYPE_TEXT_STRING) {
5866 return 2006;
5867 }
5868 QCBORDecode_ExitMap(&DCtx);
5869 if(QCBORDecode_GetNext(&DCtx, &Item1) != QCBOR_ERR_NO_MORE_ITEMS) {
5870 return 2007;
5871 }
5872
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07005873 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSimpleArray), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005874 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07005875 int64_t nDecodedInt2;
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005876 QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2);
5877 uErr = QCBORDecode_GetAndResetError(&DCtx);
5878 if(uErr != QCBOR_ERR_MAP_NOT_ENTERED){
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07005879 return 2008;
5880 }
5881 UsefulBufC String;
Laurence Lundblade323f8a92020-09-06 19:43:09 -07005882 QCBORDecode_GetTextStringInMapN(&DCtx, 88, &String);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005883 if(uErr != QCBOR_ERR_MAP_NOT_ENTERED){
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07005884 return 2009;
5885 }
Laurence Lundblade937ea812020-05-08 11:38:23 -07005886
Laurence Lundbladee6f15112020-07-23 18:44:16 -07005887
5888 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEmptyMap), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005889 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundbladee6f15112020-07-23 18:44:16 -07005890 // This will fail because the map is empty.
5891 QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2);
5892 uErr = QCBORDecode_GetAndResetError(&DCtx);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07005893 if(uErr != QCBOR_ERR_LABEL_NOT_FOUND){
Laurence Lundbladee6f15112020-07-23 18:44:16 -07005894 return 2010;
5895 }
5896 QCBORDecode_ExitMap(&DCtx);
5897 uErr = QCBORDecode_Finish(&DCtx);
5898 if(uErr != QCBOR_SUCCESS){
5899 return 2011;
5900 }
5901
5902
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08005903#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundbladee6f15112020-07-23 18:44:16 -07005904 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEmptyInDefinteLengthMap), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005905 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundbladee6f15112020-07-23 18:44:16 -07005906 // This will fail because the map is empty.
5907 QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2);
5908 uErr = QCBORDecode_GetAndResetError(&DCtx);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07005909 if(uErr != QCBOR_ERR_LABEL_NOT_FOUND){
Laurence Lundblade085d7952020-07-24 10:26:30 -07005910 return 2012;
Laurence Lundbladee6f15112020-07-23 18:44:16 -07005911 }
5912 QCBORDecode_ExitMap(&DCtx);
5913 uErr = QCBORDecode_Finish(&DCtx);
5914 if(uErr != QCBOR_SUCCESS){
Laurence Lundblade085d7952020-07-24 10:26:30 -07005915 return 2013;
Laurence Lundbladee6f15112020-07-23 18:44:16 -07005916 }
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08005917#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
Laurence Lundbladee6f15112020-07-23 18:44:16 -07005918
Laurence Lundblade5f4e8712020-07-25 11:44:43 -07005919
5920 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spArrayOfEmpty), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005921 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade323f8a92020-09-06 19:43:09 -07005922 QCBORDecode_GetByteString(&DCtx, &String);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005923 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade5f4e8712020-07-25 11:44:43 -07005924 QCBORDecode_ExitMap(&DCtx);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005925 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade5f4e8712020-07-25 11:44:43 -07005926 QCBORDecode_ExitArray(&DCtx);
5927 QCBORDecode_GetInt64(&DCtx, &nDecodedInt2);
5928 QCBORDecode_ExitArray(&DCtx);
5929 uErr = QCBORDecode_Finish(&DCtx);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005930 if(uErr != QCBOR_SUCCESS) {
Laurence Lundblade5f4e8712020-07-25 11:44:43 -07005931 return 2014;
5932 }
5933
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005934 int64_t nInt;
5935 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spRecoverableMapErrors), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005936 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade37286c02022-09-03 10:05:02 -07005937#ifndef QCBOR_DISABLE_TAGS
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005938 QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt);
Laurence Lundblade88e9db22020-11-02 03:56:33 -08005939 uErr = QCBORDecode_GetError(&DCtx);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005940 if(uErr != QCBOR_ERR_TOO_MANY_TAGS) {
5941 return 2021;
5942 }
Laurence Lundblade88e9db22020-11-02 03:56:33 -08005943 if(QCBORDecode_GetNthTagOfLast(&DCtx, 0) != CBOR_TAG_INVALID64) {
5944 return 2121;
5945 }
5946 (void)QCBORDecode_GetAndResetError(&DCtx);
Laurence Lundblade37286c02022-09-03 10:05:02 -07005947#endif
Laurence Lundblade88e9db22020-11-02 03:56:33 -08005948
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005949
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005950 QCBORDecode_GetInt64InMapN(&DCtx, 0x03, &nInt);
5951 uErr = QCBORDecode_GetAndResetError(&DCtx);
5952 if(uErr != QCBOR_ERR_INT_OVERFLOW) {
5953 return 2023;
5954 }
5955
Laurence Lundblade37286c02022-09-03 10:05:02 -07005956#ifndef QCBOR_DISABLE_TAGS
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005957 QCBORDecode_GetEpochDateInMapN(&DCtx, 0x04, QCBOR_TAG_REQUIREMENT_TAG, &nInt);
5958 uErr = QCBORDecode_GetAndResetError(&DCtx);
Laurence Lundblade16a207a2021-09-18 17:22:46 -07005959 if(uErr != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_DATE_OVERFLOW)) {
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005960 return 2024;
5961 }
Laurence Lundblade37286c02022-09-03 10:05:02 -07005962#endif
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005963
5964 QCBORDecode_GetInt64InMapN(&DCtx, 0x05, &nInt);
5965 uErr = QCBORDecode_GetAndResetError(&DCtx);
5966 if(uErr != QCBOR_ERR_DUPLICATE_LABEL) {
5967 return 2025;
5968 }
5969
5970 QCBORDecode_GetInt64InMapN(&DCtx, 0x08, &nInt);
5971
5972 QCBORDecode_ExitMap(&DCtx);
5973 uErr = QCBORDecode_Finish(&DCtx);
5974 if(uErr != QCBOR_SUCCESS) {
5975 return 2026;
5976 }
5977
5978 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUnRecoverableMapError1), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005979 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005980 QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt);
5981 uErr = QCBORDecode_GetAndResetError(&DCtx);
5982 if(uErr != QCBOR_ERR_BAD_BREAK) {
5983 return 2030;
5984 }
5985
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08005986#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005987 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUnRecoverableMapError2), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005988 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005989 QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt);
5990 uErr = QCBORDecode_GetAndResetError(&DCtx);
5991 if(uErr != QCBOR_ERR_NO_MORE_ITEMS) {
5992 return 2031;
5993 }
5994
5995 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUnRecoverableMapError3), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005996 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005997 QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt);
5998 uErr = QCBORDecode_GetAndResetError(&DCtx);
5999 if(uErr != QCBOR_ERR_HIT_END) {
6000 return 2032;
6001 }
6002
6003 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUnRecoverableMapError4), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006004 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07006005 QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt);
6006 uErr = QCBORDecode_GetAndResetError(&DCtx);
6007 if(uErr != QCBOR_ERR_ARRAY_DECODE_NESTING_TOO_DEEP) {
6008 return 2033;
6009 }
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08006010#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
6011
Laurence Lundblade732e52d2021-02-22 20:11:01 -07006012 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0);
6013 QCBORDecode_VGetNextConsume(&DCtx, &Item1);
6014 if(Item1.uDataType != QCBOR_TYPE_MAP) {
6015 return 2401;
6016 }
6017 if(QCBORDecode_GetError(&DCtx)) {
6018 return 2402;
6019 }
6020
6021 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0);
6022 QCBORDecode_VGetNext(&DCtx, &Item1);
6023 if(Item1.uDataType != QCBOR_TYPE_MAP ||
6024 Item1.val.uCount != 3 ||
6025 Item1.uNextNestLevel != 1) {
6026 return 2403;
6027 }
6028 if(QCBORDecode_GetError(&DCtx)) {
6029 return 2404;
6030 }
6031 QCBORDecode_VGetNextConsume(&DCtx, &Item1);
6032 if(Item1.uDataType != QCBOR_TYPE_INT64 ||
6033 Item1.uNextNestLevel != 1 ||
6034 Item1.val.int64 != 42) {
6035 return 2405;
6036 }
6037 if(QCBORDecode_GetError(&DCtx)) {
6038 return 2406;
6039 }
6040 QCBORDecode_VGetNextConsume(&DCtx, &Item1);
6041 if(Item1.uDataType != QCBOR_TYPE_ARRAY ||
6042 Item1.uNestingLevel != 1 ||
6043 Item1.uNextNestLevel != 1 ||
6044 Item1.val.uCount != 2) {
6045 return 2407;
6046 }
6047 if(QCBORDecode_GetError(&DCtx)) {
6048 return 2408;
6049 }
6050 QCBORDecode_VGetNextConsume(&DCtx, &Item1);
6051 if(Item1.uDataType != QCBOR_TYPE_MAP ||
6052 Item1.uNestingLevel != 1 ||
6053 Item1.uNextNestLevel != 0 ||
6054 Item1.val.uCount != 4) {
6055 return 2409;
6056 }
6057 if(QCBORDecode_GetError(&DCtx)) {
6058 return 2410;
6059 }
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006060
Laurence Lundbladeddebabe2020-11-22 11:32:17 -08006061 nReturn = DecodeNestedIterate();
6062
Laurence Lundblade63926052021-03-29 16:05:51 -07006063
6064 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(not_well_formed_submod_section), 0);
6065 QCBORDecode_EnterMap(&DCtx, NULL);
6066 QCBORDecode_EnterMapFromMapN(&DCtx, 20);
6067 if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_BAD_INT) {
6068 return 2500;
6069 }
6070
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07006071 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBadConsumeInput), 0);
6072 QCBORDecode_VGetNextConsume(&DCtx, &Item1);
6073 if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_NO_MORE_ITEMS) {
6074 return 2600;
6075 }
6076
Laurence Lundblade37286c02022-09-03 10:05:02 -07006077#ifndef QCBOR_DISABLE_TAGS
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07006078 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBadConsumeInput2), 0);
6079 QCBORDecode_VGetNextConsume(&DCtx, &Item1);
6080 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) {
6081 return 2700;
6082 }
6083
6084 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBadConsumeInput3), 0);
6085 QCBORDecode_VGetNextConsume(&DCtx, &Item1);
6086 if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT) {
6087 return 2800;
6088 }
Laurence Lundblade37286c02022-09-03 10:05:02 -07006089#endif
6090
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07006091
6092 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBadConsumeInput4), 0);
6093 QCBORDecode_VGetNextConsume(&DCtx, &Item1);
6094#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
6095 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) {
6096 return 2900;
6097 }
6098#else
6099 if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_INDEF_LEN_ARRAYS_DISABLED) {
6100 return 2901;
6101 }
6102#endif
6103
6104 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBadConsumeInput5), 0);
6105 QCBORDecode_VGetNextConsume(&DCtx, &Item1);
6106 if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_MAP_LABEL_TYPE) {
6107 return 3000;
6108 }
6109
Laurence Lundbladeddebabe2020-11-22 11:32:17 -08006110 return nReturn;
Laurence Lundblade9c905e82020-04-25 11:31:38 -07006111}
6112
6113
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006114struct NumberConversion {
6115 char *szDescription;
6116 UsefulBufC CBOR;
6117 int64_t nConvertedToInt64;
6118 QCBORError uErrorInt64;
6119 uint64_t uConvertToUInt64;
6120 QCBORError uErrorUint64;
6121 double dConvertToDouble;
6122 QCBORError uErrorDouble;
6123};
6124
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006125#ifndef QCBOR_DISABLE_EXP_AND_MANTISSA
6126#define EXP_AND_MANTISSA_ERROR(x) x
6127#else
6128#define EXP_AND_MANTISSA_ERROR(x) QCBOR_ERR_UNEXPECTED_TYPE
6129#endif
6130
6131
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07006132static const struct NumberConversion NumberConversions[] = {
Laurence Lundblade37286c02022-09-03 10:05:02 -07006133#ifndef QCBOR_DISABLE_TAGS
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006134 {
Laurence Lundblade784b54b2020-08-10 01:24:52 -07006135 "too large to fit into int64_t",
6136 {(uint8_t[]){0xc3, 0x48, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 10},
6137 0,
6138 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
6139 0,
6140 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
6141 ((double)INT64_MIN) + 1 ,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006142 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)
Laurence Lundblade784b54b2020-08-10 01:24:52 -07006143 },
6144 {
6145 "largest negative int that fits in int64_t",
6146 {(uint8_t[]){0xc3, 0x48, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, 10},
6147 INT64_MIN,
6148 QCBOR_SUCCESS,
6149 0,
6150 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
6151 (double)INT64_MIN,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006152 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)
Laurence Lundblade784b54b2020-08-10 01:24:52 -07006153 },
6154 {
Laurence Lundbladeda095972020-06-06 18:35:33 -07006155 "negative bignum -1",
6156 {(uint8_t[]){0xc3, 0x41, 0x00}, 3},
6157 -1,
6158 QCBOR_SUCCESS,
6159 0,
6160 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
6161 -1.0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006162 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)
Laurence Lundbladeda095972020-06-06 18:35:33 -07006163 },
6164 {
Laurence Lundblade9ab5abb2020-05-20 12:10:45 -07006165 "Decimal Fraction with positive bignum 257 * 10e3",
Laurence Lundblade887add82020-05-17 05:50:34 -07006166 {(uint8_t[]){0xC4, 0x82, 0x1B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03,
6167 0xC2, 0x42, 0x01, 0x01}, 15},
6168 257000,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006169 EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS),
Laurence Lundblade887add82020-05-17 05:50:34 -07006170 257000,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006171 EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS),
Laurence Lundblade887add82020-05-17 05:50:34 -07006172 257000.0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006173 FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS))
Laurence Lundblade887add82020-05-17 05:50:34 -07006174 },
6175 {
Laurence Lundbladeda095972020-06-06 18:35:33 -07006176 "bigfloat with negative bignum -258 * 2e3",
Laurence Lundblade887add82020-05-17 05:50:34 -07006177 {(uint8_t[]){0xC5, 0x82, 0x1B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03,
6178 0xC3, 0x42, 0x01, 0x01}, 15},
Laurence Lundbladeda095972020-06-06 18:35:33 -07006179 -2064,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006180 EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS),
Laurence Lundblade887add82020-05-17 05:50:34 -07006181 0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006182 EXP_AND_MANTISSA_ERROR(QCBOR_ERR_NUMBER_SIGN_CONVERSION),
Laurence Lundbladeda095972020-06-06 18:35:33 -07006183 -2064.0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006184 FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS))
Laurence Lundblade887add82020-05-17 05:50:34 -07006185 },
6186 {
Laurence Lundblade9ab5abb2020-05-20 12:10:45 -07006187 "bigfloat with positive bignum 257 * 2e3",
Laurence Lundblade887add82020-05-17 05:50:34 -07006188 {(uint8_t[]){0xC5, 0x82, 0x1B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03,
6189 0xC2, 0x42, 0x01, 0x01}, 15},
6190 2056,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006191 EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS),
Laurence Lundblade887add82020-05-17 05:50:34 -07006192 2056,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006193 EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS),
Laurence Lundblade887add82020-05-17 05:50:34 -07006194 2056.0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006195 FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS))
Laurence Lundblade887add82020-05-17 05:50:34 -07006196 },
6197 {
Laurence Lundbladeda095972020-06-06 18:35:33 -07006198 "negative bignum 0xc349010000000000000000 -18446744073709551617",
Laurence Lundblade887add82020-05-17 05:50:34 -07006199 {(uint8_t[]){0xc3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 11},
6200 0,
6201 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
6202 0,
6203 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
6204 -18446744073709551617.0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006205 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)
Laurence Lundblade887add82020-05-17 05:50:34 -07006206 },
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08006207#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
Laurence Lundblade887add82020-05-17 05:50:34 -07006208 {
Laurence Lundblade313b2862020-05-16 01:23:06 -07006209 "Positive bignum 0x01020304 indefinite length string",
6210 {(uint8_t[]){0xC2, 0x5f, 0x42, 0x01, 0x02, 0x41, 0x03, 0x41, 0x04, 0xff}, 10},
6211 0x01020304,
6212 QCBOR_SUCCESS,
6213 0x01020304,
6214 QCBOR_SUCCESS,
6215 16909060.0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006216 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)
Laurence Lundblade313b2862020-05-16 01:23:06 -07006217 },
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08006218#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
Laurence Lundblade313b2862020-05-16 01:23:06 -07006219 {
Laurence Lundblade887add82020-05-17 05:50:34 -07006220 "Decimal Fraction with neg bignum [9223372036854775807, -4759477275222530853137]",
Laurence Lundblade313b2862020-05-16 01:23:06 -07006221 {(uint8_t[]){0xC4, 0x82, 0x1B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
6222 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,}, 23},
6223 0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006224 EXP_AND_MANTISSA_ERROR(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW),
Laurence Lundblade313b2862020-05-16 01:23:06 -07006225 0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006226 EXP_AND_MANTISSA_ERROR(QCBOR_ERR_NUMBER_SIGN_CONVERSION),
Laurence Lundblade313b2862020-05-16 01:23:06 -07006227 -INFINITY,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006228 FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS))
Laurence Lundblade313b2862020-05-16 01:23:06 -07006229 },
6230 {
6231 "big float [9223372036854775806, 9223372036854775806]",
6232 {(uint8_t[]){0xC5, 0x82, 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE,
6233 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE}, 20},
6234 0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006235 EXP_AND_MANTISSA_ERROR(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW),
Laurence Lundblade313b2862020-05-16 01:23:06 -07006236 0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006237 EXP_AND_MANTISSA_ERROR(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW),
Laurence Lundblade313b2862020-05-16 01:23:06 -07006238 INFINITY,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006239 FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS))
Laurence Lundblade313b2862020-05-16 01:23:06 -07006240 },
6241 {
Laurence Lundblade983500d2020-05-14 11:49:34 -07006242 "Big float 3 * 2^^2",
6243 {(uint8_t[]){0xC5, 0x82, 0x02, 0x03}, 4},
6244 12,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006245 EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS),
Laurence Lundblade983500d2020-05-14 11:49:34 -07006246 12,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006247 EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS),
Laurence Lundblade983500d2020-05-14 11:49:34 -07006248 12.0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006249 FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS))
Laurence Lundblade983500d2020-05-14 11:49:34 -07006250 },
Laurence Lundblade983500d2020-05-14 11:49:34 -07006251 {
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006252 "Decimal fraction 3/10",
6253 {(uint8_t[]){0xC4, 0x82, 0x20, 0x03}, 4},
6254 0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006255 EXP_AND_MANTISSA_ERROR(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW),
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006256 0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006257 EXP_AND_MANTISSA_ERROR(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW),
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006258 0.30000000000000004,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006259 FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS))
Laurence Lundbladedfd49fc2020-09-01 14:17:16 -07006260 },
6261 {
Laurence Lundblade11fd78b2020-09-01 22:13:27 -07006262 "extreme pos bignum",
6263 {(uint8_t[]){0xc2, 0x59, 0x01, 0x90,
6264 // 50 rows of 8 is 400 digits.
6265 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6266 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6267 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6268 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6269 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6270 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6271 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6272 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6273 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6274 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6275 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6276 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6277 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6278 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6279 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6280 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6281 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6282 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6283 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6284 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6285 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6286 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6287 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6288 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6289 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6290 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6291 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6292 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6293 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6294 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6295 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6296 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6297 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6298 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6299 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6300 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6301 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6302 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6303 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6304 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6305 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6306 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6307 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6308 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6309 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6310 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6311 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6312 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6313 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6314 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0},
6315 404},
6316 0,
6317 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
6318 0,
6319 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
Laurence Lundblade11fd78b2020-09-01 22:13:27 -07006320 INFINITY,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006321 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS),
Laurence Lundblade11fd78b2020-09-01 22:13:27 -07006322 },
6323
6324 {
6325 "extreme neg bignum",
6326 {(uint8_t[]){0xc3, 0x59, 0x01, 0x90,
6327 // 50 rows of 8 is 400 digits.
6328 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6329 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6330 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6331 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6332 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6333 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6334 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6335 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6336 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6337 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6338 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6339 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6340 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6341 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6342 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6343 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6344 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6345 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6346 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6347 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6348 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6349 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6350 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6351 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6352 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6353 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6354 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6355 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6356 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6357 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6358 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6359 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6360 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6361 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6362 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6363 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6364 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6365 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6366 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6367 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6368 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6369 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6370 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6371 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6372 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6373 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6374 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6375 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6376 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6377 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0},
6378 404},
6379 0,
6380 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
6381 0,
6382 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
Laurence Lundblade11fd78b2020-09-01 22:13:27 -07006383 -INFINITY,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006384 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)
Laurence Lundblade11fd78b2020-09-01 22:13:27 -07006385 },
Laurence Lundblade51722fd2020-09-02 13:01:33 -07006386
6387 {
6388 "big float underflow [9223372036854775806, -9223372036854775806]",
6389 {(uint8_t[]){
6390 0xC5, 0x82,
6391 0x3B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE,
6392 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE}, 20},
Laurence Lundblade51722fd2020-09-02 13:01:33 -07006393 0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006394 EXP_AND_MANTISSA_ERROR(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW),
Laurence Lundblade51722fd2020-09-02 13:01:33 -07006395 0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006396 EXP_AND_MANTISSA_ERROR(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW),
Laurence Lundblade51722fd2020-09-02 13:01:33 -07006397 0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006398 FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS))
Laurence Lundblade51722fd2020-09-02 13:01:33 -07006399 },
6400
6401 {
6402 "bigfloat that evaluates to -INFINITY",
6403 {(uint8_t[]){
6404 0xC5, 0x82,
6405 0x1B, 0x70, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03,
6406 0xC3, 0x42, 0x01, 0x01}, 15},
Laurence Lundblade51722fd2020-09-02 13:01:33 -07006407 0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006408 EXP_AND_MANTISSA_ERROR(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW),
Laurence Lundblade51722fd2020-09-02 13:01:33 -07006409 0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006410 EXP_AND_MANTISSA_ERROR(QCBOR_ERR_NUMBER_SIGN_CONVERSION),
Laurence Lundblade51722fd2020-09-02 13:01:33 -07006411 -INFINITY,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006412 FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS))
Laurence Lundblade51722fd2020-09-02 13:01:33 -07006413 },
Laurence Lundblade37286c02022-09-03 10:05:02 -07006414 {
6415 "Positive bignum 0xffff",
6416 {(uint8_t[]){0xC2, 0x42, 0xff, 0xff}, 4},
6417 65536-1,
6418 QCBOR_SUCCESS,
6419 0xffff,
6420 QCBOR_SUCCESS,
6421 65535.0,
6422 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)
6423 },
6424#endif /* QCBOR_DISABLE_TAGS */
6425 {
6426 "Positive integer 18446744073709551615",
6427 {(uint8_t[]){0x1b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, 9},
6428 0,
6429 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
6430 18446744073709551615ULL,
6431 QCBOR_SUCCESS,
6432 18446744073709551615.0,
6433 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)
6434 },
6435
6436 {
6437 "Postive integer 0",
6438 {(uint8_t[]){0x0}, 1},
6439 0LL,
6440 QCBOR_SUCCESS,
6441 0ULL,
6442 QCBOR_SUCCESS,
6443 0.0,
6444 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)
6445 },
6446 {
6447 "Negative integer -18446744073709551616",
6448 {(uint8_t[]){0x3b, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }, 9},
6449 -9223372036854775807-1, // INT64_MIN
6450 QCBOR_SUCCESS,
6451 0ULL,
6452 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
6453 -9223372036854775808.0,
6454 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)
6455 },
6456 {
6457 "Double Floating point value 100.3",
6458 {(uint8_t[]){0xfb, 0x40, 0x59, 0x13, 0x33, 0x33, 0x33, 0x33, 0x33}, 9},
6459 100L,
6460 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS),
6461 100ULL,
6462 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS),
6463 100.3,
6464 FLOAT_ERR_CODE_NO_FLOAT(QCBOR_SUCCESS),
6465 },
6466 {
6467 "Floating point value NaN 0xfa7fc00000",
6468 {(uint8_t[]){0xfa, 0x7f, 0xc0, 0x00, 0x00}, 5},
6469 0,
6470 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_FLOAT_EXCEPTION),
6471 0,
6472 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_FLOAT_EXCEPTION),
6473 NAN,
6474 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS),
6475 },
6476 {
6477 "half-precision Floating point value -4",
6478 {(uint8_t[]){0xf9, 0xc4, 0x00}, 3},
6479 // Normal case with all enabled.
6480 -4,
6481 FLOAT_ERR_CODE_NO_HALF_PREC_NO_FLOAT_HW(QCBOR_SUCCESS),
6482 0,
6483 FLOAT_ERR_CODE_NO_HALF_PREC_NO_FLOAT_HW(QCBOR_ERR_NUMBER_SIGN_CONVERSION),
6484 -4.0,
6485 FLOAT_ERR_CODE_NO_HALF_PREC(QCBOR_SUCCESS)
6486 },
6487 {
6488 "+inifinity single precision",
6489 {(uint8_t[]){0xfa, 0x7f, 0x80, 0x00, 0x00}, 5},
6490 0,
6491 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_FLOAT_EXCEPTION),
6492 0,
6493 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW),
6494 INFINITY,
6495 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)
6496 },
6497
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006498};
Laurence Lundblade9c905e82020-04-25 11:31:38 -07006499
6500
6501
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08006502
6503static int32_t SetUpDecoder(QCBORDecodeContext *DCtx, UsefulBufC CBOR, UsefulBuf Pool)
6504{
6505 QCBORDecode_Init(DCtx, CBOR, QCBOR_DECODE_MODE_NORMAL);
6506#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
6507 if(QCBORDecode_SetMemPool(DCtx, Pool, 0)) {
6508 return 1;
6509 }
6510#else /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
6511 (void)Pool;
6512#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
6513 return 0;
6514}
6515
6516
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03006517int32_t IntegerConvertTest(void)
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006518{
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08006519 const int nNumTests = C_ARRAY_COUNT(NumberConversions,
6520 struct NumberConversion);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006521
Laurence Lundblade9ab5abb2020-05-20 12:10:45 -07006522 for(int nIndex = 0; nIndex < nNumTests; nIndex++) {
6523 const struct NumberConversion *pF = &NumberConversions[nIndex];
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07006524
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006525 // Set up the decoding context including a memory pool so that
6526 // indefinite length items can be checked
6527 QCBORDecodeContext DCtx;
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006528 UsefulBuf_MAKE_STACK_UB(Pool, 100);
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07006529
6530 /* ----- test conversion to int64_t ------ */
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08006531 if(SetUpDecoder(&DCtx, pF->CBOR, Pool)) {
6532 return (int32_t)(3333+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006533 }
6534
6535 int64_t nInt;
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006536 QCBORDecode_GetInt64ConvertAll(&DCtx, 0xffff, &nInt);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07006537 if(QCBORDecode_GetError(&DCtx) != pF->uErrorInt64) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07006538 return (int32_t)(2000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006539 }
6540 if(pF->uErrorInt64 == QCBOR_SUCCESS && pF->nConvertedToInt64 != nInt) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07006541 return (int32_t)(3000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006542 }
6543
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07006544 /* ----- test conversion to uint64_t ------ */
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08006545 if(SetUpDecoder(&DCtx, pF->CBOR, Pool)) {
6546 return (int32_t)(3333+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006547 }
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08006548
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006549 uint64_t uInt;
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006550 QCBORDecode_GetUInt64ConvertAll(&DCtx, 0xffff, &uInt);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07006551 if(QCBORDecode_GetError(&DCtx) != pF->uErrorUint64) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07006552 return (int32_t)(4000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006553 }
6554 if(pF->uErrorUint64 == QCBOR_SUCCESS && pF->uConvertToUInt64 != uInt) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07006555 return (int32_t)(5000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006556 }
6557
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07006558 /* ----- test conversion to double ------ */
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08006559 if(SetUpDecoder(&DCtx, pF->CBOR, Pool)) {
6560 return (int32_t)(3333+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006561 }
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006562
6563#ifndef USEFULBUF_DISABLE_ALL_FLOAT
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006564 double d;
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006565 QCBORDecode_GetDoubleConvertAll(&DCtx, 0xffff, &d);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07006566 if(QCBORDecode_GetError(&DCtx) != pF->uErrorDouble) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07006567 return (int32_t)(6000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006568 }
6569 if(pF->uErrorDouble == QCBOR_SUCCESS) {
6570 if(isnan(pF->dConvertToDouble)) {
Laurence Lundblade983500d2020-05-14 11:49:34 -07006571 // NaN's can't be compared for equality. A NaN is
6572 // never equal to anything including another NaN
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006573 if(!isnan(d)) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07006574 return (int32_t)(7000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006575 }
6576 } else {
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006577 if(pF->dConvertToDouble != d) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07006578 return (int32_t)(8000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006579 }
6580 }
6581 }
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006582#endif /* USEFULBUF_DISABLE_ALL_FLOAT */
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006583 }
6584
6585 return 0;
6586}
6587
Laurence Lundbladea8758502022-05-15 17:57:46 -07006588#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
6589
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03006590int32_t CBORTestIssue134(void)
David Navarro9123e5b2022-03-28 16:04:03 +02006591{
6592 QCBORDecodeContext DCtx;
6593 QCBORItem Item;
6594 QCBORError uCBORError;
6595 const uint8_t spTestIssue134[] = { 0x5F, 0x40, 0xFF };
Laurence Lundblade9c905e82020-04-25 11:31:38 -07006596
David Navarro9123e5b2022-03-28 16:04:03 +02006597 QCBORDecode_Init(&DCtx,
6598 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTestIssue134),
6599 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade97c61bf2020-05-02 11:24:06 -07006600
David Navarro9123e5b2022-03-28 16:04:03 +02006601 UsefulBuf_MAKE_STACK_UB(StringBuf, 200);
6602 QCBORDecode_SetMemPool(&DCtx, StringBuf, false);
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03006603
David Navarro9123e5b2022-03-28 16:04:03 +02006604 do {
6605 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
6606 } while (QCBOR_SUCCESS == uCBORError);
6607
6608 uCBORError = QCBORDecode_Finish(&DCtx);
6609
Laurence Lundblade11ea3612022-07-01 13:26:23 -07006610 return (int32_t)uCBORError;
David Navarro9123e5b2022-03-28 16:04:03 +02006611}
Laurence Lundblade97c61bf2020-05-02 11:24:06 -07006612
Laurence Lundbladea8758502022-05-15 17:57:46 -07006613#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
6614
Laurence Lundblade37286c02022-09-03 10:05:02 -07006615
6616
6617static const uint8_t spSequenceTestInput[] = {
6618 /* 1. The valid date string "1985-04-12" */
6619 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string
6620
6621 /* 2. */
6622 0x00,
6623
6624 /* 3. A valid epoch date, 1400000000; Tue, 13 May 2014 16:53:20 GMT */
6625 0x1a, 0x53, 0x72, 0x4E, 0x00,
6626
6627 /* 4. */
6628 0x62, 'h', 'i',
6629};
6630
6631
Laurence Lundbladee3553422020-05-02 11:11:17 -07006632int32_t CBORSequenceDecodeTests(void)
6633{
6634 QCBORDecodeContext DCtx;
Laurence Lundblade87495732021-02-26 10:05:55 -07006635 QCBORItem Item;
6636 QCBORError uCBORError;
6637 size_t uConsumed;
Laurence Lundbladee3553422020-05-02 11:11:17 -07006638
6639 // --- Test a sequence with extra bytes ---
Laurence Lundblade9b334962020-08-27 10:55:53 -07006640
Laurence Lundbladee3553422020-05-02 11:11:17 -07006641 QCBORDecode_Init(&DCtx,
Laurence Lundblade37286c02022-09-03 10:05:02 -07006642 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSequenceTestInput),
Laurence Lundbladee3553422020-05-02 11:11:17 -07006643 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade9b334962020-08-27 10:55:53 -07006644
Laurence Lundblade37286c02022-09-03 10:05:02 -07006645 // Get 1.
Laurence Lundbladee3553422020-05-02 11:11:17 -07006646 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
6647 if(uCBORError != QCBOR_SUCCESS) {
6648 return 1;
6649 }
Laurence Lundblade37286c02022-09-03 10:05:02 -07006650 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ) {
Laurence Lundbladee3553422020-05-02 11:11:17 -07006651 return 2;
6652 }
Laurence Lundbladec7114722020-08-13 05:11:40 -07006653
Laurence Lundblade87495732021-02-26 10:05:55 -07006654 uCBORError = QCBORDecode_PartialFinish(&DCtx, &uConsumed);
6655 if(uCBORError != QCBOR_ERR_EXTRA_BYTES ||
Laurence Lundblade37286c02022-09-03 10:05:02 -07006656 uConsumed != 11) {
6657 return 102;
6658 }
6659
6660 // Get 2.
6661 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
6662 if(uCBORError != QCBOR_SUCCESS) {
6663 return 66;
6664 }
6665
6666 uCBORError = QCBORDecode_PartialFinish(&DCtx, &uConsumed);
6667 if(uCBORError != QCBOR_ERR_EXTRA_BYTES ||
Laurence Lundblade87495732021-02-26 10:05:55 -07006668 uConsumed != 12) {
6669 return 102;
6670 }
6671
Laurence Lundblade37286c02022-09-03 10:05:02 -07006672 // Get 3.
Laurence Lundbladec7114722020-08-13 05:11:40 -07006673 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundbladee3553422020-05-02 11:11:17 -07006674 if(uCBORError != QCBOR_SUCCESS) {
6675 return 2;
6676 }
Laurence Lundblade37286c02022-09-03 10:05:02 -07006677 if(Item.uDataType != QCBOR_TYPE_INT64) {
Laurence Lundbladee3553422020-05-02 11:11:17 -07006678 return 3;
6679 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006680
Laurence Lundbladee3553422020-05-02 11:11:17 -07006681 // A sequence can have stuff at the end that may
6682 // or may not be valid CBOR. The protocol decoder knows
6683 // when to stop by definition of the protocol, not
6684 // when the top-level map or array is ended.
6685 // Finish still has to be called to know that
6686 // maps and arrays (if there were any) were closed
6687 // off correctly. When called like this it
6688 // must return the error QCBOR_ERR_EXTRA_BYTES.
6689 uCBORError = QCBORDecode_Finish(&DCtx);
6690 if(uCBORError != QCBOR_ERR_EXTRA_BYTES) {
6691 return 4;
6692 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006693
Laurence Lundbladee3553422020-05-02 11:11:17 -07006694 // --- Test an empty input ----
6695 uint8_t empty[1];
6696 UsefulBufC Empty = {empty, 0};
6697 QCBORDecode_Init(&DCtx,
6698 Empty,
6699 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade9b334962020-08-27 10:55:53 -07006700
Laurence Lundbladee3553422020-05-02 11:11:17 -07006701 uCBORError = QCBORDecode_Finish(&DCtx);
6702 if(uCBORError != QCBOR_SUCCESS) {
6703 return 5;
6704 }
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07006705
6706
Laurence Lundbladee3553422020-05-02 11:11:17 -07006707 // --- Sequence with unclosed indefinite length array ---
6708 static const uint8_t xx[] = {0x01, 0x9f, 0x02};
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07006709
Laurence Lundbladee3553422020-05-02 11:11:17 -07006710 QCBORDecode_Init(&DCtx,
6711 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(xx),
6712 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07006713
Laurence Lundbladee3553422020-05-02 11:11:17 -07006714 // Get the first item
6715 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
6716 if(uCBORError != QCBOR_SUCCESS) {
6717 return 7;
6718 }
6719 if(Item.uDataType != QCBOR_TYPE_INT64) {
6720 return 8;
6721 }
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07006722
Laurence Lundbladee3553422020-05-02 11:11:17 -07006723 // Get a second item
6724 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08006725#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundbladee3553422020-05-02 11:11:17 -07006726 if(uCBORError != QCBOR_SUCCESS) {
6727 return 9;
6728 }
6729 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
6730 return 10;
6731 }
6732
6733 // Try to finish before consuming all bytes to confirm
6734 // that the still-open error is returned.
6735 uCBORError = QCBORDecode_Finish(&DCtx);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006736 if(uCBORError != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
Laurence Lundbladee3553422020-05-02 11:11:17 -07006737 return 11;
6738 }
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08006739#else /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
6740 if(uCBORError != QCBOR_ERR_INDEF_LEN_ARRAYS_DISABLED) {
6741 return 20;
6742 }
6743#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
Laurence Lundbladee3553422020-05-02 11:11:17 -07006744
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07006745
Laurence Lundbladee3553422020-05-02 11:11:17 -07006746 // --- Sequence with a closed indefinite length array ---
6747 static const uint8_t yy[] = {0x01, 0x9f, 0xff};
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07006748
Laurence Lundbladee3553422020-05-02 11:11:17 -07006749 QCBORDecode_Init(&DCtx,
6750 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(yy),
6751 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07006752
Laurence Lundbladee3553422020-05-02 11:11:17 -07006753 // Get the first item
6754 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
6755 if(uCBORError != QCBOR_SUCCESS) {
6756 return 12;
6757 }
6758 if(Item.uDataType != QCBOR_TYPE_INT64) {
6759 return 13;
6760 }
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07006761
Laurence Lundbladee3553422020-05-02 11:11:17 -07006762 // Get a second item
6763 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08006764#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
6765
Laurence Lundbladee3553422020-05-02 11:11:17 -07006766 if(uCBORError != QCBOR_SUCCESS) {
6767 return 14;
6768 }
6769 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
6770 return 15;
6771 }
6772
6773 // Try to finish before consuming all bytes to confirm
6774 // that the still-open error is returned.
6775 uCBORError = QCBORDecode_Finish(&DCtx);
6776 if(uCBORError != QCBOR_SUCCESS) {
6777 return 16;
6778 }
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08006779#else /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
6780 if(uCBORError != QCBOR_ERR_INDEF_LEN_ARRAYS_DISABLED) {
6781 return 20;
6782 }
6783#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
Laurence Lundbladee3553422020-05-02 11:11:17 -07006784
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07006785
Laurence Lundbladee3553422020-05-02 11:11:17 -07006786 return 0;
6787}
6788
Laurence Lundbladee15326f2020-06-15 15:50:23 -07006789
Laurence Lundblade70ecead2020-06-15 19:40:06 -07006790
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03006791int32_t IntToTests(void)
Laurence Lundbladee15326f2020-06-15 15:50:23 -07006792{
6793 int nErrCode;
6794 int32_t n32;
6795 int16_t n16;
6796 int8_t n8;
6797 uint32_t u32;
6798 uint16_t u16;
6799 uint8_t u8;
6800 uint64_t u64;
6801
6802 nErrCode = QCBOR_Int64ToInt32(1, &n32);
6803 if(nErrCode == -1 || n32 != 1) {
6804 return 1;
6805 }
6806
6807 nErrCode = QCBOR_Int64ToInt32((int64_t)INT32_MAX, &n32);
6808 if(nErrCode == -1 || n32 != INT32_MAX) {
6809 return 2;
6810 }
6811
6812 nErrCode = QCBOR_Int64ToInt32((int64_t)INT32_MIN, &n32);
6813 if(nErrCode == -1 || n32 != INT32_MIN) {
6814 return 3;
6815 }
6816
6817 nErrCode = QCBOR_Int64ToInt32(((int64_t)INT32_MAX)+1, &n32);
6818 if(nErrCode != -1) {
6819 return 4;
6820 }
6821
6822 nErrCode = QCBOR_Int64ToInt32(((int64_t)INT32_MIN)-1, &n32);
6823 if(nErrCode != -1) {
6824 return 5;
6825 }
6826
6827
6828 nErrCode = QCBOR_Int64ToInt16((int64_t)INT16_MAX, &n16);
6829 if(nErrCode == -1 || n16 != INT16_MAX) {
6830 return 6;
6831 }
6832
6833 nErrCode = QCBOR_Int64ToInt16((int64_t)INT16_MIN, &n16);
6834 if(nErrCode == -1 || n16 != INT16_MIN) {
6835 return 7;
6836 }
6837
6838 nErrCode = QCBOR_Int64ToInt16(1, &n16);
6839 if(nErrCode == -1 || n16 != 1) {
6840 return 8;
6841 }
6842
6843 nErrCode = QCBOR_Int64ToInt16(((int64_t)INT16_MAX)+1, &n16);
6844 if(nErrCode != -1) {
6845 return 9;
6846 }
6847
6848 nErrCode = QCBOR_Int64ToInt16(((int64_t)INT16_MIN)-1, &n16);
6849 if(nErrCode != -1) {
6850 return 10;
6851 }
6852
6853
6854 nErrCode = QCBOR_Int64ToInt8(1, &n8);
6855 if(nErrCode == -1 || n8 != 1) {
6856 return 11;
6857 }
6858
6859 nErrCode = QCBOR_Int64ToInt8((int64_t)INT8_MAX, &n8);
6860 if(nErrCode == -1 || n8 != INT8_MAX) {
6861 return 12;
6862 }
6863
6864 nErrCode = QCBOR_Int64ToInt8((int64_t)INT8_MIN, &n8);
6865 if(nErrCode == -1 || n8 != INT8_MIN) {
6866 return 13;
6867 }
6868
6869 nErrCode = QCBOR_Int64ToInt8(((int64_t)INT8_MAX)+1, &n8);
6870 if(nErrCode != -1) {
6871 return 14;
6872 }
6873
6874 nErrCode = QCBOR_Int64ToInt8(((int64_t)INT8_MIN)-1, &n8);
6875 if(nErrCode != -1) {
6876 return 15;
6877 }
6878
6879
6880 nErrCode = QCBOR_Int64ToUInt32(1, &u32);
6881 if(nErrCode == -1 || u32 != 1) {
6882 return 16;
6883 }
6884
6885 nErrCode = QCBOR_Int64ToUInt32((int64_t)UINT32_MAX, &u32);
6886 if(nErrCode == -1 || u32 != UINT32_MAX) {
6887 return 17;
6888 }
6889
6890 nErrCode = QCBOR_Int64ToUInt32((int64_t)0, &u32);
6891 if(nErrCode == -1 || u32 != 0) {
6892 return 18;
6893 }
6894
6895 nErrCode = QCBOR_Int64ToUInt32(((int64_t)UINT32_MAX)+1, &u32);
6896 if(nErrCode != -1) {
6897 return 19;
6898 }
6899
6900 nErrCode = QCBOR_Int64ToUInt32((int64_t)-1, &u32);
6901 if(nErrCode != -1) {
6902 return 20;
6903 }
6904
6905
6906 nErrCode = QCBOR_Int64UToInt16((int64_t)UINT16_MAX, &u16);
6907 if(nErrCode == -1 || u16 != UINT16_MAX) {
6908 return 21;
6909 }
6910
6911 nErrCode = QCBOR_Int64UToInt16((int64_t)0, &u16);
6912 if(nErrCode == -1 || u16 != 0) {
6913 return 22;
6914 }
6915
6916 nErrCode = QCBOR_Int64UToInt16(1, &u16);
6917 if(nErrCode == -1 || u16 != 1) {
6918 return 23;
6919 }
6920
6921 nErrCode = QCBOR_Int64UToInt16(((int64_t)UINT16_MAX)+1, &u16);
6922 if(nErrCode != -1) {
6923 return 24;
6924 }
6925
6926 nErrCode = QCBOR_Int64UToInt16((int64_t)-1, &u16);
6927 if(nErrCode != -1) {
6928 return 25;
6929 }
6930
6931
6932 nErrCode = QCBOR_Int64ToUInt8((int64_t)UINT8_MAX, &u8);
6933 if(nErrCode == -1 || u8 != UINT8_MAX) {
6934 return 26;
6935 }
6936
6937 nErrCode = QCBOR_Int64ToUInt8((int64_t)0, &u8);
6938 if(nErrCode == -1 || u8 != 0) {
6939 return 27;
6940 }
6941
6942 nErrCode = QCBOR_Int64ToUInt8(1, &u8);
6943 if(nErrCode == -1 || u8 != 1) {
6944 return 28;
6945 }
6946
6947 nErrCode = QCBOR_Int64ToUInt8(((int64_t)UINT16_MAX)+1, &u8);
6948 if(nErrCode != -1) {
6949 return 29;
6950 }
6951
6952 nErrCode = QCBOR_Int64ToUInt8((int64_t)-1, &u8);
6953 if(nErrCode != -1) {
6954 return 30;
6955 }
6956
6957
6958 nErrCode = QCBOR_Int64ToUInt64(1, &u64);
6959 if(nErrCode == -1 || u64 != 1) {
6960 return 31;
6961 }
6962
6963 nErrCode = QCBOR_Int64ToUInt64(INT64_MAX, &u64);
6964 if(nErrCode == -1 || u64 != INT64_MAX) {
6965 return 32;
6966 }
6967
6968 nErrCode = QCBOR_Int64ToUInt64((int64_t)0, &u64);
6969 if(nErrCode == -1 || u64 != 0) {
6970 return 33;
6971 }
6972
6973 nErrCode = QCBOR_Int64ToUInt64((int64_t)-1, &u64);
6974 if(nErrCode != -1) {
6975 return 34;
6976 }
6977
6978 return 0;
6979}
6980
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006981
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006982
6983
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006984/*
6985A sequence with
6986 A wrapping bstr
6987 containing a map
6988 1
6989 2
6990 A wrapping bstr
6991 containing an array
6992 3
6993 wrapping bstr
6994 4
6995 5
6996 6
6997 array
6998 7
6999 8
Laurence Lundblade0750fc42020-06-20 21:02:34 -07007000 */
7001
Laurence Lundblade55013642020-09-23 05:39:22 -07007002static UsefulBufC EncodeBstrWrapTestData(UsefulBuf OutputBuffer)
Laurence Lundblade0750fc42020-06-20 21:02:34 -07007003{
Laurence Lundblade55013642020-09-23 05:39:22 -07007004 UsefulBufC Encoded;
Laurence Lundblade0750fc42020-06-20 21:02:34 -07007005 QCBOREncodeContext EC;
Laurence Lundblade55013642020-09-23 05:39:22 -07007006 QCBORError uErr;
Laurence Lundblade0750fc42020-06-20 21:02:34 -07007007
Laurence Lundblade55013642020-09-23 05:39:22 -07007008 QCBOREncode_Init(&EC, OutputBuffer);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07007009
7010 QCBOREncode_BstrWrap(&EC);
7011 QCBOREncode_OpenMap(&EC);
7012 QCBOREncode_AddInt64ToMapN(&EC, 100, 1);
7013 QCBOREncode_AddInt64ToMapN(&EC, 200, 2);
7014 QCBOREncode_CloseMap(&EC);
7015 QCBOREncode_BstrWrap(&EC);
7016 QCBOREncode_OpenArray(&EC);
7017 QCBOREncode_AddInt64(&EC, 3);
7018 QCBOREncode_BstrWrap(&EC);
7019 QCBOREncode_AddInt64(&EC, 4);
7020 QCBOREncode_CloseBstrWrap(&EC, NULL);
7021 QCBOREncode_AddInt64(&EC, 5);
7022 QCBOREncode_CloseArray(&EC);
7023 QCBOREncode_CloseBstrWrap(&EC, NULL);
7024 QCBOREncode_AddInt64(&EC, 6);
7025 QCBOREncode_CloseBstrWrap(&EC, NULL);
7026 QCBOREncode_OpenArray(&EC);
7027 QCBOREncode_AddInt64(&EC, 7);
7028 QCBOREncode_AddInt64(&EC, 8);
7029 QCBOREncode_CloseArray(&EC);
7030
7031 uErr = QCBOREncode_Finish(&EC, &Encoded);
Laurence Lundblade40a04322020-06-27 22:52:52 -07007032 if(uErr) {
7033 Encoded = NULLUsefulBufC;
7034 }
Laurence Lundblade0750fc42020-06-20 21:02:34 -07007035
7036 return Encoded;
7037}
7038
Laurence Lundbladecc7da412020-12-27 00:09:07 -08007039/* h'FF' */
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08007040static const uint8_t spBreakInByteString[] = {
7041 0x41, 0xff
7042};
7043
Laurence Lundblade0750fc42020-06-20 21:02:34 -07007044
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03007045int32_t EnterBstrTest(void)
Laurence Lundblade0750fc42020-06-20 21:02:34 -07007046{
Laurence Lundblade8510f8c2020-12-01 11:31:16 -08007047 UsefulBuf_MAKE_STACK_UB(OutputBuffer, 100);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07007048
7049 QCBORDecodeContext DC;
7050
Laurence Lundblade55013642020-09-23 05:39:22 -07007051 QCBORDecode_Init(&DC, EncodeBstrWrapTestData(OutputBuffer), 0);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07007052
Laurence Lundblade55013642020-09-23 05:39:22 -07007053 int64_t n1, n2, n3, n4, n5, n6, n7, n8;
Laurence Lundblade0750fc42020-06-20 21:02:34 -07007054
7055
Laurence Lundblade9b334962020-08-27 10:55:53 -07007056 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07007057 QCBORDecode_EnterMap(&DC, NULL);
Laurence Lundblade55013642020-09-23 05:39:22 -07007058 QCBORDecode_GetInt64InMapN(&DC, 100, &n1);
7059 QCBORDecode_GetInt64InMapN(&DC, 200, &n2);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07007060 QCBORDecode_ExitMap(&DC);
Laurence Lundblade9b334962020-08-27 10:55:53 -07007061 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07007062 QCBORDecode_EnterArray(&DC, NULL);
Laurence Lundblade55013642020-09-23 05:39:22 -07007063 QCBORDecode_GetInt64(&DC, &n3);
Laurence Lundblade9b334962020-08-27 10:55:53 -07007064 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
Laurence Lundblade55013642020-09-23 05:39:22 -07007065 QCBORDecode_GetInt64(&DC, &n4);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07007066 QCBORDecode_ExitBstrWrapped(&DC);
Laurence Lundblade55013642020-09-23 05:39:22 -07007067 QCBORDecode_GetInt64(&DC, &n5);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07007068 QCBORDecode_ExitArray(&DC);
7069 QCBORDecode_ExitBstrWrapped(&DC);
Laurence Lundblade55013642020-09-23 05:39:22 -07007070 QCBORDecode_GetInt64(&DC, &n6);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07007071 QCBORDecode_ExitBstrWrapped(&DC);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07007072 QCBORDecode_EnterArray(&DC, NULL);
Laurence Lundblade55013642020-09-23 05:39:22 -07007073 QCBORDecode_GetInt64(&DC, &n7);
7074 QCBORDecode_GetInt64(&DC, &n8);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07007075 QCBORDecode_ExitArray(&DC);
7076
7077 QCBORError uErr = QCBORDecode_Finish(&DC);
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08007078 if(uErr) {
7079 return (int32_t)uErr;
7080 }
Laurence Lundblade0750fc42020-06-20 21:02:34 -07007081
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08007082
7083 /* Enter and exit byte string wrapped CBOR that is bad. It has just a break.
7084 * Successful because no items are fetched from byte string.
7085 */
7086 QCBORDecode_Init(&DC,
7087 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBreakInByteString),
7088 0);
7089 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
7090 uErr = QCBORDecode_GetError(&DC);
7091 if(uErr) {
7092 return 100 + (int32_t)uErr;
7093 }
7094
7095 QCBORDecode_ExitBstrWrapped(&DC);
7096 uErr = QCBORDecode_GetError(&DC);
7097 if(uErr) {
7098 return 200 + (int32_t)uErr;
7099 }
7100
7101 /* Try to get item that is a break out of a byte string wrapped CBOR.
7102 * It fails because there should be no break.
7103 */
7104 QCBORDecode_Init(&DC,
7105 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBreakInByteString),
7106 0);
7107 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
7108 QCBORItem Item;
7109 uErr = QCBORDecode_GetNext(&DC, &Item);
7110 if(uErr != QCBOR_ERR_BAD_BREAK) {
7111 return 300 + (int32_t)uErr;
7112 }
7113
7114 return 0;
Laurence Lundblade0750fc42020-06-20 21:02:34 -07007115}
Laurence Lundblade37f46e52020-08-04 03:32:14 -07007116
7117
7118
7119
7120static const uint8_t spTaggedTypes[] = {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007121 0xb2,
Laurence Lundblade37f46e52020-08-04 03:32:14 -07007122
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07007123 // Date string
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007124 0x00,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07007125 0xc0, 0x74, 0x32, 0x30, 0x30, 0x33, 0x2D, 0x31, 0x32, 0x2D,
7126 0x31, 0x33, 0x54, 0x31, 0x38, 0x3A, 0x33, 0x30, 0x3A, 0x30,
7127 0x32, 0x5A,
Laurence Lundblade37f46e52020-08-04 03:32:14 -07007128
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007129 0x01,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07007130 0x74, 0x32, 0x30, 0x30, 0x33, 0x2D, 0x31, 0x32, 0x2D, 0x31,
7131 0x33, 0x54, 0x31, 0x38, 0x3A, 0x33, 0x30, 0x3A, 0x30, 0x32,
7132 0x5A,
Laurence Lundblade37f46e52020-08-04 03:32:14 -07007133
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07007134 // Bignum
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007135 10,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07007136 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
7137 0x09, 0x10,
Laurence Lundblade37f46e52020-08-04 03:32:14 -07007138
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007139 11,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07007140 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
7141 0x09, 0x10,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007142
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07007143 // URL
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007144 20,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07007145 0xd8, 0x20, 0x6f, 0x68, 0x74, 0x74, 0x70, 0x3A, 0x2F, 0x2F,
7146 0x63, 0x62, 0x6F, 0x72, 0x2E, 0x6D, 0x65, 0x2F,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007147
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07007148 21,
7149 0x6f, 0x68, 0x74, 0x74, 0x70, 0x3A, 0x2F, 0x2F, 0x63, 0x62,
7150 0x6F, 0x72, 0x2E, 0x6D, 0x65, 0x2F,
7151
7152 // B64
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007153 0x18, 0x1e,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07007154 0xd8, 0x22, 0x6c, 0x63, 0x47, 0x78, 0x6C, 0x59, 0x58, 0x4E,
7155 0x31, 0x63, 0x6D, 0x55, 0x75,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007156
7157 0x18, 0x1f,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07007158 0x6c, 0x63, 0x47, 0x78, 0x6C, 0x59, 0x58, 0x4E, 0x31, 0x63,
7159 0x6D, 0x55, 0x75,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007160
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07007161 // B64URL
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007162 0x18, 0x28,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07007163 0xd8, 0x21, 0x6c, 0x63, 0x47, 0x78, 0x6C, 0x59, 0x58, 0x4E,
7164 0x31, 0x63, 0x6D, 0x55, 0x75,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007165
7166 0x18, 0x29,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07007167 0x6c, 0x63, 0x47, 0x78, 0x6C, 0x59, 0x58, 0x4E, 0x31, 0x63,
7168 0x6D, 0x55, 0x75,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007169
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07007170 // Regex
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007171 0x18, 0x32,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07007172 0xd8, 0x23, 0x68, 0x31, 0x30, 0x30, 0x5C, 0x73, 0x2A, 0x6D,
7173 0x6B,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007174
7175 0x18, 0x33,
7176 0x68, 0x31, 0x30, 0x30, 0x5C, 0x73, 0x2A, 0x6D, 0x6B,
7177
7178 // MIME
7179 0x18, 0x3c,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07007180 0xd8, 0x24, 0x72, 0x4D, 0x49, 0x4D, 0x45, 0x2D, 0x56, 0x65,
7181 0x72, 0x73, 0x69, 0x6F, 0x6E, 0x3A, 0x20, 0x31, 0x2E, 0x30,
7182 0x0A,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007183
7184 0x18, 0x3d,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07007185 0x72, 0x4D, 0x49, 0x4D, 0x45, 0x2D, 0x56, 0x65, 0x72, 0x73,
7186 0x69, 0x6F, 0x6E, 0x3A, 0x20, 0x31, 0x2E, 0x30, 0x0A,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007187
7188 0x18, 0x3e,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07007189 0xd9, 0x01, 0x01, 0x52, 0x4D, 0x49, 0x4D, 0x45, 0x2D, 0x56,
7190 0x65, 0x72, 0x73, 0x69, 0x6F, 0x6E, 0x3A, 0x20, 0x31, 0x2E,
7191 0x30, 0x0A,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007192
7193 0x18, 0x3f,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07007194 0x52, 0x4D, 0x49, 0x4D, 0x45, 0x2D, 0x56, 0x65, 0x72, 0x73,
7195 0x69, 0x6F, 0x6E, 0x3A, 0x20, 0x31, 0x2E, 0x30, 0x0A,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007196
7197 // UUID
7198 0x18, 0x46,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07007199 0xd8, 0x25, 0x50, 0x53, 0x4D, 0x41, 0x52, 0x54, 0x43, 0x53,
7200 0x4C, 0x54, 0x54, 0x43, 0x46, 0x49, 0x43, 0x41, 0x32,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007201
7202 0x18, 0x47,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07007203 0x50, 0x53, 0x4D, 0x41, 0x52, 0x54, 0x43, 0x53, 0x4C, 0x54,
7204 0x54, 0x43, 0x46, 0x49, 0x43, 0x41, 0x32
Laurence Lundblade37f46e52020-08-04 03:32:14 -07007205};
7206
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03007207int32_t DecodeTaggedTypeTests(void)
Laurence Lundblade37f46e52020-08-04 03:32:14 -07007208{
7209 QCBORDecodeContext DC;
7210 QCBORError uErr;
7211
7212 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTaggedTypes), 0);
7213
7214 UsefulBufC String;
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007215 bool bNeg;
Laurence Lundblade37f46e52020-08-04 03:32:14 -07007216
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07007217 QCBORDecode_EnterMap(&DC, NULL);
Laurence Lundblade9b334962020-08-27 10:55:53 -07007218 QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007219 QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &String);
Laurence Lundblade37f46e52020-08-04 03:32:14 -07007220 if(QCBORDecode_GetError(&DC) != QCBOR_SUCCESS) {
7221 return 1;
7222 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07007223 QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade37f46e52020-08-04 03:32:14 -07007224 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_UNEXPECTED_TYPE) {
7225 return 2;
7226 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07007227 QCBORDecode_GetDateStringInMapN(&DC, 1, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade37f46e52020-08-04 03:32:14 -07007228 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_UNEXPECTED_TYPE) {
7229 return 3;
7230 }
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007231 QCBORDecode_GetDateStringInMapN(&DC, 1, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &String);
Laurence Lundblade9b334962020-08-27 10:55:53 -07007232 QCBORDecode_GetDateStringInMapN(&DC, 1, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade37f46e52020-08-04 03:32:14 -07007233 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
7234 return 4;
7235 }
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007236 QCBORDecode_GetDateStringInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07007237 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007238 return 5;
7239 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07007240
Laurence Lundblade9b334962020-08-27 10:55:53 -07007241 QCBORDecode_GetBignumInMapN(&DC, 10, QCBOR_TAG_REQUIREMENT_TAG, &String, &bNeg);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007242 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
7243 bNeg != false) {
7244 return 10;
7245 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07007246 QCBORDecode_GetBignumInMapN(&DC, 11, QCBOR_TAG_REQUIREMENT_TAG, &String, &bNeg);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007247 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
7248 bNeg != true) {
7249 return 11;
7250 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07007251 QCBORDecode_GetBignumInMapN(&DC, 11, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bNeg);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007252 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_UNEXPECTED_TYPE) {
7253 return 12;
7254 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07007255 QCBORDecode_GetBignumInMapN(&DC, 14, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bNeg);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07007256 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007257 return 13;
7258 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07007259 QCBORDecode_GetBignumInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bNeg);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07007260 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007261 return 14;
7262 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07007263
Laurence Lundblade9b334962020-08-27 10:55:53 -07007264 QCBORDecode_GetURIInMapN(&DC, 20, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007265 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
7266 return 20;
7267 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07007268 QCBORDecode_GetURIInMapN(&DC, 21, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007269 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
7270 return 21;
7271 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07007272 QCBORDecode_GetURIInMapN(&DC, 22, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07007273 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007274 return 22;
7275 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07007276 QCBORDecode_GetURIInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07007277 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007278 return 23;
7279 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07007280
Laurence Lundblade24bd7e12021-01-09 00:05:11 -08007281#ifndef QCBOR_DISABLE_UNCOMMON_TAGS
Laurence Lundblade9b334962020-08-27 10:55:53 -07007282 QCBORDecode_GetB64InMapN(&DC, 30, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007283 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
7284 return 30;
7285 }
Laurence Lundblade24bd7e12021-01-09 00:05:11 -08007286#endif
Laurence Lundblade9b334962020-08-27 10:55:53 -07007287 QCBORDecode_GetB64InMapN(&DC, 31, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007288 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
7289 return 31;
7290 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07007291 QCBORDecode_GetB64InMapN(&DC, 32, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07007292 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007293 return 32;
7294 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07007295 QCBORDecode_GetB64InMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07007296 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007297 return 33;
7298 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07007299
Laurence Lundblade24bd7e12021-01-09 00:05:11 -08007300#ifndef QCBOR_DISABLE_UNCOMMON_TAGS
Laurence Lundblade9b334962020-08-27 10:55:53 -07007301 QCBORDecode_GetB64URLInMapN(&DC, 40, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007302 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
7303 return 40;
7304 }
Laurence Lundblade24bd7e12021-01-09 00:05:11 -08007305#endif
Laurence Lundblade9b334962020-08-27 10:55:53 -07007306 QCBORDecode_GetB64URLInMapN(&DC, 41, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007307 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
7308 return 41;
7309 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07007310 QCBORDecode_GetB64URLInMapN(&DC, 42, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07007311 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007312 return 42;
7313 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07007314 QCBORDecode_GetB64URLInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07007315 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007316 return 43;
7317 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07007318
Laurence Lundblade24bd7e12021-01-09 00:05:11 -08007319#ifndef QCBOR_DISABLE_UNCOMMON_TAGS
Laurence Lundblade9b334962020-08-27 10:55:53 -07007320 QCBORDecode_GetRegexInMapN(&DC, 50, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007321 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
7322 return 50;
7323 }
Laurence Lundblade24bd7e12021-01-09 00:05:11 -08007324#endif
Laurence Lundblade9b334962020-08-27 10:55:53 -07007325 QCBORDecode_GetRegexInMapN(&DC, 51, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007326 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
7327 return 51;
7328 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07007329 QCBORDecode_GetRegexInMapN(&DC, 52, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07007330 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007331 return 52;
7332 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07007333 QCBORDecode_GetRegexInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07007334 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007335 return 53;
7336 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07007337
Laurence Lundblade24bd7e12021-01-09 00:05:11 -08007338#ifndef QCBOR_DISABLE_UNCOMMON_TAGS
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007339 // MIME
7340 bool bIsNot7Bit;
Laurence Lundblade9b334962020-08-27 10:55:53 -07007341 QCBORDecode_GetMIMEMessageInMapN(&DC, 60, QCBOR_TAG_REQUIREMENT_TAG, &String, &bIsNot7Bit);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007342 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
7343 bIsNot7Bit == true) {
7344 return 60;
7345 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07007346 QCBORDecode_GetMIMEMessageInMapN(&DC, 61, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bIsNot7Bit);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007347 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
7348 bIsNot7Bit == true) {
7349 return 61;
7350 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07007351 QCBORDecode_GetMIMEMessageInMapN(&DC, 62, QCBOR_TAG_REQUIREMENT_TAG, &String, &bIsNot7Bit);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007352 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
7353 bIsNot7Bit == false) {
7354 return 62;
7355 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07007356 QCBORDecode_GetMIMEMessageInMapN(&DC, 63, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bIsNot7Bit);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007357 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
7358 bIsNot7Bit == false) {
7359 return 63;
7360 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07007361 QCBORDecode_GetMIMEMessageInMapN(&DC, 64, QCBOR_TAG_REQUIREMENT_TAG, &String, &bNeg);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07007362 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007363 return 64;
7364 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07007365 QCBORDecode_GetMIMEMessageInMapSZ(&DC, "zzz", QCBOR_TAG_REQUIREMENT_TAG, &String, &bNeg);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07007366 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007367 return 65;
7368 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07007369
Laurence Lundblade24bd7e12021-01-09 00:05:11 -08007370
Laurence Lundblade9b334962020-08-27 10:55:53 -07007371 QCBORDecode_GetBinaryUUIDInMapN(&DC, 70, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007372 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
7373 return 70;
7374 }
Laurence Lundblade24bd7e12021-01-09 00:05:11 -08007375#endif /* #ifndef QCBOR_DISABLE_UNCOMMON_TAGS */
7376
Laurence Lundblade9b334962020-08-27 10:55:53 -07007377 QCBORDecode_GetBinaryUUIDInMapN(&DC, 71, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007378 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
7379 return 71;
7380 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07007381 QCBORDecode_GetBinaryUUIDInMapN(&DC, 72, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07007382 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007383 return 72;
7384 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07007385 QCBORDecode_GetBinaryUUIDInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07007386 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007387 return 73;
7388 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07007389
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07007390 // Improvement: add some more error test cases
7391
Laurence Lundblade37f46e52020-08-04 03:32:14 -07007392 QCBORDecode_ExitMap(&DC);
7393
7394 uErr = QCBORDecode_Finish(&DC);
7395 if(uErr != QCBOR_SUCCESS) {
7396 return 100;
7397 }
7398
7399 return 0;
7400}
Laurence Lundbladea4308a82020-10-03 18:08:57 -07007401
7402
7403
7404
7405/*
Laurence Lundbladecc7da412020-12-27 00:09:07 -08007406 [
7407 "aaaaaaaaaa",
7408 {}
7409 ]
Laurence Lundbladea4308a82020-10-03 18:08:57 -07007410 */
7411static const uint8_t spTooLarge1[] = {
7412 0x9f,
7413 0x6a, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61,
7414 0xa0,
7415 0xff
7416};
7417
7418/*
Laurence Lundbladecc7da412020-12-27 00:09:07 -08007419 [
7420 {
7421 0: "aaaaaaaaaa"
7422 }
7423 ]
Laurence Lundbladea4308a82020-10-03 18:08:57 -07007424 */
7425static const uint8_t spTooLarge2[] = {
7426 0x9f,
7427 0xa1,
7428 0x00,
7429 0x6a, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61,
7430 0xff
7431};
7432
7433/*
Laurence Lundbladecc7da412020-12-27 00:09:07 -08007434 h'A1006A61616161616161616161'
Laurence Lundbladea4308a82020-10-03 18:08:57 -07007435
Laurence Lundbladecc7da412020-12-27 00:09:07 -08007436 {
7437 0: "aaaaaaaaaa"
7438 }
Laurence Lundbladea4308a82020-10-03 18:08:57 -07007439 */
7440static const uint8_t spTooLarge3[] = {
7441 0x4d,
7442 0xa1,
7443 0x00,
7444 0x6a, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61,
7445};
7446
7447int32_t TooLargeInputTest(void)
7448{
7449 QCBORDecodeContext DC;
7450 QCBORError uErr;
7451 UsefulBufC String;
7452
7453 // These tests require a build with QCBOR_MAX_DECODE_INPUT_SIZE set
7454 // to 10 There's not really any way to test this error
7455 // condition. The error condition is not complex, so setting
7456 // QCBOR_MAX_DECODE_INPUT_SIZE gives an OK test.
7457
7458 // The input CBOR is only too large because the
7459 // QCBOR_MAX_DECODE_INPUT_SIZE is 10.
7460 //
7461 // This test is disabled for the normal test runs because of the
7462 // special build requirement.
7463
7464
7465 // Tests the start of a map being too large
7466 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTooLarge1), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07007467 QCBORDecode_EnterArray(&DC, NULL);
Laurence Lundbladea4308a82020-10-03 18:08:57 -07007468 QCBORDecode_GetTextString(&DC, &String);
7469 uErr = QCBORDecode_GetError(&DC);
7470 if(uErr != QCBOR_SUCCESS) {
7471 return 1;
7472 }
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07007473 QCBORDecode_EnterMap(&DC, NULL);
Laurence Lundbladea4308a82020-10-03 18:08:57 -07007474 uErr = QCBORDecode_GetError(&DC);
7475 if(uErr != QCBOR_ERR_INPUT_TOO_LARGE) {
7476 return 2;
7477 }
7478
7479 // Tests the end of a map being too large
7480 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTooLarge2), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07007481 QCBORDecode_EnterArray(&DC, NULL);
7482 QCBORDecode_EnterMap(&DC, NULL);
Laurence Lundbladea4308a82020-10-03 18:08:57 -07007483 uErr = QCBORDecode_GetError(&DC);
7484 if(uErr != QCBOR_SUCCESS) {
7485 return 3;
7486 }
7487 QCBORDecode_ExitMap(&DC);
7488 uErr = QCBORDecode_GetError(&DC);
7489 if(uErr != QCBOR_ERR_INPUT_TOO_LARGE) {
7490 return 4;
7491 }
7492
7493 // Tests the entire input CBOR being too large when processing bstr wrapping
7494 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTooLarge3), QCBOR_DECODE_MODE_NORMAL);
7495 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
7496 uErr = QCBORDecode_GetError(&DC);
7497 if(uErr != QCBOR_ERR_INPUT_TOO_LARGE) {
7498 return 5;
7499 }
7500
7501 return 0;
7502}
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07007503
7504
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08007505#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
7506
Laurence Lundblade37286c02022-09-03 10:05:02 -07007507/*
7508 An array of three map entries
7509 1) Indefinite length string label for indefinite lenght byte string
7510 2) Indefinite length string label for an integer
7511 3) Indefinite length string label for an indefinite-length negative big num
7512 */
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07007513static const uint8_t spMapWithIndefLenStrings[] = {
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08007514 0xa3,
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07007515 0x7f, 0x61, 'l', 0x64, 'a', 'b', 'e', 'l' , 0x61, '1', 0xff,
7516 0x5f, 0x42, 0x01, 0x02, 0x43, 0x03, 0x04, 0x05, 0xff,
7517 0x7f, 0x62, 'd', 'y', 0x61, 'm', 0x61, 'o', 0xff,
7518 0x03,
7519 0x7f, 0x62, 'l', 'a', 0x63, 'b', 'e', 'l', 0x61, '2', 0xff,
7520 0xc3,
7521 0x5f, 0x42, 0x00, 0x01, 0x42, 0x00, 0x01, 0x41, 0x01, 0xff,
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07007522};
7523
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03007524int32_t SpiffyIndefiniteLengthStringsTests(void)
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07007525{
7526 QCBORDecodeContext DCtx;
7527
7528 QCBORDecode_Init(&DCtx,
7529 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spMapWithIndefLenStrings),
7530 QCBOR_DECODE_MODE_NORMAL);
7531
Laurence Lundblade8510f8c2020-12-01 11:31:16 -08007532 UsefulBuf_MAKE_STACK_UB(StringBuf, 200);
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07007533 QCBORDecode_SetMemPool(&DCtx, StringBuf, false);
7534
7535 UsefulBufC ByteString;
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07007536 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07007537 QCBORDecode_GetByteStringInMapSZ(&DCtx, "label1", &ByteString);
Laurence Lundblade37286c02022-09-03 10:05:02 -07007538
7539#ifndef QCBOR_DISABLE_TAGS
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07007540 if(QCBORDecode_GetAndResetError(&DCtx)) {
7541 return 1;
7542 }
7543
7544 const uint8_t pExectedBytes[] = {0x01, 0x02, 0x03, 0x04, 0x05};
7545 if(UsefulBuf_Compare(ByteString, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pExectedBytes))) {
7546 return 2;
7547 }
7548
7549 uint64_t uInt;
7550 QCBORDecode_GetUInt64InMapSZ(&DCtx, "dymo", &uInt);
7551 if(QCBORDecode_GetAndResetError(&DCtx)) {
7552 return 3;
7553 }
7554 if(uInt != 3) {
7555 return 4;
7556 }
7557
Máté Tóth-Pálef5f07a2021-09-17 19:31:37 +02007558#ifndef USEFULBUF_DISABLE_ALL_FLOAT
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07007559 double uDouble;
7560 QCBORDecode_GetDoubleConvertAllInMapSZ(&DCtx,
7561 "label2",
7562 0xff,
7563 &uDouble);
Laurence Lundblade37286c02022-09-03 10:05:02 -07007564
Laurence Lundbladeb8e19aa2020-10-07 20:59:11 -07007565#ifndef QCBOR_DISABLE_FLOAT_HW_USE
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07007566 if(QCBORDecode_GetAndResetError(&DCtx)) {
7567 return 5;
7568 }
7569 if(uDouble != -16777474) {
7570 return 6;
7571 }
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08007572#else /* QCBOR_DISABLE_FLOAT_HW_USE */
Laurence Lundbladeb8e19aa2020-10-07 20:59:11 -07007573 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_HW_FLOAT_DISABLED) {
7574 return 7;
7575 }
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08007576#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
Máté Tóth-Pálef5f07a2021-09-17 19:31:37 +02007577#endif /* USEFULBUF_DISABLE_ALL_FLOAT */
Laurence Lundbladeb8e19aa2020-10-07 20:59:11 -07007578
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07007579 QCBORDecode_ExitMap(&DCtx);
7580
7581 if(QCBORDecode_Finish(&DCtx)) {
7582 return 99;
7583 }
7584
Laurence Lundblade37286c02022-09-03 10:05:02 -07007585#else /* QCBOR_DISABLE_TAGS */
7586 /* The big num in the input is a CBOR tag and you can't do
7587 * map lookups in a map with a tag so this test does very little
7588 * when tags are disabled. That is OK, the test coverage is still
7589 * good when they are not.
7590 */
7591 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_TAGS_DISABLED) {
7592 return 1002;
7593 }
7594#endif /*QCBOR_DISABLE_TAGS */
7595
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07007596 return 0;
7597}
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08007598#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
Laurence Lundblade2a26abb2020-11-05 19:06:54 -08007599
7600
Laurence Lundbladecf41c522021-02-20 10:19:07 -07007601/*
7602 * An array of an integer and an array. The second array contains
7603 * a bstr-wrapped map.
7604 *
7605 * [7, [h'A36D6669... (see next lines) 73']]
7606 *
7607 * {"first integer": 42,
7608 * "an array of two strings": ["string1", "string2"],
7609 * "map in a map":
7610 * { "bytes 1": h'78787878',
7611 * "bytes 2": h'79797979',
7612 * "another int": 98,
7613 * "text 2": "lies, damn lies and statistics"
7614 * }
7615 * }
7616 */
Laurence Lundblade2a26abb2020-11-05 19:06:54 -08007617
Laurence Lundbladecf41c522021-02-20 10:19:07 -07007618static const uint8_t pValidWrappedMapEncoded[] = {
7619 0x82, 0x07, 0x81, 0x58, 0x97,
7620 0xa3, 0x6d, 0x66, 0x69, 0x72, 0x73, 0x74, 0x20, 0x69, 0x6e,
7621 0x74, 0x65, 0x67, 0x65, 0x72, 0x18, 0x2a, 0x77, 0x61, 0x6e,
7622 0x20, 0x61, 0x72, 0x72, 0x61, 0x79, 0x20, 0x6f, 0x66, 0x20,
7623 0x74, 0x77, 0x6f, 0x20, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67,
7624 0x73, 0x82, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x31,
7625 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x32, 0x6c, 0x6d,
7626 0x61, 0x70, 0x20, 0x69, 0x6e, 0x20, 0x61, 0x20, 0x6d, 0x61,
7627 0x70, 0xa4, 0x67, 0x62, 0x79, 0x74, 0x65, 0x73, 0x20, 0x31,
7628 0x44, 0x78, 0x78, 0x78, 0x78, 0x67, 0x62, 0x79, 0x74, 0x65,
7629 0x73, 0x20, 0x32, 0x44, 0x79, 0x79, 0x79, 0x79, 0x6b, 0x61,
7630 0x6e, 0x6f, 0x74, 0x68, 0x65, 0x72, 0x20, 0x69, 0x6e, 0x74,
7631 0x18, 0x62, 0x66, 0x74, 0x65, 0x78, 0x74, 0x20, 0x32, 0x78,
7632 0x1e, 0x6c, 0x69, 0x65, 0x73, 0x2c, 0x20, 0x64, 0x61, 0x6d,
7633 0x6e, 0x20, 0x6c, 0x69, 0x65, 0x73, 0x20, 0x61, 0x6e, 0x64,
7634 0x20, 0x73, 0x74, 0x61, 0x74, 0x69, 0x73, 0x74, 0x69, 0x63,
7635 0x73
7636};
7637
7638#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
7639
7640/* As above, but the arrays are indefinite length */
7641static const uint8_t pValidIndefWrappedMapEncoded[] = {
7642 0x9f, 0x07, 0x9f, 0x58, 0x97,
7643 0xa3, 0x6d, 0x66, 0x69, 0x72, 0x73, 0x74, 0x20, 0x69, 0x6e,
7644 0x74, 0x65, 0x67, 0x65, 0x72, 0x18, 0x2a, 0x77, 0x61, 0x6e,
7645 0x20, 0x61, 0x72, 0x72, 0x61, 0x79, 0x20, 0x6f, 0x66, 0x20,
7646 0x74, 0x77, 0x6f, 0x20, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67,
7647 0x73, 0x82, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x31,
7648 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x32, 0x6c, 0x6d,
7649 0x61, 0x70, 0x20, 0x69, 0x6e, 0x20, 0x61, 0x20, 0x6d, 0x61,
7650 0x70, 0xa4, 0x67, 0x62, 0x79, 0x74, 0x65, 0x73, 0x20, 0x31,
7651 0x44, 0x78, 0x78, 0x78, 0x78, 0x67, 0x62, 0x79, 0x74, 0x65,
7652 0x73, 0x20, 0x32, 0x44, 0x79, 0x79, 0x79, 0x79, 0x6b, 0x61,
7653 0x6e, 0x6f, 0x74, 0x68, 0x65, 0x72, 0x20, 0x69, 0x6e, 0x74,
7654 0x18, 0x62, 0x66, 0x74, 0x65, 0x78, 0x74, 0x20, 0x32, 0x78,
7655 0x1e, 0x6c, 0x69, 0x65, 0x73, 0x2c, 0x20, 0x64, 0x61, 0x6d,
7656 0x6e, 0x20, 0x6c, 0x69, 0x65, 0x73, 0x20, 0x61, 0x6e, 0x64,
7657 0x20, 0x73, 0x74, 0x61, 0x74, 0x69, 0x73, 0x74, 0x69, 0x63,
7658 0x73,
7659 0xff, 0xff
7660};
7661#endif
7662
7663
7664static const uint8_t pWithEmptyMap[] = {0x82, 0x18, 0x64, 0xa0};
7665
7666#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
7667static const uint8_t pWithEmptyMapInDef[] = {0x9f, 0x18, 0x64, 0xbf, 0xff, 0xff};
7668#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
7669
7670#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
Laurence Lundblade37286c02022-09-03 10:05:02 -07007671
7672/*
7673An array of one that contains
7674 a byte string that is tagged 24 which means CBOR-encoded data
7675 the byte string is an indefinite length string
7676 the wrapped byte string is an array of three numbers
7677 [42, 43, 44]
7678
7679[
7680 24(
7681 (_ h'83', h'18', h'2A182B', h'182C')
7682 )
7683]
7684 */
Laurence Lundbladecf41c522021-02-20 10:19:07 -07007685static const uint8_t pWrappedByIndefiniteLength[] = {
7686 0x81,
7687 0xd8, 0x18,
7688 0x5f,
7689 0x41, 0x83,
7690 0x41, 0x18,
7691 0x43, 0x2A, 0x18, 0x2B,
7692 0x42, 0x18, 0x2C,
7693 0xff
7694};
7695#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
7696
7697
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03007698int32_t PeekAndRewindTest(void)
Laurence Lundblade2a26abb2020-11-05 19:06:54 -08007699{
7700 QCBORItem Item;
7701 QCBORError nCBORError;
7702 QCBORDecodeContext DCtx;
7703
7704 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0);
7705
7706 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
7707 return 100+(int32_t)nCBORError;
7708 }
7709 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
7710 return 200;
7711 }
7712
Laurence Lundblade3427dee2021-06-20 11:11:24 -07007713 QCBORDecode_VPeekNext(&DCtx, &Item);
7714 if((nCBORError = QCBORDecode_GetError(&DCtx))) {
7715 return 150+(int32_t)nCBORError;
7716 }
7717 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
7718 return 250;
7719 }
7720
Laurence Lundblade2a26abb2020-11-05 19:06:54 -08007721 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
7722 return (int32_t)nCBORError;
7723 }
7724 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
7725 return 300;
7726 }
7727
7728 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
7729 return 400 + (int32_t)nCBORError;
7730 }
7731 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
7732 return 500;
7733 }
7734
7735 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7736 return (int32_t)nCBORError;
7737 }
7738 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
7739 return 600;
7740 }
7741
7742 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
7743 return 900 + (int32_t)nCBORError;
7744 }
7745 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
7746 Item.uDataType != QCBOR_TYPE_INT64 ||
7747 Item.val.int64 != 42 ||
7748 Item.uDataAlloc ||
7749 Item.uLabelAlloc ||
7750 UsefulBufCompareToSZ(Item.label.string, "first integer")) {
7751 return 1000;
7752 }
7753
7754 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7755 return 1100 + (int32_t)nCBORError;
7756 }
7757
7758 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
7759 Item.uDataType != QCBOR_TYPE_INT64 ||
7760 Item.val.int64 != 42 ||
7761 Item.uDataAlloc ||
7762 Item.uLabelAlloc ||
7763 UsefulBufCompareToSZ(Item.label.string, "first integer")) {
7764 return 1200;
7765 }
7766
7767
7768 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7769 return 1300 + (int32_t)nCBORError;
7770 }
7771 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
7772 Item.uDataAlloc ||
7773 Item.uLabelAlloc ||
7774 UsefulBufCompareToSZ(Item.label.string, "an array of two strings") ||
7775 Item.uDataType != QCBOR_TYPE_ARRAY ||
Laurence Lundbladecf41c522021-02-20 10:19:07 -07007776 Item.val.uCount != 2) {
Laurence Lundblade2a26abb2020-11-05 19:06:54 -08007777 return 1400;
Laurence Lundbladecf41c522021-02-20 10:19:07 -07007778 }
Laurence Lundblade2a26abb2020-11-05 19:06:54 -08007779
7780 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7781 return 1500 + (int32_t)nCBORError;
7782 }
7783 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
7784 Item.uDataAlloc ||
7785 Item.uLabelAlloc ||
7786 UsefulBufCompareToSZ(Item.val.string, "string1")) {
7787 return 1600;
7788 }
7789
7790 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
7791 return 1700 + (int32_t)nCBORError;
7792 }
7793 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
7794 Item.uDataAlloc ||
7795 Item.uLabelAlloc ||
7796 UsefulBufCompareToSZ(Item.val.string, "string2")) {
7797 return 1800;
7798 }
7799
7800 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
7801 return (int32_t)nCBORError;
7802 }
7803 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
7804 Item.uDataAlloc ||
7805 Item.uLabelAlloc ||
7806 UsefulBufCompareToSZ(Item.val.string, "string2")) {
7807 return 1900;
7808 }
7809
7810 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7811 return (int32_t)nCBORError;
7812 }
7813 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
7814 Item.uDataAlloc ||
7815 Item.uLabelAlloc ||
7816 UsefulBufCompareToSZ(Item.val.string, "string2")) {
7817 return 2000;
7818 }
7819
7820
7821 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7822 return 2100 + (int32_t)nCBORError;
7823 }
7824 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
7825 Item.uDataAlloc ||
7826 Item.uLabelAlloc ||
7827 UsefulBufCompareToSZ(Item.label.string, "map in a map") ||
7828 Item.uDataType != QCBOR_TYPE_MAP ||
7829 Item.val.uCount != 4) {
7830 return 2100;
7831 }
7832
7833 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7834 return 2200 + (int32_t)nCBORError;
7835 }
7836 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
7837 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("bytes 1"))||
7838 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
7839 Item.uDataAlloc ||
7840 Item.uLabelAlloc ||
7841 UsefulBufCompareToSZ(Item.val.string, "xxxx")) {
7842 return 2300;
7843 }
7844
7845 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
7846 return 2400 + (int32_t)nCBORError;
7847 }
7848 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
7849 UsefulBufCompareToSZ(Item.label.string, "bytes 2") ||
7850 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
7851 Item.uDataAlloc ||
7852 Item.uLabelAlloc ||
7853 UsefulBufCompareToSZ(Item.val.string, "yyyy")) {
7854 return 2500;
7855 }
7856
7857 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7858 return 2600 + (int32_t)nCBORError;
7859 }
7860 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
7861 UsefulBufCompareToSZ(Item.label.string, "bytes 2") ||
7862 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
7863 Item.uDataAlloc ||
7864 Item.uLabelAlloc ||
7865 UsefulBufCompareToSZ(Item.val.string, "yyyy")) {
7866 return 2700;
7867 }
7868
7869 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7870 return 2800 + (int32_t)nCBORError;
7871 }
7872 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
7873 Item.uDataAlloc ||
7874 Item.uLabelAlloc ||
7875 UsefulBufCompareToSZ(Item.label.string, "another int") ||
7876 Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundbladecf41c522021-02-20 10:19:07 -07007877 Item.val.int64 != 98) {
Laurence Lundblade2a26abb2020-11-05 19:06:54 -08007878 return 2900;
Laurence Lundbladecf41c522021-02-20 10:19:07 -07007879 }
Laurence Lundblade2a26abb2020-11-05 19:06:54 -08007880
7881 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
7882 return 3000 + (int32_t)nCBORError;
7883 }
7884 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
7885 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("text 2"))||
7886 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
7887 Item.uDataAlloc ||
7888 Item.uLabelAlloc ||
7889 UsefulBufCompareToSZ(Item.val.string, "lies, damn lies and statistics")) {
7890 return 3100;
7891 }
7892
7893 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7894 return 3200 + (int32_t)nCBORError;
7895 }
7896 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
7897 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("text 2"))||
7898 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
7899 Item.uDataAlloc ||
7900 Item.uLabelAlloc ||
7901 UsefulBufCompareToSZ(Item.val.string, "lies, damn lies and statistics")) {
7902 return 3300;
7903 }
7904
Laurence Lundblade3427dee2021-06-20 11:11:24 -07007905 nCBORError = QCBORDecode_PeekNext(&DCtx, &Item);
7906 if(nCBORError != QCBOR_ERR_NO_MORE_ITEMS) {
7907 return 3300 + (int32_t)nCBORError;
7908 }
7909
7910 QCBORDecode_VPeekNext(&DCtx, &Item);
7911 nCBORError = QCBORDecode_GetError(&DCtx);
7912 if(nCBORError != QCBOR_ERR_NO_MORE_ITEMS) {
7913 return 3400 + (int32_t)nCBORError;
7914 }
7915
7916 QCBORDecode_VPeekNext(&DCtx, &Item);
7917 nCBORError = QCBORDecode_GetError(&DCtx);
7918 if(nCBORError != QCBOR_ERR_NO_MORE_ITEMS) {
7919 return 3500 + (int32_t)nCBORError;
7920 }
Laurence Lundbladecf41c522021-02-20 10:19:07 -07007921
7922
7923 // Rewind to top level after entering several maps
7924 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0);
7925
7926 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7927 return (int32_t)nCBORError;
7928 }
7929 if(Item.uDataType != QCBOR_TYPE_MAP ||
7930 Item.val.uCount != 3) {
7931 return 400;
7932 }
7933
7934 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7935 return 4000+(int32_t)nCBORError;
7936 }
7937
7938 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
7939 Item.uDataType != QCBOR_TYPE_INT64 ||
7940 Item.val.int64 != 42 ||
7941 Item.uDataAlloc ||
7942 Item.uLabelAlloc ||
7943 UsefulBufCompareToSZ(Item.label.string, "first integer")) {
7944 return 4100;
7945 }
7946
7947 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7948 return 4100+(int32_t)nCBORError;
7949 }
7950 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
7951 Item.uDataAlloc ||
7952 Item.uLabelAlloc ||
7953 UsefulBufCompareToSZ(Item.label.string, "an array of two strings") ||
7954 Item.uDataType != QCBOR_TYPE_ARRAY ||
7955 Item.val.uCount != 2) {
7956 return 4200;
7957 }
7958
7959 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7960 return 4200+(int32_t)nCBORError;
7961 }
7962 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
7963 Item.uDataAlloc ||
7964 Item.uLabelAlloc ||
7965 UsefulBufCompareToSZ(Item.val.string, "string1")) {
7966 return 4300;
7967 }
7968
7969 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7970 return 4300+(int32_t)nCBORError;
7971 }
7972 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
7973 Item.uDataAlloc ||
7974 Item.uLabelAlloc ||
7975 UsefulBufCompareToSZ(Item.val.string, "string2")) {
7976 return 4400;
7977 }
7978
7979 QCBORDecode_Rewind(&DCtx);
7980
7981 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7982 return 4400+(int32_t)nCBORError;
7983 }
7984 if(Item.uDataType != QCBOR_TYPE_MAP ||
7985 Item.val.uCount != 3) {
7986 return 4500;
7987 }
7988
7989 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7990 return (int32_t)nCBORError;
7991 }
7992
7993 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
7994 Item.uDataType != QCBOR_TYPE_INT64 ||
7995 Item.val.int64 != 42 ||
7996 Item.uDataAlloc ||
7997 Item.uLabelAlloc ||
7998 UsefulBufCompareToSZ(Item.label.string, "first integer")) {
7999 return 4600;
8000 }
8001
8002 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8003 return (int32_t)nCBORError;
8004 }
8005 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
8006 Item.uDataAlloc ||
8007 Item.uLabelAlloc ||
8008 UsefulBufCompareToSZ(Item.label.string, "an array of two strings") ||
8009 Item.uDataType != QCBOR_TYPE_ARRAY ||
8010 Item.val.uCount != 2) {
8011 return 4700;
8012 }
8013
8014 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8015 return (int32_t)nCBORError;
8016 }
8017 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
8018 Item.uDataAlloc ||
8019 Item.uLabelAlloc ||
8020 UsefulBufCompareToSZ(Item.val.string, "string1")) {
8021 return 4800;
8022 }
8023
8024 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8025 return 4900+(int32_t)nCBORError;
8026 }
8027 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
8028 Item.uDataAlloc ||
8029 Item.uLabelAlloc ||
8030 UsefulBufCompareToSZ(Item.val.string, "string2")) {
8031 return 5000;
8032 }
8033
8034
8035 // Rewind an entered map
8036 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0);
8037
8038 QCBORDecode_EnterMap(&DCtx, NULL);
8039
8040 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8041 return 5100+(int32_t)nCBORError;
8042 }
8043
8044 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
8045 Item.uDataType != QCBOR_TYPE_INT64 ||
8046 Item.val.int64 != 42 ||
8047 Item.uDataAlloc ||
8048 Item.uLabelAlloc ||
8049 UsefulBufCompareToSZ(Item.label.string, "first integer")) {
8050 return 5200;
8051 }
8052
8053 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8054 return 5200+(int32_t)nCBORError;
8055 }
8056 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
8057 Item.uDataAlloc ||
8058 Item.uLabelAlloc ||
8059 UsefulBufCompareToSZ(Item.label.string, "an array of two strings") ||
8060 Item.uDataType != QCBOR_TYPE_ARRAY ||
8061 Item.val.uCount != 2) {
8062 return -5300;
8063 }
8064
8065 QCBORDecode_Rewind(&DCtx);
8066
8067 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8068 return 5300+(int32_t)nCBORError;
8069 }
8070
8071 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
8072 Item.uDataType != QCBOR_TYPE_INT64 ||
8073 Item.val.int64 != 42 ||
8074 Item.uDataAlloc ||
8075 Item.uLabelAlloc ||
8076 UsefulBufCompareToSZ(Item.label.string, "first integer")) {
8077 return 5400;
8078 }
8079
8080 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8081 return 5400+(int32_t)nCBORError;
8082 }
8083 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
8084 Item.uDataAlloc ||
8085 Item.uLabelAlloc ||
8086 UsefulBufCompareToSZ(Item.label.string, "an array of two strings") ||
8087 Item.uDataType != QCBOR_TYPE_ARRAY ||
8088 Item.val.uCount != 2) {
8089 return 5500;
8090 }
8091
8092
8093 // Rewind and entered array inside an entered map
8094 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0);
8095
8096 QCBORDecode_EnterMap(&DCtx, NULL);
8097
8098 QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings");
8099
8100 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8101 return 5600+(int32_t)nCBORError;
8102 }
8103 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
8104 Item.uDataAlloc ||
8105 Item.uLabelAlloc ||
8106 UsefulBufCompareToSZ(Item.val.string, "string1")) {
8107 return 5700;
8108 }
8109
8110 QCBORDecode_Rewind(&DCtx);
8111
8112 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8113 return 5700+(int32_t)nCBORError;
8114 }
8115 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
8116 Item.uDataAlloc ||
8117 Item.uLabelAlloc ||
8118 UsefulBufCompareToSZ(Item.val.string, "string1")) {
8119 return 5800;
8120 }
8121
8122 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8123 return (int32_t)nCBORError;
8124 }
8125 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
8126 Item.uDataAlloc ||
8127 Item.uLabelAlloc ||
8128 UsefulBufCompareToSZ(Item.val.string, "string2")) {
8129 return 5900;
8130 }
8131
8132 QCBORDecode_Rewind(&DCtx);
8133
8134 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8135 return 5900+(int32_t)nCBORError;
8136 }
8137 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
8138 Item.uDataAlloc ||
8139 Item.uLabelAlloc ||
8140 UsefulBufCompareToSZ(Item.val.string, "string1")) {
8141 return 6000;
8142 }
8143
8144
8145 // Rewind a byte string inside an array inside an array
8146 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidWrappedMapEncoded), 0);
8147
8148 QCBORDecode_EnterArray(&DCtx, NULL);
8149
8150 uint64_t i;
8151 QCBORDecode_GetUInt64(&DCtx, &i);
8152
8153 QCBORDecode_EnterArray(&DCtx, NULL);
8154
8155 QCBORDecode_EnterBstrWrapped(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
8156 if(QCBORDecode_GetError(&DCtx)) {
8157 return 6100;
8158 }
8159
8160 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8161 return (int32_t)nCBORError;
8162 }
8163 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
8164 return 6200;
8165 }
8166
8167 QCBORDecode_Rewind(&DCtx);
8168
8169 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8170 return 6300+(int32_t)nCBORError;
8171 }
8172 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
8173 return 6400;
8174 }
8175
8176#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
8177 // Rewind a byte string inside an indefinite-length array inside
8178 // indefinite-length array
8179
8180 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidIndefWrappedMapEncoded), 0);
8181
8182 QCBORDecode_EnterArray(&DCtx, NULL);
8183
8184 QCBORDecode_GetUInt64(&DCtx, &i);
8185
8186 QCBORDecode_EnterArray(&DCtx, NULL);
8187
8188 QCBORDecode_EnterBstrWrapped(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
8189 if(QCBORDecode_GetError(&DCtx)) {
8190 return 6500;
8191 }
8192
8193 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8194 return 6600+(int32_t)nCBORError;
8195 }
8196 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
8197 return 6700;
8198 }
8199
8200 QCBORDecode_Rewind(&DCtx);
8201
8202 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8203 return 6800+(int32_t)nCBORError;
8204 }
8205 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
8206 return 6900;
8207 }
8208#endif
8209
8210 // Rewind an empty map
8211 // [100, {}]
8212 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pWithEmptyMap), 0);
8213 QCBORDecode_EnterArray(&DCtx, NULL);
8214 QCBORDecode_GetUInt64(&DCtx, &i);
8215 if(i != 100) {
8216 return 7010;
8217 }
8218 QCBORDecode_EnterMap(&DCtx, NULL);
8219
8220 /* Do it 5 times to be sure multiple rewinds work */
8221 for(int n = 0; n < 5; n++) {
8222 nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
8223 if(nCBORError != QCBOR_ERR_NO_MORE_ITEMS) {
8224 return 7000 + n;
8225 }
8226 QCBORDecode_Rewind(&DCtx);
8227 }
8228 QCBORDecode_ExitMap(&DCtx);
8229 QCBORDecode_Rewind(&DCtx);
8230 QCBORDecode_GetUInt64(&DCtx, &i);
8231 if(i != 100) {
8232 return 7010;
8233 }
8234 QCBORDecode_ExitArray(&DCtx);
8235 QCBORDecode_Rewind(&DCtx);
8236 QCBORDecode_EnterArray(&DCtx, NULL);
8237 i = 9;
8238 QCBORDecode_GetUInt64(&DCtx, &i);
8239 if(i != 100) {
8240 return 7020;
8241 }
8242 if(QCBORDecode_GetError(&DCtx)){
8243 return 7030;
8244 }
8245
8246 // Rewind an empty indefinite length map
8247#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
8248 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pWithEmptyMapInDef), 0);
8249 QCBORDecode_EnterArray(&DCtx, NULL);
8250 QCBORDecode_GetUInt64(&DCtx, &i);
8251 if(i != 100) {
8252 return 7810;
8253 }
8254 QCBORDecode_EnterMap(&DCtx, NULL);
8255
8256 /* Do it 5 times to be sure multiple rewinds work */
8257 for(int n = 0; n < 5; n++) {
8258 nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
8259 if(nCBORError != QCBOR_ERR_NO_MORE_ITEMS) {
8260 return 7800 + n;
8261 }
8262 QCBORDecode_Rewind(&DCtx);
8263 }
8264 QCBORDecode_ExitMap(&DCtx);
8265 QCBORDecode_Rewind(&DCtx);
8266 QCBORDecode_GetUInt64(&DCtx, &i);
8267 if(i != 100) {
8268 return 7810;
8269 }
8270 QCBORDecode_ExitArray(&DCtx);
8271 QCBORDecode_Rewind(&DCtx);
8272 QCBORDecode_EnterArray(&DCtx, NULL);
8273 i = 9;
8274 QCBORDecode_GetUInt64(&DCtx, &i);
8275 if(i != 100) {
8276 return 7820;
8277 }
8278 if(QCBORDecode_GetError(&DCtx)){
8279 return 7830;
8280 }
8281#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
8282
8283 // Rewind an indefnite length byte-string wrapped sequence
8284#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
Laurence Lundblade37286c02022-09-03 10:05:02 -07008285 // TODO: rewrite this test to not use tags
Laurence Lundbladecf41c522021-02-20 10:19:07 -07008286 QCBORDecode_Init(&DCtx,
8287 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pWrappedByIndefiniteLength),
8288 0);
8289 UsefulBuf_MAKE_STACK_UB(Pool, 100);
8290 QCBORDecode_SetMemPool(&DCtx, Pool, 0);
8291
8292 QCBORDecode_EnterArray(&DCtx, NULL);
8293 QCBORDecode_EnterBstrWrapped(&DCtx, 2, NULL);
Laurence Lundblade37286c02022-09-03 10:05:02 -07008294#ifndef QCBOR_DISABLE_TAGS
Laurence Lundbladecf41c522021-02-20 10:19:07 -07008295 if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_INPUT_TOO_LARGE) {
Laurence Lundblade37286c02022-09-03 10:05:02 -07008296 /* TODO: This is what happens when trying to enter
8297 * indefinite-length byte string wrapped CBOR. Tolerate for
8298 * now. Eventually it needs to be fixed so this works, but that
8299 * is not simple.
8300 */
Laurence Lundbladecf41c522021-02-20 10:19:07 -07008301 return 7300;
8302 }
8303
8304 /*
8305 QCBORDecode_GetUInt64(&DCtx, &i);
8306 if(i != 42) {
8307 return 7110;
8308 }
8309 QCBORDecode_Rewind(&DCtx);
8310 QCBORDecode_GetUInt64(&DCtx, &i);
8311 if(i != 42) {
8312 return 7220;
8313 }*/
Laurence Lundblade37286c02022-09-03 10:05:02 -07008314
8315#else /* QCBOR_DISABLE_TAGS */
8316 if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_TAGS_DISABLED) {
8317 return 7301;
8318 }
8319#endif /* QCBOR_DISABLE_TAGS */
8320
Laurence Lundbladecf41c522021-02-20 10:19:07 -07008321#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
8322
8323
8324 // Rewind an indefnite length byte-string wrapped sequence
8325
Laurence Lundblade2a26abb2020-11-05 19:06:54 -08008326 return 0;
8327}
Laurence Lundblade9f9c3732021-03-23 09:38:46 -07008328
8329
8330
8331
8332static const uint8_t spBooleansInMap[] =
8333{
8334 0xa1, 0x08, 0xf5
8335};
8336
8337static const uint8_t spBooleansInMapWrongType[] =
8338{
8339 0xa1, 0x08, 0xf6
8340};
8341
8342static const uint8_t spBooleansInMapNWF[] =
8343{
8344 0xa1, 0x08, 0x1a
8345};
8346
Laurence Lundblade8782dd32021-04-27 04:15:37 -07008347static const uint8_t spNullInMap[] =
8348{
8349 0xa1, 0x08, 0xf6
8350};
8351
8352static const uint8_t spUndefinedInMap[] =
8353{
8354 0xa1, 0x08, 0xf7
8355};
8356
Laurence Lundblade9f9c3732021-03-23 09:38:46 -07008357
8358int32_t BoolTest(void)
8359{
8360 QCBORDecodeContext DCtx;
8361 bool b;
8362
Laurence Lundblade8782dd32021-04-27 04:15:37 -07008363 QCBORDecode_Init(&DCtx,
8364 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMap),
8365 0);
Laurence Lundblade9f9c3732021-03-23 09:38:46 -07008366 QCBORDecode_EnterMap(&DCtx, NULL);
8367 QCBORDecode_GetBool(&DCtx, &b);
8368 if(QCBORDecode_GetAndResetError(&DCtx) || !b) {
8369 return 1;
8370 }
8371
8372 QCBORDecode_GetBoolInMapN(&DCtx, 7, &b);
8373 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_LABEL_NOT_FOUND) {
8374 return 2;
8375 }
8376
8377 QCBORDecode_GetBoolInMapN(&DCtx, 8, &b);
8378 if(QCBORDecode_GetAndResetError(&DCtx) || !b) {
8379 return 3;
8380 }
8381
8382
8383 QCBORDecode_GetBoolInMapSZ(&DCtx, "xx", &b);
8384 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_LABEL_NOT_FOUND) {
8385 return 4;
8386 }
8387
Laurence Lundblade8782dd32021-04-27 04:15:37 -07008388 QCBORDecode_Init(&DCtx,
8389 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMapWrongType),
8390 0);
Laurence Lundblade9f9c3732021-03-23 09:38:46 -07008391 QCBORDecode_EnterMap(&DCtx, NULL);
8392 QCBORDecode_GetBool(&DCtx, &b);
8393 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
8394 return 5;
8395 }
8396
Laurence Lundblade8782dd32021-04-27 04:15:37 -07008397 QCBORDecode_Init(&DCtx,
8398 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMapNWF),
8399 0);
Laurence Lundblade9f9c3732021-03-23 09:38:46 -07008400 QCBORDecode_EnterMap(&DCtx, NULL);
8401 QCBORDecode_GetBool(&DCtx, &b);
8402 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_HIT_END) {
8403 return 6;
8404 }
8405
Laurence Lundblade8782dd32021-04-27 04:15:37 -07008406
8407 QCBORDecode_Init(&DCtx,
8408 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spNullInMap),
8409 0);
8410 QCBORDecode_EnterMap(&DCtx, NULL);
8411 QCBORDecode_GetNull(&DCtx);
8412 if(QCBORDecode_GetAndResetError(&DCtx)) {
8413 return 7;
8414 }
8415
8416 QCBORDecode_Init(&DCtx,
8417 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMap),
8418 0);
8419 QCBORDecode_EnterMap(&DCtx, NULL);
8420 QCBORDecode_GetNull(&DCtx);
8421 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
8422 return 8;
8423 }
8424
8425 QCBORDecode_Init(&DCtx,
8426 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spNullInMap),
8427 0);
8428 QCBORDecode_EnterMap(&DCtx, NULL);
8429 QCBORDecode_GetNullInMapN(&DCtx, 8);
8430 if(QCBORDecode_GetAndResetError(&DCtx)) {
8431 return 9;
8432 }
8433
8434 QCBORDecode_Init(&DCtx,
8435 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMap),
8436 0);
8437 QCBORDecode_EnterMap(&DCtx, NULL);
8438 QCBORDecode_GetNullInMapN(&DCtx, 8);
8439 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
8440 return 10;
8441 }
8442
8443 QCBORDecode_Init(&DCtx,
8444 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMapNWF),
8445 0);
8446 QCBORDecode_EnterMap(&DCtx, NULL);
8447 QCBORDecode_GetUndefined(&DCtx);
8448 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_HIT_END) {
8449 return 11;
8450 }
8451
8452 QCBORDecode_Init(&DCtx,
8453 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUndefinedInMap),
8454 0);
8455 QCBORDecode_EnterMap(&DCtx, NULL);
8456 QCBORDecode_GetUndefined(&DCtx);
8457 if(QCBORDecode_GetAndResetError(&DCtx)) {
8458 return 12;
8459 }
8460
8461 QCBORDecode_Init(&DCtx,
8462 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMap),
8463 0);
8464 QCBORDecode_EnterMap(&DCtx, NULL);
8465 QCBORDecode_GetUndefined(&DCtx);
8466 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
8467 return 13;
8468 }
8469
8470 QCBORDecode_Init(&DCtx,
8471 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUndefinedInMap),
8472 0);
8473 QCBORDecode_EnterMap(&DCtx, NULL);
8474 QCBORDecode_GetUndefinedInMapN(&DCtx, 8);
8475 if(QCBORDecode_GetAndResetError(&DCtx)) {
8476 return 14;
8477 }
8478
8479 QCBORDecode_Init(&DCtx,
8480 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMap),
8481 0);
8482 QCBORDecode_EnterMap(&DCtx, NULL);
8483 QCBORDecode_GetUndefinedInMapN(&DCtx, 8);
8484 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
8485 return 15;
8486 }
8487
8488 QCBORDecode_Init(&DCtx,
8489 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMapNWF),
8490 0);
8491 QCBORDecode_EnterMap(&DCtx, NULL);
8492 QCBORDecode_GetUndefined(&DCtx);
8493 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_HIT_END) {
8494 return 15;
8495 }
8496
Laurence Lundblade9f9c3732021-03-23 09:38:46 -07008497 return 0;
8498}