blob: 431836cdca3099bcafb1d61ef5d1f01e52a97036 [file] [log] [blame]
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001/*==============================================================================
Laurence Lundbladed92a6162018-11-01 11:38:35 +07002 Copyright (c) 2016-2018, The Linux Foundation.
Laurence Lundbladeb24faef2022-04-26 11:03:08 -06003 Copyright (c) 2018-2022, Laurence Lundblade.
Máté Tóth-Pálef5f07a2021-09-17 19:31:37 +02004 Copyright (c) 2021, Arm Limited.
Laurence Lundbladed92a6162018-11-01 11:38:35 +07005 All rights reserved.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08006
Laurence Lundblade0dbc9172018-11-01 14:17:21 +07007Redistribution and use in source and binary forms, with or without
8modification, are permitted provided that the following conditions are
9met:
10 * Redistributions of source code must retain the above copyright
11 notice, this list of conditions and the following disclaimer.
12 * Redistributions in binary form must reproduce the above
13 copyright notice, this list of conditions and the following
14 disclaimer in the documentation and/or other materials provided
15 with the distribution.
16 * Neither the name of The Linux Foundation nor the names of its
17 contributors, nor the name "Laurence Lundblade" may be used to
18 endorse or promote products derived from this software without
19 specific prior written permission.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -080020
Laurence Lundblade0dbc9172018-11-01 14:17:21 +070021THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESS OR IMPLIED
22WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
23MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT
24ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
25BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
28BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
30OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
31IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Laurence Lundbladeee851742020-01-08 08:37:05 -080032 =============================================================================*/
Laurence Lundblade9e3651c2018-10-10 11:49:55 +080033
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080034#include "qcbor_decode_tests.h"
Laurence Lundblade844bb5c2020-03-01 17:27:25 -080035#include "qcbor/qcbor_encode.h"
36#include "qcbor/qcbor_decode.h"
Laurence Lundblade67257dc2020-07-27 03:33:37 -070037#include "qcbor/qcbor_spiffy_decode.h"
Laurence Lundbladed4728fd2018-12-17 15:15:56 -080038#include <string.h>
Laurence Lundblade9e3651c2018-10-10 11:49:55 +080039#include <math.h> // for fabs()
Laurence Lundbladebb1062e2019-08-12 23:28:54 -070040#include "not_well_formed_cbor.h"
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -080041
Laurence Lundblade9b334962020-08-27 10:55:53 -070042// Handy macro to compare a UsefulBuf to a C string
43#define UsefulBufCompareToSZ(x, y) \
44 UsefulBuf_Compare(x, UsefulBuf_FromSZ(y))
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080045
Laurence Lundbladea2e29072018-12-30 09:20:06 -080046#ifdef PRINT_FUNCTIONS_FOR_DEBUGGING
Laurence Lundblade20db9c92018-12-17 11:40:37 -080047#include <stdio.h>
Laurence Lundbladea2e29072018-12-30 09:20:06 -080048
49static void PrintUsefulBufC(const char *szLabel, UsefulBufC Buf)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080050{
51 if(szLabel) {
52 printf("%s ", szLabel);
53 }
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -080054
Laurence Lundblade570fab52018-10-13 18:28:27 +080055 size_t i;
Laurence Lundbladea2e29072018-12-30 09:20:06 -080056 for(i = 0; i < Buf.len; i++) {
57 uint8_t Z = ((uint8_t *)Buf.ptr)[i];
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080058 printf("%02x ", Z);
59 }
60 printf("\n");
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -080061
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080062 fflush(stdout);
63}
Laurence Lundbladee2c893c2020-12-26 17:41:53 -080064#endif /* PRINT_FUNCTIONS_FOR_DEBUGGING */
Laurence Lundblade2ded3d92018-10-09 21:36:11 +080065
Laurence Lundbladecc7da412020-12-27 00:09:07 -080066/*
67 [
68 -9223372036854775808,
69 -4294967297,
70 -4294967296,
71 -4294967295,
72 -4294967294,
73 -2147483648,
74 -2147483647,
75 -65538,
76 -65537,
77 -65536,
78 -65535,
79 -65534,
80 -257,
81 -256,
82 -255,
83 -254,
84 -25,
85 -24,
86 -23,
87 -1,
88 0,
89 0,
90 1,
91 22,
92 23,
93 24,
94 25,
95 26,
96 254,
97 255,
98 256,
99 257,
100 65534,
101 65535,
102 65536,
103 65537,
104 65538,
105 2147483647,
106 2147483647,
107 2147483648,
108 2147483649,
109 4294967294,
110 4294967295,
111 4294967296,
112 4294967297,
113 9223372036854775807,
114 18446744073709551615
115 ]
116 */
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800117
Laurence Lundbladeb836efb2018-10-28 20:09:58 +0700118static const uint8_t spExpectedEncodedInts[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800119 0x98, 0x2f, 0x3b, 0x7f, 0xff, 0xff, 0xff, 0xff,
120 0xff, 0xff, 0xff, 0x3b, 0x00, 0x00, 0x00, 0x01,
121 0x00, 0x00, 0x00, 0x00, 0x3a, 0xff, 0xff, 0xff,
122 0xff, 0x3a, 0xff, 0xff, 0xff, 0xfe, 0x3a, 0xff,
123 0xff, 0xff, 0xfd, 0x3a, 0x7f, 0xff, 0xff, 0xff,
124 0x3a, 0x7f, 0xff, 0xff, 0xfe, 0x3a, 0x00, 0x01,
125 0x00, 0x01, 0x3a, 0x00, 0x01, 0x00, 0x00, 0x39,
126 0xff, 0xff, 0x39, 0xff, 0xfe, 0x39, 0xff, 0xfd,
127 0x39, 0x01, 0x00, 0x38, 0xff, 0x38, 0xfe, 0x38,
128 0xfd, 0x38, 0x18, 0x37, 0x36, 0x20, 0x00, 0x00,
129 0x01, 0x16, 0x17, 0x18, 0x18, 0x18, 0x19, 0x18,
130 0x1a, 0x18, 0xfe, 0x18, 0xff, 0x19, 0x01, 0x00,
131 0x19, 0x01, 0x01, 0x19, 0xff, 0xfe, 0x19, 0xff,
132 0xff, 0x1a, 0x00, 0x01, 0x00, 0x00, 0x1a, 0x00,
133 0x01, 0x00, 0x01, 0x1a, 0x00, 0x01, 0x00, 0x02,
134 0x1a, 0x7f, 0xff, 0xff, 0xff, 0x1a, 0x7f, 0xff,
135 0xff, 0xff, 0x1a, 0x80, 0x00, 0x00, 0x00, 0x1a,
136 0x80, 0x00, 0x00, 0x01, 0x1a, 0xff, 0xff, 0xff,
137 0xfe, 0x1a, 0xff, 0xff, 0xff, 0xff, 0x1b, 0x00,
138 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x1b,
139 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x01,
140 0x1b, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
141 0xff, 0x1b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
142 0xff, 0xff};
143
144
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800145// return CBOR error or -1 if type of value doesn't match
146
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800147static int32_t IntegerValuesParseTestInternal(QCBORDecodeContext *pDCtx)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800148{
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700149 QCBORItem Item;
150 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800151
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800152 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700153 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800154 if(Item.uDataType != QCBOR_TYPE_ARRAY)
155 return -1;
156
157 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700158 return (int32_t)nCBORError;
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800159 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade570fab52018-10-13 18:28:27 +0800160 Item.val.int64 != -9223372036854775807LL - 1)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800161 return -1;
162
163 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700164 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800165 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade570fab52018-10-13 18:28:27 +0800166 Item.val.int64 != -4294967297)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800167 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800168
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800169 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700170 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800171 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade570fab52018-10-13 18:28:27 +0800172 Item.val.int64 != -4294967296)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800173 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800174
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800175 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700176 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800177 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade570fab52018-10-13 18:28:27 +0800178 Item.val.int64 != -4294967295)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800179 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800180
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800181 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700182 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800183 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade570fab52018-10-13 18:28:27 +0800184 Item.val.int64 != -4294967294)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800185 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800186
187
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800188 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700189 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800190 if(Item.uDataType != QCBOR_TYPE_INT64 ||
191 Item.val.int64 != -2147483648)
192 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800193
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800194 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700195 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800196 if(Item.uDataType != QCBOR_TYPE_INT64 ||
197 Item.val.int64 != -2147483647)
198 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800199
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800200 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700201 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800202 if(Item.uDataType != QCBOR_TYPE_INT64 ||
203 Item.val.int64 != -65538)
204 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800205
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800206 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700207 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800208 if(Item.uDataType != QCBOR_TYPE_INT64 ||
209 Item.val.int64 != -65537)
210 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800211
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800212 if((nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700213 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800214 if(Item.uDataType != QCBOR_TYPE_INT64 ||
215 Item.val.int64 != -65536)
216 return -1;
217
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800218
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800219 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700220 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800221 if(Item.uDataType != QCBOR_TYPE_INT64 ||
222 Item.val.int64 != -65535)
223 return -1;
224
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800225
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800226 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700227 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800228 if(Item.uDataType != QCBOR_TYPE_INT64 ||
229 Item.val.int64 != -65534)
230 return -1;
231
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800232
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800233 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700234 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800235 if(Item.uDataType != QCBOR_TYPE_INT64 ||
236 Item.val.int64 != -257)
237 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800238
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800239 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700240 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800241 if(Item.uDataType != QCBOR_TYPE_INT64 ||
242 Item.val.int64 != -256)
243 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800244
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800245 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700246 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800247 if(Item.uDataType != QCBOR_TYPE_INT64 ||
248 Item.val.int64 != -255)
249 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800250
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800251 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700252 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800253 if(Item.uDataType != QCBOR_TYPE_INT64 ||
254 Item.val.int64 != -254)
255 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800256
257
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800258 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700259 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800260 if(Item.uDataType != QCBOR_TYPE_INT64 ||
261 Item.val.int64 != -25)
262 return -1;
263
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800264
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800265 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700266 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800267 if(Item.uDataType != QCBOR_TYPE_INT64 ||
268 Item.val.int64 != -24)
269 return -1;
270
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800271
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800272 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700273 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800274 if(Item.uDataType != QCBOR_TYPE_INT64 ||
275 Item.val.int64 != -23)
276 return -1;
277
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800278
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800279 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700280 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800281 if(Item.uDataType != QCBOR_TYPE_INT64 ||
282 Item.val.int64 != -1)
283 return -1;
284
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800285
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800286 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700287 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800288 if(Item.uDataType != QCBOR_TYPE_INT64 ||
289 Item.val.int64 != 0)
290 return -1;
291
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800292
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800293 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700294 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800295 if(Item.uDataType != QCBOR_TYPE_INT64 ||
296 Item.val.int64 != 0)
297 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800298
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800299 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700300 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800301 if(Item.uDataType != QCBOR_TYPE_INT64 ||
302 Item.val.int64 != 1)
303 return -1;
304
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800305
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800306 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700307 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800308 if(Item.uDataType != QCBOR_TYPE_INT64 ||
309 Item.val.int64 != 22)
310 return -1;
311
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800312
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800313 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700314 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800315 if(Item.uDataType != QCBOR_TYPE_INT64 ||
316 Item.val.int64 != 23)
317 return -1;
318
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800319
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800320 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700321 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800322 if(Item.uDataType != QCBOR_TYPE_INT64 ||
323 Item.val.int64 != 24)
324 return -1;
325
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800326
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800327 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700328 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800329 if(Item.uDataType != QCBOR_TYPE_INT64 ||
330 Item.val.int64 != 25)
331 return -1;
332
333 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700334 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800335 if(Item.uDataType != QCBOR_TYPE_INT64 ||
336 Item.val.int64 != 26)
337 return -1;
338
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800339
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800340 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700341 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800342 if(Item.uDataType != QCBOR_TYPE_INT64 ||
343 Item.val.int64 != 254)
344 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800345
346
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800347 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700348 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800349 if(Item.uDataType != QCBOR_TYPE_INT64 ||
350 Item.val.int64 != 255)
351 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800352
353
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800354 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700355 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800356 if(Item.uDataType != QCBOR_TYPE_INT64 ||
357 Item.val.int64 != 256)
358 return -1;
359
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800360
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800361 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700362 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800363 if(Item.uDataType != QCBOR_TYPE_INT64 ||
364 Item.val.int64 != 257)
365 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800366
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800367 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700368 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800369 if(Item.uDataType != QCBOR_TYPE_INT64 ||
370 Item.val.int64 != 65534)
371 return -1;
372
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800373
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800374 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700375 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800376 if(Item.uDataType != QCBOR_TYPE_INT64 ||
377 Item.val.int64 != 65535)
378 return -1;
379
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800380
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800381 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700382 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800383 if(Item.uDataType != QCBOR_TYPE_INT64 ||
384 Item.val.int64 != 65536)
385 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800386
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800387 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700388 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800389 if(Item.uDataType != QCBOR_TYPE_INT64 ||
390 Item.val.int64 != 65537)
391 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800392
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800393 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700394 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800395 if(Item.uDataType != QCBOR_TYPE_INT64 ||
396 Item.val.int64 != 65538)
397 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800398
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800399 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700400 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800401 if(Item.uDataType != QCBOR_TYPE_INT64 ||
402 Item.val.int64 != 2147483647)
403 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800404
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800405 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700406 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800407 if(Item.uDataType != QCBOR_TYPE_INT64 ||
408 Item.val.int64 != 2147483647)
409 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800410
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800411 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700412 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800413 if(Item.uDataType != QCBOR_TYPE_INT64 ||
414 Item.val.int64 != 2147483648)
415 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800416
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800417 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700418 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800419 if(Item.uDataType != QCBOR_TYPE_INT64 ||
420 Item.val.int64 != 2147483649)
421 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800422
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800423 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700424 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800425 if(Item.uDataType != QCBOR_TYPE_INT64 ||
426 Item.val.int64 != 4294967294)
427 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800428
429
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800430 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700431 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800432 if(Item.uDataType != QCBOR_TYPE_INT64 ||
433 Item.val.int64 != 4294967295)
434 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800435
436
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800437 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700438 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800439 if(Item.uDataType != QCBOR_TYPE_INT64 ||
440 Item.val.int64 != 4294967296)
441 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800442
443
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800444 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700445 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800446 if(Item.uDataType != QCBOR_TYPE_INT64 ||
447 Item.val.int64 != 4294967297)
448 return -1;
449
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800450
451
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800452 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700453 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800454 if(Item.uDataType != QCBOR_TYPE_INT64 ||
455 Item.val.int64 != 9223372036854775807LL)
456 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800457
458
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800459 if(( nCBORError = QCBORDecode_GetNext(pDCtx, &Item)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -0700460 return (int32_t)nCBORError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800461 if(Item.uDataType != QCBOR_TYPE_UINT64 ||
462 Item.val.uint64 != 18446744073709551615ULL)
463 return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800464
465
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800466 if(QCBORDecode_Finish(pDCtx) != QCBOR_SUCCESS) {
467 return -1;
468 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800469
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800470 return 0;
471}
472
473
Laurence Lundbladecc7da412020-12-27 00:09:07 -0800474/* One less than the smallest negative integer allowed in C. Decoding
475 this should fail.
476 -9223372036854775809
477 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800478static const uint8_t spTooSmallNegative[] = {
479 0x3b, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
Laurence Lundblade21d1d812019-09-28 22:47:49 -1000480};
481
482
Laurence Lundblade3aee3a32018-12-17 16:17:45 -0800483/*
484 Tests the decoding of lots of different integers sizes
Laurence Lundblade0fb6c6d2018-10-12 22:02:05 +0800485 and values.
Laurence Lundblade2ded3d92018-10-09 21:36:11 +0800486 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800487int32_t IntegerValuesParseTest()
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
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800694int32_t SimpleArrayTest()
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
901int32_t EmptyMapsAndArraysTest()
902{
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
Laurence Lundbladec5fef682020-01-25 11:38:45 -0800969int32_t ParseDeepArrayTest()
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
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001002int32_t ParseTooDeepArrayTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001003{
1004 QCBORDecodeContext DCtx;
1005 int nReturn = 0;
1006 int i;
1007 QCBORItem Item;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001008
1009
Laurence Lundbladeee851742020-01-08 08:37:05 -08001010 QCBORDecode_Init(&DCtx,
1011 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTooDeepArrays),
1012 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001013
Laurence Lundblade972e59c2018-11-11 15:57:23 +07001014 for(i = 0; i < QCBOR_MAX_ARRAY_NESTING1; i++) {
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001015
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001016 if(QCBORDecode_GetNext(&DCtx, &Item) != 0 ||
1017 Item.uDataType != QCBOR_TYPE_ARRAY ||
1018 Item.uNestingLevel != i) {
1019 nReturn = -1;
1020 break;
1021 }
1022 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001023
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001024 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_ARRAY_DECODE_NESTING_TOO_DEEP)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001025 nReturn = -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08001026
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001027 return(nReturn);
1028}
1029
1030
1031
1032
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001033int32_t ShortBufferParseTest()
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
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001057int32_t ShortBufferParseTest2()
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 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001217int32_t ParseMapAsArrayTest()
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
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001640int32_t ParseMapTest()
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
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001675int32_t ParseSimpleTest()
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
Laurence Lundbladec5fef682020-01-25 11:38:45 -08001751int32_t NotWellFormedTests()
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 Lundbladeee851742020-01-08 08:37:05 -08001761 // Set up decoder context. String allocator needed for indefinite
1762 // string test cases
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001763 QCBORDecodeContext DCtx;
1764 QCBORDecode_Init(&DCtx, Input, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08001765#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001766 UsefulBuf_MAKE_STACK_UB(Pool, 100);
1767 QCBORDecode_SetMemPool(&DCtx, Pool, 0);
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08001768#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001769
1770 // Loop getting items until no more to get
Laurence Lundbladef71e1622020-08-06 18:52:13 -07001771 QCBORError uCBORError;
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001772 do {
1773 QCBORItem Item;
1774
Laurence Lundbladef71e1622020-08-06 18:52:13 -07001775 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
1776 } while(uCBORError == QCBOR_SUCCESS);
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001777
1778 // Every test vector must fail with
1779 // a not-well-formed error. If not
1780 // this test fails.
Laurence Lundbladea9489f82020-09-12 13:50:56 -07001781 if(!QCBORDecode_IsNotWellFormedError(uCBORError) &&
Laurence Lundbladef71e1622020-08-06 18:52:13 -07001782 uCBORError != QCBOR_ERR_NO_MORE_ITEMS) {
Laurence Lundblade4bf6e712020-12-10 11:53:21 -08001783 /* Return index of failure and QCBOR error in the result */
1784 return (int32_t)(nIterate * 100 + uCBORError);
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001785 }
1786 }
1787 return 0;
1788}
1789
1790
Laurence Lundblade4bf6e712020-12-10 11:53:21 -08001791// TODO: add a test index and report it so it is eaier to figure out which test failed.
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001792struct FailInput {
Laurence Lundblade59289e52019-12-30 13:44:37 -08001793 UsefulBufC Input;
1794 QCBORError nError;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08001795};
1796
Laurence Lundblade59289e52019-12-30 13:44:37 -08001797
Laurence Lundbladecc7da412020-12-27 00:09:07 -08001798static int32_t ProcessFailures(const struct FailInput *pFailInputs, size_t nNumFails)
Laurence Lundblade59289e52019-12-30 13:44:37 -08001799{
Laurence Lundbladecc7da412020-12-27 00:09:07 -08001800 for(const struct FailInput *pF = pFailInputs; pF < pFailInputs + nNumFails; pF++) {
Laurence Lundblade59289e52019-12-30 13:44:37 -08001801 QCBORDecodeContext DCtx;
Laurence Lundblade4bf6e712020-12-10 11:53:21 -08001802 QCBORError uCBORError;
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08001803
Laurence Lundblade59289e52019-12-30 13:44:37 -08001804 QCBORDecode_Init(&DCtx, pF->Input, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08001805
1806#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
Laurence Lundblade4bf6e712020-12-10 11:53:21 -08001807 // Set up the decoding context including a memory pool so that
1808 // indefinite length items can be checked
Laurence Lundblade59289e52019-12-30 13:44:37 -08001809 UsefulBuf_MAKE_STACK_UB(Pool, 100);
Laurence Lundblade830fbf92020-05-31 17:22:33 -07001810
Laurence Lundblade4bf6e712020-12-10 11:53:21 -08001811 uCBORError = QCBORDecode_SetMemPool(&DCtx, Pool, 0);
1812 if(uCBORError) {
Laurence Lundblade59289e52019-12-30 13:44:37 -08001813 return -9;
1814 }
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08001815#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
1816
Laurence Lundbladecf41c522021-02-20 10:19:07 -07001817
Laurence Lundblade59289e52019-12-30 13:44:37 -08001818 // Iterate until there is an error of some sort error
1819 QCBORItem Item;
1820 do {
Laurence Lundblade02625d42020-06-25 14:41:41 -07001821 // Set to something none-zero, something other than QCBOR_TYPE_NONE
Laurence Lundblade59289e52019-12-30 13:44:37 -08001822 memset(&Item, 0x33, sizeof(Item));
1823
Laurence Lundblade4bf6e712020-12-10 11:53:21 -08001824 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
1825 } while(uCBORError == QCBOR_SUCCESS);
1826
1827
Laurence Lundblade59289e52019-12-30 13:44:37 -08001828
1829 // Must get the expected error or the this test fails
1830 // The data and label type must also be QCBOR_TYPE_NONE
Laurence Lundblade4bf6e712020-12-10 11:53:21 -08001831 if(uCBORError != pF->nError ||
Laurence Lundblade59289e52019-12-30 13:44:37 -08001832 Item.uDataType != QCBOR_TYPE_NONE ||
1833 Item.uLabelType != QCBOR_TYPE_NONE) {
Laurence Lundblade06350ea2020-01-27 19:32:40 -08001834 // return index of CBOR + 100
Laurence Lundblade830fbf92020-05-31 17:22:33 -07001835 const size_t nIndex = (size_t)(pF - pFailInputs);
Laurence Lundblade4bf6e712020-12-10 11:53:21 -08001836 return (int32_t)(nIndex * 100 + uCBORError);
Laurence Lundblade59289e52019-12-30 13:44:37 -08001837 }
1838 }
1839
1840 return 0;
1841}
1842
1843
Laurence Lundbladecc7da412020-12-27 00:09:07 -08001844static const struct FailInput Failures[] = {
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001845 // Most of this is copied from not_well_formed.h. Here the error code
1846 // returned is also checked.
1847
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08001848#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001849 // Indefinite length strings must be closed off
1850 // An indefinite length byte string not closed off
1851 { {(uint8_t[]){0x5f, 0x41, 0x00}, 3}, QCBOR_ERR_HIT_END },
1852 // An indefinite length text string not closed off
1853 { {(uint8_t[]){0x7f, 0x61, 0x00}, 3}, QCBOR_ERR_HIT_END },
1854
1855
Laurence Lundblade2f467f92020-10-09 17:50:11 -07001856 // All the chunks in an indefinite length string must be of the type of
1857 // indefinite length string
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001858 // indefinite length byte string with text string chunk
1859 { {(uint8_t[]){0x5f, 0x61, 0x00, 0xff}, 4}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1860 // indefinite length text string with a byte string chunk
1861 { {(uint8_t[]){0x7f, 0x41, 0x00, 0xff}, 4}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1862 // indefinite length byte string with an positive integer chunk
1863 { {(uint8_t[]){0x5f, 0x00, 0xff}, 3}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1864 // indefinite length byte string with an negative integer chunk
1865 { {(uint8_t[]){0x5f, 0x21, 0xff}, 3}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1866 // indefinite length byte string with an array chunk
1867 { {(uint8_t[]){0x5f, 0x80, 0xff}, 3}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1868 // indefinite length byte string with an map chunk
1869 { {(uint8_t[]){0x5f, 0xa0, 0xff}, 3}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1870 // indefinite length byte string with tagged integer chunk
1871 { {(uint8_t[]){0x5f, 0xc0, 0x00, 0xff}, 4}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1872 // indefinite length byte string with an simple type chunk
1873 { {(uint8_t[]){0x5f, 0xe0, 0xff}, 3}, QCBOR_ERR_INDEFINITE_STRING_CHUNK },
1874 { {(uint8_t[]){0x5f, 0x5f, 0x41, 0x00, 0xff, 0xff}, 6}, QCBOR_ERR_INDEFINITE_STRING_CHUNK},
1875 // indefinite length text string with indefinite string inside
1876 { {(uint8_t[]){0x7f, 0x7f, 0x61, 0x00, 0xff, 0xff}, 6}, QCBOR_ERR_INDEFINITE_STRING_CHUNK},
1877
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08001878#else /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
1879
1880 { {(uint8_t[]){0x5f, 0x41, 0x00}, 3}, QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED },
1881 // An indefinite length text string not closed off
1882 { {(uint8_t[]){0x7f, 0x61, 0x00}, 3}, QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED },
1883
1884
1885 // All the chunks in an indefinite length string must be of the type of
1886 // indefinite length string
1887 // indefinite length byte string with text string chunk
1888 { {(uint8_t[]){0x5f, 0x61, 0x00, 0xff}, 4}, QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED },
1889 // indefinite length text string with a byte string chunk
1890 { {(uint8_t[]){0x7f, 0x41, 0x00, 0xff}, 4}, QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED },
1891 // indefinite length byte string with an positive integer chunk
1892 { {(uint8_t[]){0x5f, 0x00, 0xff}, 3}, QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED },
1893 // indefinite length byte string with an negative integer chunk
1894 { {(uint8_t[]){0x5f, 0x21, 0xff}, 3}, QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED },
1895 // indefinite length byte string with an array chunk
1896 { {(uint8_t[]){0x5f, 0x80, 0xff}, 3}, QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED },
1897 // indefinite length byte string with an map chunk
1898 { {(uint8_t[]){0x5f, 0xa0, 0xff}, 3}, QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED },
1899 // indefinite length byte string with tagged integer chunk
1900 { {(uint8_t[]){0x5f, 0xc0, 0x00, 0xff}, 4}, QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED },
1901 // indefinite length byte string with an simple type chunk
1902 { {(uint8_t[]){0x5f, 0xe0, 0xff}, 3}, QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED },
1903 { {(uint8_t[]){0x5f, 0x5f, 0x41, 0x00, 0xff, 0xff}, 6}, QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED},
1904 // indefinite length text string with indefinite string inside
1905 { {(uint8_t[]){0x7f, 0x7f, 0x61, 0x00, 0xff, 0xff}, 6}, QCBOR_ERR_INDEF_LEN_STRINGS_DISABLED},
1906#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
1907
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001908
1909 // Definte length maps and arrays must be closed by having the right number of items
1910 // A definte length array that is supposed to have 1 item, but has none
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001911 { {(uint8_t[]){0x81}, 1}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001912 // A definte length array that is supposed to have 2 items, but has only 1
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001913 { {(uint8_t[]){0x82, 0x00}, 2}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001914 // A definte length array that is supposed to have 511 items, but has only 1
1915 { {(uint8_t[]){0x9a, 0x01, 0xff, 0x00}, 4}, QCBOR_ERR_HIT_END },
1916 // A definte length map that is supposed to have 1 item, but has none
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001917 { {(uint8_t[]){0xa1}, 1}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001918 // A definte length map that is supposed to have s item, but has only 1
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001919 { {(uint8_t[]){0xa2, 0x01, 0x02}, 3}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001920
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08001921#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001922 // Indefinte length maps and arrays must be ended by a break
1923 // Indefinite length array with zero items and no break
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001924 { {(uint8_t[]){0x9f}, 1}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001925 // Indefinite length array with two items and no break
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001926 { {(uint8_t[]){0x9f, 0x01, 0x02}, 3}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001927 // Indefinite length map with zero items and no break
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001928 { {(uint8_t[]){0xbf}, 1}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001929 // Indefinite length map with two items and no break
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001930 { {(uint8_t[]){0xbf, 0x01, 0x02, 0x01, 0x02}, 5}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001931
1932
1933 // Nested maps and arrays must be closed off (some extra nested test vectors)
Laurence Lundblade642282a2020-06-23 12:00:33 -07001934 // Unclosed indefinite array containing a closed definite length array
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001935 { {(uint8_t[]){0x9f, 0x80, 0x00}, 3}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundblade642282a2020-06-23 12:00:33 -07001936 // Definite length array containing an unclosed indefinite length array
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001937 { {(uint8_t[]){0x81, 0x9f}, 2}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundblade2f467f92020-10-09 17:50:11 -07001938 // Unclosed indefinite map containing a closed definite length array
1939 { {(uint8_t[]){0xbf, 0x01, 0x80, 0x00, 0xa0}, 5}, QCBOR_ERR_NO_MORE_ITEMS },
1940 // Definite length map containing an unclosed indefinite length array
1941 { {(uint8_t[]){0xa1, 0x02, 0x9f}, 3}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001942 // Deeply nested definite length arrays with deepest one unclosed
Laurence Lundblade93d89472020-10-03 22:30:50 -07001943 { {(uint8_t[]){0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81, 0x81}, 9}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001944 // Deeply nested indefinite length arrays with deepest one unclosed
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001945 { {(uint8_t[]){0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0xff, 0xff, 0xff, 0xff}, 9}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001946 // Mixed nesting with indefinite unclosed
Laurence Lundbladee6f15112020-07-23 18:44:16 -07001947 { {(uint8_t[]){0x9f, 0x81, 0x9f, 0x81, 0x9f, 0x9f, 0xff, 0xff, 0xff}, 9}, QCBOR_ERR_NO_MORE_ITEMS },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001948 // Mixed nesting with definite unclosed
Laurence Lundbladeee851742020-01-08 08:37:05 -08001949 { {(uint8_t[]){0x9f, 0x82, 0x9f, 0x81, 0x9f, 0x9f, 0xff, 0xff, 0xff, 0xff}, 10}, QCBOR_ERR_BAD_BREAK },
Laurence Lundblade2f467f92020-10-09 17:50:11 -07001950 // Unclosed indefinite length map in definite length maps
1951 { {(uint8_t[]){0xa1, 0x01, 0xa2, 0x02, 0xbf, 0xff, 0x02, 0xbf}, 8},
1952 QCBOR_ERR_NO_MORE_ITEMS},
1953 // Unclosed definite length map in indefinite length maps
1954 { {(uint8_t[]){0xbf, 0x01, 0xbf, 0x02, 0xa1}, 5}, QCBOR_ERR_NO_MORE_ITEMS},
1955 // Unclosed indefinite length array in definite length maps
1956 { {(uint8_t[]){0xa1, 0x01, 0xa2, 0x02, 0x9f, 0xff, 0x02, 0x9f}, 8},
1957 QCBOR_ERR_NO_MORE_ITEMS},
1958 // Unclosed definite length array in indefinite length maps
1959 { {(uint8_t[]){0xbf, 0x01, 0xbf, 0x02, 0x81}, 5}, QCBOR_ERR_NO_MORE_ITEMS},
1960 // Unclosed indefinite length map in definite length arrays
1961 { {(uint8_t[]){0x81, 0x82, 0xbf, 0xff, 0xbf}, 5}, QCBOR_ERR_NO_MORE_ITEMS},
1962 // Unclosed definite length map in indefinite length arrays
1963 { {(uint8_t[]){0x9f, 0x9f, 0xa1}, 3}, QCBOR_ERR_NO_MORE_ITEMS},
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08001964#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07001965
1966 // The "argument" for the data item is incomplete
1967 // Positive integer missing 1 byte argument
1968 { {(uint8_t[]){0x18}, 1}, QCBOR_ERR_HIT_END },
1969 // Positive integer missing 2 byte argument
1970 { {(uint8_t[]){0x19}, 1}, QCBOR_ERR_HIT_END },
1971 // Positive integer missing 4 byte argument
1972 { {(uint8_t[]){0x1a}, 1}, QCBOR_ERR_HIT_END },
1973 // Positive integer missing 8 byte argument
1974 { {(uint8_t[]){0x1b}, 1}, QCBOR_ERR_HIT_END },
1975 // Positive integer missing 1 byte of 2 byte argument
1976 { {(uint8_t[]){0x19, 0x01}, 2}, QCBOR_ERR_HIT_END },
1977 // Positive integer missing 2 bytes of 4 byte argument
1978 { {(uint8_t[]){0x1a, 0x01, 0x02}, 3}, QCBOR_ERR_HIT_END },
1979 // Positive integer missing 1 bytes of 7 byte argument
1980 { {(uint8_t[]){0x1b, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07}, 8}, QCBOR_ERR_HIT_END },
1981 // Negative integer missing 1 byte argument
1982 { {(uint8_t[]){0x38}, 1}, QCBOR_ERR_HIT_END },
1983 // Binary string missing 1 byte argument
1984 { {(uint8_t[]){0x58}, 1}, QCBOR_ERR_HIT_END },
1985 // Text string missing 1 byte argument
1986 { {(uint8_t[]){0x78}, 1}, QCBOR_ERR_HIT_END },
1987 // Array missing 1 byte argument
1988 { {(uint8_t[]){0x98}, 1}, QCBOR_ERR_HIT_END },
1989 // Map missing 1 byte argument
1990 { {(uint8_t[]){0xb8}, 1}, QCBOR_ERR_HIT_END },
1991 // Tag missing 1 byte argument
1992 { {(uint8_t[]){0xd8}, 1}, QCBOR_ERR_HIT_END },
1993 // Simple missing 1 byte argument
1994 { {(uint8_t[]){0xf8}, 1}, QCBOR_ERR_HIT_END },
Laurence Lundblade2f467f92020-10-09 17:50:11 -07001995 // half-precision with 1 byte argument
1996 { {(uint8_t[]){0xf9, 0x00}, 2}, QCBOR_ERR_HIT_END },
1997 // single-precision with 2 byte argument
1998 { {(uint8_t[]){0xfa, 0x00, 0x00}, 3}, QCBOR_ERR_HIT_END },
1999 // double-precision with 3 byte argument
2000 { {(uint8_t[]){0xfb, 0x00, 0x00, 0x00}, 4}, QCBOR_ERR_HIT_END },
2001
2002
2003 // Tag with no content
2004 { {(uint8_t[]){0xc0}, 1}, QCBOR_ERR_HIT_END },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002005
2006
2007 // Breaks must not occur in definite length arrays and maps
2008 // Array of length 1 with sole member replaced by a break
2009 { {(uint8_t[]){0x81, 0xff}, 2}, QCBOR_ERR_BAD_BREAK },
2010 // Array of length 2 with 2nd member replaced by a break
2011 { {(uint8_t[]){0x82, 0x00, 0xff}, 3}, QCBOR_ERR_BAD_BREAK },
2012 // Map of length 1 with sole member label replaced by a break
2013 { {(uint8_t[]){0xa1, 0xff}, 2}, QCBOR_ERR_BAD_BREAK },
2014 // Map of length 1 with sole member label replaced by break
Laurence Lundblade2f467f92020-10-09 17:50:11 -07002015 // Alternate representation that some decoders handle differently
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002016 { {(uint8_t[]){0xa1, 0xff, 0x00}, 3}, QCBOR_ERR_BAD_BREAK },
2017 // Array of length 1 with 2nd member value replaced by a break
2018 { {(uint8_t[]){0xa1, 0x00, 0xff}, 3}, QCBOR_ERR_BAD_BREAK },
2019 // Map of length 2 with 2nd member replaced by a break
2020 { {(uint8_t[]){0xa2, 0x00, 0x00, 0xff}, 4}, QCBOR_ERR_BAD_BREAK },
2021
2022
2023 // Breaks must not occur on their own out of an indefinite length data item
2024 // A bare break is not well formed
2025 { {(uint8_t[]){0xff}, 1}, QCBOR_ERR_BAD_BREAK },
2026 // A bare break after a zero length definite length array
2027 { {(uint8_t[]){0x80, 0xff}, 2}, QCBOR_ERR_BAD_BREAK },
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08002028#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002029 // A bare break after a zero length indefinite length map
2030 { {(uint8_t[]){0x9f, 0xff, 0xff}, 3}, QCBOR_ERR_BAD_BREAK },
Laurence Lundblade2f467f92020-10-09 17:50:11 -07002031 // A break inside a definite length array inside an indefenite length array
2032 { {(uint8_t[]){0x9f, 0x81, 0xff}, 3}, QCBOR_ERR_BAD_BREAK },
2033 // Complicated mixed nesting with break outside indefinite length array
2034 { {(uint8_t[]){0x9f, 0x82, 0x9f, 0x81, 0x9f, 0x9f, 0xff, 0xff, 0xff, 0xff}, 10}, QCBOR_ERR_BAD_BREAK },
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08002035#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002036
2037
2038 // Forbidden two byte encodings of simple types
2039 // Must use 0xe0 instead
2040 { {(uint8_t[]){0xf8, 0x00}, 2}, QCBOR_ERR_BAD_TYPE_7 },
2041 // Should use 0xe1 instead
2042 { {(uint8_t[]){0xf8, 0x01}, 2}, QCBOR_ERR_BAD_TYPE_7 },
2043 // Should use 0xe2 instead
2044 { {(uint8_t[]){0xf8, 0x02}, 2}, QCBOR_ERR_BAD_TYPE_7 },
2045 // Should use 0xe3 instead
2046 { {(uint8_t[]){0xf8, 0x03}, 2}, QCBOR_ERR_BAD_TYPE_7 },
2047 // Should use 0xe4 instead
2048 { {(uint8_t[]){0xf8, 0x04}, 2}, QCBOR_ERR_BAD_TYPE_7 },
2049 // Should use 0xe5 instead
2050 { {(uint8_t[]){0xf8, 0x05}, 2}, QCBOR_ERR_BAD_TYPE_7 },
2051 // Should use 0xe6 instead
2052 { {(uint8_t[]){0xf8, 0x06}, 2}, QCBOR_ERR_BAD_TYPE_7 },
2053 // Should use 0xe7 instead
2054 { {(uint8_t[]){0xf8, 0x07}, 2}, QCBOR_ERR_BAD_TYPE_7 },
2055 // Should use 0xe8 instead
2056 { {(uint8_t[]){0xf8, 0x08}, 2}, QCBOR_ERR_BAD_TYPE_7 },
2057 // Should use 0xe9 instead
2058 { {(uint8_t[]){0xf8, 0x09}, 2}, QCBOR_ERR_BAD_TYPE_7 },
2059 // Should use 0xea instead
2060 { {(uint8_t[]){0xf8, 0x0a}, 2}, QCBOR_ERR_BAD_TYPE_7 },
2061 // Should use 0xeb instead
2062 { {(uint8_t[]){0xf8, 0x0b}, 2}, QCBOR_ERR_BAD_TYPE_7 },
2063 // Should use 0xec instead
2064 { {(uint8_t[]){0xf8, 0x0c}, 2}, QCBOR_ERR_BAD_TYPE_7 },
2065 // Should use 0xed instead
2066 { {(uint8_t[]){0xf8, 0x0d}, 2}, QCBOR_ERR_BAD_TYPE_7 },
2067 // Should use 0xee instead
2068 { {(uint8_t[]){0xf8, 0x0e}, 2}, QCBOR_ERR_BAD_TYPE_7 },
2069 // Should use 0xef instead
2070 { {(uint8_t[]){0xf8, 0x0f}, 2}, QCBOR_ERR_BAD_TYPE_7 },
2071 // Should use 0xf0 instead
2072 { {(uint8_t[]){0xf8, 0x10}, 2}, QCBOR_ERR_BAD_TYPE_7 },
2073 // Should use 0xf1 instead
2074 { {(uint8_t[]){0xf8, 0x11}, 2}, QCBOR_ERR_BAD_TYPE_7 },
2075 // Should use 0xf2 instead
2076 { {(uint8_t[]){0xf8, 0x12}, 2}, QCBOR_ERR_BAD_TYPE_7 },
2077 // Must use 0xf3 instead
2078 { {(uint8_t[]){0xf8, 0x13}, 2}, QCBOR_ERR_BAD_TYPE_7 },
2079 // Must use 0xf4 instead
2080 { {(uint8_t[]){0xf8, 0x14}, 2}, QCBOR_ERR_BAD_TYPE_7 },
2081 // Must use 0xf5 instead
2082 { {(uint8_t[]){0xf8, 0x15}, 2}, QCBOR_ERR_BAD_TYPE_7 },
2083 // Must use 0xf6 instead
2084 { {(uint8_t[]){0xf8, 0x16}, 2}, QCBOR_ERR_BAD_TYPE_7 },
2085 // Must use 0xf7 instead
2086 { {(uint8_t[]){0xf8, 0x17}, 2}, QCBOR_ERR_BAD_TYPE_7 },
2087 // Must use 0xf8 instead
2088 { {(uint8_t[]){0xf8, 0x18}, 2}, QCBOR_ERR_BAD_TYPE_7 },
Laurence Lundblade2f467f92020-10-09 17:50:11 -07002089 // Reserved
2090 { {(uint8_t[]){0xf8, 0x1f}, 2}, QCBOR_ERR_BAD_TYPE_7 },
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002091
2092 // Integers with additional info indefinite length
2093 // Positive integer with additional info indefinite length
2094 { {(uint8_t[]){0x1f}, 1}, QCBOR_ERR_BAD_INT },
2095 // Negative integer with additional info indefinite length
2096 { {(uint8_t[]){0x3f}, 1}, QCBOR_ERR_BAD_INT },
2097 // CBOR tag with "argument" an indefinite length
2098 { {(uint8_t[]){0xdf, 0x00}, 2}, QCBOR_ERR_BAD_INT },
2099 // CBOR tag with "argument" an indefinite length alternate vector
2100 { {(uint8_t[]){0xdf}, 1}, QCBOR_ERR_BAD_INT },
2101
2102
2103 // Missing bytes from a deterministic length string
2104 // A byte string is of length 1 without the 1 byte
2105 { {(uint8_t[]){0x41}, 1}, QCBOR_ERR_HIT_END },
2106 // A text string is of length 1 without the 1 byte
2107 { {(uint8_t[]){0x61}, 1}, QCBOR_ERR_HIT_END },
Laurence Lundblade4bf6e712020-12-10 11:53:21 -08002108
2109#if SIZE_MAX > 2147483647
Laurence Lundblade42272e42020-01-31 07:50:53 -08002110 // Byte string should have 2^32-15 bytes, but has one
2111 { {(uint8_t[]){0x5a, 0xff, 0xff, 0xff, 0xf0, 0x00}, 6}, QCBOR_ERR_HIT_END },
2112 // Byte string should have 2^32-15 bytes, but has one
2113 { {(uint8_t[]){0x7a, 0xff, 0xff, 0xff, 0xf0, 0x00}, 6}, QCBOR_ERR_HIT_END },
Laurence Lundblade2f467f92020-10-09 17:50:11 -07002114 // Byte string should have 2^64 bytes, but has 3
2115 { {(uint8_t[]){0x5b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
2116 0x01, 0x02, 0x03}, 6}, QCBOR_ERR_HIT_END },
2117 // Text string should have 2^64 bytes, but has 3
2118 { {(uint8_t[]){0x7b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
2119 0x01, 0x02, 0x03}, 6}, QCBOR_ERR_HIT_END },
Laurence Lundblade4bf6e712020-12-10 11:53:21 -08002120#else
2121 // Byte string should have 2^32-15 bytes, but has one
2122 { {(uint8_t[]){0x5a, 0x00, 0x00, 0xff, 0xf0, 0x00}, 6}, QCBOR_ERR_HIT_END },
2123 // Byte string should have 2^32-15 bytes, but has one
2124 { {(uint8_t[]){0x7a, 0x00, 0x00, 0xff, 0xf0, 0x00}, 6}, QCBOR_ERR_HIT_END },
2125 // Byte string should have 2^16 bytes, but has 3
2126 { {(uint8_t[]){0x5b, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0x01, 0x02, 0x03}, 6}, QCBOR_ERR_HIT_END },
2127 // Text string should have 2^64 bytes, but has 3
2128 { {(uint8_t[]){0x7b, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0x01, 0x02, 0x03}, 6}, QCBOR_ERR_HIT_END },
2129#endif
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002130
2131 // Use of unassigned additional information values
2132 // Major type positive integer with reserved value 28
2133 { {(uint8_t[]){0x1c}, 1}, QCBOR_ERR_UNSUPPORTED },
2134 // Major type positive integer with reserved value 29
2135 { {(uint8_t[]){0x1d}, 1}, QCBOR_ERR_UNSUPPORTED },
2136 // Major type positive integer with reserved value 30
2137 { {(uint8_t[]){0x1e}, 1}, QCBOR_ERR_UNSUPPORTED },
2138 // Major type negative integer with reserved value 28
2139 { {(uint8_t[]){0x3c}, 1}, QCBOR_ERR_UNSUPPORTED },
2140 // Major type negative integer with reserved value 29
2141 { {(uint8_t[]){0x3d}, 1}, QCBOR_ERR_UNSUPPORTED },
2142 // Major type negative integer with reserved value 30
2143 { {(uint8_t[]){0x3e}, 1}, QCBOR_ERR_UNSUPPORTED },
2144 // Major type byte string with reserved value 28 length
2145 { {(uint8_t[]){0x5c}, 1}, QCBOR_ERR_UNSUPPORTED },
2146 // Major type byte string with reserved value 29 length
2147 { {(uint8_t[]){0x5d}, 1}, QCBOR_ERR_UNSUPPORTED },
2148 // Major type byte string with reserved value 30 length
2149 { {(uint8_t[]){0x5e}, 1}, QCBOR_ERR_UNSUPPORTED },
2150 // Major type text string with reserved value 28 length
2151 { {(uint8_t[]){0x7c}, 1}, QCBOR_ERR_UNSUPPORTED },
2152 // Major type text string with reserved value 29 length
2153 { {(uint8_t[]){0x7d}, 1}, QCBOR_ERR_UNSUPPORTED },
2154 // Major type text string with reserved value 30 length
2155 { {(uint8_t[]){0x7e}, 1}, QCBOR_ERR_UNSUPPORTED },
2156 // Major type array with reserved value 28 length
2157 { {(uint8_t[]){0x9c}, 1}, QCBOR_ERR_UNSUPPORTED },
2158 // Major type array with reserved value 29 length
2159 { {(uint8_t[]){0x9d}, 1}, QCBOR_ERR_UNSUPPORTED },
2160 // Major type array with reserved value 30 length
2161 { {(uint8_t[]){0x9e}, 1}, QCBOR_ERR_UNSUPPORTED },
2162 // Major type map with reserved value 28 length
2163 { {(uint8_t[]){0xbc}, 1}, QCBOR_ERR_UNSUPPORTED },
2164 // Major type map with reserved value 29 length
2165 { {(uint8_t[]){0xbd}, 1}, QCBOR_ERR_UNSUPPORTED },
2166 // Major type map with reserved value 30 length
2167 { {(uint8_t[]){0xbe}, 1}, QCBOR_ERR_UNSUPPORTED },
2168 // Major type tag with reserved value 28 length
2169 { {(uint8_t[]){0xdc}, 1}, QCBOR_ERR_UNSUPPORTED },
2170 // Major type tag with reserved value 29 length
2171 { {(uint8_t[]){0xdd}, 1}, QCBOR_ERR_UNSUPPORTED },
2172 // Major type tag with reserved value 30 length
2173 { {(uint8_t[]){0xde}, 1}, QCBOR_ERR_UNSUPPORTED },
2174 // Major type simple with reserved value 28 length
2175 { {(uint8_t[]){0xfc}, 1}, QCBOR_ERR_UNSUPPORTED },
2176 // Major type simple with reserved value 29 length
2177 { {(uint8_t[]){0xfd}, 1}, QCBOR_ERR_UNSUPPORTED },
2178 // Major type simple with reserved value 30 length
2179 { {(uint8_t[]){0xfe}, 1}, QCBOR_ERR_UNSUPPORTED },
2180
2181
2182 // Maps must have an even number of data items (key & value)
2183 // Map with 1 item when it should have 2
2184 { {(uint8_t[]){0xa1, 0x00}, 2}, QCBOR_ERR_HIT_END },
2185 // Map with 3 item when it should have 4
2186 { {(uint8_t[]){0xa2, 0x00, 0x00, 0x00}, 2}, QCBOR_ERR_HIT_END },
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08002187#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002188 // Map with 1 item when it should have 2
2189 { {(uint8_t[]){0xbf, 0x00, 0xff}, 3}, QCBOR_ERR_BAD_BREAK },
2190 // Map with 3 item when it should have 4
2191 { {(uint8_t[]){0xbf, 0x00, 0x00, 0x00, 0xff}, 5}, QCBOR_ERR_BAD_BREAK },
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08002192#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002193
2194
2195 // In addition to not-well-formed, some invalid CBOR
Laurence Lundbladeee851742020-01-08 08:37:05 -08002196 // Text-based date, with an integer
2197 { {(uint8_t[]){0xc0, 0x00}, 2}, QCBOR_ERR_BAD_OPT_TAG },
2198 // Epoch date, with an byte string
2199 { {(uint8_t[]){0xc1, 0x41, 0x33}, 3}, QCBOR_ERR_BAD_OPT_TAG },
2200 // tagged as both epoch and string dates
2201 { {(uint8_t[]){0xc1, 0xc0, 0x00}, 3}, QCBOR_ERR_BAD_OPT_TAG },
2202 // big num tagged an int, not a byte string
2203 { {(uint8_t[]){0xc2, 0x00}, 2}, QCBOR_ERR_BAD_OPT_TAG },
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002204};
2205
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002206int32_t DecodeFailureTests()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002207{
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002208 int32_t nResult;
Laurence Lundbladebb1062e2019-08-12 23:28:54 -07002209
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08002210 nResult = ProcessFailures(Failures,C_ARRAY_COUNT(Failures,struct FailInput));
Laurence Lundblade59289e52019-12-30 13:44:37 -08002211 if(nResult) {
2212 return nResult;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002213 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002214
Laurence Lundblade3a6042e2019-06-28 19:58:04 -07002215 // Corrupt the UsefulInputBuf and see that
2216 // it reflected correctly for CBOR decoding
Laurence Lundbladee6f15112020-07-23 18:44:16 -07002217 QCBORDecodeContext DCtx;
2218 QCBORItem Item;
2219 QCBORError uQCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002220
Laurence Lundbladee6f15112020-07-23 18:44:16 -07002221 QCBORDecode_Init(&DCtx,
2222 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSimpleValues),
2223 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002224
Laurence Lundbladee6f15112020-07-23 18:44:16 -07002225 if((uQCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
2226 return (int32_t)uQCBORError;
2227 }
2228 if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.val.uCount != 10) {
2229 // This wasn't supposed to happen
2230 return -1;
2231 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002232
Laurence Lundbladee6f15112020-07-23 18:44:16 -07002233 DCtx.InBuf.magic = 0; // Reach in and corrupt the UsefulInputBuf
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002234
Laurence Lundbladee6f15112020-07-23 18:44:16 -07002235 uQCBORError = QCBORDecode_GetNext(&DCtx, &Item);
2236 if(uQCBORError != QCBOR_ERR_NO_MORE_ITEMS) {
2237 // Did not get back the error expected
2238 return -2;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002239 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002240
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002241
Laurence Lundblade98427e92020-09-28 21:33:23 -07002242 /*
2243 The max size of a string for QCBOR is SIZE_MAX - 4 so this
2244 tests here can be performed to see that the max length
2245 error check works correctly. See DecodeBytes(). If the max
2246 size was SIZE_MAX, it wouldn't be possible to test this.
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002247
Laurence Lundblade98427e92020-09-28 21:33:23 -07002248 This test will automatocally adapt the all CPU sizes
2249 through the use of SIZE_MAX.
2250 */
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002251
Laurence Lundblade8510f8c2020-12-01 11:31:16 -08002252 UsefulBuf_MAKE_STACK_UB( HeadBuf, QCBOR_HEAD_BUFFER_SIZE);
Laurence Lundblade98427e92020-09-28 21:33:23 -07002253 UsefulBufC EncodedHead;
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002254
Laurence Lundblade98427e92020-09-28 21:33:23 -07002255 // This makes a CBOR head with a text string that is very long
2256 // but doesn't fill in the bytes of the text string as that is
2257 // not needed to test this part of QCBOR.
2258 EncodedHead = QCBOREncode_EncodeHead(HeadBuf, CBOR_MAJOR_TYPE_TEXT_STRING, 0, SIZE_MAX);
2259
2260 QCBORDecode_Init(&DCtx, EncodedHead, QCBOR_DECODE_MODE_NORMAL);
2261
2262 if(QCBOR_ERR_STRING_TOO_LONG != QCBORDecode_GetNext(&DCtx, &Item)) {
2263 return -4;
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002264 }
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002265
Laurence Lundblade3a6042e2019-06-28 19:58:04 -07002266 return 0;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002267}
2268
2269
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002270/* Try all 256 values of the byte at nLen including recursing for
2271 each of the values to try values at nLen+1 ... up to nLenMax
2272 */
Laurence Lundblade06350ea2020-01-27 19:32:40 -08002273static void ComprehensiveInputRecurser(uint8_t *pBuf, size_t nLen, size_t nLenMax)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002274{
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002275 if(nLen >= nLenMax) {
2276 return;
2277 }
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08002278
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002279 for(int inputByte = 0; inputByte < 256; inputByte++) {
2280 // Set up the input
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002281 pBuf[nLen] = (uint8_t)inputByte;
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08002282 const UsefulBufC Input = {pBuf, nLen+1};
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08002283
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002284 // Get ready to parse
2285 QCBORDecodeContext DCtx;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002286 QCBORDecode_Init(&DCtx, Input, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002287
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002288 // Parse by getting the next item until an error occurs
2289 // Just about every possible decoder error can occur here
2290 // The goal of this test is not to check for the correct
2291 // error since that is not really possible. It is to
2292 // see that there is no crash on hostile input.
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002293 while(1) {
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002294 QCBORItem Item;
2295 QCBORError nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002296 if(nCBORError != QCBOR_SUCCESS) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002297 break;
2298 }
2299 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002300
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002301 ComprehensiveInputRecurser(pBuf, nLen+1, nLenMax);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002302 }
2303}
2304
2305
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002306int32_t ComprehensiveInputTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002307{
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002308 // Size 2 tests 64K inputs and runs quickly
2309 uint8_t pBuf[2];
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002310
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002311 ComprehensiveInputRecurser(pBuf, 0, sizeof(pBuf));
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002312
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002313 return 0;
2314}
2315
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002316
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002317int32_t BigComprehensiveInputTest()
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002318{
2319 // size 3 tests 16 million inputs and runs OK
2320 // in seconds on fast machines. Size 4 takes
2321 // 10+ minutes and 5 half a day on fast
2322 // machines. This test is kept separate from
2323 // the others so as to no slow down the use
2324 // of them as a very frequent regression.
2325 uint8_t pBuf[3]; //
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08002326
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002327 ComprehensiveInputRecurser(pBuf, 0, sizeof(pBuf));
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08002328
Laurence Lundbladea2e29072018-12-30 09:20:06 -08002329 return 0;
2330}
2331
2332
Laurence Lundbladecc7da412020-12-27 00:09:07 -08002333static const uint8_t spDateTestInput[] = {
Laurence Lundblade16a207a2021-09-18 17:22:46 -07002334 /* 1. The valid date string "1985-04-12" */
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002335 0xc0, // tag for string date
2336 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002337
Laurence Lundblade16a207a2021-09-18 17:22:46 -07002338 /* 2. An invalid date string due to wrong tag content type */
Laurence Lundbladec7114722020-08-13 05:11:40 -07002339 0xc0, // tag for string date
2340 0x00, // Wrong type for a string date
2341
Laurence Lundblade16a207a2021-09-18 17:22:46 -07002342 /* 3. A valid epoch date, 1400000000; Tue, 13 May 2014 16:53:20 GMT */
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002343 0xc1, // tag for epoch date
2344 0x1a, 0x53, 0x72, 0x4E, 0x00, // Epoch date 1400000000; Tue, 13 May 2014 16:53:20 GMT
2345
Laurence Lundblade16a207a2021-09-18 17:22:46 -07002346 /* 4. An invalid epoch date due to wrong tag content type */
Laurence Lundbladec7114722020-08-13 05:11:40 -07002347 0xc1,
2348 0x62, 'h', 'i', // wrong type tagged
2349
Laurence Lundblade16a207a2021-09-18 17:22:46 -07002350 /* 5. Valid epoch date tag as content for a two other nested tags */
Laurence Lundblade99615302020-11-29 11:19:47 -08002351 // CBOR_TAG_ENC_AS_B64
Laurence Lundblade16a207a2021-09-18 17:22:46 -07002352 0xcf, 0xd8, 0x16, 0xc1, // Epoch date with extra tags
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002353 0x1a, 0x53, 0x72, 0x4E, 0x01,
2354
Laurence Lundblade16a207a2021-09-18 17:22:46 -07002355 /* 6. Epoch date with value to large to fit into int64 */
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002356 0xc1, // tag for epoch date
2357 0x1b, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, // Too large integer
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002358
Laurence Lundblade16a207a2021-09-18 17:22:46 -07002359 /* 7. Epoch date with single-precision value of 1.1. */
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002360 0xc1, // tag for epoch date
Laurence Lundblade3ed0bca2020-07-14 22:50:10 -07002361 0xfa, 0x3f, 0x8c, 0xcc, 0xcd, // single with value 1.1
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002362
Laurence Lundblade16a207a2021-09-18 17:22:46 -07002363 /* 8. Epoch date with too-large single precision float */
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002364 0xc1, // tag for epoch date
Laurence Lundbladea1ad8782019-11-08 00:12:11 -08002365 0xfa, 0x7f, 0x7f, 0xff, 0xff, // 3.4028234663852886e+38 too large
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002366
Laurence Lundblade16a207a2021-09-18 17:22:46 -07002367 /* 9. Epoch date with slightly too-large double precision value */
Laurence Lundbladea1ad8782019-11-08 00:12:11 -08002368 0xc1, // tag for epoch date
2369 0xfb, 0x43, 0xe0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // 9223372036854775808.000000 just barely too large
2370 //0xfa, 0x7f, 0x7f, 0xff, 0xff // 3.4028234663852886e+38 too large
2371
Laurence Lundblade16a207a2021-09-18 17:22:46 -07002372 /* 10. Epoch date with largest supported double precision value */
Laurence Lundbladea1ad8782019-11-08 00:12:11 -08002373 0xc1, // tag for epoch date
Laurence Lundbladec7114722020-08-13 05:11:40 -07002374 0xfb, 0x43, 0xdf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, // 9223372036854773760 largest supported
2375
Laurence Lundblade16a207a2021-09-18 17:22:46 -07002376 /* 11. Epoch date with single-precision NaN */
Laurence Lundbladec7114722020-08-13 05:11:40 -07002377 0xc1, // tag for epoch date
2378 0xfa, 0x7f, 0xc0, 0x00, 0x00, // Single-precision NaN
2379
Laurence Lundblade16a207a2021-09-18 17:22:46 -07002380 /* 12. Epoch date with double precision plus infinity */
Laurence Lundbladec7114722020-08-13 05:11:40 -07002381 0xc1,
2382 0xfb, 0x7f, 0xf0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, // +infinity
2383
Laurence Lundblade16a207a2021-09-18 17:22:46 -07002384 /* 13. Epoch date with half-precision negative infinity */
Laurence Lundbladec7114722020-08-13 05:11:40 -07002385 0xc1, // tag for epoch date
2386 0xf9, 0xfc, 0x00, // -Infinity
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002387};
2388
2389
Laurence Lundbladec7114722020-08-13 05:11:40 -07002390
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002391// have to check float expected only to within an epsilon
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07002392#ifndef QCBOR_DISABLE_FLOAT_HW_USE
Laurence Lundblade02fcf312020-07-17 02:49:46 -07002393static int CHECK_EXPECTED_DOUBLE(double val, double expected) {
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002394
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002395 double diff = val - expected;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002396
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002397 diff = fabs(diff);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002398
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002399 return diff > 0.0000001;
2400}
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07002401#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002402
2403
Laurence Lundblade99615302020-11-29 11:19:47 -08002404
Laurence Lundbladec5fef682020-01-25 11:38:45 -08002405int32_t DateParseTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002406{
2407 QCBORDecodeContext DCtx;
Laurence Lundbladec7114722020-08-13 05:11:40 -07002408 QCBORItem Item;
2409 QCBORError uError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002410
Laurence Lundbladeee851742020-01-08 08:37:05 -08002411 QCBORDecode_Init(&DCtx,
2412 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spDateTestInput),
2413 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002414
Laurence Lundblade16a207a2021-09-18 17:22:46 -07002415 /* 1. The valid date string "1985-04-12" */
Laurence Lundbladec7114722020-08-13 05:11:40 -07002416 if((uError = QCBORDecode_GetNext(&DCtx, &Item))) {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002417 return -1;
Laurence Lundbladec7114722020-08-13 05:11:40 -07002418 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002419 if(Item.uDataType != QCBOR_TYPE_DATE_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07002420 UsefulBufCompareToSZ(Item.val.dateString, "1985-04-12")){
Laurence Lundblade67bd5512018-11-02 21:44:06 +07002421 return -2;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002422 }
2423
Laurence Lundblade16a207a2021-09-18 17:22:46 -07002424 /* 2. An invalid date string due to wrong tag content type */
Laurence Lundbladec7114722020-08-13 05:11:40 -07002425 uError = QCBORDecode_GetNext(&DCtx, &Item);
2426 if(uError != QCBOR_ERR_BAD_OPT_TAG) {
Laurence Lundblade67bd5512018-11-02 21:44:06 +07002427 return -3;
Laurence Lundbladec7114722020-08-13 05:11:40 -07002428 }
2429
Laurence Lundblade16a207a2021-09-18 17:22:46 -07002430 /* 3. A valid epoch date, 1400000000; Tue, 13 May 2014 16:53:20 GMT */
2431 uError = QCBORDecode_GetNext(&DCtx, &Item);
2432 if(uError != QCBOR_SUCCESS) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002433 return -4;
2434 }
Laurence Lundblade16a207a2021-09-18 17:22:46 -07002435 if(uError == QCBOR_SUCCESS) {
2436 if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH ||
2437 Item.val.epochDate.nSeconds != 1400000000
Máté Tóth-Pálef5f07a2021-09-17 19:31:37 +02002438#ifndef USEFULBUF_DISABLE_ALL_FLOAT
Laurence Lundblade16a207a2021-09-18 17:22:46 -07002439 || Item.val.epochDate.fSecondsFraction != 0
Máté Tóth-Pálef5f07a2021-09-17 19:31:37 +02002440#endif /* USEFULBUF_DISABLE_ALL_FLOAT */
Laurence Lundblade16a207a2021-09-18 17:22:46 -07002441 ) {
2442 return -5;
2443 }
Laurence Lundbladec7114722020-08-13 05:11:40 -07002444 }
2445
Laurence Lundblade16a207a2021-09-18 17:22:46 -07002446 /* 4. An invalid epoch date due to wrong tag content type */
Laurence Lundbladec7114722020-08-13 05:11:40 -07002447 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_BAD_OPT_TAG) {
2448 return -6;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002449 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002450
Laurence Lundblade16a207a2021-09-18 17:22:46 -07002451 /* 5. Valid epoch date tag as content for a two other nested tags */
Laurence Lundblade99615302020-11-29 11:19:47 -08002452 // Epoch date wrapped in an CBOR_TAG_ENC_AS_B64 and an unknown tag.
2453 // The date is decoded and the two tags are returned. This is to
2454 // make sure the wrapping of epoch date in another tag works OK.
Laurence Lundbladec7114722020-08-13 05:11:40 -07002455 if((uError = QCBORDecode_GetNext(&DCtx, &Item))) {
2456 return -7;
2457 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002458 if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH ||
2459 Item.val.epochDate.nSeconds != 1400000001 ||
Máté Tóth-Pálef5f07a2021-09-17 19:31:37 +02002460#ifndef USEFULBUF_DISABLE_ALL_FLOAT
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002461 Item.val.epochDate.fSecondsFraction != 0 ||
Máté Tóth-Pálef5f07a2021-09-17 19:31:37 +02002462#endif /* USEFULBUF_DISABLE_ALL_FLOAT */
Laurence Lundblade99615302020-11-29 11:19:47 -08002463 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_ENC_AS_B64)) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002464 return -8;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002465 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002466
Laurence Lundblade16a207a2021-09-18 17:22:46 -07002467 /* 6. Epoch date with value to large to fit into int64 */
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002468 if(QCBORDecode_GetNext(&DCtx, &Item) != QCBOR_ERR_DATE_OVERFLOW) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002469 return -9;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002470 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002471
Laurence Lundblade16a207a2021-09-18 17:22:46 -07002472 /* 7. Epoch date with single-precision value of 1.1. */
2473 uError = QCBORDecode_GetNext(&DCtx, &Item);
2474 if(uError != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002475 return -10;
2476 }
Laurence Lundblade16a207a2021-09-18 17:22:46 -07002477 if(uError == QCBOR_SUCCESS) {
2478 if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH ||
2479 Item.val.epochDate.nSeconds != 1
2480#ifndef QCBOR_DISABLE_FLOAT_HW_USE
2481 || CHECK_EXPECTED_DOUBLE(Item.val.epochDate.fSecondsFraction, 0.1)
Máté Tóth-Pálef5f07a2021-09-17 19:31:37 +02002482#endif /* USEFULBUF_DISABLE_ALL_FLOAT */
Laurence Lundblade16a207a2021-09-18 17:22:46 -07002483 ) {
2484 return -11;
2485 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002486 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002487
Laurence Lundblade16a207a2021-09-18 17:22:46 -07002488 /* 8. Epoch date with too-large single-precision float */
2489 uError = QCBORDecode_GetNext(&DCtx, &Item);
2490 if(uError != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_DATE_OVERFLOW)) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002491 return -12;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002492 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08002493
Laurence Lundblade16a207a2021-09-18 17:22:46 -07002494 /* 9. Epoch date with slightly too-large double-precision value */
2495 uError = QCBORDecode_GetNext(&DCtx, &Item);
2496 if(uError != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_DATE_OVERFLOW)) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002497 return -13;
Laurence Lundbladea1ad8782019-11-08 00:12:11 -08002498 }
2499
Laurence Lundblade16a207a2021-09-18 17:22:46 -07002500 /* 10. Epoch date with largest supported double-precision value */
2501 uError = QCBORDecode_GetNext(&DCtx, &Item);
2502 if(uError != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)) {
2503 return -14;
2504 }
2505 if(uError == QCBOR_SUCCESS) {
2506 if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH ||
2507 Item.val.epochDate.nSeconds != 9223372036854773760
2508#ifndef QCBOR_DISABLE_FLOAT_HW_USE
2509 || Item.val.epochDate.fSecondsFraction != 0.0
2510#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
2511 ) {
2512 return -14;
2513 }
2514 }
Laurence Lundbladec7114722020-08-13 05:11:40 -07002515
Laurence Lundblade16a207a2021-09-18 17:22:46 -07002516 /* 11. Epoch date with single-precision NaN */
2517 if(QCBORDecode_GetNext(&DCtx, &Item) != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_DATE_OVERFLOW)) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002518 return -15;
2519 }
2520
Laurence Lundblade16a207a2021-09-18 17:22:46 -07002521 /* 12. Epoch date with double-precision plus infinity */
2522 if(QCBORDecode_GetNext(&DCtx, &Item) != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_DATE_OVERFLOW)) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002523 return -16;
2524 }
2525
Laurence Lundblade16a207a2021-09-18 17:22:46 -07002526 /* 13. Epoch date with half-precision negative infinity */
2527 uError = QCBORDecode_GetNext(&DCtx, &Item);
2528 if(uError != FLOAT_ERR_CODE_NO_HALF_PREC_NO_FLOAT_HW(QCBOR_ERR_DATE_OVERFLOW)) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002529 return -17;
2530 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002531
2532 return 0;
2533}
2534
Laurence Lundblade4b270642020-08-14 12:53:07 -07002535/*
2536 Test cases covered here. Some items cover more than one of these.
2537 positive integer (zero counts as a positive integer)
2538 negative integer
2539 half-precision float
2540 single-precision float
2541 double-precision float
Laurence Lundbladec7114722020-08-13 05:11:40 -07002542
Laurence Lundblade4b270642020-08-14 12:53:07 -07002543 float Overflow error
2544 Wrong type error for epoch
2545 Wrong type error for date string
2546 float disabled error
2547 half-precision disabled error
2548 -Infinity
2549 Slightly too large integer
2550 Slightly too far from zero
Laurence Lundbladec7114722020-08-13 05:11:40 -07002551
Laurence Lundblade4b270642020-08-14 12:53:07 -07002552 Get epoch by int
2553 Get string by int
2554 Get epoch by string
2555 Get string by string
2556 Fail to get epoch by wrong int label
2557 Fail to get string by wrong string label
2558 Fail to get epoch by string because it is invalid
2559 Fail to get epoch by int because it is invalid
2560
2561 Untagged values
2562 */
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08002563static const uint8_t spSpiffyDateTestInput[] = {
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07002564 0x86, // array of 6 items
Laurence Lundblade4b270642020-08-14 12:53:07 -07002565
2566 0xc1,
2567 0xfb, 0xc3, 0xdf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, // -9.2233720368547748E+18, too negative
2568
Laurence Lundbladec7114722020-08-13 05:11:40 -07002569 0xc1, // tag for epoch date
Laurence Lundblade4b270642020-08-14 12:53:07 -07002570 0x1b, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, // Too-large integer
2571
2572 0xc1, // tag for epoch date
2573 0xf9, 0xfc, 0x00, // Half-precision -Infinity
2574
Laurence Lundblade46d63e92021-05-13 11:37:10 -07002575 0xad, // Open a map for tests involving labels.
Laurence Lundbladec7114722020-08-13 05:11:40 -07002576
2577 0x00,
2578 0xc0, // tag for string date
Laurence Lundblade4b270642020-08-14 12:53:07 -07002579 0x6a, '1','9','8','5','-','0','4','-','1','2', // Tagged date string
Laurence Lundbladec7114722020-08-13 05:11:40 -07002580
2581 0x01,
Laurence Lundblade9b334962020-08-27 10:55:53 -07002582 0xda, 0x03, 0x03, 0x03, 0x03, // An additional tag
Laurence Lundbladec7114722020-08-13 05:11:40 -07002583 0xc1, // tag for epoch date
2584 0x1a, 0x53, 0x72, 0x4E, 0x00, // Epoch date 1400000000; Tue, 13 May 2014 16:53:20 GMT
2585
2586 // Untagged integer 0
2587 0x08,
2588 0x00,
2589
2590 // Utagged date string with string label y
2591 0x61, 0x79,
Laurence Lundblade4b270642020-08-14 12:53:07 -07002592 0x6a, '2','0','8','5','-','0','4','-','1','2', // Untagged date string
Laurence Lundbladec7114722020-08-13 05:11:40 -07002593
2594 // Untagged -1000 with label z
2595 0x61, 0x7a,
Laurence Lundblade9b334962020-08-27 10:55:53 -07002596 0xda, 0x01, 0x01, 0x01, 0x01, // An additional tag
Laurence Lundbladec7114722020-08-13 05:11:40 -07002597 0x39, 0x03, 0xe7,
2598
Laurence Lundbladec7114722020-08-13 05:11:40 -07002599 0x07,
2600 0xc1, // tag for epoch date
2601 0xfb, 0x43, 0xdf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, // 9223372036854773760 largest supported
2602
Laurence Lundblade4b270642020-08-14 12:53:07 -07002603 0x05,
2604 0xc1,
2605 0xfb, 0xc3, 0xdf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, // -9223372036854773760 largest negative
2606
Laurence Lundbladec7114722020-08-13 05:11:40 -07002607 // Untagged single-precision float with value 3.14 with string label x
2608 0x61, 0x78,
2609 0xFA, 0x40, 0x48, 0xF5, 0xC3,
2610
Laurence Lundbladec7114722020-08-13 05:11:40 -07002611 // Untagged half-precision float with value -2
2612 0x09,
2613 0xF9, 0xC0, 0x00,
Laurence Lundblade46d63e92021-05-13 11:37:10 -07002614
2615 /* Tagged date-only date string */
2616 0x63, 0x53, 0x44, 0x53,
2617 0xD9, 0x03, 0xEC,
2618 0x6A, 0x31, 0x39, 0x38, 0x35, 0x2D, 0x30, 0x34, 0x2D, 0x31, 0x32, /* "1985-04-12" */
2619
2620 /* Untagged date-only date string */
2621 0x18, 0x63,
2622 0x6A, 0x31, 0x39, 0x38, 0x35, 0x2D, 0x30, 0x34, 0x2D, 0x31, 0x32, /* "1985-04-12" */
2623
2624 /* Tagged days-count epoch date */
2625 0x63, 0x53, 0x44, 0x45,
2626 0xD8, 0x64, /* tag(100) */
2627 0x39, 0x29, 0xB3, /* -10676 */
2628
2629 /* Untagged days-count epoch date */
2630 0x11,
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07002631 0x19, 0x0F, 0x9A, /* 3994 */
2632
2633 // End of map, back to array
2634
2635 // These two at the end because they are unrecoverable errors
2636 0xc1, // tag for epoch date
2637 0x80, // Erroneous empty array as content for date
2638
2639 0xc0, // tag for string date
2640 0xa0 // Erroneous empty map as content for date
Laurence Lundblade46d63e92021-05-13 11:37:10 -07002641
Laurence Lundbladec7114722020-08-13 05:11:40 -07002642};
2643
2644int32_t SpiffyDateDecodeTest()
2645{
2646 QCBORDecodeContext DC;
Laurence Lundblade4b270642020-08-14 12:53:07 -07002647 QCBORError uError;
Laurence Lundblade9b334962020-08-27 10:55:53 -07002648 int64_t nEpochDate2, nEpochDate3, nEpochDate5,
2649 nEpochDate4, nEpochDate6, nEpochDateFail,
Laurence Lundblade46d63e92021-05-13 11:37:10 -07002650 nEpochDate1400000000, nEpochDays1, nEpochDays2;
2651 UsefulBufC StringDate1, StringDate2, StringDays1, StringDays2;
Laurence Lundblade9b334962020-08-27 10:55:53 -07002652 uint64_t uTag1, uTag2;
Laurence Lundbladec7114722020-08-13 05:11:40 -07002653
2654 QCBORDecode_Init(&DC,
Laurence Lundblade4b270642020-08-14 12:53:07 -07002655 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSpiffyDateTestInput),
Laurence Lundbladec7114722020-08-13 05:11:40 -07002656 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07002657 QCBORDecode_EnterArray(&DC, NULL);
Laurence Lundbladec7114722020-08-13 05:11:40 -07002658
Laurence Lundblade9b334962020-08-27 10:55:53 -07002659 // Too-negative float, -9.2233720368547748E+18
2660 QCBORDecode_GetEpochDate(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nEpochDateFail);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002661 uError = QCBORDecode_GetAndResetError(&DC);
Laurence Lundblade16a207a2021-09-18 17:22:46 -07002662 if(uError != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_DATE_OVERFLOW)) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002663 return 1111;
2664 }
2665
2666 // Too-large integer
Laurence Lundblade9b334962020-08-27 10:55:53 -07002667 QCBORDecode_GetEpochDate(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nEpochDateFail);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002668 uError = QCBORDecode_GetAndResetError(&DC);
2669 if(uError != QCBOR_ERR_DATE_OVERFLOW) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002670 return 1;
2671 }
2672
Laurence Lundblade4b270642020-08-14 12:53:07 -07002673 // Half-precision minus infinity
Laurence Lundblade9b334962020-08-27 10:55:53 -07002674 QCBORDecode_GetEpochDate(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nEpochDateFail);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002675 uError = QCBORDecode_GetAndResetError(&DC);
Laurence Lundblade16a207a2021-09-18 17:22:46 -07002676 if(uError != FLOAT_ERR_CODE_NO_HALF_PREC_NO_FLOAT_HW(QCBOR_ERR_DATE_OVERFLOW)) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002677 return 2;
Laurence Lundbladec7114722020-08-13 05:11:40 -07002678 }
2679
Laurence Lundblade4b270642020-08-14 12:53:07 -07002680
Laurence Lundblade4b270642020-08-14 12:53:07 -07002681
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07002682 QCBORDecode_EnterMap(&DC, NULL);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002683
2684 // Get largest negative double precision epoch date allowed
Laurence Lundblade9b334962020-08-27 10:55:53 -07002685 QCBORDecode_GetEpochDateInMapN(&DC,
2686 5,
2687 QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG |
2688 QCBOR_TAG_REQUIREMENT_ALLOW_ADDITIONAL_TAGS,
2689 &nEpochDate2);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002690 uError = QCBORDecode_GetAndResetError(&DC);
Laurence Lundblade16a207a2021-09-18 17:22:46 -07002691 if(uError != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002692 return 102;
2693 }
Laurence Lundblade16a207a2021-09-18 17:22:46 -07002694 if(uError == QCBOR_SUCCESS) {
2695 if(nEpochDate2 != -9223372036854773760LL) {
2696 return 101;
2697 }
2698 }
Laurence Lundbladec7114722020-08-13 05:11:40 -07002699
Laurence Lundblade4b270642020-08-14 12:53:07 -07002700 // Get largest double precision epoch date allowed
Laurence Lundblade9b334962020-08-27 10:55:53 -07002701 QCBORDecode_GetEpochDateInMapN(&DC, 7, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
2702 &nEpochDate2);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002703 uError = QCBORDecode_GetAndResetError(&DC);
Laurence Lundblade16a207a2021-09-18 17:22:46 -07002704 if(uError != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)) {
Laurence Lundblade9b334962020-08-27 10:55:53 -07002705 return 112;
Laurence Lundbladec7114722020-08-13 05:11:40 -07002706 }
Laurence Lundblade16a207a2021-09-18 17:22:46 -07002707 if(uError == QCBOR_SUCCESS) {
2708 if(nEpochDate2 != 9223372036854773760ULL) {
2709 return 111;
2710 }
2711 }
Laurence Lundblade4b270642020-08-14 12:53:07 -07002712
2713 // A single-precision date
Laurence Lundblade9b334962020-08-27 10:55:53 -07002714 QCBORDecode_GetEpochDateInMapSZ(&DC, "x", QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
2715 &nEpochDate5);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002716 uError = QCBORDecode_GetAndResetError(&DC);
Laurence Lundblade16a207a2021-09-18 17:22:46 -07002717 if(uError != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002718 return 104;
2719 }
Laurence Lundblade16a207a2021-09-18 17:22:46 -07002720 if(uError == QCBOR_SUCCESS) {
2721 if(nEpochDate5 != 3) {
2722 return 103;
2723 }
2724 }
Laurence Lundbladec7114722020-08-13 05:11:40 -07002725
Laurence Lundblade9b334962020-08-27 10:55:53 -07002726 // A half-precision date with value -2 FFF
2727 QCBORDecode_GetEpochDateInMapN(&DC, 9, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
2728 &nEpochDate4);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002729 uError = QCBORDecode_GetAndResetError(&DC);
Laurence Lundblade16a207a2021-09-18 17:22:46 -07002730 if(uError != FLOAT_ERR_CODE_NO_HALF_PREC_NO_FLOAT_HW(QCBOR_SUCCESS)) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002731 return 106;
2732 }
Laurence Lundblade16a207a2021-09-18 17:22:46 -07002733 if(uError == QCBOR_SUCCESS) {
2734 if(nEpochDate4 != -2) {
2735 return 105;
2736 }
2737 }
Laurence Lundblade4b270642020-08-14 12:53:07 -07002738
2739 // Fail to get an epoch date by string label
Laurence Lundblade9b334962020-08-27 10:55:53 -07002740 QCBORDecode_GetEpochDateInMapSZ(&DC, "no-label",
2741 QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
2742 &nEpochDate6);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002743 uError = QCBORDecode_GetAndResetError(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002744 if(uError != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002745 return 107;
2746 }
2747
2748 // Fail to get an epoch date by integer label
Laurence Lundblade9b334962020-08-27 10:55:53 -07002749 QCBORDecode_GetEpochDateInMapN(&DC, 99999, QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
2750 &nEpochDate6);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002751 uError = QCBORDecode_GetAndResetError(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002752 if(uError != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002753 return 108;
2754 }
2755
2756 // Fail to get a string date by string label
Laurence Lundblade9b334962020-08-27 10:55:53 -07002757 QCBORDecode_GetDateStringInMapSZ(&DC, "no-label",
2758 QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
2759 &StringDate1);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002760 uError = QCBORDecode_GetAndResetError(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002761 if(uError != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002762 return 109;
2763 }
2764
2765 // Fail to get a string date by integer label
Laurence Lundblade9b334962020-08-27 10:55:53 -07002766 QCBORDecode_GetDateStringInMapN(&DC, 99999, QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
2767 &StringDate1);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002768 uError = QCBORDecode_GetAndResetError(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002769 if(uError != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002770 return 110;
2771 }
2772
2773 // The rest of these succeed even if float features are disabled
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002774
Laurence Lundblade4b270642020-08-14 12:53:07 -07002775 // Epoch date 1400000000; Tue, 13 May 2014 16:53:20 GMT
Laurence Lundblade9b334962020-08-27 10:55:53 -07002776 QCBORDecode_GetEpochDateInMapN(&DC,
2777 1,
2778 QCBOR_TAG_REQUIREMENT_TAG |
2779 QCBOR_TAG_REQUIREMENT_ALLOW_ADDITIONAL_TAGS,
2780 &nEpochDate1400000000);
2781 uTag1 = QCBORDecode_GetNthTagOfLast(&DC, 0);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002782 // Tagged date string
Laurence Lundblade9b334962020-08-27 10:55:53 -07002783 QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
2784 &StringDate1);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002785 // Untagged integer 0
Laurence Lundblade9b334962020-08-27 10:55:53 -07002786 QCBORDecode_GetEpochDateInMapN(&DC, 8, QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
2787 &nEpochDate3);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002788 // Untagged date string
Laurence Lundblade9b334962020-08-27 10:55:53 -07002789 QCBORDecode_GetDateStringInMapSZ(&DC, "y", QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
2790 &StringDate2);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002791 // Untagged -1000 with label z
Laurence Lundblade9b334962020-08-27 10:55:53 -07002792 QCBORDecode_GetEpochDateInMapSZ(&DC,
2793 "z",
2794 QCBOR_TAG_REQUIREMENT_NOT_A_TAG |
2795 QCBOR_TAG_REQUIREMENT_ALLOW_ADDITIONAL_TAGS,
2796 &nEpochDate6);
2797 uTag2 = QCBORDecode_GetNthTagOfLast(&DC, 0);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002798
Laurence Lundblade46d63e92021-05-13 11:37:10 -07002799 /* The days format is much simpler than the date format
2800 * because it can't be a floating point value. The test
2801 * of the spiffy decode functions sufficiently covers
2802 * the test of the non-spiffy decode days date decoding.
2803 * There is no full fan out of the error conditions
2804 * and decode options as that is implemented by code
2805 * that is tested well by the date testing above.
2806 */
2807 QCBORDecode_GetDaysStringInMapSZ(&DC, "SDS", QCBOR_TAG_REQUIREMENT_TAG,
2808 &StringDays1);
2809
2810 QCBORDecode_GetDaysStringInMapN(&DC, 99, QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
2811 &StringDays2);
2812
2813 QCBORDecode_GetEpochDaysInMapSZ(&DC, "SDE", QCBOR_TAG_REQUIREMENT_TAG,
2814 &nEpochDays1);
2815
2816 QCBORDecode_GetEpochDaysInMapN(&DC, 17, QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
2817 &nEpochDays2);
2818
Laurence Lundblade4b270642020-08-14 12:53:07 -07002819 QCBORDecode_ExitMap(&DC);
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07002820 if(QCBORDecode_GetError(&DC) != QCBOR_SUCCESS) {
2821 return 3001;
2822 }
2823
2824 // Bad content for epoch date
2825 QCBORDecode_GetEpochDate(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nEpochDateFail);
2826 uError = QCBORDecode_GetAndResetError(&DC);
2827 if(uError != QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT) {
2828 return 3;
2829 }
2830
2831 // Bad content for string date
2832 QCBORDecode_GetDateString(&DC, QCBOR_TAG_REQUIREMENT_TAG, &StringDate1);
2833 uError = QCBORDecode_GetAndResetError(&DC);
2834 if(uError != QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT) {
2835 return 4;
2836 }
2837
Laurence Lundblade4b270642020-08-14 12:53:07 -07002838 QCBORDecode_ExitArray(&DC);
2839 uError = QCBORDecode_Finish(&DC);
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07002840 if(uError != QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT) {
Laurence Lundblade9b334962020-08-27 10:55:53 -07002841 return 1000 + (int32_t)uError;
Laurence Lundblade4b270642020-08-14 12:53:07 -07002842 }
2843
Laurence Lundblade9b334962020-08-27 10:55:53 -07002844 if(nEpochDate1400000000 != 1400000000) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002845 return 200;
2846 }
2847
Laurence Lundblade9b334962020-08-27 10:55:53 -07002848 if(uTag1 != 0x03030303) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002849 return 201;
2850 }
2851
Laurence Lundblade9b334962020-08-27 10:55:53 -07002852 if(nEpochDate3 != 0) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002853 return 202;
2854 }
2855
Laurence Lundblade9b334962020-08-27 10:55:53 -07002856 if(nEpochDate6 != -1000) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002857 return 203;
2858 }
2859
Laurence Lundblade9b334962020-08-27 10:55:53 -07002860 if(uTag2 != 0x01010101) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002861 return 204;
2862 }
2863
Laurence Lundblade46d63e92021-05-13 11:37:10 -07002864 if(nEpochDays1 != -10676) {
2865 return 205;
2866 }
2867
2868 if(nEpochDays2 != 3994) {
2869 return 206;
2870 }
2871
Laurence Lundblade9b334962020-08-27 10:55:53 -07002872 if(UsefulBuf_Compare(StringDate1, UsefulBuf_FromSZ("1985-04-12"))) {
2873 return 205;
2874 }
2875
2876 if(UsefulBuf_Compare(StringDate2, UsefulBuf_FromSZ("2085-04-12"))) {
2877 return 206;
2878 }
2879
Laurence Lundblade46d63e92021-05-13 11:37:10 -07002880 if(UsefulBuf_Compare(StringDays1, UsefulBuf_FromSZ("1985-04-12"))) {
2881 return 207;
2882 }
2883
2884 if(UsefulBuf_Compare(StringDays2, UsefulBuf_FromSZ("1985-04-12"))) {
2885 return 208;
2886 }
2887
Laurence Lundbladec7114722020-08-13 05:11:40 -07002888 return 0;
2889}
2890
2891
2892
Laurence Lundblade9b334962020-08-27 10:55:53 -07002893// Input for one of the tagging tests
Laurence Lundbladecc7da412020-12-27 00:09:07 -08002894static const uint8_t spTagInput[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002895 0xd9, 0xd9, 0xf7, // CBOR magic number
Laurence Lundblade9b334962020-08-27 10:55:53 -07002896 0x81, // Array of one
2897 0xd8, 0x04, // non-preferred serialization of tag 4, decimal fraction
2898 0x82, // Array of two that is the faction 1/3
2899 0x01,
2900 0x03,
2901
2902 /*
2903 More than 4 tags on an item 225(226(227(228(229([])))))
2904 */
2905 0xd8, 0xe1,
2906 0xd8, 0xe2,
2907 0xd8, 0xe3,
2908 0xd8, 0xe4,
2909 0xd8, 0xe5,
2910 0x80,
2911
2912 /* tag 10489608748473423768(
2913 2442302356(
2914 21590(
2915 240(
2916 []))))
2917 */
2918 0xdb, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98,
2919 0xda, 0x91, 0x92, 0x93, 0x94,
2920 0xd9, 0x54, 0x56,
2921 0xd8, 0xf0,
2922 0x80,
2923
2924 /* tag 21590(
2925 10489608748473423768(
2926 2442302357(
2927 65534(
2928 []))))
2929 */
2930 0xdb, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x54, 0x56,
2931 0xdb, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98,
2932 0xda, 0x91, 0x92, 0x93, 0x95,
2933 0xd9, 0xff, 0xfe,
2934 0x80,
2935
2936 /* Make sure to blow past the limit of tags that must be mapped.
2937 works in conjuntion with entries above.
2938 269488144(269488145(269488146(269488147([]))))
2939 */
2940 0xda, 0x10, 0x10, 0x10, 0x10,
2941 0xda, 0x10, 0x10, 0x10, 0x11,
2942 0xda, 0x10, 0x10, 0x10, 0x12,
2943 0xda, 0x10, 0x10, 0x10, 0x13,
2944 0x80,
2945
2946 /* An invalid decimal fraction with an additional tag */
2947 0xd9, 0xff, 0xfa,
2948 0xd8, 0x02, // non-preferred serialization of tag 2, a big num
2949 0x00, // the integer 0; should be a byte string
2950};
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002951
Laurence Lundblade59289e52019-12-30 13:44:37 -08002952/*
2953 DB 9192939495969798 # tag(10489608748473423768)
Laurence Lundblade9b334962020-08-27 10:55:53 -07002954 80 # array(0)
Laurence Lundblade59289e52019-12-30 13:44:37 -08002955 */
Laurence Lundbladecc7da412020-12-27 00:09:07 -08002956static const uint8_t spEncodedLargeTag[] = {0xdb, 0x91, 0x92, 0x93, 0x94, 0x95,
Laurence Lundbladeee851742020-01-08 08:37:05 -08002957 0x96, 0x97, 0x98, 0x80};
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002958
Laurence Lundblade59289e52019-12-30 13:44:37 -08002959/*
2960DB 9192939495969798 # tag(10489608748473423768)
2961 D8 88 # tag(136)
2962 C6 # tag(6)
2963 C7 # tag(7)
2964 80 # array(0)
2965*/
Laurence Lundbladecc7da412020-12-27 00:09:07 -08002966static const uint8_t spLotsOfTags[] = {0xdb, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96,
Laurence Lundbladeee851742020-01-08 08:37:05 -08002967 0x97, 0x98, 0xd8, 0x88, 0xc6, 0xc7, 0x80};
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002968
2969/*
Laurence Lundblade9b334962020-08-27 10:55:53 -07002970 55799(55799(55799({
2971 6(7(-23)): 5859837686836516696(7({
2972 7(-20): 11({
2973 17(-18): 17(17(17("Organization"))),
2974 9(-17): 773("SSG"),
2975 -15: 16(17(6(7("Confusion")))),
2976 17(-16): 17("San Diego"),
2977 17(-14): 17("US")
2978 }),
2979 23(-19): 19({
2980 -11: 9({
2981 -9: -7
2982 }),
2983 90599561(90599561(90599561(-10))): 12(h'0102030405060708090A')
2984 })
2985 })),
2986 16(-22): 23({
2987 11(8(7(-5))): 8(-3)
2988 })
2989 })))
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002990 */
Laurence Lundbladecc7da412020-12-27 00:09:07 -08002991static const uint8_t spCSRWithTags[] = {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002992 0xd9, 0xd9, 0xf7, 0xd9, 0xd9, 0xf7, 0xd9, 0xd9, 0xf7, 0xa2,
2993 0xc6, 0xc7, 0x36,
2994 0xdb, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0xc7, 0xa2,
2995 0xda, 0x00, 0x00, 0x00, 0x07, 0x33,
2996 0xcb, 0xa5,
2997 0xd1, 0x31,
2998 0xd1, 0xd1, 0xd1, 0x6c,
2999 0x4f, 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74, 0x69, 0x6f, 0x6e,
3000 0xc9, 0x30,
3001 0xd9, 0x03, 0x05, 0x63,
3002 0x53, 0x53, 0x47,
3003 0x2e,
Laurence Lundblade9b334962020-08-27 10:55:53 -07003004 0xd0, 0xd1, 0xc6, 0xc7,
3005 0x69,
3006 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73, 0x69, 0x6f, 0x6e,
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003007 0xd1, 0x2f,
3008 0xd1, 0x69,
3009 0x53, 0x61, 0x6e, 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f,
3010 0xd1, 0x2d,
3011 0xd1, 0x62,
3012 0x55, 0x53,
3013 0xd7, 0x32,
3014 0xd3, 0xa2,
3015 0x2a,
3016 0xc9, 0xa1,
3017 0x28,
3018 0x26,
3019 0xda, 0x05, 0x66, 0x70, 0x89, 0xda, 0x05, 0x66, 0x70, 0x89, 0xda, 0x05, 0x66, 0x70, 0x89, 0x29,
3020 0xcc, 0x4a,
3021 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,0x07, 0x08, 0x09, 0x0a,
3022 0xd0, 0x35,
3023 0xd7, 0xa1,
3024 0xcb, 0xc8, 0xc7, 0x24,
3025 0xc8, 0x22};
3026
Laurence Lundblade9b334962020-08-27 10:55:53 -07003027
Laurence Lundbladecc7da412020-12-27 00:09:07 -08003028static const uint8_t spSpiffyTagInput[] = {
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08003029 0x85, // Open array
Laurence Lundblade9b334962020-08-27 10:55:53 -07003030
3031 0xc0, // tag for string date
3032 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string
3033
3034 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string
3035
3036 0x4a, '1','9','8','5','-','0','4','-','1','2', // Date string in byte string
3037
3038 0xd8, 0x23, // tag for regex
3039 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string
3040
3041 0xc0, // tag for string date
3042 0x4a, '1','9','8','5','-','0','4','-','1','2', // Date string in byte string
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07003043
3044 // This last case makes the array untraversable because it is
3045 // an uncrecoverable error. Make sure it stays last and is the only
3046 // instance so the other tests can work.
Laurence Lundblade9b334962020-08-27 10:55:53 -07003047};
3048
3049
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003050static int32_t CheckCSRMaps(QCBORDecodeContext *pDC);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003051
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003052
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003053int32_t OptTagParseTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003054{
3055 QCBORDecodeContext DCtx;
Laurence Lundblade9b334962020-08-27 10:55:53 -07003056 QCBORItem Item;
3057 QCBORError uError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003058
Laurence Lundbladeee851742020-01-08 08:37:05 -08003059 QCBORDecode_Init(&DCtx,
Laurence Lundblade9b334962020-08-27 10:55:53 -07003060 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTagInput),
Laurence Lundbladeee851742020-01-08 08:37:05 -08003061 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003062
Laurence Lundblade9b334962020-08-27 10:55:53 -07003063 /*
3064 This test matches the magic number tag and the fraction tag
3065 55799([...])
3066 */
3067 uError = QCBORDecode_GetNext(&DCtx, &Item);
3068 if(uError != QCBOR_SUCCESS) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003069 return -2;
3070 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003071 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003072 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC)) {
3073 return -3;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003074 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003075
Laurence Lundblade9b334962020-08-27 10:55:53 -07003076 /*
3077 4([1,3])
3078 */
3079 uError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundbladedd6e76e2021-03-10 01:54:01 -07003080#ifdef QCBOR_DISABLE_EXP_AND_MANTISSA
Laurence Lundblade9b334962020-08-27 10:55:53 -07003081 if(uError != QCBOR_SUCCESS ||
3082 Item.uDataType != QCBOR_TYPE_ARRAY ||
3083 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_DECIMAL_FRACTION) ||
3084 QCBORDecode_GetNthTag(&DCtx, &Item, 0) != CBOR_TAG_DECIMAL_FRACTION ||
3085 QCBORDecode_GetNthTag(&DCtx, &Item, 1) != CBOR_TAG_INVALID64 ||
3086 QCBORDecode_GetNthTag(&DCtx, &Item, 2) != CBOR_TAG_INVALID64 ||
3087 QCBORDecode_GetNthTag(&DCtx, &Item, 3) != CBOR_TAG_INVALID64 ||
3088 QCBORDecode_GetNthTag(&DCtx, &Item, 4) != CBOR_TAG_INVALID64 ||
3089 Item.val.uCount != 2) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003090 return -4;
3091 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07003092 // consume the items in the array
3093 uError = QCBORDecode_GetNext(&DCtx, &Item);
3094 uError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundblade59289e52019-12-30 13:44:37 -08003095
Laurence Lundbladedd6e76e2021-03-10 01:54:01 -07003096#else /* QCBOR_DISABLE_EXP_AND_MANTISSA */
Laurence Lundblade9b334962020-08-27 10:55:53 -07003097 if(uError != QCBOR_SUCCESS ||
3098 Item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION ||
3099 QCBORDecode_GetNthTag(&DCtx, &Item, 0) != CBOR_TAG_INVALID64 ||
3100 QCBORDecode_GetNthTag(&DCtx, &Item, 1) != CBOR_TAG_INVALID64 ||
3101 QCBORDecode_GetNthTag(&DCtx, &Item, 2) != CBOR_TAG_INVALID64 ||
3102 QCBORDecode_GetNthTag(&DCtx, &Item, 3) != CBOR_TAG_INVALID64 ||
3103 QCBORDecode_GetNthTag(&DCtx, &Item, 4) != CBOR_TAG_INVALID64 ) {
3104 return -5;
Laurence Lundblade59289e52019-12-30 13:44:37 -08003105 }
Laurence Lundbladedd6e76e2021-03-10 01:54:01 -07003106#endif /* QCBOR_DISABLE_EXP_AND_MANTISSA */
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003107
Laurence Lundblade9b334962020-08-27 10:55:53 -07003108 /*
3109 More than 4 tags on an item 225(226(227(228(229([])))))
3110 */
3111 uError = QCBORDecode_GetNext(&DCtx, &Item);
3112 if(uError != QCBOR_ERR_TOO_MANY_TAGS) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003113 return -6;
3114 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07003115
Laurence Lundblade88e9db22020-11-02 03:56:33 -08003116 if(QCBORDecode_GetNthTag(&DCtx, &Item, 0) != CBOR_TAG_INVALID64) {
3117 return -106;
3118 }
3119
3120
Laurence Lundblade9b334962020-08-27 10:55:53 -07003121 /* tag 10489608748473423768(
3122 2442302356(
3123 21590(
3124 240(
3125 []))))
3126 */
3127 uError = QCBORDecode_GetNext(&DCtx, &Item);
3128 if(uError != QCBOR_SUCCESS ||
3129 Item.uDataType != QCBOR_TYPE_ARRAY ||
3130 QCBORDecode_GetNthTag(&DCtx, &Item, 3) != 10489608748473423768ULL ||
3131 QCBORDecode_GetNthTag(&DCtx, &Item, 2) != 2442302356ULL ||
3132 QCBORDecode_GetNthTag(&DCtx, &Item, 1) != 21590ULL ||
3133 QCBORDecode_GetNthTag(&DCtx, &Item, 0) != 240ULL) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003134 return -7;
Laurence Lundblade9b334962020-08-27 10:55:53 -07003135 }
3136
3137 /* tag 21590(
3138 10489608748473423768(
3139 2442302357(
3140 21591(
3141 []))))
3142 */
3143 uError = QCBORDecode_GetNext(&DCtx, &Item);
3144 if(uError != QCBOR_SUCCESS ||
3145 Item.uDataType != QCBOR_TYPE_ARRAY ||
3146 QCBORDecode_GetNthTag(&DCtx, &Item, 0) != 65534ULL ||
3147 QCBORDecode_GetNthTag(&DCtx, &Item, 1) != 2442302357ULL ||
3148 QCBORDecode_GetNthTag(&DCtx, &Item, 2) != 10489608748473423768ULL ||
3149 QCBORDecode_GetNthTag(&DCtx, &Item, 3) != 21590ULL) {
3150 return -8;
3151 }
3152
3153 /* Make sure to blow past the limit of tags that must be mapped.
3154 works in conjuntion with entries above.
3155 269488144(269488145(269488146(269488147([]))))
3156 */
3157 uError = QCBORDecode_GetNext(&DCtx, &Item);
3158 if(uError != QCBOR_ERR_TOO_MANY_TAGS) {
3159 return -9;
3160 }
3161
3162 uError = QCBORDecode_GetNext(&DCtx, &Item);
3163 if(uError == QCBOR_SUCCESS) {
3164 return -10;
3165 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003166
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003167 // ----------------------------------
Laurence Lundbladeee851742020-01-08 08:37:05 -08003168 // This test sets up a caller-config list that includes the very large
Laurence Lundblade9b334962020-08-27 10:55:53 -07003169 // tage and then matches it. Caller-config lists are no longer
3170 // used or needed. This tests backwards compatibility with them.
Laurence Lundbladeee851742020-01-08 08:37:05 -08003171 QCBORDecode_Init(&DCtx,
3172 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEncodedLargeTag),
3173 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003174 const uint64_t puList[] = {0x9192939495969798, 257};
3175 const QCBORTagListIn TL = {2, puList};
3176 QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003177
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003178 if(QCBORDecode_GetNext(&DCtx, &Item)) {
3179 return -8;
3180 }
3181 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
3182 !QCBORDecode_IsTagged(&DCtx, &Item, 0x9192939495969798) ||
3183 QCBORDecode_IsTagged(&DCtx, &Item, 257) ||
3184 QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_BIGFLOAT) ||
3185 Item.val.uCount != 0) {
3186 return -9;
3187 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003188
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003189 //------------------------
Laurence Lundbladeee851742020-01-08 08:37:05 -08003190 // Sets up a caller-configured list and look up something not in it
Laurence Lundblade9b334962020-08-27 10:55:53 -07003191 // Another backwards compatibility test.
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003192 const uint64_t puLongList[17] = {1,2,1};
3193 const QCBORTagListIn TLLong = {17, puLongList};
Laurence Lundbladeee851742020-01-08 08:37:05 -08003194 QCBORDecode_Init(&DCtx,
3195 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEncodedLargeTag),
3196 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003197 QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TLLong);
3198 if(QCBORDecode_GetNext(&DCtx, &Item)) {
3199 return -11;
3200 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003201
Laurence Lundbladef7a70bc2020-10-24 12:23:25 -07003202 uint64_t puTags[4];
Laurence Lundblade9b334962020-08-27 10:55:53 -07003203 QCBORTagListOut Out = {0, 4, puTags};
3204
3205
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003206 // This tests retrievel of the full tag list
Laurence Lundbladeee851742020-01-08 08:37:05 -08003207 QCBORDecode_Init(&DCtx,
3208 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spLotsOfTags),
3209 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003210 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3211 return -12;
3212 }
3213 if(puTags[0] != 0x9192939495969798 ||
3214 puTags[1] != 0x88 ||
Laurence Lundblade59289e52019-12-30 13:44:37 -08003215 puTags[2] != 0x06 ||
3216 puTags[3] != 0x07) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003217 return -13;
3218 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003219
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003220 // ----------------------
Laurence Lundblade9b334962020-08-27 10:55:53 -07003221 // This tests too small of an out list
Laurence Lundbladeee851742020-01-08 08:37:05 -08003222 QCBORDecode_Init(&DCtx,
3223 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spLotsOfTags),
3224 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003225 QCBORTagListOut OutSmall = {0, 3, puTags};
3226 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &OutSmall) != QCBOR_ERR_TOO_MANY_TAGS) {
3227 return -14;
3228 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003229
Laurence Lundblade9b334962020-08-27 10:55:53 -07003230
3231
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003232 // ---------------
Laurence Lundblade9b334962020-08-27 10:55:53 -07003233 // Decode a version of the "CSR" that has had a ton of tags randomly inserted
3234 // It is a bit of a messy test and maybe could be improved, but
3235 // it is retained as a backwards compatibility check.
Laurence Lundbladeee851742020-01-08 08:37:05 -08003236 QCBORDecode_Init(&DCtx,
3237 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRWithTags),
3238 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003239 int n = CheckCSRMaps(&DCtx);
3240 if(n) {
3241 return n-2000;
3242 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003243
Laurence Lundblade59289e52019-12-30 13:44:37 -08003244 Out = (QCBORTagListOut){0, 16, puTags};
Laurence Lundbladeee851742020-01-08 08:37:05 -08003245 QCBORDecode_Init(&DCtx,
3246 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRWithTags),
3247 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003248
Laurence Lundblade9b334962020-08-27 10:55:53 -07003249 /* With the spiffy decode revision, this tag list is not used.
3250 It doesn't matter if a tag is in this list or not so some
3251 tests that couldn't process a tag because it isn't in this list
3252 now can process these unlisted tags. The tests have been
3253 adjusted for this. */
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003254 const uint64_t puTagList[] = {773, 1, 90599561};
3255 const QCBORTagListIn TagList = {3, puTagList};
3256 QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TagList);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003257
3258
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003259 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3260 return -100;
3261 }
3262 if(Item.uDataType != QCBOR_TYPE_MAP ||
3263 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC) ||
3264 QCBORDecode_IsTagged(&DCtx, &Item, 90599561) ||
3265 QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_DATE_EPOCH) ||
3266 Item.val.uCount != 2 ||
3267 puTags[0] != CBOR_TAG_CBOR_MAGIC ||
3268 puTags[1] != CBOR_TAG_CBOR_MAGIC ||
3269 puTags[2] != CBOR_TAG_CBOR_MAGIC ||
3270 Out.uNumUsed != 3) {
3271 return -101;
3272 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003273
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003274 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3275 return -102;
3276 }
3277 if(Item.uDataType != QCBOR_TYPE_MAP ||
3278 QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC) ||
3279 QCBORDecode_IsTagged(&DCtx, &Item, 6) ||
Laurence Lundbladef7a70bc2020-10-24 12:23:25 -07003280 !QCBORDecode_IsTagged(&DCtx, &Item, 7) ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003281 Item.val.uCount != 2 ||
3282 puTags[0] != 5859837686836516696 ||
3283 puTags[1] != 7 ||
3284 Out.uNumUsed != 2) {
3285 return -103;
3286 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003287
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003288 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3289 return -104;
3290 }
3291 if(Item.uDataType != QCBOR_TYPE_MAP ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003292 Item.val.uCount != 5 ||
3293 puTags[0] != 0x0b ||
3294 Out.uNumUsed != 1) {
3295 return -105;
3296 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003297
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003298 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3299 return -106;
3300 }
3301 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
3302 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_COSE_MAC0) ||
3303 Item.val.string.len != 12 ||
3304 puTags[0] != CBOR_TAG_COSE_MAC0 ||
3305 puTags[1] != CBOR_TAG_COSE_MAC0 ||
3306 puTags[2] != CBOR_TAG_COSE_MAC0 ||
3307 Out.uNumUsed != 3) {
3308 return -105;
3309 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003310
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003311 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3312 return -107;
3313 }
3314 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
3315 !QCBORDecode_IsTagged(&DCtx, &Item, 773) ||
3316 Item.val.string.len != 3 ||
3317 puTags[0] != 773 ||
3318 Out.uNumUsed != 1) {
3319 return -108;
3320 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003321
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003322 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3323 return -109;
3324 }
3325 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade59289e52019-12-30 13:44:37 -08003326 !QCBORDecode_IsTagged(&DCtx, &Item, 16) ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003327 Item.val.string.len != 9 ||
Laurence Lundblade59289e52019-12-30 13:44:37 -08003328 puTags[0] != 16 ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07003329 puTags[3] != 7 ||
3330 Out.uNumUsed != 4) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003331 return -110;
3332 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003333
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003334 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3335 return -111;
3336 }
3337 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
3338 !QCBORDecode_IsTagged(&DCtx, &Item, 17) ||
3339 Item.val.string.len != 9 ||
3340 puTags[0] != 17 ||
3341 Out.uNumUsed != 1) {
3342 return -112;
3343 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003344
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003345 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3346 return -111;
3347 }
3348 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
3349 !QCBORDecode_IsTagged(&DCtx, &Item, 17) ||
3350 Item.val.string.len != 2 ||
3351 puTags[0] != 17 ||
3352 Out.uNumUsed != 1) {
3353 return -112;
3354 }
3355
3356 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3357 return -113;
3358 }
3359 if(Item.uDataType != QCBOR_TYPE_MAP ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07003360 !QCBORDecode_IsTagged(&DCtx, &Item, 19) ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003361 Item.val.uCount != 2 ||
3362 puTags[0] != 19 ||
3363 Out.uNumUsed != 1) {
3364 return -114;
3365 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003366
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003367 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3368 return -115;
3369 }
3370 if(Item.uDataType != QCBOR_TYPE_MAP ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07003371 !QCBORDecode_IsTagged(&DCtx, &Item, 9) ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003372 Item.val.uCount != 1 ||
3373 puTags[0] != 9 ||
3374 Out.uNumUsed != 1) {
3375 return -116;
3376 }
3377
3378 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3379 return -116;
3380 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003381 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003382 Item.val.int64 != -7 ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003383 Out.uNumUsed != 0) {
3384 return -117;
3385 }
3386
3387 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3388 return -118;
3389 }
3390 if(Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
3391 Item.val.string.len != 10 ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003392 puTags[0] != 12 ||
3393 Out.uNumUsed != 1) {
3394 return -119;
3395 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003396
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003397 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3398 return -120;
3399 }
3400 if(Item.uDataType != QCBOR_TYPE_MAP ||
3401 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_ENC_AS_B16) ||
3402 Item.val.uCount != 1 ||
3403 puTags[0] != 0x17 ||
3404 Out.uNumUsed != 1) {
3405 return -121;
3406 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003407
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003408 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3409 return -122;
3410 }
3411 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07003412 !QCBORDecode_IsTagged(&DCtx, &Item, 8) ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003413 Item.val.int64 != -3 ||
3414 puTags[0] != 8 ||
3415 Out.uNumUsed != 1) {
3416 return -123;
3417 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003418
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003419 if(QCBORDecode_Finish(&DCtx)) {
3420 return -124;
3421 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07003422
3423 UsefulBufC DateString;
3424 QCBORDecode_Init(&DCtx,
3425 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSpiffyTagInput),
3426 QCBOR_DECODE_MODE_NORMAL);
3427
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07003428 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade9b334962020-08-27 10:55:53 -07003429 // tagged date string
3430 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString);
3431 // untagged date string
3432 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString);
3433 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_SUCCESS) {
3434 return 100;
3435 }
3436 // untagged byte string
3437 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString);
3438 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
3439 return 101;
3440 }
3441 // tagged regex
3442 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString);
3443 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
3444 return 102;
3445 }
3446 // tagged date string with a byte string
3447 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString);
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07003448 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT) {
Laurence Lundblade9b334962020-08-27 10:55:53 -07003449 return 103;
3450 }
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07003451 // The exit errors out because the last item, the date string with
3452 // bad content makes the array untraversable (the bad date string
3453 // could have tag content of an array or such that is not consumed
3454 // by the date decoding).
Laurence Lundblade9b334962020-08-27 10:55:53 -07003455 QCBORDecode_ExitArray(&DCtx);
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07003456 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT) {
Laurence Lundblade9b334962020-08-27 10:55:53 -07003457 return 104;
3458 }
3459
3460
3461 QCBORDecode_Init(&DCtx,
3462 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSpiffyTagInput),
3463 QCBOR_DECODE_MODE_NORMAL);
3464
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07003465 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade9b334962020-08-27 10:55:53 -07003466 // tagged date string
3467 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString);
3468 // untagged date string
3469 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString);
3470 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_SUCCESS) {
3471 return 200;
3472 }
3473 // untagged byte string
3474 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString);
3475 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
3476 return 201;
3477 }
3478 // tagged regex
3479 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString);
3480 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
3481 return 202;
3482 }
3483 // tagged date string with a byte string
3484 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString);
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07003485 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT) {
Laurence Lundblade9b334962020-08-27 10:55:53 -07003486 return 203;
3487 }
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07003488 // See comments above
Laurence Lundblade9b334962020-08-27 10:55:53 -07003489 QCBORDecode_ExitArray(&DCtx);
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07003490 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT) {
Laurence Lundblade9b334962020-08-27 10:55:53 -07003491 return 204;
3492 }
3493
3494 QCBORDecode_Init(&DCtx,
3495 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSpiffyTagInput),
3496 QCBOR_DECODE_MODE_NORMAL);
3497
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07003498 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade9b334962020-08-27 10:55:53 -07003499 // tagged date string
3500 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString);
3501 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
3502 return 300;
3503 }
3504 // untagged date string
3505 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString);
3506 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
3507 return 301;
3508 }
3509 // untagged byte string
3510 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString);
3511 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
3512 return 302;
3513 }
3514 // tagged regex
3515 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString);
3516 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
3517 return 303;
3518 }
3519 // tagged date string with a byte string
3520 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString);
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07003521 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT) {
Laurence Lundblade9b334962020-08-27 10:55:53 -07003522 return 304;
3523 }
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07003524 // See comments above
Laurence Lundblade9b334962020-08-27 10:55:53 -07003525 QCBORDecode_ExitArray(&DCtx);
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07003526 if(QCBORDecode_Finish(&DCtx) != QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT) {
Laurence Lundblade9b334962020-08-27 10:55:53 -07003527 return 305;
3528 }
3529
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003530 return 0;
3531}
3532
3533
3534
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003535
Laurence Lundbladecc7da412020-12-27 00:09:07 -08003536static const uint8_t spBigNumInput[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003537 0x83,
3538 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3539 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3540 0xA4,
3541 0x63, 0x42, 0x4E, 0x2B,
3542 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3543 0x18, 0x40,
3544 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3545 0x63, 0x42, 0x4E, 0x2D,
3546 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3547 0x38, 0x3F,
3548 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
3549
Laurence Lundbladecc7da412020-12-27 00:09:07 -08003550/* The expected big num */
3551static const uint8_t spBigNum[] = {
3552 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3553 0x00};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003554
3555
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003556int32_t BignumParseTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003557{
3558 QCBORDecodeContext DCtx;
3559 QCBORItem Item;
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07003560 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003561
Laurence Lundbladeee851742020-01-08 08:37:05 -08003562 QCBORDecode_Init(&DCtx,
3563 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNumInput),
3564 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003565
3566
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003567 //
3568 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
3569 return -1;
3570 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003571 return -2;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003572 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003573
3574 //
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003575 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003576 return -3;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003577 if(Item.uDataType != QCBOR_TYPE_POSBIGNUM ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003578 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003579 return -4;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003580 }
3581
3582 //
3583 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003584 return -5;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003585 if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003586 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003587 return -6;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003588 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003589
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003590 //
3591 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003592 return -7;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003593 if(Item.uDataType != QCBOR_TYPE_MAP) {
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003594 return -8;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003595 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003596
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003597 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003598 return -9;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003599 if(Item.uDataType != QCBOR_TYPE_POSBIGNUM ||
3600 Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003601 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003602 return -10;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003603 }
3604
3605 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003606 return -11;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003607 if(Item.uDataType != QCBOR_TYPE_POSBIGNUM ||
3608 Item.uLabelType != QCBOR_TYPE_INT64 ||
3609 Item.label.int64 != 64 ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003610 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003611 return -12;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003612 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003613
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003614 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003615 return -13;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003616 if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM ||
3617 Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003618 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003619 return -14;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003620 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003621
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003622 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003623 return -15;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003624 if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM ||
3625 Item.uLabelType != QCBOR_TYPE_INT64 ||
3626 Item.label.int64 != -64 ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003627 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003628 return -16;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003629 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003630
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003631 return 0;
3632}
3633
3634
3635
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003636static int32_t CheckItemWithIntLabel(QCBORDecodeContext *pCtx,
Laurence Lundbladeee851742020-01-08 08:37:05 -08003637 uint8_t uDataType,
3638 uint8_t uNestingLevel,
3639 uint8_t uNextNest,
3640 int64_t nLabel,
3641 QCBORItem *pItem)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003642{
3643 QCBORItem Item;
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07003644 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003645
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003646 if((nCBORError = QCBORDecode_GetNext(pCtx, &Item))) return -1;
3647 if(Item.uDataType != uDataType) return -1;
3648 if(uNestingLevel > 0) {
Laurence Lundbladeee851742020-01-08 08:37:05 -08003649 if(Item.uLabelType != QCBOR_TYPE_INT64 &&
3650 Item.uLabelType != QCBOR_TYPE_UINT64) {
3651 return -1;
3652 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003653 if(Item.uLabelType == QCBOR_TYPE_INT64) {
3654 if(Item.label.int64 != nLabel) return -1;
3655 } else {
Laurence Lundblade570fab52018-10-13 18:28:27 +08003656 if(Item.label.uint64 != (uint64_t)nLabel) return -1;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003657 }
3658 }
3659 if(Item.uNestingLevel != uNestingLevel) return -1;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303660 if(Item.uNextNestLevel != uNextNest) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003661
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003662 if(pItem) {
3663 *pItem = Item;
3664 }
3665 return 0;
3666}
3667
3668
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003669// Same code checks definite and indefinite length versions of the map
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003670static int32_t CheckCSRMaps(QCBORDecodeContext *pDC)
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003671{
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303672 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 0, 1, 0, NULL)) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003673
Laurence Lundblade9b334962020-08-27 10:55:53 -07003674 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 1, 2, -23, NULL)) return -2;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003675
Laurence Lundblade9b334962020-08-27 10:55:53 -07003676 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 2, 3, -20, NULL)) return -3;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003677
Laurence Lundblade9b334962020-08-27 10:55:53 -07003678 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -18, NULL)) return -4;
3679 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -17, NULL)) return -5;
3680 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -15, NULL)) return -6;
3681 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -16, NULL)) return -7;
3682 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 2, -14, NULL)) return -8;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003683
Laurence Lundblade9b334962020-08-27 10:55:53 -07003684 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 2, 3, -19, NULL)) return -9;
3685 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 3, 4, -11, NULL)) return -10;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003686
Laurence Lundblade9b334962020-08-27 10:55:53 -07003687 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_INT64, 4, 3, -9, NULL)) return -11;
3688 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_BYTE_STRING, 3, 1, -10, NULL)) return -12;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003689
Laurence Lundblade9b334962020-08-27 10:55:53 -07003690 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 1, 2, -22, NULL)) return -13;
3691 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_INT64, 2, 0, -5, NULL)) return -14;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003692
Laurence Lundblade9b334962020-08-27 10:55:53 -07003693 if(QCBORDecode_Finish(pDC)) return -20;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003694
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003695 return 0;
3696}
3697
3698
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003699/*
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003700{
3701 -23: {
3702 -20: {
3703 -18: "Organization",
3704 -17: "SSG",
3705 -15: "Confusion",
3706 -16: "San Diego",
3707 -14: "US"
3708 },
3709 -19: {
3710 -11: {
3711 -9: -7
3712 },
3713 -10: '\u0001\u0002\u0003\u0004\u0005\u0006\a\b\t\n'
3714 }
3715 },
3716 -22: {
3717 -5: -3
3718 }
3719}
Laurence Lundbladecc7da412020-12-27 00:09:07 -08003720*/
3721static const uint8_t spCSRInput[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003722 0xa2, 0x36, 0xa2, 0x33, 0xa5, 0x31, 0x6c, 0x4f,
3723 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74,
3724 0x69, 0x6f, 0x6e, 0x30, 0x63, 0x53, 0x53, 0x47,
3725 0x2e, 0x69, 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73,
3726 0x69, 0x6f, 0x6e, 0x2f, 0x69, 0x53, 0x61, 0x6e,
3727 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f, 0x2d, 0x62,
3728 0x55, 0x53, 0x32, 0xa2, 0x2a, 0xa1, 0x28, 0x26,
3729 0x29, 0x4a, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
3730 0x07, 0x08, 0x09, 0x0a, 0x35, 0xa1, 0x24, 0x22};
3731
Laurence Lundbladecc7da412020-12-27 00:09:07 -08003732// Same map as above, but using indefinite lengths
3733static const uint8_t spCSRInputIndefLen[] = {
3734 0xbf, 0x36, 0xbf, 0x33, 0xbf, 0x31, 0x6c, 0x4f,
3735 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74,
3736 0x69, 0x6f, 0x6e, 0x30, 0x63, 0x53, 0x53, 0x47,
3737 0x2e, 0x69, 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73,
3738 0x69, 0x6f, 0x6e, 0x2f, 0x69, 0x53, 0x61, 0x6e,
3739 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f, 0x2d, 0x62,
3740 0x55, 0x53, 0xff, 0x32, 0xbf, 0x2a, 0xbf, 0x28,
3741 0x26, 0xff, 0x29, 0x4a, 0x01, 0x02, 0x03, 0x04,
3742 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0xff, 0xff,
3743 0x35, 0xbf, 0x24, 0x22, 0xff, 0xff};
3744
3745
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003746int32_t NestedMapTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003747{
3748 QCBORDecodeContext DCtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003749
Laurence Lundbladeee851742020-01-08 08:37:05 -08003750 QCBORDecode_Init(&DCtx,
3751 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput),
3752 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003753
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003754 return CheckCSRMaps(&DCtx);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003755}
3756
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08003757
3758
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003759int32_t StringDecoderModeFailTest()
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08003760{
3761 QCBORDecodeContext DCtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003762
Laurence Lundbladeee851742020-01-08 08:37:05 -08003763 QCBORDecode_Init(&DCtx,
3764 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput),
3765 QCBOR_DECODE_MODE_MAP_STRINGS_ONLY);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003766
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08003767 QCBORItem Item;
3768 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003769
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08003770 if(QCBORDecode_GetNext(&DCtx, &Item)) {
3771 return -1;
3772 }
3773 if(Item.uDataType != QCBOR_TYPE_MAP) {
3774 return -2;
3775 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003776
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08003777 nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
3778 if(nCBORError != QCBOR_ERR_MAP_LABEL_TYPE) {
3779 return -3;
3780 }
3781
3782 return 0;
3783}
3784
3785
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003786
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003787int32_t NestedMapTestIndefLen()
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003788{
3789 QCBORDecodeContext DCtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003790
Laurence Lundbladeee851742020-01-08 08:37:05 -08003791 QCBORDecode_Init(&DCtx,
3792 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInputIndefLen),
3793 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003794
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003795 return CheckCSRMaps(&DCtx);
3796}
3797
3798
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003799
Laurence Lundblade17ede402018-10-13 11:43:07 +08003800static UsefulBufC make_nested_indefinite_arrays(int n, UsefulBuf Storage)
3801{
3802 UsefulOutBuf UOB;
3803 UsefulOutBuf_Init(&UOB, Storage);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003804
Laurence Lundblade17ede402018-10-13 11:43:07 +08003805 int i;
3806 for(i = 0; i < n; i++) {
3807 UsefulOutBuf_AppendByte(&UOB, 0x9f);
3808 }
3809
3810 for(i = 0; i < n; i++) {
3811 UsefulOutBuf_AppendByte(&UOB, 0xff);
3812 }
3813 return UsefulOutBuf_OutUBuf(&UOB);
3814}
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003815
3816
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003817static int32_t parse_indeflen_nested(UsefulBufC Nested, int nNestLevel)
Laurence Lundblade17ede402018-10-13 11:43:07 +08003818{
3819 QCBORDecodeContext DC;
3820 QCBORDecode_Init(&DC, Nested, 0);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003821
Laurence Lundblade17ede402018-10-13 11:43:07 +08003822 int j;
3823 for(j = 0; j < nNestLevel; j++) {
3824 QCBORItem Item;
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07003825 QCBORError nReturn = QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade17ede402018-10-13 11:43:07 +08003826 if(j >= QCBOR_MAX_ARRAY_NESTING) {
3827 // Should be in error
Laurence Lundbladea9489f82020-09-12 13:50:56 -07003828 if(nReturn != QCBOR_ERR_ARRAY_DECODE_NESTING_TOO_DEEP) {
Laurence Lundblade17ede402018-10-13 11:43:07 +08003829 return -4;
3830 } else {
3831 return 0; // Decoding doesn't recover after an error
3832 }
3833 } else {
3834 // Should be no error
3835 if(nReturn) {
3836 return -9; // Should not have got an error
3837 }
3838 }
3839 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
3840 return -7;
3841 }
3842 }
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07003843 QCBORError nReturn = QCBORDecode_Finish(&DC);
Laurence Lundblade17ede402018-10-13 11:43:07 +08003844 if(nReturn) {
3845 return -3;
3846 }
3847 return 0;
3848}
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003849
3850
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003851int32_t IndefiniteLengthNestTest()
Laurence Lundblade17ede402018-10-13 11:43:07 +08003852{
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05303853 UsefulBuf_MAKE_STACK_UB(Storage, 50);
Laurence Lundblade17ede402018-10-13 11:43:07 +08003854 int i;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003855 for(i=1; i < QCBOR_MAX_ARRAY_NESTING+4; i++) {
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08003856 const UsefulBufC Nested = make_nested_indefinite_arrays(i, Storage);
Laurence Lundblade17ede402018-10-13 11:43:07 +08003857 int nReturn = parse_indeflen_nested(Nested, i);
3858 if(nReturn) {
3859 return nReturn;
3860 }
3861 }
3862 return 0;
3863}
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003864
Laurence Lundbladeee851742020-01-08 08:37:05 -08003865// [1, [2, 3]]
3866static const uint8_t spIndefiniteArray[] = {0x9f, 0x01, 0x82, 0x02, 0x03, 0xff};
3867// No closing break
3868static const uint8_t spIndefiniteArrayBad1[] = {0x9f};
3869// Not enough closing breaks
3870static const uint8_t spIndefiniteArrayBad2[] = {0x9f, 0x9f, 0x02, 0xff};
3871// Too many closing breaks
3872static const uint8_t spIndefiniteArrayBad3[] = {0x9f, 0x02, 0xff, 0xff};
3873// Unclosed indeflen inside def len
3874static const uint8_t spIndefiniteArrayBad4[] = {0x81, 0x9f};
3875// confused tag
3876static const uint8_t spIndefiniteArrayBad5[] = {0x9f, 0xd1, 0xff};
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003877
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003878int32_t IndefiniteLengthArrayMapTest()
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003879{
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07003880 QCBORError nResult;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003881 // --- first test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003882 UsefulBufC IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArray);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003883
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003884 // Decode it and see if it is OK
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003885 QCBORDecodeContext DC;
3886 QCBORItem Item;
3887 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003888
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003889 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05303890
3891 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
3892 Item.uNestingLevel != 0 ||
3893 Item.uNextNestLevel != 1) {
3894 return -111;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003895 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003896
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003897 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05303898 if(Item.uDataType != QCBOR_TYPE_INT64 ||
3899 Item.uNestingLevel != 1 ||
3900 Item.uNextNestLevel != 1) {
3901 return -2;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003902 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003903
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003904 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05303905 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
3906 Item.uNestingLevel != 1 ||
3907 Item.uNextNestLevel != 2) {
3908 return -3;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003909 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003910
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003911 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade12b495d2018-12-17 11:15:54 -08003912 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade6de37062018-10-15 12:22:42 +05303913 Item.uNestingLevel != 2 ||
3914 Item.uNextNestLevel != 2) {
3915 return -4;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003916 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003917
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003918 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade12b495d2018-12-17 11:15:54 -08003919 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade6de37062018-10-15 12:22:42 +05303920 Item.uNestingLevel != 2 ||
3921 Item.uNextNestLevel != 0) {
3922 return -5;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003923 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003924
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003925 if(QCBORDecode_Finish(&DC)) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303926 return -6;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003927 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003928
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003929 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003930 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad1);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003931
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003932 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003933
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003934 nResult = QCBORDecode_GetNext(&DC, &Item);
3935 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303936 return -7;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003937 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003938
Laurence Lundblade570fab52018-10-13 18:28:27 +08003939 nResult = QCBORDecode_Finish(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07003940 if(nResult != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303941 return -8;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003942 }
3943
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003944
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003945 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003946 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad2);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003947
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003948 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003949
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003950 nResult = QCBORDecode_GetNext(&DC, &Item);
3951 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303952 return -9;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003953 }
3954
3955 nResult = QCBORDecode_GetNext(&DC, &Item);
3956 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303957 return -10;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003958 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003959
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003960 nResult = QCBORDecode_GetNext(&DC, &Item);
3961 if(nResult || Item.uDataType != QCBOR_TYPE_INT64) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303962 return -11;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003963 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003964
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003965 nResult = QCBORDecode_Finish(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07003966 if(nResult != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303967 return -12;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003968 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003969
3970
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003971 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003972 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad3);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003973
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003974 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003975
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003976 nResult = QCBORDecode_GetNext(&DC, &Item);
3977 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303978 return -13;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003979 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003980
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003981 nResult = QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade642282a2020-06-23 12:00:33 -07003982 if(nResult != QCBOR_SUCCESS) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303983 return -14;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003984 }
Laurence Lundblade6de37062018-10-15 12:22:42 +05303985
Laurence Lundblade642282a2020-06-23 12:00:33 -07003986 nResult = QCBORDecode_GetNext(&DC, &Item);
3987 if(nResult != QCBOR_ERR_BAD_BREAK) {
3988 return -140;
3989 }
3990
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003991
Laurence Lundblade570fab52018-10-13 18:28:27 +08003992 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003993 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad4);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003994
Laurence Lundblade570fab52018-10-13 18:28:27 +08003995 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003996
Laurence Lundblade570fab52018-10-13 18:28:27 +08003997 nResult = QCBORDecode_GetNext(&DC, &Item);
3998 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303999 return -15;
Laurence Lundblade570fab52018-10-13 18:28:27 +08004000 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004001
Laurence Lundblade570fab52018-10-13 18:28:27 +08004002 nResult = QCBORDecode_GetNext(&DC, &Item);
4003 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05304004 return -16;
Laurence Lundblade570fab52018-10-13 18:28:27 +08004005 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004006
Laurence Lundblade570fab52018-10-13 18:28:27 +08004007 nResult = QCBORDecode_Finish(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07004008 if(nResult != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05304009 return -17;
Laurence Lundblade570fab52018-10-13 18:28:27 +08004010 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004011
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304012 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07004013 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad5);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004014
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304015 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004016
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304017 nResult = QCBORDecode_GetNext(&DC, &Item);
4018 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05304019 return -18;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304020 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004021
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304022 nResult = QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05304023 if(nResult != QCBOR_ERR_BAD_BREAK) {
4024 return -19;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304025 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004026
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08004027 return 0;
4028}
4029
Laurence Lundblade17ede402018-10-13 11:43:07 +08004030
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08004031#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
4032
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07004033static const uint8_t spIndefiniteLenString[] = {
Laurence Lundblade17ede402018-10-13 11:43:07 +08004034 0x81, // Array of length one
4035 0x7f, // text string marked with indefinite length
4036 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment
4037 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment
4038 0xff // ending break
4039};
4040
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07004041static const uint8_t spIndefiniteLenStringBad2[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304042 0x81, // Array of length one
4043 0x7f, // text string marked with indefinite length
4044 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment
4045 0x44, 0x6d, 0x69, 0x6e, 0x67, // second segment of wrong type
4046 0xff // ending break
4047};
4048
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07004049static const uint8_t spIndefiniteLenStringBad3[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304050 0x81, // Array of length one
4051 0x7f, // text string marked with indefinite length
4052 0x01, 0x02, // Not a string
4053 0xff // ending break
4054};
4055
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07004056static const uint8_t spIndefiniteLenStringBad4[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304057 0x81, // Array of length one
4058 0x7f, // text string marked with indefinite length
4059 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment
4060 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment
4061 // missing end of string
4062};
4063
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07004064static const uint8_t spIndefiniteLenStringLabel[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304065 0xa1, // Array of length one
4066 0x7f, // text string marked with indefinite length
4067 0x65, 0x73, 0x74, 0x72, 0x75, 0x75, // first segment
4068 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment
4069 0xff, // ending break
4070 0x01 // integer being labeled.
4071};
4072
Laurence Lundbladec5fef682020-01-25 11:38:45 -08004073/**
4074 Make an indefinite length string
4075
4076 @param Storage Storage for string, must be 144 bytes in size
4077 @return The indefinite length string
4078
4079 This makes an array with one indefinite length string that has 7 chunks
4080 from size of 1 byte up to 64 bytes.
4081 */
4082static UsefulBufC MakeIndefiniteBigBstr(UsefulBuf Storage)
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304083{
4084 UsefulOutBuf UOB;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004085
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304086 UsefulOutBuf_Init(&UOB, Storage);
4087 UsefulOutBuf_AppendByte(&UOB, 0x81);
4088 UsefulOutBuf_AppendByte(&UOB, 0x5f);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004089
Laurence Lundbladec5fef682020-01-25 11:38:45 -08004090 uint8_t uStringByte = 0;
4091 // Use of type int is intentional
4092 for(int uChunkSize = 1; uChunkSize <= 128; uChunkSize *= 2) {
4093 // Not using preferred encoding here, but that is OK.
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304094 UsefulOutBuf_AppendByte(&UOB, 0x58);
Laurence Lundbladec5fef682020-01-25 11:38:45 -08004095 UsefulOutBuf_AppendByte(&UOB, (uint8_t)uChunkSize);
4096 for(int j = 0; j < uChunkSize; j++) {
4097 UsefulOutBuf_AppendByte(&UOB, uStringByte);
4098 uStringByte++;
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304099 }
4100 }
4101 UsefulOutBuf_AppendByte(&UOB, 0xff);
4102
4103 return UsefulOutBuf_OutUBuf(&UOB);
4104}
4105
4106static int CheckBigString(UsefulBufC BigString)
4107{
4108 if(BigString.len != 255) {
4109 return 1;
4110 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004111
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304112 for(uint8_t i = 0; i < 255; i++){
4113 if(((const uint8_t *)BigString.ptr)[i] != i) {
4114 return 1;
4115 }
4116 }
4117 return 0;
4118}
4119
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05304120
Laurence Lundbladec5fef682020-01-25 11:38:45 -08004121int32_t IndefiniteLengthStringTest()
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304122{
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304123 QCBORDecodeContext DC;
4124 QCBORItem Item;
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05304125 // big enough for MakeIndefiniteBigBstr() + MemPool overhead
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004126 UsefulBuf_MAKE_STACK_UB(MemPool, 350);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004127
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304128 // --- Simple normal indefinite length string ------
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07004129 UsefulBufC IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenString);
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304130 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004131
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05304132 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304133 return -1;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304134 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004135
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304136 if(QCBORDecode_GetNext(&DC, &Item)) {
4137 return -2;
4138 }
4139 if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.uDataAlloc) {
4140 return -3;
4141 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004142
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304143 if(QCBORDecode_GetNext(&DC, &Item)) {
4144 return -4;
4145 }
4146 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || !Item.uDataAlloc) {
4147 return -5;
4148 }
4149 if(QCBORDecode_Finish(&DC)) {
4150 return -6;
4151 }
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304152
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304153 // ----- types mismatch ---
Laurence Lundbladeee851742020-01-08 08:37:05 -08004154 QCBORDecode_Init(&DC,
4155 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad2),
4156 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004157
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304158 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
4159 return -7;
4160 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004161
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304162 if(QCBORDecode_GetNext(&DC, &Item)) {
4163 return -8;
4164 }
4165 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
4166 return -9;
4167 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004168
Laurence Lundblade30816f22018-11-10 13:40:22 +07004169 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_INDEFINITE_STRING_CHUNK) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304170 return -10;
4171 }
4172
4173 // ----- not a string ---
Laurence Lundbladeee851742020-01-08 08:37:05 -08004174 QCBORDecode_Init(&DC,
4175 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad3),
4176 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004177
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304178 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
4179 return -11;
4180 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004181
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304182 if(QCBORDecode_GetNext(&DC, &Item)) {
4183 return -12;
4184 }
4185 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
4186 return -13;
4187 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004188
Laurence Lundblade30816f22018-11-10 13:40:22 +07004189 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_INDEFINITE_STRING_CHUNK) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304190 return -14;
4191 }
4192
4193 // ----- no end -----
Laurence Lundbladeee851742020-01-08 08:37:05 -08004194 QCBORDecode_Init(&DC,
4195 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad4),
4196 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004197
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304198 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
4199 return -15;
4200 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004201
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304202 if(QCBORDecode_GetNext(&DC, &Item)) {
4203 return -16;
4204 }
4205 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
4206 return -17;
4207 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004208
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304209 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_HIT_END) {
4210 return -18;
4211 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004212
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304213 // ------ Don't set a string allocator and see an error -----
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304214 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004215
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304216 QCBORDecode_GetNext(&DC, &Item);
4217 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304218 return -19;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304219 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004220
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304221 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_NO_STRING_ALLOCATOR) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304222 return -20;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304223 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004224
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304225 // ----- Mempool is way too small -----
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004226 UsefulBuf_MAKE_STACK_UB(MemPoolTooSmall, QCBOR_DECODE_MIN_MEM_POOL_SIZE-1);
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304227
4228 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
4229 if(!QCBORDecode_SetMemPool(&DC, MemPoolTooSmall, false)) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304230 return -21;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304231 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004232
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304233 // ----- Mempool is way too small -----
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05304234 UsefulBuf_MAKE_STACK_UB(BigIndefBStrStorage, 290);
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08004235 const UsefulBufC BigIndefBStr = MakeIndefiniteBigBstr(BigIndefBStrStorage);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004236
Laurence Lundbladeee851742020-01-08 08:37:05 -08004237 // 80 is big enough for MemPool overhead, but not BigIndefBStr
4238 UsefulBuf_MAKE_STACK_UB(MemPoolSmall, 80);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004239
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304240 QCBORDecode_Init(&DC, BigIndefBStr, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304241 if(QCBORDecode_SetMemPool(&DC, MemPoolSmall, false)) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304242 return -22;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304243 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004244
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304245 QCBORDecode_GetNext(&DC, &Item);
4246 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304247 return -23;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304248 }
Laurence Lundblade30816f22018-11-10 13:40:22 +07004249 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_STRING_ALLOCATE) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304250 return -24;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304251 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004252
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304253 // ---- big bstr -----
4254 QCBORDecode_Init(&DC, BigIndefBStr, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004255
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304256 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
4257 return -25;
4258 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004259
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05304260 if(QCBORDecode_GetNext(&DC, &Item)) {
4261 return -26;
4262 }
4263 if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.uDataAlloc) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304264 return -26;
4265 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004266
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304267 if(QCBORDecode_GetNext(&DC, &Item)) {
4268 return -27;
4269 }
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05304270 if(Item.uDataType != QCBOR_TYPE_BYTE_STRING || !Item.uDataAlloc || Item.uNestingLevel != 1) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304271 return -28;
4272 }
4273 if(CheckBigString(Item.val.string)) {
4274 return -3;
4275 }
4276 if(QCBORDecode_Finish(&DC)) {
4277 return -29;
4278 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004279
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304280 // --- label is an indefinite length string ------
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07004281 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringLabel), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004282
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304283 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
4284 return -30;
4285 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004286
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304287 QCBORDecode_GetNext(&DC, &Item);
4288 if(Item.uDataType != QCBOR_TYPE_MAP) {
4289 return -31;
4290 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004291
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304292 if(QCBORDecode_GetNext(&DC, &Item)){
4293 return -32;
4294 }
Laurence Lundbladeee851742020-01-08 08:37:05 -08004295 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
4296 Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304297 Item.uDataAlloc || !Item.uLabelAlloc ||
4298 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("struuming"))) {
4299 return -33;
4300 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004301
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304302 if(QCBORDecode_Finish(&DC)) {
4303 return -34;
4304 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004305
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004306 return 0;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08004307}
4308
4309
Laurence Lundbladec5fef682020-01-25 11:38:45 -08004310int32_t AllocAllStringsTest()
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304311{
4312 QCBORDecodeContext DC;
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004313 QCBORError nCBORError;
4314
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004315
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304316 // First test, use the "CSRMap" as easy input and checking
Laurence Lundbladeee851742020-01-08 08:37:05 -08004317 QCBORDecode_Init(&DC,
4318 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput),
4319 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004320
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004321 UsefulBuf_MAKE_STACK_UB(Pool, sizeof(spCSRInput) + QCBOR_DECODE_MIN_MEM_POOL_SIZE);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004322
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004323 nCBORError = QCBORDecode_SetMemPool(&DC, Pool, 1); // Turn on copying.
4324 if(nCBORError) {
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304325 return -1;
4326 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004327
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004328 if(CheckCSRMaps(&DC)) {
4329 return -2;
4330 }
4331
Laurence Lundblade2f467f92020-10-09 17:50:11 -07004332 // Next parse, save pointers to a few strings, destroy original and
4333 // see all is OK.
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004334 UsefulBuf_MAKE_STACK_UB(CopyOfStorage, sizeof(pValidMapEncoded) + QCBOR_DECODE_MIN_MEM_POOL_SIZE);
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08004335 const UsefulBufC CopyOf = UsefulBuf_Copy(CopyOfStorage, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded));
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08004336
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304337 QCBORDecode_Init(&DC, CopyOf, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08004338 UsefulBuf_Set(Pool, '/');
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304339 QCBORDecode_SetMemPool(&DC, Pool, 1); // Turn on copying.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004340
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304341 QCBORItem Item1, Item2, Item3, Item4;
4342 if((nCBORError = QCBORDecode_GetNext(&DC, &Item1)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07004343 return (int32_t)nCBORError;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304344 if(Item1.uDataType != QCBOR_TYPE_MAP ||
4345 Item1.val.uCount != 3)
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004346 return -3;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304347 if((nCBORError = QCBORDecode_GetNext(&DC, &Item1)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07004348 return (int32_t)nCBORError;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304349 if((nCBORError = QCBORDecode_GetNext(&DC, &Item2)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07004350 return (int32_t)nCBORError;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304351 if((nCBORError = QCBORDecode_GetNext(&DC, &Item3)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07004352 return (int32_t)nCBORError;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304353 if((nCBORError = QCBORDecode_GetNext(&DC, &Item4)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07004354 return (int32_t)nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004355
Laurence Lundblade05ec57b2018-10-21 01:50:03 +05304356 UsefulBuf_Set(CopyOfStorage, '_');
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004357
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304358 if(Item1.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304359 Item1.uDataType != QCBOR_TYPE_INT64 ||
4360 Item1.val.int64 != 42 ||
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004361 Item1.uDataAlloc != 0 ||
4362 Item1.uLabelAlloc == 0 ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07004363 UsefulBufCompareToSZ(Item1.label.string, "first integer")) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004364 return -4;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09004365 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004366
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304367
4368 if(Item2.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07004369 UsefulBufCompareToSZ(Item2.label.string, "an array of two strings") ||
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304370 Item2.uDataType != QCBOR_TYPE_ARRAY ||
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004371 Item2.uDataAlloc != 0 ||
4372 Item2.uLabelAlloc == 0 ||
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304373 Item2.val.uCount != 2)
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004374 return -5;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004375
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304376 if(Item3.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004377 Item3.uDataAlloc == 0 ||
4378 Item3.uLabelAlloc != 0 ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07004379 UsefulBufCompareToSZ(Item3.val.string, "string1")) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004380 return -6;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09004381 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004382
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304383 if(Item4.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004384 Item4.uDataAlloc == 0 ||
4385 Item4.uLabelAlloc != 0 ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07004386 UsefulBufCompareToSZ(Item4.val.string, "string2")) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004387 return -7;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09004388 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004389
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304390 // Next parse with a pool that is too small
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004391 UsefulBuf_MAKE_STACK_UB(SmallPool, QCBOR_DECODE_MIN_MEM_POOL_SIZE + 1);
Laurence Lundbladeee851742020-01-08 08:37:05 -08004392 QCBORDecode_Init(&DC,
4393 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded),
4394 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304395 QCBORDecode_SetMemPool(&DC, SmallPool, 1); // Turn on copying.
4396 if((nCBORError = QCBORDecode_GetNext(&DC, &Item1)))
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004397 return -8;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304398 if(Item1.uDataType != QCBOR_TYPE_MAP ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09004399 Item1.val.uCount != 3) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004400 return -9;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09004401 }
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304402 if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item1))){
4403 if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item2))) {
4404 if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item3))) {
4405 nCBORError = QCBORDecode_GetNext(&DC, &Item4);
4406 }
4407 }
4408 }
Laurence Lundblade30816f22018-11-10 13:40:22 +07004409 if(nCBORError != QCBOR_ERR_STRING_ALLOCATE) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004410 return -10;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304411 }
4412
4413 return 0;
4414}
4415
Laurence Lundbladef6531662018-12-04 10:42:22 +09004416
Laurence Lundbladec5fef682020-01-25 11:38:45 -08004417int32_t MemPoolTest(void)
Laurence Lundblade0155b622018-10-12 20:04:37 +08004418{
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004419 // Set up the decoder with a tiny bit of CBOR to parse because
4420 // nothing can be done with it unless that is set up.
Laurence Lundbladef6531662018-12-04 10:42:22 +09004421 QCBORDecodeContext DC;
4422 const uint8_t pMinimalCBOR[] = {0xa0}; // One empty map
4423 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pMinimalCBOR),0);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004424
Laurence Lundbladef6531662018-12-04 10:42:22 +09004425 // Set up an memory pool of 100 bytes
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004426 // Then fish into the internals of the decode context
4427 // to get the allocator function so it can be called directly.
4428 // Also figure out how much pool is available for use
4429 // buy subtracting out the overhead.
Laurence Lundbladef6531662018-12-04 10:42:22 +09004430 UsefulBuf_MAKE_STACK_UB(Pool, 100);
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004431 QCBORError nError = QCBORDecode_SetMemPool(&DC, Pool, 0);
4432 if(nError) {
4433 return -9;
4434 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004435 QCBORStringAllocate pAlloc = DC.StringAllocator.pfAllocator;
4436 void *pAllocCtx = DC.StringAllocator.pAllocateCxt;
4437 size_t uAvailPool = Pool.len - QCBOR_DECODE_MIN_MEM_POOL_SIZE;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004438
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004439 // First test -- ask for one more byte than available and see failure
4440 UsefulBuf Allocated = (*pAlloc)(pAllocCtx, NULL, uAvailPool+1);
Laurence Lundbladef6531662018-12-04 10:42:22 +09004441 if(!UsefulBuf_IsNULL(Allocated)) {
4442 return -1;
4443 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004444
Laurence Lundbladef6531662018-12-04 10:42:22 +09004445 // Re do the set up for the next test that will do a successful alloc,
4446 // a fail, a free and then success
Laurence Lundbladef6531662018-12-04 10:42:22 +09004447 QCBORDecode_SetMemPool(&DC, Pool, 0);
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004448 pAlloc = DC.StringAllocator.pfAllocator;
4449 pAllocCtx = DC.StringAllocator.pAllocateCxt;
4450 uAvailPool = Pool.len - QCBOR_DECODE_MIN_MEM_POOL_SIZE;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004451
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004452 // Allocate one byte less than available and see success
4453 Allocated = (pAlloc)(pAllocCtx, NULL, uAvailPool-1);
Laurence Lundbladef6531662018-12-04 10:42:22 +09004454 if(UsefulBuf_IsNULL(Allocated)) { // expected to succeed
4455 return -2;
4456 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004457 // Ask for some more and see failure
4458 UsefulBuf Allocated2 = (*pAlloc)(pAllocCtx, NULL, uAvailPool/2);
Laurence Lundbladef6531662018-12-04 10:42:22 +09004459 if(!UsefulBuf_IsNULL(Allocated2)) { // expected to fail
4460 return -3;
4461 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004462 // Free the first allocate, retry the second and see success
4463 (*pAlloc)(pAllocCtx, Allocated.ptr, 0); // Free
4464 Allocated = (*pAlloc)(pAllocCtx, NULL, uAvailPool/2);
Laurence Lundbladef6531662018-12-04 10:42:22 +09004465 if(UsefulBuf_IsNULL(Allocated)) { // succeed because of the free
4466 return -4;
4467 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004468
Laurence Lundbladef6531662018-12-04 10:42:22 +09004469 // Re do set up for next test that involves a successful alloc,
4470 // and a successful realloc and a failed realloc
4471 QCBORDecode_SetMemPool(&DC, Pool, 0);
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004472 pAlloc = DC.StringAllocator.pfAllocator;
4473 pAllocCtx = DC.StringAllocator.pAllocateCxt;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004474
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004475 // Allocate half the pool and see success
4476 Allocated = (*pAlloc)(pAllocCtx, NULL, uAvailPool/2);
Laurence Lundbladef6531662018-12-04 10:42:22 +09004477 if(UsefulBuf_IsNULL(Allocated)) { // expected to succeed
4478 return -5;
4479 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004480 // Reallocate to take up the whole pool and see success
4481 Allocated2 = (*pAlloc)(pAllocCtx, Allocated.ptr, uAvailPool);
Laurence Lundbladef6531662018-12-04 10:42:22 +09004482 if(UsefulBuf_IsNULL(Allocated2)) {
4483 return -6;
4484 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004485 // Make sure its the same pointer and the size is right
Laurence Lundbladef6531662018-12-04 10:42:22 +09004486 if(Allocated2.ptr != Allocated.ptr || Allocated2.len != uAvailPool) {
4487 return -7;
4488 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004489 // Try to allocate more to be sure there is failure after a realloc
4490 UsefulBuf Allocated3 = (*pAlloc)(pAllocCtx, Allocated.ptr, uAvailPool+1);
4491 if(!UsefulBuf_IsNULL(Allocated3)) {
Laurence Lundbladef6531662018-12-04 10:42:22 +09004492 return -8;
4493 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004494
Laurence Lundbladef6531662018-12-04 10:42:22 +09004495 return 0;
4496}
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004497
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004498
4499/* Just enough of an allocator to test configuration of one */
4500static UsefulBuf AllocateTestFunction(void *pCtx, void *pOldMem, size_t uNewSize)
4501{
4502 (void)pOldMem; // unused variable
4503
4504 if(uNewSize) {
4505 // Assumes the context pointer is the buffer and
4506 // nothing too big will ever be asked for.
4507 // This is only good for this basic test!
4508 return (UsefulBuf) {pCtx, uNewSize};
4509 } else {
4510 return NULLUsefulBuf;
4511 }
4512}
4513
4514
Laurence Lundbladec5fef682020-01-25 11:38:45 -08004515int32_t SetUpAllocatorTest(void)
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004516{
4517 // Set up the decoder with a tiny bit of CBOR to parse because
4518 // nothing can be done with it unless that is set up.
4519 QCBORDecodeContext DC;
4520 const uint8_t pMinimalCBOR[] = {0x62, 0x48, 0x69}; // "Hi"
4521 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pMinimalCBOR),0);
4522
4523 uint8_t pAllocatorBuffer[50];
4524
4525 // This is really just to test that this call works.
4526 // The full functionality of string allocators is tested
4527 // elsewhere with the MemPool internal allocator.
4528 QCBORDecode_SetUpAllocator(&DC, AllocateTestFunction, pAllocatorBuffer, 1);
4529
4530 QCBORItem Item;
4531 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_SUCCESS) {
4532 return -1;
4533 }
4534
4535 if(Item.uDataAlloc == 0 ||
4536 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
4537 Item.val.string.ptr != pAllocatorBuffer) {
4538 return -2;
4539 }
4540
4541 if(QCBORDecode_Finish(&DC) != QCBOR_SUCCESS) {
4542 return -3;
4543 }
4544
4545 return 0;
4546}
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08004547#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
4548
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004549
Laurence Lundbladedd6e76e2021-03-10 01:54:01 -07004550#ifndef QCBOR_DISABLE_EXP_AND_MANTISSA
Laurence Lundblade59289e52019-12-30 13:44:37 -08004551
Laurence Lundbladea826c502020-05-10 21:07:00 -07004552/* exponent, mantissa
Laurence Lundblade59289e52019-12-30 13:44:37 -08004553 [
4554 4([-1, 3]),
Laurence Lundbladea826c502020-05-10 21:07:00 -07004555 4([-20, 4759477275222530853136]),
4556 4([9223372036854775807, -4759477275222530853137]),
Laurence Lundblade59289e52019-12-30 13:44:37 -08004557 5([300, 100]),
Laurence Lundbladea826c502020-05-10 21:07:00 -07004558 5([-20, 4759477275222530853136]),
Laurence Lundblade59289e52019-12-30 13:44:37 -08004559 5([-9223372036854775807, -4759477275222530853137])
Laurence Lundbladea826c502020-05-10 21:07:00 -07004560 5([ 9223372036854775806, -4759477275222530853137])
4561 5([ 9223372036854775806, 9223372036854775806])]
Laurence Lundbladecc7da412020-12-27 00:09:07 -08004562 ]
Laurence Lundblade59289e52019-12-30 13:44:37 -08004563 */
Laurence Lundblade59289e52019-12-30 13:44:37 -08004564static const uint8_t spExpectedExponentsAndMantissas[] = {
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004565 0x88,
Laurence Lundblade59289e52019-12-30 13:44:37 -08004566 0xC4, 0x82, 0x20,
4567 0x03,
4568 0xC4, 0x82, 0x33,
4569 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,
4570 0xC4, 0x82, 0x1B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
4571 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,
4572 0xC5, 0x82, 0x19, 0x01, 0x2C,
4573 0x18, 0x64,
4574 0xC5, 0x82, 0x33,
4575 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,
4576 0xC5, 0x82, 0x3B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE,
4577 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,
Laurence Lundbladea826c502020-05-10 21:07:00 -07004578 0xC5, 0x82, 0x1B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE,
4579 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,
Laurence Lundblade59289e52019-12-30 13:44:37 -08004580 0xC5, 0x82, 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE,
4581 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE
4582};
4583
Laurence Lundbladefaec39f2020-08-02 21:53:53 -07004584
Laurence Lundbladec5fef682020-01-25 11:38:45 -08004585int32_t ExponentAndMantissaDecodeTests(void)
Laurence Lundblade59289e52019-12-30 13:44:37 -08004586{
4587 QCBORDecodeContext DC;
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004588 QCBORError uErr;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004589 QCBORItem item;
4590
Laurence Lundblade17af4902020-01-07 19:11:55 -08004591 static const uint8_t spBigNumMantissa[] = {0x01, 0x02, 0x03, 0x04, 0x05,
4592 0x06, 0x07, 0x08, 0x09, 0x010};
4593 UsefulBufC BN = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNumMantissa);
Laurence Lundblade59289e52019-12-30 13:44:37 -08004594
4595
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004596 QCBORDecode_Init(&DC,
4597 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedExponentsAndMantissas),
4598 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade59289e52019-12-30 13:44:37 -08004599
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004600 uErr = QCBORDecode_GetNext(&DC, &item);
4601 if(uErr != QCBOR_SUCCESS) {
Laurence Lundblade59289e52019-12-30 13:44:37 -08004602 return 1;
4603 }
4604
4605 if(item.uDataType != QCBOR_TYPE_ARRAY) {
4606 return 2;
4607 }
4608
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004609 uErr = QCBORDecode_GetNext(&DC, &item);
4610 if(uErr != QCBOR_SUCCESS) {
Laurence Lundblade59289e52019-12-30 13:44:37 -08004611 return 3;
4612 }
4613
4614 if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION ||
4615 item.val.expAndMantissa.Mantissa.nInt != 3 ||
4616 item.val.expAndMantissa.nExponent != -1) {
4617 return 4;
4618 }
4619
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004620 uErr = QCBORDecode_GetNext(&DC, &item);
4621 if(uErr != QCBOR_SUCCESS) {
Laurence Lundblade59289e52019-12-30 13:44:37 -08004622 return 5;
4623 }
4624
4625 if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM ||
4626 item.val.expAndMantissa.nExponent != -20 ||
4627 UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) {
4628 return 6;
4629 }
4630
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004631 uErr = QCBORDecode_GetNext(&DC, &item);
4632 if(uErr != QCBOR_SUCCESS) {
Laurence Lundblade59289e52019-12-30 13:44:37 -08004633 return 7;
4634 }
4635
4636 if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM ||
4637 item.val.expAndMantissa.nExponent != 9223372036854775807 ||
4638 UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) {
4639 return 8;
4640 }
4641
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004642 uErr = QCBORDecode_GetNext(&DC, &item);
4643 if(uErr != QCBOR_SUCCESS) {
Laurence Lundblade59289e52019-12-30 13:44:37 -08004644 return 9;
4645 }
4646
4647 if(item.uDataType != QCBOR_TYPE_BIGFLOAT ||
4648 item.val.expAndMantissa.Mantissa.nInt != 100 ||
4649 item.val.expAndMantissa.nExponent != 300) {
4650 return 10;
4651 }
4652
Laurence Lundbladea826c502020-05-10 21:07:00 -07004653 // 5([-20, 4759477275222530853136]),
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004654 uErr = QCBORDecode_GetNext(&DC, &item);
4655 if(uErr != QCBOR_SUCCESS) {
Laurence Lundblade59289e52019-12-30 13:44:37 -08004656 return 11;
4657 }
Laurence Lundblade59289e52019-12-30 13:44:37 -08004658 if(item.uDataType != QCBOR_TYPE_BIGFLOAT_POS_BIGNUM ||
4659 item.val.expAndMantissa.nExponent != -20 ||
4660 UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) {
4661 return 12;
4662 }
4663
Laurence Lundbladea826c502020-05-10 21:07:00 -07004664 // 5([-9223372036854775807, -4759477275222530853137])
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004665 uErr = QCBORDecode_GetNext(&DC, &item);
4666 if(uErr != QCBOR_SUCCESS) {
Laurence Lundblade59289e52019-12-30 13:44:37 -08004667 return 13;
4668 }
Laurence Lundblade59289e52019-12-30 13:44:37 -08004669 if(item.uDataType != QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM ||
4670 item.val.expAndMantissa.nExponent != -9223372036854775807 ||
4671 UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) {
4672 return 14;
4673 }
4674
Laurence Lundbladea826c502020-05-10 21:07:00 -07004675 // 5([ 9223372036854775806, -4759477275222530853137])
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004676 uErr = QCBORDecode_GetNext(&DC, &item);
4677 if(uErr != QCBOR_SUCCESS) {
4678 return 15;
Laurence Lundbladea826c502020-05-10 21:07:00 -07004679 }
4680 if(item.uDataType != QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM ||
4681 item.val.expAndMantissa.nExponent != 9223372036854775806 ||
4682 UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) {
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004683 return 16;
Laurence Lundbladea826c502020-05-10 21:07:00 -07004684 }
4685
Laurence Lundbladea826c502020-05-10 21:07:00 -07004686 // 5([ 9223372036854775806, 9223372036854775806])]
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004687 uErr = QCBORDecode_GetNext(&DC, &item);
4688 if(uErr != QCBOR_SUCCESS) {
4689 return 17;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004690 }
Laurence Lundblade59289e52019-12-30 13:44:37 -08004691 if(item.uDataType != QCBOR_TYPE_BIGFLOAT ||
4692 item.val.expAndMantissa.nExponent != 9223372036854775806 ||
4693 item.val.expAndMantissa.Mantissa.nInt!= 9223372036854775806 ) {
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004694 return 18;
4695 }
4696
4697 uErr = QCBORDecode_Finish(&DC);
4698 if(uErr != QCBOR_SUCCESS) {
4699 return 18;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004700 }
4701
4702 /* Now encode some stuff and then decode it */
4703 uint8_t pBuf[40];
4704 QCBOREncodeContext EC;
4705 UsefulBufC Encoded;
4706
4707 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(pBuf));
4708 QCBOREncode_OpenArray(&EC);
4709 QCBOREncode_AddDecimalFraction(&EC, 999, 1000); // 999 * (10 ^ 1000)
4710 QCBOREncode_AddBigFloat(&EC, 100, INT32_MIN);
4711 QCBOREncode_AddDecimalFractionBigNum(&EC, BN, false, INT32_MAX);
4712 QCBOREncode_CloseArray(&EC);
4713 QCBOREncode_Finish(&EC, &Encoded);
4714
4715
4716 QCBORDecode_Init(&DC, Encoded, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004717 uErr = QCBORDecode_GetNext(&DC, &item);
4718 if(uErr != QCBOR_SUCCESS) {
4719 return 100;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004720 }
4721
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004722 uErr = QCBORDecode_GetNext(&DC, &item);
4723 if(uErr != QCBOR_SUCCESS) {
4724 return 101;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004725 }
4726
4727 if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION ||
4728 item.val.expAndMantissa.nExponent != 1000 ||
4729 item.val.expAndMantissa.Mantissa.nInt != 999) {
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004730 return 102;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004731 }
4732
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004733 uErr = QCBORDecode_GetNext(&DC, &item);
4734 if(uErr != QCBOR_SUCCESS) {
4735 return 103;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004736 }
4737
4738 if(item.uDataType != QCBOR_TYPE_BIGFLOAT ||
4739 item.val.expAndMantissa.nExponent != INT32_MIN ||
4740 item.val.expAndMantissa.Mantissa.nInt != 100) {
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004741 return 104;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004742 }
4743
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004744 uErr = QCBORDecode_GetNext(&DC, &item);
4745 if(uErr != QCBOR_SUCCESS) {
4746 return 105;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004747 }
4748
4749 if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM ||
4750 item.val.expAndMantissa.nExponent != INT32_MAX ||
4751 UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) {
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004752 return 106;
4753 }
4754
4755
4756 int64_t nExp, nMant;
4757 UsefulBuf_MAKE_STACK_UB( MantBuf, 20);
4758 UsefulBufC Mant;
4759 bool bIsNeg;
4760
4761 QCBORDecode_Init(&DC,
4762 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedExponentsAndMantissas),
4763 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07004764 QCBORDecode_EnterArray(&DC, NULL);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004765
4766 // 4([-1, 3]),
Laurence Lundblade9b334962020-08-27 10:55:53 -07004767 QCBORDecode_GetDecimalFraction(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nExp, &nMant);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004768
4769 // 4([-20, 4759477275222530853136]),
Laurence Lundblade9b334962020-08-27 10:55:53 -07004770 QCBORDecode_GetDecimalFractionBig(&DC, QCBOR_TAG_REQUIREMENT_TAG, MantBuf,
4771 &Mant, &bIsNeg, &nExp);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004772
4773 // 4([9223372036854775807, -4759477275222530853137]),
Laurence Lundblade9b334962020-08-27 10:55:53 -07004774 QCBORDecode_GetDecimalFractionBig(&DC, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
4775 MantBuf, &Mant, &bIsNeg, &nExp);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004776
4777 // 5([300, 100]),
Laurence Lundblade9b334962020-08-27 10:55:53 -07004778 QCBORDecode_GetBigFloat(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nExp, &nMant);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004779
4780 // 5([-20, 4759477275222530853136]),
Laurence Lundblade9b334962020-08-27 10:55:53 -07004781 QCBORDecode_GetBigFloatBig(&DC, QCBOR_TAG_REQUIREMENT_TAG, MantBuf, &Mant,
4782 &bIsNeg, &nExp);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004783
4784 // 5([-9223372036854775807, -4759477275222530853137])
Laurence Lundblade9b334962020-08-27 10:55:53 -07004785 QCBORDecode_GetBigFloatBig(&DC, QCBOR_TAG_REQUIREMENT_TAG, MantBuf, &Mant,
4786 &bIsNeg, &nExp);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004787
4788 // 5([ 9223372036854775806, -4759477275222530853137])
Laurence Lundblade9b334962020-08-27 10:55:53 -07004789 QCBORDecode_GetBigFloatBig(&DC, QCBOR_TAG_REQUIREMENT_TAG, MantBuf, &Mant,
4790 &bIsNeg, &nExp);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004791
4792 // 5([ 9223372036854775806, 9223372036854775806])]
Laurence Lundblade9b334962020-08-27 10:55:53 -07004793 QCBORDecode_GetBigFloatBig(&DC, QCBOR_TAG_REQUIREMENT_TAG, MantBuf, &Mant,
4794 &bIsNeg, &nExp);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004795
4796 QCBORDecode_ExitArray(&DC);
4797
4798 uErr = QCBORDecode_Finish(&DC);
4799 if(uErr != QCBOR_SUCCESS) {
4800 return 200;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004801 }
4802
4803 return 0;
4804}
4805
4806
Laurence Lundbladecc7da412020-12-27 00:09:07 -08004807static const struct FailInput ExponentAndMantissaFailures[] = {
Laurence Lundblade59289e52019-12-30 13:44:37 -08004808 // Exponent > INT64_MAX
4809 { {(uint8_t[]){0xC4, 0x82, 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
4810 0xFF, 0xFF, 0x1B, 0x80, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
4811 0xFF, 0xFF,}, 20}, QCBOR_ERR_BAD_EXP_AND_MANTISSA},
4812 // Mantissa > INT64_MAX
4813 { {(uint8_t[]){0xC4, 0x82, 0x1B, 0x80, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
4814 0xFF, 0xFF, 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05,
4815 0x06, 0x07, 0x08, 0x09, 0x10}, 23}, QCBOR_ERR_BAD_EXP_AND_MANTISSA},
4816 // End of input
Laurence Lundbladee6f15112020-07-23 18:44:16 -07004817 { {(uint8_t[]){0xC4, 0x82}, 2}, QCBOR_ERR_NO_MORE_ITEMS},
Laurence Lundblade59289e52019-12-30 13:44:37 -08004818 // End of input
Laurence Lundbladee6f15112020-07-23 18:44:16 -07004819 { {(uint8_t[]){0xC4, 0x82, 0x01}, 3}, QCBOR_ERR_NO_MORE_ITEMS},
Laurence Lundblade59289e52019-12-30 13:44:37 -08004820 // bad content for big num
4821 { {(uint8_t[]){0xC4, 0x82, 0x01, 0xc3, 0x01}, 5}, QCBOR_ERR_BAD_OPT_TAG},
4822 // bad content for big num
4823 { {(uint8_t[]){0xC4, 0x82, 0xc2, 0x01, 0x1f}, 5}, QCBOR_ERR_BAD_INT},
4824 // Bad integer for exponent
4825 { {(uint8_t[]){0xC4, 0x82, 0x01, 0x1f}, 4}, QCBOR_ERR_BAD_INT},
4826 // Bad integer for mantissa
4827 { {(uint8_t[]){0xC4, 0x82, 0x1f, 0x01}, 4}, QCBOR_ERR_BAD_INT},
4828 // 3 items in array
4829 { {(uint8_t[]){0xC4, 0x83, 0x03, 0x01, 02}, 5}, QCBOR_ERR_BAD_EXP_AND_MANTISSA},
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08004830#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundblade59289e52019-12-30 13:44:37 -08004831 // unterminated indefinite length array
4832 { {(uint8_t[]){0xC4, 0x9f, 0x03, 0x01, 0x02}, 5}, QCBOR_ERR_BAD_EXP_AND_MANTISSA},
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08004833#else /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
4834 // unterminated indefinite length array
4835 { {(uint8_t[]){0xC4, 0x9f, 0x03, 0x01, 0x02}, 5}, QCBOR_ERR_INDEF_LEN_ARRAYS_DISABLED},
4836#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
Laurence Lundblade59289e52019-12-30 13:44:37 -08004837 // Empty array
4838 { {(uint8_t[]){0xC4, 0x80}, 2}, QCBOR_ERR_NO_MORE_ITEMS},
4839 // Second is not an integer
4840 { {(uint8_t[]){0xC4, 0x82, 0x03, 0x40}, 4}, QCBOR_ERR_BAD_EXP_AND_MANTISSA},
4841 // First is not an integer
4842 { {(uint8_t[]){0xC4, 0x82, 0x40}, 3}, QCBOR_ERR_BAD_EXP_AND_MANTISSA},
4843 // Not an array
4844 { {(uint8_t[]){0xC4, 0xa2}, 2}, QCBOR_ERR_BAD_EXP_AND_MANTISSA}
4845};
4846
4847
Laurence Lundbladec5fef682020-01-25 11:38:45 -08004848int32_t ExponentAndMantissaDecodeFailTests()
Laurence Lundblade59289e52019-12-30 13:44:37 -08004849{
4850 return ProcessFailures(ExponentAndMantissaFailures,
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08004851 C_ARRAY_COUNT(ExponentAndMantissaFailures,
4852 struct FailInput));
Laurence Lundblade59289e52019-12-30 13:44:37 -08004853}
4854
Laurence Lundbladedd6e76e2021-03-10 01:54:01 -07004855#endif /* QCBOR_DISABLE_EXP_AND_MANTISSA */
Laurence Lundbladebb87be22020-04-09 19:15:32 -07004856
4857
4858
4859/*
4860 Some basic CBOR with map and array used in a lot of tests.
4861 The map labels are all strings
4862
Laurence Lundblade8ffdb742020-05-07 02:49:18 -07004863 {
4864 "first integer": 42,
Laurence Lundbladebb87be22020-04-09 19:15:32 -07004865 "an array of two strings": [
4866 "string1", "string2"
4867 ],
4868 "map in a map": {
4869 "bytes 1": h'78787878',
4870 "bytes 2": h'79797979',
4871 "another int": 98,
4872 "text 2": "lies, damn lies and statistics"
4873 }
Laurence Lundbladeddebabe2020-11-22 11:32:17 -08004874 }
Laurence Lundbladebb87be22020-04-09 19:15:32 -07004875 */
Laurence Lundblade9b334962020-08-27 10:55:53 -07004876
Laurence Lundbladef7a70bc2020-10-24 12:23:25 -07004877int32_t SpiffyDecodeBasicMap(UsefulBufC input)
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07004878{
4879 QCBORItem Item1, Item2, Item3;
4880 int64_t nDecodedInt1, nDecodedInt2;
4881 UsefulBufC B1, B2, S1, S2, S3;
4882
4883 QCBORDecodeContext DCtx;
4884 QCBORError nCBORError;
4885
4886 QCBORDecode_Init(&DCtx, input, 0);
4887
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07004888 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07004889
4890 QCBORDecode_GetInt64InMapSZ(&DCtx, "first integer", &nDecodedInt1);
4891
4892 QCBORDecode_EnterMapFromMapSZ(&DCtx, "map in a map");
4893 QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2);
Laurence Lundblade323f8a92020-09-06 19:43:09 -07004894 QCBORDecode_GetByteStringInMapSZ(&DCtx, "bytes 1", &B1);
4895 QCBORDecode_GetByteStringInMapSZ(&DCtx, "bytes 2", &B2);
4896 QCBORDecode_GetTextStringInMapSZ(&DCtx, "text 2", &S1);
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07004897 QCBORDecode_ExitMap(&DCtx);
4898
4899 QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings");
4900 QCBORDecode_GetNext(&DCtx, &Item1);
4901 QCBORDecode_GetNext(&DCtx, &Item2);
4902 if(QCBORDecode_GetNext(&DCtx, &Item3) != QCBOR_ERR_NO_MORE_ITEMS) {
4903 return -400;
4904 }
4905 QCBORDecode_ExitArray(&DCtx);
4906
4907 // Parse the same array again using GetText() instead of GetItem()
4908 QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings");
Laurence Lundblade323f8a92020-09-06 19:43:09 -07004909 QCBORDecode_GetTextString(&DCtx, &S2);
4910 QCBORDecode_GetTextString(&DCtx, &S3);
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07004911 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) {
4912 return 5000;
4913 }
4914 /* QCBORDecode_GetText(&DCtx, &S3);
4915 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_NO_MORE_ITEMS) {
4916 return 5001;
4917 } */
4918
4919 QCBORDecode_ExitArray(&DCtx);
4920
4921 QCBORDecode_ExitMap(&DCtx);
4922
4923 nCBORError = QCBORDecode_Finish(&DCtx);
4924
4925 if(nCBORError) {
4926 return (int32_t)nCBORError;
4927 }
4928
4929 if(nDecodedInt1 != 42) {
4930 return 1001;
4931 }
4932
4933 if(nDecodedInt2 != 98) {
4934 return 1002;
4935 }
4936
4937 if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07004938 UsefulBufCompareToSZ(Item1.val.string, "string1")) {
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07004939 return 1003;
4940 }
4941
4942 if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07004943 UsefulBufCompareToSZ(Item2.val.string, "string2")) {
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07004944 return 1004;
4945 }
4946
Laurence Lundblade9b334962020-08-27 10:55:53 -07004947 if(UsefulBufCompareToSZ(S1, "lies, damn lies and statistics")) {
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07004948 return 1005;
4949 }
4950
4951 if(UsefulBuf_Compare(B1, UsefulBuf_FromSZ("xxxx"))){
4952 return 1006;
4953 }
4954
4955 if(UsefulBuf_Compare(B2, UsefulBuf_FromSZ("yyyy"))){
4956 return 1007;
4957 }
4958
4959 if(UsefulBuf_Compare(S2, UsefulBuf_FromSZ("string1"))){
4960 return 1008;
4961 }
4962
4963 if(UsefulBuf_Compare(S3, UsefulBuf_FromSZ("string2"))){
4964 return 1009;
4965 }
4966
4967 return 0;
4968}
4969
Laurence Lundbladeddebabe2020-11-22 11:32:17 -08004970/*
Laurence Lundbladecc7da412020-12-27 00:09:07 -08004971 {
4972 -75008: h'05083399',
4973 88: [],
4974 100100: {
4975 "sub1": {
4976 10: [
4977 0
4978 ],
4979 -75009: h'A46823990001',
4980 100100: {
4981 "json": "{ \"ueid\", \"xyz\"}",
4982 "subsub": {
4983 100002: h'141813191001'
4984 }
4985 }
Laurence Lundbladeddebabe2020-11-22 11:32:17 -08004986 }
Laurence Lundbladecc7da412020-12-27 00:09:07 -08004987 }
Laurence Lundbladeddebabe2020-11-22 11:32:17 -08004988 }
Laurence Lundbladeddebabe2020-11-22 11:32:17 -08004989 */
4990
4991static const uint8_t spNestedCBOR[] = {
Laurence Lundbladecc7da412020-12-27 00:09:07 -08004992 0xa3, 0x3a, 0x00, 0x01, 0x24, 0xff, 0x44, 0x05,
4993 0x08, 0x33, 0x99, 0x18, 0x58, 0x80, 0x1a, 0x00,
4994 0x01, 0x87, 0x04, 0xa1, 0x64, 0x73, 0x75, 0x62,
4995 0x31, 0xa3, 0x0a, 0x81, 0x00, 0x3a, 0x00, 0x01,
4996 0x25, 0x00, 0x46, 0xa4, 0x68, 0x23, 0x99, 0x00,
4997 0x01, 0x1a, 0x00, 0x01, 0x87, 0x04, 0xa2, 0x64,
4998 0x6a, 0x73, 0x6f, 0x6e, 0x70, 0x7b, 0x20, 0x22,
4999 0x75, 0x65, 0x69, 0x64, 0x22, 0x2c, 0x20, 0x22,
5000 0x78, 0x79, 0x7a, 0x22, 0x7d, 0x66, 0x73, 0x75,
5001 0x62, 0x73, 0x75, 0x62, 0xa1, 0x1a, 0x00, 0x01,
5002 0x86, 0xa2, 0x46, 0x14, 0x18, 0x13, 0x19, 0x10,
5003 0x01
Laurence Lundbladeddebabe2020-11-22 11:32:17 -08005004};
5005
5006/* Get item in multi-level nesting in spNestedCBOR */
5007static int32_t DecodeNestedGetSubSub(QCBORDecodeContext *pDCtx)
5008{
5009 UsefulBufC String;
5010
5011 uint8_t test_oemid_bytes[] = {0x14, 0x18, 0x13, 0x19, 0x10, 0x01};
5012 const struct q_useful_buf_c test_oemid = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(test_oemid_bytes);
5013
5014 QCBORDecode_EnterMapFromMapN(pDCtx, 100100);
5015 QCBORDecode_EnterMap(pDCtx, NULL);
5016 QCBORDecode_EnterMapFromMapN(pDCtx, 100100);
5017 QCBORDecode_EnterMapFromMapSZ(pDCtx, "subsub");
5018 QCBORDecode_GetByteStringInMapN(pDCtx, 100002, &String);
5019 if(QCBORDecode_GetError(pDCtx)) {
5020 return 4001;
5021 }
5022 if(UsefulBuf_Compare(String, test_oemid)) {
5023 return 4002;
5024 }
5025 QCBORDecode_ExitMap(pDCtx);
5026 QCBORDecode_ExitMap(pDCtx);
5027 QCBORDecode_ExitMap(pDCtx);
5028 QCBORDecode_ExitMap(pDCtx);
5029
5030 return 0;
5031}
5032
5033/* Iterations on the zero-length array in spNestedCBOR */
5034static int32_t DecodeNestedGetEmpty(QCBORDecodeContext *pDCtx)
5035{
5036 QCBORItem Item;
5037 QCBORError uErr;
5038
5039 QCBORDecode_EnterArrayFromMapN(pDCtx, 88);
5040 for(int x = 0; x < 20; x++) {
5041 uErr = QCBORDecode_GetNext(pDCtx, &Item);
5042 if(uErr != QCBOR_ERR_NO_MORE_ITEMS) {
5043 return 4100;
5044
5045 }
5046 }
5047 QCBORDecode_ExitArray(pDCtx);
5048 if(QCBORDecode_GetError(pDCtx)) {
5049 return 4101;
5050 }
5051
5052 return 0;
5053}
5054
5055/* Various iterations on the array that contains a zero in spNestedCBOR */
5056static int32_t DecodeNestedGetZero(QCBORDecodeContext *pDCtx)
5057{
5058 QCBORError uErr;
5059
5060 QCBORDecode_EnterMapFromMapN(pDCtx, 100100);
5061 QCBORDecode_EnterMapFromMapSZ(pDCtx, "sub1");
5062 QCBORDecode_EnterArrayFromMapN(pDCtx, 10);
5063 int64_t nInt = 99;
5064 QCBORDecode_GetInt64(pDCtx, &nInt);
5065 if(nInt != 0) {
5066 return 4200;
5067 }
5068 for(int x = 0; x < 20; x++) {
5069 QCBORItem Item;
5070 uErr = QCBORDecode_GetNext(pDCtx, &Item);
5071 if(uErr != QCBOR_ERR_NO_MORE_ITEMS) {
5072 return 4201;
5073
5074 }
5075 }
5076 QCBORDecode_ExitArray(pDCtx);
5077 if(QCBORDecode_GetAndResetError(pDCtx)) {
5078 return 4202;
5079 }
5080 QCBORDecode_EnterArrayFromMapN(pDCtx, 10);
5081 UsefulBufC dD;
5082 QCBORDecode_GetByteString(pDCtx, &dD);
5083 if(QCBORDecode_GetAndResetError(pDCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
5084 return 4203;
5085 }
5086 for(int x = 0; x < 20; x++) {
5087 QCBORDecode_GetByteString(pDCtx, &dD);
5088 uErr = QCBORDecode_GetAndResetError(pDCtx);
5089 if(uErr != QCBOR_ERR_NO_MORE_ITEMS) {
5090 return 4204;
5091 }
5092 }
5093 QCBORDecode_ExitArray(pDCtx);
5094 QCBORDecode_ExitMap(pDCtx);
5095 QCBORDecode_ExitMap(pDCtx);
5096
5097 return 0;
5098}
5099
5100/* Repeatedly enter and exit maps and arrays, go off the end of maps
5101 and arrays and such. */
Laurence Lundbladeb9702452021-03-08 21:02:57 -08005102static int32_t DecodeNestedIterate(void)
Laurence Lundbladeddebabe2020-11-22 11:32:17 -08005103{
5104 QCBORDecodeContext DCtx;
5105 int32_t nReturn;
5106 QCBORError uErr;
5107
5108 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spNestedCBOR), 0);
5109 QCBORDecode_EnterMap(&DCtx, NULL);
5110
5111 for(int j = 0; j < 5; j++) {
5112 for(int i = 0; i < 20; i++) {
5113 nReturn = DecodeNestedGetSubSub(&DCtx);
5114 if(nReturn) {
5115 return nReturn;
5116 }
5117 }
5118
5119 for(int i = 0; i < 20; i++) {
5120 nReturn = DecodeNestedGetEmpty(&DCtx);
5121 if(nReturn ) {
5122 return nReturn;
5123 }
5124 }
5125
5126 for(int i = 0; i < 20; i++) {
5127 nReturn = DecodeNestedGetZero(&DCtx);
5128 if(nReturn ) {
5129 return nReturn;
5130 }
5131 }
5132 }
5133
5134 QCBORDecode_ExitMap(&DCtx);
5135 uErr = QCBORDecode_Finish(&DCtx);
5136 if(uErr) {
5137 return (int32_t)uErr + 4100;
5138 }
5139
5140 return 0;
5141}
5142
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07005143
5144/*
Laurence Lundbladecc7da412020-12-27 00:09:07 -08005145 [
5146 23,
5147 6000,
5148 h'67616C6163746963',
5149 h'686176656E20746F6B656E'
5150 ]
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07005151 */
5152static const uint8_t spSimpleArray[] = {
Laurence Lundblade9b334962020-08-27 10:55:53 -07005153 0x84,
5154 0x17,
5155 0x19, 0x17, 0x70,
5156 0x48, 0x67, 0x61, 0x6C, 0x61, 0x63, 0x74, 0x69, 0x63,
5157 0x4B, 0x68, 0x61, 0x76, 0x65, 0x6E, 0x20, 0x74, 0x6F, 0x6B, 0x65, 0x6E};
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07005158
Laurence Lundbladecc7da412020-12-27 00:09:07 -08005159/* [h'', {}, [], 0] */
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08005160static const uint8_t spArrayOfEmpty[] = {0x84, 0x40, 0xa0, 0x80, 0x00};
5161
Laurence Lundbladecc7da412020-12-27 00:09:07 -08005162/* {} */
Laurence Lundbladee6f15112020-07-23 18:44:16 -07005163static const uint8_t spEmptyMap[] = {0xa0};
5164
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08005165#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundbladecc7da412020-12-27 00:09:07 -08005166/* {} */
Laurence Lundbladee6f15112020-07-23 18:44:16 -07005167static const uint8_t spEmptyInDefinteLengthMap[] = {0xbf, 0xff};
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07005168
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08005169
Laurence Lundbladef0499502020-08-01 11:55:57 -07005170/*
5171 {
5172 0: [],
5173 9: [
5174 [],
5175 []
5176 ],
5177 8: {
5178 1: [],
5179 2: {},
5180 3: []
5181 },
5182 4: {},
5183 5: [],
5184 6: [
5185 [],
5186 []
5187 ]
Laurence Lundbladecc7da412020-12-27 00:09:07 -08005188 }
Laurence Lundbladef0499502020-08-01 11:55:57 -07005189 */
5190static const uint8_t spMapOfEmpty[] = {
Laurence Lundbladecc7da412020-12-27 00:09:07 -08005191 0xa6, 0x00, 0x80, 0x09, 0x82, 0x80, 0x80, 0x08,
5192 0xa3, 0x01, 0x80, 0x02, 0xa0, 0x03, 0x80, 0x04,
5193 0xa0, 0x05, 0x9f, 0xff, 0x06, 0x9f, 0x80, 0x9f,
5194 0xff, 0xff};
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005195
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08005196#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
5197
5198
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005199/*
5200 Too many tags
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005201 Duplicate label
5202 Integer overflow
5203 Date overflow
5204
Laurence Lundbladecc7da412020-12-27 00:09:07 -08005205 {
5206 1: 224(225(226(227(4(0))))),
Laurence Lundbladecc7da412020-12-27 00:09:07 -08005207 3: -18446744073709551616,
5208 4: 1(1.0e+300),
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07005209 5: 0,
5210 8: 8
Laurence Lundbladecc7da412020-12-27 00:09:07 -08005211 }
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005212 */
5213static const uint8_t spRecoverableMapErrors[] = {
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07005214 0xa6,
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005215 0x01, 0xd8, 0xe0, 0xd8, 0xe1, 0xd8, 0xe2, 0xd8, 0xe3, 0xd8, 0x04, 0x00,
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005216 0x03, 0x3b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
5217 0x04, 0xc1, 0xfb, 0x7e, 0x37, 0xe4, 0x3c, 0x88, 0x00, 0x75, 0x9c,
5218 0x05, 0x00,
5219 0x05, 0x00,
5220 0x08, 0x08,
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005221};
5222
Laurence Lundbladecc7da412020-12-27 00:09:07 -08005223/* Bad break */
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005224static const uint8_t spUnRecoverableMapError1[] = {
5225 0xa2, 0xff, 0x01, 0x00, 0x02, 0x00
5226};
5227
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08005228#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundbladecc7da412020-12-27 00:09:07 -08005229/* No more items */
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005230static const uint8_t spUnRecoverableMapError2[] = {
5231 0xbf, 0x02, 0xbf, 0xff, 0x01, 0x00, 0x02, 0x00
5232};
5233
Laurence Lundbladecc7da412020-12-27 00:09:07 -08005234/* Hit end because string is too long */
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005235static const uint8_t spUnRecoverableMapError3[] = {
5236 0xbf, 0x02, 0x69, 0x64, 0x64, 0xff
5237};
5238
Laurence Lundbladecc7da412020-12-27 00:09:07 -08005239/* Hit end because string is too long */
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005240static const uint8_t spUnRecoverableMapError4[] = {
5241 0xbf,
5242 0x02, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f,
5243 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f,
5244 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
5245 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
5246 0xff
5247};
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08005248#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005249
Laurence Lundblade63926052021-03-29 16:05:51 -07005250const unsigned char not_well_formed_submod_section[] = {
5251 0xa1, 0x14, 0x1f,
5252};
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005253
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07005254
5255/* Array of length 3, but only two items. */
5256const unsigned char spBadConsumeInput[] = {
5257 0x83, 0x00, 0x00
5258};
5259
5260/* Tag nesting too deep. */
5261const unsigned char spBadConsumeInput2[] = {
5262 0x81,
5263 0xD8, 0x37,
5264 0xD8, 0x2C,
5265 0xD8, 0x21,
5266 0xD6,
5267 0xCB,
5268 00
5269};
5270
5271const unsigned char spBadConsumeInput3[] = {
5272 0x81, 0xc0, 0x81, 0x00
5273};
5274
5275const unsigned char spBadConsumeInput4[] = {
5276 0x81, 0x9f, 0x00, 0xff
5277};
5278
5279const unsigned char spBadConsumeInput5[] = {
5280 0xa1, 0x80, 0x00
5281};
5282
5283
5284
Laurence Lundbladebb87be22020-04-09 19:15:32 -07005285int32_t EnterMapTest()
5286{
Laurence Lundbladef0499502020-08-01 11:55:57 -07005287 QCBORItem Item1;
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005288 QCBORItem ArrayItem;
Laurence Lundbladebb87be22020-04-09 19:15:32 -07005289 QCBORDecodeContext DCtx;
Laurence Lundbladef0499502020-08-01 11:55:57 -07005290 int32_t nReturn;
5291 QCBORError uErr;
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005292
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08005293#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005294 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spMapOfEmpty), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005295 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005296
Laurence Lundbladef0499502020-08-01 11:55:57 -07005297
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005298 QCBORDecode_EnterArray(&DCtx, NULL); // Label 0
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005299 QCBORDecode_ExitArray(&DCtx);
5300
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005301 QCBORDecode_EnterArray(&DCtx, NULL); // Label 9
5302 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005303 QCBORDecode_ExitArray(&DCtx);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005304 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005305 QCBORDecode_ExitArray(&DCtx);
5306 QCBORDecode_ExitArray(&DCtx);
5307
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005308 QCBORDecode_EnterMap(&DCtx, NULL); // Label 8
5309 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005310 QCBORDecode_ExitArray(&DCtx);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005311 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005312 QCBORDecode_ExitMap(&DCtx);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005313 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005314 QCBORDecode_ExitArray(&DCtx);
5315 QCBORDecode_ExitMap(&DCtx);
5316
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005317 QCBORDecode_EnterMap(&DCtx, NULL); // Label4
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005318 QCBORDecode_ExitMap(&DCtx);
5319
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005320 QCBORDecode_EnterArray(&DCtx, NULL); // Label 5
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005321 QCBORDecode_ExitArray(&DCtx);
5322
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005323 QCBORDecode_EnterArray(&DCtx, NULL); // Label 6
5324 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005325 QCBORDecode_ExitArray(&DCtx);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005326 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005327 QCBORDecode_ExitArray(&DCtx);
5328 QCBORDecode_ExitArray(&DCtx);
5329
5330 QCBORDecode_ExitMap(&DCtx);
5331
5332 uErr = QCBORDecode_Finish(&DCtx);
5333 if(uErr != QCBOR_SUCCESS){
5334 return 3011;
5335 }
5336
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07005337 (void)pValidMapIndefEncoded;
Laurence Lundbladef7a70bc2020-10-24 12:23:25 -07005338 nReturn = SpiffyDecodeBasicMap(UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapIndefEncoded));
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07005339 if(nReturn) {
5340 return nReturn + 20000;
Laurence Lundbladebb87be22020-04-09 19:15:32 -07005341 }
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08005342#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
5343
Laurence Lundbladebb87be22020-04-09 19:15:32 -07005344
Laurence Lundbladef7a70bc2020-10-24 12:23:25 -07005345 nReturn = SpiffyDecodeBasicMap(UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded));
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005346 if(nReturn) {
5347 return nReturn;
5348 }
Laurence Lundbladebb87be22020-04-09 19:15:32 -07005349
Laurence Lundblade8ffdb742020-05-07 02:49:18 -07005350
Laurence Lundblade937ea812020-05-08 11:38:23 -07005351
Laurence Lundblade2f467f92020-10-09 17:50:11 -07005352 // These tests confirm the cursor is at the right place after entering
5353 // a map or array
Laurence Lundblade9b334962020-08-27 10:55:53 -07005354 const UsefulBufC ValidEncodedMap = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded);
Laurence Lundblade937ea812020-05-08 11:38:23 -07005355
5356 // Confirm cursor is at right place
Laurence Lundblade9b334962020-08-27 10:55:53 -07005357 QCBORDecode_Init(&DCtx, ValidEncodedMap, 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005358 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade937ea812020-05-08 11:38:23 -07005359 QCBORDecode_GetNext(&DCtx, &Item1);
5360 if(Item1.uDataType != QCBOR_TYPE_INT64) {
5361 return 2001;
5362 }
5363
5364
Laurence Lundblade9b334962020-08-27 10:55:53 -07005365 QCBORDecode_Init(&DCtx, ValidEncodedMap, 0);
Laurence Lundblade6f3f78e2020-08-31 13:09:14 -07005366 QCBORDecode_VGetNext(&DCtx, &Item1);
5367 QCBORDecode_VGetNext(&DCtx, &Item1);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005368 QCBORDecode_EnterArray(&DCtx, &ArrayItem);
5369 if(ArrayItem.uLabelType != QCBOR_TYPE_TEXT_STRING ||
5370 UsefulBuf_Compare(ArrayItem.label.string,
5371 UsefulBuf_FROM_SZ_LITERAL("an array of two strings"))) {
5372 return 2051;
5373 }
Laurence Lundblade937ea812020-05-08 11:38:23 -07005374 QCBORDecode_GetNext(&DCtx, &Item1);
5375 if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING) {
5376 return 2002;
5377 }
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005378 QCBORDecode_ExitArray(&DCtx);
5379 QCBORDecode_EnterMap(&DCtx, &ArrayItem);
5380 if(ArrayItem.uLabelType != QCBOR_TYPE_TEXT_STRING ||
5381 UsefulBuf_Compare(ArrayItem.label.string,
5382 UsefulBuf_FROM_SZ_LITERAL("map in a map"))) {
5383 return 2052;
5384 }
5385
Laurence Lundblade937ea812020-05-08 11:38:23 -07005386
Laurence Lundblade9b334962020-08-27 10:55:53 -07005387 QCBORDecode_Init(&DCtx, ValidEncodedMap, 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005388 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade64b607e2020-05-13 13:05:57 -07005389 QCBORDecode_GetNext(&DCtx, &Item1);
5390 QCBORDecode_GetNext(&DCtx, &Item1);
5391 QCBORDecode_GetNext(&DCtx, &Item1);
Laurence Lundblade937ea812020-05-08 11:38:23 -07005392 QCBORDecode_EnterMapFromMapSZ(&DCtx, "map in a map");
5393 QCBORDecode_GetNext(&DCtx, &Item1);
5394 if(Item1.uDataType != QCBOR_TYPE_BYTE_STRING) {
5395 return 2003;
5396 }
5397
Laurence Lundblade9b334962020-08-27 10:55:53 -07005398 QCBORDecode_Init(&DCtx, ValidEncodedMap, 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005399 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade937ea812020-05-08 11:38:23 -07005400 QCBORDecode_GetNext(&DCtx, &Item1);
5401 QCBORDecode_GetNext(&DCtx, &Item1);
5402 QCBORDecode_GetNext(&DCtx, &Item1);
5403 QCBORDecode_GetNext(&DCtx, &Item1);
5404 QCBORDecode_GetNext(&DCtx, &Item1);
5405 QCBORDecode_GetNext(&DCtx, &Item1);
5406 QCBORDecode_GetNext(&DCtx, &Item1);
5407 QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings");
5408 QCBORDecode_GetNext(&DCtx, &Item1);
5409 if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING) {
Laurence Lundblade64b607e2020-05-13 13:05:57 -07005410 return 2004;
Laurence Lundblade937ea812020-05-08 11:38:23 -07005411 }
5412
Laurence Lundblade9b334962020-08-27 10:55:53 -07005413 QCBORDecode_Init(&DCtx, ValidEncodedMap, 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005414 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade2b843b52020-06-16 20:51:03 -07005415 QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings");
5416 QCBORDecode_ExitArray(&DCtx);
5417 QCBORDecode_GetNext(&DCtx, &Item1);
5418 if(Item1.uDataType != QCBOR_TYPE_MAP && Item1.uLabelAlloc != QCBOR_TYPE_TEXT_STRING) {
5419 return 2006;
5420 }
5421 QCBORDecode_ExitMap(&DCtx);
5422 if(QCBORDecode_GetNext(&DCtx, &Item1) != QCBOR_ERR_NO_MORE_ITEMS) {
5423 return 2007;
5424 }
5425
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07005426 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSimpleArray), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005427 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07005428 int64_t nDecodedInt2;
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005429 QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2);
5430 uErr = QCBORDecode_GetAndResetError(&DCtx);
5431 if(uErr != QCBOR_ERR_MAP_NOT_ENTERED){
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07005432 return 2008;
5433 }
5434 UsefulBufC String;
Laurence Lundblade323f8a92020-09-06 19:43:09 -07005435 QCBORDecode_GetTextStringInMapN(&DCtx, 88, &String);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005436 if(uErr != QCBOR_ERR_MAP_NOT_ENTERED){
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07005437 return 2009;
5438 }
Laurence Lundblade937ea812020-05-08 11:38:23 -07005439
Laurence Lundbladee6f15112020-07-23 18:44:16 -07005440
5441 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEmptyMap), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005442 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundbladee6f15112020-07-23 18:44:16 -07005443 // This will fail because the map is empty.
5444 QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2);
5445 uErr = QCBORDecode_GetAndResetError(&DCtx);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07005446 if(uErr != QCBOR_ERR_LABEL_NOT_FOUND){
Laurence Lundbladee6f15112020-07-23 18:44:16 -07005447 return 2010;
5448 }
5449 QCBORDecode_ExitMap(&DCtx);
5450 uErr = QCBORDecode_Finish(&DCtx);
5451 if(uErr != QCBOR_SUCCESS){
5452 return 2011;
5453 }
5454
5455
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08005456#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundbladee6f15112020-07-23 18:44:16 -07005457 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEmptyInDefinteLengthMap), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005458 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundbladee6f15112020-07-23 18:44:16 -07005459 // This will fail because the map is empty.
5460 QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2);
5461 uErr = QCBORDecode_GetAndResetError(&DCtx);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07005462 if(uErr != QCBOR_ERR_LABEL_NOT_FOUND){
Laurence Lundblade085d7952020-07-24 10:26:30 -07005463 return 2012;
Laurence Lundbladee6f15112020-07-23 18:44:16 -07005464 }
5465 QCBORDecode_ExitMap(&DCtx);
5466 uErr = QCBORDecode_Finish(&DCtx);
5467 if(uErr != QCBOR_SUCCESS){
Laurence Lundblade085d7952020-07-24 10:26:30 -07005468 return 2013;
Laurence Lundbladee6f15112020-07-23 18:44:16 -07005469 }
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08005470#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
Laurence Lundbladee6f15112020-07-23 18:44:16 -07005471
Laurence Lundblade5f4e8712020-07-25 11:44:43 -07005472
5473 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spArrayOfEmpty), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005474 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade323f8a92020-09-06 19:43:09 -07005475 QCBORDecode_GetByteString(&DCtx, &String);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005476 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade5f4e8712020-07-25 11:44:43 -07005477 QCBORDecode_ExitMap(&DCtx);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005478 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade5f4e8712020-07-25 11:44:43 -07005479 QCBORDecode_ExitArray(&DCtx);
5480 QCBORDecode_GetInt64(&DCtx, &nDecodedInt2);
5481 QCBORDecode_ExitArray(&DCtx);
5482 uErr = QCBORDecode_Finish(&DCtx);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005483 if(uErr != QCBOR_SUCCESS) {
Laurence Lundblade5f4e8712020-07-25 11:44:43 -07005484 return 2014;
5485 }
5486
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005487 int64_t nInt;
5488 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spRecoverableMapErrors), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005489 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005490 QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt);
Laurence Lundblade88e9db22020-11-02 03:56:33 -08005491 uErr = QCBORDecode_GetError(&DCtx);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005492 if(uErr != QCBOR_ERR_TOO_MANY_TAGS) {
5493 return 2021;
5494 }
Laurence Lundblade88e9db22020-11-02 03:56:33 -08005495 if(QCBORDecode_GetNthTagOfLast(&DCtx, 0) != CBOR_TAG_INVALID64) {
5496 return 2121;
5497 }
5498 (void)QCBORDecode_GetAndResetError(&DCtx);
5499
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005500
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005501 QCBORDecode_GetInt64InMapN(&DCtx, 0x03, &nInt);
5502 uErr = QCBORDecode_GetAndResetError(&DCtx);
5503 if(uErr != QCBOR_ERR_INT_OVERFLOW) {
5504 return 2023;
5505 }
5506
5507 QCBORDecode_GetEpochDateInMapN(&DCtx, 0x04, QCBOR_TAG_REQUIREMENT_TAG, &nInt);
5508 uErr = QCBORDecode_GetAndResetError(&DCtx);
Laurence Lundblade16a207a2021-09-18 17:22:46 -07005509 if(uErr != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_DATE_OVERFLOW)) {
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005510 return 2024;
5511 }
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005512
5513 QCBORDecode_GetInt64InMapN(&DCtx, 0x05, &nInt);
5514 uErr = QCBORDecode_GetAndResetError(&DCtx);
5515 if(uErr != QCBOR_ERR_DUPLICATE_LABEL) {
5516 return 2025;
5517 }
5518
5519 QCBORDecode_GetInt64InMapN(&DCtx, 0x08, &nInt);
5520
5521 QCBORDecode_ExitMap(&DCtx);
5522 uErr = QCBORDecode_Finish(&DCtx);
5523 if(uErr != QCBOR_SUCCESS) {
5524 return 2026;
5525 }
5526
5527 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUnRecoverableMapError1), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005528 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005529 QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt);
5530 uErr = QCBORDecode_GetAndResetError(&DCtx);
5531 if(uErr != QCBOR_ERR_BAD_BREAK) {
5532 return 2030;
5533 }
5534
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08005535#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005536 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUnRecoverableMapError2), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005537 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005538 QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt);
5539 uErr = QCBORDecode_GetAndResetError(&DCtx);
5540 if(uErr != QCBOR_ERR_NO_MORE_ITEMS) {
5541 return 2031;
5542 }
5543
5544 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUnRecoverableMapError3), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005545 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005546 QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt);
5547 uErr = QCBORDecode_GetAndResetError(&DCtx);
5548 if(uErr != QCBOR_ERR_HIT_END) {
5549 return 2032;
5550 }
5551
5552 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUnRecoverableMapError4), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005553 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005554 QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt);
5555 uErr = QCBORDecode_GetAndResetError(&DCtx);
5556 if(uErr != QCBOR_ERR_ARRAY_DECODE_NESTING_TOO_DEEP) {
5557 return 2033;
5558 }
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08005559#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
5560
Laurence Lundblade732e52d2021-02-22 20:11:01 -07005561 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0);
5562 QCBORDecode_VGetNextConsume(&DCtx, &Item1);
5563 if(Item1.uDataType != QCBOR_TYPE_MAP) {
5564 return 2401;
5565 }
5566 if(QCBORDecode_GetError(&DCtx)) {
5567 return 2402;
5568 }
5569
5570 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0);
5571 QCBORDecode_VGetNext(&DCtx, &Item1);
5572 if(Item1.uDataType != QCBOR_TYPE_MAP ||
5573 Item1.val.uCount != 3 ||
5574 Item1.uNextNestLevel != 1) {
5575 return 2403;
5576 }
5577 if(QCBORDecode_GetError(&DCtx)) {
5578 return 2404;
5579 }
5580 QCBORDecode_VGetNextConsume(&DCtx, &Item1);
5581 if(Item1.uDataType != QCBOR_TYPE_INT64 ||
5582 Item1.uNextNestLevel != 1 ||
5583 Item1.val.int64 != 42) {
5584 return 2405;
5585 }
5586 if(QCBORDecode_GetError(&DCtx)) {
5587 return 2406;
5588 }
5589 QCBORDecode_VGetNextConsume(&DCtx, &Item1);
5590 if(Item1.uDataType != QCBOR_TYPE_ARRAY ||
5591 Item1.uNestingLevel != 1 ||
5592 Item1.uNextNestLevel != 1 ||
5593 Item1.val.uCount != 2) {
5594 return 2407;
5595 }
5596 if(QCBORDecode_GetError(&DCtx)) {
5597 return 2408;
5598 }
5599 QCBORDecode_VGetNextConsume(&DCtx, &Item1);
5600 if(Item1.uDataType != QCBOR_TYPE_MAP ||
5601 Item1.uNestingLevel != 1 ||
5602 Item1.uNextNestLevel != 0 ||
5603 Item1.val.uCount != 4) {
5604 return 2409;
5605 }
5606 if(QCBORDecode_GetError(&DCtx)) {
5607 return 2410;
5608 }
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07005609
Laurence Lundbladeddebabe2020-11-22 11:32:17 -08005610 nReturn = DecodeNestedIterate();
5611
Laurence Lundblade63926052021-03-29 16:05:51 -07005612
5613 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(not_well_formed_submod_section), 0);
5614 QCBORDecode_EnterMap(&DCtx, NULL);
5615 QCBORDecode_EnterMapFromMapN(&DCtx, 20);
5616 if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_BAD_INT) {
5617 return 2500;
5618 }
5619
Laurence Lundbladeaf5921e2022-07-15 09:06:30 -07005620 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBadConsumeInput), 0);
5621 QCBORDecode_VGetNextConsume(&DCtx, &Item1);
5622 if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_NO_MORE_ITEMS) {
5623 return 2600;
5624 }
5625
5626 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBadConsumeInput2), 0);
5627 QCBORDecode_VGetNextConsume(&DCtx, &Item1);
5628 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) {
5629 return 2700;
5630 }
5631
5632 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBadConsumeInput3), 0);
5633 QCBORDecode_VGetNextConsume(&DCtx, &Item1);
5634 if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_UNRECOVERABLE_TAG_CONTENT) {
5635 return 2800;
5636 }
5637
5638 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBadConsumeInput4), 0);
5639 QCBORDecode_VGetNextConsume(&DCtx, &Item1);
5640#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
5641 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) {
5642 return 2900;
5643 }
5644#else
5645 if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_INDEF_LEN_ARRAYS_DISABLED) {
5646 return 2901;
5647 }
5648#endif
5649
5650 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBadConsumeInput5), 0);
5651 QCBORDecode_VGetNextConsume(&DCtx, &Item1);
5652 if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_MAP_LABEL_TYPE) {
5653 return 3000;
5654 }
5655
Laurence Lundbladeddebabe2020-11-22 11:32:17 -08005656 return nReturn;
Laurence Lundblade9c905e82020-04-25 11:31:38 -07005657}
5658
5659
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005660struct NumberConversion {
5661 char *szDescription;
5662 UsefulBufC CBOR;
5663 int64_t nConvertedToInt64;
5664 QCBORError uErrorInt64;
5665 uint64_t uConvertToUInt64;
5666 QCBORError uErrorUint64;
5667 double dConvertToDouble;
5668 QCBORError uErrorDouble;
5669};
5670
Laurence Lundblade16a207a2021-09-18 17:22:46 -07005671#ifndef QCBOR_DISABLE_EXP_AND_MANTISSA
5672#define EXP_AND_MANTISSA_ERROR(x) x
5673#else
5674#define EXP_AND_MANTISSA_ERROR(x) QCBOR_ERR_UNEXPECTED_TYPE
5675#endif
5676
5677
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07005678static const struct NumberConversion NumberConversions[] = {
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005679 {
Laurence Lundblade784b54b2020-08-10 01:24:52 -07005680 "too large to fit into int64_t",
5681 {(uint8_t[]){0xc3, 0x48, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 10},
5682 0,
5683 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5684 0,
5685 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
5686 ((double)INT64_MIN) + 1 ,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07005687 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)
Laurence Lundblade784b54b2020-08-10 01:24:52 -07005688 },
5689 {
5690 "largest negative int that fits in int64_t",
5691 {(uint8_t[]){0xc3, 0x48, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, 10},
5692 INT64_MIN,
5693 QCBOR_SUCCESS,
5694 0,
5695 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
5696 (double)INT64_MIN,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07005697 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)
Laurence Lundblade784b54b2020-08-10 01:24:52 -07005698 },
5699 {
Laurence Lundbladeda095972020-06-06 18:35:33 -07005700 "negative bignum -1",
5701 {(uint8_t[]){0xc3, 0x41, 0x00}, 3},
5702 -1,
5703 QCBOR_SUCCESS,
5704 0,
5705 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
5706 -1.0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07005707 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)
Laurence Lundbladeda095972020-06-06 18:35:33 -07005708 },
5709 {
Laurence Lundblade9ab5abb2020-05-20 12:10:45 -07005710 "Decimal Fraction with positive bignum 257 * 10e3",
Laurence Lundblade887add82020-05-17 05:50:34 -07005711 {(uint8_t[]){0xC4, 0x82, 0x1B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03,
5712 0xC2, 0x42, 0x01, 0x01}, 15},
5713 257000,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07005714 EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS),
Laurence Lundblade887add82020-05-17 05:50:34 -07005715 257000,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07005716 EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS),
Laurence Lundblade887add82020-05-17 05:50:34 -07005717 257000.0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07005718 FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS))
Laurence Lundblade887add82020-05-17 05:50:34 -07005719 },
5720 {
Laurence Lundbladeda095972020-06-06 18:35:33 -07005721 "bigfloat with negative bignum -258 * 2e3",
Laurence Lundblade887add82020-05-17 05:50:34 -07005722 {(uint8_t[]){0xC5, 0x82, 0x1B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03,
5723 0xC3, 0x42, 0x01, 0x01}, 15},
Laurence Lundbladeda095972020-06-06 18:35:33 -07005724 -2064,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07005725 EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS),
Laurence Lundblade887add82020-05-17 05:50:34 -07005726 0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07005727 EXP_AND_MANTISSA_ERROR(QCBOR_ERR_NUMBER_SIGN_CONVERSION),
Laurence Lundbladeda095972020-06-06 18:35:33 -07005728 -2064.0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07005729 FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS))
Laurence Lundblade887add82020-05-17 05:50:34 -07005730 },
5731 {
Laurence Lundblade9ab5abb2020-05-20 12:10:45 -07005732 "bigfloat with positive bignum 257 * 2e3",
Laurence Lundblade887add82020-05-17 05:50:34 -07005733 {(uint8_t[]){0xC5, 0x82, 0x1B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03,
5734 0xC2, 0x42, 0x01, 0x01}, 15},
5735 2056,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07005736 EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS),
Laurence Lundblade887add82020-05-17 05:50:34 -07005737 2056,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07005738 EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS),
Laurence Lundblade887add82020-05-17 05:50:34 -07005739 2056.0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07005740 FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS))
Laurence Lundblade887add82020-05-17 05:50:34 -07005741 },
5742 {
Laurence Lundbladeda095972020-06-06 18:35:33 -07005743 "negative bignum 0xc349010000000000000000 -18446744073709551617",
Laurence Lundblade887add82020-05-17 05:50:34 -07005744 {(uint8_t[]){0xc3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 11},
5745 0,
5746 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5747 0,
5748 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
5749 -18446744073709551617.0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07005750 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)
Laurence Lundblade887add82020-05-17 05:50:34 -07005751 },
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08005752#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
Laurence Lundblade887add82020-05-17 05:50:34 -07005753 {
Laurence Lundblade313b2862020-05-16 01:23:06 -07005754 "Positive bignum 0x01020304 indefinite length string",
5755 {(uint8_t[]){0xC2, 0x5f, 0x42, 0x01, 0x02, 0x41, 0x03, 0x41, 0x04, 0xff}, 10},
5756 0x01020304,
5757 QCBOR_SUCCESS,
5758 0x01020304,
5759 QCBOR_SUCCESS,
5760 16909060.0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07005761 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)
Laurence Lundblade313b2862020-05-16 01:23:06 -07005762 },
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08005763#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
Laurence Lundblade313b2862020-05-16 01:23:06 -07005764 {
Laurence Lundblade887add82020-05-17 05:50:34 -07005765 "Decimal Fraction with neg bignum [9223372036854775807, -4759477275222530853137]",
Laurence Lundblade313b2862020-05-16 01:23:06 -07005766 {(uint8_t[]){0xC4, 0x82, 0x1B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
5767 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,}, 23},
5768 0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07005769 EXP_AND_MANTISSA_ERROR(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW),
Laurence Lundblade313b2862020-05-16 01:23:06 -07005770 0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07005771 EXP_AND_MANTISSA_ERROR(QCBOR_ERR_NUMBER_SIGN_CONVERSION),
Laurence Lundblade313b2862020-05-16 01:23:06 -07005772 -INFINITY,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07005773 FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS))
Laurence Lundblade313b2862020-05-16 01:23:06 -07005774 },
5775 {
5776 "big float [9223372036854775806, 9223372036854775806]",
5777 {(uint8_t[]){0xC5, 0x82, 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE,
5778 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE}, 20},
5779 0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07005780 EXP_AND_MANTISSA_ERROR(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW),
Laurence Lundblade313b2862020-05-16 01:23:06 -07005781 0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07005782 EXP_AND_MANTISSA_ERROR(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW),
Laurence Lundblade313b2862020-05-16 01:23:06 -07005783 INFINITY,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07005784 FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS))
Laurence Lundblade313b2862020-05-16 01:23:06 -07005785 },
5786 {
Laurence Lundblade983500d2020-05-14 11:49:34 -07005787 "Big float 3 * 2^^2",
5788 {(uint8_t[]){0xC5, 0x82, 0x02, 0x03}, 4},
5789 12,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07005790 EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS),
Laurence Lundblade983500d2020-05-14 11:49:34 -07005791 12,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07005792 EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS),
Laurence Lundblade983500d2020-05-14 11:49:34 -07005793 12.0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07005794 FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS))
Laurence Lundblade983500d2020-05-14 11:49:34 -07005795 },
Laurence Lundblade983500d2020-05-14 11:49:34 -07005796 {
Laurence Lundblade313b2862020-05-16 01:23:06 -07005797 "Positive integer 18446744073709551615",
Laurence Lundblade983500d2020-05-14 11:49:34 -07005798 {(uint8_t[]){0x1b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, 9},
5799 0,
5800 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5801 18446744073709551615ULL,
5802 QCBOR_SUCCESS,
5803 18446744073709551615.0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07005804 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)
Laurence Lundblade983500d2020-05-14 11:49:34 -07005805 },
Laurence Lundblade983500d2020-05-14 11:49:34 -07005806 {
Laurence Lundblade313b2862020-05-16 01:23:06 -07005807 "Positive bignum 0xffff",
Laurence Lundblade983500d2020-05-14 11:49:34 -07005808 {(uint8_t[]){0xC2, 0x42, 0xff, 0xff}, 4},
5809 65536-1,
5810 QCBOR_SUCCESS,
5811 0xffff,
5812 QCBOR_SUCCESS,
5813 65535.0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07005814 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)
Laurence Lundblade983500d2020-05-14 11:49:34 -07005815 },
5816 {
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005817 "Postive integer 0",
5818 {(uint8_t[]){0x0}, 1},
5819 0LL,
5820 QCBOR_SUCCESS,
5821 0ULL,
5822 QCBOR_SUCCESS,
5823 0.0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07005824 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005825 },
5826 {
Laurence Lundblade313b2862020-05-16 01:23:06 -07005827 "Negative integer -18446744073709551616",
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005828 {(uint8_t[]){0x3b, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }, 9},
5829 -9223372036854775807-1, // INT64_MIN
5830 QCBOR_SUCCESS,
5831 0ULL,
5832 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
5833 -9223372036854775808.0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07005834 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005835 },
5836 {
Laurence Lundblade313b2862020-05-16 01:23:06 -07005837 "Double Floating point value 100.3",
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005838 {(uint8_t[]){0xfb, 0x40, 0x59, 0x13, 0x33, 0x33, 0x33, 0x33, 0x33}, 9},
5839 100L,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07005840 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS),
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005841 100ULL,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07005842 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS),
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005843 100.3,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07005844 FLOAT_ERR_CODE_NO_FLOAT(QCBOR_SUCCESS),
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005845 },
5846 {
5847 "Floating point value NaN 0xfa7fc00000",
5848 {(uint8_t[]){0xfa, 0x7f, 0xc0, 0x00, 0x00}, 5},
5849 0,
Máté Tóth-Pálef5f07a2021-09-17 19:31:37 +02005850 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_FLOAT_EXCEPTION),
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005851 0,
Máté Tóth-Pálef5f07a2021-09-17 19:31:37 +02005852 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_FLOAT_EXCEPTION),
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005853 NAN,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07005854 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS),
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005855 },
5856 {
Laurence Lundblade313b2862020-05-16 01:23:06 -07005857 "half-precision Floating point value -4",
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005858 {(uint8_t[]){0xf9, 0xc4, 0x00}, 3},
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005859 // Normal case with all enabled.
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005860 -4,
Máté Tóth-Pálef5f07a2021-09-17 19:31:37 +02005861 FLOAT_ERR_CODE_NO_HALF_PREC_NO_FLOAT_HW(QCBOR_SUCCESS),
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005862 0,
Máté Tóth-Pálef5f07a2021-09-17 19:31:37 +02005863 FLOAT_ERR_CODE_NO_HALF_PREC_NO_FLOAT_HW(QCBOR_ERR_NUMBER_SIGN_CONVERSION),
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005864 -4.0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07005865 FLOAT_ERR_CODE_NO_HALF_PREC(QCBOR_SUCCESS)
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005866 },
5867 {
5868 "Decimal fraction 3/10",
5869 {(uint8_t[]){0xC4, 0x82, 0x20, 0x03}, 4},
5870 0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07005871 EXP_AND_MANTISSA_ERROR(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW),
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005872 0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07005873 EXP_AND_MANTISSA_ERROR(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW),
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005874 0.30000000000000004,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07005875 FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS))
Laurence Lundbladedfd49fc2020-09-01 14:17:16 -07005876 },
5877 {
Laurence Lundblade16a207a2021-09-18 17:22:46 -07005878 "+inifinity single precision",
Laurence Lundbladedfd49fc2020-09-01 14:17:16 -07005879 {(uint8_t[]){0xfa, 0x7f, 0x80, 0x00, 0x00}, 5},
Laurence Lundbladedfd49fc2020-09-01 14:17:16 -07005880 0,
Máté Tóth-Pálef5f07a2021-09-17 19:31:37 +02005881 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_FLOAT_EXCEPTION),
Laurence Lundbladedfd49fc2020-09-01 14:17:16 -07005882 0,
Máté Tóth-Pálef5f07a2021-09-17 19:31:37 +02005883 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW),
Laurence Lundbladedfd49fc2020-09-01 14:17:16 -07005884 INFINITY,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07005885 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)
Laurence Lundbladedfd49fc2020-09-01 14:17:16 -07005886 },
Laurence Lundblade11fd78b2020-09-01 22:13:27 -07005887
5888 {
5889 "extreme pos bignum",
5890 {(uint8_t[]){0xc2, 0x59, 0x01, 0x90,
5891 // 50 rows of 8 is 400 digits.
5892 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5893 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5894 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5895 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5896 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5897 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5898 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5899 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5900 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5901 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5902 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5903 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5904 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5905 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5906 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5907 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5908 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5909 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5910 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5911 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5912 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5913 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5914 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5915 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5916 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5917 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5918 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5919 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5920 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5921 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5922 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5923 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5924 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5925 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5926 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5927 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5928 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5929 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5930 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5931 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5932 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5933 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5934 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5935 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5936 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5937 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5938 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5939 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5940 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5941 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0},
5942 404},
5943 0,
5944 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5945 0,
5946 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
Laurence Lundblade11fd78b2020-09-01 22:13:27 -07005947 INFINITY,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07005948 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS),
Laurence Lundblade11fd78b2020-09-01 22:13:27 -07005949 },
5950
5951 {
5952 "extreme neg bignum",
5953 {(uint8_t[]){0xc3, 0x59, 0x01, 0x90,
5954 // 50 rows of 8 is 400 digits.
5955 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5956 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5957 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5958 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5959 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5960 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5961 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5962 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5963 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5964 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5965 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5966 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5967 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5968 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5969 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5970 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5971 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5972 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5973 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5974 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5975 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5976 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5977 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5978 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5979 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5980 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5981 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5982 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5983 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5984 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5985 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5986 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5987 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5988 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5989 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5990 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5991 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5992 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5993 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5994 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5995 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5996 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5997 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5998 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5999 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6000 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6001 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6002 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6003 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
6004 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0},
6005 404},
6006 0,
6007 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
6008 0,
6009 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
Laurence Lundblade11fd78b2020-09-01 22:13:27 -07006010 -INFINITY,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006011 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)
Laurence Lundblade11fd78b2020-09-01 22:13:27 -07006012 },
Laurence Lundblade51722fd2020-09-02 13:01:33 -07006013
6014 {
6015 "big float underflow [9223372036854775806, -9223372036854775806]",
6016 {(uint8_t[]){
6017 0xC5, 0x82,
6018 0x3B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE,
6019 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE}, 20},
Laurence Lundblade51722fd2020-09-02 13:01:33 -07006020 0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006021 EXP_AND_MANTISSA_ERROR(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW),
Laurence Lundblade51722fd2020-09-02 13:01:33 -07006022 0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006023 EXP_AND_MANTISSA_ERROR(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW),
Laurence Lundblade51722fd2020-09-02 13:01:33 -07006024 0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006025 FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS))
Laurence Lundblade51722fd2020-09-02 13:01:33 -07006026 },
6027
6028 {
6029 "bigfloat that evaluates to -INFINITY",
6030 {(uint8_t[]){
6031 0xC5, 0x82,
6032 0x1B, 0x70, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03,
6033 0xC3, 0x42, 0x01, 0x01}, 15},
Laurence Lundblade51722fd2020-09-02 13:01:33 -07006034 0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006035 EXP_AND_MANTISSA_ERROR(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW),
Laurence Lundblade51722fd2020-09-02 13:01:33 -07006036 0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006037 EXP_AND_MANTISSA_ERROR(QCBOR_ERR_NUMBER_SIGN_CONVERSION),
Laurence Lundblade51722fd2020-09-02 13:01:33 -07006038 -INFINITY,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006039 FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS))
Laurence Lundblade51722fd2020-09-02 13:01:33 -07006040 },
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006041};
Laurence Lundblade9c905e82020-04-25 11:31:38 -07006042
6043
6044
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08006045
6046static int32_t SetUpDecoder(QCBORDecodeContext *DCtx, UsefulBufC CBOR, UsefulBuf Pool)
6047{
6048 QCBORDecode_Init(DCtx, CBOR, QCBOR_DECODE_MODE_NORMAL);
6049#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
6050 if(QCBORDecode_SetMemPool(DCtx, Pool, 0)) {
6051 return 1;
6052 }
6053#else /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
6054 (void)Pool;
6055#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
6056 return 0;
6057}
6058
6059
Laurence Lundblade313b2862020-05-16 01:23:06 -07006060int32_t IntegerConvertTest()
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006061{
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08006062 const int nNumTests = C_ARRAY_COUNT(NumberConversions,
6063 struct NumberConversion);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006064
Laurence Lundblade9ab5abb2020-05-20 12:10:45 -07006065 for(int nIndex = 0; nIndex < nNumTests; nIndex++) {
6066 const struct NumberConversion *pF = &NumberConversions[nIndex];
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07006067
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006068 // Set up the decoding context including a memory pool so that
6069 // indefinite length items can be checked
6070 QCBORDecodeContext DCtx;
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006071 UsefulBuf_MAKE_STACK_UB(Pool, 100);
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07006072
6073 /* ----- test conversion to int64_t ------ */
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08006074 if(SetUpDecoder(&DCtx, pF->CBOR, Pool)) {
6075 return (int32_t)(3333+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006076 }
6077
6078 int64_t nInt;
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006079 QCBORDecode_GetInt64ConvertAll(&DCtx, 0xffff, &nInt);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07006080 if(QCBORDecode_GetError(&DCtx) != pF->uErrorInt64) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07006081 return (int32_t)(2000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006082 }
6083 if(pF->uErrorInt64 == QCBOR_SUCCESS && pF->nConvertedToInt64 != nInt) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07006084 return (int32_t)(3000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006085 }
6086
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07006087 /* ----- test conversion to uint64_t ------ */
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08006088 if(SetUpDecoder(&DCtx, pF->CBOR, Pool)) {
6089 return (int32_t)(3333+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006090 }
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08006091
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006092 uint64_t uInt;
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006093 QCBORDecode_GetUInt64ConvertAll(&DCtx, 0xffff, &uInt);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07006094 if(QCBORDecode_GetError(&DCtx) != pF->uErrorUint64) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07006095 return (int32_t)(4000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006096 }
6097 if(pF->uErrorUint64 == QCBOR_SUCCESS && pF->uConvertToUInt64 != uInt) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07006098 return (int32_t)(5000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006099 }
6100
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07006101 /* ----- test conversion to double ------ */
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08006102 if(SetUpDecoder(&DCtx, pF->CBOR, Pool)) {
6103 return (int32_t)(3333+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006104 }
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006105
6106#ifndef USEFULBUF_DISABLE_ALL_FLOAT
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006107 double d;
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006108 QCBORDecode_GetDoubleConvertAll(&DCtx, 0xffff, &d);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07006109 if(QCBORDecode_GetError(&DCtx) != pF->uErrorDouble) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07006110 return (int32_t)(6000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006111 }
6112 if(pF->uErrorDouble == QCBOR_SUCCESS) {
6113 if(isnan(pF->dConvertToDouble)) {
Laurence Lundblade983500d2020-05-14 11:49:34 -07006114 // NaN's can't be compared for equality. A NaN is
6115 // never equal to anything including another NaN
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006116 if(!isnan(d)) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07006117 return (int32_t)(7000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006118 }
6119 } else {
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006120 if(pF->dConvertToDouble != d) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07006121 return (int32_t)(8000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006122 }
6123 }
6124 }
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006125#endif /* USEFULBUF_DISABLE_ALL_FLOAT */
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006126 }
6127
6128 return 0;
6129}
6130
Laurence Lundbladea8758502022-05-15 17:57:46 -07006131#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
6132
David Navarro9123e5b2022-03-28 16:04:03 +02006133int32_t CBORTestIssue134()
6134{
6135 QCBORDecodeContext DCtx;
6136 QCBORItem Item;
6137 QCBORError uCBORError;
6138 const uint8_t spTestIssue134[] = { 0x5F, 0x40, 0xFF };
Laurence Lundblade9c905e82020-04-25 11:31:38 -07006139
David Navarro9123e5b2022-03-28 16:04:03 +02006140 QCBORDecode_Init(&DCtx,
6141 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTestIssue134),
6142 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade97c61bf2020-05-02 11:24:06 -07006143
David Navarro9123e5b2022-03-28 16:04:03 +02006144 UsefulBuf_MAKE_STACK_UB(StringBuf, 200);
6145 QCBORDecode_SetMemPool(&DCtx, StringBuf, false);
6146
6147 do {
6148 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
6149 } while (QCBOR_SUCCESS == uCBORError);
6150
6151 uCBORError = QCBORDecode_Finish(&DCtx);
6152
Laurence Lundblade11ea3612022-07-01 13:26:23 -07006153 return (int32_t)uCBORError;
David Navarro9123e5b2022-03-28 16:04:03 +02006154}
Laurence Lundblade97c61bf2020-05-02 11:24:06 -07006155
Laurence Lundbladea8758502022-05-15 17:57:46 -07006156#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
6157
Laurence Lundbladee3553422020-05-02 11:11:17 -07006158int32_t CBORSequenceDecodeTests(void)
6159{
6160 QCBORDecodeContext DCtx;
Laurence Lundblade87495732021-02-26 10:05:55 -07006161 QCBORItem Item;
6162 QCBORError uCBORError;
6163 size_t uConsumed;
Laurence Lundbladee3553422020-05-02 11:11:17 -07006164
6165 // --- Test a sequence with extra bytes ---
Laurence Lundblade9b334962020-08-27 10:55:53 -07006166
Laurence Lundbladee3553422020-05-02 11:11:17 -07006167 // The input for the date test happens to be a sequence so it
6168 // is reused. It is a sequence because it doesn't start as
6169 // an array or map.
6170 QCBORDecode_Init(&DCtx,
6171 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spDateTestInput),
6172 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade9b334962020-08-27 10:55:53 -07006173
Laurence Lundbladee3553422020-05-02 11:11:17 -07006174 // Get the first item
6175 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
6176 if(uCBORError != QCBOR_SUCCESS) {
6177 return 1;
6178 }
6179 if(Item.uDataType != QCBOR_TYPE_DATE_STRING) {
6180 return 2;
6181 }
Laurence Lundbladec7114722020-08-13 05:11:40 -07006182
Laurence Lundblade87495732021-02-26 10:05:55 -07006183 uCBORError = QCBORDecode_PartialFinish(&DCtx, &uConsumed);
6184 if(uCBORError != QCBOR_ERR_EXTRA_BYTES ||
6185 uConsumed != 12) {
6186 return 102;
6187 }
6188
Laurence Lundbladee3553422020-05-02 11:11:17 -07006189 // Get a second item
6190 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundbladec7114722020-08-13 05:11:40 -07006191 if(uCBORError != QCBOR_ERR_BAD_OPT_TAG) {
6192 return 66;
6193 }
6194
Laurence Lundblade87495732021-02-26 10:05:55 -07006195 uCBORError = QCBORDecode_PartialFinish(&DCtx, &uConsumed);
6196 if(uCBORError != QCBOR_ERR_EXTRA_BYTES ||
6197 uConsumed != 14) {
6198 return 102;
6199 }
6200
Laurence Lundbladec7114722020-08-13 05:11:40 -07006201 // Get a third item
6202 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundbladee3553422020-05-02 11:11:17 -07006203 if(uCBORError != QCBOR_SUCCESS) {
6204 return 2;
6205 }
6206 if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH) {
6207 return 3;
6208 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006209
Laurence Lundbladee3553422020-05-02 11:11:17 -07006210 // A sequence can have stuff at the end that may
6211 // or may not be valid CBOR. The protocol decoder knows
6212 // when to stop by definition of the protocol, not
6213 // when the top-level map or array is ended.
6214 // Finish still has to be called to know that
6215 // maps and arrays (if there were any) were closed
6216 // off correctly. When called like this it
6217 // must return the error QCBOR_ERR_EXTRA_BYTES.
6218 uCBORError = QCBORDecode_Finish(&DCtx);
6219 if(uCBORError != QCBOR_ERR_EXTRA_BYTES) {
6220 return 4;
6221 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006222
6223
Laurence Lundbladee3553422020-05-02 11:11:17 -07006224 // --- Test an empty input ----
6225 uint8_t empty[1];
6226 UsefulBufC Empty = {empty, 0};
6227 QCBORDecode_Init(&DCtx,
6228 Empty,
6229 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade9b334962020-08-27 10:55:53 -07006230
Laurence Lundbladee3553422020-05-02 11:11:17 -07006231 uCBORError = QCBORDecode_Finish(&DCtx);
6232 if(uCBORError != QCBOR_SUCCESS) {
6233 return 5;
6234 }
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07006235
6236
Laurence Lundbladee3553422020-05-02 11:11:17 -07006237 // --- Sequence with unclosed indefinite length array ---
6238 static const uint8_t xx[] = {0x01, 0x9f, 0x02};
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07006239
Laurence Lundbladee3553422020-05-02 11:11:17 -07006240 QCBORDecode_Init(&DCtx,
6241 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(xx),
6242 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07006243
Laurence Lundbladee3553422020-05-02 11:11:17 -07006244 // Get the first item
6245 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
6246 if(uCBORError != QCBOR_SUCCESS) {
6247 return 7;
6248 }
6249 if(Item.uDataType != QCBOR_TYPE_INT64) {
6250 return 8;
6251 }
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07006252
Laurence Lundbladee3553422020-05-02 11:11:17 -07006253 // Get a second item
6254 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08006255#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundbladee3553422020-05-02 11:11:17 -07006256 if(uCBORError != QCBOR_SUCCESS) {
6257 return 9;
6258 }
6259 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
6260 return 10;
6261 }
6262
6263 // Try to finish before consuming all bytes to confirm
6264 // that the still-open error is returned.
6265 uCBORError = QCBORDecode_Finish(&DCtx);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006266 if(uCBORError != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
Laurence Lundbladee3553422020-05-02 11:11:17 -07006267 return 11;
6268 }
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08006269#else /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
6270 if(uCBORError != QCBOR_ERR_INDEF_LEN_ARRAYS_DISABLED) {
6271 return 20;
6272 }
6273#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
Laurence Lundbladee3553422020-05-02 11:11:17 -07006274
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07006275
Laurence Lundbladee3553422020-05-02 11:11:17 -07006276 // --- Sequence with a closed indefinite length array ---
6277 static const uint8_t yy[] = {0x01, 0x9f, 0xff};
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07006278
Laurence Lundbladee3553422020-05-02 11:11:17 -07006279 QCBORDecode_Init(&DCtx,
6280 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(yy),
6281 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07006282
Laurence Lundbladee3553422020-05-02 11:11:17 -07006283 // Get the first item
6284 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
6285 if(uCBORError != QCBOR_SUCCESS) {
6286 return 12;
6287 }
6288 if(Item.uDataType != QCBOR_TYPE_INT64) {
6289 return 13;
6290 }
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07006291
Laurence Lundbladee3553422020-05-02 11:11:17 -07006292 // Get a second item
6293 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08006294#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
6295
Laurence Lundbladee3553422020-05-02 11:11:17 -07006296 if(uCBORError != QCBOR_SUCCESS) {
6297 return 14;
6298 }
6299 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
6300 return 15;
6301 }
6302
6303 // Try to finish before consuming all bytes to confirm
6304 // that the still-open error is returned.
6305 uCBORError = QCBORDecode_Finish(&DCtx);
6306 if(uCBORError != QCBOR_SUCCESS) {
6307 return 16;
6308 }
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08006309#else /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
6310 if(uCBORError != QCBOR_ERR_INDEF_LEN_ARRAYS_DISABLED) {
6311 return 20;
6312 }
6313#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
Laurence Lundbladee3553422020-05-02 11:11:17 -07006314
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07006315
Laurence Lundbladee3553422020-05-02 11:11:17 -07006316 return 0;
6317}
6318
Laurence Lundbladee15326f2020-06-15 15:50:23 -07006319
Laurence Lundblade70ecead2020-06-15 19:40:06 -07006320
Laurence Lundbladee15326f2020-06-15 15:50:23 -07006321int32_t IntToTests()
6322{
6323 int nErrCode;
6324 int32_t n32;
6325 int16_t n16;
6326 int8_t n8;
6327 uint32_t u32;
6328 uint16_t u16;
6329 uint8_t u8;
6330 uint64_t u64;
6331
6332 nErrCode = QCBOR_Int64ToInt32(1, &n32);
6333 if(nErrCode == -1 || n32 != 1) {
6334 return 1;
6335 }
6336
6337 nErrCode = QCBOR_Int64ToInt32((int64_t)INT32_MAX, &n32);
6338 if(nErrCode == -1 || n32 != INT32_MAX) {
6339 return 2;
6340 }
6341
6342 nErrCode = QCBOR_Int64ToInt32((int64_t)INT32_MIN, &n32);
6343 if(nErrCode == -1 || n32 != INT32_MIN) {
6344 return 3;
6345 }
6346
6347 nErrCode = QCBOR_Int64ToInt32(((int64_t)INT32_MAX)+1, &n32);
6348 if(nErrCode != -1) {
6349 return 4;
6350 }
6351
6352 nErrCode = QCBOR_Int64ToInt32(((int64_t)INT32_MIN)-1, &n32);
6353 if(nErrCode != -1) {
6354 return 5;
6355 }
6356
6357
6358 nErrCode = QCBOR_Int64ToInt16((int64_t)INT16_MAX, &n16);
6359 if(nErrCode == -1 || n16 != INT16_MAX) {
6360 return 6;
6361 }
6362
6363 nErrCode = QCBOR_Int64ToInt16((int64_t)INT16_MIN, &n16);
6364 if(nErrCode == -1 || n16 != INT16_MIN) {
6365 return 7;
6366 }
6367
6368 nErrCode = QCBOR_Int64ToInt16(1, &n16);
6369 if(nErrCode == -1 || n16 != 1) {
6370 return 8;
6371 }
6372
6373 nErrCode = QCBOR_Int64ToInt16(((int64_t)INT16_MAX)+1, &n16);
6374 if(nErrCode != -1) {
6375 return 9;
6376 }
6377
6378 nErrCode = QCBOR_Int64ToInt16(((int64_t)INT16_MIN)-1, &n16);
6379 if(nErrCode != -1) {
6380 return 10;
6381 }
6382
6383
6384 nErrCode = QCBOR_Int64ToInt8(1, &n8);
6385 if(nErrCode == -1 || n8 != 1) {
6386 return 11;
6387 }
6388
6389 nErrCode = QCBOR_Int64ToInt8((int64_t)INT8_MAX, &n8);
6390 if(nErrCode == -1 || n8 != INT8_MAX) {
6391 return 12;
6392 }
6393
6394 nErrCode = QCBOR_Int64ToInt8((int64_t)INT8_MIN, &n8);
6395 if(nErrCode == -1 || n8 != INT8_MIN) {
6396 return 13;
6397 }
6398
6399 nErrCode = QCBOR_Int64ToInt8(((int64_t)INT8_MAX)+1, &n8);
6400 if(nErrCode != -1) {
6401 return 14;
6402 }
6403
6404 nErrCode = QCBOR_Int64ToInt8(((int64_t)INT8_MIN)-1, &n8);
6405 if(nErrCode != -1) {
6406 return 15;
6407 }
6408
6409
6410 nErrCode = QCBOR_Int64ToUInt32(1, &u32);
6411 if(nErrCode == -1 || u32 != 1) {
6412 return 16;
6413 }
6414
6415 nErrCode = QCBOR_Int64ToUInt32((int64_t)UINT32_MAX, &u32);
6416 if(nErrCode == -1 || u32 != UINT32_MAX) {
6417 return 17;
6418 }
6419
6420 nErrCode = QCBOR_Int64ToUInt32((int64_t)0, &u32);
6421 if(nErrCode == -1 || u32 != 0) {
6422 return 18;
6423 }
6424
6425 nErrCode = QCBOR_Int64ToUInt32(((int64_t)UINT32_MAX)+1, &u32);
6426 if(nErrCode != -1) {
6427 return 19;
6428 }
6429
6430 nErrCode = QCBOR_Int64ToUInt32((int64_t)-1, &u32);
6431 if(nErrCode != -1) {
6432 return 20;
6433 }
6434
6435
6436 nErrCode = QCBOR_Int64UToInt16((int64_t)UINT16_MAX, &u16);
6437 if(nErrCode == -1 || u16 != UINT16_MAX) {
6438 return 21;
6439 }
6440
6441 nErrCode = QCBOR_Int64UToInt16((int64_t)0, &u16);
6442 if(nErrCode == -1 || u16 != 0) {
6443 return 22;
6444 }
6445
6446 nErrCode = QCBOR_Int64UToInt16(1, &u16);
6447 if(nErrCode == -1 || u16 != 1) {
6448 return 23;
6449 }
6450
6451 nErrCode = QCBOR_Int64UToInt16(((int64_t)UINT16_MAX)+1, &u16);
6452 if(nErrCode != -1) {
6453 return 24;
6454 }
6455
6456 nErrCode = QCBOR_Int64UToInt16((int64_t)-1, &u16);
6457 if(nErrCode != -1) {
6458 return 25;
6459 }
6460
6461
6462 nErrCode = QCBOR_Int64ToUInt8((int64_t)UINT8_MAX, &u8);
6463 if(nErrCode == -1 || u8 != UINT8_MAX) {
6464 return 26;
6465 }
6466
6467 nErrCode = QCBOR_Int64ToUInt8((int64_t)0, &u8);
6468 if(nErrCode == -1 || u8 != 0) {
6469 return 27;
6470 }
6471
6472 nErrCode = QCBOR_Int64ToUInt8(1, &u8);
6473 if(nErrCode == -1 || u8 != 1) {
6474 return 28;
6475 }
6476
6477 nErrCode = QCBOR_Int64ToUInt8(((int64_t)UINT16_MAX)+1, &u8);
6478 if(nErrCode != -1) {
6479 return 29;
6480 }
6481
6482 nErrCode = QCBOR_Int64ToUInt8((int64_t)-1, &u8);
6483 if(nErrCode != -1) {
6484 return 30;
6485 }
6486
6487
6488 nErrCode = QCBOR_Int64ToUInt64(1, &u64);
6489 if(nErrCode == -1 || u64 != 1) {
6490 return 31;
6491 }
6492
6493 nErrCode = QCBOR_Int64ToUInt64(INT64_MAX, &u64);
6494 if(nErrCode == -1 || u64 != INT64_MAX) {
6495 return 32;
6496 }
6497
6498 nErrCode = QCBOR_Int64ToUInt64((int64_t)0, &u64);
6499 if(nErrCode == -1 || u64 != 0) {
6500 return 33;
6501 }
6502
6503 nErrCode = QCBOR_Int64ToUInt64((int64_t)-1, &u64);
6504 if(nErrCode != -1) {
6505 return 34;
6506 }
6507
6508 return 0;
6509}
6510
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006511
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006512
6513
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006514/*
6515A sequence with
6516 A wrapping bstr
6517 containing a map
6518 1
6519 2
6520 A wrapping bstr
6521 containing an array
6522 3
6523 wrapping bstr
6524 4
6525 5
6526 6
6527 array
6528 7
6529 8
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006530 */
6531
Laurence Lundblade55013642020-09-23 05:39:22 -07006532static UsefulBufC EncodeBstrWrapTestData(UsefulBuf OutputBuffer)
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006533{
Laurence Lundblade55013642020-09-23 05:39:22 -07006534 UsefulBufC Encoded;
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006535 QCBOREncodeContext EC;
Laurence Lundblade55013642020-09-23 05:39:22 -07006536 QCBORError uErr;
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006537
Laurence Lundblade55013642020-09-23 05:39:22 -07006538 QCBOREncode_Init(&EC, OutputBuffer);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006539
6540 QCBOREncode_BstrWrap(&EC);
6541 QCBOREncode_OpenMap(&EC);
6542 QCBOREncode_AddInt64ToMapN(&EC, 100, 1);
6543 QCBOREncode_AddInt64ToMapN(&EC, 200, 2);
6544 QCBOREncode_CloseMap(&EC);
6545 QCBOREncode_BstrWrap(&EC);
6546 QCBOREncode_OpenArray(&EC);
6547 QCBOREncode_AddInt64(&EC, 3);
6548 QCBOREncode_BstrWrap(&EC);
6549 QCBOREncode_AddInt64(&EC, 4);
6550 QCBOREncode_CloseBstrWrap(&EC, NULL);
6551 QCBOREncode_AddInt64(&EC, 5);
6552 QCBOREncode_CloseArray(&EC);
6553 QCBOREncode_CloseBstrWrap(&EC, NULL);
6554 QCBOREncode_AddInt64(&EC, 6);
6555 QCBOREncode_CloseBstrWrap(&EC, NULL);
6556 QCBOREncode_OpenArray(&EC);
6557 QCBOREncode_AddInt64(&EC, 7);
6558 QCBOREncode_AddInt64(&EC, 8);
6559 QCBOREncode_CloseArray(&EC);
6560
6561 uErr = QCBOREncode_Finish(&EC, &Encoded);
Laurence Lundblade40a04322020-06-27 22:52:52 -07006562 if(uErr) {
6563 Encoded = NULLUsefulBufC;
6564 }
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006565
6566 return Encoded;
6567}
6568
Laurence Lundbladecc7da412020-12-27 00:09:07 -08006569/* h'FF' */
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08006570static const uint8_t spBreakInByteString[] = {
6571 0x41, 0xff
6572};
6573
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006574
6575int32_t EnterBstrTest()
6576{
Laurence Lundblade8510f8c2020-12-01 11:31:16 -08006577 UsefulBuf_MAKE_STACK_UB(OutputBuffer, 100);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006578
6579 QCBORDecodeContext DC;
6580
Laurence Lundblade55013642020-09-23 05:39:22 -07006581 QCBORDecode_Init(&DC, EncodeBstrWrapTestData(OutputBuffer), 0);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006582
Laurence Lundblade55013642020-09-23 05:39:22 -07006583 int64_t n1, n2, n3, n4, n5, n6, n7, n8;
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006584
6585
Laurence Lundblade9b334962020-08-27 10:55:53 -07006586 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006587 QCBORDecode_EnterMap(&DC, NULL);
Laurence Lundblade55013642020-09-23 05:39:22 -07006588 QCBORDecode_GetInt64InMapN(&DC, 100, &n1);
6589 QCBORDecode_GetInt64InMapN(&DC, 200, &n2);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006590 QCBORDecode_ExitMap(&DC);
Laurence Lundblade9b334962020-08-27 10:55:53 -07006591 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006592 QCBORDecode_EnterArray(&DC, NULL);
Laurence Lundblade55013642020-09-23 05:39:22 -07006593 QCBORDecode_GetInt64(&DC, &n3);
Laurence Lundblade9b334962020-08-27 10:55:53 -07006594 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
Laurence Lundblade55013642020-09-23 05:39:22 -07006595 QCBORDecode_GetInt64(&DC, &n4);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006596 QCBORDecode_ExitBstrWrapped(&DC);
Laurence Lundblade55013642020-09-23 05:39:22 -07006597 QCBORDecode_GetInt64(&DC, &n5);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006598 QCBORDecode_ExitArray(&DC);
6599 QCBORDecode_ExitBstrWrapped(&DC);
Laurence Lundblade55013642020-09-23 05:39:22 -07006600 QCBORDecode_GetInt64(&DC, &n6);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006601 QCBORDecode_ExitBstrWrapped(&DC);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006602 QCBORDecode_EnterArray(&DC, NULL);
Laurence Lundblade55013642020-09-23 05:39:22 -07006603 QCBORDecode_GetInt64(&DC, &n7);
6604 QCBORDecode_GetInt64(&DC, &n8);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006605 QCBORDecode_ExitArray(&DC);
6606
6607 QCBORError uErr = QCBORDecode_Finish(&DC);
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08006608 if(uErr) {
6609 return (int32_t)uErr;
6610 }
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006611
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08006612
6613 /* Enter and exit byte string wrapped CBOR that is bad. It has just a break.
6614 * Successful because no items are fetched from byte string.
6615 */
6616 QCBORDecode_Init(&DC,
6617 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBreakInByteString),
6618 0);
6619 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
6620 uErr = QCBORDecode_GetError(&DC);
6621 if(uErr) {
6622 return 100 + (int32_t)uErr;
6623 }
6624
6625 QCBORDecode_ExitBstrWrapped(&DC);
6626 uErr = QCBORDecode_GetError(&DC);
6627 if(uErr) {
6628 return 200 + (int32_t)uErr;
6629 }
6630
6631 /* Try to get item that is a break out of a byte string wrapped CBOR.
6632 * It fails because there should be no break.
6633 */
6634 QCBORDecode_Init(&DC,
6635 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBreakInByteString),
6636 0);
6637 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
6638 QCBORItem Item;
6639 uErr = QCBORDecode_GetNext(&DC, &Item);
6640 if(uErr != QCBOR_ERR_BAD_BREAK) {
6641 return 300 + (int32_t)uErr;
6642 }
6643
6644 return 0;
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006645}
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006646
6647
6648
6649
6650static const uint8_t spTaggedTypes[] = {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006651 0xb2,
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006652
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006653 // Date string
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006654 0x00,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006655 0xc0, 0x74, 0x32, 0x30, 0x30, 0x33, 0x2D, 0x31, 0x32, 0x2D,
6656 0x31, 0x33, 0x54, 0x31, 0x38, 0x3A, 0x33, 0x30, 0x3A, 0x30,
6657 0x32, 0x5A,
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006658
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006659 0x01,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006660 0x74, 0x32, 0x30, 0x30, 0x33, 0x2D, 0x31, 0x32, 0x2D, 0x31,
6661 0x33, 0x54, 0x31, 0x38, 0x3A, 0x33, 0x30, 0x3A, 0x30, 0x32,
6662 0x5A,
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006663
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006664 // Bignum
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006665 10,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006666 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
6667 0x09, 0x10,
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006668
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006669 11,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006670 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
6671 0x09, 0x10,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006672
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006673 // URL
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006674 20,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006675 0xd8, 0x20, 0x6f, 0x68, 0x74, 0x74, 0x70, 0x3A, 0x2F, 0x2F,
6676 0x63, 0x62, 0x6F, 0x72, 0x2E, 0x6D, 0x65, 0x2F,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006677
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006678 21,
6679 0x6f, 0x68, 0x74, 0x74, 0x70, 0x3A, 0x2F, 0x2F, 0x63, 0x62,
6680 0x6F, 0x72, 0x2E, 0x6D, 0x65, 0x2F,
6681
6682 // B64
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006683 0x18, 0x1e,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006684 0xd8, 0x22, 0x6c, 0x63, 0x47, 0x78, 0x6C, 0x59, 0x58, 0x4E,
6685 0x31, 0x63, 0x6D, 0x55, 0x75,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006686
6687 0x18, 0x1f,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006688 0x6c, 0x63, 0x47, 0x78, 0x6C, 0x59, 0x58, 0x4E, 0x31, 0x63,
6689 0x6D, 0x55, 0x75,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006690
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006691 // B64URL
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006692 0x18, 0x28,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006693 0xd8, 0x21, 0x6c, 0x63, 0x47, 0x78, 0x6C, 0x59, 0x58, 0x4E,
6694 0x31, 0x63, 0x6D, 0x55, 0x75,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006695
6696 0x18, 0x29,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006697 0x6c, 0x63, 0x47, 0x78, 0x6C, 0x59, 0x58, 0x4E, 0x31, 0x63,
6698 0x6D, 0x55, 0x75,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006699
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006700 // Regex
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006701 0x18, 0x32,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006702 0xd8, 0x23, 0x68, 0x31, 0x30, 0x30, 0x5C, 0x73, 0x2A, 0x6D,
6703 0x6B,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006704
6705 0x18, 0x33,
6706 0x68, 0x31, 0x30, 0x30, 0x5C, 0x73, 0x2A, 0x6D, 0x6B,
6707
6708 // MIME
6709 0x18, 0x3c,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006710 0xd8, 0x24, 0x72, 0x4D, 0x49, 0x4D, 0x45, 0x2D, 0x56, 0x65,
6711 0x72, 0x73, 0x69, 0x6F, 0x6E, 0x3A, 0x20, 0x31, 0x2E, 0x30,
6712 0x0A,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006713
6714 0x18, 0x3d,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006715 0x72, 0x4D, 0x49, 0x4D, 0x45, 0x2D, 0x56, 0x65, 0x72, 0x73,
6716 0x69, 0x6F, 0x6E, 0x3A, 0x20, 0x31, 0x2E, 0x30, 0x0A,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006717
6718 0x18, 0x3e,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006719 0xd9, 0x01, 0x01, 0x52, 0x4D, 0x49, 0x4D, 0x45, 0x2D, 0x56,
6720 0x65, 0x72, 0x73, 0x69, 0x6F, 0x6E, 0x3A, 0x20, 0x31, 0x2E,
6721 0x30, 0x0A,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006722
6723 0x18, 0x3f,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006724 0x52, 0x4D, 0x49, 0x4D, 0x45, 0x2D, 0x56, 0x65, 0x72, 0x73,
6725 0x69, 0x6F, 0x6E, 0x3A, 0x20, 0x31, 0x2E, 0x30, 0x0A,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006726
6727 // UUID
6728 0x18, 0x46,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006729 0xd8, 0x25, 0x50, 0x53, 0x4D, 0x41, 0x52, 0x54, 0x43, 0x53,
6730 0x4C, 0x54, 0x54, 0x43, 0x46, 0x49, 0x43, 0x41, 0x32,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006731
6732 0x18, 0x47,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006733 0x50, 0x53, 0x4D, 0x41, 0x52, 0x54, 0x43, 0x53, 0x4C, 0x54,
6734 0x54, 0x43, 0x46, 0x49, 0x43, 0x41, 0x32
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006735};
6736
6737int32_t DecodeTaggedTypeTests()
6738{
6739 QCBORDecodeContext DC;
6740 QCBORError uErr;
6741
6742 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTaggedTypes), 0);
6743
6744 UsefulBufC String;
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006745 bool bNeg;
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006746
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006747 QCBORDecode_EnterMap(&DC, NULL);
Laurence Lundblade9b334962020-08-27 10:55:53 -07006748 QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006749 QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &String);
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006750 if(QCBORDecode_GetError(&DC) != QCBOR_SUCCESS) {
6751 return 1;
6752 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006753 QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006754 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_UNEXPECTED_TYPE) {
6755 return 2;
6756 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006757 QCBORDecode_GetDateStringInMapN(&DC, 1, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006758 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_UNEXPECTED_TYPE) {
6759 return 3;
6760 }
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006761 QCBORDecode_GetDateStringInMapN(&DC, 1, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &String);
Laurence Lundblade9b334962020-08-27 10:55:53 -07006762 QCBORDecode_GetDateStringInMapN(&DC, 1, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006763 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6764 return 4;
6765 }
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006766 QCBORDecode_GetDateStringInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006767 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006768 return 5;
6769 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006770
Laurence Lundblade9b334962020-08-27 10:55:53 -07006771 QCBORDecode_GetBignumInMapN(&DC, 10, QCBOR_TAG_REQUIREMENT_TAG, &String, &bNeg);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006772 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
6773 bNeg != false) {
6774 return 10;
6775 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006776 QCBORDecode_GetBignumInMapN(&DC, 11, QCBOR_TAG_REQUIREMENT_TAG, &String, &bNeg);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006777 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
6778 bNeg != true) {
6779 return 11;
6780 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006781 QCBORDecode_GetBignumInMapN(&DC, 11, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bNeg);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006782 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_UNEXPECTED_TYPE) {
6783 return 12;
6784 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006785 QCBORDecode_GetBignumInMapN(&DC, 14, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bNeg);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006786 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006787 return 13;
6788 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006789 QCBORDecode_GetBignumInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bNeg);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006790 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006791 return 14;
6792 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006793
Laurence Lundblade9b334962020-08-27 10:55:53 -07006794 QCBORDecode_GetURIInMapN(&DC, 20, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006795 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6796 return 20;
6797 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006798 QCBORDecode_GetURIInMapN(&DC, 21, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006799 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6800 return 21;
6801 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006802 QCBORDecode_GetURIInMapN(&DC, 22, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006803 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006804 return 22;
6805 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006806 QCBORDecode_GetURIInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006807 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006808 return 23;
6809 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006810
Laurence Lundblade24bd7e12021-01-09 00:05:11 -08006811#ifndef QCBOR_DISABLE_UNCOMMON_TAGS
Laurence Lundblade9b334962020-08-27 10:55:53 -07006812 QCBORDecode_GetB64InMapN(&DC, 30, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006813 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6814 return 30;
6815 }
Laurence Lundblade24bd7e12021-01-09 00:05:11 -08006816#endif
Laurence Lundblade9b334962020-08-27 10:55:53 -07006817 QCBORDecode_GetB64InMapN(&DC, 31, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006818 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6819 return 31;
6820 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006821 QCBORDecode_GetB64InMapN(&DC, 32, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006822 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006823 return 32;
6824 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006825 QCBORDecode_GetB64InMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006826 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006827 return 33;
6828 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006829
Laurence Lundblade24bd7e12021-01-09 00:05:11 -08006830#ifndef QCBOR_DISABLE_UNCOMMON_TAGS
Laurence Lundblade9b334962020-08-27 10:55:53 -07006831 QCBORDecode_GetB64URLInMapN(&DC, 40, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006832 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6833 return 40;
6834 }
Laurence Lundblade24bd7e12021-01-09 00:05:11 -08006835#endif
Laurence Lundblade9b334962020-08-27 10:55:53 -07006836 QCBORDecode_GetB64URLInMapN(&DC, 41, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006837 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6838 return 41;
6839 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006840 QCBORDecode_GetB64URLInMapN(&DC, 42, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006841 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006842 return 42;
6843 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006844 QCBORDecode_GetB64URLInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006845 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006846 return 43;
6847 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006848
Laurence Lundblade24bd7e12021-01-09 00:05:11 -08006849#ifndef QCBOR_DISABLE_UNCOMMON_TAGS
Laurence Lundblade9b334962020-08-27 10:55:53 -07006850 QCBORDecode_GetRegexInMapN(&DC, 50, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006851 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6852 return 50;
6853 }
Laurence Lundblade24bd7e12021-01-09 00:05:11 -08006854#endif
Laurence Lundblade9b334962020-08-27 10:55:53 -07006855 QCBORDecode_GetRegexInMapN(&DC, 51, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006856 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6857 return 51;
6858 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006859 QCBORDecode_GetRegexInMapN(&DC, 52, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006860 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006861 return 52;
6862 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006863 QCBORDecode_GetRegexInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006864 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006865 return 53;
6866 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006867
Laurence Lundblade24bd7e12021-01-09 00:05:11 -08006868#ifndef QCBOR_DISABLE_UNCOMMON_TAGS
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006869 // MIME
6870 bool bIsNot7Bit;
Laurence Lundblade9b334962020-08-27 10:55:53 -07006871 QCBORDecode_GetMIMEMessageInMapN(&DC, 60, QCBOR_TAG_REQUIREMENT_TAG, &String, &bIsNot7Bit);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006872 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
6873 bIsNot7Bit == true) {
6874 return 60;
6875 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006876 QCBORDecode_GetMIMEMessageInMapN(&DC, 61, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bIsNot7Bit);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006877 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
6878 bIsNot7Bit == true) {
6879 return 61;
6880 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006881 QCBORDecode_GetMIMEMessageInMapN(&DC, 62, QCBOR_TAG_REQUIREMENT_TAG, &String, &bIsNot7Bit);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006882 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
6883 bIsNot7Bit == false) {
6884 return 62;
6885 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006886 QCBORDecode_GetMIMEMessageInMapN(&DC, 63, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bIsNot7Bit);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006887 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
6888 bIsNot7Bit == false) {
6889 return 63;
6890 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006891 QCBORDecode_GetMIMEMessageInMapN(&DC, 64, QCBOR_TAG_REQUIREMENT_TAG, &String, &bNeg);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006892 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006893 return 64;
6894 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006895 QCBORDecode_GetMIMEMessageInMapSZ(&DC, "zzz", QCBOR_TAG_REQUIREMENT_TAG, &String, &bNeg);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006896 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006897 return 65;
6898 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006899
Laurence Lundblade24bd7e12021-01-09 00:05:11 -08006900
Laurence Lundblade9b334962020-08-27 10:55:53 -07006901 QCBORDecode_GetBinaryUUIDInMapN(&DC, 70, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006902 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6903 return 70;
6904 }
Laurence Lundblade24bd7e12021-01-09 00:05:11 -08006905#endif /* #ifndef QCBOR_DISABLE_UNCOMMON_TAGS */
6906
Laurence Lundblade9b334962020-08-27 10:55:53 -07006907 QCBORDecode_GetBinaryUUIDInMapN(&DC, 71, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006908 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6909 return 71;
6910 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006911 QCBORDecode_GetBinaryUUIDInMapN(&DC, 72, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006912 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006913 return 72;
6914 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006915 QCBORDecode_GetBinaryUUIDInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006916 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006917 return 73;
6918 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006919
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006920 // Improvement: add some more error test cases
6921
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006922 QCBORDecode_ExitMap(&DC);
6923
6924 uErr = QCBORDecode_Finish(&DC);
6925 if(uErr != QCBOR_SUCCESS) {
6926 return 100;
6927 }
6928
6929 return 0;
6930}
Laurence Lundbladea4308a82020-10-03 18:08:57 -07006931
6932
6933
6934
6935/*
Laurence Lundbladecc7da412020-12-27 00:09:07 -08006936 [
6937 "aaaaaaaaaa",
6938 {}
6939 ]
Laurence Lundbladea4308a82020-10-03 18:08:57 -07006940 */
6941static const uint8_t spTooLarge1[] = {
6942 0x9f,
6943 0x6a, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61,
6944 0xa0,
6945 0xff
6946};
6947
6948/*
Laurence Lundbladecc7da412020-12-27 00:09:07 -08006949 [
6950 {
6951 0: "aaaaaaaaaa"
6952 }
6953 ]
Laurence Lundbladea4308a82020-10-03 18:08:57 -07006954 */
6955static const uint8_t spTooLarge2[] = {
6956 0x9f,
6957 0xa1,
6958 0x00,
6959 0x6a, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61,
6960 0xff
6961};
6962
6963/*
Laurence Lundbladecc7da412020-12-27 00:09:07 -08006964 h'A1006A61616161616161616161'
Laurence Lundbladea4308a82020-10-03 18:08:57 -07006965
Laurence Lundbladecc7da412020-12-27 00:09:07 -08006966 {
6967 0: "aaaaaaaaaa"
6968 }
Laurence Lundbladea4308a82020-10-03 18:08:57 -07006969 */
6970static const uint8_t spTooLarge3[] = {
6971 0x4d,
6972 0xa1,
6973 0x00,
6974 0x6a, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61,
6975};
6976
6977int32_t TooLargeInputTest(void)
6978{
6979 QCBORDecodeContext DC;
6980 QCBORError uErr;
6981 UsefulBufC String;
6982
6983 // These tests require a build with QCBOR_MAX_DECODE_INPUT_SIZE set
6984 // to 10 There's not really any way to test this error
6985 // condition. The error condition is not complex, so setting
6986 // QCBOR_MAX_DECODE_INPUT_SIZE gives an OK test.
6987
6988 // The input CBOR is only too large because the
6989 // QCBOR_MAX_DECODE_INPUT_SIZE is 10.
6990 //
6991 // This test is disabled for the normal test runs because of the
6992 // special build requirement.
6993
6994
6995 // Tests the start of a map being too large
6996 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTooLarge1), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006997 QCBORDecode_EnterArray(&DC, NULL);
Laurence Lundbladea4308a82020-10-03 18:08:57 -07006998 QCBORDecode_GetTextString(&DC, &String);
6999 uErr = QCBORDecode_GetError(&DC);
7000 if(uErr != QCBOR_SUCCESS) {
7001 return 1;
7002 }
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07007003 QCBORDecode_EnterMap(&DC, NULL);
Laurence Lundbladea4308a82020-10-03 18:08:57 -07007004 uErr = QCBORDecode_GetError(&DC);
7005 if(uErr != QCBOR_ERR_INPUT_TOO_LARGE) {
7006 return 2;
7007 }
7008
7009 // Tests the end of a map being too large
7010 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTooLarge2), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07007011 QCBORDecode_EnterArray(&DC, NULL);
7012 QCBORDecode_EnterMap(&DC, NULL);
Laurence Lundbladea4308a82020-10-03 18:08:57 -07007013 uErr = QCBORDecode_GetError(&DC);
7014 if(uErr != QCBOR_SUCCESS) {
7015 return 3;
7016 }
7017 QCBORDecode_ExitMap(&DC);
7018 uErr = QCBORDecode_GetError(&DC);
7019 if(uErr != QCBOR_ERR_INPUT_TOO_LARGE) {
7020 return 4;
7021 }
7022
7023 // Tests the entire input CBOR being too large when processing bstr wrapping
7024 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTooLarge3), QCBOR_DECODE_MODE_NORMAL);
7025 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
7026 uErr = QCBORDecode_GetError(&DC);
7027 if(uErr != QCBOR_ERR_INPUT_TOO_LARGE) {
7028 return 5;
7029 }
7030
7031 return 0;
7032}
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07007033
7034
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08007035#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
7036
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07007037static const uint8_t spMapWithIndefLenStrings[] = {
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08007038 0xa3,
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07007039 0x7f, 0x61, 'l', 0x64, 'a', 'b', 'e', 'l' , 0x61, '1', 0xff,
7040 0x5f, 0x42, 0x01, 0x02, 0x43, 0x03, 0x04, 0x05, 0xff,
7041 0x7f, 0x62, 'd', 'y', 0x61, 'm', 0x61, 'o', 0xff,
7042 0x03,
7043 0x7f, 0x62, 'l', 'a', 0x63, 'b', 'e', 'l', 0x61, '2', 0xff,
7044 0xc3,
7045 0x5f, 0x42, 0x00, 0x01, 0x42, 0x00, 0x01, 0x41, 0x01, 0xff,
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07007046};
7047
7048int32_t SpiffyIndefiniteLengthStringsTests()
7049{
7050 QCBORDecodeContext DCtx;
7051
7052 QCBORDecode_Init(&DCtx,
7053 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spMapWithIndefLenStrings),
7054 QCBOR_DECODE_MODE_NORMAL);
7055
Laurence Lundblade8510f8c2020-12-01 11:31:16 -08007056 UsefulBuf_MAKE_STACK_UB(StringBuf, 200);
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07007057 QCBORDecode_SetMemPool(&DCtx, StringBuf, false);
7058
7059 UsefulBufC ByteString;
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07007060 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07007061 QCBORDecode_GetByteStringInMapSZ(&DCtx, "label1", &ByteString);
7062 if(QCBORDecode_GetAndResetError(&DCtx)) {
7063 return 1;
7064 }
7065
7066 const uint8_t pExectedBytes[] = {0x01, 0x02, 0x03, 0x04, 0x05};
7067 if(UsefulBuf_Compare(ByteString, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pExectedBytes))) {
7068 return 2;
7069 }
7070
7071 uint64_t uInt;
7072 QCBORDecode_GetUInt64InMapSZ(&DCtx, "dymo", &uInt);
7073 if(QCBORDecode_GetAndResetError(&DCtx)) {
7074 return 3;
7075 }
7076 if(uInt != 3) {
7077 return 4;
7078 }
7079
Máté Tóth-Pálef5f07a2021-09-17 19:31:37 +02007080#ifndef USEFULBUF_DISABLE_ALL_FLOAT
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07007081 double uDouble;
7082 QCBORDecode_GetDoubleConvertAllInMapSZ(&DCtx,
7083 "label2",
7084 0xff,
7085 &uDouble);
Laurence Lundbladeb8e19aa2020-10-07 20:59:11 -07007086#ifndef QCBOR_DISABLE_FLOAT_HW_USE
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07007087 if(QCBORDecode_GetAndResetError(&DCtx)) {
7088 return 5;
7089 }
7090 if(uDouble != -16777474) {
7091 return 6;
7092 }
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08007093#else /* QCBOR_DISABLE_FLOAT_HW_USE */
Laurence Lundbladeb8e19aa2020-10-07 20:59:11 -07007094 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_HW_FLOAT_DISABLED) {
7095 return 7;
7096 }
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08007097#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
Máté Tóth-Pálef5f07a2021-09-17 19:31:37 +02007098#endif /* USEFULBUF_DISABLE_ALL_FLOAT */
Laurence Lundbladeb8e19aa2020-10-07 20:59:11 -07007099
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07007100
7101 QCBORDecode_ExitMap(&DCtx);
7102
7103 if(QCBORDecode_Finish(&DCtx)) {
7104 return 99;
7105 }
7106
7107 return 0;
7108}
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08007109#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
Laurence Lundblade2a26abb2020-11-05 19:06:54 -08007110
7111
Laurence Lundbladecf41c522021-02-20 10:19:07 -07007112/*
7113 * An array of an integer and an array. The second array contains
7114 * a bstr-wrapped map.
7115 *
7116 * [7, [h'A36D6669... (see next lines) 73']]
7117 *
7118 * {"first integer": 42,
7119 * "an array of two strings": ["string1", "string2"],
7120 * "map in a map":
7121 * { "bytes 1": h'78787878',
7122 * "bytes 2": h'79797979',
7123 * "another int": 98,
7124 * "text 2": "lies, damn lies and statistics"
7125 * }
7126 * }
7127 */
Laurence Lundblade2a26abb2020-11-05 19:06:54 -08007128
Laurence Lundbladecf41c522021-02-20 10:19:07 -07007129static const uint8_t pValidWrappedMapEncoded[] = {
7130 0x82, 0x07, 0x81, 0x58, 0x97,
7131 0xa3, 0x6d, 0x66, 0x69, 0x72, 0x73, 0x74, 0x20, 0x69, 0x6e,
7132 0x74, 0x65, 0x67, 0x65, 0x72, 0x18, 0x2a, 0x77, 0x61, 0x6e,
7133 0x20, 0x61, 0x72, 0x72, 0x61, 0x79, 0x20, 0x6f, 0x66, 0x20,
7134 0x74, 0x77, 0x6f, 0x20, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67,
7135 0x73, 0x82, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x31,
7136 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x32, 0x6c, 0x6d,
7137 0x61, 0x70, 0x20, 0x69, 0x6e, 0x20, 0x61, 0x20, 0x6d, 0x61,
7138 0x70, 0xa4, 0x67, 0x62, 0x79, 0x74, 0x65, 0x73, 0x20, 0x31,
7139 0x44, 0x78, 0x78, 0x78, 0x78, 0x67, 0x62, 0x79, 0x74, 0x65,
7140 0x73, 0x20, 0x32, 0x44, 0x79, 0x79, 0x79, 0x79, 0x6b, 0x61,
7141 0x6e, 0x6f, 0x74, 0x68, 0x65, 0x72, 0x20, 0x69, 0x6e, 0x74,
7142 0x18, 0x62, 0x66, 0x74, 0x65, 0x78, 0x74, 0x20, 0x32, 0x78,
7143 0x1e, 0x6c, 0x69, 0x65, 0x73, 0x2c, 0x20, 0x64, 0x61, 0x6d,
7144 0x6e, 0x20, 0x6c, 0x69, 0x65, 0x73, 0x20, 0x61, 0x6e, 0x64,
7145 0x20, 0x73, 0x74, 0x61, 0x74, 0x69, 0x73, 0x74, 0x69, 0x63,
7146 0x73
7147};
7148
7149#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
7150
7151/* As above, but the arrays are indefinite length */
7152static const uint8_t pValidIndefWrappedMapEncoded[] = {
7153 0x9f, 0x07, 0x9f, 0x58, 0x97,
7154 0xa3, 0x6d, 0x66, 0x69, 0x72, 0x73, 0x74, 0x20, 0x69, 0x6e,
7155 0x74, 0x65, 0x67, 0x65, 0x72, 0x18, 0x2a, 0x77, 0x61, 0x6e,
7156 0x20, 0x61, 0x72, 0x72, 0x61, 0x79, 0x20, 0x6f, 0x66, 0x20,
7157 0x74, 0x77, 0x6f, 0x20, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67,
7158 0x73, 0x82, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x31,
7159 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x32, 0x6c, 0x6d,
7160 0x61, 0x70, 0x20, 0x69, 0x6e, 0x20, 0x61, 0x20, 0x6d, 0x61,
7161 0x70, 0xa4, 0x67, 0x62, 0x79, 0x74, 0x65, 0x73, 0x20, 0x31,
7162 0x44, 0x78, 0x78, 0x78, 0x78, 0x67, 0x62, 0x79, 0x74, 0x65,
7163 0x73, 0x20, 0x32, 0x44, 0x79, 0x79, 0x79, 0x79, 0x6b, 0x61,
7164 0x6e, 0x6f, 0x74, 0x68, 0x65, 0x72, 0x20, 0x69, 0x6e, 0x74,
7165 0x18, 0x62, 0x66, 0x74, 0x65, 0x78, 0x74, 0x20, 0x32, 0x78,
7166 0x1e, 0x6c, 0x69, 0x65, 0x73, 0x2c, 0x20, 0x64, 0x61, 0x6d,
7167 0x6e, 0x20, 0x6c, 0x69, 0x65, 0x73, 0x20, 0x61, 0x6e, 0x64,
7168 0x20, 0x73, 0x74, 0x61, 0x74, 0x69, 0x73, 0x74, 0x69, 0x63,
7169 0x73,
7170 0xff, 0xff
7171};
7172#endif
7173
7174
7175static const uint8_t pWithEmptyMap[] = {0x82, 0x18, 0x64, 0xa0};
7176
7177#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
7178static const uint8_t pWithEmptyMapInDef[] = {0x9f, 0x18, 0x64, 0xbf, 0xff, 0xff};
7179#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
7180
7181#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
7182static const uint8_t pWrappedByIndefiniteLength[] = {
7183 0x81,
7184 0xd8, 0x18,
7185 0x5f,
7186 0x41, 0x83,
7187 0x41, 0x18,
7188 0x43, 0x2A, 0x18, 0x2B,
7189 0x42, 0x18, 0x2C,
7190 0xff
7191};
7192#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
7193
7194
7195int32_t PeekAndRewindTest()
Laurence Lundblade2a26abb2020-11-05 19:06:54 -08007196{
7197 QCBORItem Item;
7198 QCBORError nCBORError;
7199 QCBORDecodeContext DCtx;
7200
7201 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0);
7202
7203 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
7204 return 100+(int32_t)nCBORError;
7205 }
7206 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
7207 return 200;
7208 }
7209
Laurence Lundblade3427dee2021-06-20 11:11:24 -07007210 QCBORDecode_VPeekNext(&DCtx, &Item);
7211 if((nCBORError = QCBORDecode_GetError(&DCtx))) {
7212 return 150+(int32_t)nCBORError;
7213 }
7214 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
7215 return 250;
7216 }
7217
Laurence Lundblade2a26abb2020-11-05 19:06:54 -08007218 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
7219 return (int32_t)nCBORError;
7220 }
7221 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
7222 return 300;
7223 }
7224
7225 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
7226 return 400 + (int32_t)nCBORError;
7227 }
7228 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
7229 return 500;
7230 }
7231
7232 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7233 return (int32_t)nCBORError;
7234 }
7235 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
7236 return 600;
7237 }
7238
7239 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
7240 return 900 + (int32_t)nCBORError;
7241 }
7242 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
7243 Item.uDataType != QCBOR_TYPE_INT64 ||
7244 Item.val.int64 != 42 ||
7245 Item.uDataAlloc ||
7246 Item.uLabelAlloc ||
7247 UsefulBufCompareToSZ(Item.label.string, "first integer")) {
7248 return 1000;
7249 }
7250
7251 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7252 return 1100 + (int32_t)nCBORError;
7253 }
7254
7255 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
7256 Item.uDataType != QCBOR_TYPE_INT64 ||
7257 Item.val.int64 != 42 ||
7258 Item.uDataAlloc ||
7259 Item.uLabelAlloc ||
7260 UsefulBufCompareToSZ(Item.label.string, "first integer")) {
7261 return 1200;
7262 }
7263
7264
7265 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7266 return 1300 + (int32_t)nCBORError;
7267 }
7268 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
7269 Item.uDataAlloc ||
7270 Item.uLabelAlloc ||
7271 UsefulBufCompareToSZ(Item.label.string, "an array of two strings") ||
7272 Item.uDataType != QCBOR_TYPE_ARRAY ||
Laurence Lundbladecf41c522021-02-20 10:19:07 -07007273 Item.val.uCount != 2) {
Laurence Lundblade2a26abb2020-11-05 19:06:54 -08007274 return 1400;
Laurence Lundbladecf41c522021-02-20 10:19:07 -07007275 }
Laurence Lundblade2a26abb2020-11-05 19:06:54 -08007276
7277 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7278 return 1500 + (int32_t)nCBORError;
7279 }
7280 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
7281 Item.uDataAlloc ||
7282 Item.uLabelAlloc ||
7283 UsefulBufCompareToSZ(Item.val.string, "string1")) {
7284 return 1600;
7285 }
7286
7287 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
7288 return 1700 + (int32_t)nCBORError;
7289 }
7290 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
7291 Item.uDataAlloc ||
7292 Item.uLabelAlloc ||
7293 UsefulBufCompareToSZ(Item.val.string, "string2")) {
7294 return 1800;
7295 }
7296
7297 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
7298 return (int32_t)nCBORError;
7299 }
7300 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
7301 Item.uDataAlloc ||
7302 Item.uLabelAlloc ||
7303 UsefulBufCompareToSZ(Item.val.string, "string2")) {
7304 return 1900;
7305 }
7306
7307 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7308 return (int32_t)nCBORError;
7309 }
7310 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
7311 Item.uDataAlloc ||
7312 Item.uLabelAlloc ||
7313 UsefulBufCompareToSZ(Item.val.string, "string2")) {
7314 return 2000;
7315 }
7316
7317
7318 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7319 return 2100 + (int32_t)nCBORError;
7320 }
7321 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
7322 Item.uDataAlloc ||
7323 Item.uLabelAlloc ||
7324 UsefulBufCompareToSZ(Item.label.string, "map in a map") ||
7325 Item.uDataType != QCBOR_TYPE_MAP ||
7326 Item.val.uCount != 4) {
7327 return 2100;
7328 }
7329
7330 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7331 return 2200 + (int32_t)nCBORError;
7332 }
7333 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
7334 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("bytes 1"))||
7335 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
7336 Item.uDataAlloc ||
7337 Item.uLabelAlloc ||
7338 UsefulBufCompareToSZ(Item.val.string, "xxxx")) {
7339 return 2300;
7340 }
7341
7342 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
7343 return 2400 + (int32_t)nCBORError;
7344 }
7345 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
7346 UsefulBufCompareToSZ(Item.label.string, "bytes 2") ||
7347 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
7348 Item.uDataAlloc ||
7349 Item.uLabelAlloc ||
7350 UsefulBufCompareToSZ(Item.val.string, "yyyy")) {
7351 return 2500;
7352 }
7353
7354 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7355 return 2600 + (int32_t)nCBORError;
7356 }
7357 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
7358 UsefulBufCompareToSZ(Item.label.string, "bytes 2") ||
7359 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
7360 Item.uDataAlloc ||
7361 Item.uLabelAlloc ||
7362 UsefulBufCompareToSZ(Item.val.string, "yyyy")) {
7363 return 2700;
7364 }
7365
7366 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7367 return 2800 + (int32_t)nCBORError;
7368 }
7369 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
7370 Item.uDataAlloc ||
7371 Item.uLabelAlloc ||
7372 UsefulBufCompareToSZ(Item.label.string, "another int") ||
7373 Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundbladecf41c522021-02-20 10:19:07 -07007374 Item.val.int64 != 98) {
Laurence Lundblade2a26abb2020-11-05 19:06:54 -08007375 return 2900;
Laurence Lundbladecf41c522021-02-20 10:19:07 -07007376 }
Laurence Lundblade2a26abb2020-11-05 19:06:54 -08007377
7378 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
7379 return 3000 + (int32_t)nCBORError;
7380 }
7381 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
7382 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("text 2"))||
7383 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
7384 Item.uDataAlloc ||
7385 Item.uLabelAlloc ||
7386 UsefulBufCompareToSZ(Item.val.string, "lies, damn lies and statistics")) {
7387 return 3100;
7388 }
7389
7390 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7391 return 3200 + (int32_t)nCBORError;
7392 }
7393 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
7394 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("text 2"))||
7395 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
7396 Item.uDataAlloc ||
7397 Item.uLabelAlloc ||
7398 UsefulBufCompareToSZ(Item.val.string, "lies, damn lies and statistics")) {
7399 return 3300;
7400 }
7401
Laurence Lundblade3427dee2021-06-20 11:11:24 -07007402 nCBORError = QCBORDecode_PeekNext(&DCtx, &Item);
7403 if(nCBORError != QCBOR_ERR_NO_MORE_ITEMS) {
7404 return 3300 + (int32_t)nCBORError;
7405 }
7406
7407 QCBORDecode_VPeekNext(&DCtx, &Item);
7408 nCBORError = QCBORDecode_GetError(&DCtx);
7409 if(nCBORError != QCBOR_ERR_NO_MORE_ITEMS) {
7410 return 3400 + (int32_t)nCBORError;
7411 }
7412
7413 QCBORDecode_VPeekNext(&DCtx, &Item);
7414 nCBORError = QCBORDecode_GetError(&DCtx);
7415 if(nCBORError != QCBOR_ERR_NO_MORE_ITEMS) {
7416 return 3500 + (int32_t)nCBORError;
7417 }
Laurence Lundbladecf41c522021-02-20 10:19:07 -07007418
7419
7420 // Rewind to top level after entering several maps
7421 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0);
7422
7423 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7424 return (int32_t)nCBORError;
7425 }
7426 if(Item.uDataType != QCBOR_TYPE_MAP ||
7427 Item.val.uCount != 3) {
7428 return 400;
7429 }
7430
7431 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7432 return 4000+(int32_t)nCBORError;
7433 }
7434
7435 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
7436 Item.uDataType != QCBOR_TYPE_INT64 ||
7437 Item.val.int64 != 42 ||
7438 Item.uDataAlloc ||
7439 Item.uLabelAlloc ||
7440 UsefulBufCompareToSZ(Item.label.string, "first integer")) {
7441 return 4100;
7442 }
7443
7444 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7445 return 4100+(int32_t)nCBORError;
7446 }
7447 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
7448 Item.uDataAlloc ||
7449 Item.uLabelAlloc ||
7450 UsefulBufCompareToSZ(Item.label.string, "an array of two strings") ||
7451 Item.uDataType != QCBOR_TYPE_ARRAY ||
7452 Item.val.uCount != 2) {
7453 return 4200;
7454 }
7455
7456 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7457 return 4200+(int32_t)nCBORError;
7458 }
7459 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
7460 Item.uDataAlloc ||
7461 Item.uLabelAlloc ||
7462 UsefulBufCompareToSZ(Item.val.string, "string1")) {
7463 return 4300;
7464 }
7465
7466 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7467 return 4300+(int32_t)nCBORError;
7468 }
7469 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
7470 Item.uDataAlloc ||
7471 Item.uLabelAlloc ||
7472 UsefulBufCompareToSZ(Item.val.string, "string2")) {
7473 return 4400;
7474 }
7475
7476 QCBORDecode_Rewind(&DCtx);
7477
7478 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7479 return 4400+(int32_t)nCBORError;
7480 }
7481 if(Item.uDataType != QCBOR_TYPE_MAP ||
7482 Item.val.uCount != 3) {
7483 return 4500;
7484 }
7485
7486 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7487 return (int32_t)nCBORError;
7488 }
7489
7490 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
7491 Item.uDataType != QCBOR_TYPE_INT64 ||
7492 Item.val.int64 != 42 ||
7493 Item.uDataAlloc ||
7494 Item.uLabelAlloc ||
7495 UsefulBufCompareToSZ(Item.label.string, "first integer")) {
7496 return 4600;
7497 }
7498
7499 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7500 return (int32_t)nCBORError;
7501 }
7502 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
7503 Item.uDataAlloc ||
7504 Item.uLabelAlloc ||
7505 UsefulBufCompareToSZ(Item.label.string, "an array of two strings") ||
7506 Item.uDataType != QCBOR_TYPE_ARRAY ||
7507 Item.val.uCount != 2) {
7508 return 4700;
7509 }
7510
7511 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7512 return (int32_t)nCBORError;
7513 }
7514 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
7515 Item.uDataAlloc ||
7516 Item.uLabelAlloc ||
7517 UsefulBufCompareToSZ(Item.val.string, "string1")) {
7518 return 4800;
7519 }
7520
7521 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7522 return 4900+(int32_t)nCBORError;
7523 }
7524 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
7525 Item.uDataAlloc ||
7526 Item.uLabelAlloc ||
7527 UsefulBufCompareToSZ(Item.val.string, "string2")) {
7528 return 5000;
7529 }
7530
7531
7532 // Rewind an entered map
7533 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0);
7534
7535 QCBORDecode_EnterMap(&DCtx, NULL);
7536
7537 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7538 return 5100+(int32_t)nCBORError;
7539 }
7540
7541 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
7542 Item.uDataType != QCBOR_TYPE_INT64 ||
7543 Item.val.int64 != 42 ||
7544 Item.uDataAlloc ||
7545 Item.uLabelAlloc ||
7546 UsefulBufCompareToSZ(Item.label.string, "first integer")) {
7547 return 5200;
7548 }
7549
7550 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7551 return 5200+(int32_t)nCBORError;
7552 }
7553 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
7554 Item.uDataAlloc ||
7555 Item.uLabelAlloc ||
7556 UsefulBufCompareToSZ(Item.label.string, "an array of two strings") ||
7557 Item.uDataType != QCBOR_TYPE_ARRAY ||
7558 Item.val.uCount != 2) {
7559 return -5300;
7560 }
7561
7562 QCBORDecode_Rewind(&DCtx);
7563
7564 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7565 return 5300+(int32_t)nCBORError;
7566 }
7567
7568 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
7569 Item.uDataType != QCBOR_TYPE_INT64 ||
7570 Item.val.int64 != 42 ||
7571 Item.uDataAlloc ||
7572 Item.uLabelAlloc ||
7573 UsefulBufCompareToSZ(Item.label.string, "first integer")) {
7574 return 5400;
7575 }
7576
7577 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7578 return 5400+(int32_t)nCBORError;
7579 }
7580 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
7581 Item.uDataAlloc ||
7582 Item.uLabelAlloc ||
7583 UsefulBufCompareToSZ(Item.label.string, "an array of two strings") ||
7584 Item.uDataType != QCBOR_TYPE_ARRAY ||
7585 Item.val.uCount != 2) {
7586 return 5500;
7587 }
7588
7589
7590 // Rewind and entered array inside an entered map
7591 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0);
7592
7593 QCBORDecode_EnterMap(&DCtx, NULL);
7594
7595 QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings");
7596
7597 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7598 return 5600+(int32_t)nCBORError;
7599 }
7600 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
7601 Item.uDataAlloc ||
7602 Item.uLabelAlloc ||
7603 UsefulBufCompareToSZ(Item.val.string, "string1")) {
7604 return 5700;
7605 }
7606
7607 QCBORDecode_Rewind(&DCtx);
7608
7609 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7610 return 5700+(int32_t)nCBORError;
7611 }
7612 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
7613 Item.uDataAlloc ||
7614 Item.uLabelAlloc ||
7615 UsefulBufCompareToSZ(Item.val.string, "string1")) {
7616 return 5800;
7617 }
7618
7619 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7620 return (int32_t)nCBORError;
7621 }
7622 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
7623 Item.uDataAlloc ||
7624 Item.uLabelAlloc ||
7625 UsefulBufCompareToSZ(Item.val.string, "string2")) {
7626 return 5900;
7627 }
7628
7629 QCBORDecode_Rewind(&DCtx);
7630
7631 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7632 return 5900+(int32_t)nCBORError;
7633 }
7634 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
7635 Item.uDataAlloc ||
7636 Item.uLabelAlloc ||
7637 UsefulBufCompareToSZ(Item.val.string, "string1")) {
7638 return 6000;
7639 }
7640
7641
7642 // Rewind a byte string inside an array inside an array
7643 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidWrappedMapEncoded), 0);
7644
7645 QCBORDecode_EnterArray(&DCtx, NULL);
7646
7647 uint64_t i;
7648 QCBORDecode_GetUInt64(&DCtx, &i);
7649
7650 QCBORDecode_EnterArray(&DCtx, NULL);
7651
7652 QCBORDecode_EnterBstrWrapped(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
7653 if(QCBORDecode_GetError(&DCtx)) {
7654 return 6100;
7655 }
7656
7657 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7658 return (int32_t)nCBORError;
7659 }
7660 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
7661 return 6200;
7662 }
7663
7664 QCBORDecode_Rewind(&DCtx);
7665
7666 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7667 return 6300+(int32_t)nCBORError;
7668 }
7669 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
7670 return 6400;
7671 }
7672
7673#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
7674 // Rewind a byte string inside an indefinite-length array inside
7675 // indefinite-length array
7676
7677 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidIndefWrappedMapEncoded), 0);
7678
7679 QCBORDecode_EnterArray(&DCtx, NULL);
7680
7681 QCBORDecode_GetUInt64(&DCtx, &i);
7682
7683 QCBORDecode_EnterArray(&DCtx, NULL);
7684
7685 QCBORDecode_EnterBstrWrapped(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
7686 if(QCBORDecode_GetError(&DCtx)) {
7687 return 6500;
7688 }
7689
7690 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7691 return 6600+(int32_t)nCBORError;
7692 }
7693 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
7694 return 6700;
7695 }
7696
7697 QCBORDecode_Rewind(&DCtx);
7698
7699 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7700 return 6800+(int32_t)nCBORError;
7701 }
7702 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
7703 return 6900;
7704 }
7705#endif
7706
7707 // Rewind an empty map
7708 // [100, {}]
7709 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pWithEmptyMap), 0);
7710 QCBORDecode_EnterArray(&DCtx, NULL);
7711 QCBORDecode_GetUInt64(&DCtx, &i);
7712 if(i != 100) {
7713 return 7010;
7714 }
7715 QCBORDecode_EnterMap(&DCtx, NULL);
7716
7717 /* Do it 5 times to be sure multiple rewinds work */
7718 for(int n = 0; n < 5; n++) {
7719 nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
7720 if(nCBORError != QCBOR_ERR_NO_MORE_ITEMS) {
7721 return 7000 + n;
7722 }
7723 QCBORDecode_Rewind(&DCtx);
7724 }
7725 QCBORDecode_ExitMap(&DCtx);
7726 QCBORDecode_Rewind(&DCtx);
7727 QCBORDecode_GetUInt64(&DCtx, &i);
7728 if(i != 100) {
7729 return 7010;
7730 }
7731 QCBORDecode_ExitArray(&DCtx);
7732 QCBORDecode_Rewind(&DCtx);
7733 QCBORDecode_EnterArray(&DCtx, NULL);
7734 i = 9;
7735 QCBORDecode_GetUInt64(&DCtx, &i);
7736 if(i != 100) {
7737 return 7020;
7738 }
7739 if(QCBORDecode_GetError(&DCtx)){
7740 return 7030;
7741 }
7742
7743 // Rewind an empty indefinite length map
7744#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
7745 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pWithEmptyMapInDef), 0);
7746 QCBORDecode_EnterArray(&DCtx, NULL);
7747 QCBORDecode_GetUInt64(&DCtx, &i);
7748 if(i != 100) {
7749 return 7810;
7750 }
7751 QCBORDecode_EnterMap(&DCtx, NULL);
7752
7753 /* Do it 5 times to be sure multiple rewinds work */
7754 for(int n = 0; n < 5; n++) {
7755 nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
7756 if(nCBORError != QCBOR_ERR_NO_MORE_ITEMS) {
7757 return 7800 + n;
7758 }
7759 QCBORDecode_Rewind(&DCtx);
7760 }
7761 QCBORDecode_ExitMap(&DCtx);
7762 QCBORDecode_Rewind(&DCtx);
7763 QCBORDecode_GetUInt64(&DCtx, &i);
7764 if(i != 100) {
7765 return 7810;
7766 }
7767 QCBORDecode_ExitArray(&DCtx);
7768 QCBORDecode_Rewind(&DCtx);
7769 QCBORDecode_EnterArray(&DCtx, NULL);
7770 i = 9;
7771 QCBORDecode_GetUInt64(&DCtx, &i);
7772 if(i != 100) {
7773 return 7820;
7774 }
7775 if(QCBORDecode_GetError(&DCtx)){
7776 return 7830;
7777 }
7778#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
7779
7780 // Rewind an indefnite length byte-string wrapped sequence
7781#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
7782 QCBORDecode_Init(&DCtx,
7783 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pWrappedByIndefiniteLength),
7784 0);
7785 UsefulBuf_MAKE_STACK_UB(Pool, 100);
7786 QCBORDecode_SetMemPool(&DCtx, Pool, 0);
7787
7788 QCBORDecode_EnterArray(&DCtx, NULL);
7789 QCBORDecode_EnterBstrWrapped(&DCtx, 2, NULL);
7790 if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_INPUT_TOO_LARGE) {
Laurence Lundblade732e52d2021-02-22 20:11:01 -07007791 /* this is what happens when trying to enter
7792 indefinite-length byte string
Laurence Lundbladecf41c522021-02-20 10:19:07 -07007793 wrapped CBOR. Tolerate for now. Eventually it needs
7794 to be fixed so this works, but that is not simple. */
7795 return 7300;
7796 }
7797
7798 /*
7799 QCBORDecode_GetUInt64(&DCtx, &i);
7800 if(i != 42) {
7801 return 7110;
7802 }
7803 QCBORDecode_Rewind(&DCtx);
7804 QCBORDecode_GetUInt64(&DCtx, &i);
7805 if(i != 42) {
7806 return 7220;
7807 }*/
7808#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
7809
7810
7811 // Rewind an indefnite length byte-string wrapped sequence
7812
Laurence Lundblade2a26abb2020-11-05 19:06:54 -08007813 return 0;
7814}
Laurence Lundblade9f9c3732021-03-23 09:38:46 -07007815
7816
7817
7818
7819static const uint8_t spBooleansInMap[] =
7820{
7821 0xa1, 0x08, 0xf5
7822};
7823
7824static const uint8_t spBooleansInMapWrongType[] =
7825{
7826 0xa1, 0x08, 0xf6
7827};
7828
7829static const uint8_t spBooleansInMapNWF[] =
7830{
7831 0xa1, 0x08, 0x1a
7832};
7833
Laurence Lundblade8782dd32021-04-27 04:15:37 -07007834static const uint8_t spNullInMap[] =
7835{
7836 0xa1, 0x08, 0xf6
7837};
7838
7839static const uint8_t spUndefinedInMap[] =
7840{
7841 0xa1, 0x08, 0xf7
7842};
7843
Laurence Lundblade9f9c3732021-03-23 09:38:46 -07007844
7845int32_t BoolTest(void)
7846{
7847 QCBORDecodeContext DCtx;
7848 bool b;
7849
Laurence Lundblade8782dd32021-04-27 04:15:37 -07007850 QCBORDecode_Init(&DCtx,
7851 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMap),
7852 0);
Laurence Lundblade9f9c3732021-03-23 09:38:46 -07007853 QCBORDecode_EnterMap(&DCtx, NULL);
7854 QCBORDecode_GetBool(&DCtx, &b);
7855 if(QCBORDecode_GetAndResetError(&DCtx) || !b) {
7856 return 1;
7857 }
7858
7859 QCBORDecode_GetBoolInMapN(&DCtx, 7, &b);
7860 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_LABEL_NOT_FOUND) {
7861 return 2;
7862 }
7863
7864 QCBORDecode_GetBoolInMapN(&DCtx, 8, &b);
7865 if(QCBORDecode_GetAndResetError(&DCtx) || !b) {
7866 return 3;
7867 }
7868
7869
7870 QCBORDecode_GetBoolInMapSZ(&DCtx, "xx", &b);
7871 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_LABEL_NOT_FOUND) {
7872 return 4;
7873 }
7874
Laurence Lundblade8782dd32021-04-27 04:15:37 -07007875 QCBORDecode_Init(&DCtx,
7876 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMapWrongType),
7877 0);
Laurence Lundblade9f9c3732021-03-23 09:38:46 -07007878 QCBORDecode_EnterMap(&DCtx, NULL);
7879 QCBORDecode_GetBool(&DCtx, &b);
7880 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
7881 return 5;
7882 }
7883
Laurence Lundblade8782dd32021-04-27 04:15:37 -07007884 QCBORDecode_Init(&DCtx,
7885 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMapNWF),
7886 0);
Laurence Lundblade9f9c3732021-03-23 09:38:46 -07007887 QCBORDecode_EnterMap(&DCtx, NULL);
7888 QCBORDecode_GetBool(&DCtx, &b);
7889 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_HIT_END) {
7890 return 6;
7891 }
7892
Laurence Lundblade8782dd32021-04-27 04:15:37 -07007893
7894 QCBORDecode_Init(&DCtx,
7895 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spNullInMap),
7896 0);
7897 QCBORDecode_EnterMap(&DCtx, NULL);
7898 QCBORDecode_GetNull(&DCtx);
7899 if(QCBORDecode_GetAndResetError(&DCtx)) {
7900 return 7;
7901 }
7902
7903 QCBORDecode_Init(&DCtx,
7904 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMap),
7905 0);
7906 QCBORDecode_EnterMap(&DCtx, NULL);
7907 QCBORDecode_GetNull(&DCtx);
7908 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
7909 return 8;
7910 }
7911
7912 QCBORDecode_Init(&DCtx,
7913 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spNullInMap),
7914 0);
7915 QCBORDecode_EnterMap(&DCtx, NULL);
7916 QCBORDecode_GetNullInMapN(&DCtx, 8);
7917 if(QCBORDecode_GetAndResetError(&DCtx)) {
7918 return 9;
7919 }
7920
7921 QCBORDecode_Init(&DCtx,
7922 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMap),
7923 0);
7924 QCBORDecode_EnterMap(&DCtx, NULL);
7925 QCBORDecode_GetNullInMapN(&DCtx, 8);
7926 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
7927 return 10;
7928 }
7929
7930 QCBORDecode_Init(&DCtx,
7931 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMapNWF),
7932 0);
7933 QCBORDecode_EnterMap(&DCtx, NULL);
7934 QCBORDecode_GetUndefined(&DCtx);
7935 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_HIT_END) {
7936 return 11;
7937 }
7938
7939 QCBORDecode_Init(&DCtx,
7940 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUndefinedInMap),
7941 0);
7942 QCBORDecode_EnterMap(&DCtx, NULL);
7943 QCBORDecode_GetUndefined(&DCtx);
7944 if(QCBORDecode_GetAndResetError(&DCtx)) {
7945 return 12;
7946 }
7947
7948 QCBORDecode_Init(&DCtx,
7949 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMap),
7950 0);
7951 QCBORDecode_EnterMap(&DCtx, NULL);
7952 QCBORDecode_GetUndefined(&DCtx);
7953 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
7954 return 13;
7955 }
7956
7957 QCBORDecode_Init(&DCtx,
7958 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUndefinedInMap),
7959 0);
7960 QCBORDecode_EnterMap(&DCtx, NULL);
7961 QCBORDecode_GetUndefinedInMapN(&DCtx, 8);
7962 if(QCBORDecode_GetAndResetError(&DCtx)) {
7963 return 14;
7964 }
7965
7966 QCBORDecode_Init(&DCtx,
7967 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMap),
7968 0);
7969 QCBORDecode_EnterMap(&DCtx, NULL);
7970 QCBORDecode_GetUndefinedInMapN(&DCtx, 8);
7971 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
7972 return 15;
7973 }
7974
7975 QCBORDecode_Init(&DCtx,
7976 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMapNWF),
7977 0);
7978 QCBORDecode_EnterMap(&DCtx, NULL);
7979 QCBORDecode_GetUndefined(&DCtx);
7980 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_HIT_END) {
7981 return 15;
7982 }
7983
Laurence Lundblade9f9c3732021-03-23 09:38:46 -07007984 return 0;
7985}