blob: e0d8202e114fb52ee5dba9fd3a2b417dfee74d8b [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 Lundblade4b270642020-08-14 12:53:07 -07002564 0x86,
2565
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
2575 0xc1, // tag for epoch date
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08002576 0x80, // Erroneous empty array as content for date
Laurence Lundblade4b270642020-08-14 12:53:07 -07002577
2578 0xc0, // tag for string date
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08002579 0xa0, // Erroneous empty map as content for date
Laurence Lundblade4b270642020-08-14 12:53:07 -07002580
Laurence Lundblade46d63e92021-05-13 11:37:10 -07002581 0xad, // Open a map for tests involving labels.
Laurence Lundbladec7114722020-08-13 05:11:40 -07002582
2583 0x00,
2584 0xc0, // tag for string date
Laurence Lundblade4b270642020-08-14 12:53:07 -07002585 0x6a, '1','9','8','5','-','0','4','-','1','2', // Tagged date string
Laurence Lundbladec7114722020-08-13 05:11:40 -07002586
2587 0x01,
Laurence Lundblade9b334962020-08-27 10:55:53 -07002588 0xda, 0x03, 0x03, 0x03, 0x03, // An additional tag
Laurence Lundbladec7114722020-08-13 05:11:40 -07002589 0xc1, // tag for epoch date
2590 0x1a, 0x53, 0x72, 0x4E, 0x00, // Epoch date 1400000000; Tue, 13 May 2014 16:53:20 GMT
2591
2592 // Untagged integer 0
2593 0x08,
2594 0x00,
2595
2596 // Utagged date string with string label y
2597 0x61, 0x79,
Laurence Lundblade4b270642020-08-14 12:53:07 -07002598 0x6a, '2','0','8','5','-','0','4','-','1','2', // Untagged date string
Laurence Lundbladec7114722020-08-13 05:11:40 -07002599
2600 // Untagged -1000 with label z
2601 0x61, 0x7a,
Laurence Lundblade9b334962020-08-27 10:55:53 -07002602 0xda, 0x01, 0x01, 0x01, 0x01, // An additional tag
Laurence Lundbladec7114722020-08-13 05:11:40 -07002603 0x39, 0x03, 0xe7,
2604
Laurence Lundbladec7114722020-08-13 05:11:40 -07002605 0x07,
2606 0xc1, // tag for epoch date
2607 0xfb, 0x43, 0xdf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, // 9223372036854773760 largest supported
2608
Laurence Lundblade4b270642020-08-14 12:53:07 -07002609 0x05,
2610 0xc1,
2611 0xfb, 0xc3, 0xdf, 0xff, 0xff, 0xff, 0xff, 0xff, 0xfe, // -9223372036854773760 largest negative
2612
Laurence Lundbladec7114722020-08-13 05:11:40 -07002613 // Untagged single-precision float with value 3.14 with string label x
2614 0x61, 0x78,
2615 0xFA, 0x40, 0x48, 0xF5, 0xC3,
2616
Laurence Lundbladec7114722020-08-13 05:11:40 -07002617 // Untagged half-precision float with value -2
2618 0x09,
2619 0xF9, 0xC0, 0x00,
Laurence Lundblade46d63e92021-05-13 11:37:10 -07002620
2621 /* Tagged date-only date string */
2622 0x63, 0x53, 0x44, 0x53,
2623 0xD9, 0x03, 0xEC,
2624 0x6A, 0x31, 0x39, 0x38, 0x35, 0x2D, 0x30, 0x34, 0x2D, 0x31, 0x32, /* "1985-04-12" */
2625
2626 /* Untagged date-only date string */
2627 0x18, 0x63,
2628 0x6A, 0x31, 0x39, 0x38, 0x35, 0x2D, 0x30, 0x34, 0x2D, 0x31, 0x32, /* "1985-04-12" */
2629
2630 /* Tagged days-count epoch date */
2631 0x63, 0x53, 0x44, 0x45,
2632 0xD8, 0x64, /* tag(100) */
2633 0x39, 0x29, 0xB3, /* -10676 */
2634
2635 /* Untagged days-count epoch date */
2636 0x11,
2637 0x19, 0x0F, 0x9A /* 3994 */
2638
Laurence Lundbladec7114722020-08-13 05:11:40 -07002639};
2640
2641int32_t SpiffyDateDecodeTest()
2642{
2643 QCBORDecodeContext DC;
Laurence Lundblade4b270642020-08-14 12:53:07 -07002644 QCBORError uError;
Laurence Lundblade9b334962020-08-27 10:55:53 -07002645 int64_t nEpochDate2, nEpochDate3, nEpochDate5,
2646 nEpochDate4, nEpochDate6, nEpochDateFail,
Laurence Lundblade46d63e92021-05-13 11:37:10 -07002647 nEpochDate1400000000, nEpochDays1, nEpochDays2;
2648 UsefulBufC StringDate1, StringDate2, StringDays1, StringDays2;
Laurence Lundblade9b334962020-08-27 10:55:53 -07002649 uint64_t uTag1, uTag2;
Laurence Lundbladec7114722020-08-13 05:11:40 -07002650
2651 QCBORDecode_Init(&DC,
Laurence Lundblade4b270642020-08-14 12:53:07 -07002652 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSpiffyDateTestInput),
Laurence Lundbladec7114722020-08-13 05:11:40 -07002653 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07002654 QCBORDecode_EnterArray(&DC, NULL);
Laurence Lundbladec7114722020-08-13 05:11:40 -07002655
Laurence Lundblade9b334962020-08-27 10:55:53 -07002656 // Too-negative float, -9.2233720368547748E+18
2657 QCBORDecode_GetEpochDate(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nEpochDateFail);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002658 uError = QCBORDecode_GetAndResetError(&DC);
Laurence Lundblade16a207a2021-09-18 17:22:46 -07002659 if(uError != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_DATE_OVERFLOW)) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002660 return 1111;
2661 }
2662
2663 // Too-large integer
Laurence Lundblade9b334962020-08-27 10:55:53 -07002664 QCBORDecode_GetEpochDate(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nEpochDateFail);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002665 uError = QCBORDecode_GetAndResetError(&DC);
2666 if(uError != QCBOR_ERR_DATE_OVERFLOW) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002667 return 1;
2668 }
2669
Laurence Lundblade4b270642020-08-14 12:53:07 -07002670 // Half-precision minus infinity
Laurence Lundblade9b334962020-08-27 10:55:53 -07002671 QCBORDecode_GetEpochDate(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nEpochDateFail);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002672 uError = QCBORDecode_GetAndResetError(&DC);
Laurence Lundblade16a207a2021-09-18 17:22:46 -07002673 if(uError != FLOAT_ERR_CODE_NO_HALF_PREC_NO_FLOAT_HW(QCBOR_ERR_DATE_OVERFLOW)) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002674 return 2;
Laurence Lundbladec7114722020-08-13 05:11:40 -07002675 }
2676
Laurence Lundblade4b270642020-08-14 12:53:07 -07002677 // Bad content for epoch date
Laurence Lundblade9b334962020-08-27 10:55:53 -07002678 QCBORDecode_GetEpochDate(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nEpochDateFail);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002679 uError = QCBORDecode_GetAndResetError(&DC);
2680 if(uError != QCBOR_ERR_BAD_OPT_TAG) {
2681 return 3;
2682 }
2683
2684 // Bad content for string date
Laurence Lundblade9b334962020-08-27 10:55:53 -07002685 QCBORDecode_GetDateString(&DC, QCBOR_TAG_REQUIREMENT_TAG, &StringDate1);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002686 uError = QCBORDecode_GetAndResetError(&DC);
2687 if(uError != QCBOR_ERR_BAD_OPT_TAG) {
2688 return 4;
2689 }
2690
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07002691 QCBORDecode_EnterMap(&DC, NULL);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002692
2693 // Get largest negative double precision epoch date allowed
Laurence Lundblade9b334962020-08-27 10:55:53 -07002694 QCBORDecode_GetEpochDateInMapN(&DC,
2695 5,
2696 QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG |
2697 QCBOR_TAG_REQUIREMENT_ALLOW_ADDITIONAL_TAGS,
2698 &nEpochDate2);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002699 uError = QCBORDecode_GetAndResetError(&DC);
Laurence Lundblade16a207a2021-09-18 17:22:46 -07002700 if(uError != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)) {
Laurence Lundbladec7114722020-08-13 05:11:40 -07002701 return 102;
2702 }
Laurence Lundblade16a207a2021-09-18 17:22:46 -07002703 if(uError == QCBOR_SUCCESS) {
2704 if(nEpochDate2 != -9223372036854773760LL) {
2705 return 101;
2706 }
2707 }
Laurence Lundbladec7114722020-08-13 05:11:40 -07002708
Laurence Lundblade4b270642020-08-14 12:53:07 -07002709 // Get largest double precision epoch date allowed
Laurence Lundblade9b334962020-08-27 10:55:53 -07002710 QCBORDecode_GetEpochDateInMapN(&DC, 7, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
2711 &nEpochDate2);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002712 uError = QCBORDecode_GetAndResetError(&DC);
Laurence Lundblade16a207a2021-09-18 17:22:46 -07002713 if(uError != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)) {
Laurence Lundblade9b334962020-08-27 10:55:53 -07002714 return 112;
Laurence Lundbladec7114722020-08-13 05:11:40 -07002715 }
Laurence Lundblade16a207a2021-09-18 17:22:46 -07002716 if(uError == QCBOR_SUCCESS) {
2717 if(nEpochDate2 != 9223372036854773760ULL) {
2718 return 111;
2719 }
2720 }
Laurence Lundblade4b270642020-08-14 12:53:07 -07002721
2722 // A single-precision date
Laurence Lundblade9b334962020-08-27 10:55:53 -07002723 QCBORDecode_GetEpochDateInMapSZ(&DC, "x", QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
2724 &nEpochDate5);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002725 uError = QCBORDecode_GetAndResetError(&DC);
Laurence Lundblade16a207a2021-09-18 17:22:46 -07002726 if(uError != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002727 return 104;
2728 }
Laurence Lundblade16a207a2021-09-18 17:22:46 -07002729 if(uError == QCBOR_SUCCESS) {
2730 if(nEpochDate5 != 3) {
2731 return 103;
2732 }
2733 }
Laurence Lundbladec7114722020-08-13 05:11:40 -07002734
Laurence Lundblade9b334962020-08-27 10:55:53 -07002735 // A half-precision date with value -2 FFF
2736 QCBORDecode_GetEpochDateInMapN(&DC, 9, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
2737 &nEpochDate4);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002738 uError = QCBORDecode_GetAndResetError(&DC);
Laurence Lundblade16a207a2021-09-18 17:22:46 -07002739 if(uError != FLOAT_ERR_CODE_NO_HALF_PREC_NO_FLOAT_HW(QCBOR_SUCCESS)) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002740 return 106;
2741 }
Laurence Lundblade16a207a2021-09-18 17:22:46 -07002742 if(uError == QCBOR_SUCCESS) {
2743 if(nEpochDate4 != -2) {
2744 return 105;
2745 }
2746 }
Laurence Lundblade4b270642020-08-14 12:53:07 -07002747
2748 // Fail to get an epoch date by string label
Laurence Lundblade9b334962020-08-27 10:55:53 -07002749 QCBORDecode_GetEpochDateInMapSZ(&DC, "no-label",
2750 QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
2751 &nEpochDate6);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002752 uError = QCBORDecode_GetAndResetError(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002753 if(uError != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002754 return 107;
2755 }
2756
2757 // Fail to get an epoch date by integer label
Laurence Lundblade9b334962020-08-27 10:55:53 -07002758 QCBORDecode_GetEpochDateInMapN(&DC, 99999, QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
2759 &nEpochDate6);
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 108;
2763 }
2764
2765 // Fail to get a string date by string label
Laurence Lundblade9b334962020-08-27 10:55:53 -07002766 QCBORDecode_GetDateStringInMapSZ(&DC, "no-label",
2767 QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
2768 &StringDate1);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002769 uError = QCBORDecode_GetAndResetError(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002770 if(uError != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002771 return 109;
2772 }
2773
2774 // Fail to get a string date by integer label
Laurence Lundblade9b334962020-08-27 10:55:53 -07002775 QCBORDecode_GetDateStringInMapN(&DC, 99999, QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
2776 &StringDate1);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002777 uError = QCBORDecode_GetAndResetError(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002778 if(uError != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002779 return 110;
2780 }
2781
2782 // The rest of these succeed even if float features are disabled
Laurence Lundbladea9489f82020-09-12 13:50:56 -07002783
Laurence Lundblade4b270642020-08-14 12:53:07 -07002784 // Epoch date 1400000000; Tue, 13 May 2014 16:53:20 GMT
Laurence Lundblade9b334962020-08-27 10:55:53 -07002785 QCBORDecode_GetEpochDateInMapN(&DC,
2786 1,
2787 QCBOR_TAG_REQUIREMENT_TAG |
2788 QCBOR_TAG_REQUIREMENT_ALLOW_ADDITIONAL_TAGS,
2789 &nEpochDate1400000000);
2790 uTag1 = QCBORDecode_GetNthTagOfLast(&DC, 0);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002791 // Tagged date string
Laurence Lundblade9b334962020-08-27 10:55:53 -07002792 QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
2793 &StringDate1);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002794 // Untagged integer 0
Laurence Lundblade9b334962020-08-27 10:55:53 -07002795 QCBORDecode_GetEpochDateInMapN(&DC, 8, QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
2796 &nEpochDate3);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002797 // Untagged date string
Laurence Lundblade9b334962020-08-27 10:55:53 -07002798 QCBORDecode_GetDateStringInMapSZ(&DC, "y", QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
2799 &StringDate2);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002800 // Untagged -1000 with label z
Laurence Lundblade9b334962020-08-27 10:55:53 -07002801 QCBORDecode_GetEpochDateInMapSZ(&DC,
2802 "z",
2803 QCBOR_TAG_REQUIREMENT_NOT_A_TAG |
2804 QCBOR_TAG_REQUIREMENT_ALLOW_ADDITIONAL_TAGS,
2805 &nEpochDate6);
2806 uTag2 = QCBORDecode_GetNthTagOfLast(&DC, 0);
Laurence Lundblade4b270642020-08-14 12:53:07 -07002807
Laurence Lundblade46d63e92021-05-13 11:37:10 -07002808 /* The days format is much simpler than the date format
2809 * because it can't be a floating point value. The test
2810 * of the spiffy decode functions sufficiently covers
2811 * the test of the non-spiffy decode days date decoding.
2812 * There is no full fan out of the error conditions
2813 * and decode options as that is implemented by code
2814 * that is tested well by the date testing above.
2815 */
2816 QCBORDecode_GetDaysStringInMapSZ(&DC, "SDS", QCBOR_TAG_REQUIREMENT_TAG,
2817 &StringDays1);
2818
2819 QCBORDecode_GetDaysStringInMapN(&DC, 99, QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
2820 &StringDays2);
2821
2822 QCBORDecode_GetEpochDaysInMapSZ(&DC, "SDE", QCBOR_TAG_REQUIREMENT_TAG,
2823 &nEpochDays1);
2824
2825 QCBORDecode_GetEpochDaysInMapN(&DC, 17, QCBOR_TAG_REQUIREMENT_NOT_A_TAG,
2826 &nEpochDays2);
2827
Laurence Lundblade4b270642020-08-14 12:53:07 -07002828 QCBORDecode_ExitMap(&DC);
2829 QCBORDecode_ExitArray(&DC);
2830 uError = QCBORDecode_Finish(&DC);
2831 if(uError) {
Laurence Lundblade9b334962020-08-27 10:55:53 -07002832 return 1000 + (int32_t)uError;
Laurence Lundblade4b270642020-08-14 12:53:07 -07002833 }
2834
Laurence Lundblade9b334962020-08-27 10:55:53 -07002835 if(nEpochDate1400000000 != 1400000000) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002836 return 200;
2837 }
2838
Laurence Lundblade9b334962020-08-27 10:55:53 -07002839 if(uTag1 != 0x03030303) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002840 return 201;
2841 }
2842
Laurence Lundblade9b334962020-08-27 10:55:53 -07002843 if(nEpochDate3 != 0) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002844 return 202;
2845 }
2846
Laurence Lundblade9b334962020-08-27 10:55:53 -07002847 if(nEpochDate6 != -1000) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002848 return 203;
2849 }
2850
Laurence Lundblade9b334962020-08-27 10:55:53 -07002851 if(uTag2 != 0x01010101) {
Laurence Lundblade4b270642020-08-14 12:53:07 -07002852 return 204;
2853 }
2854
Laurence Lundblade46d63e92021-05-13 11:37:10 -07002855 if(nEpochDays1 != -10676) {
2856 return 205;
2857 }
2858
2859 if(nEpochDays2 != 3994) {
2860 return 206;
2861 }
2862
Laurence Lundblade9b334962020-08-27 10:55:53 -07002863 if(UsefulBuf_Compare(StringDate1, UsefulBuf_FromSZ("1985-04-12"))) {
2864 return 205;
2865 }
2866
2867 if(UsefulBuf_Compare(StringDate2, UsefulBuf_FromSZ("2085-04-12"))) {
2868 return 206;
2869 }
2870
Laurence Lundblade46d63e92021-05-13 11:37:10 -07002871 if(UsefulBuf_Compare(StringDays1, UsefulBuf_FromSZ("1985-04-12"))) {
2872 return 207;
2873 }
2874
2875 if(UsefulBuf_Compare(StringDays2, UsefulBuf_FromSZ("1985-04-12"))) {
2876 return 208;
2877 }
2878
Laurence Lundbladec7114722020-08-13 05:11:40 -07002879 return 0;
2880}
2881
2882
2883
Laurence Lundblade9b334962020-08-27 10:55:53 -07002884// Input for one of the tagging tests
Laurence Lundbladecc7da412020-12-27 00:09:07 -08002885static const uint8_t spTagInput[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08002886 0xd9, 0xd9, 0xf7, // CBOR magic number
Laurence Lundblade9b334962020-08-27 10:55:53 -07002887 0x81, // Array of one
2888 0xd8, 0x04, // non-preferred serialization of tag 4, decimal fraction
2889 0x82, // Array of two that is the faction 1/3
2890 0x01,
2891 0x03,
2892
2893 /*
2894 More than 4 tags on an item 225(226(227(228(229([])))))
2895 */
2896 0xd8, 0xe1,
2897 0xd8, 0xe2,
2898 0xd8, 0xe3,
2899 0xd8, 0xe4,
2900 0xd8, 0xe5,
2901 0x80,
2902
2903 /* tag 10489608748473423768(
2904 2442302356(
2905 21590(
2906 240(
2907 []))))
2908 */
2909 0xdb, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98,
2910 0xda, 0x91, 0x92, 0x93, 0x94,
2911 0xd9, 0x54, 0x56,
2912 0xd8, 0xf0,
2913 0x80,
2914
2915 /* tag 21590(
2916 10489608748473423768(
2917 2442302357(
2918 65534(
2919 []))))
2920 */
2921 0xdb, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x54, 0x56,
2922 0xdb, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97, 0x98,
2923 0xda, 0x91, 0x92, 0x93, 0x95,
2924 0xd9, 0xff, 0xfe,
2925 0x80,
2926
2927 /* Make sure to blow past the limit of tags that must be mapped.
2928 works in conjuntion with entries above.
2929 269488144(269488145(269488146(269488147([]))))
2930 */
2931 0xda, 0x10, 0x10, 0x10, 0x10,
2932 0xda, 0x10, 0x10, 0x10, 0x11,
2933 0xda, 0x10, 0x10, 0x10, 0x12,
2934 0xda, 0x10, 0x10, 0x10, 0x13,
2935 0x80,
2936
2937 /* An invalid decimal fraction with an additional tag */
2938 0xd9, 0xff, 0xfa,
2939 0xd8, 0x02, // non-preferred serialization of tag 2, a big num
2940 0x00, // the integer 0; should be a byte string
2941};
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002942
Laurence Lundblade59289e52019-12-30 13:44:37 -08002943/*
2944 DB 9192939495969798 # tag(10489608748473423768)
Laurence Lundblade9b334962020-08-27 10:55:53 -07002945 80 # array(0)
Laurence Lundblade59289e52019-12-30 13:44:37 -08002946 */
Laurence Lundbladecc7da412020-12-27 00:09:07 -08002947static const uint8_t spEncodedLargeTag[] = {0xdb, 0x91, 0x92, 0x93, 0x94, 0x95,
Laurence Lundbladeee851742020-01-08 08:37:05 -08002948 0x96, 0x97, 0x98, 0x80};
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002949
Laurence Lundblade59289e52019-12-30 13:44:37 -08002950/*
2951DB 9192939495969798 # tag(10489608748473423768)
2952 D8 88 # tag(136)
2953 C6 # tag(6)
2954 C7 # tag(7)
2955 80 # array(0)
2956*/
Laurence Lundbladecc7da412020-12-27 00:09:07 -08002957static const uint8_t spLotsOfTags[] = {0xdb, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96,
Laurence Lundbladeee851742020-01-08 08:37:05 -08002958 0x97, 0x98, 0xd8, 0x88, 0xc6, 0xc7, 0x80};
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002959
2960/*
Laurence Lundblade9b334962020-08-27 10:55:53 -07002961 55799(55799(55799({
2962 6(7(-23)): 5859837686836516696(7({
2963 7(-20): 11({
2964 17(-18): 17(17(17("Organization"))),
2965 9(-17): 773("SSG"),
2966 -15: 16(17(6(7("Confusion")))),
2967 17(-16): 17("San Diego"),
2968 17(-14): 17("US")
2969 }),
2970 23(-19): 19({
2971 -11: 9({
2972 -9: -7
2973 }),
2974 90599561(90599561(90599561(-10))): 12(h'0102030405060708090A')
2975 })
2976 })),
2977 16(-22): 23({
2978 11(8(7(-5))): 8(-3)
2979 })
2980 })))
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002981 */
Laurence Lundbladecc7da412020-12-27 00:09:07 -08002982static const uint8_t spCSRWithTags[] = {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002983 0xd9, 0xd9, 0xf7, 0xd9, 0xd9, 0xf7, 0xd9, 0xd9, 0xf7, 0xa2,
2984 0xc6, 0xc7, 0x36,
2985 0xdb, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, 0x57, 0x58, 0xc7, 0xa2,
2986 0xda, 0x00, 0x00, 0x00, 0x07, 0x33,
2987 0xcb, 0xa5,
2988 0xd1, 0x31,
2989 0xd1, 0xd1, 0xd1, 0x6c,
2990 0x4f, 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74, 0x69, 0x6f, 0x6e,
2991 0xc9, 0x30,
2992 0xd9, 0x03, 0x05, 0x63,
2993 0x53, 0x53, 0x47,
2994 0x2e,
Laurence Lundblade9b334962020-08-27 10:55:53 -07002995 0xd0, 0xd1, 0xc6, 0xc7,
2996 0x69,
2997 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73, 0x69, 0x6f, 0x6e,
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07002998 0xd1, 0x2f,
2999 0xd1, 0x69,
3000 0x53, 0x61, 0x6e, 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f,
3001 0xd1, 0x2d,
3002 0xd1, 0x62,
3003 0x55, 0x53,
3004 0xd7, 0x32,
3005 0xd3, 0xa2,
3006 0x2a,
3007 0xc9, 0xa1,
3008 0x28,
3009 0x26,
3010 0xda, 0x05, 0x66, 0x70, 0x89, 0xda, 0x05, 0x66, 0x70, 0x89, 0xda, 0x05, 0x66, 0x70, 0x89, 0x29,
3011 0xcc, 0x4a,
3012 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,0x07, 0x08, 0x09, 0x0a,
3013 0xd0, 0x35,
3014 0xd7, 0xa1,
3015 0xcb, 0xc8, 0xc7, 0x24,
3016 0xc8, 0x22};
3017
Laurence Lundblade9b334962020-08-27 10:55:53 -07003018
Laurence Lundbladecc7da412020-12-27 00:09:07 -08003019static const uint8_t spSpiffyTagInput[] = {
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08003020 0x85, // Open array
Laurence Lundblade9b334962020-08-27 10:55:53 -07003021
3022 0xc0, // tag for string date
3023 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string
3024
3025 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string
3026
3027 0x4a, '1','9','8','5','-','0','4','-','1','2', // Date string in byte string
3028
3029 0xd8, 0x23, // tag for regex
3030 0x6a, '1','9','8','5','-','0','4','-','1','2', // Date string
3031
3032 0xc0, // tag for string date
3033 0x4a, '1','9','8','5','-','0','4','-','1','2', // Date string in byte string
Laurence Lundblade9b334962020-08-27 10:55:53 -07003034};
3035
3036
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003037static int32_t CheckCSRMaps(QCBORDecodeContext *pDC);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003038
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003039
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003040int32_t OptTagParseTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003041{
3042 QCBORDecodeContext DCtx;
Laurence Lundblade9b334962020-08-27 10:55:53 -07003043 QCBORItem Item;
3044 QCBORError uError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003045
Laurence Lundbladeee851742020-01-08 08:37:05 -08003046 QCBORDecode_Init(&DCtx,
Laurence Lundblade9b334962020-08-27 10:55:53 -07003047 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTagInput),
Laurence Lundbladeee851742020-01-08 08:37:05 -08003048 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003049
Laurence Lundblade9b334962020-08-27 10:55:53 -07003050 /*
3051 This test matches the magic number tag and the fraction tag
3052 55799([...])
3053 */
3054 uError = QCBORDecode_GetNext(&DCtx, &Item);
3055 if(uError != QCBOR_SUCCESS) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003056 return -2;
3057 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003058 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003059 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC)) {
3060 return -3;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003061 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003062
Laurence Lundblade9b334962020-08-27 10:55:53 -07003063 /*
3064 4([1,3])
3065 */
3066 uError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundbladedd6e76e2021-03-10 01:54:01 -07003067#ifdef QCBOR_DISABLE_EXP_AND_MANTISSA
Laurence Lundblade9b334962020-08-27 10:55:53 -07003068 if(uError != QCBOR_SUCCESS ||
3069 Item.uDataType != QCBOR_TYPE_ARRAY ||
3070 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_DECIMAL_FRACTION) ||
3071 QCBORDecode_GetNthTag(&DCtx, &Item, 0) != CBOR_TAG_DECIMAL_FRACTION ||
3072 QCBORDecode_GetNthTag(&DCtx, &Item, 1) != CBOR_TAG_INVALID64 ||
3073 QCBORDecode_GetNthTag(&DCtx, &Item, 2) != CBOR_TAG_INVALID64 ||
3074 QCBORDecode_GetNthTag(&DCtx, &Item, 3) != CBOR_TAG_INVALID64 ||
3075 QCBORDecode_GetNthTag(&DCtx, &Item, 4) != CBOR_TAG_INVALID64 ||
3076 Item.val.uCount != 2) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003077 return -4;
3078 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07003079 // consume the items in the array
3080 uError = QCBORDecode_GetNext(&DCtx, &Item);
3081 uError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundblade59289e52019-12-30 13:44:37 -08003082
Laurence Lundbladedd6e76e2021-03-10 01:54:01 -07003083#else /* QCBOR_DISABLE_EXP_AND_MANTISSA */
Laurence Lundblade9b334962020-08-27 10:55:53 -07003084 if(uError != QCBOR_SUCCESS ||
3085 Item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION ||
3086 QCBORDecode_GetNthTag(&DCtx, &Item, 0) != CBOR_TAG_INVALID64 ||
3087 QCBORDecode_GetNthTag(&DCtx, &Item, 1) != CBOR_TAG_INVALID64 ||
3088 QCBORDecode_GetNthTag(&DCtx, &Item, 2) != CBOR_TAG_INVALID64 ||
3089 QCBORDecode_GetNthTag(&DCtx, &Item, 3) != CBOR_TAG_INVALID64 ||
3090 QCBORDecode_GetNthTag(&DCtx, &Item, 4) != CBOR_TAG_INVALID64 ) {
3091 return -5;
Laurence Lundblade59289e52019-12-30 13:44:37 -08003092 }
Laurence Lundbladedd6e76e2021-03-10 01:54:01 -07003093#endif /* QCBOR_DISABLE_EXP_AND_MANTISSA */
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003094
Laurence Lundblade9b334962020-08-27 10:55:53 -07003095 /*
3096 More than 4 tags on an item 225(226(227(228(229([])))))
3097 */
3098 uError = QCBORDecode_GetNext(&DCtx, &Item);
3099 if(uError != QCBOR_ERR_TOO_MANY_TAGS) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003100 return -6;
3101 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07003102
Laurence Lundblade88e9db22020-11-02 03:56:33 -08003103 if(QCBORDecode_GetNthTag(&DCtx, &Item, 0) != CBOR_TAG_INVALID64) {
3104 return -106;
3105 }
3106
3107
Laurence Lundblade9b334962020-08-27 10:55:53 -07003108 /* tag 10489608748473423768(
3109 2442302356(
3110 21590(
3111 240(
3112 []))))
3113 */
3114 uError = QCBORDecode_GetNext(&DCtx, &Item);
3115 if(uError != QCBOR_SUCCESS ||
3116 Item.uDataType != QCBOR_TYPE_ARRAY ||
3117 QCBORDecode_GetNthTag(&DCtx, &Item, 3) != 10489608748473423768ULL ||
3118 QCBORDecode_GetNthTag(&DCtx, &Item, 2) != 2442302356ULL ||
3119 QCBORDecode_GetNthTag(&DCtx, &Item, 1) != 21590ULL ||
3120 QCBORDecode_GetNthTag(&DCtx, &Item, 0) != 240ULL) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003121 return -7;
Laurence Lundblade9b334962020-08-27 10:55:53 -07003122 }
3123
3124 /* tag 21590(
3125 10489608748473423768(
3126 2442302357(
3127 21591(
3128 []))))
3129 */
3130 uError = QCBORDecode_GetNext(&DCtx, &Item);
3131 if(uError != QCBOR_SUCCESS ||
3132 Item.uDataType != QCBOR_TYPE_ARRAY ||
3133 QCBORDecode_GetNthTag(&DCtx, &Item, 0) != 65534ULL ||
3134 QCBORDecode_GetNthTag(&DCtx, &Item, 1) != 2442302357ULL ||
3135 QCBORDecode_GetNthTag(&DCtx, &Item, 2) != 10489608748473423768ULL ||
3136 QCBORDecode_GetNthTag(&DCtx, &Item, 3) != 21590ULL) {
3137 return -8;
3138 }
3139
3140 /* Make sure to blow past the limit of tags that must be mapped.
3141 works in conjuntion with entries above.
3142 269488144(269488145(269488146(269488147([]))))
3143 */
3144 uError = QCBORDecode_GetNext(&DCtx, &Item);
3145 if(uError != QCBOR_ERR_TOO_MANY_TAGS) {
3146 return -9;
3147 }
3148
3149 uError = QCBORDecode_GetNext(&DCtx, &Item);
3150 if(uError == QCBOR_SUCCESS) {
3151 return -10;
3152 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003153
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003154 // ----------------------------------
Laurence Lundbladeee851742020-01-08 08:37:05 -08003155 // This test sets up a caller-config list that includes the very large
Laurence Lundblade9b334962020-08-27 10:55:53 -07003156 // tage and then matches it. Caller-config lists are no longer
3157 // used or needed. This tests backwards compatibility with them.
Laurence Lundbladeee851742020-01-08 08:37:05 -08003158 QCBORDecode_Init(&DCtx,
3159 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEncodedLargeTag),
3160 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003161 const uint64_t puList[] = {0x9192939495969798, 257};
3162 const QCBORTagListIn TL = {2, puList};
3163 QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003164
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003165 if(QCBORDecode_GetNext(&DCtx, &Item)) {
3166 return -8;
3167 }
3168 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
3169 !QCBORDecode_IsTagged(&DCtx, &Item, 0x9192939495969798) ||
3170 QCBORDecode_IsTagged(&DCtx, &Item, 257) ||
3171 QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_BIGFLOAT) ||
3172 Item.val.uCount != 0) {
3173 return -9;
3174 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003175
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003176 //------------------------
Laurence Lundbladeee851742020-01-08 08:37:05 -08003177 // Sets up a caller-configured list and look up something not in it
Laurence Lundblade9b334962020-08-27 10:55:53 -07003178 // Another backwards compatibility test.
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003179 const uint64_t puLongList[17] = {1,2,1};
3180 const QCBORTagListIn TLLong = {17, puLongList};
Laurence Lundbladeee851742020-01-08 08:37:05 -08003181 QCBORDecode_Init(&DCtx,
3182 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEncodedLargeTag),
3183 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003184 QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TLLong);
3185 if(QCBORDecode_GetNext(&DCtx, &Item)) {
3186 return -11;
3187 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003188
Laurence Lundbladef7a70bc2020-10-24 12:23:25 -07003189 uint64_t puTags[4];
Laurence Lundblade9b334962020-08-27 10:55:53 -07003190 QCBORTagListOut Out = {0, 4, puTags};
3191
3192
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003193 // This tests retrievel of the full tag list
Laurence Lundbladeee851742020-01-08 08:37:05 -08003194 QCBORDecode_Init(&DCtx,
3195 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spLotsOfTags),
3196 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003197 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3198 return -12;
3199 }
3200 if(puTags[0] != 0x9192939495969798 ||
3201 puTags[1] != 0x88 ||
Laurence Lundblade59289e52019-12-30 13:44:37 -08003202 puTags[2] != 0x06 ||
3203 puTags[3] != 0x07) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003204 return -13;
3205 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003206
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003207 // ----------------------
Laurence Lundblade9b334962020-08-27 10:55:53 -07003208 // This tests too small of an out list
Laurence Lundbladeee851742020-01-08 08:37:05 -08003209 QCBORDecode_Init(&DCtx,
3210 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spLotsOfTags),
3211 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003212 QCBORTagListOut OutSmall = {0, 3, puTags};
3213 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &OutSmall) != QCBOR_ERR_TOO_MANY_TAGS) {
3214 return -14;
3215 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003216
Laurence Lundblade9b334962020-08-27 10:55:53 -07003217
3218
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003219 // ---------------
Laurence Lundblade9b334962020-08-27 10:55:53 -07003220 // Decode a version of the "CSR" that has had a ton of tags randomly inserted
3221 // It is a bit of a messy test and maybe could be improved, but
3222 // it is retained as a backwards compatibility check.
Laurence Lundbladeee851742020-01-08 08:37:05 -08003223 QCBORDecode_Init(&DCtx,
3224 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRWithTags),
3225 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003226 int n = CheckCSRMaps(&DCtx);
3227 if(n) {
3228 return n-2000;
3229 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003230
Laurence Lundblade59289e52019-12-30 13:44:37 -08003231 Out = (QCBORTagListOut){0, 16, puTags};
Laurence Lundbladeee851742020-01-08 08:37:05 -08003232 QCBORDecode_Init(&DCtx,
3233 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRWithTags),
3234 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003235
Laurence Lundblade9b334962020-08-27 10:55:53 -07003236 /* With the spiffy decode revision, this tag list is not used.
3237 It doesn't matter if a tag is in this list or not so some
3238 tests that couldn't process a tag because it isn't in this list
3239 now can process these unlisted tags. The tests have been
3240 adjusted for this. */
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003241 const uint64_t puTagList[] = {773, 1, 90599561};
3242 const QCBORTagListIn TagList = {3, puTagList};
3243 QCBORDecode_SetCallerConfiguredTagList(&DCtx, &TagList);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003244
3245
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003246 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3247 return -100;
3248 }
3249 if(Item.uDataType != QCBOR_TYPE_MAP ||
3250 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC) ||
3251 QCBORDecode_IsTagged(&DCtx, &Item, 90599561) ||
3252 QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_DATE_EPOCH) ||
3253 Item.val.uCount != 2 ||
3254 puTags[0] != CBOR_TAG_CBOR_MAGIC ||
3255 puTags[1] != CBOR_TAG_CBOR_MAGIC ||
3256 puTags[2] != CBOR_TAG_CBOR_MAGIC ||
3257 Out.uNumUsed != 3) {
3258 return -101;
3259 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003260
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003261 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3262 return -102;
3263 }
3264 if(Item.uDataType != QCBOR_TYPE_MAP ||
3265 QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_CBOR_MAGIC) ||
3266 QCBORDecode_IsTagged(&DCtx, &Item, 6) ||
Laurence Lundbladef7a70bc2020-10-24 12:23:25 -07003267 !QCBORDecode_IsTagged(&DCtx, &Item, 7) ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003268 Item.val.uCount != 2 ||
3269 puTags[0] != 5859837686836516696 ||
3270 puTags[1] != 7 ||
3271 Out.uNumUsed != 2) {
3272 return -103;
3273 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003274
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003275 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3276 return -104;
3277 }
3278 if(Item.uDataType != QCBOR_TYPE_MAP ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003279 Item.val.uCount != 5 ||
3280 puTags[0] != 0x0b ||
3281 Out.uNumUsed != 1) {
3282 return -105;
3283 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003284
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003285 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3286 return -106;
3287 }
3288 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
3289 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_COSE_MAC0) ||
3290 Item.val.string.len != 12 ||
3291 puTags[0] != CBOR_TAG_COSE_MAC0 ||
3292 puTags[1] != CBOR_TAG_COSE_MAC0 ||
3293 puTags[2] != CBOR_TAG_COSE_MAC0 ||
3294 Out.uNumUsed != 3) {
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 -107;
3300 }
3301 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
3302 !QCBORDecode_IsTagged(&DCtx, &Item, 773) ||
3303 Item.val.string.len != 3 ||
3304 puTags[0] != 773 ||
3305 Out.uNumUsed != 1) {
3306 return -108;
3307 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003308
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003309 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3310 return -109;
3311 }
3312 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade59289e52019-12-30 13:44:37 -08003313 !QCBORDecode_IsTagged(&DCtx, &Item, 16) ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003314 Item.val.string.len != 9 ||
Laurence Lundblade59289e52019-12-30 13:44:37 -08003315 puTags[0] != 16 ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07003316 puTags[3] != 7 ||
3317 Out.uNumUsed != 4) {
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003318 return -110;
3319 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003320
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003321 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3322 return -111;
3323 }
3324 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
3325 !QCBORDecode_IsTagged(&DCtx, &Item, 17) ||
3326 Item.val.string.len != 9 ||
3327 puTags[0] != 17 ||
3328 Out.uNumUsed != 1) {
3329 return -112;
3330 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003331
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003332 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3333 return -111;
3334 }
3335 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
3336 !QCBORDecode_IsTagged(&DCtx, &Item, 17) ||
3337 Item.val.string.len != 2 ||
3338 puTags[0] != 17 ||
3339 Out.uNumUsed != 1) {
3340 return -112;
3341 }
3342
3343 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3344 return -113;
3345 }
3346 if(Item.uDataType != QCBOR_TYPE_MAP ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07003347 !QCBORDecode_IsTagged(&DCtx, &Item, 19) ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003348 Item.val.uCount != 2 ||
3349 puTags[0] != 19 ||
3350 Out.uNumUsed != 1) {
3351 return -114;
3352 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003353
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003354 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3355 return -115;
3356 }
3357 if(Item.uDataType != QCBOR_TYPE_MAP ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07003358 !QCBORDecode_IsTagged(&DCtx, &Item, 9) ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003359 Item.val.uCount != 1 ||
3360 puTags[0] != 9 ||
3361 Out.uNumUsed != 1) {
3362 return -116;
3363 }
3364
3365 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3366 return -116;
3367 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003368 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003369 Item.val.int64 != -7 ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003370 Out.uNumUsed != 0) {
3371 return -117;
3372 }
3373
3374 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3375 return -118;
3376 }
3377 if(Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
3378 Item.val.string.len != 10 ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003379 puTags[0] != 12 ||
3380 Out.uNumUsed != 1) {
3381 return -119;
3382 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003383
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003384 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3385 return -120;
3386 }
3387 if(Item.uDataType != QCBOR_TYPE_MAP ||
3388 !QCBORDecode_IsTagged(&DCtx, &Item, CBOR_TAG_ENC_AS_B16) ||
3389 Item.val.uCount != 1 ||
3390 puTags[0] != 0x17 ||
3391 Out.uNumUsed != 1) {
3392 return -121;
3393 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003394
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003395 if(QCBORDecode_GetNextWithTags(&DCtx, &Item, &Out)) {
3396 return -122;
3397 }
3398 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07003399 !QCBORDecode_IsTagged(&DCtx, &Item, 8) ||
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003400 Item.val.int64 != -3 ||
3401 puTags[0] != 8 ||
3402 Out.uNumUsed != 1) {
3403 return -123;
3404 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003405
Laurence Lundbladedbe6f212018-10-28 11:37:53 +07003406 if(QCBORDecode_Finish(&DCtx)) {
3407 return -124;
3408 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07003409
3410 UsefulBufC DateString;
3411 QCBORDecode_Init(&DCtx,
3412 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSpiffyTagInput),
3413 QCBOR_DECODE_MODE_NORMAL);
3414
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07003415 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade9b334962020-08-27 10:55:53 -07003416 // tagged date string
3417 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString);
3418 // untagged date string
3419 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString);
3420 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_SUCCESS) {
3421 return 100;
3422 }
3423 // untagged byte string
3424 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString);
3425 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
3426 return 101;
3427 }
3428 // tagged regex
3429 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString);
3430 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
3431 return 102;
3432 }
3433 // tagged date string with a byte string
3434 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString);
3435 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_BAD_OPT_TAG) {
3436 return 103;
3437 }
3438 QCBORDecode_ExitArray(&DCtx);
3439 if(QCBORDecode_Finish(&DCtx) != QCBOR_SUCCESS) {
3440 return 104;
3441 }
3442
3443
3444 QCBORDecode_Init(&DCtx,
3445 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSpiffyTagInput),
3446 QCBOR_DECODE_MODE_NORMAL);
3447
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07003448 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade9b334962020-08-27 10:55:53 -07003449 // tagged date string
3450 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString);
3451 // untagged date string
3452 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString);
3453 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_SUCCESS) {
3454 return 200;
3455 }
3456 // untagged byte string
3457 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString);
3458 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
3459 return 201;
3460 }
3461 // tagged regex
3462 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString);
3463 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
3464 return 202;
3465 }
3466 // tagged date string with a byte string
3467 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &DateString);
3468 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_BAD_OPT_TAG) {
3469 return 203;
3470 }
3471 QCBORDecode_ExitArray(&DCtx);
3472 if(QCBORDecode_Finish(&DCtx) != QCBOR_SUCCESS) {
3473 return 204;
3474 }
3475
3476 QCBORDecode_Init(&DCtx,
3477 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSpiffyTagInput),
3478 QCBOR_DECODE_MODE_NORMAL);
3479
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07003480 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade9b334962020-08-27 10:55:53 -07003481 // tagged date string
3482 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString);
3483 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
3484 return 300;
3485 }
3486 // untagged date string
3487 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString);
3488 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
3489 return 301;
3490 }
3491 // untagged byte string
3492 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_TAG, &DateString);
3493 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
3494 return 302;
3495 }
3496 // tagged regex
3497 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString);
3498 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
3499 return 303;
3500 }
3501 // tagged date string with a byte string
3502 QCBORDecode_GetDateString(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &DateString);
3503 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_BAD_OPT_TAG) {
3504 return 304;
3505 }
3506 QCBORDecode_ExitArray(&DCtx);
3507 if(QCBORDecode_Finish(&DCtx) != QCBOR_SUCCESS) {
3508 return 305;
3509 }
3510
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003511 return 0;
3512}
3513
3514
3515
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003516
Laurence Lundbladecc7da412020-12-27 00:09:07 -08003517static const uint8_t spBigNumInput[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003518 0x83,
3519 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3520 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3521 0xA4,
3522 0x63, 0x42, 0x4E, 0x2B,
3523 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3524 0x18, 0x40,
3525 0xC2, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3526 0x63, 0x42, 0x4E, 0x2D,
3527 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3528 0x38, 0x3F,
3529 0xC3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
3530
Laurence Lundbladecc7da412020-12-27 00:09:07 -08003531/* The expected big num */
3532static const uint8_t spBigNum[] = {
3533 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
3534 0x00};
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003535
3536
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003537int32_t BignumParseTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003538{
3539 QCBORDecodeContext DCtx;
3540 QCBORItem Item;
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07003541 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003542
Laurence Lundbladeee851742020-01-08 08:37:05 -08003543 QCBORDecode_Init(&DCtx,
3544 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNumInput),
3545 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003546
3547
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003548 //
3549 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
3550 return -1;
3551 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003552 return -2;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003553 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003554
3555 //
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003556 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003557 return -3;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003558 if(Item.uDataType != QCBOR_TYPE_POSBIGNUM ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003559 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003560 return -4;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003561 }
3562
3563 //
3564 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003565 return -5;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003566 if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003567 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003568 return -6;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003569 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003570
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003571 //
3572 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003573 return -7;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003574 if(Item.uDataType != QCBOR_TYPE_MAP) {
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003575 return -8;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003576 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003577
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003578 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003579 return -9;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003580 if(Item.uDataType != QCBOR_TYPE_POSBIGNUM ||
3581 Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003582 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003583 return -10;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003584 }
3585
3586 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003587 return -11;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003588 if(Item.uDataType != QCBOR_TYPE_POSBIGNUM ||
3589 Item.uLabelType != QCBOR_TYPE_INT64 ||
3590 Item.label.int64 != 64 ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003591 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003592 return -12;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003593 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003594
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003595 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003596 return -13;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003597 if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM ||
3598 Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003599 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003600 return -14;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003601 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003602
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003603 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item)))
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003604 return -15;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003605 if(Item.uDataType != QCBOR_TYPE_NEGBIGNUM ||
3606 Item.uLabelType != QCBOR_TYPE_INT64 ||
3607 Item.label.int64 != -64 ||
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003608 UsefulBuf_Compare(Item.val.bigNum, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNum))){
Laurence Lundblade830fbf92020-05-31 17:22:33 -07003609 return -16;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003610 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003611
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003612 return 0;
3613}
3614
3615
3616
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003617static int32_t CheckItemWithIntLabel(QCBORDecodeContext *pCtx,
Laurence Lundbladeee851742020-01-08 08:37:05 -08003618 uint8_t uDataType,
3619 uint8_t uNestingLevel,
3620 uint8_t uNextNest,
3621 int64_t nLabel,
3622 QCBORItem *pItem)
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003623{
3624 QCBORItem Item;
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07003625 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003626
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003627 if((nCBORError = QCBORDecode_GetNext(pCtx, &Item))) return -1;
3628 if(Item.uDataType != uDataType) return -1;
3629 if(uNestingLevel > 0) {
Laurence Lundbladeee851742020-01-08 08:37:05 -08003630 if(Item.uLabelType != QCBOR_TYPE_INT64 &&
3631 Item.uLabelType != QCBOR_TYPE_UINT64) {
3632 return -1;
3633 }
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003634 if(Item.uLabelType == QCBOR_TYPE_INT64) {
3635 if(Item.label.int64 != nLabel) return -1;
3636 } else {
Laurence Lundblade570fab52018-10-13 18:28:27 +08003637 if(Item.label.uint64 != (uint64_t)nLabel) return -1;
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003638 }
3639 }
3640 if(Item.uNestingLevel != uNestingLevel) return -1;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303641 if(Item.uNextNestLevel != uNextNest) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003642
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003643 if(pItem) {
3644 *pItem = Item;
3645 }
3646 return 0;
3647}
3648
3649
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003650// Same code checks definite and indefinite length versions of the map
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003651static int32_t CheckCSRMaps(QCBORDecodeContext *pDC)
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003652{
Laurence Lundbladea44d5062018-10-17 18:45:12 +05303653 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 0, 1, 0, NULL)) return -1;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003654
Laurence Lundblade9b334962020-08-27 10:55:53 -07003655 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 1, 2, -23, NULL)) return -2;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003656
Laurence Lundblade9b334962020-08-27 10:55:53 -07003657 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 2, 3, -20, NULL)) return -3;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003658
Laurence Lundblade9b334962020-08-27 10:55:53 -07003659 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -18, NULL)) return -4;
3660 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -17, NULL)) return -5;
3661 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -15, NULL)) return -6;
3662 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 3, -16, NULL)) return -7;
3663 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_TEXT_STRING, 3, 2, -14, NULL)) return -8;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003664
Laurence Lundblade9b334962020-08-27 10:55:53 -07003665 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 2, 3, -19, NULL)) return -9;
3666 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 3, 4, -11, NULL)) return -10;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003667
Laurence Lundblade9b334962020-08-27 10:55:53 -07003668 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_INT64, 4, 3, -9, NULL)) return -11;
3669 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_BYTE_STRING, 3, 1, -10, NULL)) return -12;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003670
Laurence Lundblade9b334962020-08-27 10:55:53 -07003671 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_MAP, 1, 2, -22, NULL)) return -13;
3672 if(CheckItemWithIntLabel(pDC, QCBOR_TYPE_INT64, 2, 0, -5, NULL)) return -14;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003673
Laurence Lundblade9b334962020-08-27 10:55:53 -07003674 if(QCBORDecode_Finish(pDC)) return -20;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003675
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003676 return 0;
3677}
3678
3679
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003680/*
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003681{
3682 -23: {
3683 -20: {
3684 -18: "Organization",
3685 -17: "SSG",
3686 -15: "Confusion",
3687 -16: "San Diego",
3688 -14: "US"
3689 },
3690 -19: {
3691 -11: {
3692 -9: -7
3693 },
3694 -10: '\u0001\u0002\u0003\u0004\u0005\u0006\a\b\t\n'
3695 }
3696 },
3697 -22: {
3698 -5: -3
3699 }
3700}
Laurence Lundbladecc7da412020-12-27 00:09:07 -08003701*/
3702static const uint8_t spCSRInput[] = {
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003703 0xa2, 0x36, 0xa2, 0x33, 0xa5, 0x31, 0x6c, 0x4f,
3704 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74,
3705 0x69, 0x6f, 0x6e, 0x30, 0x63, 0x53, 0x53, 0x47,
3706 0x2e, 0x69, 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73,
3707 0x69, 0x6f, 0x6e, 0x2f, 0x69, 0x53, 0x61, 0x6e,
3708 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f, 0x2d, 0x62,
3709 0x55, 0x53, 0x32, 0xa2, 0x2a, 0xa1, 0x28, 0x26,
3710 0x29, 0x4a, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06,
3711 0x07, 0x08, 0x09, 0x0a, 0x35, 0xa1, 0x24, 0x22};
3712
Laurence Lundbladecc7da412020-12-27 00:09:07 -08003713// Same map as above, but using indefinite lengths
3714static const uint8_t spCSRInputIndefLen[] = {
3715 0xbf, 0x36, 0xbf, 0x33, 0xbf, 0x31, 0x6c, 0x4f,
3716 0x72, 0x67, 0x61, 0x6e, 0x69, 0x7a, 0x61, 0x74,
3717 0x69, 0x6f, 0x6e, 0x30, 0x63, 0x53, 0x53, 0x47,
3718 0x2e, 0x69, 0x43, 0x6f, 0x6e, 0x66, 0x75, 0x73,
3719 0x69, 0x6f, 0x6e, 0x2f, 0x69, 0x53, 0x61, 0x6e,
3720 0x20, 0x44, 0x69, 0x65, 0x67, 0x6f, 0x2d, 0x62,
3721 0x55, 0x53, 0xff, 0x32, 0xbf, 0x2a, 0xbf, 0x28,
3722 0x26, 0xff, 0x29, 0x4a, 0x01, 0x02, 0x03, 0x04,
3723 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0xff, 0xff,
3724 0x35, 0xbf, 0x24, 0x22, 0xff, 0xff};
3725
3726
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003727int32_t NestedMapTest()
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003728{
3729 QCBORDecodeContext DCtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003730
Laurence Lundbladeee851742020-01-08 08:37:05 -08003731 QCBORDecode_Init(&DCtx,
3732 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput),
3733 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003734
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003735 return CheckCSRMaps(&DCtx);
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003736}
3737
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08003738
3739
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003740int32_t StringDecoderModeFailTest()
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08003741{
3742 QCBORDecodeContext DCtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003743
Laurence Lundbladeee851742020-01-08 08:37:05 -08003744 QCBORDecode_Init(&DCtx,
3745 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput),
3746 QCBOR_DECODE_MODE_MAP_STRINGS_ONLY);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003747
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08003748 QCBORItem Item;
3749 QCBORError nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003750
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08003751 if(QCBORDecode_GetNext(&DCtx, &Item)) {
3752 return -1;
3753 }
3754 if(Item.uDataType != QCBOR_TYPE_MAP) {
3755 return -2;
3756 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003757
Laurence Lundbladeea567ac2018-12-09 14:03:21 -08003758 nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
3759 if(nCBORError != QCBOR_ERR_MAP_LABEL_TYPE) {
3760 return -3;
3761 }
3762
3763 return 0;
3764}
3765
3766
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003767
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003768int32_t NestedMapTestIndefLen()
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003769{
3770 QCBORDecodeContext DCtx;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003771
Laurence Lundbladeee851742020-01-08 08:37:05 -08003772 QCBORDecode_Init(&DCtx,
3773 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInputIndefLen),
3774 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003775
Laurence Lundblade742df4a2018-10-13 20:07:17 +08003776 return CheckCSRMaps(&DCtx);
3777}
3778
3779
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08003780
Laurence Lundblade17ede402018-10-13 11:43:07 +08003781static UsefulBufC make_nested_indefinite_arrays(int n, UsefulBuf Storage)
3782{
3783 UsefulOutBuf UOB;
3784 UsefulOutBuf_Init(&UOB, Storage);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003785
Laurence Lundblade17ede402018-10-13 11:43:07 +08003786 int i;
3787 for(i = 0; i < n; i++) {
3788 UsefulOutBuf_AppendByte(&UOB, 0x9f);
3789 }
3790
3791 for(i = 0; i < n; i++) {
3792 UsefulOutBuf_AppendByte(&UOB, 0xff);
3793 }
3794 return UsefulOutBuf_OutUBuf(&UOB);
3795}
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003796
3797
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003798static int32_t parse_indeflen_nested(UsefulBufC Nested, int nNestLevel)
Laurence Lundblade17ede402018-10-13 11:43:07 +08003799{
3800 QCBORDecodeContext DC;
3801 QCBORDecode_Init(&DC, Nested, 0);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003802
Laurence Lundblade17ede402018-10-13 11:43:07 +08003803 int j;
3804 for(j = 0; j < nNestLevel; j++) {
3805 QCBORItem Item;
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07003806 QCBORError nReturn = QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade17ede402018-10-13 11:43:07 +08003807 if(j >= QCBOR_MAX_ARRAY_NESTING) {
3808 // Should be in error
Laurence Lundbladea9489f82020-09-12 13:50:56 -07003809 if(nReturn != QCBOR_ERR_ARRAY_DECODE_NESTING_TOO_DEEP) {
Laurence Lundblade17ede402018-10-13 11:43:07 +08003810 return -4;
3811 } else {
3812 return 0; // Decoding doesn't recover after an error
3813 }
3814 } else {
3815 // Should be no error
3816 if(nReturn) {
3817 return -9; // Should not have got an error
3818 }
3819 }
3820 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
3821 return -7;
3822 }
3823 }
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07003824 QCBORError nReturn = QCBORDecode_Finish(&DC);
Laurence Lundblade17ede402018-10-13 11:43:07 +08003825 if(nReturn) {
3826 return -3;
3827 }
3828 return 0;
3829}
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003830
3831
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003832int32_t IndefiniteLengthNestTest()
Laurence Lundblade17ede402018-10-13 11:43:07 +08003833{
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05303834 UsefulBuf_MAKE_STACK_UB(Storage, 50);
Laurence Lundblade17ede402018-10-13 11:43:07 +08003835 int i;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003836 for(i=1; i < QCBOR_MAX_ARRAY_NESTING+4; i++) {
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08003837 const UsefulBufC Nested = make_nested_indefinite_arrays(i, Storage);
Laurence Lundblade17ede402018-10-13 11:43:07 +08003838 int nReturn = parse_indeflen_nested(Nested, i);
3839 if(nReturn) {
3840 return nReturn;
3841 }
3842 }
3843 return 0;
3844}
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003845
Laurence Lundbladeee851742020-01-08 08:37:05 -08003846// [1, [2, 3]]
3847static const uint8_t spIndefiniteArray[] = {0x9f, 0x01, 0x82, 0x02, 0x03, 0xff};
3848// No closing break
3849static const uint8_t spIndefiniteArrayBad1[] = {0x9f};
3850// Not enough closing breaks
3851static const uint8_t spIndefiniteArrayBad2[] = {0x9f, 0x9f, 0x02, 0xff};
3852// Too many closing breaks
3853static const uint8_t spIndefiniteArrayBad3[] = {0x9f, 0x02, 0xff, 0xff};
3854// Unclosed indeflen inside def len
3855static const uint8_t spIndefiniteArrayBad4[] = {0x81, 0x9f};
3856// confused tag
3857static const uint8_t spIndefiniteArrayBad5[] = {0x9f, 0xd1, 0xff};
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003858
Laurence Lundbladec5fef682020-01-25 11:38:45 -08003859int32_t IndefiniteLengthArrayMapTest()
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003860{
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07003861 QCBORError nResult;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003862 // --- first test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003863 UsefulBufC IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArray);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003864
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003865 // Decode it and see if it is OK
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003866 QCBORDecodeContext DC;
3867 QCBORItem Item;
3868 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003869
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003870 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05303871
3872 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
3873 Item.uNestingLevel != 0 ||
3874 Item.uNextNestLevel != 1) {
3875 return -111;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003876 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003877
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003878 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05303879 if(Item.uDataType != QCBOR_TYPE_INT64 ||
3880 Item.uNestingLevel != 1 ||
3881 Item.uNextNestLevel != 1) {
3882 return -2;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003883 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003884
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003885 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05303886 if(Item.uDataType != QCBOR_TYPE_ARRAY ||
3887 Item.uNestingLevel != 1 ||
3888 Item.uNextNestLevel != 2) {
3889 return -3;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003890 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003891
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003892 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade12b495d2018-12-17 11:15:54 -08003893 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade6de37062018-10-15 12:22:42 +05303894 Item.uNestingLevel != 2 ||
3895 Item.uNextNestLevel != 2) {
3896 return -4;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003897 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003898
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003899 QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade12b495d2018-12-17 11:15:54 -08003900 if(Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade6de37062018-10-15 12:22:42 +05303901 Item.uNestingLevel != 2 ||
3902 Item.uNextNestLevel != 0) {
3903 return -5;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003904 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003905
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003906 if(QCBORDecode_Finish(&DC)) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303907 return -6;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08003908 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003909
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003910 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003911 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad1);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003912
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003913 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003914
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003915 nResult = QCBORDecode_GetNext(&DC, &Item);
3916 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303917 return -7;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003918 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003919
Laurence Lundblade570fab52018-10-13 18:28:27 +08003920 nResult = QCBORDecode_Finish(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07003921 if(nResult != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303922 return -8;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003923 }
3924
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003925
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003926 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003927 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad2);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003928
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003929 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003930
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003931 nResult = QCBORDecode_GetNext(&DC, &Item);
3932 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303933 return -9;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003934 }
3935
3936 nResult = QCBORDecode_GetNext(&DC, &Item);
3937 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303938 return -10;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003939 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003940
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003941 nResult = QCBORDecode_GetNext(&DC, &Item);
3942 if(nResult || Item.uDataType != QCBOR_TYPE_INT64) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303943 return -11;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003944 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003945
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003946 nResult = QCBORDecode_Finish(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07003947 if(nResult != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303948 return -12;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003949 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003950
3951
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003952 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003953 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad3);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003954
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003955 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003956
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003957 nResult = QCBORDecode_GetNext(&DC, &Item);
3958 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303959 return -13;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003960 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003961
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003962 nResult = QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade642282a2020-06-23 12:00:33 -07003963 if(nResult != QCBOR_SUCCESS) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303964 return -14;
Laurence Lundblade19e0c802018-10-13 12:19:55 +08003965 }
Laurence Lundblade6de37062018-10-15 12:22:42 +05303966
Laurence Lundblade642282a2020-06-23 12:00:33 -07003967 nResult = QCBORDecode_GetNext(&DC, &Item);
3968 if(nResult != QCBOR_ERR_BAD_BREAK) {
3969 return -140;
3970 }
3971
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003972
Laurence Lundblade570fab52018-10-13 18:28:27 +08003973 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003974 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad4);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003975
Laurence Lundblade570fab52018-10-13 18:28:27 +08003976 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003977
Laurence Lundblade570fab52018-10-13 18:28:27 +08003978 nResult = QCBORDecode_GetNext(&DC, &Item);
3979 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303980 return -15;
Laurence Lundblade570fab52018-10-13 18:28:27 +08003981 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003982
Laurence Lundblade570fab52018-10-13 18:28:27 +08003983 nResult = QCBORDecode_GetNext(&DC, &Item);
3984 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303985 return -16;
Laurence Lundblade570fab52018-10-13 18:28:27 +08003986 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003987
Laurence Lundblade570fab52018-10-13 18:28:27 +08003988 nResult = QCBORDecode_Finish(&DC);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07003989 if(nResult != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05303990 return -17;
Laurence Lundblade570fab52018-10-13 18:28:27 +08003991 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003992
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303993 // --- next test -----
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07003994 IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteArrayBad5);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003995
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303996 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08003997
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05303998 nResult = QCBORDecode_GetNext(&DC, &Item);
3999 if(nResult || Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade6de37062018-10-15 12:22:42 +05304000 return -18;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304001 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004002
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304003 nResult = QCBORDecode_GetNext(&DC, &Item);
Laurence Lundblade6de37062018-10-15 12:22:42 +05304004 if(nResult != QCBOR_ERR_BAD_BREAK) {
4005 return -19;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304006 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004007
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08004008 return 0;
4009}
4010
Laurence Lundblade17ede402018-10-13 11:43:07 +08004011
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08004012#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
4013
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07004014static const uint8_t spIndefiniteLenString[] = {
Laurence Lundblade17ede402018-10-13 11:43:07 +08004015 0x81, // Array of length one
4016 0x7f, // text string marked with indefinite length
4017 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment
4018 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment
4019 0xff // ending break
4020};
4021
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07004022static const uint8_t spIndefiniteLenStringBad2[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304023 0x81, // Array of length one
4024 0x7f, // text string marked with indefinite length
4025 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment
4026 0x44, 0x6d, 0x69, 0x6e, 0x67, // second segment of wrong type
4027 0xff // ending break
4028};
4029
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07004030static const uint8_t spIndefiniteLenStringBad3[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304031 0x81, // Array of length one
4032 0x7f, // text string marked with indefinite length
4033 0x01, 0x02, // Not a string
4034 0xff // ending break
4035};
4036
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07004037static const uint8_t spIndefiniteLenStringBad4[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304038 0x81, // Array of length one
4039 0x7f, // text string marked with indefinite length
4040 0x65, 0x73, 0x74, 0x72, 0x65, 0x61, // first segment
4041 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment
4042 // missing end of string
4043};
4044
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07004045static const uint8_t spIndefiniteLenStringLabel[] = {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304046 0xa1, // Array of length one
4047 0x7f, // text string marked with indefinite length
4048 0x65, 0x73, 0x74, 0x72, 0x75, 0x75, // first segment
4049 0x64, 0x6d, 0x69, 0x6e, 0x67, // second segment
4050 0xff, // ending break
4051 0x01 // integer being labeled.
4052};
4053
Laurence Lundbladec5fef682020-01-25 11:38:45 -08004054/**
4055 Make an indefinite length string
4056
4057 @param Storage Storage for string, must be 144 bytes in size
4058 @return The indefinite length string
4059
4060 This makes an array with one indefinite length string that has 7 chunks
4061 from size of 1 byte up to 64 bytes.
4062 */
4063static UsefulBufC MakeIndefiniteBigBstr(UsefulBuf Storage)
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304064{
4065 UsefulOutBuf UOB;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004066
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304067 UsefulOutBuf_Init(&UOB, Storage);
4068 UsefulOutBuf_AppendByte(&UOB, 0x81);
4069 UsefulOutBuf_AppendByte(&UOB, 0x5f);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004070
Laurence Lundbladec5fef682020-01-25 11:38:45 -08004071 uint8_t uStringByte = 0;
4072 // Use of type int is intentional
4073 for(int uChunkSize = 1; uChunkSize <= 128; uChunkSize *= 2) {
4074 // Not using preferred encoding here, but that is OK.
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304075 UsefulOutBuf_AppendByte(&UOB, 0x58);
Laurence Lundbladec5fef682020-01-25 11:38:45 -08004076 UsefulOutBuf_AppendByte(&UOB, (uint8_t)uChunkSize);
4077 for(int j = 0; j < uChunkSize; j++) {
4078 UsefulOutBuf_AppendByte(&UOB, uStringByte);
4079 uStringByte++;
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304080 }
4081 }
4082 UsefulOutBuf_AppendByte(&UOB, 0xff);
4083
4084 return UsefulOutBuf_OutUBuf(&UOB);
4085}
4086
4087static int CheckBigString(UsefulBufC BigString)
4088{
4089 if(BigString.len != 255) {
4090 return 1;
4091 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004092
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304093 for(uint8_t i = 0; i < 255; i++){
4094 if(((const uint8_t *)BigString.ptr)[i] != i) {
4095 return 1;
4096 }
4097 }
4098 return 0;
4099}
4100
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05304101
Laurence Lundbladec5fef682020-01-25 11:38:45 -08004102int32_t IndefiniteLengthStringTest()
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304103{
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304104 QCBORDecodeContext DC;
4105 QCBORItem Item;
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05304106 // big enough for MakeIndefiniteBigBstr() + MemPool overhead
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004107 UsefulBuf_MAKE_STACK_UB(MemPool, 350);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004108
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304109 // --- Simple normal indefinite length string ------
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07004110 UsefulBufC IndefLen = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenString);
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304111 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004112
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05304113 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304114 return -1;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304115 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004116
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304117 if(QCBORDecode_GetNext(&DC, &Item)) {
4118 return -2;
4119 }
4120 if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.uDataAlloc) {
4121 return -3;
4122 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004123
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304124 if(QCBORDecode_GetNext(&DC, &Item)) {
4125 return -4;
4126 }
4127 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING || !Item.uDataAlloc) {
4128 return -5;
4129 }
4130 if(QCBORDecode_Finish(&DC)) {
4131 return -6;
4132 }
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304133
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304134 // ----- types mismatch ---
Laurence Lundbladeee851742020-01-08 08:37:05 -08004135 QCBORDecode_Init(&DC,
4136 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad2),
4137 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004138
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304139 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
4140 return -7;
4141 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004142
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304143 if(QCBORDecode_GetNext(&DC, &Item)) {
4144 return -8;
4145 }
4146 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
4147 return -9;
4148 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004149
Laurence Lundblade30816f22018-11-10 13:40:22 +07004150 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_INDEFINITE_STRING_CHUNK) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304151 return -10;
4152 }
4153
4154 // ----- not a string ---
Laurence Lundbladeee851742020-01-08 08:37:05 -08004155 QCBORDecode_Init(&DC,
4156 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad3),
4157 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004158
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304159 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
4160 return -11;
4161 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004162
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304163 if(QCBORDecode_GetNext(&DC, &Item)) {
4164 return -12;
4165 }
4166 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
4167 return -13;
4168 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004169
Laurence Lundblade30816f22018-11-10 13:40:22 +07004170 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_INDEFINITE_STRING_CHUNK) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304171 return -14;
4172 }
4173
4174 // ----- no end -----
Laurence Lundbladeee851742020-01-08 08:37:05 -08004175 QCBORDecode_Init(&DC,
4176 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringBad4),
4177 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004178
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304179 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
4180 return -15;
4181 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004182
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304183 if(QCBORDecode_GetNext(&DC, &Item)) {
4184 return -16;
4185 }
4186 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
4187 return -17;
4188 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004189
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304190 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_HIT_END) {
4191 return -18;
4192 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004193
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304194 // ------ Don't set a string allocator and see an error -----
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304195 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004196
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304197 QCBORDecode_GetNext(&DC, &Item);
4198 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304199 return -19;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304200 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004201
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304202 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_NO_STRING_ALLOCATOR) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304203 return -20;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304204 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004205
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304206 // ----- Mempool is way too small -----
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004207 UsefulBuf_MAKE_STACK_UB(MemPoolTooSmall, QCBOR_DECODE_MIN_MEM_POOL_SIZE-1);
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304208
4209 QCBORDecode_Init(&DC, IndefLen, QCBOR_DECODE_MODE_NORMAL);
4210 if(!QCBORDecode_SetMemPool(&DC, MemPoolTooSmall, false)) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304211 return -21;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304212 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004213
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304214 // ----- Mempool is way too small -----
Laurence Lundblade4fe9f312018-10-22 10:22:39 +05304215 UsefulBuf_MAKE_STACK_UB(BigIndefBStrStorage, 290);
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08004216 const UsefulBufC BigIndefBStr = MakeIndefiniteBigBstr(BigIndefBStrStorage);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004217
Laurence Lundbladeee851742020-01-08 08:37:05 -08004218 // 80 is big enough for MemPool overhead, but not BigIndefBStr
4219 UsefulBuf_MAKE_STACK_UB(MemPoolSmall, 80);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004220
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304221 QCBORDecode_Init(&DC, BigIndefBStr, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304222 if(QCBORDecode_SetMemPool(&DC, MemPoolSmall, false)) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304223 return -22;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304224 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004225
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304226 QCBORDecode_GetNext(&DC, &Item);
4227 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304228 return -23;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304229 }
Laurence Lundblade30816f22018-11-10 13:40:22 +07004230 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_ERR_STRING_ALLOCATE) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304231 return -24;
Laurence Lundblade5b8c5852018-10-14 21:11:42 +05304232 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004233
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304234 // ---- big bstr -----
4235 QCBORDecode_Init(&DC, BigIndefBStr, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004236
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304237 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
4238 return -25;
4239 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004240
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05304241 if(QCBORDecode_GetNext(&DC, &Item)) {
4242 return -26;
4243 }
4244 if(Item.uDataType != QCBOR_TYPE_ARRAY || Item.uDataAlloc) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304245 return -26;
4246 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004247
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304248 if(QCBORDecode_GetNext(&DC, &Item)) {
4249 return -27;
4250 }
Laurence Lundblade7e0d13b2018-10-16 19:54:13 +05304251 if(Item.uDataType != QCBOR_TYPE_BYTE_STRING || !Item.uDataAlloc || Item.uNestingLevel != 1) {
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304252 return -28;
4253 }
4254 if(CheckBigString(Item.val.string)) {
4255 return -3;
4256 }
4257 if(QCBORDecode_Finish(&DC)) {
4258 return -29;
4259 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004260
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304261 // --- label is an indefinite length string ------
Laurence Lundbladeb836efb2018-10-28 20:09:58 +07004262 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spIndefiniteLenStringLabel), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004263
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304264 if(QCBORDecode_SetMemPool(&DC, MemPool, false)) {
4265 return -30;
4266 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004267
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304268 QCBORDecode_GetNext(&DC, &Item);
4269 if(Item.uDataType != QCBOR_TYPE_MAP) {
4270 return -31;
4271 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004272
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304273 if(QCBORDecode_GetNext(&DC, &Item)){
4274 return -32;
4275 }
Laurence Lundbladeee851742020-01-08 08:37:05 -08004276 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
4277 Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304278 Item.uDataAlloc || !Item.uLabelAlloc ||
4279 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("struuming"))) {
4280 return -33;
4281 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004282
Laurence Lundblade57dd1442018-10-15 20:26:28 +05304283 if(QCBORDecode_Finish(&DC)) {
4284 return -34;
4285 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004286
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004287 return 0;
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08004288}
4289
4290
Laurence Lundbladec5fef682020-01-25 11:38:45 -08004291int32_t AllocAllStringsTest()
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304292{
4293 QCBORDecodeContext DC;
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004294 QCBORError nCBORError;
4295
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004296
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304297 // First test, use the "CSRMap" as easy input and checking
Laurence Lundbladeee851742020-01-08 08:37:05 -08004298 QCBORDecode_Init(&DC,
4299 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spCSRInput),
4300 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004301
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004302 UsefulBuf_MAKE_STACK_UB(Pool, sizeof(spCSRInput) + QCBOR_DECODE_MIN_MEM_POOL_SIZE);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004303
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004304 nCBORError = QCBORDecode_SetMemPool(&DC, Pool, 1); // Turn on copying.
4305 if(nCBORError) {
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304306 return -1;
4307 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004308
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004309 if(CheckCSRMaps(&DC)) {
4310 return -2;
4311 }
4312
Laurence Lundblade2f467f92020-10-09 17:50:11 -07004313 // Next parse, save pointers to a few strings, destroy original and
4314 // see all is OK.
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004315 UsefulBuf_MAKE_STACK_UB(CopyOfStorage, sizeof(pValidMapEncoded) + QCBOR_DECODE_MIN_MEM_POOL_SIZE);
Laurence Lundblade25c6c0a2018-12-17 13:21:59 -08004316 const UsefulBufC CopyOf = UsefulBuf_Copy(CopyOfStorage, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded));
Laurence Lundblade4d1ecba2018-10-12 21:22:30 +08004317
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304318 QCBORDecode_Init(&DC, CopyOf, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade1f8b5b02019-01-01 22:27:38 -08004319 UsefulBuf_Set(Pool, '/');
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304320 QCBORDecode_SetMemPool(&DC, Pool, 1); // Turn on copying.
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004321
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304322 QCBORItem Item1, Item2, Item3, Item4;
4323 if((nCBORError = QCBORDecode_GetNext(&DC, &Item1)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07004324 return (int32_t)nCBORError;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304325 if(Item1.uDataType != QCBOR_TYPE_MAP ||
4326 Item1.val.uCount != 3)
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004327 return -3;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304328 if((nCBORError = QCBORDecode_GetNext(&DC, &Item1)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07004329 return (int32_t)nCBORError;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304330 if((nCBORError = QCBORDecode_GetNext(&DC, &Item2)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07004331 return (int32_t)nCBORError;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304332 if((nCBORError = QCBORDecode_GetNext(&DC, &Item3)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07004333 return (int32_t)nCBORError;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304334 if((nCBORError = QCBORDecode_GetNext(&DC, &Item4)))
Laurence Lundbladee6bcef12020-04-01 10:56:27 -07004335 return (int32_t)nCBORError;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004336
Laurence Lundblade05ec57b2018-10-21 01:50:03 +05304337 UsefulBuf_Set(CopyOfStorage, '_');
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004338
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304339 if(Item1.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304340 Item1.uDataType != QCBOR_TYPE_INT64 ||
4341 Item1.val.int64 != 42 ||
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004342 Item1.uDataAlloc != 0 ||
4343 Item1.uLabelAlloc == 0 ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07004344 UsefulBufCompareToSZ(Item1.label.string, "first integer")) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004345 return -4;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09004346 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004347
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304348
4349 if(Item2.uLabelType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07004350 UsefulBufCompareToSZ(Item2.label.string, "an array of two strings") ||
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304351 Item2.uDataType != QCBOR_TYPE_ARRAY ||
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004352 Item2.uDataAlloc != 0 ||
4353 Item2.uLabelAlloc == 0 ||
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304354 Item2.val.uCount != 2)
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004355 return -5;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004356
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304357 if(Item3.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004358 Item3.uDataAlloc == 0 ||
4359 Item3.uLabelAlloc != 0 ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07004360 UsefulBufCompareToSZ(Item3.val.string, "string1")) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004361 return -6;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09004362 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004363
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304364 if(Item4.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004365 Item4.uDataAlloc == 0 ||
4366 Item4.uLabelAlloc != 0 ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07004367 UsefulBufCompareToSZ(Item4.val.string, "string2")) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004368 return -7;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09004369 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004370
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304371 // Next parse with a pool that is too small
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004372 UsefulBuf_MAKE_STACK_UB(SmallPool, QCBOR_DECODE_MIN_MEM_POOL_SIZE + 1);
Laurence Lundbladeee851742020-01-08 08:37:05 -08004373 QCBORDecode_Init(&DC,
4374 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded),
4375 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304376 QCBORDecode_SetMemPool(&DC, SmallPool, 1); // Turn on copying.
4377 if((nCBORError = QCBORDecode_GetNext(&DC, &Item1)))
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004378 return -8;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304379 if(Item1.uDataType != QCBOR_TYPE_MAP ||
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09004380 Item1.val.uCount != 3) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004381 return -9;
Laurence Lundbladeccfb8cd2018-12-07 21:11:30 +09004382 }
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304383 if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item1))){
4384 if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item2))) {
4385 if(!(nCBORError = QCBORDecode_GetNext(&DC, &Item3))) {
4386 nCBORError = QCBORDecode_GetNext(&DC, &Item4);
4387 }
4388 }
4389 }
Laurence Lundblade30816f22018-11-10 13:40:22 +07004390 if(nCBORError != QCBOR_ERR_STRING_ALLOCATE) {
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004391 return -10;
Laurence Lundbladea44d5062018-10-17 18:45:12 +05304392 }
4393
4394 return 0;
4395}
4396
Laurence Lundbladef6531662018-12-04 10:42:22 +09004397
Laurence Lundbladec5fef682020-01-25 11:38:45 -08004398int32_t MemPoolTest(void)
Laurence Lundblade0155b622018-10-12 20:04:37 +08004399{
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004400 // Set up the decoder with a tiny bit of CBOR to parse because
4401 // nothing can be done with it unless that is set up.
Laurence Lundbladef6531662018-12-04 10:42:22 +09004402 QCBORDecodeContext DC;
4403 const uint8_t pMinimalCBOR[] = {0xa0}; // One empty map
4404 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pMinimalCBOR),0);
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004405
Laurence Lundbladef6531662018-12-04 10:42:22 +09004406 // Set up an memory pool of 100 bytes
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004407 // Then fish into the internals of the decode context
4408 // to get the allocator function so it can be called directly.
4409 // Also figure out how much pool is available for use
4410 // buy subtracting out the overhead.
Laurence Lundbladef6531662018-12-04 10:42:22 +09004411 UsefulBuf_MAKE_STACK_UB(Pool, 100);
Laurence Lundbladedf2836b2018-12-19 18:13:29 -08004412 QCBORError nError = QCBORDecode_SetMemPool(&DC, Pool, 0);
4413 if(nError) {
4414 return -9;
4415 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004416 QCBORStringAllocate pAlloc = DC.StringAllocator.pfAllocator;
4417 void *pAllocCtx = DC.StringAllocator.pAllocateCxt;
4418 size_t uAvailPool = Pool.len - QCBOR_DECODE_MIN_MEM_POOL_SIZE;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004419
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004420 // First test -- ask for one more byte than available and see failure
4421 UsefulBuf Allocated = (*pAlloc)(pAllocCtx, NULL, uAvailPool+1);
Laurence Lundbladef6531662018-12-04 10:42:22 +09004422 if(!UsefulBuf_IsNULL(Allocated)) {
4423 return -1;
4424 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004425
Laurence Lundbladef6531662018-12-04 10:42:22 +09004426 // Re do the set up for the next test that will do a successful alloc,
4427 // a fail, a free and then success
Laurence Lundbladef6531662018-12-04 10:42:22 +09004428 QCBORDecode_SetMemPool(&DC, Pool, 0);
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004429 pAlloc = DC.StringAllocator.pfAllocator;
4430 pAllocCtx = DC.StringAllocator.pAllocateCxt;
4431 uAvailPool = Pool.len - QCBOR_DECODE_MIN_MEM_POOL_SIZE;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004432
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004433 // Allocate one byte less than available and see success
4434 Allocated = (pAlloc)(pAllocCtx, NULL, uAvailPool-1);
Laurence Lundbladef6531662018-12-04 10:42:22 +09004435 if(UsefulBuf_IsNULL(Allocated)) { // expected to succeed
4436 return -2;
4437 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004438 // Ask for some more and see failure
4439 UsefulBuf Allocated2 = (*pAlloc)(pAllocCtx, NULL, uAvailPool/2);
Laurence Lundbladef6531662018-12-04 10:42:22 +09004440 if(!UsefulBuf_IsNULL(Allocated2)) { // expected to fail
4441 return -3;
4442 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004443 // Free the first allocate, retry the second and see success
4444 (*pAlloc)(pAllocCtx, Allocated.ptr, 0); // Free
4445 Allocated = (*pAlloc)(pAllocCtx, NULL, uAvailPool/2);
Laurence Lundbladef6531662018-12-04 10:42:22 +09004446 if(UsefulBuf_IsNULL(Allocated)) { // succeed because of the free
4447 return -4;
4448 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004449
Laurence Lundbladef6531662018-12-04 10:42:22 +09004450 // Re do set up for next test that involves a successful alloc,
4451 // and a successful realloc and a failed realloc
4452 QCBORDecode_SetMemPool(&DC, Pool, 0);
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004453 pAlloc = DC.StringAllocator.pfAllocator;
4454 pAllocCtx = DC.StringAllocator.pAllocateCxt;
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004455
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004456 // Allocate half the pool and see success
4457 Allocated = (*pAlloc)(pAllocCtx, NULL, uAvailPool/2);
Laurence Lundbladef6531662018-12-04 10:42:22 +09004458 if(UsefulBuf_IsNULL(Allocated)) { // expected to succeed
4459 return -5;
4460 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004461 // Reallocate to take up the whole pool and see success
4462 Allocated2 = (*pAlloc)(pAllocCtx, Allocated.ptr, uAvailPool);
Laurence Lundbladef6531662018-12-04 10:42:22 +09004463 if(UsefulBuf_IsNULL(Allocated2)) {
4464 return -6;
4465 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004466 // Make sure its the same pointer and the size is right
Laurence Lundbladef6531662018-12-04 10:42:22 +09004467 if(Allocated2.ptr != Allocated.ptr || Allocated2.len != uAvailPool) {
4468 return -7;
4469 }
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004470 // Try to allocate more to be sure there is failure after a realloc
4471 UsefulBuf Allocated3 = (*pAlloc)(pAllocCtx, Allocated.ptr, uAvailPool+1);
4472 if(!UsefulBuf_IsNULL(Allocated3)) {
Laurence Lundbladef6531662018-12-04 10:42:22 +09004473 return -8;
4474 }
Laurence Lundblade3aee3a32018-12-17 16:17:45 -08004475
Laurence Lundbladef6531662018-12-04 10:42:22 +09004476 return 0;
4477}
Laurence Lundblade2ded3d92018-10-09 21:36:11 +08004478
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004479
4480/* Just enough of an allocator to test configuration of one */
4481static UsefulBuf AllocateTestFunction(void *pCtx, void *pOldMem, size_t uNewSize)
4482{
4483 (void)pOldMem; // unused variable
4484
4485 if(uNewSize) {
4486 // Assumes the context pointer is the buffer and
4487 // nothing too big will ever be asked for.
4488 // This is only good for this basic test!
4489 return (UsefulBuf) {pCtx, uNewSize};
4490 } else {
4491 return NULLUsefulBuf;
4492 }
4493}
4494
4495
Laurence Lundbladec5fef682020-01-25 11:38:45 -08004496int32_t SetUpAllocatorTest(void)
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004497{
4498 // Set up the decoder with a tiny bit of CBOR to parse because
4499 // nothing can be done with it unless that is set up.
4500 QCBORDecodeContext DC;
4501 const uint8_t pMinimalCBOR[] = {0x62, 0x48, 0x69}; // "Hi"
4502 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pMinimalCBOR),0);
4503
4504 uint8_t pAllocatorBuffer[50];
4505
4506 // This is really just to test that this call works.
4507 // The full functionality of string allocators is tested
4508 // elsewhere with the MemPool internal allocator.
4509 QCBORDecode_SetUpAllocator(&DC, AllocateTestFunction, pAllocatorBuffer, 1);
4510
4511 QCBORItem Item;
4512 if(QCBORDecode_GetNext(&DC, &Item) != QCBOR_SUCCESS) {
4513 return -1;
4514 }
4515
4516 if(Item.uDataAlloc == 0 ||
4517 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
4518 Item.val.string.ptr != pAllocatorBuffer) {
4519 return -2;
4520 }
4521
4522 if(QCBORDecode_Finish(&DC) != QCBOR_SUCCESS) {
4523 return -3;
4524 }
4525
4526 return 0;
4527}
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08004528#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
4529
Laurence Lundblade1d7eb632019-02-17 17:23:38 -08004530
Laurence Lundbladedd6e76e2021-03-10 01:54:01 -07004531#ifndef QCBOR_DISABLE_EXP_AND_MANTISSA
Laurence Lundblade59289e52019-12-30 13:44:37 -08004532
Laurence Lundbladea826c502020-05-10 21:07:00 -07004533/* exponent, mantissa
Laurence Lundblade59289e52019-12-30 13:44:37 -08004534 [
4535 4([-1, 3]),
Laurence Lundbladea826c502020-05-10 21:07:00 -07004536 4([-20, 4759477275222530853136]),
4537 4([9223372036854775807, -4759477275222530853137]),
Laurence Lundblade59289e52019-12-30 13:44:37 -08004538 5([300, 100]),
Laurence Lundbladea826c502020-05-10 21:07:00 -07004539 5([-20, 4759477275222530853136]),
Laurence Lundblade59289e52019-12-30 13:44:37 -08004540 5([-9223372036854775807, -4759477275222530853137])
Laurence Lundbladea826c502020-05-10 21:07:00 -07004541 5([ 9223372036854775806, -4759477275222530853137])
4542 5([ 9223372036854775806, 9223372036854775806])]
Laurence Lundbladecc7da412020-12-27 00:09:07 -08004543 ]
Laurence Lundblade59289e52019-12-30 13:44:37 -08004544 */
Laurence Lundblade59289e52019-12-30 13:44:37 -08004545static const uint8_t spExpectedExponentsAndMantissas[] = {
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004546 0x88,
Laurence Lundblade59289e52019-12-30 13:44:37 -08004547 0xC4, 0x82, 0x20,
4548 0x03,
4549 0xC4, 0x82, 0x33,
4550 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,
4551 0xC4, 0x82, 0x1B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
4552 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,
4553 0xC5, 0x82, 0x19, 0x01, 0x2C,
4554 0x18, 0x64,
4555 0xC5, 0x82, 0x33,
4556 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,
4557 0xC5, 0x82, 0x3B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE,
4558 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,
Laurence Lundbladea826c502020-05-10 21:07:00 -07004559 0xC5, 0x82, 0x1B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE,
4560 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,
Laurence Lundblade59289e52019-12-30 13:44:37 -08004561 0xC5, 0x82, 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE,
4562 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE
4563};
4564
Laurence Lundbladefaec39f2020-08-02 21:53:53 -07004565
Laurence Lundbladec5fef682020-01-25 11:38:45 -08004566int32_t ExponentAndMantissaDecodeTests(void)
Laurence Lundblade59289e52019-12-30 13:44:37 -08004567{
4568 QCBORDecodeContext DC;
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004569 QCBORError uErr;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004570 QCBORItem item;
4571
Laurence Lundblade17af4902020-01-07 19:11:55 -08004572 static const uint8_t spBigNumMantissa[] = {0x01, 0x02, 0x03, 0x04, 0x05,
4573 0x06, 0x07, 0x08, 0x09, 0x010};
4574 UsefulBufC BN = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBigNumMantissa);
Laurence Lundblade59289e52019-12-30 13:44:37 -08004575
4576
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004577 QCBORDecode_Init(&DC,
4578 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedExponentsAndMantissas),
4579 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade59289e52019-12-30 13:44:37 -08004580
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004581 uErr = QCBORDecode_GetNext(&DC, &item);
4582 if(uErr != QCBOR_SUCCESS) {
Laurence Lundblade59289e52019-12-30 13:44:37 -08004583 return 1;
4584 }
4585
4586 if(item.uDataType != QCBOR_TYPE_ARRAY) {
4587 return 2;
4588 }
4589
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004590 uErr = QCBORDecode_GetNext(&DC, &item);
4591 if(uErr != QCBOR_SUCCESS) {
Laurence Lundblade59289e52019-12-30 13:44:37 -08004592 return 3;
4593 }
4594
4595 if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION ||
4596 item.val.expAndMantissa.Mantissa.nInt != 3 ||
4597 item.val.expAndMantissa.nExponent != -1) {
4598 return 4;
4599 }
4600
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004601 uErr = QCBORDecode_GetNext(&DC, &item);
4602 if(uErr != QCBOR_SUCCESS) {
Laurence Lundblade59289e52019-12-30 13:44:37 -08004603 return 5;
4604 }
4605
4606 if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM ||
4607 item.val.expAndMantissa.nExponent != -20 ||
4608 UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) {
4609 return 6;
4610 }
4611
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004612 uErr = QCBORDecode_GetNext(&DC, &item);
4613 if(uErr != QCBOR_SUCCESS) {
Laurence Lundblade59289e52019-12-30 13:44:37 -08004614 return 7;
4615 }
4616
4617 if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION_NEG_BIGNUM ||
4618 item.val.expAndMantissa.nExponent != 9223372036854775807 ||
4619 UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) {
4620 return 8;
4621 }
4622
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004623 uErr = QCBORDecode_GetNext(&DC, &item);
4624 if(uErr != QCBOR_SUCCESS) {
Laurence Lundblade59289e52019-12-30 13:44:37 -08004625 return 9;
4626 }
4627
4628 if(item.uDataType != QCBOR_TYPE_BIGFLOAT ||
4629 item.val.expAndMantissa.Mantissa.nInt != 100 ||
4630 item.val.expAndMantissa.nExponent != 300) {
4631 return 10;
4632 }
4633
Laurence Lundbladea826c502020-05-10 21:07:00 -07004634 // 5([-20, 4759477275222530853136]),
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004635 uErr = QCBORDecode_GetNext(&DC, &item);
4636 if(uErr != QCBOR_SUCCESS) {
Laurence Lundblade59289e52019-12-30 13:44:37 -08004637 return 11;
4638 }
Laurence Lundblade59289e52019-12-30 13:44:37 -08004639 if(item.uDataType != QCBOR_TYPE_BIGFLOAT_POS_BIGNUM ||
4640 item.val.expAndMantissa.nExponent != -20 ||
4641 UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) {
4642 return 12;
4643 }
4644
Laurence Lundbladea826c502020-05-10 21:07:00 -07004645 // 5([-9223372036854775807, -4759477275222530853137])
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004646 uErr = QCBORDecode_GetNext(&DC, &item);
4647 if(uErr != QCBOR_SUCCESS) {
Laurence Lundblade59289e52019-12-30 13:44:37 -08004648 return 13;
4649 }
Laurence Lundblade59289e52019-12-30 13:44:37 -08004650 if(item.uDataType != QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM ||
4651 item.val.expAndMantissa.nExponent != -9223372036854775807 ||
4652 UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) {
4653 return 14;
4654 }
4655
Laurence Lundbladea826c502020-05-10 21:07:00 -07004656 // 5([ 9223372036854775806, -4759477275222530853137])
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004657 uErr = QCBORDecode_GetNext(&DC, &item);
4658 if(uErr != QCBOR_SUCCESS) {
4659 return 15;
Laurence Lundbladea826c502020-05-10 21:07:00 -07004660 }
4661 if(item.uDataType != QCBOR_TYPE_BIGFLOAT_NEG_BIGNUM ||
4662 item.val.expAndMantissa.nExponent != 9223372036854775806 ||
4663 UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) {
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004664 return 16;
Laurence Lundbladea826c502020-05-10 21:07:00 -07004665 }
4666
Laurence Lundbladea826c502020-05-10 21:07:00 -07004667 // 5([ 9223372036854775806, 9223372036854775806])]
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004668 uErr = QCBORDecode_GetNext(&DC, &item);
4669 if(uErr != QCBOR_SUCCESS) {
4670 return 17;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004671 }
Laurence Lundblade59289e52019-12-30 13:44:37 -08004672 if(item.uDataType != QCBOR_TYPE_BIGFLOAT ||
4673 item.val.expAndMantissa.nExponent != 9223372036854775806 ||
4674 item.val.expAndMantissa.Mantissa.nInt!= 9223372036854775806 ) {
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004675 return 18;
4676 }
4677
4678 uErr = QCBORDecode_Finish(&DC);
4679 if(uErr != QCBOR_SUCCESS) {
4680 return 18;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004681 }
4682
4683 /* Now encode some stuff and then decode it */
4684 uint8_t pBuf[40];
4685 QCBOREncodeContext EC;
4686 UsefulBufC Encoded;
4687
4688 QCBOREncode_Init(&EC, UsefulBuf_FROM_BYTE_ARRAY(pBuf));
4689 QCBOREncode_OpenArray(&EC);
4690 QCBOREncode_AddDecimalFraction(&EC, 999, 1000); // 999 * (10 ^ 1000)
4691 QCBOREncode_AddBigFloat(&EC, 100, INT32_MIN);
4692 QCBOREncode_AddDecimalFractionBigNum(&EC, BN, false, INT32_MAX);
4693 QCBOREncode_CloseArray(&EC);
4694 QCBOREncode_Finish(&EC, &Encoded);
4695
4696
4697 QCBORDecode_Init(&DC, Encoded, QCBOR_DECODE_MODE_NORMAL);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004698 uErr = QCBORDecode_GetNext(&DC, &item);
4699 if(uErr != QCBOR_SUCCESS) {
4700 return 100;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004701 }
4702
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004703 uErr = QCBORDecode_GetNext(&DC, &item);
4704 if(uErr != QCBOR_SUCCESS) {
4705 return 101;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004706 }
4707
4708 if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION ||
4709 item.val.expAndMantissa.nExponent != 1000 ||
4710 item.val.expAndMantissa.Mantissa.nInt != 999) {
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004711 return 102;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004712 }
4713
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004714 uErr = QCBORDecode_GetNext(&DC, &item);
4715 if(uErr != QCBOR_SUCCESS) {
4716 return 103;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004717 }
4718
4719 if(item.uDataType != QCBOR_TYPE_BIGFLOAT ||
4720 item.val.expAndMantissa.nExponent != INT32_MIN ||
4721 item.val.expAndMantissa.Mantissa.nInt != 100) {
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004722 return 104;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004723 }
4724
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004725 uErr = QCBORDecode_GetNext(&DC, &item);
4726 if(uErr != QCBOR_SUCCESS) {
4727 return 105;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004728 }
4729
4730 if(item.uDataType != QCBOR_TYPE_DECIMAL_FRACTION_POS_BIGNUM ||
4731 item.val.expAndMantissa.nExponent != INT32_MAX ||
4732 UsefulBuf_Compare(item.val.expAndMantissa.Mantissa.bigNum, BN)) {
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004733 return 106;
4734 }
4735
4736
4737 int64_t nExp, nMant;
4738 UsefulBuf_MAKE_STACK_UB( MantBuf, 20);
4739 UsefulBufC Mant;
4740 bool bIsNeg;
4741
4742 QCBORDecode_Init(&DC,
4743 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spExpectedExponentsAndMantissas),
4744 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07004745 QCBORDecode_EnterArray(&DC, NULL);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004746
4747 // 4([-1, 3]),
Laurence Lundblade9b334962020-08-27 10:55:53 -07004748 QCBORDecode_GetDecimalFraction(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nExp, &nMant);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004749
4750 // 4([-20, 4759477275222530853136]),
Laurence Lundblade9b334962020-08-27 10:55:53 -07004751 QCBORDecode_GetDecimalFractionBig(&DC, QCBOR_TAG_REQUIREMENT_TAG, MantBuf,
4752 &Mant, &bIsNeg, &nExp);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004753
4754 // 4([9223372036854775807, -4759477275222530853137]),
Laurence Lundblade9b334962020-08-27 10:55:53 -07004755 QCBORDecode_GetDecimalFractionBig(&DC, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG,
4756 MantBuf, &Mant, &bIsNeg, &nExp);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004757
4758 // 5([300, 100]),
Laurence Lundblade9b334962020-08-27 10:55:53 -07004759 QCBORDecode_GetBigFloat(&DC, QCBOR_TAG_REQUIREMENT_TAG, &nExp, &nMant);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004760
4761 // 5([-20, 4759477275222530853136]),
Laurence Lundblade9b334962020-08-27 10:55:53 -07004762 QCBORDecode_GetBigFloatBig(&DC, QCBOR_TAG_REQUIREMENT_TAG, MantBuf, &Mant,
4763 &bIsNeg, &nExp);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004764
4765 // 5([-9223372036854775807, -4759477275222530853137])
Laurence Lundblade9b334962020-08-27 10:55:53 -07004766 QCBORDecode_GetBigFloatBig(&DC, QCBOR_TAG_REQUIREMENT_TAG, MantBuf, &Mant,
4767 &bIsNeg, &nExp);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004768
4769 // 5([ 9223372036854775806, -4759477275222530853137])
Laurence Lundblade9b334962020-08-27 10:55:53 -07004770 QCBORDecode_GetBigFloatBig(&DC, QCBOR_TAG_REQUIREMENT_TAG, MantBuf, &Mant,
4771 &bIsNeg, &nExp);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004772
4773 // 5([ 9223372036854775806, 9223372036854775806])]
Laurence Lundblade9b334962020-08-27 10:55:53 -07004774 QCBORDecode_GetBigFloatBig(&DC, QCBOR_TAG_REQUIREMENT_TAG, MantBuf, &Mant,
4775 &bIsNeg, &nExp);
Laurence Lundbladeb3683da2020-08-02 17:44:54 -07004776
4777 QCBORDecode_ExitArray(&DC);
4778
4779 uErr = QCBORDecode_Finish(&DC);
4780 if(uErr != QCBOR_SUCCESS) {
4781 return 200;
Laurence Lundblade59289e52019-12-30 13:44:37 -08004782 }
4783
4784 return 0;
4785}
4786
4787
Laurence Lundbladecc7da412020-12-27 00:09:07 -08004788static const struct FailInput ExponentAndMantissaFailures[] = {
Laurence Lundblade59289e52019-12-30 13:44:37 -08004789 // Exponent > INT64_MAX
4790 { {(uint8_t[]){0xC4, 0x82, 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
4791 0xFF, 0xFF, 0x1B, 0x80, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
4792 0xFF, 0xFF,}, 20}, QCBOR_ERR_BAD_EXP_AND_MANTISSA},
4793 // Mantissa > INT64_MAX
4794 { {(uint8_t[]){0xC4, 0x82, 0x1B, 0x80, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
4795 0xFF, 0xFF, 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05,
4796 0x06, 0x07, 0x08, 0x09, 0x10}, 23}, QCBOR_ERR_BAD_EXP_AND_MANTISSA},
4797 // End of input
Laurence Lundbladee6f15112020-07-23 18:44:16 -07004798 { {(uint8_t[]){0xC4, 0x82}, 2}, QCBOR_ERR_NO_MORE_ITEMS},
Laurence Lundblade59289e52019-12-30 13:44:37 -08004799 // End of input
Laurence Lundbladee6f15112020-07-23 18:44:16 -07004800 { {(uint8_t[]){0xC4, 0x82, 0x01}, 3}, QCBOR_ERR_NO_MORE_ITEMS},
Laurence Lundblade59289e52019-12-30 13:44:37 -08004801 // bad content for big num
4802 { {(uint8_t[]){0xC4, 0x82, 0x01, 0xc3, 0x01}, 5}, QCBOR_ERR_BAD_OPT_TAG},
4803 // bad content for big num
4804 { {(uint8_t[]){0xC4, 0x82, 0xc2, 0x01, 0x1f}, 5}, QCBOR_ERR_BAD_INT},
4805 // Bad integer for exponent
4806 { {(uint8_t[]){0xC4, 0x82, 0x01, 0x1f}, 4}, QCBOR_ERR_BAD_INT},
4807 // Bad integer for mantissa
4808 { {(uint8_t[]){0xC4, 0x82, 0x1f, 0x01}, 4}, QCBOR_ERR_BAD_INT},
4809 // 3 items in array
4810 { {(uint8_t[]){0xC4, 0x83, 0x03, 0x01, 02}, 5}, QCBOR_ERR_BAD_EXP_AND_MANTISSA},
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08004811#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundblade59289e52019-12-30 13:44:37 -08004812 // unterminated indefinite length array
4813 { {(uint8_t[]){0xC4, 0x9f, 0x03, 0x01, 0x02}, 5}, QCBOR_ERR_BAD_EXP_AND_MANTISSA},
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08004814#else /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
4815 // unterminated indefinite length array
4816 { {(uint8_t[]){0xC4, 0x9f, 0x03, 0x01, 0x02}, 5}, QCBOR_ERR_INDEF_LEN_ARRAYS_DISABLED},
4817#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
Laurence Lundblade59289e52019-12-30 13:44:37 -08004818 // Empty array
4819 { {(uint8_t[]){0xC4, 0x80}, 2}, QCBOR_ERR_NO_MORE_ITEMS},
4820 // Second is not an integer
4821 { {(uint8_t[]){0xC4, 0x82, 0x03, 0x40}, 4}, QCBOR_ERR_BAD_EXP_AND_MANTISSA},
4822 // First is not an integer
4823 { {(uint8_t[]){0xC4, 0x82, 0x40}, 3}, QCBOR_ERR_BAD_EXP_AND_MANTISSA},
4824 // Not an array
4825 { {(uint8_t[]){0xC4, 0xa2}, 2}, QCBOR_ERR_BAD_EXP_AND_MANTISSA}
4826};
4827
4828
Laurence Lundbladec5fef682020-01-25 11:38:45 -08004829int32_t ExponentAndMantissaDecodeFailTests()
Laurence Lundblade59289e52019-12-30 13:44:37 -08004830{
4831 return ProcessFailures(ExponentAndMantissaFailures,
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08004832 C_ARRAY_COUNT(ExponentAndMantissaFailures,
4833 struct FailInput));
Laurence Lundblade59289e52019-12-30 13:44:37 -08004834}
4835
Laurence Lundbladedd6e76e2021-03-10 01:54:01 -07004836#endif /* QCBOR_DISABLE_EXP_AND_MANTISSA */
Laurence Lundbladebb87be22020-04-09 19:15:32 -07004837
4838
4839
4840/*
4841 Some basic CBOR with map and array used in a lot of tests.
4842 The map labels are all strings
4843
Laurence Lundblade8ffdb742020-05-07 02:49:18 -07004844 {
4845 "first integer": 42,
Laurence Lundbladebb87be22020-04-09 19:15:32 -07004846 "an array of two strings": [
4847 "string1", "string2"
4848 ],
4849 "map in a map": {
4850 "bytes 1": h'78787878',
4851 "bytes 2": h'79797979',
4852 "another int": 98,
4853 "text 2": "lies, damn lies and statistics"
4854 }
Laurence Lundbladeddebabe2020-11-22 11:32:17 -08004855 }
Laurence Lundbladebb87be22020-04-09 19:15:32 -07004856 */
Laurence Lundblade9b334962020-08-27 10:55:53 -07004857
Laurence Lundbladef7a70bc2020-10-24 12:23:25 -07004858int32_t SpiffyDecodeBasicMap(UsefulBufC input)
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07004859{
4860 QCBORItem Item1, Item2, Item3;
4861 int64_t nDecodedInt1, nDecodedInt2;
4862 UsefulBufC B1, B2, S1, S2, S3;
4863
4864 QCBORDecodeContext DCtx;
4865 QCBORError nCBORError;
4866
4867 QCBORDecode_Init(&DCtx, input, 0);
4868
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07004869 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07004870
4871 QCBORDecode_GetInt64InMapSZ(&DCtx, "first integer", &nDecodedInt1);
4872
4873 QCBORDecode_EnterMapFromMapSZ(&DCtx, "map in a map");
4874 QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2);
Laurence Lundblade323f8a92020-09-06 19:43:09 -07004875 QCBORDecode_GetByteStringInMapSZ(&DCtx, "bytes 1", &B1);
4876 QCBORDecode_GetByteStringInMapSZ(&DCtx, "bytes 2", &B2);
4877 QCBORDecode_GetTextStringInMapSZ(&DCtx, "text 2", &S1);
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07004878 QCBORDecode_ExitMap(&DCtx);
4879
4880 QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings");
4881 QCBORDecode_GetNext(&DCtx, &Item1);
4882 QCBORDecode_GetNext(&DCtx, &Item2);
4883 if(QCBORDecode_GetNext(&DCtx, &Item3) != QCBOR_ERR_NO_MORE_ITEMS) {
4884 return -400;
4885 }
4886 QCBORDecode_ExitArray(&DCtx);
4887
4888 // Parse the same array again using GetText() instead of GetItem()
4889 QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings");
Laurence Lundblade323f8a92020-09-06 19:43:09 -07004890 QCBORDecode_GetTextString(&DCtx, &S2);
4891 QCBORDecode_GetTextString(&DCtx, &S3);
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07004892 if(QCBORDecode_GetError(&DCtx) != QCBOR_SUCCESS) {
4893 return 5000;
4894 }
4895 /* QCBORDecode_GetText(&DCtx, &S3);
4896 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_NO_MORE_ITEMS) {
4897 return 5001;
4898 } */
4899
4900 QCBORDecode_ExitArray(&DCtx);
4901
4902 QCBORDecode_ExitMap(&DCtx);
4903
4904 nCBORError = QCBORDecode_Finish(&DCtx);
4905
4906 if(nCBORError) {
4907 return (int32_t)nCBORError;
4908 }
4909
4910 if(nDecodedInt1 != 42) {
4911 return 1001;
4912 }
4913
4914 if(nDecodedInt2 != 98) {
4915 return 1002;
4916 }
4917
4918 if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07004919 UsefulBufCompareToSZ(Item1.val.string, "string1")) {
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07004920 return 1003;
4921 }
4922
4923 if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING ||
Laurence Lundblade9b334962020-08-27 10:55:53 -07004924 UsefulBufCompareToSZ(Item2.val.string, "string2")) {
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07004925 return 1004;
4926 }
4927
Laurence Lundblade9b334962020-08-27 10:55:53 -07004928 if(UsefulBufCompareToSZ(S1, "lies, damn lies and statistics")) {
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07004929 return 1005;
4930 }
4931
4932 if(UsefulBuf_Compare(B1, UsefulBuf_FromSZ("xxxx"))){
4933 return 1006;
4934 }
4935
4936 if(UsefulBuf_Compare(B2, UsefulBuf_FromSZ("yyyy"))){
4937 return 1007;
4938 }
4939
4940 if(UsefulBuf_Compare(S2, UsefulBuf_FromSZ("string1"))){
4941 return 1008;
4942 }
4943
4944 if(UsefulBuf_Compare(S3, UsefulBuf_FromSZ("string2"))){
4945 return 1009;
4946 }
4947
4948 return 0;
4949}
4950
Laurence Lundbladeddebabe2020-11-22 11:32:17 -08004951/*
Laurence Lundbladecc7da412020-12-27 00:09:07 -08004952 {
4953 -75008: h'05083399',
4954 88: [],
4955 100100: {
4956 "sub1": {
4957 10: [
4958 0
4959 ],
4960 -75009: h'A46823990001',
4961 100100: {
4962 "json": "{ \"ueid\", \"xyz\"}",
4963 "subsub": {
4964 100002: h'141813191001'
4965 }
4966 }
Laurence Lundbladeddebabe2020-11-22 11:32:17 -08004967 }
Laurence Lundbladecc7da412020-12-27 00:09:07 -08004968 }
Laurence Lundbladeddebabe2020-11-22 11:32:17 -08004969 }
Laurence Lundbladeddebabe2020-11-22 11:32:17 -08004970 */
4971
4972static const uint8_t spNestedCBOR[] = {
Laurence Lundbladecc7da412020-12-27 00:09:07 -08004973 0xa3, 0x3a, 0x00, 0x01, 0x24, 0xff, 0x44, 0x05,
4974 0x08, 0x33, 0x99, 0x18, 0x58, 0x80, 0x1a, 0x00,
4975 0x01, 0x87, 0x04, 0xa1, 0x64, 0x73, 0x75, 0x62,
4976 0x31, 0xa3, 0x0a, 0x81, 0x00, 0x3a, 0x00, 0x01,
4977 0x25, 0x00, 0x46, 0xa4, 0x68, 0x23, 0x99, 0x00,
4978 0x01, 0x1a, 0x00, 0x01, 0x87, 0x04, 0xa2, 0x64,
4979 0x6a, 0x73, 0x6f, 0x6e, 0x70, 0x7b, 0x20, 0x22,
4980 0x75, 0x65, 0x69, 0x64, 0x22, 0x2c, 0x20, 0x22,
4981 0x78, 0x79, 0x7a, 0x22, 0x7d, 0x66, 0x73, 0x75,
4982 0x62, 0x73, 0x75, 0x62, 0xa1, 0x1a, 0x00, 0x01,
4983 0x86, 0xa2, 0x46, 0x14, 0x18, 0x13, 0x19, 0x10,
4984 0x01
Laurence Lundbladeddebabe2020-11-22 11:32:17 -08004985};
4986
4987/* Get item in multi-level nesting in spNestedCBOR */
4988static int32_t DecodeNestedGetSubSub(QCBORDecodeContext *pDCtx)
4989{
4990 UsefulBufC String;
4991
4992 uint8_t test_oemid_bytes[] = {0x14, 0x18, 0x13, 0x19, 0x10, 0x01};
4993 const struct q_useful_buf_c test_oemid = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(test_oemid_bytes);
4994
4995 QCBORDecode_EnterMapFromMapN(pDCtx, 100100);
4996 QCBORDecode_EnterMap(pDCtx, NULL);
4997 QCBORDecode_EnterMapFromMapN(pDCtx, 100100);
4998 QCBORDecode_EnterMapFromMapSZ(pDCtx, "subsub");
4999 QCBORDecode_GetByteStringInMapN(pDCtx, 100002, &String);
5000 if(QCBORDecode_GetError(pDCtx)) {
5001 return 4001;
5002 }
5003 if(UsefulBuf_Compare(String, test_oemid)) {
5004 return 4002;
5005 }
5006 QCBORDecode_ExitMap(pDCtx);
5007 QCBORDecode_ExitMap(pDCtx);
5008 QCBORDecode_ExitMap(pDCtx);
5009 QCBORDecode_ExitMap(pDCtx);
5010
5011 return 0;
5012}
5013
5014/* Iterations on the zero-length array in spNestedCBOR */
5015static int32_t DecodeNestedGetEmpty(QCBORDecodeContext *pDCtx)
5016{
5017 QCBORItem Item;
5018 QCBORError uErr;
5019
5020 QCBORDecode_EnterArrayFromMapN(pDCtx, 88);
5021 for(int x = 0; x < 20; x++) {
5022 uErr = QCBORDecode_GetNext(pDCtx, &Item);
5023 if(uErr != QCBOR_ERR_NO_MORE_ITEMS) {
5024 return 4100;
5025
5026 }
5027 }
5028 QCBORDecode_ExitArray(pDCtx);
5029 if(QCBORDecode_GetError(pDCtx)) {
5030 return 4101;
5031 }
5032
5033 return 0;
5034}
5035
5036/* Various iterations on the array that contains a zero in spNestedCBOR */
5037static int32_t DecodeNestedGetZero(QCBORDecodeContext *pDCtx)
5038{
5039 QCBORError uErr;
5040
5041 QCBORDecode_EnterMapFromMapN(pDCtx, 100100);
5042 QCBORDecode_EnterMapFromMapSZ(pDCtx, "sub1");
5043 QCBORDecode_EnterArrayFromMapN(pDCtx, 10);
5044 int64_t nInt = 99;
5045 QCBORDecode_GetInt64(pDCtx, &nInt);
5046 if(nInt != 0) {
5047 return 4200;
5048 }
5049 for(int x = 0; x < 20; x++) {
5050 QCBORItem Item;
5051 uErr = QCBORDecode_GetNext(pDCtx, &Item);
5052 if(uErr != QCBOR_ERR_NO_MORE_ITEMS) {
5053 return 4201;
5054
5055 }
5056 }
5057 QCBORDecode_ExitArray(pDCtx);
5058 if(QCBORDecode_GetAndResetError(pDCtx)) {
5059 return 4202;
5060 }
5061 QCBORDecode_EnterArrayFromMapN(pDCtx, 10);
5062 UsefulBufC dD;
5063 QCBORDecode_GetByteString(pDCtx, &dD);
5064 if(QCBORDecode_GetAndResetError(pDCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
5065 return 4203;
5066 }
5067 for(int x = 0; x < 20; x++) {
5068 QCBORDecode_GetByteString(pDCtx, &dD);
5069 uErr = QCBORDecode_GetAndResetError(pDCtx);
5070 if(uErr != QCBOR_ERR_NO_MORE_ITEMS) {
5071 return 4204;
5072 }
5073 }
5074 QCBORDecode_ExitArray(pDCtx);
5075 QCBORDecode_ExitMap(pDCtx);
5076 QCBORDecode_ExitMap(pDCtx);
5077
5078 return 0;
5079}
5080
5081/* Repeatedly enter and exit maps and arrays, go off the end of maps
5082 and arrays and such. */
Laurence Lundbladeb9702452021-03-08 21:02:57 -08005083static int32_t DecodeNestedIterate(void)
Laurence Lundbladeddebabe2020-11-22 11:32:17 -08005084{
5085 QCBORDecodeContext DCtx;
5086 int32_t nReturn;
5087 QCBORError uErr;
5088
5089 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spNestedCBOR), 0);
5090 QCBORDecode_EnterMap(&DCtx, NULL);
5091
5092 for(int j = 0; j < 5; j++) {
5093 for(int i = 0; i < 20; i++) {
5094 nReturn = DecodeNestedGetSubSub(&DCtx);
5095 if(nReturn) {
5096 return nReturn;
5097 }
5098 }
5099
5100 for(int i = 0; i < 20; i++) {
5101 nReturn = DecodeNestedGetEmpty(&DCtx);
5102 if(nReturn ) {
5103 return nReturn;
5104 }
5105 }
5106
5107 for(int i = 0; i < 20; i++) {
5108 nReturn = DecodeNestedGetZero(&DCtx);
5109 if(nReturn ) {
5110 return nReturn;
5111 }
5112 }
5113 }
5114
5115 QCBORDecode_ExitMap(&DCtx);
5116 uErr = QCBORDecode_Finish(&DCtx);
5117 if(uErr) {
5118 return (int32_t)uErr + 4100;
5119 }
5120
5121 return 0;
5122}
5123
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07005124
5125/*
Laurence Lundbladecc7da412020-12-27 00:09:07 -08005126 [
5127 23,
5128 6000,
5129 h'67616C6163746963',
5130 h'686176656E20746F6B656E'
5131 ]
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07005132 */
5133static const uint8_t spSimpleArray[] = {
Laurence Lundblade9b334962020-08-27 10:55:53 -07005134 0x84,
5135 0x17,
5136 0x19, 0x17, 0x70,
5137 0x48, 0x67, 0x61, 0x6C, 0x61, 0x63, 0x74, 0x69, 0x63,
5138 0x4B, 0x68, 0x61, 0x76, 0x65, 0x6E, 0x20, 0x74, 0x6F, 0x6B, 0x65, 0x6E};
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07005139
Laurence Lundbladecc7da412020-12-27 00:09:07 -08005140/* [h'', {}, [], 0] */
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08005141static const uint8_t spArrayOfEmpty[] = {0x84, 0x40, 0xa0, 0x80, 0x00};
5142
Laurence Lundbladecc7da412020-12-27 00:09:07 -08005143/* {} */
Laurence Lundbladee6f15112020-07-23 18:44:16 -07005144static const uint8_t spEmptyMap[] = {0xa0};
5145
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08005146#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundbladecc7da412020-12-27 00:09:07 -08005147/* {} */
Laurence Lundbladee6f15112020-07-23 18:44:16 -07005148static const uint8_t spEmptyInDefinteLengthMap[] = {0xbf, 0xff};
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07005149
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08005150
Laurence Lundbladef0499502020-08-01 11:55:57 -07005151/*
5152 {
5153 0: [],
5154 9: [
5155 [],
5156 []
5157 ],
5158 8: {
5159 1: [],
5160 2: {},
5161 3: []
5162 },
5163 4: {},
5164 5: [],
5165 6: [
5166 [],
5167 []
5168 ]
Laurence Lundbladecc7da412020-12-27 00:09:07 -08005169 }
Laurence Lundbladef0499502020-08-01 11:55:57 -07005170 */
5171static const uint8_t spMapOfEmpty[] = {
Laurence Lundbladecc7da412020-12-27 00:09:07 -08005172 0xa6, 0x00, 0x80, 0x09, 0x82, 0x80, 0x80, 0x08,
5173 0xa3, 0x01, 0x80, 0x02, 0xa0, 0x03, 0x80, 0x04,
5174 0xa0, 0x05, 0x9f, 0xff, 0x06, 0x9f, 0x80, 0x9f,
5175 0xff, 0xff};
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005176
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08005177#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
5178
5179
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005180/*
5181 Too many tags
5182 Invalid tag content
5183 Duplicate label
5184 Integer overflow
5185 Date overflow
5186
Laurence Lundbladecc7da412020-12-27 00:09:07 -08005187 {
5188 1: 224(225(226(227(4(0))))),
5189 2: 1(h''),
5190 3: -18446744073709551616,
5191 4: 1(1.0e+300),
5192 5: 0, 8: 8
5193 }
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005194 */
5195static const uint8_t spRecoverableMapErrors[] = {
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08005196 0xa7,
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005197 0x01, 0xd8, 0xe0, 0xd8, 0xe1, 0xd8, 0xe2, 0xd8, 0xe3, 0xd8, 0x04, 0x00,
5198 0x02, 0xc1, 0x40,
5199 0x03, 0x3b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
5200 0x04, 0xc1, 0xfb, 0x7e, 0x37, 0xe4, 0x3c, 0x88, 0x00, 0x75, 0x9c,
5201 0x05, 0x00,
5202 0x05, 0x00,
5203 0x08, 0x08,
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005204};
5205
Laurence Lundbladecc7da412020-12-27 00:09:07 -08005206/* Bad break */
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005207static const uint8_t spUnRecoverableMapError1[] = {
5208 0xa2, 0xff, 0x01, 0x00, 0x02, 0x00
5209};
5210
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08005211#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundbladecc7da412020-12-27 00:09:07 -08005212/* No more items */
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005213static const uint8_t spUnRecoverableMapError2[] = {
5214 0xbf, 0x02, 0xbf, 0xff, 0x01, 0x00, 0x02, 0x00
5215};
5216
Laurence Lundbladecc7da412020-12-27 00:09:07 -08005217/* Hit end because string is too long */
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005218static const uint8_t spUnRecoverableMapError3[] = {
5219 0xbf, 0x02, 0x69, 0x64, 0x64, 0xff
5220};
5221
Laurence Lundbladecc7da412020-12-27 00:09:07 -08005222/* Hit end because string is too long */
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005223static const uint8_t spUnRecoverableMapError4[] = {
5224 0xbf,
5225 0x02, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f,
5226 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f, 0x9f,
5227 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
5228 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
5229 0xff
5230};
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08005231#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005232
Laurence Lundblade63926052021-03-29 16:05:51 -07005233const unsigned char not_well_formed_submod_section[] = {
5234 0xa1, 0x14, 0x1f,
5235};
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005236
Laurence Lundbladebb87be22020-04-09 19:15:32 -07005237int32_t EnterMapTest()
5238{
Laurence Lundbladef0499502020-08-01 11:55:57 -07005239 QCBORItem Item1;
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005240 QCBORItem ArrayItem;
Laurence Lundbladebb87be22020-04-09 19:15:32 -07005241 QCBORDecodeContext DCtx;
Laurence Lundbladef0499502020-08-01 11:55:57 -07005242 int32_t nReturn;
5243 QCBORError uErr;
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005244
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08005245#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005246 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spMapOfEmpty), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005247 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005248
Laurence Lundbladef0499502020-08-01 11:55:57 -07005249
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005250 QCBORDecode_EnterArray(&DCtx, NULL); // Label 0
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005251 QCBORDecode_ExitArray(&DCtx);
5252
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005253 QCBORDecode_EnterArray(&DCtx, NULL); // Label 9
5254 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005255 QCBORDecode_ExitArray(&DCtx);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005256 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005257 QCBORDecode_ExitArray(&DCtx);
5258 QCBORDecode_ExitArray(&DCtx);
5259
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005260 QCBORDecode_EnterMap(&DCtx, NULL); // Label 8
5261 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005262 QCBORDecode_ExitArray(&DCtx);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005263 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005264 QCBORDecode_ExitMap(&DCtx);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005265 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005266 QCBORDecode_ExitArray(&DCtx);
5267 QCBORDecode_ExitMap(&DCtx);
5268
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005269 QCBORDecode_EnterMap(&DCtx, NULL); // Label4
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005270 QCBORDecode_ExitMap(&DCtx);
5271
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005272 QCBORDecode_EnterArray(&DCtx, NULL); // Label 5
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005273 QCBORDecode_ExitArray(&DCtx);
5274
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005275 QCBORDecode_EnterArray(&DCtx, NULL); // Label 6
5276 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005277 QCBORDecode_ExitArray(&DCtx);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005278 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005279 QCBORDecode_ExitArray(&DCtx);
5280 QCBORDecode_ExitArray(&DCtx);
5281
5282 QCBORDecode_ExitMap(&DCtx);
5283
5284 uErr = QCBORDecode_Finish(&DCtx);
5285 if(uErr != QCBOR_SUCCESS){
5286 return 3011;
5287 }
5288
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07005289 (void)pValidMapIndefEncoded;
Laurence Lundbladef7a70bc2020-10-24 12:23:25 -07005290 nReturn = SpiffyDecodeBasicMap(UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapIndefEncoded));
Laurence Lundblade2c1faf92020-06-26 22:43:56 -07005291 if(nReturn) {
5292 return nReturn + 20000;
Laurence Lundbladebb87be22020-04-09 19:15:32 -07005293 }
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08005294#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
5295
Laurence Lundbladebb87be22020-04-09 19:15:32 -07005296
Laurence Lundbladef7a70bc2020-10-24 12:23:25 -07005297 nReturn = SpiffyDecodeBasicMap(UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded));
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005298 if(nReturn) {
5299 return nReturn;
5300 }
Laurence Lundbladebb87be22020-04-09 19:15:32 -07005301
Laurence Lundblade8ffdb742020-05-07 02:49:18 -07005302
Laurence Lundblade937ea812020-05-08 11:38:23 -07005303
Laurence Lundblade2f467f92020-10-09 17:50:11 -07005304 // These tests confirm the cursor is at the right place after entering
5305 // a map or array
Laurence Lundblade9b334962020-08-27 10:55:53 -07005306 const UsefulBufC ValidEncodedMap = UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded);
Laurence Lundblade937ea812020-05-08 11:38:23 -07005307
5308 // Confirm cursor is at right place
Laurence Lundblade9b334962020-08-27 10:55:53 -07005309 QCBORDecode_Init(&DCtx, ValidEncodedMap, 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005310 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade937ea812020-05-08 11:38:23 -07005311 QCBORDecode_GetNext(&DCtx, &Item1);
5312 if(Item1.uDataType != QCBOR_TYPE_INT64) {
5313 return 2001;
5314 }
5315
5316
Laurence Lundblade9b334962020-08-27 10:55:53 -07005317 QCBORDecode_Init(&DCtx, ValidEncodedMap, 0);
Laurence Lundblade6f3f78e2020-08-31 13:09:14 -07005318 QCBORDecode_VGetNext(&DCtx, &Item1);
5319 QCBORDecode_VGetNext(&DCtx, &Item1);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005320 QCBORDecode_EnterArray(&DCtx, &ArrayItem);
5321 if(ArrayItem.uLabelType != QCBOR_TYPE_TEXT_STRING ||
5322 UsefulBuf_Compare(ArrayItem.label.string,
5323 UsefulBuf_FROM_SZ_LITERAL("an array of two strings"))) {
5324 return 2051;
5325 }
Laurence Lundblade937ea812020-05-08 11:38:23 -07005326 QCBORDecode_GetNext(&DCtx, &Item1);
5327 if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING) {
5328 return 2002;
5329 }
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005330 QCBORDecode_ExitArray(&DCtx);
5331 QCBORDecode_EnterMap(&DCtx, &ArrayItem);
5332 if(ArrayItem.uLabelType != QCBOR_TYPE_TEXT_STRING ||
5333 UsefulBuf_Compare(ArrayItem.label.string,
5334 UsefulBuf_FROM_SZ_LITERAL("map in a map"))) {
5335 return 2052;
5336 }
5337
Laurence Lundblade937ea812020-05-08 11:38:23 -07005338
Laurence Lundblade9b334962020-08-27 10:55:53 -07005339 QCBORDecode_Init(&DCtx, ValidEncodedMap, 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005340 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade64b607e2020-05-13 13:05:57 -07005341 QCBORDecode_GetNext(&DCtx, &Item1);
5342 QCBORDecode_GetNext(&DCtx, &Item1);
5343 QCBORDecode_GetNext(&DCtx, &Item1);
Laurence Lundblade937ea812020-05-08 11:38:23 -07005344 QCBORDecode_EnterMapFromMapSZ(&DCtx, "map in a map");
5345 QCBORDecode_GetNext(&DCtx, &Item1);
5346 if(Item1.uDataType != QCBOR_TYPE_BYTE_STRING) {
5347 return 2003;
5348 }
5349
Laurence Lundblade9b334962020-08-27 10:55:53 -07005350 QCBORDecode_Init(&DCtx, ValidEncodedMap, 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005351 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade937ea812020-05-08 11:38:23 -07005352 QCBORDecode_GetNext(&DCtx, &Item1);
5353 QCBORDecode_GetNext(&DCtx, &Item1);
5354 QCBORDecode_GetNext(&DCtx, &Item1);
5355 QCBORDecode_GetNext(&DCtx, &Item1);
5356 QCBORDecode_GetNext(&DCtx, &Item1);
5357 QCBORDecode_GetNext(&DCtx, &Item1);
5358 QCBORDecode_GetNext(&DCtx, &Item1);
5359 QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings");
5360 QCBORDecode_GetNext(&DCtx, &Item1);
5361 if(Item1.uDataType != QCBOR_TYPE_TEXT_STRING) {
Laurence Lundblade64b607e2020-05-13 13:05:57 -07005362 return 2004;
Laurence Lundblade937ea812020-05-08 11:38:23 -07005363 }
5364
Laurence Lundblade9b334962020-08-27 10:55:53 -07005365 QCBORDecode_Init(&DCtx, ValidEncodedMap, 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005366 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade2b843b52020-06-16 20:51:03 -07005367 QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings");
5368 QCBORDecode_ExitArray(&DCtx);
5369 QCBORDecode_GetNext(&DCtx, &Item1);
5370 if(Item1.uDataType != QCBOR_TYPE_MAP && Item1.uLabelAlloc != QCBOR_TYPE_TEXT_STRING) {
5371 return 2006;
5372 }
5373 QCBORDecode_ExitMap(&DCtx);
5374 if(QCBORDecode_GetNext(&DCtx, &Item1) != QCBOR_ERR_NO_MORE_ITEMS) {
5375 return 2007;
5376 }
5377
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07005378 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spSimpleArray), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005379 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07005380 int64_t nDecodedInt2;
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005381 QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2);
5382 uErr = QCBORDecode_GetAndResetError(&DCtx);
5383 if(uErr != QCBOR_ERR_MAP_NOT_ENTERED){
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07005384 return 2008;
5385 }
5386 UsefulBufC String;
Laurence Lundblade323f8a92020-09-06 19:43:09 -07005387 QCBORDecode_GetTextStringInMapN(&DCtx, 88, &String);
Laurence Lundblade2cd4b0d2020-07-31 08:29:07 -07005388 if(uErr != QCBOR_ERR_MAP_NOT_ENTERED){
Laurence Lundbladeabf5c572020-06-29 21:21:29 -07005389 return 2009;
5390 }
Laurence Lundblade937ea812020-05-08 11:38:23 -07005391
Laurence Lundbladee6f15112020-07-23 18:44:16 -07005392
5393 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEmptyMap), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005394 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundbladee6f15112020-07-23 18:44:16 -07005395 // This will fail because the map is empty.
5396 QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2);
5397 uErr = QCBORDecode_GetAndResetError(&DCtx);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07005398 if(uErr != QCBOR_ERR_LABEL_NOT_FOUND){
Laurence Lundbladee6f15112020-07-23 18:44:16 -07005399 return 2010;
5400 }
5401 QCBORDecode_ExitMap(&DCtx);
5402 uErr = QCBORDecode_Finish(&DCtx);
5403 if(uErr != QCBOR_SUCCESS){
5404 return 2011;
5405 }
5406
5407
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08005408#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundbladee6f15112020-07-23 18:44:16 -07005409 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spEmptyInDefinteLengthMap), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005410 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundbladee6f15112020-07-23 18:44:16 -07005411 // This will fail because the map is empty.
5412 QCBORDecode_GetInt64InMapSZ(&DCtx, "another int", &nDecodedInt2);
5413 uErr = QCBORDecode_GetAndResetError(&DCtx);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07005414 if(uErr != QCBOR_ERR_LABEL_NOT_FOUND){
Laurence Lundblade085d7952020-07-24 10:26:30 -07005415 return 2012;
Laurence Lundbladee6f15112020-07-23 18:44:16 -07005416 }
5417 QCBORDecode_ExitMap(&DCtx);
5418 uErr = QCBORDecode_Finish(&DCtx);
5419 if(uErr != QCBOR_SUCCESS){
Laurence Lundblade085d7952020-07-24 10:26:30 -07005420 return 2013;
Laurence Lundbladee6f15112020-07-23 18:44:16 -07005421 }
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08005422#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
Laurence Lundbladee6f15112020-07-23 18:44:16 -07005423
Laurence Lundblade5f4e8712020-07-25 11:44:43 -07005424
5425 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spArrayOfEmpty), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005426 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade323f8a92020-09-06 19:43:09 -07005427 QCBORDecode_GetByteString(&DCtx, &String);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005428 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade5f4e8712020-07-25 11:44:43 -07005429 QCBORDecode_ExitMap(&DCtx);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005430 QCBORDecode_EnterArray(&DCtx, NULL);
Laurence Lundblade5f4e8712020-07-25 11:44:43 -07005431 QCBORDecode_ExitArray(&DCtx);
5432 QCBORDecode_GetInt64(&DCtx, &nDecodedInt2);
5433 QCBORDecode_ExitArray(&DCtx);
5434 uErr = QCBORDecode_Finish(&DCtx);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005435 if(uErr != QCBOR_SUCCESS) {
Laurence Lundblade5f4e8712020-07-25 11:44:43 -07005436 return 2014;
5437 }
5438
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005439 int64_t nInt;
5440 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spRecoverableMapErrors), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005441 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005442 QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt);
Laurence Lundblade88e9db22020-11-02 03:56:33 -08005443 uErr = QCBORDecode_GetError(&DCtx);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005444 if(uErr != QCBOR_ERR_TOO_MANY_TAGS) {
5445 return 2021;
5446 }
Laurence Lundblade88e9db22020-11-02 03:56:33 -08005447 if(QCBORDecode_GetNthTagOfLast(&DCtx, 0) != CBOR_TAG_INVALID64) {
5448 return 2121;
5449 }
5450 (void)QCBORDecode_GetAndResetError(&DCtx);
5451
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005452
5453 QCBORDecode_GetEpochDateInMapN(&DCtx, 0x02, QCBOR_TAG_REQUIREMENT_TAG, &nInt);
5454 uErr = QCBORDecode_GetAndResetError(&DCtx);
5455 if(uErr != QCBOR_ERR_BAD_OPT_TAG) {
5456 return 2022;
5457 }
5458
5459 QCBORDecode_GetInt64InMapN(&DCtx, 0x03, &nInt);
5460 uErr = QCBORDecode_GetAndResetError(&DCtx);
5461 if(uErr != QCBOR_ERR_INT_OVERFLOW) {
5462 return 2023;
5463 }
5464
5465 QCBORDecode_GetEpochDateInMapN(&DCtx, 0x04, QCBOR_TAG_REQUIREMENT_TAG, &nInt);
5466 uErr = QCBORDecode_GetAndResetError(&DCtx);
Laurence Lundblade16a207a2021-09-18 17:22:46 -07005467 if(uErr != FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_DATE_OVERFLOW)) {
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005468 return 2024;
5469 }
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005470
5471 QCBORDecode_GetInt64InMapN(&DCtx, 0x05, &nInt);
5472 uErr = QCBORDecode_GetAndResetError(&DCtx);
5473 if(uErr != QCBOR_ERR_DUPLICATE_LABEL) {
5474 return 2025;
5475 }
5476
5477 QCBORDecode_GetInt64InMapN(&DCtx, 0x08, &nInt);
5478
5479 QCBORDecode_ExitMap(&DCtx);
5480 uErr = QCBORDecode_Finish(&DCtx);
5481 if(uErr != QCBOR_SUCCESS) {
5482 return 2026;
5483 }
5484
5485 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUnRecoverableMapError1), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005486 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005487 QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt);
5488 uErr = QCBORDecode_GetAndResetError(&DCtx);
5489 if(uErr != QCBOR_ERR_BAD_BREAK) {
5490 return 2030;
5491 }
5492
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08005493#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005494 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUnRecoverableMapError2), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005495 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005496 QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt);
5497 uErr = QCBORDecode_GetAndResetError(&DCtx);
5498 if(uErr != QCBOR_ERR_NO_MORE_ITEMS) {
5499 return 2031;
5500 }
5501
5502 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUnRecoverableMapError3), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005503 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005504 QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt);
5505 uErr = QCBORDecode_GetAndResetError(&DCtx);
5506 if(uErr != QCBOR_ERR_HIT_END) {
5507 return 2032;
5508 }
5509
5510 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUnRecoverableMapError4), 0);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07005511 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundblade93d3f532020-09-28 21:09:12 -07005512 QCBORDecode_GetInt64InMapN(&DCtx, 0x01, &nInt);
5513 uErr = QCBORDecode_GetAndResetError(&DCtx);
5514 if(uErr != QCBOR_ERR_ARRAY_DECODE_NESTING_TOO_DEEP) {
5515 return 2033;
5516 }
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08005517#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
5518
Laurence Lundblade732e52d2021-02-22 20:11:01 -07005519 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0);
5520 QCBORDecode_VGetNextConsume(&DCtx, &Item1);
5521 if(Item1.uDataType != QCBOR_TYPE_MAP) {
5522 return 2401;
5523 }
5524 if(QCBORDecode_GetError(&DCtx)) {
5525 return 2402;
5526 }
5527
5528 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0);
5529 QCBORDecode_VGetNext(&DCtx, &Item1);
5530 if(Item1.uDataType != QCBOR_TYPE_MAP ||
5531 Item1.val.uCount != 3 ||
5532 Item1.uNextNestLevel != 1) {
5533 return 2403;
5534 }
5535 if(QCBORDecode_GetError(&DCtx)) {
5536 return 2404;
5537 }
5538 QCBORDecode_VGetNextConsume(&DCtx, &Item1);
5539 if(Item1.uDataType != QCBOR_TYPE_INT64 ||
5540 Item1.uNextNestLevel != 1 ||
5541 Item1.val.int64 != 42) {
5542 return 2405;
5543 }
5544 if(QCBORDecode_GetError(&DCtx)) {
5545 return 2406;
5546 }
5547 QCBORDecode_VGetNextConsume(&DCtx, &Item1);
5548 if(Item1.uDataType != QCBOR_TYPE_ARRAY ||
5549 Item1.uNestingLevel != 1 ||
5550 Item1.uNextNestLevel != 1 ||
5551 Item1.val.uCount != 2) {
5552 return 2407;
5553 }
5554 if(QCBORDecode_GetError(&DCtx)) {
5555 return 2408;
5556 }
5557 QCBORDecode_VGetNextConsume(&DCtx, &Item1);
5558 if(Item1.uDataType != QCBOR_TYPE_MAP ||
5559 Item1.uNestingLevel != 1 ||
5560 Item1.uNextNestLevel != 0 ||
5561 Item1.val.uCount != 4) {
5562 return 2409;
5563 }
5564 if(QCBORDecode_GetError(&DCtx)) {
5565 return 2410;
5566 }
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07005567
Laurence Lundbladeddebabe2020-11-22 11:32:17 -08005568 nReturn = DecodeNestedIterate();
5569
Laurence Lundblade63926052021-03-29 16:05:51 -07005570
5571 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(not_well_formed_submod_section), 0);
5572 QCBORDecode_EnterMap(&DCtx, NULL);
5573 QCBORDecode_EnterMapFromMapN(&DCtx, 20);
5574 if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_BAD_INT) {
5575 return 2500;
5576 }
5577
Laurence Lundbladeddebabe2020-11-22 11:32:17 -08005578 return nReturn;
Laurence Lundblade9c905e82020-04-25 11:31:38 -07005579}
5580
5581
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005582struct NumberConversion {
5583 char *szDescription;
5584 UsefulBufC CBOR;
5585 int64_t nConvertedToInt64;
5586 QCBORError uErrorInt64;
5587 uint64_t uConvertToUInt64;
5588 QCBORError uErrorUint64;
5589 double dConvertToDouble;
5590 QCBORError uErrorDouble;
5591};
5592
Laurence Lundblade16a207a2021-09-18 17:22:46 -07005593#ifndef QCBOR_DISABLE_EXP_AND_MANTISSA
5594#define EXP_AND_MANTISSA_ERROR(x) x
5595#else
5596#define EXP_AND_MANTISSA_ERROR(x) QCBOR_ERR_UNEXPECTED_TYPE
5597#endif
5598
5599
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07005600static const struct NumberConversion NumberConversions[] = {
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005601 {
Laurence Lundblade784b54b2020-08-10 01:24:52 -07005602 "too large to fit into int64_t",
5603 {(uint8_t[]){0xc3, 0x48, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 10},
5604 0,
5605 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5606 0,
5607 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
5608 ((double)INT64_MIN) + 1 ,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07005609 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)
Laurence Lundblade784b54b2020-08-10 01:24:52 -07005610 },
5611 {
5612 "largest negative int that fits in int64_t",
5613 {(uint8_t[]){0xc3, 0x48, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, 10},
5614 INT64_MIN,
5615 QCBOR_SUCCESS,
5616 0,
5617 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
5618 (double)INT64_MIN,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07005619 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)
Laurence Lundblade784b54b2020-08-10 01:24:52 -07005620 },
5621 {
Laurence Lundbladeda095972020-06-06 18:35:33 -07005622 "negative bignum -1",
5623 {(uint8_t[]){0xc3, 0x41, 0x00}, 3},
5624 -1,
5625 QCBOR_SUCCESS,
5626 0,
5627 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
5628 -1.0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07005629 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)
Laurence Lundbladeda095972020-06-06 18:35:33 -07005630 },
5631 {
Laurence Lundblade9ab5abb2020-05-20 12:10:45 -07005632 "Decimal Fraction with positive bignum 257 * 10e3",
Laurence Lundblade887add82020-05-17 05:50:34 -07005633 {(uint8_t[]){0xC4, 0x82, 0x1B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03,
5634 0xC2, 0x42, 0x01, 0x01}, 15},
5635 257000,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07005636 EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS),
Laurence Lundblade887add82020-05-17 05:50:34 -07005637 257000,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07005638 EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS),
Laurence Lundblade887add82020-05-17 05:50:34 -07005639 257000.0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07005640 FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS))
Laurence Lundblade887add82020-05-17 05:50:34 -07005641 },
5642 {
Laurence Lundbladeda095972020-06-06 18:35:33 -07005643 "bigfloat with negative bignum -258 * 2e3",
Laurence Lundblade887add82020-05-17 05:50:34 -07005644 {(uint8_t[]){0xC5, 0x82, 0x1B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03,
5645 0xC3, 0x42, 0x01, 0x01}, 15},
Laurence Lundbladeda095972020-06-06 18:35:33 -07005646 -2064,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07005647 EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS),
Laurence Lundblade887add82020-05-17 05:50:34 -07005648 0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07005649 EXP_AND_MANTISSA_ERROR(QCBOR_ERR_NUMBER_SIGN_CONVERSION),
Laurence Lundbladeda095972020-06-06 18:35:33 -07005650 -2064.0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07005651 FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS))
Laurence Lundblade887add82020-05-17 05:50:34 -07005652 },
5653 {
Laurence Lundblade9ab5abb2020-05-20 12:10:45 -07005654 "bigfloat with positive bignum 257 * 2e3",
Laurence Lundblade887add82020-05-17 05:50:34 -07005655 {(uint8_t[]){0xC5, 0x82, 0x1B, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03,
5656 0xC2, 0x42, 0x01, 0x01}, 15},
5657 2056,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07005658 EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS),
Laurence Lundblade887add82020-05-17 05:50:34 -07005659 2056,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07005660 EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS),
Laurence Lundblade887add82020-05-17 05:50:34 -07005661 2056.0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07005662 FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS))
Laurence Lundblade887add82020-05-17 05:50:34 -07005663 },
5664 {
Laurence Lundbladeda095972020-06-06 18:35:33 -07005665 "negative bignum 0xc349010000000000000000 -18446744073709551617",
Laurence Lundblade887add82020-05-17 05:50:34 -07005666 {(uint8_t[]){0xc3, 0x49, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}, 11},
5667 0,
5668 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5669 0,
5670 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
5671 -18446744073709551617.0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07005672 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)
Laurence Lundblade887add82020-05-17 05:50:34 -07005673 },
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08005674#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
Laurence Lundblade887add82020-05-17 05:50:34 -07005675 {
Laurence Lundblade313b2862020-05-16 01:23:06 -07005676 "Positive bignum 0x01020304 indefinite length string",
5677 {(uint8_t[]){0xC2, 0x5f, 0x42, 0x01, 0x02, 0x41, 0x03, 0x41, 0x04, 0xff}, 10},
5678 0x01020304,
5679 QCBOR_SUCCESS,
5680 0x01020304,
5681 QCBOR_SUCCESS,
5682 16909060.0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07005683 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)
Laurence Lundblade313b2862020-05-16 01:23:06 -07005684 },
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08005685#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
Laurence Lundblade313b2862020-05-16 01:23:06 -07005686 {
Laurence Lundblade887add82020-05-17 05:50:34 -07005687 "Decimal Fraction with neg bignum [9223372036854775807, -4759477275222530853137]",
Laurence Lundblade313b2862020-05-16 01:23:06 -07005688 {(uint8_t[]){0xC4, 0x82, 0x1B, 0x7F, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
5689 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x10,}, 23},
5690 0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07005691 EXP_AND_MANTISSA_ERROR(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW),
Laurence Lundblade313b2862020-05-16 01:23:06 -07005692 0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07005693 EXP_AND_MANTISSA_ERROR(QCBOR_ERR_NUMBER_SIGN_CONVERSION),
Laurence Lundblade313b2862020-05-16 01:23:06 -07005694 -INFINITY,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07005695 FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS))
Laurence Lundblade313b2862020-05-16 01:23:06 -07005696 },
5697 {
5698 "big float [9223372036854775806, 9223372036854775806]",
5699 {(uint8_t[]){0xC5, 0x82, 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE,
5700 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE}, 20},
5701 0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07005702 EXP_AND_MANTISSA_ERROR(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW),
Laurence Lundblade313b2862020-05-16 01:23:06 -07005703 0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07005704 EXP_AND_MANTISSA_ERROR(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW),
Laurence Lundblade313b2862020-05-16 01:23:06 -07005705 INFINITY,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07005706 FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS))
Laurence Lundblade313b2862020-05-16 01:23:06 -07005707 },
5708 {
Laurence Lundblade983500d2020-05-14 11:49:34 -07005709 "Big float 3 * 2^^2",
5710 {(uint8_t[]){0xC5, 0x82, 0x02, 0x03}, 4},
5711 12,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07005712 EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS),
Laurence Lundblade983500d2020-05-14 11:49:34 -07005713 12,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07005714 EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS),
Laurence Lundblade983500d2020-05-14 11:49:34 -07005715 12.0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07005716 FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS))
Laurence Lundblade983500d2020-05-14 11:49:34 -07005717 },
Laurence Lundblade983500d2020-05-14 11:49:34 -07005718 {
Laurence Lundblade313b2862020-05-16 01:23:06 -07005719 "Positive integer 18446744073709551615",
Laurence Lundblade983500d2020-05-14 11:49:34 -07005720 {(uint8_t[]){0x1b, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}, 9},
5721 0,
5722 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5723 18446744073709551615ULL,
5724 QCBOR_SUCCESS,
5725 18446744073709551615.0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07005726 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)
Laurence Lundblade983500d2020-05-14 11:49:34 -07005727 },
Laurence Lundblade983500d2020-05-14 11:49:34 -07005728 {
Laurence Lundblade313b2862020-05-16 01:23:06 -07005729 "Positive bignum 0xffff",
Laurence Lundblade983500d2020-05-14 11:49:34 -07005730 {(uint8_t[]){0xC2, 0x42, 0xff, 0xff}, 4},
5731 65536-1,
5732 QCBOR_SUCCESS,
5733 0xffff,
5734 QCBOR_SUCCESS,
5735 65535.0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07005736 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)
Laurence Lundblade983500d2020-05-14 11:49:34 -07005737 },
5738 {
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005739 "Postive integer 0",
5740 {(uint8_t[]){0x0}, 1},
5741 0LL,
5742 QCBOR_SUCCESS,
5743 0ULL,
5744 QCBOR_SUCCESS,
5745 0.0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07005746 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005747 },
5748 {
Laurence Lundblade313b2862020-05-16 01:23:06 -07005749 "Negative integer -18446744073709551616",
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005750 {(uint8_t[]){0x3b, 0x7f, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff }, 9},
5751 -9223372036854775807-1, // INT64_MIN
5752 QCBOR_SUCCESS,
5753 0ULL,
5754 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
5755 -9223372036854775808.0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07005756 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005757 },
5758 {
Laurence Lundblade313b2862020-05-16 01:23:06 -07005759 "Double Floating point value 100.3",
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005760 {(uint8_t[]){0xfb, 0x40, 0x59, 0x13, 0x33, 0x33, 0x33, 0x33, 0x33}, 9},
5761 100L,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07005762 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS),
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005763 100ULL,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07005764 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS),
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005765 100.3,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07005766 FLOAT_ERR_CODE_NO_FLOAT(QCBOR_SUCCESS),
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005767 },
5768 {
5769 "Floating point value NaN 0xfa7fc00000",
5770 {(uint8_t[]){0xfa, 0x7f, 0xc0, 0x00, 0x00}, 5},
5771 0,
Máté Tóth-Pálef5f07a2021-09-17 19:31:37 +02005772 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_FLOAT_EXCEPTION),
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005773 0,
Máté Tóth-Pálef5f07a2021-09-17 19:31:37 +02005774 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_FLOAT_EXCEPTION),
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005775 NAN,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07005776 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS),
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005777 },
5778 {
Laurence Lundblade313b2862020-05-16 01:23:06 -07005779 "half-precision Floating point value -4",
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005780 {(uint8_t[]){0xf9, 0xc4, 0x00}, 3},
Laurence Lundbladef7c0adb2020-08-08 20:20:58 -07005781 // Normal case with all enabled.
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005782 -4,
Máté Tóth-Pálef5f07a2021-09-17 19:31:37 +02005783 FLOAT_ERR_CODE_NO_HALF_PREC_NO_FLOAT_HW(QCBOR_SUCCESS),
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005784 0,
Máté Tóth-Pálef5f07a2021-09-17 19:31:37 +02005785 FLOAT_ERR_CODE_NO_HALF_PREC_NO_FLOAT_HW(QCBOR_ERR_NUMBER_SIGN_CONVERSION),
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005786 -4.0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07005787 FLOAT_ERR_CODE_NO_HALF_PREC(QCBOR_SUCCESS)
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005788 },
5789 {
5790 "Decimal fraction 3/10",
5791 {(uint8_t[]){0xC4, 0x82, 0x20, 0x03}, 4},
5792 0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07005793 EXP_AND_MANTISSA_ERROR(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW),
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005794 0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07005795 EXP_AND_MANTISSA_ERROR(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW),
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005796 0.30000000000000004,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07005797 FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS))
Laurence Lundbladedfd49fc2020-09-01 14:17:16 -07005798 },
5799 {
Laurence Lundblade16a207a2021-09-18 17:22:46 -07005800 "+inifinity single precision",
Laurence Lundbladedfd49fc2020-09-01 14:17:16 -07005801 {(uint8_t[]){0xfa, 0x7f, 0x80, 0x00, 0x00}, 5},
Laurence Lundbladedfd49fc2020-09-01 14:17:16 -07005802 0,
Máté Tóth-Pálef5f07a2021-09-17 19:31:37 +02005803 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_FLOAT_EXCEPTION),
Laurence Lundbladedfd49fc2020-09-01 14:17:16 -07005804 0,
Máté Tóth-Pálef5f07a2021-09-17 19:31:37 +02005805 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW),
Laurence Lundbladedfd49fc2020-09-01 14:17:16 -07005806 INFINITY,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07005807 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)
Laurence Lundbladedfd49fc2020-09-01 14:17:16 -07005808 },
Laurence Lundblade11fd78b2020-09-01 22:13:27 -07005809
5810 {
5811 "extreme pos bignum",
5812 {(uint8_t[]){0xc2, 0x59, 0x01, 0x90,
5813 // 50 rows of 8 is 400 digits.
5814 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5815 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5816 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5817 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5818 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5819 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5820 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5821 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5822 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5823 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5824 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5825 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5826 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5827 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5828 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5829 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5830 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5831 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5832 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5833 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5834 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5835 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5836 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5837 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5838 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5839 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5840 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5841 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5842 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5843 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5844 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5845 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5846 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5847 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5848 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5849 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5850 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5851 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5852 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5853 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5854 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5855 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5856 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5857 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5858 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5859 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5860 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5861 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5862 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5863 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0},
5864 404},
5865 0,
5866 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5867 0,
5868 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
Laurence Lundblade11fd78b2020-09-01 22:13:27 -07005869 INFINITY,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07005870 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS),
Laurence Lundblade11fd78b2020-09-01 22:13:27 -07005871 },
5872
5873 {
5874 "extreme neg bignum",
5875 {(uint8_t[]){0xc3, 0x59, 0x01, 0x90,
5876 // 50 rows of 8 is 400 digits.
5877 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5878 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5879 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5880 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5881 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5882 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5883 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5884 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5885 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5886 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5887 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5888 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5889 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5890 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
5891 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0, 0xf0,
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},
5927 404},
5928 0,
5929 QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW,
5930 0,
5931 QCBOR_ERR_NUMBER_SIGN_CONVERSION,
Laurence Lundblade11fd78b2020-09-01 22:13:27 -07005932 -INFINITY,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07005933 FLOAT_ERR_CODE_NO_FLOAT_HW(QCBOR_SUCCESS)
Laurence Lundblade11fd78b2020-09-01 22:13:27 -07005934 },
Laurence Lundblade51722fd2020-09-02 13:01:33 -07005935
5936 {
5937 "big float underflow [9223372036854775806, -9223372036854775806]",
5938 {(uint8_t[]){
5939 0xC5, 0x82,
5940 0x3B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE,
5941 0x1B, 0x7f, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFE}, 20},
Laurence Lundblade51722fd2020-09-02 13:01:33 -07005942 0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07005943 EXP_AND_MANTISSA_ERROR(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW),
Laurence Lundblade51722fd2020-09-02 13:01:33 -07005944 0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07005945 EXP_AND_MANTISSA_ERROR(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW),
Laurence Lundblade51722fd2020-09-02 13:01:33 -07005946 0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07005947 FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS))
Laurence Lundblade51722fd2020-09-02 13:01:33 -07005948 },
5949
5950 {
5951 "bigfloat that evaluates to -INFINITY",
5952 {(uint8_t[]){
5953 0xC5, 0x82,
5954 0x1B, 0x70, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x03,
5955 0xC3, 0x42, 0x01, 0x01}, 15},
Laurence Lundblade51722fd2020-09-02 13:01:33 -07005956 0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07005957 EXP_AND_MANTISSA_ERROR(QCBOR_ERR_CONVERSION_UNDER_OVER_FLOW),
Laurence Lundblade51722fd2020-09-02 13:01:33 -07005958 0,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07005959 EXP_AND_MANTISSA_ERROR(QCBOR_ERR_NUMBER_SIGN_CONVERSION),
Laurence Lundblade51722fd2020-09-02 13:01:33 -07005960 -INFINITY,
Laurence Lundblade16a207a2021-09-18 17:22:46 -07005961 FLOAT_ERR_CODE_NO_FLOAT_HW(EXP_AND_MANTISSA_ERROR(QCBOR_SUCCESS))
Laurence Lundblade51722fd2020-09-02 13:01:33 -07005962 },
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005963};
Laurence Lundblade9c905e82020-04-25 11:31:38 -07005964
5965
5966
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08005967
5968static int32_t SetUpDecoder(QCBORDecodeContext *DCtx, UsefulBufC CBOR, UsefulBuf Pool)
5969{
5970 QCBORDecode_Init(DCtx, CBOR, QCBOR_DECODE_MODE_NORMAL);
5971#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
5972 if(QCBORDecode_SetMemPool(DCtx, Pool, 0)) {
5973 return 1;
5974 }
5975#else /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
5976 (void)Pool;
5977#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
5978 return 0;
5979}
5980
5981
Laurence Lundblade313b2862020-05-16 01:23:06 -07005982int32_t IntegerConvertTest()
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005983{
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08005984 const int nNumTests = C_ARRAY_COUNT(NumberConversions,
5985 struct NumberConversion);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005986
Laurence Lundblade9ab5abb2020-05-20 12:10:45 -07005987 for(int nIndex = 0; nIndex < nNumTests; nIndex++) {
5988 const struct NumberConversion *pF = &NumberConversions[nIndex];
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07005989
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005990 // Set up the decoding context including a memory pool so that
5991 // indefinite length items can be checked
5992 QCBORDecodeContext DCtx;
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005993 UsefulBuf_MAKE_STACK_UB(Pool, 100);
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07005994
5995 /* ----- test conversion to int64_t ------ */
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08005996 if(SetUpDecoder(&DCtx, pF->CBOR, Pool)) {
5997 return (int32_t)(3333+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07005998 }
5999
6000 int64_t nInt;
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006001 QCBORDecode_GetInt64ConvertAll(&DCtx, 0xffff, &nInt);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07006002 if(QCBORDecode_GetError(&DCtx) != pF->uErrorInt64) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07006003 return (int32_t)(2000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006004 }
6005 if(pF->uErrorInt64 == QCBOR_SUCCESS && pF->nConvertedToInt64 != nInt) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07006006 return (int32_t)(3000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006007 }
6008
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07006009 /* ----- test conversion to uint64_t ------ */
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08006010 if(SetUpDecoder(&DCtx, pF->CBOR, Pool)) {
6011 return (int32_t)(3333+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006012 }
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08006013
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006014 uint64_t uInt;
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006015 QCBORDecode_GetUInt64ConvertAll(&DCtx, 0xffff, &uInt);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07006016 if(QCBORDecode_GetError(&DCtx) != pF->uErrorUint64) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07006017 return (int32_t)(4000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006018 }
6019 if(pF->uErrorUint64 == QCBOR_SUCCESS && pF->uConvertToUInt64 != uInt) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07006020 return (int32_t)(5000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006021 }
6022
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07006023 /* ----- test conversion to double ------ */
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08006024 if(SetUpDecoder(&DCtx, pF->CBOR, Pool)) {
6025 return (int32_t)(3333+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006026 }
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006027
6028#ifndef USEFULBUF_DISABLE_ALL_FLOAT
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006029 double d;
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006030 QCBORDecode_GetDoubleConvertAll(&DCtx, 0xffff, &d);
Laurence Lundbladeb340ba72020-05-14 11:41:10 -07006031 if(QCBORDecode_GetError(&DCtx) != pF->uErrorDouble) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07006032 return (int32_t)(6000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006033 }
6034 if(pF->uErrorDouble == QCBOR_SUCCESS) {
6035 if(isnan(pF->dConvertToDouble)) {
Laurence Lundblade983500d2020-05-14 11:49:34 -07006036 // NaN's can't be compared for equality. A NaN is
6037 // never equal to anything including another NaN
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006038 if(!isnan(d)) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07006039 return (int32_t)(7000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006040 }
6041 } else {
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006042 if(pF->dConvertToDouble != d) {
Laurence Lundblade54cd99c2020-05-15 02:25:32 -07006043 return (int32_t)(8000+nIndex);
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006044 }
6045 }
6046 }
Laurence Lundblade16a207a2021-09-18 17:22:46 -07006047#endif /* USEFULBUF_DISABLE_ALL_FLOAT */
Laurence Lundbladef6c86662020-05-12 02:08:00 -07006048 }
6049
6050 return 0;
6051}
6052
Laurence Lundbladea8758502022-05-15 17:57:46 -07006053#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
6054
David Navarro9123e5b2022-03-28 16:04:03 +02006055int32_t CBORTestIssue134()
6056{
6057 QCBORDecodeContext DCtx;
6058 QCBORItem Item;
6059 QCBORError uCBORError;
6060 const uint8_t spTestIssue134[] = { 0x5F, 0x40, 0xFF };
Laurence Lundblade9c905e82020-04-25 11:31:38 -07006061
David Navarro9123e5b2022-03-28 16:04:03 +02006062 QCBORDecode_Init(&DCtx,
6063 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTestIssue134),
6064 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade97c61bf2020-05-02 11:24:06 -07006065
David Navarro9123e5b2022-03-28 16:04:03 +02006066 UsefulBuf_MAKE_STACK_UB(StringBuf, 200);
6067 QCBORDecode_SetMemPool(&DCtx, StringBuf, false);
6068
6069 do {
6070 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
6071 } while (QCBOR_SUCCESS == uCBORError);
6072
6073 uCBORError = QCBORDecode_Finish(&DCtx);
6074
6075 return uCBORError;
6076}
Laurence Lundblade97c61bf2020-05-02 11:24:06 -07006077
Laurence Lundbladea8758502022-05-15 17:57:46 -07006078#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
6079
Laurence Lundbladee3553422020-05-02 11:11:17 -07006080int32_t CBORSequenceDecodeTests(void)
6081{
6082 QCBORDecodeContext DCtx;
Laurence Lundblade87495732021-02-26 10:05:55 -07006083 QCBORItem Item;
6084 QCBORError uCBORError;
6085 size_t uConsumed;
Laurence Lundbladee3553422020-05-02 11:11:17 -07006086
6087 // --- Test a sequence with extra bytes ---
Laurence Lundblade9b334962020-08-27 10:55:53 -07006088
Laurence Lundbladee3553422020-05-02 11:11:17 -07006089 // The input for the date test happens to be a sequence so it
6090 // is reused. It is a sequence because it doesn't start as
6091 // an array or map.
6092 QCBORDecode_Init(&DCtx,
6093 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spDateTestInput),
6094 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade9b334962020-08-27 10:55:53 -07006095
Laurence Lundbladee3553422020-05-02 11:11:17 -07006096 // Get the first item
6097 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
6098 if(uCBORError != QCBOR_SUCCESS) {
6099 return 1;
6100 }
6101 if(Item.uDataType != QCBOR_TYPE_DATE_STRING) {
6102 return 2;
6103 }
Laurence Lundbladec7114722020-08-13 05:11:40 -07006104
Laurence Lundblade87495732021-02-26 10:05:55 -07006105 uCBORError = QCBORDecode_PartialFinish(&DCtx, &uConsumed);
6106 if(uCBORError != QCBOR_ERR_EXTRA_BYTES ||
6107 uConsumed != 12) {
6108 return 102;
6109 }
6110
Laurence Lundbladee3553422020-05-02 11:11:17 -07006111 // Get a second item
6112 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundbladec7114722020-08-13 05:11:40 -07006113 if(uCBORError != QCBOR_ERR_BAD_OPT_TAG) {
6114 return 66;
6115 }
6116
Laurence Lundblade87495732021-02-26 10:05:55 -07006117 uCBORError = QCBORDecode_PartialFinish(&DCtx, &uConsumed);
6118 if(uCBORError != QCBOR_ERR_EXTRA_BYTES ||
6119 uConsumed != 14) {
6120 return 102;
6121 }
6122
Laurence Lundbladec7114722020-08-13 05:11:40 -07006123 // Get a third item
6124 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundbladee3553422020-05-02 11:11:17 -07006125 if(uCBORError != QCBOR_SUCCESS) {
6126 return 2;
6127 }
6128 if(Item.uDataType != QCBOR_TYPE_DATE_EPOCH) {
6129 return 3;
6130 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006131
Laurence Lundbladee3553422020-05-02 11:11:17 -07006132 // A sequence can have stuff at the end that may
6133 // or may not be valid CBOR. The protocol decoder knows
6134 // when to stop by definition of the protocol, not
6135 // when the top-level map or array is ended.
6136 // Finish still has to be called to know that
6137 // maps and arrays (if there were any) were closed
6138 // off correctly. When called like this it
6139 // must return the error QCBOR_ERR_EXTRA_BYTES.
6140 uCBORError = QCBORDecode_Finish(&DCtx);
6141 if(uCBORError != QCBOR_ERR_EXTRA_BYTES) {
6142 return 4;
6143 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006144
6145
Laurence Lundbladee3553422020-05-02 11:11:17 -07006146 // --- Test an empty input ----
6147 uint8_t empty[1];
6148 UsefulBufC Empty = {empty, 0};
6149 QCBORDecode_Init(&DCtx,
6150 Empty,
6151 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade9b334962020-08-27 10:55:53 -07006152
Laurence Lundbladee3553422020-05-02 11:11:17 -07006153 uCBORError = QCBORDecode_Finish(&DCtx);
6154 if(uCBORError != QCBOR_SUCCESS) {
6155 return 5;
6156 }
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07006157
6158
Laurence Lundbladee3553422020-05-02 11:11:17 -07006159 // --- Sequence with unclosed indefinite length array ---
6160 static const uint8_t xx[] = {0x01, 0x9f, 0x02};
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07006161
Laurence Lundbladee3553422020-05-02 11:11:17 -07006162 QCBORDecode_Init(&DCtx,
6163 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(xx),
6164 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07006165
Laurence Lundbladee3553422020-05-02 11:11:17 -07006166 // Get the first item
6167 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
6168 if(uCBORError != QCBOR_SUCCESS) {
6169 return 7;
6170 }
6171 if(Item.uDataType != QCBOR_TYPE_INT64) {
6172 return 8;
6173 }
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07006174
Laurence Lundbladee3553422020-05-02 11:11:17 -07006175 // Get a second item
6176 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08006177#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
Laurence Lundbladee3553422020-05-02 11:11:17 -07006178 if(uCBORError != QCBOR_SUCCESS) {
6179 return 9;
6180 }
6181 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
6182 return 10;
6183 }
6184
6185 // Try to finish before consuming all bytes to confirm
6186 // that the still-open error is returned.
6187 uCBORError = QCBORDecode_Finish(&DCtx);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006188 if(uCBORError != QCBOR_ERR_ARRAY_OR_MAP_UNCONSUMED) {
Laurence Lundbladee3553422020-05-02 11:11:17 -07006189 return 11;
6190 }
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08006191#else /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
6192 if(uCBORError != QCBOR_ERR_INDEF_LEN_ARRAYS_DISABLED) {
6193 return 20;
6194 }
6195#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
Laurence Lundbladee3553422020-05-02 11:11:17 -07006196
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07006197
Laurence Lundbladee3553422020-05-02 11:11:17 -07006198 // --- Sequence with a closed indefinite length array ---
6199 static const uint8_t yy[] = {0x01, 0x9f, 0xff};
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07006200
Laurence Lundbladee3553422020-05-02 11:11:17 -07006201 QCBORDecode_Init(&DCtx,
6202 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(yy),
6203 QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07006204
Laurence Lundbladee3553422020-05-02 11:11:17 -07006205 // Get the first item
6206 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
6207 if(uCBORError != QCBOR_SUCCESS) {
6208 return 12;
6209 }
6210 if(Item.uDataType != QCBOR_TYPE_INT64) {
6211 return 13;
6212 }
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07006213
Laurence Lundbladee3553422020-05-02 11:11:17 -07006214 // Get a second item
6215 uCBORError = QCBORDecode_GetNext(&DCtx, &Item);
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08006216#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
6217
Laurence Lundbladee3553422020-05-02 11:11:17 -07006218 if(uCBORError != QCBOR_SUCCESS) {
6219 return 14;
6220 }
6221 if(Item.uDataType != QCBOR_TYPE_ARRAY) {
6222 return 15;
6223 }
6224
6225 // Try to finish before consuming all bytes to confirm
6226 // that the still-open error is returned.
6227 uCBORError = QCBORDecode_Finish(&DCtx);
6228 if(uCBORError != QCBOR_SUCCESS) {
6229 return 16;
6230 }
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08006231#else /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
6232 if(uCBORError != QCBOR_ERR_INDEF_LEN_ARRAYS_DISABLED) {
6233 return 20;
6234 }
6235#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
Laurence Lundbladee3553422020-05-02 11:11:17 -07006236
Laurence Lundblade2feb1e12020-07-15 03:50:45 -07006237
Laurence Lundbladee3553422020-05-02 11:11:17 -07006238 return 0;
6239}
6240
Laurence Lundbladee15326f2020-06-15 15:50:23 -07006241
Laurence Lundblade70ecead2020-06-15 19:40:06 -07006242
Laurence Lundbladee15326f2020-06-15 15:50:23 -07006243int32_t IntToTests()
6244{
6245 int nErrCode;
6246 int32_t n32;
6247 int16_t n16;
6248 int8_t n8;
6249 uint32_t u32;
6250 uint16_t u16;
6251 uint8_t u8;
6252 uint64_t u64;
6253
6254 nErrCode = QCBOR_Int64ToInt32(1, &n32);
6255 if(nErrCode == -1 || n32 != 1) {
6256 return 1;
6257 }
6258
6259 nErrCode = QCBOR_Int64ToInt32((int64_t)INT32_MAX, &n32);
6260 if(nErrCode == -1 || n32 != INT32_MAX) {
6261 return 2;
6262 }
6263
6264 nErrCode = QCBOR_Int64ToInt32((int64_t)INT32_MIN, &n32);
6265 if(nErrCode == -1 || n32 != INT32_MIN) {
6266 return 3;
6267 }
6268
6269 nErrCode = QCBOR_Int64ToInt32(((int64_t)INT32_MAX)+1, &n32);
6270 if(nErrCode != -1) {
6271 return 4;
6272 }
6273
6274 nErrCode = QCBOR_Int64ToInt32(((int64_t)INT32_MIN)-1, &n32);
6275 if(nErrCode != -1) {
6276 return 5;
6277 }
6278
6279
6280 nErrCode = QCBOR_Int64ToInt16((int64_t)INT16_MAX, &n16);
6281 if(nErrCode == -1 || n16 != INT16_MAX) {
6282 return 6;
6283 }
6284
6285 nErrCode = QCBOR_Int64ToInt16((int64_t)INT16_MIN, &n16);
6286 if(nErrCode == -1 || n16 != INT16_MIN) {
6287 return 7;
6288 }
6289
6290 nErrCode = QCBOR_Int64ToInt16(1, &n16);
6291 if(nErrCode == -1 || n16 != 1) {
6292 return 8;
6293 }
6294
6295 nErrCode = QCBOR_Int64ToInt16(((int64_t)INT16_MAX)+1, &n16);
6296 if(nErrCode != -1) {
6297 return 9;
6298 }
6299
6300 nErrCode = QCBOR_Int64ToInt16(((int64_t)INT16_MIN)-1, &n16);
6301 if(nErrCode != -1) {
6302 return 10;
6303 }
6304
6305
6306 nErrCode = QCBOR_Int64ToInt8(1, &n8);
6307 if(nErrCode == -1 || n8 != 1) {
6308 return 11;
6309 }
6310
6311 nErrCode = QCBOR_Int64ToInt8((int64_t)INT8_MAX, &n8);
6312 if(nErrCode == -1 || n8 != INT8_MAX) {
6313 return 12;
6314 }
6315
6316 nErrCode = QCBOR_Int64ToInt8((int64_t)INT8_MIN, &n8);
6317 if(nErrCode == -1 || n8 != INT8_MIN) {
6318 return 13;
6319 }
6320
6321 nErrCode = QCBOR_Int64ToInt8(((int64_t)INT8_MAX)+1, &n8);
6322 if(nErrCode != -1) {
6323 return 14;
6324 }
6325
6326 nErrCode = QCBOR_Int64ToInt8(((int64_t)INT8_MIN)-1, &n8);
6327 if(nErrCode != -1) {
6328 return 15;
6329 }
6330
6331
6332 nErrCode = QCBOR_Int64ToUInt32(1, &u32);
6333 if(nErrCode == -1 || u32 != 1) {
6334 return 16;
6335 }
6336
6337 nErrCode = QCBOR_Int64ToUInt32((int64_t)UINT32_MAX, &u32);
6338 if(nErrCode == -1 || u32 != UINT32_MAX) {
6339 return 17;
6340 }
6341
6342 nErrCode = QCBOR_Int64ToUInt32((int64_t)0, &u32);
6343 if(nErrCode == -1 || u32 != 0) {
6344 return 18;
6345 }
6346
6347 nErrCode = QCBOR_Int64ToUInt32(((int64_t)UINT32_MAX)+1, &u32);
6348 if(nErrCode != -1) {
6349 return 19;
6350 }
6351
6352 nErrCode = QCBOR_Int64ToUInt32((int64_t)-1, &u32);
6353 if(nErrCode != -1) {
6354 return 20;
6355 }
6356
6357
6358 nErrCode = QCBOR_Int64UToInt16((int64_t)UINT16_MAX, &u16);
6359 if(nErrCode == -1 || u16 != UINT16_MAX) {
6360 return 21;
6361 }
6362
6363 nErrCode = QCBOR_Int64UToInt16((int64_t)0, &u16);
6364 if(nErrCode == -1 || u16 != 0) {
6365 return 22;
6366 }
6367
6368 nErrCode = QCBOR_Int64UToInt16(1, &u16);
6369 if(nErrCode == -1 || u16 != 1) {
6370 return 23;
6371 }
6372
6373 nErrCode = QCBOR_Int64UToInt16(((int64_t)UINT16_MAX)+1, &u16);
6374 if(nErrCode != -1) {
6375 return 24;
6376 }
6377
6378 nErrCode = QCBOR_Int64UToInt16((int64_t)-1, &u16);
6379 if(nErrCode != -1) {
6380 return 25;
6381 }
6382
6383
6384 nErrCode = QCBOR_Int64ToUInt8((int64_t)UINT8_MAX, &u8);
6385 if(nErrCode == -1 || u8 != UINT8_MAX) {
6386 return 26;
6387 }
6388
6389 nErrCode = QCBOR_Int64ToUInt8((int64_t)0, &u8);
6390 if(nErrCode == -1 || u8 != 0) {
6391 return 27;
6392 }
6393
6394 nErrCode = QCBOR_Int64ToUInt8(1, &u8);
6395 if(nErrCode == -1 || u8 != 1) {
6396 return 28;
6397 }
6398
6399 nErrCode = QCBOR_Int64ToUInt8(((int64_t)UINT16_MAX)+1, &u8);
6400 if(nErrCode != -1) {
6401 return 29;
6402 }
6403
6404 nErrCode = QCBOR_Int64ToUInt8((int64_t)-1, &u8);
6405 if(nErrCode != -1) {
6406 return 30;
6407 }
6408
6409
6410 nErrCode = QCBOR_Int64ToUInt64(1, &u64);
6411 if(nErrCode == -1 || u64 != 1) {
6412 return 31;
6413 }
6414
6415 nErrCode = QCBOR_Int64ToUInt64(INT64_MAX, &u64);
6416 if(nErrCode == -1 || u64 != INT64_MAX) {
6417 return 32;
6418 }
6419
6420 nErrCode = QCBOR_Int64ToUInt64((int64_t)0, &u64);
6421 if(nErrCode == -1 || u64 != 0) {
6422 return 33;
6423 }
6424
6425 nErrCode = QCBOR_Int64ToUInt64((int64_t)-1, &u64);
6426 if(nErrCode != -1) {
6427 return 34;
6428 }
6429
6430 return 0;
6431}
6432
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006433
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006434
6435
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006436/*
6437A sequence with
6438 A wrapping bstr
6439 containing a map
6440 1
6441 2
6442 A wrapping bstr
6443 containing an array
6444 3
6445 wrapping bstr
6446 4
6447 5
6448 6
6449 array
6450 7
6451 8
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006452 */
6453
Laurence Lundblade55013642020-09-23 05:39:22 -07006454static UsefulBufC EncodeBstrWrapTestData(UsefulBuf OutputBuffer)
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006455{
Laurence Lundblade55013642020-09-23 05:39:22 -07006456 UsefulBufC Encoded;
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006457 QCBOREncodeContext EC;
Laurence Lundblade55013642020-09-23 05:39:22 -07006458 QCBORError uErr;
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006459
Laurence Lundblade55013642020-09-23 05:39:22 -07006460 QCBOREncode_Init(&EC, OutputBuffer);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006461
6462 QCBOREncode_BstrWrap(&EC);
6463 QCBOREncode_OpenMap(&EC);
6464 QCBOREncode_AddInt64ToMapN(&EC, 100, 1);
6465 QCBOREncode_AddInt64ToMapN(&EC, 200, 2);
6466 QCBOREncode_CloseMap(&EC);
6467 QCBOREncode_BstrWrap(&EC);
6468 QCBOREncode_OpenArray(&EC);
6469 QCBOREncode_AddInt64(&EC, 3);
6470 QCBOREncode_BstrWrap(&EC);
6471 QCBOREncode_AddInt64(&EC, 4);
6472 QCBOREncode_CloseBstrWrap(&EC, NULL);
6473 QCBOREncode_AddInt64(&EC, 5);
6474 QCBOREncode_CloseArray(&EC);
6475 QCBOREncode_CloseBstrWrap(&EC, NULL);
6476 QCBOREncode_AddInt64(&EC, 6);
6477 QCBOREncode_CloseBstrWrap(&EC, NULL);
6478 QCBOREncode_OpenArray(&EC);
6479 QCBOREncode_AddInt64(&EC, 7);
6480 QCBOREncode_AddInt64(&EC, 8);
6481 QCBOREncode_CloseArray(&EC);
6482
6483 uErr = QCBOREncode_Finish(&EC, &Encoded);
Laurence Lundblade40a04322020-06-27 22:52:52 -07006484 if(uErr) {
6485 Encoded = NULLUsefulBufC;
6486 }
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006487
6488 return Encoded;
6489}
6490
Laurence Lundbladecc7da412020-12-27 00:09:07 -08006491/* h'FF' */
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08006492static const uint8_t spBreakInByteString[] = {
6493 0x41, 0xff
6494};
6495
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006496
6497int32_t EnterBstrTest()
6498{
Laurence Lundblade8510f8c2020-12-01 11:31:16 -08006499 UsefulBuf_MAKE_STACK_UB(OutputBuffer, 100);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006500
6501 QCBORDecodeContext DC;
6502
Laurence Lundblade55013642020-09-23 05:39:22 -07006503 QCBORDecode_Init(&DC, EncodeBstrWrapTestData(OutputBuffer), 0);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006504
Laurence Lundblade55013642020-09-23 05:39:22 -07006505 int64_t n1, n2, n3, n4, n5, n6, n7, n8;
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006506
6507
Laurence Lundblade9b334962020-08-27 10:55:53 -07006508 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006509 QCBORDecode_EnterMap(&DC, NULL);
Laurence Lundblade55013642020-09-23 05:39:22 -07006510 QCBORDecode_GetInt64InMapN(&DC, 100, &n1);
6511 QCBORDecode_GetInt64InMapN(&DC, 200, &n2);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006512 QCBORDecode_ExitMap(&DC);
Laurence Lundblade9b334962020-08-27 10:55:53 -07006513 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006514 QCBORDecode_EnterArray(&DC, NULL);
Laurence Lundblade55013642020-09-23 05:39:22 -07006515 QCBORDecode_GetInt64(&DC, &n3);
Laurence Lundblade9b334962020-08-27 10:55:53 -07006516 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
Laurence Lundblade55013642020-09-23 05:39:22 -07006517 QCBORDecode_GetInt64(&DC, &n4);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006518 QCBORDecode_ExitBstrWrapped(&DC);
Laurence Lundblade55013642020-09-23 05:39:22 -07006519 QCBORDecode_GetInt64(&DC, &n5);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006520 QCBORDecode_ExitArray(&DC);
6521 QCBORDecode_ExitBstrWrapped(&DC);
Laurence Lundblade55013642020-09-23 05:39:22 -07006522 QCBORDecode_GetInt64(&DC, &n6);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006523 QCBORDecode_ExitBstrWrapped(&DC);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006524 QCBORDecode_EnterArray(&DC, NULL);
Laurence Lundblade55013642020-09-23 05:39:22 -07006525 QCBORDecode_GetInt64(&DC, &n7);
6526 QCBORDecode_GetInt64(&DC, &n8);
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006527 QCBORDecode_ExitArray(&DC);
6528
6529 QCBORError uErr = QCBORDecode_Finish(&DC);
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08006530 if(uErr) {
6531 return (int32_t)uErr;
6532 }
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006533
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08006534
6535 /* Enter and exit byte string wrapped CBOR that is bad. It has just a break.
6536 * Successful because no items are fetched from byte string.
6537 */
6538 QCBORDecode_Init(&DC,
6539 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBreakInByteString),
6540 0);
6541 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
6542 uErr = QCBORDecode_GetError(&DC);
6543 if(uErr) {
6544 return 100 + (int32_t)uErr;
6545 }
6546
6547 QCBORDecode_ExitBstrWrapped(&DC);
6548 uErr = QCBORDecode_GetError(&DC);
6549 if(uErr) {
6550 return 200 + (int32_t)uErr;
6551 }
6552
6553 /* Try to get item that is a break out of a byte string wrapped CBOR.
6554 * It fails because there should be no break.
6555 */
6556 QCBORDecode_Init(&DC,
6557 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBreakInByteString),
6558 0);
6559 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
6560 QCBORItem Item;
6561 uErr = QCBORDecode_GetNext(&DC, &Item);
6562 if(uErr != QCBOR_ERR_BAD_BREAK) {
6563 return 300 + (int32_t)uErr;
6564 }
6565
6566 return 0;
Laurence Lundblade0750fc42020-06-20 21:02:34 -07006567}
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006568
6569
6570
6571
6572static const uint8_t spTaggedTypes[] = {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006573 0xb2,
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006574
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006575 // Date string
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006576 0x00,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006577 0xc0, 0x74, 0x32, 0x30, 0x30, 0x33, 0x2D, 0x31, 0x32, 0x2D,
6578 0x31, 0x33, 0x54, 0x31, 0x38, 0x3A, 0x33, 0x30, 0x3A, 0x30,
6579 0x32, 0x5A,
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006580
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006581 0x01,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006582 0x74, 0x32, 0x30, 0x30, 0x33, 0x2D, 0x31, 0x32, 0x2D, 0x31,
6583 0x33, 0x54, 0x31, 0x38, 0x3A, 0x33, 0x30, 0x3A, 0x30, 0x32,
6584 0x5A,
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006585
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006586 // Bignum
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006587 10,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006588 0xC2, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
6589 0x09, 0x10,
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006590
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006591 11,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006592 0xC3, 0x4A, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08,
6593 0x09, 0x10,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006594
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006595 // URL
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006596 20,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006597 0xd8, 0x20, 0x6f, 0x68, 0x74, 0x74, 0x70, 0x3A, 0x2F, 0x2F,
6598 0x63, 0x62, 0x6F, 0x72, 0x2E, 0x6D, 0x65, 0x2F,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006599
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006600 21,
6601 0x6f, 0x68, 0x74, 0x74, 0x70, 0x3A, 0x2F, 0x2F, 0x63, 0x62,
6602 0x6F, 0x72, 0x2E, 0x6D, 0x65, 0x2F,
6603
6604 // B64
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006605 0x18, 0x1e,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006606 0xd8, 0x22, 0x6c, 0x63, 0x47, 0x78, 0x6C, 0x59, 0x58, 0x4E,
6607 0x31, 0x63, 0x6D, 0x55, 0x75,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006608
6609 0x18, 0x1f,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006610 0x6c, 0x63, 0x47, 0x78, 0x6C, 0x59, 0x58, 0x4E, 0x31, 0x63,
6611 0x6D, 0x55, 0x75,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006612
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006613 // B64URL
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006614 0x18, 0x28,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006615 0xd8, 0x21, 0x6c, 0x63, 0x47, 0x78, 0x6C, 0x59, 0x58, 0x4E,
6616 0x31, 0x63, 0x6D, 0x55, 0x75,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006617
6618 0x18, 0x29,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006619 0x6c, 0x63, 0x47, 0x78, 0x6C, 0x59, 0x58, 0x4E, 0x31, 0x63,
6620 0x6D, 0x55, 0x75,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006621
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006622 // Regex
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006623 0x18, 0x32,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006624 0xd8, 0x23, 0x68, 0x31, 0x30, 0x30, 0x5C, 0x73, 0x2A, 0x6D,
6625 0x6B,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006626
6627 0x18, 0x33,
6628 0x68, 0x31, 0x30, 0x30, 0x5C, 0x73, 0x2A, 0x6D, 0x6B,
6629
6630 // MIME
6631 0x18, 0x3c,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006632 0xd8, 0x24, 0x72, 0x4D, 0x49, 0x4D, 0x45, 0x2D, 0x56, 0x65,
6633 0x72, 0x73, 0x69, 0x6F, 0x6E, 0x3A, 0x20, 0x31, 0x2E, 0x30,
6634 0x0A,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006635
6636 0x18, 0x3d,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006637 0x72, 0x4D, 0x49, 0x4D, 0x45, 0x2D, 0x56, 0x65, 0x72, 0x73,
6638 0x69, 0x6F, 0x6E, 0x3A, 0x20, 0x31, 0x2E, 0x30, 0x0A,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006639
6640 0x18, 0x3e,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006641 0xd9, 0x01, 0x01, 0x52, 0x4D, 0x49, 0x4D, 0x45, 0x2D, 0x56,
6642 0x65, 0x72, 0x73, 0x69, 0x6F, 0x6E, 0x3A, 0x20, 0x31, 0x2E,
6643 0x30, 0x0A,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006644
6645 0x18, 0x3f,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006646 0x52, 0x4D, 0x49, 0x4D, 0x45, 0x2D, 0x56, 0x65, 0x72, 0x73,
6647 0x69, 0x6F, 0x6E, 0x3A, 0x20, 0x31, 0x2E, 0x30, 0x0A,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006648
6649 // UUID
6650 0x18, 0x46,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006651 0xd8, 0x25, 0x50, 0x53, 0x4D, 0x41, 0x52, 0x54, 0x43, 0x53,
6652 0x4C, 0x54, 0x54, 0x43, 0x46, 0x49, 0x43, 0x41, 0x32,
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006653
6654 0x18, 0x47,
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006655 0x50, 0x53, 0x4D, 0x41, 0x52, 0x54, 0x43, 0x53, 0x4C, 0x54,
6656 0x54, 0x43, 0x46, 0x49, 0x43, 0x41, 0x32
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006657};
6658
6659int32_t DecodeTaggedTypeTests()
6660{
6661 QCBORDecodeContext DC;
6662 QCBORError uErr;
6663
6664 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTaggedTypes), 0);
6665
6666 UsefulBufC String;
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006667 bool bNeg;
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006668
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006669 QCBORDecode_EnterMap(&DC, NULL);
Laurence Lundblade9b334962020-08-27 10:55:53 -07006670 QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006671 QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &String);
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006672 if(QCBORDecode_GetError(&DC) != QCBOR_SUCCESS) {
6673 return 1;
6674 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006675 QCBORDecode_GetDateStringInMapN(&DC, 0, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006676 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_UNEXPECTED_TYPE) {
6677 return 2;
6678 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006679 QCBORDecode_GetDateStringInMapN(&DC, 1, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006680 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_UNEXPECTED_TYPE) {
6681 return 3;
6682 }
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006683 QCBORDecode_GetDateStringInMapN(&DC, 1, QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &String);
Laurence Lundblade9b334962020-08-27 10:55:53 -07006684 QCBORDecode_GetDateStringInMapN(&DC, 1, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006685 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6686 return 4;
6687 }
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006688 QCBORDecode_GetDateStringInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_OPTIONAL_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006689 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006690 return 5;
6691 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006692
Laurence Lundblade9b334962020-08-27 10:55:53 -07006693 QCBORDecode_GetBignumInMapN(&DC, 10, QCBOR_TAG_REQUIREMENT_TAG, &String, &bNeg);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006694 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
6695 bNeg != false) {
6696 return 10;
6697 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006698 QCBORDecode_GetBignumInMapN(&DC, 11, QCBOR_TAG_REQUIREMENT_TAG, &String, &bNeg);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006699 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
6700 bNeg != true) {
6701 return 11;
6702 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006703 QCBORDecode_GetBignumInMapN(&DC, 11, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bNeg);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006704 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_UNEXPECTED_TYPE) {
6705 return 12;
6706 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006707 QCBORDecode_GetBignumInMapN(&DC, 14, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bNeg);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006708 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006709 return 13;
6710 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006711 QCBORDecode_GetBignumInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bNeg);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006712 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006713 return 14;
6714 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006715
Laurence Lundblade9b334962020-08-27 10:55:53 -07006716 QCBORDecode_GetURIInMapN(&DC, 20, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006717 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6718 return 20;
6719 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006720 QCBORDecode_GetURIInMapN(&DC, 21, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006721 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6722 return 21;
6723 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006724 QCBORDecode_GetURIInMapN(&DC, 22, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006725 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006726 return 22;
6727 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006728 QCBORDecode_GetURIInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006729 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006730 return 23;
6731 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006732
Laurence Lundblade24bd7e12021-01-09 00:05:11 -08006733#ifndef QCBOR_DISABLE_UNCOMMON_TAGS
Laurence Lundblade9b334962020-08-27 10:55:53 -07006734 QCBORDecode_GetB64InMapN(&DC, 30, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006735 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6736 return 30;
6737 }
Laurence Lundblade24bd7e12021-01-09 00:05:11 -08006738#endif
Laurence Lundblade9b334962020-08-27 10:55:53 -07006739 QCBORDecode_GetB64InMapN(&DC, 31, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006740 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6741 return 31;
6742 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006743 QCBORDecode_GetB64InMapN(&DC, 32, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006744 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006745 return 32;
6746 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006747 QCBORDecode_GetB64InMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006748 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006749 return 33;
6750 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006751
Laurence Lundblade24bd7e12021-01-09 00:05:11 -08006752#ifndef QCBOR_DISABLE_UNCOMMON_TAGS
Laurence Lundblade9b334962020-08-27 10:55:53 -07006753 QCBORDecode_GetB64URLInMapN(&DC, 40, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006754 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6755 return 40;
6756 }
Laurence Lundblade24bd7e12021-01-09 00:05:11 -08006757#endif
Laurence Lundblade9b334962020-08-27 10:55:53 -07006758 QCBORDecode_GetB64URLInMapN(&DC, 41, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006759 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6760 return 41;
6761 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006762 QCBORDecode_GetB64URLInMapN(&DC, 42, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006763 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006764 return 42;
6765 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006766 QCBORDecode_GetB64URLInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_NOT_A_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 43;
6769 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006770
Laurence Lundblade24bd7e12021-01-09 00:05:11 -08006771#ifndef QCBOR_DISABLE_UNCOMMON_TAGS
Laurence Lundblade9b334962020-08-27 10:55:53 -07006772 QCBORDecode_GetRegexInMapN(&DC, 50, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006773 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6774 return 50;
6775 }
Laurence Lundblade24bd7e12021-01-09 00:05:11 -08006776#endif
Laurence Lundblade9b334962020-08-27 10:55:53 -07006777 QCBORDecode_GetRegexInMapN(&DC, 51, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006778 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6779 return 51;
6780 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006781 QCBORDecode_GetRegexInMapN(&DC, 52, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006782 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006783 return 52;
6784 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006785 QCBORDecode_GetRegexInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_TAG, &String);
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 53;
6788 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006789
Laurence Lundblade24bd7e12021-01-09 00:05:11 -08006790#ifndef QCBOR_DISABLE_UNCOMMON_TAGS
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006791 // MIME
6792 bool bIsNot7Bit;
Laurence Lundblade9b334962020-08-27 10:55:53 -07006793 QCBORDecode_GetMIMEMessageInMapN(&DC, 60, QCBOR_TAG_REQUIREMENT_TAG, &String, &bIsNot7Bit);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006794 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
6795 bIsNot7Bit == true) {
6796 return 60;
6797 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006798 QCBORDecode_GetMIMEMessageInMapN(&DC, 61, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bIsNot7Bit);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006799 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
6800 bIsNot7Bit == true) {
6801 return 61;
6802 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006803 QCBORDecode_GetMIMEMessageInMapN(&DC, 62, QCBOR_TAG_REQUIREMENT_TAG, &String, &bIsNot7Bit);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006804 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
6805 bIsNot7Bit == false) {
6806 return 62;
6807 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006808 QCBORDecode_GetMIMEMessageInMapN(&DC, 63, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String, &bIsNot7Bit);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006809 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS ||
6810 bIsNot7Bit == false) {
6811 return 63;
6812 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006813 QCBORDecode_GetMIMEMessageInMapN(&DC, 64, QCBOR_TAG_REQUIREMENT_TAG, &String, &bNeg);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006814 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006815 return 64;
6816 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006817 QCBORDecode_GetMIMEMessageInMapSZ(&DC, "zzz", QCBOR_TAG_REQUIREMENT_TAG, &String, &bNeg);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006818 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006819 return 65;
6820 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006821
Laurence Lundblade24bd7e12021-01-09 00:05:11 -08006822
Laurence Lundblade9b334962020-08-27 10:55:53 -07006823 QCBORDecode_GetBinaryUUIDInMapN(&DC, 70, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006824 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6825 return 70;
6826 }
Laurence Lundblade24bd7e12021-01-09 00:05:11 -08006827#endif /* #ifndef QCBOR_DISABLE_UNCOMMON_TAGS */
6828
Laurence Lundblade9b334962020-08-27 10:55:53 -07006829 QCBORDecode_GetBinaryUUIDInMapN(&DC, 71, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, &String);
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006830 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_SUCCESS) {
6831 return 71;
6832 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006833 QCBORDecode_GetBinaryUUIDInMapN(&DC, 72, QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006834 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006835 return 72;
6836 }
Laurence Lundblade9b334962020-08-27 10:55:53 -07006837 QCBORDecode_GetBinaryUUIDInMapSZ(&DC, "xxx", QCBOR_TAG_REQUIREMENT_TAG, &String);
Laurence Lundbladea9489f82020-09-12 13:50:56 -07006838 if(QCBORDecode_GetAndResetError(&DC) != QCBOR_ERR_LABEL_NOT_FOUND) {
Laurence Lundblade2f5e16d2020-08-04 20:35:23 -07006839 return 73;
6840 }
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006841
Laurence Lundblade9bb039a2020-08-05 12:25:15 -07006842 // Improvement: add some more error test cases
6843
Laurence Lundblade37f46e52020-08-04 03:32:14 -07006844 QCBORDecode_ExitMap(&DC);
6845
6846 uErr = QCBORDecode_Finish(&DC);
6847 if(uErr != QCBOR_SUCCESS) {
6848 return 100;
6849 }
6850
6851 return 0;
6852}
Laurence Lundbladea4308a82020-10-03 18:08:57 -07006853
6854
6855
6856
6857/*
Laurence Lundbladecc7da412020-12-27 00:09:07 -08006858 [
6859 "aaaaaaaaaa",
6860 {}
6861 ]
Laurence Lundbladea4308a82020-10-03 18:08:57 -07006862 */
6863static const uint8_t spTooLarge1[] = {
6864 0x9f,
6865 0x6a, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61,
6866 0xa0,
6867 0xff
6868};
6869
6870/*
Laurence Lundbladecc7da412020-12-27 00:09:07 -08006871 [
6872 {
6873 0: "aaaaaaaaaa"
6874 }
6875 ]
Laurence Lundbladea4308a82020-10-03 18:08:57 -07006876 */
6877static const uint8_t spTooLarge2[] = {
6878 0x9f,
6879 0xa1,
6880 0x00,
6881 0x6a, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61,
6882 0xff
6883};
6884
6885/*
Laurence Lundbladecc7da412020-12-27 00:09:07 -08006886 h'A1006A61616161616161616161'
Laurence Lundbladea4308a82020-10-03 18:08:57 -07006887
Laurence Lundbladecc7da412020-12-27 00:09:07 -08006888 {
6889 0: "aaaaaaaaaa"
6890 }
Laurence Lundbladea4308a82020-10-03 18:08:57 -07006891 */
6892static const uint8_t spTooLarge3[] = {
6893 0x4d,
6894 0xa1,
6895 0x00,
6896 0x6a, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61, 0x61,
6897};
6898
6899int32_t TooLargeInputTest(void)
6900{
6901 QCBORDecodeContext DC;
6902 QCBORError uErr;
6903 UsefulBufC String;
6904
6905 // These tests require a build with QCBOR_MAX_DECODE_INPUT_SIZE set
6906 // to 10 There's not really any way to test this error
6907 // condition. The error condition is not complex, so setting
6908 // QCBOR_MAX_DECODE_INPUT_SIZE gives an OK test.
6909
6910 // The input CBOR is only too large because the
6911 // QCBOR_MAX_DECODE_INPUT_SIZE is 10.
6912 //
6913 // This test is disabled for the normal test runs because of the
6914 // special build requirement.
6915
6916
6917 // Tests the start of a map being too large
6918 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTooLarge1), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006919 QCBORDecode_EnterArray(&DC, NULL);
Laurence Lundbladea4308a82020-10-03 18:08:57 -07006920 QCBORDecode_GetTextString(&DC, &String);
6921 uErr = QCBORDecode_GetError(&DC);
6922 if(uErr != QCBOR_SUCCESS) {
6923 return 1;
6924 }
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006925 QCBORDecode_EnterMap(&DC, NULL);
Laurence Lundbladea4308a82020-10-03 18:08:57 -07006926 uErr = QCBORDecode_GetError(&DC);
6927 if(uErr != QCBOR_ERR_INPUT_TOO_LARGE) {
6928 return 2;
6929 }
6930
6931 // Tests the end of a map being too large
6932 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTooLarge2), QCBOR_DECODE_MODE_NORMAL);
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006933 QCBORDecode_EnterArray(&DC, NULL);
6934 QCBORDecode_EnterMap(&DC, NULL);
Laurence Lundbladea4308a82020-10-03 18:08:57 -07006935 uErr = QCBORDecode_GetError(&DC);
6936 if(uErr != QCBOR_SUCCESS) {
6937 return 3;
6938 }
6939 QCBORDecode_ExitMap(&DC);
6940 uErr = QCBORDecode_GetError(&DC);
6941 if(uErr != QCBOR_ERR_INPUT_TOO_LARGE) {
6942 return 4;
6943 }
6944
6945 // Tests the entire input CBOR being too large when processing bstr wrapping
6946 QCBORDecode_Init(&DC, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spTooLarge3), QCBOR_DECODE_MODE_NORMAL);
6947 QCBORDecode_EnterBstrWrapped(&DC, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
6948 uErr = QCBORDecode_GetError(&DC);
6949 if(uErr != QCBOR_ERR_INPUT_TOO_LARGE) {
6950 return 5;
6951 }
6952
6953 return 0;
6954}
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07006955
6956
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08006957#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
6958
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07006959static const uint8_t spMapWithIndefLenStrings[] = {
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08006960 0xa3,
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07006961 0x7f, 0x61, 'l', 0x64, 'a', 'b', 'e', 'l' , 0x61, '1', 0xff,
6962 0x5f, 0x42, 0x01, 0x02, 0x43, 0x03, 0x04, 0x05, 0xff,
6963 0x7f, 0x62, 'd', 'y', 0x61, 'm', 0x61, 'o', 0xff,
6964 0x03,
6965 0x7f, 0x62, 'l', 'a', 0x63, 'b', 'e', 'l', 0x61, '2', 0xff,
6966 0xc3,
6967 0x5f, 0x42, 0x00, 0x01, 0x42, 0x00, 0x01, 0x41, 0x01, 0xff,
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07006968};
6969
6970int32_t SpiffyIndefiniteLengthStringsTests()
6971{
6972 QCBORDecodeContext DCtx;
6973
6974 QCBORDecode_Init(&DCtx,
6975 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spMapWithIndefLenStrings),
6976 QCBOR_DECODE_MODE_NORMAL);
6977
Laurence Lundblade8510f8c2020-12-01 11:31:16 -08006978 UsefulBuf_MAKE_STACK_UB(StringBuf, 200);
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07006979 QCBORDecode_SetMemPool(&DCtx, StringBuf, false);
6980
6981 UsefulBufC ByteString;
Laurence Lundblade6545d1b2020-10-14 11:13:13 -07006982 QCBORDecode_EnterMap(&DCtx, NULL);
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07006983 QCBORDecode_GetByteStringInMapSZ(&DCtx, "label1", &ByteString);
6984 if(QCBORDecode_GetAndResetError(&DCtx)) {
6985 return 1;
6986 }
6987
6988 const uint8_t pExectedBytes[] = {0x01, 0x02, 0x03, 0x04, 0x05};
6989 if(UsefulBuf_Compare(ByteString, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pExectedBytes))) {
6990 return 2;
6991 }
6992
6993 uint64_t uInt;
6994 QCBORDecode_GetUInt64InMapSZ(&DCtx, "dymo", &uInt);
6995 if(QCBORDecode_GetAndResetError(&DCtx)) {
6996 return 3;
6997 }
6998 if(uInt != 3) {
6999 return 4;
7000 }
7001
Máté Tóth-Pálef5f07a2021-09-17 19:31:37 +02007002#ifndef USEFULBUF_DISABLE_ALL_FLOAT
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07007003 double uDouble;
7004 QCBORDecode_GetDoubleConvertAllInMapSZ(&DCtx,
7005 "label2",
7006 0xff,
7007 &uDouble);
Laurence Lundbladeb8e19aa2020-10-07 20:59:11 -07007008#ifndef QCBOR_DISABLE_FLOAT_HW_USE
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07007009 if(QCBORDecode_GetAndResetError(&DCtx)) {
7010 return 5;
7011 }
7012 if(uDouble != -16777474) {
7013 return 6;
7014 }
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08007015#else /* QCBOR_DISABLE_FLOAT_HW_USE */
Laurence Lundbladeb8e19aa2020-10-07 20:59:11 -07007016 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_HW_FLOAT_DISABLED) {
7017 return 7;
7018 }
Laurence Lundbladee2c893c2020-12-26 17:41:53 -08007019#endif /* QCBOR_DISABLE_FLOAT_HW_USE */
Máté Tóth-Pálef5f07a2021-09-17 19:31:37 +02007020#endif /* USEFULBUF_DISABLE_ALL_FLOAT */
Laurence Lundbladeb8e19aa2020-10-07 20:59:11 -07007021
Laurence Lundbladeb6d1c692020-10-07 18:37:48 -07007022
7023 QCBORDecode_ExitMap(&DCtx);
7024
7025 if(QCBORDecode_Finish(&DCtx)) {
7026 return 99;
7027 }
7028
7029 return 0;
7030}
Laurence Lundbladef6da33c2020-11-26 18:15:05 -08007031#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
Laurence Lundblade2a26abb2020-11-05 19:06:54 -08007032
7033
Laurence Lundbladecf41c522021-02-20 10:19:07 -07007034/*
7035 * An array of an integer and an array. The second array contains
7036 * a bstr-wrapped map.
7037 *
7038 * [7, [h'A36D6669... (see next lines) 73']]
7039 *
7040 * {"first integer": 42,
7041 * "an array of two strings": ["string1", "string2"],
7042 * "map in a map":
7043 * { "bytes 1": h'78787878',
7044 * "bytes 2": h'79797979',
7045 * "another int": 98,
7046 * "text 2": "lies, damn lies and statistics"
7047 * }
7048 * }
7049 */
Laurence Lundblade2a26abb2020-11-05 19:06:54 -08007050
Laurence Lundbladecf41c522021-02-20 10:19:07 -07007051static const uint8_t pValidWrappedMapEncoded[] = {
7052 0x82, 0x07, 0x81, 0x58, 0x97,
7053 0xa3, 0x6d, 0x66, 0x69, 0x72, 0x73, 0x74, 0x20, 0x69, 0x6e,
7054 0x74, 0x65, 0x67, 0x65, 0x72, 0x18, 0x2a, 0x77, 0x61, 0x6e,
7055 0x20, 0x61, 0x72, 0x72, 0x61, 0x79, 0x20, 0x6f, 0x66, 0x20,
7056 0x74, 0x77, 0x6f, 0x20, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67,
7057 0x73, 0x82, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x31,
7058 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x32, 0x6c, 0x6d,
7059 0x61, 0x70, 0x20, 0x69, 0x6e, 0x20, 0x61, 0x20, 0x6d, 0x61,
7060 0x70, 0xa4, 0x67, 0x62, 0x79, 0x74, 0x65, 0x73, 0x20, 0x31,
7061 0x44, 0x78, 0x78, 0x78, 0x78, 0x67, 0x62, 0x79, 0x74, 0x65,
7062 0x73, 0x20, 0x32, 0x44, 0x79, 0x79, 0x79, 0x79, 0x6b, 0x61,
7063 0x6e, 0x6f, 0x74, 0x68, 0x65, 0x72, 0x20, 0x69, 0x6e, 0x74,
7064 0x18, 0x62, 0x66, 0x74, 0x65, 0x78, 0x74, 0x20, 0x32, 0x78,
7065 0x1e, 0x6c, 0x69, 0x65, 0x73, 0x2c, 0x20, 0x64, 0x61, 0x6d,
7066 0x6e, 0x20, 0x6c, 0x69, 0x65, 0x73, 0x20, 0x61, 0x6e, 0x64,
7067 0x20, 0x73, 0x74, 0x61, 0x74, 0x69, 0x73, 0x74, 0x69, 0x63,
7068 0x73
7069};
7070
7071#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
7072
7073/* As above, but the arrays are indefinite length */
7074static const uint8_t pValidIndefWrappedMapEncoded[] = {
7075 0x9f, 0x07, 0x9f, 0x58, 0x97,
7076 0xa3, 0x6d, 0x66, 0x69, 0x72, 0x73, 0x74, 0x20, 0x69, 0x6e,
7077 0x74, 0x65, 0x67, 0x65, 0x72, 0x18, 0x2a, 0x77, 0x61, 0x6e,
7078 0x20, 0x61, 0x72, 0x72, 0x61, 0x79, 0x20, 0x6f, 0x66, 0x20,
7079 0x74, 0x77, 0x6f, 0x20, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67,
7080 0x73, 0x82, 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x31,
7081 0x67, 0x73, 0x74, 0x72, 0x69, 0x6e, 0x67, 0x32, 0x6c, 0x6d,
7082 0x61, 0x70, 0x20, 0x69, 0x6e, 0x20, 0x61, 0x20, 0x6d, 0x61,
7083 0x70, 0xa4, 0x67, 0x62, 0x79, 0x74, 0x65, 0x73, 0x20, 0x31,
7084 0x44, 0x78, 0x78, 0x78, 0x78, 0x67, 0x62, 0x79, 0x74, 0x65,
7085 0x73, 0x20, 0x32, 0x44, 0x79, 0x79, 0x79, 0x79, 0x6b, 0x61,
7086 0x6e, 0x6f, 0x74, 0x68, 0x65, 0x72, 0x20, 0x69, 0x6e, 0x74,
7087 0x18, 0x62, 0x66, 0x74, 0x65, 0x78, 0x74, 0x20, 0x32, 0x78,
7088 0x1e, 0x6c, 0x69, 0x65, 0x73, 0x2c, 0x20, 0x64, 0x61, 0x6d,
7089 0x6e, 0x20, 0x6c, 0x69, 0x65, 0x73, 0x20, 0x61, 0x6e, 0x64,
7090 0x20, 0x73, 0x74, 0x61, 0x74, 0x69, 0x73, 0x74, 0x69, 0x63,
7091 0x73,
7092 0xff, 0xff
7093};
7094#endif
7095
7096
7097static const uint8_t pWithEmptyMap[] = {0x82, 0x18, 0x64, 0xa0};
7098
7099#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
7100static const uint8_t pWithEmptyMapInDef[] = {0x9f, 0x18, 0x64, 0xbf, 0xff, 0xff};
7101#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
7102
7103#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
7104static const uint8_t pWrappedByIndefiniteLength[] = {
7105 0x81,
7106 0xd8, 0x18,
7107 0x5f,
7108 0x41, 0x83,
7109 0x41, 0x18,
7110 0x43, 0x2A, 0x18, 0x2B,
7111 0x42, 0x18, 0x2C,
7112 0xff
7113};
7114#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
7115
7116
7117int32_t PeekAndRewindTest()
Laurence Lundblade2a26abb2020-11-05 19:06:54 -08007118{
7119 QCBORItem Item;
7120 QCBORError nCBORError;
7121 QCBORDecodeContext DCtx;
7122
7123 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0);
7124
7125 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
7126 return 100+(int32_t)nCBORError;
7127 }
7128 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
7129 return 200;
7130 }
7131
Laurence Lundblade3427dee2021-06-20 11:11:24 -07007132 QCBORDecode_VPeekNext(&DCtx, &Item);
7133 if((nCBORError = QCBORDecode_GetError(&DCtx))) {
7134 return 150+(int32_t)nCBORError;
7135 }
7136 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
7137 return 250;
7138 }
7139
Laurence Lundblade2a26abb2020-11-05 19:06:54 -08007140 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
7141 return (int32_t)nCBORError;
7142 }
7143 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
7144 return 300;
7145 }
7146
7147 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
7148 return 400 + (int32_t)nCBORError;
7149 }
7150 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
7151 return 500;
7152 }
7153
7154 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7155 return (int32_t)nCBORError;
7156 }
7157 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
7158 return 600;
7159 }
7160
7161 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
7162 return 900 + (int32_t)nCBORError;
7163 }
7164 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
7165 Item.uDataType != QCBOR_TYPE_INT64 ||
7166 Item.val.int64 != 42 ||
7167 Item.uDataAlloc ||
7168 Item.uLabelAlloc ||
7169 UsefulBufCompareToSZ(Item.label.string, "first integer")) {
7170 return 1000;
7171 }
7172
7173 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7174 return 1100 + (int32_t)nCBORError;
7175 }
7176
7177 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
7178 Item.uDataType != QCBOR_TYPE_INT64 ||
7179 Item.val.int64 != 42 ||
7180 Item.uDataAlloc ||
7181 Item.uLabelAlloc ||
7182 UsefulBufCompareToSZ(Item.label.string, "first integer")) {
7183 return 1200;
7184 }
7185
7186
7187 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7188 return 1300 + (int32_t)nCBORError;
7189 }
7190 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
7191 Item.uDataAlloc ||
7192 Item.uLabelAlloc ||
7193 UsefulBufCompareToSZ(Item.label.string, "an array of two strings") ||
7194 Item.uDataType != QCBOR_TYPE_ARRAY ||
Laurence Lundbladecf41c522021-02-20 10:19:07 -07007195 Item.val.uCount != 2) {
Laurence Lundblade2a26abb2020-11-05 19:06:54 -08007196 return 1400;
Laurence Lundbladecf41c522021-02-20 10:19:07 -07007197 }
Laurence Lundblade2a26abb2020-11-05 19:06:54 -08007198
7199 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7200 return 1500 + (int32_t)nCBORError;
7201 }
7202 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
7203 Item.uDataAlloc ||
7204 Item.uLabelAlloc ||
7205 UsefulBufCompareToSZ(Item.val.string, "string1")) {
7206 return 1600;
7207 }
7208
7209 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
7210 return 1700 + (int32_t)nCBORError;
7211 }
7212 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
7213 Item.uDataAlloc ||
7214 Item.uLabelAlloc ||
7215 UsefulBufCompareToSZ(Item.val.string, "string2")) {
7216 return 1800;
7217 }
7218
7219 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
7220 return (int32_t)nCBORError;
7221 }
7222 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
7223 Item.uDataAlloc ||
7224 Item.uLabelAlloc ||
7225 UsefulBufCompareToSZ(Item.val.string, "string2")) {
7226 return 1900;
7227 }
7228
7229 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7230 return (int32_t)nCBORError;
7231 }
7232 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
7233 Item.uDataAlloc ||
7234 Item.uLabelAlloc ||
7235 UsefulBufCompareToSZ(Item.val.string, "string2")) {
7236 return 2000;
7237 }
7238
7239
7240 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7241 return 2100 + (int32_t)nCBORError;
7242 }
7243 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
7244 Item.uDataAlloc ||
7245 Item.uLabelAlloc ||
7246 UsefulBufCompareToSZ(Item.label.string, "map in a map") ||
7247 Item.uDataType != QCBOR_TYPE_MAP ||
7248 Item.val.uCount != 4) {
7249 return 2100;
7250 }
7251
7252 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7253 return 2200 + (int32_t)nCBORError;
7254 }
7255 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
7256 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("bytes 1"))||
7257 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
7258 Item.uDataAlloc ||
7259 Item.uLabelAlloc ||
7260 UsefulBufCompareToSZ(Item.val.string, "xxxx")) {
7261 return 2300;
7262 }
7263
7264 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
7265 return 2400 + (int32_t)nCBORError;
7266 }
7267 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
7268 UsefulBufCompareToSZ(Item.label.string, "bytes 2") ||
7269 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
7270 Item.uDataAlloc ||
7271 Item.uLabelAlloc ||
7272 UsefulBufCompareToSZ(Item.val.string, "yyyy")) {
7273 return 2500;
7274 }
7275
7276 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7277 return 2600 + (int32_t)nCBORError;
7278 }
7279 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
7280 UsefulBufCompareToSZ(Item.label.string, "bytes 2") ||
7281 Item.uDataType != QCBOR_TYPE_BYTE_STRING ||
7282 Item.uDataAlloc ||
7283 Item.uLabelAlloc ||
7284 UsefulBufCompareToSZ(Item.val.string, "yyyy")) {
7285 return 2700;
7286 }
7287
7288 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7289 return 2800 + (int32_t)nCBORError;
7290 }
7291 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
7292 Item.uDataAlloc ||
7293 Item.uLabelAlloc ||
7294 UsefulBufCompareToSZ(Item.label.string, "another int") ||
7295 Item.uDataType != QCBOR_TYPE_INT64 ||
Laurence Lundbladecf41c522021-02-20 10:19:07 -07007296 Item.val.int64 != 98) {
Laurence Lundblade2a26abb2020-11-05 19:06:54 -08007297 return 2900;
Laurence Lundbladecf41c522021-02-20 10:19:07 -07007298 }
Laurence Lundblade2a26abb2020-11-05 19:06:54 -08007299
7300 if((nCBORError = QCBORDecode_PeekNext(&DCtx, &Item))) {
7301 return 3000 + (int32_t)nCBORError;
7302 }
7303 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
7304 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("text 2"))||
7305 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
7306 Item.uDataAlloc ||
7307 Item.uLabelAlloc ||
7308 UsefulBufCompareToSZ(Item.val.string, "lies, damn lies and statistics")) {
7309 return 3100;
7310 }
7311
7312 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7313 return 3200 + (int32_t)nCBORError;
7314 }
7315 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
7316 UsefulBuf_Compare(Item.label.string, UsefulBuf_FromSZ("text 2"))||
7317 Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
7318 Item.uDataAlloc ||
7319 Item.uLabelAlloc ||
7320 UsefulBufCompareToSZ(Item.val.string, "lies, damn lies and statistics")) {
7321 return 3300;
7322 }
7323
Laurence Lundblade3427dee2021-06-20 11:11:24 -07007324 nCBORError = QCBORDecode_PeekNext(&DCtx, &Item);
7325 if(nCBORError != QCBOR_ERR_NO_MORE_ITEMS) {
7326 return 3300 + (int32_t)nCBORError;
7327 }
7328
7329 QCBORDecode_VPeekNext(&DCtx, &Item);
7330 nCBORError = QCBORDecode_GetError(&DCtx);
7331 if(nCBORError != QCBOR_ERR_NO_MORE_ITEMS) {
7332 return 3400 + (int32_t)nCBORError;
7333 }
7334
7335 QCBORDecode_VPeekNext(&DCtx, &Item);
7336 nCBORError = QCBORDecode_GetError(&DCtx);
7337 if(nCBORError != QCBOR_ERR_NO_MORE_ITEMS) {
7338 return 3500 + (int32_t)nCBORError;
7339 }
Laurence Lundbladecf41c522021-02-20 10:19:07 -07007340
7341
7342 // Rewind to top level after entering several maps
7343 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0);
7344
7345 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7346 return (int32_t)nCBORError;
7347 }
7348 if(Item.uDataType != QCBOR_TYPE_MAP ||
7349 Item.val.uCount != 3) {
7350 return 400;
7351 }
7352
7353 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7354 return 4000+(int32_t)nCBORError;
7355 }
7356
7357 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
7358 Item.uDataType != QCBOR_TYPE_INT64 ||
7359 Item.val.int64 != 42 ||
7360 Item.uDataAlloc ||
7361 Item.uLabelAlloc ||
7362 UsefulBufCompareToSZ(Item.label.string, "first integer")) {
7363 return 4100;
7364 }
7365
7366 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7367 return 4100+(int32_t)nCBORError;
7368 }
7369 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
7370 Item.uDataAlloc ||
7371 Item.uLabelAlloc ||
7372 UsefulBufCompareToSZ(Item.label.string, "an array of two strings") ||
7373 Item.uDataType != QCBOR_TYPE_ARRAY ||
7374 Item.val.uCount != 2) {
7375 return 4200;
7376 }
7377
7378 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7379 return 4200+(int32_t)nCBORError;
7380 }
7381 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
7382 Item.uDataAlloc ||
7383 Item.uLabelAlloc ||
7384 UsefulBufCompareToSZ(Item.val.string, "string1")) {
7385 return 4300;
7386 }
7387
7388 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7389 return 4300+(int32_t)nCBORError;
7390 }
7391 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
7392 Item.uDataAlloc ||
7393 Item.uLabelAlloc ||
7394 UsefulBufCompareToSZ(Item.val.string, "string2")) {
7395 return 4400;
7396 }
7397
7398 QCBORDecode_Rewind(&DCtx);
7399
7400 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7401 return 4400+(int32_t)nCBORError;
7402 }
7403 if(Item.uDataType != QCBOR_TYPE_MAP ||
7404 Item.val.uCount != 3) {
7405 return 4500;
7406 }
7407
7408 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7409 return (int32_t)nCBORError;
7410 }
7411
7412 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
7413 Item.uDataType != QCBOR_TYPE_INT64 ||
7414 Item.val.int64 != 42 ||
7415 Item.uDataAlloc ||
7416 Item.uLabelAlloc ||
7417 UsefulBufCompareToSZ(Item.label.string, "first integer")) {
7418 return 4600;
7419 }
7420
7421 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7422 return (int32_t)nCBORError;
7423 }
7424 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
7425 Item.uDataAlloc ||
7426 Item.uLabelAlloc ||
7427 UsefulBufCompareToSZ(Item.label.string, "an array of two strings") ||
7428 Item.uDataType != QCBOR_TYPE_ARRAY ||
7429 Item.val.uCount != 2) {
7430 return 4700;
7431 }
7432
7433 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7434 return (int32_t)nCBORError;
7435 }
7436 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
7437 Item.uDataAlloc ||
7438 Item.uLabelAlloc ||
7439 UsefulBufCompareToSZ(Item.val.string, "string1")) {
7440 return 4800;
7441 }
7442
7443 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7444 return 4900+(int32_t)nCBORError;
7445 }
7446 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
7447 Item.uDataAlloc ||
7448 Item.uLabelAlloc ||
7449 UsefulBufCompareToSZ(Item.val.string, "string2")) {
7450 return 5000;
7451 }
7452
7453
7454 // Rewind an entered map
7455 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0);
7456
7457 QCBORDecode_EnterMap(&DCtx, NULL);
7458
7459 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7460 return 5100+(int32_t)nCBORError;
7461 }
7462
7463 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
7464 Item.uDataType != QCBOR_TYPE_INT64 ||
7465 Item.val.int64 != 42 ||
7466 Item.uDataAlloc ||
7467 Item.uLabelAlloc ||
7468 UsefulBufCompareToSZ(Item.label.string, "first integer")) {
7469 return 5200;
7470 }
7471
7472 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7473 return 5200+(int32_t)nCBORError;
7474 }
7475 if(Item.uLabelType != QCBOR_TYPE_TEXT_STRING ||
7476 Item.uDataAlloc ||
7477 Item.uLabelAlloc ||
7478 UsefulBufCompareToSZ(Item.label.string, "an array of two strings") ||
7479 Item.uDataType != QCBOR_TYPE_ARRAY ||
7480 Item.val.uCount != 2) {
7481 return -5300;
7482 }
7483
7484 QCBORDecode_Rewind(&DCtx);
7485
7486 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7487 return 5300+(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 5400;
7497 }
7498
7499 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7500 return 5400+(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 5500;
7509 }
7510
7511
7512 // Rewind and entered array inside an entered map
7513 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidMapEncoded), 0);
7514
7515 QCBORDecode_EnterMap(&DCtx, NULL);
7516
7517 QCBORDecode_EnterArrayFromMapSZ(&DCtx, "an array of two strings");
7518
7519 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7520 return 5600+(int32_t)nCBORError;
7521 }
7522 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
7523 Item.uDataAlloc ||
7524 Item.uLabelAlloc ||
7525 UsefulBufCompareToSZ(Item.val.string, "string1")) {
7526 return 5700;
7527 }
7528
7529 QCBORDecode_Rewind(&DCtx);
7530
7531 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7532 return 5700+(int32_t)nCBORError;
7533 }
7534 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
7535 Item.uDataAlloc ||
7536 Item.uLabelAlloc ||
7537 UsefulBufCompareToSZ(Item.val.string, "string1")) {
7538 return 5800;
7539 }
7540
7541 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7542 return (int32_t)nCBORError;
7543 }
7544 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
7545 Item.uDataAlloc ||
7546 Item.uLabelAlloc ||
7547 UsefulBufCompareToSZ(Item.val.string, "string2")) {
7548 return 5900;
7549 }
7550
7551 QCBORDecode_Rewind(&DCtx);
7552
7553 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7554 return 5900+(int32_t)nCBORError;
7555 }
7556 if(Item.uDataType != QCBOR_TYPE_TEXT_STRING ||
7557 Item.uDataAlloc ||
7558 Item.uLabelAlloc ||
7559 UsefulBufCompareToSZ(Item.val.string, "string1")) {
7560 return 6000;
7561 }
7562
7563
7564 // Rewind a byte string inside an array inside an array
7565 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidWrappedMapEncoded), 0);
7566
7567 QCBORDecode_EnterArray(&DCtx, NULL);
7568
7569 uint64_t i;
7570 QCBORDecode_GetUInt64(&DCtx, &i);
7571
7572 QCBORDecode_EnterArray(&DCtx, NULL);
7573
7574 QCBORDecode_EnterBstrWrapped(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
7575 if(QCBORDecode_GetError(&DCtx)) {
7576 return 6100;
7577 }
7578
7579 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7580 return (int32_t)nCBORError;
7581 }
7582 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
7583 return 6200;
7584 }
7585
7586 QCBORDecode_Rewind(&DCtx);
7587
7588 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7589 return 6300+(int32_t)nCBORError;
7590 }
7591 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
7592 return 6400;
7593 }
7594
7595#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
7596 // Rewind a byte string inside an indefinite-length array inside
7597 // indefinite-length array
7598
7599 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pValidIndefWrappedMapEncoded), 0);
7600
7601 QCBORDecode_EnterArray(&DCtx, NULL);
7602
7603 QCBORDecode_GetUInt64(&DCtx, &i);
7604
7605 QCBORDecode_EnterArray(&DCtx, NULL);
7606
7607 QCBORDecode_EnterBstrWrapped(&DCtx, QCBOR_TAG_REQUIREMENT_NOT_A_TAG, NULL);
7608 if(QCBORDecode_GetError(&DCtx)) {
7609 return 6500;
7610 }
7611
7612 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7613 return 6600+(int32_t)nCBORError;
7614 }
7615 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
7616 return 6700;
7617 }
7618
7619 QCBORDecode_Rewind(&DCtx);
7620
7621 if((nCBORError = QCBORDecode_GetNext(&DCtx, &Item))) {
7622 return 6800+(int32_t)nCBORError;
7623 }
7624 if(Item.uDataType != QCBOR_TYPE_MAP || Item.val.uCount != 3) {
7625 return 6900;
7626 }
7627#endif
7628
7629 // Rewind an empty map
7630 // [100, {}]
7631 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pWithEmptyMap), 0);
7632 QCBORDecode_EnterArray(&DCtx, NULL);
7633 QCBORDecode_GetUInt64(&DCtx, &i);
7634 if(i != 100) {
7635 return 7010;
7636 }
7637 QCBORDecode_EnterMap(&DCtx, NULL);
7638
7639 /* Do it 5 times to be sure multiple rewinds work */
7640 for(int n = 0; n < 5; n++) {
7641 nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
7642 if(nCBORError != QCBOR_ERR_NO_MORE_ITEMS) {
7643 return 7000 + n;
7644 }
7645 QCBORDecode_Rewind(&DCtx);
7646 }
7647 QCBORDecode_ExitMap(&DCtx);
7648 QCBORDecode_Rewind(&DCtx);
7649 QCBORDecode_GetUInt64(&DCtx, &i);
7650 if(i != 100) {
7651 return 7010;
7652 }
7653 QCBORDecode_ExitArray(&DCtx);
7654 QCBORDecode_Rewind(&DCtx);
7655 QCBORDecode_EnterArray(&DCtx, NULL);
7656 i = 9;
7657 QCBORDecode_GetUInt64(&DCtx, &i);
7658 if(i != 100) {
7659 return 7020;
7660 }
7661 if(QCBORDecode_GetError(&DCtx)){
7662 return 7030;
7663 }
7664
7665 // Rewind an empty indefinite length map
7666#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS
7667 QCBORDecode_Init(&DCtx, UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pWithEmptyMapInDef), 0);
7668 QCBORDecode_EnterArray(&DCtx, NULL);
7669 QCBORDecode_GetUInt64(&DCtx, &i);
7670 if(i != 100) {
7671 return 7810;
7672 }
7673 QCBORDecode_EnterMap(&DCtx, NULL);
7674
7675 /* Do it 5 times to be sure multiple rewinds work */
7676 for(int n = 0; n < 5; n++) {
7677 nCBORError = QCBORDecode_GetNext(&DCtx, &Item);
7678 if(nCBORError != QCBOR_ERR_NO_MORE_ITEMS) {
7679 return 7800 + n;
7680 }
7681 QCBORDecode_Rewind(&DCtx);
7682 }
7683 QCBORDecode_ExitMap(&DCtx);
7684 QCBORDecode_Rewind(&DCtx);
7685 QCBORDecode_GetUInt64(&DCtx, &i);
7686 if(i != 100) {
7687 return 7810;
7688 }
7689 QCBORDecode_ExitArray(&DCtx);
7690 QCBORDecode_Rewind(&DCtx);
7691 QCBORDecode_EnterArray(&DCtx, NULL);
7692 i = 9;
7693 QCBORDecode_GetUInt64(&DCtx, &i);
7694 if(i != 100) {
7695 return 7820;
7696 }
7697 if(QCBORDecode_GetError(&DCtx)){
7698 return 7830;
7699 }
7700#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_ARRAYS */
7701
7702 // Rewind an indefnite length byte-string wrapped sequence
7703#ifndef QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS
7704 QCBORDecode_Init(&DCtx,
7705 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(pWrappedByIndefiniteLength),
7706 0);
7707 UsefulBuf_MAKE_STACK_UB(Pool, 100);
7708 QCBORDecode_SetMemPool(&DCtx, Pool, 0);
7709
7710 QCBORDecode_EnterArray(&DCtx, NULL);
7711 QCBORDecode_EnterBstrWrapped(&DCtx, 2, NULL);
7712 if(QCBORDecode_GetError(&DCtx) != QCBOR_ERR_INPUT_TOO_LARGE) {
Laurence Lundblade732e52d2021-02-22 20:11:01 -07007713 /* this is what happens when trying to enter
7714 indefinite-length byte string
Laurence Lundbladecf41c522021-02-20 10:19:07 -07007715 wrapped CBOR. Tolerate for now. Eventually it needs
7716 to be fixed so this works, but that is not simple. */
7717 return 7300;
7718 }
7719
7720 /*
7721 QCBORDecode_GetUInt64(&DCtx, &i);
7722 if(i != 42) {
7723 return 7110;
7724 }
7725 QCBORDecode_Rewind(&DCtx);
7726 QCBORDecode_GetUInt64(&DCtx, &i);
7727 if(i != 42) {
7728 return 7220;
7729 }*/
7730#endif /* QCBOR_DISABLE_INDEFINITE_LENGTH_STRINGS */
7731
7732
7733 // Rewind an indefnite length byte-string wrapped sequence
7734
Laurence Lundblade2a26abb2020-11-05 19:06:54 -08007735 return 0;
7736}
Laurence Lundblade9f9c3732021-03-23 09:38:46 -07007737
7738
7739
7740
7741static const uint8_t spBooleansInMap[] =
7742{
7743 0xa1, 0x08, 0xf5
7744};
7745
7746static const uint8_t spBooleansInMapWrongType[] =
7747{
7748 0xa1, 0x08, 0xf6
7749};
7750
7751static const uint8_t spBooleansInMapNWF[] =
7752{
7753 0xa1, 0x08, 0x1a
7754};
7755
Laurence Lundblade8782dd32021-04-27 04:15:37 -07007756static const uint8_t spNullInMap[] =
7757{
7758 0xa1, 0x08, 0xf6
7759};
7760
7761static const uint8_t spUndefinedInMap[] =
7762{
7763 0xa1, 0x08, 0xf7
7764};
7765
Laurence Lundblade9f9c3732021-03-23 09:38:46 -07007766
7767int32_t BoolTest(void)
7768{
7769 QCBORDecodeContext DCtx;
7770 bool b;
7771
Laurence Lundblade8782dd32021-04-27 04:15:37 -07007772 QCBORDecode_Init(&DCtx,
7773 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMap),
7774 0);
Laurence Lundblade9f9c3732021-03-23 09:38:46 -07007775 QCBORDecode_EnterMap(&DCtx, NULL);
7776 QCBORDecode_GetBool(&DCtx, &b);
7777 if(QCBORDecode_GetAndResetError(&DCtx) || !b) {
7778 return 1;
7779 }
7780
7781 QCBORDecode_GetBoolInMapN(&DCtx, 7, &b);
7782 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_LABEL_NOT_FOUND) {
7783 return 2;
7784 }
7785
7786 QCBORDecode_GetBoolInMapN(&DCtx, 8, &b);
7787 if(QCBORDecode_GetAndResetError(&DCtx) || !b) {
7788 return 3;
7789 }
7790
7791
7792 QCBORDecode_GetBoolInMapSZ(&DCtx, "xx", &b);
7793 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_LABEL_NOT_FOUND) {
7794 return 4;
7795 }
7796
Laurence Lundblade8782dd32021-04-27 04:15:37 -07007797 QCBORDecode_Init(&DCtx,
7798 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMapWrongType),
7799 0);
Laurence Lundblade9f9c3732021-03-23 09:38:46 -07007800 QCBORDecode_EnterMap(&DCtx, NULL);
7801 QCBORDecode_GetBool(&DCtx, &b);
7802 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
7803 return 5;
7804 }
7805
Laurence Lundblade8782dd32021-04-27 04:15:37 -07007806 QCBORDecode_Init(&DCtx,
7807 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMapNWF),
7808 0);
Laurence Lundblade9f9c3732021-03-23 09:38:46 -07007809 QCBORDecode_EnterMap(&DCtx, NULL);
7810 QCBORDecode_GetBool(&DCtx, &b);
7811 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_HIT_END) {
7812 return 6;
7813 }
7814
Laurence Lundblade8782dd32021-04-27 04:15:37 -07007815
7816 QCBORDecode_Init(&DCtx,
7817 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spNullInMap),
7818 0);
7819 QCBORDecode_EnterMap(&DCtx, NULL);
7820 QCBORDecode_GetNull(&DCtx);
7821 if(QCBORDecode_GetAndResetError(&DCtx)) {
7822 return 7;
7823 }
7824
7825 QCBORDecode_Init(&DCtx,
7826 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMap),
7827 0);
7828 QCBORDecode_EnterMap(&DCtx, NULL);
7829 QCBORDecode_GetNull(&DCtx);
7830 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
7831 return 8;
7832 }
7833
7834 QCBORDecode_Init(&DCtx,
7835 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spNullInMap),
7836 0);
7837 QCBORDecode_EnterMap(&DCtx, NULL);
7838 QCBORDecode_GetNullInMapN(&DCtx, 8);
7839 if(QCBORDecode_GetAndResetError(&DCtx)) {
7840 return 9;
7841 }
7842
7843 QCBORDecode_Init(&DCtx,
7844 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMap),
7845 0);
7846 QCBORDecode_EnterMap(&DCtx, NULL);
7847 QCBORDecode_GetNullInMapN(&DCtx, 8);
7848 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
7849 return 10;
7850 }
7851
7852 QCBORDecode_Init(&DCtx,
7853 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMapNWF),
7854 0);
7855 QCBORDecode_EnterMap(&DCtx, NULL);
7856 QCBORDecode_GetUndefined(&DCtx);
7857 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_HIT_END) {
7858 return 11;
7859 }
7860
7861 QCBORDecode_Init(&DCtx,
7862 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUndefinedInMap),
7863 0);
7864 QCBORDecode_EnterMap(&DCtx, NULL);
7865 QCBORDecode_GetUndefined(&DCtx);
7866 if(QCBORDecode_GetAndResetError(&DCtx)) {
7867 return 12;
7868 }
7869
7870 QCBORDecode_Init(&DCtx,
7871 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMap),
7872 0);
7873 QCBORDecode_EnterMap(&DCtx, NULL);
7874 QCBORDecode_GetUndefined(&DCtx);
7875 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
7876 return 13;
7877 }
7878
7879 QCBORDecode_Init(&DCtx,
7880 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spUndefinedInMap),
7881 0);
7882 QCBORDecode_EnterMap(&DCtx, NULL);
7883 QCBORDecode_GetUndefinedInMapN(&DCtx, 8);
7884 if(QCBORDecode_GetAndResetError(&DCtx)) {
7885 return 14;
7886 }
7887
7888 QCBORDecode_Init(&DCtx,
7889 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMap),
7890 0);
7891 QCBORDecode_EnterMap(&DCtx, NULL);
7892 QCBORDecode_GetUndefinedInMapN(&DCtx, 8);
7893 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_UNEXPECTED_TYPE) {
7894 return 15;
7895 }
7896
7897 QCBORDecode_Init(&DCtx,
7898 UsefulBuf_FROM_BYTE_ARRAY_LITERAL(spBooleansInMapNWF),
7899 0);
7900 QCBORDecode_EnterMap(&DCtx, NULL);
7901 QCBORDecode_GetUndefined(&DCtx);
7902 if(QCBORDecode_GetAndResetError(&DCtx) != QCBOR_ERR_HIT_END) {
7903 return 15;
7904 }
7905
Laurence Lundblade9f9c3732021-03-23 09:38:46 -07007906 return 0;
7907}