blob: e913854d7a3d0c181a456edff0a5b99b4afb3c8b [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 Lundblade4e808ba2022-12-29 12:45:20 -07006135 "Big float: INT64_MIN * 2e-1 to test handling of INT64_MIN",
6136 {(uint8_t[]){0xC5, 0x82, 0x20,
6137 0x3B, 0x7f, 0xff, 0xff, 0xff, 0xff, 0x0ff, 0xff, 0xff,
6138 }, 15},
6139 -4611686018427387904, /* INT64_MIN / 2 */
6140 EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS),
6141 0,
6142 EXP_AND_MANTISSA_ERROR(QCBOR_ERR_NUMBER_SIGN_CONVERSION),
6143 -4.6116860184273879E+18,
6144 FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS))
6145 },
6146 {
Laurence Lundblade784b54b2020-08-10 01:24:52 -07006147 "too large to fit into int64_t",
6148 {(uint8_t[]){0xc3, 0x48, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 10},
6149 0,
6150 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
6151 0,
6152 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
6153 ((double)INT64_MIN) + 1 ,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006154 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)
Laurence Lundblade784b54b2020-08-10 01:24:52 -07006155 },
6156 {
6157 "largest negative int that fits in int64_t",
6158 {(uint8_t[]){0xc3, 0x48, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, 10},
6159 INT64_MIN,
6160 QCBOR_SUCCESS,
6161 0,
6162 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
6163 (double)INT64_MIN,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006164 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)
Laurence Lundblade784b54b2020-08-10 01:24:52 -07006165 },
6166 {
Laurence Lundbladeda095972020-06-06 18:35:33 -07006167 "negative bignum -1",
6168 {(uint8_t[]){0xc3, 0x41, 0x00}, 3},
6169 -1,
6170 QCBOR_SUCCESS,
6171 0,
6172 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
6173 -1.0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006174 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)
Laurence Lundbladeda095972020-06-06 18:35:33 -07006175 },
6176 {
Laurence Lundblade9ab5abb2020-05-20 12:10:45 -07006177 "Decimal Fraction with positive bignum 257 * 10e3",
Laurence Lundblade887add82020-05-17 05:50:34 -07006178 {(uint8_t[]){0xC4, 0x82, 0x1B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03,
6179 0xC2, 0x42, 0x01, 0x01}, 15},
6180 257000,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006181 EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS),
Laurence Lundblade887add82020-05-17 05:50:34 -07006182 257000,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006183 EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS),
Laurence Lundblade887add82020-05-17 05:50:34 -07006184 257000.0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006185 FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS))
Laurence Lundblade887add82020-05-17 05:50:34 -07006186 },
6187 {
Laurence Lundbladeda095972020-06-06 18:35:33 -07006188 "bigfloat with negative bignum -258 * 2e3",
Laurence Lundblade887add82020-05-17 05:50:34 -07006189 {(uint8_t[]){0xC5, 0x82, 0x1B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03,
6190 0xC3, 0x42, 0x01, 0x01}, 15},
Laurence Lundbladeda095972020-06-06 18:35:33 -07006191 -2064,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006192 EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS),
Laurence Lundblade887add82020-05-17 05:50:34 -07006193 0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006194 EXP_AND_MANTISSA_ERROR(QCBOR_ERR_NUMBER_SIGN_CONVERSION),
Laurence Lundbladeda095972020-06-06 18:35:33 -07006195 -2064.0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006196 FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS))
Laurence Lundblade887add82020-05-17 05:50:34 -07006197 },
6198 {
Laurence Lundblade9ab5abb2020-05-20 12:10:45 -07006199 "bigfloat with positive bignum 257 * 2e3",
Laurence Lundblade887add82020-05-17 05:50:34 -07006200 {(uint8_t[]){0xC5, 0x82, 0x1B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03,
6201 0xC2, 0x42, 0x01, 0x01}, 15},
6202 2056,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006203 EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS),
Laurence Lundblade887add82020-05-17 05:50:34 -07006204 2056,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006205 EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS),
Laurence Lundblade887add82020-05-17 05:50:34 -07006206 2056.0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006207 FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS))
Laurence Lundblade887add82020-05-17 05:50:34 -07006208 },
6209 {
Laurence Lundbladeda095972020-06-06 18:35:33 -07006210 "negative bignum 0xc349010000000000000000 -18446744073709551617",
Laurence Lundblade887add82020-05-17 05:50:34 -07006211 {(uint8_t[]){0xc3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 11},
6212 0,
6213 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
6214 0,
6215 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
6216 -18446744073709551617.0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006217 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)
Laurence Lundblade887add82020-05-17 05:50:34 -07006218 },
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08006219#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
Laurence Lundblade887add82020-05-17 05:50:34 -07006220 {
Laurence Lundblade313b2862020-05-16 01:23:06 -07006221 "Positive bignum 0x01020304 indefinite length string",
6222 {(uint8_t[]){0xC2, 0x5f, 0x42, 0x01, 0x02, 0x41, 0x03, 0x41, 0x04, 0xff}, 10},
6223 0x01020304,
6224 QCBOR_SUCCESS,
6225 0x01020304,
6226 QCBOR_SUCCESS,
6227 16909060.0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006228 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)
Laurence Lundblade313b2862020-05-16 01:23:06 -07006229 },
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08006230#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
Laurence Lundblade313b2862020-05-16 01:23:06 -07006231 {
Laurence Lundblade887add82020-05-17 05:50:34 -07006232 "Decimal Fraction with neg bignum [9223372036854775807, -4759477275222530853137]",
Laurence Lundblade313b2862020-05-16 01:23:06 -07006233 {(uint8_t[]){0xC4, 0x82, 0x1B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
6234 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,}, 23},
6235 0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006236 EXP_AND_MANTISSA_ERROR(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW),
Laurence Lundblade313b2862020-05-16 01:23:06 -07006237 0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006238 EXP_AND_MANTISSA_ERROR(QCBOR_ERR_NUMBER_SIGN_CONVERSION),
Laurence Lundblade313b2862020-05-16 01:23:06 -07006239 -INFINITY,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006240 FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS))
Laurence Lundblade313b2862020-05-16 01:23:06 -07006241 },
6242 {
6243 "big float [9223372036854775806, 9223372036854775806]",
6244 {(uint8_t[]){0xC5, 0x82, 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE,
6245 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE}, 20},
6246 0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006247 EXP_AND_MANTISSA_ERROR(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW),
Laurence Lundblade313b2862020-05-16 01:23:06 -07006248 0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006249 EXP_AND_MANTISSA_ERROR(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW),
Laurence Lundblade313b2862020-05-16 01:23:06 -07006250 INFINITY,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006251 FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS))
Laurence Lundblade313b2862020-05-16 01:23:06 -07006252 },
6253 {
Laurence Lundblade983500d2020-05-14 11:49:34 -07006254 "Big float 3 * 2^^2",
6255 {(uint8_t[]){0xC5, 0x82, 0x02, 0x03}, 4},
6256 12,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006257 EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS),
Laurence Lundblade983500d2020-05-14 11:49:34 -07006258 12,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006259 EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS),
Laurence Lundblade983500d2020-05-14 11:49:34 -07006260 12.0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006261 FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS))
Laurence Lundblade983500d2020-05-14 11:49:34 -07006262 },
Laurence Lundblade983500d2020-05-14 11:49:34 -07006263 {
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006264 "Decimal fraction 3/10",
6265 {(uint8_t[]){0xC4, 0x82, 0x20, 0x03}, 4},
6266 0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006267 EXP_AND_MANTISSA_ERROR(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW),
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006268 0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006269 EXP_AND_MANTISSA_ERROR(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW),
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006270 0.30000000000000004,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006271 FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS))
Laurence Lundbladedfd49fc2020-09-01 14:17:16 -07006272 },
6273 {
Laurence Lundblade11fd78b2020-09-01 22:13:27 -07006274 "extreme pos bignum",
6275 {(uint8_t[]){0xc2, 0x59, 0x01, 0x90,
6276 // 50 rows of 8 is 400 digits.
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, 0xf0,
6315 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6316 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6317 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6318 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6319 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6320 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6321 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6322 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6323 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6324 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6325 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6326 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0},
6327 404},
6328 0,
6329 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
6330 0,
6331 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
Laurence Lundblade11fd78b2020-09-01 22:13:27 -07006332 INFINITY,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006333 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS),
Laurence Lundblade11fd78b2020-09-01 22:13:27 -07006334 },
6335
6336 {
6337 "extreme neg bignum",
6338 {(uint8_t[]){0xc3, 0x59, 0x01, 0x90,
6339 // 50 rows of 8 is 400 digits.
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, 0xf0,
6378 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6379 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6380 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6381 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6382 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6383 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6384 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6385 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6386 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6387 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6388 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6389 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0},
6390 404},
6391 0,
6392 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
6393 0,
6394 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
Laurence Lundblade11fd78b2020-09-01 22:13:27 -07006395 -INFINITY,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006396 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)
Laurence Lundblade11fd78b2020-09-01 22:13:27 -07006397 },
Laurence Lundblade51722fd2020-09-02 13:01:33 -07006398
6399 {
6400 "big float underflow [9223372036854775806, -9223372036854775806]",
6401 {(uint8_t[]){
6402 0xC5, 0x82,
6403 0x3B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE,
6404 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE}, 20},
Laurence Lundblade51722fd2020-09-02 13:01:33 -07006405 0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006406 EXP_AND_MANTISSA_ERROR(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW),
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 FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS))
Laurence Lundblade51722fd2020-09-02 13:01:33 -07006411 },
6412
6413 {
6414 "bigfloat that evaluates to -INFINITY",
6415 {(uint8_t[]){
6416 0xC5, 0x82,
6417 0x1B, 0x70, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03,
6418 0xC3, 0x42, 0x01, 0x01}, 15},
Laurence Lundblade51722fd2020-09-02 13:01:33 -07006419 0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006420 EXP_AND_MANTISSA_ERROR(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW),
Laurence Lundblade51722fd2020-09-02 13:01:33 -07006421 0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006422 EXP_AND_MANTISSA_ERROR(QCBOR_ERR_NUMBER_SIGN_CONVERSION),
Laurence Lundblade51722fd2020-09-02 13:01:33 -07006423 -INFINITY,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006424 FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS))
Laurence Lundblade51722fd2020-09-02 13:01:33 -07006425 },
Laurence Lundblade37286c02022-09-03 10:05:02 -07006426 {
6427 "Positive bignum 0xffff",
6428 {(uint8_t[]){0xC2, 0x42, 0xff, 0xff}, 4},
6429 65536-1,
6430 QCBOR_SUCCESS,
6431 0xffff,
6432 QCBOR_SUCCESS,
6433 65535.0,
6434 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)
6435 },
6436#endif /* QCBOR_DISABLE_TAGS */
6437 {
6438 "Positive integer 18446744073709551615",
6439 {(uint8_t[]){0x1b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, 9},
6440 0,
6441 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
6442 18446744073709551615ULL,
6443 QCBOR_SUCCESS,
6444 18446744073709551615.0,
6445 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)
6446 },
6447
6448 {
6449 "Postive integer 0",
6450 {(uint8_t[]){0x0}, 1},
6451 0LL,
6452 QCBOR_SUCCESS,
6453 0ULL,
6454 QCBOR_SUCCESS,
6455 0.0,
6456 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)
6457 },
6458 {
6459 "Negative integer -18446744073709551616",
6460 {(uint8_t[]){0x3b, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }, 9},
6461 -9223372036854775807-1, // INT64_MIN
6462 QCBOR_SUCCESS,
6463 0ULL,
6464 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
6465 -9223372036854775808.0,
6466 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)
6467 },
6468 {
6469 "Double Floating point value 100.3",
6470 {(uint8_t[]){0xfb, 0x40, 0x59, 0x13, 0x33, 0x33, 0x33, 0x33, 0x33}, 9},
6471 100L,
6472 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS),
6473 100ULL,
6474 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS),
6475 100.3,
6476 FLOAT_ERR_CODE_NO_FLOAT(QCBOR_SUCCESS),
6477 },
6478 {
6479 "Floating point value NaN 0xfa7fc00000",
6480 {(uint8_t[]){0xfa, 0x7f, 0xc0, 0x00, 0x00}, 5},
6481 0,
6482 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_FLOAT_EXCEPTION),
6483 0,
6484 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_FLOAT_EXCEPTION),
6485 NAN,
6486 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS),
6487 },
6488 {
6489 "half-precision Floating point value -4",
6490 {(uint8_t[]){0xf9, 0xc4, 0x00}, 3},
6491 // Normal case with all enabled.
6492 -4,
6493 FLOAT_ERR_CODE_NO_HALF_PREC_NO_FLOAT_HW(QCBOR_SUCCESS),
6494 0,
6495 FLOAT_ERR_CODE_NO_HALF_PREC_NO_FLOAT_HW(QCBOR_ERR_NUMBER_SIGN_CONVERSION),
6496 -4.0,
6497 FLOAT_ERR_CODE_NO_HALF_PREC(QCBOR_SUCCESS)
6498 },
6499 {
6500 "+inifinity single precision",
6501 {(uint8_t[]){0xfa, 0x7f, 0x80, 0x00, 0x00}, 5},
6502 0,
6503 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_FLOAT_EXCEPTION),
6504 0,
6505 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW),
6506 INFINITY,
6507 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)
6508 },
6509
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006510};
Laurence Lundblade9c905e82020-04-25 11:31:38 -07006511
6512
6513
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08006514
6515static int32_t SetUpDecoder(QCBORDecodeContext *DCtx, UsefulBufC CBOR, UsefulBuf Pool)
6516{
6517 QCBORDecode_Init(DCtx, CBOR, QCBOR_DECODE_MODE_NORMAL);
6518#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
6519 if(QCBORDecode_SetMemPool(DCtx, Pool, 0)) {
6520 return 1;
6521 }
6522#else /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
6523 (void)Pool;
6524#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
6525 return 0;
6526}
6527
6528
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03006529int32_t IntegerConvertTest(void)
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006530{
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08006531 const int nNumTests = C_ARRAY_COUNT(NumberConversions,
6532 struct NumberConversion);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006533
Laurence Lundblade9ab5abb2020-05-20 12:10:45 -07006534 for(int nIndex = 0; nIndex < nNumTests; nIndex++) {
6535 const struct NumberConversion *pF = &NumberConversions[nIndex];
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07006536
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006537 // Set up the decoding context including a memory pool so that
6538 // indefinite length items can be checked
6539 QCBORDecodeContext DCtx;
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006540 UsefulBuf_MAKE_STACK_UB(Pool, 100);
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07006541
6542 /* ----- test conversion to int64_t ------ */
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08006543 if(SetUpDecoder(&DCtx, pF->CBOR, Pool)) {
6544 return (int32_t)(3333+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006545 }
6546
6547 int64_t nInt;
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006548 QCBORDecode_GetInt64ConvertAll(&DCtx, 0xffff, &nInt);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07006549 if(QCBORDecode_GetError(&DCtx) != pF->uErrorInt64) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07006550 return (int32_t)(2000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006551 }
6552 if(pF->uErrorInt64 == QCBOR_SUCCESS && pF->nConvertedToInt64 != nInt) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07006553 return (int32_t)(3000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006554 }
6555
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07006556 /* ----- test conversion to uint64_t ------ */
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08006557 if(SetUpDecoder(&DCtx, pF->CBOR, Pool)) {
6558 return (int32_t)(3333+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006559 }
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08006560
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006561 uint64_t uInt;
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006562 QCBORDecode_GetUInt64ConvertAll(&DCtx, 0xffff, &uInt);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07006563 if(QCBORDecode_GetError(&DCtx) != pF->uErrorUint64) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07006564 return (int32_t)(4000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006565 }
6566 if(pF->uErrorUint64 == QCBOR_SUCCESS && pF->uConvertToUInt64 != uInt) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07006567 return (int32_t)(5000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006568 }
6569
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07006570 /* ----- test conversion to double ------ */
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08006571 if(SetUpDecoder(&DCtx, pF->CBOR, Pool)) {
6572 return (int32_t)(3333+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006573 }
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006574
6575#ifndef USEFULBUF_DISABLE_ALL_FLOAT
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006576 double d;
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006577 QCBORDecode_GetDoubleConvertAll(&DCtx, 0xffff, &d);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07006578 if(QCBORDecode_GetError(&DCtx) != pF->uErrorDouble) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07006579 return (int32_t)(6000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006580 }
6581 if(pF->uErrorDouble == QCBOR_SUCCESS) {
6582 if(isnan(pF->dConvertToDouble)) {
Laurence Lundblade983500d2020-05-14 11:49:34 -07006583 // NaN's can't be compared for equality. A NaN is
6584 // never equal to anything including another NaN
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006585 if(!isnan(d)) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07006586 return (int32_t)(7000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006587 }
6588 } else {
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006589 if(pF->dConvertToDouble != d) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07006590 return (int32_t)(8000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006591 }
6592 }
6593 }
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006594#endif /* USEFULBUF_DISABLE_ALL_FLOAT */
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006595 }
6596
6597 return 0;
6598}
6599
Laurence Lundbladea8758502022-05-15 17:57:46 -07006600#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
6601
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03006602int32_t CBORTestIssue134(void)
David Navarro9123e5b2022-03-28 16:04:03 +02006603{
6604 QCBORDecodeContext DCtx;
6605 QCBORItem Item;
6606 QCBORError uCBORError;
6607 const uint8_t spTestIssue134[] = { 0x5F, 0x40, 0xFF };
Laurence Lundblade9c905e82020-04-25 11:31:38 -07006608
David Navarro9123e5b2022-03-28 16:04:03 +02006609 QCBORDecode_Init(&DCtx,
6610 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTestIssue134),
6611 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade97c61bf2020-05-02 11:24:06 -07006612
David Navarro9123e5b2022-03-28 16:04:03 +02006613 UsefulBuf_MAKE_STACK_UB(StringBuf, 200);
6614 QCBORDecode_SetMemPool(&DCtx, StringBuf, false);
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03006615
David Navarro9123e5b2022-03-28 16:04:03 +02006616 do {
6617 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
6618 } while (QCBOR_SUCCESS == uCBORError);
6619
6620 uCBORError = QCBORDecode_Finish(&DCtx);
6621
Laurence Lundblade11ea3612022-07-01 13:26:23 -07006622 return (int32_t)uCBORError;
David Navarro9123e5b2022-03-28 16:04:03 +02006623}
Laurence Lundblade97c61bf2020-05-02 11:24:06 -07006624
Laurence Lundbladea8758502022-05-15 17:57:46 -07006625#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
6626
Laurence Lundblade37286c02022-09-03 10:05:02 -07006627
6628
6629static const uint8_t spSequenceTestInput[] = {
6630 /* 1. The valid date string "1985-04-12" */
6631 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string
6632
6633 /* 2. */
6634 0x00,
6635
6636 /* 3. A valid epoch date, 1400000000; Tue, 13 May 2014 16:53:20 GMT */
6637 0x1a, 0x53, 0x72, 0x4E, 0x00,
6638
6639 /* 4. */
6640 0x62, 'h', 'i',
6641};
6642
6643
Laurence Lundbladee3553422020-05-02 11:11:17 -07006644int32_t CBORSequenceDecodeTests(void)
6645{
6646 QCBORDecodeContext DCtx;
Laurence Lundblade87495732021-02-26 10:05:55 -07006647 QCBORItem Item;
6648 QCBORError uCBORError;
6649 size_t uConsumed;
Laurence Lundbladee3553422020-05-02 11:11:17 -07006650
6651 // --- Test a sequence with extra bytes ---
Laurence Lundblade9b334962020-08-27 10:55:53 -07006652
Laurence Lundbladee3553422020-05-02 11:11:17 -07006653 QCBORDecode_Init(&DCtx,
Laurence Lundblade37286c02022-09-03 10:05:02 -07006654 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSequenceTestInput),
Laurence Lundbladee3553422020-05-02 11:11:17 -07006655 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade9b334962020-08-27 10:55:53 -07006656
Laurence Lundblade37286c02022-09-03 10:05:02 -07006657 // Get 1.
Laurence Lundbladee3553422020-05-02 11:11:17 -07006658 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
6659 if(uCBORError != QCBOR_SUCCESS) {
6660 return 1;
6661 }
Laurence Lundblade37286c02022-09-03 10:05:02 -07006662 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ) {
Laurence Lundbladee3553422020-05-02 11:11:17 -07006663 return 2;
6664 }
Laurence Lundbladec7114722020-08-13 05:11:40 -07006665
Laurence Lundblade87495732021-02-26 10:05:55 -07006666 uCBORError = QCBORDecode_PartialFinish(&DCtx, &uConsumed);
6667 if(uCBORError != QCBOR_ERR_EXTRA_BYTES ||
Laurence Lundblade37286c02022-09-03 10:05:02 -07006668 uConsumed != 11) {
6669 return 102;
6670 }
6671
6672 // Get 2.
6673 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
6674 if(uCBORError != QCBOR_SUCCESS) {
6675 return 66;
6676 }
6677
6678 uCBORError = QCBORDecode_PartialFinish(&DCtx, &uConsumed);
6679 if(uCBORError != QCBOR_ERR_EXTRA_BYTES ||
Laurence Lundblade87495732021-02-26 10:05:55 -07006680 uConsumed != 12) {
6681 return 102;
6682 }
6683
Laurence Lundblade37286c02022-09-03 10:05:02 -07006684 // Get 3.
Laurence Lundbladec7114722020-08-13 05:11:40 -07006685 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundbladee3553422020-05-02 11:11:17 -07006686 if(uCBORError != QCBOR_SUCCESS) {
6687 return 2;
6688 }
Laurence Lundblade37286c02022-09-03 10:05:02 -07006689 if(Item.uDataType != QCBOR_TYPE_INT64) {
Laurence Lundbladee3553422020-05-02 11:11:17 -07006690 return 3;
6691 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006692
Laurence Lundbladee3553422020-05-02 11:11:17 -07006693 // A sequence can have stuff at the end that may
6694 // or may not be valid CBOR. The protocol decoder knows
6695 // when to stop by definition of the protocol, not
6696 // when the top-level map or array is ended.
6697 // Finish still has to be called to know that
6698 // maps and arrays (if there were any) were closed
6699 // off correctly. When called like this it
6700 // must return the error QCBOR_ERR_EXTRA_BYTES.
6701 uCBORError = QCBORDecode_Finish(&DCtx);
6702 if(uCBORError != QCBOR_ERR_EXTRA_BYTES) {
6703 return 4;
6704 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006705
Laurence Lundbladee3553422020-05-02 11:11:17 -07006706 // --- Test an empty input ----
6707 uint8_t empty[1];
6708 UsefulBufC Empty = {empty, 0};
6709 QCBORDecode_Init(&DCtx,
6710 Empty,
6711 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade9b334962020-08-27 10:55:53 -07006712
Laurence Lundbladee3553422020-05-02 11:11:17 -07006713 uCBORError = QCBORDecode_Finish(&DCtx);
6714 if(uCBORError != QCBOR_SUCCESS) {
6715 return 5;
6716 }
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07006717
6718
Laurence Lundbladee3553422020-05-02 11:11:17 -07006719 // --- Sequence with unclosed indefinite length array ---
6720 static const uint8_t xx[] = {0x01, 0x9f, 0x02};
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07006721
Laurence Lundbladee3553422020-05-02 11:11:17 -07006722 QCBORDecode_Init(&DCtx,
6723 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(xx),
6724 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07006725
Laurence Lundbladee3553422020-05-02 11:11:17 -07006726 // Get the first item
6727 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
6728 if(uCBORError != QCBOR_SUCCESS) {
6729 return 7;
6730 }
6731 if(Item.uDataType != QCBOR_TYPE_INT64) {
6732 return 8;
6733 }
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07006734
Laurence Lundbladee3553422020-05-02 11:11:17 -07006735 // Get a second item
6736 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08006737#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundbladee3553422020-05-02 11:11:17 -07006738 if(uCBORError != QCBOR_SUCCESS) {
6739 return 9;
6740 }
6741 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
6742 return 10;
6743 }
6744
6745 // Try to finish before consuming all bytes to confirm
6746 // that the still-open error is returned.
6747 uCBORError = QCBORDecode_Finish(&DCtx);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006748 if(uCBORError != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
Laurence Lundbladee3553422020-05-02 11:11:17 -07006749 return 11;
6750 }
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08006751#else /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
6752 if(uCBORError != QCBOR_ERR_INDEF_LEN_ARRAYS_DISABLED) {
6753 return 20;
6754 }
6755#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
Laurence Lundbladee3553422020-05-02 11:11:17 -07006756
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07006757
Laurence Lundbladee3553422020-05-02 11:11:17 -07006758 // --- Sequence with a closed indefinite length array ---
6759 static const uint8_t yy[] = {0x01, 0x9f, 0xff};
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07006760
Laurence Lundbladee3553422020-05-02 11:11:17 -07006761 QCBORDecode_Init(&DCtx,
6762 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(yy),
6763 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07006764
Laurence Lundbladee3553422020-05-02 11:11:17 -07006765 // Get the first item
6766 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
6767 if(uCBORError != QCBOR_SUCCESS) {
6768 return 12;
6769 }
6770 if(Item.uDataType != QCBOR_TYPE_INT64) {
6771 return 13;
6772 }
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07006773
Laurence Lundbladee3553422020-05-02 11:11:17 -07006774 // Get a second item
6775 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08006776#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
6777
Laurence Lundbladee3553422020-05-02 11:11:17 -07006778 if(uCBORError != QCBOR_SUCCESS) {
6779 return 14;
6780 }
6781 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
6782 return 15;
6783 }
6784
6785 // Try to finish before consuming all bytes to confirm
6786 // that the still-open error is returned.
6787 uCBORError = QCBORDecode_Finish(&DCtx);
6788 if(uCBORError != QCBOR_SUCCESS) {
6789 return 16;
6790 }
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08006791#else /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
6792 if(uCBORError != QCBOR_ERR_INDEF_LEN_ARRAYS_DISABLED) {
6793 return 20;
6794 }
6795#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
Laurence Lundbladee3553422020-05-02 11:11:17 -07006796
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07006797
Laurence Lundbladee3553422020-05-02 11:11:17 -07006798 return 0;
6799}
6800
Laurence Lundbladee15326f2020-06-15 15:50:23 -07006801
Laurence Lundblade70ecead2020-06-15 19:40:06 -07006802
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03006803int32_t IntToTests(void)
Laurence Lundbladee15326f2020-06-15 15:50:23 -07006804{
6805 int nErrCode;
6806 int32_t n32;
6807 int16_t n16;
6808 int8_t n8;
6809 uint32_t u32;
6810 uint16_t u16;
6811 uint8_t u8;
6812 uint64_t u64;
6813
6814 nErrCode = QCBOR_Int64ToInt32(1, &n32);
6815 if(nErrCode == -1 || n32 != 1) {
6816 return 1;
6817 }
6818
6819 nErrCode = QCBOR_Int64ToInt32((int64_t)INT32_MAX, &n32);
6820 if(nErrCode == -1 || n32 != INT32_MAX) {
6821 return 2;
6822 }
6823
6824 nErrCode = QCBOR_Int64ToInt32((int64_t)INT32_MIN, &n32);
6825 if(nErrCode == -1 || n32 != INT32_MIN) {
6826 return 3;
6827 }
6828
6829 nErrCode = QCBOR_Int64ToInt32(((int64_t)INT32_MAX)+1, &n32);
6830 if(nErrCode != -1) {
6831 return 4;
6832 }
6833
6834 nErrCode = QCBOR_Int64ToInt32(((int64_t)INT32_MIN)-1, &n32);
6835 if(nErrCode != -1) {
6836 return 5;
6837 }
6838
6839
6840 nErrCode = QCBOR_Int64ToInt16((int64_t)INT16_MAX, &n16);
6841 if(nErrCode == -1 || n16 != INT16_MAX) {
6842 return 6;
6843 }
6844
6845 nErrCode = QCBOR_Int64ToInt16((int64_t)INT16_MIN, &n16);
6846 if(nErrCode == -1 || n16 != INT16_MIN) {
6847 return 7;
6848 }
6849
6850 nErrCode = QCBOR_Int64ToInt16(1, &n16);
6851 if(nErrCode == -1 || n16 != 1) {
6852 return 8;
6853 }
6854
6855 nErrCode = QCBOR_Int64ToInt16(((int64_t)INT16_MAX)+1, &n16);
6856 if(nErrCode != -1) {
6857 return 9;
6858 }
6859
6860 nErrCode = QCBOR_Int64ToInt16(((int64_t)INT16_MIN)-1, &n16);
6861 if(nErrCode != -1) {
6862 return 10;
6863 }
6864
6865
6866 nErrCode = QCBOR_Int64ToInt8(1, &n8);
6867 if(nErrCode == -1 || n8 != 1) {
6868 return 11;
6869 }
6870
6871 nErrCode = QCBOR_Int64ToInt8((int64_t)INT8_MAX, &n8);
6872 if(nErrCode == -1 || n8 != INT8_MAX) {
6873 return 12;
6874 }
6875
6876 nErrCode = QCBOR_Int64ToInt8((int64_t)INT8_MIN, &n8);
6877 if(nErrCode == -1 || n8 != INT8_MIN) {
6878 return 13;
6879 }
6880
6881 nErrCode = QCBOR_Int64ToInt8(((int64_t)INT8_MAX)+1, &n8);
6882 if(nErrCode != -1) {
6883 return 14;
6884 }
6885
6886 nErrCode = QCBOR_Int64ToInt8(((int64_t)INT8_MIN)-1, &n8);
6887 if(nErrCode != -1) {
6888 return 15;
6889 }
6890
6891
6892 nErrCode = QCBOR_Int64ToUInt32(1, &u32);
6893 if(nErrCode == -1 || u32 != 1) {
6894 return 16;
6895 }
6896
6897 nErrCode = QCBOR_Int64ToUInt32((int64_t)UINT32_MAX, &u32);
6898 if(nErrCode == -1 || u32 != UINT32_MAX) {
6899 return 17;
6900 }
6901
6902 nErrCode = QCBOR_Int64ToUInt32((int64_t)0, &u32);
6903 if(nErrCode == -1 || u32 != 0) {
6904 return 18;
6905 }
6906
6907 nErrCode = QCBOR_Int64ToUInt32(((int64_t)UINT32_MAX)+1, &u32);
6908 if(nErrCode != -1) {
6909 return 19;
6910 }
6911
6912 nErrCode = QCBOR_Int64ToUInt32((int64_t)-1, &u32);
6913 if(nErrCode != -1) {
6914 return 20;
6915 }
6916
6917
6918 nErrCode = QCBOR_Int64UToInt16((int64_t)UINT16_MAX, &u16);
6919 if(nErrCode == -1 || u16 != UINT16_MAX) {
6920 return 21;
6921 }
6922
6923 nErrCode = QCBOR_Int64UToInt16((int64_t)0, &u16);
6924 if(nErrCode == -1 || u16 != 0) {
6925 return 22;
6926 }
6927
6928 nErrCode = QCBOR_Int64UToInt16(1, &u16);
6929 if(nErrCode == -1 || u16 != 1) {
6930 return 23;
6931 }
6932
6933 nErrCode = QCBOR_Int64UToInt16(((int64_t)UINT16_MAX)+1, &u16);
6934 if(nErrCode != -1) {
6935 return 24;
6936 }
6937
6938 nErrCode = QCBOR_Int64UToInt16((int64_t)-1, &u16);
6939 if(nErrCode != -1) {
6940 return 25;
6941 }
6942
6943
6944 nErrCode = QCBOR_Int64ToUInt8((int64_t)UINT8_MAX, &u8);
6945 if(nErrCode == -1 || u8 != UINT8_MAX) {
6946 return 26;
6947 }
6948
6949 nErrCode = QCBOR_Int64ToUInt8((int64_t)0, &u8);
6950 if(nErrCode == -1 || u8 != 0) {
6951 return 27;
6952 }
6953
6954 nErrCode = QCBOR_Int64ToUInt8(1, &u8);
6955 if(nErrCode == -1 || u8 != 1) {
6956 return 28;
6957 }
6958
6959 nErrCode = QCBOR_Int64ToUInt8(((int64_t)UINT16_MAX)+1, &u8);
6960 if(nErrCode != -1) {
6961 return 29;
6962 }
6963
6964 nErrCode = QCBOR_Int64ToUInt8((int64_t)-1, &u8);
6965 if(nErrCode != -1) {
6966 return 30;
6967 }
6968
6969
6970 nErrCode = QCBOR_Int64ToUInt64(1, &u64);
6971 if(nErrCode == -1 || u64 != 1) {
6972 return 31;
6973 }
6974
6975 nErrCode = QCBOR_Int64ToUInt64(INT64_MAX, &u64);
6976 if(nErrCode == -1 || u64 != INT64_MAX) {
6977 return 32;
6978 }
6979
6980 nErrCode = QCBOR_Int64ToUInt64((int64_t)0, &u64);
6981 if(nErrCode == -1 || u64 != 0) {
6982 return 33;
6983 }
6984
6985 nErrCode = QCBOR_Int64ToUInt64((int64_t)-1, &u64);
6986 if(nErrCode != -1) {
6987 return 34;
6988 }
6989
6990 return 0;
6991}
6992
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006993
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006994
6995
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006996/*
6997A sequence with
6998 A wrapping bstr
6999 containing a map
7000 1
7001 2
7002 A wrapping bstr
7003 containing an array
7004 3
7005 wrapping bstr
7006 4
7007 5
7008 6
7009 array
7010 7
7011 8
Laurence Lundblade0750fc42020-06-20 21:02:34 -07007012 */
7013
Laurence Lundblade55013642020-09-23 05:39:22 -07007014static UsefulBufC EncodeBstrWrapTestData(UsefulBuf OutputBuffer)
Laurence Lundblade0750fc42020-06-20 21:02:34 -07007015{
Laurence Lundblade55013642020-09-23 05:39:22 -07007016 UsefulBufC Encoded;
Laurence Lundblade0750fc42020-06-20 21:02:34 -07007017 QCBOREncodeContext EC;
Laurence Lundblade55013642020-09-23 05:39:22 -07007018 QCBORError uErr;
Laurence Lundblade0750fc42020-06-20 21:02:34 -07007019
Laurence Lundblade55013642020-09-23 05:39:22 -07007020 QCBOREncode_Init(&EC, OutputBuffer);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07007021
7022 QCBOREncode_BstrWrap(&EC);
7023 QCBOREncode_OpenMap(&EC);
7024 QCBOREncode_AddInt64ToMapN(&EC, 100, 1);
7025 QCBOREncode_AddInt64ToMapN(&EC, 200, 2);
7026 QCBOREncode_CloseMap(&EC);
7027 QCBOREncode_BstrWrap(&EC);
7028 QCBOREncode_OpenArray(&EC);
7029 QCBOREncode_AddInt64(&EC, 3);
7030 QCBOREncode_BstrWrap(&EC);
7031 QCBOREncode_AddInt64(&EC, 4);
7032 QCBOREncode_CloseBstrWrap(&EC, NULL);
7033 QCBOREncode_AddInt64(&EC, 5);
7034 QCBOREncode_CloseArray(&EC);
7035 QCBOREncode_CloseBstrWrap(&EC, NULL);
7036 QCBOREncode_AddInt64(&EC, 6);
7037 QCBOREncode_CloseBstrWrap(&EC, NULL);
7038 QCBOREncode_OpenArray(&EC);
7039 QCBOREncode_AddInt64(&EC, 7);
7040 QCBOREncode_AddInt64(&EC, 8);
7041 QCBOREncode_CloseArray(&EC);
7042
7043 uErr = QCBOREncode_Finish(&EC, &Encoded);
Laurence Lundblade40a04322020-06-27 22:52:52 -07007044 if(uErr) {
7045 Encoded = NULLUsefulBufC;
7046 }
Laurence Lundblade0750fc42020-06-20 21:02:34 -07007047
7048 return Encoded;
7049}
7050
Laurence Lundbladecc7da412020-12-27 00:09:07 -08007051/* h'FF' */
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08007052static const uint8_t spBreakInByteString[] = {
7053 0x41, 0xff
7054};
7055
Laurence Lundblade0750fc42020-06-20 21:02:34 -07007056
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03007057int32_t EnterBstrTest(void)
Laurence Lundblade0750fc42020-06-20 21:02:34 -07007058{
Laurence Lundblade8510f8c2020-12-01 11:31:16 -08007059 UsefulBuf_MAKE_STACK_UB(OutputBuffer, 100);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07007060
7061 QCBORDecodeContext DC;
7062
Laurence Lundblade55013642020-09-23 05:39:22 -07007063 QCBORDecode_Init(&DC, EncodeBstrWrapTestData(OutputBuffer), 0);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07007064
Laurence Lundblade55013642020-09-23 05:39:22 -07007065 int64_t n1, n2, n3, n4, n5, n6, n7, n8;
Laurence Lundblade0750fc42020-06-20 21:02:34 -07007066
7067
Laurence Lundblade9b334962020-08-27 10:55:53 -07007068 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07007069 QCBORDecode_EnterMap(&DC, NULL);
Laurence Lundblade55013642020-09-23 05:39:22 -07007070 QCBORDecode_GetInt64InMapN(&DC, 100, &n1);
7071 QCBORDecode_GetInt64InMapN(&DC, 200, &n2);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07007072 QCBORDecode_ExitMap(&DC);
Laurence Lundblade9b334962020-08-27 10:55:53 -07007073 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07007074 QCBORDecode_EnterArray(&DC, NULL);
Laurence Lundblade55013642020-09-23 05:39:22 -07007075 QCBORDecode_GetInt64(&DC, &n3);
Laurence Lundblade9b334962020-08-27 10:55:53 -07007076 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
Laurence Lundblade55013642020-09-23 05:39:22 -07007077 QCBORDecode_GetInt64(&DC, &n4);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07007078 QCBORDecode_ExitBstrWrapped(&DC);
Laurence Lundblade55013642020-09-23 05:39:22 -07007079 QCBORDecode_GetInt64(&DC, &n5);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07007080 QCBORDecode_ExitArray(&DC);
7081 QCBORDecode_ExitBstrWrapped(&DC);
Laurence Lundblade55013642020-09-23 05:39:22 -07007082 QCBORDecode_GetInt64(&DC, &n6);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07007083 QCBORDecode_ExitBstrWrapped(&DC);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07007084 QCBORDecode_EnterArray(&DC, NULL);
Laurence Lundblade55013642020-09-23 05:39:22 -07007085 QCBORDecode_GetInt64(&DC, &n7);
7086 QCBORDecode_GetInt64(&DC, &n8);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07007087 QCBORDecode_ExitArray(&DC);
7088
7089 QCBORError uErr = QCBORDecode_Finish(&DC);
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08007090 if(uErr) {
7091 return (int32_t)uErr;
7092 }
Laurence Lundblade0750fc42020-06-20 21:02:34 -07007093
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08007094
7095 /* Enter and exit byte string wrapped CBOR that is bad. It has just a break.
7096 * Successful because no items are fetched from byte string.
7097 */
7098 QCBORDecode_Init(&DC,
7099 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBreakInByteString),
7100 0);
7101 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
7102 uErr = QCBORDecode_GetError(&DC);
7103 if(uErr) {
7104 return 100 + (int32_t)uErr;
7105 }
7106
7107 QCBORDecode_ExitBstrWrapped(&DC);
7108 uErr = QCBORDecode_GetError(&DC);
7109 if(uErr) {
7110 return 200 + (int32_t)uErr;
7111 }
7112
7113 /* Try to get item that is a break out of a byte string wrapped CBOR.
7114 * It fails because there should be no break.
7115 */
7116 QCBORDecode_Init(&DC,
7117 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBreakInByteString),
7118 0);
7119 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
7120 QCBORItem Item;
7121 uErr = QCBORDecode_GetNext(&DC, &Item);
7122 if(uErr != QCBOR_ERR_BAD_BREAK) {
7123 return 300 + (int32_t)uErr;
7124 }
7125
7126 return 0;
Laurence Lundblade0750fc42020-06-20 21:02:34 -07007127}
Laurence Lundblade37f46e52020-08-04 03:32:14 -07007128
7129
7130
7131
7132static const uint8_t spTaggedTypes[] = {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007133 0xb2,
Laurence Lundblade37f46e52020-08-04 03:32:14 -07007134
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07007135 // Date string
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007136 0x00,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07007137 0xc0, 0x74, 0x32, 0x30, 0x30, 0x33, 0x2D, 0x31, 0x32, 0x2D,
7138 0x31, 0x33, 0x54, 0x31, 0x38, 0x3A, 0x33, 0x30, 0x3A, 0x30,
7139 0x32, 0x5A,
Laurence Lundblade37f46e52020-08-04 03:32:14 -07007140
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007141 0x01,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07007142 0x74, 0x32, 0x30, 0x30, 0x33, 0x2D, 0x31, 0x32, 0x2D, 0x31,
7143 0x33, 0x54, 0x31, 0x38, 0x3A, 0x33, 0x30, 0x3A, 0x30, 0x32,
7144 0x5A,
Laurence Lundblade37f46e52020-08-04 03:32:14 -07007145
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07007146 // Bignum
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007147 10,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07007148 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
7149 0x09, 0x10,
Laurence Lundblade37f46e52020-08-04 03:32:14 -07007150
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007151 11,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07007152 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
7153 0x09, 0x10,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007154
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07007155 // URL
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007156 20,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07007157 0xd8, 0x20, 0x6f, 0x68, 0x74, 0x74, 0x70, 0x3A, 0x2F, 0x2F,
7158 0x63, 0x62, 0x6F, 0x72, 0x2E, 0x6D, 0x65, 0x2F,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007159
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07007160 21,
7161 0x6f, 0x68, 0x74, 0x74, 0x70, 0x3A, 0x2F, 0x2F, 0x63, 0x62,
7162 0x6F, 0x72, 0x2E, 0x6D, 0x65, 0x2F,
7163
7164 // B64
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007165 0x18, 0x1e,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07007166 0xd8, 0x22, 0x6c, 0x63, 0x47, 0x78, 0x6C, 0x59, 0x58, 0x4E,
7167 0x31, 0x63, 0x6D, 0x55, 0x75,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007168
7169 0x18, 0x1f,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07007170 0x6c, 0x63, 0x47, 0x78, 0x6C, 0x59, 0x58, 0x4E, 0x31, 0x63,
7171 0x6D, 0x55, 0x75,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007172
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07007173 // B64URL
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007174 0x18, 0x28,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07007175 0xd8, 0x21, 0x6c, 0x63, 0x47, 0x78, 0x6C, 0x59, 0x58, 0x4E,
7176 0x31, 0x63, 0x6D, 0x55, 0x75,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007177
7178 0x18, 0x29,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07007179 0x6c, 0x63, 0x47, 0x78, 0x6C, 0x59, 0x58, 0x4E, 0x31, 0x63,
7180 0x6D, 0x55, 0x75,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007181
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07007182 // Regex
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007183 0x18, 0x32,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07007184 0xd8, 0x23, 0x68, 0x31, 0x30, 0x30, 0x5C, 0x73, 0x2A, 0x6D,
7185 0x6B,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007186
7187 0x18, 0x33,
7188 0x68, 0x31, 0x30, 0x30, 0x5C, 0x73, 0x2A, 0x6D, 0x6B,
7189
7190 // MIME
7191 0x18, 0x3c,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07007192 0xd8, 0x24, 0x72, 0x4D, 0x49, 0x4D, 0x45, 0x2D, 0x56, 0x65,
7193 0x72, 0x73, 0x69, 0x6F, 0x6E, 0x3A, 0x20, 0x31, 0x2E, 0x30,
7194 0x0A,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007195
7196 0x18, 0x3d,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07007197 0x72, 0x4D, 0x49, 0x4D, 0x45, 0x2D, 0x56, 0x65, 0x72, 0x73,
7198 0x69, 0x6F, 0x6E, 0x3A, 0x20, 0x31, 0x2E, 0x30, 0x0A,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007199
7200 0x18, 0x3e,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07007201 0xd9, 0x01, 0x01, 0x52, 0x4D, 0x49, 0x4D, 0x45, 0x2D, 0x56,
7202 0x65, 0x72, 0x73, 0x69, 0x6F, 0x6E, 0x3A, 0x20, 0x31, 0x2E,
7203 0x30, 0x0A,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007204
7205 0x18, 0x3f,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07007206 0x52, 0x4D, 0x49, 0x4D, 0x45, 0x2D, 0x56, 0x65, 0x72, 0x73,
7207 0x69, 0x6F, 0x6E, 0x3A, 0x20, 0x31, 0x2E, 0x30, 0x0A,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007208
7209 // UUID
7210 0x18, 0x46,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07007211 0xd8, 0x25, 0x50, 0x53, 0x4D, 0x41, 0x52, 0x54, 0x43, 0x53,
7212 0x4C, 0x54, 0x54, 0x43, 0x46, 0x49, 0x43, 0x41, 0x32,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007213
7214 0x18, 0x47,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07007215 0x50, 0x53, 0x4D, 0x41, 0x52, 0x54, 0x43, 0x53, 0x4C, 0x54,
7216 0x54, 0x43, 0x46, 0x49, 0x43, 0x41, 0x32
Laurence Lundblade37f46e52020-08-04 03:32:14 -07007217};
7218
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03007219int32_t DecodeTaggedTypeTests(void)
Laurence Lundblade37f46e52020-08-04 03:32:14 -07007220{
7221 QCBORDecodeContext DC;
7222 QCBORError uErr;
7223
7224 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTaggedTypes), 0);
7225
7226 UsefulBufC String;
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007227 bool bNeg;
Laurence Lundblade37f46e52020-08-04 03:32:14 -07007228
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07007229 QCBORDecode_EnterMap(&DC, NULL);
Laurence Lundblade9b334962020-08-27 10:55:53 -07007230 QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007231 QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &String);
Laurence Lundblade37f46e52020-08-04 03:32:14 -07007232 if(QCBORDecode_GetError(&DC) != QCBOR_SUCCESS) {
7233 return 1;
7234 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07007235 QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade37f46e52020-08-04 03:32:14 -07007236 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_UNEXPECTED_TYPE) {
7237 return 2;
7238 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07007239 QCBORDecode_GetDateStringInMapN(&DC, 1, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade37f46e52020-08-04 03:32:14 -07007240 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_UNEXPECTED_TYPE) {
7241 return 3;
7242 }
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007243 QCBORDecode_GetDateStringInMapN(&DC, 1, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &String);
Laurence Lundblade9b334962020-08-27 10:55:53 -07007244 QCBORDecode_GetDateStringInMapN(&DC, 1, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade37f46e52020-08-04 03:32:14 -07007245 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
7246 return 4;
7247 }
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007248 QCBORDecode_GetDateStringInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07007249 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007250 return 5;
7251 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07007252
Laurence Lundblade9b334962020-08-27 10:55:53 -07007253 QCBORDecode_GetBignumInMapN(&DC, 10, QCBOR_TAG_REQUIREMENT_TAG, &String, &bNeg);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007254 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
7255 bNeg != false) {
7256 return 10;
7257 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07007258 QCBORDecode_GetBignumInMapN(&DC, 11, QCBOR_TAG_REQUIREMENT_TAG, &String, &bNeg);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007259 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
7260 bNeg != true) {
7261 return 11;
7262 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07007263 QCBORDecode_GetBignumInMapN(&DC, 11, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bNeg);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007264 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_UNEXPECTED_TYPE) {
7265 return 12;
7266 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07007267 QCBORDecode_GetBignumInMapN(&DC, 14, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bNeg);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07007268 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007269 return 13;
7270 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07007271 QCBORDecode_GetBignumInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bNeg);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07007272 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007273 return 14;
7274 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07007275
Laurence Lundblade9b334962020-08-27 10:55:53 -07007276 QCBORDecode_GetURIInMapN(&DC, 20, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007277 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
7278 return 20;
7279 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07007280 QCBORDecode_GetURIInMapN(&DC, 21, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007281 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
7282 return 21;
7283 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07007284 QCBORDecode_GetURIInMapN(&DC, 22, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07007285 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007286 return 22;
7287 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07007288 QCBORDecode_GetURIInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07007289 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007290 return 23;
7291 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07007292
Laurence Lundblade24bd7e12021-01-09 00:05:11 -08007293#ifndef QCBOR_DISABLE_UNCOMMON_TAGS
Laurence Lundblade9b334962020-08-27 10:55:53 -07007294 QCBORDecode_GetB64InMapN(&DC, 30, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007295 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
7296 return 30;
7297 }
Laurence Lundblade24bd7e12021-01-09 00:05:11 -08007298#endif
Laurence Lundblade9b334962020-08-27 10:55:53 -07007299 QCBORDecode_GetB64InMapN(&DC, 31, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007300 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
7301 return 31;
7302 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07007303 QCBORDecode_GetB64InMapN(&DC, 32, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07007304 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007305 return 32;
7306 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07007307 QCBORDecode_GetB64InMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07007308 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007309 return 33;
7310 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07007311
Laurence Lundblade24bd7e12021-01-09 00:05:11 -08007312#ifndef QCBOR_DISABLE_UNCOMMON_TAGS
Laurence Lundblade9b334962020-08-27 10:55:53 -07007313 QCBORDecode_GetB64URLInMapN(&DC, 40, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007314 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
7315 return 40;
7316 }
Laurence Lundblade24bd7e12021-01-09 00:05:11 -08007317#endif
Laurence Lundblade9b334962020-08-27 10:55:53 -07007318 QCBORDecode_GetB64URLInMapN(&DC, 41, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007319 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
7320 return 41;
7321 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07007322 QCBORDecode_GetB64URLInMapN(&DC, 42, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07007323 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007324 return 42;
7325 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07007326 QCBORDecode_GetB64URLInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07007327 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007328 return 43;
7329 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07007330
Laurence Lundblade24bd7e12021-01-09 00:05:11 -08007331#ifndef QCBOR_DISABLE_UNCOMMON_TAGS
Laurence Lundblade9b334962020-08-27 10:55:53 -07007332 QCBORDecode_GetRegexInMapN(&DC, 50, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007333 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
7334 return 50;
7335 }
Laurence Lundblade24bd7e12021-01-09 00:05:11 -08007336#endif
Laurence Lundblade9b334962020-08-27 10:55:53 -07007337 QCBORDecode_GetRegexInMapN(&DC, 51, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007338 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
7339 return 51;
7340 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07007341 QCBORDecode_GetRegexInMapN(&DC, 52, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07007342 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007343 return 52;
7344 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07007345 QCBORDecode_GetRegexInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07007346 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007347 return 53;
7348 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07007349
Laurence Lundblade24bd7e12021-01-09 00:05:11 -08007350#ifndef QCBOR_DISABLE_UNCOMMON_TAGS
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007351 // MIME
7352 bool bIsNot7Bit;
Laurence Lundblade9b334962020-08-27 10:55:53 -07007353 QCBORDecode_GetMIMEMessageInMapN(&DC, 60, QCBOR_TAG_REQUIREMENT_TAG, &String, &bIsNot7Bit);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007354 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
7355 bIsNot7Bit == true) {
7356 return 60;
7357 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07007358 QCBORDecode_GetMIMEMessageInMapN(&DC, 61, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bIsNot7Bit);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007359 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
7360 bIsNot7Bit == true) {
7361 return 61;
7362 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07007363 QCBORDecode_GetMIMEMessageInMapN(&DC, 62, QCBOR_TAG_REQUIREMENT_TAG, &String, &bIsNot7Bit);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007364 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
7365 bIsNot7Bit == false) {
7366 return 62;
7367 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07007368 QCBORDecode_GetMIMEMessageInMapN(&DC, 63, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bIsNot7Bit);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007369 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
7370 bIsNot7Bit == false) {
7371 return 63;
7372 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07007373 QCBORDecode_GetMIMEMessageInMapN(&DC, 64, QCBOR_TAG_REQUIREMENT_TAG, &String, &bNeg);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07007374 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007375 return 64;
7376 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07007377 QCBORDecode_GetMIMEMessageInMapSZ(&DC, "zzz", QCBOR_TAG_REQUIREMENT_TAG, &String, &bNeg);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07007378 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007379 return 65;
7380 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07007381
Laurence Lundblade24bd7e12021-01-09 00:05:11 -08007382
Laurence Lundblade9b334962020-08-27 10:55:53 -07007383 QCBORDecode_GetBinaryUUIDInMapN(&DC, 70, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007384 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
7385 return 70;
7386 }
Laurence Lundblade24bd7e12021-01-09 00:05:11 -08007387#endif /* #ifndef QCBOR_DISABLE_UNCOMMON_TAGS */
7388
Laurence Lundblade9b334962020-08-27 10:55:53 -07007389 QCBORDecode_GetBinaryUUIDInMapN(&DC, 71, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007390 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
7391 return 71;
7392 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07007393 QCBORDecode_GetBinaryUUIDInMapN(&DC, 72, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07007394 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007395 return 72;
7396 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07007397 QCBORDecode_GetBinaryUUIDInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07007398 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007399 return 73;
7400 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07007401
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07007402 // Improvement: add some more error test cases
7403
Laurence Lundblade37f46e52020-08-04 03:32:14 -07007404 QCBORDecode_ExitMap(&DC);
7405
7406 uErr = QCBORDecode_Finish(&DC);
7407 if(uErr != QCBOR_SUCCESS) {
7408 return 100;
7409 }
7410
7411 return 0;
7412}
Laurence Lundbladea4308a82020-10-03 18:08:57 -07007413
7414
7415
7416
7417/*
Laurence Lundbladecc7da412020-12-27 00:09:07 -08007418 [
7419 "aaaaaaaaaa",
7420 {}
7421 ]
Laurence Lundbladea4308a82020-10-03 18:08:57 -07007422 */
7423static const uint8_t spTooLarge1[] = {
7424 0x9f,
7425 0x6a, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61,
7426 0xa0,
7427 0xff
7428};
7429
7430/*
Laurence Lundbladecc7da412020-12-27 00:09:07 -08007431 [
7432 {
7433 0: "aaaaaaaaaa"
7434 }
7435 ]
Laurence Lundbladea4308a82020-10-03 18:08:57 -07007436 */
7437static const uint8_t spTooLarge2[] = {
7438 0x9f,
7439 0xa1,
7440 0x00,
7441 0x6a, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61,
7442 0xff
7443};
7444
7445/*
Laurence Lundbladecc7da412020-12-27 00:09:07 -08007446 h'A1006A61616161616161616161'
Laurence Lundbladea4308a82020-10-03 18:08:57 -07007447
Laurence Lundbladecc7da412020-12-27 00:09:07 -08007448 {
7449 0: "aaaaaaaaaa"
7450 }
Laurence Lundbladea4308a82020-10-03 18:08:57 -07007451 */
7452static const uint8_t spTooLarge3[] = {
7453 0x4d,
7454 0xa1,
7455 0x00,
7456 0x6a, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61,
7457};
7458
7459int32_t TooLargeInputTest(void)
7460{
7461 QCBORDecodeContext DC;
7462 QCBORError uErr;
7463 UsefulBufC String;
7464
7465 // These tests require a build with QCBOR_MAX_DECODE_INPUT_SIZE set
7466 // to 10 There's not really any way to test this error
7467 // condition. The error condition is not complex, so setting
7468 // QCBOR_MAX_DECODE_INPUT_SIZE gives an OK test.
7469
7470 // The input CBOR is only too large because the
7471 // QCBOR_MAX_DECODE_INPUT_SIZE is 10.
7472 //
7473 // This test is disabled for the normal test runs because of the
7474 // special build requirement.
7475
7476
7477 // Tests the start of a map being too large
7478 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTooLarge1), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07007479 QCBORDecode_EnterArray(&DC, NULL);
Laurence Lundbladea4308a82020-10-03 18:08:57 -07007480 QCBORDecode_GetTextString(&DC, &String);
7481 uErr = QCBORDecode_GetError(&DC);
7482 if(uErr != QCBOR_SUCCESS) {
7483 return 1;
7484 }
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07007485 QCBORDecode_EnterMap(&DC, NULL);
Laurence Lundbladea4308a82020-10-03 18:08:57 -07007486 uErr = QCBORDecode_GetError(&DC);
7487 if(uErr != QCBOR_ERR_INPUT_TOO_LARGE) {
7488 return 2;
7489 }
7490
7491 // Tests the end of a map being too large
7492 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTooLarge2), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07007493 QCBORDecode_EnterArray(&DC, NULL);
7494 QCBORDecode_EnterMap(&DC, NULL);
Laurence Lundbladea4308a82020-10-03 18:08:57 -07007495 uErr = QCBORDecode_GetError(&DC);
7496 if(uErr != QCBOR_SUCCESS) {
7497 return 3;
7498 }
7499 QCBORDecode_ExitMap(&DC);
7500 uErr = QCBORDecode_GetError(&DC);
7501 if(uErr != QCBOR_ERR_INPUT_TOO_LARGE) {
7502 return 4;
7503 }
7504
7505 // Tests the entire input CBOR being too large when processing bstr wrapping
7506 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTooLarge3), QCBOR_DECODE_MODE_NORMAL);
7507 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
7508 uErr = QCBORDecode_GetError(&DC);
7509 if(uErr != QCBOR_ERR_INPUT_TOO_LARGE) {
7510 return 5;
7511 }
7512
7513 return 0;
7514}
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07007515
7516
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08007517#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
7518
Laurence Lundblade37286c02022-09-03 10:05:02 -07007519/*
7520 An array of three map entries
7521 1) Indefinite length string label for indefinite lenght byte string
7522 2) Indefinite length string label for an integer
7523 3) Indefinite length string label for an indefinite-length negative big num
7524 */
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07007525static const uint8_t spMapWithIndefLenStrings[] = {
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08007526 0xa3,
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07007527 0x7f, 0x61, 'l', 0x64, 'a', 'b', 'e', 'l' , 0x61, '1', 0xff,
7528 0x5f, 0x42, 0x01, 0x02, 0x43, 0x03, 0x04, 0x05, 0xff,
7529 0x7f, 0x62, 'd', 'y', 0x61, 'm', 0x61, 'o', 0xff,
7530 0x03,
7531 0x7f, 0x62, 'l', 'a', 0x63, 'b', 'e', 'l', 0x61, '2', 0xff,
7532 0xc3,
7533 0x5f, 0x42, 0x00, 0x01, 0x42, 0x00, 0x01, 0x41, 0x01, 0xff,
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07007534};
7535
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03007536int32_t SpiffyIndefiniteLengthStringsTests(void)
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07007537{
7538 QCBORDecodeContext DCtx;
7539
7540 QCBORDecode_Init(&DCtx,
7541 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spMapWithIndefLenStrings),
7542 QCBOR_DECODE_MODE_NORMAL);
7543
Laurence Lundblade8510f8c2020-12-01 11:31:16 -08007544 UsefulBuf_MAKE_STACK_UB(StringBuf, 200);
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07007545 QCBORDecode_SetMemPool(&DCtx, StringBuf, false);
7546
7547 UsefulBufC ByteString;
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07007548 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07007549 QCBORDecode_GetByteStringInMapSZ(&DCtx, "label1", &ByteString);
Laurence Lundblade37286c02022-09-03 10:05:02 -07007550
7551#ifndef QCBOR_DISABLE_TAGS
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07007552 if(QCBORDecode_GetAndResetError(&DCtx)) {
7553 return 1;
7554 }
7555
7556 const uint8_t pExectedBytes[] = {0x01, 0x02, 0x03, 0x04, 0x05};
7557 if(UsefulBuf_Compare(ByteString, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pExectedBytes))) {
7558 return 2;
7559 }
7560
7561 uint64_t uInt;
7562 QCBORDecode_GetUInt64InMapSZ(&DCtx, "dymo", &uInt);
7563 if(QCBORDecode_GetAndResetError(&DCtx)) {
7564 return 3;
7565 }
7566 if(uInt != 3) {
7567 return 4;
7568 }
7569
Máté Tóth-Pálef5f07a2021-09-17 19:31:37 +02007570#ifndef USEFULBUF_DISABLE_ALL_FLOAT
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07007571 double uDouble;
7572 QCBORDecode_GetDoubleConvertAllInMapSZ(&DCtx,
7573 "label2",
7574 0xff,
7575 &uDouble);
Laurence Lundblade37286c02022-09-03 10:05:02 -07007576
Laurence Lundbladeb8e19aa2020-10-07 20:59:11 -07007577#ifndef QCBOR_DISABLE_FLOAT_HW_USE
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07007578 if(QCBORDecode_GetAndResetError(&DCtx)) {
7579 return 5;
7580 }
7581 if(uDouble != -16777474) {
7582 return 6;
7583 }
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08007584#else /* QCBOR_DISABLE_FLOAT_HW_USE */
Laurence Lundbladeb8e19aa2020-10-07 20:59:11 -07007585 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_HW_FLOAT_DISABLED) {
7586 return 7;
7587 }
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08007588#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
Máté Tóth-Pálef5f07a2021-09-17 19:31:37 +02007589#endif /* USEFULBUF_DISABLE_ALL_FLOAT */
Laurence Lundbladeb8e19aa2020-10-07 20:59:11 -07007590
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07007591 QCBORDecode_ExitMap(&DCtx);
7592
7593 if(QCBORDecode_Finish(&DCtx)) {
7594 return 99;
7595 }
7596
Laurence Lundblade37286c02022-09-03 10:05:02 -07007597#else /* QCBOR_DISABLE_TAGS */
7598 /* The big num in the input is a CBOR tag and you can't do
7599 * map lookups in a map with a tag so this test does very little
7600 * when tags are disabled. That is OK, the test coverage is still
7601 * good when they are not.
7602 */
7603 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_TAGS_DISABLED) {
7604 return 1002;
7605 }
7606#endif /*QCBOR_DISABLE_TAGS */
7607
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07007608 return 0;
7609}
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08007610#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
Laurence Lundblade2a26abb2020-11-05 19:06:54 -08007611
7612
Laurence Lundbladecf41c522021-02-20 10:19:07 -07007613/*
7614 * An array of an integer and an array. The second array contains
7615 * a bstr-wrapped map.
7616 *
7617 * [7, [h'A36D6669... (see next lines) 73']]
7618 *
7619 * {"first integer": 42,
7620 * "an array of two strings": ["string1", "string2"],
7621 * "map in a map":
7622 * { "bytes 1": h'78787878',
7623 * "bytes 2": h'79797979',
7624 * "another int": 98,
7625 * "text 2": "lies, damn lies and statistics"
7626 * }
7627 * }
7628 */
Laurence Lundblade2a26abb2020-11-05 19:06:54 -08007629
Laurence Lundbladecf41c522021-02-20 10:19:07 -07007630static const uint8_t pValidWrappedMapEncoded[] = {
7631 0x82, 0x07, 0x81, 0x58, 0x97,
7632 0xa3, 0x6d, 0x66, 0x69, 0x72, 0x73, 0x74, 0x20, 0x69, 0x6e,
7633 0x74, 0x65, 0x67, 0x65, 0x72, 0x18, 0x2a, 0x77, 0x61, 0x6e,
7634 0x20, 0x61, 0x72, 0x72, 0x61, 0x79, 0x20, 0x6f, 0x66, 0x20,
7635 0x74, 0x77, 0x6f, 0x20, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67,
7636 0x73, 0x82, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x31,
7637 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x32, 0x6c, 0x6d,
7638 0x61, 0x70, 0x20, 0x69, 0x6e, 0x20, 0x61, 0x20, 0x6d, 0x61,
7639 0x70, 0xa4, 0x67, 0x62, 0x79, 0x74, 0x65, 0x73, 0x20, 0x31,
7640 0x44, 0x78, 0x78, 0x78, 0x78, 0x67, 0x62, 0x79, 0x74, 0x65,
7641 0x73, 0x20, 0x32, 0x44, 0x79, 0x79, 0x79, 0x79, 0x6b, 0x61,
7642 0x6e, 0x6f, 0x74, 0x68, 0x65, 0x72, 0x20, 0x69, 0x6e, 0x74,
7643 0x18, 0x62, 0x66, 0x74, 0x65, 0x78, 0x74, 0x20, 0x32, 0x78,
7644 0x1e, 0x6c, 0x69, 0x65, 0x73, 0x2c, 0x20, 0x64, 0x61, 0x6d,
7645 0x6e, 0x20, 0x6c, 0x69, 0x65, 0x73, 0x20, 0x61, 0x6e, 0x64,
7646 0x20, 0x73, 0x74, 0x61, 0x74, 0x69, 0x73, 0x74, 0x69, 0x63,
7647 0x73
7648};
7649
7650#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
7651
7652/* As above, but the arrays are indefinite length */
7653static const uint8_t pValidIndefWrappedMapEncoded[] = {
7654 0x9f, 0x07, 0x9f, 0x58, 0x97,
7655 0xa3, 0x6d, 0x66, 0x69, 0x72, 0x73, 0x74, 0x20, 0x69, 0x6e,
7656 0x74, 0x65, 0x67, 0x65, 0x72, 0x18, 0x2a, 0x77, 0x61, 0x6e,
7657 0x20, 0x61, 0x72, 0x72, 0x61, 0x79, 0x20, 0x6f, 0x66, 0x20,
7658 0x74, 0x77, 0x6f, 0x20, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67,
7659 0x73, 0x82, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x31,
7660 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x32, 0x6c, 0x6d,
7661 0x61, 0x70, 0x20, 0x69, 0x6e, 0x20, 0x61, 0x20, 0x6d, 0x61,
7662 0x70, 0xa4, 0x67, 0x62, 0x79, 0x74, 0x65, 0x73, 0x20, 0x31,
7663 0x44, 0x78, 0x78, 0x78, 0x78, 0x67, 0x62, 0x79, 0x74, 0x65,
7664 0x73, 0x20, 0x32, 0x44, 0x79, 0x79, 0x79, 0x79, 0x6b, 0x61,
7665 0x6e, 0x6f, 0x74, 0x68, 0x65, 0x72, 0x20, 0x69, 0x6e, 0x74,
7666 0x18, 0x62, 0x66, 0x74, 0x65, 0x78, 0x74, 0x20, 0x32, 0x78,
7667 0x1e, 0x6c, 0x69, 0x65, 0x73, 0x2c, 0x20, 0x64, 0x61, 0x6d,
7668 0x6e, 0x20, 0x6c, 0x69, 0x65, 0x73, 0x20, 0x61, 0x6e, 0x64,
7669 0x20, 0x73, 0x74, 0x61, 0x74, 0x69, 0x73, 0x74, 0x69, 0x63,
7670 0x73,
7671 0xff, 0xff
7672};
7673#endif
7674
7675
7676static const uint8_t pWithEmptyMap[] = {0x82, 0x18, 0x64, 0xa0};
7677
7678#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
7679static const uint8_t pWithEmptyMapInDef[] = {0x9f, 0x18, 0x64, 0xbf, 0xff, 0xff};
7680#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
7681
7682#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
Laurence Lundblade37286c02022-09-03 10:05:02 -07007683
7684/*
7685An array of one that contains
7686 a byte string that is tagged 24 which means CBOR-encoded data
7687 the byte string is an indefinite length string
7688 the wrapped byte string is an array of three numbers
7689 [42, 43, 44]
7690
7691[
7692 24(
7693 (_ h'83', h'18', h'2A182B', h'182C')
7694 )
7695]
7696 */
Laurence Lundbladecf41c522021-02-20 10:19:07 -07007697static const uint8_t pWrappedByIndefiniteLength[] = {
7698 0x81,
7699 0xd8, 0x18,
7700 0x5f,
7701 0x41, 0x83,
7702 0x41, 0x18,
7703 0x43, 0x2A, 0x18, 0x2B,
7704 0x42, 0x18, 0x2C,
7705 0xff
7706};
7707#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
7708
7709
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03007710int32_t PeekAndRewindTest(void)
Laurence Lundblade2a26abb2020-11-05 19:06:54 -08007711{
7712 QCBORItem Item;
7713 QCBORError nCBORError;
7714 QCBORDecodeContext DCtx;
7715
7716 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0);
7717
7718 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
7719 return 100+(int32_t)nCBORError;
7720 }
7721 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
7722 return 200;
7723 }
7724
Laurence Lundblade3427dee2021-06-20 11:11:24 -07007725 QCBORDecode_VPeekNext(&DCtx, &Item);
7726 if((nCBORError = QCBORDecode_GetError(&DCtx))) {
7727 return 150+(int32_t)nCBORError;
7728 }
7729 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
7730 return 250;
7731 }
7732
Laurence Lundblade2a26abb2020-11-05 19:06:54 -08007733 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
7734 return (int32_t)nCBORError;
7735 }
7736 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
7737 return 300;
7738 }
7739
7740 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
7741 return 400 + (int32_t)nCBORError;
7742 }
7743 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
7744 return 500;
7745 }
7746
7747 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7748 return (int32_t)nCBORError;
7749 }
7750 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
7751 return 600;
7752 }
7753
7754 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
7755 return 900 + (int32_t)nCBORError;
7756 }
7757 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
7758 Item.uDataType != QCBOR_TYPE_INT64 ||
7759 Item.val.int64 != 42 ||
7760 Item.uDataAlloc ||
7761 Item.uLabelAlloc ||
7762 UsefulBufCompareToSZ(Item.label.string, "first integer")) {
7763 return 1000;
7764 }
7765
7766 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7767 return 1100 + (int32_t)nCBORError;
7768 }
7769
7770 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
7771 Item.uDataType != QCBOR_TYPE_INT64 ||
7772 Item.val.int64 != 42 ||
7773 Item.uDataAlloc ||
7774 Item.uLabelAlloc ||
7775 UsefulBufCompareToSZ(Item.label.string, "first integer")) {
7776 return 1200;
7777 }
7778
7779
7780 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7781 return 1300 + (int32_t)nCBORError;
7782 }
7783 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
7784 Item.uDataAlloc ||
7785 Item.uLabelAlloc ||
7786 UsefulBufCompareToSZ(Item.label.string, "an array of two strings") ||
7787 Item.uDataType != QCBOR_TYPE_ARRAY ||
Laurence Lundbladecf41c522021-02-20 10:19:07 -07007788 Item.val.uCount != 2) {
Laurence Lundblade2a26abb2020-11-05 19:06:54 -08007789 return 1400;
Laurence Lundbladecf41c522021-02-20 10:19:07 -07007790 }
Laurence Lundblade2a26abb2020-11-05 19:06:54 -08007791
7792 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7793 return 1500 + (int32_t)nCBORError;
7794 }
7795 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
7796 Item.uDataAlloc ||
7797 Item.uLabelAlloc ||
7798 UsefulBufCompareToSZ(Item.val.string, "string1")) {
7799 return 1600;
7800 }
7801
7802 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
7803 return 1700 + (int32_t)nCBORError;
7804 }
7805 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
7806 Item.uDataAlloc ||
7807 Item.uLabelAlloc ||
7808 UsefulBufCompareToSZ(Item.val.string, "string2")) {
7809 return 1800;
7810 }
7811
7812 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
7813 return (int32_t)nCBORError;
7814 }
7815 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
7816 Item.uDataAlloc ||
7817 Item.uLabelAlloc ||
7818 UsefulBufCompareToSZ(Item.val.string, "string2")) {
7819 return 1900;
7820 }
7821
7822 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7823 return (int32_t)nCBORError;
7824 }
7825 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
7826 Item.uDataAlloc ||
7827 Item.uLabelAlloc ||
7828 UsefulBufCompareToSZ(Item.val.string, "string2")) {
7829 return 2000;
7830 }
7831
7832
7833 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7834 return 2100 + (int32_t)nCBORError;
7835 }
7836 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
7837 Item.uDataAlloc ||
7838 Item.uLabelAlloc ||
7839 UsefulBufCompareToSZ(Item.label.string, "map in a map") ||
7840 Item.uDataType != QCBOR_TYPE_MAP ||
7841 Item.val.uCount != 4) {
7842 return 2100;
7843 }
7844
7845 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7846 return 2200 + (int32_t)nCBORError;
7847 }
7848 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
7849 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("bytes 1"))||
7850 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
7851 Item.uDataAlloc ||
7852 Item.uLabelAlloc ||
7853 UsefulBufCompareToSZ(Item.val.string, "xxxx")) {
7854 return 2300;
7855 }
7856
7857 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
7858 return 2400 + (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 2500;
7867 }
7868
7869 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7870 return 2600 + (int32_t)nCBORError;
7871 }
7872 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
7873 UsefulBufCompareToSZ(Item.label.string, "bytes 2") ||
7874 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
7875 Item.uDataAlloc ||
7876 Item.uLabelAlloc ||
7877 UsefulBufCompareToSZ(Item.val.string, "yyyy")) {
7878 return 2700;
7879 }
7880
7881 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7882 return 2800 + (int32_t)nCBORError;
7883 }
7884 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
7885 Item.uDataAlloc ||
7886 Item.uLabelAlloc ||
7887 UsefulBufCompareToSZ(Item.label.string, "another int") ||
7888 Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundbladecf41c522021-02-20 10:19:07 -07007889 Item.val.int64 != 98) {
Laurence Lundblade2a26abb2020-11-05 19:06:54 -08007890 return 2900;
Laurence Lundbladecf41c522021-02-20 10:19:07 -07007891 }
Laurence Lundblade2a26abb2020-11-05 19:06:54 -08007892
7893 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
7894 return 3000 + (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 3100;
7903 }
7904
7905 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7906 return 3200 + (int32_t)nCBORError;
7907 }
7908 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
7909 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("text 2"))||
7910 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
7911 Item.uDataAlloc ||
7912 Item.uLabelAlloc ||
7913 UsefulBufCompareToSZ(Item.val.string, "lies, damn lies and statistics")) {
7914 return 3300;
7915 }
7916
Laurence Lundblade3427dee2021-06-20 11:11:24 -07007917 nCBORError = QCBORDecode_PeekNext(&DCtx, &Item);
7918 if(nCBORError != QCBOR_ERR_NO_MORE_ITEMS) {
7919 return 3300 + (int32_t)nCBORError;
7920 }
7921
7922 QCBORDecode_VPeekNext(&DCtx, &Item);
7923 nCBORError = QCBORDecode_GetError(&DCtx);
7924 if(nCBORError != QCBOR_ERR_NO_MORE_ITEMS) {
7925 return 3400 + (int32_t)nCBORError;
7926 }
7927
7928 QCBORDecode_VPeekNext(&DCtx, &Item);
7929 nCBORError = QCBORDecode_GetError(&DCtx);
7930 if(nCBORError != QCBOR_ERR_NO_MORE_ITEMS) {
7931 return 3500 + (int32_t)nCBORError;
7932 }
Laurence Lundbladecf41c522021-02-20 10:19:07 -07007933
7934
7935 // Rewind to top level after entering several maps
7936 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0);
7937
7938 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7939 return (int32_t)nCBORError;
7940 }
7941 if(Item.uDataType != QCBOR_TYPE_MAP ||
7942 Item.val.uCount != 3) {
7943 return 400;
7944 }
7945
7946 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7947 return 4000+(int32_t)nCBORError;
7948 }
7949
7950 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
7951 Item.uDataType != QCBOR_TYPE_INT64 ||
7952 Item.val.int64 != 42 ||
7953 Item.uDataAlloc ||
7954 Item.uLabelAlloc ||
7955 UsefulBufCompareToSZ(Item.label.string, "first integer")) {
7956 return 4100;
7957 }
7958
7959 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7960 return 4100+(int32_t)nCBORError;
7961 }
7962 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
7963 Item.uDataAlloc ||
7964 Item.uLabelAlloc ||
7965 UsefulBufCompareToSZ(Item.label.string, "an array of two strings") ||
7966 Item.uDataType != QCBOR_TYPE_ARRAY ||
7967 Item.val.uCount != 2) {
7968 return 4200;
7969 }
7970
7971 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7972 return 4200+(int32_t)nCBORError;
7973 }
7974 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
7975 Item.uDataAlloc ||
7976 Item.uLabelAlloc ||
7977 UsefulBufCompareToSZ(Item.val.string, "string1")) {
7978 return 4300;
7979 }
7980
7981 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7982 return 4300+(int32_t)nCBORError;
7983 }
7984 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
7985 Item.uDataAlloc ||
7986 Item.uLabelAlloc ||
7987 UsefulBufCompareToSZ(Item.val.string, "string2")) {
7988 return 4400;
7989 }
7990
7991 QCBORDecode_Rewind(&DCtx);
7992
7993 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7994 return 4400+(int32_t)nCBORError;
7995 }
7996 if(Item.uDataType != QCBOR_TYPE_MAP ||
7997 Item.val.uCount != 3) {
7998 return 4500;
7999 }
8000
8001 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8002 return (int32_t)nCBORError;
8003 }
8004
8005 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
8006 Item.uDataType != QCBOR_TYPE_INT64 ||
8007 Item.val.int64 != 42 ||
8008 Item.uDataAlloc ||
8009 Item.uLabelAlloc ||
8010 UsefulBufCompareToSZ(Item.label.string, "first integer")) {
8011 return 4600;
8012 }
8013
8014 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8015 return (int32_t)nCBORError;
8016 }
8017 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
8018 Item.uDataAlloc ||
8019 Item.uLabelAlloc ||
8020 UsefulBufCompareToSZ(Item.label.string, "an array of two strings") ||
8021 Item.uDataType != QCBOR_TYPE_ARRAY ||
8022 Item.val.uCount != 2) {
8023 return 4700;
8024 }
8025
8026 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8027 return (int32_t)nCBORError;
8028 }
8029 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
8030 Item.uDataAlloc ||
8031 Item.uLabelAlloc ||
8032 UsefulBufCompareToSZ(Item.val.string, "string1")) {
8033 return 4800;
8034 }
8035
8036 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8037 return 4900+(int32_t)nCBORError;
8038 }
8039 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
8040 Item.uDataAlloc ||
8041 Item.uLabelAlloc ||
8042 UsefulBufCompareToSZ(Item.val.string, "string2")) {
8043 return 5000;
8044 }
8045
8046
8047 // Rewind an entered map
8048 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0);
8049
8050 QCBORDecode_EnterMap(&DCtx, NULL);
8051
8052 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8053 return 5100+(int32_t)nCBORError;
8054 }
8055
8056 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
8057 Item.uDataType != QCBOR_TYPE_INT64 ||
8058 Item.val.int64 != 42 ||
8059 Item.uDataAlloc ||
8060 Item.uLabelAlloc ||
8061 UsefulBufCompareToSZ(Item.label.string, "first integer")) {
8062 return 5200;
8063 }
8064
8065 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8066 return 5200+(int32_t)nCBORError;
8067 }
8068 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
8069 Item.uDataAlloc ||
8070 Item.uLabelAlloc ||
8071 UsefulBufCompareToSZ(Item.label.string, "an array of two strings") ||
8072 Item.uDataType != QCBOR_TYPE_ARRAY ||
8073 Item.val.uCount != 2) {
8074 return -5300;
8075 }
8076
8077 QCBORDecode_Rewind(&DCtx);
8078
8079 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8080 return 5300+(int32_t)nCBORError;
8081 }
8082
8083 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
8084 Item.uDataType != QCBOR_TYPE_INT64 ||
8085 Item.val.int64 != 42 ||
8086 Item.uDataAlloc ||
8087 Item.uLabelAlloc ||
8088 UsefulBufCompareToSZ(Item.label.string, "first integer")) {
8089 return 5400;
8090 }
8091
8092 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8093 return 5400+(int32_t)nCBORError;
8094 }
8095 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
8096 Item.uDataAlloc ||
8097 Item.uLabelAlloc ||
8098 UsefulBufCompareToSZ(Item.label.string, "an array of two strings") ||
8099 Item.uDataType != QCBOR_TYPE_ARRAY ||
8100 Item.val.uCount != 2) {
8101 return 5500;
8102 }
8103
8104
8105 // Rewind and entered array inside an entered map
8106 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0);
8107
8108 QCBORDecode_EnterMap(&DCtx, NULL);
8109
8110 QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings");
8111
8112 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8113 return 5600+(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 5700;
8120 }
8121
8122 QCBORDecode_Rewind(&DCtx);
8123
8124 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8125 return 5700+(int32_t)nCBORError;
8126 }
8127 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
8128 Item.uDataAlloc ||
8129 Item.uLabelAlloc ||
8130 UsefulBufCompareToSZ(Item.val.string, "string1")) {
8131 return 5800;
8132 }
8133
8134 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8135 return (int32_t)nCBORError;
8136 }
8137 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
8138 Item.uDataAlloc ||
8139 Item.uLabelAlloc ||
8140 UsefulBufCompareToSZ(Item.val.string, "string2")) {
8141 return 5900;
8142 }
8143
8144 QCBORDecode_Rewind(&DCtx);
8145
8146 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8147 return 5900+(int32_t)nCBORError;
8148 }
8149 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
8150 Item.uDataAlloc ||
8151 Item.uLabelAlloc ||
8152 UsefulBufCompareToSZ(Item.val.string, "string1")) {
8153 return 6000;
8154 }
8155
8156
8157 // Rewind a byte string inside an array inside an array
8158 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidWrappedMapEncoded), 0);
8159
8160 QCBORDecode_EnterArray(&DCtx, NULL);
8161
8162 uint64_t i;
8163 QCBORDecode_GetUInt64(&DCtx, &i);
8164
8165 QCBORDecode_EnterArray(&DCtx, NULL);
8166
8167 QCBORDecode_EnterBstrWrapped(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
8168 if(QCBORDecode_GetError(&DCtx)) {
8169 return 6100;
8170 }
8171
8172 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8173 return (int32_t)nCBORError;
8174 }
8175 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
8176 return 6200;
8177 }
8178
8179 QCBORDecode_Rewind(&DCtx);
8180
8181 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8182 return 6300+(int32_t)nCBORError;
8183 }
8184 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
8185 return 6400;
8186 }
8187
8188#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
8189 // Rewind a byte string inside an indefinite-length array inside
8190 // indefinite-length array
8191
8192 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidIndefWrappedMapEncoded), 0);
8193
8194 QCBORDecode_EnterArray(&DCtx, NULL);
8195
8196 QCBORDecode_GetUInt64(&DCtx, &i);
8197
8198 QCBORDecode_EnterArray(&DCtx, NULL);
8199
8200 QCBORDecode_EnterBstrWrapped(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
8201 if(QCBORDecode_GetError(&DCtx)) {
8202 return 6500;
8203 }
8204
8205 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8206 return 6600+(int32_t)nCBORError;
8207 }
8208 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
8209 return 6700;
8210 }
8211
8212 QCBORDecode_Rewind(&DCtx);
8213
8214 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8215 return 6800+(int32_t)nCBORError;
8216 }
8217 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
8218 return 6900;
8219 }
8220#endif
8221
8222 // Rewind an empty map
8223 // [100, {}]
8224 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pWithEmptyMap), 0);
8225 QCBORDecode_EnterArray(&DCtx, NULL);
8226 QCBORDecode_GetUInt64(&DCtx, &i);
8227 if(i != 100) {
8228 return 7010;
8229 }
8230 QCBORDecode_EnterMap(&DCtx, NULL);
8231
8232 /* Do it 5 times to be sure multiple rewinds work */
8233 for(int n = 0; n < 5; n++) {
8234 nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
8235 if(nCBORError != QCBOR_ERR_NO_MORE_ITEMS) {
8236 return 7000 + n;
8237 }
8238 QCBORDecode_Rewind(&DCtx);
8239 }
8240 QCBORDecode_ExitMap(&DCtx);
8241 QCBORDecode_Rewind(&DCtx);
8242 QCBORDecode_GetUInt64(&DCtx, &i);
8243 if(i != 100) {
8244 return 7010;
8245 }
8246 QCBORDecode_ExitArray(&DCtx);
8247 QCBORDecode_Rewind(&DCtx);
8248 QCBORDecode_EnterArray(&DCtx, NULL);
8249 i = 9;
8250 QCBORDecode_GetUInt64(&DCtx, &i);
8251 if(i != 100) {
8252 return 7020;
8253 }
8254 if(QCBORDecode_GetError(&DCtx)){
8255 return 7030;
8256 }
8257
8258 // Rewind an empty indefinite length map
8259#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
8260 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pWithEmptyMapInDef), 0);
8261 QCBORDecode_EnterArray(&DCtx, NULL);
8262 QCBORDecode_GetUInt64(&DCtx, &i);
8263 if(i != 100) {
8264 return 7810;
8265 }
8266 QCBORDecode_EnterMap(&DCtx, NULL);
8267
8268 /* Do it 5 times to be sure multiple rewinds work */
8269 for(int n = 0; n < 5; n++) {
8270 nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
8271 if(nCBORError != QCBOR_ERR_NO_MORE_ITEMS) {
8272 return 7800 + n;
8273 }
8274 QCBORDecode_Rewind(&DCtx);
8275 }
8276 QCBORDecode_ExitMap(&DCtx);
8277 QCBORDecode_Rewind(&DCtx);
8278 QCBORDecode_GetUInt64(&DCtx, &i);
8279 if(i != 100) {
8280 return 7810;
8281 }
8282 QCBORDecode_ExitArray(&DCtx);
8283 QCBORDecode_Rewind(&DCtx);
8284 QCBORDecode_EnterArray(&DCtx, NULL);
8285 i = 9;
8286 QCBORDecode_GetUInt64(&DCtx, &i);
8287 if(i != 100) {
8288 return 7820;
8289 }
8290 if(QCBORDecode_GetError(&DCtx)){
8291 return 7830;
8292 }
8293#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
8294
8295 // Rewind an indefnite length byte-string wrapped sequence
8296#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
Laurence Lundblade37286c02022-09-03 10:05:02 -07008297 // TODO: rewrite this test to not use tags
Laurence Lundbladecf41c522021-02-20 10:19:07 -07008298 QCBORDecode_Init(&DCtx,
8299 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pWrappedByIndefiniteLength),
8300 0);
8301 UsefulBuf_MAKE_STACK_UB(Pool, 100);
8302 QCBORDecode_SetMemPool(&DCtx, Pool, 0);
8303
8304 QCBORDecode_EnterArray(&DCtx, NULL);
8305 QCBORDecode_EnterBstrWrapped(&DCtx, 2, NULL);
Laurence Lundblade37286c02022-09-03 10:05:02 -07008306#ifndef QCBOR_DISABLE_TAGS
Laurence Lundbladecf41c522021-02-20 10:19:07 -07008307 if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_INPUT_TOO_LARGE) {
Laurence Lundblade37286c02022-09-03 10:05:02 -07008308 /* TODO: This is what happens when trying to enter
8309 * indefinite-length byte string wrapped CBOR. Tolerate for
8310 * now. Eventually it needs to be fixed so this works, but that
8311 * is not simple.
8312 */
Laurence Lundbladecf41c522021-02-20 10:19:07 -07008313 return 7300;
8314 }
8315
8316 /*
8317 QCBORDecode_GetUInt64(&DCtx, &i);
8318 if(i != 42) {
8319 return 7110;
8320 }
8321 QCBORDecode_Rewind(&DCtx);
8322 QCBORDecode_GetUInt64(&DCtx, &i);
8323 if(i != 42) {
8324 return 7220;
8325 }*/
Laurence Lundblade37286c02022-09-03 10:05:02 -07008326
8327#else /* QCBOR_DISABLE_TAGS */
8328 if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_TAGS_DISABLED) {
8329 return 7301;
8330 }
8331#endif /* QCBOR_DISABLE_TAGS */
8332
Laurence Lundbladecf41c522021-02-20 10:19:07 -07008333#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
8334
8335
8336 // Rewind an indefnite length byte-string wrapped sequence
8337
Laurence Lundblade2a26abb2020-11-05 19:06:54 -08008338 return 0;
8339}
Laurence Lundblade9f9c3732021-03-23 09:38:46 -07008340
8341
8342
8343
8344static const uint8_t spBooleansInMap[] =
8345{
8346 0xa1, 0x08, 0xf5
8347};
8348
8349static const uint8_t spBooleansInMapWrongType[] =
8350{
8351 0xa1, 0x08, 0xf6
8352};
8353
8354static const uint8_t spBooleansInMapNWF[] =
8355{
8356 0xa1, 0x08, 0x1a
8357};
8358
Laurence Lundblade8782dd32021-04-27 04:15:37 -07008359static const uint8_t spNullInMap[] =
8360{
8361 0xa1, 0x08, 0xf6
8362};
8363
8364static const uint8_t spUndefinedInMap[] =
8365{
8366 0xa1, 0x08, 0xf7
8367};
8368
Laurence Lundblade9f9c3732021-03-23 09:38:46 -07008369
8370int32_t BoolTest(void)
8371{
8372 QCBORDecodeContext DCtx;
8373 bool b;
8374
Laurence Lundblade8782dd32021-04-27 04:15:37 -07008375 QCBORDecode_Init(&DCtx,
8376 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMap),
8377 0);
Laurence Lundblade9f9c3732021-03-23 09:38:46 -07008378 QCBORDecode_EnterMap(&DCtx, NULL);
8379 QCBORDecode_GetBool(&DCtx, &b);
8380 if(QCBORDecode_GetAndResetError(&DCtx) || !b) {
8381 return 1;
8382 }
8383
8384 QCBORDecode_GetBoolInMapN(&DCtx, 7, &b);
8385 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_LABEL_NOT_FOUND) {
8386 return 2;
8387 }
8388
8389 QCBORDecode_GetBoolInMapN(&DCtx, 8, &b);
8390 if(QCBORDecode_GetAndResetError(&DCtx) || !b) {
8391 return 3;
8392 }
8393
8394
8395 QCBORDecode_GetBoolInMapSZ(&DCtx, "xx", &b);
8396 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_LABEL_NOT_FOUND) {
8397 return 4;
8398 }
8399
Laurence Lundblade8782dd32021-04-27 04:15:37 -07008400 QCBORDecode_Init(&DCtx,
8401 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMapWrongType),
8402 0);
Laurence Lundblade9f9c3732021-03-23 09:38:46 -07008403 QCBORDecode_EnterMap(&DCtx, NULL);
8404 QCBORDecode_GetBool(&DCtx, &b);
8405 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
8406 return 5;
8407 }
8408
Laurence Lundblade8782dd32021-04-27 04:15:37 -07008409 QCBORDecode_Init(&DCtx,
8410 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMapNWF),
8411 0);
Laurence Lundblade9f9c3732021-03-23 09:38:46 -07008412 QCBORDecode_EnterMap(&DCtx, NULL);
8413 QCBORDecode_GetBool(&DCtx, &b);
8414 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_HIT_END) {
8415 return 6;
8416 }
8417
Laurence Lundblade8782dd32021-04-27 04:15:37 -07008418
8419 QCBORDecode_Init(&DCtx,
8420 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spNullInMap),
8421 0);
8422 QCBORDecode_EnterMap(&DCtx, NULL);
8423 QCBORDecode_GetNull(&DCtx);
8424 if(QCBORDecode_GetAndResetError(&DCtx)) {
8425 return 7;
8426 }
8427
8428 QCBORDecode_Init(&DCtx,
8429 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMap),
8430 0);
8431 QCBORDecode_EnterMap(&DCtx, NULL);
8432 QCBORDecode_GetNull(&DCtx);
8433 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
8434 return 8;
8435 }
8436
8437 QCBORDecode_Init(&DCtx,
8438 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spNullInMap),
8439 0);
8440 QCBORDecode_EnterMap(&DCtx, NULL);
8441 QCBORDecode_GetNullInMapN(&DCtx, 8);
8442 if(QCBORDecode_GetAndResetError(&DCtx)) {
8443 return 9;
8444 }
8445
8446 QCBORDecode_Init(&DCtx,
8447 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMap),
8448 0);
8449 QCBORDecode_EnterMap(&DCtx, NULL);
8450 QCBORDecode_GetNullInMapN(&DCtx, 8);
8451 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
8452 return 10;
8453 }
8454
8455 QCBORDecode_Init(&DCtx,
8456 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMapNWF),
8457 0);
8458 QCBORDecode_EnterMap(&DCtx, NULL);
8459 QCBORDecode_GetUndefined(&DCtx);
8460 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_HIT_END) {
8461 return 11;
8462 }
8463
8464 QCBORDecode_Init(&DCtx,
8465 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUndefinedInMap),
8466 0);
8467 QCBORDecode_EnterMap(&DCtx, NULL);
8468 QCBORDecode_GetUndefined(&DCtx);
8469 if(QCBORDecode_GetAndResetError(&DCtx)) {
8470 return 12;
8471 }
8472
8473 QCBORDecode_Init(&DCtx,
8474 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMap),
8475 0);
8476 QCBORDecode_EnterMap(&DCtx, NULL);
8477 QCBORDecode_GetUndefined(&DCtx);
8478 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
8479 return 13;
8480 }
8481
8482 QCBORDecode_Init(&DCtx,
8483 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUndefinedInMap),
8484 0);
8485 QCBORDecode_EnterMap(&DCtx, NULL);
8486 QCBORDecode_GetUndefinedInMapN(&DCtx, 8);
8487 if(QCBORDecode_GetAndResetError(&DCtx)) {
8488 return 14;
8489 }
8490
8491 QCBORDecode_Init(&DCtx,
8492 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMap),
8493 0);
8494 QCBORDecode_EnterMap(&DCtx, NULL);
8495 QCBORDecode_GetUndefinedInMapN(&DCtx, 8);
8496 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
8497 return 15;
8498 }
8499
8500 QCBORDecode_Init(&DCtx,
8501 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMapNWF),
8502 0);
8503 QCBORDecode_EnterMap(&DCtx, NULL);
8504 QCBORDecode_GetUndefined(&DCtx);
8505 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_HIT_END) {
8506 return 15;
8507 }
8508
Laurence Lundblade9f9c3732021-03-23 09:38:46 -07008509 return 0;
8510}