blob: 7fedf9167e3f4ef78fbae6cdba0655a5f1a73ec6 [file] [log] [blame]
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001/*==============================================================================
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002 * Copyright (c) 2016-2018, The Linux Foundation.
3 * Copyright (c) 2018-2024, Laurence Lundblade.
4 * Copyright (c) 2021, Arm Limited.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are
9 * met:
Laurence Lundblade0dbc9172018-11-01 14:17:21 +070010 * Redistributions of source code must retain the above copyright
11 notice, this list of conditions and the following disclaimer.
12 * Redistributions in binary form must reproduce the above
13 copyright notice, this list of conditions and the following
14 disclaimer in the documentation and/or other materials provided
15 with the distribution.
16 * Neither the name of The Linux Foundation nor the names of its
17 contributors, nor the name "Laurence Lundblade" may be used to
18 endorse or promote products derived from this software without
19 specific prior written permission.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -080020
Laurence Lundblade0dbc9172018-11-01 14:17:21 +070021THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
22WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
23MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
24ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
25BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
28BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
30OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
31IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Laurence Lundbladeee851742020-01-08 08:37:05 -080032 =============================================================================*/
Laurence Lundblade9e3651c2018-10-10 11:49:55 +080033
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080034#include "qcbor_decode_tests.h"
Laurence Lundblade844bb5c2020-03-01 17:27:25 -080035#include "qcbor/qcbor_encode.h"
36#include "qcbor/qcbor_decode.h"
Laurence Lundblade67257dc2020-07-27 03:33:37 -070037#include "qcbor/qcbor_spiffy_decode.h"
Laurence Lundbladed4728fd2018-12-17 15:15:56 -080038#include <string.h>
Laurence Lundblade9e3651c2018-10-10 11:49:55 +080039#include <math.h> // for fabs()
Laurence Lundbladebb1062e2019-08-12 23:28:54 -070040#include "not_well_formed_cbor.h"
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -080041
Laurence Lundblade9b334962020-08-27 10:55:53 -070042// Handy macro to compare a UsefulBuf to a C string
43#define UsefulBufCompareToSZ(x, y) \
44 UsefulBuf_Compare(x, UsefulBuf_FromSZ(y))
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080045
Laurence Lundbladea2e29072018-12-30 09:20:06 -080046#ifdef PRINT_FUNCTIONS_FOR_DEBUGGING
Laurence Lundblade20db9c92018-12-17 11:40:37 -080047#include <stdio.h>
Laurence Lundbladea2e29072018-12-30 09:20:06 -080048
49static void PrintUsefulBufC(const char *szLabel, UsefulBufC Buf)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080050{
51 if(szLabel) {
52 printf("%s ", szLabel);
53 }
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -080054
Laurence Lundblade570fab52018-10-13 18:28:27 +080055 size_t i;
Laurence Lundbladea2e29072018-12-30 09:20:06 -080056 for(i = 0; i < Buf.len; i++) {
57 uint8_t Z = ((uint8_t *)Buf.ptr)[i];
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080058 printf("%02x ", Z);
59 }
60 printf("\n");
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -080061
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080062 fflush(stdout);
63}
Laurence Lundbladee2c893c2020-12-26 17:41:53 -080064#endif /* PRINT_FUNCTIONS_FOR_DEBUGGING */
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080065
Laurence Lundbladecc7da412020-12-27 00:09:07 -080066/*
67 [
68 -9223372036854775808,
69 -4294967297,
70 -4294967296,
71 -4294967295,
72 -4294967294,
73 -2147483648,
74 -2147483647,
75 -65538,
76 -65537,
77 -65536,
78 -65535,
79 -65534,
80 -257,
81 -256,
82 -255,
83 -254,
84 -25,
85 -24,
86 -23,
87 -1,
88 0,
89 0,
90 1,
91 22,
92 23,
93 24,
94 25,
95 26,
96 254,
97 255,
98 256,
99 257,
100 65534,
101 65535,
102 65536,
103 65537,
104 65538,
105 2147483647,
106 2147483647,
107 2147483648,
108 2147483649,
109 4294967294,
110 4294967295,
111 4294967296,
112 4294967297,
113 9223372036854775807,
114 18446744073709551615
115 ]
116 */
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800117
Laurence Lundbladeb836efb2018-10-28 20:09:58 +0700118static const uint8_t spExpectedEncodedInts[] = {
Laurence 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 Lundbladed6b33f52024-03-06 12:51:15 -08001795struct DecodeFailTestInput {
1796 const char *szDescription; /* Description of the test */
1797 QCBORDecodeMode DecoderMode; /* The QCBOR Decoder Mode for test */
1798 UsefulBufC Input; /* Chunk of CBOR that cases error */
1799 QCBORError nError; /* The expected error */
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001800};
1801
Laurence Lundblade59289e52019-12-30 13:44:37 -08001802
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08001803static int32_t
1804ProcessDecodeFailures(const struct DecodeFailTestInput *pFailInputs, const int nNumFails)
Laurence Lundblade59289e52019-12-30 13:44:37 -08001805{
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08001806 int nIndex;
1807 QCBORDecodeContext DCtx;
1808 QCBORError uCBORError;
1809 QCBORItem Item;
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08001810
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08001811 for(nIndex = 0; nIndex < nNumFails; nIndex++) {
1812 const struct DecodeFailTestInput *pF = &pFailInputs[nIndex];
1813
1814 QCBORDecode_Init(&DCtx, pF->Input, pF->DecoderMode);
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08001815
1816#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08001817 /* Set up the decoding context including a memory pool so that
1818 * indefinite length items can be checked.
1819 */
Laurence Lundblade59289e52019-12-30 13:44:37 -08001820 UsefulBuf_MAKE_STACK_UB(Pool, 100);
Laurence Lundblade830fbf92020-05-31 17:22:33 -07001821
Laurence Lundblade4bf6e712020-12-10 11:53:21 -08001822 uCBORError = QCBORDecode_SetMemPool(&DCtx, Pool, 0);
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08001823 if(uCBORError != QCBOR_SUCCESS) {
1824 return -1;
Laurence Lundblade59289e52019-12-30 13:44:37 -08001825 }
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08001826#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
1827
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08001828 if(nIndex == 8) {
1829 uCBORError = 9; /* For setting break points */
Laurence Lundblade37286c02022-09-03 10:05:02 -07001830 }
1831
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08001832 /* Iterate until there is an error of some sort of error */
Laurence Lundblade59289e52019-12-30 13:44:37 -08001833 do {
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08001834 /* Set to something none-zero, something other than QCBOR_TYPE_NONE */
Laurence Lundblade59289e52019-12-30 13:44:37 -08001835 memset(&Item, 0x33, sizeof(Item));
1836
Laurence Lundblade4bf6e712020-12-10 11:53:21 -08001837 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
1838 } while(uCBORError == QCBOR_SUCCESS);
1839
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08001840 /* Must get the expected error or the this test fails
1841 * The data and label type must also be QCBOR_TYPE_NONE.
1842 */
Laurence Lundblade4bf6e712020-12-10 11:53:21 -08001843 if(uCBORError != pF->nError ||
Laurence Lundblade59289e52019-12-30 13:44:37 -08001844 Item.uDataType != QCBOR_TYPE_NONE ||
1845 Item.uLabelType != QCBOR_TYPE_NONE) {
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08001846 return (int32_t)(nIndex * 1000 + (int)uCBORError);
Laurence Lundblade59289e52019-12-30 13:44:37 -08001847 }
1848 }
1849
1850 return 0;
1851}
1852
1853
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08001854static const struct DecodeFailTestInput Failures[] = {
1855 /* Most of this is copied from not_well_formed.h. Here the error
1856 * code returned is also checked.
1857 */
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001858
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08001859#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08001860 /* Indefinite length strings must be closed off */
1861 { "An indefinite length byte string not closed off",
1862 QCBOR_DECODE_MODE_NORMAL,
1863 {"0x5f\x41\x00", 3},
1864 QCBOR_ERR_HIT_END
1865 },
1866 { "An indefinite length text string not closed off",
1867 QCBOR_DECODE_MODE_NORMAL,
1868 {"\x7f\x61\x00", 3},
1869 QCBOR_ERR_HIT_END
1870 },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001871
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08001872 /* All the chunks in an indefinite length string must be of the
1873 * type of indefinite length string
1874 */
1875 { "Indefinite length byte string with text string chunk",
1876 QCBOR_DECODE_MODE_NORMAL,
1877 {"\x5f\x61\x00\xff", 4},
1878 QCBOR_ERR_INDEFINITE_STRING_CHUNK
1879 },
1880 { "Indefinite length text string with a byte string chunk",
1881 QCBOR_DECODE_MODE_NORMAL,
1882 {"\x7f\x41\x00\xff", 4},
1883 QCBOR_ERR_INDEFINITE_STRING_CHUNK
1884 },
1885 { "Indefinite length byte string with a positive integer chunk",
1886 QCBOR_DECODE_MODE_NORMAL,
1887 {"\x5f\x00\xff", 3},
1888 QCBOR_ERR_INDEFINITE_STRING_CHUNK
1889 },
1890 { "Indefinite length byte string with an negative integer chunk",
1891 QCBOR_DECODE_MODE_NORMAL,
1892 {"\x5f\x21\xff", 3},
1893 QCBOR_ERR_INDEFINITE_STRING_CHUNK
1894 },
1895 { "Indefinite length byte string with an array chunk",
1896 QCBOR_DECODE_MODE_NORMAL,
1897 {"\x5f\x80\xff", 3},
1898 QCBOR_ERR_INDEFINITE_STRING_CHUNK
1899 },
1900 { "Indefinite length byte string with an map chunk",
1901 QCBOR_DECODE_MODE_NORMAL,
1902 {"\x5f\xa0\xff", 3},
1903 QCBOR_ERR_INDEFINITE_STRING_CHUNK
1904 },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001905
Laurence Lundblade37286c02022-09-03 10:05:02 -07001906#ifndef QCBOR_DISABLE_TAGS
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08001907 { "Indefinite length byte string with tagged integer chunk",
1908 QCBOR_DECODE_MODE_NORMAL,
1909 {"\x5f\xc0\x00\xff", 4},
1910 QCBOR_ERR_INDEFINITE_STRING_CHUNK
1911 },
Laurence Lundblade37286c02022-09-03 10:05:02 -07001912#else
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08001913 { "Indefinite length byte string with tagged integer chunk",
1914 QCBOR_DECODE_MODE_NORMAL,
1915 {"\x5f\xc0\x00\xff", 4},
1916 QCBOR_ERR_TAGS_DISABLED
1917 },
Laurence Lundblade37286c02022-09-03 10:05:02 -07001918#endif /* QCBOR_DISABLE_TAGS */
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001919
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08001920 { "Indefinite length byte string with an simple type chunk",
1921 QCBOR_DECODE_MODE_NORMAL,
1922 {"\x5f\xe0\xff", 3},
1923 QCBOR_ERR_INDEFINITE_STRING_CHUNK
1924 },
1925 { "???",
1926 QCBOR_DECODE_MODE_NORMAL,
1927 {"\x5f\x5f\x41\x00\xff\xff", 6},
1928 QCBOR_ERR_INDEFINITE_STRING_CHUNK
1929 },
1930 { "indefinite length text string with indefinite string inside",
1931 QCBOR_DECODE_MODE_NORMAL,
1932 {"\x7f\x7f\x61\x00\xff\xff", 6},
1933 QCBOR_ERR_INDEFINITE_STRING_CHUNK
1934 },
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08001935#else /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
1936
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08001937#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
1938
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08001939 /* Definte length maps and arrays must be closed by having the right number of items */
1940 { "A definte length array that is supposed to have 1 item, but has none",
1941 QCBOR_DECODE_MODE_NORMAL,
1942 {"\x81", 1},
1943 QCBOR_ERR_NO_MORE_ITEMS
1944 },
1945 { "A definte length array that is supposed to have 2 items, but has only 1",
1946 QCBOR_DECODE_MODE_NORMAL,
1947 {"\x82\x00", 2},
1948 QCBOR_ERR_NO_MORE_ITEMS
1949 },
1950 { "A definte length array that is supposed to have 511 items, but has only 1",
1951 QCBOR_DECODE_MODE_NORMAL,
1952 {"\x9a\x01\xff\x00", 4},
1953 QCBOR_ERR_HIT_END
1954 },
1955 { "A definte length map that is supposed to have 1 item, but has none",
1956 QCBOR_DECODE_MODE_NORMAL,
1957 {"\xa1", 1},
1958 QCBOR_ERR_NO_MORE_ITEMS
1959 },
1960 { "A definte length map that is supposed to have s item, but has only 1",
1961 QCBOR_DECODE_MODE_NORMAL,
1962 {"\xa2\x01\x02", 3},
1963 QCBOR_ERR_NO_MORE_ITEMS
1964 },
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08001965#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08001966 /* Indefinte length maps and arrays must be ended by a break */
1967 { "Indefinite length array with zero items and no break",
1968 QCBOR_DECODE_MODE_NORMAL,
1969 {"\x9f", 1},
1970 QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001971
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08001972 { "Indefinite length array with two items and no break",
1973 QCBOR_DECODE_MODE_NORMAL,
1974 {"\x9\x01\x02", 3},
1975 QCBOR_ERR_NO_MORE_ITEMS
1976 },
1977 { "Indefinite length map with zero items and no break",
1978 QCBOR_DECODE_MODE_NORMAL,
1979 {"\xbf", 1},
1980 QCBOR_ERR_NO_MORE_ITEMS
1981 },
1982 { "Indefinite length map with two items and no break",
1983 QCBOR_DECODE_MODE_NORMAL,
1984 {"\xbf\x01\x02\x01\x02", 5},
1985 QCBOR_ERR_NO_MORE_ITEMS
1986 },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001987
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08001988 /* Nested maps and arrays must be closed off (some extra nested test vectors) */
1989 { "Unclosed indefinite array containing a closed definite length array",
1990 QCBOR_DECODE_MODE_NORMAL,
1991 {"\x9f\x80\x00", 3},
1992 QCBOR_ERR_NO_MORE_ITEMS
1993 },
1994
1995 { "Definite length array containing an unclosed indefinite length array",
1996 QCBOR_DECODE_MODE_NORMAL,
1997 {"\x81\x9f", 2},
1998 QCBOR_ERR_NO_MORE_ITEMS
1999 },
2000 { "Unclosed indefinite map containing a closed definite length array",
2001 QCBOR_DECODE_MODE_NORMAL,
2002 {"\xbf\x01\x80\x00\xa0", 5},
2003 QCBOR_ERR_NO_MORE_ITEMS
2004 },
2005 { "Definite length map containing an unclosed indefinite length array",
2006 QCBOR_DECODE_MODE_NORMAL,
2007 {"\xa1\x02\x9f", 3},
2008 QCBOR_ERR_NO_MORE_ITEMS
2009 },
2010 { "Deeply nested definite length arrays with deepest one unclosed",
2011 QCBOR_DECODE_MODE_NORMAL,
2012 {"\x81\x81\x81\x81\x81\x81\x81\x81\x81", 9},
2013 QCBOR_ERR_NO_MORE_ITEMS
2014 },
2015 { "Deeply nested indefinite length arrays with deepest one unclosed",
2016 QCBOR_DECODE_MODE_NORMAL,
2017 {"\x9f\x9f\x9f\x9f\x9f\xff\xff\xff\xff", 9},
2018 QCBOR_ERR_NO_MORE_ITEMS
2019 },
2020 { "Mixed nesting with indefinite unclosed",
2021 QCBOR_DECODE_MODE_NORMAL,
2022 {"\x9f\x81\x9f\x81\x9f\x9f\xff\xff\xff", 9},
2023 QCBOR_ERR_NO_MORE_ITEMS },
2024 { "Mixed nesting with definite unclosed",
2025 QCBOR_DECODE_MODE_NORMAL,
2026 {"\x9f\x82\x9f\x81\x9f\x9f\xff\xff\xff\xff", 10},
2027 QCBOR_ERR_BAD_BREAK
2028 },
2029 { "Unclosed indefinite length map in definite length maps",
2030 QCBOR_DECODE_MODE_NORMAL,
2031 {"\xa1\x01\xa2\x02\xbf\xff\x02\xbf", 8},
2032 QCBOR_ERR_NO_MORE_ITEMS
2033 },
2034 { "Unclosed definite length map in indefinite length maps",
2035 QCBOR_DECODE_MODE_NORMAL,
2036 {"\xbf\x01\xbf\x02\xa1", 5},
2037 QCBOR_ERR_NO_MORE_ITEMS
2038 },
2039 { "Unclosed indefinite length array in definite length maps",
2040 QCBOR_DECODE_MODE_NORMAL,
2041 {"\xa1\x01\xa2\x02\x9f\xff\x02\x9f", 8},
2042 QCBOR_ERR_NO_MORE_ITEMS
2043 },
2044 { "Unclosed definite length array in indefinite length maps",
2045 QCBOR_DECODE_MODE_NORMAL,
2046 {"\xbf\x01\xbf\x02\x81", 5},
2047 QCBOR_ERR_NO_MORE_ITEMS
2048 },
2049 { "Unclosed indefinite length map in definite length arrays",
2050 QCBOR_DECODE_MODE_NORMAL,
2051 {"\x81\x82\xbf\xff\xbf", 5},
2052 QCBOR_ERR_NO_MORE_ITEMS
2053 },
2054 { "Unclosed definite length map in indefinite length arrays",
2055 QCBOR_DECODE_MODE_NORMAL,
2056 {"\x9f\x9f\xa1", 3},
2057 QCBOR_ERR_NO_MORE_ITEMS
2058 },
2059
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08002060#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002061
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002062 /* The "argument" for the data item is incomplete */
2063 { "Positive integer missing 1 byte argument",
2064 QCBOR_DECODE_MODE_NORMAL,
2065 {"\x18", 1},
2066 QCBOR_ERR_HIT_END
2067 },
2068 { "Positive integer missing 2 byte argument",
2069 QCBOR_DECODE_MODE_NORMAL,
2070 {"\x19", 1},
2071 QCBOR_ERR_HIT_END
2072 },
2073 { "Positive integer missing 4 byte argument",
2074 QCBOR_DECODE_MODE_NORMAL,
2075 {"\x1a", 1},
2076 QCBOR_ERR_HIT_END
2077 },
2078 { "Positive integer missing 8 byte argument",
2079 QCBOR_DECODE_MODE_NORMAL,
2080 {"\x1b", 1},
2081 QCBOR_ERR_HIT_END
2082 },
2083 { "Positive integer missing 1 byte of 2 byte argument",
2084 QCBOR_DECODE_MODE_NORMAL,
2085 {"\x19\x01", 2},
2086 QCBOR_ERR_HIT_END
2087 },
2088 { "Positive integer missing 2 bytes of 4 byte argument",
2089 QCBOR_DECODE_MODE_NORMAL,
2090 {"\x1a\x01\x02", 3},
2091 QCBOR_ERR_HIT_END
2092 },
2093 { "Positive integer missing 1 bytes of 7 byte argument",
2094 QCBOR_DECODE_MODE_NORMAL,
2095 {"\x1b\x01\x02\x03\x04\x05\x06\x07", 8},
2096 QCBOR_ERR_HIT_END
2097 },
2098 { "Negative integer missing 1 byte argument",
2099 QCBOR_DECODE_MODE_NORMAL,
2100 {"\x38", 1},
2101 QCBOR_ERR_HIT_END
2102 },
2103 { "Binary string missing 1 byte argument",
2104 QCBOR_DECODE_MODE_NORMAL,
2105 {"\x58", 1},
2106 QCBOR_ERR_HIT_END
2107 },
2108 { "Text string missing 1 byte argument",
2109 QCBOR_DECODE_MODE_NORMAL,
2110 {"\x78", 1},
2111 QCBOR_ERR_HIT_END
2112 },
2113 { "Array missing 1 byte argument",
2114 QCBOR_DECODE_MODE_NORMAL,
2115 {"\x98", 1},
2116 QCBOR_ERR_HIT_END
2117 },
2118 { "Map missing 1 byte argument",
2119 QCBOR_DECODE_MODE_NORMAL,
2120 {"\xb8", 1},
2121 QCBOR_ERR_HIT_END
2122 },
2123 { "Tag missing 1 byte argument",
2124 QCBOR_DECODE_MODE_NORMAL,
2125 {"\xd8", 1},
2126 QCBOR_ERR_HIT_END
2127 },
2128 { "Simple missing 1 byte argument",
2129 QCBOR_DECODE_MODE_NORMAL,
2130 {"\xf8", 1},
2131 QCBOR_ERR_HIT_END
2132 },
2133 { "half-precision with 1 byte argument",
2134 QCBOR_DECODE_MODE_NORMAL,
2135 {"\xf9\x00", 2},
2136 QCBOR_ERR_HIT_END
2137 },
2138 { "single-precision with 2 byte argument",
2139 QCBOR_DECODE_MODE_NORMAL,
2140 {"\0xfa\x00\x00", 3},
2141 QCBOR_ERR_HIT_END
2142 },
2143 { "double-precision with 3 byte argument",
2144 QCBOR_DECODE_MODE_NORMAL,
2145 {"\xfb\x00\x00\x00", 4},
2146 QCBOR_ERR_HIT_END
2147 },
Laurence Lundblade2f467f92020-10-09 17:50:11 -07002148
Laurence Lundblade37286c02022-09-03 10:05:02 -07002149#ifndef QCBOR_DISABLE_TAGS
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002150 { "Tag with no content",
2151 QCBOR_DECODE_MODE_NORMAL,
2152 {"\xc0", 1},
2153 QCBOR_ERR_HIT_END
2154 },
Laurence Lundblade37286c02022-09-03 10:05:02 -07002155#else /* QCBOR_DISABLE_TAGS */
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002156 { "Tag with no content",
2157 QCBOR_DECODE_MODE_NORMAL,
2158 {"\xc0", 1},
2159 QCBOR_ERR_TAGS_DISABLED
2160 },
Laurence Lundblade37286c02022-09-03 10:05:02 -07002161#endif /* QCBOR_DISABLE_TAGS */
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002162
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002163 /* Breaks must not occur in definite length arrays and maps */
2164 { "Array of length 1 with sole member replaced by a break",
2165 QCBOR_DECODE_MODE_NORMAL,
2166 {"\x81\xff", 2},
2167 QCBOR_ERR_BAD_BREAK
2168 },
2169 { "Array of length 2 with 2nd member replaced by a break",
2170 QCBOR_DECODE_MODE_NORMAL,
2171 {"\x82\x00\xff", 3},
2172 QCBOR_ERR_BAD_BREAK
2173 },
2174 { "Map of length 1 with sole member label replaced by a break",
2175 QCBOR_DECODE_MODE_NORMAL,
2176 {"\xa1\xff", 2},
2177 QCBOR_ERR_BAD_BREAK
2178 },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002179
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002180 /* Map of length 1 with sole member label replaced by break */
2181 { "Alternate representation that some decoders handle differently",
2182 QCBOR_DECODE_MODE_NORMAL,
2183 {"\xa1\xff\x00", 3},
2184 QCBOR_ERR_BAD_BREAK
2185 },
2186 { "Array of length 1 with 2nd member value replaced by a break",
2187 QCBOR_DECODE_MODE_NORMAL,
2188 {"\xa1\x00\xff", 3},
2189 QCBOR_ERR_BAD_BREAK
2190 },
2191 { "Map of length 2 with 2nd member replaced by a break",
2192 QCBOR_DECODE_MODE_NORMAL,
2193 {"\xa2\x00\x00\xff", 4},
2194 QCBOR_ERR_BAD_BREAK
2195 },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002196
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002197 /* Breaks must not occur on their own out of an indefinite length data item */
2198 { "A bare break is not well formed",
2199 QCBOR_DECODE_MODE_NORMAL,
2200 {"\xff", 1},
2201 QCBOR_ERR_BAD_BREAK
2202 },
2203 { "A bare break after a zero length definite length array",
2204 QCBOR_DECODE_MODE_NORMAL,
2205 {"\x80\xff", 2},
2206 QCBOR_ERR_BAD_BREAK
2207 },
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08002208#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002209 { "A bare break after a zero length indefinite length map",
2210 QCBOR_DECODE_MODE_NORMAL,
2211 {"\x9f\xff\xff", 3},
2212 QCBOR_ERR_BAD_BREAK
2213 },
2214 { "A break inside a definite length array inside an indefenite length array",
2215 QCBOR_DECODE_MODE_NORMAL,
2216 {"\x9f\x81\xff", 3},
2217 QCBOR_ERR_BAD_BREAK
2218 },
2219 { "Complicated mixed nesting with break outside indefinite length array",
2220 QCBOR_DECODE_MODE_NORMAL,
2221 {"\x9f\x82\x9f\x81\x9f\x9f\xff\xff\xff\xff", 10},
2222 QCBOR_ERR_BAD_BREAK },
2223#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
2224
2225 /* Forbidden two byte encodings of simple types */
2226 { "Must use 0xe0 instead",
2227 QCBOR_DECODE_MODE_NORMAL,
2228 {"\xf8\x00", 2},
2229 QCBOR_ERR_BAD_TYPE_7
2230 },
2231 { "Should use 0xe1 instead",
2232 QCBOR_DECODE_MODE_NORMAL,
2233 {"\xf8\x01", 2},
2234 QCBOR_ERR_BAD_TYPE_7
2235 },
2236 { "Should use 0xe2 instead",
2237 QCBOR_DECODE_MODE_NORMAL,
2238 {"\xf8\x02", 2},
2239 QCBOR_ERR_BAD_TYPE_7
2240 }, { "Should use 0xe3 instead",
2241 QCBOR_DECODE_MODE_NORMAL,
2242 {"\xf8\x03", 2},
2243 QCBOR_ERR_BAD_TYPE_7
2244 },
2245 { "Should use 0xe4 instead",
2246 QCBOR_DECODE_MODE_NORMAL,
2247 {"\xf8\x04", 2},
2248 QCBOR_ERR_BAD_TYPE_7
2249 },
2250 { "Should use 0xe5 instead",
2251 QCBOR_DECODE_MODE_NORMAL,
2252 {"\xf8\x05", 2},
2253 QCBOR_ERR_BAD_TYPE_7
2254 },
2255 { "Should use 0xe6 instead",
2256 QCBOR_DECODE_MODE_NORMAL,
2257 {"\xf8\x06", 2},
2258 QCBOR_ERR_BAD_TYPE_7
2259 },
2260 { "Should use 0xe7 instead",
2261 QCBOR_DECODE_MODE_NORMAL,
2262 {"\xf8\x07", 2},
2263 QCBOR_ERR_BAD_TYPE_7
2264 },
2265 { "Should use 0xe8 instead",
2266 QCBOR_DECODE_MODE_NORMAL,
2267 {"\xf8\x08", 2},
2268 QCBOR_ERR_BAD_TYPE_7
2269 },
2270 { "Should use 0xe9 instead",
2271 QCBOR_DECODE_MODE_NORMAL,
2272 {"\xf8\x09", 2},
2273 QCBOR_ERR_BAD_TYPE_7
2274 },
2275 { "Should use 0xea instead",
2276 QCBOR_DECODE_MODE_NORMAL,
2277 {"\xf8\x0a", 2},
2278 QCBOR_ERR_BAD_TYPE_7
2279 },
2280 { "Should use 0xeb instead",
2281 QCBOR_DECODE_MODE_NORMAL,
2282 {"\xf8\x0b", 2},
2283 QCBOR_ERR_BAD_TYPE_7
2284 },
2285 { "Should use 0xec instead",
2286 QCBOR_DECODE_MODE_NORMAL,
2287 {"\xf8\x0c", 2},
2288 QCBOR_ERR_BAD_TYPE_7
2289 },
2290 { "Should use 0xed instead",
2291 QCBOR_DECODE_MODE_NORMAL,
2292 {"\xf8\x0d", 2},
2293 QCBOR_ERR_BAD_TYPE_7
2294 },
2295 { "Should use 0xee instead",
2296 QCBOR_DECODE_MODE_NORMAL,
2297 {"\xf8\x0e", 2},
2298 QCBOR_ERR_BAD_TYPE_7
2299 },
2300 { "Should use 0xef instead",
2301 QCBOR_DECODE_MODE_NORMAL,
2302 {"\xf8\x0f", 2},
2303 QCBOR_ERR_BAD_TYPE_7
2304 },
2305 { "Should use 0xf0 instead",
2306 QCBOR_DECODE_MODE_NORMAL,
2307 {"\xf8\x10", 2},
2308 QCBOR_ERR_BAD_TYPE_7
2309 },
2310 { "Should use 0xf1 instead",
2311 QCBOR_DECODE_MODE_NORMAL,
2312 {"\xf8\x11", 2},
2313 QCBOR_ERR_BAD_TYPE_7
2314 },
2315 { "Should use 0xf2 instead",
2316 QCBOR_DECODE_MODE_NORMAL,
2317 {"\xf8\x12", 2},
2318 QCBOR_ERR_BAD_TYPE_7
2319 },
2320 { "Should use 0xf3 instead",
2321 QCBOR_DECODE_MODE_NORMAL,
2322 {"\xf8\x13", 2},
2323 QCBOR_ERR_BAD_TYPE_7
2324 },
2325 { "Should use 0xf4 instead",
2326 QCBOR_DECODE_MODE_NORMAL,
2327 {"\xf8\x14", 2},
2328 QCBOR_ERR_BAD_TYPE_7
2329 },
2330 { "Should use 0xf5 instead",
2331 QCBOR_DECODE_MODE_NORMAL,
2332 {"\xf8\x15", 2},
2333 QCBOR_ERR_BAD_TYPE_7
2334 },
2335 { "Should use 0xf6 instead",
2336 QCBOR_DECODE_MODE_NORMAL,
2337 {"\xf8\x16", 2},
2338 QCBOR_ERR_BAD_TYPE_7
2339 },
2340 { "Should use 0xef7 instead",
2341 QCBOR_DECODE_MODE_NORMAL,
2342 {"\xf8\x17", 2},
2343 QCBOR_ERR_BAD_TYPE_7
2344 },
2345 { "Should use 0xef8 instead",
2346 QCBOR_DECODE_MODE_NORMAL,
2347 {"\xf8\x18", 2},
2348 QCBOR_ERR_BAD_TYPE_7
2349 },
2350 { "Reserved",
2351 QCBOR_DECODE_MODE_NORMAL,
2352 {"\xf8\x18", 2},
2353 QCBOR_ERR_BAD_TYPE_7
2354 },
2355
2356 /* Maps must have an even number of data items (key & value) */
2357 { "Map with 1 item when it should have 2",
2358 QCBOR_DECODE_MODE_NORMAL,
2359 {"\xa1\x00", 2},
2360 QCBOR_ERR_HIT_END
2361 },
2362 { "Map with 3 item when it should have 4",
2363 QCBOR_DECODE_MODE_NORMAL,
2364 {"\xa2\x00\x00\x00", 2},
2365 QCBOR_ERR_HIT_END
2366 },
2367#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
2368 { "Map with 1 item when it should have 2",
2369 QCBOR_DECODE_MODE_NORMAL,
2370 {"\xbf\x00\xff", 3},
2371 QCBOR_ERR_BAD_BREAK
2372 },
2373 { "Map with 3 item when it should have 4",
2374 QCBOR_DECODE_MODE_NORMAL,
2375 {"\xbf\x00\x00\x00\xff", 5},
2376 QCBOR_ERR_BAD_BREAK
2377 },
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08002378#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002379
2380
Laurence Lundblade37286c02022-09-03 10:05:02 -07002381#ifndef QCBOR_DISABLE_TAGS
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002382 /* In addition to not-well-formed, some invalid CBOR */
2383 { "Text-based date, with an integer",
2384 QCBOR_DECODE_MODE_NORMAL,
2385 {"\xc0\x00", 2},
2386 QCBOR_ERR_BAD_OPT_TAG
2387 },
2388 { "Epoch date, with an byte string",
2389 QCBOR_DECODE_MODE_NORMAL,
2390 {"\xc1\x41\x33", 3},
2391 QCBOR_ERR_BAD_OPT_TAG
2392 },
2393 { "tagged as both epoch and string dates",
2394 QCBOR_DECODE_MODE_NORMAL,
2395 {"\xc1\xc0\x00", 3},
2396 QCBOR_ERR_BAD_OPT_TAG
2397 },
2398 { "big num tagged an int, not a byte string",
2399 QCBOR_DECODE_MODE_NORMAL,
2400 {"\xc2\x00", 2},
2401 QCBOR_ERR_BAD_OPT_TAG
2402 },
Laurence Lundblade37286c02022-09-03 10:05:02 -07002403#else /* QCBOR_DISABLE_TAGS */
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002404 /* In addition to not-well-formed, some invalid CBOR */
2405 { "Text-based date, with an integer",
2406 QCBOR_DECODE_MODE_NORMAL,
2407 {"\xc0\x00", 2},
2408 QCBOR_ERR_TAGS_DISABLED
2409 },
2410 { "Epoch date, with an byte string",
2411 QCBOR_DECODE_MODE_NORMAL,
2412 {"\xc1\x41\x33", 3},
2413 QCBOR_ERR_TAGS_DISABLED
2414 },
2415 { "tagged as both epoch and string dates",
2416 QCBOR_DECODE_MODE_NORMAL,
2417 {"\xc1\xc0\x00", 3},
2418 QCBOR_ERR_TAGS_DISABLED
2419 },
2420 { "big num tagged an int, not a byte string",
2421 QCBOR_DECODE_MODE_NORMAL,
2422 {"\xc2\x00", 2},
2423 QCBOR_ERR_TAGS_DISABLED
2424 },
Laurence Lundblade37286c02022-09-03 10:05:02 -07002425#endif /* QCBOR_DISABLE_TAGS */
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002426};
2427
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002428
2429
2430int32_t
2431DecodeFailureTests(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002432{
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002433 int32_t nResult;
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002434
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08002435 nResult = ProcessDecodeFailures(Failures ,C_ARRAY_COUNT(Failures, struct DecodeFailTestInput));
Laurence Lundblade59289e52019-12-30 13:44:37 -08002436 if(nResult) {
2437 return nResult;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002438 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002439
Laurence Lundblade3a6042e2019-06-28 19:58:04 -07002440 // Corrupt the UsefulInputBuf and see that
2441 // it reflected correctly for CBOR decoding
Laurence Lundbladee6f15112020-07-23 18:44:16 -07002442 QCBORDecodeContext DCtx;
2443 QCBORItem Item;
2444 QCBORError uQCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002445
Laurence Lundbladee6f15112020-07-23 18:44:16 -07002446 QCBORDecode_Init(&DCtx,
2447 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSimpleValues),
2448 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002449
Laurence Lundbladee6f15112020-07-23 18:44:16 -07002450 if((uQCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
2451 return (int32_t)uQCBORError;
2452 }
2453 if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.val.uCount != 10) {
2454 // This wasn't supposed to happen
2455 return -1;
2456 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002457
Laurence Lundbladee6f15112020-07-23 18:44:16 -07002458 DCtx.InBuf.magic = 0; // Reach in and corrupt the UsefulInputBuf
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002459
Laurence Lundbladee6f15112020-07-23 18:44:16 -07002460 uQCBORError = QCBORDecode_GetNext(&DCtx, &Item);
2461 if(uQCBORError != QCBOR_ERR_NO_MORE_ITEMS) {
2462 // Did not get back the error expected
2463 return -2;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002464 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002465
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002466
Laurence Lundblade98427e92020-09-28 21:33:23 -07002467 /*
2468 The max size of a string for QCBOR is SIZE_MAX - 4 so this
2469 tests here can be performed to see that the max length
2470 error check works correctly. See DecodeBytes(). If the max
2471 size was SIZE_MAX, it wouldn't be possible to test this.
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002472
Laurence Lundblade98427e92020-09-28 21:33:23 -07002473 This test will automatocally adapt the all CPU sizes
2474 through the use of SIZE_MAX.
2475 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002476
Laurence Lundblade8510f8c2020-12-01 11:31:16 -08002477 UsefulBuf_MAKE_STACK_UB( HeadBuf, QCBOR_HEAD_BUFFER_SIZE);
Laurence Lundblade98427e92020-09-28 21:33:23 -07002478 UsefulBufC EncodedHead;
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002479
Laurence Lundblade98427e92020-09-28 21:33:23 -07002480 // This makes a CBOR head with a text string that is very long
2481 // but doesn't fill in the bytes of the text string as that is
2482 // not needed to test this part of QCBOR.
2483 EncodedHead = QCBOREncode_EncodeHead(HeadBuf, CBOR_MAJOR_TYPE_TEXT_STRING, 0, SIZE_MAX);
2484
2485 QCBORDecode_Init(&DCtx, EncodedHead, QCBOR_DECODE_MODE_NORMAL);
2486
2487 if(QCBOR_ERR_STRING_TOO_LONG != QCBORDecode_GetNext(&DCtx, &Item)) {
2488 return -4;
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002489 }
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002490
Laurence Lundblade3a6042e2019-06-28 19:58:04 -07002491 return 0;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002492}
2493
2494
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002495/* Try all 256 values of the byte at nLen including recursing for
2496 each of the values to try values at nLen+1 ... up to nLenMax
2497 */
Laurence Lundblade06350ea2020-01-27 19:32:40 -08002498static void ComprehensiveInputRecurser(uint8_t *pBuf, size_t nLen, size_t nLenMax)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002499{
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002500 if(nLen >= nLenMax) {
2501 return;
2502 }
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08002503
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002504 for(int inputByte = 0; inputByte < 256; inputByte++) {
2505 // Set up the input
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002506 pBuf[nLen] = (uint8_t)inputByte;
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08002507 const UsefulBufC Input = {pBuf, nLen+1};
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08002508
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002509 // Get ready to parse
2510 QCBORDecodeContext DCtx;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002511 QCBORDecode_Init(&DCtx, Input, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002512
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002513 // Parse by getting the next item until an error occurs
2514 // Just about every possible decoder error can occur here
2515 // The goal of this test is not to check for the correct
2516 // error since that is not really possible. It is to
2517 // see that there is no crash on hostile input.
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002518 while(1) {
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002519 QCBORItem Item;
2520 QCBORError nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002521 if(nCBORError != QCBOR_SUCCESS) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002522 break;
2523 }
2524 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002525
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002526 ComprehensiveInputRecurser(pBuf, nLen+1, nLenMax);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002527 }
2528}
2529
2530
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03002531int32_t ComprehensiveInputTest(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002532{
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002533 // Size 2 tests 64K inputs and runs quickly
2534 uint8_t pBuf[2];
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002535
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002536 ComprehensiveInputRecurser(pBuf, 0, sizeof(pBuf));
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002537
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002538 return 0;
2539}
2540
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002541
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03002542int32_t BigComprehensiveInputTest(void)
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002543{
2544 // size 3 tests 16 million inputs and runs OK
2545 // in seconds on fast machines. Size 4 takes
2546 // 10+ minutes and 5 half a day on fast
2547 // machines. This test is kept separate from
2548 // the others so as to no slow down the use
2549 // of them as a very frequent regression.
2550 uint8_t pBuf[3]; //
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08002551
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002552 ComprehensiveInputRecurser(pBuf, 0, sizeof(pBuf));
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08002553
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002554 return 0;
2555}
2556
2557
Laurence Lundbladecc7da412020-12-27 00:09:07 -08002558static const uint8_t spDateTestInput[] = {
Laurence Lundblade16a207a2021-09-18 17:22:46 -07002559 /* 1. The valid date string "1985-04-12" */
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002560 0xc0, // tag for string date
2561 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002562
Laurence Lundblade16a207a2021-09-18 17:22:46 -07002563 /* 2. An invalid date string due to wrong tag content type */
Laurence Lundbladec7114722020-08-13 05:11:40 -07002564 0xc0, // tag for string date
2565 0x00, // Wrong type for a string date
2566
Laurence Lundblade16a207a2021-09-18 17:22:46 -07002567 /* 3. A valid epoch date, 1400000000; Tue, 13 May 2014 16:53:20 GMT */
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002568 0xc1, // tag for epoch date
2569 0x1a, 0x53, 0x72, 0x4E, 0x00, // Epoch date 1400000000; Tue, 13 May 2014 16:53:20 GMT
2570
Laurence Lundblade16a207a2021-09-18 17:22:46 -07002571 /* 4. An invalid epoch date due to wrong tag content type */
Laurence Lundbladec7114722020-08-13 05:11:40 -07002572 0xc1,
2573 0x62, 'h', 'i', // wrong type tagged
2574
Laurence Lundblade16a207a2021-09-18 17:22:46 -07002575 /* 5. Valid epoch date tag as content for a two other nested tags */
Laurence Lundblade99615302020-11-29 11:19:47 -08002576 // CBOR_TAG_ENC_AS_B64
Laurence Lundblade16a207a2021-09-18 17:22:46 -07002577 0xcf, 0xd8, 0x16, 0xc1, // Epoch date with extra tags
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002578 0x1a, 0x53, 0x72, 0x4E, 0x01,
2579
Laurence Lundblade16a207a2021-09-18 17:22:46 -07002580 /* 6. Epoch date with value to large to fit into int64 */
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002581 0xc1, // tag for epoch date
2582 0x1b, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, // Too large integer
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002583
Laurence Lundblade16a207a2021-09-18 17:22:46 -07002584 /* 7. Epoch date with single-precision value of 1.1. */
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002585 0xc1, // tag for epoch date
Laurence Lundblade3ed0bca2020-07-14 22:50:10 -07002586 0xfa, 0x3f, 0x8c, 0xcc, 0xcd, // single with value 1.1
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002587
Laurence Lundblade16a207a2021-09-18 17:22:46 -07002588 /* 8. Epoch date with too-large single precision float */
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002589 0xc1, // tag for epoch date
Laurence Lundbladea1ad8782019-11-08 00:12:11 -08002590 0xfa, 0x7f, 0x7f, 0xff, 0xff, // 3.4028234663852886e+38 too large
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002591
Laurence Lundblade16a207a2021-09-18 17:22:46 -07002592 /* 9. Epoch date with slightly too-large double precision value */
Laurence Lundbladea1ad8782019-11-08 00:12:11 -08002593 0xc1, // tag for epoch date
2594 0xfb, 0x43, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 9223372036854775808.000000 just barely too large
2595 //0xfa, 0x7f, 0x7f, 0xff, 0xff // 3.4028234663852886e+38 too large
2596
Laurence Lundblade16a207a2021-09-18 17:22:46 -07002597 /* 10. Epoch date with largest supported double precision value */
Laurence Lundbladea1ad8782019-11-08 00:12:11 -08002598 0xc1, // tag for epoch date
Laurence Lundbladec7114722020-08-13 05:11:40 -07002599 0xfb, 0x43, 0xdf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, // 9223372036854773760 largest supported
2600
Laurence Lundblade16a207a2021-09-18 17:22:46 -07002601 /* 11. Epoch date with single-precision NaN */
Laurence Lundbladec7114722020-08-13 05:11:40 -07002602 0xc1, // tag for epoch date
2603 0xfa, 0x7f, 0xc0, 0x00, 0x00, // Single-precision NaN
2604
Laurence Lundblade16a207a2021-09-18 17:22:46 -07002605 /* 12. Epoch date with double precision plus infinity */
Laurence Lundbladec7114722020-08-13 05:11:40 -07002606 0xc1,
2607 0xfb, 0x7f, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // +infinity
2608
Laurence Lundblade16a207a2021-09-18 17:22:46 -07002609 /* 13. Epoch date with half-precision negative infinity */
Laurence Lundbladec7114722020-08-13 05:11:40 -07002610 0xc1, // tag for epoch date
2611 0xf9, 0xfc, 0x00, // -Infinity
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002612};
2613
2614
Laurence Lundbladec7114722020-08-13 05:11:40 -07002615
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002616// have to check float expected only to within an epsilon
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07002617#ifndef QCBOR_DISABLE_FLOAT_HW_USE
Laurence Lundblade02fcf312020-07-17 02:49:46 -07002618static int CHECK_EXPECTED_DOUBLE(double val, double expected) {
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002619
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002620 double diff = val - expected;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002621
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002622 diff = fabs(diff);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002623
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002624 return diff > 0.0000001;
2625}
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07002626#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002627
2628
Laurence Lundblade37286c02022-09-03 10:05:02 -07002629/* Test date decoding using GetNext() */
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03002630int32_t DateParseTest(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002631{
2632 QCBORDecodeContext DCtx;
Laurence Lundbladec7114722020-08-13 05:11:40 -07002633 QCBORItem Item;
2634 QCBORError uError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002635
Laurence Lundbladeee851742020-01-08 08:37:05 -08002636 QCBORDecode_Init(&DCtx,
2637 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spDateTestInput),
2638 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002639
Laurence Lundblade16a207a2021-09-18 17:22:46 -07002640 /* 1. The valid date string "1985-04-12" */
Laurence Lundbladec7114722020-08-13 05:11:40 -07002641 if((uError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002642 return -1;
Laurence Lundbladec7114722020-08-13 05:11:40 -07002643 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002644 if(Item.uDataType != QCBOR_TYPE_DATE_STRING ||
Laurence Lundbladeba587682024-02-07 16:46:43 -08002645 UsefulBufCompareToSZ(Item.val.string, "1985-04-12")){
Laurence Lundblade67bd5512018-11-02 21:44:06 +07002646 return -2;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002647 }
2648
Laurence Lundblade16a207a2021-09-18 17:22:46 -07002649 /* 2. An invalid date string due to wrong tag content type */
Laurence Lundbladec7114722020-08-13 05:11:40 -07002650 uError = QCBORDecode_GetNext(&DCtx, &Item);
2651 if(uError != QCBOR_ERR_BAD_OPT_TAG) {
Laurence Lundblade67bd5512018-11-02 21:44:06 +07002652 return -3;
Laurence Lundbladec7114722020-08-13 05:11:40 -07002653 }
2654
Laurence Lundblade16a207a2021-09-18 17:22:46 -07002655 /* 3. A valid epoch date, 1400000000; Tue, 13 May 2014 16:53:20 GMT */
2656 uError = QCBORDecode_GetNext(&DCtx, &Item);
2657 if(uError != QCBOR_SUCCESS) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002658 return -4;
2659 }
Laurence Lundblade16a207a2021-09-18 17:22:46 -07002660 if(uError == QCBOR_SUCCESS) {
2661 if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH ||
2662 Item.val.epochDate.nSeconds != 1400000000
Máté Tóth-Pálef5f07a2021-09-17 19:31:37 +02002663#ifndef USEFULBUF_DISABLE_ALL_FLOAT
Laurence Lundblade16a207a2021-09-18 17:22:46 -07002664 || Item.val.epochDate.fSecondsFraction != 0
Máté Tóth-Pálef5f07a2021-09-17 19:31:37 +02002665#endif /* USEFULBUF_DISABLE_ALL_FLOAT */
Laurence Lundblade16a207a2021-09-18 17:22:46 -07002666 ) {
2667 return -5;
2668 }
Laurence Lundbladec7114722020-08-13 05:11:40 -07002669 }
2670
Laurence Lundblade16a207a2021-09-18 17:22:46 -07002671 /* 4. An invalid epoch date due to wrong tag content type */
Laurence Lundbladec7114722020-08-13 05:11:40 -07002672 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_OPT_TAG) {
2673 return -6;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002674 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002675
Laurence Lundblade16a207a2021-09-18 17:22:46 -07002676 /* 5. Valid epoch date tag as content for a two other nested tags */
Laurence Lundblade99615302020-11-29 11:19:47 -08002677 // Epoch date wrapped in an CBOR_TAG_ENC_AS_B64 and an unknown tag.
2678 // The date is decoded and the two tags are returned. This is to
2679 // make sure the wrapping of epoch date in another tag works OK.
Laurence Lundbladec7114722020-08-13 05:11:40 -07002680 if((uError = QCBORDecode_GetNext(&DCtx, &Item))) {
2681 return -7;
2682 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002683 if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH ||
2684 Item.val.epochDate.nSeconds != 1400000001 ||
Máté Tóth-Pálef5f07a2021-09-17 19:31:37 +02002685#ifndef USEFULBUF_DISABLE_ALL_FLOAT
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002686 Item.val.epochDate.fSecondsFraction != 0 ||
Máté Tóth-Pálef5f07a2021-09-17 19:31:37 +02002687#endif /* USEFULBUF_DISABLE_ALL_FLOAT */
Laurence Lundblade99615302020-11-29 11:19:47 -08002688 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_ENC_AS_B64)) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002689 return -8;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002690 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002691
Laurence Lundblade16a207a2021-09-18 17:22:46 -07002692 /* 6. Epoch date with value to large to fit into int64 */
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002693 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_DATE_OVERFLOW) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002694 return -9;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002695 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002696
Laurence Lundblade16a207a2021-09-18 17:22:46 -07002697 /* 7. Epoch date with single-precision value of 1.1. */
2698 uError = QCBORDecode_GetNext(&DCtx, &Item);
2699 if(uError != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002700 return -10;
2701 }
Laurence Lundblade16a207a2021-09-18 17:22:46 -07002702 if(uError == QCBOR_SUCCESS) {
2703 if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH ||
2704 Item.val.epochDate.nSeconds != 1
2705#ifndef QCBOR_DISABLE_FLOAT_HW_USE
2706 || CHECK_EXPECTED_DOUBLE(Item.val.epochDate.fSecondsFraction, 0.1)
Máté Tóth-Pálef5f07a2021-09-17 19:31:37 +02002707#endif /* USEFULBUF_DISABLE_ALL_FLOAT */
Laurence Lundblade16a207a2021-09-18 17:22:46 -07002708 ) {
2709 return -11;
2710 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002711 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002712
Laurence Lundblade16a207a2021-09-18 17:22:46 -07002713 /* 8. Epoch date with too-large single-precision float */
2714 uError = QCBORDecode_GetNext(&DCtx, &Item);
2715 if(uError != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_DATE_OVERFLOW)) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002716 return -12;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002717 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002718
Laurence Lundblade16a207a2021-09-18 17:22:46 -07002719 /* 9. Epoch date with slightly too-large double-precision value */
2720 uError = QCBORDecode_GetNext(&DCtx, &Item);
2721 if(uError != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_DATE_OVERFLOW)) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002722 return -13;
Laurence Lundbladea1ad8782019-11-08 00:12:11 -08002723 }
2724
Laurence Lundblade16a207a2021-09-18 17:22:46 -07002725 /* 10. Epoch date with largest supported double-precision value */
2726 uError = QCBORDecode_GetNext(&DCtx, &Item);
2727 if(uError != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)) {
2728 return -14;
2729 }
2730 if(uError == QCBOR_SUCCESS) {
2731 if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH ||
2732 Item.val.epochDate.nSeconds != 9223372036854773760
2733#ifndef QCBOR_DISABLE_FLOAT_HW_USE
2734 || Item.val.epochDate.fSecondsFraction != 0.0
2735#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
2736 ) {
2737 return -14;
2738 }
2739 }
Laurence Lundbladec7114722020-08-13 05:11:40 -07002740
Laurence Lundblade16a207a2021-09-18 17:22:46 -07002741 /* 11. Epoch date with single-precision NaN */
2742 if(QCBORDecode_GetNext(&DCtx, &Item) != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_DATE_OVERFLOW)) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002743 return -15;
2744 }
2745
Laurence Lundblade16a207a2021-09-18 17:22:46 -07002746 /* 12. Epoch date with double-precision plus infinity */
2747 if(QCBORDecode_GetNext(&DCtx, &Item) != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_DATE_OVERFLOW)) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002748 return -16;
2749 }
2750
Laurence Lundblade16a207a2021-09-18 17:22:46 -07002751 /* 13. Epoch date with half-precision negative infinity */
2752 uError = QCBORDecode_GetNext(&DCtx, &Item);
2753 if(uError != FLOAT_ERR_CODE_NO_HALF_PREC_NO_FLOAT_HW(QCBOR_ERR_DATE_OVERFLOW)) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002754 return -17;
2755 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002756
2757 return 0;
2758}
2759
Laurence Lundblade37286c02022-09-03 10:05:02 -07002760
Laurence Lundblade4b270642020-08-14 12:53:07 -07002761/*
2762 Test cases covered here. Some items cover more than one of these.
2763 positive integer (zero counts as a positive integer)
2764 negative integer
2765 half-precision float
2766 single-precision float
2767 double-precision float
Laurence Lundbladec7114722020-08-13 05:11:40 -07002768
Laurence Lundblade4b270642020-08-14 12:53:07 -07002769 float Overflow error
2770 Wrong type error for epoch
2771 Wrong type error for date string
2772 float disabled error
2773 half-precision disabled error
2774 -Infinity
2775 Slightly too large integer
2776 Slightly too far from zero
Laurence Lundbladec7114722020-08-13 05:11:40 -07002777
Laurence Lundblade4b270642020-08-14 12:53:07 -07002778 Get epoch by int
2779 Get string by int
2780 Get epoch by string
2781 Get string by string
2782 Fail to get epoch by wrong int label
2783 Fail to get string by wrong string label
2784 Fail to get epoch by string because it is invalid
2785 Fail to get epoch by int because it is invalid
2786
2787 Untagged values
2788 */
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08002789static const uint8_t spSpiffyDateTestInput[] = {
Laurence Lundblade37286c02022-09-03 10:05:02 -07002790 0x87, // array of 7 items
2791
2792 0xa6, // Open a map for tests involving untagged items with labels.
2793
2794 // Untagged integer 0
2795 0x08,
2796 0x00,
2797
2798 // Utagged date string with string label y
2799 0x61, 0x79,
2800 0x6a, '2','0','8','5','-','0','4','-','1','2', // Untagged date string
2801
2802 // Untagged single-precision float with value 3.14 with string label x
2803 0x61, 0x78,
2804 0xFA, 0x40, 0x48, 0xF5, 0xC3,
2805
2806 // Untagged half-precision float with value -2
2807 0x09,
2808 0xF9, 0xC0, 0x00,
2809
2810 /* Untagged date-only date string */
2811 0x18, 0x63,
2812 0x6A, 0x31, 0x39, 0x38, 0x35, 0x2D, 0x30, 0x34, 0x2D, 0x31, 0x32, /* "1985-04-12" */
2813
2814 /* Untagged days-count epoch date */
2815 0x11,
2816 0x19, 0x0F, 0x9A, /* 3994 */
2817
2818 // End of map, back to array
2819
2820 0xa7, // Open map of tagged items with labels
2821
2822 0x00,
2823 0xc0, // tag for string date
2824 0x6a, '1','9','8','5','-','0','4','-','1','2', // Tagged date string
2825
2826
2827 0x01,
2828 0xda, 0x03, 0x03, 0x03, 0x03, // An additional tag
2829 0xc1, // tag for epoch date
2830 0x1a, 0x53, 0x72, 0x4E, 0x00, // Epoch date 1400000000; Tue, 13 May 2014 16:53:20 GMT
2831
2832 0x05,
2833 0xc1,
2834 0xfb, 0xc3, 0xdf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, // -9223372036854773760 largest negative
2835
2836
2837 0x07,
2838 0xc1, // tag for epoch date
2839 0xfb, 0x43, 0xdf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, // 9223372036854773760 largest supported
2840
2841 /* Tagged days-count epoch date */
2842 0x63, 0x53, 0x44, 0x45,
2843 0xD8, 0x64, /* tag(100) */
2844 0x39, 0x29, 0xB3, /* -10676 */
2845
2846 // Untagged -1000 with label z
2847 0x61, 0x7a,
2848 0xda, 0x01, 0x01, 0x01, 0x01, // An additional tag
2849 0x39, 0x03, 0xe7,
2850
2851 /* Tagged date-only date string */
2852 0x63, 0x53, 0x44, 0x53,
2853 0xD9, 0x03, 0xEC,
2854 0x6A, 0x31, 0x39, 0x38, 0x35, 0x2D, 0x30, 0x34, 0x2D, 0x31, 0x32, /* "1985-04-12" */
2855
2856 // End of map of tagged items
Laurence Lundblade4b270642020-08-14 12:53:07 -07002857
2858 0xc1,
2859 0xfb, 0xc3, 0xdf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, // -9.2233720368547748E+18, too negative
2860
Laurence Lundbladec7114722020-08-13 05:11:40 -07002861 0xc1, // tag for epoch date
Laurence Lundblade4b270642020-08-14 12:53:07 -07002862 0x1b, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, // Too-large integer
2863
2864 0xc1, // tag for epoch date
2865 0xf9, 0xfc, 0x00, // Half-precision -Infinity
2866
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07002867 // These two at the end because they are unrecoverable errors
2868 0xc1, // tag for epoch date
2869 0x80, // Erroneous empty array as content for date
2870
2871 0xc0, // tag for string date
2872 0xa0 // Erroneous empty map as content for date
Laurence Lundbladec7114722020-08-13 05:11:40 -07002873};
2874
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03002875int32_t SpiffyDateDecodeTest(void)
Laurence Lundbladec7114722020-08-13 05:11:40 -07002876{
2877 QCBORDecodeContext DC;
Laurence Lundblade4b270642020-08-14 12:53:07 -07002878 QCBORError uError;
Laurence Lundblade37286c02022-09-03 10:05:02 -07002879 int64_t nEpochDate3, nEpochDate5,
2880 nEpochDate4, nEpochDate6,
2881 nEpochDays2;
2882 UsefulBufC StringDate1, StringDate2, StringDays2;
Laurence Lundbladec7114722020-08-13 05:11:40 -07002883
2884 QCBORDecode_Init(&DC,
Laurence Lundblade4b270642020-08-14 12:53:07 -07002885 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSpiffyDateTestInput),
Laurence Lundbladec7114722020-08-13 05:11:40 -07002886 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade37286c02022-09-03 10:05:02 -07002887
2888 /* Items are in an array or map to test look up by label and other
2889 * that might not occur in isolated items. But it does make the
2890 * test a bit messy. */
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07002891 QCBORDecode_EnterArray(&DC, NULL);
Laurence Lundbladec7114722020-08-13 05:11:40 -07002892
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07002893 QCBORDecode_EnterMap(&DC, NULL);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002894
Laurence Lundblade4b270642020-08-14 12:53:07 -07002895 // A single-precision date
Laurence Lundblade9b334962020-08-27 10:55:53 -07002896 QCBORDecode_GetEpochDateInMapSZ(&DC, "x", QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
2897 &nEpochDate5);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002898 uError = QCBORDecode_GetAndResetError(&DC);
Laurence Lundblade16a207a2021-09-18 17:22:46 -07002899 if(uError != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002900 return 104;
2901 }
Laurence Lundblade16a207a2021-09-18 17:22:46 -07002902 if(uError == QCBOR_SUCCESS) {
2903 if(nEpochDate5 != 3) {
2904 return 103;
2905 }
2906 }
Laurence Lundbladec7114722020-08-13 05:11:40 -07002907
Laurence Lundblade9b334962020-08-27 10:55:53 -07002908 // A half-precision date with value -2 FFF
2909 QCBORDecode_GetEpochDateInMapN(&DC, 9, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
2910 &nEpochDate4);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002911 uError = QCBORDecode_GetAndResetError(&DC);
Laurence Lundblade16a207a2021-09-18 17:22:46 -07002912 if(uError != FLOAT_ERR_CODE_NO_HALF_PREC_NO_FLOAT_HW(QCBOR_SUCCESS)) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002913 return 106;
2914 }
Laurence Lundblade16a207a2021-09-18 17:22:46 -07002915 if(uError == QCBOR_SUCCESS) {
2916 if(nEpochDate4 != -2) {
2917 return 105;
2918 }
2919 }
Laurence Lundblade4b270642020-08-14 12:53:07 -07002920
2921 // Fail to get an epoch date by string label
Laurence Lundblade9b334962020-08-27 10:55:53 -07002922 QCBORDecode_GetEpochDateInMapSZ(&DC, "no-label",
2923 QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
2924 &nEpochDate6);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002925 uError = QCBORDecode_GetAndResetError(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002926 if(uError != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002927 return 107;
2928 }
2929
2930 // Fail to get an epoch date by integer label
Laurence Lundblade9b334962020-08-27 10:55:53 -07002931 QCBORDecode_GetEpochDateInMapN(&DC, 99999, QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
2932 &nEpochDate6);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002933 uError = QCBORDecode_GetAndResetError(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002934 if(uError != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002935 return 108;
2936 }
2937
2938 // Fail to get a string date by string label
Laurence Lundblade9b334962020-08-27 10:55:53 -07002939 QCBORDecode_GetDateStringInMapSZ(&DC, "no-label",
2940 QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
2941 &StringDate1);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002942 uError = QCBORDecode_GetAndResetError(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002943 if(uError != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002944 return 109;
2945 }
2946
2947 // Fail to get a string date by integer label
Laurence Lundblade9b334962020-08-27 10:55:53 -07002948 QCBORDecode_GetDateStringInMapN(&DC, 99999, QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
2949 &StringDate1);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002950 uError = QCBORDecode_GetAndResetError(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002951 if(uError != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002952 return 110;
2953 }
2954
2955 // The rest of these succeed even if float features are disabled
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002956
Laurence Lundblade37286c02022-09-03 10:05:02 -07002957
2958 // Untagged integer 0
2959 QCBORDecode_GetEpochDateInMapN(&DC, 8, QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
2960 &nEpochDate3);
2961 // Untagged date string
2962 QCBORDecode_GetDateStringInMapSZ(&DC, "y", QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
2963 &StringDate2);
2964
2965 QCBORDecode_GetDaysStringInMapN(&DC, 99, QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
2966 &StringDays2);
2967
2968 QCBORDecode_GetEpochDaysInMapN(&DC, 17, QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
2969 &nEpochDays2);
2970
2971 QCBORDecode_ExitMap(&DC);
2972 if(QCBORDecode_GetError(&DC) != QCBOR_SUCCESS) {
2973 return 3001;
2974 }
2975
2976 // The map of tagged items
2977 QCBORDecode_EnterMap(&DC, NULL);
2978
2979#ifndef QCBOR_DISABLE_TAGS
2980 int64_t nEpochDate2,
2981 nEpochDateFail,
2982 nEpochDate1400000000, nEpochDays1;
2983 UsefulBufC StringDays1;
2984 uint64_t uTag1, uTag2;
2985
2986 // Tagged date string
2987 QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
2988 &StringDate1);
2989
Laurence Lundblade4b270642020-08-14 12:53:07 -07002990 // Epoch date 1400000000; Tue, 13 May 2014 16:53:20 GMT
Laurence Lundblade9b334962020-08-27 10:55:53 -07002991 QCBORDecode_GetEpochDateInMapN(&DC,
2992 1,
2993 QCBOR_TAG_REQUIREMENT_TAG |
2994 QCBOR_TAG_REQUIREMENT_ALLOW_ADDITIONAL_TAGS,
2995 &nEpochDate1400000000);
2996 uTag1 = QCBORDecode_GetNthTagOfLast(&DC, 0);
Laurence Lundblade37286c02022-09-03 10:05:02 -07002997
2998 // Get largest negative double precision epoch date allowed
2999 QCBORDecode_GetEpochDateInMapN(&DC,
3000 5,
3001 QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG |
3002 QCBOR_TAG_REQUIREMENT_ALLOW_ADDITIONAL_TAGS,
3003 &nEpochDate2);
3004 uError = QCBORDecode_GetAndResetError(&DC);
3005 if(uError != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)) {
3006 return 102;
3007 }
3008 if(uError == QCBOR_SUCCESS) {
3009 if(nEpochDate2 != -9223372036854773760LL) {
3010 return 101;
3011 }
3012 }
3013
Laurence Lundblade4b270642020-08-14 12:53:07 -07003014 // Untagged -1000 with label z
Laurence Lundblade9b334962020-08-27 10:55:53 -07003015 QCBORDecode_GetEpochDateInMapSZ(&DC,
3016 "z",
3017 QCBOR_TAG_REQUIREMENT_NOT_A_TAG |
3018 QCBOR_TAG_REQUIREMENT_ALLOW_ADDITIONAL_TAGS,
3019 &nEpochDate6);
3020 uTag2 = QCBORDecode_GetNthTagOfLast(&DC, 0);
Laurence Lundblade4b270642020-08-14 12:53:07 -07003021
Laurence Lundblade37286c02022-09-03 10:05:02 -07003022
3023 // Get largest double precision epoch date allowed
3024 QCBORDecode_GetEpochDateInMapN(&DC, 7, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
3025 &nEpochDate2);
3026 uError = QCBORDecode_GetAndResetError(&DC);
3027 if(uError != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)) {
3028 return 112;
3029 }
3030 if(uError == QCBOR_SUCCESS) {
3031 if(nEpochDate2 != 9223372036854773760ULL) {
3032 return 111;
3033 }
3034 }
3035
Laurence Lundblade46d63e92021-05-13 11:37:10 -07003036 /* The days format is much simpler than the date format
3037 * because it can't be a floating point value. The test
3038 * of the spiffy decode functions sufficiently covers
3039 * the test of the non-spiffy decode days date decoding.
3040 * There is no full fan out of the error conditions
3041 * and decode options as that is implemented by code
3042 * that is tested well by the date testing above.
3043 */
3044 QCBORDecode_GetDaysStringInMapSZ(&DC, "SDS", QCBOR_TAG_REQUIREMENT_TAG,
3045 &StringDays1);
3046
Laurence Lundblade46d63e92021-05-13 11:37:10 -07003047 QCBORDecode_GetEpochDaysInMapSZ(&DC, "SDE", QCBOR_TAG_REQUIREMENT_TAG,
3048 &nEpochDays1);
3049
Laurence Lundblade4b270642020-08-14 12:53:07 -07003050 QCBORDecode_ExitMap(&DC);
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07003051 if(QCBORDecode_GetError(&DC) != QCBOR_SUCCESS) {
3052 return 3001;
3053 }
3054
Laurence Lundblade37286c02022-09-03 10:05:02 -07003055 // Too-negative float, -9.2233720368547748E+18
3056 QCBORDecode_GetEpochDate(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nEpochDateFail);
3057 uError = QCBORDecode_GetAndResetError(&DC);
3058 if(uError != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_DATE_OVERFLOW)) {
3059 return 1111;
3060 }
3061
3062 // Too-large integer
3063 QCBORDecode_GetEpochDate(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nEpochDateFail);
3064 uError = QCBORDecode_GetAndResetError(&DC);
3065 if(uError != QCBOR_ERR_DATE_OVERFLOW) {
3066 return 1;
3067 }
3068
3069 // Half-precision minus infinity
3070 QCBORDecode_GetEpochDate(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nEpochDateFail);
3071 uError = QCBORDecode_GetAndResetError(&DC);
3072 if(uError != FLOAT_ERR_CODE_NO_HALF_PREC_NO_FLOAT_HW(QCBOR_ERR_DATE_OVERFLOW)) {
3073 return 2;
3074 }
3075
3076
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07003077 // Bad content for epoch date
3078 QCBORDecode_GetEpochDate(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nEpochDateFail);
3079 uError = QCBORDecode_GetAndResetError(&DC);
3080 if(uError != QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT) {
3081 return 3;
3082 }
3083
3084 // Bad content for string date
3085 QCBORDecode_GetDateString(&DC, QCBOR_TAG_REQUIREMENT_TAG, &StringDate1);
3086 uError = QCBORDecode_GetAndResetError(&DC);
3087 if(uError != QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT) {
3088 return 4;
3089 }
3090
Laurence Lundblade4b270642020-08-14 12:53:07 -07003091 QCBORDecode_ExitArray(&DC);
3092 uError = QCBORDecode_Finish(&DC);
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07003093 if(uError != QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT) {
Laurence Lundblade9b334962020-08-27 10:55:53 -07003094 return 1000 + (int32_t)uError;
Laurence Lundblade4b270642020-08-14 12:53:07 -07003095 }
Laurence Lundblade37286c02022-09-03 10:05:02 -07003096#else /* QCBOR_DISABLE_TAGS */
3097 QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
3098 &StringDate1);
3099 uError = QCBORDecode_GetAndResetError(&DC);
3100 if(uError != QCBOR_ERR_TAGS_DISABLED) {
3101 return 4;
3102 }
3103#endif /* QCBOR_DISABLE_TAGS */
3104
3105
3106#ifndef QCBOR_DISABLE_TAGS
Laurence Lundblade4b270642020-08-14 12:53:07 -07003107
Laurence Lundblade9b334962020-08-27 10:55:53 -07003108 if(nEpochDate1400000000 != 1400000000) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07003109 return 200;
3110 }
3111
Laurence Lundblade9b334962020-08-27 10:55:53 -07003112 if(uTag1 != 0x03030303) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07003113 return 201;
3114 }
3115
Laurence Lundblade37286c02022-09-03 10:05:02 -07003116 if(nEpochDays1 != -10676) {
3117 return 205;
Laurence Lundblade4b270642020-08-14 12:53:07 -07003118 }
3119
Laurence Lundblade37286c02022-09-03 10:05:02 -07003120 if(UsefulBuf_Compare(StringDays1, UsefulBuf_FromSZ("1985-04-12"))) {
3121 return 207;
Laurence Lundblade4b270642020-08-14 12:53:07 -07003122 }
3123
Laurence Lundblade9b334962020-08-27 10:55:53 -07003124 if(uTag2 != 0x01010101) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07003125 return 204;
3126 }
3127
Laurence Lundblade37286c02022-09-03 10:05:02 -07003128 if(nEpochDate6 != -1000) {
3129 return 203;
Laurence Lundblade46d63e92021-05-13 11:37:10 -07003130 }
3131
Laurence Lundblade9b334962020-08-27 10:55:53 -07003132 if(UsefulBuf_Compare(StringDate1, UsefulBuf_FromSZ("1985-04-12"))) {
3133 return 205;
3134 }
3135
Laurence Lundblade37286c02022-09-03 10:05:02 -07003136#endif /* QCBOR_DISABLE_TAGS */
3137
3138 if(nEpochDate3 != 0) {
3139 return 202;
3140 }
3141
3142 if(nEpochDays2 != 3994) {
Laurence Lundblade9b334962020-08-27 10:55:53 -07003143 return 206;
3144 }
3145
Laurence Lundblade37286c02022-09-03 10:05:02 -07003146 if(UsefulBuf_Compare(StringDate2, UsefulBuf_FromSZ("2085-04-12"))) {
3147 return 206;
Laurence Lundblade46d63e92021-05-13 11:37:10 -07003148 }
3149
3150 if(UsefulBuf_Compare(StringDays2, UsefulBuf_FromSZ("1985-04-12"))) {
3151 return 208;
3152 }
3153
Laurence Lundbladec7114722020-08-13 05:11:40 -07003154 return 0;
3155}
3156
3157
Laurence Lundblade9b334962020-08-27 10:55:53 -07003158// Input for one of the tagging tests
Laurence Lundbladecc7da412020-12-27 00:09:07 -08003159static const uint8_t spTagInput[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003160 0xd9, 0xd9, 0xf7, // CBOR magic number
Laurence Lundblade9b334962020-08-27 10:55:53 -07003161 0x81, // Array of one
3162 0xd8, 0x04, // non-preferred serialization of tag 4, decimal fraction
3163 0x82, // Array of two that is the faction 1/3
3164 0x01,
3165 0x03,
3166
3167 /*
3168 More than 4 tags on an item 225(226(227(228(229([])))))
3169 */
3170 0xd8, 0xe1,
3171 0xd8, 0xe2,
3172 0xd8, 0xe3,
3173 0xd8, 0xe4,
3174 0xd8, 0xe5,
3175 0x80,
3176
3177 /* tag 10489608748473423768(
3178 2442302356(
3179 21590(
3180 240(
3181 []))))
3182 */
3183 0xdb, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98,
3184 0xda, 0x91, 0x92, 0x93, 0x94,
3185 0xd9, 0x54, 0x56,
3186 0xd8, 0xf0,
3187 0x80,
3188
3189 /* tag 21590(
3190 10489608748473423768(
3191 2442302357(
3192 65534(
3193 []))))
3194 */
3195 0xdb, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x54, 0x56,
3196 0xdb, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98,
3197 0xda, 0x91, 0x92, 0x93, 0x95,
3198 0xd9, 0xff, 0xfe,
3199 0x80,
3200
3201 /* Make sure to blow past the limit of tags that must be mapped.
3202 works in conjuntion with entries above.
3203 269488144(269488145(269488146(269488147([]))))
3204 */
3205 0xda, 0x10, 0x10, 0x10, 0x10,
3206 0xda, 0x10, 0x10, 0x10, 0x11,
3207 0xda, 0x10, 0x10, 0x10, 0x12,
3208 0xda, 0x10, 0x10, 0x10, 0x13,
3209 0x80,
3210
3211 /* An invalid decimal fraction with an additional tag */
3212 0xd9, 0xff, 0xfa,
3213 0xd8, 0x02, // non-preferred serialization of tag 2, a big num
3214 0x00, // the integer 0; should be a byte string
3215};
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003216
Laurence Lundblade59289e52019-12-30 13:44:37 -08003217/*
3218 DB 9192939495969798 # tag(10489608748473423768)
Laurence Lundblade9b334962020-08-27 10:55:53 -07003219 80 # array(0)
Laurence Lundblade59289e52019-12-30 13:44:37 -08003220 */
Laurence Lundbladecc7da412020-12-27 00:09:07 -08003221static const uint8_t spEncodedLargeTag[] = {0xdb, 0x91, 0x92, 0x93, 0x94, 0x95,
Laurence Lundbladeee851742020-01-08 08:37:05 -08003222 0x96, 0x97, 0x98, 0x80};
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003223
Laurence Lundblade59289e52019-12-30 13:44:37 -08003224/*
3225DB 9192939495969798 # tag(10489608748473423768)
3226 D8 88 # tag(136)
3227 C6 # tag(6)
3228 C7 # tag(7)
3229 80 # array(0)
3230*/
Laurence Lundbladecc7da412020-12-27 00:09:07 -08003231static const uint8_t spLotsOfTags[] = {0xdb, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96,
Laurence Lundbladeee851742020-01-08 08:37:05 -08003232 0x97, 0x98, 0xd8, 0x88, 0xc6, 0xc7, 0x80};
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003233
3234/*
Laurence Lundblade9b334962020-08-27 10:55:53 -07003235 55799(55799(55799({
3236 6(7(-23)): 5859837686836516696(7({
3237 7(-20): 11({
3238 17(-18): 17(17(17("Organization"))),
3239 9(-17): 773("SSG"),
3240 -15: 16(17(6(7("Confusion")))),
3241 17(-16): 17("San Diego"),
3242 17(-14): 17("US")
3243 }),
3244 23(-19): 19({
3245 -11: 9({
3246 -9: -7
3247 }),
3248 90599561(90599561(90599561(-10))): 12(h'0102030405060708090A')
3249 })
3250 })),
3251 16(-22): 23({
3252 11(8(7(-5))): 8(-3)
3253 })
3254 })))
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003255 */
Laurence Lundbladecc7da412020-12-27 00:09:07 -08003256static const uint8_t spCSRWithTags[] = {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003257 0xd9, 0xd9, 0xf7, 0xd9, 0xd9, 0xf7, 0xd9, 0xd9, 0xf7, 0xa2,
3258 0xc6, 0xc7, 0x36,
3259 0xdb, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0xc7, 0xa2,
3260 0xda, 0x00, 0x00, 0x00, 0x07, 0x33,
3261 0xcb, 0xa5,
3262 0xd1, 0x31,
3263 0xd1, 0xd1, 0xd1, 0x6c,
3264 0x4f, 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74, 0x69, 0x6f, 0x6e,
3265 0xc9, 0x30,
3266 0xd9, 0x03, 0x05, 0x63,
3267 0x53, 0x53, 0x47,
3268 0x2e,
Laurence Lundblade9b334962020-08-27 10:55:53 -07003269 0xd0, 0xd1, 0xc6, 0xc7,
3270 0x69,
3271 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73, 0x69, 0x6f, 0x6e,
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003272 0xd1, 0x2f,
3273 0xd1, 0x69,
3274 0x53, 0x61, 0x6e, 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f,
3275 0xd1, 0x2d,
3276 0xd1, 0x62,
3277 0x55, 0x53,
3278 0xd7, 0x32,
3279 0xd3, 0xa2,
3280 0x2a,
3281 0xc9, 0xa1,
3282 0x28,
3283 0x26,
3284 0xda, 0x05, 0x66, 0x70, 0x89, 0xda, 0x05, 0x66, 0x70, 0x89, 0xda, 0x05, 0x66, 0x70, 0x89, 0x29,
3285 0xcc, 0x4a,
3286 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,0x07, 0x08, 0x09, 0x0a,
3287 0xd0, 0x35,
3288 0xd7, 0xa1,
3289 0xcb, 0xc8, 0xc7, 0x24,
3290 0xc8, 0x22};
3291
Laurence Lundblade9b334962020-08-27 10:55:53 -07003292
Laurence Lundbladecc7da412020-12-27 00:09:07 -08003293static const uint8_t spSpiffyTagInput[] = {
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08003294 0x85, // Open array
Laurence Lundblade9b334962020-08-27 10:55:53 -07003295
3296 0xc0, // tag for string date
3297 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string
3298
3299 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string
3300
3301 0x4a, '1','9','8','5','-','0','4','-','1','2', // Date string in byte string
3302
3303 0xd8, 0x23, // tag for regex
3304 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string
3305
3306 0xc0, // tag for string date
3307 0x4a, '1','9','8','5','-','0','4','-','1','2', // Date string in byte string
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07003308
3309 // This last case makes the array untraversable because it is
3310 // an uncrecoverable error. Make sure it stays last and is the only
3311 // instance so the other tests can work.
Laurence Lundblade9b334962020-08-27 10:55:53 -07003312};
3313
3314
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003315static int32_t CheckCSRMaps(QCBORDecodeContext *pDC);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003316
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003317
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03003318int32_t OptTagParseTest(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003319{
3320 QCBORDecodeContext DCtx;
Laurence Lundblade9b334962020-08-27 10:55:53 -07003321 QCBORItem Item;
3322 QCBORError uError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003323
Laurence Lundbladeee851742020-01-08 08:37:05 -08003324 QCBORDecode_Init(&DCtx,
Laurence Lundblade9b334962020-08-27 10:55:53 -07003325 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTagInput),
Laurence Lundbladeee851742020-01-08 08:37:05 -08003326 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003327
Laurence Lundblade9b334962020-08-27 10:55:53 -07003328 /*
3329 This test matches the magic number tag and the fraction tag
3330 55799([...])
3331 */
3332 uError = QCBORDecode_GetNext(&DCtx, &Item);
3333 if(uError != QCBOR_SUCCESS) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003334 return -2;
3335 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003336 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003337 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC)) {
3338 return -3;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003339 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003340
Laurence Lundblade9b334962020-08-27 10:55:53 -07003341 /*
3342 4([1,3])
3343 */
3344 uError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundbladedd6e76e2021-03-10 01:54:01 -07003345#ifdef QCBOR_DISABLE_EXP_AND_MANTISSA
Laurence Lundblade9b334962020-08-27 10:55:53 -07003346 if(uError != QCBOR_SUCCESS ||
3347 Item.uDataType != QCBOR_TYPE_ARRAY ||
3348 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_DECIMAL_FRACTION) ||
3349 QCBORDecode_GetNthTag(&DCtx, &Item, 0) != CBOR_TAG_DECIMAL_FRACTION ||
3350 QCBORDecode_GetNthTag(&DCtx, &Item, 1) != CBOR_TAG_INVALID64 ||
3351 QCBORDecode_GetNthTag(&DCtx, &Item, 2) != CBOR_TAG_INVALID64 ||
3352 QCBORDecode_GetNthTag(&DCtx, &Item, 3) != CBOR_TAG_INVALID64 ||
3353 QCBORDecode_GetNthTag(&DCtx, &Item, 4) != CBOR_TAG_INVALID64 ||
3354 Item.val.uCount != 2) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003355 return -4;
3356 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07003357 // consume the items in the array
3358 uError = QCBORDecode_GetNext(&DCtx, &Item);
3359 uError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundblade59289e52019-12-30 13:44:37 -08003360
Laurence Lundbladedd6e76e2021-03-10 01:54:01 -07003361#else /* QCBOR_DISABLE_EXP_AND_MANTISSA */
Laurence Lundblade9b334962020-08-27 10:55:53 -07003362 if(uError != QCBOR_SUCCESS ||
3363 Item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION ||
3364 QCBORDecode_GetNthTag(&DCtx, &Item, 0) != CBOR_TAG_INVALID64 ||
3365 QCBORDecode_GetNthTag(&DCtx, &Item, 1) != CBOR_TAG_INVALID64 ||
3366 QCBORDecode_GetNthTag(&DCtx, &Item, 2) != CBOR_TAG_INVALID64 ||
3367 QCBORDecode_GetNthTag(&DCtx, &Item, 3) != CBOR_TAG_INVALID64 ||
3368 QCBORDecode_GetNthTag(&DCtx, &Item, 4) != CBOR_TAG_INVALID64 ) {
3369 return -5;
Laurence Lundblade59289e52019-12-30 13:44:37 -08003370 }
Laurence Lundbladedd6e76e2021-03-10 01:54:01 -07003371#endif /* QCBOR_DISABLE_EXP_AND_MANTISSA */
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003372
Laurence Lundblade9b334962020-08-27 10:55:53 -07003373 /*
3374 More than 4 tags on an item 225(226(227(228(229([])))))
3375 */
3376 uError = QCBORDecode_GetNext(&DCtx, &Item);
3377 if(uError != QCBOR_ERR_TOO_MANY_TAGS) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003378 return -6;
3379 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07003380
Laurence Lundblade88e9db22020-11-02 03:56:33 -08003381 if(QCBORDecode_GetNthTag(&DCtx, &Item, 0) != CBOR_TAG_INVALID64) {
3382 return -106;
3383 }
3384
3385
Laurence Lundblade9b334962020-08-27 10:55:53 -07003386 /* tag 10489608748473423768(
3387 2442302356(
3388 21590(
3389 240(
3390 []))))
3391 */
3392 uError = QCBORDecode_GetNext(&DCtx, &Item);
3393 if(uError != QCBOR_SUCCESS ||
3394 Item.uDataType != QCBOR_TYPE_ARRAY ||
3395 QCBORDecode_GetNthTag(&DCtx, &Item, 3) != 10489608748473423768ULL ||
3396 QCBORDecode_GetNthTag(&DCtx, &Item, 2) != 2442302356ULL ||
3397 QCBORDecode_GetNthTag(&DCtx, &Item, 1) != 21590ULL ||
3398 QCBORDecode_GetNthTag(&DCtx, &Item, 0) != 240ULL) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003399 return -7;
Laurence Lundblade9b334962020-08-27 10:55:53 -07003400 }
3401
3402 /* tag 21590(
3403 10489608748473423768(
3404 2442302357(
3405 21591(
3406 []))))
3407 */
3408 uError = QCBORDecode_GetNext(&DCtx, &Item);
3409 if(uError != QCBOR_SUCCESS ||
3410 Item.uDataType != QCBOR_TYPE_ARRAY ||
3411 QCBORDecode_GetNthTag(&DCtx, &Item, 0) != 65534ULL ||
3412 QCBORDecode_GetNthTag(&DCtx, &Item, 1) != 2442302357ULL ||
3413 QCBORDecode_GetNthTag(&DCtx, &Item, 2) != 10489608748473423768ULL ||
3414 QCBORDecode_GetNthTag(&DCtx, &Item, 3) != 21590ULL) {
3415 return -8;
3416 }
3417
3418 /* Make sure to blow past the limit of tags that must be mapped.
3419 works in conjuntion with entries above.
3420 269488144(269488145(269488146(269488147([]))))
3421 */
3422 uError = QCBORDecode_GetNext(&DCtx, &Item);
3423 if(uError != QCBOR_ERR_TOO_MANY_TAGS) {
3424 return -9;
3425 }
3426
3427 uError = QCBORDecode_GetNext(&DCtx, &Item);
3428 if(uError == QCBOR_SUCCESS) {
3429 return -10;
3430 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003431
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003432 // ----------------------------------
Laurence Lundbladeee851742020-01-08 08:37:05 -08003433 // This test sets up a caller-config list that includes the very large
Laurence Lundblade9b334962020-08-27 10:55:53 -07003434 // tage and then matches it. Caller-config lists are no longer
3435 // used or needed. This tests backwards compatibility with them.
Laurence Lundbladeee851742020-01-08 08:37:05 -08003436 QCBORDecode_Init(&DCtx,
3437 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEncodedLargeTag),
3438 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003439 const uint64_t puList[] = {0x9192939495969798, 257};
3440 const QCBORTagListIn TL = {2, puList};
3441 QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003442
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003443 if(QCBORDecode_GetNext(&DCtx, &Item)) {
3444 return -8;
3445 }
3446 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
3447 !QCBORDecode_IsTagged(&DCtx, &Item, 0x9192939495969798) ||
3448 QCBORDecode_IsTagged(&DCtx, &Item, 257) ||
3449 QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_BIGFLOAT) ||
3450 Item.val.uCount != 0) {
3451 return -9;
3452 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003453
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003454 //------------------------
Laurence Lundbladeee851742020-01-08 08:37:05 -08003455 // Sets up a caller-configured list and look up something not in it
Laurence Lundblade9b334962020-08-27 10:55:53 -07003456 // Another backwards compatibility test.
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003457 const uint64_t puLongList[17] = {1,2,1};
3458 const QCBORTagListIn TLLong = {17, puLongList};
Laurence Lundbladeee851742020-01-08 08:37:05 -08003459 QCBORDecode_Init(&DCtx,
3460 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEncodedLargeTag),
3461 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003462 QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TLLong);
3463 if(QCBORDecode_GetNext(&DCtx, &Item)) {
3464 return -11;
3465 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003466
Laurence Lundbladef7a70bc2020-10-24 12:23:25 -07003467 uint64_t puTags[4];
Laurence Lundblade9b334962020-08-27 10:55:53 -07003468 QCBORTagListOut Out = {0, 4, puTags};
3469
3470
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003471 // This tests retrievel of the full tag list
Laurence Lundbladeee851742020-01-08 08:37:05 -08003472 QCBORDecode_Init(&DCtx,
3473 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spLotsOfTags),
3474 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003475 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3476 return -12;
3477 }
3478 if(puTags[0] != 0x9192939495969798 ||
3479 puTags[1] != 0x88 ||
Laurence Lundblade59289e52019-12-30 13:44:37 -08003480 puTags[2] != 0x06 ||
3481 puTags[3] != 0x07) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003482 return -13;
3483 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003484
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003485 // ----------------------
Laurence Lundblade9b334962020-08-27 10:55:53 -07003486 // This tests too small of an out list
Laurence Lundbladeee851742020-01-08 08:37:05 -08003487 QCBORDecode_Init(&DCtx,
3488 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spLotsOfTags),
3489 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003490 QCBORTagListOut OutSmall = {0, 3, puTags};
3491 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &OutSmall) != QCBOR_ERR_TOO_MANY_TAGS) {
3492 return -14;
3493 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003494
Laurence Lundblade9b334962020-08-27 10:55:53 -07003495
3496
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003497 // ---------------
Laurence Lundblade9b334962020-08-27 10:55:53 -07003498 // Decode a version of the "CSR" that has had a ton of tags randomly inserted
3499 // It is a bit of a messy test and maybe could be improved, but
3500 // it is retained as a backwards compatibility check.
Laurence Lundbladeee851742020-01-08 08:37:05 -08003501 QCBORDecode_Init(&DCtx,
3502 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRWithTags),
3503 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003504 int n = CheckCSRMaps(&DCtx);
3505 if(n) {
3506 return n-2000;
3507 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003508
Laurence Lundblade59289e52019-12-30 13:44:37 -08003509 Out = (QCBORTagListOut){0, 16, puTags};
Laurence Lundbladeee851742020-01-08 08:37:05 -08003510 QCBORDecode_Init(&DCtx,
3511 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRWithTags),
3512 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003513
Laurence Lundblade9b334962020-08-27 10:55:53 -07003514 /* With the spiffy decode revision, this tag list is not used.
3515 It doesn't matter if a tag is in this list or not so some
3516 tests that couldn't process a tag because it isn't in this list
3517 now can process these unlisted tags. The tests have been
3518 adjusted for this. */
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003519 const uint64_t puTagList[] = {773, 1, 90599561};
3520 const QCBORTagListIn TagList = {3, puTagList};
3521 QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TagList);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003522
3523
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003524 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3525 return -100;
3526 }
3527 if(Item.uDataType != QCBOR_TYPE_MAP ||
3528 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC) ||
3529 QCBORDecode_IsTagged(&DCtx, &Item, 90599561) ||
3530 QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_DATE_EPOCH) ||
3531 Item.val.uCount != 2 ||
3532 puTags[0] != CBOR_TAG_CBOR_MAGIC ||
3533 puTags[1] != CBOR_TAG_CBOR_MAGIC ||
3534 puTags[2] != CBOR_TAG_CBOR_MAGIC ||
3535 Out.uNumUsed != 3) {
3536 return -101;
3537 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003538
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003539 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3540 return -102;
3541 }
3542 if(Item.uDataType != QCBOR_TYPE_MAP ||
3543 QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC) ||
3544 QCBORDecode_IsTagged(&DCtx, &Item, 6) ||
Laurence Lundbladef7a70bc2020-10-24 12:23:25 -07003545 !QCBORDecode_IsTagged(&DCtx, &Item, 7) ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003546 Item.val.uCount != 2 ||
3547 puTags[0] != 5859837686836516696 ||
3548 puTags[1] != 7 ||
3549 Out.uNumUsed != 2) {
3550 return -103;
3551 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003552
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003553 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3554 return -104;
3555 }
3556 if(Item.uDataType != QCBOR_TYPE_MAP ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003557 Item.val.uCount != 5 ||
3558 puTags[0] != 0x0b ||
3559 Out.uNumUsed != 1) {
3560 return -105;
3561 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003562
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003563 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3564 return -106;
3565 }
3566 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
3567 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_COSE_MAC0) ||
3568 Item.val.string.len != 12 ||
3569 puTags[0] != CBOR_TAG_COSE_MAC0 ||
3570 puTags[1] != CBOR_TAG_COSE_MAC0 ||
3571 puTags[2] != CBOR_TAG_COSE_MAC0 ||
3572 Out.uNumUsed != 3) {
3573 return -105;
3574 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003575
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003576 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3577 return -107;
3578 }
3579 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
3580 !QCBORDecode_IsTagged(&DCtx, &Item, 773) ||
3581 Item.val.string.len != 3 ||
3582 puTags[0] != 773 ||
3583 Out.uNumUsed != 1) {
3584 return -108;
3585 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003586
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003587 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3588 return -109;
3589 }
3590 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade59289e52019-12-30 13:44:37 -08003591 !QCBORDecode_IsTagged(&DCtx, &Item, 16) ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003592 Item.val.string.len != 9 ||
Laurence Lundblade59289e52019-12-30 13:44:37 -08003593 puTags[0] != 16 ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07003594 puTags[3] != 7 ||
3595 Out.uNumUsed != 4) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003596 return -110;
3597 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003598
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003599 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3600 return -111;
3601 }
3602 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
3603 !QCBORDecode_IsTagged(&DCtx, &Item, 17) ||
3604 Item.val.string.len != 9 ||
3605 puTags[0] != 17 ||
3606 Out.uNumUsed != 1) {
3607 return -112;
3608 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003609
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003610 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3611 return -111;
3612 }
3613 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
3614 !QCBORDecode_IsTagged(&DCtx, &Item, 17) ||
3615 Item.val.string.len != 2 ||
3616 puTags[0] != 17 ||
3617 Out.uNumUsed != 1) {
3618 return -112;
3619 }
3620
3621 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3622 return -113;
3623 }
3624 if(Item.uDataType != QCBOR_TYPE_MAP ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07003625 !QCBORDecode_IsTagged(&DCtx, &Item, 19) ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003626 Item.val.uCount != 2 ||
3627 puTags[0] != 19 ||
3628 Out.uNumUsed != 1) {
3629 return -114;
3630 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003631
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003632 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3633 return -115;
3634 }
3635 if(Item.uDataType != QCBOR_TYPE_MAP ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07003636 !QCBORDecode_IsTagged(&DCtx, &Item, 9) ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003637 Item.val.uCount != 1 ||
3638 puTags[0] != 9 ||
3639 Out.uNumUsed != 1) {
3640 return -116;
3641 }
3642
3643 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3644 return -116;
3645 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003646 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003647 Item.val.int64 != -7 ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003648 Out.uNumUsed != 0) {
3649 return -117;
3650 }
3651
3652 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3653 return -118;
3654 }
3655 if(Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
3656 Item.val.string.len != 10 ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003657 puTags[0] != 12 ||
3658 Out.uNumUsed != 1) {
3659 return -119;
3660 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003661
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003662 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3663 return -120;
3664 }
3665 if(Item.uDataType != QCBOR_TYPE_MAP ||
3666 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_ENC_AS_B16) ||
3667 Item.val.uCount != 1 ||
3668 puTags[0] != 0x17 ||
3669 Out.uNumUsed != 1) {
3670 return -121;
3671 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003672
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003673 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3674 return -122;
3675 }
3676 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07003677 !QCBORDecode_IsTagged(&DCtx, &Item, 8) ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003678 Item.val.int64 != -3 ||
3679 puTags[0] != 8 ||
3680 Out.uNumUsed != 1) {
3681 return -123;
3682 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003683
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003684 if(QCBORDecode_Finish(&DCtx)) {
3685 return -124;
3686 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07003687
3688 UsefulBufC DateString;
3689 QCBORDecode_Init(&DCtx,
3690 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSpiffyTagInput),
3691 QCBOR_DECODE_MODE_NORMAL);
3692
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07003693 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade9b334962020-08-27 10:55:53 -07003694 // tagged date string
3695 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString);
3696 // untagged date string
3697 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString);
3698 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_SUCCESS) {
3699 return 100;
3700 }
3701 // untagged byte string
3702 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString);
3703 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
3704 return 101;
3705 }
3706 // tagged regex
3707 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString);
3708 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
3709 return 102;
3710 }
3711 // tagged date string with a byte string
3712 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString);
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07003713 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT) {
Laurence Lundblade9b334962020-08-27 10:55:53 -07003714 return 103;
3715 }
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07003716 // The exit errors out because the last item, the date string with
3717 // bad content makes the array untraversable (the bad date string
3718 // could have tag content of an array or such that is not consumed
3719 // by the date decoding).
Laurence Lundblade9b334962020-08-27 10:55:53 -07003720 QCBORDecode_ExitArray(&DCtx);
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07003721 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT) {
Laurence Lundblade9b334962020-08-27 10:55:53 -07003722 return 104;
3723 }
3724
3725
3726 QCBORDecode_Init(&DCtx,
3727 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSpiffyTagInput),
3728 QCBOR_DECODE_MODE_NORMAL);
3729
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07003730 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade9b334962020-08-27 10:55:53 -07003731 // tagged date string
3732 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString);
3733 // untagged date string
3734 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString);
3735 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_SUCCESS) {
3736 return 200;
3737 }
3738 // untagged byte string
3739 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString);
3740 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
3741 return 201;
3742 }
3743 // tagged regex
3744 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString);
3745 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
3746 return 202;
3747 }
3748 // tagged date string with a byte string
3749 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString);
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07003750 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT) {
Laurence Lundblade9b334962020-08-27 10:55:53 -07003751 return 203;
3752 }
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07003753 // See comments above
Laurence Lundblade9b334962020-08-27 10:55:53 -07003754 QCBORDecode_ExitArray(&DCtx);
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07003755 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT) {
Laurence Lundblade9b334962020-08-27 10:55:53 -07003756 return 204;
3757 }
3758
3759 QCBORDecode_Init(&DCtx,
3760 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSpiffyTagInput),
3761 QCBOR_DECODE_MODE_NORMAL);
3762
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07003763 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade9b334962020-08-27 10:55:53 -07003764 // tagged date string
3765 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString);
3766 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
3767 return 300;
3768 }
3769 // untagged date string
3770 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString);
3771 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
3772 return 301;
3773 }
3774 // untagged byte string
3775 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString);
3776 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
3777 return 302;
3778 }
3779 // tagged regex
3780 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString);
3781 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
3782 return 303;
3783 }
3784 // tagged date string with a byte string
3785 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString);
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07003786 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT) {
Laurence Lundblade9b334962020-08-27 10:55:53 -07003787 return 304;
3788 }
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07003789 // See comments above
Laurence Lundblade9b334962020-08-27 10:55:53 -07003790 QCBORDecode_ExitArray(&DCtx);
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07003791 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT) {
Laurence Lundblade9b334962020-08-27 10:55:53 -07003792 return 305;
3793 }
3794
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003795 return 0;
3796}
3797
Laurence Lundblade37286c02022-09-03 10:05:02 -07003798/*
3799 * These are showing the big numbers converted to integers.
3800 * The tag numbers are not shown.
3801 *
3802 * [ 18446744073709551616,
3803 * -18446744073709551617,
3804 * {"BN+": 18446744073709551616,
3805 * 64: 18446744073709551616,
3806 * "BN-": -18446744073709551617,
3807 * -64: -18446744073709551617
3808 * }
3809 * ]
3810 */
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003811
Laurence Lundbladecc7da412020-12-27 00:09:07 -08003812static const uint8_t spBigNumInput[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003813 0x83,
3814 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3815 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3816 0xA4,
3817 0x63, 0x42, 0x4E, 0x2B,
3818 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3819 0x18, 0x40,
3820 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3821 0x63, 0x42, 0x4E, 0x2D,
3822 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3823 0x38, 0x3F,
3824 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
3825
Laurence Lundblade37286c02022-09-03 10:05:02 -07003826#ifndef QCBOR_DISABLE_TAGS
Laurence Lundbladecc7da412020-12-27 00:09:07 -08003827/* The expected big num */
3828static const uint8_t spBigNum[] = {
3829 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3830 0x00};
Laurence Lundblade37286c02022-09-03 10:05:02 -07003831#endif /* QCBOR_DISABLE_TAGS */
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003832
3833
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03003834int32_t BignumParseTest(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003835{
3836 QCBORDecodeContext DCtx;
3837 QCBORItem Item;
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07003838 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003839
Laurence Lundbladeee851742020-01-08 08:37:05 -08003840 QCBORDecode_Init(&DCtx,
3841 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNumInput),
3842 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003843
3844
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003845 //
3846 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
3847 return -1;
3848 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003849 return -2;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003850 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003851
Laurence Lundblade37286c02022-09-03 10:05:02 -07003852#ifndef QCBOR_DISABLE_TAGS
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003853 //
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003854 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003855 return -3;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003856 if(Item.uDataType != QCBOR_TYPE_POSBIGNUM ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003857 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003858 return -4;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003859 }
3860
3861 //
3862 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003863 return -5;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003864 if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003865 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003866 return -6;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003867 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003868
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003869 //
3870 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003871 return -7;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003872 if(Item.uDataType != QCBOR_TYPE_MAP) {
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003873 return -8;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003874 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003875
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003876 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003877 return -9;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003878 if(Item.uDataType != QCBOR_TYPE_POSBIGNUM ||
3879 Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003880 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003881 return -10;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003882 }
3883
3884 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003885 return -11;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003886 if(Item.uDataType != QCBOR_TYPE_POSBIGNUM ||
3887 Item.uLabelType != QCBOR_TYPE_INT64 ||
3888 Item.label.int64 != 64 ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003889 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003890 return -12;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003891 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003892
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003893 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003894 return -13;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003895 if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM ||
3896 Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003897 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003898 return -14;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003899 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003900
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003901 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003902 return -15;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003903 if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM ||
3904 Item.uLabelType != QCBOR_TYPE_INT64 ||
3905 Item.label.int64 != -64 ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003906 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003907 return -16;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003908 }
Laurence Lundblade37286c02022-09-03 10:05:02 -07003909#else
3910
3911 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_TAGS_DISABLED) {
3912 return -100;
3913 }
3914#endif /* QCBOR_DISABLE_TAGS */
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003915
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003916 return 0;
3917}
3918
3919
3920
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003921static int32_t CheckItemWithIntLabel(QCBORDecodeContext *pCtx,
Laurence Lundbladeee851742020-01-08 08:37:05 -08003922 uint8_t uDataType,
3923 uint8_t uNestingLevel,
3924 uint8_t uNextNest,
3925 int64_t nLabel,
3926 QCBORItem *pItem)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003927{
3928 QCBORItem Item;
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07003929 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003930
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003931 if((nCBORError = QCBORDecode_GetNext(pCtx, &Item))) return -1;
3932 if(Item.uDataType != uDataType) return -1;
3933 if(uNestingLevel > 0) {
Laurence Lundbladeee851742020-01-08 08:37:05 -08003934 if(Item.uLabelType != QCBOR_TYPE_INT64 &&
3935 Item.uLabelType != QCBOR_TYPE_UINT64) {
3936 return -1;
3937 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003938 if(Item.uLabelType == QCBOR_TYPE_INT64) {
3939 if(Item.label.int64 != nLabel) return -1;
3940 } else {
Laurence Lundblade570fab52018-10-13 18:28:27 +08003941 if(Item.label.uint64 != (uint64_t)nLabel) return -1;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003942 }
3943 }
3944 if(Item.uNestingLevel != uNestingLevel) return -1;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303945 if(Item.uNextNestLevel != uNextNest) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003946
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003947 if(pItem) {
3948 *pItem = Item;
3949 }
3950 return 0;
3951}
3952
3953
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003954// Same code checks definite and indefinite length versions of the map
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003955static int32_t CheckCSRMaps(QCBORDecodeContext *pDC)
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003956{
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303957 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 0, 1, 0, NULL)) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003958
Laurence Lundblade9b334962020-08-27 10:55:53 -07003959 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 1, 2, -23, NULL)) return -2;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003960
Laurence Lundblade9b334962020-08-27 10:55:53 -07003961 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 2, 3, -20, NULL)) return -3;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003962
Laurence Lundblade9b334962020-08-27 10:55:53 -07003963 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -18, NULL)) return -4;
3964 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -17, NULL)) return -5;
3965 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -15, NULL)) return -6;
3966 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -16, NULL)) return -7;
3967 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 2, -14, NULL)) return -8;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003968
Laurence Lundblade9b334962020-08-27 10:55:53 -07003969 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 2, 3, -19, NULL)) return -9;
3970 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 3, 4, -11, NULL)) return -10;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003971
Laurence Lundblade9b334962020-08-27 10:55:53 -07003972 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_INT64, 4, 3, -9, NULL)) return -11;
3973 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_BYTE_STRING, 3, 1, -10, NULL)) return -12;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003974
Laurence Lundblade9b334962020-08-27 10:55:53 -07003975 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 1, 2, -22, NULL)) return -13;
3976 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_INT64, 2, 0, -5, NULL)) return -14;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003977
Laurence Lundblade9b334962020-08-27 10:55:53 -07003978 if(QCBORDecode_Finish(pDC)) return -20;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003979
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003980 return 0;
3981}
3982
3983
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003984/*
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003985{
3986 -23: {
3987 -20: {
3988 -18: "Organization",
3989 -17: "SSG",
3990 -15: "Confusion",
3991 -16: "San Diego",
3992 -14: "US"
3993 },
3994 -19: {
3995 -11: {
3996 -9: -7
3997 },
3998 -10: '\u0001\u0002\u0003\u0004\u0005\u0006\a\b\t\n'
3999 }
4000 },
4001 -22: {
4002 -5: -3
4003 }
4004}
Laurence Lundbladecc7da412020-12-27 00:09:07 -08004005*/
4006static const uint8_t spCSRInput[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004007 0xa2, 0x36, 0xa2, 0x33, 0xa5, 0x31, 0x6c, 0x4f,
4008 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74,
4009 0x69, 0x6f, 0x6e, 0x30, 0x63, 0x53, 0x53, 0x47,
4010 0x2e, 0x69, 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73,
4011 0x69, 0x6f, 0x6e, 0x2f, 0x69, 0x53, 0x61, 0x6e,
4012 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f, 0x2d, 0x62,
4013 0x55, 0x53, 0x32, 0xa2, 0x2a, 0xa1, 0x28, 0x26,
4014 0x29, 0x4a, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
4015 0x07, 0x08, 0x09, 0x0a, 0x35, 0xa1, 0x24, 0x22};
4016
Laurence Lundbladecc7da412020-12-27 00:09:07 -08004017// Same map as above, but using indefinite lengths
4018static const uint8_t spCSRInputIndefLen[] = {
4019 0xbf, 0x36, 0xbf, 0x33, 0xbf, 0x31, 0x6c, 0x4f,
4020 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74,
4021 0x69, 0x6f, 0x6e, 0x30, 0x63, 0x53, 0x53, 0x47,
4022 0x2e, 0x69, 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73,
4023 0x69, 0x6f, 0x6e, 0x2f, 0x69, 0x53, 0x61, 0x6e,
4024 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f, 0x2d, 0x62,
4025 0x55, 0x53, 0xff, 0x32, 0xbf, 0x2a, 0xbf, 0x28,
4026 0x26, 0xff, 0x29, 0x4a, 0x01, 0x02, 0x03, 0x04,
4027 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0xff, 0xff,
4028 0x35, 0xbf, 0x24, 0x22, 0xff, 0xff};
4029
4030
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03004031int32_t NestedMapTest(void)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004032{
4033 QCBORDecodeContext DCtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004034
Laurence Lundbladeee851742020-01-08 08:37:05 -08004035 QCBORDecode_Init(&DCtx,
4036 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput),
4037 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004038
Laurence Lundblade742df4a2018-10-13 20:07:17 +08004039 return CheckCSRMaps(&DCtx);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004040}
4041
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08004042
4043
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03004044int32_t StringDecoderModeFailTest(void)
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08004045{
4046 QCBORDecodeContext DCtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004047
Laurence Lundbladeee851742020-01-08 08:37:05 -08004048 QCBORDecode_Init(&DCtx,
4049 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput),
4050 QCBOR_DECODE_MODE_MAP_STRINGS_ONLY);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004051
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08004052 QCBORItem Item;
4053 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004054
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08004055 if(QCBORDecode_GetNext(&DCtx, &Item)) {
4056 return -1;
4057 }
4058 if(Item.uDataType != QCBOR_TYPE_MAP) {
4059 return -2;
4060 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004061
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08004062 nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
4063 if(nCBORError != QCBOR_ERR_MAP_LABEL_TYPE) {
4064 return -3;
4065 }
4066
4067 return 0;
4068}
4069
4070
Laurence Lundblade742df4a2018-10-13 20:07:17 +08004071
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03004072int32_t NestedMapTestIndefLen(void)
Laurence Lundblade742df4a2018-10-13 20:07:17 +08004073{
4074 QCBORDecodeContext DCtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004075
Laurence Lundbladeee851742020-01-08 08:37:05 -08004076 QCBORDecode_Init(&DCtx,
4077 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInputIndefLen),
4078 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004079
Laurence Lundblade742df4a2018-10-13 20:07:17 +08004080 return CheckCSRMaps(&DCtx);
4081}
4082
4083
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004084
Laurence Lundblade17ede402018-10-13 11:43:07 +08004085static UsefulBufC make_nested_indefinite_arrays(int n, UsefulBuf Storage)
4086{
4087 UsefulOutBuf UOB;
4088 UsefulOutBuf_Init(&UOB, Storage);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004089
Laurence Lundblade17ede402018-10-13 11:43:07 +08004090 int i;
4091 for(i = 0; i < n; i++) {
4092 UsefulOutBuf_AppendByte(&UOB, 0x9f);
4093 }
4094
4095 for(i = 0; i < n; i++) {
4096 UsefulOutBuf_AppendByte(&UOB, 0xff);
4097 }
4098 return UsefulOutBuf_OutUBuf(&UOB);
4099}
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08004100
4101
Laurence Lundbladec5fef682020-01-25 11:38:45 -08004102static int32_t parse_indeflen_nested(UsefulBufC Nested, int nNestLevel)
Laurence Lundblade17ede402018-10-13 11:43:07 +08004103{
4104 QCBORDecodeContext DC;
4105 QCBORDecode_Init(&DC, Nested, 0);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004106
Laurence Lundblade17ede402018-10-13 11:43:07 +08004107 int j;
4108 for(j = 0; j < nNestLevel; j++) {
4109 QCBORItem Item;
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07004110 QCBORError nReturn = QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade17ede402018-10-13 11:43:07 +08004111 if(j >= QCBOR_MAX_ARRAY_NESTING) {
4112 // Should be in error
Laurence Lundbladea9489f82020-09-12 13:50:56 -07004113 if(nReturn != QCBOR_ERR_ARRAY_DECODE_NESTING_TOO_DEEP) {
Laurence Lundblade17ede402018-10-13 11:43:07 +08004114 return -4;
4115 } else {
4116 return 0; // Decoding doesn't recover after an error
4117 }
4118 } else {
4119 // Should be no error
4120 if(nReturn) {
4121 return -9; // Should not have got an error
4122 }
4123 }
4124 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
4125 return -7;
4126 }
4127 }
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07004128 QCBORError nReturn = QCBORDecode_Finish(&DC);
Laurence Lundblade17ede402018-10-13 11:43:07 +08004129 if(nReturn) {
4130 return -3;
4131 }
4132 return 0;
4133}
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08004134
4135
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03004136int32_t IndefiniteLengthNestTest(void)
Laurence Lundblade17ede402018-10-13 11:43:07 +08004137{
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05304138 UsefulBuf_MAKE_STACK_UB(Storage, 50);
Laurence Lundblade17ede402018-10-13 11:43:07 +08004139 int i;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004140 for(i=1; i < QCBOR_MAX_ARRAY_NESTING+4; i++) {
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08004141 const UsefulBufC Nested = make_nested_indefinite_arrays(i, Storage);
Laurence Lundblade17ede402018-10-13 11:43:07 +08004142 int nReturn = parse_indeflen_nested(Nested, i);
4143 if(nReturn) {
4144 return nReturn;
4145 }
4146 }
4147 return 0;
4148}
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08004149
Laurence Lundbladeee851742020-01-08 08:37:05 -08004150// [1, [2, 3]]
4151static const uint8_t spIndefiniteArray[] = {0x9f, 0x01, 0x82, 0x02, 0x03, 0xff};
4152// No closing break
4153static const uint8_t spIndefiniteArrayBad1[] = {0x9f};
4154// Not enough closing breaks
4155static const uint8_t spIndefiniteArrayBad2[] = {0x9f, 0x9f, 0x02, 0xff};
4156// Too many closing breaks
4157static const uint8_t spIndefiniteArrayBad3[] = {0x9f, 0x02, 0xff, 0xff};
4158// Unclosed indeflen inside def len
4159static const uint8_t spIndefiniteArrayBad4[] = {0x81, 0x9f};
4160// confused tag
4161static const uint8_t spIndefiniteArrayBad5[] = {0x9f, 0xd1, 0xff};
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08004162
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03004163int32_t IndefiniteLengthArrayMapTest(void)
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08004164{
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07004165 QCBORError nResult;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08004166 // --- first test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07004167 UsefulBufC IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArray);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004168
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08004169 // Decode it and see if it is OK
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08004170 QCBORDecodeContext DC;
4171 QCBORItem Item;
4172 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004173
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08004174 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05304175
4176 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
4177 Item.uNestingLevel != 0 ||
4178 Item.uNextNestLevel != 1) {
4179 return -111;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08004180 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004181
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08004182 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05304183 if(Item.uDataType != QCBOR_TYPE_INT64 ||
4184 Item.uNestingLevel != 1 ||
4185 Item.uNextNestLevel != 1) {
4186 return -2;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08004187 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004188
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08004189 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05304190 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
4191 Item.uNestingLevel != 1 ||
4192 Item.uNextNestLevel != 2) {
4193 return -3;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08004194 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004195
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08004196 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade12b495d2018-12-17 11:15:54 -08004197 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade6de37062018-10-15 12:22:42 +05304198 Item.uNestingLevel != 2 ||
4199 Item.uNextNestLevel != 2) {
4200 return -4;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08004201 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004202
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08004203 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade12b495d2018-12-17 11:15:54 -08004204 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade6de37062018-10-15 12:22:42 +05304205 Item.uNestingLevel != 2 ||
4206 Item.uNextNestLevel != 0) {
4207 return -5;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08004208 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004209
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08004210 if(QCBORDecode_Finish(&DC)) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05304211 return -6;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08004212 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004213
Laurence Lundblade19e0c802018-10-13 12:19:55 +08004214 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07004215 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad1);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004216
Laurence Lundblade19e0c802018-10-13 12:19:55 +08004217 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004218
Laurence Lundblade19e0c802018-10-13 12:19:55 +08004219 nResult = QCBORDecode_GetNext(&DC, &Item);
4220 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05304221 return -7;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08004222 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004223
Laurence Lundblade570fab52018-10-13 18:28:27 +08004224 nResult = QCBORDecode_Finish(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07004225 if(nResult != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05304226 return -8;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08004227 }
4228
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004229
Laurence Lundblade19e0c802018-10-13 12:19:55 +08004230 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07004231 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad2);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004232
Laurence Lundblade19e0c802018-10-13 12:19:55 +08004233 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004234
Laurence Lundblade19e0c802018-10-13 12:19:55 +08004235 nResult = QCBORDecode_GetNext(&DC, &Item);
4236 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05304237 return -9;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08004238 }
4239
4240 nResult = QCBORDecode_GetNext(&DC, &Item);
4241 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05304242 return -10;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08004243 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004244
Laurence Lundblade19e0c802018-10-13 12:19:55 +08004245 nResult = QCBORDecode_GetNext(&DC, &Item);
4246 if(nResult || Item.uDataType != QCBOR_TYPE_INT64) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05304247 return -11;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08004248 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004249
Laurence Lundblade19e0c802018-10-13 12:19:55 +08004250 nResult = QCBORDecode_Finish(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07004251 if(nResult != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05304252 return -12;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08004253 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004254
4255
Laurence Lundblade19e0c802018-10-13 12:19:55 +08004256 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07004257 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad3);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004258
Laurence Lundblade19e0c802018-10-13 12:19:55 +08004259 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004260
Laurence Lundblade19e0c802018-10-13 12:19:55 +08004261 nResult = QCBORDecode_GetNext(&DC, &Item);
4262 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05304263 return -13;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08004264 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004265
Laurence Lundblade19e0c802018-10-13 12:19:55 +08004266 nResult = QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade642282a2020-06-23 12:00:33 -07004267 if(nResult != QCBOR_SUCCESS) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05304268 return -14;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08004269 }
Laurence Lundblade6de37062018-10-15 12:22:42 +05304270
Laurence Lundblade642282a2020-06-23 12:00:33 -07004271 nResult = QCBORDecode_GetNext(&DC, &Item);
4272 if(nResult != QCBOR_ERR_BAD_BREAK) {
4273 return -140;
4274 }
4275
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004276
Laurence Lundblade570fab52018-10-13 18:28:27 +08004277 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07004278 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad4);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004279
Laurence Lundblade570fab52018-10-13 18:28:27 +08004280 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004281
Laurence Lundblade570fab52018-10-13 18:28:27 +08004282 nResult = QCBORDecode_GetNext(&DC, &Item);
4283 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05304284 return -15;
Laurence Lundblade570fab52018-10-13 18:28:27 +08004285 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004286
Laurence Lundblade570fab52018-10-13 18:28:27 +08004287 nResult = QCBORDecode_GetNext(&DC, &Item);
4288 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05304289 return -16;
Laurence Lundblade570fab52018-10-13 18:28:27 +08004290 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004291
Laurence Lundblade570fab52018-10-13 18:28:27 +08004292 nResult = QCBORDecode_Finish(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07004293 if(nResult != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05304294 return -17;
Laurence Lundblade570fab52018-10-13 18:28:27 +08004295 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004296
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304297 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07004298 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad5);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004299
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304300 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004301
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304302 nResult = QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade37286c02022-09-03 10:05:02 -07004303
4304#ifndef QCBOR_DISABLE_TAGS
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304305 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05304306 return -18;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304307 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004308
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304309 nResult = QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05304310 if(nResult != QCBOR_ERR_BAD_BREAK) {
4311 return -19;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304312 }
Laurence Lundblade37286c02022-09-03 10:05:02 -07004313#else /* QCBOR_DISABLE_TAGS */
4314 if(nResult != QCBOR_ERR_TAGS_DISABLED) {
4315 return -20;
4316 }
4317#endif /* QCBOR_DISABLE_TAGS */
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004318
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08004319 return 0;
4320}
4321
Laurence Lundblade17ede402018-10-13 11:43:07 +08004322
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08004323#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
4324
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07004325static const uint8_t spIndefiniteLenString[] = {
Laurence Lundblade17ede402018-10-13 11:43:07 +08004326 0x81, // Array of length one
4327 0x7f, // text string marked with indefinite length
4328 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment
4329 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment
4330 0xff // ending break
4331};
4332
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07004333static const uint8_t spIndefiniteLenStringBad2[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304334 0x81, // Array of length one
4335 0x7f, // text string marked with indefinite length
4336 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment
4337 0x44, 0x6d, 0x69, 0x6e, 0x67, // second segment of wrong type
4338 0xff // ending break
4339};
4340
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07004341static const uint8_t spIndefiniteLenStringBad3[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304342 0x81, // Array of length one
4343 0x7f, // text string marked with indefinite length
4344 0x01, 0x02, // Not a string
4345 0xff // ending break
4346};
4347
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07004348static const uint8_t spIndefiniteLenStringBad4[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304349 0x81, // Array of length one
4350 0x7f, // text string marked with indefinite length
4351 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment
4352 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment
4353 // missing end of string
4354};
4355
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07004356static const uint8_t spIndefiniteLenStringLabel[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304357 0xa1, // Array of length one
4358 0x7f, // text string marked with indefinite length
4359 0x65, 0x73, 0x74, 0x72, 0x75, 0x75, // first segment
4360 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment
4361 0xff, // ending break
4362 0x01 // integer being labeled.
4363};
4364
Laurence Lundbladec5fef682020-01-25 11:38:45 -08004365/**
4366 Make an indefinite length string
4367
4368 @param Storage Storage for string, must be 144 bytes in size
4369 @return The indefinite length string
4370
4371 This makes an array with one indefinite length string that has 7 chunks
4372 from size of 1 byte up to 64 bytes.
4373 */
4374static UsefulBufC MakeIndefiniteBigBstr(UsefulBuf Storage)
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304375{
4376 UsefulOutBuf UOB;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004377
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304378 UsefulOutBuf_Init(&UOB, Storage);
4379 UsefulOutBuf_AppendByte(&UOB, 0x81);
4380 UsefulOutBuf_AppendByte(&UOB, 0x5f);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004381
Laurence Lundbladec5fef682020-01-25 11:38:45 -08004382 uint8_t uStringByte = 0;
4383 // Use of type int is intentional
4384 for(int uChunkSize = 1; uChunkSize <= 128; uChunkSize *= 2) {
4385 // Not using preferred encoding here, but that is OK.
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304386 UsefulOutBuf_AppendByte(&UOB, 0x58);
Laurence Lundbladec5fef682020-01-25 11:38:45 -08004387 UsefulOutBuf_AppendByte(&UOB, (uint8_t)uChunkSize);
4388 for(int j = 0; j < uChunkSize; j++) {
4389 UsefulOutBuf_AppendByte(&UOB, uStringByte);
4390 uStringByte++;
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304391 }
4392 }
4393 UsefulOutBuf_AppendByte(&UOB, 0xff);
4394
4395 return UsefulOutBuf_OutUBuf(&UOB);
4396}
4397
4398static int CheckBigString(UsefulBufC BigString)
4399{
4400 if(BigString.len != 255) {
4401 return 1;
4402 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004403
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304404 for(uint8_t i = 0; i < 255; i++){
4405 if(((const uint8_t *)BigString.ptr)[i] != i) {
4406 return 1;
4407 }
4408 }
4409 return 0;
4410}
4411
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05304412
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03004413int32_t IndefiniteLengthStringTest(void)
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304414{
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304415 QCBORDecodeContext DC;
4416 QCBORItem Item;
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05304417 // big enough for MakeIndefiniteBigBstr() + MemPool overhead
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004418 UsefulBuf_MAKE_STACK_UB(MemPool, 350);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004419
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304420 // --- Simple normal indefinite length string ------
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07004421 UsefulBufC IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenString);
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304422 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004423
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05304424 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304425 return -1;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304426 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004427
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304428 if(QCBORDecode_GetNext(&DC, &Item)) {
4429 return -2;
4430 }
4431 if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.uDataAlloc) {
4432 return -3;
4433 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004434
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304435 if(QCBORDecode_GetNext(&DC, &Item)) {
4436 return -4;
4437 }
4438 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || !Item.uDataAlloc) {
4439 return -5;
4440 }
4441 if(QCBORDecode_Finish(&DC)) {
4442 return -6;
4443 }
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304444
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304445 // ----- types mismatch ---
Laurence Lundbladeee851742020-01-08 08:37:05 -08004446 QCBORDecode_Init(&DC,
4447 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad2),
4448 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004449
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304450 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
4451 return -7;
4452 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004453
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304454 if(QCBORDecode_GetNext(&DC, &Item)) {
4455 return -8;
4456 }
4457 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
4458 return -9;
4459 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004460
Laurence Lundblade30816f22018-11-10 13:40:22 +07004461 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_INDEFINITE_STRING_CHUNK) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304462 return -10;
4463 }
4464
4465 // ----- not a string ---
Laurence Lundbladeee851742020-01-08 08:37:05 -08004466 QCBORDecode_Init(&DC,
4467 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad3),
4468 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004469
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304470 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
4471 return -11;
4472 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004473
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304474 if(QCBORDecode_GetNext(&DC, &Item)) {
4475 return -12;
4476 }
4477 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
4478 return -13;
4479 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004480
Laurence Lundblade30816f22018-11-10 13:40:22 +07004481 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_INDEFINITE_STRING_CHUNK) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304482 return -14;
4483 }
4484
4485 // ----- no end -----
Laurence Lundbladeee851742020-01-08 08:37:05 -08004486 QCBORDecode_Init(&DC,
4487 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad4),
4488 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004489
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304490 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
4491 return -15;
4492 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004493
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304494 if(QCBORDecode_GetNext(&DC, &Item)) {
4495 return -16;
4496 }
4497 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
4498 return -17;
4499 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004500
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304501 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_HIT_END) {
4502 return -18;
4503 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004504
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304505 // ------ Don't set a string allocator and see an error -----
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304506 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004507
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304508 QCBORDecode_GetNext(&DC, &Item);
4509 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304510 return -19;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304511 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004512
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304513 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_NO_STRING_ALLOCATOR) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304514 return -20;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304515 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004516
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304517 // ----- Mempool is way too small -----
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004518 UsefulBuf_MAKE_STACK_UB(MemPoolTooSmall, QCBOR_DECODE_MIN_MEM_POOL_SIZE-1);
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304519
4520 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
4521 if(!QCBORDecode_SetMemPool(&DC, MemPoolTooSmall, false)) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304522 return -21;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304523 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004524
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304525 // ----- Mempool is way too small -----
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05304526 UsefulBuf_MAKE_STACK_UB(BigIndefBStrStorage, 290);
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08004527 const UsefulBufC BigIndefBStr = MakeIndefiniteBigBstr(BigIndefBStrStorage);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004528
Laurence Lundbladeee851742020-01-08 08:37:05 -08004529 // 80 is big enough for MemPool overhead, but not BigIndefBStr
4530 UsefulBuf_MAKE_STACK_UB(MemPoolSmall, 80);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004531
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304532 QCBORDecode_Init(&DC, BigIndefBStr, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304533 if(QCBORDecode_SetMemPool(&DC, MemPoolSmall, false)) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304534 return -22;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304535 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004536
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304537 QCBORDecode_GetNext(&DC, &Item);
4538 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304539 return -23;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304540 }
Laurence Lundblade30816f22018-11-10 13:40:22 +07004541 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_STRING_ALLOCATE) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304542 return -24;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304543 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004544
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304545 // ---- big bstr -----
4546 QCBORDecode_Init(&DC, BigIndefBStr, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004547
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304548 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
4549 return -25;
4550 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004551
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05304552 if(QCBORDecode_GetNext(&DC, &Item)) {
4553 return -26;
4554 }
4555 if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.uDataAlloc) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304556 return -26;
4557 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004558
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304559 if(QCBORDecode_GetNext(&DC, &Item)) {
4560 return -27;
4561 }
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05304562 if(Item.uDataType != QCBOR_TYPE_BYTE_STRING || !Item.uDataAlloc || Item.uNestingLevel != 1) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304563 return -28;
4564 }
4565 if(CheckBigString(Item.val.string)) {
4566 return -3;
4567 }
4568 if(QCBORDecode_Finish(&DC)) {
4569 return -29;
4570 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004571
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304572 // --- label is an indefinite length string ------
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07004573 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringLabel), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004574
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304575 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
4576 return -30;
4577 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004578
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304579 QCBORDecode_GetNext(&DC, &Item);
4580 if(Item.uDataType != QCBOR_TYPE_MAP) {
4581 return -31;
4582 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004583
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304584 if(QCBORDecode_GetNext(&DC, &Item)){
4585 return -32;
4586 }
Laurence Lundbladeee851742020-01-08 08:37:05 -08004587 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
4588 Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304589 Item.uDataAlloc || !Item.uLabelAlloc ||
4590 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("struuming"))) {
4591 return -33;
4592 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004593
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304594 if(QCBORDecode_Finish(&DC)) {
4595 return -34;
4596 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004597
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004598 return 0;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08004599}
4600
4601
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03004602int32_t AllocAllStringsTest(void)
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304603{
4604 QCBORDecodeContext DC;
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004605 QCBORError nCBORError;
4606
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004607
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304608 // First test, use the "CSRMap" as easy input and checking
Laurence Lundbladeee851742020-01-08 08:37:05 -08004609 QCBORDecode_Init(&DC,
4610 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput),
4611 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004612
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004613 UsefulBuf_MAKE_STACK_UB(Pool, sizeof(spCSRInput) + QCBOR_DECODE_MIN_MEM_POOL_SIZE);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004614
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004615 nCBORError = QCBORDecode_SetMemPool(&DC, Pool, 1); // Turn on copying.
4616 if(nCBORError) {
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304617 return -1;
4618 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004619
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004620 if(CheckCSRMaps(&DC)) {
4621 return -2;
4622 }
4623
Laurence Lundblade2f467f92020-10-09 17:50:11 -07004624 // Next parse, save pointers to a few strings, destroy original and
4625 // see all is OK.
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004626 UsefulBuf_MAKE_STACK_UB(CopyOfStorage, sizeof(pValidMapEncoded) + QCBOR_DECODE_MIN_MEM_POOL_SIZE);
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08004627 const UsefulBufC CopyOf = UsefulBuf_Copy(CopyOfStorage, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded));
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08004628
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304629 QCBORDecode_Init(&DC, CopyOf, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08004630 UsefulBuf_Set(Pool, '/');
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304631 QCBORDecode_SetMemPool(&DC, Pool, 1); // Turn on copying.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004632
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304633 QCBORItem Item1, Item2, Item3, Item4;
4634 if((nCBORError = QCBORDecode_GetNext(&DC, &Item1)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07004635 return (int32_t)nCBORError;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304636 if(Item1.uDataType != QCBOR_TYPE_MAP ||
4637 Item1.val.uCount != 3)
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004638 return -3;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304639 if((nCBORError = QCBORDecode_GetNext(&DC, &Item1)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07004640 return (int32_t)nCBORError;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304641 if((nCBORError = QCBORDecode_GetNext(&DC, &Item2)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07004642 return (int32_t)nCBORError;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304643 if((nCBORError = QCBORDecode_GetNext(&DC, &Item3)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07004644 return (int32_t)nCBORError;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304645 if((nCBORError = QCBORDecode_GetNext(&DC, &Item4)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07004646 return (int32_t)nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004647
Laurence Lundblade05ec57b2018-10-21 01:50:03 +05304648 UsefulBuf_Set(CopyOfStorage, '_');
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004649
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304650 if(Item1.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304651 Item1.uDataType != QCBOR_TYPE_INT64 ||
4652 Item1.val.int64 != 42 ||
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004653 Item1.uDataAlloc != 0 ||
4654 Item1.uLabelAlloc == 0 ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07004655 UsefulBufCompareToSZ(Item1.label.string, "first integer")) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004656 return -4;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09004657 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004658
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304659
4660 if(Item2.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07004661 UsefulBufCompareToSZ(Item2.label.string, "an array of two strings") ||
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304662 Item2.uDataType != QCBOR_TYPE_ARRAY ||
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004663 Item2.uDataAlloc != 0 ||
4664 Item2.uLabelAlloc == 0 ||
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304665 Item2.val.uCount != 2)
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004666 return -5;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004667
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304668 if(Item3.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004669 Item3.uDataAlloc == 0 ||
4670 Item3.uLabelAlloc != 0 ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07004671 UsefulBufCompareToSZ(Item3.val.string, "string1")) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004672 return -6;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09004673 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004674
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304675 if(Item4.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004676 Item4.uDataAlloc == 0 ||
4677 Item4.uLabelAlloc != 0 ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07004678 UsefulBufCompareToSZ(Item4.val.string, "string2")) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004679 return -7;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09004680 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004681
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304682 // Next parse with a pool that is too small
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004683 UsefulBuf_MAKE_STACK_UB(SmallPool, QCBOR_DECODE_MIN_MEM_POOL_SIZE + 1);
Laurence Lundbladeee851742020-01-08 08:37:05 -08004684 QCBORDecode_Init(&DC,
4685 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded),
4686 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304687 QCBORDecode_SetMemPool(&DC, SmallPool, 1); // Turn on copying.
4688 if((nCBORError = QCBORDecode_GetNext(&DC, &Item1)))
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004689 return -8;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304690 if(Item1.uDataType != QCBOR_TYPE_MAP ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09004691 Item1.val.uCount != 3) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004692 return -9;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09004693 }
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304694 if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item1))){
4695 if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item2))) {
4696 if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item3))) {
4697 nCBORError = QCBORDecode_GetNext(&DC, &Item4);
4698 }
4699 }
4700 }
Laurence Lundblade30816f22018-11-10 13:40:22 +07004701 if(nCBORError != QCBOR_ERR_STRING_ALLOCATE) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004702 return -10;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304703 }
4704
4705 return 0;
4706}
4707
Laurence Lundbladef6531662018-12-04 10:42:22 +09004708
Laurence Lundbladec5fef682020-01-25 11:38:45 -08004709int32_t MemPoolTest(void)
Laurence Lundblade0155b622018-10-12 20:04:37 +08004710{
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004711 // Set up the decoder with a tiny bit of CBOR to parse because
4712 // nothing can be done with it unless that is set up.
Laurence Lundbladef6531662018-12-04 10:42:22 +09004713 QCBORDecodeContext DC;
4714 const uint8_t pMinimalCBOR[] = {0xa0}; // One empty map
4715 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pMinimalCBOR),0);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004716
Laurence Lundbladef6531662018-12-04 10:42:22 +09004717 // Set up an memory pool of 100 bytes
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004718 // Then fish into the internals of the decode context
4719 // to get the allocator function so it can be called directly.
4720 // Also figure out how much pool is available for use
4721 // buy subtracting out the overhead.
Laurence Lundbladef6531662018-12-04 10:42:22 +09004722 UsefulBuf_MAKE_STACK_UB(Pool, 100);
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004723 QCBORError nError = QCBORDecode_SetMemPool(&DC, Pool, 0);
4724 if(nError) {
4725 return -9;
4726 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004727 QCBORStringAllocate pAlloc = DC.StringAllocator.pfAllocator;
4728 void *pAllocCtx = DC.StringAllocator.pAllocateCxt;
4729 size_t uAvailPool = Pool.len - QCBOR_DECODE_MIN_MEM_POOL_SIZE;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004730
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004731 // First test -- ask for one more byte than available and see failure
4732 UsefulBuf Allocated = (*pAlloc)(pAllocCtx, NULL, uAvailPool+1);
Laurence Lundbladef6531662018-12-04 10:42:22 +09004733 if(!UsefulBuf_IsNULL(Allocated)) {
4734 return -1;
4735 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004736
Laurence Lundbladef6531662018-12-04 10:42:22 +09004737 // Re do the set up for the next test that will do a successful alloc,
4738 // a fail, a free and then success
Laurence Lundbladef6531662018-12-04 10:42:22 +09004739 QCBORDecode_SetMemPool(&DC, Pool, 0);
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004740 pAlloc = DC.StringAllocator.pfAllocator;
4741 pAllocCtx = DC.StringAllocator.pAllocateCxt;
4742 uAvailPool = Pool.len - QCBOR_DECODE_MIN_MEM_POOL_SIZE;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004743
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004744 // Allocate one byte less than available and see success
4745 Allocated = (pAlloc)(pAllocCtx, NULL, uAvailPool-1);
Laurence Lundbladef6531662018-12-04 10:42:22 +09004746 if(UsefulBuf_IsNULL(Allocated)) { // expected to succeed
4747 return -2;
4748 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004749 // Ask for some more and see failure
4750 UsefulBuf Allocated2 = (*pAlloc)(pAllocCtx, NULL, uAvailPool/2);
Laurence Lundbladef6531662018-12-04 10:42:22 +09004751 if(!UsefulBuf_IsNULL(Allocated2)) { // expected to fail
4752 return -3;
4753 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004754 // Free the first allocate, retry the second and see success
4755 (*pAlloc)(pAllocCtx, Allocated.ptr, 0); // Free
4756 Allocated = (*pAlloc)(pAllocCtx, NULL, uAvailPool/2);
Laurence Lundbladef6531662018-12-04 10:42:22 +09004757 if(UsefulBuf_IsNULL(Allocated)) { // succeed because of the free
4758 return -4;
4759 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004760
Laurence Lundbladef6531662018-12-04 10:42:22 +09004761 // Re do set up for next test that involves a successful alloc,
4762 // and a successful realloc and a failed realloc
4763 QCBORDecode_SetMemPool(&DC, Pool, 0);
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004764 pAlloc = DC.StringAllocator.pfAllocator;
4765 pAllocCtx = DC.StringAllocator.pAllocateCxt;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004766
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004767 // Allocate half the pool and see success
4768 Allocated = (*pAlloc)(pAllocCtx, NULL, uAvailPool/2);
Laurence Lundbladef6531662018-12-04 10:42:22 +09004769 if(UsefulBuf_IsNULL(Allocated)) { // expected to succeed
4770 return -5;
4771 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004772 // Reallocate to take up the whole pool and see success
4773 Allocated2 = (*pAlloc)(pAllocCtx, Allocated.ptr, uAvailPool);
Laurence Lundbladef6531662018-12-04 10:42:22 +09004774 if(UsefulBuf_IsNULL(Allocated2)) {
4775 return -6;
4776 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004777 // Make sure its the same pointer and the size is right
Laurence Lundbladef6531662018-12-04 10:42:22 +09004778 if(Allocated2.ptr != Allocated.ptr || Allocated2.len != uAvailPool) {
4779 return -7;
4780 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004781 // Try to allocate more to be sure there is failure after a realloc
4782 UsefulBuf Allocated3 = (*pAlloc)(pAllocCtx, Allocated.ptr, uAvailPool+1);
4783 if(!UsefulBuf_IsNULL(Allocated3)) {
Laurence Lundbladef6531662018-12-04 10:42:22 +09004784 return -8;
4785 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004786
Laurence Lundbladef6531662018-12-04 10:42:22 +09004787 return 0;
4788}
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004789
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004790
4791/* Just enough of an allocator to test configuration of one */
4792static UsefulBuf AllocateTestFunction(void *pCtx, void *pOldMem, size_t uNewSize)
4793{
4794 (void)pOldMem; // unused variable
4795
4796 if(uNewSize) {
4797 // Assumes the context pointer is the buffer and
4798 // nothing too big will ever be asked for.
4799 // This is only good for this basic test!
4800 return (UsefulBuf) {pCtx, uNewSize};
4801 } else {
4802 return NULLUsefulBuf;
4803 }
4804}
4805
4806
Laurence Lundbladec5fef682020-01-25 11:38:45 -08004807int32_t SetUpAllocatorTest(void)
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004808{
4809 // Set up the decoder with a tiny bit of CBOR to parse because
4810 // nothing can be done with it unless that is set up.
4811 QCBORDecodeContext DC;
4812 const uint8_t pMinimalCBOR[] = {0x62, 0x48, 0x69}; // "Hi"
4813 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pMinimalCBOR),0);
4814
4815 uint8_t pAllocatorBuffer[50];
4816
4817 // This is really just to test that this call works.
4818 // The full functionality of string allocators is tested
4819 // elsewhere with the MemPool internal allocator.
4820 QCBORDecode_SetUpAllocator(&DC, AllocateTestFunction, pAllocatorBuffer, 1);
4821
4822 QCBORItem Item;
4823 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_SUCCESS) {
4824 return -1;
4825 }
4826
4827 if(Item.uDataAlloc == 0 ||
4828 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
4829 Item.val.string.ptr != pAllocatorBuffer) {
4830 return -2;
4831 }
4832
4833 if(QCBORDecode_Finish(&DC) != QCBOR_SUCCESS) {
4834 return -3;
4835 }
4836
4837 return 0;
4838}
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08004839#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
4840
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004841
Laurence Lundbladedd6e76e2021-03-10 01:54:01 -07004842#ifndef QCBOR_DISABLE_EXP_AND_MANTISSA
Laurence Lundblade59289e52019-12-30 13:44:37 -08004843
Laurence Lundblade37286c02022-09-03 10:05:02 -07004844struct EaMTest {
4845 const char *szName;
4846 UsefulBufC Input;
4847 uint8_t uTagRequirement;
4848 bool bHasTags;
4849
4850 /* Expected values for GetNext */
4851 QCBORError uExpectedErrorGN;
4852 uint8_t uQCBORTypeGN;
4853 int64_t nExponentGN;
4854 int64_t nMantissaGN;
4855 UsefulBufC MantissaGN;
4856
4857 /* Expected values for GetDecimalFraction */
4858 QCBORError uExpectedErrorGDF;
4859 int64_t nExponentGDF;
4860 int64_t nMantissaGDF;
4861
4862 /* Expected values for GetDecimalFractionBig */
4863 QCBORError uExpectedErrorGDFB;
4864 int64_t nExponentGDFB;
4865 UsefulBufC MantissaGDFB;
4866 bool IsNegativeGDFB;
4867
4868 /* Expected values for GetBigFloat */
4869 QCBORError uExpectedErrorGBF;
4870 int64_t nExponentGBF;
4871 int64_t nMantissaGBF;
4872
4873 /* Expected values for GetBigFloatBig */
4874 QCBORError uExpectedErrorGBFB;
4875 int64_t nExponentGBFB;
4876 UsefulBufC MantissaGBFB;
4877 bool IsNegativeGBFB;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004878};
4879
Laurence Lundbladefaec39f2020-08-02 21:53:53 -07004880
Laurence Lundblade37286c02022-09-03 10:05:02 -07004881
4882static const struct EaMTest pEaMTests[] = {
4883 {
4884 "1. Untagged pair (big float or decimal fraction), no tag required",
4885 {(const uint8_t []){0x82, 0x20, 0x03}, 3},
4886 QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
4887 false,
4888
4889 QCBOR_SUCCESS, /* for GetNext */
4890 QCBOR_TYPE_ARRAY,
4891 0,
4892 0,
4893 {(const uint8_t []){0x00}, 1},
4894
4895 QCBOR_SUCCESS, /* GetDecimalFraction */
4896 -1,
4897 3,
4898
4899 QCBOR_SUCCESS, /* for GetDecimalFractionBig */
4900 -1,
4901 {(const uint8_t []){0x03}, 1},
4902 false,
4903
4904 QCBOR_SUCCESS, /* for GetBigFloat */
4905 -1,
4906 3,
4907
4908 QCBOR_SUCCESS, /* for GetBigFloatBig */
4909 -1,
4910 {(const uint8_t []){0x03}, 1},
4911 false
4912 },
4913
4914 {
4915 "2. Untagged pair (big float or decimal fraction), tag required",
4916 {(const uint8_t []){0x82, 0x20, 0x03}, 3},
4917 QCBOR_TAG_REQUIREMENT_TAG,
4918 false,
4919
4920 QCBOR_SUCCESS, /* for GetNext */
4921 QCBOR_TYPE_ARRAY,
4922 0,
4923 0,
4924 {(const uint8_t []){0x00}, 1},
4925
4926 QCBOR_ERR_UNEXPECTED_TYPE, /* for GetDecimalFraction */
4927 0,
4928 0,
4929
4930 QCBOR_ERR_UNEXPECTED_TYPE, /* for GetDecimalFractionBig */
4931 0,
4932 {(const uint8_t []){0x00}, 1},
4933 false,
4934
4935 QCBOR_ERR_UNEXPECTED_TYPE, /* for GetBigFloat */
4936 0,
4937 0,
4938
4939 QCBOR_ERR_UNEXPECTED_TYPE, /* for GetBigFloatBig */
4940 0,
4941 {(const uint8_t []){0x00}, 1},
4942 false
4943
4944 },
4945
4946 {
4947 "3. Tagged 1.5 decimal fraction, tag 4 optional",
4948 {(const uint8_t []){0xC4, 0x82, 0x20, 0x03}, 4},
4949 QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
4950 true,
4951
4952 QCBOR_SUCCESS, /* for GetNext */
4953 QCBOR_TYPE_DECIMAL_FRACTION,
4954 -1,
4955 3,
4956 {(const uint8_t []){0x00}, 1},
4957
4958
4959 QCBOR_SUCCESS, /* for GetDecimalFraction */
4960 -1,
4961 3,
4962
4963 QCBOR_SUCCESS, /* for GetDecimalFractionBig */
4964 -1,
4965 {(const uint8_t []){0x03}, 1},
4966 false,
4967
4968 QCBOR_ERR_UNEXPECTED_TYPE, /* for GetBigFloat */
4969 0,
4970 0,
4971
4972 QCBOR_ERR_UNEXPECTED_TYPE, /* for GetBigFloatBig */
4973 0,
4974 {(const uint8_t []){0x00}, 1},
4975 false
4976 },
4977 {
4978 "4. Tagged 100 * 2^300 big float, tag 5 optional",
4979 {(const uint8_t []){0xC5, 0x82, 0x19, 0x01, 0x2C, 0x18, 0x64}, 7},
4980 QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
4981 true,
4982
4983 QCBOR_SUCCESS, /* for GetNext */
4984 QCBOR_TYPE_BIGFLOAT,
4985 300,
4986 100,
4987 {(const uint8_t []){0x00}, 1},
4988
4989
4990 QCBOR_ERR_UNEXPECTED_TYPE, /* for GetDecimalFraction */
4991 0,
4992 0,
4993
4994 QCBOR_ERR_UNEXPECTED_TYPE, /* for GetDecimalFractionBig */
4995 0,
4996 {(const uint8_t []){0x03}, 1},
4997 false,
4998
4999 QCBOR_SUCCESS, /* for GetBigFloat */
5000 300,
5001 100,
5002
5003 QCBOR_SUCCESS, /* for GetBigFloatBig */
5004 300,
5005 {(const uint8_t []){0x64}, 1},
5006 false
5007 },
5008
5009 {
5010 "5. Tagged 4([-20, 4759477275222530853136]) decimal fraction, tag 4 required",
5011 {(const uint8_t []){0xC4, 0x82, 0x33,
5012 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,}, 15},
5013 QCBOR_TAG_REQUIREMENT_TAG,
5014 true,
5015
5016 QCBOR_SUCCESS, /* for GetNext */
5017 QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM,
5018 -20,
5019 0,
5020 {(const uint8_t []){0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10}, 10},
5021
5022 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW, /* for GetDecimalFraction */
5023 0,
5024 0,
5025
5026 QCBOR_SUCCESS, /* for GetDecimalFractionBig */
5027 -20,
5028 {(const uint8_t []){0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10}, 10},
5029 false,
5030
5031 QCBOR_ERR_UNEXPECTED_TYPE, /* for GetBigFloat */
5032 0,
5033 0,
5034
5035 QCBOR_ERR_UNEXPECTED_TYPE, /* for GetBigFloatBig */
5036 0,
5037 {(const uint8_t []){0x00}, 0},
5038 false
5039 },
5040
5041 {
5042 "6. Error: Mantissa and exponent inside a Mantissa and exponent",
5043 {(const uint8_t []){0xC4, 0x82, 0x33,
5044 0xC5, 0x82, 0x19, 0x01, 0x2C, 0x18, 0x64}, 10},
5045 QCBOR_TAG_REQUIREMENT_TAG,
5046 true,
5047
5048 QCBOR_ERR_BAD_EXP_AND_MANTISSA, /* for GetNext */
5049 QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM,
5050 0,
5051 0,
5052 {(const uint8_t []){0x00}, 0},
5053
5054 QCBOR_ERR_BAD_EXP_AND_MANTISSA, /* for GetDecimalFraction */
5055 0,
5056 0,
5057
5058 QCBOR_ERR_BAD_EXP_AND_MANTISSA, /* for GetDecimalFractionBig */
5059 0,
5060 {(const uint8_t []){0x00}, 0},
5061 false,
5062
5063 QCBOR_ERR_BAD_EXP_AND_MANTISSA, /* for GetBigFloat */
5064 0,
5065 0,
5066
5067 QCBOR_ERR_BAD_EXP_AND_MANTISSA, /* for GetBigFloatBig */
5068 0,
5069 {(const uint8_t []){0x00}, 0},
5070 false
5071 },
5072 {
5073 "7. Tagged 5([-20, 4294967295]) big float, big num mantissa, tag 5 required",
5074 {(const uint8_t []){0xC5, 0x82, 0x33,
5075 0xC2, 0x44, 0xff, 0xff, 0xff, 0xff}, 9},
5076 QCBOR_TAG_REQUIREMENT_TAG,
5077 true,
5078
5079 QCBOR_SUCCESS, /* for GetNext */
5080 QCBOR_TYPE_BIGFLOAT_POS_BIGNUM,
5081 -20,
5082 0,
5083 {(const uint8_t []){0xff, 0xff, 0xff, 0xff}, 4},
5084
5085 QCBOR_ERR_UNEXPECTED_TYPE, /* for GetDecimalFraction */
5086 0,
5087 0,
5088
5089 QCBOR_ERR_UNEXPECTED_TYPE, /* for GetDecimalFractionBig */
5090 -20,
5091 {(const uint8_t []){0x00}, 1},
5092 false,
5093
5094 QCBOR_SUCCESS, /* for GetBigFloat */
5095 -20,
5096 4294967295,
5097
5098 QCBOR_SUCCESS, /* for GetBigFloatBig */
5099 -20,
5100 {(const uint8_t []){0xff, 0xff, 0xff, 0xff}, 4},
5101 false
5102 },
5103
5104 {
5105 /* Special case for test 8. Don't renumber it. */
5106 "8. Untagged pair with big num (big float or decimal fraction), tag optional",
5107 {(const uint8_t []){0x82, 0x33, 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10}, 14},
5108 QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
5109 true,
5110
5111 QCBOR_SUCCESS, /* for GetNext */
5112 QCBOR_TYPE_ARRAY,
5113 0,
5114 0,
5115 {(const uint8_t []){0x00}, 1},
5116
5117 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW, /* GetDecimalFraction */
5118 0,
5119 0,
5120
5121 QCBOR_SUCCESS, /* for GetDecimalFractionBig */
5122 -20,
5123 {(const uint8_t []){0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10}, 10},
5124 false,
5125
5126 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW, /* for GetBigFloat */
5127 0,
5128 0,
5129
5130 QCBOR_SUCCESS, /* for GetBigFloatBig */
5131 -20,
5132 {(const uint8_t []){0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10}, 10},
5133 false
5134 },
5135
5136 {
5137 "9. decimal fraction with large exponent and negative big num mantissa",
5138 {(const uint8_t []){0xC4, 0x82, 0x1B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
5139 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10}, 23},
5140 QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
5141 true,
5142
5143 QCBOR_SUCCESS, /* for GetNext */
5144 QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM,
5145 9223372036854775807,
5146 0,
5147 {(const uint8_t []){0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10}, 10},
5148
5149 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW, /* GetDecimalFraction */
5150 0,
5151 0,
5152
5153 QCBOR_SUCCESS, /* for GetDecimalFractionBig */
5154 9223372036854775807,
5155 {(const uint8_t []){0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10}, 10},
5156 true,
5157
5158 QCBOR_ERR_UNEXPECTED_TYPE, /* for GetBigFloat */
5159 0,
5160 0,
5161
5162 QCBOR_ERR_UNEXPECTED_TYPE, /* for GetBigFloatBig */
5163 0,
5164 {(const uint8_t []){0x00}, 1},
5165 false
5166 },
5167};
5168
5169
5170
5171int32_t ProcessEaMTests(void)
Laurence Lundblade59289e52019-12-30 13:44:37 -08005172{
Laurence Lundblade37286c02022-09-03 10:05:02 -07005173 size_t uIndex;
5174 QCBORDecodeContext DCtx;
5175 QCBORItem Item;
5176 QCBORError uError;
5177 int64_t nMantissa, nExponent;
5178 MakeUsefulBufOnStack( MantissaBuf, 200);
5179 UsefulBufC Mantissa;
5180 bool bMantissaIsNegative;
5181
5182 for(uIndex = 0; uIndex < C_ARRAY_COUNT(pEaMTests, struct EaMTest); uIndex++) {
5183 const struct EaMTest *pT = &pEaMTests[uIndex];
5184 /* Decode with GetNext */
5185 QCBORDecode_Init(&DCtx, pT->Input, 0);
5186
5187 if(uIndex + 1 == 9) {
5188 nExponent = 99; // just to set a break point
5189 }
5190
5191 uError = QCBORDecode_GetNext(&DCtx, &Item);
5192#ifdef QCBOR_DISABLE_TAGS
5193 /* Test 8 is a special case when tags are disabled */
5194 if(pT->bHasTags && uIndex + 1 != 8) {
5195 if(uError != QCBOR_ERR_TAGS_DISABLED) {
5196 return (int32_t)(1+uIndex) * 1000 + 9;
5197 }
5198 } else {
5199#endif
5200 /* Now check return code, data type, mantissa and exponent */
5201 if(pT->uExpectedErrorGN != uError) {
5202 return (int32_t)(1+uIndex) * 1000 + 1;
5203 }
5204 if(uError == QCBOR_SUCCESS && pT->uQCBORTypeGN != QCBOR_TYPE_ARRAY) {
5205 if(pT->uQCBORTypeGN != Item.uDataType) {
5206 return (int32_t)(1+uIndex) * 1000 + 2;
5207 }
5208 if(pT->nExponentGN != Item.val.expAndMantissa.nExponent) {
5209 return (int32_t)(1+uIndex) * 1000 + 3;
5210 }
5211 if(Item.uDataType == QCBOR_TYPE_DECIMAL_FRACTION || Item.uDataType == QCBOR_TYPE_BIGFLOAT ) {
5212 if(pT->nMantissaGN != Item.val.expAndMantissa.Mantissa.nInt) {
5213 return (int32_t)(1+uIndex) * 1000 + 4;
5214 }
5215 } else {
5216 if(UsefulBuf_Compare(Item.val.expAndMantissa.Mantissa.bigNum, pT->MantissaGN)) {
5217 return (int32_t)(1+uIndex) * 1000 + 5;
5218 }
5219 }
5220 }
5221#ifdef QCBOR_DISABLE_TAGS
5222 }
5223#endif
5224
5225 /* Decode with GetDecimalFraction */
5226 QCBORDecode_Init(&DCtx, pT->Input, 0);
5227 QCBORDecode_GetDecimalFraction(&DCtx,
5228 pT->uTagRequirement,
5229 &nMantissa,
5230 &nExponent);
5231 uError = QCBORDecode_GetAndResetError(&DCtx);
5232#ifdef QCBOR_DISABLE_TAGS
5233 if(pT->bHasTags) {
5234 if(uError != QCBOR_ERR_TAGS_DISABLED) {
5235 return (int32_t)(1+uIndex) * 1000 + 39;
5236 }
5237 } else {
5238#endif
5239 /* Now check return code, mantissa and exponent */
5240 if(pT->uExpectedErrorGDF != uError) {
5241 return (int32_t)(1+uIndex) * 1000 + 31;
5242 }
5243 if(uError == QCBOR_SUCCESS) {
5244 if(pT->nExponentGDF != nExponent) {
5245 return (int32_t)(1+uIndex) * 1000 + 32;
5246 }
5247 if(pT->nMantissaGDF != nMantissa) {
5248 return (int32_t)(1+uIndex) * 1000 + 33;
5249 }
5250 }
5251#ifdef QCBOR_DISABLE_TAGS
5252 }
5253#endif
5254
5255 /* Decode with GetDecimalFractionBig */
5256 QCBORDecode_Init(&DCtx, pT->Input, 0);
5257 QCBORDecode_GetDecimalFractionBig(&DCtx,
5258 pT->uTagRequirement,
5259 MantissaBuf,
5260 &Mantissa,
5261 &bMantissaIsNegative,
5262 &nExponent);
5263 uError = QCBORDecode_GetAndResetError(&DCtx);
5264#ifdef QCBOR_DISABLE_TAGS
5265 if(pT->bHasTags) {
5266 if(uError != QCBOR_ERR_TAGS_DISABLED) {
5267 return (int32_t)(1+uIndex) * 1000 + 49;
5268 }
5269 } else {
5270#endif
5271 /* Now check return code, mantissa (bytes and sign) and exponent */
5272 if(pT->uExpectedErrorGDFB != uError) {
5273 return (int32_t)(1+uIndex) * 1000 + 41;
5274 }
5275 if(uError == QCBOR_SUCCESS) {
5276 if(pT->nExponentGDFB != nExponent) {
5277 return (int32_t)(1+uIndex) * 1000 + 42;
5278 }
5279 if(pT->IsNegativeGDFB != bMantissaIsNegative) {
5280 return (int32_t)(1+uIndex) * 1000 + 43;
5281 }
5282 if(UsefulBuf_Compare(Mantissa, pT->MantissaGDFB)) {
5283 return (int32_t)(1+uIndex) * 1000 + 44;
5284 }
5285 }
5286#ifdef QCBOR_DISABLE_TAGS
5287 }
5288#endif
5289
5290 /* Decode with GetBigFloat */
5291 QCBORDecode_Init(&DCtx, pT->Input, 0);
5292 QCBORDecode_GetBigFloat(&DCtx,
5293 pT->uTagRequirement,
5294 &nMantissa,
5295 &nExponent);
5296 uError = QCBORDecode_GetAndResetError(&DCtx);
5297#ifdef QCBOR_DISABLE_TAGS
5298 if(pT->bHasTags) {
5299 if(uError != QCBOR_ERR_TAGS_DISABLED) {
5300 return (int32_t)(1+uIndex) * 1000 + 19;
5301 }
5302 } else {
5303#endif
5304 /* Now check return code, mantissa and exponent */
5305 if(pT->uExpectedErrorGBF != uError) {
5306 return (int32_t)(1+uIndex) * 1000 + 11;
5307 }
5308 if(uError == QCBOR_SUCCESS) {
5309 if(pT->nExponentGBF != nExponent) {
5310 return (int32_t)(1+uIndex) * 1000 + 12;
5311 }
5312 if(pT->nMantissaGBF != nMantissa) {
5313 return (int32_t)(1+uIndex) * 1000 + 13;
5314 }
5315 }
5316#ifdef QCBOR_DISABLE_TAGS
5317 }
5318#endif
5319
5320 /* Decode with GetBigFloatBig */
5321 QCBORDecode_Init(&DCtx, pT->Input, 0);
5322 QCBORDecode_GetBigFloatBig(&DCtx,
5323 pT->uTagRequirement,
5324 MantissaBuf,
5325 &Mantissa,
5326 &bMantissaIsNegative,
5327 &nExponent);
5328 uError = QCBORDecode_GetAndResetError(&DCtx);
5329#ifdef QCBOR_DISABLE_TAGS
5330 if(pT->bHasTags) {
5331 if(uError != QCBOR_ERR_TAGS_DISABLED) {
5332 return (int32_t)(1+uIndex) * 1000 + 29;
5333 }
5334 } else {
5335#endif
5336 /* Now check return code, mantissa (bytes and sign) and exponent */
5337 if(pT->uExpectedErrorGBFB != uError) {
5338 return (int32_t)(1+uIndex) * 1000 + 21;
5339 }
5340 if(uError == QCBOR_SUCCESS) {
5341 if(pT->nExponentGBFB != nExponent) {
5342 return (int32_t)(1+uIndex) * 1000 + 22;
5343 }
5344 if(pT->IsNegativeGBFB != bMantissaIsNegative) {
5345 return (int32_t)(1+uIndex) * 1000 + 23;
5346 }
5347 if(UsefulBuf_Compare(Mantissa, pT->MantissaGBFB)) {
5348 return (int32_t)(1+uIndex) * 1000 + 24;
5349 }
5350 }
5351#ifdef QCBOR_DISABLE_TAGS
5352 }
5353#endif
5354 }
5355
5356 return 0;
5357}
5358
5359
5360int32_t ExponentAndMantissaDecodeTestsSecondary(void)
5361{
5362#ifndef QCBOR_DISABLE_TAGS
Laurence Lundblade59289e52019-12-30 13:44:37 -08005363 QCBORDecodeContext DC;
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07005364 QCBORError uErr;
Laurence Lundblade59289e52019-12-30 13:44:37 -08005365 QCBORItem item;
5366
Laurence Lundblade17af4902020-01-07 19:11:55 -08005367 static const uint8_t spBigNumMantissa[] = {0x01, 0x02, 0x03, 0x04, 0x05,
5368 0x06, 0x07, 0x08, 0x09, 0x010};
5369 UsefulBufC BN = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNumMantissa);
Laurence Lundblade59289e52019-12-30 13:44:37 -08005370
5371
Laurence Lundblade59289e52019-12-30 13:44:37 -08005372
5373 /* Now encode some stuff and then decode it */
5374 uint8_t pBuf[40];
5375 QCBOREncodeContext EC;
5376 UsefulBufC Encoded;
5377
5378 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(pBuf));
5379 QCBOREncode_OpenArray(&EC);
5380 QCBOREncode_AddDecimalFraction(&EC, 999, 1000); // 999 * (10 ^ 1000)
5381 QCBOREncode_AddBigFloat(&EC, 100, INT32_MIN);
5382 QCBOREncode_AddDecimalFractionBigNum(&EC, BN, false, INT32_MAX);
5383 QCBOREncode_CloseArray(&EC);
5384 QCBOREncode_Finish(&EC, &Encoded);
5385
5386
5387 QCBORDecode_Init(&DC, Encoded, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07005388 uErr = QCBORDecode_GetNext(&DC, &item);
5389 if(uErr != QCBOR_SUCCESS) {
5390 return 100;
Laurence Lundblade59289e52019-12-30 13:44:37 -08005391 }
5392
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07005393 uErr = QCBORDecode_GetNext(&DC, &item);
5394 if(uErr != QCBOR_SUCCESS) {
5395 return 101;
Laurence Lundblade59289e52019-12-30 13:44:37 -08005396 }
5397
5398 if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION ||
5399 item.val.expAndMantissa.nExponent != 1000 ||
5400 item.val.expAndMantissa.Mantissa.nInt != 999) {
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07005401 return 102;
Laurence Lundblade59289e52019-12-30 13:44:37 -08005402 }
5403
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07005404 uErr = QCBORDecode_GetNext(&DC, &item);
5405 if(uErr != QCBOR_SUCCESS) {
5406 return 103;
Laurence Lundblade59289e52019-12-30 13:44:37 -08005407 }
5408
5409 if(item.uDataType != QCBOR_TYPE_BIGFLOAT ||
5410 item.val.expAndMantissa.nExponent != INT32_MIN ||
5411 item.val.expAndMantissa.Mantissa.nInt != 100) {
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07005412 return 104;
Laurence Lundblade59289e52019-12-30 13:44:37 -08005413 }
5414
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07005415 uErr = QCBORDecode_GetNext(&DC, &item);
5416 if(uErr != QCBOR_SUCCESS) {
5417 return 105;
Laurence Lundblade59289e52019-12-30 13:44:37 -08005418 }
5419
5420 if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM ||
5421 item.val.expAndMantissa.nExponent != INT32_MAX ||
5422 UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) {
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07005423 return 106;
5424 }
5425
Laurence Lundblade37286c02022-09-03 10:05:02 -07005426#endif /* QCBOR_TAGS_DISABLED */
Laurence Lundblade59289e52019-12-30 13:44:37 -08005427
5428 return 0;
5429}
5430
5431
Laurence Lundblade37286c02022-09-03 10:05:02 -07005432int32_t ExponentAndMantissaDecodeTests(void)
5433{
5434 int32_t rv = ProcessEaMTests();
5435 if(rv) {
5436 return rv;
5437 }
5438
5439 return ExponentAndMantissaDecodeTestsSecondary();
5440}
5441
5442
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08005443static const struct DecodeFailTestInput ExponentAndMantissaFailures[] = {
5444 { "Exponent > INT64_MAX",
5445 QCBOR_DECODE_MODE_NORMAL,
5446 {"\xC4\x82\x1B\x7f\xFF\xFF\xFF\xFF\xFF\xFF\xFF\x1B\x80\xFF\xFF\xFF\xFF\xFF\xFF\xFF", 20},
5447 QCBOR_ERR_BAD_EXP_AND_MANTISSA
5448 },
5449 { "Mantissa > INT64_MAX",
5450 QCBOR_DECODE_MODE_NORMAL,
5451 {"\xC4\x82\x1B\x80\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xC3\x4A\x01\x02\x03\x04\x05\x06\x07\x08\x09\x10", 23},
5452 QCBOR_ERR_BAD_EXP_AND_MANTISSA
5453 },
5454 {
5455 "End of input",
5456 QCBOR_DECODE_MODE_NORMAL,
5457 {"\xC4\x82", 2},
5458 QCBOR_ERR_NO_MORE_ITEMS
5459 },
5460 {"bad content for big num",
5461 QCBOR_DECODE_MODE_NORMAL,
5462 {"\xC4\x82\x01\xc3\x01", 5},
5463 QCBOR_ERR_BAD_OPT_TAG
5464 },
5465 {"bad content for big num",
5466 QCBOR_DECODE_MODE_NORMAL,
5467 {"\xC4\x82\xc2\x01\x1f", 5},
5468 QCBOR_ERR_BAD_INT
5469 },
5470 {"Bad integer for exponent",
5471 QCBOR_DECODE_MODE_NORMAL,
5472 {"\xC4\x82\x01\x1f", 4},
5473 QCBOR_ERR_BAD_INT
5474 },
5475 {"Bad integer for mantissa",
5476 QCBOR_DECODE_MODE_NORMAL,
5477 {"\xC4\x82\x1f\x01", 4},
5478 QCBOR_ERR_BAD_INT
5479 },
5480 {"3 items in array",
5481 QCBOR_DECODE_MODE_NORMAL,
5482 {"\xC4\x83\x03\x01\x02", 5},
5483 QCBOR_ERR_BAD_EXP_AND_MANTISSA},
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08005484#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08005485 {"unterminated indefinite length array",
5486 QCBOR_DECODE_MODE_NORMAL,
5487 {"\xC4\x9f\x03\x01\x02", 5},
5488 QCBOR_ERR_BAD_EXP_AND_MANTISSA
5489 },
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08005490#else /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08005491 {"unterminated indefinite length array",
5492 QCBOR_DECODE_MODE_NORMAL,
5493 {"\xC4\x9f\x03\x01\x02", 5},
5494 QCBOR_ERR_INDEF_LEN_ARRAYS_DISABLED
5495 },
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08005496#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08005497 {"Empty array",
5498 QCBOR_DECODE_MODE_NORMAL,
5499 {"\xC4\x80", 2},
5500 QCBOR_ERR_NO_MORE_ITEMS
5501 },
5502 {"Second is not an integer",
5503 QCBOR_DECODE_MODE_NORMAL,
5504 {"\xC4\x82\x03\x40", 4},
5505 QCBOR_ERR_BAD_EXP_AND_MANTISSA
5506 },
5507 {"First is not an integer",
5508 QCBOR_DECODE_MODE_NORMAL,
5509 {"\xC4\x82\x40", 3},
5510 QCBOR_ERR_BAD_EXP_AND_MANTISSA
5511 },
5512 {"Not an array",
5513 QCBOR_DECODE_MODE_NORMAL,
5514 {"\xC4\xA2", 2},
5515 QCBOR_ERR_BAD_EXP_AND_MANTISSA
5516 }
Laurence Lundblade59289e52019-12-30 13:44:37 -08005517};
5518
5519
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08005520int32_t
5521ExponentAndMantissaDecodeFailTests(void)
Laurence Lundblade59289e52019-12-30 13:44:37 -08005522{
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08005523 return ProcessDecodeFailures(ExponentAndMantissaFailures,
5524 C_ARRAY_COUNT(ExponentAndMantissaFailures,
5525 struct DecodeFailTestInput));
Laurence Lundblade59289e52019-12-30 13:44:37 -08005526}
5527
Laurence Lundbladedd6e76e2021-03-10 01:54:01 -07005528#endif /* QCBOR_DISABLE_EXP_AND_MANTISSA */
Laurence Lundbladebb87be22020-04-09 19:15:32 -07005529
5530
5531
5532/*
5533 Some basic CBOR with map and array used in a lot of tests.
5534 The map labels are all strings
5535
Laurence Lundblade8ffdb742020-05-07 02:49:18 -07005536 {
5537 "first integer": 42,
Laurence Lundbladebb87be22020-04-09 19:15:32 -07005538 "an array of two strings": [
5539 "string1", "string2"
5540 ],
5541 "map in a map": {
5542 "bytes 1": h'78787878',
5543 "bytes 2": h'79797979',
5544 "another int": 98,
5545 "text 2": "lies, damn lies and statistics"
5546 }
Laurence Lundbladeddebabe2020-11-22 11:32:17 -08005547 }
Laurence Lundbladebb87be22020-04-09 19:15:32 -07005548 */
Laurence Lundblade9b334962020-08-27 10:55:53 -07005549
Laurence Lundbladef7a70bc2020-10-24 12:23:25 -07005550int32_t SpiffyDecodeBasicMap(UsefulBufC input)
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07005551{
5552 QCBORItem Item1, Item2, Item3;
5553 int64_t nDecodedInt1, nDecodedInt2;
5554 UsefulBufC B1, B2, S1, S2, S3;
5555
5556 QCBORDecodeContext DCtx;
5557 QCBORError nCBORError;
5558
5559 QCBORDecode_Init(&DCtx, input, 0);
5560
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005561 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07005562
5563 QCBORDecode_GetInt64InMapSZ(&DCtx, "first integer", &nDecodedInt1);
5564
5565 QCBORDecode_EnterMapFromMapSZ(&DCtx, "map in a map");
5566 QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2);
Laurence Lundblade323f8a92020-09-06 19:43:09 -07005567 QCBORDecode_GetByteStringInMapSZ(&DCtx, "bytes 1", &B1);
5568 QCBORDecode_GetByteStringInMapSZ(&DCtx, "bytes 2", &B2);
5569 QCBORDecode_GetTextStringInMapSZ(&DCtx, "text 2", &S1);
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07005570 QCBORDecode_ExitMap(&DCtx);
5571
5572 QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings");
5573 QCBORDecode_GetNext(&DCtx, &Item1);
5574 QCBORDecode_GetNext(&DCtx, &Item2);
5575 if(QCBORDecode_GetNext(&DCtx, &Item3) != QCBOR_ERR_NO_MORE_ITEMS) {
5576 return -400;
5577 }
5578 QCBORDecode_ExitArray(&DCtx);
5579
5580 // Parse the same array again using GetText() instead of GetItem()
5581 QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings");
Laurence Lundblade323f8a92020-09-06 19:43:09 -07005582 QCBORDecode_GetTextString(&DCtx, &S2);
5583 QCBORDecode_GetTextString(&DCtx, &S3);
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07005584 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) {
5585 return 5000;
5586 }
5587 /* QCBORDecode_GetText(&DCtx, &S3);
5588 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_NO_MORE_ITEMS) {
5589 return 5001;
5590 } */
5591
5592 QCBORDecode_ExitArray(&DCtx);
5593
5594 QCBORDecode_ExitMap(&DCtx);
5595
5596 nCBORError = QCBORDecode_Finish(&DCtx);
5597
5598 if(nCBORError) {
5599 return (int32_t)nCBORError;
5600 }
5601
5602 if(nDecodedInt1 != 42) {
5603 return 1001;
5604 }
5605
5606 if(nDecodedInt2 != 98) {
5607 return 1002;
5608 }
5609
5610 if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07005611 UsefulBufCompareToSZ(Item1.val.string, "string1")) {
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07005612 return 1003;
5613 }
5614
5615 if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07005616 UsefulBufCompareToSZ(Item2.val.string, "string2")) {
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07005617 return 1004;
5618 }
5619
Laurence Lundblade9b334962020-08-27 10:55:53 -07005620 if(UsefulBufCompareToSZ(S1, "lies, damn lies and statistics")) {
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07005621 return 1005;
5622 }
5623
5624 if(UsefulBuf_Compare(B1, UsefulBuf_FromSZ("xxxx"))){
5625 return 1006;
5626 }
5627
5628 if(UsefulBuf_Compare(B2, UsefulBuf_FromSZ("yyyy"))){
5629 return 1007;
5630 }
5631
5632 if(UsefulBuf_Compare(S2, UsefulBuf_FromSZ("string1"))){
5633 return 1008;
5634 }
5635
5636 if(UsefulBuf_Compare(S3, UsefulBuf_FromSZ("string2"))){
5637 return 1009;
5638 }
5639
5640 return 0;
5641}
5642
Laurence Lundbladeddebabe2020-11-22 11:32:17 -08005643/*
Laurence Lundbladecc7da412020-12-27 00:09:07 -08005644 {
5645 -75008: h'05083399',
5646 88: [],
5647 100100: {
5648 "sub1": {
5649 10: [
5650 0
5651 ],
5652 -75009: h'A46823990001',
5653 100100: {
5654 "json": "{ \"ueid\", \"xyz\"}",
5655 "subsub": {
5656 100002: h'141813191001'
5657 }
5658 }
Laurence Lundbladeddebabe2020-11-22 11:32:17 -08005659 }
Laurence Lundbladecc7da412020-12-27 00:09:07 -08005660 }
Laurence Lundbladeddebabe2020-11-22 11:32:17 -08005661 }
Laurence Lundbladeddebabe2020-11-22 11:32:17 -08005662 */
5663
5664static const uint8_t spNestedCBOR[] = {
Laurence Lundbladecc7da412020-12-27 00:09:07 -08005665 0xa3, 0x3a, 0x00, 0x01, 0x24, 0xff, 0x44, 0x05,
5666 0x08, 0x33, 0x99, 0x18, 0x58, 0x80, 0x1a, 0x00,
5667 0x01, 0x87, 0x04, 0xa1, 0x64, 0x73, 0x75, 0x62,
5668 0x31, 0xa3, 0x0a, 0x81, 0x00, 0x3a, 0x00, 0x01,
5669 0x25, 0x00, 0x46, 0xa4, 0x68, 0x23, 0x99, 0x00,
5670 0x01, 0x1a, 0x00, 0x01, 0x87, 0x04, 0xa2, 0x64,
5671 0x6a, 0x73, 0x6f, 0x6e, 0x70, 0x7b, 0x20, 0x22,
5672 0x75, 0x65, 0x69, 0x64, 0x22, 0x2c, 0x20, 0x22,
5673 0x78, 0x79, 0x7a, 0x22, 0x7d, 0x66, 0x73, 0x75,
5674 0x62, 0x73, 0x75, 0x62, 0xa1, 0x1a, 0x00, 0x01,
5675 0x86, 0xa2, 0x46, 0x14, 0x18, 0x13, 0x19, 0x10,
5676 0x01
Laurence Lundbladeddebabe2020-11-22 11:32:17 -08005677};
5678
5679/* Get item in multi-level nesting in spNestedCBOR */
5680static int32_t DecodeNestedGetSubSub(QCBORDecodeContext *pDCtx)
5681{
5682 UsefulBufC String;
5683
5684 uint8_t test_oemid_bytes[] = {0x14, 0x18, 0x13, 0x19, 0x10, 0x01};
5685 const struct q_useful_buf_c test_oemid = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(test_oemid_bytes);
5686
5687 QCBORDecode_EnterMapFromMapN(pDCtx, 100100);
5688 QCBORDecode_EnterMap(pDCtx, NULL);
5689 QCBORDecode_EnterMapFromMapN(pDCtx, 100100);
5690 QCBORDecode_EnterMapFromMapSZ(pDCtx, "subsub");
5691 QCBORDecode_GetByteStringInMapN(pDCtx, 100002, &String);
5692 if(QCBORDecode_GetError(pDCtx)) {
5693 return 4001;
5694 }
5695 if(UsefulBuf_Compare(String, test_oemid)) {
5696 return 4002;
5697 }
5698 QCBORDecode_ExitMap(pDCtx);
5699 QCBORDecode_ExitMap(pDCtx);
5700 QCBORDecode_ExitMap(pDCtx);
5701 QCBORDecode_ExitMap(pDCtx);
5702
5703 return 0;
5704}
5705
5706/* Iterations on the zero-length array in spNestedCBOR */
5707static int32_t DecodeNestedGetEmpty(QCBORDecodeContext *pDCtx)
5708{
5709 QCBORItem Item;
5710 QCBORError uErr;
5711
5712 QCBORDecode_EnterArrayFromMapN(pDCtx, 88);
5713 for(int x = 0; x < 20; x++) {
5714 uErr = QCBORDecode_GetNext(pDCtx, &Item);
5715 if(uErr != QCBOR_ERR_NO_MORE_ITEMS) {
5716 return 4100;
5717
5718 }
5719 }
5720 QCBORDecode_ExitArray(pDCtx);
5721 if(QCBORDecode_GetError(pDCtx)) {
5722 return 4101;
5723 }
5724
5725 return 0;
5726}
5727
5728/* Various iterations on the array that contains a zero in spNestedCBOR */
5729static int32_t DecodeNestedGetZero(QCBORDecodeContext *pDCtx)
5730{
5731 QCBORError uErr;
5732
5733 QCBORDecode_EnterMapFromMapN(pDCtx, 100100);
5734 QCBORDecode_EnterMapFromMapSZ(pDCtx, "sub1");
5735 QCBORDecode_EnterArrayFromMapN(pDCtx, 10);
5736 int64_t nInt = 99;
5737 QCBORDecode_GetInt64(pDCtx, &nInt);
5738 if(nInt != 0) {
5739 return 4200;
5740 }
5741 for(int x = 0; x < 20; x++) {
5742 QCBORItem Item;
5743 uErr = QCBORDecode_GetNext(pDCtx, &Item);
5744 if(uErr != QCBOR_ERR_NO_MORE_ITEMS) {
5745 return 4201;
5746
5747 }
5748 }
5749 QCBORDecode_ExitArray(pDCtx);
5750 if(QCBORDecode_GetAndResetError(pDCtx)) {
5751 return 4202;
5752 }
5753 QCBORDecode_EnterArrayFromMapN(pDCtx, 10);
5754 UsefulBufC dD;
5755 QCBORDecode_GetByteString(pDCtx, &dD);
5756 if(QCBORDecode_GetAndResetError(pDCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
5757 return 4203;
5758 }
5759 for(int x = 0; x < 20; x++) {
5760 QCBORDecode_GetByteString(pDCtx, &dD);
5761 uErr = QCBORDecode_GetAndResetError(pDCtx);
5762 if(uErr != QCBOR_ERR_NO_MORE_ITEMS) {
5763 return 4204;
5764 }
5765 }
5766 QCBORDecode_ExitArray(pDCtx);
5767 QCBORDecode_ExitMap(pDCtx);
5768 QCBORDecode_ExitMap(pDCtx);
5769
5770 return 0;
5771}
5772
5773/* Repeatedly enter and exit maps and arrays, go off the end of maps
5774 and arrays and such. */
Laurence Lundbladeb9702452021-03-08 21:02:57 -08005775static int32_t DecodeNestedIterate(void)
Laurence Lundbladeddebabe2020-11-22 11:32:17 -08005776{
5777 QCBORDecodeContext DCtx;
5778 int32_t nReturn;
5779 QCBORError uErr;
5780
5781 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spNestedCBOR), 0);
5782 QCBORDecode_EnterMap(&DCtx, NULL);
5783
5784 for(int j = 0; j < 5; j++) {
5785 for(int i = 0; i < 20; i++) {
5786 nReturn = DecodeNestedGetSubSub(&DCtx);
5787 if(nReturn) {
5788 return nReturn;
5789 }
5790 }
5791
5792 for(int i = 0; i < 20; i++) {
5793 nReturn = DecodeNestedGetEmpty(&DCtx);
5794 if(nReturn ) {
5795 return nReturn;
5796 }
5797 }
5798
5799 for(int i = 0; i < 20; i++) {
5800 nReturn = DecodeNestedGetZero(&DCtx);
5801 if(nReturn ) {
5802 return nReturn;
5803 }
5804 }
5805 }
5806
5807 QCBORDecode_ExitMap(&DCtx);
5808 uErr = QCBORDecode_Finish(&DCtx);
5809 if(uErr) {
5810 return (int32_t)uErr + 4100;
5811 }
5812
5813 return 0;
5814}
5815
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07005816
5817/*
Laurence Lundbladecc7da412020-12-27 00:09:07 -08005818 [
5819 23,
5820 6000,
5821 h'67616C6163746963',
5822 h'686176656E20746F6B656E'
5823 ]
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07005824 */
5825static const uint8_t spSimpleArray[] = {
Laurence Lundblade9b334962020-08-27 10:55:53 -07005826 0x84,
5827 0x17,
5828 0x19, 0x17, 0x70,
5829 0x48, 0x67, 0x61, 0x6C, 0x61, 0x63, 0x74, 0x69, 0x63,
5830 0x4B, 0x68, 0x61, 0x76, 0x65, 0x6E, 0x20, 0x74, 0x6F, 0x6B, 0x65, 0x6E};
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07005831
Laurence Lundbladecc7da412020-12-27 00:09:07 -08005832/* [h'', {}, [], 0] */
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08005833static const uint8_t spArrayOfEmpty[] = {0x84, 0x40, 0xa0, 0x80, 0x00};
5834
Laurence Lundbladecc7da412020-12-27 00:09:07 -08005835/* {} */
Laurence Lundbladee6f15112020-07-23 18:44:16 -07005836static const uint8_t spEmptyMap[] = {0xa0};
5837
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08005838#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundbladecc7da412020-12-27 00:09:07 -08005839/* {} */
Laurence Lundbladee6f15112020-07-23 18:44:16 -07005840static const uint8_t spEmptyInDefinteLengthMap[] = {0xbf, 0xff};
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07005841
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08005842
Laurence Lundbladef0499502020-08-01 11:55:57 -07005843/*
5844 {
5845 0: [],
5846 9: [
5847 [],
5848 []
5849 ],
5850 8: {
5851 1: [],
5852 2: {},
5853 3: []
5854 },
5855 4: {},
5856 5: [],
5857 6: [
5858 [],
5859 []
5860 ]
Laurence Lundbladecc7da412020-12-27 00:09:07 -08005861 }
Laurence Lundbladef0499502020-08-01 11:55:57 -07005862 */
5863static const uint8_t spMapOfEmpty[] = {
Laurence Lundbladecc7da412020-12-27 00:09:07 -08005864 0xa6, 0x00, 0x80, 0x09, 0x82, 0x80, 0x80, 0x08,
5865 0xa3, 0x01, 0x80, 0x02, 0xa0, 0x03, 0x80, 0x04,
5866 0xa0, 0x05, 0x9f, 0xff, 0x06, 0x9f, 0x80, 0x9f,
5867 0xff, 0xff};
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005868
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08005869#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
5870
5871
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005872/*
5873 Too many tags
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005874 Duplicate label
5875 Integer overflow
5876 Date overflow
5877
Laurence Lundbladecc7da412020-12-27 00:09:07 -08005878 {
5879 1: 224(225(226(227(4(0))))),
Laurence Lundbladecc7da412020-12-27 00:09:07 -08005880 3: -18446744073709551616,
5881 4: 1(1.0e+300),
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07005882 5: 0,
5883 8: 8
Laurence Lundbladecc7da412020-12-27 00:09:07 -08005884 }
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005885 */
5886static const uint8_t spRecoverableMapErrors[] = {
Laurence Lundblade37286c02022-09-03 10:05:02 -07005887#ifndef QCBOR_DISABLE_TAGS
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07005888 0xa6,
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005889 0x04, 0xc1, 0xfb, 0x7e, 0x37, 0xe4, 0x3c, 0x88, 0x00, 0x75, 0x9c,
Laurence Lundblade37286c02022-09-03 10:05:02 -07005890 0x01, 0xd8, 0xe0, 0xd8, 0xe1, 0xd8, 0xe2, 0xd8, 0xe3, 0xd8, 0x04, 0x00,
5891#else
5892 0xa4,
5893#endif
5894 0x03, 0x3b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005895 0x05, 0x00,
5896 0x05, 0x00,
5897 0x08, 0x08,
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005898};
5899
Laurence Lundbladecc7da412020-12-27 00:09:07 -08005900/* Bad break */
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005901static const uint8_t spUnRecoverableMapError1[] = {
5902 0xa2, 0xff, 0x01, 0x00, 0x02, 0x00
5903};
5904
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08005905#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundbladecc7da412020-12-27 00:09:07 -08005906/* No more items */
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005907static const uint8_t spUnRecoverableMapError2[] = {
5908 0xbf, 0x02, 0xbf, 0xff, 0x01, 0x00, 0x02, 0x00
5909};
5910
Laurence Lundbladecc7da412020-12-27 00:09:07 -08005911/* Hit end because string is too long */
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005912static const uint8_t spUnRecoverableMapError3[] = {
5913 0xbf, 0x02, 0x69, 0x64, 0x64, 0xff
5914};
5915
Laurence Lundbladecc7da412020-12-27 00:09:07 -08005916/* Hit end because string is too long */
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005917static const uint8_t spUnRecoverableMapError4[] = {
5918 0xbf,
5919 0x02, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f,
5920 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f,
5921 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
5922 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
5923 0xff
5924};
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08005925#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005926
Laurence Lundblade63926052021-03-29 16:05:51 -07005927const unsigned char not_well_formed_submod_section[] = {
5928 0xa1, 0x14, 0x1f,
5929};
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005930
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07005931
5932/* Array of length 3, but only two items. */
5933const unsigned char spBadConsumeInput[] = {
5934 0x83, 0x00, 0x00
5935};
5936
5937/* Tag nesting too deep. */
5938const unsigned char spBadConsumeInput2[] = {
5939 0x81,
5940 0xD8, 0x37,
5941 0xD8, 0x2C,
5942 0xD8, 0x21,
5943 0xD6,
5944 0xCB,
5945 00
5946};
5947
5948const unsigned char spBadConsumeInput3[] = {
5949 0x81, 0xc0, 0x81, 0x00
5950};
5951
5952const unsigned char spBadConsumeInput4[] = {
5953 0x81, 0x9f, 0x00, 0xff
5954};
5955
5956const unsigned char spBadConsumeInput5[] = {
5957 0xa1, 0x80, 0x00
5958};
5959
Laurence Lundbladec5f45e42023-12-18 09:23:20 -07005960/*
5961 Lots of nesting for various nesting tests.
5962 { 1:1,
5963 2:{
5964 21:21,
5965 22:{
5966 221:[2111, 2112, 2113],
5967 222:222,
5968 223: {}
5969 },
5970 23: 23
5971 },
5972 3:3,
5973 4: [ {} ]
5974 }
5975 */
5976static const uint8_t spNested[] = {
59770xA4, /* Map of 4 */
5978 0x01, 0x01, /* Map entry 1 : 1 */
5979 0x02, 0xA3, /* Map entry 2 : {, an array of 3 */
5980 0x15, 0x15, /* Map entry 21 : 21 */
5981 0x16, 0xA3, /* Map entry 22 : {, a map of 3 */
5982 0x18, 0xDD, 0x83, /* Map entry 221 : [ an array of 3 */
5983 0x19, 0x08, 0x3F, /* Array item 2111 */
5984 0x19, 0x08, 0x40, /* Array item 2112 */
5985 0x19, 0x08, 0x41, /* Array item 2113 */
5986 0x18, 0xDE, 0x18, 0xDE, /* Map entry 222 : 222 */
5987 0x18, 0xDF, 0xA0, /* Map entry 223 : {} */
5988 0x17, 0x17, /* Map entry 23 : 23 */
5989 0x03, 0x03, /* Map entry 3 : 3 */
5990 0x04, 0x81, /* Map entry 4: [, an array of 1 */
5991 0xA0 /* Array entry {}, an empty map */
5992};
5993
5994
5995static int32_t EnterMapCursorTest(void)
5996{
5997 QCBORDecodeContext DCtx;
5998 QCBORItem Item1;
Laurence Lundblade11654912024-05-09 11:49:24 -07005999 int64_t nInt;
6000 QCBORError uErr;
6001
6002 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spNested), 0);
6003 QCBORDecode_EnterMap(&DCtx, NULL);
6004 QCBORDecode_GetInt64InMapN (&DCtx, 3, &nInt);
6005 uErr = QCBORDecode_GetNext(&DCtx, &Item1);
6006 if(Item1.uDataType != QCBOR_TYPE_INT64) {
6007 return 700;
6008 }
6009
Laurence Lundbladec5f45e42023-12-18 09:23:20 -07006010
6011 int i;
6012 for(i = 0; i < 13; i++) {
6013 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spNested), 0);
6014 QCBORDecode_EnterMap(&DCtx, NULL);
6015 int j;
6016 /* Move travesal cursor */
6017 for(j = 0; j < i; j++) {
6018 QCBORDecode_GetNext(&DCtx, &Item1);
6019 }
6020 QCBORDecode_EnterMapFromMapN(&DCtx, 2);
6021 QCBORDecode_ExitMap(&DCtx);
6022 QCBORDecode_GetNext(&DCtx, &Item1);
6023 if(Item1.label.int64 != 3) {
6024 return 8000;
6025 }
6026 }
6027
6028 for(i = 0; i < 13; i++) {
6029 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spNested), 0);
6030 QCBORDecode_EnterMap(&DCtx, NULL);
6031 int j;
6032 /* Move travesal cursor */
6033 for(j = 0; j < i; j++) {
6034 QCBORDecode_GetNext(&DCtx, &Item1);
6035 }
6036 QCBORDecode_EnterMapFromMapN(&DCtx, 2);
6037 QCBORDecode_EnterMapFromMapN(&DCtx, 22);
6038 QCBORDecode_ExitMap(&DCtx);
6039 QCBORDecode_GetNext(&DCtx, &Item1);
6040 if(Item1.label.int64 != 23) {
6041 return 8000;
6042 }
6043 }
6044
6045 for(i = 0; i < 13; i++) {
6046 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spNested), 0);
6047 QCBORDecode_EnterMap(&DCtx, NULL);
6048 int j;
6049 /* Move travesal cursor */
6050 for(j = 0; j < i; j++) {
6051 QCBORDecode_GetNext(&DCtx, &Item1);
6052 }
6053 QCBORDecode_EnterMapFromMapN(&DCtx, 2);
6054 QCBORDecode_EnterMapFromMapN(&DCtx, 22);
6055 for(j = 0; j < i; j++) {
6056 QCBORDecode_GetNext(&DCtx, &Item1);
6057 }
6058 QCBORDecode_EnterArrayFromMapN(&DCtx, 221);
6059 QCBORDecode_ExitArray(&DCtx);
6060 QCBORDecode_ExitMap(&DCtx);
6061 QCBORDecode_GetNext(&DCtx, &Item1);
6062 if(Item1.label.int64 != 23) {
6063 return 8000;
6064 }
6065 QCBORDecode_ExitMap(&DCtx);
6066 QCBORDecode_GetNext(&DCtx, &Item1);
6067 if(Item1.label.int64 != 3) {
6068 return 8000;
6069 }
6070 }
6071
6072 return 0;
6073}
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07006074
6075
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03006076int32_t EnterMapTest(void)
Laurence Lundbladebb87be22020-04-09 19:15:32 -07006077{
Laurence Lundbladef0499502020-08-01 11:55:57 -07006078 QCBORItem Item1;
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006079 QCBORItem ArrayItem;
Laurence Lundbladebb87be22020-04-09 19:15:32 -07006080 QCBORDecodeContext DCtx;
Laurence Lundbladef0499502020-08-01 11:55:57 -07006081 int32_t nReturn;
6082 QCBORError uErr;
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07006083
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08006084#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07006085 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spMapOfEmpty), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006086 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07006087
Laurence Lundbladef0499502020-08-01 11:55:57 -07006088
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006089 QCBORDecode_EnterArray(&DCtx, NULL); // Label 0
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07006090 QCBORDecode_ExitArray(&DCtx);
6091
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006092 QCBORDecode_EnterArray(&DCtx, NULL); // Label 9
6093 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07006094 QCBORDecode_ExitArray(&DCtx);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006095 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07006096 QCBORDecode_ExitArray(&DCtx);
6097 QCBORDecode_ExitArray(&DCtx);
6098
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006099 QCBORDecode_EnterMap(&DCtx, NULL); // Label 8
6100 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07006101 QCBORDecode_ExitArray(&DCtx);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006102 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07006103 QCBORDecode_ExitMap(&DCtx);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006104 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07006105 QCBORDecode_ExitArray(&DCtx);
6106 QCBORDecode_ExitMap(&DCtx);
6107
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006108 QCBORDecode_EnterMap(&DCtx, NULL); // Label4
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07006109 QCBORDecode_ExitMap(&DCtx);
6110
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006111 QCBORDecode_EnterArray(&DCtx, NULL); // Label 5
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07006112 QCBORDecode_ExitArray(&DCtx);
6113
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006114 QCBORDecode_EnterArray(&DCtx, NULL); // Label 6
6115 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07006116 QCBORDecode_ExitArray(&DCtx);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006117 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07006118 QCBORDecode_ExitArray(&DCtx);
6119 QCBORDecode_ExitArray(&DCtx);
6120
6121 QCBORDecode_ExitMap(&DCtx);
6122
6123 uErr = QCBORDecode_Finish(&DCtx);
6124 if(uErr != QCBOR_SUCCESS){
6125 return 3011;
6126 }
6127
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07006128 (void)pValidMapIndefEncoded;
Laurence Lundbladef7a70bc2020-10-24 12:23:25 -07006129 nReturn = SpiffyDecodeBasicMap(UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapIndefEncoded));
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07006130 if(nReturn) {
6131 return nReturn + 20000;
Laurence Lundbladebb87be22020-04-09 19:15:32 -07006132 }
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08006133#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
6134
Laurence Lundbladebb87be22020-04-09 19:15:32 -07006135
Laurence Lundbladef7a70bc2020-10-24 12:23:25 -07006136 nReturn = SpiffyDecodeBasicMap(UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded));
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006137 if(nReturn) {
6138 return nReturn;
6139 }
Laurence Lundbladebb87be22020-04-09 19:15:32 -07006140
Laurence Lundblade8ffdb742020-05-07 02:49:18 -07006141
Laurence Lundblade937ea812020-05-08 11:38:23 -07006142
Laurence Lundblade2f467f92020-10-09 17:50:11 -07006143 // These tests confirm the cursor is at the right place after entering
6144 // a map or array
Laurence Lundblade9b334962020-08-27 10:55:53 -07006145 const UsefulBufC ValidEncodedMap = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded);
Laurence Lundblade937ea812020-05-08 11:38:23 -07006146
6147 // Confirm cursor is at right place
Laurence Lundblade9b334962020-08-27 10:55:53 -07006148 QCBORDecode_Init(&DCtx, ValidEncodedMap, 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006149 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade937ea812020-05-08 11:38:23 -07006150 QCBORDecode_GetNext(&DCtx, &Item1);
6151 if(Item1.uDataType != QCBOR_TYPE_INT64) {
6152 return 2001;
6153 }
6154
6155
Laurence Lundblade9b334962020-08-27 10:55:53 -07006156 QCBORDecode_Init(&DCtx, ValidEncodedMap, 0);
Laurence Lundblade6f3f78e2020-08-31 13:09:14 -07006157 QCBORDecode_VGetNext(&DCtx, &Item1);
6158 QCBORDecode_VGetNext(&DCtx, &Item1);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006159 QCBORDecode_EnterArray(&DCtx, &ArrayItem);
6160 if(ArrayItem.uLabelType != QCBOR_TYPE_TEXT_STRING ||
6161 UsefulBuf_Compare(ArrayItem.label.string,
6162 UsefulBuf_FROM_SZ_LITERAL("an array of two strings"))) {
6163 return 2051;
6164 }
Laurence Lundblade937ea812020-05-08 11:38:23 -07006165 QCBORDecode_GetNext(&DCtx, &Item1);
6166 if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING) {
6167 return 2002;
6168 }
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006169 QCBORDecode_ExitArray(&DCtx);
6170 QCBORDecode_EnterMap(&DCtx, &ArrayItem);
6171 if(ArrayItem.uLabelType != QCBOR_TYPE_TEXT_STRING ||
6172 UsefulBuf_Compare(ArrayItem.label.string,
6173 UsefulBuf_FROM_SZ_LITERAL("map in a map"))) {
6174 return 2052;
6175 }
6176
Laurence Lundblade937ea812020-05-08 11:38:23 -07006177
Laurence Lundblade9b334962020-08-27 10:55:53 -07006178 QCBORDecode_Init(&DCtx, ValidEncodedMap, 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006179 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade64b607e2020-05-13 13:05:57 -07006180 QCBORDecode_GetNext(&DCtx, &Item1);
6181 QCBORDecode_GetNext(&DCtx, &Item1);
6182 QCBORDecode_GetNext(&DCtx, &Item1);
Laurence Lundblade937ea812020-05-08 11:38:23 -07006183 QCBORDecode_EnterMapFromMapSZ(&DCtx, "map in a map");
6184 QCBORDecode_GetNext(&DCtx, &Item1);
6185 if(Item1.uDataType != QCBOR_TYPE_BYTE_STRING) {
6186 return 2003;
6187 }
6188
Laurence Lundblade9b334962020-08-27 10:55:53 -07006189 QCBORDecode_Init(&DCtx, ValidEncodedMap, 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006190 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade937ea812020-05-08 11:38:23 -07006191 QCBORDecode_GetNext(&DCtx, &Item1);
6192 QCBORDecode_GetNext(&DCtx, &Item1);
6193 QCBORDecode_GetNext(&DCtx, &Item1);
6194 QCBORDecode_GetNext(&DCtx, &Item1);
6195 QCBORDecode_GetNext(&DCtx, &Item1);
6196 QCBORDecode_GetNext(&DCtx, &Item1);
6197 QCBORDecode_GetNext(&DCtx, &Item1);
6198 QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings");
6199 QCBORDecode_GetNext(&DCtx, &Item1);
6200 if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING) {
Laurence Lundblade64b607e2020-05-13 13:05:57 -07006201 return 2004;
Laurence Lundblade937ea812020-05-08 11:38:23 -07006202 }
6203
Laurence Lundblade9b334962020-08-27 10:55:53 -07006204 QCBORDecode_Init(&DCtx, ValidEncodedMap, 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006205 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade2b843b52020-06-16 20:51:03 -07006206 QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings");
6207 QCBORDecode_ExitArray(&DCtx);
6208 QCBORDecode_GetNext(&DCtx, &Item1);
6209 if(Item1.uDataType != QCBOR_TYPE_MAP && Item1.uLabelAlloc != QCBOR_TYPE_TEXT_STRING) {
6210 return 2006;
6211 }
6212 QCBORDecode_ExitMap(&DCtx);
6213 if(QCBORDecode_GetNext(&DCtx, &Item1) != QCBOR_ERR_NO_MORE_ITEMS) {
6214 return 2007;
6215 }
6216
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07006217 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSimpleArray), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006218 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07006219 int64_t nDecodedInt2;
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07006220 QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2);
6221 uErr = QCBORDecode_GetAndResetError(&DCtx);
6222 if(uErr != QCBOR_ERR_MAP_NOT_ENTERED){
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07006223 return 2008;
6224 }
6225 UsefulBufC String;
Laurence Lundblade323f8a92020-09-06 19:43:09 -07006226 QCBORDecode_GetTextStringInMapN(&DCtx, 88, &String);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07006227 if(uErr != QCBOR_ERR_MAP_NOT_ENTERED){
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07006228 return 2009;
6229 }
Laurence Lundblade937ea812020-05-08 11:38:23 -07006230
Laurence Lundbladee6f15112020-07-23 18:44:16 -07006231
6232 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEmptyMap), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006233 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundbladee6f15112020-07-23 18:44:16 -07006234 // This will fail because the map is empty.
6235 QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2);
6236 uErr = QCBORDecode_GetAndResetError(&DCtx);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006237 if(uErr != QCBOR_ERR_LABEL_NOT_FOUND){
Laurence Lundbladee6f15112020-07-23 18:44:16 -07006238 return 2010;
6239 }
6240 QCBORDecode_ExitMap(&DCtx);
6241 uErr = QCBORDecode_Finish(&DCtx);
6242 if(uErr != QCBOR_SUCCESS){
6243 return 2011;
6244 }
6245
6246
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08006247#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundbladee6f15112020-07-23 18:44:16 -07006248 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEmptyInDefinteLengthMap), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006249 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundbladee6f15112020-07-23 18:44:16 -07006250 // This will fail because the map is empty.
6251 QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2);
6252 uErr = QCBORDecode_GetAndResetError(&DCtx);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006253 if(uErr != QCBOR_ERR_LABEL_NOT_FOUND){
Laurence Lundblade085d7952020-07-24 10:26:30 -07006254 return 2012;
Laurence Lundbladee6f15112020-07-23 18:44:16 -07006255 }
6256 QCBORDecode_ExitMap(&DCtx);
6257 uErr = QCBORDecode_Finish(&DCtx);
6258 if(uErr != QCBOR_SUCCESS){
Laurence Lundblade085d7952020-07-24 10:26:30 -07006259 return 2013;
Laurence Lundbladee6f15112020-07-23 18:44:16 -07006260 }
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08006261#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
Laurence Lundbladee6f15112020-07-23 18:44:16 -07006262
Laurence Lundblade5f4e8712020-07-25 11:44:43 -07006263
6264 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spArrayOfEmpty), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006265 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade323f8a92020-09-06 19:43:09 -07006266 QCBORDecode_GetByteString(&DCtx, &String);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006267 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade5f4e8712020-07-25 11:44:43 -07006268 QCBORDecode_ExitMap(&DCtx);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006269 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade5f4e8712020-07-25 11:44:43 -07006270 QCBORDecode_ExitArray(&DCtx);
6271 QCBORDecode_GetInt64(&DCtx, &nDecodedInt2);
6272 QCBORDecode_ExitArray(&DCtx);
6273 uErr = QCBORDecode_Finish(&DCtx);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07006274 if(uErr != QCBOR_SUCCESS) {
Laurence Lundblade5f4e8712020-07-25 11:44:43 -07006275 return 2014;
6276 }
6277
Laurence Lundblade93d3f532020-09-28 21:09:12 -07006278 int64_t nInt;
6279 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spRecoverableMapErrors), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006280 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade37286c02022-09-03 10:05:02 -07006281#ifndef QCBOR_DISABLE_TAGS
Laurence Lundblade93d3f532020-09-28 21:09:12 -07006282 QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt);
Laurence Lundblade88e9db22020-11-02 03:56:33 -08006283 uErr = QCBORDecode_GetError(&DCtx);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07006284 if(uErr != QCBOR_ERR_TOO_MANY_TAGS) {
6285 return 2021;
6286 }
Laurence Lundblade88e9db22020-11-02 03:56:33 -08006287 if(QCBORDecode_GetNthTagOfLast(&DCtx, 0) != CBOR_TAG_INVALID64) {
6288 return 2121;
6289 }
6290 (void)QCBORDecode_GetAndResetError(&DCtx);
Laurence Lundblade37286c02022-09-03 10:05:02 -07006291#endif
Laurence Lundblade88e9db22020-11-02 03:56:33 -08006292
Laurence Lundblade93d3f532020-09-28 21:09:12 -07006293
Laurence Lundblade93d3f532020-09-28 21:09:12 -07006294 QCBORDecode_GetInt64InMapN(&DCtx, 0x03, &nInt);
6295 uErr = QCBORDecode_GetAndResetError(&DCtx);
6296 if(uErr != QCBOR_ERR_INT_OVERFLOW) {
6297 return 2023;
6298 }
6299
Laurence Lundblade37286c02022-09-03 10:05:02 -07006300#ifndef QCBOR_DISABLE_TAGS
Laurence Lundblade93d3f532020-09-28 21:09:12 -07006301 QCBORDecode_GetEpochDateInMapN(&DCtx, 0x04, QCBOR_TAG_REQUIREMENT_TAG, &nInt);
6302 uErr = QCBORDecode_GetAndResetError(&DCtx);
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006303 if(uErr != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_DATE_OVERFLOW)) {
Laurence Lundblade93d3f532020-09-28 21:09:12 -07006304 return 2024;
6305 }
Laurence Lundblade37286c02022-09-03 10:05:02 -07006306#endif
Laurence Lundblade93d3f532020-09-28 21:09:12 -07006307
6308 QCBORDecode_GetInt64InMapN(&DCtx, 0x05, &nInt);
6309 uErr = QCBORDecode_GetAndResetError(&DCtx);
6310 if(uErr != QCBOR_ERR_DUPLICATE_LABEL) {
6311 return 2025;
6312 }
6313
6314 QCBORDecode_GetInt64InMapN(&DCtx, 0x08, &nInt);
6315
6316 QCBORDecode_ExitMap(&DCtx);
6317 uErr = QCBORDecode_Finish(&DCtx);
6318 if(uErr != QCBOR_SUCCESS) {
6319 return 2026;
6320 }
6321
6322 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUnRecoverableMapError1), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006323 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07006324 QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt);
6325 uErr = QCBORDecode_GetAndResetError(&DCtx);
6326 if(uErr != QCBOR_ERR_BAD_BREAK) {
6327 return 2030;
6328 }
6329
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08006330#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundblade93d3f532020-09-28 21:09:12 -07006331 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUnRecoverableMapError2), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006332 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07006333 QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt);
6334 uErr = QCBORDecode_GetAndResetError(&DCtx);
6335 if(uErr != QCBOR_ERR_NO_MORE_ITEMS) {
6336 return 2031;
6337 }
6338
6339 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUnRecoverableMapError3), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006340 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07006341 QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt);
6342 uErr = QCBORDecode_GetAndResetError(&DCtx);
6343 if(uErr != QCBOR_ERR_HIT_END) {
6344 return 2032;
6345 }
6346
6347 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUnRecoverableMapError4), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006348 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07006349 QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt);
6350 uErr = QCBORDecode_GetAndResetError(&DCtx);
6351 if(uErr != QCBOR_ERR_ARRAY_DECODE_NESTING_TOO_DEEP) {
6352 return 2033;
6353 }
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08006354#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
6355
Laurence Lundblade732e52d2021-02-22 20:11:01 -07006356 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0);
6357 QCBORDecode_VGetNextConsume(&DCtx, &Item1);
6358 if(Item1.uDataType != QCBOR_TYPE_MAP) {
6359 return 2401;
6360 }
6361 if(QCBORDecode_GetError(&DCtx)) {
6362 return 2402;
6363 }
6364
6365 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0);
6366 QCBORDecode_VGetNext(&DCtx, &Item1);
6367 if(Item1.uDataType != QCBOR_TYPE_MAP ||
6368 Item1.val.uCount != 3 ||
6369 Item1.uNextNestLevel != 1) {
6370 return 2403;
6371 }
6372 if(QCBORDecode_GetError(&DCtx)) {
6373 return 2404;
6374 }
6375 QCBORDecode_VGetNextConsume(&DCtx, &Item1);
6376 if(Item1.uDataType != QCBOR_TYPE_INT64 ||
6377 Item1.uNextNestLevel != 1 ||
6378 Item1.val.int64 != 42) {
6379 return 2405;
6380 }
6381 if(QCBORDecode_GetError(&DCtx)) {
6382 return 2406;
6383 }
6384 QCBORDecode_VGetNextConsume(&DCtx, &Item1);
6385 if(Item1.uDataType != QCBOR_TYPE_ARRAY ||
6386 Item1.uNestingLevel != 1 ||
6387 Item1.uNextNestLevel != 1 ||
6388 Item1.val.uCount != 2) {
6389 return 2407;
6390 }
6391 if(QCBORDecode_GetError(&DCtx)) {
6392 return 2408;
6393 }
6394 QCBORDecode_VGetNextConsume(&DCtx, &Item1);
6395 if(Item1.uDataType != QCBOR_TYPE_MAP ||
6396 Item1.uNestingLevel != 1 ||
6397 Item1.uNextNestLevel != 0 ||
6398 Item1.val.uCount != 4) {
6399 return 2409;
6400 }
6401 if(QCBORDecode_GetError(&DCtx)) {
6402 return 2410;
6403 }
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006404
Laurence Lundbladeddebabe2020-11-22 11:32:17 -08006405 nReturn = DecodeNestedIterate();
6406
Laurence Lundblade63926052021-03-29 16:05:51 -07006407
6408 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(not_well_formed_submod_section), 0);
6409 QCBORDecode_EnterMap(&DCtx, NULL);
6410 QCBORDecode_EnterMapFromMapN(&DCtx, 20);
6411 if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_BAD_INT) {
6412 return 2500;
6413 }
6414
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07006415 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBadConsumeInput), 0);
6416 QCBORDecode_VGetNextConsume(&DCtx, &Item1);
6417 if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_NO_MORE_ITEMS) {
6418 return 2600;
6419 }
6420
Laurence Lundblade37286c02022-09-03 10:05:02 -07006421#ifndef QCBOR_DISABLE_TAGS
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07006422 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBadConsumeInput2), 0);
6423 QCBORDecode_VGetNextConsume(&DCtx, &Item1);
6424 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) {
6425 return 2700;
6426 }
6427
6428 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBadConsumeInput3), 0);
6429 QCBORDecode_VGetNextConsume(&DCtx, &Item1);
6430 if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT) {
6431 return 2800;
6432 }
Laurence Lundblade37286c02022-09-03 10:05:02 -07006433#endif
6434
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07006435
6436 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBadConsumeInput4), 0);
6437 QCBORDecode_VGetNextConsume(&DCtx, &Item1);
6438#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
6439 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) {
6440 return 2900;
6441 }
6442#else
6443 if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_INDEF_LEN_ARRAYS_DISABLED) {
6444 return 2901;
6445 }
6446#endif
6447
6448 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBadConsumeInput5), 0);
6449 QCBORDecode_VGetNextConsume(&DCtx, &Item1);
6450 if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_MAP_LABEL_TYPE) {
6451 return 3000;
6452 }
6453
Laurence Lundbladec5f45e42023-12-18 09:23:20 -07006454 nReturn = EnterMapCursorTest();
6455
Laurence Lundbladeddebabe2020-11-22 11:32:17 -08006456 return nReturn;
Laurence Lundblade9c905e82020-04-25 11:31:38 -07006457}
6458
6459
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006460struct NumberConversion {
6461 char *szDescription;
6462 UsefulBufC CBOR;
6463 int64_t nConvertedToInt64;
6464 QCBORError uErrorInt64;
6465 uint64_t uConvertToUInt64;
6466 QCBORError uErrorUint64;
6467 double dConvertToDouble;
6468 QCBORError uErrorDouble;
6469};
6470
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006471#ifndef QCBOR_DISABLE_EXP_AND_MANTISSA
6472#define EXP_AND_MANTISSA_ERROR(x) x
6473#else
6474#define EXP_AND_MANTISSA_ERROR(x) QCBOR_ERR_UNEXPECTED_TYPE
6475#endif
6476
6477
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07006478static const struct NumberConversion NumberConversions[] = {
Laurence Lundblade37286c02022-09-03 10:05:02 -07006479#ifndef QCBOR_DISABLE_TAGS
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006480 {
Laurence Lundblade4e808ba2022-12-29 12:45:20 -07006481 "Big float: INT64_MIN * 2e-1 to test handling of INT64_MIN",
6482 {(uint8_t[]){0xC5, 0x82, 0x20,
6483 0x3B, 0x7f, 0xff, 0xff, 0xff, 0xff, 0x0ff, 0xff, 0xff,
6484 }, 15},
6485 -4611686018427387904, /* INT64_MIN / 2 */
6486 EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS),
6487 0,
6488 EXP_AND_MANTISSA_ERROR(QCBOR_ERR_NUMBER_SIGN_CONVERSION),
6489 -4.6116860184273879E+18,
6490 FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS))
6491 },
6492 {
Laurence Lundblade784b54b2020-08-10 01:24:52 -07006493 "too large to fit into int64_t",
6494 {(uint8_t[]){0xc3, 0x48, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 10},
6495 0,
6496 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
6497 0,
6498 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
6499 ((double)INT64_MIN) + 1 ,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006500 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)
Laurence Lundblade784b54b2020-08-10 01:24:52 -07006501 },
6502 {
6503 "largest negative int that fits in int64_t",
6504 {(uint8_t[]){0xc3, 0x48, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, 10},
6505 INT64_MIN,
6506 QCBOR_SUCCESS,
6507 0,
6508 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
6509 (double)INT64_MIN,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006510 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)
Laurence Lundblade784b54b2020-08-10 01:24:52 -07006511 },
6512 {
Laurence Lundbladeda095972020-06-06 18:35:33 -07006513 "negative bignum -1",
6514 {(uint8_t[]){0xc3, 0x41, 0x00}, 3},
6515 -1,
6516 QCBOR_SUCCESS,
6517 0,
6518 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
6519 -1.0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006520 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)
Laurence Lundbladeda095972020-06-06 18:35:33 -07006521 },
6522 {
Laurence Lundblade9ab5abb2020-05-20 12:10:45 -07006523 "Decimal Fraction with positive bignum 257 * 10e3",
Laurence Lundblade887add82020-05-17 05:50:34 -07006524 {(uint8_t[]){0xC4, 0x82, 0x1B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03,
6525 0xC2, 0x42, 0x01, 0x01}, 15},
6526 257000,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006527 EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS),
Laurence Lundblade887add82020-05-17 05:50:34 -07006528 257000,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006529 EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS),
Laurence Lundblade887add82020-05-17 05:50:34 -07006530 257000.0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006531 FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS))
Laurence Lundblade887add82020-05-17 05:50:34 -07006532 },
6533 {
Laurence Lundbladeda095972020-06-06 18:35:33 -07006534 "bigfloat with negative bignum -258 * 2e3",
Laurence Lundblade887add82020-05-17 05:50:34 -07006535 {(uint8_t[]){0xC5, 0x82, 0x1B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03,
6536 0xC3, 0x42, 0x01, 0x01}, 15},
Laurence Lundbladeda095972020-06-06 18:35:33 -07006537 -2064,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006538 EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS),
Laurence Lundblade887add82020-05-17 05:50:34 -07006539 0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006540 EXP_AND_MANTISSA_ERROR(QCBOR_ERR_NUMBER_SIGN_CONVERSION),
Laurence Lundbladeda095972020-06-06 18:35:33 -07006541 -2064.0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006542 FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS))
Laurence Lundblade887add82020-05-17 05:50:34 -07006543 },
6544 {
Laurence Lundblade9ab5abb2020-05-20 12:10:45 -07006545 "bigfloat with positive bignum 257 * 2e3",
Laurence Lundblade887add82020-05-17 05:50:34 -07006546 {(uint8_t[]){0xC5, 0x82, 0x1B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03,
6547 0xC2, 0x42, 0x01, 0x01}, 15},
6548 2056,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006549 EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS),
Laurence Lundblade887add82020-05-17 05:50:34 -07006550 2056,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006551 EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS),
Laurence Lundblade887add82020-05-17 05:50:34 -07006552 2056.0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006553 FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS))
Laurence Lundblade887add82020-05-17 05:50:34 -07006554 },
6555 {
Laurence Lundbladeda095972020-06-06 18:35:33 -07006556 "negative bignum 0xc349010000000000000000 -18446744073709551617",
Laurence Lundblade887add82020-05-17 05:50:34 -07006557 {(uint8_t[]){0xc3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 11},
6558 0,
6559 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
6560 0,
6561 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
6562 -18446744073709551617.0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006563 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)
Laurence Lundblade887add82020-05-17 05:50:34 -07006564 },
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08006565#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
Laurence Lundblade887add82020-05-17 05:50:34 -07006566 {
Laurence Lundblade313b2862020-05-16 01:23:06 -07006567 "Positive bignum 0x01020304 indefinite length string",
6568 {(uint8_t[]){0xC2, 0x5f, 0x42, 0x01, 0x02, 0x41, 0x03, 0x41, 0x04, 0xff}, 10},
6569 0x01020304,
6570 QCBOR_SUCCESS,
6571 0x01020304,
6572 QCBOR_SUCCESS,
6573 16909060.0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006574 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)
Laurence Lundblade313b2862020-05-16 01:23:06 -07006575 },
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08006576#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
Laurence Lundblade313b2862020-05-16 01:23:06 -07006577 {
Laurence Lundblade887add82020-05-17 05:50:34 -07006578 "Decimal Fraction with neg bignum [9223372036854775807, -4759477275222530853137]",
Laurence Lundblade313b2862020-05-16 01:23:06 -07006579 {(uint8_t[]){0xC4, 0x82, 0x1B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
6580 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,}, 23},
6581 0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006582 EXP_AND_MANTISSA_ERROR(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW),
Laurence Lundblade313b2862020-05-16 01:23:06 -07006583 0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006584 EXP_AND_MANTISSA_ERROR(QCBOR_ERR_NUMBER_SIGN_CONVERSION),
Laurence Lundblade313b2862020-05-16 01:23:06 -07006585 -INFINITY,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006586 FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS))
Laurence Lundblade313b2862020-05-16 01:23:06 -07006587 },
6588 {
6589 "big float [9223372036854775806, 9223372036854775806]",
6590 {(uint8_t[]){0xC5, 0x82, 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE,
6591 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE}, 20},
6592 0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006593 EXP_AND_MANTISSA_ERROR(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW),
Laurence Lundblade313b2862020-05-16 01:23:06 -07006594 0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006595 EXP_AND_MANTISSA_ERROR(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW),
Laurence Lundblade313b2862020-05-16 01:23:06 -07006596 INFINITY,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006597 FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS))
Laurence Lundblade313b2862020-05-16 01:23:06 -07006598 },
6599 {
Laurence Lundblade983500d2020-05-14 11:49:34 -07006600 "Big float 3 * 2^^2",
6601 {(uint8_t[]){0xC5, 0x82, 0x02, 0x03}, 4},
6602 12,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006603 EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS),
Laurence Lundblade983500d2020-05-14 11:49:34 -07006604 12,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006605 EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS),
Laurence Lundblade983500d2020-05-14 11:49:34 -07006606 12.0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006607 FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS))
Laurence Lundblade983500d2020-05-14 11:49:34 -07006608 },
Laurence Lundblade983500d2020-05-14 11:49:34 -07006609 {
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006610 "Decimal fraction 3/10",
6611 {(uint8_t[]){0xC4, 0x82, 0x20, 0x03}, 4},
6612 0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006613 EXP_AND_MANTISSA_ERROR(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW),
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006614 0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006615 EXP_AND_MANTISSA_ERROR(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW),
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006616 0.30000000000000004,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006617 FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS))
Laurence Lundbladedfd49fc2020-09-01 14:17:16 -07006618 },
6619 {
Laurence Lundblade11fd78b2020-09-01 22:13:27 -07006620 "extreme pos bignum",
6621 {(uint8_t[]){0xc2, 0x59, 0x01, 0x90,
6622 // 50 rows of 8 is 400 digits.
6623 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6624 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6625 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6626 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6627 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6628 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6629 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6630 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6631 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6632 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6633 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6634 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6635 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6636 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6637 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6638 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6639 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6640 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6641 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6642 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6643 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6644 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6645 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6646 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6647 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6648 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6649 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6650 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6651 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6652 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6653 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6654 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6655 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6656 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6657 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6658 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6659 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6660 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6661 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6662 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6663 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6664 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6665 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6666 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6667 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6668 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6669 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6670 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6671 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
gtravisb787e82a2023-11-30 18:38:21 -08006672 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0},
Laurence Lundblade11fd78b2020-09-01 22:13:27 -07006673 404},
6674 0,
6675 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
6676 0,
6677 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
Laurence Lundblade11fd78b2020-09-01 22:13:27 -07006678 INFINITY,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006679 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS),
Laurence Lundblade11fd78b2020-09-01 22:13:27 -07006680 },
6681
6682 {
6683 "extreme neg bignum",
6684 {(uint8_t[]){0xc3, 0x59, 0x01, 0x90,
6685 // 50 rows of 8 is 400 digits.
6686 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6687 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6688 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6689 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6690 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6691 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6692 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6693 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6694 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6695 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6696 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6697 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6698 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6699 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6700 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6701 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6702 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6703 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6704 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6705 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6706 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6707 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6708 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6709 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6710 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6711 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6712 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6713 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6714 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6715 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6716 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6717 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6718 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6719 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6720 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6721 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6722 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6723 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6724 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6725 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6726 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6727 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6728 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6729 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6730 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6731 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6732 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6733 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6734 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
gtravisb787e82a2023-11-30 18:38:21 -08006735 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0},
Laurence Lundblade11fd78b2020-09-01 22:13:27 -07006736 404},
6737 0,
6738 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
6739 0,
6740 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
Laurence Lundblade11fd78b2020-09-01 22:13:27 -07006741 -INFINITY,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006742 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)
Laurence Lundblade11fd78b2020-09-01 22:13:27 -07006743 },
Laurence Lundblade51722fd2020-09-02 13:01:33 -07006744
6745 {
6746 "big float underflow [9223372036854775806, -9223372036854775806]",
6747 {(uint8_t[]){
6748 0xC5, 0x82,
6749 0x3B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE,
6750 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE}, 20},
Laurence Lundblade51722fd2020-09-02 13:01:33 -07006751 0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006752 EXP_AND_MANTISSA_ERROR(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW),
Laurence Lundblade51722fd2020-09-02 13:01:33 -07006753 0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006754 EXP_AND_MANTISSA_ERROR(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW),
Laurence Lundblade51722fd2020-09-02 13:01:33 -07006755 0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006756 FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS))
Laurence Lundblade51722fd2020-09-02 13:01:33 -07006757 },
6758
6759 {
6760 "bigfloat that evaluates to -INFINITY",
6761 {(uint8_t[]){
6762 0xC5, 0x82,
6763 0x1B, 0x70, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03,
6764 0xC3, 0x42, 0x01, 0x01}, 15},
Laurence Lundblade51722fd2020-09-02 13:01:33 -07006765 0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006766 EXP_AND_MANTISSA_ERROR(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW),
Laurence Lundblade51722fd2020-09-02 13:01:33 -07006767 0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006768 EXP_AND_MANTISSA_ERROR(QCBOR_ERR_NUMBER_SIGN_CONVERSION),
Laurence Lundblade51722fd2020-09-02 13:01:33 -07006769 -INFINITY,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006770 FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS))
Laurence Lundblade51722fd2020-09-02 13:01:33 -07006771 },
Laurence Lundblade37286c02022-09-03 10:05:02 -07006772 {
6773 "Positive bignum 0xffff",
6774 {(uint8_t[]){0xC2, 0x42, 0xff, 0xff}, 4},
6775 65536-1,
6776 QCBOR_SUCCESS,
6777 0xffff,
6778 QCBOR_SUCCESS,
6779 65535.0,
6780 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)
6781 },
6782#endif /* QCBOR_DISABLE_TAGS */
6783 {
6784 "Positive integer 18446744073709551615",
6785 {(uint8_t[]){0x1b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, 9},
6786 0,
6787 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
6788 18446744073709551615ULL,
6789 QCBOR_SUCCESS,
6790 18446744073709551615.0,
6791 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)
6792 },
6793
6794 {
6795 "Postive integer 0",
6796 {(uint8_t[]){0x0}, 1},
6797 0LL,
6798 QCBOR_SUCCESS,
6799 0ULL,
6800 QCBOR_SUCCESS,
6801 0.0,
6802 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)
6803 },
6804 {
6805 "Negative integer -18446744073709551616",
6806 {(uint8_t[]){0x3b, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }, 9},
6807 -9223372036854775807-1, // INT64_MIN
6808 QCBOR_SUCCESS,
6809 0ULL,
6810 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
6811 -9223372036854775808.0,
6812 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)
6813 },
6814 {
6815 "Double Floating point value 100.3",
6816 {(uint8_t[]){0xfb, 0x40, 0x59, 0x13, 0x33, 0x33, 0x33, 0x33, 0x33}, 9},
6817 100L,
6818 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS),
6819 100ULL,
6820 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS),
6821 100.3,
6822 FLOAT_ERR_CODE_NO_FLOAT(QCBOR_SUCCESS),
6823 },
6824 {
6825 "Floating point value NaN 0xfa7fc00000",
6826 {(uint8_t[]){0xfa, 0x7f, 0xc0, 0x00, 0x00}, 5},
6827 0,
6828 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_FLOAT_EXCEPTION),
6829 0,
6830 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_FLOAT_EXCEPTION),
6831 NAN,
6832 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS),
6833 },
6834 {
6835 "half-precision Floating point value -4",
6836 {(uint8_t[]){0xf9, 0xc4, 0x00}, 3},
6837 // Normal case with all enabled.
6838 -4,
6839 FLOAT_ERR_CODE_NO_HALF_PREC_NO_FLOAT_HW(QCBOR_SUCCESS),
6840 0,
6841 FLOAT_ERR_CODE_NO_HALF_PREC_NO_FLOAT_HW(QCBOR_ERR_NUMBER_SIGN_CONVERSION),
6842 -4.0,
6843 FLOAT_ERR_CODE_NO_HALF_PREC(QCBOR_SUCCESS)
6844 },
6845 {
6846 "+inifinity single precision",
6847 {(uint8_t[]){0xfa, 0x7f, 0x80, 0x00, 0x00}, 5},
6848 0,
6849 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_FLOAT_EXCEPTION),
6850 0,
6851 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW),
6852 INFINITY,
6853 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)
6854 },
6855
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006856};
Laurence Lundblade9c905e82020-04-25 11:31:38 -07006857
6858
6859
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08006860
6861static int32_t SetUpDecoder(QCBORDecodeContext *DCtx, UsefulBufC CBOR, UsefulBuf Pool)
6862{
6863 QCBORDecode_Init(DCtx, CBOR, QCBOR_DECODE_MODE_NORMAL);
6864#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
6865 if(QCBORDecode_SetMemPool(DCtx, Pool, 0)) {
6866 return 1;
6867 }
6868#else /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
6869 (void)Pool;
6870#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
6871 return 0;
6872}
6873
6874
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03006875int32_t IntegerConvertTest(void)
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006876{
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08006877 const int nNumTests = C_ARRAY_COUNT(NumberConversions,
6878 struct NumberConversion);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006879
Laurence Lundblade9ab5abb2020-05-20 12:10:45 -07006880 for(int nIndex = 0; nIndex < nNumTests; nIndex++) {
6881 const struct NumberConversion *pF = &NumberConversions[nIndex];
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07006882
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006883 // Set up the decoding context including a memory pool so that
6884 // indefinite length items can be checked
6885 QCBORDecodeContext DCtx;
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006886 UsefulBuf_MAKE_STACK_UB(Pool, 100);
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07006887
6888 /* ----- test conversion to int64_t ------ */
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08006889 if(SetUpDecoder(&DCtx, pF->CBOR, Pool)) {
6890 return (int32_t)(3333+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006891 }
6892
6893 int64_t nInt;
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006894 QCBORDecode_GetInt64ConvertAll(&DCtx, 0xffff, &nInt);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07006895 if(QCBORDecode_GetError(&DCtx) != pF->uErrorInt64) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07006896 return (int32_t)(2000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006897 }
6898 if(pF->uErrorInt64 == QCBOR_SUCCESS && pF->nConvertedToInt64 != nInt) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07006899 return (int32_t)(3000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006900 }
6901
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07006902 /* ----- test conversion to uint64_t ------ */
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08006903 if(SetUpDecoder(&DCtx, pF->CBOR, Pool)) {
6904 return (int32_t)(3333+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006905 }
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08006906
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006907 uint64_t uInt;
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006908 QCBORDecode_GetUInt64ConvertAll(&DCtx, 0xffff, &uInt);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07006909 if(QCBORDecode_GetError(&DCtx) != pF->uErrorUint64) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07006910 return (int32_t)(4000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006911 }
6912 if(pF->uErrorUint64 == QCBOR_SUCCESS && pF->uConvertToUInt64 != uInt) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07006913 return (int32_t)(5000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006914 }
6915
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07006916 /* ----- test conversion to double ------ */
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08006917 if(SetUpDecoder(&DCtx, pF->CBOR, Pool)) {
6918 return (int32_t)(3333+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006919 }
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006920
6921#ifndef USEFULBUF_DISABLE_ALL_FLOAT
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006922 double d;
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006923 QCBORDecode_GetDoubleConvertAll(&DCtx, 0xffff, &d);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07006924 if(QCBORDecode_GetError(&DCtx) != pF->uErrorDouble) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07006925 return (int32_t)(6000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006926 }
6927 if(pF->uErrorDouble == QCBOR_SUCCESS) {
6928 if(isnan(pF->dConvertToDouble)) {
Laurence Lundblade983500d2020-05-14 11:49:34 -07006929 // NaN's can't be compared for equality. A NaN is
6930 // never equal to anything including another NaN
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006931 if(!isnan(d)) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07006932 return (int32_t)(7000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006933 }
6934 } else {
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006935 if(pF->dConvertToDouble != d) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07006936 return (int32_t)(8000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006937 }
6938 }
6939 }
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006940#endif /* USEFULBUF_DISABLE_ALL_FLOAT */
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006941 }
6942
6943 return 0;
6944}
6945
Laurence Lundbladea8758502022-05-15 17:57:46 -07006946#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
6947
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03006948int32_t CBORTestIssue134(void)
David Navarro9123e5b2022-03-28 16:04:03 +02006949{
6950 QCBORDecodeContext DCtx;
6951 QCBORItem Item;
6952 QCBORError uCBORError;
6953 const uint8_t spTestIssue134[] = { 0x5F, 0x40, 0xFF };
Laurence Lundblade9c905e82020-04-25 11:31:38 -07006954
David Navarro9123e5b2022-03-28 16:04:03 +02006955 QCBORDecode_Init(&DCtx,
6956 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTestIssue134),
6957 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade97c61bf2020-05-02 11:24:06 -07006958
David Navarro9123e5b2022-03-28 16:04:03 +02006959 UsefulBuf_MAKE_STACK_UB(StringBuf, 200);
6960 QCBORDecode_SetMemPool(&DCtx, StringBuf, false);
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03006961
David Navarro9123e5b2022-03-28 16:04:03 +02006962 do {
6963 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
6964 } while (QCBOR_SUCCESS == uCBORError);
6965
6966 uCBORError = QCBORDecode_Finish(&DCtx);
6967
Laurence Lundblade11ea3612022-07-01 13:26:23 -07006968 return (int32_t)uCBORError;
David Navarro9123e5b2022-03-28 16:04:03 +02006969}
Laurence Lundblade97c61bf2020-05-02 11:24:06 -07006970
Laurence Lundbladea8758502022-05-15 17:57:46 -07006971#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
6972
Laurence Lundblade37286c02022-09-03 10:05:02 -07006973
6974
6975static const uint8_t spSequenceTestInput[] = {
6976 /* 1. The valid date string "1985-04-12" */
6977 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string
6978
6979 /* 2. */
6980 0x00,
6981
6982 /* 3. A valid epoch date, 1400000000; Tue, 13 May 2014 16:53:20 GMT */
6983 0x1a, 0x53, 0x72, 0x4E, 0x00,
6984
6985 /* 4. */
6986 0x62, 'h', 'i',
6987};
6988
6989
Laurence Lundbladee3553422020-05-02 11:11:17 -07006990int32_t CBORSequenceDecodeTests(void)
6991{
6992 QCBORDecodeContext DCtx;
Laurence Lundblade87495732021-02-26 10:05:55 -07006993 QCBORItem Item;
6994 QCBORError uCBORError;
6995 size_t uConsumed;
Laurence Lundbladee3553422020-05-02 11:11:17 -07006996
6997 // --- Test a sequence with extra bytes ---
Laurence Lundblade9b334962020-08-27 10:55:53 -07006998
Laurence Lundbladee3553422020-05-02 11:11:17 -07006999 QCBORDecode_Init(&DCtx,
Laurence Lundblade37286c02022-09-03 10:05:02 -07007000 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSequenceTestInput),
Laurence Lundbladee3553422020-05-02 11:11:17 -07007001 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade9b334962020-08-27 10:55:53 -07007002
Laurence Lundblade37286c02022-09-03 10:05:02 -07007003 // Get 1.
Laurence Lundbladee3553422020-05-02 11:11:17 -07007004 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
7005 if(uCBORError != QCBOR_SUCCESS) {
7006 return 1;
7007 }
Laurence Lundblade37286c02022-09-03 10:05:02 -07007008 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ) {
Laurence Lundbladee3553422020-05-02 11:11:17 -07007009 return 2;
7010 }
Laurence Lundbladec7114722020-08-13 05:11:40 -07007011
Laurence Lundblade87495732021-02-26 10:05:55 -07007012 uCBORError = QCBORDecode_PartialFinish(&DCtx, &uConsumed);
7013 if(uCBORError != QCBOR_ERR_EXTRA_BYTES ||
Laurence Lundblade37286c02022-09-03 10:05:02 -07007014 uConsumed != 11) {
7015 return 102;
7016 }
7017
7018 // Get 2.
7019 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
7020 if(uCBORError != QCBOR_SUCCESS) {
7021 return 66;
7022 }
7023
7024 uCBORError = QCBORDecode_PartialFinish(&DCtx, &uConsumed);
7025 if(uCBORError != QCBOR_ERR_EXTRA_BYTES ||
Laurence Lundblade87495732021-02-26 10:05:55 -07007026 uConsumed != 12) {
7027 return 102;
7028 }
7029
Laurence Lundblade37286c02022-09-03 10:05:02 -07007030 // Get 3.
Laurence Lundbladec7114722020-08-13 05:11:40 -07007031 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundbladee3553422020-05-02 11:11:17 -07007032 if(uCBORError != QCBOR_SUCCESS) {
7033 return 2;
7034 }
Laurence Lundblade37286c02022-09-03 10:05:02 -07007035 if(Item.uDataType != QCBOR_TYPE_INT64) {
Laurence Lundbladee3553422020-05-02 11:11:17 -07007036 return 3;
7037 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07007038
Laurence Lundbladee3553422020-05-02 11:11:17 -07007039 // A sequence can have stuff at the end that may
7040 // or may not be valid CBOR. The protocol decoder knows
7041 // when to stop by definition of the protocol, not
7042 // when the top-level map or array is ended.
7043 // Finish still has to be called to know that
7044 // maps and arrays (if there were any) were closed
7045 // off correctly. When called like this it
7046 // must return the error QCBOR_ERR_EXTRA_BYTES.
7047 uCBORError = QCBORDecode_Finish(&DCtx);
7048 if(uCBORError != QCBOR_ERR_EXTRA_BYTES) {
7049 return 4;
7050 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07007051
Laurence Lundbladee3553422020-05-02 11:11:17 -07007052 // --- Test an empty input ----
7053 uint8_t empty[1];
7054 UsefulBufC Empty = {empty, 0};
7055 QCBORDecode_Init(&DCtx,
7056 Empty,
7057 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade9b334962020-08-27 10:55:53 -07007058
Laurence Lundbladee3553422020-05-02 11:11:17 -07007059 uCBORError = QCBORDecode_Finish(&DCtx);
7060 if(uCBORError != QCBOR_SUCCESS) {
7061 return 5;
7062 }
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07007063
7064
Laurence Lundbladee3553422020-05-02 11:11:17 -07007065 // --- Sequence with unclosed indefinite length array ---
7066 static const uint8_t xx[] = {0x01, 0x9f, 0x02};
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07007067
Laurence Lundbladee3553422020-05-02 11:11:17 -07007068 QCBORDecode_Init(&DCtx,
7069 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(xx),
7070 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07007071
Laurence Lundbladee3553422020-05-02 11:11:17 -07007072 // Get the first item
7073 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
7074 if(uCBORError != QCBOR_SUCCESS) {
7075 return 7;
7076 }
7077 if(Item.uDataType != QCBOR_TYPE_INT64) {
7078 return 8;
7079 }
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07007080
Laurence Lundbladee3553422020-05-02 11:11:17 -07007081 // Get a second item
7082 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08007083#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundbladee3553422020-05-02 11:11:17 -07007084 if(uCBORError != QCBOR_SUCCESS) {
7085 return 9;
7086 }
7087 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
7088 return 10;
7089 }
7090
7091 // Try to finish before consuming all bytes to confirm
7092 // that the still-open error is returned.
7093 uCBORError = QCBORDecode_Finish(&DCtx);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07007094 if(uCBORError != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
Laurence Lundbladee3553422020-05-02 11:11:17 -07007095 return 11;
7096 }
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08007097#else /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
7098 if(uCBORError != QCBOR_ERR_INDEF_LEN_ARRAYS_DISABLED) {
7099 return 20;
7100 }
7101#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
Laurence Lundbladee3553422020-05-02 11:11:17 -07007102
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07007103
Laurence Lundbladee3553422020-05-02 11:11:17 -07007104 // --- Sequence with a closed indefinite length array ---
7105 static const uint8_t yy[] = {0x01, 0x9f, 0xff};
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07007106
Laurence Lundbladee3553422020-05-02 11:11:17 -07007107 QCBORDecode_Init(&DCtx,
7108 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(yy),
7109 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07007110
Laurence Lundbladee3553422020-05-02 11:11:17 -07007111 // Get the first item
7112 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
7113 if(uCBORError != QCBOR_SUCCESS) {
7114 return 12;
7115 }
7116 if(Item.uDataType != QCBOR_TYPE_INT64) {
7117 return 13;
7118 }
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07007119
Laurence Lundbladee3553422020-05-02 11:11:17 -07007120 // Get a second item
7121 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08007122#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
7123
Laurence Lundbladee3553422020-05-02 11:11:17 -07007124 if(uCBORError != QCBOR_SUCCESS) {
7125 return 14;
7126 }
7127 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
7128 return 15;
7129 }
7130
7131 // Try to finish before consuming all bytes to confirm
7132 // that the still-open error is returned.
7133 uCBORError = QCBORDecode_Finish(&DCtx);
7134 if(uCBORError != QCBOR_SUCCESS) {
7135 return 16;
7136 }
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08007137#else /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
7138 if(uCBORError != QCBOR_ERR_INDEF_LEN_ARRAYS_DISABLED) {
7139 return 20;
7140 }
7141#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
Laurence Lundbladee3553422020-05-02 11:11:17 -07007142
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07007143
Laurence Lundbladee3553422020-05-02 11:11:17 -07007144 return 0;
7145}
7146
Laurence Lundbladee15326f2020-06-15 15:50:23 -07007147
Laurence Lundblade70ecead2020-06-15 19:40:06 -07007148
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03007149int32_t IntToTests(void)
Laurence Lundbladee15326f2020-06-15 15:50:23 -07007150{
7151 int nErrCode;
7152 int32_t n32;
7153 int16_t n16;
7154 int8_t n8;
7155 uint32_t u32;
7156 uint16_t u16;
7157 uint8_t u8;
7158 uint64_t u64;
7159
7160 nErrCode = QCBOR_Int64ToInt32(1, &n32);
7161 if(nErrCode == -1 || n32 != 1) {
7162 return 1;
7163 }
7164
7165 nErrCode = QCBOR_Int64ToInt32((int64_t)INT32_MAX, &n32);
7166 if(nErrCode == -1 || n32 != INT32_MAX) {
7167 return 2;
7168 }
7169
7170 nErrCode = QCBOR_Int64ToInt32((int64_t)INT32_MIN, &n32);
7171 if(nErrCode == -1 || n32 != INT32_MIN) {
7172 return 3;
7173 }
7174
7175 nErrCode = QCBOR_Int64ToInt32(((int64_t)INT32_MAX)+1, &n32);
7176 if(nErrCode != -1) {
7177 return 4;
7178 }
7179
7180 nErrCode = QCBOR_Int64ToInt32(((int64_t)INT32_MIN)-1, &n32);
7181 if(nErrCode != -1) {
7182 return 5;
7183 }
7184
7185
7186 nErrCode = QCBOR_Int64ToInt16((int64_t)INT16_MAX, &n16);
7187 if(nErrCode == -1 || n16 != INT16_MAX) {
7188 return 6;
7189 }
7190
7191 nErrCode = QCBOR_Int64ToInt16((int64_t)INT16_MIN, &n16);
7192 if(nErrCode == -1 || n16 != INT16_MIN) {
7193 return 7;
7194 }
7195
7196 nErrCode = QCBOR_Int64ToInt16(1, &n16);
7197 if(nErrCode == -1 || n16 != 1) {
7198 return 8;
7199 }
7200
7201 nErrCode = QCBOR_Int64ToInt16(((int64_t)INT16_MAX)+1, &n16);
7202 if(nErrCode != -1) {
7203 return 9;
7204 }
7205
7206 nErrCode = QCBOR_Int64ToInt16(((int64_t)INT16_MIN)-1, &n16);
7207 if(nErrCode != -1) {
7208 return 10;
7209 }
7210
7211
7212 nErrCode = QCBOR_Int64ToInt8(1, &n8);
7213 if(nErrCode == -1 || n8 != 1) {
7214 return 11;
7215 }
7216
7217 nErrCode = QCBOR_Int64ToInt8((int64_t)INT8_MAX, &n8);
7218 if(nErrCode == -1 || n8 != INT8_MAX) {
7219 return 12;
7220 }
7221
7222 nErrCode = QCBOR_Int64ToInt8((int64_t)INT8_MIN, &n8);
7223 if(nErrCode == -1 || n8 != INT8_MIN) {
7224 return 13;
7225 }
7226
7227 nErrCode = QCBOR_Int64ToInt8(((int64_t)INT8_MAX)+1, &n8);
7228 if(nErrCode != -1) {
7229 return 14;
7230 }
7231
7232 nErrCode = QCBOR_Int64ToInt8(((int64_t)INT8_MIN)-1, &n8);
7233 if(nErrCode != -1) {
7234 return 15;
7235 }
7236
7237
7238 nErrCode = QCBOR_Int64ToUInt32(1, &u32);
7239 if(nErrCode == -1 || u32 != 1) {
7240 return 16;
7241 }
7242
7243 nErrCode = QCBOR_Int64ToUInt32((int64_t)UINT32_MAX, &u32);
7244 if(nErrCode == -1 || u32 != UINT32_MAX) {
7245 return 17;
7246 }
7247
7248 nErrCode = QCBOR_Int64ToUInt32((int64_t)0, &u32);
7249 if(nErrCode == -1 || u32 != 0) {
7250 return 18;
7251 }
7252
7253 nErrCode = QCBOR_Int64ToUInt32(((int64_t)UINT32_MAX)+1, &u32);
7254 if(nErrCode != -1) {
7255 return 19;
7256 }
7257
7258 nErrCode = QCBOR_Int64ToUInt32((int64_t)-1, &u32);
7259 if(nErrCode != -1) {
7260 return 20;
7261 }
7262
7263
7264 nErrCode = QCBOR_Int64UToInt16((int64_t)UINT16_MAX, &u16);
7265 if(nErrCode == -1 || u16 != UINT16_MAX) {
7266 return 21;
7267 }
7268
7269 nErrCode = QCBOR_Int64UToInt16((int64_t)0, &u16);
7270 if(nErrCode == -1 || u16 != 0) {
7271 return 22;
7272 }
7273
7274 nErrCode = QCBOR_Int64UToInt16(1, &u16);
7275 if(nErrCode == -1 || u16 != 1) {
7276 return 23;
7277 }
7278
7279 nErrCode = QCBOR_Int64UToInt16(((int64_t)UINT16_MAX)+1, &u16);
7280 if(nErrCode != -1) {
7281 return 24;
7282 }
7283
7284 nErrCode = QCBOR_Int64UToInt16((int64_t)-1, &u16);
7285 if(nErrCode != -1) {
7286 return 25;
7287 }
7288
7289
7290 nErrCode = QCBOR_Int64ToUInt8((int64_t)UINT8_MAX, &u8);
7291 if(nErrCode == -1 || u8 != UINT8_MAX) {
7292 return 26;
7293 }
7294
7295 nErrCode = QCBOR_Int64ToUInt8((int64_t)0, &u8);
7296 if(nErrCode == -1 || u8 != 0) {
7297 return 27;
7298 }
7299
7300 nErrCode = QCBOR_Int64ToUInt8(1, &u8);
7301 if(nErrCode == -1 || u8 != 1) {
7302 return 28;
7303 }
7304
7305 nErrCode = QCBOR_Int64ToUInt8(((int64_t)UINT16_MAX)+1, &u8);
7306 if(nErrCode != -1) {
7307 return 29;
7308 }
7309
7310 nErrCode = QCBOR_Int64ToUInt8((int64_t)-1, &u8);
7311 if(nErrCode != -1) {
7312 return 30;
7313 }
7314
7315
7316 nErrCode = QCBOR_Int64ToUInt64(1, &u64);
7317 if(nErrCode == -1 || u64 != 1) {
7318 return 31;
7319 }
7320
7321 nErrCode = QCBOR_Int64ToUInt64(INT64_MAX, &u64);
7322 if(nErrCode == -1 || u64 != INT64_MAX) {
7323 return 32;
7324 }
7325
7326 nErrCode = QCBOR_Int64ToUInt64((int64_t)0, &u64);
7327 if(nErrCode == -1 || u64 != 0) {
7328 return 33;
7329 }
7330
7331 nErrCode = QCBOR_Int64ToUInt64((int64_t)-1, &u64);
7332 if(nErrCode != -1) {
7333 return 34;
7334 }
7335
7336 return 0;
7337}
7338
Laurence Lundblade0750fc42020-06-20 21:02:34 -07007339
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07007340
7341
Laurence Lundblade0750fc42020-06-20 21:02:34 -07007342/*
7343A sequence with
7344 A wrapping bstr
7345 containing a map
7346 1
7347 2
7348 A wrapping bstr
7349 containing an array
7350 3
7351 wrapping bstr
7352 4
7353 5
7354 6
7355 array
7356 7
7357 8
Laurence Lundblade0750fc42020-06-20 21:02:34 -07007358 */
7359
Laurence Lundblade55013642020-09-23 05:39:22 -07007360static UsefulBufC EncodeBstrWrapTestData(UsefulBuf OutputBuffer)
Laurence Lundblade0750fc42020-06-20 21:02:34 -07007361{
Laurence Lundblade55013642020-09-23 05:39:22 -07007362 UsefulBufC Encoded;
Laurence Lundblade0750fc42020-06-20 21:02:34 -07007363 QCBOREncodeContext EC;
Laurence Lundblade55013642020-09-23 05:39:22 -07007364 QCBORError uErr;
Laurence Lundblade0750fc42020-06-20 21:02:34 -07007365
Laurence Lundblade55013642020-09-23 05:39:22 -07007366 QCBOREncode_Init(&EC, OutputBuffer);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07007367
7368 QCBOREncode_BstrWrap(&EC);
7369 QCBOREncode_OpenMap(&EC);
7370 QCBOREncode_AddInt64ToMapN(&EC, 100, 1);
7371 QCBOREncode_AddInt64ToMapN(&EC, 200, 2);
7372 QCBOREncode_CloseMap(&EC);
7373 QCBOREncode_BstrWrap(&EC);
7374 QCBOREncode_OpenArray(&EC);
7375 QCBOREncode_AddInt64(&EC, 3);
7376 QCBOREncode_BstrWrap(&EC);
7377 QCBOREncode_AddInt64(&EC, 4);
7378 QCBOREncode_CloseBstrWrap(&EC, NULL);
7379 QCBOREncode_AddInt64(&EC, 5);
7380 QCBOREncode_CloseArray(&EC);
7381 QCBOREncode_CloseBstrWrap(&EC, NULL);
7382 QCBOREncode_AddInt64(&EC, 6);
7383 QCBOREncode_CloseBstrWrap(&EC, NULL);
7384 QCBOREncode_OpenArray(&EC);
7385 QCBOREncode_AddInt64(&EC, 7);
7386 QCBOREncode_AddInt64(&EC, 8);
7387 QCBOREncode_CloseArray(&EC);
7388
7389 uErr = QCBOREncode_Finish(&EC, &Encoded);
Laurence Lundblade40a04322020-06-27 22:52:52 -07007390 if(uErr) {
7391 Encoded = NULLUsefulBufC;
7392 }
Laurence Lundblade0750fc42020-06-20 21:02:34 -07007393
7394 return Encoded;
7395}
7396
Laurence Lundbladecc7da412020-12-27 00:09:07 -08007397/* h'FF' */
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08007398static const uint8_t spBreakInByteString[] = {
7399 0x41, 0xff
7400};
7401
Laurence Lundblade0750fc42020-06-20 21:02:34 -07007402
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03007403int32_t EnterBstrTest(void)
Laurence Lundblade0750fc42020-06-20 21:02:34 -07007404{
Laurence Lundblade8510f8c2020-12-01 11:31:16 -08007405 UsefulBuf_MAKE_STACK_UB(OutputBuffer, 100);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07007406
7407 QCBORDecodeContext DC;
7408
Laurence Lundblade55013642020-09-23 05:39:22 -07007409 QCBORDecode_Init(&DC, EncodeBstrWrapTestData(OutputBuffer), 0);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07007410
Laurence Lundblade55013642020-09-23 05:39:22 -07007411 int64_t n1, n2, n3, n4, n5, n6, n7, n8;
Laurence Lundblade0750fc42020-06-20 21:02:34 -07007412
7413
Laurence Lundblade9b334962020-08-27 10:55:53 -07007414 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07007415 QCBORDecode_EnterMap(&DC, NULL);
Laurence Lundblade55013642020-09-23 05:39:22 -07007416 QCBORDecode_GetInt64InMapN(&DC, 100, &n1);
7417 QCBORDecode_GetInt64InMapN(&DC, 200, &n2);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07007418 QCBORDecode_ExitMap(&DC);
Laurence Lundblade9b334962020-08-27 10:55:53 -07007419 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07007420 QCBORDecode_EnterArray(&DC, NULL);
Laurence Lundblade55013642020-09-23 05:39:22 -07007421 QCBORDecode_GetInt64(&DC, &n3);
Laurence Lundblade9b334962020-08-27 10:55:53 -07007422 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
Laurence Lundblade55013642020-09-23 05:39:22 -07007423 QCBORDecode_GetInt64(&DC, &n4);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07007424 QCBORDecode_ExitBstrWrapped(&DC);
Laurence Lundblade55013642020-09-23 05:39:22 -07007425 QCBORDecode_GetInt64(&DC, &n5);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07007426 QCBORDecode_ExitArray(&DC);
7427 QCBORDecode_ExitBstrWrapped(&DC);
Laurence Lundblade55013642020-09-23 05:39:22 -07007428 QCBORDecode_GetInt64(&DC, &n6);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07007429 QCBORDecode_ExitBstrWrapped(&DC);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07007430 QCBORDecode_EnterArray(&DC, NULL);
Laurence Lundblade55013642020-09-23 05:39:22 -07007431 QCBORDecode_GetInt64(&DC, &n7);
7432 QCBORDecode_GetInt64(&DC, &n8);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07007433 QCBORDecode_ExitArray(&DC);
7434
7435 QCBORError uErr = QCBORDecode_Finish(&DC);
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08007436 if(uErr) {
7437 return (int32_t)uErr;
7438 }
Laurence Lundblade0750fc42020-06-20 21:02:34 -07007439
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08007440
7441 /* Enter and exit byte string wrapped CBOR that is bad. It has just a break.
7442 * Successful because no items are fetched from byte string.
7443 */
7444 QCBORDecode_Init(&DC,
7445 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBreakInByteString),
7446 0);
7447 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
7448 uErr = QCBORDecode_GetError(&DC);
7449 if(uErr) {
7450 return 100 + (int32_t)uErr;
7451 }
7452
7453 QCBORDecode_ExitBstrWrapped(&DC);
7454 uErr = QCBORDecode_GetError(&DC);
7455 if(uErr) {
7456 return 200 + (int32_t)uErr;
7457 }
7458
7459 /* Try to get item that is a break out of a byte string wrapped CBOR.
7460 * It fails because there should be no break.
7461 */
7462 QCBORDecode_Init(&DC,
7463 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBreakInByteString),
7464 0);
7465 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
7466 QCBORItem Item;
7467 uErr = QCBORDecode_GetNext(&DC, &Item);
7468 if(uErr != QCBOR_ERR_BAD_BREAK) {
7469 return 300 + (int32_t)uErr;
7470 }
7471
7472 return 0;
Laurence Lundblade0750fc42020-06-20 21:02:34 -07007473}
Laurence Lundblade37f46e52020-08-04 03:32:14 -07007474
7475
7476
7477
7478static const uint8_t spTaggedTypes[] = {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007479 0xb2,
Laurence Lundblade37f46e52020-08-04 03:32:14 -07007480
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07007481 // Date string
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007482 0x00,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07007483 0xc0, 0x74, 0x32, 0x30, 0x30, 0x33, 0x2D, 0x31, 0x32, 0x2D,
7484 0x31, 0x33, 0x54, 0x31, 0x38, 0x3A, 0x33, 0x30, 0x3A, 0x30,
7485 0x32, 0x5A,
Laurence Lundblade37f46e52020-08-04 03:32:14 -07007486
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007487 0x01,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07007488 0x74, 0x32, 0x30, 0x30, 0x33, 0x2D, 0x31, 0x32, 0x2D, 0x31,
7489 0x33, 0x54, 0x31, 0x38, 0x3A, 0x33, 0x30, 0x3A, 0x30, 0x32,
7490 0x5A,
Laurence Lundblade37f46e52020-08-04 03:32:14 -07007491
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07007492 // Bignum
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007493 10,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07007494 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
7495 0x09, 0x10,
Laurence Lundblade37f46e52020-08-04 03:32:14 -07007496
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007497 11,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07007498 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
7499 0x09, 0x10,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007500
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07007501 // URL
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007502 20,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07007503 0xd8, 0x20, 0x6f, 0x68, 0x74, 0x74, 0x70, 0x3A, 0x2F, 0x2F,
7504 0x63, 0x62, 0x6F, 0x72, 0x2E, 0x6D, 0x65, 0x2F,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007505
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07007506 21,
7507 0x6f, 0x68, 0x74, 0x74, 0x70, 0x3A, 0x2F, 0x2F, 0x63, 0x62,
7508 0x6F, 0x72, 0x2E, 0x6D, 0x65, 0x2F,
7509
7510 // B64
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007511 0x18, 0x1e,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07007512 0xd8, 0x22, 0x6c, 0x63, 0x47, 0x78, 0x6C, 0x59, 0x58, 0x4E,
7513 0x31, 0x63, 0x6D, 0x55, 0x75,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007514
7515 0x18, 0x1f,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07007516 0x6c, 0x63, 0x47, 0x78, 0x6C, 0x59, 0x58, 0x4E, 0x31, 0x63,
7517 0x6D, 0x55, 0x75,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007518
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07007519 // B64URL
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007520 0x18, 0x28,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07007521 0xd8, 0x21, 0x6c, 0x63, 0x47, 0x78, 0x6C, 0x59, 0x58, 0x4E,
7522 0x31, 0x63, 0x6D, 0x55, 0x75,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007523
7524 0x18, 0x29,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07007525 0x6c, 0x63, 0x47, 0x78, 0x6C, 0x59, 0x58, 0x4E, 0x31, 0x63,
7526 0x6D, 0x55, 0x75,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007527
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07007528 // Regex
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007529 0x18, 0x32,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07007530 0xd8, 0x23, 0x68, 0x31, 0x30, 0x30, 0x5C, 0x73, 0x2A, 0x6D,
7531 0x6B,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007532
7533 0x18, 0x33,
7534 0x68, 0x31, 0x30, 0x30, 0x5C, 0x73, 0x2A, 0x6D, 0x6B,
7535
7536 // MIME
7537 0x18, 0x3c,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07007538 0xd8, 0x24, 0x72, 0x4D, 0x49, 0x4D, 0x45, 0x2D, 0x56, 0x65,
7539 0x72, 0x73, 0x69, 0x6F, 0x6E, 0x3A, 0x20, 0x31, 0x2E, 0x30,
7540 0x0A,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007541
7542 0x18, 0x3d,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07007543 0x72, 0x4D, 0x49, 0x4D, 0x45, 0x2D, 0x56, 0x65, 0x72, 0x73,
7544 0x69, 0x6F, 0x6E, 0x3A, 0x20, 0x31, 0x2E, 0x30, 0x0A,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007545
7546 0x18, 0x3e,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07007547 0xd9, 0x01, 0x01, 0x52, 0x4D, 0x49, 0x4D, 0x45, 0x2D, 0x56,
7548 0x65, 0x72, 0x73, 0x69, 0x6F, 0x6E, 0x3A, 0x20, 0x31, 0x2E,
7549 0x30, 0x0A,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007550
7551 0x18, 0x3f,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07007552 0x52, 0x4D, 0x49, 0x4D, 0x45, 0x2D, 0x56, 0x65, 0x72, 0x73,
7553 0x69, 0x6F, 0x6E, 0x3A, 0x20, 0x31, 0x2E, 0x30, 0x0A,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007554
7555 // UUID
7556 0x18, 0x46,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07007557 0xd8, 0x25, 0x50, 0x53, 0x4D, 0x41, 0x52, 0x54, 0x43, 0x53,
7558 0x4C, 0x54, 0x54, 0x43, 0x46, 0x49, 0x43, 0x41, 0x32,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007559
7560 0x18, 0x47,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07007561 0x50, 0x53, 0x4D, 0x41, 0x52, 0x54, 0x43, 0x53, 0x4C, 0x54,
7562 0x54, 0x43, 0x46, 0x49, 0x43, 0x41, 0x32
Laurence Lundblade37f46e52020-08-04 03:32:14 -07007563};
7564
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03007565int32_t DecodeTaggedTypeTests(void)
Laurence Lundblade37f46e52020-08-04 03:32:14 -07007566{
7567 QCBORDecodeContext DC;
7568 QCBORError uErr;
7569
7570 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTaggedTypes), 0);
7571
7572 UsefulBufC String;
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007573 bool bNeg;
Laurence Lundblade37f46e52020-08-04 03:32:14 -07007574
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07007575 QCBORDecode_EnterMap(&DC, NULL);
Laurence Lundblade9b334962020-08-27 10:55:53 -07007576 QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007577 QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &String);
Laurence Lundblade37f46e52020-08-04 03:32:14 -07007578 if(QCBORDecode_GetError(&DC) != QCBOR_SUCCESS) {
7579 return 1;
7580 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07007581 QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade37f46e52020-08-04 03:32:14 -07007582 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_UNEXPECTED_TYPE) {
7583 return 2;
7584 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07007585 QCBORDecode_GetDateStringInMapN(&DC, 1, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade37f46e52020-08-04 03:32:14 -07007586 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_UNEXPECTED_TYPE) {
7587 return 3;
7588 }
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007589 QCBORDecode_GetDateStringInMapN(&DC, 1, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &String);
Laurence Lundblade9b334962020-08-27 10:55:53 -07007590 QCBORDecode_GetDateStringInMapN(&DC, 1, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade37f46e52020-08-04 03:32:14 -07007591 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
7592 return 4;
7593 }
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007594 QCBORDecode_GetDateStringInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07007595 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007596 return 5;
7597 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07007598
Laurence Lundblade9b334962020-08-27 10:55:53 -07007599 QCBORDecode_GetBignumInMapN(&DC, 10, QCBOR_TAG_REQUIREMENT_TAG, &String, &bNeg);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007600 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
7601 bNeg != false) {
7602 return 10;
7603 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07007604 QCBORDecode_GetBignumInMapN(&DC, 11, QCBOR_TAG_REQUIREMENT_TAG, &String, &bNeg);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007605 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
7606 bNeg != true) {
7607 return 11;
7608 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07007609 QCBORDecode_GetBignumInMapN(&DC, 11, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bNeg);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007610 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_UNEXPECTED_TYPE) {
7611 return 12;
7612 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07007613 QCBORDecode_GetBignumInMapN(&DC, 14, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bNeg);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07007614 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007615 return 13;
7616 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07007617 QCBORDecode_GetBignumInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bNeg);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07007618 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007619 return 14;
7620 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07007621
Laurence Lundblade9b334962020-08-27 10:55:53 -07007622 QCBORDecode_GetURIInMapN(&DC, 20, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007623 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
7624 return 20;
7625 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07007626 QCBORDecode_GetURIInMapN(&DC, 21, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007627 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
7628 return 21;
7629 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07007630 QCBORDecode_GetURIInMapN(&DC, 22, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07007631 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007632 return 22;
7633 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07007634 QCBORDecode_GetURIInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07007635 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007636 return 23;
7637 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07007638
Laurence Lundblade24bd7e12021-01-09 00:05:11 -08007639#ifndef QCBOR_DISABLE_UNCOMMON_TAGS
Laurence Lundblade9b334962020-08-27 10:55:53 -07007640 QCBORDecode_GetB64InMapN(&DC, 30, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007641 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
7642 return 30;
7643 }
Laurence Lundblade24bd7e12021-01-09 00:05:11 -08007644#endif
Laurence Lundblade9b334962020-08-27 10:55:53 -07007645 QCBORDecode_GetB64InMapN(&DC, 31, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007646 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
7647 return 31;
7648 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07007649 QCBORDecode_GetB64InMapN(&DC, 32, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07007650 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007651 return 32;
7652 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07007653 QCBORDecode_GetB64InMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07007654 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007655 return 33;
7656 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07007657
Laurence Lundblade24bd7e12021-01-09 00:05:11 -08007658#ifndef QCBOR_DISABLE_UNCOMMON_TAGS
Laurence Lundblade9b334962020-08-27 10:55:53 -07007659 QCBORDecode_GetB64URLInMapN(&DC, 40, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007660 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
7661 return 40;
7662 }
Laurence Lundblade24bd7e12021-01-09 00:05:11 -08007663#endif
Laurence Lundblade9b334962020-08-27 10:55:53 -07007664 QCBORDecode_GetB64URLInMapN(&DC, 41, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007665 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
7666 return 41;
7667 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07007668 QCBORDecode_GetB64URLInMapN(&DC, 42, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07007669 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007670 return 42;
7671 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07007672 QCBORDecode_GetB64URLInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07007673 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007674 return 43;
7675 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07007676
Laurence Lundblade24bd7e12021-01-09 00:05:11 -08007677#ifndef QCBOR_DISABLE_UNCOMMON_TAGS
Laurence Lundblade9b334962020-08-27 10:55:53 -07007678 QCBORDecode_GetRegexInMapN(&DC, 50, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007679 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
7680 return 50;
7681 }
Laurence Lundblade24bd7e12021-01-09 00:05:11 -08007682#endif
Laurence Lundblade9b334962020-08-27 10:55:53 -07007683 QCBORDecode_GetRegexInMapN(&DC, 51, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007684 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
7685 return 51;
7686 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07007687 QCBORDecode_GetRegexInMapN(&DC, 52, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07007688 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007689 return 52;
7690 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07007691 QCBORDecode_GetRegexInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07007692 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007693 return 53;
7694 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07007695
Laurence Lundblade24bd7e12021-01-09 00:05:11 -08007696#ifndef QCBOR_DISABLE_UNCOMMON_TAGS
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007697 // MIME
7698 bool bIsNot7Bit;
Laurence Lundblade9b334962020-08-27 10:55:53 -07007699 QCBORDecode_GetMIMEMessageInMapN(&DC, 60, QCBOR_TAG_REQUIREMENT_TAG, &String, &bIsNot7Bit);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007700 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
7701 bIsNot7Bit == true) {
7702 return 60;
7703 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07007704 QCBORDecode_GetMIMEMessageInMapN(&DC, 61, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bIsNot7Bit);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007705 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
7706 bIsNot7Bit == true) {
7707 return 61;
7708 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07007709 QCBORDecode_GetMIMEMessageInMapN(&DC, 62, QCBOR_TAG_REQUIREMENT_TAG, &String, &bIsNot7Bit);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007710 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
7711 bIsNot7Bit == false) {
7712 return 62;
7713 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07007714 QCBORDecode_GetMIMEMessageInMapN(&DC, 63, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bIsNot7Bit);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007715 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
7716 bIsNot7Bit == false) {
7717 return 63;
7718 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07007719 QCBORDecode_GetMIMEMessageInMapN(&DC, 64, QCBOR_TAG_REQUIREMENT_TAG, &String, &bNeg);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07007720 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007721 return 64;
7722 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07007723 QCBORDecode_GetMIMEMessageInMapSZ(&DC, "zzz", QCBOR_TAG_REQUIREMENT_TAG, &String, &bNeg);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07007724 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007725 return 65;
7726 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07007727
Laurence Lundblade24bd7e12021-01-09 00:05:11 -08007728
Laurence Lundblade9b334962020-08-27 10:55:53 -07007729 QCBORDecode_GetBinaryUUIDInMapN(&DC, 70, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007730 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
7731 return 70;
7732 }
Laurence Lundblade24bd7e12021-01-09 00:05:11 -08007733#endif /* #ifndef QCBOR_DISABLE_UNCOMMON_TAGS */
7734
Laurence Lundblade9b334962020-08-27 10:55:53 -07007735 QCBORDecode_GetBinaryUUIDInMapN(&DC, 71, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007736 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
7737 return 71;
7738 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07007739 QCBORDecode_GetBinaryUUIDInMapN(&DC, 72, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07007740 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007741 return 72;
7742 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07007743 QCBORDecode_GetBinaryUUIDInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07007744 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007745 return 73;
7746 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07007747
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07007748 // Improvement: add some more error test cases
7749
Laurence Lundblade37f46e52020-08-04 03:32:14 -07007750 QCBORDecode_ExitMap(&DC);
7751
7752 uErr = QCBORDecode_Finish(&DC);
7753 if(uErr != QCBOR_SUCCESS) {
7754 return 100;
7755 }
7756
7757 return 0;
7758}
Laurence Lundbladea4308a82020-10-03 18:08:57 -07007759
7760
7761
7762
7763/*
Laurence Lundbladecc7da412020-12-27 00:09:07 -08007764 [
7765 "aaaaaaaaaa",
7766 {}
7767 ]
Laurence Lundbladea4308a82020-10-03 18:08:57 -07007768 */
7769static const uint8_t spTooLarge1[] = {
7770 0x9f,
7771 0x6a, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61,
7772 0xa0,
7773 0xff
7774};
7775
7776/*
Laurence Lundbladecc7da412020-12-27 00:09:07 -08007777 [
7778 {
7779 0: "aaaaaaaaaa"
7780 }
7781 ]
Laurence Lundbladea4308a82020-10-03 18:08:57 -07007782 */
7783static const uint8_t spTooLarge2[] = {
7784 0x9f,
7785 0xa1,
7786 0x00,
7787 0x6a, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61,
7788 0xff
7789};
7790
7791/*
Laurence Lundbladecc7da412020-12-27 00:09:07 -08007792 h'A1006A61616161616161616161'
Laurence Lundbladea4308a82020-10-03 18:08:57 -07007793
Laurence Lundbladecc7da412020-12-27 00:09:07 -08007794 {
7795 0: "aaaaaaaaaa"
7796 }
Laurence Lundbladea4308a82020-10-03 18:08:57 -07007797 */
7798static const uint8_t spTooLarge3[] = {
7799 0x4d,
7800 0xa1,
7801 0x00,
7802 0x6a, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61,
7803};
7804
7805int32_t TooLargeInputTest(void)
7806{
7807 QCBORDecodeContext DC;
7808 QCBORError uErr;
7809 UsefulBufC String;
7810
7811 // These tests require a build with QCBOR_MAX_DECODE_INPUT_SIZE set
7812 // to 10 There's not really any way to test this error
7813 // condition. The error condition is not complex, so setting
7814 // QCBOR_MAX_DECODE_INPUT_SIZE gives an OK test.
7815
7816 // The input CBOR is only too large because the
7817 // QCBOR_MAX_DECODE_INPUT_SIZE is 10.
7818 //
7819 // This test is disabled for the normal test runs because of the
7820 // special build requirement.
7821
7822
7823 // Tests the start of a map being too large
7824 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTooLarge1), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07007825 QCBORDecode_EnterArray(&DC, NULL);
Laurence Lundbladea4308a82020-10-03 18:08:57 -07007826 QCBORDecode_GetTextString(&DC, &String);
7827 uErr = QCBORDecode_GetError(&DC);
7828 if(uErr != QCBOR_SUCCESS) {
7829 return 1;
7830 }
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07007831 QCBORDecode_EnterMap(&DC, NULL);
Laurence Lundbladea4308a82020-10-03 18:08:57 -07007832 uErr = QCBORDecode_GetError(&DC);
7833 if(uErr != QCBOR_ERR_INPUT_TOO_LARGE) {
7834 return 2;
7835 }
7836
7837 // Tests the end of a map being too large
7838 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTooLarge2), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07007839 QCBORDecode_EnterArray(&DC, NULL);
7840 QCBORDecode_EnterMap(&DC, NULL);
Laurence Lundbladea4308a82020-10-03 18:08:57 -07007841 uErr = QCBORDecode_GetError(&DC);
7842 if(uErr != QCBOR_SUCCESS) {
7843 return 3;
7844 }
7845 QCBORDecode_ExitMap(&DC);
7846 uErr = QCBORDecode_GetError(&DC);
7847 if(uErr != QCBOR_ERR_INPUT_TOO_LARGE) {
7848 return 4;
7849 }
7850
7851 // Tests the entire input CBOR being too large when processing bstr wrapping
7852 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTooLarge3), QCBOR_DECODE_MODE_NORMAL);
7853 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
7854 uErr = QCBORDecode_GetError(&DC);
7855 if(uErr != QCBOR_ERR_INPUT_TOO_LARGE) {
7856 return 5;
7857 }
7858
7859 return 0;
7860}
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07007861
7862
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08007863#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
7864
Laurence Lundblade37286c02022-09-03 10:05:02 -07007865/*
7866 An array of three map entries
7867 1) Indefinite length string label for indefinite lenght byte string
7868 2) Indefinite length string label for an integer
7869 3) Indefinite length string label for an indefinite-length negative big num
7870 */
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07007871static const uint8_t spMapWithIndefLenStrings[] = {
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08007872 0xa3,
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07007873 0x7f, 0x61, 'l', 0x64, 'a', 'b', 'e', 'l' , 0x61, '1', 0xff,
7874 0x5f, 0x42, 0x01, 0x02, 0x43, 0x03, 0x04, 0x05, 0xff,
7875 0x7f, 0x62, 'd', 'y', 0x61, 'm', 0x61, 'o', 0xff,
7876 0x03,
7877 0x7f, 0x62, 'l', 'a', 0x63, 'b', 'e', 'l', 0x61, '2', 0xff,
7878 0xc3,
7879 0x5f, 0x42, 0x00, 0x01, 0x42, 0x00, 0x01, 0x41, 0x01, 0xff,
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07007880};
7881
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03007882int32_t SpiffyIndefiniteLengthStringsTests(void)
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07007883{
7884 QCBORDecodeContext DCtx;
7885
7886 QCBORDecode_Init(&DCtx,
7887 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spMapWithIndefLenStrings),
7888 QCBOR_DECODE_MODE_NORMAL);
7889
Laurence Lundblade8510f8c2020-12-01 11:31:16 -08007890 UsefulBuf_MAKE_STACK_UB(StringBuf, 200);
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07007891 QCBORDecode_SetMemPool(&DCtx, StringBuf, false);
7892
7893 UsefulBufC ByteString;
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07007894 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07007895 QCBORDecode_GetByteStringInMapSZ(&DCtx, "label1", &ByteString);
Laurence Lundblade37286c02022-09-03 10:05:02 -07007896
7897#ifndef QCBOR_DISABLE_TAGS
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07007898 if(QCBORDecode_GetAndResetError(&DCtx)) {
7899 return 1;
7900 }
7901
7902 const uint8_t pExectedBytes[] = {0x01, 0x02, 0x03, 0x04, 0x05};
7903 if(UsefulBuf_Compare(ByteString, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pExectedBytes))) {
7904 return 2;
7905 }
7906
7907 uint64_t uInt;
7908 QCBORDecode_GetUInt64InMapSZ(&DCtx, "dymo", &uInt);
7909 if(QCBORDecode_GetAndResetError(&DCtx)) {
7910 return 3;
7911 }
7912 if(uInt != 3) {
7913 return 4;
7914 }
7915
Máté Tóth-Pálef5f07a2021-09-17 19:31:37 +02007916#ifndef USEFULBUF_DISABLE_ALL_FLOAT
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07007917 double uDouble;
7918 QCBORDecode_GetDoubleConvertAllInMapSZ(&DCtx,
7919 "label2",
7920 0xff,
7921 &uDouble);
Laurence Lundblade37286c02022-09-03 10:05:02 -07007922
Laurence Lundbladeb8e19aa2020-10-07 20:59:11 -07007923#ifndef QCBOR_DISABLE_FLOAT_HW_USE
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07007924 if(QCBORDecode_GetAndResetError(&DCtx)) {
7925 return 5;
7926 }
7927 if(uDouble != -16777474) {
7928 return 6;
7929 }
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08007930#else /* QCBOR_DISABLE_FLOAT_HW_USE */
Laurence Lundbladeb8e19aa2020-10-07 20:59:11 -07007931 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_HW_FLOAT_DISABLED) {
7932 return 7;
7933 }
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08007934#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
Máté Tóth-Pálef5f07a2021-09-17 19:31:37 +02007935#endif /* USEFULBUF_DISABLE_ALL_FLOAT */
Laurence Lundbladeb8e19aa2020-10-07 20:59:11 -07007936
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07007937 QCBORDecode_ExitMap(&DCtx);
7938
7939 if(QCBORDecode_Finish(&DCtx)) {
7940 return 99;
7941 }
7942
Laurence Lundblade37286c02022-09-03 10:05:02 -07007943#else /* QCBOR_DISABLE_TAGS */
7944 /* The big num in the input is a CBOR tag and you can't do
7945 * map lookups in a map with a tag so this test does very little
7946 * when tags are disabled. That is OK, the test coverage is still
7947 * good when they are not.
7948 */
7949 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_TAGS_DISABLED) {
7950 return 1002;
7951 }
7952#endif /*QCBOR_DISABLE_TAGS */
7953
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07007954 return 0;
7955}
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08007956#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
Laurence Lundblade2a26abb2020-11-05 19:06:54 -08007957
7958
Laurence Lundbladecf41c522021-02-20 10:19:07 -07007959/*
7960 * An array of an integer and an array. The second array contains
7961 * a bstr-wrapped map.
7962 *
7963 * [7, [h'A36D6669... (see next lines) 73']]
7964 *
7965 * {"first integer": 42,
7966 * "an array of two strings": ["string1", "string2"],
7967 * "map in a map":
7968 * { "bytes 1": h'78787878',
7969 * "bytes 2": h'79797979',
7970 * "another int": 98,
7971 * "text 2": "lies, damn lies and statistics"
7972 * }
7973 * }
7974 */
Laurence Lundblade2a26abb2020-11-05 19:06:54 -08007975
Laurence Lundbladecf41c522021-02-20 10:19:07 -07007976static const uint8_t pValidWrappedMapEncoded[] = {
7977 0x82, 0x07, 0x81, 0x58, 0x97,
7978 0xa3, 0x6d, 0x66, 0x69, 0x72, 0x73, 0x74, 0x20, 0x69, 0x6e,
7979 0x74, 0x65, 0x67, 0x65, 0x72, 0x18, 0x2a, 0x77, 0x61, 0x6e,
7980 0x20, 0x61, 0x72, 0x72, 0x61, 0x79, 0x20, 0x6f, 0x66, 0x20,
7981 0x74, 0x77, 0x6f, 0x20, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67,
7982 0x73, 0x82, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x31,
7983 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x32, 0x6c, 0x6d,
7984 0x61, 0x70, 0x20, 0x69, 0x6e, 0x20, 0x61, 0x20, 0x6d, 0x61,
7985 0x70, 0xa4, 0x67, 0x62, 0x79, 0x74, 0x65, 0x73, 0x20, 0x31,
7986 0x44, 0x78, 0x78, 0x78, 0x78, 0x67, 0x62, 0x79, 0x74, 0x65,
7987 0x73, 0x20, 0x32, 0x44, 0x79, 0x79, 0x79, 0x79, 0x6b, 0x61,
7988 0x6e, 0x6f, 0x74, 0x68, 0x65, 0x72, 0x20, 0x69, 0x6e, 0x74,
7989 0x18, 0x62, 0x66, 0x74, 0x65, 0x78, 0x74, 0x20, 0x32, 0x78,
7990 0x1e, 0x6c, 0x69, 0x65, 0x73, 0x2c, 0x20, 0x64, 0x61, 0x6d,
7991 0x6e, 0x20, 0x6c, 0x69, 0x65, 0x73, 0x20, 0x61, 0x6e, 0x64,
7992 0x20, 0x73, 0x74, 0x61, 0x74, 0x69, 0x73, 0x74, 0x69, 0x63,
7993 0x73
7994};
7995
7996#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
7997
7998/* As above, but the arrays are indefinite length */
7999static const uint8_t pValidIndefWrappedMapEncoded[] = {
8000 0x9f, 0x07, 0x9f, 0x58, 0x97,
8001 0xa3, 0x6d, 0x66, 0x69, 0x72, 0x73, 0x74, 0x20, 0x69, 0x6e,
8002 0x74, 0x65, 0x67, 0x65, 0x72, 0x18, 0x2a, 0x77, 0x61, 0x6e,
8003 0x20, 0x61, 0x72, 0x72, 0x61, 0x79, 0x20, 0x6f, 0x66, 0x20,
8004 0x74, 0x77, 0x6f, 0x20, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67,
8005 0x73, 0x82, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x31,
8006 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x32, 0x6c, 0x6d,
8007 0x61, 0x70, 0x20, 0x69, 0x6e, 0x20, 0x61, 0x20, 0x6d, 0x61,
8008 0x70, 0xa4, 0x67, 0x62, 0x79, 0x74, 0x65, 0x73, 0x20, 0x31,
8009 0x44, 0x78, 0x78, 0x78, 0x78, 0x67, 0x62, 0x79, 0x74, 0x65,
8010 0x73, 0x20, 0x32, 0x44, 0x79, 0x79, 0x79, 0x79, 0x6b, 0x61,
8011 0x6e, 0x6f, 0x74, 0x68, 0x65, 0x72, 0x20, 0x69, 0x6e, 0x74,
8012 0x18, 0x62, 0x66, 0x74, 0x65, 0x78, 0x74, 0x20, 0x32, 0x78,
8013 0x1e, 0x6c, 0x69, 0x65, 0x73, 0x2c, 0x20, 0x64, 0x61, 0x6d,
8014 0x6e, 0x20, 0x6c, 0x69, 0x65, 0x73, 0x20, 0x61, 0x6e, 0x64,
8015 0x20, 0x73, 0x74, 0x61, 0x74, 0x69, 0x73, 0x74, 0x69, 0x63,
8016 0x73,
8017 0xff, 0xff
8018};
8019#endif
8020
8021
8022static const uint8_t pWithEmptyMap[] = {0x82, 0x18, 0x64, 0xa0};
8023
8024#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
8025static const uint8_t pWithEmptyMapInDef[] = {0x9f, 0x18, 0x64, 0xbf, 0xff, 0xff};
8026#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
8027
8028#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
Laurence Lundblade37286c02022-09-03 10:05:02 -07008029/*
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08008030 * An array of one that contains a byte string that is an indefinite
8031 * length string that CBOR wraps an array of three numbers [42, 43,
8032 * 44]. The byte string is an implicit tag 24.
8033 *
8034 * [
8035 * (_ h'83', h'18', h'2A182B', h'182C')
8036 * ]
Laurence Lundblade37286c02022-09-03 10:05:02 -07008037 */
Laurence Lundbladecf41c522021-02-20 10:19:07 -07008038static const uint8_t pWrappedByIndefiniteLength[] = {
8039 0x81,
Laurence Lundbladecf41c522021-02-20 10:19:07 -07008040 0x5f,
8041 0x41, 0x83,
8042 0x41, 0x18,
8043 0x43, 0x2A, 0x18, 0x2B,
8044 0x42, 0x18, 0x2C,
8045 0xff
8046};
8047#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
8048
8049
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03008050int32_t PeekAndRewindTest(void)
Laurence Lundblade2a26abb2020-11-05 19:06:54 -08008051{
8052 QCBORItem Item;
8053 QCBORError nCBORError;
8054 QCBORDecodeContext DCtx;
8055
8056 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0);
8057
8058 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
8059 return 100+(int32_t)nCBORError;
8060 }
8061 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
8062 return 200;
8063 }
8064
Laurence Lundblade3427dee2021-06-20 11:11:24 -07008065 QCBORDecode_VPeekNext(&DCtx, &Item);
8066 if((nCBORError = QCBORDecode_GetError(&DCtx))) {
8067 return 150+(int32_t)nCBORError;
8068 }
8069 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
8070 return 250;
8071 }
8072
Laurence Lundblade2a26abb2020-11-05 19:06:54 -08008073 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
8074 return (int32_t)nCBORError;
8075 }
8076 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
8077 return 300;
8078 }
8079
8080 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
8081 return 400 + (int32_t)nCBORError;
8082 }
8083 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
8084 return 500;
8085 }
8086
8087 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8088 return (int32_t)nCBORError;
8089 }
8090 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
8091 return 600;
8092 }
8093
8094 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
8095 return 900 + (int32_t)nCBORError;
8096 }
8097 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
8098 Item.uDataType != QCBOR_TYPE_INT64 ||
8099 Item.val.int64 != 42 ||
8100 Item.uDataAlloc ||
8101 Item.uLabelAlloc ||
8102 UsefulBufCompareToSZ(Item.label.string, "first integer")) {
8103 return 1000;
8104 }
8105
8106 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8107 return 1100 + (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 1200;
8117 }
8118
8119
8120 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8121 return 1300 + (int32_t)nCBORError;
8122 }
8123 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
8124 Item.uDataAlloc ||
8125 Item.uLabelAlloc ||
8126 UsefulBufCompareToSZ(Item.label.string, "an array of two strings") ||
8127 Item.uDataType != QCBOR_TYPE_ARRAY ||
Laurence Lundbladecf41c522021-02-20 10:19:07 -07008128 Item.val.uCount != 2) {
Laurence Lundblade2a26abb2020-11-05 19:06:54 -08008129 return 1400;
Laurence Lundbladecf41c522021-02-20 10:19:07 -07008130 }
Laurence Lundblade2a26abb2020-11-05 19:06:54 -08008131
8132 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8133 return 1500 + (int32_t)nCBORError;
8134 }
8135 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
8136 Item.uDataAlloc ||
8137 Item.uLabelAlloc ||
8138 UsefulBufCompareToSZ(Item.val.string, "string1")) {
8139 return 1600;
8140 }
8141
8142 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
8143 return 1700 + (int32_t)nCBORError;
8144 }
8145 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
8146 Item.uDataAlloc ||
8147 Item.uLabelAlloc ||
8148 UsefulBufCompareToSZ(Item.val.string, "string2")) {
8149 return 1800;
8150 }
8151
8152 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
8153 return (int32_t)nCBORError;
8154 }
8155 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
8156 Item.uDataAlloc ||
8157 Item.uLabelAlloc ||
8158 UsefulBufCompareToSZ(Item.val.string, "string2")) {
8159 return 1900;
8160 }
8161
8162 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8163 return (int32_t)nCBORError;
8164 }
8165 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
8166 Item.uDataAlloc ||
8167 Item.uLabelAlloc ||
8168 UsefulBufCompareToSZ(Item.val.string, "string2")) {
8169 return 2000;
8170 }
8171
8172
8173 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8174 return 2100 + (int32_t)nCBORError;
8175 }
8176 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
8177 Item.uDataAlloc ||
8178 Item.uLabelAlloc ||
8179 UsefulBufCompareToSZ(Item.label.string, "map in a map") ||
8180 Item.uDataType != QCBOR_TYPE_MAP ||
8181 Item.val.uCount != 4) {
8182 return 2100;
8183 }
8184
8185 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8186 return 2200 + (int32_t)nCBORError;
8187 }
8188 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
8189 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("bytes 1"))||
8190 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
8191 Item.uDataAlloc ||
8192 Item.uLabelAlloc ||
8193 UsefulBufCompareToSZ(Item.val.string, "xxxx")) {
8194 return 2300;
8195 }
8196
8197 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
8198 return 2400 + (int32_t)nCBORError;
8199 }
8200 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
8201 UsefulBufCompareToSZ(Item.label.string, "bytes 2") ||
8202 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
8203 Item.uDataAlloc ||
8204 Item.uLabelAlloc ||
8205 UsefulBufCompareToSZ(Item.val.string, "yyyy")) {
8206 return 2500;
8207 }
8208
8209 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8210 return 2600 + (int32_t)nCBORError;
8211 }
8212 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
8213 UsefulBufCompareToSZ(Item.label.string, "bytes 2") ||
8214 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
8215 Item.uDataAlloc ||
8216 Item.uLabelAlloc ||
8217 UsefulBufCompareToSZ(Item.val.string, "yyyy")) {
8218 return 2700;
8219 }
8220
8221 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8222 return 2800 + (int32_t)nCBORError;
8223 }
8224 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
8225 Item.uDataAlloc ||
8226 Item.uLabelAlloc ||
8227 UsefulBufCompareToSZ(Item.label.string, "another int") ||
8228 Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundbladecf41c522021-02-20 10:19:07 -07008229 Item.val.int64 != 98) {
Laurence Lundblade2a26abb2020-11-05 19:06:54 -08008230 return 2900;
Laurence Lundbladecf41c522021-02-20 10:19:07 -07008231 }
Laurence Lundblade2a26abb2020-11-05 19:06:54 -08008232
8233 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
8234 return 3000 + (int32_t)nCBORError;
8235 }
8236 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
8237 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("text 2"))||
8238 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
8239 Item.uDataAlloc ||
8240 Item.uLabelAlloc ||
8241 UsefulBufCompareToSZ(Item.val.string, "lies, damn lies and statistics")) {
8242 return 3100;
8243 }
8244
8245 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8246 return 3200 + (int32_t)nCBORError;
8247 }
8248 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
8249 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("text 2"))||
8250 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
8251 Item.uDataAlloc ||
8252 Item.uLabelAlloc ||
8253 UsefulBufCompareToSZ(Item.val.string, "lies, damn lies and statistics")) {
8254 return 3300;
8255 }
8256
Laurence Lundblade3427dee2021-06-20 11:11:24 -07008257 nCBORError = QCBORDecode_PeekNext(&DCtx, &Item);
8258 if(nCBORError != QCBOR_ERR_NO_MORE_ITEMS) {
8259 return 3300 + (int32_t)nCBORError;
8260 }
8261
8262 QCBORDecode_VPeekNext(&DCtx, &Item);
8263 nCBORError = QCBORDecode_GetError(&DCtx);
8264 if(nCBORError != QCBOR_ERR_NO_MORE_ITEMS) {
8265 return 3400 + (int32_t)nCBORError;
8266 }
8267
8268 QCBORDecode_VPeekNext(&DCtx, &Item);
8269 nCBORError = QCBORDecode_GetError(&DCtx);
8270 if(nCBORError != QCBOR_ERR_NO_MORE_ITEMS) {
8271 return 3500 + (int32_t)nCBORError;
8272 }
Laurence Lundbladecf41c522021-02-20 10:19:07 -07008273
8274
8275 // Rewind to top level after entering several maps
8276 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0);
8277
8278 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8279 return (int32_t)nCBORError;
8280 }
8281 if(Item.uDataType != QCBOR_TYPE_MAP ||
8282 Item.val.uCount != 3) {
8283 return 400;
8284 }
8285
8286 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8287 return 4000+(int32_t)nCBORError;
8288 }
8289
8290 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
8291 Item.uDataType != QCBOR_TYPE_INT64 ||
8292 Item.val.int64 != 42 ||
8293 Item.uDataAlloc ||
8294 Item.uLabelAlloc ||
8295 UsefulBufCompareToSZ(Item.label.string, "first integer")) {
8296 return 4100;
8297 }
8298
8299 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8300 return 4100+(int32_t)nCBORError;
8301 }
8302 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
8303 Item.uDataAlloc ||
8304 Item.uLabelAlloc ||
8305 UsefulBufCompareToSZ(Item.label.string, "an array of two strings") ||
8306 Item.uDataType != QCBOR_TYPE_ARRAY ||
8307 Item.val.uCount != 2) {
8308 return 4200;
8309 }
8310
8311 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8312 return 4200+(int32_t)nCBORError;
8313 }
8314 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
8315 Item.uDataAlloc ||
8316 Item.uLabelAlloc ||
8317 UsefulBufCompareToSZ(Item.val.string, "string1")) {
8318 return 4300;
8319 }
8320
8321 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8322 return 4300+(int32_t)nCBORError;
8323 }
8324 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
8325 Item.uDataAlloc ||
8326 Item.uLabelAlloc ||
8327 UsefulBufCompareToSZ(Item.val.string, "string2")) {
8328 return 4400;
8329 }
8330
8331 QCBORDecode_Rewind(&DCtx);
8332
8333 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8334 return 4400+(int32_t)nCBORError;
8335 }
8336 if(Item.uDataType != QCBOR_TYPE_MAP ||
8337 Item.val.uCount != 3) {
8338 return 4500;
8339 }
8340
8341 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8342 return (int32_t)nCBORError;
8343 }
8344
8345 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
8346 Item.uDataType != QCBOR_TYPE_INT64 ||
8347 Item.val.int64 != 42 ||
8348 Item.uDataAlloc ||
8349 Item.uLabelAlloc ||
8350 UsefulBufCompareToSZ(Item.label.string, "first integer")) {
8351 return 4600;
8352 }
8353
8354 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8355 return (int32_t)nCBORError;
8356 }
8357 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
8358 Item.uDataAlloc ||
8359 Item.uLabelAlloc ||
8360 UsefulBufCompareToSZ(Item.label.string, "an array of two strings") ||
8361 Item.uDataType != QCBOR_TYPE_ARRAY ||
8362 Item.val.uCount != 2) {
8363 return 4700;
8364 }
8365
8366 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8367 return (int32_t)nCBORError;
8368 }
8369 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
8370 Item.uDataAlloc ||
8371 Item.uLabelAlloc ||
8372 UsefulBufCompareToSZ(Item.val.string, "string1")) {
8373 return 4800;
8374 }
8375
8376 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8377 return 4900+(int32_t)nCBORError;
8378 }
8379 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
8380 Item.uDataAlloc ||
8381 Item.uLabelAlloc ||
8382 UsefulBufCompareToSZ(Item.val.string, "string2")) {
8383 return 5000;
8384 }
8385
8386
8387 // Rewind an entered map
8388 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0);
8389
8390 QCBORDecode_EnterMap(&DCtx, NULL);
8391
8392 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8393 return 5100+(int32_t)nCBORError;
8394 }
8395
8396 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
8397 Item.uDataType != QCBOR_TYPE_INT64 ||
8398 Item.val.int64 != 42 ||
8399 Item.uDataAlloc ||
8400 Item.uLabelAlloc ||
8401 UsefulBufCompareToSZ(Item.label.string, "first integer")) {
8402 return 5200;
8403 }
8404
8405 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8406 return 5200+(int32_t)nCBORError;
8407 }
8408 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
8409 Item.uDataAlloc ||
8410 Item.uLabelAlloc ||
8411 UsefulBufCompareToSZ(Item.label.string, "an array of two strings") ||
8412 Item.uDataType != QCBOR_TYPE_ARRAY ||
8413 Item.val.uCount != 2) {
8414 return -5300;
8415 }
8416
8417 QCBORDecode_Rewind(&DCtx);
8418
8419 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8420 return 5300+(int32_t)nCBORError;
8421 }
8422
8423 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
8424 Item.uDataType != QCBOR_TYPE_INT64 ||
8425 Item.val.int64 != 42 ||
8426 Item.uDataAlloc ||
8427 Item.uLabelAlloc ||
8428 UsefulBufCompareToSZ(Item.label.string, "first integer")) {
8429 return 5400;
8430 }
8431
8432 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8433 return 5400+(int32_t)nCBORError;
8434 }
8435 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
8436 Item.uDataAlloc ||
8437 Item.uLabelAlloc ||
8438 UsefulBufCompareToSZ(Item.label.string, "an array of two strings") ||
8439 Item.uDataType != QCBOR_TYPE_ARRAY ||
8440 Item.val.uCount != 2) {
8441 return 5500;
8442 }
8443
8444
8445 // Rewind and entered array inside an entered map
8446 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0);
8447
8448 QCBORDecode_EnterMap(&DCtx, NULL);
8449
8450 QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings");
8451
8452 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8453 return 5600+(int32_t)nCBORError;
8454 }
8455 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
8456 Item.uDataAlloc ||
8457 Item.uLabelAlloc ||
8458 UsefulBufCompareToSZ(Item.val.string, "string1")) {
8459 return 5700;
8460 }
8461
8462 QCBORDecode_Rewind(&DCtx);
8463
8464 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8465 return 5700+(int32_t)nCBORError;
8466 }
8467 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
8468 Item.uDataAlloc ||
8469 Item.uLabelAlloc ||
8470 UsefulBufCompareToSZ(Item.val.string, "string1")) {
8471 return 5800;
8472 }
8473
8474 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8475 return (int32_t)nCBORError;
8476 }
8477 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
8478 Item.uDataAlloc ||
8479 Item.uLabelAlloc ||
8480 UsefulBufCompareToSZ(Item.val.string, "string2")) {
8481 return 5900;
8482 }
8483
8484 QCBORDecode_Rewind(&DCtx);
8485
8486 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8487 return 5900+(int32_t)nCBORError;
8488 }
8489 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
8490 Item.uDataAlloc ||
8491 Item.uLabelAlloc ||
8492 UsefulBufCompareToSZ(Item.val.string, "string1")) {
8493 return 6000;
8494 }
8495
8496
8497 // Rewind a byte string inside an array inside an array
8498 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidWrappedMapEncoded), 0);
8499
8500 QCBORDecode_EnterArray(&DCtx, NULL);
8501
8502 uint64_t i;
8503 QCBORDecode_GetUInt64(&DCtx, &i);
8504
8505 QCBORDecode_EnterArray(&DCtx, NULL);
8506
8507 QCBORDecode_EnterBstrWrapped(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
8508 if(QCBORDecode_GetError(&DCtx)) {
8509 return 6100;
8510 }
8511
8512 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8513 return (int32_t)nCBORError;
8514 }
8515 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
8516 return 6200;
8517 }
8518
8519 QCBORDecode_Rewind(&DCtx);
8520
8521 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8522 return 6300+(int32_t)nCBORError;
8523 }
8524 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
8525 return 6400;
8526 }
8527
8528#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
8529 // Rewind a byte string inside an indefinite-length array inside
8530 // indefinite-length array
8531
8532 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidIndefWrappedMapEncoded), 0);
8533
8534 QCBORDecode_EnterArray(&DCtx, NULL);
8535
8536 QCBORDecode_GetUInt64(&DCtx, &i);
8537
8538 QCBORDecode_EnterArray(&DCtx, NULL);
8539
8540 QCBORDecode_EnterBstrWrapped(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
8541 if(QCBORDecode_GetError(&DCtx)) {
8542 return 6500;
8543 }
8544
8545 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8546 return 6600+(int32_t)nCBORError;
8547 }
8548 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
8549 return 6700;
8550 }
8551
8552 QCBORDecode_Rewind(&DCtx);
8553
8554 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8555 return 6800+(int32_t)nCBORError;
8556 }
8557 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
8558 return 6900;
8559 }
8560#endif
8561
8562 // Rewind an empty map
8563 // [100, {}]
8564 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pWithEmptyMap), 0);
8565 QCBORDecode_EnterArray(&DCtx, NULL);
8566 QCBORDecode_GetUInt64(&DCtx, &i);
8567 if(i != 100) {
8568 return 7010;
8569 }
8570 QCBORDecode_EnterMap(&DCtx, NULL);
8571
8572 /* Do it 5 times to be sure multiple rewinds work */
8573 for(int n = 0; n < 5; n++) {
8574 nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
8575 if(nCBORError != QCBOR_ERR_NO_MORE_ITEMS) {
8576 return 7000 + n;
8577 }
8578 QCBORDecode_Rewind(&DCtx);
8579 }
8580 QCBORDecode_ExitMap(&DCtx);
8581 QCBORDecode_Rewind(&DCtx);
8582 QCBORDecode_GetUInt64(&DCtx, &i);
8583 if(i != 100) {
8584 return 7010;
8585 }
8586 QCBORDecode_ExitArray(&DCtx);
8587 QCBORDecode_Rewind(&DCtx);
8588 QCBORDecode_EnterArray(&DCtx, NULL);
8589 i = 9;
8590 QCBORDecode_GetUInt64(&DCtx, &i);
8591 if(i != 100) {
8592 return 7020;
8593 }
8594 if(QCBORDecode_GetError(&DCtx)){
8595 return 7030;
8596 }
8597
8598 // Rewind an empty indefinite length map
8599#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
8600 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pWithEmptyMapInDef), 0);
8601 QCBORDecode_EnterArray(&DCtx, NULL);
8602 QCBORDecode_GetUInt64(&DCtx, &i);
8603 if(i != 100) {
8604 return 7810;
8605 }
8606 QCBORDecode_EnterMap(&DCtx, NULL);
8607
8608 /* Do it 5 times to be sure multiple rewinds work */
8609 for(int n = 0; n < 5; n++) {
8610 nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
8611 if(nCBORError != QCBOR_ERR_NO_MORE_ITEMS) {
8612 return 7800 + n;
8613 }
8614 QCBORDecode_Rewind(&DCtx);
8615 }
8616 QCBORDecode_ExitMap(&DCtx);
8617 QCBORDecode_Rewind(&DCtx);
8618 QCBORDecode_GetUInt64(&DCtx, &i);
8619 if(i != 100) {
8620 return 7810;
8621 }
8622 QCBORDecode_ExitArray(&DCtx);
8623 QCBORDecode_Rewind(&DCtx);
8624 QCBORDecode_EnterArray(&DCtx, NULL);
8625 i = 9;
8626 QCBORDecode_GetUInt64(&DCtx, &i);
8627 if(i != 100) {
8628 return 7820;
8629 }
8630 if(QCBORDecode_GetError(&DCtx)){
8631 return 7830;
8632 }
8633#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
8634
8635 // Rewind an indefnite length byte-string wrapped sequence
8636#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
8637 QCBORDecode_Init(&DCtx,
8638 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pWrappedByIndefiniteLength),
8639 0);
8640 UsefulBuf_MAKE_STACK_UB(Pool, 100);
8641 QCBORDecode_SetMemPool(&DCtx, Pool, 0);
8642
8643 QCBORDecode_EnterArray(&DCtx, NULL);
8644 QCBORDecode_EnterBstrWrapped(&DCtx, 2, NULL);
Laurence Lundblade31fddb72024-05-13 13:03:35 -07008645 if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_CANNOT_ENTER_ALLOCATED_STRING) {
Laurence Lundbladecf41c522021-02-20 10:19:07 -07008646 return 7300;
8647 }
8648
8649 /*
Laurence Lundblade31fddb72024-05-13 13:03:35 -07008650 Improvement: Fix QCBORDecode_EnterBstrWrapped() so it can work on
8651 allocated strings. This is a fairly big job because of all the
8652 UsefulBuf internal book keeping that needs tweaking.
Laurence Lundbladecf41c522021-02-20 10:19:07 -07008653 QCBORDecode_GetUInt64(&DCtx, &i);
8654 if(i != 42) {
8655 return 7110;
8656 }
8657 QCBORDecode_Rewind(&DCtx);
8658 QCBORDecode_GetUInt64(&DCtx, &i);
8659 if(i != 42) {
8660 return 7220;
Laurence Lundblade31fddb72024-05-13 13:03:35 -07008661 }
8662 */
Laurence Lundblade37286c02022-09-03 10:05:02 -07008663
Laurence Lundbladecf41c522021-02-20 10:19:07 -07008664#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
8665
8666
8667 // Rewind an indefnite length byte-string wrapped sequence
8668
Laurence Lundblade2a26abb2020-11-05 19:06:54 -08008669 return 0;
8670}
Laurence Lundblade9f9c3732021-03-23 09:38:46 -07008671
8672
8673
8674
8675static const uint8_t spBooleansInMap[] =
8676{
8677 0xa1, 0x08, 0xf5
8678};
8679
8680static const uint8_t spBooleansInMapWrongType[] =
8681{
8682 0xa1, 0x08, 0xf6
8683};
8684
8685static const uint8_t spBooleansInMapNWF[] =
8686{
8687 0xa1, 0x08, 0x1a
8688};
8689
Laurence Lundblade8782dd32021-04-27 04:15:37 -07008690static const uint8_t spNullInMap[] =
8691{
8692 0xa1, 0x08, 0xf6
8693};
8694
8695static const uint8_t spUndefinedInMap[] =
8696{
8697 0xa1, 0x08, 0xf7
8698};
8699
Laurence Lundblade9f9c3732021-03-23 09:38:46 -07008700
8701int32_t BoolTest(void)
8702{
8703 QCBORDecodeContext DCtx;
8704 bool b;
8705
Laurence Lundblade8782dd32021-04-27 04:15:37 -07008706 QCBORDecode_Init(&DCtx,
8707 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMap),
8708 0);
Laurence Lundblade9f9c3732021-03-23 09:38:46 -07008709 QCBORDecode_EnterMap(&DCtx, NULL);
8710 QCBORDecode_GetBool(&DCtx, &b);
8711 if(QCBORDecode_GetAndResetError(&DCtx) || !b) {
8712 return 1;
8713 }
8714
8715 QCBORDecode_GetBoolInMapN(&DCtx, 7, &b);
8716 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_LABEL_NOT_FOUND) {
8717 return 2;
8718 }
8719
8720 QCBORDecode_GetBoolInMapN(&DCtx, 8, &b);
8721 if(QCBORDecode_GetAndResetError(&DCtx) || !b) {
8722 return 3;
8723 }
8724
8725
8726 QCBORDecode_GetBoolInMapSZ(&DCtx, "xx", &b);
8727 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_LABEL_NOT_FOUND) {
8728 return 4;
8729 }
8730
Laurence Lundblade8782dd32021-04-27 04:15:37 -07008731 QCBORDecode_Init(&DCtx,
8732 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMapWrongType),
8733 0);
Laurence Lundblade9f9c3732021-03-23 09:38:46 -07008734 QCBORDecode_EnterMap(&DCtx, NULL);
8735 QCBORDecode_GetBool(&DCtx, &b);
8736 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
8737 return 5;
8738 }
8739
Laurence Lundblade8782dd32021-04-27 04:15:37 -07008740 QCBORDecode_Init(&DCtx,
8741 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMapNWF),
8742 0);
Laurence Lundblade9f9c3732021-03-23 09:38:46 -07008743 QCBORDecode_EnterMap(&DCtx, NULL);
8744 QCBORDecode_GetBool(&DCtx, &b);
8745 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_HIT_END) {
8746 return 6;
8747 }
8748
Laurence Lundblade8782dd32021-04-27 04:15:37 -07008749
8750 QCBORDecode_Init(&DCtx,
8751 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spNullInMap),
8752 0);
8753 QCBORDecode_EnterMap(&DCtx, NULL);
8754 QCBORDecode_GetNull(&DCtx);
8755 if(QCBORDecode_GetAndResetError(&DCtx)) {
8756 return 7;
8757 }
8758
8759 QCBORDecode_Init(&DCtx,
8760 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMap),
8761 0);
8762 QCBORDecode_EnterMap(&DCtx, NULL);
8763 QCBORDecode_GetNull(&DCtx);
8764 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
8765 return 8;
8766 }
8767
8768 QCBORDecode_Init(&DCtx,
8769 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spNullInMap),
8770 0);
8771 QCBORDecode_EnterMap(&DCtx, NULL);
8772 QCBORDecode_GetNullInMapN(&DCtx, 8);
8773 if(QCBORDecode_GetAndResetError(&DCtx)) {
8774 return 9;
8775 }
8776
8777 QCBORDecode_Init(&DCtx,
8778 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMap),
8779 0);
8780 QCBORDecode_EnterMap(&DCtx, NULL);
8781 QCBORDecode_GetNullInMapN(&DCtx, 8);
8782 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
8783 return 10;
8784 }
8785
8786 QCBORDecode_Init(&DCtx,
8787 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMapNWF),
8788 0);
8789 QCBORDecode_EnterMap(&DCtx, NULL);
8790 QCBORDecode_GetUndefined(&DCtx);
8791 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_HIT_END) {
8792 return 11;
8793 }
8794
8795 QCBORDecode_Init(&DCtx,
8796 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUndefinedInMap),
8797 0);
8798 QCBORDecode_EnterMap(&DCtx, NULL);
8799 QCBORDecode_GetUndefined(&DCtx);
8800 if(QCBORDecode_GetAndResetError(&DCtx)) {
8801 return 12;
8802 }
8803
8804 QCBORDecode_Init(&DCtx,
8805 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMap),
8806 0);
8807 QCBORDecode_EnterMap(&DCtx, NULL);
8808 QCBORDecode_GetUndefined(&DCtx);
8809 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
8810 return 13;
8811 }
8812
8813 QCBORDecode_Init(&DCtx,
8814 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUndefinedInMap),
8815 0);
8816 QCBORDecode_EnterMap(&DCtx, NULL);
8817 QCBORDecode_GetUndefinedInMapN(&DCtx, 8);
8818 if(QCBORDecode_GetAndResetError(&DCtx)) {
8819 return 14;
8820 }
8821
8822 QCBORDecode_Init(&DCtx,
8823 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMap),
8824 0);
8825 QCBORDecode_EnterMap(&DCtx, NULL);
8826 QCBORDecode_GetUndefinedInMapN(&DCtx, 8);
8827 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
8828 return 15;
8829 }
8830
8831 QCBORDecode_Init(&DCtx,
8832 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMapNWF),
8833 0);
8834 QCBORDecode_EnterMap(&DCtx, NULL);
8835 QCBORDecode_GetUndefined(&DCtx);
8836 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_HIT_END) {
8837 return 15;
8838 }
8839
Laurence Lundblade9f9c3732021-03-23 09:38:46 -07008840 return 0;
8841}
Laurence Lundbladef00b8be2024-03-08 10:34:33 -08008842
8843
8844int32_t
8845ErrorHandlingTests(void)
8846{
8847 QCBORDecodeContext DCtx;
8848 QCBORItem Item;
8849 QCBORError uError;
8850 int64_t integer;
8851
8852 /* Test QCBORDecode_SetError() */
8853 QCBORDecode_Init(&DCtx,
8854 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded),
8855 QCBOR_DECODE_MODE_NORMAL);
8856
8857 QCBORDecode_SetError(&DCtx, QCBOR_ERR_FIRST_USER_DEFINED);
8858
8859 QCBORDecode_VGetNext(&DCtx, &Item);
8860
8861 uError = QCBORDecode_GetError(&DCtx);
8862
8863 if(uError != QCBOR_ERR_FIRST_USER_DEFINED) {
8864 return -1;
8865 }
8866
8867 if(Item.uLabelType != QCBOR_TYPE_NONE ||
8868 Item.uDataType != QCBOR_TYPE_NONE) {
8869 return -2;
8870 }
8871
8872
8873 /* Test data type returned from previous error */
8874 QCBORDecode_Init(&DCtx,
8875 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded),
8876 QCBOR_DECODE_MODE_NORMAL);
8877 QCBORDecode_GetInt64(&DCtx, &integer);
8878 uError = QCBORDecode_GetError(&DCtx);
8879 if(uError != QCBOR_ERR_UNEXPECTED_TYPE) {
8880 return -3;
8881 }
8882
8883 QCBORDecode_VGetNext(&DCtx, &Item);
8884 if(Item.uLabelType != QCBOR_TYPE_NONE ||
8885 Item.uDataType != QCBOR_TYPE_NONE) {
8886 return -2;
8887 }
8888 uError = QCBORDecode_GetError(&DCtx);
8889 if(uError != QCBOR_ERR_UNEXPECTED_TYPE) {
8890 return -3;
8891 }
8892
8893
8894 /* Test error classification functions */
8895
8896 if(!QCBORDecode_IsUnrecoverableError(QCBOR_ERR_INDEFINITE_STRING_CHUNK)) {
8897 return -10;
8898 }
8899 if(QCBORDecode_IsUnrecoverableError(QCBOR_SUCCESS)) {
8900 return -11;
8901 }
8902 if(!QCBORDecode_IsUnrecoverableError(QCBOR_ERR_INDEFINITE_STRING_CHUNK)) {
8903 return -12;
8904 }
8905 if(QCBORDecode_IsUnrecoverableError(QCBOR_ERR_DUPLICATE_LABEL)) {
8906 return -13;
8907 }
8908
8909 if(!QCBORDecode_IsNotWellFormedError(QCBOR_ERR_BAD_TYPE_7)) {
8910 return -20;
8911 }
8912 if(!QCBORDecode_IsNotWellFormedError(QCBOR_ERR_BAD_BREAK)) {
8913 return -21;
8914 }
8915 if(QCBORDecode_IsNotWellFormedError(QCBOR_SUCCESS)) {
8916 return -22;
8917 }
8918 if(QCBORDecode_IsNotWellFormedError(QCBOR_ERR_ARRAY_DECODE_TOO_LONG)) {
8919 return -23;
8920 }
8921
8922 return 0;
8923}
Laurence Lundblade11654912024-05-09 11:49:24 -07008924
8925
8926int32_t TellTests(void)
8927{
8928 QCBORDecodeContext DCtx;
8929 QCBORItem Item;
8930 uint32_t uPosition;
8931 int nIndex;
8932 int64_t nDecodedInt;
8933
8934 static const uint32_t aPos[] =
8935 {0, 1, 17, 42, 50, 58, 72, 85, 98, 112, UINT32_MAX};
8936 QCBORDecode_Init(&DCtx,
8937 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded),
8938 0);
8939 for(nIndex = 0; ; nIndex++) {
8940 uPosition = QCBORDecode_Tell(&DCtx);
8941 if(uPosition != aPos[nIndex]) {
8942 return nIndex;
8943 }
8944
8945 if(uPosition == UINT32_MAX) {
8946 break;
8947 }
8948
8949 QCBORDecode_VGetNext(&DCtx, &Item);
8950 }
8951
8952#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
8953 static const uint32_t aPosIndef[] =
8954 {0, 1, 17, 42, 50, 59, 73, 86, 99, 113, UINT32_MAX};
8955 QCBORDecode_Init(&DCtx,
8956 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapIndefEncoded),
8957 0);
8958
8959 for(nIndex = 0; ; nIndex++) {
8960 uPosition = QCBORDecode_Tell(&DCtx);
8961 if(uPosition != aPosIndef[nIndex]) {
8962 return nIndex + 100;
8963 }
8964
8965 if(uPosition == UINT32_MAX) {
8966 break;
8967 }
8968
8969 QCBORDecode_VGetNext(&DCtx, &Item);
8970 }
8971#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
8972
8973 /* Next, some tests with entered maps and arrays */
8974 QCBORDecode_Init(&DCtx,
8975 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded),
8976 0);
8977 QCBORDecode_EnterMap(&DCtx, &Item);
8978 if(QCBORDecode_Tell(&DCtx) != 1) {
8979 return 1001;
8980 }
8981 QCBORDecode_GetInt64InMapSZ(&DCtx, "first integer", &nDecodedInt);
8982 if(QCBORDecode_Tell(&DCtx) != 1) {
8983 return 1002;
8984 }
8985 QCBORDecode_EnterMapFromMapSZ(&DCtx, "map in a map");
8986 if(QCBORDecode_Tell(&DCtx) != 72) {
8987 return 1003;
8988 }
8989
8990 QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt);
8991 if(nDecodedInt != 98) {
8992 return 1004;
8993 }
8994 /* Getting non-aggregate types doesn't affect cursor position. */
8995 if(QCBORDecode_Tell(&DCtx) != 72) {
8996 return 1005;
8997 }
8998 QCBORDecode_VGetNext(&DCtx, &Item);
8999 if(QCBORDecode_Tell(&DCtx) != 85) {
9000 return 1006;
9001 }
9002 QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt);
9003 if(nDecodedInt != 98) {
9004 return 1007;
9005 }
9006 /* Getting non-aggregate types doesn't affect cursor position. */
9007 if(QCBORDecode_Tell(&DCtx) != 85) {
9008 return 1008;
9009 }
9010
9011 QCBORDecode_ExitMap(&DCtx);
9012 if(QCBORDecode_Tell(&DCtx) != UINT32_MAX) {
9013 return 1009;
9014 }
9015 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_NO_MORE_ITEMS) {
9016 return 1010;
9017 }
9018
9019#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
9020 /* Next, some tests with entered maps and arrays */
9021 QCBORDecode_Init(&DCtx,
9022 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapIndefEncoded),
9023 0);
9024 QCBORDecode_EnterMap(&DCtx, &Item);
9025 if(QCBORDecode_Tell(&DCtx) != 1) {
9026 return 2000;
9027 }
9028 QCBORDecode_GetInt64InMapSZ(&DCtx, "first integer", &nDecodedInt);
9029 if(QCBORDecode_Tell(&DCtx) != 1) {
9030 return 2001;
9031 }
9032 QCBORDecode_EnterMapFromMapSZ(&DCtx, "map in a map");
9033 if(QCBORDecode_Tell(&DCtx) != 73) {
9034 return 2002;
9035 }
9036
9037 QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt);
9038 if(nDecodedInt != 98) {
9039 return 2003;
9040 }
9041 /* Getting non-aggregate types doesn't affect cursor position. */
9042 if(QCBORDecode_Tell(&DCtx) != 73) {
9043 return 2004;
9044 }
9045 QCBORDecode_VGetNext(&DCtx, &Item);
9046 if(QCBORDecode_Tell(&DCtx) != 86) {
9047 return 2005;
9048 }
9049 QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt);
9050 if(nDecodedInt != 98) {
9051 return 2006;
9052 }
9053 /* Getting non-aggregate types doesn't affect cursor position. */
9054 if(QCBORDecode_Tell(&DCtx) != 86) {
9055 return 2007;
9056 }
9057
9058 QCBORDecode_ExitMap(&DCtx);
9059 if(QCBORDecode_Tell(&DCtx) != UINT32_MAX) {
9060 return 2008;
9061 }
9062 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_NO_MORE_ITEMS) {
9063 return 2010;
9064 }
9065#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
9066
9067
9068
9069 /* Error state test */
9070 QCBORDecode_Init(&DCtx,
9071 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded),
9072 0);
9073 /* Cause an error */
9074 QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt);
9075 if(QCBORDecode_Tell(&DCtx) != UINT32_MAX) {
9076 return 3000;
9077 }
9078
9079 /* Empties tests */
9080 const uint8_t pMinimalCBOR[] = {0xa0}; // One empty map
9081 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pMinimalCBOR),0);
9082 if(QCBORDecode_Tell(&DCtx) != 0) {
9083 return 4000;
9084 }
9085 QCBORDecode_EnterMap(&DCtx, &Item);
9086 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) {
9087 return 4001;
9088 }
9089 if(QCBORDecode_Tell(&DCtx) != UINT32_MAX) {
9090 return 4002;
9091 }
9092 QCBORDecode_ExitMap(&DCtx);
9093 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) {
9094 return 4001;
9095 }
9096 if(QCBORDecode_Tell(&DCtx) != UINT32_MAX) {
9097 return 4002;
9098 }
9099 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_NO_MORE_ITEMS) {
9100 return 4010;
9101 }
9102
9103#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
9104 const uint8_t pMinimalIndefCBOR[] = {0xbf, 0xff}; // One empty map
9105 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pMinimalIndefCBOR),0);
9106 if(QCBORDecode_Tell(&DCtx) != 0) {
9107 return 4100;
9108 }
9109 QCBORDecode_EnterMap(&DCtx, &Item);
9110 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) {
9111 return 4101;
9112 }
9113 if(QCBORDecode_Tell(&DCtx) != UINT32_MAX) {
9114 return 4102;
9115 }
9116 QCBORDecode_ExitMap(&DCtx);
9117 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) {
9118 return 4101;
9119 }
9120 if(QCBORDecode_Tell(&DCtx) != UINT32_MAX) {
9121 return 4102;
9122 }
9123 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_NO_MORE_ITEMS) {
9124 return 4110;
9125 }
9126#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
9127
9128 /* Test on a CBOR sequence */
9129 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSequenceTestInput),0);
9130 if(QCBORDecode_Tell(&DCtx) != 0) {
9131 return 5000;
9132 }
9133 QCBORDecode_VGetNext(&DCtx, &Item);
9134 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) {
9135 return 5001;
9136 }
9137 if(QCBORDecode_Tell(&DCtx) != 11) {
9138 return 5002;
9139 }
9140 QCBORDecode_VGetNext(&DCtx, &Item);
9141 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) {
9142 return 5003;
9143 }
9144 if(QCBORDecode_Tell(&DCtx) != 12) {
9145 return 5004;
9146 }
9147 QCBORDecode_VGetNext(&DCtx, &Item);
9148 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) {
9149 return 5005;
9150 }
9151 if(QCBORDecode_Tell(&DCtx) != 17) {
9152 return 5006;
9153 }
9154 QCBORDecode_VGetNext(&DCtx, &Item);
9155 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) {
9156 return 5007;
9157 }
9158 if(QCBORDecode_Tell(&DCtx) != UINT32_MAX) {
9159 return 5008;
9160 }
9161 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_NO_MORE_ITEMS) {
9162 return 5010;
9163 }
9164
9165
9166 QCBORDecode_Init(&DCtx,
9167 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded),
9168 0);
9169 QCBORDecode_EnterMap(&DCtx, &Item);
9170 QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings");
9171 if(QCBORDecode_Tell(&DCtx) != 42) {
9172 return 6001;
9173 }
9174 QCBORDecode_VGetNext(&DCtx, &Item);
9175 if(QCBORDecode_Tell(&DCtx) != 50) {
9176 return 6002;
9177 }
9178 QCBORDecode_VGetNext(&DCtx, &Item);
9179 if(QCBORDecode_Tell(&DCtx) != 58) {
9180 return 6008;
9181 }
9182 QCBORDecode_VGetNext(&DCtx, &Item);
9183 (void)QCBORDecode_GetAndResetError(&DCtx);
9184 if(QCBORDecode_Tell(&DCtx) != 58) {
9185 return 6003;
9186 }
9187 QCBORDecode_ExitArray(&DCtx);
9188 if(QCBORDecode_Tell(&DCtx) != 58) {
9189 return 6004;
9190 }
9191
9192 static const uint32_t aEmptiesPos[] =
9193 {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 11, 13, UINT32_MAX};
9194 QCBORDecode_Init(&DCtx,
9195 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(sEmpties),
9196 0);
9197 for(nIndex = 0; ; nIndex++) {
9198 uPosition = QCBORDecode_Tell(&DCtx);
9199 if(uPosition != aEmptiesPos[nIndex]) {
9200 return nIndex + 200;
9201 }
9202
9203 if(uPosition == UINT32_MAX) {
9204 break;
9205 }
9206
9207 QCBORDecode_VGetNext(&DCtx, &Item);
9208 }
9209
9210#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
9211 static const uint32_t aIndefEmptiesPos[] =
9212 {0, 1, 2, 4, 5, 7, 8, 10, 12, 13, 16, 19, UINT32_MAX};
9213 QCBORDecode_Init(&DCtx,
9214 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(sEmptiesIndef),
9215 0);
9216 for(nIndex = 0; ; nIndex++) {
9217 uPosition = QCBORDecode_Tell(&DCtx);
9218 if(uPosition != aIndefEmptiesPos[nIndex]) {
9219 return nIndex + 300;
9220 }
9221
9222 if(uPosition == UINT32_MAX) {
9223 break;
9224 }
9225
9226 QCBORDecode_VGetNext(&DCtx, &Item);
9227 }
9228#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
9229
9230
9231 return 0;
9232}