blob: 16ab10f5cfcb6a5a376a32ea74a1480164562738 [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;
5999
6000 int i;
6001 for(i = 0; i < 13; i++) {
6002 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spNested), 0);
6003 QCBORDecode_EnterMap(&DCtx, NULL);
6004 int j;
6005 /* Move travesal cursor */
6006 for(j = 0; j < i; j++) {
6007 QCBORDecode_GetNext(&DCtx, &Item1);
6008 }
6009 QCBORDecode_EnterMapFromMapN(&DCtx, 2);
6010 QCBORDecode_ExitMap(&DCtx);
6011 QCBORDecode_GetNext(&DCtx, &Item1);
6012 if(Item1.label.int64 != 3) {
6013 return 8000;
6014 }
6015 }
6016
6017 for(i = 0; i < 13; i++) {
6018 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spNested), 0);
6019 QCBORDecode_EnterMap(&DCtx, NULL);
6020 int j;
6021 /* Move travesal cursor */
6022 for(j = 0; j < i; j++) {
6023 QCBORDecode_GetNext(&DCtx, &Item1);
6024 }
6025 QCBORDecode_EnterMapFromMapN(&DCtx, 2);
6026 QCBORDecode_EnterMapFromMapN(&DCtx, 22);
6027 QCBORDecode_ExitMap(&DCtx);
6028 QCBORDecode_GetNext(&DCtx, &Item1);
6029 if(Item1.label.int64 != 23) {
6030 return 8000;
6031 }
6032 }
6033
6034 for(i = 0; i < 13; i++) {
6035 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spNested), 0);
6036 QCBORDecode_EnterMap(&DCtx, NULL);
6037 int j;
6038 /* Move travesal cursor */
6039 for(j = 0; j < i; j++) {
6040 QCBORDecode_GetNext(&DCtx, &Item1);
6041 }
6042 QCBORDecode_EnterMapFromMapN(&DCtx, 2);
6043 QCBORDecode_EnterMapFromMapN(&DCtx, 22);
6044 for(j = 0; j < i; j++) {
6045 QCBORDecode_GetNext(&DCtx, &Item1);
6046 }
6047 QCBORDecode_EnterArrayFromMapN(&DCtx, 221);
6048 QCBORDecode_ExitArray(&DCtx);
6049 QCBORDecode_ExitMap(&DCtx);
6050 QCBORDecode_GetNext(&DCtx, &Item1);
6051 if(Item1.label.int64 != 23) {
6052 return 8000;
6053 }
6054 QCBORDecode_ExitMap(&DCtx);
6055 QCBORDecode_GetNext(&DCtx, &Item1);
6056 if(Item1.label.int64 != 3) {
6057 return 8000;
6058 }
6059 }
6060
6061 return 0;
6062}
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07006063
6064
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03006065int32_t EnterMapTest(void)
Laurence Lundbladebb87be22020-04-09 19:15:32 -07006066{
Laurence Lundbladef0499502020-08-01 11:55:57 -07006067 QCBORItem Item1;
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006068 QCBORItem ArrayItem;
Laurence Lundbladebb87be22020-04-09 19:15:32 -07006069 QCBORDecodeContext DCtx;
Laurence Lundbladef0499502020-08-01 11:55:57 -07006070 int32_t nReturn;
6071 QCBORError uErr;
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07006072
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08006073#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07006074 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spMapOfEmpty), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006075 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07006076
Laurence Lundbladef0499502020-08-01 11:55:57 -07006077
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006078 QCBORDecode_EnterArray(&DCtx, NULL); // Label 0
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07006079 QCBORDecode_ExitArray(&DCtx);
6080
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006081 QCBORDecode_EnterArray(&DCtx, NULL); // Label 9
6082 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07006083 QCBORDecode_ExitArray(&DCtx);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006084 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07006085 QCBORDecode_ExitArray(&DCtx);
6086 QCBORDecode_ExitArray(&DCtx);
6087
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006088 QCBORDecode_EnterMap(&DCtx, NULL); // Label 8
6089 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07006090 QCBORDecode_ExitArray(&DCtx);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006091 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07006092 QCBORDecode_ExitMap(&DCtx);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006093 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07006094 QCBORDecode_ExitArray(&DCtx);
6095 QCBORDecode_ExitMap(&DCtx);
6096
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006097 QCBORDecode_EnterMap(&DCtx, NULL); // Label4
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07006098 QCBORDecode_ExitMap(&DCtx);
6099
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006100 QCBORDecode_EnterArray(&DCtx, NULL); // Label 5
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07006101 QCBORDecode_ExitArray(&DCtx);
6102
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006103 QCBORDecode_EnterArray(&DCtx, NULL); // Label 6
6104 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07006105 QCBORDecode_ExitArray(&DCtx);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006106 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07006107 QCBORDecode_ExitArray(&DCtx);
6108 QCBORDecode_ExitArray(&DCtx);
6109
6110 QCBORDecode_ExitMap(&DCtx);
6111
6112 uErr = QCBORDecode_Finish(&DCtx);
6113 if(uErr != QCBOR_SUCCESS){
6114 return 3011;
6115 }
6116
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07006117 (void)pValidMapIndefEncoded;
Laurence Lundbladef7a70bc2020-10-24 12:23:25 -07006118 nReturn = SpiffyDecodeBasicMap(UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapIndefEncoded));
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07006119 if(nReturn) {
6120 return nReturn + 20000;
Laurence Lundbladebb87be22020-04-09 19:15:32 -07006121 }
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08006122#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
6123
Laurence Lundbladebb87be22020-04-09 19:15:32 -07006124
Laurence Lundbladef7a70bc2020-10-24 12:23:25 -07006125 nReturn = SpiffyDecodeBasicMap(UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded));
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006126 if(nReturn) {
6127 return nReturn;
6128 }
Laurence Lundbladebb87be22020-04-09 19:15:32 -07006129
Laurence Lundblade8ffdb742020-05-07 02:49:18 -07006130
Laurence Lundblade937ea812020-05-08 11:38:23 -07006131
Laurence Lundblade2f467f92020-10-09 17:50:11 -07006132 // These tests confirm the cursor is at the right place after entering
6133 // a map or array
Laurence Lundblade9b334962020-08-27 10:55:53 -07006134 const UsefulBufC ValidEncodedMap = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded);
Laurence Lundblade937ea812020-05-08 11:38:23 -07006135
6136 // Confirm cursor is at right place
Laurence Lundblade9b334962020-08-27 10:55:53 -07006137 QCBORDecode_Init(&DCtx, ValidEncodedMap, 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006138 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade937ea812020-05-08 11:38:23 -07006139 QCBORDecode_GetNext(&DCtx, &Item1);
6140 if(Item1.uDataType != QCBOR_TYPE_INT64) {
6141 return 2001;
6142 }
6143
6144
Laurence Lundblade9b334962020-08-27 10:55:53 -07006145 QCBORDecode_Init(&DCtx, ValidEncodedMap, 0);
Laurence Lundblade6f3f78e2020-08-31 13:09:14 -07006146 QCBORDecode_VGetNext(&DCtx, &Item1);
6147 QCBORDecode_VGetNext(&DCtx, &Item1);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006148 QCBORDecode_EnterArray(&DCtx, &ArrayItem);
6149 if(ArrayItem.uLabelType != QCBOR_TYPE_TEXT_STRING ||
6150 UsefulBuf_Compare(ArrayItem.label.string,
6151 UsefulBuf_FROM_SZ_LITERAL("an array of two strings"))) {
6152 return 2051;
6153 }
Laurence Lundblade937ea812020-05-08 11:38:23 -07006154 QCBORDecode_GetNext(&DCtx, &Item1);
6155 if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING) {
6156 return 2002;
6157 }
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006158 QCBORDecode_ExitArray(&DCtx);
6159 QCBORDecode_EnterMap(&DCtx, &ArrayItem);
6160 if(ArrayItem.uLabelType != QCBOR_TYPE_TEXT_STRING ||
6161 UsefulBuf_Compare(ArrayItem.label.string,
6162 UsefulBuf_FROM_SZ_LITERAL("map in a map"))) {
6163 return 2052;
6164 }
6165
Laurence Lundblade937ea812020-05-08 11:38:23 -07006166
Laurence Lundblade9b334962020-08-27 10:55:53 -07006167 QCBORDecode_Init(&DCtx, ValidEncodedMap, 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006168 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade64b607e2020-05-13 13:05:57 -07006169 QCBORDecode_GetNext(&DCtx, &Item1);
6170 QCBORDecode_GetNext(&DCtx, &Item1);
6171 QCBORDecode_GetNext(&DCtx, &Item1);
Laurence Lundblade937ea812020-05-08 11:38:23 -07006172 QCBORDecode_EnterMapFromMapSZ(&DCtx, "map in a map");
6173 QCBORDecode_GetNext(&DCtx, &Item1);
6174 if(Item1.uDataType != QCBOR_TYPE_BYTE_STRING) {
6175 return 2003;
6176 }
6177
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 Lundblade937ea812020-05-08 11:38:23 -07006180 QCBORDecode_GetNext(&DCtx, &Item1);
6181 QCBORDecode_GetNext(&DCtx, &Item1);
6182 QCBORDecode_GetNext(&DCtx, &Item1);
6183 QCBORDecode_GetNext(&DCtx, &Item1);
6184 QCBORDecode_GetNext(&DCtx, &Item1);
6185 QCBORDecode_GetNext(&DCtx, &Item1);
6186 QCBORDecode_GetNext(&DCtx, &Item1);
6187 QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings");
6188 QCBORDecode_GetNext(&DCtx, &Item1);
6189 if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING) {
Laurence Lundblade64b607e2020-05-13 13:05:57 -07006190 return 2004;
Laurence Lundblade937ea812020-05-08 11:38:23 -07006191 }
6192
Laurence Lundblade9b334962020-08-27 10:55:53 -07006193 QCBORDecode_Init(&DCtx, ValidEncodedMap, 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006194 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade2b843b52020-06-16 20:51:03 -07006195 QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings");
6196 QCBORDecode_ExitArray(&DCtx);
6197 QCBORDecode_GetNext(&DCtx, &Item1);
6198 if(Item1.uDataType != QCBOR_TYPE_MAP && Item1.uLabelAlloc != QCBOR_TYPE_TEXT_STRING) {
6199 return 2006;
6200 }
6201 QCBORDecode_ExitMap(&DCtx);
6202 if(QCBORDecode_GetNext(&DCtx, &Item1) != QCBOR_ERR_NO_MORE_ITEMS) {
6203 return 2007;
6204 }
6205
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07006206 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSimpleArray), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006207 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07006208 int64_t nDecodedInt2;
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07006209 QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2);
6210 uErr = QCBORDecode_GetAndResetError(&DCtx);
6211 if(uErr != QCBOR_ERR_MAP_NOT_ENTERED){
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07006212 return 2008;
6213 }
6214 UsefulBufC String;
Laurence Lundblade323f8a92020-09-06 19:43:09 -07006215 QCBORDecode_GetTextStringInMapN(&DCtx, 88, &String);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07006216 if(uErr != QCBOR_ERR_MAP_NOT_ENTERED){
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07006217 return 2009;
6218 }
Laurence Lundblade937ea812020-05-08 11:38:23 -07006219
Laurence Lundbladee6f15112020-07-23 18:44:16 -07006220
6221 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEmptyMap), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006222 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundbladee6f15112020-07-23 18:44:16 -07006223 // This will fail because the map is empty.
6224 QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2);
6225 uErr = QCBORDecode_GetAndResetError(&DCtx);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006226 if(uErr != QCBOR_ERR_LABEL_NOT_FOUND){
Laurence Lundbladee6f15112020-07-23 18:44:16 -07006227 return 2010;
6228 }
6229 QCBORDecode_ExitMap(&DCtx);
6230 uErr = QCBORDecode_Finish(&DCtx);
6231 if(uErr != QCBOR_SUCCESS){
6232 return 2011;
6233 }
6234
6235
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08006236#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundbladee6f15112020-07-23 18:44:16 -07006237 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEmptyInDefinteLengthMap), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006238 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundbladee6f15112020-07-23 18:44:16 -07006239 // This will fail because the map is empty.
6240 QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2);
6241 uErr = QCBORDecode_GetAndResetError(&DCtx);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006242 if(uErr != QCBOR_ERR_LABEL_NOT_FOUND){
Laurence Lundblade085d7952020-07-24 10:26:30 -07006243 return 2012;
Laurence Lundbladee6f15112020-07-23 18:44:16 -07006244 }
6245 QCBORDecode_ExitMap(&DCtx);
6246 uErr = QCBORDecode_Finish(&DCtx);
6247 if(uErr != QCBOR_SUCCESS){
Laurence Lundblade085d7952020-07-24 10:26:30 -07006248 return 2013;
Laurence Lundbladee6f15112020-07-23 18:44:16 -07006249 }
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08006250#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
Laurence Lundbladee6f15112020-07-23 18:44:16 -07006251
Laurence Lundblade5f4e8712020-07-25 11:44:43 -07006252
6253 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spArrayOfEmpty), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006254 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade323f8a92020-09-06 19:43:09 -07006255 QCBORDecode_GetByteString(&DCtx, &String);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006256 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade5f4e8712020-07-25 11:44:43 -07006257 QCBORDecode_ExitMap(&DCtx);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006258 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade5f4e8712020-07-25 11:44:43 -07006259 QCBORDecode_ExitArray(&DCtx);
6260 QCBORDecode_GetInt64(&DCtx, &nDecodedInt2);
6261 QCBORDecode_ExitArray(&DCtx);
6262 uErr = QCBORDecode_Finish(&DCtx);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07006263 if(uErr != QCBOR_SUCCESS) {
Laurence Lundblade5f4e8712020-07-25 11:44:43 -07006264 return 2014;
6265 }
6266
Laurence Lundblade93d3f532020-09-28 21:09:12 -07006267 int64_t nInt;
6268 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spRecoverableMapErrors), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006269 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade37286c02022-09-03 10:05:02 -07006270#ifndef QCBOR_DISABLE_TAGS
Laurence Lundblade93d3f532020-09-28 21:09:12 -07006271 QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt);
Laurence Lundblade88e9db22020-11-02 03:56:33 -08006272 uErr = QCBORDecode_GetError(&DCtx);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07006273 if(uErr != QCBOR_ERR_TOO_MANY_TAGS) {
6274 return 2021;
6275 }
Laurence Lundblade88e9db22020-11-02 03:56:33 -08006276 if(QCBORDecode_GetNthTagOfLast(&DCtx, 0) != CBOR_TAG_INVALID64) {
6277 return 2121;
6278 }
6279 (void)QCBORDecode_GetAndResetError(&DCtx);
Laurence Lundblade37286c02022-09-03 10:05:02 -07006280#endif
Laurence Lundblade88e9db22020-11-02 03:56:33 -08006281
Laurence Lundblade93d3f532020-09-28 21:09:12 -07006282
Laurence Lundblade93d3f532020-09-28 21:09:12 -07006283 QCBORDecode_GetInt64InMapN(&DCtx, 0x03, &nInt);
6284 uErr = QCBORDecode_GetAndResetError(&DCtx);
6285 if(uErr != QCBOR_ERR_INT_OVERFLOW) {
6286 return 2023;
6287 }
6288
Laurence Lundblade37286c02022-09-03 10:05:02 -07006289#ifndef QCBOR_DISABLE_TAGS
Laurence Lundblade93d3f532020-09-28 21:09:12 -07006290 QCBORDecode_GetEpochDateInMapN(&DCtx, 0x04, QCBOR_TAG_REQUIREMENT_TAG, &nInt);
6291 uErr = QCBORDecode_GetAndResetError(&DCtx);
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006292 if(uErr != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_DATE_OVERFLOW)) {
Laurence Lundblade93d3f532020-09-28 21:09:12 -07006293 return 2024;
6294 }
Laurence Lundblade37286c02022-09-03 10:05:02 -07006295#endif
Laurence Lundblade93d3f532020-09-28 21:09:12 -07006296
6297 QCBORDecode_GetInt64InMapN(&DCtx, 0x05, &nInt);
6298 uErr = QCBORDecode_GetAndResetError(&DCtx);
6299 if(uErr != QCBOR_ERR_DUPLICATE_LABEL) {
6300 return 2025;
6301 }
6302
6303 QCBORDecode_GetInt64InMapN(&DCtx, 0x08, &nInt);
6304
6305 QCBORDecode_ExitMap(&DCtx);
6306 uErr = QCBORDecode_Finish(&DCtx);
6307 if(uErr != QCBOR_SUCCESS) {
6308 return 2026;
6309 }
6310
6311 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUnRecoverableMapError1), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006312 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07006313 QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt);
6314 uErr = QCBORDecode_GetAndResetError(&DCtx);
6315 if(uErr != QCBOR_ERR_BAD_BREAK) {
6316 return 2030;
6317 }
6318
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08006319#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundblade93d3f532020-09-28 21:09:12 -07006320 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUnRecoverableMapError2), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006321 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07006322 QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt);
6323 uErr = QCBORDecode_GetAndResetError(&DCtx);
6324 if(uErr != QCBOR_ERR_NO_MORE_ITEMS) {
6325 return 2031;
6326 }
6327
6328 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUnRecoverableMapError3), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006329 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07006330 QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt);
6331 uErr = QCBORDecode_GetAndResetError(&DCtx);
6332 if(uErr != QCBOR_ERR_HIT_END) {
6333 return 2032;
6334 }
6335
6336 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUnRecoverableMapError4), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006337 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07006338 QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt);
6339 uErr = QCBORDecode_GetAndResetError(&DCtx);
6340 if(uErr != QCBOR_ERR_ARRAY_DECODE_NESTING_TOO_DEEP) {
6341 return 2033;
6342 }
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08006343#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
6344
Laurence Lundblade732e52d2021-02-22 20:11:01 -07006345 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0);
6346 QCBORDecode_VGetNextConsume(&DCtx, &Item1);
6347 if(Item1.uDataType != QCBOR_TYPE_MAP) {
6348 return 2401;
6349 }
6350 if(QCBORDecode_GetError(&DCtx)) {
6351 return 2402;
6352 }
6353
6354 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0);
6355 QCBORDecode_VGetNext(&DCtx, &Item1);
6356 if(Item1.uDataType != QCBOR_TYPE_MAP ||
6357 Item1.val.uCount != 3 ||
6358 Item1.uNextNestLevel != 1) {
6359 return 2403;
6360 }
6361 if(QCBORDecode_GetError(&DCtx)) {
6362 return 2404;
6363 }
6364 QCBORDecode_VGetNextConsume(&DCtx, &Item1);
6365 if(Item1.uDataType != QCBOR_TYPE_INT64 ||
6366 Item1.uNextNestLevel != 1 ||
6367 Item1.val.int64 != 42) {
6368 return 2405;
6369 }
6370 if(QCBORDecode_GetError(&DCtx)) {
6371 return 2406;
6372 }
6373 QCBORDecode_VGetNextConsume(&DCtx, &Item1);
6374 if(Item1.uDataType != QCBOR_TYPE_ARRAY ||
6375 Item1.uNestingLevel != 1 ||
6376 Item1.uNextNestLevel != 1 ||
6377 Item1.val.uCount != 2) {
6378 return 2407;
6379 }
6380 if(QCBORDecode_GetError(&DCtx)) {
6381 return 2408;
6382 }
6383 QCBORDecode_VGetNextConsume(&DCtx, &Item1);
6384 if(Item1.uDataType != QCBOR_TYPE_MAP ||
6385 Item1.uNestingLevel != 1 ||
6386 Item1.uNextNestLevel != 0 ||
6387 Item1.val.uCount != 4) {
6388 return 2409;
6389 }
6390 if(QCBORDecode_GetError(&DCtx)) {
6391 return 2410;
6392 }
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006393
Laurence Lundbladeddebabe2020-11-22 11:32:17 -08006394 nReturn = DecodeNestedIterate();
6395
Laurence Lundblade63926052021-03-29 16:05:51 -07006396
6397 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(not_well_formed_submod_section), 0);
6398 QCBORDecode_EnterMap(&DCtx, NULL);
6399 QCBORDecode_EnterMapFromMapN(&DCtx, 20);
6400 if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_BAD_INT) {
6401 return 2500;
6402 }
6403
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07006404 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBadConsumeInput), 0);
6405 QCBORDecode_VGetNextConsume(&DCtx, &Item1);
6406 if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_NO_MORE_ITEMS) {
6407 return 2600;
6408 }
6409
Laurence Lundblade37286c02022-09-03 10:05:02 -07006410#ifndef QCBOR_DISABLE_TAGS
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07006411 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBadConsumeInput2), 0);
6412 QCBORDecode_VGetNextConsume(&DCtx, &Item1);
6413 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) {
6414 return 2700;
6415 }
6416
6417 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBadConsumeInput3), 0);
6418 QCBORDecode_VGetNextConsume(&DCtx, &Item1);
6419 if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT) {
6420 return 2800;
6421 }
Laurence Lundblade37286c02022-09-03 10:05:02 -07006422#endif
6423
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07006424
6425 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBadConsumeInput4), 0);
6426 QCBORDecode_VGetNextConsume(&DCtx, &Item1);
6427#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
6428 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) {
6429 return 2900;
6430 }
6431#else
6432 if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_INDEF_LEN_ARRAYS_DISABLED) {
6433 return 2901;
6434 }
6435#endif
6436
6437 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBadConsumeInput5), 0);
6438 QCBORDecode_VGetNextConsume(&DCtx, &Item1);
6439 if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_MAP_LABEL_TYPE) {
6440 return 3000;
6441 }
6442
Laurence Lundbladec5f45e42023-12-18 09:23:20 -07006443 nReturn = EnterMapCursorTest();
6444
Laurence Lundbladeddebabe2020-11-22 11:32:17 -08006445 return nReturn;
Laurence Lundblade9c905e82020-04-25 11:31:38 -07006446}
6447
6448
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006449struct NumberConversion {
6450 char *szDescription;
6451 UsefulBufC CBOR;
6452 int64_t nConvertedToInt64;
6453 QCBORError uErrorInt64;
6454 uint64_t uConvertToUInt64;
6455 QCBORError uErrorUint64;
6456 double dConvertToDouble;
6457 QCBORError uErrorDouble;
6458};
6459
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006460#ifndef QCBOR_DISABLE_EXP_AND_MANTISSA
6461#define EXP_AND_MANTISSA_ERROR(x) x
6462#else
6463#define EXP_AND_MANTISSA_ERROR(x) QCBOR_ERR_UNEXPECTED_TYPE
6464#endif
6465
6466
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07006467static const struct NumberConversion NumberConversions[] = {
Laurence Lundblade37286c02022-09-03 10:05:02 -07006468#ifndef QCBOR_DISABLE_TAGS
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006469 {
Laurence Lundblade4e808ba2022-12-29 12:45:20 -07006470 "Big float: INT64_MIN * 2e-1 to test handling of INT64_MIN",
6471 {(uint8_t[]){0xC5, 0x82, 0x20,
6472 0x3B, 0x7f, 0xff, 0xff, 0xff, 0xff, 0x0ff, 0xff, 0xff,
6473 }, 15},
6474 -4611686018427387904, /* INT64_MIN / 2 */
6475 EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS),
6476 0,
6477 EXP_AND_MANTISSA_ERROR(QCBOR_ERR_NUMBER_SIGN_CONVERSION),
6478 -4.6116860184273879E+18,
6479 FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS))
6480 },
6481 {
Laurence Lundblade784b54b2020-08-10 01:24:52 -07006482 "too large to fit into int64_t",
6483 {(uint8_t[]){0xc3, 0x48, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 10},
6484 0,
6485 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
6486 0,
6487 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
6488 ((double)INT64_MIN) + 1 ,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006489 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)
Laurence Lundblade784b54b2020-08-10 01:24:52 -07006490 },
6491 {
6492 "largest negative int that fits in int64_t",
6493 {(uint8_t[]){0xc3, 0x48, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, 10},
6494 INT64_MIN,
6495 QCBOR_SUCCESS,
6496 0,
6497 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
6498 (double)INT64_MIN,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006499 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)
Laurence Lundblade784b54b2020-08-10 01:24:52 -07006500 },
6501 {
Laurence Lundbladeda095972020-06-06 18:35:33 -07006502 "negative bignum -1",
6503 {(uint8_t[]){0xc3, 0x41, 0x00}, 3},
6504 -1,
6505 QCBOR_SUCCESS,
6506 0,
6507 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
6508 -1.0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006509 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)
Laurence Lundbladeda095972020-06-06 18:35:33 -07006510 },
6511 {
Laurence Lundblade9ab5abb2020-05-20 12:10:45 -07006512 "Decimal Fraction with positive bignum 257 * 10e3",
Laurence Lundblade887add82020-05-17 05:50:34 -07006513 {(uint8_t[]){0xC4, 0x82, 0x1B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03,
6514 0xC2, 0x42, 0x01, 0x01}, 15},
6515 257000,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006516 EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS),
Laurence Lundblade887add82020-05-17 05:50:34 -07006517 257000,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006518 EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS),
Laurence Lundblade887add82020-05-17 05:50:34 -07006519 257000.0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006520 FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS))
Laurence Lundblade887add82020-05-17 05:50:34 -07006521 },
6522 {
Laurence Lundbladeda095972020-06-06 18:35:33 -07006523 "bigfloat with negative bignum -258 * 2e3",
Laurence Lundblade887add82020-05-17 05:50:34 -07006524 {(uint8_t[]){0xC5, 0x82, 0x1B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03,
6525 0xC3, 0x42, 0x01, 0x01}, 15},
Laurence Lundbladeda095972020-06-06 18:35:33 -07006526 -2064,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006527 EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS),
Laurence Lundblade887add82020-05-17 05:50:34 -07006528 0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006529 EXP_AND_MANTISSA_ERROR(QCBOR_ERR_NUMBER_SIGN_CONVERSION),
Laurence Lundbladeda095972020-06-06 18:35:33 -07006530 -2064.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 Lundblade9ab5abb2020-05-20 12:10:45 -07006534 "bigfloat with positive bignum 257 * 2e3",
Laurence Lundblade887add82020-05-17 05:50:34 -07006535 {(uint8_t[]){0xC5, 0x82, 0x1B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03,
6536 0xC2, 0x42, 0x01, 0x01}, 15},
6537 2056,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006538 EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS),
Laurence Lundblade887add82020-05-17 05:50:34 -07006539 2056,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006540 EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS),
Laurence Lundblade887add82020-05-17 05:50:34 -07006541 2056.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 Lundbladeda095972020-06-06 18:35:33 -07006545 "negative bignum 0xc349010000000000000000 -18446744073709551617",
Laurence Lundblade887add82020-05-17 05:50:34 -07006546 {(uint8_t[]){0xc3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 11},
6547 0,
6548 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
6549 0,
6550 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
6551 -18446744073709551617.0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006552 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)
Laurence Lundblade887add82020-05-17 05:50:34 -07006553 },
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08006554#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
Laurence Lundblade887add82020-05-17 05:50:34 -07006555 {
Laurence Lundblade313b2862020-05-16 01:23:06 -07006556 "Positive bignum 0x01020304 indefinite length string",
6557 {(uint8_t[]){0xC2, 0x5f, 0x42, 0x01, 0x02, 0x41, 0x03, 0x41, 0x04, 0xff}, 10},
6558 0x01020304,
6559 QCBOR_SUCCESS,
6560 0x01020304,
6561 QCBOR_SUCCESS,
6562 16909060.0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006563 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)
Laurence Lundblade313b2862020-05-16 01:23:06 -07006564 },
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08006565#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
Laurence Lundblade313b2862020-05-16 01:23:06 -07006566 {
Laurence Lundblade887add82020-05-17 05:50:34 -07006567 "Decimal Fraction with neg bignum [9223372036854775807, -4759477275222530853137]",
Laurence Lundblade313b2862020-05-16 01:23:06 -07006568 {(uint8_t[]){0xC4, 0x82, 0x1B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
6569 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,}, 23},
6570 0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006571 EXP_AND_MANTISSA_ERROR(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW),
Laurence Lundblade313b2862020-05-16 01:23:06 -07006572 0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006573 EXP_AND_MANTISSA_ERROR(QCBOR_ERR_NUMBER_SIGN_CONVERSION),
Laurence Lundblade313b2862020-05-16 01:23:06 -07006574 -INFINITY,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006575 FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS))
Laurence Lundblade313b2862020-05-16 01:23:06 -07006576 },
6577 {
6578 "big float [9223372036854775806, 9223372036854775806]",
6579 {(uint8_t[]){0xC5, 0x82, 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE,
6580 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE}, 20},
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_CONVERSION_UNDER_OVER_FLOW),
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 {
Laurence Lundblade983500d2020-05-14 11:49:34 -07006589 "Big float 3 * 2^^2",
6590 {(uint8_t[]){0xC5, 0x82, 0x02, 0x03}, 4},
6591 12,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006592 EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS),
Laurence Lundblade983500d2020-05-14 11:49:34 -07006593 12,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006594 EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS),
Laurence Lundblade983500d2020-05-14 11:49:34 -07006595 12.0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006596 FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS))
Laurence Lundblade983500d2020-05-14 11:49:34 -07006597 },
Laurence Lundblade983500d2020-05-14 11:49:34 -07006598 {
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006599 "Decimal fraction 3/10",
6600 {(uint8_t[]){0xC4, 0x82, 0x20, 0x03}, 4},
6601 0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006602 EXP_AND_MANTISSA_ERROR(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW),
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006603 0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006604 EXP_AND_MANTISSA_ERROR(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW),
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006605 0.30000000000000004,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006606 FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS))
Laurence Lundbladedfd49fc2020-09-01 14:17:16 -07006607 },
6608 {
Laurence Lundblade11fd78b2020-09-01 22:13:27 -07006609 "extreme pos bignum",
6610 {(uint8_t[]){0xc2, 0x59, 0x01, 0x90,
6611 // 50 rows of 8 is 400 digits.
6612 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6613 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6614 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6615 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6616 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6617 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6618 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6619 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6620 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6621 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6622 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6623 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6624 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6625 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6626 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6627 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6628 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6629 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6630 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6631 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6632 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6633 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6634 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6635 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6636 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6637 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6638 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6639 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6640 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6641 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6642 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6643 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6644 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6645 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6646 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6647 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6648 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6649 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
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,
gtravisb787e82a2023-11-30 18:38:21 -08006661 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0},
Laurence Lundblade11fd78b2020-09-01 22:13:27 -07006662 404},
6663 0,
6664 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
6665 0,
6666 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
Laurence Lundblade11fd78b2020-09-01 22:13:27 -07006667 INFINITY,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006668 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS),
Laurence Lundblade11fd78b2020-09-01 22:13:27 -07006669 },
6670
6671 {
6672 "extreme neg bignum",
6673 {(uint8_t[]){0xc3, 0x59, 0x01, 0x90,
6674 // 50 rows of 8 is 400 digits.
6675 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6676 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6677 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6678 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6679 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6680 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6681 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6682 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6683 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6684 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6685 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6686 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6687 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6688 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6689 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6690 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6691 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6692 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6693 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6694 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6695 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6696 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6697 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6698 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6699 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6700 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6701 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6702 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6703 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6704 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6705 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6706 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6707 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6708 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6709 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6710 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6711 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6712 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
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,
gtravisb787e82a2023-11-30 18:38:21 -08006724 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0},
Laurence Lundblade11fd78b2020-09-01 22:13:27 -07006725 404},
6726 0,
6727 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
6728 0,
6729 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
Laurence Lundblade11fd78b2020-09-01 22:13:27 -07006730 -INFINITY,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006731 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)
Laurence Lundblade11fd78b2020-09-01 22:13:27 -07006732 },
Laurence Lundblade51722fd2020-09-02 13:01:33 -07006733
6734 {
6735 "big float underflow [9223372036854775806, -9223372036854775806]",
6736 {(uint8_t[]){
6737 0xC5, 0x82,
6738 0x3B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE,
6739 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE}, 20},
Laurence Lundblade51722fd2020-09-02 13:01:33 -07006740 0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006741 EXP_AND_MANTISSA_ERROR(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW),
Laurence Lundblade51722fd2020-09-02 13:01:33 -07006742 0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006743 EXP_AND_MANTISSA_ERROR(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW),
Laurence Lundblade51722fd2020-09-02 13:01:33 -07006744 0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006745 FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS))
Laurence Lundblade51722fd2020-09-02 13:01:33 -07006746 },
6747
6748 {
6749 "bigfloat that evaluates to -INFINITY",
6750 {(uint8_t[]){
6751 0xC5, 0x82,
6752 0x1B, 0x70, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03,
6753 0xC3, 0x42, 0x01, 0x01}, 15},
Laurence Lundblade51722fd2020-09-02 13:01:33 -07006754 0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006755 EXP_AND_MANTISSA_ERROR(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW),
Laurence Lundblade51722fd2020-09-02 13:01:33 -07006756 0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006757 EXP_AND_MANTISSA_ERROR(QCBOR_ERR_NUMBER_SIGN_CONVERSION),
Laurence Lundblade51722fd2020-09-02 13:01:33 -07006758 -INFINITY,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006759 FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS))
Laurence Lundblade51722fd2020-09-02 13:01:33 -07006760 },
Laurence Lundblade37286c02022-09-03 10:05:02 -07006761 {
6762 "Positive bignum 0xffff",
6763 {(uint8_t[]){0xC2, 0x42, 0xff, 0xff}, 4},
6764 65536-1,
6765 QCBOR_SUCCESS,
6766 0xffff,
6767 QCBOR_SUCCESS,
6768 65535.0,
6769 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)
6770 },
6771#endif /* QCBOR_DISABLE_TAGS */
6772 {
6773 "Positive integer 18446744073709551615",
6774 {(uint8_t[]){0x1b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, 9},
6775 0,
6776 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
6777 18446744073709551615ULL,
6778 QCBOR_SUCCESS,
6779 18446744073709551615.0,
6780 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)
6781 },
6782
6783 {
6784 "Postive integer 0",
6785 {(uint8_t[]){0x0}, 1},
6786 0LL,
6787 QCBOR_SUCCESS,
6788 0ULL,
6789 QCBOR_SUCCESS,
6790 0.0,
6791 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)
6792 },
6793 {
6794 "Negative integer -18446744073709551616",
6795 {(uint8_t[]){0x3b, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }, 9},
6796 -9223372036854775807-1, // INT64_MIN
6797 QCBOR_SUCCESS,
6798 0ULL,
6799 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
6800 -9223372036854775808.0,
6801 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)
6802 },
6803 {
6804 "Double Floating point value 100.3",
6805 {(uint8_t[]){0xfb, 0x40, 0x59, 0x13, 0x33, 0x33, 0x33, 0x33, 0x33}, 9},
6806 100L,
6807 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS),
6808 100ULL,
6809 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS),
6810 100.3,
6811 FLOAT_ERR_CODE_NO_FLOAT(QCBOR_SUCCESS),
6812 },
6813 {
6814 "Floating point value NaN 0xfa7fc00000",
6815 {(uint8_t[]){0xfa, 0x7f, 0xc0, 0x00, 0x00}, 5},
6816 0,
6817 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_FLOAT_EXCEPTION),
6818 0,
6819 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_FLOAT_EXCEPTION),
6820 NAN,
6821 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS),
6822 },
6823 {
6824 "half-precision Floating point value -4",
6825 {(uint8_t[]){0xf9, 0xc4, 0x00}, 3},
6826 // Normal case with all enabled.
6827 -4,
6828 FLOAT_ERR_CODE_NO_HALF_PREC_NO_FLOAT_HW(QCBOR_SUCCESS),
6829 0,
6830 FLOAT_ERR_CODE_NO_HALF_PREC_NO_FLOAT_HW(QCBOR_ERR_NUMBER_SIGN_CONVERSION),
6831 -4.0,
6832 FLOAT_ERR_CODE_NO_HALF_PREC(QCBOR_SUCCESS)
6833 },
6834 {
6835 "+inifinity single precision",
6836 {(uint8_t[]){0xfa, 0x7f, 0x80, 0x00, 0x00}, 5},
6837 0,
6838 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_FLOAT_EXCEPTION),
6839 0,
6840 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW),
6841 INFINITY,
6842 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)
6843 },
6844
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006845};
Laurence Lundblade9c905e82020-04-25 11:31:38 -07006846
6847
6848
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08006849
6850static int32_t SetUpDecoder(QCBORDecodeContext *DCtx, UsefulBufC CBOR, UsefulBuf Pool)
6851{
6852 QCBORDecode_Init(DCtx, CBOR, QCBOR_DECODE_MODE_NORMAL);
6853#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
6854 if(QCBORDecode_SetMemPool(DCtx, Pool, 0)) {
6855 return 1;
6856 }
6857#else /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
6858 (void)Pool;
6859#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
6860 return 0;
6861}
6862
6863
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03006864int32_t IntegerConvertTest(void)
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006865{
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08006866 const int nNumTests = C_ARRAY_COUNT(NumberConversions,
6867 struct NumberConversion);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006868
Laurence Lundblade9ab5abb2020-05-20 12:10:45 -07006869 for(int nIndex = 0; nIndex < nNumTests; nIndex++) {
6870 const struct NumberConversion *pF = &NumberConversions[nIndex];
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07006871
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006872 // Set up the decoding context including a memory pool so that
6873 // indefinite length items can be checked
6874 QCBORDecodeContext DCtx;
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006875 UsefulBuf_MAKE_STACK_UB(Pool, 100);
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07006876
6877 /* ----- test conversion to int64_t ------ */
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08006878 if(SetUpDecoder(&DCtx, pF->CBOR, Pool)) {
6879 return (int32_t)(3333+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006880 }
6881
6882 int64_t nInt;
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006883 QCBORDecode_GetInt64ConvertAll(&DCtx, 0xffff, &nInt);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07006884 if(QCBORDecode_GetError(&DCtx) != pF->uErrorInt64) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07006885 return (int32_t)(2000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006886 }
6887 if(pF->uErrorInt64 == QCBOR_SUCCESS && pF->nConvertedToInt64 != nInt) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07006888 return (int32_t)(3000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006889 }
6890
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07006891 /* ----- test conversion to uint64_t ------ */
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08006892 if(SetUpDecoder(&DCtx, pF->CBOR, Pool)) {
6893 return (int32_t)(3333+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006894 }
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08006895
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006896 uint64_t uInt;
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006897 QCBORDecode_GetUInt64ConvertAll(&DCtx, 0xffff, &uInt);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07006898 if(QCBORDecode_GetError(&DCtx) != pF->uErrorUint64) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07006899 return (int32_t)(4000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006900 }
6901 if(pF->uErrorUint64 == QCBOR_SUCCESS && pF->uConvertToUInt64 != uInt) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07006902 return (int32_t)(5000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006903 }
6904
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07006905 /* ----- test conversion to double ------ */
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08006906 if(SetUpDecoder(&DCtx, pF->CBOR, Pool)) {
6907 return (int32_t)(3333+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006908 }
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006909
6910#ifndef USEFULBUF_DISABLE_ALL_FLOAT
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006911 double d;
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006912 QCBORDecode_GetDoubleConvertAll(&DCtx, 0xffff, &d);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07006913 if(QCBORDecode_GetError(&DCtx) != pF->uErrorDouble) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07006914 return (int32_t)(6000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006915 }
6916 if(pF->uErrorDouble == QCBOR_SUCCESS) {
6917 if(isnan(pF->dConvertToDouble)) {
Laurence Lundblade983500d2020-05-14 11:49:34 -07006918 // NaN's can't be compared for equality. A NaN is
6919 // never equal to anything including another NaN
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006920 if(!isnan(d)) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07006921 return (int32_t)(7000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006922 }
6923 } else {
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006924 if(pF->dConvertToDouble != d) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07006925 return (int32_t)(8000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006926 }
6927 }
6928 }
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006929#endif /* USEFULBUF_DISABLE_ALL_FLOAT */
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006930 }
6931
6932 return 0;
6933}
6934
Laurence Lundbladea8758502022-05-15 17:57:46 -07006935#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
6936
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03006937int32_t CBORTestIssue134(void)
David Navarro9123e5b2022-03-28 16:04:03 +02006938{
6939 QCBORDecodeContext DCtx;
6940 QCBORItem Item;
6941 QCBORError uCBORError;
6942 const uint8_t spTestIssue134[] = { 0x5F, 0x40, 0xFF };
Laurence Lundblade9c905e82020-04-25 11:31:38 -07006943
David Navarro9123e5b2022-03-28 16:04:03 +02006944 QCBORDecode_Init(&DCtx,
6945 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTestIssue134),
6946 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade97c61bf2020-05-02 11:24:06 -07006947
David Navarro9123e5b2022-03-28 16:04:03 +02006948 UsefulBuf_MAKE_STACK_UB(StringBuf, 200);
6949 QCBORDecode_SetMemPool(&DCtx, StringBuf, false);
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03006950
David Navarro9123e5b2022-03-28 16:04:03 +02006951 do {
6952 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
6953 } while (QCBOR_SUCCESS == uCBORError);
6954
6955 uCBORError = QCBORDecode_Finish(&DCtx);
6956
Laurence Lundblade11ea3612022-07-01 13:26:23 -07006957 return (int32_t)uCBORError;
David Navarro9123e5b2022-03-28 16:04:03 +02006958}
Laurence Lundblade97c61bf2020-05-02 11:24:06 -07006959
Laurence Lundbladea8758502022-05-15 17:57:46 -07006960#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
6961
Laurence Lundblade37286c02022-09-03 10:05:02 -07006962
6963
6964static const uint8_t spSequenceTestInput[] = {
6965 /* 1. The valid date string "1985-04-12" */
6966 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string
6967
6968 /* 2. */
6969 0x00,
6970
6971 /* 3. A valid epoch date, 1400000000; Tue, 13 May 2014 16:53:20 GMT */
6972 0x1a, 0x53, 0x72, 0x4E, 0x00,
6973
6974 /* 4. */
6975 0x62, 'h', 'i',
6976};
6977
6978
Laurence Lundbladee3553422020-05-02 11:11:17 -07006979int32_t CBORSequenceDecodeTests(void)
6980{
6981 QCBORDecodeContext DCtx;
Laurence Lundblade87495732021-02-26 10:05:55 -07006982 QCBORItem Item;
6983 QCBORError uCBORError;
6984 size_t uConsumed;
Laurence Lundbladee3553422020-05-02 11:11:17 -07006985
6986 // --- Test a sequence with extra bytes ---
Laurence Lundblade9b334962020-08-27 10:55:53 -07006987
Laurence Lundbladee3553422020-05-02 11:11:17 -07006988 QCBORDecode_Init(&DCtx,
Laurence Lundblade37286c02022-09-03 10:05:02 -07006989 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSequenceTestInput),
Laurence Lundbladee3553422020-05-02 11:11:17 -07006990 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade9b334962020-08-27 10:55:53 -07006991
Laurence Lundblade37286c02022-09-03 10:05:02 -07006992 // Get 1.
Laurence Lundbladee3553422020-05-02 11:11:17 -07006993 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
6994 if(uCBORError != QCBOR_SUCCESS) {
6995 return 1;
6996 }
Laurence Lundblade37286c02022-09-03 10:05:02 -07006997 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ) {
Laurence Lundbladee3553422020-05-02 11:11:17 -07006998 return 2;
6999 }
Laurence Lundbladec7114722020-08-13 05:11:40 -07007000
Laurence Lundblade87495732021-02-26 10:05:55 -07007001 uCBORError = QCBORDecode_PartialFinish(&DCtx, &uConsumed);
7002 if(uCBORError != QCBOR_ERR_EXTRA_BYTES ||
Laurence Lundblade37286c02022-09-03 10:05:02 -07007003 uConsumed != 11) {
7004 return 102;
7005 }
7006
7007 // Get 2.
7008 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
7009 if(uCBORError != QCBOR_SUCCESS) {
7010 return 66;
7011 }
7012
7013 uCBORError = QCBORDecode_PartialFinish(&DCtx, &uConsumed);
7014 if(uCBORError != QCBOR_ERR_EXTRA_BYTES ||
Laurence Lundblade87495732021-02-26 10:05:55 -07007015 uConsumed != 12) {
7016 return 102;
7017 }
7018
Laurence Lundblade37286c02022-09-03 10:05:02 -07007019 // Get 3.
Laurence Lundbladec7114722020-08-13 05:11:40 -07007020 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundbladee3553422020-05-02 11:11:17 -07007021 if(uCBORError != QCBOR_SUCCESS) {
7022 return 2;
7023 }
Laurence Lundblade37286c02022-09-03 10:05:02 -07007024 if(Item.uDataType != QCBOR_TYPE_INT64) {
Laurence Lundbladee3553422020-05-02 11:11:17 -07007025 return 3;
7026 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07007027
Laurence Lundbladee3553422020-05-02 11:11:17 -07007028 // A sequence can have stuff at the end that may
7029 // or may not be valid CBOR. The protocol decoder knows
7030 // when to stop by definition of the protocol, not
7031 // when the top-level map or array is ended.
7032 // Finish still has to be called to know that
7033 // maps and arrays (if there were any) were closed
7034 // off correctly. When called like this it
7035 // must return the error QCBOR_ERR_EXTRA_BYTES.
7036 uCBORError = QCBORDecode_Finish(&DCtx);
7037 if(uCBORError != QCBOR_ERR_EXTRA_BYTES) {
7038 return 4;
7039 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07007040
Laurence Lundbladee3553422020-05-02 11:11:17 -07007041 // --- Test an empty input ----
7042 uint8_t empty[1];
7043 UsefulBufC Empty = {empty, 0};
7044 QCBORDecode_Init(&DCtx,
7045 Empty,
7046 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade9b334962020-08-27 10:55:53 -07007047
Laurence Lundbladee3553422020-05-02 11:11:17 -07007048 uCBORError = QCBORDecode_Finish(&DCtx);
7049 if(uCBORError != QCBOR_SUCCESS) {
7050 return 5;
7051 }
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07007052
7053
Laurence Lundbladee3553422020-05-02 11:11:17 -07007054 // --- Sequence with unclosed indefinite length array ---
7055 static const uint8_t xx[] = {0x01, 0x9f, 0x02};
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07007056
Laurence Lundbladee3553422020-05-02 11:11:17 -07007057 QCBORDecode_Init(&DCtx,
7058 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(xx),
7059 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07007060
Laurence Lundbladee3553422020-05-02 11:11:17 -07007061 // Get the first item
7062 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
7063 if(uCBORError != QCBOR_SUCCESS) {
7064 return 7;
7065 }
7066 if(Item.uDataType != QCBOR_TYPE_INT64) {
7067 return 8;
7068 }
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07007069
Laurence Lundbladee3553422020-05-02 11:11:17 -07007070 // Get a second item
7071 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08007072#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundbladee3553422020-05-02 11:11:17 -07007073 if(uCBORError != QCBOR_SUCCESS) {
7074 return 9;
7075 }
7076 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
7077 return 10;
7078 }
7079
7080 // Try to finish before consuming all bytes to confirm
7081 // that the still-open error is returned.
7082 uCBORError = QCBORDecode_Finish(&DCtx);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07007083 if(uCBORError != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
Laurence Lundbladee3553422020-05-02 11:11:17 -07007084 return 11;
7085 }
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08007086#else /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
7087 if(uCBORError != QCBOR_ERR_INDEF_LEN_ARRAYS_DISABLED) {
7088 return 20;
7089 }
7090#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
Laurence Lundbladee3553422020-05-02 11:11:17 -07007091
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07007092
Laurence Lundbladee3553422020-05-02 11:11:17 -07007093 // --- Sequence with a closed indefinite length array ---
7094 static const uint8_t yy[] = {0x01, 0x9f, 0xff};
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07007095
Laurence Lundbladee3553422020-05-02 11:11:17 -07007096 QCBORDecode_Init(&DCtx,
7097 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(yy),
7098 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07007099
Laurence Lundbladee3553422020-05-02 11:11:17 -07007100 // Get the first item
7101 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
7102 if(uCBORError != QCBOR_SUCCESS) {
7103 return 12;
7104 }
7105 if(Item.uDataType != QCBOR_TYPE_INT64) {
7106 return 13;
7107 }
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07007108
Laurence Lundbladee3553422020-05-02 11:11:17 -07007109 // Get a second item
7110 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08007111#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
7112
Laurence Lundbladee3553422020-05-02 11:11:17 -07007113 if(uCBORError != QCBOR_SUCCESS) {
7114 return 14;
7115 }
7116 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
7117 return 15;
7118 }
7119
7120 // Try to finish before consuming all bytes to confirm
7121 // that the still-open error is returned.
7122 uCBORError = QCBORDecode_Finish(&DCtx);
7123 if(uCBORError != QCBOR_SUCCESS) {
7124 return 16;
7125 }
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08007126#else /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
7127 if(uCBORError != QCBOR_ERR_INDEF_LEN_ARRAYS_DISABLED) {
7128 return 20;
7129 }
7130#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
Laurence Lundbladee3553422020-05-02 11:11:17 -07007131
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07007132
Laurence Lundbladee3553422020-05-02 11:11:17 -07007133 return 0;
7134}
7135
Laurence Lundbladee15326f2020-06-15 15:50:23 -07007136
Laurence Lundblade70ecead2020-06-15 19:40:06 -07007137
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03007138int32_t IntToTests(void)
Laurence Lundbladee15326f2020-06-15 15:50:23 -07007139{
7140 int nErrCode;
7141 int32_t n32;
7142 int16_t n16;
7143 int8_t n8;
7144 uint32_t u32;
7145 uint16_t u16;
7146 uint8_t u8;
7147 uint64_t u64;
7148
7149 nErrCode = QCBOR_Int64ToInt32(1, &n32);
7150 if(nErrCode == -1 || n32 != 1) {
7151 return 1;
7152 }
7153
7154 nErrCode = QCBOR_Int64ToInt32((int64_t)INT32_MAX, &n32);
7155 if(nErrCode == -1 || n32 != INT32_MAX) {
7156 return 2;
7157 }
7158
7159 nErrCode = QCBOR_Int64ToInt32((int64_t)INT32_MIN, &n32);
7160 if(nErrCode == -1 || n32 != INT32_MIN) {
7161 return 3;
7162 }
7163
7164 nErrCode = QCBOR_Int64ToInt32(((int64_t)INT32_MAX)+1, &n32);
7165 if(nErrCode != -1) {
7166 return 4;
7167 }
7168
7169 nErrCode = QCBOR_Int64ToInt32(((int64_t)INT32_MIN)-1, &n32);
7170 if(nErrCode != -1) {
7171 return 5;
7172 }
7173
7174
7175 nErrCode = QCBOR_Int64ToInt16((int64_t)INT16_MAX, &n16);
7176 if(nErrCode == -1 || n16 != INT16_MAX) {
7177 return 6;
7178 }
7179
7180 nErrCode = QCBOR_Int64ToInt16((int64_t)INT16_MIN, &n16);
7181 if(nErrCode == -1 || n16 != INT16_MIN) {
7182 return 7;
7183 }
7184
7185 nErrCode = QCBOR_Int64ToInt16(1, &n16);
7186 if(nErrCode == -1 || n16 != 1) {
7187 return 8;
7188 }
7189
7190 nErrCode = QCBOR_Int64ToInt16(((int64_t)INT16_MAX)+1, &n16);
7191 if(nErrCode != -1) {
7192 return 9;
7193 }
7194
7195 nErrCode = QCBOR_Int64ToInt16(((int64_t)INT16_MIN)-1, &n16);
7196 if(nErrCode != -1) {
7197 return 10;
7198 }
7199
7200
7201 nErrCode = QCBOR_Int64ToInt8(1, &n8);
7202 if(nErrCode == -1 || n8 != 1) {
7203 return 11;
7204 }
7205
7206 nErrCode = QCBOR_Int64ToInt8((int64_t)INT8_MAX, &n8);
7207 if(nErrCode == -1 || n8 != INT8_MAX) {
7208 return 12;
7209 }
7210
7211 nErrCode = QCBOR_Int64ToInt8((int64_t)INT8_MIN, &n8);
7212 if(nErrCode == -1 || n8 != INT8_MIN) {
7213 return 13;
7214 }
7215
7216 nErrCode = QCBOR_Int64ToInt8(((int64_t)INT8_MAX)+1, &n8);
7217 if(nErrCode != -1) {
7218 return 14;
7219 }
7220
7221 nErrCode = QCBOR_Int64ToInt8(((int64_t)INT8_MIN)-1, &n8);
7222 if(nErrCode != -1) {
7223 return 15;
7224 }
7225
7226
7227 nErrCode = QCBOR_Int64ToUInt32(1, &u32);
7228 if(nErrCode == -1 || u32 != 1) {
7229 return 16;
7230 }
7231
7232 nErrCode = QCBOR_Int64ToUInt32((int64_t)UINT32_MAX, &u32);
7233 if(nErrCode == -1 || u32 != UINT32_MAX) {
7234 return 17;
7235 }
7236
7237 nErrCode = QCBOR_Int64ToUInt32((int64_t)0, &u32);
7238 if(nErrCode == -1 || u32 != 0) {
7239 return 18;
7240 }
7241
7242 nErrCode = QCBOR_Int64ToUInt32(((int64_t)UINT32_MAX)+1, &u32);
7243 if(nErrCode != -1) {
7244 return 19;
7245 }
7246
7247 nErrCode = QCBOR_Int64ToUInt32((int64_t)-1, &u32);
7248 if(nErrCode != -1) {
7249 return 20;
7250 }
7251
7252
7253 nErrCode = QCBOR_Int64UToInt16((int64_t)UINT16_MAX, &u16);
7254 if(nErrCode == -1 || u16 != UINT16_MAX) {
7255 return 21;
7256 }
7257
7258 nErrCode = QCBOR_Int64UToInt16((int64_t)0, &u16);
7259 if(nErrCode == -1 || u16 != 0) {
7260 return 22;
7261 }
7262
7263 nErrCode = QCBOR_Int64UToInt16(1, &u16);
7264 if(nErrCode == -1 || u16 != 1) {
7265 return 23;
7266 }
7267
7268 nErrCode = QCBOR_Int64UToInt16(((int64_t)UINT16_MAX)+1, &u16);
7269 if(nErrCode != -1) {
7270 return 24;
7271 }
7272
7273 nErrCode = QCBOR_Int64UToInt16((int64_t)-1, &u16);
7274 if(nErrCode != -1) {
7275 return 25;
7276 }
7277
7278
7279 nErrCode = QCBOR_Int64ToUInt8((int64_t)UINT8_MAX, &u8);
7280 if(nErrCode == -1 || u8 != UINT8_MAX) {
7281 return 26;
7282 }
7283
7284 nErrCode = QCBOR_Int64ToUInt8((int64_t)0, &u8);
7285 if(nErrCode == -1 || u8 != 0) {
7286 return 27;
7287 }
7288
7289 nErrCode = QCBOR_Int64ToUInt8(1, &u8);
7290 if(nErrCode == -1 || u8 != 1) {
7291 return 28;
7292 }
7293
7294 nErrCode = QCBOR_Int64ToUInt8(((int64_t)UINT16_MAX)+1, &u8);
7295 if(nErrCode != -1) {
7296 return 29;
7297 }
7298
7299 nErrCode = QCBOR_Int64ToUInt8((int64_t)-1, &u8);
7300 if(nErrCode != -1) {
7301 return 30;
7302 }
7303
7304
7305 nErrCode = QCBOR_Int64ToUInt64(1, &u64);
7306 if(nErrCode == -1 || u64 != 1) {
7307 return 31;
7308 }
7309
7310 nErrCode = QCBOR_Int64ToUInt64(INT64_MAX, &u64);
7311 if(nErrCode == -1 || u64 != INT64_MAX) {
7312 return 32;
7313 }
7314
7315 nErrCode = QCBOR_Int64ToUInt64((int64_t)0, &u64);
7316 if(nErrCode == -1 || u64 != 0) {
7317 return 33;
7318 }
7319
7320 nErrCode = QCBOR_Int64ToUInt64((int64_t)-1, &u64);
7321 if(nErrCode != -1) {
7322 return 34;
7323 }
7324
7325 return 0;
7326}
7327
Laurence Lundblade0750fc42020-06-20 21:02:34 -07007328
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07007329
7330
Laurence Lundblade0750fc42020-06-20 21:02:34 -07007331/*
7332A sequence with
7333 A wrapping bstr
7334 containing a map
7335 1
7336 2
7337 A wrapping bstr
7338 containing an array
7339 3
7340 wrapping bstr
7341 4
7342 5
7343 6
7344 array
7345 7
7346 8
Laurence Lundblade0750fc42020-06-20 21:02:34 -07007347 */
7348
Laurence Lundblade55013642020-09-23 05:39:22 -07007349static UsefulBufC EncodeBstrWrapTestData(UsefulBuf OutputBuffer)
Laurence Lundblade0750fc42020-06-20 21:02:34 -07007350{
Laurence Lundblade55013642020-09-23 05:39:22 -07007351 UsefulBufC Encoded;
Laurence Lundblade0750fc42020-06-20 21:02:34 -07007352 QCBOREncodeContext EC;
Laurence Lundblade55013642020-09-23 05:39:22 -07007353 QCBORError uErr;
Laurence Lundblade0750fc42020-06-20 21:02:34 -07007354
Laurence Lundblade55013642020-09-23 05:39:22 -07007355 QCBOREncode_Init(&EC, OutputBuffer);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07007356
7357 QCBOREncode_BstrWrap(&EC);
7358 QCBOREncode_OpenMap(&EC);
7359 QCBOREncode_AddInt64ToMapN(&EC, 100, 1);
7360 QCBOREncode_AddInt64ToMapN(&EC, 200, 2);
7361 QCBOREncode_CloseMap(&EC);
7362 QCBOREncode_BstrWrap(&EC);
7363 QCBOREncode_OpenArray(&EC);
7364 QCBOREncode_AddInt64(&EC, 3);
7365 QCBOREncode_BstrWrap(&EC);
7366 QCBOREncode_AddInt64(&EC, 4);
7367 QCBOREncode_CloseBstrWrap(&EC, NULL);
7368 QCBOREncode_AddInt64(&EC, 5);
7369 QCBOREncode_CloseArray(&EC);
7370 QCBOREncode_CloseBstrWrap(&EC, NULL);
7371 QCBOREncode_AddInt64(&EC, 6);
7372 QCBOREncode_CloseBstrWrap(&EC, NULL);
7373 QCBOREncode_OpenArray(&EC);
7374 QCBOREncode_AddInt64(&EC, 7);
7375 QCBOREncode_AddInt64(&EC, 8);
7376 QCBOREncode_CloseArray(&EC);
7377
7378 uErr = QCBOREncode_Finish(&EC, &Encoded);
Laurence Lundblade40a04322020-06-27 22:52:52 -07007379 if(uErr) {
7380 Encoded = NULLUsefulBufC;
7381 }
Laurence Lundblade0750fc42020-06-20 21:02:34 -07007382
7383 return Encoded;
7384}
7385
Laurence Lundbladecc7da412020-12-27 00:09:07 -08007386/* h'FF' */
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08007387static const uint8_t spBreakInByteString[] = {
7388 0x41, 0xff
7389};
7390
Laurence Lundblade0750fc42020-06-20 21:02:34 -07007391
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03007392int32_t EnterBstrTest(void)
Laurence Lundblade0750fc42020-06-20 21:02:34 -07007393{
Laurence Lundblade8510f8c2020-12-01 11:31:16 -08007394 UsefulBuf_MAKE_STACK_UB(OutputBuffer, 100);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07007395
7396 QCBORDecodeContext DC;
7397
Laurence Lundblade55013642020-09-23 05:39:22 -07007398 QCBORDecode_Init(&DC, EncodeBstrWrapTestData(OutputBuffer), 0);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07007399
Laurence Lundblade55013642020-09-23 05:39:22 -07007400 int64_t n1, n2, n3, n4, n5, n6, n7, n8;
Laurence Lundblade0750fc42020-06-20 21:02:34 -07007401
7402
Laurence Lundblade9b334962020-08-27 10:55:53 -07007403 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07007404 QCBORDecode_EnterMap(&DC, NULL);
Laurence Lundblade55013642020-09-23 05:39:22 -07007405 QCBORDecode_GetInt64InMapN(&DC, 100, &n1);
7406 QCBORDecode_GetInt64InMapN(&DC, 200, &n2);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07007407 QCBORDecode_ExitMap(&DC);
Laurence Lundblade9b334962020-08-27 10:55:53 -07007408 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07007409 QCBORDecode_EnterArray(&DC, NULL);
Laurence Lundblade55013642020-09-23 05:39:22 -07007410 QCBORDecode_GetInt64(&DC, &n3);
Laurence Lundblade9b334962020-08-27 10:55:53 -07007411 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
Laurence Lundblade55013642020-09-23 05:39:22 -07007412 QCBORDecode_GetInt64(&DC, &n4);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07007413 QCBORDecode_ExitBstrWrapped(&DC);
Laurence Lundblade55013642020-09-23 05:39:22 -07007414 QCBORDecode_GetInt64(&DC, &n5);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07007415 QCBORDecode_ExitArray(&DC);
7416 QCBORDecode_ExitBstrWrapped(&DC);
Laurence Lundblade55013642020-09-23 05:39:22 -07007417 QCBORDecode_GetInt64(&DC, &n6);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07007418 QCBORDecode_ExitBstrWrapped(&DC);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07007419 QCBORDecode_EnterArray(&DC, NULL);
Laurence Lundblade55013642020-09-23 05:39:22 -07007420 QCBORDecode_GetInt64(&DC, &n7);
7421 QCBORDecode_GetInt64(&DC, &n8);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07007422 QCBORDecode_ExitArray(&DC);
7423
7424 QCBORError uErr = QCBORDecode_Finish(&DC);
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08007425 if(uErr) {
7426 return (int32_t)uErr;
7427 }
Laurence Lundblade0750fc42020-06-20 21:02:34 -07007428
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08007429
7430 /* Enter and exit byte string wrapped CBOR that is bad. It has just a break.
7431 * Successful because no items are fetched from byte string.
7432 */
7433 QCBORDecode_Init(&DC,
7434 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBreakInByteString),
7435 0);
7436 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
7437 uErr = QCBORDecode_GetError(&DC);
7438 if(uErr) {
7439 return 100 + (int32_t)uErr;
7440 }
7441
7442 QCBORDecode_ExitBstrWrapped(&DC);
7443 uErr = QCBORDecode_GetError(&DC);
7444 if(uErr) {
7445 return 200 + (int32_t)uErr;
7446 }
7447
7448 /* Try to get item that is a break out of a byte string wrapped CBOR.
7449 * It fails because there should be no break.
7450 */
7451 QCBORDecode_Init(&DC,
7452 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBreakInByteString),
7453 0);
7454 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
7455 QCBORItem Item;
7456 uErr = QCBORDecode_GetNext(&DC, &Item);
7457 if(uErr != QCBOR_ERR_BAD_BREAK) {
7458 return 300 + (int32_t)uErr;
7459 }
7460
7461 return 0;
Laurence Lundblade0750fc42020-06-20 21:02:34 -07007462}
Laurence Lundblade37f46e52020-08-04 03:32:14 -07007463
7464
7465
7466
7467static const uint8_t spTaggedTypes[] = {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007468 0xb2,
Laurence Lundblade37f46e52020-08-04 03:32:14 -07007469
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07007470 // Date string
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007471 0x00,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07007472 0xc0, 0x74, 0x32, 0x30, 0x30, 0x33, 0x2D, 0x31, 0x32, 0x2D,
7473 0x31, 0x33, 0x54, 0x31, 0x38, 0x3A, 0x33, 0x30, 0x3A, 0x30,
7474 0x32, 0x5A,
Laurence Lundblade37f46e52020-08-04 03:32:14 -07007475
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007476 0x01,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07007477 0x74, 0x32, 0x30, 0x30, 0x33, 0x2D, 0x31, 0x32, 0x2D, 0x31,
7478 0x33, 0x54, 0x31, 0x38, 0x3A, 0x33, 0x30, 0x3A, 0x30, 0x32,
7479 0x5A,
Laurence Lundblade37f46e52020-08-04 03:32:14 -07007480
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07007481 // Bignum
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007482 10,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07007483 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
7484 0x09, 0x10,
Laurence Lundblade37f46e52020-08-04 03:32:14 -07007485
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007486 11,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07007487 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
7488 0x09, 0x10,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007489
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07007490 // URL
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007491 20,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07007492 0xd8, 0x20, 0x6f, 0x68, 0x74, 0x74, 0x70, 0x3A, 0x2F, 0x2F,
7493 0x63, 0x62, 0x6F, 0x72, 0x2E, 0x6D, 0x65, 0x2F,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007494
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07007495 21,
7496 0x6f, 0x68, 0x74, 0x74, 0x70, 0x3A, 0x2F, 0x2F, 0x63, 0x62,
7497 0x6F, 0x72, 0x2E, 0x6D, 0x65, 0x2F,
7498
7499 // B64
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007500 0x18, 0x1e,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07007501 0xd8, 0x22, 0x6c, 0x63, 0x47, 0x78, 0x6C, 0x59, 0x58, 0x4E,
7502 0x31, 0x63, 0x6D, 0x55, 0x75,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007503
7504 0x18, 0x1f,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07007505 0x6c, 0x63, 0x47, 0x78, 0x6C, 0x59, 0x58, 0x4E, 0x31, 0x63,
7506 0x6D, 0x55, 0x75,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007507
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07007508 // B64URL
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007509 0x18, 0x28,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07007510 0xd8, 0x21, 0x6c, 0x63, 0x47, 0x78, 0x6C, 0x59, 0x58, 0x4E,
7511 0x31, 0x63, 0x6D, 0x55, 0x75,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007512
7513 0x18, 0x29,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07007514 0x6c, 0x63, 0x47, 0x78, 0x6C, 0x59, 0x58, 0x4E, 0x31, 0x63,
7515 0x6D, 0x55, 0x75,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007516
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07007517 // Regex
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007518 0x18, 0x32,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07007519 0xd8, 0x23, 0x68, 0x31, 0x30, 0x30, 0x5C, 0x73, 0x2A, 0x6D,
7520 0x6B,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007521
7522 0x18, 0x33,
7523 0x68, 0x31, 0x30, 0x30, 0x5C, 0x73, 0x2A, 0x6D, 0x6B,
7524
7525 // MIME
7526 0x18, 0x3c,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07007527 0xd8, 0x24, 0x72, 0x4D, 0x49, 0x4D, 0x45, 0x2D, 0x56, 0x65,
7528 0x72, 0x73, 0x69, 0x6F, 0x6E, 0x3A, 0x20, 0x31, 0x2E, 0x30,
7529 0x0A,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007530
7531 0x18, 0x3d,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07007532 0x72, 0x4D, 0x49, 0x4D, 0x45, 0x2D, 0x56, 0x65, 0x72, 0x73,
7533 0x69, 0x6F, 0x6E, 0x3A, 0x20, 0x31, 0x2E, 0x30, 0x0A,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007534
7535 0x18, 0x3e,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07007536 0xd9, 0x01, 0x01, 0x52, 0x4D, 0x49, 0x4D, 0x45, 0x2D, 0x56,
7537 0x65, 0x72, 0x73, 0x69, 0x6F, 0x6E, 0x3A, 0x20, 0x31, 0x2E,
7538 0x30, 0x0A,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007539
7540 0x18, 0x3f,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07007541 0x52, 0x4D, 0x49, 0x4D, 0x45, 0x2D, 0x56, 0x65, 0x72, 0x73,
7542 0x69, 0x6F, 0x6E, 0x3A, 0x20, 0x31, 0x2E, 0x30, 0x0A,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007543
7544 // UUID
7545 0x18, 0x46,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07007546 0xd8, 0x25, 0x50, 0x53, 0x4D, 0x41, 0x52, 0x54, 0x43, 0x53,
7547 0x4C, 0x54, 0x54, 0x43, 0x46, 0x49, 0x43, 0x41, 0x32,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007548
7549 0x18, 0x47,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07007550 0x50, 0x53, 0x4D, 0x41, 0x52, 0x54, 0x43, 0x53, 0x4C, 0x54,
7551 0x54, 0x43, 0x46, 0x49, 0x43, 0x41, 0x32
Laurence Lundblade37f46e52020-08-04 03:32:14 -07007552};
7553
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03007554int32_t DecodeTaggedTypeTests(void)
Laurence Lundblade37f46e52020-08-04 03:32:14 -07007555{
7556 QCBORDecodeContext DC;
7557 QCBORError uErr;
7558
7559 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTaggedTypes), 0);
7560
7561 UsefulBufC String;
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007562 bool bNeg;
Laurence Lundblade37f46e52020-08-04 03:32:14 -07007563
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07007564 QCBORDecode_EnterMap(&DC, NULL);
Laurence Lundblade9b334962020-08-27 10:55:53 -07007565 QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007566 QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &String);
Laurence Lundblade37f46e52020-08-04 03:32:14 -07007567 if(QCBORDecode_GetError(&DC) != QCBOR_SUCCESS) {
7568 return 1;
7569 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07007570 QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade37f46e52020-08-04 03:32:14 -07007571 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_UNEXPECTED_TYPE) {
7572 return 2;
7573 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07007574 QCBORDecode_GetDateStringInMapN(&DC, 1, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade37f46e52020-08-04 03:32:14 -07007575 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_UNEXPECTED_TYPE) {
7576 return 3;
7577 }
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007578 QCBORDecode_GetDateStringInMapN(&DC, 1, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &String);
Laurence Lundblade9b334962020-08-27 10:55:53 -07007579 QCBORDecode_GetDateStringInMapN(&DC, 1, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade37f46e52020-08-04 03:32:14 -07007580 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
7581 return 4;
7582 }
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007583 QCBORDecode_GetDateStringInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07007584 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007585 return 5;
7586 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07007587
Laurence Lundblade9b334962020-08-27 10:55:53 -07007588 QCBORDecode_GetBignumInMapN(&DC, 10, QCBOR_TAG_REQUIREMENT_TAG, &String, &bNeg);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007589 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
7590 bNeg != false) {
7591 return 10;
7592 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07007593 QCBORDecode_GetBignumInMapN(&DC, 11, QCBOR_TAG_REQUIREMENT_TAG, &String, &bNeg);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007594 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
7595 bNeg != true) {
7596 return 11;
7597 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07007598 QCBORDecode_GetBignumInMapN(&DC, 11, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bNeg);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007599 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_UNEXPECTED_TYPE) {
7600 return 12;
7601 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07007602 QCBORDecode_GetBignumInMapN(&DC, 14, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bNeg);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07007603 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007604 return 13;
7605 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07007606 QCBORDecode_GetBignumInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bNeg);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07007607 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007608 return 14;
7609 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07007610
Laurence Lundblade9b334962020-08-27 10:55:53 -07007611 QCBORDecode_GetURIInMapN(&DC, 20, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007612 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
7613 return 20;
7614 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07007615 QCBORDecode_GetURIInMapN(&DC, 21, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007616 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
7617 return 21;
7618 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07007619 QCBORDecode_GetURIInMapN(&DC, 22, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07007620 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007621 return 22;
7622 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07007623 QCBORDecode_GetURIInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07007624 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007625 return 23;
7626 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07007627
Laurence Lundblade24bd7e12021-01-09 00:05:11 -08007628#ifndef QCBOR_DISABLE_UNCOMMON_TAGS
Laurence Lundblade9b334962020-08-27 10:55:53 -07007629 QCBORDecode_GetB64InMapN(&DC, 30, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007630 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
7631 return 30;
7632 }
Laurence Lundblade24bd7e12021-01-09 00:05:11 -08007633#endif
Laurence Lundblade9b334962020-08-27 10:55:53 -07007634 QCBORDecode_GetB64InMapN(&DC, 31, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007635 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
7636 return 31;
7637 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07007638 QCBORDecode_GetB64InMapN(&DC, 32, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07007639 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007640 return 32;
7641 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07007642 QCBORDecode_GetB64InMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07007643 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007644 return 33;
7645 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07007646
Laurence Lundblade24bd7e12021-01-09 00:05:11 -08007647#ifndef QCBOR_DISABLE_UNCOMMON_TAGS
Laurence Lundblade9b334962020-08-27 10:55:53 -07007648 QCBORDecode_GetB64URLInMapN(&DC, 40, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007649 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
7650 return 40;
7651 }
Laurence Lundblade24bd7e12021-01-09 00:05:11 -08007652#endif
Laurence Lundblade9b334962020-08-27 10:55:53 -07007653 QCBORDecode_GetB64URLInMapN(&DC, 41, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007654 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
7655 return 41;
7656 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07007657 QCBORDecode_GetB64URLInMapN(&DC, 42, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07007658 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007659 return 42;
7660 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07007661 QCBORDecode_GetB64URLInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07007662 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007663 return 43;
7664 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07007665
Laurence Lundblade24bd7e12021-01-09 00:05:11 -08007666#ifndef QCBOR_DISABLE_UNCOMMON_TAGS
Laurence Lundblade9b334962020-08-27 10:55:53 -07007667 QCBORDecode_GetRegexInMapN(&DC, 50, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007668 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
7669 return 50;
7670 }
Laurence Lundblade24bd7e12021-01-09 00:05:11 -08007671#endif
Laurence Lundblade9b334962020-08-27 10:55:53 -07007672 QCBORDecode_GetRegexInMapN(&DC, 51, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007673 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
7674 return 51;
7675 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07007676 QCBORDecode_GetRegexInMapN(&DC, 52, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07007677 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007678 return 52;
7679 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07007680 QCBORDecode_GetRegexInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07007681 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007682 return 53;
7683 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07007684
Laurence Lundblade24bd7e12021-01-09 00:05:11 -08007685#ifndef QCBOR_DISABLE_UNCOMMON_TAGS
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007686 // MIME
7687 bool bIsNot7Bit;
Laurence Lundblade9b334962020-08-27 10:55:53 -07007688 QCBORDecode_GetMIMEMessageInMapN(&DC, 60, QCBOR_TAG_REQUIREMENT_TAG, &String, &bIsNot7Bit);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007689 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
7690 bIsNot7Bit == true) {
7691 return 60;
7692 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07007693 QCBORDecode_GetMIMEMessageInMapN(&DC, 61, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bIsNot7Bit);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007694 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
7695 bIsNot7Bit == true) {
7696 return 61;
7697 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07007698 QCBORDecode_GetMIMEMessageInMapN(&DC, 62, QCBOR_TAG_REQUIREMENT_TAG, &String, &bIsNot7Bit);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007699 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
7700 bIsNot7Bit == false) {
7701 return 62;
7702 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07007703 QCBORDecode_GetMIMEMessageInMapN(&DC, 63, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bIsNot7Bit);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007704 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
7705 bIsNot7Bit == false) {
7706 return 63;
7707 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07007708 QCBORDecode_GetMIMEMessageInMapN(&DC, 64, QCBOR_TAG_REQUIREMENT_TAG, &String, &bNeg);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07007709 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007710 return 64;
7711 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07007712 QCBORDecode_GetMIMEMessageInMapSZ(&DC, "zzz", QCBOR_TAG_REQUIREMENT_TAG, &String, &bNeg);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07007713 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007714 return 65;
7715 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07007716
Laurence Lundblade24bd7e12021-01-09 00:05:11 -08007717
Laurence Lundblade9b334962020-08-27 10:55:53 -07007718 QCBORDecode_GetBinaryUUIDInMapN(&DC, 70, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007719 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
7720 return 70;
7721 }
Laurence Lundblade24bd7e12021-01-09 00:05:11 -08007722#endif /* #ifndef QCBOR_DISABLE_UNCOMMON_TAGS */
7723
Laurence Lundblade9b334962020-08-27 10:55:53 -07007724 QCBORDecode_GetBinaryUUIDInMapN(&DC, 71, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007725 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
7726 return 71;
7727 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07007728 QCBORDecode_GetBinaryUUIDInMapN(&DC, 72, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07007729 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007730 return 72;
7731 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07007732 QCBORDecode_GetBinaryUUIDInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07007733 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07007734 return 73;
7735 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07007736
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07007737 // Improvement: add some more error test cases
7738
Laurence Lundblade37f46e52020-08-04 03:32:14 -07007739 QCBORDecode_ExitMap(&DC);
7740
7741 uErr = QCBORDecode_Finish(&DC);
7742 if(uErr != QCBOR_SUCCESS) {
7743 return 100;
7744 }
7745
7746 return 0;
7747}
Laurence Lundbladea4308a82020-10-03 18:08:57 -07007748
7749
7750
7751
7752/*
Laurence Lundbladecc7da412020-12-27 00:09:07 -08007753 [
7754 "aaaaaaaaaa",
7755 {}
7756 ]
Laurence Lundbladea4308a82020-10-03 18:08:57 -07007757 */
7758static const uint8_t spTooLarge1[] = {
7759 0x9f,
7760 0x6a, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61,
7761 0xa0,
7762 0xff
7763};
7764
7765/*
Laurence Lundbladecc7da412020-12-27 00:09:07 -08007766 [
7767 {
7768 0: "aaaaaaaaaa"
7769 }
7770 ]
Laurence Lundbladea4308a82020-10-03 18:08:57 -07007771 */
7772static const uint8_t spTooLarge2[] = {
7773 0x9f,
7774 0xa1,
7775 0x00,
7776 0x6a, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61,
7777 0xff
7778};
7779
7780/*
Laurence Lundbladecc7da412020-12-27 00:09:07 -08007781 h'A1006A61616161616161616161'
Laurence Lundbladea4308a82020-10-03 18:08:57 -07007782
Laurence Lundbladecc7da412020-12-27 00:09:07 -08007783 {
7784 0: "aaaaaaaaaa"
7785 }
Laurence Lundbladea4308a82020-10-03 18:08:57 -07007786 */
7787static const uint8_t spTooLarge3[] = {
7788 0x4d,
7789 0xa1,
7790 0x00,
7791 0x6a, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61,
7792};
7793
7794int32_t TooLargeInputTest(void)
7795{
7796 QCBORDecodeContext DC;
7797 QCBORError uErr;
7798 UsefulBufC String;
7799
7800 // These tests require a build with QCBOR_MAX_DECODE_INPUT_SIZE set
7801 // to 10 There's not really any way to test this error
7802 // condition. The error condition is not complex, so setting
7803 // QCBOR_MAX_DECODE_INPUT_SIZE gives an OK test.
7804
7805 // The input CBOR is only too large because the
7806 // QCBOR_MAX_DECODE_INPUT_SIZE is 10.
7807 //
7808 // This test is disabled for the normal test runs because of the
7809 // special build requirement.
7810
7811
7812 // Tests the start of a map being too large
7813 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTooLarge1), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07007814 QCBORDecode_EnterArray(&DC, NULL);
Laurence Lundbladea4308a82020-10-03 18:08:57 -07007815 QCBORDecode_GetTextString(&DC, &String);
7816 uErr = QCBORDecode_GetError(&DC);
7817 if(uErr != QCBOR_SUCCESS) {
7818 return 1;
7819 }
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07007820 QCBORDecode_EnterMap(&DC, NULL);
Laurence Lundbladea4308a82020-10-03 18:08:57 -07007821 uErr = QCBORDecode_GetError(&DC);
7822 if(uErr != QCBOR_ERR_INPUT_TOO_LARGE) {
7823 return 2;
7824 }
7825
7826 // Tests the end of a map being too large
7827 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTooLarge2), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07007828 QCBORDecode_EnterArray(&DC, NULL);
7829 QCBORDecode_EnterMap(&DC, NULL);
Laurence Lundbladea4308a82020-10-03 18:08:57 -07007830 uErr = QCBORDecode_GetError(&DC);
7831 if(uErr != QCBOR_SUCCESS) {
7832 return 3;
7833 }
7834 QCBORDecode_ExitMap(&DC);
7835 uErr = QCBORDecode_GetError(&DC);
7836 if(uErr != QCBOR_ERR_INPUT_TOO_LARGE) {
7837 return 4;
7838 }
7839
7840 // Tests the entire input CBOR being too large when processing bstr wrapping
7841 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTooLarge3), QCBOR_DECODE_MODE_NORMAL);
7842 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
7843 uErr = QCBORDecode_GetError(&DC);
7844 if(uErr != QCBOR_ERR_INPUT_TOO_LARGE) {
7845 return 5;
7846 }
7847
7848 return 0;
7849}
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07007850
7851
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08007852#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
7853
Laurence Lundblade37286c02022-09-03 10:05:02 -07007854/*
7855 An array of three map entries
7856 1) Indefinite length string label for indefinite lenght byte string
7857 2) Indefinite length string label for an integer
7858 3) Indefinite length string label for an indefinite-length negative big num
7859 */
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07007860static const uint8_t spMapWithIndefLenStrings[] = {
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08007861 0xa3,
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07007862 0x7f, 0x61, 'l', 0x64, 'a', 'b', 'e', 'l' , 0x61, '1', 0xff,
7863 0x5f, 0x42, 0x01, 0x02, 0x43, 0x03, 0x04, 0x05, 0xff,
7864 0x7f, 0x62, 'd', 'y', 0x61, 'm', 0x61, 'o', 0xff,
7865 0x03,
7866 0x7f, 0x62, 'l', 'a', 0x63, 'b', 'e', 'l', 0x61, '2', 0xff,
7867 0xc3,
7868 0x5f, 0x42, 0x00, 0x01, 0x42, 0x00, 0x01, 0x41, 0x01, 0xff,
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07007869};
7870
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03007871int32_t SpiffyIndefiniteLengthStringsTests(void)
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07007872{
7873 QCBORDecodeContext DCtx;
7874
7875 QCBORDecode_Init(&DCtx,
7876 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spMapWithIndefLenStrings),
7877 QCBOR_DECODE_MODE_NORMAL);
7878
Laurence Lundblade8510f8c2020-12-01 11:31:16 -08007879 UsefulBuf_MAKE_STACK_UB(StringBuf, 200);
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07007880 QCBORDecode_SetMemPool(&DCtx, StringBuf, false);
7881
7882 UsefulBufC ByteString;
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07007883 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07007884 QCBORDecode_GetByteStringInMapSZ(&DCtx, "label1", &ByteString);
Laurence Lundblade37286c02022-09-03 10:05:02 -07007885
7886#ifndef QCBOR_DISABLE_TAGS
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07007887 if(QCBORDecode_GetAndResetError(&DCtx)) {
7888 return 1;
7889 }
7890
7891 const uint8_t pExectedBytes[] = {0x01, 0x02, 0x03, 0x04, 0x05};
7892 if(UsefulBuf_Compare(ByteString, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pExectedBytes))) {
7893 return 2;
7894 }
7895
7896 uint64_t uInt;
7897 QCBORDecode_GetUInt64InMapSZ(&DCtx, "dymo", &uInt);
7898 if(QCBORDecode_GetAndResetError(&DCtx)) {
7899 return 3;
7900 }
7901 if(uInt != 3) {
7902 return 4;
7903 }
7904
Máté Tóth-Pálef5f07a2021-09-17 19:31:37 +02007905#ifndef USEFULBUF_DISABLE_ALL_FLOAT
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07007906 double uDouble;
7907 QCBORDecode_GetDoubleConvertAllInMapSZ(&DCtx,
7908 "label2",
7909 0xff,
7910 &uDouble);
Laurence Lundblade37286c02022-09-03 10:05:02 -07007911
Laurence Lundbladeb8e19aa2020-10-07 20:59:11 -07007912#ifndef QCBOR_DISABLE_FLOAT_HW_USE
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07007913 if(QCBORDecode_GetAndResetError(&DCtx)) {
7914 return 5;
7915 }
7916 if(uDouble != -16777474) {
7917 return 6;
7918 }
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08007919#else /* QCBOR_DISABLE_FLOAT_HW_USE */
Laurence Lundbladeb8e19aa2020-10-07 20:59:11 -07007920 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_HW_FLOAT_DISABLED) {
7921 return 7;
7922 }
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08007923#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
Máté Tóth-Pálef5f07a2021-09-17 19:31:37 +02007924#endif /* USEFULBUF_DISABLE_ALL_FLOAT */
Laurence Lundbladeb8e19aa2020-10-07 20:59:11 -07007925
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07007926 QCBORDecode_ExitMap(&DCtx);
7927
7928 if(QCBORDecode_Finish(&DCtx)) {
7929 return 99;
7930 }
7931
Laurence Lundblade37286c02022-09-03 10:05:02 -07007932#else /* QCBOR_DISABLE_TAGS */
7933 /* The big num in the input is a CBOR tag and you can't do
7934 * map lookups in a map with a tag so this test does very little
7935 * when tags are disabled. That is OK, the test coverage is still
7936 * good when they are not.
7937 */
7938 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_TAGS_DISABLED) {
7939 return 1002;
7940 }
7941#endif /*QCBOR_DISABLE_TAGS */
7942
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07007943 return 0;
7944}
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08007945#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
Laurence Lundblade2a26abb2020-11-05 19:06:54 -08007946
7947
Laurence Lundbladecf41c522021-02-20 10:19:07 -07007948/*
7949 * An array of an integer and an array. The second array contains
7950 * a bstr-wrapped map.
7951 *
7952 * [7, [h'A36D6669... (see next lines) 73']]
7953 *
7954 * {"first integer": 42,
7955 * "an array of two strings": ["string1", "string2"],
7956 * "map in a map":
7957 * { "bytes 1": h'78787878',
7958 * "bytes 2": h'79797979',
7959 * "another int": 98,
7960 * "text 2": "lies, damn lies and statistics"
7961 * }
7962 * }
7963 */
Laurence Lundblade2a26abb2020-11-05 19:06:54 -08007964
Laurence Lundbladecf41c522021-02-20 10:19:07 -07007965static const uint8_t pValidWrappedMapEncoded[] = {
7966 0x82, 0x07, 0x81, 0x58, 0x97,
7967 0xa3, 0x6d, 0x66, 0x69, 0x72, 0x73, 0x74, 0x20, 0x69, 0x6e,
7968 0x74, 0x65, 0x67, 0x65, 0x72, 0x18, 0x2a, 0x77, 0x61, 0x6e,
7969 0x20, 0x61, 0x72, 0x72, 0x61, 0x79, 0x20, 0x6f, 0x66, 0x20,
7970 0x74, 0x77, 0x6f, 0x20, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67,
7971 0x73, 0x82, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x31,
7972 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x32, 0x6c, 0x6d,
7973 0x61, 0x70, 0x20, 0x69, 0x6e, 0x20, 0x61, 0x20, 0x6d, 0x61,
7974 0x70, 0xa4, 0x67, 0x62, 0x79, 0x74, 0x65, 0x73, 0x20, 0x31,
7975 0x44, 0x78, 0x78, 0x78, 0x78, 0x67, 0x62, 0x79, 0x74, 0x65,
7976 0x73, 0x20, 0x32, 0x44, 0x79, 0x79, 0x79, 0x79, 0x6b, 0x61,
7977 0x6e, 0x6f, 0x74, 0x68, 0x65, 0x72, 0x20, 0x69, 0x6e, 0x74,
7978 0x18, 0x62, 0x66, 0x74, 0x65, 0x78, 0x74, 0x20, 0x32, 0x78,
7979 0x1e, 0x6c, 0x69, 0x65, 0x73, 0x2c, 0x20, 0x64, 0x61, 0x6d,
7980 0x6e, 0x20, 0x6c, 0x69, 0x65, 0x73, 0x20, 0x61, 0x6e, 0x64,
7981 0x20, 0x73, 0x74, 0x61, 0x74, 0x69, 0x73, 0x74, 0x69, 0x63,
7982 0x73
7983};
7984
7985#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
7986
7987/* As above, but the arrays are indefinite length */
7988static const uint8_t pValidIndefWrappedMapEncoded[] = {
7989 0x9f, 0x07, 0x9f, 0x58, 0x97,
7990 0xa3, 0x6d, 0x66, 0x69, 0x72, 0x73, 0x74, 0x20, 0x69, 0x6e,
7991 0x74, 0x65, 0x67, 0x65, 0x72, 0x18, 0x2a, 0x77, 0x61, 0x6e,
7992 0x20, 0x61, 0x72, 0x72, 0x61, 0x79, 0x20, 0x6f, 0x66, 0x20,
7993 0x74, 0x77, 0x6f, 0x20, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67,
7994 0x73, 0x82, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x31,
7995 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x32, 0x6c, 0x6d,
7996 0x61, 0x70, 0x20, 0x69, 0x6e, 0x20, 0x61, 0x20, 0x6d, 0x61,
7997 0x70, 0xa4, 0x67, 0x62, 0x79, 0x74, 0x65, 0x73, 0x20, 0x31,
7998 0x44, 0x78, 0x78, 0x78, 0x78, 0x67, 0x62, 0x79, 0x74, 0x65,
7999 0x73, 0x20, 0x32, 0x44, 0x79, 0x79, 0x79, 0x79, 0x6b, 0x61,
8000 0x6e, 0x6f, 0x74, 0x68, 0x65, 0x72, 0x20, 0x69, 0x6e, 0x74,
8001 0x18, 0x62, 0x66, 0x74, 0x65, 0x78, 0x74, 0x20, 0x32, 0x78,
8002 0x1e, 0x6c, 0x69, 0x65, 0x73, 0x2c, 0x20, 0x64, 0x61, 0x6d,
8003 0x6e, 0x20, 0x6c, 0x69, 0x65, 0x73, 0x20, 0x61, 0x6e, 0x64,
8004 0x20, 0x73, 0x74, 0x61, 0x74, 0x69, 0x73, 0x74, 0x69, 0x63,
8005 0x73,
8006 0xff, 0xff
8007};
8008#endif
8009
8010
8011static const uint8_t pWithEmptyMap[] = {0x82, 0x18, 0x64, 0xa0};
8012
8013#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
8014static const uint8_t pWithEmptyMapInDef[] = {0x9f, 0x18, 0x64, 0xbf, 0xff, 0xff};
8015#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
8016
8017#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
Laurence Lundblade37286c02022-09-03 10:05:02 -07008018/*
Laurence Lundbladed6b33f52024-03-06 12:51:15 -08008019 * An array of one that contains a byte string that is an indefinite
8020 * length string that CBOR wraps an array of three numbers [42, 43,
8021 * 44]. The byte string is an implicit tag 24.
8022 *
8023 * [
8024 * (_ h'83', h'18', h'2A182B', h'182C')
8025 * ]
Laurence Lundblade37286c02022-09-03 10:05:02 -07008026 */
Laurence Lundbladecf41c522021-02-20 10:19:07 -07008027static const uint8_t pWrappedByIndefiniteLength[] = {
8028 0x81,
Laurence Lundbladecf41c522021-02-20 10:19:07 -07008029 0x5f,
8030 0x41, 0x83,
8031 0x41, 0x18,
8032 0x43, 0x2A, 0x18, 0x2B,
8033 0x42, 0x18, 0x2C,
8034 0xff
8035};
8036#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
8037
8038
Maxim Zhukovd538f0a2022-12-20 20:40:38 +03008039int32_t PeekAndRewindTest(void)
Laurence Lundblade2a26abb2020-11-05 19:06:54 -08008040{
8041 QCBORItem Item;
8042 QCBORError nCBORError;
8043 QCBORDecodeContext DCtx;
8044
8045 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0);
8046
8047 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
8048 return 100+(int32_t)nCBORError;
8049 }
8050 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
8051 return 200;
8052 }
8053
Laurence Lundblade3427dee2021-06-20 11:11:24 -07008054 QCBORDecode_VPeekNext(&DCtx, &Item);
8055 if((nCBORError = QCBORDecode_GetError(&DCtx))) {
8056 return 150+(int32_t)nCBORError;
8057 }
8058 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
8059 return 250;
8060 }
8061
Laurence Lundblade2a26abb2020-11-05 19:06:54 -08008062 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
8063 return (int32_t)nCBORError;
8064 }
8065 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
8066 return 300;
8067 }
8068
8069 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
8070 return 400 + (int32_t)nCBORError;
8071 }
8072 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
8073 return 500;
8074 }
8075
8076 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8077 return (int32_t)nCBORError;
8078 }
8079 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
8080 return 600;
8081 }
8082
8083 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
8084 return 900 + (int32_t)nCBORError;
8085 }
8086 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
8087 Item.uDataType != QCBOR_TYPE_INT64 ||
8088 Item.val.int64 != 42 ||
8089 Item.uDataAlloc ||
8090 Item.uLabelAlloc ||
8091 UsefulBufCompareToSZ(Item.label.string, "first integer")) {
8092 return 1000;
8093 }
8094
8095 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8096 return 1100 + (int32_t)nCBORError;
8097 }
8098
8099 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
8100 Item.uDataType != QCBOR_TYPE_INT64 ||
8101 Item.val.int64 != 42 ||
8102 Item.uDataAlloc ||
8103 Item.uLabelAlloc ||
8104 UsefulBufCompareToSZ(Item.label.string, "first integer")) {
8105 return 1200;
8106 }
8107
8108
8109 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8110 return 1300 + (int32_t)nCBORError;
8111 }
8112 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
8113 Item.uDataAlloc ||
8114 Item.uLabelAlloc ||
8115 UsefulBufCompareToSZ(Item.label.string, "an array of two strings") ||
8116 Item.uDataType != QCBOR_TYPE_ARRAY ||
Laurence Lundbladecf41c522021-02-20 10:19:07 -07008117 Item.val.uCount != 2) {
Laurence Lundblade2a26abb2020-11-05 19:06:54 -08008118 return 1400;
Laurence Lundbladecf41c522021-02-20 10:19:07 -07008119 }
Laurence Lundblade2a26abb2020-11-05 19:06:54 -08008120
8121 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8122 return 1500 + (int32_t)nCBORError;
8123 }
8124 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
8125 Item.uDataAlloc ||
8126 Item.uLabelAlloc ||
8127 UsefulBufCompareToSZ(Item.val.string, "string1")) {
8128 return 1600;
8129 }
8130
8131 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
8132 return 1700 + (int32_t)nCBORError;
8133 }
8134 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
8135 Item.uDataAlloc ||
8136 Item.uLabelAlloc ||
8137 UsefulBufCompareToSZ(Item.val.string, "string2")) {
8138 return 1800;
8139 }
8140
8141 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
8142 return (int32_t)nCBORError;
8143 }
8144 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
8145 Item.uDataAlloc ||
8146 Item.uLabelAlloc ||
8147 UsefulBufCompareToSZ(Item.val.string, "string2")) {
8148 return 1900;
8149 }
8150
8151 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8152 return (int32_t)nCBORError;
8153 }
8154 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
8155 Item.uDataAlloc ||
8156 Item.uLabelAlloc ||
8157 UsefulBufCompareToSZ(Item.val.string, "string2")) {
8158 return 2000;
8159 }
8160
8161
8162 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8163 return 2100 + (int32_t)nCBORError;
8164 }
8165 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
8166 Item.uDataAlloc ||
8167 Item.uLabelAlloc ||
8168 UsefulBufCompareToSZ(Item.label.string, "map in a map") ||
8169 Item.uDataType != QCBOR_TYPE_MAP ||
8170 Item.val.uCount != 4) {
8171 return 2100;
8172 }
8173
8174 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8175 return 2200 + (int32_t)nCBORError;
8176 }
8177 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
8178 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("bytes 1"))||
8179 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
8180 Item.uDataAlloc ||
8181 Item.uLabelAlloc ||
8182 UsefulBufCompareToSZ(Item.val.string, "xxxx")) {
8183 return 2300;
8184 }
8185
8186 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
8187 return 2400 + (int32_t)nCBORError;
8188 }
8189 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
8190 UsefulBufCompareToSZ(Item.label.string, "bytes 2") ||
8191 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
8192 Item.uDataAlloc ||
8193 Item.uLabelAlloc ||
8194 UsefulBufCompareToSZ(Item.val.string, "yyyy")) {
8195 return 2500;
8196 }
8197
8198 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8199 return 2600 + (int32_t)nCBORError;
8200 }
8201 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
8202 UsefulBufCompareToSZ(Item.label.string, "bytes 2") ||
8203 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
8204 Item.uDataAlloc ||
8205 Item.uLabelAlloc ||
8206 UsefulBufCompareToSZ(Item.val.string, "yyyy")) {
8207 return 2700;
8208 }
8209
8210 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8211 return 2800 + (int32_t)nCBORError;
8212 }
8213 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
8214 Item.uDataAlloc ||
8215 Item.uLabelAlloc ||
8216 UsefulBufCompareToSZ(Item.label.string, "another int") ||
8217 Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundbladecf41c522021-02-20 10:19:07 -07008218 Item.val.int64 != 98) {
Laurence Lundblade2a26abb2020-11-05 19:06:54 -08008219 return 2900;
Laurence Lundbladecf41c522021-02-20 10:19:07 -07008220 }
Laurence Lundblade2a26abb2020-11-05 19:06:54 -08008221
8222 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
8223 return 3000 + (int32_t)nCBORError;
8224 }
8225 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
8226 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("text 2"))||
8227 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
8228 Item.uDataAlloc ||
8229 Item.uLabelAlloc ||
8230 UsefulBufCompareToSZ(Item.val.string, "lies, damn lies and statistics")) {
8231 return 3100;
8232 }
8233
8234 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8235 return 3200 + (int32_t)nCBORError;
8236 }
8237 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
8238 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("text 2"))||
8239 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
8240 Item.uDataAlloc ||
8241 Item.uLabelAlloc ||
8242 UsefulBufCompareToSZ(Item.val.string, "lies, damn lies and statistics")) {
8243 return 3300;
8244 }
8245
Laurence Lundblade3427dee2021-06-20 11:11:24 -07008246 nCBORError = QCBORDecode_PeekNext(&DCtx, &Item);
8247 if(nCBORError != QCBOR_ERR_NO_MORE_ITEMS) {
8248 return 3300 + (int32_t)nCBORError;
8249 }
8250
8251 QCBORDecode_VPeekNext(&DCtx, &Item);
8252 nCBORError = QCBORDecode_GetError(&DCtx);
8253 if(nCBORError != QCBOR_ERR_NO_MORE_ITEMS) {
8254 return 3400 + (int32_t)nCBORError;
8255 }
8256
8257 QCBORDecode_VPeekNext(&DCtx, &Item);
8258 nCBORError = QCBORDecode_GetError(&DCtx);
8259 if(nCBORError != QCBOR_ERR_NO_MORE_ITEMS) {
8260 return 3500 + (int32_t)nCBORError;
8261 }
Laurence Lundbladecf41c522021-02-20 10:19:07 -07008262
8263
8264 // Rewind to top level after entering several maps
8265 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0);
8266
8267 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8268 return (int32_t)nCBORError;
8269 }
8270 if(Item.uDataType != QCBOR_TYPE_MAP ||
8271 Item.val.uCount != 3) {
8272 return 400;
8273 }
8274
8275 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8276 return 4000+(int32_t)nCBORError;
8277 }
8278
8279 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
8280 Item.uDataType != QCBOR_TYPE_INT64 ||
8281 Item.val.int64 != 42 ||
8282 Item.uDataAlloc ||
8283 Item.uLabelAlloc ||
8284 UsefulBufCompareToSZ(Item.label.string, "first integer")) {
8285 return 4100;
8286 }
8287
8288 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8289 return 4100+(int32_t)nCBORError;
8290 }
8291 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
8292 Item.uDataAlloc ||
8293 Item.uLabelAlloc ||
8294 UsefulBufCompareToSZ(Item.label.string, "an array of two strings") ||
8295 Item.uDataType != QCBOR_TYPE_ARRAY ||
8296 Item.val.uCount != 2) {
8297 return 4200;
8298 }
8299
8300 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8301 return 4200+(int32_t)nCBORError;
8302 }
8303 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
8304 Item.uDataAlloc ||
8305 Item.uLabelAlloc ||
8306 UsefulBufCompareToSZ(Item.val.string, "string1")) {
8307 return 4300;
8308 }
8309
8310 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8311 return 4300+(int32_t)nCBORError;
8312 }
8313 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
8314 Item.uDataAlloc ||
8315 Item.uLabelAlloc ||
8316 UsefulBufCompareToSZ(Item.val.string, "string2")) {
8317 return 4400;
8318 }
8319
8320 QCBORDecode_Rewind(&DCtx);
8321
8322 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8323 return 4400+(int32_t)nCBORError;
8324 }
8325 if(Item.uDataType != QCBOR_TYPE_MAP ||
8326 Item.val.uCount != 3) {
8327 return 4500;
8328 }
8329
8330 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8331 return (int32_t)nCBORError;
8332 }
8333
8334 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
8335 Item.uDataType != QCBOR_TYPE_INT64 ||
8336 Item.val.int64 != 42 ||
8337 Item.uDataAlloc ||
8338 Item.uLabelAlloc ||
8339 UsefulBufCompareToSZ(Item.label.string, "first integer")) {
8340 return 4600;
8341 }
8342
8343 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8344 return (int32_t)nCBORError;
8345 }
8346 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
8347 Item.uDataAlloc ||
8348 Item.uLabelAlloc ||
8349 UsefulBufCompareToSZ(Item.label.string, "an array of two strings") ||
8350 Item.uDataType != QCBOR_TYPE_ARRAY ||
8351 Item.val.uCount != 2) {
8352 return 4700;
8353 }
8354
8355 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8356 return (int32_t)nCBORError;
8357 }
8358 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
8359 Item.uDataAlloc ||
8360 Item.uLabelAlloc ||
8361 UsefulBufCompareToSZ(Item.val.string, "string1")) {
8362 return 4800;
8363 }
8364
8365 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8366 return 4900+(int32_t)nCBORError;
8367 }
8368 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
8369 Item.uDataAlloc ||
8370 Item.uLabelAlloc ||
8371 UsefulBufCompareToSZ(Item.val.string, "string2")) {
8372 return 5000;
8373 }
8374
8375
8376 // Rewind an entered map
8377 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0);
8378
8379 QCBORDecode_EnterMap(&DCtx, NULL);
8380
8381 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8382 return 5100+(int32_t)nCBORError;
8383 }
8384
8385 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
8386 Item.uDataType != QCBOR_TYPE_INT64 ||
8387 Item.val.int64 != 42 ||
8388 Item.uDataAlloc ||
8389 Item.uLabelAlloc ||
8390 UsefulBufCompareToSZ(Item.label.string, "first integer")) {
8391 return 5200;
8392 }
8393
8394 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8395 return 5200+(int32_t)nCBORError;
8396 }
8397 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
8398 Item.uDataAlloc ||
8399 Item.uLabelAlloc ||
8400 UsefulBufCompareToSZ(Item.label.string, "an array of two strings") ||
8401 Item.uDataType != QCBOR_TYPE_ARRAY ||
8402 Item.val.uCount != 2) {
8403 return -5300;
8404 }
8405
8406 QCBORDecode_Rewind(&DCtx);
8407
8408 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8409 return 5300+(int32_t)nCBORError;
8410 }
8411
8412 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
8413 Item.uDataType != QCBOR_TYPE_INT64 ||
8414 Item.val.int64 != 42 ||
8415 Item.uDataAlloc ||
8416 Item.uLabelAlloc ||
8417 UsefulBufCompareToSZ(Item.label.string, "first integer")) {
8418 return 5400;
8419 }
8420
8421 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8422 return 5400+(int32_t)nCBORError;
8423 }
8424 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
8425 Item.uDataAlloc ||
8426 Item.uLabelAlloc ||
8427 UsefulBufCompareToSZ(Item.label.string, "an array of two strings") ||
8428 Item.uDataType != QCBOR_TYPE_ARRAY ||
8429 Item.val.uCount != 2) {
8430 return 5500;
8431 }
8432
8433
8434 // Rewind and entered array inside an entered map
8435 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0);
8436
8437 QCBORDecode_EnterMap(&DCtx, NULL);
8438
8439 QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings");
8440
8441 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8442 return 5600+(int32_t)nCBORError;
8443 }
8444 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
8445 Item.uDataAlloc ||
8446 Item.uLabelAlloc ||
8447 UsefulBufCompareToSZ(Item.val.string, "string1")) {
8448 return 5700;
8449 }
8450
8451 QCBORDecode_Rewind(&DCtx);
8452
8453 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8454 return 5700+(int32_t)nCBORError;
8455 }
8456 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
8457 Item.uDataAlloc ||
8458 Item.uLabelAlloc ||
8459 UsefulBufCompareToSZ(Item.val.string, "string1")) {
8460 return 5800;
8461 }
8462
8463 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8464 return (int32_t)nCBORError;
8465 }
8466 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
8467 Item.uDataAlloc ||
8468 Item.uLabelAlloc ||
8469 UsefulBufCompareToSZ(Item.val.string, "string2")) {
8470 return 5900;
8471 }
8472
8473 QCBORDecode_Rewind(&DCtx);
8474
8475 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8476 return 5900+(int32_t)nCBORError;
8477 }
8478 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
8479 Item.uDataAlloc ||
8480 Item.uLabelAlloc ||
8481 UsefulBufCompareToSZ(Item.val.string, "string1")) {
8482 return 6000;
8483 }
8484
8485
8486 // Rewind a byte string inside an array inside an array
8487 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidWrappedMapEncoded), 0);
8488
8489 QCBORDecode_EnterArray(&DCtx, NULL);
8490
8491 uint64_t i;
8492 QCBORDecode_GetUInt64(&DCtx, &i);
8493
8494 QCBORDecode_EnterArray(&DCtx, NULL);
8495
8496 QCBORDecode_EnterBstrWrapped(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
8497 if(QCBORDecode_GetError(&DCtx)) {
8498 return 6100;
8499 }
8500
8501 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8502 return (int32_t)nCBORError;
8503 }
8504 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
8505 return 6200;
8506 }
8507
8508 QCBORDecode_Rewind(&DCtx);
8509
8510 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8511 return 6300+(int32_t)nCBORError;
8512 }
8513 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
8514 return 6400;
8515 }
8516
8517#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
8518 // Rewind a byte string inside an indefinite-length array inside
8519 // indefinite-length array
8520
8521 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidIndefWrappedMapEncoded), 0);
8522
8523 QCBORDecode_EnterArray(&DCtx, NULL);
8524
8525 QCBORDecode_GetUInt64(&DCtx, &i);
8526
8527 QCBORDecode_EnterArray(&DCtx, NULL);
8528
8529 QCBORDecode_EnterBstrWrapped(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
8530 if(QCBORDecode_GetError(&DCtx)) {
8531 return 6500;
8532 }
8533
8534 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8535 return 6600+(int32_t)nCBORError;
8536 }
8537 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
8538 return 6700;
8539 }
8540
8541 QCBORDecode_Rewind(&DCtx);
8542
8543 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
8544 return 6800+(int32_t)nCBORError;
8545 }
8546 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
8547 return 6900;
8548 }
8549#endif
8550
8551 // Rewind an empty map
8552 // [100, {}]
8553 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pWithEmptyMap), 0);
8554 QCBORDecode_EnterArray(&DCtx, NULL);
8555 QCBORDecode_GetUInt64(&DCtx, &i);
8556 if(i != 100) {
8557 return 7010;
8558 }
8559 QCBORDecode_EnterMap(&DCtx, NULL);
8560
8561 /* Do it 5 times to be sure multiple rewinds work */
8562 for(int n = 0; n < 5; n++) {
8563 nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
8564 if(nCBORError != QCBOR_ERR_NO_MORE_ITEMS) {
8565 return 7000 + n;
8566 }
8567 QCBORDecode_Rewind(&DCtx);
8568 }
8569 QCBORDecode_ExitMap(&DCtx);
8570 QCBORDecode_Rewind(&DCtx);
8571 QCBORDecode_GetUInt64(&DCtx, &i);
8572 if(i != 100) {
8573 return 7010;
8574 }
8575 QCBORDecode_ExitArray(&DCtx);
8576 QCBORDecode_Rewind(&DCtx);
8577 QCBORDecode_EnterArray(&DCtx, NULL);
8578 i = 9;
8579 QCBORDecode_GetUInt64(&DCtx, &i);
8580 if(i != 100) {
8581 return 7020;
8582 }
8583 if(QCBORDecode_GetError(&DCtx)){
8584 return 7030;
8585 }
8586
8587 // Rewind an empty indefinite length map
8588#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
8589 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pWithEmptyMapInDef), 0);
8590 QCBORDecode_EnterArray(&DCtx, NULL);
8591 QCBORDecode_GetUInt64(&DCtx, &i);
8592 if(i != 100) {
8593 return 7810;
8594 }
8595 QCBORDecode_EnterMap(&DCtx, NULL);
8596
8597 /* Do it 5 times to be sure multiple rewinds work */
8598 for(int n = 0; n < 5; n++) {
8599 nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
8600 if(nCBORError != QCBOR_ERR_NO_MORE_ITEMS) {
8601 return 7800 + n;
8602 }
8603 QCBORDecode_Rewind(&DCtx);
8604 }
8605 QCBORDecode_ExitMap(&DCtx);
8606 QCBORDecode_Rewind(&DCtx);
8607 QCBORDecode_GetUInt64(&DCtx, &i);
8608 if(i != 100) {
8609 return 7810;
8610 }
8611 QCBORDecode_ExitArray(&DCtx);
8612 QCBORDecode_Rewind(&DCtx);
8613 QCBORDecode_EnterArray(&DCtx, NULL);
8614 i = 9;
8615 QCBORDecode_GetUInt64(&DCtx, &i);
8616 if(i != 100) {
8617 return 7820;
8618 }
8619 if(QCBORDecode_GetError(&DCtx)){
8620 return 7830;
8621 }
8622#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
8623
8624 // Rewind an indefnite length byte-string wrapped sequence
8625#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
8626 QCBORDecode_Init(&DCtx,
8627 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pWrappedByIndefiniteLength),
8628 0);
8629 UsefulBuf_MAKE_STACK_UB(Pool, 100);
8630 QCBORDecode_SetMemPool(&DCtx, Pool, 0);
8631
8632 QCBORDecode_EnterArray(&DCtx, NULL);
8633 QCBORDecode_EnterBstrWrapped(&DCtx, 2, NULL);
8634 if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_INPUT_TOO_LARGE) {
Laurence Lundblade37286c02022-09-03 10:05:02 -07008635 /* TODO: This is what happens when trying to enter
8636 * indefinite-length byte string wrapped CBOR. Tolerate for
8637 * now. Eventually it needs to be fixed so this works, but that
8638 * is not simple.
8639 */
Laurence Lundbladecf41c522021-02-20 10:19:07 -07008640 return 7300;
8641 }
8642
8643 /*
8644 QCBORDecode_GetUInt64(&DCtx, &i);
8645 if(i != 42) {
8646 return 7110;
8647 }
8648 QCBORDecode_Rewind(&DCtx);
8649 QCBORDecode_GetUInt64(&DCtx, &i);
8650 if(i != 42) {
8651 return 7220;
8652 }*/
Laurence Lundblade37286c02022-09-03 10:05:02 -07008653
Laurence Lundbladecf41c522021-02-20 10:19:07 -07008654#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
8655
8656
8657 // Rewind an indefnite length byte-string wrapped sequence
8658
Laurence Lundblade2a26abb2020-11-05 19:06:54 -08008659 return 0;
8660}
Laurence Lundblade9f9c3732021-03-23 09:38:46 -07008661
8662
8663
8664
8665static const uint8_t spBooleansInMap[] =
8666{
8667 0xa1, 0x08, 0xf5
8668};
8669
8670static const uint8_t spBooleansInMapWrongType[] =
8671{
8672 0xa1, 0x08, 0xf6
8673};
8674
8675static const uint8_t spBooleansInMapNWF[] =
8676{
8677 0xa1, 0x08, 0x1a
8678};
8679
Laurence Lundblade8782dd32021-04-27 04:15:37 -07008680static const uint8_t spNullInMap[] =
8681{
8682 0xa1, 0x08, 0xf6
8683};
8684
8685static const uint8_t spUndefinedInMap[] =
8686{
8687 0xa1, 0x08, 0xf7
8688};
8689
Laurence Lundblade9f9c3732021-03-23 09:38:46 -07008690
8691int32_t BoolTest(void)
8692{
8693 QCBORDecodeContext DCtx;
8694 bool b;
8695
Laurence Lundblade8782dd32021-04-27 04:15:37 -07008696 QCBORDecode_Init(&DCtx,
8697 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMap),
8698 0);
Laurence Lundblade9f9c3732021-03-23 09:38:46 -07008699 QCBORDecode_EnterMap(&DCtx, NULL);
8700 QCBORDecode_GetBool(&DCtx, &b);
8701 if(QCBORDecode_GetAndResetError(&DCtx) || !b) {
8702 return 1;
8703 }
8704
8705 QCBORDecode_GetBoolInMapN(&DCtx, 7, &b);
8706 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_LABEL_NOT_FOUND) {
8707 return 2;
8708 }
8709
8710 QCBORDecode_GetBoolInMapN(&DCtx, 8, &b);
8711 if(QCBORDecode_GetAndResetError(&DCtx) || !b) {
8712 return 3;
8713 }
8714
8715
8716 QCBORDecode_GetBoolInMapSZ(&DCtx, "xx", &b);
8717 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_LABEL_NOT_FOUND) {
8718 return 4;
8719 }
8720
Laurence Lundblade8782dd32021-04-27 04:15:37 -07008721 QCBORDecode_Init(&DCtx,
8722 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMapWrongType),
8723 0);
Laurence Lundblade9f9c3732021-03-23 09:38:46 -07008724 QCBORDecode_EnterMap(&DCtx, NULL);
8725 QCBORDecode_GetBool(&DCtx, &b);
8726 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
8727 return 5;
8728 }
8729
Laurence Lundblade8782dd32021-04-27 04:15:37 -07008730 QCBORDecode_Init(&DCtx,
8731 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMapNWF),
8732 0);
Laurence Lundblade9f9c3732021-03-23 09:38:46 -07008733 QCBORDecode_EnterMap(&DCtx, NULL);
8734 QCBORDecode_GetBool(&DCtx, &b);
8735 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_HIT_END) {
8736 return 6;
8737 }
8738
Laurence Lundblade8782dd32021-04-27 04:15:37 -07008739
8740 QCBORDecode_Init(&DCtx,
8741 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spNullInMap),
8742 0);
8743 QCBORDecode_EnterMap(&DCtx, NULL);
8744 QCBORDecode_GetNull(&DCtx);
8745 if(QCBORDecode_GetAndResetError(&DCtx)) {
8746 return 7;
8747 }
8748
8749 QCBORDecode_Init(&DCtx,
8750 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMap),
8751 0);
8752 QCBORDecode_EnterMap(&DCtx, NULL);
8753 QCBORDecode_GetNull(&DCtx);
8754 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
8755 return 8;
8756 }
8757
8758 QCBORDecode_Init(&DCtx,
8759 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spNullInMap),
8760 0);
8761 QCBORDecode_EnterMap(&DCtx, NULL);
8762 QCBORDecode_GetNullInMapN(&DCtx, 8);
8763 if(QCBORDecode_GetAndResetError(&DCtx)) {
8764 return 9;
8765 }
8766
8767 QCBORDecode_Init(&DCtx,
8768 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMap),
8769 0);
8770 QCBORDecode_EnterMap(&DCtx, NULL);
8771 QCBORDecode_GetNullInMapN(&DCtx, 8);
8772 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
8773 return 10;
8774 }
8775
8776 QCBORDecode_Init(&DCtx,
8777 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMapNWF),
8778 0);
8779 QCBORDecode_EnterMap(&DCtx, NULL);
8780 QCBORDecode_GetUndefined(&DCtx);
8781 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_HIT_END) {
8782 return 11;
8783 }
8784
8785 QCBORDecode_Init(&DCtx,
8786 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUndefinedInMap),
8787 0);
8788 QCBORDecode_EnterMap(&DCtx, NULL);
8789 QCBORDecode_GetUndefined(&DCtx);
8790 if(QCBORDecode_GetAndResetError(&DCtx)) {
8791 return 12;
8792 }
8793
8794 QCBORDecode_Init(&DCtx,
8795 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMap),
8796 0);
8797 QCBORDecode_EnterMap(&DCtx, NULL);
8798 QCBORDecode_GetUndefined(&DCtx);
8799 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
8800 return 13;
8801 }
8802
8803 QCBORDecode_Init(&DCtx,
8804 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUndefinedInMap),
8805 0);
8806 QCBORDecode_EnterMap(&DCtx, NULL);
8807 QCBORDecode_GetUndefinedInMapN(&DCtx, 8);
8808 if(QCBORDecode_GetAndResetError(&DCtx)) {
8809 return 14;
8810 }
8811
8812 QCBORDecode_Init(&DCtx,
8813 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMap),
8814 0);
8815 QCBORDecode_EnterMap(&DCtx, NULL);
8816 QCBORDecode_GetUndefinedInMapN(&DCtx, 8);
8817 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
8818 return 15;
8819 }
8820
8821 QCBORDecode_Init(&DCtx,
8822 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMapNWF),
8823 0);
8824 QCBORDecode_EnterMap(&DCtx, NULL);
8825 QCBORDecode_GetUndefined(&DCtx);
8826 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_HIT_END) {
8827 return 15;
8828 }
8829
Laurence Lundblade9f9c3732021-03-23 09:38:46 -07008830 return 0;
8831}
Laurence Lundbladef00b8be2024-03-08 10:34:33 -08008832
8833
8834int32_t
8835ErrorHandlingTests(void)
8836{
8837 QCBORDecodeContext DCtx;
8838 QCBORItem Item;
8839 QCBORError uError;
8840 int64_t integer;
8841
8842 /* Test QCBORDecode_SetError() */
8843 QCBORDecode_Init(&DCtx,
8844 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded),
8845 QCBOR_DECODE_MODE_NORMAL);
8846
8847 QCBORDecode_SetError(&DCtx, QCBOR_ERR_FIRST_USER_DEFINED);
8848
8849 QCBORDecode_VGetNext(&DCtx, &Item);
8850
8851 uError = QCBORDecode_GetError(&DCtx);
8852
8853 if(uError != QCBOR_ERR_FIRST_USER_DEFINED) {
8854 return -1;
8855 }
8856
8857 if(Item.uLabelType != QCBOR_TYPE_NONE ||
8858 Item.uDataType != QCBOR_TYPE_NONE) {
8859 return -2;
8860 }
8861
8862
8863 /* Test data type returned from previous error */
8864 QCBORDecode_Init(&DCtx,
8865 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded),
8866 QCBOR_DECODE_MODE_NORMAL);
8867 QCBORDecode_GetInt64(&DCtx, &integer);
8868 uError = QCBORDecode_GetError(&DCtx);
8869 if(uError != QCBOR_ERR_UNEXPECTED_TYPE) {
8870 return -3;
8871 }
8872
8873 QCBORDecode_VGetNext(&DCtx, &Item);
8874 if(Item.uLabelType != QCBOR_TYPE_NONE ||
8875 Item.uDataType != QCBOR_TYPE_NONE) {
8876 return -2;
8877 }
8878 uError = QCBORDecode_GetError(&DCtx);
8879 if(uError != QCBOR_ERR_UNEXPECTED_TYPE) {
8880 return -3;
8881 }
8882
8883
8884 /* Test error classification functions */
8885
8886 if(!QCBORDecode_IsUnrecoverableError(QCBOR_ERR_INDEFINITE_STRING_CHUNK)) {
8887 return -10;
8888 }
8889 if(QCBORDecode_IsUnrecoverableError(QCBOR_SUCCESS)) {
8890 return -11;
8891 }
8892 if(!QCBORDecode_IsUnrecoverableError(QCBOR_ERR_INDEFINITE_STRING_CHUNK)) {
8893 return -12;
8894 }
8895 if(QCBORDecode_IsUnrecoverableError(QCBOR_ERR_DUPLICATE_LABEL)) {
8896 return -13;
8897 }
8898
8899 if(!QCBORDecode_IsNotWellFormedError(QCBOR_ERR_BAD_TYPE_7)) {
8900 return -20;
8901 }
8902 if(!QCBORDecode_IsNotWellFormedError(QCBOR_ERR_BAD_BREAK)) {
8903 return -21;
8904 }
8905 if(QCBORDecode_IsNotWellFormedError(QCBOR_SUCCESS)) {
8906 return -22;
8907 }
8908 if(QCBORDecode_IsNotWellFormedError(QCBOR_ERR_ARRAY_DECODE_TOO_LONG)) {
8909 return -23;
8910 }
8911
8912 return 0;
8913}