blob: bc91b721b35b5e63ee04b503bf821f858c9da2f2 [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 Lundblade3e0b2302023-12-04 14:02:38 -07001014 for(i = 0; i < QCBOR_MAX_ARRAY_NESTING; 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 Lundbladeba587682024-02-07 16:46:43 -08002452 UsefulBufCompareToSZ(Item.val.string, "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
Laurence Lundbladec5f45e42023-12-18 09:23:20 -07005730/*
5731 Lots of nesting for various nesting tests.
5732 { 1:1,
5733 2:{
5734 21:21,
5735 22:{
5736 221:[2111, 2112, 2113],
5737 222:222,
5738 223: {}
5739 },
5740 23: 23
5741 },
5742 3:3,
5743 4: [ {} ]
5744 }
5745 */
5746static const uint8_t spNested[] = {
57470xA4, /* Map of 4 */
5748 0x01, 0x01, /* Map entry 1 : 1 */
5749 0x02, 0xA3, /* Map entry 2 : {, an array of 3 */
5750 0x15, 0x15, /* Map entry 21 : 21 */
5751 0x16, 0xA3, /* Map entry 22 : {, a map of 3 */
5752 0x18, 0xDD, 0x83, /* Map entry 221 : [ an array of 3 */
5753 0x19, 0x08, 0x3F, /* Array item 2111 */
5754 0x19, 0x08, 0x40, /* Array item 2112 */
5755 0x19, 0x08, 0x41, /* Array item 2113 */
5756 0x18, 0xDE, 0x18, 0xDE, /* Map entry 222 : 222 */
5757 0x18, 0xDF, 0xA0, /* Map entry 223 : {} */
5758 0x17, 0x17, /* Map entry 23 : 23 */
5759 0x03, 0x03, /* Map entry 3 : 3 */
5760 0x04, 0x81, /* Map entry 4: [, an array of 1 */
5761 0xA0 /* Array entry {}, an empty map */
5762};
5763
5764
5765static int32_t EnterMapCursorTest(void)
5766{
5767 QCBORDecodeContext DCtx;
5768 QCBORItem Item1;
5769
5770 int i;
5771 for(i = 0; i < 13; i++) {
5772 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spNested), 0);
5773 QCBORDecode_EnterMap(&DCtx, NULL);
5774 int j;
5775 /* Move travesal cursor */
5776 for(j = 0; j < i; j++) {
5777 QCBORDecode_GetNext(&DCtx, &Item1);
5778 }
5779 QCBORDecode_EnterMapFromMapN(&DCtx, 2);
5780 QCBORDecode_ExitMap(&DCtx);
5781 QCBORDecode_GetNext(&DCtx, &Item1);
5782 if(Item1.label.int64 != 3) {
5783 return 8000;
5784 }
5785 }
5786
5787 for(i = 0; i < 13; i++) {
5788 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spNested), 0);
5789 QCBORDecode_EnterMap(&DCtx, NULL);
5790 int j;
5791 /* Move travesal cursor */
5792 for(j = 0; j < i; j++) {
5793 QCBORDecode_GetNext(&DCtx, &Item1);
5794 }
5795 QCBORDecode_EnterMapFromMapN(&DCtx, 2);
5796 QCBORDecode_EnterMapFromMapN(&DCtx, 22);
5797 QCBORDecode_ExitMap(&DCtx);
5798 QCBORDecode_GetNext(&DCtx, &Item1);
5799 if(Item1.label.int64 != 23) {
5800 return 8000;
5801 }
5802 }
5803
5804 for(i = 0; i < 13; i++) {
5805 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spNested), 0);
5806 QCBORDecode_EnterMap(&DCtx, NULL);
5807 int j;
5808 /* Move travesal cursor */
5809 for(j = 0; j < i; j++) {
5810 QCBORDecode_GetNext(&DCtx, &Item1);
5811 }
5812 QCBORDecode_EnterMapFromMapN(&DCtx, 2);
5813 QCBORDecode_EnterMapFromMapN(&DCtx, 22);
5814 for(j = 0; j < i; j++) {
5815 QCBORDecode_GetNext(&DCtx, &Item1);
5816 }
5817 QCBORDecode_EnterArrayFromMapN(&DCtx, 221);
5818 QCBORDecode_ExitArray(&DCtx);
5819 QCBORDecode_ExitMap(&DCtx);
5820 QCBORDecode_GetNext(&DCtx, &Item1);
5821 if(Item1.label.int64 != 23) {
5822 return 8000;
5823 }
5824 QCBORDecode_ExitMap(&DCtx);
5825 QCBORDecode_GetNext(&DCtx, &Item1);
5826 if(Item1.label.int64 != 3) {
5827 return 8000;
5828 }
5829 }
5830
5831 return 0;
5832}
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07005833
5834
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03005835int32_t EnterMapTest(void)
Laurence Lundbladebb87be22020-04-09 19:15:32 -07005836{
Laurence Lundbladef0499502020-08-01 11:55:57 -07005837 QCBORItem Item1;
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005838 QCBORItem ArrayItem;
Laurence Lundbladebb87be22020-04-09 19:15:32 -07005839 QCBORDecodeContext DCtx;
Laurence Lundbladef0499502020-08-01 11:55:57 -07005840 int32_t nReturn;
5841 QCBORError uErr;
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005842
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08005843#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005844 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spMapOfEmpty), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005845 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005846
Laurence Lundbladef0499502020-08-01 11:55:57 -07005847
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005848 QCBORDecode_EnterArray(&DCtx, NULL); // Label 0
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005849 QCBORDecode_ExitArray(&DCtx);
5850
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005851 QCBORDecode_EnterArray(&DCtx, NULL); // Label 9
5852 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005853 QCBORDecode_ExitArray(&DCtx);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005854 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005855 QCBORDecode_ExitArray(&DCtx);
5856 QCBORDecode_ExitArray(&DCtx);
5857
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005858 QCBORDecode_EnterMap(&DCtx, NULL); // Label 8
5859 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005860 QCBORDecode_ExitArray(&DCtx);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005861 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005862 QCBORDecode_ExitMap(&DCtx);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005863 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005864 QCBORDecode_ExitArray(&DCtx);
5865 QCBORDecode_ExitMap(&DCtx);
5866
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005867 QCBORDecode_EnterMap(&DCtx, NULL); // Label4
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005868 QCBORDecode_ExitMap(&DCtx);
5869
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005870 QCBORDecode_EnterArray(&DCtx, NULL); // Label 5
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005871 QCBORDecode_ExitArray(&DCtx);
5872
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005873 QCBORDecode_EnterArray(&DCtx, NULL); // Label 6
5874 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005875 QCBORDecode_ExitArray(&DCtx);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005876 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005877 QCBORDecode_ExitArray(&DCtx);
5878 QCBORDecode_ExitArray(&DCtx);
5879
5880 QCBORDecode_ExitMap(&DCtx);
5881
5882 uErr = QCBORDecode_Finish(&DCtx);
5883 if(uErr != QCBOR_SUCCESS){
5884 return 3011;
5885 }
5886
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07005887 (void)pValidMapIndefEncoded;
Laurence Lundbladef7a70bc2020-10-24 12:23:25 -07005888 nReturn = SpiffyDecodeBasicMap(UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapIndefEncoded));
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07005889 if(nReturn) {
5890 return nReturn + 20000;
Laurence Lundbladebb87be22020-04-09 19:15:32 -07005891 }
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08005892#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
5893
Laurence Lundbladebb87be22020-04-09 19:15:32 -07005894
Laurence Lundbladef7a70bc2020-10-24 12:23:25 -07005895 nReturn = SpiffyDecodeBasicMap(UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded));
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005896 if(nReturn) {
5897 return nReturn;
5898 }
Laurence Lundbladebb87be22020-04-09 19:15:32 -07005899
Laurence Lundblade8ffdb742020-05-07 02:49:18 -07005900
Laurence Lundblade937ea812020-05-08 11:38:23 -07005901
Laurence Lundblade2f467f92020-10-09 17:50:11 -07005902 // These tests confirm the cursor is at the right place after entering
5903 // a map or array
Laurence Lundblade9b334962020-08-27 10:55:53 -07005904 const UsefulBufC ValidEncodedMap = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded);
Laurence Lundblade937ea812020-05-08 11:38:23 -07005905
5906 // Confirm cursor is at right place
Laurence Lundblade9b334962020-08-27 10:55:53 -07005907 QCBORDecode_Init(&DCtx, ValidEncodedMap, 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005908 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade937ea812020-05-08 11:38:23 -07005909 QCBORDecode_GetNext(&DCtx, &Item1);
5910 if(Item1.uDataType != QCBOR_TYPE_INT64) {
5911 return 2001;
5912 }
5913
5914
Laurence Lundblade9b334962020-08-27 10:55:53 -07005915 QCBORDecode_Init(&DCtx, ValidEncodedMap, 0);
Laurence Lundblade6f3f78e2020-08-31 13:09:14 -07005916 QCBORDecode_VGetNext(&DCtx, &Item1);
5917 QCBORDecode_VGetNext(&DCtx, &Item1);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005918 QCBORDecode_EnterArray(&DCtx, &ArrayItem);
5919 if(ArrayItem.uLabelType != QCBOR_TYPE_TEXT_STRING ||
5920 UsefulBuf_Compare(ArrayItem.label.string,
5921 UsefulBuf_FROM_SZ_LITERAL("an array of two strings"))) {
5922 return 2051;
5923 }
Laurence Lundblade937ea812020-05-08 11:38:23 -07005924 QCBORDecode_GetNext(&DCtx, &Item1);
5925 if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING) {
5926 return 2002;
5927 }
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005928 QCBORDecode_ExitArray(&DCtx);
5929 QCBORDecode_EnterMap(&DCtx, &ArrayItem);
5930 if(ArrayItem.uLabelType != QCBOR_TYPE_TEXT_STRING ||
5931 UsefulBuf_Compare(ArrayItem.label.string,
5932 UsefulBuf_FROM_SZ_LITERAL("map in a map"))) {
5933 return 2052;
5934 }
5935
Laurence Lundblade937ea812020-05-08 11:38:23 -07005936
Laurence Lundblade9b334962020-08-27 10:55:53 -07005937 QCBORDecode_Init(&DCtx, ValidEncodedMap, 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005938 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade64b607e2020-05-13 13:05:57 -07005939 QCBORDecode_GetNext(&DCtx, &Item1);
5940 QCBORDecode_GetNext(&DCtx, &Item1);
5941 QCBORDecode_GetNext(&DCtx, &Item1);
Laurence Lundblade937ea812020-05-08 11:38:23 -07005942 QCBORDecode_EnterMapFromMapSZ(&DCtx, "map in a map");
5943 QCBORDecode_GetNext(&DCtx, &Item1);
5944 if(Item1.uDataType != QCBOR_TYPE_BYTE_STRING) {
5945 return 2003;
5946 }
5947
Laurence Lundblade9b334962020-08-27 10:55:53 -07005948 QCBORDecode_Init(&DCtx, ValidEncodedMap, 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005949 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade937ea812020-05-08 11:38:23 -07005950 QCBORDecode_GetNext(&DCtx, &Item1);
5951 QCBORDecode_GetNext(&DCtx, &Item1);
5952 QCBORDecode_GetNext(&DCtx, &Item1);
5953 QCBORDecode_GetNext(&DCtx, &Item1);
5954 QCBORDecode_GetNext(&DCtx, &Item1);
5955 QCBORDecode_GetNext(&DCtx, &Item1);
5956 QCBORDecode_GetNext(&DCtx, &Item1);
5957 QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings");
5958 QCBORDecode_GetNext(&DCtx, &Item1);
5959 if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING) {
Laurence Lundblade64b607e2020-05-13 13:05:57 -07005960 return 2004;
Laurence Lundblade937ea812020-05-08 11:38:23 -07005961 }
5962
Laurence Lundblade9b334962020-08-27 10:55:53 -07005963 QCBORDecode_Init(&DCtx, ValidEncodedMap, 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005964 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade2b843b52020-06-16 20:51:03 -07005965 QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings");
5966 QCBORDecode_ExitArray(&DCtx);
5967 QCBORDecode_GetNext(&DCtx, &Item1);
5968 if(Item1.uDataType != QCBOR_TYPE_MAP && Item1.uLabelAlloc != QCBOR_TYPE_TEXT_STRING) {
5969 return 2006;
5970 }
5971 QCBORDecode_ExitMap(&DCtx);
5972 if(QCBORDecode_GetNext(&DCtx, &Item1) != QCBOR_ERR_NO_MORE_ITEMS) {
5973 return 2007;
5974 }
5975
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07005976 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSimpleArray), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005977 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07005978 int64_t nDecodedInt2;
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005979 QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2);
5980 uErr = QCBORDecode_GetAndResetError(&DCtx);
5981 if(uErr != QCBOR_ERR_MAP_NOT_ENTERED){
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07005982 return 2008;
5983 }
5984 UsefulBufC String;
Laurence Lundblade323f8a92020-09-06 19:43:09 -07005985 QCBORDecode_GetTextStringInMapN(&DCtx, 88, &String);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005986 if(uErr != QCBOR_ERR_MAP_NOT_ENTERED){
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07005987 return 2009;
5988 }
Laurence Lundblade937ea812020-05-08 11:38:23 -07005989
Laurence Lundbladee6f15112020-07-23 18:44:16 -07005990
5991 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEmptyMap), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005992 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundbladee6f15112020-07-23 18:44:16 -07005993 // This will fail because the map is empty.
5994 QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2);
5995 uErr = QCBORDecode_GetAndResetError(&DCtx);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07005996 if(uErr != QCBOR_ERR_LABEL_NOT_FOUND){
Laurence Lundbladee6f15112020-07-23 18:44:16 -07005997 return 2010;
5998 }
5999 QCBORDecode_ExitMap(&DCtx);
6000 uErr = QCBORDecode_Finish(&DCtx);
6001 if(uErr != QCBOR_SUCCESS){
6002 return 2011;
6003 }
6004
6005
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08006006#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundbladee6f15112020-07-23 18:44:16 -07006007 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEmptyInDefinteLengthMap), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006008 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundbladee6f15112020-07-23 18:44:16 -07006009 // This will fail because the map is empty.
6010 QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2);
6011 uErr = QCBORDecode_GetAndResetError(&DCtx);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006012 if(uErr != QCBOR_ERR_LABEL_NOT_FOUND){
Laurence Lundblade085d7952020-07-24 10:26:30 -07006013 return 2012;
Laurence Lundbladee6f15112020-07-23 18:44:16 -07006014 }
6015 QCBORDecode_ExitMap(&DCtx);
6016 uErr = QCBORDecode_Finish(&DCtx);
6017 if(uErr != QCBOR_SUCCESS){
Laurence Lundblade085d7952020-07-24 10:26:30 -07006018 return 2013;
Laurence Lundbladee6f15112020-07-23 18:44:16 -07006019 }
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08006020#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
Laurence Lundbladee6f15112020-07-23 18:44:16 -07006021
Laurence Lundblade5f4e8712020-07-25 11:44:43 -07006022
6023 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spArrayOfEmpty), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006024 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade323f8a92020-09-06 19:43:09 -07006025 QCBORDecode_GetByteString(&DCtx, &String);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006026 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade5f4e8712020-07-25 11:44:43 -07006027 QCBORDecode_ExitMap(&DCtx);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006028 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade5f4e8712020-07-25 11:44:43 -07006029 QCBORDecode_ExitArray(&DCtx);
6030 QCBORDecode_GetInt64(&DCtx, &nDecodedInt2);
6031 QCBORDecode_ExitArray(&DCtx);
6032 uErr = QCBORDecode_Finish(&DCtx);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07006033 if(uErr != QCBOR_SUCCESS) {
Laurence Lundblade5f4e8712020-07-25 11:44:43 -07006034 return 2014;
6035 }
6036
Laurence Lundblade93d3f532020-09-28 21:09:12 -07006037 int64_t nInt;
6038 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spRecoverableMapErrors), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006039 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade37286c02022-09-03 10:05:02 -07006040#ifndef QCBOR_DISABLE_TAGS
Laurence Lundblade93d3f532020-09-28 21:09:12 -07006041 QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt);
Laurence Lundblade88e9db22020-11-02 03:56:33 -08006042 uErr = QCBORDecode_GetError(&DCtx);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07006043 if(uErr != QCBOR_ERR_TOO_MANY_TAGS) {
6044 return 2021;
6045 }
Laurence Lundblade88e9db22020-11-02 03:56:33 -08006046 if(QCBORDecode_GetNthTagOfLast(&DCtx, 0) != CBOR_TAG_INVALID64) {
6047 return 2121;
6048 }
6049 (void)QCBORDecode_GetAndResetError(&DCtx);
Laurence Lundblade37286c02022-09-03 10:05:02 -07006050#endif
Laurence Lundblade88e9db22020-11-02 03:56:33 -08006051
Laurence Lundblade93d3f532020-09-28 21:09:12 -07006052
Laurence Lundblade93d3f532020-09-28 21:09:12 -07006053 QCBORDecode_GetInt64InMapN(&DCtx, 0x03, &nInt);
6054 uErr = QCBORDecode_GetAndResetError(&DCtx);
6055 if(uErr != QCBOR_ERR_INT_OVERFLOW) {
6056 return 2023;
6057 }
6058
Laurence Lundblade37286c02022-09-03 10:05:02 -07006059#ifndef QCBOR_DISABLE_TAGS
Laurence Lundblade93d3f532020-09-28 21:09:12 -07006060 QCBORDecode_GetEpochDateInMapN(&DCtx, 0x04, QCBOR_TAG_REQUIREMENT_TAG, &nInt);
6061 uErr = QCBORDecode_GetAndResetError(&DCtx);
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006062 if(uErr != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_DATE_OVERFLOW)) {
Laurence Lundblade93d3f532020-09-28 21:09:12 -07006063 return 2024;
6064 }
Laurence Lundblade37286c02022-09-03 10:05:02 -07006065#endif
Laurence Lundblade93d3f532020-09-28 21:09:12 -07006066
6067 QCBORDecode_GetInt64InMapN(&DCtx, 0x05, &nInt);
6068 uErr = QCBORDecode_GetAndResetError(&DCtx);
6069 if(uErr != QCBOR_ERR_DUPLICATE_LABEL) {
6070 return 2025;
6071 }
6072
6073 QCBORDecode_GetInt64InMapN(&DCtx, 0x08, &nInt);
6074
6075 QCBORDecode_ExitMap(&DCtx);
6076 uErr = QCBORDecode_Finish(&DCtx);
6077 if(uErr != QCBOR_SUCCESS) {
6078 return 2026;
6079 }
6080
6081 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUnRecoverableMapError1), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006082 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07006083 QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt);
6084 uErr = QCBORDecode_GetAndResetError(&DCtx);
6085 if(uErr != QCBOR_ERR_BAD_BREAK) {
6086 return 2030;
6087 }
6088
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08006089#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundblade93d3f532020-09-28 21:09:12 -07006090 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUnRecoverableMapError2), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006091 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07006092 QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt);
6093 uErr = QCBORDecode_GetAndResetError(&DCtx);
6094 if(uErr != QCBOR_ERR_NO_MORE_ITEMS) {
6095 return 2031;
6096 }
6097
6098 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUnRecoverableMapError3), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006099 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07006100 QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt);
6101 uErr = QCBORDecode_GetAndResetError(&DCtx);
6102 if(uErr != QCBOR_ERR_HIT_END) {
6103 return 2032;
6104 }
6105
6106 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUnRecoverableMapError4), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006107 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07006108 QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt);
6109 uErr = QCBORDecode_GetAndResetError(&DCtx);
6110 if(uErr != QCBOR_ERR_ARRAY_DECODE_NESTING_TOO_DEEP) {
6111 return 2033;
6112 }
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08006113#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
6114
Laurence Lundblade732e52d2021-02-22 20:11:01 -07006115 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0);
6116 QCBORDecode_VGetNextConsume(&DCtx, &Item1);
6117 if(Item1.uDataType != QCBOR_TYPE_MAP) {
6118 return 2401;
6119 }
6120 if(QCBORDecode_GetError(&DCtx)) {
6121 return 2402;
6122 }
6123
6124 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0);
6125 QCBORDecode_VGetNext(&DCtx, &Item1);
6126 if(Item1.uDataType != QCBOR_TYPE_MAP ||
6127 Item1.val.uCount != 3 ||
6128 Item1.uNextNestLevel != 1) {
6129 return 2403;
6130 }
6131 if(QCBORDecode_GetError(&DCtx)) {
6132 return 2404;
6133 }
6134 QCBORDecode_VGetNextConsume(&DCtx, &Item1);
6135 if(Item1.uDataType != QCBOR_TYPE_INT64 ||
6136 Item1.uNextNestLevel != 1 ||
6137 Item1.val.int64 != 42) {
6138 return 2405;
6139 }
6140 if(QCBORDecode_GetError(&DCtx)) {
6141 return 2406;
6142 }
6143 QCBORDecode_VGetNextConsume(&DCtx, &Item1);
6144 if(Item1.uDataType != QCBOR_TYPE_ARRAY ||
6145 Item1.uNestingLevel != 1 ||
6146 Item1.uNextNestLevel != 1 ||
6147 Item1.val.uCount != 2) {
6148 return 2407;
6149 }
6150 if(QCBORDecode_GetError(&DCtx)) {
6151 return 2408;
6152 }
6153 QCBORDecode_VGetNextConsume(&DCtx, &Item1);
6154 if(Item1.uDataType != QCBOR_TYPE_MAP ||
6155 Item1.uNestingLevel != 1 ||
6156 Item1.uNextNestLevel != 0 ||
6157 Item1.val.uCount != 4) {
6158 return 2409;
6159 }
6160 if(QCBORDecode_GetError(&DCtx)) {
6161 return 2410;
6162 }
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006163
Laurence Lundbladeddebabe2020-11-22 11:32:17 -08006164 nReturn = DecodeNestedIterate();
6165
Laurence Lundblade63926052021-03-29 16:05:51 -07006166
6167 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(not_well_formed_submod_section), 0);
6168 QCBORDecode_EnterMap(&DCtx, NULL);
6169 QCBORDecode_EnterMapFromMapN(&DCtx, 20);
6170 if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_BAD_INT) {
6171 return 2500;
6172 }
6173
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07006174 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBadConsumeInput), 0);
6175 QCBORDecode_VGetNextConsume(&DCtx, &Item1);
6176 if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_NO_MORE_ITEMS) {
6177 return 2600;
6178 }
6179
Laurence Lundblade37286c02022-09-03 10:05:02 -07006180#ifndef QCBOR_DISABLE_TAGS
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07006181 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBadConsumeInput2), 0);
6182 QCBORDecode_VGetNextConsume(&DCtx, &Item1);
6183 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) {
6184 return 2700;
6185 }
6186
6187 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBadConsumeInput3), 0);
6188 QCBORDecode_VGetNextConsume(&DCtx, &Item1);
6189 if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT) {
6190 return 2800;
6191 }
Laurence Lundblade37286c02022-09-03 10:05:02 -07006192#endif
6193
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07006194
6195 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBadConsumeInput4), 0);
6196 QCBORDecode_VGetNextConsume(&DCtx, &Item1);
6197#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
6198 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) {
6199 return 2900;
6200 }
6201#else
6202 if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_INDEF_LEN_ARRAYS_DISABLED) {
6203 return 2901;
6204 }
6205#endif
6206
6207 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBadConsumeInput5), 0);
6208 QCBORDecode_VGetNextConsume(&DCtx, &Item1);
6209 if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_MAP_LABEL_TYPE) {
6210 return 3000;
6211 }
6212
Laurence Lundbladec5f45e42023-12-18 09:23:20 -07006213 nReturn = EnterMapCursorTest();
6214
Laurence Lundbladeddebabe2020-11-22 11:32:17 -08006215 return nReturn;
Laurence Lundblade9c905e82020-04-25 11:31:38 -07006216}
6217
6218
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006219struct NumberConversion {
6220 char *szDescription;
6221 UsefulBufC CBOR;
6222 int64_t nConvertedToInt64;
6223 QCBORError uErrorInt64;
6224 uint64_t uConvertToUInt64;
6225 QCBORError uErrorUint64;
6226 double dConvertToDouble;
6227 QCBORError uErrorDouble;
6228};
6229
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006230#ifndef QCBOR_DISABLE_EXP_AND_MANTISSA
6231#define EXP_AND_MANTISSA_ERROR(x) x
6232#else
6233#define EXP_AND_MANTISSA_ERROR(x) QCBOR_ERR_UNEXPECTED_TYPE
6234#endif
6235
6236
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07006237static const struct NumberConversion NumberConversions[] = {
Laurence Lundblade37286c02022-09-03 10:05:02 -07006238#ifndef QCBOR_DISABLE_TAGS
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006239 {
Laurence Lundblade4e808ba2022-12-29 12:45:20 -07006240 "Big float: INT64_MIN * 2e-1 to test handling of INT64_MIN",
6241 {(uint8_t[]){0xC5, 0x82, 0x20,
6242 0x3B, 0x7f, 0xff, 0xff, 0xff, 0xff, 0x0ff, 0xff, 0xff,
6243 }, 15},
6244 -4611686018427387904, /* INT64_MIN / 2 */
6245 EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS),
6246 0,
6247 EXP_AND_MANTISSA_ERROR(QCBOR_ERR_NUMBER_SIGN_CONVERSION),
6248 -4.6116860184273879E+18,
6249 FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS))
6250 },
6251 {
Laurence Lundblade784b54b2020-08-10 01:24:52 -07006252 "too large to fit into int64_t",
6253 {(uint8_t[]){0xc3, 0x48, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 10},
6254 0,
6255 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
6256 0,
6257 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
6258 ((double)INT64_MIN) + 1 ,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006259 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)
Laurence Lundblade784b54b2020-08-10 01:24:52 -07006260 },
6261 {
6262 "largest negative int that fits in int64_t",
6263 {(uint8_t[]){0xc3, 0x48, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, 10},
6264 INT64_MIN,
6265 QCBOR_SUCCESS,
6266 0,
6267 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
6268 (double)INT64_MIN,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006269 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)
Laurence Lundblade784b54b2020-08-10 01:24:52 -07006270 },
6271 {
Laurence Lundbladeda095972020-06-06 18:35:33 -07006272 "negative bignum -1",
6273 {(uint8_t[]){0xc3, 0x41, 0x00}, 3},
6274 -1,
6275 QCBOR_SUCCESS,
6276 0,
6277 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
6278 -1.0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006279 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)
Laurence Lundbladeda095972020-06-06 18:35:33 -07006280 },
6281 {
Laurence Lundblade9ab5abb2020-05-20 12:10:45 -07006282 "Decimal Fraction with positive bignum 257 * 10e3",
Laurence Lundblade887add82020-05-17 05:50:34 -07006283 {(uint8_t[]){0xC4, 0x82, 0x1B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03,
6284 0xC2, 0x42, 0x01, 0x01}, 15},
6285 257000,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006286 EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS),
Laurence Lundblade887add82020-05-17 05:50:34 -07006287 257000,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006288 EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS),
Laurence Lundblade887add82020-05-17 05:50:34 -07006289 257000.0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006290 FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS))
Laurence Lundblade887add82020-05-17 05:50:34 -07006291 },
6292 {
Laurence Lundbladeda095972020-06-06 18:35:33 -07006293 "bigfloat with negative bignum -258 * 2e3",
Laurence Lundblade887add82020-05-17 05:50:34 -07006294 {(uint8_t[]){0xC5, 0x82, 0x1B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03,
6295 0xC3, 0x42, 0x01, 0x01}, 15},
Laurence Lundbladeda095972020-06-06 18:35:33 -07006296 -2064,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006297 EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS),
Laurence Lundblade887add82020-05-17 05:50:34 -07006298 0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006299 EXP_AND_MANTISSA_ERROR(QCBOR_ERR_NUMBER_SIGN_CONVERSION),
Laurence Lundbladeda095972020-06-06 18:35:33 -07006300 -2064.0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006301 FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS))
Laurence Lundblade887add82020-05-17 05:50:34 -07006302 },
6303 {
Laurence Lundblade9ab5abb2020-05-20 12:10:45 -07006304 "bigfloat with positive bignum 257 * 2e3",
Laurence Lundblade887add82020-05-17 05:50:34 -07006305 {(uint8_t[]){0xC5, 0x82, 0x1B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03,
6306 0xC2, 0x42, 0x01, 0x01}, 15},
6307 2056,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006308 EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS),
Laurence Lundblade887add82020-05-17 05:50:34 -07006309 2056,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006310 EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS),
Laurence Lundblade887add82020-05-17 05:50:34 -07006311 2056.0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006312 FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS))
Laurence Lundblade887add82020-05-17 05:50:34 -07006313 },
6314 {
Laurence Lundbladeda095972020-06-06 18:35:33 -07006315 "negative bignum 0xc349010000000000000000 -18446744073709551617",
Laurence Lundblade887add82020-05-17 05:50:34 -07006316 {(uint8_t[]){0xc3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 11},
6317 0,
6318 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
6319 0,
6320 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
6321 -18446744073709551617.0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006322 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)
Laurence Lundblade887add82020-05-17 05:50:34 -07006323 },
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08006324#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
Laurence Lundblade887add82020-05-17 05:50:34 -07006325 {
Laurence Lundblade313b2862020-05-16 01:23:06 -07006326 "Positive bignum 0x01020304 indefinite length string",
6327 {(uint8_t[]){0xC2, 0x5f, 0x42, 0x01, 0x02, 0x41, 0x03, 0x41, 0x04, 0xff}, 10},
6328 0x01020304,
6329 QCBOR_SUCCESS,
6330 0x01020304,
6331 QCBOR_SUCCESS,
6332 16909060.0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006333 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)
Laurence Lundblade313b2862020-05-16 01:23:06 -07006334 },
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08006335#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
Laurence Lundblade313b2862020-05-16 01:23:06 -07006336 {
Laurence Lundblade887add82020-05-17 05:50:34 -07006337 "Decimal Fraction with neg bignum [9223372036854775807, -4759477275222530853137]",
Laurence Lundblade313b2862020-05-16 01:23:06 -07006338 {(uint8_t[]){0xC4, 0x82, 0x1B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
6339 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,}, 23},
6340 0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006341 EXP_AND_MANTISSA_ERROR(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW),
Laurence Lundblade313b2862020-05-16 01:23:06 -07006342 0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006343 EXP_AND_MANTISSA_ERROR(QCBOR_ERR_NUMBER_SIGN_CONVERSION),
Laurence Lundblade313b2862020-05-16 01:23:06 -07006344 -INFINITY,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006345 FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS))
Laurence Lundblade313b2862020-05-16 01:23:06 -07006346 },
6347 {
6348 "big float [9223372036854775806, 9223372036854775806]",
6349 {(uint8_t[]){0xC5, 0x82, 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE,
6350 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE}, 20},
6351 0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006352 EXP_AND_MANTISSA_ERROR(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW),
Laurence Lundblade313b2862020-05-16 01:23:06 -07006353 0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006354 EXP_AND_MANTISSA_ERROR(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW),
Laurence Lundblade313b2862020-05-16 01:23:06 -07006355 INFINITY,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006356 FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS))
Laurence Lundblade313b2862020-05-16 01:23:06 -07006357 },
6358 {
Laurence Lundblade983500d2020-05-14 11:49:34 -07006359 "Big float 3 * 2^^2",
6360 {(uint8_t[]){0xC5, 0x82, 0x02, 0x03}, 4},
6361 12,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006362 EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS),
Laurence Lundblade983500d2020-05-14 11:49:34 -07006363 12,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006364 EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS),
Laurence Lundblade983500d2020-05-14 11:49:34 -07006365 12.0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006366 FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS))
Laurence Lundblade983500d2020-05-14 11:49:34 -07006367 },
Laurence Lundblade983500d2020-05-14 11:49:34 -07006368 {
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006369 "Decimal fraction 3/10",
6370 {(uint8_t[]){0xC4, 0x82, 0x20, 0x03}, 4},
6371 0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006372 EXP_AND_MANTISSA_ERROR(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW),
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006373 0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006374 EXP_AND_MANTISSA_ERROR(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW),
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006375 0.30000000000000004,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006376 FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS))
Laurence Lundbladedfd49fc2020-09-01 14:17:16 -07006377 },
6378 {
Laurence Lundblade11fd78b2020-09-01 22:13:27 -07006379 "extreme pos bignum",
6380 {(uint8_t[]){0xc2, 0x59, 0x01, 0x90,
6381 // 50 rows of 8 is 400 digits.
6382 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6383 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6384 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6385 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6386 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6387 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6388 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6389 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6390 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6391 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6392 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6393 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6394 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6395 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6396 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6397 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6398 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6399 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6400 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6401 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6402 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6403 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6404 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6405 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6406 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6407 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6408 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6409 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6410 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6411 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6412 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6413 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6414 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6415 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6416 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6417 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6418 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6419 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6420 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6421 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6422 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6423 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6424 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6425 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6426 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6427 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6428 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6429 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6430 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
gtravisb787e82a2023-11-30 18:38:21 -08006431 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0},
Laurence Lundblade11fd78b2020-09-01 22:13:27 -07006432 404},
6433 0,
6434 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
6435 0,
6436 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
Laurence Lundblade11fd78b2020-09-01 22:13:27 -07006437 INFINITY,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006438 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS),
Laurence Lundblade11fd78b2020-09-01 22:13:27 -07006439 },
6440
6441 {
6442 "extreme neg bignum",
6443 {(uint8_t[]){0xc3, 0x59, 0x01, 0x90,
6444 // 50 rows of 8 is 400 digits.
6445 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6446 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6447 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6448 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6449 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6450 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6451 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6452 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6453 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6454 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6455 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6456 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6457 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6458 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6459 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6460 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6461 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6462 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6463 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6464 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6465 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6466 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6467 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6468 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6469 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6470 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6471 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6472 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6473 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6474 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6475 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6476 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6477 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6478 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6479 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6480 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6481 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6482 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6483 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6484 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6485 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6486 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6487 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6488 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6489 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6490 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6491 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6492 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6493 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
gtravisb787e82a2023-11-30 18:38:21 -08006494 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0},
Laurence Lundblade11fd78b2020-09-01 22:13:27 -07006495 404},
6496 0,
6497 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
6498 0,
6499 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
Laurence Lundblade11fd78b2020-09-01 22:13:27 -07006500 -INFINITY,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006501 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)
Laurence Lundblade11fd78b2020-09-01 22:13:27 -07006502 },
Laurence Lundblade51722fd2020-09-02 13:01:33 -07006503
6504 {
6505 "big float underflow [9223372036854775806, -9223372036854775806]",
6506 {(uint8_t[]){
6507 0xC5, 0x82,
6508 0x3B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE,
6509 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE}, 20},
Laurence Lundblade51722fd2020-09-02 13:01:33 -07006510 0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006511 EXP_AND_MANTISSA_ERROR(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW),
Laurence Lundblade51722fd2020-09-02 13:01:33 -07006512 0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006513 EXP_AND_MANTISSA_ERROR(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW),
Laurence Lundblade51722fd2020-09-02 13:01:33 -07006514 0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006515 FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS))
Laurence Lundblade51722fd2020-09-02 13:01:33 -07006516 },
6517
6518 {
6519 "bigfloat that evaluates to -INFINITY",
6520 {(uint8_t[]){
6521 0xC5, 0x82,
6522 0x1B, 0x70, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03,
6523 0xC3, 0x42, 0x01, 0x01}, 15},
Laurence Lundblade51722fd2020-09-02 13:01:33 -07006524 0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006525 EXP_AND_MANTISSA_ERROR(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW),
Laurence Lundblade51722fd2020-09-02 13:01:33 -07006526 0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006527 EXP_AND_MANTISSA_ERROR(QCBOR_ERR_NUMBER_SIGN_CONVERSION),
Laurence Lundblade51722fd2020-09-02 13:01:33 -07006528 -INFINITY,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006529 FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS))
Laurence Lundblade51722fd2020-09-02 13:01:33 -07006530 },
Laurence Lundblade37286c02022-09-03 10:05:02 -07006531 {
6532 "Positive bignum 0xffff",
6533 {(uint8_t[]){0xC2, 0x42, 0xff, 0xff}, 4},
6534 65536-1,
6535 QCBOR_SUCCESS,
6536 0xffff,
6537 QCBOR_SUCCESS,
6538 65535.0,
6539 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)
6540 },
6541#endif /* QCBOR_DISABLE_TAGS */
6542 {
6543 "Positive integer 18446744073709551615",
6544 {(uint8_t[]){0x1b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, 9},
6545 0,
6546 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
6547 18446744073709551615ULL,
6548 QCBOR_SUCCESS,
6549 18446744073709551615.0,
6550 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)
6551 },
6552
6553 {
6554 "Postive integer 0",
6555 {(uint8_t[]){0x0}, 1},
6556 0LL,
6557 QCBOR_SUCCESS,
6558 0ULL,
6559 QCBOR_SUCCESS,
6560 0.0,
6561 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)
6562 },
6563 {
6564 "Negative integer -18446744073709551616",
6565 {(uint8_t[]){0x3b, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }, 9},
6566 -9223372036854775807-1, // INT64_MIN
6567 QCBOR_SUCCESS,
6568 0ULL,
6569 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
6570 -9223372036854775808.0,
6571 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)
6572 },
6573 {
6574 "Double Floating point value 100.3",
6575 {(uint8_t[]){0xfb, 0x40, 0x59, 0x13, 0x33, 0x33, 0x33, 0x33, 0x33}, 9},
6576 100L,
6577 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS),
6578 100ULL,
6579 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS),
6580 100.3,
6581 FLOAT_ERR_CODE_NO_FLOAT(QCBOR_SUCCESS),
6582 },
6583 {
6584 "Floating point value NaN 0xfa7fc00000",
6585 {(uint8_t[]){0xfa, 0x7f, 0xc0, 0x00, 0x00}, 5},
6586 0,
6587 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_FLOAT_EXCEPTION),
6588 0,
6589 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_FLOAT_EXCEPTION),
6590 NAN,
6591 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS),
6592 },
6593 {
6594 "half-precision Floating point value -4",
6595 {(uint8_t[]){0xf9, 0xc4, 0x00}, 3},
6596 // Normal case with all enabled.
6597 -4,
6598 FLOAT_ERR_CODE_NO_HALF_PREC_NO_FLOAT_HW(QCBOR_SUCCESS),
6599 0,
6600 FLOAT_ERR_CODE_NO_HALF_PREC_NO_FLOAT_HW(QCBOR_ERR_NUMBER_SIGN_CONVERSION),
6601 -4.0,
6602 FLOAT_ERR_CODE_NO_HALF_PREC(QCBOR_SUCCESS)
6603 },
6604 {
6605 "+inifinity single precision",
6606 {(uint8_t[]){0xfa, 0x7f, 0x80, 0x00, 0x00}, 5},
6607 0,
6608 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_FLOAT_EXCEPTION),
6609 0,
6610 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW),
6611 INFINITY,
6612 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)
6613 },
6614
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006615};
Laurence Lundblade9c905e82020-04-25 11:31:38 -07006616
6617
6618
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08006619
6620static int32_t SetUpDecoder(QCBORDecodeContext *DCtx, UsefulBufC CBOR, UsefulBuf Pool)
6621{
6622 QCBORDecode_Init(DCtx, CBOR, QCBOR_DECODE_MODE_NORMAL);
6623#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
6624 if(QCBORDecode_SetMemPool(DCtx, Pool, 0)) {
6625 return 1;
6626 }
6627#else /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
6628 (void)Pool;
6629#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
6630 return 0;
6631}
6632
6633
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03006634int32_t IntegerConvertTest(void)
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006635{
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08006636 const int nNumTests = C_ARRAY_COUNT(NumberConversions,
6637 struct NumberConversion);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006638
Laurence Lundblade9ab5abb2020-05-20 12:10:45 -07006639 for(int nIndex = 0; nIndex < nNumTests; nIndex++) {
6640 const struct NumberConversion *pF = &NumberConversions[nIndex];
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07006641
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006642 // Set up the decoding context including a memory pool so that
6643 // indefinite length items can be checked
6644 QCBORDecodeContext DCtx;
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006645 UsefulBuf_MAKE_STACK_UB(Pool, 100);
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07006646
6647 /* ----- test conversion to int64_t ------ */
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08006648 if(SetUpDecoder(&DCtx, pF->CBOR, Pool)) {
6649 return (int32_t)(3333+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006650 }
6651
6652 int64_t nInt;
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006653 QCBORDecode_GetInt64ConvertAll(&DCtx, 0xffff, &nInt);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07006654 if(QCBORDecode_GetError(&DCtx) != pF->uErrorInt64) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07006655 return (int32_t)(2000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006656 }
6657 if(pF->uErrorInt64 == QCBOR_SUCCESS && pF->nConvertedToInt64 != nInt) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07006658 return (int32_t)(3000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006659 }
6660
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07006661 /* ----- test conversion to uint64_t ------ */
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08006662 if(SetUpDecoder(&DCtx, pF->CBOR, Pool)) {
6663 return (int32_t)(3333+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006664 }
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08006665
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006666 uint64_t uInt;
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006667 QCBORDecode_GetUInt64ConvertAll(&DCtx, 0xffff, &uInt);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07006668 if(QCBORDecode_GetError(&DCtx) != pF->uErrorUint64) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07006669 return (int32_t)(4000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006670 }
6671 if(pF->uErrorUint64 == QCBOR_SUCCESS && pF->uConvertToUInt64 != uInt) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07006672 return (int32_t)(5000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006673 }
6674
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07006675 /* ----- test conversion to double ------ */
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08006676 if(SetUpDecoder(&DCtx, pF->CBOR, Pool)) {
6677 return (int32_t)(3333+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006678 }
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006679
6680#ifndef USEFULBUF_DISABLE_ALL_FLOAT
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006681 double d;
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006682 QCBORDecode_GetDoubleConvertAll(&DCtx, 0xffff, &d);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07006683 if(QCBORDecode_GetError(&DCtx) != pF->uErrorDouble) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07006684 return (int32_t)(6000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006685 }
6686 if(pF->uErrorDouble == QCBOR_SUCCESS) {
6687 if(isnan(pF->dConvertToDouble)) {
Laurence Lundblade983500d2020-05-14 11:49:34 -07006688 // NaN's can't be compared for equality. A NaN is
6689 // never equal to anything including another NaN
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006690 if(!isnan(d)) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07006691 return (int32_t)(7000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006692 }
6693 } else {
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006694 if(pF->dConvertToDouble != d) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07006695 return (int32_t)(8000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006696 }
6697 }
6698 }
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006699#endif /* USEFULBUF_DISABLE_ALL_FLOAT */
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006700 }
6701
6702 return 0;
6703}
6704
Laurence Lundbladea8758502022-05-15 17:57:46 -07006705#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
6706
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03006707int32_t CBORTestIssue134(void)
David Navarro9123e5b2022-03-28 16:04:03 +02006708{
6709 QCBORDecodeContext DCtx;
6710 QCBORItem Item;
6711 QCBORError uCBORError;
6712 const uint8_t spTestIssue134[] = { 0x5F, 0x40, 0xFF };
Laurence Lundblade9c905e82020-04-25 11:31:38 -07006713
David Navarro9123e5b2022-03-28 16:04:03 +02006714 QCBORDecode_Init(&DCtx,
6715 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTestIssue134),
6716 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade97c61bf2020-05-02 11:24:06 -07006717
David Navarro9123e5b2022-03-28 16:04:03 +02006718 UsefulBuf_MAKE_STACK_UB(StringBuf, 200);
6719 QCBORDecode_SetMemPool(&DCtx, StringBuf, false);
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03006720
David Navarro9123e5b2022-03-28 16:04:03 +02006721 do {
6722 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
6723 } while (QCBOR_SUCCESS == uCBORError);
6724
6725 uCBORError = QCBORDecode_Finish(&DCtx);
6726
Laurence Lundblade11ea3612022-07-01 13:26:23 -07006727 return (int32_t)uCBORError;
David Navarro9123e5b2022-03-28 16:04:03 +02006728}
Laurence Lundblade97c61bf2020-05-02 11:24:06 -07006729
Laurence Lundbladea8758502022-05-15 17:57:46 -07006730#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
6731
Laurence Lundblade37286c02022-09-03 10:05:02 -07006732
6733
6734static const uint8_t spSequenceTestInput[] = {
6735 /* 1. The valid date string "1985-04-12" */
6736 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string
6737
6738 /* 2. */
6739 0x00,
6740
6741 /* 3. A valid epoch date, 1400000000; Tue, 13 May 2014 16:53:20 GMT */
6742 0x1a, 0x53, 0x72, 0x4E, 0x00,
6743
6744 /* 4. */
6745 0x62, 'h', 'i',
6746};
6747
6748
Laurence Lundbladee3553422020-05-02 11:11:17 -07006749int32_t CBORSequenceDecodeTests(void)
6750{
6751 QCBORDecodeContext DCtx;
Laurence Lundblade87495732021-02-26 10:05:55 -07006752 QCBORItem Item;
6753 QCBORError uCBORError;
6754 size_t uConsumed;
Laurence Lundbladee3553422020-05-02 11:11:17 -07006755
6756 // --- Test a sequence with extra bytes ---
Laurence Lundblade9b334962020-08-27 10:55:53 -07006757
Laurence Lundbladee3553422020-05-02 11:11:17 -07006758 QCBORDecode_Init(&DCtx,
Laurence Lundblade37286c02022-09-03 10:05:02 -07006759 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSequenceTestInput),
Laurence Lundbladee3553422020-05-02 11:11:17 -07006760 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade9b334962020-08-27 10:55:53 -07006761
Laurence Lundblade37286c02022-09-03 10:05:02 -07006762 // Get 1.
Laurence Lundbladee3553422020-05-02 11:11:17 -07006763 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
6764 if(uCBORError != QCBOR_SUCCESS) {
6765 return 1;
6766 }
Laurence Lundblade37286c02022-09-03 10:05:02 -07006767 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ) {
Laurence Lundbladee3553422020-05-02 11:11:17 -07006768 return 2;
6769 }
Laurence Lundbladec7114722020-08-13 05:11:40 -07006770
Laurence Lundblade87495732021-02-26 10:05:55 -07006771 uCBORError = QCBORDecode_PartialFinish(&DCtx, &uConsumed);
6772 if(uCBORError != QCBOR_ERR_EXTRA_BYTES ||
Laurence Lundblade37286c02022-09-03 10:05:02 -07006773 uConsumed != 11) {
6774 return 102;
6775 }
6776
6777 // Get 2.
6778 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
6779 if(uCBORError != QCBOR_SUCCESS) {
6780 return 66;
6781 }
6782
6783 uCBORError = QCBORDecode_PartialFinish(&DCtx, &uConsumed);
6784 if(uCBORError != QCBOR_ERR_EXTRA_BYTES ||
Laurence Lundblade87495732021-02-26 10:05:55 -07006785 uConsumed != 12) {
6786 return 102;
6787 }
6788
Laurence Lundblade37286c02022-09-03 10:05:02 -07006789 // Get 3.
Laurence Lundbladec7114722020-08-13 05:11:40 -07006790 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundbladee3553422020-05-02 11:11:17 -07006791 if(uCBORError != QCBOR_SUCCESS) {
6792 return 2;
6793 }
Laurence Lundblade37286c02022-09-03 10:05:02 -07006794 if(Item.uDataType != QCBOR_TYPE_INT64) {
Laurence Lundbladee3553422020-05-02 11:11:17 -07006795 return 3;
6796 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006797
Laurence Lundbladee3553422020-05-02 11:11:17 -07006798 // A sequence can have stuff at the end that may
6799 // or may not be valid CBOR. The protocol decoder knows
6800 // when to stop by definition of the protocol, not
6801 // when the top-level map or array is ended.
6802 // Finish still has to be called to know that
6803 // maps and arrays (if there were any) were closed
6804 // off correctly. When called like this it
6805 // must return the error QCBOR_ERR_EXTRA_BYTES.
6806 uCBORError = QCBORDecode_Finish(&DCtx);
6807 if(uCBORError != QCBOR_ERR_EXTRA_BYTES) {
6808 return 4;
6809 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006810
Laurence Lundbladee3553422020-05-02 11:11:17 -07006811 // --- Test an empty input ----
6812 uint8_t empty[1];
6813 UsefulBufC Empty = {empty, 0};
6814 QCBORDecode_Init(&DCtx,
6815 Empty,
6816 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade9b334962020-08-27 10:55:53 -07006817
Laurence Lundbladee3553422020-05-02 11:11:17 -07006818 uCBORError = QCBORDecode_Finish(&DCtx);
6819 if(uCBORError != QCBOR_SUCCESS) {
6820 return 5;
6821 }
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07006822
6823
Laurence Lundbladee3553422020-05-02 11:11:17 -07006824 // --- Sequence with unclosed indefinite length array ---
6825 static const uint8_t xx[] = {0x01, 0x9f, 0x02};
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07006826
Laurence Lundbladee3553422020-05-02 11:11:17 -07006827 QCBORDecode_Init(&DCtx,
6828 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(xx),
6829 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07006830
Laurence Lundbladee3553422020-05-02 11:11:17 -07006831 // Get the first item
6832 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
6833 if(uCBORError != QCBOR_SUCCESS) {
6834 return 7;
6835 }
6836 if(Item.uDataType != QCBOR_TYPE_INT64) {
6837 return 8;
6838 }
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07006839
Laurence Lundbladee3553422020-05-02 11:11:17 -07006840 // Get a second item
6841 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08006842#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundbladee3553422020-05-02 11:11:17 -07006843 if(uCBORError != QCBOR_SUCCESS) {
6844 return 9;
6845 }
6846 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
6847 return 10;
6848 }
6849
6850 // Try to finish before consuming all bytes to confirm
6851 // that the still-open error is returned.
6852 uCBORError = QCBORDecode_Finish(&DCtx);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006853 if(uCBORError != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
Laurence Lundbladee3553422020-05-02 11:11:17 -07006854 return 11;
6855 }
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08006856#else /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
6857 if(uCBORError != QCBOR_ERR_INDEF_LEN_ARRAYS_DISABLED) {
6858 return 20;
6859 }
6860#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
Laurence Lundbladee3553422020-05-02 11:11:17 -07006861
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07006862
Laurence Lundbladee3553422020-05-02 11:11:17 -07006863 // --- Sequence with a closed indefinite length array ---
6864 static const uint8_t yy[] = {0x01, 0x9f, 0xff};
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07006865
Laurence Lundbladee3553422020-05-02 11:11:17 -07006866 QCBORDecode_Init(&DCtx,
6867 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(yy),
6868 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07006869
Laurence Lundbladee3553422020-05-02 11:11:17 -07006870 // Get the first item
6871 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
6872 if(uCBORError != QCBOR_SUCCESS) {
6873 return 12;
6874 }
6875 if(Item.uDataType != QCBOR_TYPE_INT64) {
6876 return 13;
6877 }
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07006878
Laurence Lundbladee3553422020-05-02 11:11:17 -07006879 // Get a second item
6880 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08006881#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
6882
Laurence Lundbladee3553422020-05-02 11:11:17 -07006883 if(uCBORError != QCBOR_SUCCESS) {
6884 return 14;
6885 }
6886 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
6887 return 15;
6888 }
6889
6890 // Try to finish before consuming all bytes to confirm
6891 // that the still-open error is returned.
6892 uCBORError = QCBORDecode_Finish(&DCtx);
6893 if(uCBORError != QCBOR_SUCCESS) {
6894 return 16;
6895 }
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08006896#else /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
6897 if(uCBORError != QCBOR_ERR_INDEF_LEN_ARRAYS_DISABLED) {
6898 return 20;
6899 }
6900#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
Laurence Lundbladee3553422020-05-02 11:11:17 -07006901
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07006902
Laurence Lundbladee3553422020-05-02 11:11:17 -07006903 return 0;
6904}
6905
Laurence Lundbladee15326f2020-06-15 15:50:23 -07006906
Laurence Lundblade70ecead2020-06-15 19:40:06 -07006907
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03006908int32_t IntToTests(void)
Laurence Lundbladee15326f2020-06-15 15:50:23 -07006909{
6910 int nErrCode;
6911 int32_t n32;
6912 int16_t n16;
6913 int8_t n8;
6914 uint32_t u32;
6915 uint16_t u16;
6916 uint8_t u8;
6917 uint64_t u64;
6918
6919 nErrCode = QCBOR_Int64ToInt32(1, &n32);
6920 if(nErrCode == -1 || n32 != 1) {
6921 return 1;
6922 }
6923
6924 nErrCode = QCBOR_Int64ToInt32((int64_t)INT32_MAX, &n32);
6925 if(nErrCode == -1 || n32 != INT32_MAX) {
6926 return 2;
6927 }
6928
6929 nErrCode = QCBOR_Int64ToInt32((int64_t)INT32_MIN, &n32);
6930 if(nErrCode == -1 || n32 != INT32_MIN) {
6931 return 3;
6932 }
6933
6934 nErrCode = QCBOR_Int64ToInt32(((int64_t)INT32_MAX)+1, &n32);
6935 if(nErrCode != -1) {
6936 return 4;
6937 }
6938
6939 nErrCode = QCBOR_Int64ToInt32(((int64_t)INT32_MIN)-1, &n32);
6940 if(nErrCode != -1) {
6941 return 5;
6942 }
6943
6944
6945 nErrCode = QCBOR_Int64ToInt16((int64_t)INT16_MAX, &n16);
6946 if(nErrCode == -1 || n16 != INT16_MAX) {
6947 return 6;
6948 }
6949
6950 nErrCode = QCBOR_Int64ToInt16((int64_t)INT16_MIN, &n16);
6951 if(nErrCode == -1 || n16 != INT16_MIN) {
6952 return 7;
6953 }
6954
6955 nErrCode = QCBOR_Int64ToInt16(1, &n16);
6956 if(nErrCode == -1 || n16 != 1) {
6957 return 8;
6958 }
6959
6960 nErrCode = QCBOR_Int64ToInt16(((int64_t)INT16_MAX)+1, &n16);
6961 if(nErrCode != -1) {
6962 return 9;
6963 }
6964
6965 nErrCode = QCBOR_Int64ToInt16(((int64_t)INT16_MIN)-1, &n16);
6966 if(nErrCode != -1) {
6967 return 10;
6968 }
6969
6970
6971 nErrCode = QCBOR_Int64ToInt8(1, &n8);
6972 if(nErrCode == -1 || n8 != 1) {
6973 return 11;
6974 }
6975
6976 nErrCode = QCBOR_Int64ToInt8((int64_t)INT8_MAX, &n8);
6977 if(nErrCode == -1 || n8 != INT8_MAX) {
6978 return 12;
6979 }
6980
6981 nErrCode = QCBOR_Int64ToInt8((int64_t)INT8_MIN, &n8);
6982 if(nErrCode == -1 || n8 != INT8_MIN) {
6983 return 13;
6984 }
6985
6986 nErrCode = QCBOR_Int64ToInt8(((int64_t)INT8_MAX)+1, &n8);
6987 if(nErrCode != -1) {
6988 return 14;
6989 }
6990
6991 nErrCode = QCBOR_Int64ToInt8(((int64_t)INT8_MIN)-1, &n8);
6992 if(nErrCode != -1) {
6993 return 15;
6994 }
6995
6996
6997 nErrCode = QCBOR_Int64ToUInt32(1, &u32);
6998 if(nErrCode == -1 || u32 != 1) {
6999 return 16;
7000 }
7001
7002 nErrCode = QCBOR_Int64ToUInt32((int64_t)UINT32_MAX, &u32);
7003 if(nErrCode == -1 || u32 != UINT32_MAX) {
7004 return 17;
7005 }
7006
7007 nErrCode = QCBOR_Int64ToUInt32((int64_t)0, &u32);
7008 if(nErrCode == -1 || u32 != 0) {
7009 return 18;
7010 }
7011
7012 nErrCode = QCBOR_Int64ToUInt32(((int64_t)UINT32_MAX)+1, &u32);
7013 if(nErrCode != -1) {
7014 return 19;
7015 }
7016
7017 nErrCode = QCBOR_Int64ToUInt32((int64_t)-1, &u32);
7018 if(nErrCode != -1) {
7019 return 20;
7020 }
7021
7022
7023 nErrCode = QCBOR_Int64UToInt16((int64_t)UINT16_MAX, &u16);
7024 if(nErrCode == -1 || u16 != UINT16_MAX) {
7025 return 21;
7026 }
7027
7028 nErrCode = QCBOR_Int64UToInt16((int64_t)0, &u16);
7029 if(nErrCode == -1 || u16 != 0) {
7030 return 22;
7031 }
7032
7033 nErrCode = QCBOR_Int64UToInt16(1, &u16);
7034 if(nErrCode == -1 || u16 != 1) {
7035 return 23;
7036 }
7037
7038 nErrCode = QCBOR_Int64UToInt16(((int64_t)UINT16_MAX)+1, &u16);
7039 if(nErrCode != -1) {
7040 return 24;
7041 }
7042
7043 nErrCode = QCBOR_Int64UToInt16((int64_t)-1, &u16);
7044 if(nErrCode != -1) {
7045 return 25;
7046 }
7047
7048
7049 nErrCode = QCBOR_Int64ToUInt8((int64_t)UINT8_MAX, &u8);
7050 if(nErrCode == -1 || u8 != UINT8_MAX) {
7051 return 26;
7052 }
7053
7054 nErrCode = QCBOR_Int64ToUInt8((int64_t)0, &u8);
7055 if(nErrCode == -1 || u8 != 0) {
7056 return 27;
7057 }
7058
7059 nErrCode = QCBOR_Int64ToUInt8(1, &u8);
7060 if(nErrCode == -1 || u8 != 1) {
7061 return 28;
7062 }
7063
7064 nErrCode = QCBOR_Int64ToUInt8(((int64_t)UINT16_MAX)+1, &u8);
7065 if(nErrCode != -1) {
7066 return 29;
7067 }
7068
7069 nErrCode = QCBOR_Int64ToUInt8((int64_t)-1, &u8);
7070 if(nErrCode != -1) {
7071 return 30;
7072 }
7073
7074
7075 nErrCode = QCBOR_Int64ToUInt64(1, &u64);
7076 if(nErrCode == -1 || u64 != 1) {
7077 return 31;
7078 }
7079
7080 nErrCode = QCBOR_Int64ToUInt64(INT64_MAX, &u64);
7081 if(nErrCode == -1 || u64 != INT64_MAX) {
7082 return 32;
7083 }
7084
7085 nErrCode = QCBOR_Int64ToUInt64((int64_t)0, &u64);
7086 if(nErrCode == -1 || u64 != 0) {
7087 return 33;
7088 }
7089
7090 nErrCode = QCBOR_Int64ToUInt64((int64_t)-1, &u64);
7091 if(nErrCode != -1) {
7092 return 34;
7093 }
7094
7095 return 0;
7096}
7097
Laurence Lundblade0750fc42020-06-20 21:02:34 -07007098
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07007099
7100
Laurence Lundblade0750fc42020-06-20 21:02:34 -07007101/*
7102A sequence with
7103 A wrapping bstr
7104 containing a map
7105 1
7106 2
7107 A wrapping bstr
7108 containing an array
7109 3
7110 wrapping bstr
7111 4
7112 5
7113 6
7114 array
7115 7
7116 8
Laurence Lundblade0750fc42020-06-20 21:02:34 -07007117 */
7118
Laurence Lundblade55013642020-09-23 05:39:22 -07007119static UsefulBufC EncodeBstrWrapTestData(UsefulBuf OutputBuffer)
Laurence Lundblade0750fc42020-06-20 21:02:34 -07007120{
Laurence Lundblade55013642020-09-23 05:39:22 -07007121 UsefulBufC Encoded;
Laurence Lundblade0750fc42020-06-20 21:02:34 -07007122 QCBOREncodeContext EC;
Laurence Lundblade55013642020-09-23 05:39:22 -07007123 QCBORError uErr;
Laurence Lundblade0750fc42020-06-20 21:02:34 -07007124
Laurence Lundblade55013642020-09-23 05:39:22 -07007125 QCBOREncode_Init(&EC, OutputBuffer);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07007126
7127 QCBOREncode_BstrWrap(&EC);
7128 QCBOREncode_OpenMap(&EC);
7129 QCBOREncode_AddInt64ToMapN(&EC, 100, 1);
7130 QCBOREncode_AddInt64ToMapN(&EC, 200, 2);
7131 QCBOREncode_CloseMap(&EC);
7132 QCBOREncode_BstrWrap(&EC);
7133 QCBOREncode_OpenArray(&EC);
7134 QCBOREncode_AddInt64(&EC, 3);
7135 QCBOREncode_BstrWrap(&EC);
7136 QCBOREncode_AddInt64(&EC, 4);
7137 QCBOREncode_CloseBstrWrap(&EC, NULL);
7138 QCBOREncode_AddInt64(&EC, 5);
7139 QCBOREncode_CloseArray(&EC);
7140 QCBOREncode_CloseBstrWrap(&EC, NULL);
7141 QCBOREncode_AddInt64(&EC, 6);
7142 QCBOREncode_CloseBstrWrap(&EC, NULL);
7143 QCBOREncode_OpenArray(&EC);
7144 QCBOREncode_AddInt64(&EC, 7);
7145 QCBOREncode_AddInt64(&EC, 8);
7146 QCBOREncode_CloseArray(&EC);
7147
7148 uErr = QCBOREncode_Finish(&EC, &Encoded);
Laurence Lundblade40a04322020-06-27 22:52:52 -07007149 if(uErr) {
7150 Encoded = NULLUsefulBufC;
7151 }
Laurence Lundblade0750fc42020-06-20 21:02:34 -07007152
7153 return Encoded;
7154}
7155
Laurence Lundbladecc7da412020-12-27 00:09:07 -08007156/* h'FF' */
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08007157static const uint8_t spBreakInByteString[] = {
7158 0x41, 0xff
7159};
7160
Laurence Lundblade0750fc42020-06-20 21:02:34 -07007161
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03007162int32_t EnterBstrTest(void)
Laurence Lundblade0750fc42020-06-20 21:02:34 -07007163{
Laurence Lundblade8510f8c2020-12-01 11:31:16 -08007164 UsefulBuf_MAKE_STACK_UB(OutputBuffer, 100);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07007165
7166 QCBORDecodeContext DC;
7167
Laurence Lundblade55013642020-09-23 05:39:22 -07007168 QCBORDecode_Init(&DC, EncodeBstrWrapTestData(OutputBuffer), 0);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07007169
Laurence Lundblade55013642020-09-23 05:39:22 -07007170 int64_t n1, n2, n3, n4, n5, n6, n7, n8;
Laurence Lundblade0750fc42020-06-20 21:02:34 -07007171
7172
Laurence Lundblade9b334962020-08-27 10:55:53 -07007173 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07007174 QCBORDecode_EnterMap(&DC, NULL);
Laurence Lundblade55013642020-09-23 05:39:22 -07007175 QCBORDecode_GetInt64InMapN(&DC, 100, &n1);
7176 QCBORDecode_GetInt64InMapN(&DC, 200, &n2);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07007177 QCBORDecode_ExitMap(&DC);
Laurence Lundblade9b334962020-08-27 10:55:53 -07007178 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07007179 QCBORDecode_EnterArray(&DC, NULL);
Laurence Lundblade55013642020-09-23 05:39:22 -07007180 QCBORDecode_GetInt64(&DC, &n3);
Laurence Lundblade9b334962020-08-27 10:55:53 -07007181 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
Laurence Lundblade55013642020-09-23 05:39:22 -07007182 QCBORDecode_GetInt64(&DC, &n4);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07007183 QCBORDecode_ExitBstrWrapped(&DC);
Laurence Lundblade55013642020-09-23 05:39:22 -07007184 QCBORDecode_GetInt64(&DC, &n5);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07007185 QCBORDecode_ExitArray(&DC);
7186 QCBORDecode_ExitBstrWrapped(&DC);
Laurence Lundblade55013642020-09-23 05:39:22 -07007187 QCBORDecode_GetInt64(&DC, &n6);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07007188 QCBORDecode_ExitBstrWrapped(&DC);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07007189 QCBORDecode_EnterArray(&DC, NULL);
Laurence Lundblade55013642020-09-23 05:39:22 -07007190 QCBORDecode_GetInt64(&DC, &n7);
7191 QCBORDecode_GetInt64(&DC, &n8);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07007192 QCBORDecode_ExitArray(&DC);
7193
7194 QCBORError uErr = QCBORDecode_Finish(&DC);
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08007195 if(uErr) {
7196 return (int32_t)uErr;
7197 }
Laurence Lundblade0750fc42020-06-20 21:02:34 -07007198
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08007199
7200 /* Enter and exit byte string wrapped CBOR that is bad. It has just a break.
7201 * Successful because no items are fetched from byte string.
7202 */
7203 QCBORDecode_Init(&DC,
7204 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBreakInByteString),
7205 0);
7206 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
7207 uErr = QCBORDecode_GetError(&DC);
7208 if(uErr) {
7209 return 100 + (int32_t)uErr;
7210 }
7211
7212 QCBORDecode_ExitBstrWrapped(&DC);
7213 uErr = QCBORDecode_GetError(&DC);
7214 if(uErr) {
7215 return 200 + (int32_t)uErr;
7216 }
7217
7218 /* Try to get item that is a break out of a byte string wrapped CBOR.
7219 * It fails because there should be no break.
7220 */
7221 QCBORDecode_Init(&DC,
7222 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBreakInByteString),
7223 0);
7224 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
7225 QCBORItem Item;
7226 uErr = QCBORDecode_GetNext(&DC, &Item);
7227 if(uErr != QCBOR_ERR_BAD_BREAK) {
7228 return 300 + (int32_t)uErr;
7229 }
7230
7231 return 0;
Laurence Lundblade0750fc42020-06-20 21:02:34 -07007232}
Laurence Lundblade37f46e52020-08-04 03:32:14 -07007233
7234
7235
7236
7237static const uint8_t spTaggedTypes[] = {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007238 0xb2,
Laurence Lundblade37f46e52020-08-04 03:32:14 -07007239
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07007240 // Date string
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007241 0x00,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07007242 0xc0, 0x74, 0x32, 0x30, 0x30, 0x33, 0x2D, 0x31, 0x32, 0x2D,
7243 0x31, 0x33, 0x54, 0x31, 0x38, 0x3A, 0x33, 0x30, 0x3A, 0x30,
7244 0x32, 0x5A,
Laurence Lundblade37f46e52020-08-04 03:32:14 -07007245
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007246 0x01,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07007247 0x74, 0x32, 0x30, 0x30, 0x33, 0x2D, 0x31, 0x32, 0x2D, 0x31,
7248 0x33, 0x54, 0x31, 0x38, 0x3A, 0x33, 0x30, 0x3A, 0x30, 0x32,
7249 0x5A,
Laurence Lundblade37f46e52020-08-04 03:32:14 -07007250
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07007251 // Bignum
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007252 10,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07007253 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
7254 0x09, 0x10,
Laurence Lundblade37f46e52020-08-04 03:32:14 -07007255
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007256 11,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07007257 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
7258 0x09, 0x10,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007259
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07007260 // URL
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007261 20,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07007262 0xd8, 0x20, 0x6f, 0x68, 0x74, 0x74, 0x70, 0x3A, 0x2F, 0x2F,
7263 0x63, 0x62, 0x6F, 0x72, 0x2E, 0x6D, 0x65, 0x2F,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007264
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07007265 21,
7266 0x6f, 0x68, 0x74, 0x74, 0x70, 0x3A, 0x2F, 0x2F, 0x63, 0x62,
7267 0x6F, 0x72, 0x2E, 0x6D, 0x65, 0x2F,
7268
7269 // B64
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007270 0x18, 0x1e,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07007271 0xd8, 0x22, 0x6c, 0x63, 0x47, 0x78, 0x6C, 0x59, 0x58, 0x4E,
7272 0x31, 0x63, 0x6D, 0x55, 0x75,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007273
7274 0x18, 0x1f,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07007275 0x6c, 0x63, 0x47, 0x78, 0x6C, 0x59, 0x58, 0x4E, 0x31, 0x63,
7276 0x6D, 0x55, 0x75,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007277
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07007278 // B64URL
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007279 0x18, 0x28,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07007280 0xd8, 0x21, 0x6c, 0x63, 0x47, 0x78, 0x6C, 0x59, 0x58, 0x4E,
7281 0x31, 0x63, 0x6D, 0x55, 0x75,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007282
7283 0x18, 0x29,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07007284 0x6c, 0x63, 0x47, 0x78, 0x6C, 0x59, 0x58, 0x4E, 0x31, 0x63,
7285 0x6D, 0x55, 0x75,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007286
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07007287 // Regex
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007288 0x18, 0x32,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07007289 0xd8, 0x23, 0x68, 0x31, 0x30, 0x30, 0x5C, 0x73, 0x2A, 0x6D,
7290 0x6B,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007291
7292 0x18, 0x33,
7293 0x68, 0x31, 0x30, 0x30, 0x5C, 0x73, 0x2A, 0x6D, 0x6B,
7294
7295 // MIME
7296 0x18, 0x3c,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07007297 0xd8, 0x24, 0x72, 0x4D, 0x49, 0x4D, 0x45, 0x2D, 0x56, 0x65,
7298 0x72, 0x73, 0x69, 0x6F, 0x6E, 0x3A, 0x20, 0x31, 0x2E, 0x30,
7299 0x0A,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007300
7301 0x18, 0x3d,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07007302 0x72, 0x4D, 0x49, 0x4D, 0x45, 0x2D, 0x56, 0x65, 0x72, 0x73,
7303 0x69, 0x6F, 0x6E, 0x3A, 0x20, 0x31, 0x2E, 0x30, 0x0A,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007304
7305 0x18, 0x3e,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07007306 0xd9, 0x01, 0x01, 0x52, 0x4D, 0x49, 0x4D, 0x45, 0x2D, 0x56,
7307 0x65, 0x72, 0x73, 0x69, 0x6F, 0x6E, 0x3A, 0x20, 0x31, 0x2E,
7308 0x30, 0x0A,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007309
7310 0x18, 0x3f,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07007311 0x52, 0x4D, 0x49, 0x4D, 0x45, 0x2D, 0x56, 0x65, 0x72, 0x73,
7312 0x69, 0x6F, 0x6E, 0x3A, 0x20, 0x31, 0x2E, 0x30, 0x0A,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007313
7314 // UUID
7315 0x18, 0x46,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07007316 0xd8, 0x25, 0x50, 0x53, 0x4D, 0x41, 0x52, 0x54, 0x43, 0x53,
7317 0x4C, 0x54, 0x54, 0x43, 0x46, 0x49, 0x43, 0x41, 0x32,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007318
7319 0x18, 0x47,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07007320 0x50, 0x53, 0x4D, 0x41, 0x52, 0x54, 0x43, 0x53, 0x4C, 0x54,
7321 0x54, 0x43, 0x46, 0x49, 0x43, 0x41, 0x32
Laurence Lundblade37f46e52020-08-04 03:32:14 -07007322};
7323
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03007324int32_t DecodeTaggedTypeTests(void)
Laurence Lundblade37f46e52020-08-04 03:32:14 -07007325{
7326 QCBORDecodeContext DC;
7327 QCBORError uErr;
7328
7329 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTaggedTypes), 0);
7330
7331 UsefulBufC String;
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007332 bool bNeg;
Laurence Lundblade37f46e52020-08-04 03:32:14 -07007333
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07007334 QCBORDecode_EnterMap(&DC, NULL);
Laurence Lundblade9b334962020-08-27 10:55:53 -07007335 QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007336 QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &String);
Laurence Lundblade37f46e52020-08-04 03:32:14 -07007337 if(QCBORDecode_GetError(&DC) != QCBOR_SUCCESS) {
7338 return 1;
7339 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07007340 QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade37f46e52020-08-04 03:32:14 -07007341 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_UNEXPECTED_TYPE) {
7342 return 2;
7343 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07007344 QCBORDecode_GetDateStringInMapN(&DC, 1, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade37f46e52020-08-04 03:32:14 -07007345 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_UNEXPECTED_TYPE) {
7346 return 3;
7347 }
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007348 QCBORDecode_GetDateStringInMapN(&DC, 1, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &String);
Laurence Lundblade9b334962020-08-27 10:55:53 -07007349 QCBORDecode_GetDateStringInMapN(&DC, 1, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade37f46e52020-08-04 03:32:14 -07007350 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
7351 return 4;
7352 }
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007353 QCBORDecode_GetDateStringInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07007354 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007355 return 5;
7356 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07007357
Laurence Lundblade9b334962020-08-27 10:55:53 -07007358 QCBORDecode_GetBignumInMapN(&DC, 10, QCBOR_TAG_REQUIREMENT_TAG, &String, &bNeg);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007359 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
7360 bNeg != false) {
7361 return 10;
7362 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07007363 QCBORDecode_GetBignumInMapN(&DC, 11, QCBOR_TAG_REQUIREMENT_TAG, &String, &bNeg);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007364 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
7365 bNeg != true) {
7366 return 11;
7367 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07007368 QCBORDecode_GetBignumInMapN(&DC, 11, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bNeg);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007369 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_UNEXPECTED_TYPE) {
7370 return 12;
7371 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07007372 QCBORDecode_GetBignumInMapN(&DC, 14, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bNeg);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07007373 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007374 return 13;
7375 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07007376 QCBORDecode_GetBignumInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bNeg);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07007377 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007378 return 14;
7379 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07007380
Laurence Lundblade9b334962020-08-27 10:55:53 -07007381 QCBORDecode_GetURIInMapN(&DC, 20, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007382 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
7383 return 20;
7384 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07007385 QCBORDecode_GetURIInMapN(&DC, 21, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007386 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
7387 return 21;
7388 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07007389 QCBORDecode_GetURIInMapN(&DC, 22, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07007390 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007391 return 22;
7392 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07007393 QCBORDecode_GetURIInMapSZ(&DC, "xxx", 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 23;
7396 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07007397
Laurence Lundblade24bd7e12021-01-09 00:05:11 -08007398#ifndef QCBOR_DISABLE_UNCOMMON_TAGS
Laurence Lundblade9b334962020-08-27 10:55:53 -07007399 QCBORDecode_GetB64InMapN(&DC, 30, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007400 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
7401 return 30;
7402 }
Laurence Lundblade24bd7e12021-01-09 00:05:11 -08007403#endif
Laurence Lundblade9b334962020-08-27 10:55:53 -07007404 QCBORDecode_GetB64InMapN(&DC, 31, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007405 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
7406 return 31;
7407 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07007408 QCBORDecode_GetB64InMapN(&DC, 32, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07007409 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007410 return 32;
7411 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07007412 QCBORDecode_GetB64InMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07007413 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007414 return 33;
7415 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07007416
Laurence Lundblade24bd7e12021-01-09 00:05:11 -08007417#ifndef QCBOR_DISABLE_UNCOMMON_TAGS
Laurence Lundblade9b334962020-08-27 10:55:53 -07007418 QCBORDecode_GetB64URLInMapN(&DC, 40, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007419 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
7420 return 40;
7421 }
Laurence Lundblade24bd7e12021-01-09 00:05:11 -08007422#endif
Laurence Lundblade9b334962020-08-27 10:55:53 -07007423 QCBORDecode_GetB64URLInMapN(&DC, 41, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007424 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
7425 return 41;
7426 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07007427 QCBORDecode_GetB64URLInMapN(&DC, 42, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07007428 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007429 return 42;
7430 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07007431 QCBORDecode_GetB64URLInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07007432 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007433 return 43;
7434 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07007435
Laurence Lundblade24bd7e12021-01-09 00:05:11 -08007436#ifndef QCBOR_DISABLE_UNCOMMON_TAGS
Laurence Lundblade9b334962020-08-27 10:55:53 -07007437 QCBORDecode_GetRegexInMapN(&DC, 50, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007438 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
7439 return 50;
7440 }
Laurence Lundblade24bd7e12021-01-09 00:05:11 -08007441#endif
Laurence Lundblade9b334962020-08-27 10:55:53 -07007442 QCBORDecode_GetRegexInMapN(&DC, 51, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007443 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
7444 return 51;
7445 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07007446 QCBORDecode_GetRegexInMapN(&DC, 52, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07007447 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007448 return 52;
7449 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07007450 QCBORDecode_GetRegexInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07007451 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007452 return 53;
7453 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07007454
Laurence Lundblade24bd7e12021-01-09 00:05:11 -08007455#ifndef QCBOR_DISABLE_UNCOMMON_TAGS
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007456 // MIME
7457 bool bIsNot7Bit;
Laurence Lundblade9b334962020-08-27 10:55:53 -07007458 QCBORDecode_GetMIMEMessageInMapN(&DC, 60, QCBOR_TAG_REQUIREMENT_TAG, &String, &bIsNot7Bit);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007459 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
7460 bIsNot7Bit == true) {
7461 return 60;
7462 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07007463 QCBORDecode_GetMIMEMessageInMapN(&DC, 61, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bIsNot7Bit);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007464 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
7465 bIsNot7Bit == true) {
7466 return 61;
7467 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07007468 QCBORDecode_GetMIMEMessageInMapN(&DC, 62, QCBOR_TAG_REQUIREMENT_TAG, &String, &bIsNot7Bit);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007469 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
7470 bIsNot7Bit == false) {
7471 return 62;
7472 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07007473 QCBORDecode_GetMIMEMessageInMapN(&DC, 63, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bIsNot7Bit);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007474 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
7475 bIsNot7Bit == false) {
7476 return 63;
7477 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07007478 QCBORDecode_GetMIMEMessageInMapN(&DC, 64, QCBOR_TAG_REQUIREMENT_TAG, &String, &bNeg);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07007479 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007480 return 64;
7481 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07007482 QCBORDecode_GetMIMEMessageInMapSZ(&DC, "zzz", QCBOR_TAG_REQUIREMENT_TAG, &String, &bNeg);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07007483 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007484 return 65;
7485 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07007486
Laurence Lundblade24bd7e12021-01-09 00:05:11 -08007487
Laurence Lundblade9b334962020-08-27 10:55:53 -07007488 QCBORDecode_GetBinaryUUIDInMapN(&DC, 70, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007489 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
7490 return 70;
7491 }
Laurence Lundblade24bd7e12021-01-09 00:05:11 -08007492#endif /* #ifndef QCBOR_DISABLE_UNCOMMON_TAGS */
7493
Laurence Lundblade9b334962020-08-27 10:55:53 -07007494 QCBORDecode_GetBinaryUUIDInMapN(&DC, 71, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007495 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
7496 return 71;
7497 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07007498 QCBORDecode_GetBinaryUUIDInMapN(&DC, 72, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07007499 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007500 return 72;
7501 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07007502 QCBORDecode_GetBinaryUUIDInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07007503 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007504 return 73;
7505 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07007506
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07007507 // Improvement: add some more error test cases
7508
Laurence Lundblade37f46e52020-08-04 03:32:14 -07007509 QCBORDecode_ExitMap(&DC);
7510
7511 uErr = QCBORDecode_Finish(&DC);
7512 if(uErr != QCBOR_SUCCESS) {
7513 return 100;
7514 }
7515
7516 return 0;
7517}
Laurence Lundbladea4308a82020-10-03 18:08:57 -07007518
7519
7520
7521
7522/*
Laurence Lundbladecc7da412020-12-27 00:09:07 -08007523 [
7524 "aaaaaaaaaa",
7525 {}
7526 ]
Laurence Lundbladea4308a82020-10-03 18:08:57 -07007527 */
7528static const uint8_t spTooLarge1[] = {
7529 0x9f,
7530 0x6a, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61,
7531 0xa0,
7532 0xff
7533};
7534
7535/*
Laurence Lundbladecc7da412020-12-27 00:09:07 -08007536 [
7537 {
7538 0: "aaaaaaaaaa"
7539 }
7540 ]
Laurence Lundbladea4308a82020-10-03 18:08:57 -07007541 */
7542static const uint8_t spTooLarge2[] = {
7543 0x9f,
7544 0xa1,
7545 0x00,
7546 0x6a, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61,
7547 0xff
7548};
7549
7550/*
Laurence Lundbladecc7da412020-12-27 00:09:07 -08007551 h'A1006A61616161616161616161'
Laurence Lundbladea4308a82020-10-03 18:08:57 -07007552
Laurence Lundbladecc7da412020-12-27 00:09:07 -08007553 {
7554 0: "aaaaaaaaaa"
7555 }
Laurence Lundbladea4308a82020-10-03 18:08:57 -07007556 */
7557static const uint8_t spTooLarge3[] = {
7558 0x4d,
7559 0xa1,
7560 0x00,
7561 0x6a, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61,
7562};
7563
7564int32_t TooLargeInputTest(void)
7565{
7566 QCBORDecodeContext DC;
7567 QCBORError uErr;
7568 UsefulBufC String;
7569
7570 // These tests require a build with QCBOR_MAX_DECODE_INPUT_SIZE set
7571 // to 10 There's not really any way to test this error
7572 // condition. The error condition is not complex, so setting
7573 // QCBOR_MAX_DECODE_INPUT_SIZE gives an OK test.
7574
7575 // The input CBOR is only too large because the
7576 // QCBOR_MAX_DECODE_INPUT_SIZE is 10.
7577 //
7578 // This test is disabled for the normal test runs because of the
7579 // special build requirement.
7580
7581
7582 // Tests the start of a map being too large
7583 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTooLarge1), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07007584 QCBORDecode_EnterArray(&DC, NULL);
Laurence Lundbladea4308a82020-10-03 18:08:57 -07007585 QCBORDecode_GetTextString(&DC, &String);
7586 uErr = QCBORDecode_GetError(&DC);
7587 if(uErr != QCBOR_SUCCESS) {
7588 return 1;
7589 }
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07007590 QCBORDecode_EnterMap(&DC, NULL);
Laurence Lundbladea4308a82020-10-03 18:08:57 -07007591 uErr = QCBORDecode_GetError(&DC);
7592 if(uErr != QCBOR_ERR_INPUT_TOO_LARGE) {
7593 return 2;
7594 }
7595
7596 // Tests the end of a map being too large
7597 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTooLarge2), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07007598 QCBORDecode_EnterArray(&DC, NULL);
7599 QCBORDecode_EnterMap(&DC, NULL);
Laurence Lundbladea4308a82020-10-03 18:08:57 -07007600 uErr = QCBORDecode_GetError(&DC);
7601 if(uErr != QCBOR_SUCCESS) {
7602 return 3;
7603 }
7604 QCBORDecode_ExitMap(&DC);
7605 uErr = QCBORDecode_GetError(&DC);
7606 if(uErr != QCBOR_ERR_INPUT_TOO_LARGE) {
7607 return 4;
7608 }
7609
7610 // Tests the entire input CBOR being too large when processing bstr wrapping
7611 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTooLarge3), QCBOR_DECODE_MODE_NORMAL);
7612 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
7613 uErr = QCBORDecode_GetError(&DC);
7614 if(uErr != QCBOR_ERR_INPUT_TOO_LARGE) {
7615 return 5;
7616 }
7617
7618 return 0;
7619}
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07007620
7621
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08007622#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
7623
Laurence Lundblade37286c02022-09-03 10:05:02 -07007624/*
7625 An array of three map entries
7626 1) Indefinite length string label for indefinite lenght byte string
7627 2) Indefinite length string label for an integer
7628 3) Indefinite length string label for an indefinite-length negative big num
7629 */
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07007630static const uint8_t spMapWithIndefLenStrings[] = {
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08007631 0xa3,
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07007632 0x7f, 0x61, 'l', 0x64, 'a', 'b', 'e', 'l' , 0x61, '1', 0xff,
7633 0x5f, 0x42, 0x01, 0x02, 0x43, 0x03, 0x04, 0x05, 0xff,
7634 0x7f, 0x62, 'd', 'y', 0x61, 'm', 0x61, 'o', 0xff,
7635 0x03,
7636 0x7f, 0x62, 'l', 'a', 0x63, 'b', 'e', 'l', 0x61, '2', 0xff,
7637 0xc3,
7638 0x5f, 0x42, 0x00, 0x01, 0x42, 0x00, 0x01, 0x41, 0x01, 0xff,
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07007639};
7640
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03007641int32_t SpiffyIndefiniteLengthStringsTests(void)
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07007642{
7643 QCBORDecodeContext DCtx;
7644
7645 QCBORDecode_Init(&DCtx,
7646 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spMapWithIndefLenStrings),
7647 QCBOR_DECODE_MODE_NORMAL);
7648
Laurence Lundblade8510f8c2020-12-01 11:31:16 -08007649 UsefulBuf_MAKE_STACK_UB(StringBuf, 200);
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07007650 QCBORDecode_SetMemPool(&DCtx, StringBuf, false);
7651
7652 UsefulBufC ByteString;
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07007653 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07007654 QCBORDecode_GetByteStringInMapSZ(&DCtx, "label1", &ByteString);
Laurence Lundblade37286c02022-09-03 10:05:02 -07007655
7656#ifndef QCBOR_DISABLE_TAGS
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07007657 if(QCBORDecode_GetAndResetError(&DCtx)) {
7658 return 1;
7659 }
7660
7661 const uint8_t pExectedBytes[] = {0x01, 0x02, 0x03, 0x04, 0x05};
7662 if(UsefulBuf_Compare(ByteString, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pExectedBytes))) {
7663 return 2;
7664 }
7665
7666 uint64_t uInt;
7667 QCBORDecode_GetUInt64InMapSZ(&DCtx, "dymo", &uInt);
7668 if(QCBORDecode_GetAndResetError(&DCtx)) {
7669 return 3;
7670 }
7671 if(uInt != 3) {
7672 return 4;
7673 }
7674
Máté Tóth-Pálef5f07a2021-09-17 19:31:37 +02007675#ifndef USEFULBUF_DISABLE_ALL_FLOAT
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07007676 double uDouble;
7677 QCBORDecode_GetDoubleConvertAllInMapSZ(&DCtx,
7678 "label2",
7679 0xff,
7680 &uDouble);
Laurence Lundblade37286c02022-09-03 10:05:02 -07007681
Laurence Lundbladeb8e19aa2020-10-07 20:59:11 -07007682#ifndef QCBOR_DISABLE_FLOAT_HW_USE
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07007683 if(QCBORDecode_GetAndResetError(&DCtx)) {
7684 return 5;
7685 }
7686 if(uDouble != -16777474) {
7687 return 6;
7688 }
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08007689#else /* QCBOR_DISABLE_FLOAT_HW_USE */
Laurence Lundbladeb8e19aa2020-10-07 20:59:11 -07007690 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_HW_FLOAT_DISABLED) {
7691 return 7;
7692 }
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08007693#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
Máté Tóth-Pálef5f07a2021-09-17 19:31:37 +02007694#endif /* USEFULBUF_DISABLE_ALL_FLOAT */
Laurence Lundbladeb8e19aa2020-10-07 20:59:11 -07007695
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07007696 QCBORDecode_ExitMap(&DCtx);
7697
7698 if(QCBORDecode_Finish(&DCtx)) {
7699 return 99;
7700 }
7701
Laurence Lundblade37286c02022-09-03 10:05:02 -07007702#else /* QCBOR_DISABLE_TAGS */
7703 /* The big num in the input is a CBOR tag and you can't do
7704 * map lookups in a map with a tag so this test does very little
7705 * when tags are disabled. That is OK, the test coverage is still
7706 * good when they are not.
7707 */
7708 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_TAGS_DISABLED) {
7709 return 1002;
7710 }
7711#endif /*QCBOR_DISABLE_TAGS */
7712
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07007713 return 0;
7714}
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08007715#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
Laurence Lundblade2a26abb2020-11-05 19:06:54 -08007716
7717
Laurence Lundbladecf41c522021-02-20 10:19:07 -07007718/*
7719 * An array of an integer and an array. The second array contains
7720 * a bstr-wrapped map.
7721 *
7722 * [7, [h'A36D6669... (see next lines) 73']]
7723 *
7724 * {"first integer": 42,
7725 * "an array of two strings": ["string1", "string2"],
7726 * "map in a map":
7727 * { "bytes 1": h'78787878',
7728 * "bytes 2": h'79797979',
7729 * "another int": 98,
7730 * "text 2": "lies, damn lies and statistics"
7731 * }
7732 * }
7733 */
Laurence Lundblade2a26abb2020-11-05 19:06:54 -08007734
Laurence Lundbladecf41c522021-02-20 10:19:07 -07007735static const uint8_t pValidWrappedMapEncoded[] = {
7736 0x82, 0x07, 0x81, 0x58, 0x97,
7737 0xa3, 0x6d, 0x66, 0x69, 0x72, 0x73, 0x74, 0x20, 0x69, 0x6e,
7738 0x74, 0x65, 0x67, 0x65, 0x72, 0x18, 0x2a, 0x77, 0x61, 0x6e,
7739 0x20, 0x61, 0x72, 0x72, 0x61, 0x79, 0x20, 0x6f, 0x66, 0x20,
7740 0x74, 0x77, 0x6f, 0x20, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67,
7741 0x73, 0x82, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x31,
7742 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x32, 0x6c, 0x6d,
7743 0x61, 0x70, 0x20, 0x69, 0x6e, 0x20, 0x61, 0x20, 0x6d, 0x61,
7744 0x70, 0xa4, 0x67, 0x62, 0x79, 0x74, 0x65, 0x73, 0x20, 0x31,
7745 0x44, 0x78, 0x78, 0x78, 0x78, 0x67, 0x62, 0x79, 0x74, 0x65,
7746 0x73, 0x20, 0x32, 0x44, 0x79, 0x79, 0x79, 0x79, 0x6b, 0x61,
7747 0x6e, 0x6f, 0x74, 0x68, 0x65, 0x72, 0x20, 0x69, 0x6e, 0x74,
7748 0x18, 0x62, 0x66, 0x74, 0x65, 0x78, 0x74, 0x20, 0x32, 0x78,
7749 0x1e, 0x6c, 0x69, 0x65, 0x73, 0x2c, 0x20, 0x64, 0x61, 0x6d,
7750 0x6e, 0x20, 0x6c, 0x69, 0x65, 0x73, 0x20, 0x61, 0x6e, 0x64,
7751 0x20, 0x73, 0x74, 0x61, 0x74, 0x69, 0x73, 0x74, 0x69, 0x63,
7752 0x73
7753};
7754
7755#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
7756
7757/* As above, but the arrays are indefinite length */
7758static const uint8_t pValidIndefWrappedMapEncoded[] = {
7759 0x9f, 0x07, 0x9f, 0x58, 0x97,
7760 0xa3, 0x6d, 0x66, 0x69, 0x72, 0x73, 0x74, 0x20, 0x69, 0x6e,
7761 0x74, 0x65, 0x67, 0x65, 0x72, 0x18, 0x2a, 0x77, 0x61, 0x6e,
7762 0x20, 0x61, 0x72, 0x72, 0x61, 0x79, 0x20, 0x6f, 0x66, 0x20,
7763 0x74, 0x77, 0x6f, 0x20, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67,
7764 0x73, 0x82, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x31,
7765 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x32, 0x6c, 0x6d,
7766 0x61, 0x70, 0x20, 0x69, 0x6e, 0x20, 0x61, 0x20, 0x6d, 0x61,
7767 0x70, 0xa4, 0x67, 0x62, 0x79, 0x74, 0x65, 0x73, 0x20, 0x31,
7768 0x44, 0x78, 0x78, 0x78, 0x78, 0x67, 0x62, 0x79, 0x74, 0x65,
7769 0x73, 0x20, 0x32, 0x44, 0x79, 0x79, 0x79, 0x79, 0x6b, 0x61,
7770 0x6e, 0x6f, 0x74, 0x68, 0x65, 0x72, 0x20, 0x69, 0x6e, 0x74,
7771 0x18, 0x62, 0x66, 0x74, 0x65, 0x78, 0x74, 0x20, 0x32, 0x78,
7772 0x1e, 0x6c, 0x69, 0x65, 0x73, 0x2c, 0x20, 0x64, 0x61, 0x6d,
7773 0x6e, 0x20, 0x6c, 0x69, 0x65, 0x73, 0x20, 0x61, 0x6e, 0x64,
7774 0x20, 0x73, 0x74, 0x61, 0x74, 0x69, 0x73, 0x74, 0x69, 0x63,
7775 0x73,
7776 0xff, 0xff
7777};
7778#endif
7779
7780
7781static const uint8_t pWithEmptyMap[] = {0x82, 0x18, 0x64, 0xa0};
7782
7783#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
7784static const uint8_t pWithEmptyMapInDef[] = {0x9f, 0x18, 0x64, 0xbf, 0xff, 0xff};
7785#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
7786
7787#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
Laurence Lundblade37286c02022-09-03 10:05:02 -07007788
7789/*
7790An array of one that contains
7791 a byte string that is tagged 24 which means CBOR-encoded data
7792 the byte string is an indefinite length string
7793 the wrapped byte string is an array of three numbers
7794 [42, 43, 44]
7795
7796[
7797 24(
7798 (_ h'83', h'18', h'2A182B', h'182C')
7799 )
7800]
7801 */
Laurence Lundbladecf41c522021-02-20 10:19:07 -07007802static const uint8_t pWrappedByIndefiniteLength[] = {
7803 0x81,
7804 0xd8, 0x18,
7805 0x5f,
7806 0x41, 0x83,
7807 0x41, 0x18,
7808 0x43, 0x2A, 0x18, 0x2B,
7809 0x42, 0x18, 0x2C,
7810 0xff
7811};
7812#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
7813
7814
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03007815int32_t PeekAndRewindTest(void)
Laurence Lundblade2a26abb2020-11-05 19:06:54 -08007816{
7817 QCBORItem Item;
7818 QCBORError nCBORError;
7819 QCBORDecodeContext DCtx;
7820
7821 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0);
7822
7823 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
7824 return 100+(int32_t)nCBORError;
7825 }
7826 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
7827 return 200;
7828 }
7829
Laurence Lundblade3427dee2021-06-20 11:11:24 -07007830 QCBORDecode_VPeekNext(&DCtx, &Item);
7831 if((nCBORError = QCBORDecode_GetError(&DCtx))) {
7832 return 150+(int32_t)nCBORError;
7833 }
7834 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
7835 return 250;
7836 }
7837
Laurence Lundblade2a26abb2020-11-05 19:06:54 -08007838 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
7839 return (int32_t)nCBORError;
7840 }
7841 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
7842 return 300;
7843 }
7844
7845 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
7846 return 400 + (int32_t)nCBORError;
7847 }
7848 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
7849 return 500;
7850 }
7851
7852 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7853 return (int32_t)nCBORError;
7854 }
7855 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
7856 return 600;
7857 }
7858
7859 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
7860 return 900 + (int32_t)nCBORError;
7861 }
7862 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
7863 Item.uDataType != QCBOR_TYPE_INT64 ||
7864 Item.val.int64 != 42 ||
7865 Item.uDataAlloc ||
7866 Item.uLabelAlloc ||
7867 UsefulBufCompareToSZ(Item.label.string, "first integer")) {
7868 return 1000;
7869 }
7870
7871 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7872 return 1100 + (int32_t)nCBORError;
7873 }
7874
7875 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
7876 Item.uDataType != QCBOR_TYPE_INT64 ||
7877 Item.val.int64 != 42 ||
7878 Item.uDataAlloc ||
7879 Item.uLabelAlloc ||
7880 UsefulBufCompareToSZ(Item.label.string, "first integer")) {
7881 return 1200;
7882 }
7883
7884
7885 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7886 return 1300 + (int32_t)nCBORError;
7887 }
7888 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
7889 Item.uDataAlloc ||
7890 Item.uLabelAlloc ||
7891 UsefulBufCompareToSZ(Item.label.string, "an array of two strings") ||
7892 Item.uDataType != QCBOR_TYPE_ARRAY ||
Laurence Lundbladecf41c522021-02-20 10:19:07 -07007893 Item.val.uCount != 2) {
Laurence Lundblade2a26abb2020-11-05 19:06:54 -08007894 return 1400;
Laurence Lundbladecf41c522021-02-20 10:19:07 -07007895 }
Laurence Lundblade2a26abb2020-11-05 19:06:54 -08007896
7897 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7898 return 1500 + (int32_t)nCBORError;
7899 }
7900 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
7901 Item.uDataAlloc ||
7902 Item.uLabelAlloc ||
7903 UsefulBufCompareToSZ(Item.val.string, "string1")) {
7904 return 1600;
7905 }
7906
7907 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
7908 return 1700 + (int32_t)nCBORError;
7909 }
7910 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
7911 Item.uDataAlloc ||
7912 Item.uLabelAlloc ||
7913 UsefulBufCompareToSZ(Item.val.string, "string2")) {
7914 return 1800;
7915 }
7916
7917 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
7918 return (int32_t)nCBORError;
7919 }
7920 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
7921 Item.uDataAlloc ||
7922 Item.uLabelAlloc ||
7923 UsefulBufCompareToSZ(Item.val.string, "string2")) {
7924 return 1900;
7925 }
7926
7927 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7928 return (int32_t)nCBORError;
7929 }
7930 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
7931 Item.uDataAlloc ||
7932 Item.uLabelAlloc ||
7933 UsefulBufCompareToSZ(Item.val.string, "string2")) {
7934 return 2000;
7935 }
7936
7937
7938 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7939 return 2100 + (int32_t)nCBORError;
7940 }
7941 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
7942 Item.uDataAlloc ||
7943 Item.uLabelAlloc ||
7944 UsefulBufCompareToSZ(Item.label.string, "map in a map") ||
7945 Item.uDataType != QCBOR_TYPE_MAP ||
7946 Item.val.uCount != 4) {
7947 return 2100;
7948 }
7949
7950 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7951 return 2200 + (int32_t)nCBORError;
7952 }
7953 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
7954 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("bytes 1"))||
7955 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
7956 Item.uDataAlloc ||
7957 Item.uLabelAlloc ||
7958 UsefulBufCompareToSZ(Item.val.string, "xxxx")) {
7959 return 2300;
7960 }
7961
7962 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
7963 return 2400 + (int32_t)nCBORError;
7964 }
7965 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
7966 UsefulBufCompareToSZ(Item.label.string, "bytes 2") ||
7967 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
7968 Item.uDataAlloc ||
7969 Item.uLabelAlloc ||
7970 UsefulBufCompareToSZ(Item.val.string, "yyyy")) {
7971 return 2500;
7972 }
7973
7974 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7975 return 2600 + (int32_t)nCBORError;
7976 }
7977 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
7978 UsefulBufCompareToSZ(Item.label.string, "bytes 2") ||
7979 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
7980 Item.uDataAlloc ||
7981 Item.uLabelAlloc ||
7982 UsefulBufCompareToSZ(Item.val.string, "yyyy")) {
7983 return 2700;
7984 }
7985
7986 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7987 return 2800 + (int32_t)nCBORError;
7988 }
7989 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
7990 Item.uDataAlloc ||
7991 Item.uLabelAlloc ||
7992 UsefulBufCompareToSZ(Item.label.string, "another int") ||
7993 Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundbladecf41c522021-02-20 10:19:07 -07007994 Item.val.int64 != 98) {
Laurence Lundblade2a26abb2020-11-05 19:06:54 -08007995 return 2900;
Laurence Lundbladecf41c522021-02-20 10:19:07 -07007996 }
Laurence Lundblade2a26abb2020-11-05 19:06:54 -08007997
7998 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
7999 return 3000 + (int32_t)nCBORError;
8000 }
8001 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
8002 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("text 2"))||
8003 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
8004 Item.uDataAlloc ||
8005 Item.uLabelAlloc ||
8006 UsefulBufCompareToSZ(Item.val.string, "lies, damn lies and statistics")) {
8007 return 3100;
8008 }
8009
8010 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8011 return 3200 + (int32_t)nCBORError;
8012 }
8013 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
8014 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("text 2"))||
8015 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
8016 Item.uDataAlloc ||
8017 Item.uLabelAlloc ||
8018 UsefulBufCompareToSZ(Item.val.string, "lies, damn lies and statistics")) {
8019 return 3300;
8020 }
8021
Laurence Lundblade3427dee2021-06-20 11:11:24 -07008022 nCBORError = QCBORDecode_PeekNext(&DCtx, &Item);
8023 if(nCBORError != QCBOR_ERR_NO_MORE_ITEMS) {
8024 return 3300 + (int32_t)nCBORError;
8025 }
8026
8027 QCBORDecode_VPeekNext(&DCtx, &Item);
8028 nCBORError = QCBORDecode_GetError(&DCtx);
8029 if(nCBORError != QCBOR_ERR_NO_MORE_ITEMS) {
8030 return 3400 + (int32_t)nCBORError;
8031 }
8032
8033 QCBORDecode_VPeekNext(&DCtx, &Item);
8034 nCBORError = QCBORDecode_GetError(&DCtx);
8035 if(nCBORError != QCBOR_ERR_NO_MORE_ITEMS) {
8036 return 3500 + (int32_t)nCBORError;
8037 }
Laurence Lundbladecf41c522021-02-20 10:19:07 -07008038
8039
8040 // Rewind to top level after entering several maps
8041 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0);
8042
8043 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8044 return (int32_t)nCBORError;
8045 }
8046 if(Item.uDataType != QCBOR_TYPE_MAP ||
8047 Item.val.uCount != 3) {
8048 return 400;
8049 }
8050
8051 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8052 return 4000+(int32_t)nCBORError;
8053 }
8054
8055 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
8056 Item.uDataType != QCBOR_TYPE_INT64 ||
8057 Item.val.int64 != 42 ||
8058 Item.uDataAlloc ||
8059 Item.uLabelAlloc ||
8060 UsefulBufCompareToSZ(Item.label.string, "first integer")) {
8061 return 4100;
8062 }
8063
8064 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8065 return 4100+(int32_t)nCBORError;
8066 }
8067 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
8068 Item.uDataAlloc ||
8069 Item.uLabelAlloc ||
8070 UsefulBufCompareToSZ(Item.label.string, "an array of two strings") ||
8071 Item.uDataType != QCBOR_TYPE_ARRAY ||
8072 Item.val.uCount != 2) {
8073 return 4200;
8074 }
8075
8076 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8077 return 4200+(int32_t)nCBORError;
8078 }
8079 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
8080 Item.uDataAlloc ||
8081 Item.uLabelAlloc ||
8082 UsefulBufCompareToSZ(Item.val.string, "string1")) {
8083 return 4300;
8084 }
8085
8086 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8087 return 4300+(int32_t)nCBORError;
8088 }
8089 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
8090 Item.uDataAlloc ||
8091 Item.uLabelAlloc ||
8092 UsefulBufCompareToSZ(Item.val.string, "string2")) {
8093 return 4400;
8094 }
8095
8096 QCBORDecode_Rewind(&DCtx);
8097
8098 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8099 return 4400+(int32_t)nCBORError;
8100 }
8101 if(Item.uDataType != QCBOR_TYPE_MAP ||
8102 Item.val.uCount != 3) {
8103 return 4500;
8104 }
8105
8106 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8107 return (int32_t)nCBORError;
8108 }
8109
8110 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
8111 Item.uDataType != QCBOR_TYPE_INT64 ||
8112 Item.val.int64 != 42 ||
8113 Item.uDataAlloc ||
8114 Item.uLabelAlloc ||
8115 UsefulBufCompareToSZ(Item.label.string, "first integer")) {
8116 return 4600;
8117 }
8118
8119 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8120 return (int32_t)nCBORError;
8121 }
8122 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
8123 Item.uDataAlloc ||
8124 Item.uLabelAlloc ||
8125 UsefulBufCompareToSZ(Item.label.string, "an array of two strings") ||
8126 Item.uDataType != QCBOR_TYPE_ARRAY ||
8127 Item.val.uCount != 2) {
8128 return 4700;
8129 }
8130
8131 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8132 return (int32_t)nCBORError;
8133 }
8134 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
8135 Item.uDataAlloc ||
8136 Item.uLabelAlloc ||
8137 UsefulBufCompareToSZ(Item.val.string, "string1")) {
8138 return 4800;
8139 }
8140
8141 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8142 return 4900+(int32_t)nCBORError;
8143 }
8144 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
8145 Item.uDataAlloc ||
8146 Item.uLabelAlloc ||
8147 UsefulBufCompareToSZ(Item.val.string, "string2")) {
8148 return 5000;
8149 }
8150
8151
8152 // Rewind an entered map
8153 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0);
8154
8155 QCBORDecode_EnterMap(&DCtx, NULL);
8156
8157 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8158 return 5100+(int32_t)nCBORError;
8159 }
8160
8161 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
8162 Item.uDataType != QCBOR_TYPE_INT64 ||
8163 Item.val.int64 != 42 ||
8164 Item.uDataAlloc ||
8165 Item.uLabelAlloc ||
8166 UsefulBufCompareToSZ(Item.label.string, "first integer")) {
8167 return 5200;
8168 }
8169
8170 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8171 return 5200+(int32_t)nCBORError;
8172 }
8173 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
8174 Item.uDataAlloc ||
8175 Item.uLabelAlloc ||
8176 UsefulBufCompareToSZ(Item.label.string, "an array of two strings") ||
8177 Item.uDataType != QCBOR_TYPE_ARRAY ||
8178 Item.val.uCount != 2) {
8179 return -5300;
8180 }
8181
8182 QCBORDecode_Rewind(&DCtx);
8183
8184 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8185 return 5300+(int32_t)nCBORError;
8186 }
8187
8188 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
8189 Item.uDataType != QCBOR_TYPE_INT64 ||
8190 Item.val.int64 != 42 ||
8191 Item.uDataAlloc ||
8192 Item.uLabelAlloc ||
8193 UsefulBufCompareToSZ(Item.label.string, "first integer")) {
8194 return 5400;
8195 }
8196
8197 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8198 return 5400+(int32_t)nCBORError;
8199 }
8200 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
8201 Item.uDataAlloc ||
8202 Item.uLabelAlloc ||
8203 UsefulBufCompareToSZ(Item.label.string, "an array of two strings") ||
8204 Item.uDataType != QCBOR_TYPE_ARRAY ||
8205 Item.val.uCount != 2) {
8206 return 5500;
8207 }
8208
8209
8210 // Rewind and entered array inside an entered map
8211 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0);
8212
8213 QCBORDecode_EnterMap(&DCtx, NULL);
8214
8215 QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings");
8216
8217 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8218 return 5600+(int32_t)nCBORError;
8219 }
8220 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
8221 Item.uDataAlloc ||
8222 Item.uLabelAlloc ||
8223 UsefulBufCompareToSZ(Item.val.string, "string1")) {
8224 return 5700;
8225 }
8226
8227 QCBORDecode_Rewind(&DCtx);
8228
8229 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8230 return 5700+(int32_t)nCBORError;
8231 }
8232 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
8233 Item.uDataAlloc ||
8234 Item.uLabelAlloc ||
8235 UsefulBufCompareToSZ(Item.val.string, "string1")) {
8236 return 5800;
8237 }
8238
8239 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8240 return (int32_t)nCBORError;
8241 }
8242 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
8243 Item.uDataAlloc ||
8244 Item.uLabelAlloc ||
8245 UsefulBufCompareToSZ(Item.val.string, "string2")) {
8246 return 5900;
8247 }
8248
8249 QCBORDecode_Rewind(&DCtx);
8250
8251 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8252 return 5900+(int32_t)nCBORError;
8253 }
8254 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
8255 Item.uDataAlloc ||
8256 Item.uLabelAlloc ||
8257 UsefulBufCompareToSZ(Item.val.string, "string1")) {
8258 return 6000;
8259 }
8260
8261
8262 // Rewind a byte string inside an array inside an array
8263 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidWrappedMapEncoded), 0);
8264
8265 QCBORDecode_EnterArray(&DCtx, NULL);
8266
8267 uint64_t i;
8268 QCBORDecode_GetUInt64(&DCtx, &i);
8269
8270 QCBORDecode_EnterArray(&DCtx, NULL);
8271
8272 QCBORDecode_EnterBstrWrapped(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
8273 if(QCBORDecode_GetError(&DCtx)) {
8274 return 6100;
8275 }
8276
8277 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8278 return (int32_t)nCBORError;
8279 }
8280 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
8281 return 6200;
8282 }
8283
8284 QCBORDecode_Rewind(&DCtx);
8285
8286 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8287 return 6300+(int32_t)nCBORError;
8288 }
8289 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
8290 return 6400;
8291 }
8292
8293#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
8294 // Rewind a byte string inside an indefinite-length array inside
8295 // indefinite-length array
8296
8297 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidIndefWrappedMapEncoded), 0);
8298
8299 QCBORDecode_EnterArray(&DCtx, NULL);
8300
8301 QCBORDecode_GetUInt64(&DCtx, &i);
8302
8303 QCBORDecode_EnterArray(&DCtx, NULL);
8304
8305 QCBORDecode_EnterBstrWrapped(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
8306 if(QCBORDecode_GetError(&DCtx)) {
8307 return 6500;
8308 }
8309
8310 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8311 return 6600+(int32_t)nCBORError;
8312 }
8313 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
8314 return 6700;
8315 }
8316
8317 QCBORDecode_Rewind(&DCtx);
8318
8319 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8320 return 6800+(int32_t)nCBORError;
8321 }
8322 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
8323 return 6900;
8324 }
8325#endif
8326
8327 // Rewind an empty map
8328 // [100, {}]
8329 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pWithEmptyMap), 0);
8330 QCBORDecode_EnterArray(&DCtx, NULL);
8331 QCBORDecode_GetUInt64(&DCtx, &i);
8332 if(i != 100) {
8333 return 7010;
8334 }
8335 QCBORDecode_EnterMap(&DCtx, NULL);
8336
8337 /* Do it 5 times to be sure multiple rewinds work */
8338 for(int n = 0; n < 5; n++) {
8339 nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
8340 if(nCBORError != QCBOR_ERR_NO_MORE_ITEMS) {
8341 return 7000 + n;
8342 }
8343 QCBORDecode_Rewind(&DCtx);
8344 }
8345 QCBORDecode_ExitMap(&DCtx);
8346 QCBORDecode_Rewind(&DCtx);
8347 QCBORDecode_GetUInt64(&DCtx, &i);
8348 if(i != 100) {
8349 return 7010;
8350 }
8351 QCBORDecode_ExitArray(&DCtx);
8352 QCBORDecode_Rewind(&DCtx);
8353 QCBORDecode_EnterArray(&DCtx, NULL);
8354 i = 9;
8355 QCBORDecode_GetUInt64(&DCtx, &i);
8356 if(i != 100) {
8357 return 7020;
8358 }
8359 if(QCBORDecode_GetError(&DCtx)){
8360 return 7030;
8361 }
8362
8363 // Rewind an empty indefinite length map
8364#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
8365 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pWithEmptyMapInDef), 0);
8366 QCBORDecode_EnterArray(&DCtx, NULL);
8367 QCBORDecode_GetUInt64(&DCtx, &i);
8368 if(i != 100) {
8369 return 7810;
8370 }
8371 QCBORDecode_EnterMap(&DCtx, NULL);
8372
8373 /* Do it 5 times to be sure multiple rewinds work */
8374 for(int n = 0; n < 5; n++) {
8375 nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
8376 if(nCBORError != QCBOR_ERR_NO_MORE_ITEMS) {
8377 return 7800 + n;
8378 }
8379 QCBORDecode_Rewind(&DCtx);
8380 }
8381 QCBORDecode_ExitMap(&DCtx);
8382 QCBORDecode_Rewind(&DCtx);
8383 QCBORDecode_GetUInt64(&DCtx, &i);
8384 if(i != 100) {
8385 return 7810;
8386 }
8387 QCBORDecode_ExitArray(&DCtx);
8388 QCBORDecode_Rewind(&DCtx);
8389 QCBORDecode_EnterArray(&DCtx, NULL);
8390 i = 9;
8391 QCBORDecode_GetUInt64(&DCtx, &i);
8392 if(i != 100) {
8393 return 7820;
8394 }
8395 if(QCBORDecode_GetError(&DCtx)){
8396 return 7830;
8397 }
8398#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
8399
8400 // Rewind an indefnite length byte-string wrapped sequence
8401#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
Laurence Lundblade37286c02022-09-03 10:05:02 -07008402 // TODO: rewrite this test to not use tags
Laurence Lundbladecf41c522021-02-20 10:19:07 -07008403 QCBORDecode_Init(&DCtx,
8404 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pWrappedByIndefiniteLength),
8405 0);
8406 UsefulBuf_MAKE_STACK_UB(Pool, 100);
8407 QCBORDecode_SetMemPool(&DCtx, Pool, 0);
8408
8409 QCBORDecode_EnterArray(&DCtx, NULL);
8410 QCBORDecode_EnterBstrWrapped(&DCtx, 2, NULL);
Laurence Lundblade37286c02022-09-03 10:05:02 -07008411#ifndef QCBOR_DISABLE_TAGS
Laurence Lundbladecf41c522021-02-20 10:19:07 -07008412 if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_INPUT_TOO_LARGE) {
Laurence Lundblade37286c02022-09-03 10:05:02 -07008413 /* TODO: This is what happens when trying to enter
8414 * indefinite-length byte string wrapped CBOR. Tolerate for
8415 * now. Eventually it needs to be fixed so this works, but that
8416 * is not simple.
8417 */
Laurence Lundbladecf41c522021-02-20 10:19:07 -07008418 return 7300;
8419 }
8420
8421 /*
8422 QCBORDecode_GetUInt64(&DCtx, &i);
8423 if(i != 42) {
8424 return 7110;
8425 }
8426 QCBORDecode_Rewind(&DCtx);
8427 QCBORDecode_GetUInt64(&DCtx, &i);
8428 if(i != 42) {
8429 return 7220;
8430 }*/
Laurence Lundblade37286c02022-09-03 10:05:02 -07008431
8432#else /* QCBOR_DISABLE_TAGS */
8433 if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_TAGS_DISABLED) {
8434 return 7301;
8435 }
8436#endif /* QCBOR_DISABLE_TAGS */
8437
Laurence Lundbladecf41c522021-02-20 10:19:07 -07008438#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
8439
8440
8441 // Rewind an indefnite length byte-string wrapped sequence
8442
Laurence Lundblade2a26abb2020-11-05 19:06:54 -08008443 return 0;
8444}
Laurence Lundblade9f9c3732021-03-23 09:38:46 -07008445
8446
8447
8448
8449static const uint8_t spBooleansInMap[] =
8450{
8451 0xa1, 0x08, 0xf5
8452};
8453
8454static const uint8_t spBooleansInMapWrongType[] =
8455{
8456 0xa1, 0x08, 0xf6
8457};
8458
8459static const uint8_t spBooleansInMapNWF[] =
8460{
8461 0xa1, 0x08, 0x1a
8462};
8463
Laurence Lundblade8782dd32021-04-27 04:15:37 -07008464static const uint8_t spNullInMap[] =
8465{
8466 0xa1, 0x08, 0xf6
8467};
8468
8469static const uint8_t spUndefinedInMap[] =
8470{
8471 0xa1, 0x08, 0xf7
8472};
8473
Laurence Lundblade9f9c3732021-03-23 09:38:46 -07008474
8475int32_t BoolTest(void)
8476{
8477 QCBORDecodeContext DCtx;
8478 bool b;
8479
Laurence Lundblade8782dd32021-04-27 04:15:37 -07008480 QCBORDecode_Init(&DCtx,
8481 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMap),
8482 0);
Laurence Lundblade9f9c3732021-03-23 09:38:46 -07008483 QCBORDecode_EnterMap(&DCtx, NULL);
8484 QCBORDecode_GetBool(&DCtx, &b);
8485 if(QCBORDecode_GetAndResetError(&DCtx) || !b) {
8486 return 1;
8487 }
8488
8489 QCBORDecode_GetBoolInMapN(&DCtx, 7, &b);
8490 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_LABEL_NOT_FOUND) {
8491 return 2;
8492 }
8493
8494 QCBORDecode_GetBoolInMapN(&DCtx, 8, &b);
8495 if(QCBORDecode_GetAndResetError(&DCtx) || !b) {
8496 return 3;
8497 }
8498
8499
8500 QCBORDecode_GetBoolInMapSZ(&DCtx, "xx", &b);
8501 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_LABEL_NOT_FOUND) {
8502 return 4;
8503 }
8504
Laurence Lundblade8782dd32021-04-27 04:15:37 -07008505 QCBORDecode_Init(&DCtx,
8506 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMapWrongType),
8507 0);
Laurence Lundblade9f9c3732021-03-23 09:38:46 -07008508 QCBORDecode_EnterMap(&DCtx, NULL);
8509 QCBORDecode_GetBool(&DCtx, &b);
8510 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
8511 return 5;
8512 }
8513
Laurence Lundblade8782dd32021-04-27 04:15:37 -07008514 QCBORDecode_Init(&DCtx,
8515 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMapNWF),
8516 0);
Laurence Lundblade9f9c3732021-03-23 09:38:46 -07008517 QCBORDecode_EnterMap(&DCtx, NULL);
8518 QCBORDecode_GetBool(&DCtx, &b);
8519 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_HIT_END) {
8520 return 6;
8521 }
8522
Laurence Lundblade8782dd32021-04-27 04:15:37 -07008523
8524 QCBORDecode_Init(&DCtx,
8525 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spNullInMap),
8526 0);
8527 QCBORDecode_EnterMap(&DCtx, NULL);
8528 QCBORDecode_GetNull(&DCtx);
8529 if(QCBORDecode_GetAndResetError(&DCtx)) {
8530 return 7;
8531 }
8532
8533 QCBORDecode_Init(&DCtx,
8534 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMap),
8535 0);
8536 QCBORDecode_EnterMap(&DCtx, NULL);
8537 QCBORDecode_GetNull(&DCtx);
8538 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
8539 return 8;
8540 }
8541
8542 QCBORDecode_Init(&DCtx,
8543 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spNullInMap),
8544 0);
8545 QCBORDecode_EnterMap(&DCtx, NULL);
8546 QCBORDecode_GetNullInMapN(&DCtx, 8);
8547 if(QCBORDecode_GetAndResetError(&DCtx)) {
8548 return 9;
8549 }
8550
8551 QCBORDecode_Init(&DCtx,
8552 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMap),
8553 0);
8554 QCBORDecode_EnterMap(&DCtx, NULL);
8555 QCBORDecode_GetNullInMapN(&DCtx, 8);
8556 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
8557 return 10;
8558 }
8559
8560 QCBORDecode_Init(&DCtx,
8561 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMapNWF),
8562 0);
8563 QCBORDecode_EnterMap(&DCtx, NULL);
8564 QCBORDecode_GetUndefined(&DCtx);
8565 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_HIT_END) {
8566 return 11;
8567 }
8568
8569 QCBORDecode_Init(&DCtx,
8570 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUndefinedInMap),
8571 0);
8572 QCBORDecode_EnterMap(&DCtx, NULL);
8573 QCBORDecode_GetUndefined(&DCtx);
8574 if(QCBORDecode_GetAndResetError(&DCtx)) {
8575 return 12;
8576 }
8577
8578 QCBORDecode_Init(&DCtx,
8579 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMap),
8580 0);
8581 QCBORDecode_EnterMap(&DCtx, NULL);
8582 QCBORDecode_GetUndefined(&DCtx);
8583 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
8584 return 13;
8585 }
8586
8587 QCBORDecode_Init(&DCtx,
8588 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUndefinedInMap),
8589 0);
8590 QCBORDecode_EnterMap(&DCtx, NULL);
8591 QCBORDecode_GetUndefinedInMapN(&DCtx, 8);
8592 if(QCBORDecode_GetAndResetError(&DCtx)) {
8593 return 14;
8594 }
8595
8596 QCBORDecode_Init(&DCtx,
8597 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMap),
8598 0);
8599 QCBORDecode_EnterMap(&DCtx, NULL);
8600 QCBORDecode_GetUndefinedInMapN(&DCtx, 8);
8601 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
8602 return 15;
8603 }
8604
8605 QCBORDecode_Init(&DCtx,
8606 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMapNWF),
8607 0);
8608 QCBORDecode_EnterMap(&DCtx, NULL);
8609 QCBORDecode_GetUndefined(&DCtx);
8610 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_HIT_END) {
8611 return 15;
8612 }
8613
Laurence Lundblade9f9c3732021-03-23 09:38:46 -07008614 return 0;
8615}